| """CI gate: a lesion label/mask must NEVER be accessible during subspace construction. |
| |
| This test must FAIL the build if any label read can occur inside |
| `subspace_construction_guard()` (IMPLEMENTATION_SPEC §5, CLAUDE.md). It exercises the |
| runtime guard directly and also simulates a subspace `fit` that wrongly tries to read a |
| label, asserting the guard stops it. |
| """ |
| import sys |
| from pathlib import Path |
|
|
| import pytest |
|
|
| sys.path.insert(0, str(Path(__file__).resolve().parents[1])) |
|
|
| from data.leak_guard import ( |
| LabelLeakError, |
| assert_label_free, |
| subspace_construction_guard, |
| ) |
| from data.masks import label_from_manifest_record |
|
|
| _REC = { |
| "slice_id": "s0", |
| "has_nodule": True, |
| "nodule_pixel_area": 42.0, |
| "nodule_diameter_mm": 8.1, |
| "label": "tumor", |
| } |
|
|
|
|
| def test_label_read_allowed_outside_construction(): |
| |
| lab = label_from_manifest_record(_REC) |
| assert lab.has_nodule is True |
| assert lab.label == "tumor" |
|
|
|
|
| def test_label_read_blocked_during_construction(): |
| with subspace_construction_guard(): |
| with pytest.raises(LabelLeakError): |
| label_from_manifest_record(_REC) |
|
|
|
|
| def test_raw_guard_raises_inside_construction(): |
| with subspace_construction_guard(): |
| with pytest.raises(LabelLeakError): |
| assert_label_free("lesion mask") |
|
|
|
|
| def test_guard_is_reentrant_and_restores(): |
| with subspace_construction_guard(): |
| with subspace_construction_guard(): |
| with pytest.raises(LabelLeakError): |
| assert_label_free() |
| |
| assert_label_free() |
|
|
|
|
| def test_simulated_subspace_fit_with_leak_fails(): |
| """A subspace fit() that illegally reads a label must blow up via the guard.""" |
|
|
| def bad_fit(token_bank, record): |
| with subspace_construction_guard(): |
| |
| _ = label_from_manifest_record(record) |
| return token_bank.mean() |
|
|
| with pytest.raises(LabelLeakError): |
| bad_fit([1.0, 2.0], _REC) |
|
|