fix: finish cancelled diagnostic startup after target loading

This commit is contained in:
阿白叔 committed 2026-11-17 07:33:52 +08:00
1 parent d5d1a3deea
commit 845ae58690
3 files changed
+267 -3

No files matched your search

+52
View File
@@ -365,6 +365,58 @@ def _pending_prompt(windows, *, closing=False, frame=None, session=None, clicked
return False
def cancelled_startup_step(job, pcb_pid, state, *, closed_frame, deadline, post, emit):
"""Finish only the owned no-write diagnostic startup; never establish PASS.
Cancellation already ended supervisor polling. Cleanup is its sole prompt
actor and uses the caller's original deadline and single-close state.
"""
from types import SimpleNamespace
windows = _windows(pcb_pid)
if any(window['pid'] != pcb_pid for window in windows):
raise RuntimeError('STARTUP_PROCESS_UNCONFIRMED')
current = _target_frame(windows, Path(job['pcb']).stem, require_ready=False)
frame = current
if frame is None and closed_frame is not None:
frame = state.get('frame')
if frame is None:
return closed_frame, False
old = state.get('frame')
if old is not None and any(frame[k] != old[k] for k in ('hwnd', 'pid', 'class', 'text')):
raise RuntimeError('STARTUP_DOCUMENT_CHANGED')
if closed_frame is not None and frame['hwnd'] != closed_frame:
raise RuntimeError('STARTUP_DOCUMENT_CHANGED')
if old is None:
if _load_evidence(job, pcb_pid, job['diagnostic_log_baseline'], require_autosave=False) is None:
return closed_frame, False
state['frame'] = frame
# Validate every window before allowing a known No. An unknown sibling
# must not be hidden by the first recognized modal in enumeration order.
try:
for window in windows:
_pending_prompt([window], closing=closed_frame is not None, frame=frame,
pcb_name=Path(job['pcb']).name)
except RuntimeError as error:
if str(error).startswith('DIAGNOSTIC_CLOSE_UNCONFIRMED: unknown native prompt'):
return closed_frame, False
raise
actions = []
def act(hwnd, message):
if _windows(pcb_pid) != windows or time.monotonic() >= deadline:
raise RuntimeError('STARTUP_ACTION_UNCONFIRMED: native state or deadline changed')
post(hwnd, message)
actions.append((hwnd, message))
if message == 0xF5:
emit(stage='startup_prompt_no', pcb_pid=pcb_pid, hwnd=hwnd, control_id=7)
pending = _pending_prompt(windows, closing=closed_frame is not None, frame=frame,
session=SimpleNamespace(post=act), clicked=state['clicked'], pcb_name=Path(job['pcb']).name)
if (closed_frame is None and not pending
and _target_frame(windows, Path(job['pcb']).stem) is not None):
act(frame['hwnd'], 0x10)
closed_frame = frame['hwnd']
return closed_frame, bool(actions)
def check_target_window(job, pcb_pid, *, emit, error_reader, launcher_process=None) -> dict:
"""Prove this launch opened its native document, close it, and prove exit.
+37 -3
View File
@@ -63,7 +63,7 @@ def _tip_target(snapshot):
def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event: None,
timeout_seconds=20, launcher_process=None):
timeout_seconds=None, launcher_process=None):
"""Normally close our empty PCB after a diagnosed database startup failure.
No COM attach: the global ROT can name another session. Only the exact
@@ -90,6 +90,15 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
if not pcb_pid or pcb_pid in baseline_pids:
raise ValueError('STARTUP_PROCESS_UNCONFIRMED')
expected = os.path.normcase(os.path.abspath(job['pcb']))
cancelled_diagnostic = (job.get('diagnostic_only') is True
and bool(job.get('cancel_signal')) and Path(job['cancel_signal']).exists()
and isinstance(job.get('diagnostic_log_baseline'), dict)
and {'started_ns', 'files'} <= job['diagnostic_log_baseline'].keys()
and not (root/'save.log').exists())
# Diagnostic close already owns a 30-second cleanup budget. Start it
# once here; the empty-to-loaded transition never resets that deadline.
if timeout_seconds is None:
timeout_seconds = 30 if cancelled_diagnostic else 20
deadline = time.monotonic() + max(0, timeout_seconds)
sent = False
declined_hwnd = None
@@ -99,6 +108,14 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
close_handle = None
close_identity = None
close_frame = None
diagnostic_state = {'clicked': set()}
def cleanup_post(hwnd, message):
if (time.monotonic() >= deadline or _current_desktop_name() != desktop
or win32event.WaitForSingleObject(close_handle, 0) != win32con.WAIT_TIMEOUT
or residual_sessions._handle_identity(close_handle) != close_identity
or win32process.GetWindowThreadProcessId(hwnd)[1] != pcb_pid):
raise ValueError('STARTUP_ACTION_UNCONFIRMED')
win32gui.PostMessage(hwnd, message, 0, 0)
while True:
records = [r for r in _process_details('ExpeditionPCB.exe') if r[0] == pcb_pid]
if not records:
@@ -133,6 +150,22 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
or '-reusablecircuit' not in [a.casefold() for a in args]
or expected not in [os.path.normcase(os.path.abspath(a)) for a in args[1:]]):
raise ValueError('STARTUP_PROCESS_UNCONFIRMED')
if cancelled_diagnostic and time.monotonic() < deadline:
if close_handle is None:
close_handle = handles.enter_context(residual_sessions._process_handle(pcb_pid))
close_identity = residual_sessions._handle_identity(close_handle)
if Path(close_identity['image']).name.casefold() != 'expeditionpcb.exe':
raise ValueError('STARTUP_PROCESS_UNCONFIRMED')
from .diagnostic_open import cancelled_startup_step
closed, acted = cancelled_startup_step(job, pcb_pid, diagnostic_state,
closed_frame=close_frame, deadline=deadline, post=cleanup_post, emit=emit)
if closed is not None and not sent:
sent, close_frame = True, closed
emit(stage='startup_close_requested', pcb_pid=pcb_pid, hwnd=closed,
document_identity='native_path_pid_logs', cleanup='cancelled_diagnostic')
if acted:
continue
if not sent:
windows = []
def observe(hwnd, _):
if (win32process.GetWindowThreadProcessId(hwnd)[1] == pcb_pid
@@ -213,8 +246,9 @@ def close_failed_startup(job, pcb_pid, *, baseline_pids=(), emit=lambda **event:
win32gui.EnumChildWindows(main[0], child, None)
addresses = unreleased_addresses(job['lmc_file'], job['project'], job['pcb'])
if len(main) == 1 and not unknown and not documents and (cancelled_open or not addresses):
close_handle = handles.enter_context(residual_sessions._process_handle(pcb_pid))
close_identity = residual_sessions._handle_identity(close_handle)
if close_handle is None:
close_handle = handles.enter_context(residual_sessions._process_handle(pcb_pid))
close_identity = residual_sessions._handle_identity(close_handle)
if Path(close_identity['image']).name.casefold() != 'expeditionpcb.exe':
raise ValueError('STARTUP_PROCESS_UNCONFIRMED')
win32gui.PostMessage(main[0], win32con.WM_CLOSE, 0, 0)