feat: integrate native diagnostics and coordinated CBB transactions
This commit is contained in:
1 parent
9e230e1a46
commit
da67de026f
91 files changed
+14370
-435
No files matched your search
@@ -236,6 +236,22 @@ class DialogActionExecutor:
|
||||
def perform(self, snapshot: WindowSnapshot, action: dict) -> ActionResult:
|
||||
channels = tuple(action.get("channels", ()))
|
||||
action_name = action.get("action")
|
||||
if action_name == 'user_selected_pcb_version':
|
||||
try:
|
||||
from .pcb_restore_prompt import validate_action_window
|
||||
if channels != ('win32_button_click',):
|
||||
raise ValueError('PCB_VERSION_ACTION_CHANNEL_MISMATCH')
|
||||
validate_action_window(snapshot, action)
|
||||
except Exception as error:
|
||||
return ActionResult(error_code='PCB_VERSION_CHOICE_REQUIRED', detail=str(error))
|
||||
if action_name == 'post_repair_local_read_write':
|
||||
try:
|
||||
from .post_repair_consent import validate_action_window
|
||||
validate_action_window(snapshot, action)
|
||||
if channels != ('win32_button_click',) or action.get('target') != {'control_id': 6}:
|
||||
raise ValueError('POST_REPAIR_ACTION_MISMATCH')
|
||||
except Exception as error:
|
||||
return ActionResult(error_code='DATABASE_RECOVERY_REQUIRED', detail=str(error))
|
||||
if action_name == "accept_license":
|
||||
target = self._license_target(snapshot)
|
||||
elif action_name == "restore_last_saved":
|
||||
@@ -308,6 +324,7 @@ class DialogAgentServer:
|
||||
self.channel = JsonChannel(connection)
|
||||
self.channel.send({"token": paths.token}, timeout_seconds=10)
|
||||
self.observed_snapshots = {}
|
||||
self.observed_pids = None
|
||||
|
||||
@classmethod
|
||||
def from_config(cls, config_path: Path, *, probe, executor=None):
|
||||
@@ -332,7 +349,15 @@ class DialogAgentServer:
|
||||
try:
|
||||
phase = DialogPhase(request["phase"])
|
||||
if request.get("op") == "observe":
|
||||
snapshots = self.probe.capture(phase)
|
||||
pids = request.get('pids')
|
||||
if pids is not None and (not isinstance(pids, list)
|
||||
or any(type(pid) is not int or pid <= 0 for pid in pids)):
|
||||
raise ValueError('DIALOG_PID_SCOPE_INVALID')
|
||||
self.observed_pids = set(pids) if pids is not None else None
|
||||
snapshots = (self.probe.capture(phase, pids=self.observed_pids) if pids is not None
|
||||
else self.probe.capture(phase))
|
||||
if pids is not None:
|
||||
snapshots = tuple(w for w in snapshots if w.pid in self.observed_pids)
|
||||
self.observed_snapshots.clear()
|
||||
for snapshot in snapshots:
|
||||
self.observed_snapshots[stable_fingerprint(snapshot)] = snapshot
|
||||
@@ -340,7 +365,9 @@ class DialogAgentServer:
|
||||
return
|
||||
if request.get("op") == "perform":
|
||||
expected = self.observed_snapshots.get(request.get("fingerprint"))
|
||||
if expected is None:
|
||||
if (expected is None or int(request.get('hwnd', 0)) != expected.hwnd
|
||||
or self.observed_pids is not None and (expected.pid not in self.observed_pids
|
||||
or not self.probe.matches_identity(expected))):
|
||||
self._response(request, result=ActionResult(
|
||||
error_code="STALE_DIALOG",
|
||||
detail="窗口已消失或指纹已变化",
|
||||
@@ -406,27 +433,45 @@ class DialogAgentClient:
|
||||
raise
|
||||
|
||||
@classmethod
|
||||
def start(cls, worker, root: Path, *, timeout_seconds: float = 2.0):
|
||||
def start(cls, worker, root: Path, *, timeout_seconds: float = 2.0, stop=None):
|
||||
client = cls.prepare(root, desktop_name=worker.name, timeout_seconds=timeout_seconds)
|
||||
try:
|
||||
from .defaults import DIALOG_AGENT_STARTUP_TIMEOUT_SECONDS
|
||||
worker.start_companion(dialog_agent_command(client.paths.config))
|
||||
client._accept(timeout_seconds=10.0)
|
||||
# Frozen imports can exceed ten seconds; PCB remains behind its gate.
|
||||
client._accept(timeout_seconds=DIALOG_AGENT_STARTUP_TIMEOUT_SECONDS, stop=stop)
|
||||
return client
|
||||
except Exception:
|
||||
client.close()
|
||||
raise
|
||||
|
||||
def _accept(self, timeout_seconds):
|
||||
def _accept(self, timeout_seconds, *, stop=None):
|
||||
if self.channel is not None:
|
||||
return
|
||||
self.listener.settimeout(timeout_seconds)
|
||||
connection, _address = self.listener.accept()
|
||||
deadline = time.monotonic() + timeout_seconds
|
||||
while True:
|
||||
if stop is not None and stop.is_set():
|
||||
raise InterruptedError('VERIFY_CANCELLED: 弹窗代理启动已取消,未启动 PCB')
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
raise TimeoutError('dialog-agent startup timed out')
|
||||
self.listener.settimeout(min(.2, remaining))
|
||||
try:
|
||||
connection, _address = self.listener.accept()
|
||||
break
|
||||
except socket.timeout:
|
||||
continue
|
||||
channel = JsonChannel(connection)
|
||||
try:
|
||||
connection.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
||||
hello = channel.receive(timeout_seconds)
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
raise TimeoutError('dialog-agent handshake timed out')
|
||||
hello = channel.receive(min(self.timeout_seconds, remaining))
|
||||
if hello.get("token") != self.paths.token:
|
||||
raise DialogAgentProtocolError("dialog-agent authentication failed")
|
||||
if stop is not None and stop.is_set():
|
||||
raise InterruptedError('VERIFY_CANCELLED: 弹窗代理启动已取消,未启动 PCB')
|
||||
self.channel = channel
|
||||
except Exception:
|
||||
channel.close()
|
||||
@@ -457,8 +502,8 @@ class DialogAgentClient:
|
||||
raise DialogAgentProtocolError(f"{response['error_code']}: {response.get('detail', '')}")
|
||||
return response.get("result")
|
||||
|
||||
def observe(self, phase: DialogPhase) -> tuple[WindowSnapshot, ...]:
|
||||
result = self._request("observe", phase)
|
||||
def observe(self, phase: DialogPhase, *, pids=None) -> tuple[WindowSnapshot, ...]:
|
||||
result = self._request("observe", phase, **({'pids': sorted(pids)} if pids is not None else {}))
|
||||
return tuple(WindowSnapshot.from_record(item) for item in (result or ()))
|
||||
|
||||
def perform(self, *, hwnd: int, fingerprint: str, action: dict,
|
||||
|
||||
Reference in new issue
Block a user