mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-04 10:48:19 +00:00
feat(detection-core): derive reviewed admission authority
This commit is contained in:
@@ -538,7 +538,7 @@ contract work and use synthetic/in-memory workflow boundaries only.
|
|||||||
- _Requirements: 17.10, 17.11, 17.12_
|
- _Requirements: 17.10, 17.11, 17.12_
|
||||||
- _Boundary: Evaluation Attempt History_
|
- _Boundary: Evaluation Attempt History_
|
||||||
- _Depends: 13.3_
|
- _Depends: 13.3_
|
||||||
- [ ] 14.3 Validate the independently reviewed admission authority
|
- [x] 14.3 Validate the independently reviewed admission authority
|
||||||
- Bind the feasible plan to the exact sealed observation set, attempt-history head, current readiness, validation context, and current complete custody head.
|
- Bind the feasible plan to the exact sealed observation set, attempt-history head, current readiness, validation context, and current complete custody head.
|
||||||
- Require the admission-authority payload to be reviewed independently from its producer and resolve its review evidence before authorization.
|
- Require the admission-authority payload to be reviewed independently from its producer and resolve its review evidence before authorization.
|
||||||
- Revalidate feasibility, all observation bytes, attempt history, readiness, and custody immediately before producing authority.
|
- Revalidate feasibility, all observation bytes, attempt history, readiness, and custody immediately before producing authority.
|
||||||
@@ -718,6 +718,7 @@ contract work and use synthetic/in-memory workflow boundaries only.
|
|||||||
- Task 13.4 revalidates the reviewed manifest before deriving one exact arm/scope feasibility row, preserves the V2 reachability witnesses and same-ecosystem ordinary capacity, and keeps insufficiency and zero denominators outside GO/NO-GO while blocking shared spend.
|
- Task 13.4 revalidates the reviewed manifest before deriving one exact arm/scope feasibility row, preserves the V2 reachability witnesses and same-ecosystem ordinary capacity, and keeps insufficiency and zero denominators outside GO/NO-GO while blocking shared spend.
|
||||||
- Task 14.1 added strict canonical, nonzero, label-blind observation records and sealed references whose trusted resolved bytes, read/stage evidence, configuration identities, and complete slot set must match the reviewed plan before authority.
|
- Task 14.1 added strict canonical, nonzero, label-blind observation records and sealed references whose trusted resolved bytes, read/stage evidence, configuration identities, and complete slot set must match the reviewed plan before authority.
|
||||||
- Task 14.2 added strict canonical reviewed attempt events and exact predecessor-chain validation so only unchanged, closed-access, all-sealed retry histories remain eligible before authority.
|
- Task 14.2 added strict canonical reviewed attempt events and exact predecessor-chain validation so only unchanged, closed-access, all-sealed retry histories remain eligible before authority.
|
||||||
|
- Task 14.3 added an independently reviewed V3 admission manifest and revalidates feasibility, observation bytes, sealed attempts, readiness, context, and current available custody before deriving one exact pre-spend authority.
|
||||||
- The frozen migration inventory is a reviewed worklist with stable identities, not a semantic-use census. It may only close entries as `v2_migrated` or `retired`; it never grows, deletes entries, or proves absence by interpreting Python flow.
|
- The frozen migration inventory is a reviewed worklist with stable identities, not a semantic-use census. It may only close entries as `v2_migrated` or `retired`; it never grows, deletes entries, or proves absence by interpreting Python flow.
|
||||||
- Cutover is proved by explicit syntax checks plus deletion of the compatibility and v1 write capability. No task extends the discarded alias/data-flow interpreter.
|
- Cutover is proved by explicit syntax checks plus deletion of the compatibility and v1 write capability. No task extends the discarded alias/data-flow interpreter.
|
||||||
- Task 10.5 debug established that executable owner probes cannot compensate for blanket source-closure acceptance; resumed hunt attempts also require collision-resistant attempt identity and create-only persistence before handoff validation is authoritative.
|
- Task 10.5 debug established that executable owner probes cannot compensate for blanket source-closure acceptance; resumed hunt attempts also require collision-resistant attempt identity and create-only persistence before handoff validation is authoritative.
|
||||||
|
|||||||
@@ -0,0 +1,359 @@
|
|||||||
|
"""Complete reviewed pre-spend authority validation for evaluation admission V3."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
from collections.abc import Sequence
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import ClassVar
|
||||||
|
|
||||||
|
from openultrasast.core import canonical
|
||||||
|
from openultrasast.core.admission_context import AdmissionValidationContext, _validate_context
|
||||||
|
from openultrasast.core.custody import PopulationCustodyLog, PopulationCustodyPort, PopulationStatus, validate_population_custody_log
|
||||||
|
from openultrasast.core.evaluation_attempt import AttemptHistoryV3, validate_evaluation_attempt_history
|
||||||
|
from openultrasast.core.evaluation_feasibility import (
|
||||||
|
EvaluationFeasibilityV3,
|
||||||
|
validate_evaluation_feasibility,
|
||||||
|
)
|
||||||
|
from openultrasast.core.evaluation_observation import SealedObservationRefV3, validate_evaluation_observations
|
||||||
|
from openultrasast.core.evaluation_plan import EvaluationPlanManifestV3
|
||||||
|
from openultrasast.core.evaluation_version import (
|
||||||
|
EVALUATION_ADMISSION_V3_SCHEMA,
|
||||||
|
EvaluationV3Contract,
|
||||||
|
NonAuthorizingV3,
|
||||||
|
require_exact_v3_contract,
|
||||||
|
)
|
||||||
|
from openultrasast.core.factpack import IndependenceAttestation
|
||||||
|
from openultrasast.core.readiness import ReadinessResult, validate_readiness_result
|
||||||
|
|
||||||
|
_DIGEST = re.compile(r"[0-9a-f]{64}")
|
||||||
|
_MANIFEST_PAYLOAD_SCHEMA = "openultrasast.detection-core.evaluation-admission-manifest-payload.v3"
|
||||||
|
_MANIFEST_SCHEMA = "openultrasast.detection-core.evaluation-admission-manifest.v3"
|
||||||
|
_OBSERVATION_SET_SCHEMA = "openultrasast.detection-core.evaluation-observation-set.v3"
|
||||||
|
_READINESS_SET_SCHEMA = "openultrasast.detection-core.evaluation-readiness-set.v3"
|
||||||
|
_AUTHORITY_SCHEMA = "openultrasast.detection-core.evaluation-admission-authority.v3"
|
||||||
|
|
||||||
|
|
||||||
|
class EvaluationAuthorityError(ValueError):
|
||||||
|
"""The complete reviewed V3 admission graph cannot produce pre-spend authority."""
|
||||||
|
|
||||||
|
|
||||||
|
def _digest(value: object, field: str) -> str:
|
||||||
|
if type(value) is not str or _DIGEST.fullmatch(value) is None:
|
||||||
|
raise EvaluationAuthorityError(f"{field} must be a full lowercase SHA-256 digest")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def _digest_tuple(value: object, field: str) -> tuple[str, ...]:
|
||||||
|
if type(value) is not tuple or not value:
|
||||||
|
raise EvaluationAuthorityError(f"{field} must be a nonempty canonical tuple")
|
||||||
|
for item in value:
|
||||||
|
_digest(item, field)
|
||||||
|
if value != tuple(sorted(set(value))):
|
||||||
|
raise EvaluationAuthorityError(f"{field} must be unique and canonical")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def _ordered_digest_tuple(value: object, field: str) -> tuple[str, ...]:
|
||||||
|
if type(value) is not tuple or not value:
|
||||||
|
raise EvaluationAuthorityError(f"{field} must be a nonempty exact tuple")
|
||||||
|
for item in value:
|
||||||
|
_digest(item, field)
|
||||||
|
if len(set(value)) != len(value):
|
||||||
|
raise EvaluationAuthorityError(f"{field} must not contain duplicates")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
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 EvaluationAuthorityError(f"{field} does not match canonical content")
|
||||||
|
|
||||||
|
|
||||||
|
def _content_ref_value(reference: canonical.ContentRef) -> canonical.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 _review_value(review: IndependenceAttestation) -> canonical.JsonValue:
|
||||||
|
return {
|
||||||
|
"subject_digest": review.subject_digest,
|
||||||
|
"producer_identity": review.producer_identity,
|
||||||
|
"reviewer_identity": review.reviewer_identity,
|
||||||
|
"conflict_disclosure": _content_ref_value(review.conflict_disclosure),
|
||||||
|
"attestation_digest": review.attestation_digest,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class EvaluationAdmissionManifestV3(EvaluationV3Contract, NonAuthorizingV3):
|
||||||
|
"""Externally produced complete label-blind graph plus independent review."""
|
||||||
|
|
||||||
|
__evaluation_v3_contract_schema__: ClassVar[str] = EVALUATION_ADMISSION_V3_SCHEMA
|
||||||
|
plan_manifest: EvaluationPlanManifestV3
|
||||||
|
feasibility_digest: str
|
||||||
|
observations: tuple[SealedObservationRefV3, ...]
|
||||||
|
readiness_digests: tuple[str, ...]
|
||||||
|
custody_log_digest: str
|
||||||
|
attempt_history: AttemptHistoryV3
|
||||||
|
payload_digest: str
|
||||||
|
review: IndependenceAttestation
|
||||||
|
manifest_digest: str
|
||||||
|
|
||||||
|
def __post_init__(self) -> None:
|
||||||
|
require_exact_v3_contract(self, EvaluationAdmissionManifestV3, field="evaluation admission manifest")
|
||||||
|
if type(self.plan_manifest) is not EvaluationPlanManifestV3:
|
||||||
|
raise EvaluationAuthorityError("admission plan manifest must use exact EvaluationPlanManifestV3")
|
||||||
|
self.plan_manifest.__post_init__()
|
||||||
|
_digest(self.feasibility_digest, "admission feasibility digest")
|
||||||
|
if type(self.observations) is not tuple or not self.observations:
|
||||||
|
raise EvaluationAuthorityError("admission observations must be a nonempty exact tuple")
|
||||||
|
if any(type(item) is not SealedObservationRefV3 for item in self.observations):
|
||||||
|
raise EvaluationAuthorityError("admission observations must use exact sealed references")
|
||||||
|
for item in self.observations:
|
||||||
|
item.__post_init__()
|
||||||
|
_digest_tuple(self.readiness_digests, "admission readiness digest")
|
||||||
|
_digest(self.custody_log_digest, "admission custody log digest")
|
||||||
|
if type(self.attempt_history) is not AttemptHistoryV3:
|
||||||
|
raise EvaluationAuthorityError("admission attempt history must use exact AttemptHistoryV3")
|
||||||
|
self.attempt_history.__post_init__()
|
||||||
|
_digest(self.payload_digest, "admission payload digest")
|
||||||
|
if type(self.review) is not IndependenceAttestation:
|
||||||
|
raise EvaluationAuthorityError("admission review must use exact IndependenceAttestation")
|
||||||
|
self.review.__post_init__()
|
||||||
|
if type(self.review.conflict_disclosure) is not canonical.ContentRef:
|
||||||
|
raise EvaluationAuthorityError("admission review disclosure must use exact ContentRef")
|
||||||
|
self.review.conflict_disclosure.__post_init__()
|
||||||
|
_digest(self.manifest_digest, "admission manifest digest")
|
||||||
|
|
||||||
|
|
||||||
|
def _manifest_payload_value(manifest: EvaluationAdmissionManifestV3) -> canonical.JsonValue:
|
||||||
|
return {
|
||||||
|
"schema_version": _MANIFEST_PAYLOAD_SCHEMA,
|
||||||
|
"plan_manifest_digest": manifest.plan_manifest.manifest_digest,
|
||||||
|
"feasibility_digest": manifest.feasibility_digest,
|
||||||
|
"observation_ref_digests": tuple(item.ref_digest for item in manifest.observations),
|
||||||
|
"readiness_digests": manifest.readiness_digests,
|
||||||
|
"custody_log_digest": manifest.custody_log_digest,
|
||||||
|
"attempt_history_digest": manifest.attempt_history.history_digest,
|
||||||
|
"attempt_history_head_digest": manifest.attempt_history.head_event_digest,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def derive_evaluation_admission_manifest_payload_digest(manifest: EvaluationAdmissionManifestV3) -> str:
|
||||||
|
if type(manifest) is not EvaluationAdmissionManifestV3:
|
||||||
|
raise EvaluationAuthorityError("admission manifest must use exact EvaluationAdmissionManifestV3")
|
||||||
|
manifest.__post_init__()
|
||||||
|
return canonical.content_digest(_manifest_payload_value(manifest))
|
||||||
|
|
||||||
|
|
||||||
|
def derive_evaluation_admission_manifest_digest(manifest: EvaluationAdmissionManifestV3) -> str:
|
||||||
|
if type(manifest) is not EvaluationAdmissionManifestV3:
|
||||||
|
raise EvaluationAuthorityError("admission manifest must use exact EvaluationAdmissionManifestV3")
|
||||||
|
manifest.__post_init__()
|
||||||
|
return canonical.content_digest(
|
||||||
|
{
|
||||||
|
"schema_version": _MANIFEST_SCHEMA,
|
||||||
|
"payload_digest": manifest.payload_digest,
|
||||||
|
"review": _review_value(manifest.review),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def derive_evaluation_observation_set_digest(observations: tuple[SealedObservationRefV3, ...]) -> str:
|
||||||
|
if type(observations) is not tuple or not observations or any(type(item) is not SealedObservationRefV3 for item in observations):
|
||||||
|
raise EvaluationAuthorityError("observation set must be a nonempty tuple of exact sealed references")
|
||||||
|
for item in observations:
|
||||||
|
item.__post_init__()
|
||||||
|
return canonical.content_digest(
|
||||||
|
{"schema_version": _OBSERVATION_SET_SCHEMA, "observation_ref_digests": tuple(item.ref_digest for item in observations)}
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def derive_evaluation_readiness_set_digest(readiness_digests: tuple[str, ...]) -> str:
|
||||||
|
values = _digest_tuple(readiness_digests, "readiness digest")
|
||||||
|
return canonical.content_digest({"schema_version": _READINESS_SET_SCHEMA, "readiness_digests": values})
|
||||||
|
|
||||||
|
|
||||||
|
def _authority_value(authority: EvaluationAdmissionAuthorityV3) -> canonical.JsonValue:
|
||||||
|
return {
|
||||||
|
"schema_version": _AUTHORITY_SCHEMA,
|
||||||
|
"admission_manifest_digest": authority.admission_manifest_digest,
|
||||||
|
"plan_manifest_digest": authority.plan_manifest_digest,
|
||||||
|
"baseline_digests": authority.baseline_digests,
|
||||||
|
"feasibility_digest": authority.feasibility_digest,
|
||||||
|
"observation_set_digest": authority.observation_set_digest,
|
||||||
|
"readiness_set_digest": authority.readiness_set_digest,
|
||||||
|
"validation_context_digest": authority.validation_context_digest,
|
||||||
|
"custody_log_digest": authority.custody_log_digest,
|
||||||
|
"attempt_history_head_digest": authority.attempt_history_head_digest,
|
||||||
|
"admission_review_digest": authority.admission_review_digest,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class EvaluationAdmissionAuthorityV3(EvaluationV3Contract, NonAuthorizingV3):
|
||||||
|
"""Validated projection that may feed preparation but never consumer use."""
|
||||||
|
|
||||||
|
__evaluation_v3_contract_schema__: ClassVar[str] = EVALUATION_ADMISSION_V3_SCHEMA
|
||||||
|
admission_manifest_digest: str
|
||||||
|
plan_manifest_digest: str
|
||||||
|
baseline_digests: tuple[str, ...]
|
||||||
|
feasibility_digest: str
|
||||||
|
observation_set_digest: str
|
||||||
|
readiness_set_digest: str
|
||||||
|
validation_context_digest: str
|
||||||
|
custody_log_digest: str
|
||||||
|
attempt_history_head_digest: str
|
||||||
|
admission_review_digest: str
|
||||||
|
authority_digest: str
|
||||||
|
|
||||||
|
def __post_init__(self) -> None:
|
||||||
|
require_exact_v3_contract(self, EvaluationAdmissionAuthorityV3, field="evaluation admission authority")
|
||||||
|
for field in (
|
||||||
|
"admission_manifest_digest",
|
||||||
|
"plan_manifest_digest",
|
||||||
|
"feasibility_digest",
|
||||||
|
"observation_set_digest",
|
||||||
|
"readiness_set_digest",
|
||||||
|
"validation_context_digest",
|
||||||
|
"custody_log_digest",
|
||||||
|
"attempt_history_head_digest",
|
||||||
|
"admission_review_digest",
|
||||||
|
):
|
||||||
|
_digest(getattr(self, field), field)
|
||||||
|
_ordered_digest_tuple(self.baseline_digests, "authority baseline digest")
|
||||||
|
_bind(self, "authority_digest", canonical.content_digest(_authority_value(self)))
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_manifest_review(
|
||||||
|
manifest: EvaluationAdmissionManifestV3,
|
||||||
|
context: AdmissionValidationContext,
|
||||||
|
resolver: canonical.ContentResolver,
|
||||||
|
) -> None:
|
||||||
|
from openultrasast.core._evaluation_plan_review import _validate_policy, _validate_review
|
||||||
|
|
||||||
|
policy = _validate_policy(context.admission_review_policy)
|
||||||
|
expected_payload = derive_evaluation_admission_manifest_payload_digest(manifest)
|
||||||
|
if manifest.payload_digest != expected_payload:
|
||||||
|
raise EvaluationAuthorityError("admission payload digest cannot be re-derived")
|
||||||
|
_validate_review(manifest.review, expected_payload, policy, resolver)
|
||||||
|
if manifest.manifest_digest != derive_evaluation_admission_manifest_digest(manifest):
|
||||||
|
raise EvaluationAuthorityError("admission manifest digest cannot be re-derived")
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_readiness(
|
||||||
|
manifest: EvaluationAdmissionManifestV3,
|
||||||
|
readiness: Sequence[ReadinessResult],
|
||||||
|
) -> None:
|
||||||
|
if type(readiness) is not tuple or any(type(item) is not ReadinessResult for item in readiness):
|
||||||
|
raise EvaluationAuthorityError("current readiness must be an exact tuple of ReadinessResult values")
|
||||||
|
for item in readiness:
|
||||||
|
validate_readiness_result(item)
|
||||||
|
digests = tuple(sorted(item.result_digest for item in readiness))
|
||||||
|
if len(set(digests)) != len(digests) or digests != manifest.readiness_digests:
|
||||||
|
raise EvaluationAuthorityError("current readiness set does not match the reviewed admission manifest")
|
||||||
|
if digests != manifest.plan_manifest.readiness_digests:
|
||||||
|
raise EvaluationAuthorityError("current readiness set does not match the reviewed plan")
|
||||||
|
|
||||||
|
|
||||||
|
def _current_custody(
|
||||||
|
manifest: EvaluationAdmissionManifestV3,
|
||||||
|
context: AdmissionValidationContext,
|
||||||
|
custody: PopulationCustodyPort,
|
||||||
|
) -> PopulationCustodyLog:
|
||||||
|
population = manifest.plan_manifest.declaration.population_digest
|
||||||
|
current = custody.current_log(population)
|
||||||
|
validate_population_custody_log(current, context.evidence.custody_policy, current=custody)
|
||||||
|
if current.population_digest != population:
|
||||||
|
raise EvaluationAuthorityError("current custody population does not match the reviewed plan")
|
||||||
|
expected = manifest.custody_log_digest
|
||||||
|
if manifest.plan_manifest.custody_log_digest != expected or current.log_digest != expected:
|
||||||
|
raise EvaluationAuthorityError("admission custody head is stale or differs from the reviewed plan")
|
||||||
|
if current.status is not PopulationStatus.AVAILABLE:
|
||||||
|
raise EvaluationAuthorityError("admission population is not currently available")
|
||||||
|
return current
|
||||||
|
|
||||||
|
|
||||||
|
def _make_authority(
|
||||||
|
manifest: EvaluationAdmissionManifestV3,
|
||||||
|
context: AdmissionValidationContext,
|
||||||
|
) -> EvaluationAdmissionAuthorityV3:
|
||||||
|
plan = manifest.plan_manifest
|
||||||
|
return EvaluationAdmissionAuthorityV3(
|
||||||
|
manifest.manifest_digest,
|
||||||
|
plan.manifest_digest,
|
||||||
|
tuple(item.baseline_digest for item in plan.baselines),
|
||||||
|
manifest.feasibility_digest,
|
||||||
|
derive_evaluation_observation_set_digest(manifest.observations),
|
||||||
|
derive_evaluation_readiness_set_digest(manifest.readiness_digests),
|
||||||
|
context.context_digest,
|
||||||
|
manifest.custody_log_digest,
|
||||||
|
manifest.attempt_history.head_event_digest,
|
||||||
|
manifest.review.attestation_digest,
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def authorize_evaluation_admission(
|
||||||
|
manifest: EvaluationAdmissionManifestV3,
|
||||||
|
feasibility: EvaluationFeasibilityV3,
|
||||||
|
readiness: Sequence[ReadinessResult],
|
||||||
|
context: AdmissionValidationContext,
|
||||||
|
custody: PopulationCustodyPort,
|
||||||
|
resolver: canonical.ContentResolver,
|
||||||
|
) -> EvaluationAdmissionAuthorityV3:
|
||||||
|
"""Revalidate the complete current label-blind graph and derive one exact authority."""
|
||||||
|
try:
|
||||||
|
if type(manifest) is not EvaluationAdmissionManifestV3:
|
||||||
|
raise EvaluationAuthorityError("admission manifest must use exact EvaluationAdmissionManifestV3")
|
||||||
|
manifest.__post_init__()
|
||||||
|
if type(context) is not AdmissionValidationContext:
|
||||||
|
raise EvaluationAuthorityError("validation context must use exact AdmissionValidationContext")
|
||||||
|
_validate_context(context)
|
||||||
|
_validate_manifest_review(manifest, context, resolver)
|
||||||
|
policy = context.admission_review_policy
|
||||||
|
validate_evaluation_feasibility(manifest.plan_manifest, feasibility, policy, resolver)
|
||||||
|
if not feasibility.may_spend or feasibility.feasibility_digest != manifest.feasibility_digest:
|
||||||
|
raise EvaluationAuthorityError("reviewed feasibility is insufficient or differs from the admission manifest")
|
||||||
|
validate_evaluation_observations(
|
||||||
|
manifest.plan_manifest.declaration,
|
||||||
|
manifest.observations,
|
||||||
|
resolver,
|
||||||
|
trusted_store_identities=policy.trusted_store_identities,
|
||||||
|
maximum_bytes=policy.maximum_evidence_bytes,
|
||||||
|
)
|
||||||
|
validate_evaluation_attempt_history(manifest.plan_manifest, manifest.attempt_history, policy, resolver)
|
||||||
|
_validate_readiness(manifest, readiness)
|
||||||
|
current = _current_custody(manifest, context, custody)
|
||||||
|
authority = _make_authority(manifest, context)
|
||||||
|
latest = custody.current_log(current.population_digest)
|
||||||
|
if latest.log_digest != current.log_digest or latest.status is not PopulationStatus.AVAILABLE:
|
||||||
|
raise EvaluationAuthorityError("admission custody changed during authority derivation")
|
||||||
|
return authority
|
||||||
|
except (canonical.ArtifactResolutionError, canonical.ArtifactValidationError):
|
||||||
|
raise
|
||||||
|
except EvaluationAuthorityError:
|
||||||
|
raise
|
||||||
|
except (AttributeError, LookupError, TypeError, ValueError) as exc:
|
||||||
|
raise EvaluationAuthorityError(f"evaluation admission authority refused: {exc}") from exc
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"EvaluationAdmissionAuthorityV3",
|
||||||
|
"EvaluationAdmissionManifestV3",
|
||||||
|
"EvaluationAuthorityError",
|
||||||
|
"authorize_evaluation_admission",
|
||||||
|
"derive_evaluation_admission_manifest_digest",
|
||||||
|
"derive_evaluation_admission_manifest_payload_digest",
|
||||||
|
"derive_evaluation_observation_set_digest",
|
||||||
|
"derive_evaluation_readiness_set_digest",
|
||||||
|
]
|
||||||
@@ -0,0 +1,332 @@
|
|||||||
|
"""Complete reviewed authority derivation for evaluation admission V3."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import fields, replace
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from detection_core_fixtures import InMemoryContentResolver
|
||||||
|
from test_core_admission_context import _case as _context_case
|
||||||
|
from test_core_evaluation_attempt import _event, _projection, _reference
|
||||||
|
from test_core_evaluation_feasibility import _feasible_manifest
|
||||||
|
from test_core_evaluation_observation import _observation, _seal
|
||||||
|
from test_core_evaluation_plan import _digest
|
||||||
|
from test_core_evaluation_plan_review import _attestation
|
||||||
|
|
||||||
|
from openultrasast.core import canonical
|
||||||
|
from openultrasast.core.custody import LineagePurpose, PopulationStatus, PopulationUseRequest
|
||||||
|
from openultrasast.core.evaluation_attempt import (
|
||||||
|
AttemptHistoryV3,
|
||||||
|
ObservationAccessPolicyV3,
|
||||||
|
PreSpendDispositionV3,
|
||||||
|
PublicStatusFieldV3,
|
||||||
|
derive_attempt_identity,
|
||||||
|
)
|
||||||
|
from openultrasast.core.evaluation_authority import (
|
||||||
|
EvaluationAdmissionAuthorityV3,
|
||||||
|
EvaluationAdmissionManifestV3,
|
||||||
|
EvaluationAuthorityError,
|
||||||
|
authorize_evaluation_admission,
|
||||||
|
derive_evaluation_admission_manifest_digest,
|
||||||
|
derive_evaluation_admission_manifest_payload_digest,
|
||||||
|
)
|
||||||
|
from openultrasast.core.evaluation_feasibility import derive_evaluation_feasibility
|
||||||
|
from openultrasast.core.evaluation_plan import (
|
||||||
|
EvaluationPlanManifestV3,
|
||||||
|
create_evaluation_plan_declaration,
|
||||||
|
derive_evaluation_baselines,
|
||||||
|
derive_evaluation_census_digest,
|
||||||
|
derive_evaluation_census_payload_digest,
|
||||||
|
derive_evaluation_plan_manifest_digest,
|
||||||
|
derive_evaluation_plan_manifest_payload_digest,
|
||||||
|
)
|
||||||
|
from openultrasast.core.evaluation_version import NonAuthorizingV3
|
||||||
|
|
||||||
|
|
||||||
|
def _reviewed_plan():
|
||||||
|
context_case = _context_case()
|
||||||
|
template, _, _ = _feasible_manifest()
|
||||||
|
inputs = tuple(sorted({slot.input_identity_digest for slot in template.declaration.expected_observation_slots}))
|
||||||
|
declaration = create_evaluation_plan_declaration(
|
||||||
|
population_digest=context_case.manifest.identity.population_digest,
|
||||||
|
freeze_digest=template.declaration.freeze_digest,
|
||||||
|
input_set_digest=template.declaration.input_set_digest,
|
||||||
|
arm_configurations=template.declaration.arm_configurations,
|
||||||
|
input_identity_digests=inputs,
|
||||||
|
)
|
||||||
|
reviewer = context_case.context.admission_review_policy.reviewed_reviewer_digests[0]
|
||||||
|
disclosure = context_case.manifest.review.conflict_disclosure
|
||||||
|
producer = _digest("v3-authority-plan-producer")
|
||||||
|
provisional = _attestation("0" * 64, producer, reviewer, disclosure)
|
||||||
|
census_draft = replace(
|
||||||
|
template.census,
|
||||||
|
population_digest=declaration.population_digest,
|
||||||
|
payload_digest="0" * 64,
|
||||||
|
review=provisional,
|
||||||
|
census_digest="0" * 64,
|
||||||
|
)
|
||||||
|
census_payload = derive_evaluation_census_payload_digest(census_draft)
|
||||||
|
census = replace(census_draft, payload_digest=census_payload, review=_attestation(census_payload, producer, reviewer, disclosure))
|
||||||
|
census = replace(census, census_digest=derive_evaluation_census_digest(census))
|
||||||
|
readiness = tuple(context_case.readiness)
|
||||||
|
custody_head = context_case.port.current_log(declaration.population_digest)
|
||||||
|
manifest_draft = EvaluationPlanManifestV3(
|
||||||
|
declaration,
|
||||||
|
derive_evaluation_baselines(declaration),
|
||||||
|
census,
|
||||||
|
tuple(sorted(item.result_digest for item in readiness)),
|
||||||
|
custody_head.log_digest,
|
||||||
|
"0" * 64,
|
||||||
|
provisional,
|
||||||
|
"0" * 64,
|
||||||
|
)
|
||||||
|
payload = derive_evaluation_plan_manifest_payload_digest(manifest_draft)
|
||||||
|
manifest = replace(manifest_draft, payload_digest=payload, review=_attestation(payload, producer, reviewer, disclosure))
|
||||||
|
manifest = replace(manifest, manifest_digest=derive_evaluation_plan_manifest_digest(manifest))
|
||||||
|
return context_case, manifest, readiness
|
||||||
|
|
||||||
|
|
||||||
|
def _authority_case():
|
||||||
|
context_case, plan, readiness = _reviewed_plan()
|
||||||
|
policy = context_case.context.admission_review_policy
|
||||||
|
feasibility = derive_evaluation_feasibility(plan, policy, context_case.resolver)
|
||||||
|
configurations = {row.arm_id: row for row in plan.declaration.arm_configurations}
|
||||||
|
observations = []
|
||||||
|
sealed = []
|
||||||
|
artifacts = dict(context_case.resolver.artifacts)
|
||||||
|
for index, slot in enumerate(plan.declaration.expected_observation_slots):
|
||||||
|
observation = _observation(slot, configurations[slot.arm_id], case_number=index if index % 2 else None)
|
||||||
|
reference, resolved = _seal(observation, f"authority-observation-{index}")
|
||||||
|
observations.append(observation)
|
||||||
|
sealed.append(reference)
|
||||||
|
artifacts[reference.observation.artifact_id] = resolved
|
||||||
|
|
||||||
|
projection, projection_resolver = _projection(artifact_id="authority-status")
|
||||||
|
artifacts.update(projection_resolver.artifacts)
|
||||||
|
identity = derive_attempt_identity(plan)
|
||||||
|
access = ObservationAccessPolicyV3(tuple(PublicStatusFieldV3), False, False, "")
|
||||||
|
predecessor = None
|
||||||
|
event_refs = []
|
||||||
|
for index in range(2):
|
||||||
|
event = _event(
|
||||||
|
identity,
|
||||||
|
PreSpendDispositionV3.SEALED,
|
||||||
|
access,
|
||||||
|
projection,
|
||||||
|
predecessor,
|
||||||
|
plan.review.conflict_disclosure,
|
||||||
|
policy.reviewed_reviewer_digests[0],
|
||||||
|
producer=_digest(f"authority-attempt-producer-{index}"),
|
||||||
|
)
|
||||||
|
event_ref, resolved = _reference(event, f"authority-attempt-{index}")
|
||||||
|
event_refs.append(event_ref)
|
||||||
|
artifacts[event_ref.event.artifact_id] = resolved
|
||||||
|
predecessor = event.event_digest
|
||||||
|
history = AttemptHistoryV3(plan.declaration.population_digest, tuple(event_refs), predecessor, "")
|
||||||
|
review_producer = _digest("admission-authority-producer")
|
||||||
|
provisional_review = _attestation(
|
||||||
|
"0" * 64,
|
||||||
|
review_producer,
|
||||||
|
policy.reviewed_reviewer_digests[0],
|
||||||
|
plan.review.conflict_disclosure,
|
||||||
|
)
|
||||||
|
draft = EvaluationAdmissionManifestV3(
|
||||||
|
plan,
|
||||||
|
feasibility.feasibility_digest,
|
||||||
|
tuple(sealed),
|
||||||
|
tuple(sorted(item.result_digest for item in readiness)),
|
||||||
|
context_case.port.current_log(plan.declaration.population_digest).log_digest,
|
||||||
|
history,
|
||||||
|
"0" * 64,
|
||||||
|
provisional_review,
|
||||||
|
"0" * 64,
|
||||||
|
)
|
||||||
|
payload = derive_evaluation_admission_manifest_payload_digest(draft)
|
||||||
|
reviewed = replace(
|
||||||
|
draft,
|
||||||
|
payload_digest=payload,
|
||||||
|
review=_attestation(payload, review_producer, policy.reviewed_reviewer_digests[0], plan.review.conflict_disclosure),
|
||||||
|
)
|
||||||
|
manifest = replace(reviewed, manifest_digest=derive_evaluation_admission_manifest_digest(reviewed))
|
||||||
|
resolver = InMemoryContentResolver(artifacts)
|
||||||
|
return context_case, manifest, feasibility, readiness, tuple(observations), resolver
|
||||||
|
|
||||||
|
|
||||||
|
def _authorize(case):
|
||||||
|
context_case, manifest, feasibility, readiness, _, resolver = case
|
||||||
|
return authorize_evaluation_admission(
|
||||||
|
manifest,
|
||||||
|
feasibility,
|
||||||
|
readiness,
|
||||||
|
context_case.context,
|
||||||
|
context_case.port,
|
||||||
|
resolver,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _resign(manifest: EvaluationAdmissionManifestV3, **changes) -> EvaluationAdmissionManifestV3:
|
||||||
|
draft = replace(
|
||||||
|
manifest,
|
||||||
|
**changes,
|
||||||
|
payload_digest="0" * 64,
|
||||||
|
review=replace(manifest.review, subject_digest="0" * 64, attestation_digest="0" * 64),
|
||||||
|
manifest_digest="0" * 64,
|
||||||
|
)
|
||||||
|
payload = derive_evaluation_admission_manifest_payload_digest(draft)
|
||||||
|
reviewed = replace(
|
||||||
|
draft,
|
||||||
|
payload_digest=payload,
|
||||||
|
review=_attestation(
|
||||||
|
payload,
|
||||||
|
manifest.review.producer_identity,
|
||||||
|
manifest.review.reviewer_identity,
|
||||||
|
manifest.review.conflict_disclosure,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
return replace(reviewed, manifest_digest=derive_evaluation_admission_manifest_digest(reviewed))
|
||||||
|
|
||||||
|
|
||||||
|
def test_complete_reviewed_graph_derives_one_exact_authority() -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
authority = _authorize(case)
|
||||||
|
context_case, manifest, feasibility, readiness, observations, _ = case
|
||||||
|
assert authority.admission_manifest_digest == manifest.manifest_digest
|
||||||
|
assert authority.plan_manifest_digest == manifest.plan_manifest.manifest_digest
|
||||||
|
assert authority.baseline_digests == tuple(item.baseline_digest for item in manifest.plan_manifest.baselines)
|
||||||
|
assert authority.feasibility_digest == feasibility.feasibility_digest
|
||||||
|
assert authority.validation_context_digest == context_case.context.context_digest
|
||||||
|
assert authority.custody_log_digest == context_case.port.current_log(manifest.plan_manifest.declaration.population_digest).log_digest
|
||||||
|
assert authority.attempt_history_head_digest == manifest.attempt_history.head_event_digest
|
||||||
|
assert authority.admission_review_digest == manifest.review.attestation_digest
|
||||||
|
assert authority.authority_digest
|
||||||
|
assert len(observations) == len(manifest.observations)
|
||||||
|
assert tuple(sorted(item.result_digest for item in readiness)) == manifest.readiness_digests
|
||||||
|
|
||||||
|
|
||||||
|
def test_authority_contracts_are_exact_v3_and_pre_consumer_non_authorizing() -> None:
|
||||||
|
manifest_fields = {item.name for item in fields(EvaluationAdmissionManifestV3)}
|
||||||
|
authority_fields = {item.name for item in fields(EvaluationAdmissionAuthorityV3)}
|
||||||
|
assert manifest_fields == {
|
||||||
|
"plan_manifest",
|
||||||
|
"feasibility_digest",
|
||||||
|
"observations",
|
||||||
|
"readiness_digests",
|
||||||
|
"custody_log_digest",
|
||||||
|
"attempt_history",
|
||||||
|
"payload_digest",
|
||||||
|
"review",
|
||||||
|
"manifest_digest",
|
||||||
|
}
|
||||||
|
assert authority_fields == {
|
||||||
|
"admission_manifest_digest",
|
||||||
|
"plan_manifest_digest",
|
||||||
|
"baseline_digests",
|
||||||
|
"feasibility_digest",
|
||||||
|
"observation_set_digest",
|
||||||
|
"readiness_set_digest",
|
||||||
|
"validation_context_digest",
|
||||||
|
"custody_log_digest",
|
||||||
|
"attempt_history_head_digest",
|
||||||
|
"admission_review_digest",
|
||||||
|
"authority_digest",
|
||||||
|
}
|
||||||
|
assert issubclass(EvaluationAdmissionManifestV3, NonAuthorizingV3)
|
||||||
|
assert issubclass(EvaluationAdmissionAuthorityV3, NonAuthorizingV3)
|
||||||
|
assert not hasattr(_authorize(_authority_case()), "authorize_consumer")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("part", ("plan", "feasibility", "observation", "attempt", "readiness"))
|
||||||
|
def test_substituted_or_incomplete_reviewed_sources_fail_before_authority(part: str) -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
context_case, manifest, feasibility, readiness, observations, resolver = case
|
||||||
|
if part == "plan":
|
||||||
|
changed_plan = replace(manifest.plan_manifest, manifest_digest=_digest("other-plan"))
|
||||||
|
manifest = _resign(manifest, plan_manifest=changed_plan)
|
||||||
|
elif part == "feasibility":
|
||||||
|
manifest = _resign(manifest, feasibility_digest=_digest("other-feasibility"))
|
||||||
|
elif part == "observation":
|
||||||
|
manifest = _resign(manifest, observations=manifest.observations[:-1])
|
||||||
|
elif part == "attempt":
|
||||||
|
history = replace(manifest.attempt_history, head_event_digest=manifest.attempt_history.events[0].event_digest, history_digest="")
|
||||||
|
manifest = _resign(manifest, attempt_history=history)
|
||||||
|
else:
|
||||||
|
readiness = readiness[:-1]
|
||||||
|
changed = context_case, manifest, feasibility, readiness, observations, resolver
|
||||||
|
with pytest.raises((EvaluationAuthorityError, ValueError)):
|
||||||
|
_authorize(changed)
|
||||||
|
|
||||||
|
|
||||||
|
def test_changed_observation_bytes_are_rechecked_at_authority_time() -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
context_case, manifest, feasibility, readiness, observations, resolver = case
|
||||||
|
first = manifest.observations[0].observation
|
||||||
|
resolved = resolver.artifacts[first.artifact_id]
|
||||||
|
changed_resolver = InMemoryContentResolver({**resolver.artifacts, first.artifact_id: replace(resolved, content=resolved.content[:-1])})
|
||||||
|
with pytest.raises(canonical.ArtifactValidationError):
|
||||||
|
_authorize((context_case, manifest, feasibility, readiness, observations, changed_resolver))
|
||||||
|
|
||||||
|
|
||||||
|
def test_admission_review_must_be_independent_subject_bound_and_resolvable() -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
context_case, manifest, feasibility, readiness, observations, resolver = case
|
||||||
|
self_review = replace(
|
||||||
|
manifest.review,
|
||||||
|
producer_identity=manifest.review.reviewer_identity,
|
||||||
|
attestation_digest="0" * 64,
|
||||||
|
)
|
||||||
|
with pytest.raises(EvaluationAuthorityError, match="independent"):
|
||||||
|
_authorize((context_case, replace(manifest, review=self_review), feasibility, readiness, observations, resolver))
|
||||||
|
missing = InMemoryContentResolver(
|
||||||
|
{key: value for key, value in resolver.artifacts.items() if key != manifest.review.conflict_disclosure.artifact_id}
|
||||||
|
)
|
||||||
|
with pytest.raises(canonical.ArtifactResolutionError):
|
||||||
|
_authorize((context_case, manifest, feasibility, readiness, observations, missing))
|
||||||
|
|
||||||
|
|
||||||
|
def test_current_context_and_available_custody_head_are_revalidated() -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
context_case, manifest, feasibility, readiness, observations, resolver = case
|
||||||
|
changed_context = replace(context_case.context, context_digest=_digest("other-context"))
|
||||||
|
with pytest.raises(EvaluationAuthorityError, match="context"):
|
||||||
|
authorize_evaluation_admission(
|
||||||
|
manifest,
|
||||||
|
feasibility,
|
||||||
|
readiness,
|
||||||
|
changed_context,
|
||||||
|
context_case.port,
|
||||||
|
resolver,
|
||||||
|
)
|
||||||
|
|
||||||
|
population = manifest.plan_manifest.declaration.population_digest
|
||||||
|
current = context_case.port.current_log(population)
|
||||||
|
request = PopulationUseRequest(
|
||||||
|
population,
|
||||||
|
"unrelated-arm",
|
||||||
|
LineagePurpose.PROMOTION,
|
||||||
|
False,
|
||||||
|
_digest("unrelated-artifact"),
|
||||||
|
current.log_digest,
|
||||||
|
)
|
||||||
|
context_case.port.append(request, current.custodian_identity)
|
||||||
|
assert context_case.port.current_log(population).status is PopulationStatus.AVAILABLE
|
||||||
|
with pytest.raises(EvaluationAuthorityError, match="custody|stale"):
|
||||||
|
_authorize(case)
|
||||||
|
|
||||||
|
|
||||||
|
def test_spent_or_availability_restored_custody_never_produces_authority() -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
context_case, manifest, _, _, _, _ = case
|
||||||
|
population = manifest.plan_manifest.declaration.population_digest
|
||||||
|
current = context_case.port.current_log(population)
|
||||||
|
request = PopulationUseRequest(
|
||||||
|
population,
|
||||||
|
"planned-evaluation",
|
||||||
|
LineagePurpose.ADMISSION_LABEL_OPEN,
|
||||||
|
True,
|
||||||
|
manifest.manifest_digest,
|
||||||
|
current.log_digest,
|
||||||
|
)
|
||||||
|
context_case.port.append(request, current.custodian_identity)
|
||||||
|
assert context_case.port.current_log(population).status is PopulationStatus.SPENT
|
||||||
|
with pytest.raises(EvaluationAuthorityError, match="available|custody|stale"):
|
||||||
|
_authorize(case)
|
||||||
Reference in New Issue
Block a user