mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-03 10:18:18 +00:00
feat(detection-core): define per-case outcome projection
This commit is contained in:
@@ -9,6 +9,7 @@ from itertools import product
|
||||
from openultrasast.core import canonical
|
||||
from openultrasast.core._evaluation_measurement_contracts import (
|
||||
ArmScopeKey,
|
||||
CaseOutcomeProjectionPolicyV3,
|
||||
DenominatorPolicyV3,
|
||||
EvaluationMeasurementError,
|
||||
MetricId,
|
||||
@@ -132,6 +133,7 @@ class ArmConfigurationV3:
|
||||
registered_budget_digest: str
|
||||
execution_identity: str
|
||||
outcome_mapping_digest: str
|
||||
case_outcome_policy: CaseOutcomeProjectionPolicyV3
|
||||
combination_rule: CombinationRuleV3 | None
|
||||
configuration_identity_digest: str
|
||||
|
||||
@@ -188,9 +190,16 @@ class ArmConfigurationV3:
|
||||
_text(self.execution_identity, "execution identity")
|
||||
|
||||
def _validate_combination(self) -> None:
|
||||
if self.kind is ArmKindV3.COMPONENT and self.combination_rule is not None:
|
||||
raise EvaluationMeasurementError("component arm must not carry a combination rule")
|
||||
if type(self.case_outcome_policy) is not CaseOutcomeProjectionPolicyV3:
|
||||
raise EvaluationMeasurementError("case outcome policy must use the closed CaseOutcomeProjectionPolicyV3 enum")
|
||||
if self.kind is ArmKindV3.COMPONENT:
|
||||
if self.case_outcome_policy is not CaseOutcomeProjectionPolicyV3.UNANIMOUS_EVIDENCE:
|
||||
raise EvaluationMeasurementError("component arm must use unanimous evidence outcome projection")
|
||||
if self.combination_rule is not None:
|
||||
raise EvaluationMeasurementError("component arm must not carry a combination rule")
|
||||
if self.kind is ArmKindV3.COMBINED:
|
||||
if self.case_outcome_policy is not CaseOutcomeProjectionPolicyV3.DECLARATIVE_COMBINATION_TABLE:
|
||||
raise EvaluationMeasurementError("combined arm must use declarative combination-table outcome projection")
|
||||
if type(self.combination_rule) is not CombinationRuleV3:
|
||||
raise EvaluationMeasurementError("combined arm requires a combination rule")
|
||||
self.combination_rule.__post_init__()
|
||||
@@ -214,6 +223,7 @@ def derive_arm_configuration_identity_digest(value: ArmConfigurationV3) -> str:
|
||||
"registered_budget_digest": value.registered_budget_digest,
|
||||
"execution_identity": value.execution_identity,
|
||||
"outcome_mapping_digest": value.outcome_mapping_digest,
|
||||
"case_outcome_policy": value.case_outcome_policy.value,
|
||||
"combination_rule_digest": None if value.combination_rule is None else value.combination_rule.rule_digest,
|
||||
}
|
||||
return canonical.content_digest(_value("arm_configuration", payload))
|
||||
@@ -225,6 +235,7 @@ __all__ = [
|
||||
"CombinationDecisionRowV3",
|
||||
"CombinationOperatorV3",
|
||||
"CombinationRuleV3",
|
||||
"CaseOutcomeProjectionPolicyV3",
|
||||
"ProofCandidatePolicyV3",
|
||||
"derive_arm_configuration_identity_digest",
|
||||
]
|
||||
|
||||
@@ -114,6 +114,11 @@ class Comparator(Enum):
|
||||
AT_MOST = "at_most"
|
||||
|
||||
|
||||
class CaseOutcomeProjectionPolicyV3(Enum):
|
||||
UNANIMOUS_EVIDENCE = "unanimous_evidence"
|
||||
DECLARATIVE_COMBINATION_TABLE = "declarative_combination_table"
|
||||
|
||||
|
||||
@dataclass(frozen=True, order=True)
|
||||
class ScopeKey:
|
||||
family_id: str
|
||||
@@ -411,6 +416,7 @@ def validate_aggregate_weights(
|
||||
__all__ = [
|
||||
"AggregateWeightV3",
|
||||
"ArmScopeKey",
|
||||
"CaseOutcomeProjectionPolicyV3",
|
||||
"Comparator",
|
||||
"DenominatorKind",
|
||||
"DenominatorPolicyV3",
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
"""Fail-closed per-case outcome projection contracts for evaluation admission V3."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from typing import ClassVar
|
||||
|
||||
from openultrasast.core import canonical
|
||||
from openultrasast.core._evaluation_measurement_contracts import CaseOutcomeProjectionPolicyV3
|
||||
from openultrasast.core.evaluation_version import (
|
||||
EVALUATION_ADMISSION_V3_SCHEMA,
|
||||
EvaluationV3Contract,
|
||||
NonAuthorizingV3,
|
||||
require_exact_v3_contract,
|
||||
)
|
||||
from openultrasast.core.evidence import Decision, EvidencePacket, EvidenceRecord, NeutralOutcome, derive_record_id
|
||||
from openultrasast.core.provenance import ProvenanceStamp
|
||||
|
||||
_DIGEST = re.compile(r"[0-9a-f]{64}")
|
||||
_SCHEMA = "openultrasast.detection-core.evaluation-case-outcome.v3"
|
||||
|
||||
|
||||
class CaseOutcomeProjectionError(ValueError):
|
||||
"""A per-case outcome projection is incomplete, ambiguous, or non-canonical."""
|
||||
|
||||
|
||||
def _digest(value: object, field: str) -> str:
|
||||
if type(value) is not str or _DIGEST.fullmatch(value) is None:
|
||||
raise CaseOutcomeProjectionError(f"{field} must be a full lowercase SHA-256 digest")
|
||||
return value
|
||||
|
||||
|
||||
def _value(kind: str, payload: canonical.JsonValue) -> canonical.JsonValue:
|
||||
return {"schema_version": _SCHEMA, "kind": kind, "payload": payload}
|
||||
|
||||
|
||||
def _bind(value: object, field: str, expected: str) -> None:
|
||||
actual = getattr(value, field)
|
||||
if actual == "":
|
||||
object.__setattr__(value, field, expected)
|
||||
elif _digest(actual, field) != expected:
|
||||
raise CaseOutcomeProjectionError(f"{field} does not match canonical content")
|
||||
|
||||
|
||||
def _source_digests(value: object, policy: CaseOutcomeProjectionPolicyV3) -> tuple[str, ...]:
|
||||
if type(value) is not tuple or not value:
|
||||
raise CaseOutcomeProjectionError("source outcome digests must be a nonempty exact tuple")
|
||||
for item in value:
|
||||
_digest(item, "source outcome digest")
|
||||
if policy is CaseOutcomeProjectionPolicyV3.UNANIMOUS_EVIDENCE:
|
||||
if len(set(value)) != len(value):
|
||||
raise CaseOutcomeProjectionError("unanimous evidence source outcome digests must be unique")
|
||||
if value != tuple(sorted(value)):
|
||||
raise CaseOutcomeProjectionError("unanimous evidence source outcome digests must use canonical order")
|
||||
return value
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CaseOutcomeProjectionV3(EvaluationV3Contract, NonAuthorizingV3):
|
||||
"""One digest-bound neutral outcome for one authenticated case input."""
|
||||
|
||||
__evaluation_v3_contract_schema__: ClassVar[str] = EVALUATION_ADMISSION_V3_SCHEMA
|
||||
case_input_digest: str
|
||||
policy: CaseOutcomeProjectionPolicyV3
|
||||
source_outcome_digests: tuple[str, ...]
|
||||
normalized_outcome: NeutralOutcome
|
||||
projection_digest: str
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
require_exact_v3_contract(self, CaseOutcomeProjectionV3, field="case outcome projection")
|
||||
_digest(self.case_input_digest, "case input digest")
|
||||
if type(self.policy) is not CaseOutcomeProjectionPolicyV3:
|
||||
raise CaseOutcomeProjectionError("projection policy must use exact CaseOutcomeProjectionPolicyV3")
|
||||
sources = _source_digests(self.source_outcome_digests, self.policy)
|
||||
if type(self.normalized_outcome) is not NeutralOutcome:
|
||||
raise CaseOutcomeProjectionError("normalized outcome must use exact NeutralOutcome")
|
||||
payload: canonical.JsonValue = {
|
||||
"case_input_digest": self.case_input_digest,
|
||||
"policy": self.policy.value,
|
||||
"source_outcome_digests": sources,
|
||||
"normalized_outcome": self.normalized_outcome.value,
|
||||
}
|
||||
_bind(self, "projection_digest", canonical.content_digest(_value("case_outcome_projection", payload)))
|
||||
|
||||
|
||||
def _canonical_records(records: object) -> tuple[EvidenceRecord, ...]:
|
||||
if type(records) is not tuple or not records:
|
||||
raise CaseOutcomeProjectionError("component evidence records must be a nonempty exact tuple")
|
||||
if any(type(record) is not EvidenceRecord for record in records):
|
||||
raise CaseOutcomeProjectionError("component evidence records must contain exact EvidenceRecord values")
|
||||
validated = records
|
||||
for record in validated:
|
||||
try:
|
||||
if type(record.packet) is not EvidencePacket:
|
||||
raise CaseOutcomeProjectionError("evidence record packet must use exact EvidencePacket")
|
||||
if type(record.decision) is not Decision:
|
||||
raise CaseOutcomeProjectionError("evidence record decision must use exact Decision")
|
||||
if type(record.packet.provenance) is not ProvenanceStamp:
|
||||
raise CaseOutcomeProjectionError("evidence record provenance change input digest must use exact ProvenanceStamp")
|
||||
_digest(record.packet.provenance.change_input_digest, "change input digest")
|
||||
record.packet.provenance.__post_init__()
|
||||
record.packet.__post_init__()
|
||||
record.decision.__post_init__()
|
||||
record.__post_init__()
|
||||
expected_record_id = derive_record_id(record.packet, record.decision)
|
||||
except CaseOutcomeProjectionError:
|
||||
raise
|
||||
except (AttributeError, TypeError, ValueError) as exc:
|
||||
raise CaseOutcomeProjectionError("component evidence record cannot be revalidated") from exc
|
||||
if record.record_id != expected_record_id:
|
||||
raise CaseOutcomeProjectionError("evidence record identity cannot be re-derived")
|
||||
identities = tuple(record.record_id for record in validated)
|
||||
if len(set(identities)) != len(identities):
|
||||
raise CaseOutcomeProjectionError("component evidence record identities must be unique")
|
||||
return tuple(sorted(validated, key=lambda item: item.record_id))
|
||||
|
||||
|
||||
def derive_case_input_digest(records: tuple[EvidenceRecord, ...]) -> str:
|
||||
"""Derive correlation identity from exact, already trust-validated evidence records."""
|
||||
|
||||
validated = _canonical_records(records)
|
||||
change_input_digests = tuple(sorted({record.packet.provenance.change_input_digest for record in validated}))
|
||||
payload: canonical.JsonValue = {"change_input_digests": change_input_digests}
|
||||
return canonical.content_digest(_value("case_input", payload))
|
||||
|
||||
|
||||
def derive_component_case_outcome_projection(
|
||||
records: tuple[EvidenceRecord, ...],
|
||||
) -> CaseOutcomeProjectionV3:
|
||||
"""Project exact trust-validated component records only when every outcome agrees."""
|
||||
|
||||
validated = _canonical_records(records)
|
||||
outcomes = {record.decision.outcome for record in validated}
|
||||
if len(outcomes) != 1:
|
||||
raise CaseOutcomeProjectionError("component evidence records must carry the same neutral outcome")
|
||||
outcome = next(iter(outcomes))
|
||||
return CaseOutcomeProjectionV3(
|
||||
derive_case_input_digest(validated),
|
||||
CaseOutcomeProjectionPolicyV3.UNANIMOUS_EVIDENCE,
|
||||
tuple(record.record_id for record in validated),
|
||||
outcome,
|
||||
"",
|
||||
)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"CaseOutcomeProjectionError",
|
||||
"CaseOutcomeProjectionPolicyV3",
|
||||
"CaseOutcomeProjectionV3",
|
||||
"derive_case_input_digest",
|
||||
"derive_component_case_outcome_projection",
|
||||
]
|
||||
@@ -3,6 +3,7 @@
|
||||
from openultrasast.core._evaluation_arm_configuration import (
|
||||
ArmConfigurationV3,
|
||||
ArmKindV3,
|
||||
CaseOutcomeProjectionPolicyV3,
|
||||
CombinationDecisionRowV3,
|
||||
CombinationOperatorV3,
|
||||
CombinationRuleV3,
|
||||
@@ -37,6 +38,7 @@ __all__ = [
|
||||
"ArmConfigurationV3",
|
||||
"ArmKindV3",
|
||||
"ArmScopeKey",
|
||||
"CaseOutcomeProjectionPolicyV3",
|
||||
"CombinationDecisionRowV3",
|
||||
"CombinationOperatorV3",
|
||||
"CombinationRuleV3",
|
||||
|
||||
Reference in New Issue
Block a user