fix: acknowledge diagnostic prompt ownership before native close

This commit is contained in:
阿白叔 committed 2026-11-17 06:02:23 +08:00
1 parent de81bbf293
commit 4402486baa
5 files changed
+232 -16

No files matched your search

+42 -10
View File
@@ -9,6 +9,7 @@ import ctypes
from ctypes import wintypes
import heapq
import hashlib
import json
import ntpath
import os
from pathlib import Path
@@ -283,6 +284,32 @@ def _cancel(job):
raise RuntimeError('VERIFY_CANCELLED: native opening check cancelled')
def _request_close_handoff(job, pcb_pid, frame, session, emit, deadline):
from uuid import uuid4
intent = dict(pcb_pid=int(pcb_pid), hwnd=frame['hwnd'], operation_id=job['operation_id'],
desktop=job['desktop'], project=job['project'], pcb=job['pcb'], close_id=uuid4().hex)
ack = Path(job['root'])/'diagnostic-close-ack.json'
emit(stage='diagnostic_close_requested', **intent)
def read_ack():
try:
with ack.open(encoding='utf-8') as stream:
return json.loads(stream.read(8192))
except (OSError, ValueError):
return None
while True:
_cancel(job)
remaining = deadline-time.monotonic()
if remaining <= 0 or not session.pcb_alive():
raise RuntimeError('DIAGNOSTIC_CLOSE_UNCONFIRMED: supervisor handoff not acknowledged')
try:
acknowledged = _bounded_call(read_ack, min(2, remaining))
except TimeoutError as error:
raise RuntimeError('DIAGNOSTIC_CLOSE_UNCONFIRMED: supervisor acknowledgement read timed out') from error
if acknowledged == intent:
return intent
time.sleep(min(.1, max(0, deadline-time.monotonic())))
def _owned_by_frame(window, frame):
if frame is None or window['pid'] != frame['pid']: return False
if window.get('owner') == frame['hwnd']: return True
@@ -388,17 +415,22 @@ def check_target_window(job, pcb_pid, *, emit, error_reader, launcher_process=No
if time.monotonic() >= deadline:
raise RuntimeError('DIAGNOSTIC_OPEN_UNCONFIRMED: current target MDI/path/PID logs missing')
time.sleep(.1)
# Re-read the exact PID/frame before the sole close request.
_cancel(job)
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']:
raise RuntimeError('DIAGNOSTIC_CLOSE_UNCONFIRMED: target frame changed')
session.post(frame['hwnd'], 0x10)
emit(stage='diagnostic_wm_close', pcb_pid=int(pcb_pid), hwnd=frame['hwnd'],
operation_id=job.get('operation_id'), desktop=job.get('desktop'), project=job['project'], pcb=job['pcb'])
# Finish any in-flight supervisor poll before WM_CLOSE can create a prompt.
close_deadline = time.monotonic() + 30
intent = _request_close_handoff(job, pcb_pid, frame, session, emit, close_deadline)
# Re-read the exact PID/frame after acknowledgement and before the sole close.
_cancel(job)
error = _read_bounded(error_reader) if error_reader else None
if error: raise RuntimeError(str(error))
windows = scan(close_deadline)
pending = _pending_prompt(windows, frame=frame, pcb_name=Path(job['pcb']).name)
current = _target_frame(windows, Path(job['pcb']).stem)
if (pending or time.monotonic() >= close_deadline or current is None
or any(current[key] != frame[key] for key in ('hwnd', 'pid', 'class', 'text'))):
raise RuntimeError('DIAGNOSTIC_CLOSE_UNCONFIRMED: target frame changed')
_cancel(job)
session.post(frame['hwnd'], 0x10)
emit(stage='diagnostic_wm_close', **intent)
clicked = set()
last_pending = ''
while session.pcb_alive() or session.launcher_alive():
+38 -2
View File
@@ -413,11 +413,39 @@ def phase_for_event(event, current=None):
def _diagnostic_close_event(job, event, target_pid):
return (job.get('diagnostic_only') is True and event.get('stage') == 'diagnostic_wm_close'
close_id = event.get('close_id')
return (job.get('diagnostic_only') is True and event.get('stage') == 'diagnostic_close_requested'
and target_pid is not None and event.get('pcb_pid') == target_pid
and type(event.get('hwnd')) is int and event['hwnd'] > 0
and isinstance(close_id, str) and len(close_id) == 32 and all(c in '0123456789abcdef' for c in close_id)
and bool(job.get('operation_id')) and event.get('operation_id') == job['operation_id']
and all(event.get(key) == job.get(key) for key in ('project', 'pcb', 'desktop')))
and all(job.get(key) and event.get(key) == job[key] for key in ('project', 'pcb', 'desktop')))
def _acknowledge_diagnostic_close(job, event, stop):
from .operation_queue import check_active
check_active()
if stop is not None and stop.is_set() or Path(job['cancel_signal']).exists():
raise InterruptedError('VERIFY_CANCELLED: diagnostic handoff cancelled')
ack = Path(job['root'])/'diagnostic-close-ack.json'
pending = ack.with_suffix('.pending')
pending.write_text(json.dumps({key: event[key] for key in
('pcb_pid', 'hwnd', 'operation_id', 'desktop', 'project', 'pcb', 'close_id')}), encoding='utf-8')
check_active()
if stop is not None and stop.is_set() or Path(job['cancel_signal']).exists():
raise InterruptedError('VERIFY_CANCELLED: diagnostic handoff cancelled')
pending.replace(ack)
def _close_intent_after_poll(job, events, offset, target_pid):
if not job.get('diagnostic_only') or not events.exists(): return None
with events.open(encoding='utf-8') as stream:
stream.seek(offset)
for line in stream:
try: event = json.loads(line)
except json.JSONDecodeError: break
if _diagnostic_close_event(job, event, target_pid): return event
return None
def classify_verify_dispatch(*, verify_invoked, invoke_returned, target_pid, observation):
@@ -1055,6 +1083,7 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
break
timing.record(event)
if _diagnostic_close_event(job, event, target_pid):
_acknowledge_diagnostic_close(job, event, stop)
diagnostic_closing = True
if event.get('pcb_pid'): target_pid = event['pcb_pid']
if event.get('launcher_pid'): launcher_pid = event['launcher_pid']
@@ -1116,6 +1145,13 @@ def _run_verify(request, *, timing, progress=None, flag_reader=None, process_run
dialog_result = dialog_supervisor.poll()
else:
dialog_result = dialog_supervisor.poll()
if not diagnostic_closing:
intent = _close_intent_after_poll(job, events, offset, target_pid)
if intent is not None:
_acknowledge_diagnostic_close(job, intent, stop)
diagnostic_closing = True
from .dialog_supervisor import DialogPollResult
dialog_result = DialogPollResult(activity=False)
if (version_prompt is not None and consent is None and dialog_agent is not None
and not request.diagnostic_only and not request.native_lm_verify
and dialog_result.error_code == 'DATABASE_RECOVERY_REQUIRED'
+1 -1
View File
@@ -21,7 +21,7 @@ def test_only_current_diagnostic_close_leaves_prompt_actions_to_checker(tmp_path
def __init__(self, command, name):
self.name = name
path = Path(command[-1]); job = json.loads(path.read_text())
event = dict(stage='diagnostic_wm_close', pcb_pid=901, hwnd=900,
event = dict(stage='diagnostic_close_requested', close_id='b'*32, pcb_pid=901, hwnd=900,
operation_id=job['operation_id'], desktop=job['desktop'], project=job['project'], pcb=job['pcb'])
for condition, key, value in [('wrong_pid','pcb_pid',999),('wrong_operation','operation_id','foreign'),
('wrong_project','project','foreign.prj'),('wrong_desktop','desktop','Default')]:
+136
View File
@@ -0,0 +1,136 @@
import json
from pathlib import Path
import time
from types import SimpleNamespace
import pytest
from test_verify_runner import fixture, request, PORTS_OK
from test_diagnostic_open import job, write_logs, frame, Session
@pytest.mark.parametrize('fault', ['', 'foreign_operation', 'ordinary', 'missing_nonce'])
def test_close_handoff_arriving_during_poll_prevents_competing_abort(tmp_path, monkeypatch, fault):
from cbb_refresh_verify import desktop_worker
from cbb_refresh_verify.verify_runner import run_verify
from cbb_refresh_verify.dialog_supervisor import DialogPollResult
fixture(tmp_path); state = {}
class Worker:
desktop = None; pid = 700; companion_pids = []; returncode = None; count = 0
def __init__(self, command, name):
self.name = name
path = Path(command[-1]); state.update(path=path, job=json.loads(path.read_text()))
path.with_name('events.jsonl').write_text(json.dumps(dict(stage='pcb_worker_started', pcb_pid=901))+'\n')
path.with_name('outcome.json').write_text(json.dumps(dict(status='FAIL', safe_to_continue=False,
stderr='DIAGNOSTIC_CLOSE_UNCONFIRMED: checker retained unknown prompt')))
def poll(self):
self.count += 1
if self.count > 2: self.returncode = 1
return self.returncode
def close(self): pass
def wait(self, timeout=5): self.returncode = 1; return 1
class Supervisor:
def poll(self, *args):
j = state['job']
event = dict(stage='diagnostic_close_requested', close_id='a'*32, pcb_pid=901, hwnd=900,
operation_id=j['operation_id'], desktop=j['desktop'], project=j['project'], pcb=j['pcb'])
if fault == 'foreign_operation': event['operation_id'] = 'foreign'
if fault == 'missing_nonce': event.pop('close_id')
with state['path'].with_name('events.jsonl').open('a') as stream:
stream.write(json.dumps(event)+'\n')
return DialogPollResult(error_code='UNKNOWN_DIALOG', detail='in-flight competing save prompt')
monkeypatch.setattr(desktop_worker, 'DesktopWorker', Worker)
result = run_verify(request(tmp_path, diagnostic_only=fault!='ordinary'), port_check=PORTS_OK,
dialog_supervisor_factory=lambda **kw: Supervisor())
assert result.status == 'FAIL'
ack = state['path'].with_name('diagnostic-close-ack.json')
if fault:
assert 'UNKNOWN_DIALOG' in result.stderr and not ack.exists()
else:
assert 'DIAGNOSTIC_CLOSE_UNCONFIRMED' in result.stderr
assert 'UNKNOWN_DIALOG' not in result.stderr
assert json.loads(ack.read_text())['close_id'] == 'a'*32
def test_checker_never_posts_close_before_bound_supervisor_ack(job, monkeypatch):
from cbb_refresh_verify import diagnostic_open as diagnostic
job['operation_id'] = 'verify-protocol'
Path(job['root']).mkdir(exist_ok=True)
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job); events = []
ack = Path(job['root'])/'diagnostic-close-ack.json'
class AcknowledgedSession(Session):
def post(self, hwnd, message):
assert ack.exists(), 'WM_CLOSE raced ahead of supervisor ownership acknowledgement'
assert events[-1]['stage'] == 'diagnostic_close_requested'
super().post(hwnd, message)
def emit(**event):
events.append(event)
if event['stage'] == 'diagnostic_close_requested':
ack.write_text(json.dumps({k:v for k,v in event.items() if k != 'stage'}))
monkeypatch.setattr(diagnostic, '_NativeSession', AcknowledgedSession)
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job)] if Session.instances[-1].alive else [])
assert diagnostic.check_target_window(job, 42, emit=emit, error_reader=lambda: None)['status'] == 'PASS'
assert Session.instances[-1].posted == [(10, 0x10)]
@pytest.mark.parametrize('fault', ['missing', 'stale_nonce', 'foreign_pid', 'foreign_project',
'foreign_desktop', 'cancel_wait', 'cancel_ack', 'late_ack'])
def test_missing_stale_cancelled_or_late_ack_preserves_native(job, monkeypatch, fault):
from cbb_refresh_verify import diagnostic_open as diagnostic
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job)
offset = [0]; clock = time.monotonic
cancel = Path(job['root'])/'cancel.signal'; job['cancel_signal'] = str(cancel)
def wait(_):
if fault == 'cancel_wait': cancel.touch()
else: offset[0] += 31
monkeypatch.setattr(diagnostic, 'time', SimpleNamespace(monotonic=lambda:clock()+offset[0], sleep=wait))
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
monkeypatch.setattr(diagnostic, '_windows', lambda pid: [frame(job)])
def emit(**event):
if event['stage'] != 'diagnostic_close_requested': return
if fault in ('missing', 'cancel_wait'): return
ack = {k:v for k,v in event.items() if k != 'stage'}
for condition,key,value in [('stale_nonce','close_id','0'*32),('foreign_pid','pcb_pid',99),
('foreign_project','project','foreign'),('foreign_desktop','desktop','Default')]:
if fault == condition: ack[key] = value
(Path(job['root'])/'diagnostic-close-ack.json').write_text(json.dumps(ack))
if fault == 'cancel_ack': cancel.touch()
if fault == 'late_ack': offset[0] += 31
with pytest.raises(RuntimeError, match='VERIFY_CANCELLED' if fault.startswith('cancel') else 'DIAGNOSTIC_CLOSE_UNCONFIRMED'):
diagnostic.check_target_window(job,42,emit=emit,error_reader=lambda:None)
assert Session.instances[-1].alive and Session.instances[-1].posted == []
@pytest.mark.parametrize('change', ['progress', 'disabled', 'hwnd', 'pid', 'unknown', 'forward', 'native_error', 'cancel_recheck'])
def test_ready_frame_and_prompt_are_revalidated_after_ack(job, monkeypatch, change):
from cbb_refresh_verify import diagnostic_open as diagnostic
job['diagnostic_log_baseline'] = diagnostic.snapshot_diagnostic_logs(job)
write_logs(job); acknowledged = []
job['cancel_signal'] = str(Path(job['root'])/'cancel.signal')
monkeypatch.setattr(diagnostic, '_NativeSession', Session)
def windows(pid):
target = frame(job); rows = [target]
if not acknowledged: return rows
if change == 'progress': target['children'].append({'class':'msctls_progress32','visible':True})
if change == 'disabled': target['enabled'] = False
if change == 'hwnd': target['hwnd'] = 99
if change == 'pid': target['pid'] = 99
if change == 'cancel_recheck': Path(job['cancel_signal']).touch()
if change == 'unknown': rows.append({'hwnd':20,'pid':42,'class':'#32770','text':'Unknown','children':[]})
if change == 'forward': rows.append({'hwnd':20,'pid':42,'class':'#32770','text':'Expedition PCB','owner':10,
'children':[{'class':'Static','id':6172,'text':'New changes are ready for Forward Annotation. Do you want to Forward Annotate now?'},
{'hwnd':21,'class':'Button','id':7,'text':'No'}, {'hwnd':22,'class':'Button','id':6,'text':'Yes'}]})
return rows
monkeypatch.setattr(diagnostic, '_windows', windows)
def emit(**event):
if event['stage'] == 'diagnostic_close_requested':
(Path(job['root'])/'diagnostic-close-ack.json').write_text(json.dumps({k:v for k,v in event.items() if k!='stage'}))
acknowledged.append(True)
def error():
return 'ICDB_INCONSISTENT: fresh error after handoff' if change == 'native_error' and acknowledged else None
expected = {'native_error':'ICDB_INCONSISTENT', 'cancel_recheck':'VERIFY_CANCELLED'}.get(change, 'DIAGNOSTIC_CLOSE_UNCONFIRMED')
with pytest.raises(RuntimeError, match=expected):
diagnostic.check_target_window(job,42,emit=emit,error_reader=error)
assert acknowledged and Session.instances[-1].posted == []
+15 -3
View File
@@ -1,14 +1,25 @@
import importlib
import importlib.util
import json
from pathlib import Path
import pytest
@pytest.fixture
def diagnostic():
def diagnostic(monkeypatch):
assert importlib.util.find_spec('cbb_refresh_verify.diagnostic_open') is not None, 'diagnostic open module is missing'
return importlib.import_module('cbb_refresh_verify.diagnostic_open')
module = importlib.import_module('cbb_refresh_verify.diagnostic_open')
check = module.check_target_window
def acknowledged(job, pid, *, emit, **kwargs):
def parent(**event):
emit(**event)
if event['stage'] == 'diagnostic_close_requested':
(Path(job['root'])/'diagnostic-close-ack.json').write_text(json.dumps(
{key:value for key,value in event.items() if key != 'stage'}))
return check(job, pid, emit=parent, **kwargs)
monkeypatch.setattr(module, 'check_target_window', acknowledged)
return module
@pytest.fixture
@@ -17,8 +28,9 @@ def job(tmp_path):
pcb = project / 'PCB/IP101GR.pcb'
pcb.parent.mkdir(parents=True)
pcb.write_bytes(b'pcb')
(tmp_path/'evidence').mkdir()
return {'project': str(project / 'IP101GR.prj'), 'pcb': str(pcb), 'root': str(tmp_path / 'evidence'),
'desktop': 'CbbDiagnosticTest', 'timeout': .03}
'desktop': 'CbbDiagnosticTest', 'operation_id': 'verify-fixture', 'timeout': .03}
def write_logs(job, *, pid=42, foreign=False, success=True):