fix: retry incomplete native window observations within deadlines

This commit is contained in:
阿白叔 committed 2026-11-17 00:57:45 +08:00
1 parent 07206ce0d1
commit 36ff964230
2 files changed
+98 -3

No files matched your search

+17 -3
View File
@@ -301,6 +301,20 @@ def check_target_window(job, pcb_pid, *, emit, error_reader, launcher_process=No
if not isinstance(baseline, dict) or 'started_ns' not in baseline or 'files' not in baseline:
raise RuntimeError('DIAGNOSTIC_BASELINE_REQUIRED: snapshot before native launch')
session = _NativeSession(job, int(pcb_pid), launcher_process)
def scan(deadline, *, closing=False):
while True:
if not closing:
_cancel(job)
try:
return _windows(int(pcb_pid))
except RuntimeError as error:
if str(error) != 'DIAGNOSTIC_WINDOW_LIMIT: window scan exceeded 0.2s':
raise
# Discard the incomplete snapshot; no control action may use it.
emit(stage='diagnostic_window_retry', detail=str(error))
if time.monotonic() >= deadline:
raise
time.sleep(min(.1, max(0, deadline-time.monotonic())))
try:
deadline = time.monotonic() + max(.01, float(job['timeout']))
while True:
@@ -310,7 +324,7 @@ def check_target_window(job, pcb_pid, *, emit, error_reader, launcher_process=No
raise RuntimeError(str(error))
if not session.pcb_alive():
raise RuntimeError('DIAGNOSTIC_OPEN_UNCONFIRMED: target PCB exited before loaded evidence')
windows = _windows(int(pcb_pid))
windows = scan(deadline)
if any(window['pid'] != int(pcb_pid) for window in windows):
raise RuntimeError('DIAGNOSTIC_PID_MISMATCH')
frame = _target_frame(windows, Path(job['pcb']).stem)
@@ -328,7 +342,7 @@ def check_target_window(job, pcb_pid, *, emit, error_reader, launcher_process=No
time.sleep(.1)
# Re-read the exact PID/frame before the sole close request.
_cancel(job)
windows = _windows(int(pcb_pid))
windows = scan(deadline)
_pending_prompt(windows, frame=frame, pcb_name=Path(job['pcb']).name)
current = _target_frame(windows, Path(job['pcb']).stem)
if current is None or current['hwnd'] != frame['hwnd']:
@@ -341,7 +355,7 @@ def check_target_window(job, pcb_pid, *, emit, error_reader, launcher_process=No
while session.pcb_alive() or session.launcher_alive():
# Cancellation stops future work; an accepted normal close must
# still finish before its queue slot can be safely released.
windows = _windows(int(pcb_pid)) if session.pcb_alive() else []
windows = scan(close_deadline, closing=True) if session.pcb_alive() else []
previous_clicks = set(clicked)
pending_error = None
try:
+81
View File
@@ -127,6 +127,87 @@ def test_success_posts_one_exact_close_and_reports_only_open_check(diagnostic, j
assert Session.instances[-1].closed
@pytest.mark.parametrize('incomplete_scan', [1, 2, 3])
def test_transient_incomplete_scan_is_retried_without_acting_on_partial_windows(diagnostic, job, monkeypatch, incomplete_scan):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
job['timeout'] = 1
write_logs(job)
class ClosingSession(Session):
closing = False
def post(self, hwnd, message):
self.posted.append((hwnd, message))
self.closing = True
monkeypatch.setattr(diagnostic, '_NativeSession', ClosingSession)
scans = []
def windows(pid):
scans.append(True)
if len(scans) == incomplete_scan:
raise RuntimeError('DIAGNOSTIC_WINDOW_LIMIT: window scan exceeded 0.2s')
if Session.instances[-1].closing:
Session.instances[-1].alive = False
return []
return [frame(job)]
monkeypatch.setattr(diagnostic, '_windows', windows)
events = []
result = diagnostic.check_target_window(job, 42, emit=lambda **event:events.append(event), error_reader=lambda:None)
assert result['status'] == 'PASS'
assert Session.instances[-1].posted == [(10, 0x10)]
assert any(e['stage'] == 'diagnostic_window_retry' for e in events)
def test_incomplete_scans_keep_original_open_deadline_and_never_close(diagnostic, job, monkeypatch):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
calls = []
def windows(pid):
calls.append(True)
raise RuntimeError('DIAGNOSTIC_WINDOW_LIMIT: window scan exceeded 0.2s')
monkeypatch.setattr(diagnostic, '_windows', windows)
with pytest.raises(RuntimeError, match='DIAGNOSTIC_WINDOW_LIMIT'):
diagnostic.check_target_window(job, 42, emit=lambda **event:None, error_reader=lambda:None)
assert len(calls) <= 3
assert Session.instances[-1].posted == []
def test_cancel_during_incomplete_scan_stops_retry_without_close(diagnostic, job, monkeypatch):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
job['timeout'] = 10
cancel = Path(job['root']).parent/'cancel-scan'
job['cancel_signal'] = str(cancel)
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
calls = []
def windows(pid):
calls.append(True)
cancel.touch()
raise RuntimeError('DIAGNOSTIC_WINDOW_LIMIT: window scan exceeded 0.2s')
monkeypatch.setattr(diagnostic, '_windows', windows)
with pytest.raises(RuntimeError, match='VERIFY_CANCELLED'):
diagnostic.check_target_window(job, 42, emit=lambda **event:None, error_reader=lambda:None)
assert len(calls) == 1 and Session.instances[-1].posted == []
def test_incomplete_close_scan_cannot_extend_thirty_second_deadline(diagnostic, job, monkeypatch):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)
clock, offset = diagnostic.time.monotonic, [0]
monkeypatch.setattr(diagnostic.time, 'monotonic', lambda:clock()+offset[0])
class ClosingSession(Session):
closing = False
def post(self, hwnd, message):
self.posted.append((hwnd, message))
self.closing = True
monkeypatch.setattr(diagnostic, '_NativeSession', ClosingSession)
def windows(pid):
if Session.instances[-1].closing:
offset[0] = 31
raise RuntimeError('DIAGNOSTIC_WINDOW_LIMIT: window scan exceeded 0.2s')
return [frame(job)]
monkeypatch.setattr(diagnostic, '_windows', windows)
with pytest.raises(RuntimeError, match='DIAGNOSTIC_WINDOW_LIMIT'):
diagnostic.check_target_window(job, 42, emit=lambda **event:None, error_reader=lambda:None)
assert Session.instances[-1].posted == [(10, 0x10)]
def test_transient_startup_window_cannot_abort_before_target_is_loaded(diagnostic, job, monkeypatch):
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)