"""Review 319 regressions; native databases are replaced by isolated files.""" from contextlib import contextmanager from pathlib import Path from threading import Event import json import subprocess import sys import time import pytest from cbb_refresh_verify import session_record as sessions from test_session_record import child, record def retain_unconfirmed(root, library): root.mkdir(parents=True) path = root/'session.json' sessions.write_session_record(path, dict(operation_id=root.name, library=str(library), processes=[])) done = Event() sessions.handoff_session(path, evidence_root=root, progress=lambda _: done.set()) assert done.wait(3) with sessions._observer_guard: observer = sessions._observers.get(str(path.absolute()).casefold()) if observer: observer[0].join(3) assert not observer[0].is_alive() assert not sessions.library_available(library) return path def test_explicit_proof_resumes_queue_after_unconfirmed_observer_ended(tmp_path, monkeypatch): from cbb_refresh_verify import library_lock as locks from cbb_refresh_verify.operation_queue import NativeQueue monkeypatch.setattr(sessions, '_retained_libraries', {}) library = tmp_path/'library/lib.lmc' library.parent.mkdir(); library.touch() other = tmp_path/'other.lmc' retain_unconfirmed(tmp_path/'old', library) retain_unconfirmed(tmp_path/'independent', other) @contextmanager def proof(*args, **kwargs): yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]} monkeypatch.setattr(locks, '_prove_library_released', proof) result = locks.release_retained_library(library, tmp_path/'proof') assert result['status'] == 'RELEASE_CONFIRMED' assert sessions.library_available(library), 'successful explicit proof left the queue blocked' assert sessions.discover_sessions(tmp_path/'old') == [], 'released history reappeared as unconfirmed' assert not sessions.library_available(other) assert NativeQueue().run(lambda: 'resumed', ready=lambda: sessions.library_available(library)) == 'resumed' def test_failed_proof_and_newer_handoff_cannot_clear_queue_retention(tmp_path, monkeypatch): from cbb_refresh_verify import library_lock as locks monkeypatch.setattr(sessions, '_retained_libraries', {}) library = tmp_path/'library/lib.lmc' library.parent.mkdir(); library.touch() old = retain_unconfirmed(tmp_path/'old', library) def denied(*args, **kwargs): raise OSError('external owner remains') monkeypatch.setattr(locks, '_prove_library_released', denied) with pytest.raises(OSError, match='external owner'): locks.release_retained_library(library, tmp_path/'proof') assert not sessions.library_available(library) @contextmanager def proof(*args, **kwargs): retain_unconfirmed(tmp_path/'new', library) yield {'native_owners': [], 'addresses': [], 'exclusive_files': [str(library)]} monkeypatch.setattr(locks, '_prove_library_released', proof) locks.release_retained_library(library, tmp_path/'proof') assert not sessions.library_available(library) assert str(old.absolute()).casefold() not in sessions._retained_libraries def test_discovery_of_exited_session_never_reconciles_library(tmp_path, child, monkeypatch): path = record(tmp_path, [dict(role='worker', **sessions.process_identity(child.pid))]) child.terminate(); child.wait(5) def forbidden(*args, **kwargs): pytest.fail('passive history scan attempted library release') monkeypatch.setattr('cbb_refresh_verify.library_lock.release_retained_library', forbidden) found = sessions.discover_sessions(tmp_path) assert len(found) == 1 and found[0]['path'] == str(path) assert found[0]['status'] == 'UNCONFIRMED' assert found[0]['release_proof'] is None @pytest.mark.parametrize('phase', ['PRECALL', 'POSTCALL']) @pytest.mark.parametrize('broken_evidence', [False, True]) def test_parent_loss_exits_idle_worker_even_when_stop_evidence_fails(tmp_path, phase, broken_evidence): run_watchdog_case(tmp_path, phase, broken_evidence=broken_evidence, exits=True) @pytest.mark.parametrize('phase', ['NATIVE_CALL', 'CLOSING', 'UNCONFIRMED']) def test_parent_loss_preserves_pending_or_unknown_worker(tmp_path, phase): run_watchdog_case(tmp_path, phase, exits=False) def test_stale_postcall_journal_cannot_override_in_memory_active_call(tmp_path): run_watchdog_case(tmp_path, 'NATIVE_CALL', stale=True, exits=False) def test_missing_journal_preserves_worker_until_state_can_be_confirmed(tmp_path): run_watchdog_case(tmp_path, 'POSTCALL', missing=True, exits=False) def run_watchdog_case(root, phase, *, exits, broken_evidence=False, stale=False, missing=False): supervisor = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(30)']) script, ready, returned = root/'worker.py', root/'ready', root/'returned' cancel = root/'missing/cancel' if broken_evidence else root/'cancel' script.write_text( 'import time\nfrom pathlib import Path\n' 'from cbb_refresh_verify import session_record as s, desktop_worker as d\n' f'job=dict(operation_id="watchdog-test", session_record_path={str(root/"session.json")!r})\n' f's.update_worker_state(job, {"POSTCALL" if stale else phase!r})\n' 'original=s.write_session_record\n' 'def write(path,value):\n' f' if {broken_evidence!r} and Path(path).name=="supervisor-lost.json": raise OSError("disk full")\n' f' if {stale!r} and Path(path).name=="worker-state.json": raise OSError("stale journal")\n' ' return original(path,value)\n' 's.write_session_record=write\n' + (f's.update_worker_state(job,{phase!r})\n' if stale else '') + (f'Path({str(root/"worker-state.json")!r}).unlink()\n' if missing else '') + f'd.start_supervisor_watchdog({supervisor.pid},cancel_path={str(cancel)!r},' 'session_record_path=job["session_record_path"],operation_id=job["operation_id"])\n' f'Path({str(ready)!r}).touch()\n' f'while not Path({str(returned)!r}).exists(): time.sleep(.02)\n' 's.write_session_record=original\n' 's.update_worker_state(job,"POSTCALL")\n' 'time.sleep(30)\n', encoding='utf-8') log = (root/'worker.log').open('wb') worker = subprocess.Popen([sys.executable, str(script)], stdout=log, stderr=log) try: deadline = time.monotonic()+6 while not ready.exists() and worker.poll() is None and time.monotonic()