From 169a08fa4ad75127d995fe171df03dbf4e73e3ca Mon Sep 17 00:00:00 2001 From: Marius Ciepluch <11855163+norandom@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:38:35 +0100 Subject: [PATCH] feat(valid-detection-benchmark): pin validated v3 predecessor --- .../specs/valid-detection-benchmark/tasks.md | 6 +- benchmarks/valid/core_conformance.py | 115 +++++++++++++++ .../test_valid_benchmark_core_conformance.py | 133 ++++++++++++++++++ 3 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 benchmarks/valid/core_conformance.py create mode 100644 tests/test_valid_benchmark_core_conformance.py diff --git a/.kiro/specs/valid-detection-benchmark/tasks.md b/.kiro/specs/valid-detection-benchmark/tasks.md index fad7f7e0..f38f70cc 100644 --- a/.kiro/specs/valid-detection-benchmark/tasks.md +++ b/.kiro/specs/valid-detection-benchmark/tasks.md @@ -19,7 +19,7 @@ - Detector logic, framework vocabulary, rollout, infrastructure administration, and the parked access-control arm remain outside this plan. - [ ] 1. Establish the replacement benchmark foundation -- [ ] 1.1 Pin the validated V3 predecessor and frozen historical compatibility surface +- [x] 1.1 Pin the validated V3 predecessor and frozen historical compatibility surface - Verify the exact V3 schema, per-arm exact-scope plan and admission roots, shared-spend authority, case-release validation, and complete evaluation replay through the public core facade. - Keep pooled V2 and historical pilot artifacts replayable only under their original schemas and refuse inferred conversion into V3. - Refuse startup when the core schema or required V3 capability is absent, and prove all compatibility fixtures are readable and nonzero before interpreting them. @@ -408,3 +408,7 @@ - _Requirements: 4.6, 4.10, 7.7, 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 9.5, 9.6, 9.7, 10.3, 10.4, 10.5, 10.6_ - _Boundary: Final Benchmark Validation Integration_ - _Depends: 11.6_ + +## Implementation Notes + +- Task 1.1 pins the exact public V3 facade and delegates decoding and complete replay validation to detection-core; frozen V2 and pilot artifacts remain byte-identical, read-only compatibility inputs with no V3 conversion path. diff --git a/benchmarks/valid/core_conformance.py b/benchmarks/valid/core_conformance.py new file mode 100644 index 00000000..deecb05c --- /dev/null +++ b/benchmarks/valid/core_conformance.py @@ -0,0 +1,115 @@ +"""Fail-closed benchmark consumer boundary for scoped admission V3.""" + +from __future__ import annotations + +from dataclasses import dataclass +from typing import Protocol, TypeVar, cast + +from openultrasast.core import evaluation_admission + +_EXPECTED_SCHEMA = "openultrasast.detection-core.evaluation-admission.v3" +_REQUIRED_OPERATIONS = ( + "authorize_evaluation", + "decode_evaluation_v3", + "preflight_evaluation", + "score_and_replay", + "validate_evaluation_replay", + "validate_shared_spend", +) +_EXPECTED_EXPORTS = ( + "EVALUATION_ADMISSION_V3_SCHEMA", + "EvaluationAdmissionV3Error", + *_REQUIRED_OPERATIONS, +) +_ValueT = TypeVar("_ValueT") + + +class _ConsumerFacade(Protocol): + def decode_evaluation_v3(self, schema: str, value: object, expected_type: type[_ValueT]) -> _ValueT: ... + + def validate_evaluation_replay( + self, + replay: object, + context: object, + custody: object, + resolver: object, + ) -> tuple[object, ...]: ... + + +class ScopedAdmissionV3ConformanceError(RuntimeError): + """The installed core cannot serve the pinned benchmark contract.""" + + +@dataclass(frozen=True) +class ScopedAdmissionV3Capability: + """Validated identity of the core contract consumed by the benchmark.""" + + schema: str + operations: tuple[str, ...] + + +def _require_exact_exports(facade: object) -> None: + exports = getattr(facade, "__all__", None) + if type(exports) not in (list, tuple) or not all(type(name) is str for name in exports): + raise ScopedAdmissionV3ConformanceError("V3 facade public exports are missing or malformed") + actual = tuple(exports) + missing = tuple(name for name in _EXPECTED_EXPORTS if name not in actual) + unexpected = tuple(name for name in actual if name not in _EXPECTED_EXPORTS) + if actual != _EXPECTED_EXPORTS: + raise ScopedAdmissionV3ConformanceError( + f"V3 facade exports differ from the pinned contract; missing={missing}, unexpected={unexpected}" + ) + + +def _require_operations(facade: object) -> None: + missing = tuple(name for name in _REQUIRED_OPERATIONS if not callable(getattr(facade, name, None))) + if missing: + raise ScopedAdmissionV3ConformanceError(f"V3 facade required callable capabilities are missing: {missing}") + + +def require_scoped_admission_v3( + facade: object = evaluation_admission, +) -> ScopedAdmissionV3Capability: + """Refuse benchmark startup unless the exact V3 predecessor is available.""" + _require_exact_exports(facade) + schema = getattr(facade, "EVALUATION_ADMISSION_V3_SCHEMA", None) + if type(schema) is not str or schema != _EXPECTED_SCHEMA: + raise ScopedAdmissionV3ConformanceError( + f"V3 facade schema differs from the pinned contract: expected {_EXPECTED_SCHEMA!r}, got {schema!r}" + ) + _require_operations(facade) + return ScopedAdmissionV3Capability(schema=schema, operations=_REQUIRED_OPERATIONS) + + +def decode_scoped_admission_v3( + schema: str, + value: object, + expected_type: type[_ValueT], + *, + facade: object = evaluation_admission, +) -> _ValueT: + """Decode only an exact V3 root; historical roots are never converted.""" + require_scoped_admission_v3(facade) + return cast(_ConsumerFacade, facade).decode_evaluation_v3(schema, value, expected_type) + + +def validate_scoped_admission_replay( + replay: object, + context: object, + custody: object, + resolver: object, + *, + facade: object = evaluation_admission, +) -> tuple[object, ...]: + """Validate a complete replay only after startup conformance succeeds.""" + require_scoped_admission_v3(facade) + return cast(_ConsumerFacade, facade).validate_evaluation_replay(replay, context, custody, resolver) + + +__all__ = [ + "ScopedAdmissionV3Capability", + "ScopedAdmissionV3ConformanceError", + "decode_scoped_admission_v3", + "require_scoped_admission_v3", + "validate_scoped_admission_replay", +] diff --git a/tests/test_valid_benchmark_core_conformance.py b/tests/test_valid_benchmark_core_conformance.py new file mode 100644 index 00000000..1191002c --- /dev/null +++ b/tests/test_valid_benchmark_core_conformance.py @@ -0,0 +1,133 @@ +"""Benchmark-only conformance gate for the validated core V3 predecessor.""" + +from __future__ import annotations + +import json +import sys +import tomllib +from hashlib import sha256 +from pathlib import Path +from types import SimpleNamespace + +import pytest +from test_core_evaluation_end_to_end import _assert_conformance, _complete_replay + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +from benchmarks.valid.core_conformance import ( + ScopedAdmissionV3ConformanceError, + decode_scoped_admission_v3, + require_scoped_admission_v3, + validate_scoped_admission_replay, +) +from openultrasast.core import evaluation_admission + +REPO = Path(__file__).resolve().parents[1] +V2_REPORT_SCHEMA = "openultrasast.detection-core.admission-report.v2" +V3_SCHEMA = "openultrasast.detection-core.evaluation-admission.v3" +FROZEN_HISTORY = { + "benchmarks/valid/pilot/freeze-valid.json": "fc6c97a034a6e2828bc003d72a2ab03445a18a3f64387310f26c7cd3ae9bf6ba", + "benchmarks/valid/pilot/pool-valid.toml": "48765c7703c6465f0f7fc00df81c07618b52065ed637cf97c112fdea98896c3f", + "tests/fixtures/detection_core/admission_v2/go_replay.json": "16dfa63087ac7ef11404196df1766961c16a3f0f148e743251df191a8070f7be", + "tests/fixtures/detection_core/admission_v2/go_report.json": "bf5d839c9c29f22db4a54ce2bbfff986315bf910248ea1a28ac64de0660ac59f", + "tests/fixtures/detection_core/admission_v2/manifest.json": "c470ccd0d27eaf98de1ac50846c2281d2ec777a96def4211e92051b158317b2b", + "tests/fixtures/detection_core/admission_v2/no_go_replay.json": "a385eee59970fba07d615172524c90038997911f3bd302e56bb23be029e50e6e", + "tests/fixtures/detection_core/admission_v2/no_go_report.json": "3c0d8698bb72157de384fa1ad63c4c1b5bafbd1001235e416aaeced9cb5da8b7", + "tests/fixtures/detection_core/admission_v2/validation_failures.json": ( + "c80a1214c59c26e25db0978c85ce07ced9642baf9dc57ef0a5f0f1504f11c49f" + ), + "tests/fixtures/detection_core/admission_v2/zero_model_replay.json": "c17ab6fc00c5aea7e981a07dbb28aaa9b3da399429948029aaeb1f67eebecfd9", + "tests/fixtures/detection_core/admission_v2/zero_model_report.json": "336f07de37ec15eeaffb6711ce398762c6091d49b7993fd7a38f7cdebf33f85c", +} + + +def _read_frozen_history() -> dict[str, bytes]: + contents: dict[str, bytes] = {} + for relative, expected_digest in FROZEN_HISTORY.items(): + path = REPO / relative + assert path.is_file(), f"historical fixture is missing: {relative}" + content = path.read_bytes() + assert len(content) > 0, f"historical fixture read zero bytes: {relative}" + assert sha256(content).hexdigest() == expected_digest + contents[relative] = content + return contents + + +def test_shipped_facade_satisfies_pinned_v3_startup_contract() -> None: + capability = require_scoped_admission_v3() + + assert capability.schema == V3_SCHEMA + assert capability.operations == ( + "authorize_evaluation", + "decode_evaluation_v3", + "preflight_evaluation", + "score_and_replay", + "validate_evaluation_replay", + "validate_shared_spend", + ) + + +def test_startup_refuses_missing_schema_export() -> None: + values = {name: getattr(evaluation_admission, name) for name in evaluation_admission.__all__} + values["__all__"] = tuple(name for name in evaluation_admission.__all__ if name != "EVALUATION_ADMISSION_V3_SCHEMA") + values.pop("EVALUATION_ADMISSION_V3_SCHEMA") + + with pytest.raises(ScopedAdmissionV3ConformanceError, match="missing"): + require_scoped_admission_v3(SimpleNamespace(**values)) + + +def test_startup_refuses_absent_required_capability() -> None: + values = {name: getattr(evaluation_admission, name) for name in evaluation_admission.__all__} + values["__all__"] = tuple(evaluation_admission.__all__) + values["score_and_replay"] = None + + with pytest.raises(ScopedAdmissionV3ConformanceError, match="required callable capabilities are missing"): + require_scoped_admission_v3(SimpleNamespace(**values)) + + +def test_startup_refuses_changed_schema() -> None: + values = {name: getattr(evaluation_admission, name) for name in evaluation_admission.__all__} + values["__all__"] = tuple(evaluation_admission.__all__) + values["EVALUATION_ADMISSION_V3_SCHEMA"] = "openultrasast.detection-core.evaluation-admission.v4" + + with pytest.raises(ScopedAdmissionV3ConformanceError, match="schema"): + require_scoped_admission_v3(SimpleNamespace(**values)) + + +def test_complete_v3_replay_validates_through_benchmark_consumer( + monkeypatch: pytest.MonkeyPatch, +) -> None: + bundle, replay, *_, scopes = _complete_replay(monkeypatch) + + decoded = decode_scoped_admission_v3(V3_SCHEMA, replay, type(replay)) + reports = validate_scoped_admission_replay( + replay, + bundle[5][0].context, + bundle[5][0].port, + bundle[4], + ) + + assert decoded is replay + assert reports == replay.reports + _assert_conformance(bundle, replay, scopes) + + +def test_history_is_nonzero_readable_frozen_and_never_inferred_as_v3() -> None: + before = _read_frozen_history() + report = json.loads(before["tests/fixtures/detection_core/admission_v2/go_report.json"]) + freeze = json.loads(before["benchmarks/valid/pilot/freeze-valid.json"]) + pool = tomllib.loads(before["benchmarks/valid/pilot/pool-valid.toml"].decode()) + + assert report["schema_version"] == V2_REPORT_SCHEMA + assert freeze["freeze_digest"] + assert pool["status"] == "frozen" + with pytest.raises(evaluation_admission.EvaluationAdmissionV3Error, match="schema|contract|refused"): + decode_scoped_admission_v3(V2_REPORT_SCHEMA, report, object) + with pytest.raises(evaluation_admission.EvaluationAdmissionV3Error, match="contract|refused"): + decode_scoped_admission_v3(V3_SCHEMA, report, object) + with pytest.raises(evaluation_admission.EvaluationAdmissionV3Error, match="schema|contract|refused"): + decode_scoped_admission_v3("unknown-evaluation-root", report, object) + with pytest.raises(evaluation_admission.EvaluationAdmissionV3Error, match="contract|refused"): + decode_scoped_admission_v3(V3_SCHEMA, {"schema_version": V3_SCHEMA}, object) + + assert _read_frozen_history() == before