feat(valid-detection-benchmark): 9.2 remove replacement authority from legacy aggregate scoring

This commit is contained in:
Marius Ciepluch
2026-11-21 13:39:38 +01:00
parent 9ba0a9d4c5
commit 14bf931f01
3 changed files with 376 additions and 14 deletions
+161 -13
View File
@@ -1,33 +1,181 @@
"""Paired scoring and the admission report for the access-control prover arm.
"""Scoring authority for the valid-detection benchmark (task 9.2).
Task 12.6 of the access-control-prover specification (Reqs 5.2, 12.4, 12.5,
12.7, 12.8, 12.9). The scorer consumes a validated admission manifest plus the
judgments measured on its population: it scores paired vulnerable/fixed twins
and reviewed ordinary changes, keeps sample counts and Wilson 95% intervals on
every rate, and returns NO-GO when the budget gate, the instrument gate or any
pre-registered quality criterion fails. Indeterminate pairs are reported as
indeterminate and never converted to clean results; ordinary changes whose
review found a real missing-authorization defect are refused before scoring.
The replacement admission path computes its metrics and gates, validates
cases, proofs, and replays exclusively through the pinned detection-core V3
facade reached via ``benchmarks.valid.core_conformance``; no benchmark-owned
scorer may substitute for that root. The legacy aggregate helpers below are
the frozen access-control prover pilot's paired scorer (task 12.6 of the
access-control-prover specification): they remain available only for
historical V2 replay, never own a replacement decision, and their pooled or
target-specific output is structurally refused as a replacement admission
root before the core boundary is reached.
"""
from __future__ import annotations
from collections.abc import Sequence
from collections.abc import Mapping, Sequence
from dataclasses import dataclass
from typing import Any, Protocol
from enum import StrEnum
from typing import Any, Protocol, TypeVar
from benchmarks.unseen import score
from openultrasast.core import canonical
from benchmarks.valid.core_conformance import decode_scoped_admission_v3, require_scoped_admission_v3
from openultrasast.core import canonical, evaluation_admission
from openultrasast.core.admission_case import AdmissionCaseRelease, AdmissionCaseResult
from openultrasast.core.admission_context import AdmissionValidationContext
from openultrasast.core.admission_report import AdmissionReplay, score_admission, validate_admission_replay
from openultrasast.core.admission_review import AdmissionManifestV2
from openultrasast.core.admission_spend import AdmissionPreparation
from openultrasast.core.custody import PopulationCustodyPort, PopulationUseReceipt
from openultrasast.core.evaluation_authority import EvaluationAdmissionManifestV3, EvaluationPlanManifestV3
from openultrasast.core.evaluation_feasibility import EvaluationFeasibilityV3
from openultrasast.core.evaluation_release import AdmissionCaseReleaseV3, ArmAdmissionCasesV3
from openultrasast.core.evaluation_replay import EvaluationReplayV3
from openultrasast.core.evaluation_report import ArmEvaluationReportV3
from openultrasast.core.evaluation_spend import EvaluationPreparationV3, EvaluationSpendBindingV3
from openultrasast.core.measurement import DenominatorPolicy
from openultrasast.core.readiness import ReadinessResult
__all__ = ["AdmissionCase", "AdmissionScoringError", "OrdinaryChange", "admission_report", "score_v2_admission"]
__all__ = [
"AdmissionCase",
"AdmissionScoringError",
"OrdinaryChange",
"ReplacementScoringRefusal",
"ReplacementScoringRefusalReason",
"V3_EVALUATION_SCHEMA",
"admission_report",
"decode_replacement_root",
"preflight_replacement_gates",
"score_replacement_replay",
"score_v2_admission",
"validate_replacement_replay",
]
V3_EVALUATION_SCHEMA = "openultrasast.detection-core.evaluation-admission.v3"
_RootT = TypeVar("_RootT")
class ReplacementScoringRefusalReason(StrEnum):
"""Closed reasons a root can be refused on the replacement scoring path."""
LEGACY_AGGREGATE_ROOT = "legacy_aggregate_root"
class ReplacementScoringRefusal(ValueError):
"""Refuse non-V3 roots on the replacement path with a closed reason."""
def __init__(self, reason: ReplacementScoringRefusalReason) -> None:
self.reason = reason
super().__init__(f"replacement_scoring_{reason.value}")
_LEGACY_AGGREGATE_KEYS = frozenset(
{
"fix_recall",
"flagged_ordinary_per_100",
"indeterminate_rate",
"verified_location_recall",
}
)
_LEGACY_REPORT_SCHEMA_PREFIX = "openultrasast.detection-core.admission-report."
def _is_legacy_aggregate_root(value: Mapping[object, object]) -> bool:
"""True when a mapping carries the legacy aggregate scorer's vocabulary."""
schema = value.get("schema_version")
return bool(_LEGACY_AGGREGATE_KEYS & set(value)) or (isinstance(schema, str) and schema.startswith(_LEGACY_REPORT_SCHEMA_PREFIX))
def _carries_legacy_aggregate(value: Mapping[object, object]) -> bool:
"""True when a pooled wrapper carries legacy aggregate output inside it."""
for item in value.values():
if isinstance(item, Mapping) and _is_legacy_aggregate_root(item):
return True
if isinstance(item, (list, tuple)) and any(isinstance(entry, Mapping) and _is_legacy_aggregate_root(entry) for entry in item):
return True
return False
def _refuse_legacy_aggregate_root(value: object) -> None:
"""Refuse legacy aggregate output (flat, target-specific, or pooled) as a replacement root."""
if not isinstance(value, Mapping):
return
if _is_legacy_aggregate_root(value) or _carries_legacy_aggregate(value):
raise ReplacementScoringRefusal(ReplacementScoringRefusalReason.LEGACY_AGGREGATE_ROOT)
def decode_replacement_root(schema: str, value: object, expected_type: type[_RootT]) -> _RootT:
"""Decode one replacement admission root through the pinned core facade.
Legacy aggregate scoring output (pooled or target-specific) is refused
here with a closed reason before the core boundary is reached; only an
exact V3 root decodes.
"""
_refuse_legacy_aggregate_root(value)
return decode_scoped_admission_v3(schema, value, expected_type)
def preflight_replacement_gates(
plan_manifest: EvaluationPlanManifestV3,
context: AdmissionValidationContext,
resolver: canonical.ContentResolver,
) -> EvaluationFeasibilityV3:
"""Derive replacement gate and metric feasibility only through the pinned facade."""
require_scoped_admission_v3()
return evaluation_admission.preflight_evaluation(plan_manifest, context, resolver)
def score_replacement_replay(
admission_manifest: EvaluationAdmissionManifestV3,
feasibility: EvaluationFeasibilityV3,
preparation: EvaluationPreparationV3,
receipt: PopulationUseReceipt,
spend_binding: EvaluationSpendBindingV3,
release: AdmissionCaseReleaseV3,
cases_by_arm: tuple[ArmAdmissionCasesV3, ...],
readiness: tuple[ReadinessResult, ...],
context: AdmissionValidationContext,
custody: PopulationCustodyPort,
resolver: canonical.ContentResolver,
) -> EvaluationReplayV3:
"""Replacement metrics, case validation, proof validation, and one replay.
Every argument is an exact core V3 root and the pinned facade derives all
metrics, gates, and the complete replay, so no alternative scorer exists
on this path.
"""
require_scoped_admission_v3()
return evaluation_admission.score_and_replay(
admission_manifest,
feasibility,
preparation,
receipt,
spend_binding,
release,
cases_by_arm,
readiness,
context,
custody,
resolver,
)
def validate_replacement_replay(
replay: EvaluationReplayV3,
context: AdmissionValidationContext,
custody: PopulationCustodyPort,
resolver: canonical.ContentResolver,
) -> tuple[ArmEvaluationReportV3, ...]:
"""Validate one complete replacement replay only through the pinned facade."""
_refuse_legacy_aggregate_root(replay)
require_scoped_admission_v3()
return evaluation_admission.validate_evaluation_replay(replay, context, custody, resolver)
# Frozen historical replay helpers (access-control prover pilot, task 12.6).
# These aggregate helpers never authorize replacement admission: any pooled or
# target-specific output they produce is refused at the replacement roots above.
_JUDGMENTS = ("unguarded", "discharged", "indeterminate")