mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-04 02:38:18 +00:00
feat(valid-detection-benchmark): register immutable evaluation arms
This commit is contained in:
@@ -162,7 +162,7 @@
|
|||||||
- _Depends: 5.3_
|
- _Depends: 5.3_
|
||||||
|
|
||||||
- [ ] 6. Register and execute immutable label-blind arms
|
- [ ] 6. Register and execute immutable label-blind arms
|
||||||
- [ ] 6.1 Register exact arm identities, scopes, policies, and lifecycle state
|
- [x] 6.1 Register exact arm identities, scopes, policies, and lifecycle state
|
||||||
- Bind shipped, experimental, and combined arms separately to immutable analyzer, configuration, fact, vocabulary, outcome, baseline, denominator, threshold, sufficiency, and claimed-scope identities.
|
- Bind shipped, experimental, and combined arms separately to immutable analyzer, configuration, fact, vocabulary, outcome, baseline, denominator, threshold, sufficiency, and claimed-scope identities.
|
||||||
- Accept only active arms unless a parked arm carries a separate reviewed strategy-decision reference; provide no such reference for access control.
|
- Accept only active arms unless a parked arm carries a separate reviewed strategy-decision reference; provide no such reference for access control.
|
||||||
- Produce one canonical evaluation plan covering every component and combined arm before label access.
|
- Produce one canonical evaluation plan covering every component and combined arm before label access.
|
||||||
@@ -429,3 +429,4 @@
|
|||||||
- Task 5.2 rebuilds exact caller-selected durable projection roots and ordered members from the object store alone; canonical root-envelope binding, mixed-access population recovery, and explicit optional expiry prevent forged authority, latest-value selection, and unintended terminal-artifact expiration.
|
- Task 5.2 rebuilds exact caller-selected durable projection roots and ordered members from the object store alone; canonical root-envelope binding, mixed-access population recovery, and explicit optional expiry prevent forged authority, latest-value selection, and unintended terminal-artifact expiration.
|
||||||
- Task 5.3 gates real-population admission on a canonical report bound to exact store namespaces and a typed retention profile; negative conditional-write proofs, distinct visible versions, bound pre-deadline current/noncurrent evidence, and closed handling of null IDs, delete markers, and provider ambiguity prevent plausible-but-unsupported capability passes.
|
- Task 5.3 gates real-population admission on a canonical report bound to exact store namespaces and a typed retention profile; negative conditional-write proofs, distinct visible versions, bound pre-deadline current/noncurrent evidence, and closed handling of null IDs, delete markers, and provider ambiguity prevent plausible-but-unsupported capability passes.
|
||||||
- Task 5.4 persists one canonical AVAILABLE head and permits only its direct AVAILABLE-to-SPENT successor through capability-gated conditional writes; domain digests govern state while advancing version and ETag tokens prove storage concurrency and exact rereads before any caller may expose labels.
|
- Task 5.4 persists one canonical AVAILABLE head and permits only its direct AVAILABLE-to-SPENT successor through capability-gated conditional writes; domain digests govern state while advancing version and ETag tokens prove storage concurrency and exact rereads before any caller may expose labels.
|
||||||
|
- Task 6.1 delegates plan and baseline algebra exclusively to detection-core V3 while binding separate arm roles and lifecycle identity; every parked-arm factory, validator, and serializer path re-resolves an exact independently reviewed strategy-decision authority, and access control remains unconditionally excluded.
|
||||||
|
|||||||
@@ -0,0 +1,299 @@
|
|||||||
|
"""Immutable label-blind arm registrations over detection-core V3 plans."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from enum import StrEnum
|
||||||
|
|
||||||
|
from benchmarks.valid.arm_strategy_decision import (
|
||||||
|
ArmRegistryError,
|
||||||
|
StrategyDecisionAuthorityV1,
|
||||||
|
StrategyDecisionDisposition,
|
||||||
|
review_strategy_decision_authority,
|
||||||
|
strategy_decision_artifact_bytes,
|
||||||
|
validate_strategy_decision_authority,
|
||||||
|
)
|
||||||
|
from openultrasast.core import canonical
|
||||||
|
from openultrasast.core.canonical import JsonValue
|
||||||
|
from openultrasast.core.evaluation_measurement import ArmConfigurationV3, ArmKindV3
|
||||||
|
from openultrasast.core.evaluation_plan import (
|
||||||
|
EvaluationBaselineIdentityV3,
|
||||||
|
EvaluationPlanDeclarationV3,
|
||||||
|
create_evaluation_plan_declaration,
|
||||||
|
derive_evaluation_baselines,
|
||||||
|
validate_evaluation_baselines,
|
||||||
|
)
|
||||||
|
from openultrasast.core.factpack import ReviewIndependencePolicy
|
||||||
|
|
||||||
|
ARM_REGISTRATION_SCHEMA = "openultrasast.valid-detection-benchmark.arm-registration.v1"
|
||||||
|
REGISTERED_PLAN_SCHEMA = "openultrasast.valid-detection-benchmark.registered-evaluation-plan.v1"
|
||||||
|
_DIGEST = re.compile(r"[0-9a-f]{64}")
|
||||||
|
_ROLE_ORDER: dict[ArmRole, int]
|
||||||
|
|
||||||
|
|
||||||
|
class ArmRole(StrEnum):
|
||||||
|
"""Benchmark-visible arm roles that must remain separately reportable."""
|
||||||
|
|
||||||
|
SHIPPED = "shipped"
|
||||||
|
EXPERIMENTAL = "experimental"
|
||||||
|
COMBINED = "combined"
|
||||||
|
|
||||||
|
|
||||||
|
class ArmLifecycleState(StrEnum):
|
||||||
|
"""Closed lifecycle state consumed by the benchmark registry."""
|
||||||
|
|
||||||
|
ACTIVE = "active"
|
||||||
|
PARKED = "parked"
|
||||||
|
|
||||||
|
|
||||||
|
_ROLE_ORDER = {ArmRole.SHIPPED: 0, ArmRole.EXPERIMENTAL: 1, ArmRole.COMBINED: 2}
|
||||||
|
|
||||||
|
|
||||||
|
def _digest(value: object, field: str) -> str:
|
||||||
|
if type(value) is not str or _DIGEST.fullmatch(value) is None:
|
||||||
|
raise ArmRegistryError(f"{field} must be a full lowercase SHA-256 digest")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def _registration_value(registration: ArmRegistrationV1, *, include_digest: bool) -> dict[str, JsonValue]:
|
||||||
|
value: dict[str, JsonValue] = {
|
||||||
|
"schema_version": ARM_REGISTRATION_SCHEMA,
|
||||||
|
"role": registration.role.value,
|
||||||
|
"lifecycle_state": registration.lifecycle_state.value,
|
||||||
|
"strategy_decision_authority_digest": None
|
||||||
|
if registration.strategy_decision_authority is None
|
||||||
|
else registration.strategy_decision_authority.authority_digest,
|
||||||
|
"arm_id": registration.configuration.arm_id,
|
||||||
|
"configuration_identity_digest": registration.configuration.configuration_identity_digest,
|
||||||
|
}
|
||||||
|
if include_digest:
|
||||||
|
value["registration_digest"] = registration.registration_digest
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class ArmRegistrationV1:
|
||||||
|
"""One lifecycle-qualified role around an exact core V3 configuration."""
|
||||||
|
|
||||||
|
role: ArmRole
|
||||||
|
lifecycle_state: ArmLifecycleState
|
||||||
|
strategy_decision_authority: StrategyDecisionAuthorityV1 | None
|
||||||
|
configuration: ArmConfigurationV3
|
||||||
|
registration_digest: str
|
||||||
|
|
||||||
|
@property
|
||||||
|
def arm_id(self) -> str:
|
||||||
|
return self.configuration.arm_id
|
||||||
|
|
||||||
|
def __post_init__(self) -> None:
|
||||||
|
if type(self) is not ArmRegistrationV1:
|
||||||
|
raise ArmRegistryError("arm registration must use exact ArmRegistrationV1")
|
||||||
|
if type(self.role) is not ArmRole:
|
||||||
|
raise ArmRegistryError("arm role must use the closed ArmRole enum")
|
||||||
|
if type(self.lifecycle_state) is not ArmLifecycleState:
|
||||||
|
raise ArmRegistryError("lifecycle state must use the closed ArmLifecycleState enum")
|
||||||
|
if type(self.configuration) is not ArmConfigurationV3:
|
||||||
|
raise ArmRegistryError("configuration must use exact ArmConfigurationV3")
|
||||||
|
self.configuration.__post_init__()
|
||||||
|
_validate_role_kind(self.role, self.configuration.kind)
|
||||||
|
_validate_strategy_decision(self.lifecycle_state, self.strategy_decision_authority)
|
||||||
|
expected = canonical.content_digest(_registration_value(self, include_digest=False))
|
||||||
|
if self.registration_digest == "":
|
||||||
|
object.__setattr__(self, "registration_digest", expected)
|
||||||
|
elif _digest(self.registration_digest, "registration digest") != expected:
|
||||||
|
raise ArmRegistryError("registration digest does not match canonical content")
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_role_kind(role: ArmRole, kind: ArmKindV3) -> None:
|
||||||
|
if role is ArmRole.COMBINED and kind is not ArmKindV3.COMBINED:
|
||||||
|
raise ArmRegistryError("combined role requires a combined core V3 configuration")
|
||||||
|
if role is not ArmRole.COMBINED and kind is not ArmKindV3.COMPONENT:
|
||||||
|
raise ArmRegistryError("shipped and experimental roles require component core V3 configurations")
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_strategy_decision(state: ArmLifecycleState, authority: StrategyDecisionAuthorityV1 | None) -> None:
|
||||||
|
if state is ArmLifecycleState.ACTIVE:
|
||||||
|
if authority is not None:
|
||||||
|
raise ArmRegistryError("active arm must not carry a strategy-decision reference")
|
||||||
|
return
|
||||||
|
if type(authority) is not StrategyDecisionAuthorityV1:
|
||||||
|
raise ArmRegistryError("parked arm requires an exact reviewed strategy-decision authority")
|
||||||
|
authority.__post_init__()
|
||||||
|
|
||||||
|
|
||||||
|
def _registration_key(registration: ArmRegistrationV1) -> tuple[int, str]:
|
||||||
|
return (_ROLE_ORDER[registration.role], registration.arm_id)
|
||||||
|
|
||||||
|
|
||||||
|
def _canonical_registrations(value: object) -> tuple[ArmRegistrationV1, ...]:
|
||||||
|
if type(value) is not tuple or not value or any(type(row) is not ArmRegistrationV1 for row in value):
|
||||||
|
raise ArmRegistryError("registrations must be a nonempty tuple of exact ArmRegistrationV1 values")
|
||||||
|
rows = value
|
||||||
|
for row in rows:
|
||||||
|
row.__post_init__()
|
||||||
|
if rows != tuple(sorted(rows, key=_registration_key)):
|
||||||
|
raise ArmRegistryError("registrations must use canonical role and arm order")
|
||||||
|
if len({row.arm_id for row in rows}) != len(rows):
|
||||||
|
raise ArmRegistryError("registrations must use unique arm identities")
|
||||||
|
return rows
|
||||||
|
|
||||||
|
|
||||||
|
def _refuses_parked_access_control(registrations: tuple[ArmRegistrationV1, ...]) -> None:
|
||||||
|
for registration in registrations:
|
||||||
|
if any(scope.family_id == "access_control" for scope in registration.configuration.exact_scopes):
|
||||||
|
raise ArmRegistryError("the access-control arm remains parked without a strategy decision supplied by this spec")
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_parked_authorities(
|
||||||
|
registrations: tuple[ArmRegistrationV1, ...],
|
||||||
|
policy: ReviewIndependencePolicy | None,
|
||||||
|
resolver: canonical.ContentResolver | None,
|
||||||
|
) -> None:
|
||||||
|
parked = tuple(row for row in registrations if row.lifecycle_state is ArmLifecycleState.PARKED)
|
||||||
|
if not parked:
|
||||||
|
return
|
||||||
|
if type(policy) is not ReviewIndependencePolicy or resolver is None:
|
||||||
|
raise ArmRegistryError("parked arms require an exact strategy review policy and resolver")
|
||||||
|
for registration in parked:
|
||||||
|
authority = registration.strategy_decision_authority
|
||||||
|
if type(authority) is not StrategyDecisionAuthorityV1:
|
||||||
|
raise ArmRegistryError("parked arm strategy authority is missing")
|
||||||
|
validate_strategy_decision_authority(authority, registration.configuration, policy, resolver)
|
||||||
|
|
||||||
|
|
||||||
|
def _plan_value(plan: RegisteredEvaluationPlanV1, *, include_digest: bool) -> dict[str, JsonValue]:
|
||||||
|
value: dict[str, JsonValue] = {
|
||||||
|
"schema_version": REGISTERED_PLAN_SCHEMA,
|
||||||
|
"registration_digests": tuple(row.registration_digest for row in plan.registrations),
|
||||||
|
"declaration_digest": plan.declaration.declaration_digest,
|
||||||
|
"baseline_digests": tuple(row.baseline_digest for row in plan.baselines),
|
||||||
|
}
|
||||||
|
if include_digest:
|
||||||
|
value["plan_digest"] = plan.plan_digest
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class RegisteredEvaluationPlanV1:
|
||||||
|
"""Structural benchmark plan; parked-arm acceptance requires revalidation."""
|
||||||
|
|
||||||
|
registrations: tuple[ArmRegistrationV1, ...]
|
||||||
|
declaration: EvaluationPlanDeclarationV3
|
||||||
|
baselines: tuple[EvaluationBaselineIdentityV3, ...]
|
||||||
|
plan_digest: str
|
||||||
|
|
||||||
|
def __post_init__(self) -> None:
|
||||||
|
if type(self) is not RegisteredEvaluationPlanV1:
|
||||||
|
raise ArmRegistryError("registered plan must use exact RegisteredEvaluationPlanV1")
|
||||||
|
registrations = _canonical_registrations(self.registrations)
|
||||||
|
_refuses_parked_access_control(registrations)
|
||||||
|
if type(self.declaration) is not EvaluationPlanDeclarationV3:
|
||||||
|
raise ArmRegistryError("declaration must use exact EvaluationPlanDeclarationV3")
|
||||||
|
self.declaration.__post_init__()
|
||||||
|
registered_ids = tuple(sorted(row.arm_id for row in registrations))
|
||||||
|
declared_ids = tuple(row.arm_id for row in self.declaration.arm_configurations)
|
||||||
|
if declared_ids != registered_ids:
|
||||||
|
raise ArmRegistryError("declaration arm identities do not equal registered arms")
|
||||||
|
if tuple(row.configuration for row in registrations) != _configurations_by_registration(self.declaration, registrations):
|
||||||
|
raise ArmRegistryError("declaration configurations do not match registered arms")
|
||||||
|
validate_evaluation_baselines(self.declaration, self.baselines)
|
||||||
|
expected = canonical.content_digest(_plan_value(self, include_digest=False))
|
||||||
|
if self.plan_digest == "":
|
||||||
|
object.__setattr__(self, "plan_digest", expected)
|
||||||
|
elif _digest(self.plan_digest, "plan digest") != expected:
|
||||||
|
raise ArmRegistryError("plan digest does not match canonical content")
|
||||||
|
|
||||||
|
|
||||||
|
def _configurations_by_registration(
|
||||||
|
declaration: EvaluationPlanDeclarationV3,
|
||||||
|
registrations: tuple[ArmRegistrationV1, ...],
|
||||||
|
) -> tuple[ArmConfigurationV3, ...]:
|
||||||
|
configurations = {row.arm_id: row for row in declaration.arm_configurations}
|
||||||
|
try:
|
||||||
|
return tuple(configurations[row.arm_id] for row in registrations)
|
||||||
|
except KeyError as exc:
|
||||||
|
raise ArmRegistryError("declaration is missing a registered arm") from exc
|
||||||
|
|
||||||
|
|
||||||
|
def register_evaluation_plan(
|
||||||
|
*,
|
||||||
|
registrations: tuple[ArmRegistrationV1, ...],
|
||||||
|
population_digest: str,
|
||||||
|
freeze_digest: str,
|
||||||
|
input_set_digest: str,
|
||||||
|
input_identity_digests: tuple[str, ...],
|
||||||
|
strategy_review_policy: ReviewIndependencePolicy | None = None,
|
||||||
|
strategy_resolver: canonical.ContentResolver | None = None,
|
||||||
|
) -> RegisteredEvaluationPlanV1:
|
||||||
|
"""Canonicalize registry order and derive the only accepted core V3 plan."""
|
||||||
|
if type(registrations) is not tuple or not registrations:
|
||||||
|
raise ArmRegistryError("registrations must be a nonempty tuple")
|
||||||
|
if any(type(row) is not ArmRegistrationV1 for row in registrations):
|
||||||
|
raise ArmRegistryError("registrations must use exact ArmRegistrationV1 values")
|
||||||
|
canonical_registrations = tuple(sorted(registrations, key=_registration_key))
|
||||||
|
_canonical_registrations(canonical_registrations)
|
||||||
|
_refuses_parked_access_control(canonical_registrations)
|
||||||
|
declaration = create_evaluation_plan_declaration(
|
||||||
|
population_digest=population_digest,
|
||||||
|
freeze_digest=freeze_digest,
|
||||||
|
input_set_digest=input_set_digest,
|
||||||
|
arm_configurations=tuple(row.configuration for row in canonical_registrations),
|
||||||
|
input_identity_digests=input_identity_digests,
|
||||||
|
)
|
||||||
|
baselines = derive_evaluation_baselines(declaration)
|
||||||
|
plan = RegisteredEvaluationPlanV1(canonical_registrations, declaration, baselines, "")
|
||||||
|
validate_registered_evaluation_plan(
|
||||||
|
plan,
|
||||||
|
strategy_review_policy=strategy_review_policy,
|
||||||
|
strategy_resolver=strategy_resolver,
|
||||||
|
)
|
||||||
|
return plan
|
||||||
|
|
||||||
|
|
||||||
|
def validate_registered_evaluation_plan(
|
||||||
|
plan: RegisteredEvaluationPlanV1,
|
||||||
|
*,
|
||||||
|
strategy_review_policy: ReviewIndependencePolicy | None = None,
|
||||||
|
strategy_resolver: canonical.ContentResolver | None = None,
|
||||||
|
) -> None:
|
||||||
|
"""Revalidate every parked-arm authority before accepting a structural plan."""
|
||||||
|
if type(plan) is not RegisteredEvaluationPlanV1:
|
||||||
|
raise ArmRegistryError("plan must use exact RegisteredEvaluationPlanV1")
|
||||||
|
plan.__post_init__()
|
||||||
|
_refuses_parked_access_control(plan.registrations)
|
||||||
|
_validate_parked_authorities(plan.registrations, strategy_review_policy, strategy_resolver)
|
||||||
|
|
||||||
|
|
||||||
|
def registered_evaluation_plan_bytes(
|
||||||
|
plan: RegisteredEvaluationPlanV1,
|
||||||
|
*,
|
||||||
|
strategy_review_policy: ReviewIndependencePolicy | None = None,
|
||||||
|
strategy_resolver: canonical.ContentResolver | None = None,
|
||||||
|
) -> bytes:
|
||||||
|
"""Return canonical bytes after resolving every parked-arm authority."""
|
||||||
|
validate_registered_evaluation_plan(
|
||||||
|
plan,
|
||||||
|
strategy_review_policy=strategy_review_policy,
|
||||||
|
strategy_resolver=strategy_resolver,
|
||||||
|
)
|
||||||
|
return canonical.canonical_bytes(_plan_value(plan, include_digest=True))
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"ARM_REGISTRATION_SCHEMA",
|
||||||
|
"REGISTERED_PLAN_SCHEMA",
|
||||||
|
"ArmLifecycleState",
|
||||||
|
"ArmRegistrationV1",
|
||||||
|
"ArmRegistryError",
|
||||||
|
"ArmRole",
|
||||||
|
"RegisteredEvaluationPlanV1",
|
||||||
|
"StrategyDecisionAuthorityV1",
|
||||||
|
"StrategyDecisionDisposition",
|
||||||
|
"register_evaluation_plan",
|
||||||
|
"review_strategy_decision_authority",
|
||||||
|
"registered_evaluation_plan_bytes",
|
||||||
|
"strategy_decision_artifact_bytes",
|
||||||
|
"validate_registered_evaluation_plan",
|
||||||
|
"validate_strategy_decision_authority",
|
||||||
|
]
|
||||||
@@ -0,0 +1,279 @@
|
|||||||
|
"""Reviewed strategy-decision authority for parked benchmark arms."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from enum import StrEnum
|
||||||
|
|
||||||
|
from openultrasast.core import canonical
|
||||||
|
from openultrasast.core.canonical import ContentRef, JsonValue
|
||||||
|
from openultrasast.core.evaluation_measurement import ArmConfigurationV3
|
||||||
|
from openultrasast.core.factpack import (
|
||||||
|
FactPackValidationError,
|
||||||
|
IndependenceAttestation,
|
||||||
|
ReviewIndependencePolicy,
|
||||||
|
derive_independence_attestation_digest,
|
||||||
|
derive_review_independence_policy_digest,
|
||||||
|
)
|
||||||
|
|
||||||
|
STRATEGY_DECISION_SCHEMA = "openultrasast.valid-detection-benchmark.strategy-decision.v1"
|
||||||
|
STRATEGY_DECISION_AUTHORITY_SCHEMA = "openultrasast.valid-detection-benchmark.strategy-decision-authority.v1"
|
||||||
|
_DIGEST = re.compile(r"[0-9a-f]{64}")
|
||||||
|
|
||||||
|
|
||||||
|
class ArmRegistryError(ValueError):
|
||||||
|
"""An arm registration or strategy-decision authority is invalid."""
|
||||||
|
|
||||||
|
|
||||||
|
class StrategyDecisionDisposition(StrEnum):
|
||||||
|
"""The only strategy decision that can admit a parked arm to a plan."""
|
||||||
|
|
||||||
|
ADMIT_TO_PLAN = "admit_to_plan"
|
||||||
|
|
||||||
|
|
||||||
|
def _digest(value: object, field: str) -> str:
|
||||||
|
if type(value) is not str or _DIGEST.fullmatch(value) is None:
|
||||||
|
raise ArmRegistryError(f"{field} must be a full lowercase SHA-256 digest")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def _reference_value(reference: ContentRef) -> dict[str, JsonValue]:
|
||||||
|
return {
|
||||||
|
"artifact_id": reference.artifact_id,
|
||||||
|
"content_digest": reference.content_digest,
|
||||||
|
"media_type": reference.media_type,
|
||||||
|
"byte_length": reference.byte_length,
|
||||||
|
"retention_policy_digest": reference.retention_policy_digest,
|
||||||
|
"storage_receipt_digest": reference.storage_receipt_digest,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _strategy_decision_value(
|
||||||
|
arm_id: str,
|
||||||
|
configuration_identity_digest: str,
|
||||||
|
decision: StrategyDecisionDisposition,
|
||||||
|
) -> dict[str, JsonValue]:
|
||||||
|
return {
|
||||||
|
"schema_version": STRATEGY_DECISION_SCHEMA,
|
||||||
|
"kind": "reviewed_strategy_decision",
|
||||||
|
"arm_id": arm_id,
|
||||||
|
"configuration_identity_digest": configuration_identity_digest,
|
||||||
|
"decision": decision.value,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def strategy_decision_artifact_bytes(
|
||||||
|
arm_id: str,
|
||||||
|
configuration_identity_digest: str,
|
||||||
|
decision: StrategyDecisionDisposition,
|
||||||
|
) -> bytes:
|
||||||
|
"""Create the exact review subject for one parked-arm lifecycle decision."""
|
||||||
|
if type(arm_id) is not str or not arm_id:
|
||||||
|
raise ArmRegistryError("strategy decision arm identity must be nonempty text")
|
||||||
|
_digest(configuration_identity_digest, "strategy decision configuration identity")
|
||||||
|
if type(decision) is not StrategyDecisionDisposition:
|
||||||
|
raise ArmRegistryError("strategy decision must use the closed disposition enum")
|
||||||
|
return canonical.canonical_bytes(_strategy_decision_value(arm_id, configuration_identity_digest, decision))
|
||||||
|
|
||||||
|
|
||||||
|
def _review_value(review: IndependenceAttestation) -> dict[str, JsonValue]:
|
||||||
|
return {
|
||||||
|
"subject_digest": review.subject_digest,
|
||||||
|
"producer_identity": review.producer_identity,
|
||||||
|
"reviewer_identity": review.reviewer_identity,
|
||||||
|
"conflict_disclosure": _reference_value(review.conflict_disclosure),
|
||||||
|
"attestation_digest": review.attestation_digest,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _authority_value(authority: StrategyDecisionAuthorityV1, *, include_digest: bool) -> dict[str, JsonValue]:
|
||||||
|
value: dict[str, JsonValue] = {
|
||||||
|
"schema_version": STRATEGY_DECISION_AUTHORITY_SCHEMA,
|
||||||
|
"arm_id": authority.arm_id,
|
||||||
|
"configuration_identity_digest": authority.configuration_identity_digest,
|
||||||
|
"decision": authority.decision.value,
|
||||||
|
"decision_ref": _reference_value(authority.decision_ref),
|
||||||
|
"review": _review_value(authority.review),
|
||||||
|
"review_policy_digest": authority.review_policy_digest,
|
||||||
|
}
|
||||||
|
if include_digest:
|
||||||
|
value["authority_digest"] = authority.authority_digest
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class StrategyDecisionAuthorityV1:
|
||||||
|
"""Independently reviewed authority over one exact parked-arm configuration."""
|
||||||
|
|
||||||
|
arm_id: str
|
||||||
|
configuration_identity_digest: str
|
||||||
|
decision: StrategyDecisionDisposition
|
||||||
|
decision_ref: ContentRef
|
||||||
|
review: IndependenceAttestation
|
||||||
|
review_policy_digest: str
|
||||||
|
authority_digest: str
|
||||||
|
|
||||||
|
def __post_init__(self) -> None:
|
||||||
|
if type(self) is not StrategyDecisionAuthorityV1:
|
||||||
|
raise ArmRegistryError("strategy decision authority must use exact StrategyDecisionAuthorityV1")
|
||||||
|
if type(self.arm_id) is not str or not self.arm_id:
|
||||||
|
raise ArmRegistryError("strategy decision arm identity must be nonempty text")
|
||||||
|
_digest(self.configuration_identity_digest, "strategy decision configuration identity")
|
||||||
|
if type(self.decision) is not StrategyDecisionDisposition:
|
||||||
|
raise ArmRegistryError("strategy decision must use the closed disposition enum")
|
||||||
|
if type(self.decision_ref) is not ContentRef or type(self.review) is not IndependenceAttestation:
|
||||||
|
raise ArmRegistryError("strategy decision must use exact reference and review contracts")
|
||||||
|
_digest(self.review_policy_digest, "strategy decision review policy digest")
|
||||||
|
expected = canonical.content_digest(_authority_value(self, include_digest=False))
|
||||||
|
if self.authority_digest == "":
|
||||||
|
object.__setattr__(self, "authority_digest", expected)
|
||||||
|
elif _digest(self.authority_digest, "strategy decision authority digest") != expected:
|
||||||
|
raise ArmRegistryError("strategy decision authority digest does not match canonical content")
|
||||||
|
|
||||||
|
|
||||||
|
def _validated_review_policy(policy: object) -> ReviewIndependencePolicy:
|
||||||
|
if type(policy) is not ReviewIndependencePolicy:
|
||||||
|
raise ArmRegistryError("strategy review policy must use exact ReviewIndependencePolicy")
|
||||||
|
try:
|
||||||
|
policy.__post_init__()
|
||||||
|
expected = derive_review_independence_policy_digest(
|
||||||
|
policy.reviewed_reviewer_digests,
|
||||||
|
policy.forbidden_identity_pairs,
|
||||||
|
policy.trusted_store_identities,
|
||||||
|
policy.maximum_evidence_bytes,
|
||||||
|
)
|
||||||
|
except (FactPackValidationError, TypeError, ValueError):
|
||||||
|
raise ArmRegistryError("strategy review policy is invalid") from None
|
||||||
|
if policy.policy_digest != expected:
|
||||||
|
raise ArmRegistryError("strategy review policy digest cannot be re-derived")
|
||||||
|
return policy
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_nonempty(
|
||||||
|
reference: ContentRef,
|
||||||
|
policy: ReviewIndependencePolicy,
|
||||||
|
resolver: canonical.ContentResolver,
|
||||||
|
field: str,
|
||||||
|
) -> bytes:
|
||||||
|
failed = False
|
||||||
|
try:
|
||||||
|
content = canonical.resolve_content(
|
||||||
|
reference,
|
||||||
|
resolver,
|
||||||
|
trusted_store_identities=policy.trusted_store_identities,
|
||||||
|
maximum_bytes=policy.maximum_evidence_bytes,
|
||||||
|
)
|
||||||
|
except (AttributeError, TypeError, ValueError, LookupError):
|
||||||
|
content = b""
|
||||||
|
failed = True
|
||||||
|
if failed or not content:
|
||||||
|
raise ArmRegistryError(f"{field} cannot be resolved")
|
||||||
|
return content
|
||||||
|
|
||||||
|
|
||||||
|
def _decode_strategy_decision(content: bytes) -> dict[str, object]:
|
||||||
|
try:
|
||||||
|
decoded = json.loads(content.decode("utf-8"))
|
||||||
|
except (UnicodeDecodeError, json.JSONDecodeError):
|
||||||
|
decoded = None
|
||||||
|
if type(decoded) is not dict:
|
||||||
|
raise ArmRegistryError("strategy decision artifact must be a canonical object")
|
||||||
|
expected_keys = {"schema_version", "kind", "arm_id", "configuration_identity_digest", "decision"}
|
||||||
|
if set(decoded) != expected_keys or canonical.canonical_bytes(decoded) != content:
|
||||||
|
raise ArmRegistryError("strategy decision artifact is non-canonical or has unknown fields")
|
||||||
|
if decoded["schema_version"] != STRATEGY_DECISION_SCHEMA:
|
||||||
|
raise ArmRegistryError("strategy decision schema version is unsupported")
|
||||||
|
if decoded["kind"] != "reviewed_strategy_decision":
|
||||||
|
raise ArmRegistryError("strategy decision artifact kind is invalid")
|
||||||
|
return decoded
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_strategy_review(
|
||||||
|
authority: StrategyDecisionAuthorityV1,
|
||||||
|
policy: ReviewIndependencePolicy,
|
||||||
|
resolver: canonical.ContentResolver,
|
||||||
|
) -> None:
|
||||||
|
review = authority.review
|
||||||
|
if type(review) is not IndependenceAttestation or type(review.conflict_disclosure) is not ContentRef:
|
||||||
|
raise ArmRegistryError("strategy decision review must use exact independence contracts")
|
||||||
|
try:
|
||||||
|
review.__post_init__()
|
||||||
|
review.conflict_disclosure.__post_init__()
|
||||||
|
except (FactPackValidationError, TypeError, ValueError):
|
||||||
|
raise ArmRegistryError("strategy decision review is invalid") from None
|
||||||
|
if review.subject_digest != authority.decision_ref.content_digest:
|
||||||
|
raise ArmRegistryError("strategy decision review subject does not match artifact bytes")
|
||||||
|
if review.producer_identity == review.reviewer_identity:
|
||||||
|
raise ArmRegistryError("strategy decision review must be independent")
|
||||||
|
if review.reviewer_identity not in policy.reviewed_reviewer_digests:
|
||||||
|
raise ArmRegistryError("strategy decision reviewer is not admitted by policy")
|
||||||
|
if (review.producer_identity, review.reviewer_identity) in policy.forbidden_identity_pairs:
|
||||||
|
raise ArmRegistryError("strategy decision producer/reviewer pair is forbidden")
|
||||||
|
if review.attestation_digest != derive_independence_attestation_digest(review):
|
||||||
|
raise ArmRegistryError("strategy decision review digest cannot be re-derived")
|
||||||
|
_resolve_nonempty(review.conflict_disclosure, policy, resolver, "strategy conflict disclosure")
|
||||||
|
|
||||||
|
|
||||||
|
def validate_strategy_decision_authority(
|
||||||
|
authority: StrategyDecisionAuthorityV1,
|
||||||
|
configuration: ArmConfigurationV3,
|
||||||
|
policy: ReviewIndependencePolicy,
|
||||||
|
resolver: canonical.ContentResolver,
|
||||||
|
) -> None:
|
||||||
|
"""Re-resolve one reviewed strategy decision against its exact arm configuration."""
|
||||||
|
if type(authority) is not StrategyDecisionAuthorityV1:
|
||||||
|
raise ArmRegistryError("strategy decision must use exact StrategyDecisionAuthorityV1")
|
||||||
|
if type(configuration) is not ArmConfigurationV3:
|
||||||
|
raise ArmRegistryError("strategy decision configuration must use exact ArmConfigurationV3")
|
||||||
|
authority.__post_init__()
|
||||||
|
configuration.__post_init__()
|
||||||
|
policy = _validated_review_policy(policy)
|
||||||
|
if authority.review_policy_digest != policy.policy_digest:
|
||||||
|
raise ArmRegistryError("strategy decision review policy identity does not match")
|
||||||
|
content = _resolve_nonempty(authority.decision_ref, policy, resolver, "strategy decision artifact")
|
||||||
|
decoded = _decode_strategy_decision(content)
|
||||||
|
expected = _strategy_decision_value(authority.arm_id, authority.configuration_identity_digest, authority.decision)
|
||||||
|
if decoded != expected:
|
||||||
|
raise ArmRegistryError("strategy decision artifact does not match its authority")
|
||||||
|
if authority.arm_id != configuration.arm_id:
|
||||||
|
raise ArmRegistryError("strategy decision arm identity does not match registration")
|
||||||
|
if authority.configuration_identity_digest != configuration.configuration_identity_digest:
|
||||||
|
raise ArmRegistryError("strategy decision configuration identity does not match registration")
|
||||||
|
_validate_strategy_review(authority, policy, resolver)
|
||||||
|
|
||||||
|
|
||||||
|
def review_strategy_decision_authority(
|
||||||
|
configuration: ArmConfigurationV3,
|
||||||
|
decision_ref: ContentRef,
|
||||||
|
review: IndependenceAttestation,
|
||||||
|
policy: ReviewIndependencePolicy,
|
||||||
|
resolver: canonical.ContentResolver,
|
||||||
|
) -> StrategyDecisionAuthorityV1:
|
||||||
|
"""Accept an independently reviewed lifecycle decision over one exact arm."""
|
||||||
|
if type(configuration) is not ArmConfigurationV3 or type(decision_ref) is not ContentRef:
|
||||||
|
raise ArmRegistryError("strategy decision requires exact configuration and content reference")
|
||||||
|
draft = StrategyDecisionAuthorityV1(
|
||||||
|
configuration.arm_id,
|
||||||
|
configuration.configuration_identity_digest,
|
||||||
|
StrategyDecisionDisposition.ADMIT_TO_PLAN,
|
||||||
|
decision_ref,
|
||||||
|
review,
|
||||||
|
policy.policy_digest,
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
validate_strategy_decision_authority(draft, configuration, policy, resolver)
|
||||||
|
return draft
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"STRATEGY_DECISION_AUTHORITY_SCHEMA",
|
||||||
|
"STRATEGY_DECISION_SCHEMA",
|
||||||
|
"ArmRegistryError",
|
||||||
|
"StrategyDecisionAuthorityV1",
|
||||||
|
"StrategyDecisionDisposition",
|
||||||
|
"review_strategy_decision_authority",
|
||||||
|
"strategy_decision_artifact_bytes",
|
||||||
|
"validate_strategy_decision_authority",
|
||||||
|
]
|
||||||
@@ -0,0 +1,601 @@
|
|||||||
|
"""Task 6.1 immutable label-blind arm registry tests."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from dataclasses import fields, replace
|
||||||
|
from itertools import product
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||||
|
|
||||||
|
from detection_core_fixtures import InMemoryContentResolver, stored_artifact # noqa: E402
|
||||||
|
|
||||||
|
from benchmarks.valid import arm_contract as registry
|
||||||
|
from openultrasast.core import canonical
|
||||||
|
from openultrasast.core.canonical import ContentRef
|
||||||
|
from openultrasast.core.evaluation_measurement import (
|
||||||
|
ArmConfigurationV3,
|
||||||
|
ArmKindV3,
|
||||||
|
ArmScopeKey,
|
||||||
|
CaseOutcomeProjectionPolicyV3,
|
||||||
|
CombinationDecisionRowV3,
|
||||||
|
CombinationOperatorV3,
|
||||||
|
CombinationRuleV3,
|
||||||
|
Comparator,
|
||||||
|
DenominatorPolicyV3,
|
||||||
|
EvaluationMeasurementError,
|
||||||
|
IntervalMethod,
|
||||||
|
IntervalPrecisionRuleV3,
|
||||||
|
MetricId,
|
||||||
|
NeutralOutcome,
|
||||||
|
ProofCandidatePolicyV3,
|
||||||
|
ScopedThresholdV3,
|
||||||
|
ScopeKey,
|
||||||
|
ScopeSufficiencyRuleV3,
|
||||||
|
Statistic,
|
||||||
|
standard_scoped_metric_rules,
|
||||||
|
)
|
||||||
|
from openultrasast.core.evaluation_plan import (
|
||||||
|
EvaluationPlanDeclarationV3,
|
||||||
|
create_evaluation_plan_declaration,
|
||||||
|
derive_evaluation_baselines,
|
||||||
|
derive_evaluation_input_set_digest,
|
||||||
|
)
|
||||||
|
from openultrasast.core.factpack import ( # noqa: E402
|
||||||
|
IndependenceAttestation,
|
||||||
|
ReviewIndependencePolicy,
|
||||||
|
derive_independence_attestation_digest,
|
||||||
|
derive_review_independence_policy_digest,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _digest(label: str) -> str:
|
||||||
|
return canonical.content_digest(label)
|
||||||
|
|
||||||
|
|
||||||
|
def _resolver(*values: InMemoryContentResolver) -> InMemoryContentResolver:
|
||||||
|
return InMemoryContentResolver({key: item for value in values for key, item in value.artifacts.items()})
|
||||||
|
|
||||||
|
|
||||||
|
def _policy(reviewer: str) -> ReviewIndependencePolicy:
|
||||||
|
digest = derive_review_independence_policy_digest((reviewer,), (), ("trusted-store",), 4096)
|
||||||
|
return ReviewIndependencePolicy((reviewer,), (), ("trusted-store",), 4096, digest)
|
||||||
|
|
||||||
|
|
||||||
|
def _review(subject: str, disclosure: ContentRef, *, self_reviewed: bool = False) -> IndependenceAttestation:
|
||||||
|
producer = _digest("strategy-producer")
|
||||||
|
reviewer = producer if self_reviewed else _digest("strategy-reviewer")
|
||||||
|
draft = IndependenceAttestation(subject, producer, reviewer, disclosure, "0" * 64)
|
||||||
|
return replace(draft, attestation_digest=derive_independence_attestation_digest(draft))
|
||||||
|
|
||||||
|
|
||||||
|
def _combination_rule() -> CombinationRuleV3:
|
||||||
|
arm_ids = ("arm-experimental", "arm-shipped")
|
||||||
|
rows = tuple(
|
||||||
|
CombinationDecisionRowV3(
|
||||||
|
outcomes,
|
||||||
|
NeutralOutcome.VIOLATION if NeutralOutcome.VIOLATION in outcomes else outcomes[0],
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
for outcomes in product(tuple(NeutralOutcome), repeat=len(arm_ids))
|
||||||
|
)
|
||||||
|
return CombinationRuleV3(
|
||||||
|
"arm-combined",
|
||||||
|
arm_ids,
|
||||||
|
CombinationOperatorV3.DECLARATIVE_TABLE,
|
||||||
|
rows,
|
||||||
|
ProofCandidatePolicyV3.IDENTICAL_COMPONENT_ONLY,
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _configuration(
|
||||||
|
arm_id: str,
|
||||||
|
*,
|
||||||
|
family: str = "injection",
|
||||||
|
ecosystem: str = "python",
|
||||||
|
combined: bool = False,
|
||||||
|
analyzer: str = "analyzer@sha256:abc",
|
||||||
|
configuration: str = "configuration",
|
||||||
|
) -> ArmConfigurationV3:
|
||||||
|
scope = ScopeKey(family, ecosystem)
|
||||||
|
key = ArmScopeKey(arm_id, scope)
|
||||||
|
thresholds = tuple(
|
||||||
|
ScopedThresholdV3(
|
||||||
|
key,
|
||||||
|
metric_id,
|
||||||
|
Statistic.PER_100 if metric_id is MetricId.ORDINARY_FALSE_ALARMS_PER_100 else Statistic.RATE,
|
||||||
|
Comparator.AT_MOST
|
||||||
|
if metric_id in {MetricId.FIXED_FALSE_ALARM_RATE, MetricId.ORDINARY_FALSE_ALARMS_PER_100}
|
||||||
|
else Comparator.AT_LEAST,
|
||||||
|
0.5,
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
for metric_id in MetricId
|
||||||
|
)
|
||||||
|
precision = tuple(IntervalPrecisionRuleV3(metric_id, 0.25, 4, "") for metric_id in MetricId)
|
||||||
|
return ArmConfigurationV3(
|
||||||
|
arm_id,
|
||||||
|
ArmKindV3.COMBINED if combined else ArmKindV3.COMPONENT,
|
||||||
|
(scope,),
|
||||||
|
(DenominatorPolicyV3(key, standard_scoped_metric_rules(key), IntervalMethod.WILSON_95, ""),),
|
||||||
|
thresholds,
|
||||||
|
(ScopeSufficiencyRuleV3(key, 4, 8, precision, ""),),
|
||||||
|
_digest("judgment-vocabulary"),
|
||||||
|
analyzer,
|
||||||
|
_digest("facts"),
|
||||||
|
_digest(configuration),
|
||||||
|
_digest("budget"),
|
||||||
|
"container@sha256:def",
|
||||||
|
_digest("outcome-mapping"),
|
||||||
|
CaseOutcomeProjectionPolicyV3.DECLARATIVE_COMBINATION_TABLE if combined else CaseOutcomeProjectionPolicyV3.UNANIMOUS_EVIDENCE,
|
||||||
|
_combination_rule() if combined else None,
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _reviewed_strategy_decision(
|
||||||
|
configuration: ArmConfigurationV3,
|
||||||
|
*,
|
||||||
|
label: str = "strategy",
|
||||||
|
) -> tuple[registry.StrategyDecisionAuthorityV1, ReviewIndependencePolicy, InMemoryContentResolver]:
|
||||||
|
content = registry.strategy_decision_artifact_bytes(
|
||||||
|
configuration.arm_id,
|
||||||
|
configuration.configuration_identity_digest,
|
||||||
|
registry.StrategyDecisionDisposition.ADMIT_TO_PLAN,
|
||||||
|
)
|
||||||
|
reference, decision_resolver = stored_artifact(content, artifact_id=f"{label}-decision")
|
||||||
|
disclosure, disclosure_resolver = stored_artifact(b'{"conflicts":[]}', artifact_id=f"{label}-conflicts")
|
||||||
|
review = _review(reference.content_digest, disclosure)
|
||||||
|
policy = _policy(review.reviewer_identity)
|
||||||
|
resolver = _resolver(decision_resolver, disclosure_resolver)
|
||||||
|
authority = registry.review_strategy_decision_authority(
|
||||||
|
configuration,
|
||||||
|
reference,
|
||||||
|
review,
|
||||||
|
policy,
|
||||||
|
resolver,
|
||||||
|
)
|
||||||
|
return authority, policy, resolver
|
||||||
|
|
||||||
|
|
||||||
|
def _registration(
|
||||||
|
role: registry.ArmRole,
|
||||||
|
configuration: ArmConfigurationV3,
|
||||||
|
*,
|
||||||
|
lifecycle: registry.ArmLifecycleState = registry.ArmLifecycleState.ACTIVE,
|
||||||
|
decision: registry.StrategyDecisionAuthorityV1 | None = None,
|
||||||
|
) -> registry.ArmRegistrationV1:
|
||||||
|
return registry.ArmRegistrationV1(role, lifecycle, decision, configuration, "")
|
||||||
|
|
||||||
|
|
||||||
|
def _registrations() -> tuple[registry.ArmRegistrationV1, ...]:
|
||||||
|
return (
|
||||||
|
_registration(registry.ArmRole.COMBINED, _configuration("arm-combined", combined=True)),
|
||||||
|
_registration(registry.ArmRole.EXPERIMENTAL, _configuration("arm-experimental")),
|
||||||
|
_registration(registry.ArmRole.SHIPPED, _configuration("arm-shipped")),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _plan(
|
||||||
|
registrations: tuple[registry.ArmRegistrationV1, ...] | None = None,
|
||||||
|
*,
|
||||||
|
policy: ReviewIndependencePolicy | None = None,
|
||||||
|
resolver: InMemoryContentResolver | None = None,
|
||||||
|
) -> registry.RegisteredEvaluationPlanV1:
|
||||||
|
inputs = (_digest("input-b"), _digest("input-a"))
|
||||||
|
return registry.register_evaluation_plan(
|
||||||
|
registrations=_registrations() if registrations is None else registrations,
|
||||||
|
population_digest=_digest("population"),
|
||||||
|
freeze_digest=_digest("freeze"),
|
||||||
|
input_set_digest=derive_evaluation_input_set_digest(inputs),
|
||||||
|
input_identity_digests=inputs,
|
||||||
|
strategy_review_policy=policy,
|
||||||
|
strategy_resolver=resolver,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _direct_plan(registration: registry.ArmRegistrationV1) -> registry.RegisteredEvaluationPlanV1:
|
||||||
|
inputs = (_digest("input-b"), _digest("input-a"))
|
||||||
|
declaration = create_evaluation_plan_declaration(
|
||||||
|
population_digest=_digest("population"),
|
||||||
|
freeze_digest=_digest("freeze"),
|
||||||
|
input_set_digest=derive_evaluation_input_set_digest(inputs),
|
||||||
|
arm_configurations=(registration.configuration,),
|
||||||
|
input_identity_digests=inputs,
|
||||||
|
)
|
||||||
|
return registry.RegisteredEvaluationPlanV1(
|
||||||
|
(registration,),
|
||||||
|
declaration,
|
||||||
|
derive_evaluation_baselines(declaration),
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_registry_builds_one_canonical_plan_with_separate_arm_roles() -> None:
|
||||||
|
plan = _plan()
|
||||||
|
|
||||||
|
assert tuple(row.role for row in plan.registrations) == (
|
||||||
|
registry.ArmRole.SHIPPED,
|
||||||
|
registry.ArmRole.EXPERIMENTAL,
|
||||||
|
registry.ArmRole.COMBINED,
|
||||||
|
)
|
||||||
|
assert type(plan.declaration) is EvaluationPlanDeclarationV3
|
||||||
|
assert tuple(row.arm_id for row in plan.declaration.arm_configurations) == (
|
||||||
|
"arm-combined",
|
||||||
|
"arm-experimental",
|
||||||
|
"arm-shipped",
|
||||||
|
)
|
||||||
|
assert tuple(row.arm_id for row in plan.baselines) == (
|
||||||
|
"arm-combined",
|
||||||
|
"arm-experimental",
|
||||||
|
"arm-shipped",
|
||||||
|
)
|
||||||
|
assert registry.registered_evaluation_plan_bytes(plan) == registry.registered_evaluation_plan_bytes(_plan())
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"changed",
|
||||||
|
(
|
||||||
|
_configuration("arm-shipped", analyzer="analyzer@sha256:changed"),
|
||||||
|
_configuration("arm-shipped", configuration="changed-configuration"),
|
||||||
|
_configuration("arm-shipped", ecosystem="javascript"),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def test_identity_or_scope_change_creates_a_new_plan(changed: ArmConfigurationV3) -> None:
|
||||||
|
base = _plan()
|
||||||
|
registrations = tuple(
|
||||||
|
_registration(row.role, changed if row.arm_id == "arm-shipped" else row.configuration) for row in base.registrations
|
||||||
|
)
|
||||||
|
|
||||||
|
altered = _plan(registrations)
|
||||||
|
|
||||||
|
assert altered.plan_digest != base.plan_digest
|
||||||
|
assert altered.declaration.declaration_digest != base.declaration.declaration_digest
|
||||||
|
|
||||||
|
|
||||||
|
def test_each_claimed_scope_requires_one_exact_v3_policy_and_sufficiency_rule() -> None:
|
||||||
|
configuration = _configuration("arm-shipped")
|
||||||
|
|
||||||
|
with pytest.raises(EvaluationMeasurementError, match="denominator policies"):
|
||||||
|
replace(configuration, denominator_policies=(), configuration_identity_digest="")
|
||||||
|
with pytest.raises(EvaluationMeasurementError, match="denominator policies"):
|
||||||
|
replace(
|
||||||
|
configuration,
|
||||||
|
denominator_policies=configuration.denominator_policies * 2,
|
||||||
|
configuration_identity_digest="",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert len(configuration.denominator_policies) == len(configuration.exact_scopes) == 1
|
||||||
|
assert len(configuration.sufficiency_rules) == len(configuration.exact_scopes) == 1
|
||||||
|
assert all(type(row) is DenominatorPolicyV3 for row in configuration.denominator_policies)
|
||||||
|
|
||||||
|
|
||||||
|
def test_parked_arm_requires_a_strategy_decision_and_active_arm_forbids_one() -> None:
|
||||||
|
configuration = _configuration("generic-parked", family="path_traversal")
|
||||||
|
authority, policy, resolver = _reviewed_strategy_decision(configuration)
|
||||||
|
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="parked arm requires"):
|
||||||
|
_registration(registry.ArmRole.EXPERIMENTAL, configuration, lifecycle=registry.ArmLifecycleState.PARKED)
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="active arm must not"):
|
||||||
|
_registration(registry.ArmRole.EXPERIMENTAL, configuration, decision=authority)
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="exact reviewed strategy-decision authority"):
|
||||||
|
registry.ArmRegistrationV1( # type: ignore[arg-type]
|
||||||
|
registry.ArmRole.EXPERIMENTAL,
|
||||||
|
registry.ArmLifecycleState.PARKED,
|
||||||
|
ContentRef("fabricated", _digest("content"), "application/json", 1, _digest("retention"), _digest("receipt")),
|
||||||
|
configuration,
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
|
||||||
|
plan = _plan(
|
||||||
|
(
|
||||||
|
_registration(
|
||||||
|
registry.ArmRole.EXPERIMENTAL,
|
||||||
|
configuration,
|
||||||
|
lifecycle=registry.ArmLifecycleState.PARKED,
|
||||||
|
decision=authority,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
policy=policy,
|
||||||
|
resolver=resolver,
|
||||||
|
)
|
||||||
|
assert plan.registrations[0].lifecycle_state is registry.ArmLifecycleState.PARKED
|
||||||
|
|
||||||
|
|
||||||
|
def test_lifecycle_or_strategy_decision_change_creates_a_new_registry_plan() -> None:
|
||||||
|
configuration = _configuration("generic-arm", family="path_traversal")
|
||||||
|
active = _plan((_registration(registry.ArmRole.EXPERIMENTAL, configuration),))
|
||||||
|
first, first_policy, first_resolver = _reviewed_strategy_decision(configuration, label="strategy-a")
|
||||||
|
second, second_policy, second_resolver = _reviewed_strategy_decision(configuration, label="strategy-b")
|
||||||
|
parked = _plan(
|
||||||
|
(
|
||||||
|
_registration(
|
||||||
|
registry.ArmRole.EXPERIMENTAL,
|
||||||
|
configuration,
|
||||||
|
lifecycle=registry.ArmLifecycleState.PARKED,
|
||||||
|
decision=first,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
policy=first_policy,
|
||||||
|
resolver=first_resolver,
|
||||||
|
)
|
||||||
|
changed_decision = _plan(
|
||||||
|
(
|
||||||
|
_registration(
|
||||||
|
registry.ArmRole.EXPERIMENTAL,
|
||||||
|
configuration,
|
||||||
|
lifecycle=registry.ArmLifecycleState.PARKED,
|
||||||
|
decision=second,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
policy=second_policy,
|
||||||
|
resolver=second_resolver,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert active.declaration.declaration_digest == parked.declaration.declaration_digest
|
||||||
|
assert len({active.plan_digest, parked.plan_digest, changed_decision.plan_digest}) == 3
|
||||||
|
|
||||||
|
|
||||||
|
def test_parked_authority_is_re_resolved_and_bound_to_exact_configuration() -> None:
|
||||||
|
configuration = _configuration("generic-arm", family="path_traversal")
|
||||||
|
authority, policy, resolver = _reviewed_strategy_decision(configuration)
|
||||||
|
registration = _registration(
|
||||||
|
registry.ArmRole.EXPERIMENTAL,
|
||||||
|
configuration,
|
||||||
|
lifecycle=registry.ArmLifecycleState.PARKED,
|
||||||
|
decision=authority,
|
||||||
|
)
|
||||||
|
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="cannot be resolved"):
|
||||||
|
_plan((registration,), policy=policy, resolver=InMemoryContentResolver({}))
|
||||||
|
|
||||||
|
changed = _configuration("generic-arm", family="path_traversal", configuration="changed")
|
||||||
|
changed_registration = _registration(
|
||||||
|
registry.ArmRole.EXPERIMENTAL,
|
||||||
|
changed,
|
||||||
|
lifecycle=registry.ArmLifecycleState.PARKED,
|
||||||
|
decision=authority,
|
||||||
|
)
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="configuration identity"):
|
||||||
|
_plan((changed_registration,), policy=policy, resolver=resolver)
|
||||||
|
|
||||||
|
|
||||||
|
def test_strategy_decision_rejects_wrong_kind_self_review_and_mutated_bytes() -> None:
|
||||||
|
configuration = _configuration("generic-arm", family="path_traversal")
|
||||||
|
valid = registry.strategy_decision_artifact_bytes(
|
||||||
|
configuration.arm_id,
|
||||||
|
configuration.configuration_identity_digest,
|
||||||
|
registry.StrategyDecisionDisposition.ADMIT_TO_PLAN,
|
||||||
|
)
|
||||||
|
wrong_value = json.loads(valid)
|
||||||
|
wrong_value["kind"] = "unreviewed_note"
|
||||||
|
wrong_reference, wrong_resolver = stored_artifact(canonical.canonical_bytes(wrong_value), artifact_id="wrong-kind")
|
||||||
|
disclosure, disclosure_resolver = stored_artifact(b'{"conflicts":[]}', artifact_id="wrong-kind-conflicts")
|
||||||
|
wrong_review = _review(wrong_reference.content_digest, disclosure)
|
||||||
|
policy = _policy(wrong_review.reviewer_identity)
|
||||||
|
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="kind"):
|
||||||
|
registry.review_strategy_decision_authority(
|
||||||
|
configuration,
|
||||||
|
wrong_reference,
|
||||||
|
wrong_review,
|
||||||
|
policy,
|
||||||
|
_resolver(wrong_resolver, disclosure_resolver),
|
||||||
|
)
|
||||||
|
|
||||||
|
reference, decision_resolver = stored_artifact(valid, artifact_id="self-reviewed")
|
||||||
|
self_review = _review(reference.content_digest, disclosure, self_reviewed=True)
|
||||||
|
self_policy = _policy(self_review.reviewer_identity)
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="independent"):
|
||||||
|
registry.review_strategy_decision_authority(
|
||||||
|
configuration,
|
||||||
|
reference,
|
||||||
|
self_review,
|
||||||
|
self_policy,
|
||||||
|
_resolver(decision_resolver, disclosure_resolver),
|
||||||
|
)
|
||||||
|
|
||||||
|
authority, valid_policy, valid_resolver = _reviewed_strategy_decision(configuration, label="mutated")
|
||||||
|
artifacts = dict(valid_resolver.artifacts)
|
||||||
|
original = artifacts[authority.decision_ref.artifact_id]
|
||||||
|
artifacts[authority.decision_ref.artifact_id] = canonical.ResolvedContent(b"mutated", original.receipt)
|
||||||
|
registration = _registration(
|
||||||
|
registry.ArmRole.EXPERIMENTAL,
|
||||||
|
configuration,
|
||||||
|
lifecycle=registry.ArmLifecycleState.PARKED,
|
||||||
|
decision=authority,
|
||||||
|
)
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="cannot be resolved"):
|
||||||
|
_plan(
|
||||||
|
(registration,),
|
||||||
|
policy=valid_policy,
|
||||||
|
resolver=InMemoryContentResolver(artifacts),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_direct_parked_plan_cannot_serialize_self_reviewed_or_unresolved_authority() -> None:
|
||||||
|
configuration = _configuration("direct-parked", family="path_traversal")
|
||||||
|
content = registry.strategy_decision_artifact_bytes(
|
||||||
|
configuration.arm_id,
|
||||||
|
configuration.configuration_identity_digest,
|
||||||
|
registry.StrategyDecisionDisposition.ADMIT_TO_PLAN,
|
||||||
|
)
|
||||||
|
reference, decision_resolver = stored_artifact(content, artifact_id="direct-decision")
|
||||||
|
disclosure, disclosure_resolver = stored_artifact(b'{"conflicts":[]}', artifact_id="direct-conflicts")
|
||||||
|
self_review = _review(reference.content_digest, disclosure, self_reviewed=True)
|
||||||
|
policy = _policy(self_review.reviewer_identity)
|
||||||
|
authority = registry.StrategyDecisionAuthorityV1(
|
||||||
|
configuration.arm_id,
|
||||||
|
configuration.configuration_identity_digest,
|
||||||
|
registry.StrategyDecisionDisposition.ADMIT_TO_PLAN,
|
||||||
|
reference,
|
||||||
|
self_review,
|
||||||
|
policy.policy_digest,
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
plan = _direct_plan(
|
||||||
|
_registration(
|
||||||
|
registry.ArmRole.EXPERIMENTAL,
|
||||||
|
configuration,
|
||||||
|
lifecycle=registry.ArmLifecycleState.PARKED,
|
||||||
|
decision=authority,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="review policy and resolver"):
|
||||||
|
registry.registered_evaluation_plan_bytes(plan)
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="independent"):
|
||||||
|
registry.registered_evaluation_plan_bytes(
|
||||||
|
plan,
|
||||||
|
strategy_review_policy=policy,
|
||||||
|
strategy_resolver=_resolver(decision_resolver, disclosure_resolver),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_direct_parked_plan_serialization_rejects_unresolved_and_policy_mismatch() -> None:
|
||||||
|
configuration = _configuration("direct-reviewed", family="path_traversal")
|
||||||
|
authority, policy, resolver = _reviewed_strategy_decision(configuration, label="direct-reviewed")
|
||||||
|
plan = _direct_plan(
|
||||||
|
_registration(
|
||||||
|
registry.ArmRole.EXPERIMENTAL,
|
||||||
|
configuration,
|
||||||
|
lifecycle=registry.ArmLifecycleState.PARKED,
|
||||||
|
decision=authority,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="cannot be resolved"):
|
||||||
|
registry.registered_evaluation_plan_bytes(
|
||||||
|
plan,
|
||||||
|
strategy_review_policy=policy,
|
||||||
|
strategy_resolver=InMemoryContentResolver({}),
|
||||||
|
)
|
||||||
|
wrong_policy = _policy(_digest("other-maintainer"))
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="review policy identity"):
|
||||||
|
registry.registered_evaluation_plan_bytes(
|
||||||
|
plan,
|
||||||
|
strategy_review_policy=wrong_policy,
|
||||||
|
strategy_resolver=resolver,
|
||||||
|
)
|
||||||
|
|
||||||
|
artifacts = dict(resolver.artifacts)
|
||||||
|
original = artifacts[authority.decision_ref.artifact_id]
|
||||||
|
artifacts[authority.decision_ref.artifact_id] = canonical.ResolvedContent(b"mutated", original.receipt)
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="cannot be resolved"):
|
||||||
|
registry.registered_evaluation_plan_bytes(
|
||||||
|
plan,
|
||||||
|
strategy_review_policy=policy,
|
||||||
|
strategy_resolver=InMemoryContentResolver(artifacts),
|
||||||
|
)
|
||||||
|
|
||||||
|
changed = _configuration("direct-reviewed", family="path_traversal", configuration="changed")
|
||||||
|
changed_plan = _direct_plan(
|
||||||
|
_registration(
|
||||||
|
registry.ArmRole.EXPERIMENTAL,
|
||||||
|
changed,
|
||||||
|
lifecycle=registry.ArmLifecycleState.PARKED,
|
||||||
|
decision=authority,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="configuration identity"):
|
||||||
|
registry.registered_evaluation_plan_bytes(
|
||||||
|
changed_plan,
|
||||||
|
strategy_review_policy=policy,
|
||||||
|
strategy_resolver=resolver,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("lifecycle", tuple(registry.ArmLifecycleState))
|
||||||
|
def test_parked_access_control_scope_cannot_enter_a_plan(lifecycle: registry.ArmLifecycleState) -> None:
|
||||||
|
configuration = _configuration("access-control-arm", family="access_control")
|
||||||
|
if lifecycle is registry.ArmLifecycleState.PARKED:
|
||||||
|
decision, policy, resolver = _reviewed_strategy_decision(configuration, label="unaccepted-access-control")
|
||||||
|
else:
|
||||||
|
decision, policy, resolver = None, None, None
|
||||||
|
registration = _registration(
|
||||||
|
registry.ArmRole.EXPERIMENTAL,
|
||||||
|
configuration,
|
||||||
|
lifecycle=lifecycle,
|
||||||
|
decision=decision,
|
||||||
|
)
|
||||||
|
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="access-control arm remains parked"):
|
||||||
|
_plan((registration,), policy=policy, resolver=resolver)
|
||||||
|
|
||||||
|
|
||||||
|
def test_duplicate_arm_and_role_kind_mismatch_are_refused() -> None:
|
||||||
|
configuration = _configuration("same-arm")
|
||||||
|
duplicate = (
|
||||||
|
_registration(registry.ArmRole.SHIPPED, configuration),
|
||||||
|
_registration(registry.ArmRole.EXPERIMENTAL, configuration),
|
||||||
|
)
|
||||||
|
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="unique arm identities"):
|
||||||
|
_plan(duplicate)
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="combined role requires"):
|
||||||
|
_registration(registry.ArmRole.COMBINED, configuration)
|
||||||
|
|
||||||
|
|
||||||
|
def test_direct_plan_construction_rejects_an_extra_unregistered_core_arm() -> None:
|
||||||
|
plan = _plan()
|
||||||
|
inputs = tuple(sorted({slot.input_identity_digest for slot in plan.declaration.expected_observation_slots}))
|
||||||
|
declaration = create_evaluation_plan_declaration(
|
||||||
|
population_digest=plan.declaration.population_digest,
|
||||||
|
freeze_digest=plan.declaration.freeze_digest,
|
||||||
|
input_set_digest=plan.declaration.input_set_digest,
|
||||||
|
arm_configurations=(*plan.declaration.arm_configurations, _configuration("unregistered-arm")),
|
||||||
|
input_identity_digests=inputs,
|
||||||
|
)
|
||||||
|
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="do not equal registered arms"):
|
||||||
|
registry.RegisteredEvaluationPlanV1(
|
||||||
|
plan.registrations,
|
||||||
|
declaration,
|
||||||
|
derive_evaluation_baselines(declaration),
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_registry_requires_exact_core_v3_configuration_type() -> None:
|
||||||
|
class Lookalike:
|
||||||
|
arm_id = "lookalike"
|
||||||
|
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="exact ArmConfigurationV3"):
|
||||||
|
registry.ArmRegistrationV1( # type: ignore[arg-type]
|
||||||
|
registry.ArmRole.EXPERIMENTAL,
|
||||||
|
registry.ArmLifecycleState.ACTIVE,
|
||||||
|
None,
|
||||||
|
Lookalike(),
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_registry_rejects_strategy_authority_lookalikes() -> None:
|
||||||
|
class LookalikeAuthority:
|
||||||
|
authority_digest = _digest("lookalike-authority")
|
||||||
|
|
||||||
|
with pytest.raises(registry.ArmRegistryError, match="exact reviewed strategy-decision authority"):
|
||||||
|
registry.ArmRegistrationV1( # type: ignore[arg-type]
|
||||||
|
registry.ArmRole.EXPERIMENTAL,
|
||||||
|
registry.ArmLifecycleState.PARKED,
|
||||||
|
LookalikeAuthority(),
|
||||||
|
_configuration("lookalike-arm", family="path_traversal"),
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_plan_contract_has_no_label_or_result_surface() -> None:
|
||||||
|
plan = _plan()
|
||||||
|
contract_fields = {field.name for field in fields(registry.RegisteredEvaluationPlanV1)}
|
||||||
|
registration_fields = {field.name for field in fields(registry.ArmRegistrationV1)}
|
||||||
|
content = registry.registered_evaluation_plan_bytes(plan)
|
||||||
|
|
||||||
|
assert not {"labels", "label_bytes", "expected_outcomes", "results", "findings"} & contract_fields
|
||||||
|
assert not {"labels", "label_bytes", "expected_outcomes", "results", "findings"} & registration_fields
|
||||||
|
for forbidden in (b"label_bytes", b"expected_outcomes", b"findings", b"password=", b"/home/"):
|
||||||
|
assert forbidden not in content
|
||||||
Reference in New Issue
Block a user