diff --git a/src/cbb_refresh_verify/residual_sessions.py b/src/cbb_refresh_verify/residual_sessions.py index b815f2f..d7bf12f 100644 --- a/src/cbb_refresh_verify/residual_sessions.py +++ b/src/cbb_refresh_verify/residual_sessions.py @@ -974,7 +974,7 @@ def _orphan_prewrite_client(path, job, session, state, identities): clients = [p for p in identities if p.get('role') == 'native' and p['pid'] == pid] if len(clients) != 1 or not is_reusable_circuit_pcb(clients[0]['image'], started['command'], job['pcb']): return None - return clients[0] + return clients[0], ready.get('baseline') def recoverable_orphan_session(path): @@ -1102,8 +1102,11 @@ def finish_owned_idle_session(path, *, stop=None): workers = [p for p in identities if p.get('role') in ('worker','helper')] clients = [p for p in identities if p.get('role') == 'native' and Path(p['image']).name.casefold() == client_name] orphan_client = None + orphan_baseline = None if operation == 'verify' and result is None and not _saved_and_closed(_worker_save_facts(state, identities)): - orphan_client = _orphan_prewrite_client(path, job, session, state, identities) + orphan = _orphan_prewrite_client(path, job, session, state, identities) + if orphan is not None: + orphan_client, orphan_baseline = orphan clients = [orphan_client] if orphan_client is not None else [] if not workers or len(clients) != 1: return False if any(r.get('operation_id') != session['operation_id'] for r in active_write_records(session['library'])): @@ -1135,19 +1138,15 @@ def finish_owned_idle_session(path, *, stop=None): target, _ = close_target(client_name, windows, incomplete=_window_error(snapshot, client['pid'])) if target is None or not any(w.get('documents') for w in windows): return target - if orphan_client is None: + if not isinstance(orphan_baseline, dict) or not {'started_ns', 'files'} <= orphan_baseline.keys(): return None # PCB can finish loading after its worker has stopped. Close # only the design independently bound by this launch's logs. from .diagnostic_open import _load_evidence, _target_frame - from .failed_session import cancelled_prewrite_startup - baseline = cancelled_prewrite_startup(job, client['pid']) - if baseline is None: - return None frames = _legacy_diagnostic_windows(dict(client, cleanup_desktop=desktop)) frame = _target_frame(frames, Path(job['pcb']).stem) if (frame and frame['hwnd'] == target['hwnd'] - and _load_evidence(job, client['pid'], baseline, require_autosave=False) is not None): + and _load_evidence(job, client['pid'], orphan_baseline, require_autosave=False) is not None): return target return None def startup_prompt(): @@ -1160,6 +1159,8 @@ def finish_owned_idle_session(path, *, stop=None): modals = [w for w in windows if w['class'] == '#32770'] if len(modals) != 1: return None + if session.get('owned_startup_prompt') == modals[0]['hwnd']: + return None with _window_desktop(desktop): modal = modals[0] observed = _startup_snapshot(modal['hwnd'], client['pid']) @@ -1172,10 +1173,10 @@ def finish_owned_idle_session(path, *, stop=None): and event.get('hwnd') == modal['hwnd']) for event in read('events.jsonl', lines=True)): return None - return observed, tip.handle, 'tip' + return observed, tip.handle, 'tip', modal['hwnd'] target = _cancel_target(observed) if target is not None and (orphan_client is not None or target[0] == 'database_load_log'): - return observed, target[1].handle, target[0] + return observed, target[1].handle, target[0], modal['hwnd'] return None if not same_client(): return False @@ -1238,6 +1239,14 @@ def finish_owned_idle_session(path, *, stop=None): break if stop is not None and stop.is_set(): break + elif prompt is not None: + # Allow a later close after the confirmed startup prompt + # disappears and the design completes loading. + latest = read('session.json') + if latest.get('owned_idle_cleanup') == claim: + latest.pop('owned_idle_cleanup') + latest['owned_startup_prompt'] = prompt[3] + write_session_record(path, latest) exited = win32event.WaitForSingleObject(handle,0) == win32con.WAIT_OBJECT_0 write_session_record(path.with_name('owned-idle-cleanup-result.json'),dict( operation_id=session['operation_id'],library=session['library'], diff --git a/tests/test_issue21_session_recovery.py b/tests/test_issue21_session_recovery.py index 6705797..312e8b7 100644 --- a/tests/test_issue21_session_recovery.py +++ b/tests/test_issue21_session_recovery.py @@ -179,7 +179,7 @@ def test_cancelled_prewrite_can_dismiss_exact_late_load_failure(qtbot, tmp_path, assert (root/'result.json').read_bytes() == original -@pytest.mark.parametrize('obstacle', [None, 'tip', 'cancelled_tip', 'previously_dismissed_tip', +@pytest.mark.parametrize('obstacle', [None, 'tip', 'slow_tip', 'cancelled_tip', 'previously_dismissed_tip', 'read_write', 'unknown_prompt', 'save', 'uncertain_phase', 'live_supervisor', 'live_worker', 'wrong_pcb', 'possible_write', 'loaded', 'unproved_loaded']) @@ -249,7 +249,7 @@ def test_restarted_verify_closes_exact_prewrite_orphan(tmp_path, monkeypatch, ob monkeypatch.setattr(residual_sessions, '_recorded_process_exited', lambda identity: obstacle != 'live_supervisor' and identity == supervisor) - if obstacle in ('tip', 'cancelled_tip', 'previously_dismissed_tip', 'read_write', 'unknown_prompt'): + if obstacle in ('tip', 'slow_tip', 'cancelled_tip', 'previously_dismissed_tip', 'read_write', 'unknown_prompt'): frame = windows[0] frame['enabled'] = False windows.append(dict(hwnd=200, title='Tip of the Day' if obstacle.endswith('tip') else 'Expedition PCB', @@ -261,7 +261,7 @@ def test_restarted_verify_closes_exact_prewrite_orphan(tmp_path, monkeypatch, ob state['posts'].append((hwnd, pid, message)) if hwnd == 201: windows.pop() - frame['enabled'] = True + frame['enabled'] = obstacle != 'slow_tip' else: state['native_alive'] = False @@ -276,8 +276,19 @@ def test_restarted_verify_closes_exact_prewrite_orphan(tmp_path, monkeypatch, ob monkeypatch.setattr(failed_session, '_tip_target', lambda _: SimpleNamespace(handle=201)) monkeypatch.setattr(failed_session, '_cancel_target', lambda _: ('read_write_access', SimpleNamespace(handle=201)) if obstacle == 'read_write' else None) + if obstacle == 'slow_tip': + ticks = iter([0., 6., 7., 8., 9.]) + monkeypatch.setattr(residual_sessions, 'time', SimpleNamespace(monotonic=lambda: next(ticks))) recovered = residual_sessions.finish_owned_idle_session(path) + if obstacle == 'slow_tip': + assert recovered and state['posts'] == [(201, 12, 245)] + journal = session_record.read_session_record(path, evidence_root=root) + assert journal['owned_startup_prompt'] == 200 and 'owned_idle_cleanup' not in journal + frame['enabled'] = True + assert residual_sessions.finish_owned_idle_session(path) + assert state['posts'] == [(201, 12, 245), (100, 12, 16)] + return assert recovered is (obstacle in (None, 'tip', 'cancelled_tip', 'read_write', 'loaded')) expected_posts = [(201, 12, 245)] if obstacle in ('tip', 'cancelled_tip', 'read_write') else [] if recovered: