diff --git a/.kiro/specs/detection-core/tasks.md b/.kiro/specs/detection-core/tasks.md index 69ef6f67..4e004a52 100644 --- a/.kiro/specs/detection-core/tasks.md +++ b/.kiro/specs/detection-core/tasks.md @@ -684,7 +684,7 @@ contract work and use synthetic/in-memory workflow boundaries only. - _Boundary: Fitted Consumer V3_ - _Depends: 17.2_ -- [ ] 18. Validate the V3 predecessor end to end +- [x] 18. Validate the V3 predecessor end to end - [x] 18.1 Run a synthetic multi-arm, multi-family evaluation through one shared spend - Use at least two component arms and one combined arm across at least two families and two ecosystems with multi-scope fixes and representative ordinary pushes. - Prove nonzero reads, reviewed plan, exact preflight, sealed observations, reviewed attempt history, one preparation/receipt, V3 release, scoped reports, complete replay, and exact fitted selection. @@ -717,7 +717,7 @@ contract work and use synthetic/in-memory workflow boundaries only. - _Requirements: 16.2, 20.6, 20.8_ - _Boundary: V3 Boundary and Performance Validation_ - _Depends: 18.1, 18.2, 18.3_ -- [ ] 18.5 Run the final compatibility, regression, and schema-export gate +- [x] 18.5 Run the final compatibility, regression, and schema-export gate - Run the V2 golden suite, every V3 unit/integration/adversarial suite, and the existing core regression suite without consuming a real admission population. - Prove all fixtures are readable and nonzero and the exact `openultrasast.detection-core.evaluation-admission.v3` schema is publicly exported. - Done when all gates pass, V2 outputs remain frozen, validation stays bounded by supplied artifacts, and the validated predecessor is ready for downstream benchmark task generation. @@ -759,6 +759,7 @@ contract work and use synthetic/in-memory workflow boundaries only. - Task 18.2 proves exact owning-layer pre-spend and version refusals with a connected synthetic downstream driver, loud input-read instrumentation, frozen V2 outputs, custody-preserving insufficiency, and zero detector/label/result/quality effects after rejection. - Task 18.3 proves exact owner-layer post-spend refusals across release, projection, combination, proof, metrics, reports, replay, current source/custody, aggregate, and fitted boundaries with connected authorization-effect instrumentation and all-arm visibility. - Task 18.4 enforces exact dependency and ownership boundaries across absolute, relative, package-root, wildcard, and near-prefix imports; validates genuine 1/2/4 replay-graph growth with connected bounded operation counts; and keeps all V3 validators within complexity caps. +- Task 18.5 closes the V3 predecessor with a non-circular classified input census, measured synthetic in-memory runtime provenance, exact frozen-V2/V3/core regression inventories, and the public schema export, without consuming a real admission population. - The frozen migration inventory is a reviewed worklist with stable identities, not a semantic-use census. It may only close entries as `v2_migrated` or `retired`; it never grows, deletes entries, or proves absence by interpreting Python flow. - Cutover is proved by explicit syntax checks plus deletion of the compatibility and v1 write capability. No task extends the discarded alias/data-flow interpreter. - Task 10.5 debug established that executable owner probes cannot compensate for blanket source-closure acceptance; resumed hunt attempts also require collision-resistant attempt identity and create-only persistence before handoff validation is authoritative. diff --git a/tests/detection_core_final_integration.py b/tests/detection_core_final_integration.py new file mode 100644 index 00000000..3957c6f2 --- /dev/null +++ b/tests/detection_core_final_integration.py @@ -0,0 +1,306 @@ +"""Closed input and runtime-provenance instruments for the final V3 gate.""" + +from __future__ import annotations + +import json +from dataclasses import dataclass, field +from enum import Enum +from pathlib import Path + +from detection_core_fixtures import InMemoryContentResolver + +from openultrasast.core import canonical + +EVALUATION_ADMISSION_SCHEMA_V3 = "openultrasast.detection-core.evaluation-admission.v3" +_GATE_OWNED_NAMES = frozenset( + { + "detection_core_final_integration.py", + "test_core_evaluation_final_integration.py", + } +) +_V2_FIXTURE_NAMES = frozenset( + { + "go_replay.json", + "go_report.json", + "manifest.json", + "no_go_replay.json", + "no_go_report.json", + "validation_failures.json", + "zero_model_replay.json", + "zero_model_report.json", + } +) + + +class FinalGateInputError(AssertionError): + """A final-gate input or runtime source violated its closed declaration.""" + + +class FinalGateInputClass(Enum): + """Closed kinds of filesystem input the final gate is allowed to read.""" + + V2_FIXTURE = "v2_fixture" + V3_SUITE = "v3_suite" + CORE_REGRESSION = "core_regression" + GATE_INSTRUMENT = "gate_instrument" + FIXTURE_SUPPORT = "fixture_support" + EXTERNAL_POPULATION = "external_population" + + +class FinalGateInputProvenance(Enum): + """Why one filesystem input is admitted to the final gate.""" + + FROZEN_V2_ARTIFACT = "frozen_v2_artifact" + TEST_SUITE_SOURCE = "test_suite_source" + GATE_INSTRUMENT_SOURCE = "gate_instrument_source" + SYNTHETIC_FIXTURE_SOURCE = "synthetic_fixture_source" + + +@dataclass(frozen=True) +class FinalGateInputRecord: + """One classified, contained, regular, nonzero filesystem read.""" + + source_path: Path + resolved_path: Path + classification: FinalGateInputClass + provenance: FinalGateInputProvenance + byte_count: int + + +@dataclass(frozen=True) +class FinalGateInputs: + """Exact filesystem census consumed by the final validation gate.""" + + repository_root: Path + v2_fixture_paths: tuple[Path, ...] + v3_suite_paths: tuple[Path, ...] + core_regression_paths: tuple[Path, ...] + gate_instrument_paths: tuple[Path, ...] + support_paths: tuple[Path, ...] + records: tuple[FinalGateInputRecord, ...] + external_population_inputs: tuple[Path, ...] = field(init=False) + + def __post_init__(self) -> None: + declared = { + *self.v2_fixture_paths, + *self.v3_suite_paths, + *self.core_regression_paths, + *self.gate_instrument_paths, + *self.support_paths, + } + recorded = {record.source_path for record in self.records} + if declared != recorded or len(recorded) != len(self.records): + raise FinalGateInputError("final-gate input census does not exactly cover every declared path") + external = tuple( + record.source_path + for record in self.records + if not record.resolved_path.is_relative_to(self.repository_root) + or record.classification is FinalGateInputClass.EXTERNAL_POPULATION + ) + object.__setattr__(self, "external_population_inputs", external) + if external: + raise FinalGateInputError(f"external population inputs are forbidden: {external}") + + def _bytes(self, paths: tuple[Path, ...]) -> int: + sizes = {record.source_path: record.byte_count for record in self.records} + return sum(sizes[path] for path in paths) + + @property + def v2_fixture_bytes(self) -> int: + return self._bytes(self.v2_fixture_paths) + + @property + def v3_suite_bytes(self) -> int: + return self._bytes(self.v3_suite_paths) + + @property + def core_regression_bytes(self) -> int: + return self._bytes(self.core_regression_paths) + + @property + def gate_instrument_bytes(self) -> int: + return self._bytes(self.gate_instrument_paths) + + @property + def support_bytes(self) -> int: + return self._bytes(self.support_paths) + + +def _repository_root(repo: Path) -> Path: + try: + root = repo.resolve(strict=True) + except OSError as exc: + raise FinalGateInputError(f"repository root is unreadable: {repo}: {exc}") from exc + if not root.is_dir(): + raise FinalGateInputError(f"repository root is not a directory: {root}") + return root + + +def _reject_symlink_path(root: Path, candidate: Path) -> None: + current = root + for part in candidate.relative_to(root).parts: + current /= part + if current.is_symlink(): + raise FinalGateInputError(f"final-gate input path contains a symlink: {candidate}") + + +def _classify(relative: Path) -> tuple[FinalGateInputClass, FinalGateInputProvenance]: + parts = relative.parts + if len(parts) == 5 and parts[:4] == ("tests", "fixtures", "detection_core", "admission_v2") and relative.name in _V2_FIXTURE_NAMES: + return FinalGateInputClass.V2_FIXTURE, FinalGateInputProvenance.FROZEN_V2_ARTIFACT + if len(parts) == 2 and parts[0] == "tests": + if relative.name in _GATE_OWNED_NAMES: + return FinalGateInputClass.GATE_INSTRUMENT, FinalGateInputProvenance.GATE_INSTRUMENT_SOURCE + if relative.name == "detection_core_fixtures.py": + return FinalGateInputClass.FIXTURE_SUPPORT, FinalGateInputProvenance.SYNTHETIC_FIXTURE_SOURCE + if relative.match("test_core_evaluation_*.py"): + return FinalGateInputClass.V3_SUITE, FinalGateInputProvenance.TEST_SUITE_SOURCE + if relative.match("test_core*.py"): + return FinalGateInputClass.CORE_REGRESSION, FinalGateInputProvenance.TEST_SUITE_SOURCE + raise FinalGateInputError(f"final-gate input path is unclassified: {relative}") + + +def _read_final_gate_input(repo: Path, path: Path) -> tuple[FinalGateInputRecord, bytes]: + root = _repository_root(repo) + candidate = path.absolute() + if not candidate.is_relative_to(root): + raise FinalGateInputError(f"final-gate input is outside repository: {candidate}") + _reject_symlink_path(root, candidate) + try: + resolved = candidate.resolve(strict=True) + except OSError as exc: + raise FinalGateInputError(f"final-gate input is unreadable: {candidate}: {exc}") from exc + if not resolved.is_relative_to(root): + raise FinalGateInputError(f"final-gate input resolves outside repository: {candidate}") + if not resolved.is_file(): + raise FinalGateInputError(f"final-gate input is not a regular file: {candidate}") + classification, provenance = _classify(resolved.relative_to(root)) + try: + content = resolved.read_bytes() + except OSError as exc: + raise FinalGateInputError(f"final-gate input could not be read: {candidate}: {exc}") from exc + if not content: + raise FinalGateInputError(f"final-gate input read zero bytes: {candidate}") + return FinalGateInputRecord(candidate, resolved, classification, provenance, len(content)), content + + +def record_final_gate_input(repo: Path, path: Path) -> FinalGateInputRecord: + """Classify and prove one candidate filesystem input before using it.""" + return _read_final_gate_input(repo, path)[0] + + +def _paths(root: Path, pattern: str, purpose: str) -> tuple[Path, ...]: + paths = tuple(sorted(root.glob(pattern))) + if not paths: + raise FinalGateInputError(f"{purpose} discovered zero inputs under {root}") + return paths + + +def _validate_v2_manifest(contents: dict[str, bytes]) -> None: + try: + manifest = json.loads(contents["manifest.json"]) + except (KeyError, UnicodeDecodeError, json.JSONDecodeError) as exc: + raise FinalGateInputError(f"V2 compatibility manifest is unreadable: {exc}") from exc + if manifest.get("schema_version") != "openultrasast.detection-core.admission-v2-compatibility-fixtures.v1": + raise FinalGateInputError("V2 compatibility manifest has an unexpected schema") + cases = manifest.get("cases") + if not isinstance(cases, list) or len(cases) != 3: + raise FinalGateInputError("V2 compatibility manifest must declare exactly three frozen cases") + referenced = { + value + for row in cases + if isinstance(row, dict) + for key in ("report_file", "replay_file") + if isinstance((value := row.get(key)), str) + } + expected = _V2_FIXTURE_NAMES - {"manifest.json", "validation_failures.json"} + if referenced != expected: + raise FinalGateInputError("V2 compatibility manifest does not reference the exact frozen outputs") + + +def collect_final_gate_inputs(repo: Path) -> FinalGateInputs: + """Read and classify every supplied filesystem artifact before validation.""" + root = _repository_root(repo) + tests = root / "tests" + fixture_root = tests / "fixtures/detection_core/admission_v2" + fixture_paths = _paths(fixture_root, "*.json", "V2 compatibility gate") + fixture_names = {path.name for path in fixture_paths} + if fixture_names != _V2_FIXTURE_NAMES: + raise FinalGateInputError( + f"V2 compatibility fixture set changed: missing={sorted(_V2_FIXTURE_NAMES - fixture_names)}, " + f"extra={sorted(fixture_names - _V2_FIXTURE_NAMES)}" + ) + gate_paths = tuple(sorted(tests / name for name in _GATE_OWNED_NAMES)) + gate_set = set(gate_paths) + v3_paths = tuple(path for path in _paths(tests, "test_core_evaluation_*.py", "V3 validation gate") if path not in gate_set) + core_paths = tuple(path for path in _paths(tests, "test_core*.py", "core regression gate") if path not in gate_set) + support_paths = (tests / "detection_core_fixtures.py",) + all_paths = tuple(sorted({*fixture_paths, *v3_paths, *core_paths, *gate_paths, *support_paths})) + reads = {path: _read_final_gate_input(root, path) for path in all_paths} + fixture_contents = {path.name: reads[path][1] for path in fixture_paths} + _validate_v2_manifest(fixture_contents) + return FinalGateInputs( + root, + fixture_paths, + v3_paths, + core_paths, + gate_paths, + support_paths, + tuple(reads[path][0] for path in all_paths), + ) + + +class RuntimeInputOrigin(Enum): + """Closed runtime sources accepted by the final synthetic replay gate.""" + + SYNTHETIC_IN_MEMORY = "synthetic_in_memory" + EXTERNAL_PATH = "external_path" + + +@dataclass(frozen=True) +class RuntimeContentRead: + """One positive content read made during public replay validation.""" + + artifact_id: str + read_number: int + byte_count: int + origin: RuntimeInputOrigin + + +class RecordingContentResolver: + """Allow and record only exact synthetic in-memory content resolution.""" + + def __init__(self, wrapped: InMemoryContentResolver, origin: RuntimeInputOrigin) -> None: + if type(wrapped) is not InMemoryContentResolver: + raise FinalGateInputError("runtime resolver must be exact InMemoryContentResolver") + if origin is not RuntimeInputOrigin.SYNTHETIC_IN_MEMORY: + raise FinalGateInputError("runtime resolver must declare synthetic in-memory origin") + self._wrapped = wrapped + self._origin = origin + self._records: list[RuntimeContentRead] = [] + + @property + def records(self) -> tuple[RuntimeContentRead, ...]: + return tuple(self._records) + + def resolve(self, reference: canonical.ContentRef) -> canonical.ResolvedContent: + resolved = self._wrapped.resolve(reference) + if not resolved.content: + raise FinalGateInputError(f"runtime resolver read zero bytes for {reference.artifact_id}") + self._records.append(RuntimeContentRead(reference.artifact_id, len(self._records) + 1, len(resolved.content), self._origin)) + return resolved + + +__all__ = [ + "EVALUATION_ADMISSION_SCHEMA_V3", + "FinalGateInputClass", + "FinalGateInputError", + "FinalGateInputProvenance", + "FinalGateInputRecord", + "FinalGateInputs", + "RecordingContentResolver", + "RuntimeContentRead", + "RuntimeInputOrigin", + "collect_final_gate_inputs", + "record_final_gate_input", +] diff --git a/tests/test_core_evaluation_final_integration.py b/tests/test_core_evaluation_final_integration.py new file mode 100644 index 00000000..0b3c6860 --- /dev/null +++ b/tests/test_core_evaluation_final_integration.py @@ -0,0 +1,142 @@ +"""Final compatibility, regression, and schema-export gate for evaluation admission V3.""" + +from __future__ import annotations + +from pathlib import Path + +import pytest +from detection_core_final_integration import ( + EVALUATION_ADMISSION_SCHEMA_V3, + FinalGateInputClass, + FinalGateInputError, + RecordingContentResolver, + RuntimeInputOrigin, + collect_final_gate_inputs, + record_final_gate_input, +) +from detection_core_fixtures import InMemoryContentResolver +from test_core_evaluation_end_to_end import _complete_replay + +from openultrasast.core import __all__ as core_exports +from openultrasast.core import evaluation_admission + +REPO = Path(__file__).resolve().parents[1] +TESTS = REPO / "tests" +GATE_OWNED_NAMES = frozenset( + { + "detection_core_final_integration.py", + "test_core_evaluation_final_integration.py", + } +) + + +def test_final_gate_reads_every_bounded_input_before_interpreting_results() -> None: + inputs = collect_final_gate_inputs(REPO) + gate_owned = {TESTS / name for name in GATE_OWNED_NAMES} + matching_v3 = set(TESTS.glob("test_core_evaluation_*.py")) + matching_core = set(TESTS.glob("test_core*.py")) + + assert set(inputs.gate_instrument_paths) == gate_owned + assert set(inputs.v3_suite_paths) == matching_v3 - gate_owned + assert set(inputs.core_regression_paths) == matching_core - gate_owned + assert inputs.v2_fixture_bytes > 0 + assert inputs.v3_suite_bytes > 0 + assert inputs.core_regression_bytes >= inputs.v3_suite_bytes + assert inputs.gate_instrument_bytes > 0 + assert inputs.support_bytes > 0 + assert inputs.external_population_inputs == () + assert inputs.records + assert all(record.byte_count > 0 for record in inputs.records) + assert all(record.resolved_path.is_relative_to(REPO.resolve()) for record in inputs.records) + assert {record.classification for record in inputs.records} == { + FinalGateInputClass.V2_FIXTURE, + FinalGateInputClass.V3_SUITE, + FinalGateInputClass.CORE_REGRESSION, + FinalGateInputClass.GATE_INSTRUMENT, + FinalGateInputClass.FIXTURE_SUPPORT, + } + + +def test_exact_v3_schema_is_exported_only_through_the_public_v3_facade() -> None: + assert EVALUATION_ADMISSION_SCHEMA_V3 == "openultrasast.detection-core.evaluation-admission.v3" + assert "evaluation_admission" in core_exports + assert evaluation_admission.EVALUATION_ADMISSION_V3_SCHEMA == EVALUATION_ADMISSION_SCHEMA_V3 + assert "EVALUATION_ADMISSION_V3_SCHEMA" in evaluation_admission.__all__ + + +def test_final_gate_inventory_includes_compatibility_adversarial_and_growth_suites() -> None: + inputs = collect_final_gate_inputs(REPO) + v3_names = {path.name for path in inputs.v3_suite_paths} + core_names = {path.name for path in inputs.core_regression_paths} + gate_names = {path.name for path in inputs.gate_instrument_paths} + + assert "test_core_admission_v2_compatibility.py" in core_names + assert "test_core_evaluation_pre_spend_matrix.py" in v3_names + assert "test_core_evaluation_post_spend_matrix.py" in v3_names + assert "test_core_evaluation_end_to_end.py" in v3_names + assert "test_core_evaluation_boundary_performance.py" in v3_names + assert gate_names == GATE_OWNED_NAMES + assert GATE_OWNED_NAMES.isdisjoint(v3_names) + assert GATE_OWNED_NAMES.isdisjoint(core_names) + + +def test_input_census_refuses_outside_symlink_empty_and_unclassified_paths(tmp_path: Path) -> None: + outside = tmp_path / "outside.json" + outside.write_text("{}", encoding="utf-8") + with pytest.raises(FinalGateInputError, match="outside repository"): + record_final_gate_input(REPO, outside) + + temporary_repo = tmp_path / "repo" + temporary_tests = temporary_repo / "tests" + temporary_tests.mkdir(parents=True) + unclassified = temporary_tests / "other.py" + unclassified.write_text("pass\n", encoding="utf-8") + with pytest.raises(FinalGateInputError, match="unclassified"): + record_final_gate_input(temporary_repo, unclassified) + + empty = temporary_tests / "test_core_empty.py" + empty.touch() + with pytest.raises(FinalGateInputError, match="zero bytes"): + record_final_gate_input(temporary_repo, empty) + + symlink = temporary_tests / "test_core_evaluation_link.py" + symlink.symlink_to(outside) + with pytest.raises(FinalGateInputError, match="symlink"): + record_final_gate_input(temporary_repo, symlink) + + +def test_public_synthetic_replay_records_only_nonzero_in_memory_reads(monkeypatch: pytest.MonkeyPatch) -> None: + bundle, replay, *_ = _complete_replay(monkeypatch) + recorder = RecordingContentResolver(bundle[4], RuntimeInputOrigin.SYNTHETIC_IN_MEMORY) + + reports = evaluation_admission.validate_evaluation_replay( + replay, + bundle[5][0].context, + bundle[5][0].port, + recorder, + ) + + assert reports == replay.reports + assert recorder.records + assert all(record.read_number > 0 for record in recorder.records) + assert all(record.byte_count > 0 for record in recorder.records) + assert {record.origin for record in recorder.records} == {RuntimeInputOrigin.SYNTHETIC_IN_MEMORY} + + +def test_runtime_recorder_refuses_path_backed_and_external_origins_before_validation(tmp_path: Path) -> None: + class PathBackedResolver: + def __init__(self) -> None: + self.path = tmp_path / "population.json" + self.resolve_calls = 0 + + def resolve(self, reference: object) -> object: + self.resolve_calls += 1 + raise AssertionError(f"path-backed resolver unexpectedly invoked: {reference}") + + path_backed = PathBackedResolver() + with pytest.raises(FinalGateInputError, match="exact InMemoryContentResolver"): + RecordingContentResolver(path_backed, RuntimeInputOrigin.SYNTHETIC_IN_MEMORY) # type: ignore[arg-type] + assert path_backed.resolve_calls == 0 + + with pytest.raises(FinalGateInputError, match="synthetic in-memory origin"): + RecordingContentResolver(InMemoryContentResolver({}), RuntimeInputOrigin.EXTERNAL_PATH)