diff --git a/docs/releases/2026-11-03-native-crud.md b/docs/releases/2026-11-03-native-crud.md index 7de651f..eccda39 100644 --- a/docs/releases/2026-11-03-native-crud.md +++ b/docs/releases/2026-11-03-native-crud.md @@ -9,3 +9,5 @@ Before native activation the worker revalidates that evidence, exact current lea Verification is mocked/file-only: the original focused suite passed 86 tests plus two close/reopen checks. Follow-up tests reject public bypass, unprepared activation, missing current-thread ownership, invalid nonce/token/lease, substituted original source, changed backup and expired supervisor, and exercise a valid leased fixture with unchanged original source bytes. No Mentor processes, builds or full suite were launched by this implementation agent; serialized native evidence and semantic-identity research remain coordinator-owned. The follow-up focused suite passed 99 tests. After restoring ordinary catalog queries for non-DxDesigner flow readback, the native boundary/field subset passed 47 tests (including four additional flow cases). `git diff --check` passed. The original ignored scratch report remains on disk but is removed from version control. + +A second review found ownership lifetime and reverse containment gaps. The fixture route now nests the already-proven caller lease around worker dispatch and retains that same exclusive handle/checkpoint on unsafe return or exception, including callers that supply no release callback. It also rejects a fixture nested anywhere inside the original source tree before staging. Three new failure cases were observed before the fix; the final native-boundary/field subset passed 51 tests, including normal release and uploader-handle denial after uncertain release. No native processes were launched. diff --git a/src/cbb_refresh_verify/native_management.py b/src/cbb_refresh_verify/native_management.py index 5064810..85236d4 100644 --- a/src/cbb_refresh_verify/native_management.py +++ b/src/cbb_refresh_verify/native_management.py @@ -112,7 +112,15 @@ class NativeManagementAdapter: if not acquired: raise RuntimeError('Another CBB operation owns the management mutex.') job = prepare_owned_fixture_add(job, evidence) - return self._launch_worker(job, evidence) + from .library_lock import LibraryLock + owner_safe = False + # Nest only after preparation proves an existing caller lease. + # An unsafe result/exception retains that same owner even when + # the caller used a plain LibraryLock without a callback. + with LibraryLock(job['library'], safe_to_release=lambda: owner_safe): + result = self._launch_worker(job, evidence) + owner_safe = result.get('safe_to_continue') is True + return result finally: if acquired: win32event.ReleaseMutex(mutex) mutex.Close() @@ -250,7 +258,8 @@ def prepare_owned_fixture_add(job, root): lease_handle = int(owner['handle']) source = Path(job['source']).resolve(strict=True) if (source.parent.is_relative_to(root.resolve()) or root.resolve().is_relative_to(source.parent) - or source.parent.is_relative_to(library.parent.resolve())): + or source.parent.is_relative_to(library.parent.resolve()) + or library.parent.resolve().is_relative_to(source.parent)): raise ValueError('Research source must be independent of fixture and transaction evidence.') source_manifest = copy_verified(source.parent, root/'source') backup = copy_verified(library.parent, root/'library-before', diff --git a/tests/test_native_management.py b/tests/test_native_management.py index e2193d6..72826d1 100644 --- a/tests/test_native_management.py +++ b/tests/test_native_management.py @@ -89,6 +89,67 @@ def test_fixture_metadata_without_current_thread_lease_cannot_stage_source(tmp_p assert not (root/'source').exists() +@pytest.mark.parametrize('outcome', ['unsafe', 'exception', 'safe']) +def test_owned_fixture_retains_default_caller_lease_until_native_release(tmp_path, monkeypatch, outcome): + import cbb_refresh_verify.native_management as native + from cbb_refresh_verify.management import ManagementRequest + from cbb_refresh_verify.library_lock import LibraryLock, LibraryBusy, blocker_path, _owners, _guard + library = tmp_path/'native-add/fixture/mentor_lib.lmc' + library.parent.mkdir(parents=True); library.write_bytes(b'fixture') + source = tmp_path/'original/Source.prj'; source.parent.mkdir(); source.write_bytes(b'source') + (tmp_path/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'owned'}), encoding='utf-8') + adapter = native.NativeManagementAdapter(mentor_home=tmp_path) + monkeypatch.setattr(adapter, 'route_capabilities', lambda: {'add': native.Capability(True, 'ready')}) + monkeypatch.setattr(native, 'engineering_identity', lambda _: {'source': 'bytes'}) + def launch(*args): + if outcome == 'exception': raise OSError('worker result lost after dispatch') + return {'success': outcome == 'safe', 'safe_to_continue': outcome == 'safe'} + monkeypatch.setattr(adapter, '_launch_worker', launch) + lease = LibraryLock(library) + try: + with lease: + if outcome == 'exception': + with pytest.raises(OSError, match='after dispatch'): + adapter.add_owned_fixture(ManagementRequest('add', library, 'Block', source=source), + tmp_path/'add', probe_token='owned') + else: + adapter.add_owned_fixture(ManagementRequest('add', library, 'Block', source=source), + tmp_path/'add', probe_token='owned') + assert blocker_path(library).exists() is (outcome != 'safe') + if outcome != 'safe': + with pytest.raises(LibraryBusy): + with LibraryLock(library): pass + import win32file, win32con, pywintypes + with pytest.raises(pywintypes.error): + win32file.CreateFile(str(lease.path), win32con.GENERIC_READ, 0, None, + win32con.OPEN_EXISTING, 0, None) + else: + with LibraryLock(library): pass + finally: + # Only the file-only fake worker above ran; release its test lease. + with _guard: + owner = _owners.pop(lease.key, None) + if owner: owner['handle'].Close() + blocker_path(library).unlink(missing_ok=True) + + +def test_fixture_nested_inside_source_tree_rejected_before_staging(tmp_path): + import cbb_refresh_verify.native_management as native + from cbb_refresh_verify.library_lock import LibraryLock + library = tmp_path/'native-add/fixture/mentor_lib.lmc' + library.parent.mkdir(parents=True); library.write_bytes(b'fixture') + source = tmp_path/'native-add/Source.prj'; source.write_bytes(b'original source') + (tmp_path/'probe-owner.json').write_text(json.dumps({'library': str(library), 'token': 'owned'}), encoding='utf-8') + root = tmp_path/'add'; root.mkdir() + with LibraryLock(library): + with pytest.raises(ValueError, match='independent'): + native.prepare_owned_fixture_add({'library': str(library), 'source': str(source), + 'name': 'Block', 'partition': 'Logical-Physical', 'source_identity': {}, + 'probe_token': 'owned'}, root) + assert source.read_bytes() == b'original source' and library.read_bytes() == b'fixture' + assert not (root/'source').exists() + + def test_add_route_readiness_is_separate_from_conservative_capability_gate(tmp_path, monkeypatch): import hashlib import cbb_refresh_verify.native_management as native