"""A corrupt checkpoint cannot disown its independent identity-export copy.""" import json import pytest from cbb_refresh_verify import library_lock as locks, native_export_guard as exports def external_copy(tmp_path, monkeypatch, *, associated, process_name, invalid=None): library = tmp_path/'library/lib.lmc' library.parent.mkdir() library.touch() other = tmp_path/'other/lib.lmc' other.parent.mkdir() other.touch() root = tmp_path/'evidence/identity' (root/'project/database').mkdir(parents=True) project = root/'project/Source.prj' # Source-library references may differ from the transaction's owner library. project.write_text(f'KEY CentralLibrary "{other if associated else library}"\n', 'utf-8') (root/'project/database/icdb.dat').touch() owner_library = library if associated else other with locks.LibraryLock(owner_library) as lease: exports.begin_export_analysis(owner_library, root, project) # Restart removes process-local retained ownership, but independent evidence remains. locks._owners.pop(lease.key)['handle'].Close() locks.blocker_path(library).write_bytes(b'{truncated checkpoint') process = dict(pid=123456, parent=98765, name=process_name, executable='C:/Mentor/'+process_name, created='20261116120000.000000+480', command=(f'icdb2ascii.exe -project="{project}"' if process_name == 'icdb2ascii.exe' else f'icdbnetserver.exe -icdb "{project.parent}/database"')) record = dict(command=['C:/Mentor/icdb2ascii.exe', '-project='+str(project)], kind='ascii', success=False, safe_to_continue=False, native_started=True, pid=process['pid'] if process_name == 'icdb2ascii.exe' else 98765, observed_pids=[process['pid']], root_created='2026-11-16T04:00:00+00:00', process_identities=[{k: process[k] for k in ('pid', 'created', 'executable')}]) if invalid == 'identity': record['process_identities'][0]['created'] = '20261115120000.000000+480' (root/'ascii-process.json').write_text(json.dumps(record), 'utf-8') if invalid == 'missing_owner': (root/'export-owner.json').unlink() monkeypatch.setattr('cbb_refresh_verify.native_engineering_identity._retained_exports', []) monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [process]) monkeypatch.setattr('cbb_refresh_verify.native_engineering_identity._process_snapshot', lambda: [process]) return library, root @pytest.mark.parametrize('process_name', ['icdb2ascii.exe', 'icdbnetserver.exe']) @pytest.mark.parametrize('associated', [True, False]) def test_corrupt_checkpoint_requires_independent_export_association(tmp_path, monkeypatch, process_name, associated): library, root = external_copy(tmp_path, monkeypatch, associated=associated, process_name=process_name) marker = locks.blocker_path(library) before = marker.read_bytes() if associated: with pytest.raises(locks.LibraryBusy): locks.release_retained_library(library, tmp_path/'release') assert marker.read_bytes() == before else: proof = locks.release_retained_library(library, tmp_path/'release') assert proof['status'] == 'RELEASE_CONFIRMED' assert (root/'export-owner.json').is_file() @pytest.mark.parametrize('invalid', ['missing_owner', 'identity']) def test_unproven_other_export_remains_unknown(tmp_path, monkeypatch, invalid): library, _ = external_copy(tmp_path, monkeypatch, associated=False, process_name='icdbnetserver.exe', invalid=invalid) before = locks.blocker_path(library).read_bytes() with pytest.raises(locks.LibraryBusy): locks.release_retained_library(library, tmp_path/'release') assert locks.blocker_path(library).read_bytes() == before