fix: bind release receipts and external export ownership

This commit is contained in:
阿白叔 committed 2026-11-16 17:53:58 +08:00
1 parent b070837acb
commit 0440b2efbf
6 files changed
+262 -12

No files matched your search

+72
View File
@@ -0,0 +1,72 @@
"""A durable receipt cannot expand the scope of its resource proof."""
import pytest
from cbb_refresh_verify import library_lock as locks, residual_sessions, session_record as sessions
@pytest.mark.parametrize('selected', [('A',), ('A', 'C')])
def test_scoped_release_receipt_leaves_unselected_journal_discoverable(tmp_path, monkeypatch, selected):
library = tmp_path/'library/lib.lmc'
library.parent.mkdir()
library.touch()
address = library.parent/'ReusableBlocksLP/B/Work/database/cdbsvr/sAddress.adr'
address.parent.mkdir(parents=True)
address.write_bytes(b'unselected B owner')
history = tmp_path/'history'
records = []
for block in ('A', 'B', 'C'):
path = history/block/'session.json'
path.parent.mkdir(parents=True)
value = dict(operation_id='op-'+block, library=str(library), block=block, processes=[])
sessions.write_session_record(path, value)
records.append({**value, 'path': str(path)})
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [])
monkeypatch.setattr(residual_sessions, 'query_process_snapshot', lambda: [])
monkeypatch.setattr(residual_sessions, '_process_windows', lambda _: {})
with locks.LibraryLock(library, block_name='A', safe_to_release=lambda: False) as lease:
pass
try:
result = residual_sessions.release_residual_sessions(dict(library=str(library),
block_names=list(selected), sessions=[], runtime_sessions=records), tmp_path/'evidence')
assert result['status'] == 'RELEASE_CONFIRMED', result.get('detail')
assert not list((history/'B').glob('observation-release-*.json'))
assert address.read_bytes() == b'unselected B owner'
monkeypatch.setattr(sessions, '_retained_libraries', {})
monkeypatch.setattr(sessions, '_retained_results', {})
found = sessions.discover_sessions(history, library=library)
assert {row['block'] for row in found} == {'A', 'B', 'C'}-set(selected)
finally:
owner = locks._owners.pop(lease.key, None)
if owner:
owner['handle'].Close()
@pytest.mark.parametrize('proof_scope,selected', [('BLOCK', ['A']), ('BLOCKS', ['A', 'C'])])
def test_discovery_rejects_existing_receipt_for_uncovered_operation(tmp_path, monkeypatch, proof_scope, selected):
library = tmp_path/'lib.lmc'
library.touch()
value = dict(operation_id='op-B', library=str(library), block='B', processes=[])
sessions.write_session_record(tmp_path/'session.json', value)
proof = dict(status='RELEASE_CONFIRMED', lease_state='RELEASED', library=str(library),
proof_scope=proof_scope, proof_block_names=selected,
proof_block_name=selected[0] if len(selected) == 1 else None,
resource_scope=dict(scope_state='OBSERVED', library=str(library), block_names=selected))
# Reproduce a receipt written by the prior implementation, then restart discovery.
sessions.write_session_record(tmp_path/'observation-release-old.json',
dict(operation_id='op-B', library=str(library), status='RELEASED', release_proof=proof))
monkeypatch.setattr(sessions, '_retained_libraries', {})
monkeypatch.setattr(sessions, '_retained_results', {})
found = sessions.discover_sessions(tmp_path, library=library)
assert len(found) == 1 and found[0]['operation_id'] == 'op-B'
def test_receipt_writer_keeps_replaced_operation_unresolved(tmp_path):
library = tmp_path/'lib.lmc'
library.touch()
path = tmp_path/'session.json'
sessions.write_session_record(path, dict(operation_id='new', library=str(library), block='A', processes=[]))
proof = dict(status='RELEASE_CONFIRMED', lease_state='RELEASED', library=str(library),
proof_scope='BLOCK', proof_block_names=['A'], proof_block_name='A',
resource_scope=dict(scope_state='OBSERVED', library=str(library), block_names=['A']))
sessions.record_session_release(proof, [{'path': str(path), 'operation_id': 'old'}])
assert not list(tmp_path.glob('observation-release-*.json'))
+3 -3
View File
@@ -162,15 +162,15 @@ def test_target_address_uses_native_inspection_and_keeps_recovery_separate(tmp_p
cleanup(lease)
def test_unknown_checkpoint_excludes_explicit_other_export_server(tmp_path, monkeypatch):
def test_unknown_checkpoint_does_not_disown_export_server_from_path_alone(tmp_path, monkeypatch):
library = library_file(tmp_path)
locks.blocker_path(library).write_bytes(b'unknown')
other = {'pid': 123456, 'name': 'icdbnetserver.exe',
'command': f'icdbnetserver.exe -icdb "{tmp_path}/Other/database"'}
monkeypatch.setattr('cbb_refresh_verify.owner_probe.query_owners', lambda *_: [other])
monkeypatch.setattr('cbb_refresh_verify.native_engineering_identity._process_snapshot', lambda: [other])
proof = locks.release_retained_library(library, tmp_path/'evidence')
assert proof['status'] == 'RELEASE_CONFIRMED'
with pytest.raises(locks.LibraryBusy):
locks.release_retained_library(library, tmp_path/'evidence')
def test_file_proof_holds_real_handle_until_context_exit(tmp_path):
+72
View File
@@ -0,0 +1,72 @@
"""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