mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-04 02:38:18 +00:00
feat(detection-core): bind atomic evaluation spend
This commit is contained in:
@@ -520,7 +520,7 @@ contract work and use synthetic/in-memory workflow boundaries only.
|
|||||||
- _Requirements: 17.3, 17.4, 20.2_
|
- _Requirements: 17.3, 17.4, 20.2_
|
||||||
- _Boundary: Evaluation Feasibility_
|
- _Boundary: Evaluation Feasibility_
|
||||||
|
|
||||||
- [ ] 14. Seal planned observations and authorize one atomic spend
|
- [x] 14. Seal planned observations and authorize one atomic spend
|
||||||
- [x] 14.1 Validate exact nonzero observations against every expected slot
|
- [x] 14.1 Validate exact nonzero observations against every expected slot
|
||||||
- Represent label-blind observation headers that bind slot, arm, input, execution, registered budget, configuration, input-read evidence, stage-execution evidence, cases, and proof candidates.
|
- Represent label-blind observation headers that bind slot, arm, input, execution, registered budget, configuration, input-read evidence, stage-execution evidence, cases, and proof candidates.
|
||||||
- Resolve every sealed observation through trusted content references and prove its bytes are nonzero before accepting its header.
|
- Resolve every sealed observation through trusted content references and prove its bytes are nonzero before accepting its header.
|
||||||
@@ -547,7 +547,7 @@ contract work and use synthetic/in-memory workflow boundaries only.
|
|||||||
- _Requirements: 17.5, 17.6, 17.8, 17.10, 17.11, 17.12_
|
- _Requirements: 17.5, 17.6, 17.8, 17.10, 17.11, 17.12_
|
||||||
- _Boundary: Evaluation Authority_
|
- _Boundary: Evaluation Authority_
|
||||||
- _Depends: 13.4, 14.1, 14.2_
|
- _Depends: 13.4, 14.1, 14.2_
|
||||||
- [ ] 14.4 Bind every planned arm to one preparation and custody receipt
|
- [x] 14.4 Bind every planned arm to one preparation and custody receipt
|
||||||
- Construct one head-bound label-release request whose influenced artifact transitively commits to every planned baseline and exact authority.
|
- Construct one head-bound label-release request whose influenced artifact transitively commits to every planned baseline and exact authority.
|
||||||
- Validate only the exact monotonic available-to-spent successor and derive an explicit all-arm binding between preparation, receipt, and planned baselines.
|
- Validate only the exact monotonic available-to-spent successor and derive an explicit all-arm binding between preparation, receipt, and planned baselines.
|
||||||
- Reject subset preparations, missing or mismatched receipts, stale concurrent attempts, broken custody chains, and any later restoration of availability.
|
- Reject subset preparations, missing or mismatched receipts, stale concurrent attempts, broken custody chains, and any later restoration of availability.
|
||||||
@@ -719,6 +719,7 @@ contract work and use synthetic/in-memory workflow boundaries only.
|
|||||||
- 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.
|
- 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.
|
||||||
|
- Task 14.4 validates the complete manifest/authority/preparation graph before external CAS and again after append, so only the current direct available-to-spent receipt can bind every planned baseline and self-consistent subset authorities remain unspendable.
|
||||||
- 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,343 @@
|
|||||||
|
"""Pure all-arm preparation and shared-spend binding for evaluation admission V3."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import ClassVar
|
||||||
|
|
||||||
|
from openultrasast.core import canonical
|
||||||
|
from openultrasast.core.admission_context import AdmissionValidationContext, _validate_context
|
||||||
|
from openultrasast.core.custody import (
|
||||||
|
LineagePurpose,
|
||||||
|
PopulationCustodyLog,
|
||||||
|
PopulationCustodyPort,
|
||||||
|
PopulationStatus,
|
||||||
|
PopulationUseReceipt,
|
||||||
|
PopulationUseRequest,
|
||||||
|
derive_population_use_request_digest,
|
||||||
|
validate_population_custody_log,
|
||||||
|
)
|
||||||
|
from openultrasast.core.evaluation_authority import (
|
||||||
|
EvaluationAdmissionAuthorityV3,
|
||||||
|
EvaluationAdmissionManifestV3,
|
||||||
|
derive_evaluation_admission_manifest_digest,
|
||||||
|
derive_evaluation_admission_manifest_payload_digest,
|
||||||
|
derive_evaluation_observation_set_digest,
|
||||||
|
derive_evaluation_readiness_set_digest,
|
||||||
|
)
|
||||||
|
from openultrasast.core.evaluation_version import (
|
||||||
|
EVALUATION_ADMISSION_V3_SCHEMA,
|
||||||
|
EvaluationV3Contract,
|
||||||
|
NonAuthorizingV3,
|
||||||
|
require_exact_v3_contract,
|
||||||
|
)
|
||||||
|
|
||||||
|
_DIGEST = re.compile(r"[0-9a-f]{64}")
|
||||||
|
_PREPARATION_SCHEMA = "openultrasast.detection-core.evaluation-preparation.v3"
|
||||||
|
_SPEND_BINDING_SCHEMA = "openultrasast.detection-core.evaluation-spend-binding.v3"
|
||||||
|
|
||||||
|
|
||||||
|
class EvaluationSharedSpendError(ValueError):
|
||||||
|
"""A V3 preparation or all-arm shared-spend binding is invalid."""
|
||||||
|
|
||||||
|
|
||||||
|
def _digest(value: object, field: str) -> str:
|
||||||
|
if type(value) is not str or _DIGEST.fullmatch(value) is None:
|
||||||
|
raise EvaluationSharedSpendError(f"{field} must be a full lowercase SHA-256 digest")
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def _ordered_digests(value: object, field: str) -> tuple[str, ...]:
|
||||||
|
if type(value) is not tuple or not value:
|
||||||
|
raise EvaluationSharedSpendError(f"{field} must be a nonempty exact tuple")
|
||||||
|
for item in value:
|
||||||
|
_digest(item, field)
|
||||||
|
if len(set(value)) != len(value):
|
||||||
|
raise EvaluationSharedSpendError(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 EvaluationSharedSpendError(f"{field} does not match canonical content")
|
||||||
|
|
||||||
|
|
||||||
|
def _preparation_value(value: EvaluationPreparationV3) -> canonical.JsonValue:
|
||||||
|
return {
|
||||||
|
"schema_version": _PREPARATION_SCHEMA,
|
||||||
|
"authority_digest": value.authority.authority_digest,
|
||||||
|
"label_release_request_digest": derive_population_use_request_digest(value.label_release_request),
|
||||||
|
"planned_baseline_digests": value.planned_baseline_digests,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class EvaluationPreparationV3(EvaluationV3Contract, NonAuthorizingV3):
|
||||||
|
"""Pure head-bound request covering every baseline in one authority."""
|
||||||
|
|
||||||
|
__evaluation_v3_contract_schema__: ClassVar[str] = EVALUATION_ADMISSION_V3_SCHEMA
|
||||||
|
authority: EvaluationAdmissionAuthorityV3
|
||||||
|
label_release_request: PopulationUseRequest
|
||||||
|
planned_baseline_digests: tuple[str, ...]
|
||||||
|
preparation_digest: str
|
||||||
|
|
||||||
|
def __post_init__(self) -> None:
|
||||||
|
require_exact_v3_contract(self, EvaluationPreparationV3, field="evaluation preparation")
|
||||||
|
if type(self.authority) is not EvaluationAdmissionAuthorityV3:
|
||||||
|
raise EvaluationSharedSpendError("preparation authority must use exact EvaluationAdmissionAuthorityV3")
|
||||||
|
self.authority.__post_init__()
|
||||||
|
if type(self.label_release_request) is not PopulationUseRequest:
|
||||||
|
raise EvaluationSharedSpendError("label-release request must use exact PopulationUseRequest")
|
||||||
|
self.label_release_request.__post_init__()
|
||||||
|
baselines = _ordered_digests(self.planned_baseline_digests, "planned baseline digest")
|
||||||
|
if baselines != self.authority.baseline_digests:
|
||||||
|
raise EvaluationSharedSpendError("planned baseline set must equal the exact authority baseline set")
|
||||||
|
request = self.label_release_request
|
||||||
|
expected = (
|
||||||
|
self.authority.plan_manifest_digest,
|
||||||
|
LineagePurpose.ADMISSION_LABEL_OPEN,
|
||||||
|
True,
|
||||||
|
self.authority.authority_digest,
|
||||||
|
self.authority.custody_log_digest,
|
||||||
|
)
|
||||||
|
actual = (
|
||||||
|
request.arm_id,
|
||||||
|
request.purpose,
|
||||||
|
request.labels_accessed,
|
||||||
|
request.influenced_artifact_digest,
|
||||||
|
request.expected_log_digest,
|
||||||
|
)
|
||||||
|
if actual != expected:
|
||||||
|
raise EvaluationSharedSpendError("label-release request does not match the exact authority")
|
||||||
|
_bind(self, "preparation_digest", canonical.content_digest(_preparation_value(self)))
|
||||||
|
|
||||||
|
|
||||||
|
def derive_evaluation_preparation_digest(value: EvaluationPreparationV3) -> str:
|
||||||
|
if type(value) is not EvaluationPreparationV3:
|
||||||
|
raise EvaluationSharedSpendError("preparation must use exact EvaluationPreparationV3")
|
||||||
|
value.__post_init__()
|
||||||
|
return canonical.content_digest(_preparation_value(value))
|
||||||
|
|
||||||
|
|
||||||
|
def _binding_value(value: EvaluationSpendBindingV3) -> canonical.JsonValue:
|
||||||
|
return {
|
||||||
|
"schema_version": _SPEND_BINDING_SCHEMA,
|
||||||
|
"preparation_digest": value.preparation_digest,
|
||||||
|
"receipt_digest": value.receipt_digest,
|
||||||
|
"planned_baseline_digests": value.planned_baseline_digests,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class EvaluationSpendBindingV3(EvaluationV3Contract, NonAuthorizingV3):
|
||||||
|
"""Exact current spend receipt bound to the complete planned baseline set."""
|
||||||
|
|
||||||
|
__evaluation_v3_contract_schema__: ClassVar[str] = EVALUATION_ADMISSION_V3_SCHEMA
|
||||||
|
preparation_digest: str
|
||||||
|
receipt_digest: str
|
||||||
|
planned_baseline_digests: tuple[str, ...]
|
||||||
|
binding_digest: str
|
||||||
|
|
||||||
|
def __post_init__(self) -> None:
|
||||||
|
require_exact_v3_contract(self, EvaluationSpendBindingV3, field="evaluation spend binding")
|
||||||
|
_digest(self.preparation_digest, "spend preparation digest")
|
||||||
|
_digest(self.receipt_digest, "spend receipt digest")
|
||||||
|
_ordered_digests(self.planned_baseline_digests, "spend planned baseline digest")
|
||||||
|
_bind(self, "binding_digest", canonical.content_digest(_binding_value(self)))
|
||||||
|
|
||||||
|
|
||||||
|
def derive_evaluation_spend_binding_digest(value: EvaluationSpendBindingV3) -> str:
|
||||||
|
if type(value) is not EvaluationSpendBindingV3:
|
||||||
|
raise EvaluationSharedSpendError("spend binding must use exact EvaluationSpendBindingV3")
|
||||||
|
value.__post_init__()
|
||||||
|
return canonical.content_digest(_binding_value(value))
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_authority_binding(
|
||||||
|
manifest: EvaluationAdmissionManifestV3,
|
||||||
|
authority: EvaluationAdmissionAuthorityV3,
|
||||||
|
context: AdmissionValidationContext,
|
||||||
|
) -> None:
|
||||||
|
manifest.__post_init__()
|
||||||
|
authority.__post_init__()
|
||||||
|
if manifest.payload_digest != derive_evaluation_admission_manifest_payload_digest(manifest):
|
||||||
|
raise EvaluationSharedSpendError("admission manifest payload is not canonical")
|
||||||
|
if manifest.manifest_digest != derive_evaluation_admission_manifest_digest(manifest):
|
||||||
|
raise EvaluationSharedSpendError("admission manifest digest is not canonical")
|
||||||
|
plan = manifest.plan_manifest
|
||||||
|
expected = (
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
actual = (
|
||||||
|
authority.admission_manifest_digest,
|
||||||
|
authority.plan_manifest_digest,
|
||||||
|
authority.baseline_digests,
|
||||||
|
authority.feasibility_digest,
|
||||||
|
authority.observation_set_digest,
|
||||||
|
authority.readiness_set_digest,
|
||||||
|
authority.validation_context_digest,
|
||||||
|
authority.custody_log_digest,
|
||||||
|
authority.attempt_history_head_digest,
|
||||||
|
authority.admission_review_digest,
|
||||||
|
)
|
||||||
|
if actual != expected:
|
||||||
|
raise EvaluationSharedSpendError("authority does not match the complete reviewed admission graph")
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_preparation_graph(
|
||||||
|
manifest: EvaluationAdmissionManifestV3,
|
||||||
|
preparation: EvaluationPreparationV3,
|
||||||
|
context: AdmissionValidationContext,
|
||||||
|
) -> str:
|
||||||
|
if type(manifest) is not EvaluationAdmissionManifestV3 or type(preparation) is not EvaluationPreparationV3:
|
||||||
|
raise EvaluationSharedSpendError("manifest and preparation must use exact V3 contract types")
|
||||||
|
if type(context) is not AdmissionValidationContext:
|
||||||
|
raise EvaluationSharedSpendError("validation context must use exact AdmissionValidationContext")
|
||||||
|
_validate_context(context)
|
||||||
|
preparation.__post_init__()
|
||||||
|
_validate_authority_binding(manifest, preparation.authority, context)
|
||||||
|
population = manifest.plan_manifest.declaration.population_digest
|
||||||
|
if preparation.label_release_request.population_digest != population:
|
||||||
|
raise EvaluationSharedSpendError("label-release request population differs from the reviewed plan")
|
||||||
|
return population
|
||||||
|
|
||||||
|
|
||||||
|
def validate_evaluation_preparation(
|
||||||
|
manifest: EvaluationAdmissionManifestV3,
|
||||||
|
preparation: EvaluationPreparationV3,
|
||||||
|
context: AdmissionValidationContext,
|
||||||
|
custody: PopulationCustodyPort,
|
||||||
|
) -> None:
|
||||||
|
"""Validate the exact graph and current available head before an external CAS."""
|
||||||
|
try:
|
||||||
|
population = _validate_preparation_graph(manifest, preparation, context)
|
||||||
|
current = custody.current_log(population)
|
||||||
|
validate_population_custody_log(current, context.evidence.custody_policy, current=custody)
|
||||||
|
if current.log_digest != preparation.authority.custody_log_digest or current.status is not PopulationStatus.AVAILABLE:
|
||||||
|
raise EvaluationSharedSpendError("preparation custody head is stale or unavailable")
|
||||||
|
latest = custody.current_log(population)
|
||||||
|
if latest.log_digest != current.log_digest or latest.status is not PopulationStatus.AVAILABLE:
|
||||||
|
raise EvaluationSharedSpendError("custody changed during evaluation preparation validation")
|
||||||
|
except EvaluationSharedSpendError:
|
||||||
|
raise
|
||||||
|
except (AttributeError, LookupError, TypeError, ValueError) as exc:
|
||||||
|
raise EvaluationSharedSpendError(f"evaluation preparation validation refused: {exc}") from exc
|
||||||
|
|
||||||
|
|
||||||
|
def prepare_evaluation_spend(
|
||||||
|
manifest: EvaluationAdmissionManifestV3,
|
||||||
|
authority: EvaluationAdmissionAuthorityV3,
|
||||||
|
context: AdmissionValidationContext,
|
||||||
|
custody: PopulationCustodyPort,
|
||||||
|
) -> EvaluationPreparationV3:
|
||||||
|
"""Build a pure all-arm request against the exact current available head."""
|
||||||
|
try:
|
||||||
|
if type(manifest) is not EvaluationAdmissionManifestV3 or type(authority) is not EvaluationAdmissionAuthorityV3:
|
||||||
|
raise EvaluationSharedSpendError("manifest and authority must use exact V3 contract types")
|
||||||
|
if type(context) is not AdmissionValidationContext:
|
||||||
|
raise EvaluationSharedSpendError("validation context must use exact AdmissionValidationContext")
|
||||||
|
_validate_context(context)
|
||||||
|
_validate_authority_binding(manifest, authority, context)
|
||||||
|
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.log_digest != authority.custody_log_digest or current.status is not PopulationStatus.AVAILABLE:
|
||||||
|
raise EvaluationSharedSpendError("preparation custody head is stale or unavailable")
|
||||||
|
request = PopulationUseRequest(
|
||||||
|
population,
|
||||||
|
authority.plan_manifest_digest,
|
||||||
|
LineagePurpose.ADMISSION_LABEL_OPEN,
|
||||||
|
True,
|
||||||
|
authority.authority_digest,
|
||||||
|
authority.custody_log_digest,
|
||||||
|
)
|
||||||
|
preparation = EvaluationPreparationV3(authority, request, authority.baseline_digests, "")
|
||||||
|
validate_evaluation_preparation(manifest, preparation, context, custody)
|
||||||
|
return preparation
|
||||||
|
except EvaluationSharedSpendError:
|
||||||
|
raise
|
||||||
|
except (AttributeError, LookupError, TypeError, ValueError) as exc:
|
||||||
|
raise EvaluationSharedSpendError(f"evaluation preparation refused: {exc}") from exc
|
||||||
|
|
||||||
|
|
||||||
|
def _exact_successor_receipt(
|
||||||
|
preparation: EvaluationPreparationV3,
|
||||||
|
receipt: PopulationUseReceipt | None,
|
||||||
|
) -> PopulationUseReceipt:
|
||||||
|
if receipt is None:
|
||||||
|
raise EvaluationSharedSpendError("shared-spend receipt is missing")
|
||||||
|
if type(receipt) is not PopulationUseReceipt:
|
||||||
|
raise EvaluationSharedSpendError("shared-spend receipt must use exact PopulationUseReceipt")
|
||||||
|
if receipt.prior_status is not PopulationStatus.AVAILABLE or receipt.status is not PopulationStatus.SPENT:
|
||||||
|
raise EvaluationSharedSpendError("shared-spend receipt must change available to spent")
|
||||||
|
if receipt.request != preparation.label_release_request:
|
||||||
|
raise EvaluationSharedSpendError("shared-spend receipt does not match the preparation")
|
||||||
|
return receipt
|
||||||
|
|
||||||
|
|
||||||
|
def _current_spent_successor(
|
||||||
|
preparation: EvaluationPreparationV3,
|
||||||
|
receipt: PopulationUseReceipt,
|
||||||
|
context: AdmissionValidationContext,
|
||||||
|
custody: PopulationCustodyPort,
|
||||||
|
) -> PopulationCustodyLog:
|
||||||
|
population = preparation.label_release_request.population_digest
|
||||||
|
current = custody.current_log(population)
|
||||||
|
validate_population_custody_log(current, context.evidence.custody_policy, current=custody)
|
||||||
|
is_head = bool(current.receipts) and current.receipts[-1] == receipt
|
||||||
|
if not is_head or current.receipt_count != receipt.sequence or current.status is not PopulationStatus.SPENT:
|
||||||
|
raise EvaluationSharedSpendError("shared-spend receipt is not the current-chain direct successor")
|
||||||
|
return current
|
||||||
|
|
||||||
|
|
||||||
|
def bind_evaluation_spend(
|
||||||
|
manifest: EvaluationAdmissionManifestV3,
|
||||||
|
preparation: EvaluationPreparationV3,
|
||||||
|
receipt: PopulationUseReceipt | None,
|
||||||
|
context: AdmissionValidationContext,
|
||||||
|
custody: PopulationCustodyPort,
|
||||||
|
) -> EvaluationSpendBindingV3:
|
||||||
|
"""Bind only the exact current AVAILABLE-to-SPENT successor for all arms."""
|
||||||
|
try:
|
||||||
|
population = _validate_preparation_graph(manifest, preparation, context)
|
||||||
|
exact_receipt = _exact_successor_receipt(preparation, receipt)
|
||||||
|
current = _current_spent_successor(preparation, exact_receipt, context, custody)
|
||||||
|
binding = EvaluationSpendBindingV3(
|
||||||
|
preparation.preparation_digest,
|
||||||
|
exact_receipt.receipt_digest,
|
||||||
|
preparation.planned_baseline_digests,
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
latest = custody.current_log(population)
|
||||||
|
if latest.log_digest != current.log_digest or latest.status is not PopulationStatus.SPENT:
|
||||||
|
raise EvaluationSharedSpendError("shared-spend receipt is no longer the current-chain direct successor")
|
||||||
|
return binding
|
||||||
|
except EvaluationSharedSpendError:
|
||||||
|
raise
|
||||||
|
except (AttributeError, LookupError, TypeError, ValueError) as exc:
|
||||||
|
raise EvaluationSharedSpendError(f"evaluation shared spend refused: {exc}") from exc
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"EvaluationPreparationV3",
|
||||||
|
"EvaluationSharedSpendError",
|
||||||
|
"EvaluationSpendBindingV3",
|
||||||
|
"bind_evaluation_spend",
|
||||||
|
"derive_evaluation_preparation_digest",
|
||||||
|
"derive_evaluation_spend_binding_digest",
|
||||||
|
"prepare_evaluation_spend",
|
||||||
|
"validate_evaluation_preparation",
|
||||||
|
]
|
||||||
@@ -0,0 +1,344 @@
|
|||||||
|
"""Atomic all-arm shared-spend binding for evaluation admission V3."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import fields, replace
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from test_core_evaluation_authority import _authority_case, _authorize
|
||||||
|
from test_core_evaluation_plan import _digest
|
||||||
|
|
||||||
|
from openultrasast.core import custody
|
||||||
|
from openultrasast.core.evaluation_authority import EvaluationAdmissionAuthorityV3
|
||||||
|
from openultrasast.core.evaluation_spend import (
|
||||||
|
EvaluationPreparationV3,
|
||||||
|
EvaluationSharedSpendError,
|
||||||
|
EvaluationSpendBindingV3,
|
||||||
|
bind_evaluation_spend,
|
||||||
|
derive_evaluation_preparation_digest,
|
||||||
|
prepare_evaluation_spend,
|
||||||
|
validate_evaluation_preparation,
|
||||||
|
)
|
||||||
|
from openultrasast.core.evaluation_version import NonAuthorizingV3
|
||||||
|
|
||||||
|
|
||||||
|
def _prepare(case):
|
||||||
|
context_case, manifest, _, _, _, _ = case
|
||||||
|
authority = _authorize(case)
|
||||||
|
preparation = prepare_evaluation_spend(
|
||||||
|
manifest,
|
||||||
|
authority,
|
||||||
|
context_case.context,
|
||||||
|
context_case.port,
|
||||||
|
)
|
||||||
|
return authority, preparation
|
||||||
|
|
||||||
|
|
||||||
|
def _spend(case, preparation: EvaluationPreparationV3):
|
||||||
|
context_case = case[0]
|
||||||
|
validate_evaluation_preparation(
|
||||||
|
case[1],
|
||||||
|
preparation,
|
||||||
|
context_case.context,
|
||||||
|
context_case.port,
|
||||||
|
)
|
||||||
|
return _append_without_precheck(case, preparation)
|
||||||
|
|
||||||
|
|
||||||
|
def _append_without_precheck(case, preparation: EvaluationPreparationV3):
|
||||||
|
context_case = case[0]
|
||||||
|
request = preparation.label_release_request
|
||||||
|
current = context_case.port.current_log(request.population_digest)
|
||||||
|
receipt = custody.record_population_use(
|
||||||
|
request,
|
||||||
|
current,
|
||||||
|
current.custodian_identity,
|
||||||
|
context_case.context.evidence.custody_policy,
|
||||||
|
)
|
||||||
|
context_case.port.logs[current.population_digest] = custody.append_population_use(
|
||||||
|
current,
|
||||||
|
receipt,
|
||||||
|
context_case.context.evidence.custody_policy,
|
||||||
|
)
|
||||||
|
return receipt
|
||||||
|
|
||||||
|
|
||||||
|
def _bind(case, preparation, receipt):
|
||||||
|
context_case = case[0]
|
||||||
|
return bind_evaluation_spend(
|
||||||
|
case[1],
|
||||||
|
preparation,
|
||||||
|
receipt,
|
||||||
|
context_case.context,
|
||||||
|
context_case.port,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_preparation_is_pure_and_binds_plan_authority_all_baselines_and_head() -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
context_case, manifest, _, _, _, _ = case
|
||||||
|
population = manifest.plan_manifest.declaration.population_digest
|
||||||
|
before = context_case.port.current_log(population)
|
||||||
|
|
||||||
|
authority, preparation = _prepare(case)
|
||||||
|
|
||||||
|
assert context_case.port.current_log(population) == before
|
||||||
|
assert preparation.authority == authority
|
||||||
|
assert preparation.planned_baseline_digests == authority.baseline_digests
|
||||||
|
assert preparation.label_release_request == custody.PopulationUseRequest(
|
||||||
|
population,
|
||||||
|
manifest.plan_manifest.manifest_digest,
|
||||||
|
custody.LineagePurpose.ADMISSION_LABEL_OPEN,
|
||||||
|
True,
|
||||||
|
authority.authority_digest,
|
||||||
|
before.log_digest,
|
||||||
|
)
|
||||||
|
assert preparation.preparation_digest == derive_evaluation_preparation_digest(preparation)
|
||||||
|
|
||||||
|
|
||||||
|
def test_shared_spend_contracts_are_exact_v3_non_consumer_authorities() -> None:
|
||||||
|
assert {item.name for item in fields(EvaluationPreparationV3)} == {
|
||||||
|
"authority",
|
||||||
|
"label_release_request",
|
||||||
|
"planned_baseline_digests",
|
||||||
|
"preparation_digest",
|
||||||
|
}
|
||||||
|
assert {item.name for item in fields(EvaluationSpendBindingV3)} == {
|
||||||
|
"preparation_digest",
|
||||||
|
"receipt_digest",
|
||||||
|
"planned_baseline_digests",
|
||||||
|
"binding_digest",
|
||||||
|
}
|
||||||
|
assert issubclass(EvaluationPreparationV3, NonAuthorizingV3)
|
||||||
|
assert issubclass(EvaluationSpendBindingV3, NonAuthorizingV3)
|
||||||
|
assert not hasattr(EvaluationSpendBindingV3, "authorize_consumer")
|
||||||
|
|
||||||
|
|
||||||
|
def test_exact_external_available_to_spent_successor_yields_all_arm_binding() -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
authority, preparation = _prepare(case)
|
||||||
|
receipt = _spend(case, preparation)
|
||||||
|
|
||||||
|
binding = _bind(case, preparation, receipt)
|
||||||
|
|
||||||
|
assert binding.preparation_digest == preparation.preparation_digest
|
||||||
|
assert binding.receipt_digest == receipt.receipt_digest
|
||||||
|
assert binding.planned_baseline_digests == authority.baseline_digests
|
||||||
|
assert binding.binding_digest
|
||||||
|
assert receipt.prior_status is custody.PopulationStatus.AVAILABLE
|
||||||
|
assert receipt.status is custody.PopulationStatus.SPENT
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("field", "value"),
|
||||||
|
(
|
||||||
|
("population_digest", _digest("other-population")),
|
||||||
|
("arm_id", _digest("other-plan")),
|
||||||
|
("purpose", custody.LineagePurpose.PROMOTION),
|
||||||
|
("labels_accessed", False),
|
||||||
|
("influenced_artifact_digest", _digest("other-authority")),
|
||||||
|
("expected_log_digest", _digest("other-head")),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def test_every_release_request_identity_mutation_is_refused(field: str, value: object) -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
authority, preparation = _prepare(case)
|
||||||
|
request = replace(preparation.label_release_request, **{field: value})
|
||||||
|
if field == "population_digest":
|
||||||
|
changed = EvaluationPreparationV3(authority, request, authority.baseline_digests, "")
|
||||||
|
with pytest.raises(EvaluationSharedSpendError, match="population"):
|
||||||
|
validate_evaluation_preparation(
|
||||||
|
case[1],
|
||||||
|
changed,
|
||||||
|
case[0].context,
|
||||||
|
case[0].port,
|
||||||
|
)
|
||||||
|
receipt = _spend(case, preparation)
|
||||||
|
with pytest.raises(EvaluationSharedSpendError):
|
||||||
|
_bind(case, changed, receipt)
|
||||||
|
return
|
||||||
|
with pytest.raises(EvaluationSharedSpendError):
|
||||||
|
EvaluationPreparationV3(authority, request, authority.baseline_digests, "")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("mutation", ("subset", "extra", "reordered"))
|
||||||
|
def test_partial_extra_or_reordered_planned_baselines_are_not_constructible(mutation: str) -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
authority, preparation = _prepare(case)
|
||||||
|
baselines = authority.baseline_digests
|
||||||
|
if mutation == "subset":
|
||||||
|
changed = baselines[:-1]
|
||||||
|
elif mutation == "extra":
|
||||||
|
changed = (*baselines, _digest("extra-baseline"))
|
||||||
|
else:
|
||||||
|
changed = tuple(reversed(baselines))
|
||||||
|
with pytest.raises(EvaluationSharedSpendError, match="baseline"):
|
||||||
|
EvaluationPreparationV3(authority, preparation.label_release_request, changed, "")
|
||||||
|
|
||||||
|
|
||||||
|
def test_substituted_authority_cannot_create_a_canonical_preparation() -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
context_case, manifest, _, _, _, _ = case
|
||||||
|
authority = _authorize(case)
|
||||||
|
changed = replace(
|
||||||
|
authority,
|
||||||
|
feasibility_digest=_digest("other-feasibility"),
|
||||||
|
authority_digest="",
|
||||||
|
)
|
||||||
|
with pytest.raises(EvaluationSharedSpendError, match="authority"):
|
||||||
|
prepare_evaluation_spend(manifest, changed, context_case.context, context_case.port)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("mutation", ("subset", "extra", "reordered"))
|
||||||
|
def test_self_consistent_mutated_authority_cannot_pass_either_spend_boundary(mutation: str) -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
authority, preparation = _prepare(case)
|
||||||
|
assert len(authority.baseline_digests) == 3
|
||||||
|
if mutation == "subset":
|
||||||
|
changed_baselines = authority.baseline_digests[:-1]
|
||||||
|
elif mutation == "extra":
|
||||||
|
changed_baselines = (*authority.baseline_digests, _digest("extra-authority-baseline"))
|
||||||
|
else:
|
||||||
|
changed_baselines = tuple(reversed(authority.baseline_digests))
|
||||||
|
changed_authority = replace(
|
||||||
|
authority,
|
||||||
|
baseline_digests=changed_baselines,
|
||||||
|
authority_digest="",
|
||||||
|
)
|
||||||
|
changed_request = replace(
|
||||||
|
preparation.label_release_request,
|
||||||
|
influenced_artifact_digest=changed_authority.authority_digest,
|
||||||
|
)
|
||||||
|
changed_preparation = EvaluationPreparationV3(
|
||||||
|
changed_authority,
|
||||||
|
changed_request,
|
||||||
|
changed_authority.baseline_digests,
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
with pytest.raises(EvaluationSharedSpendError, match="baseline|authority|manifest"):
|
||||||
|
validate_evaluation_preparation(
|
||||||
|
case[1],
|
||||||
|
changed_preparation,
|
||||||
|
case[0].context,
|
||||||
|
case[0].port,
|
||||||
|
)
|
||||||
|
receipt = _append_without_precheck(case, changed_preparation)
|
||||||
|
|
||||||
|
with pytest.raises(EvaluationSharedSpendError, match="baseline|authority|manifest"):
|
||||||
|
_bind(case, changed_preparation, receipt)
|
||||||
|
|
||||||
|
|
||||||
|
def test_missing_or_mismatched_receipt_never_yields_a_binding() -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
_, preparation = _prepare(case)
|
||||||
|
with pytest.raises(EvaluationSharedSpendError, match="missing"):
|
||||||
|
_bind(case, preparation, None)
|
||||||
|
|
||||||
|
current = case[0].port.current_log(preparation.label_release_request.population_digest)
|
||||||
|
wrong_request = replace(
|
||||||
|
preparation.label_release_request,
|
||||||
|
influenced_artifact_digest=_digest("wrong-authority"),
|
||||||
|
)
|
||||||
|
wrong = custody.record_population_use(
|
||||||
|
wrong_request,
|
||||||
|
current,
|
||||||
|
current.custodian_identity,
|
||||||
|
case[0].context.evidence.custody_policy,
|
||||||
|
)
|
||||||
|
case[0].port.logs[current.population_digest] = custody.append_population_use(
|
||||||
|
current,
|
||||||
|
wrong,
|
||||||
|
case[0].context.evidence.custody_policy,
|
||||||
|
)
|
||||||
|
with pytest.raises(EvaluationSharedSpendError, match="preparation"):
|
||||||
|
_bind(case, preparation, wrong)
|
||||||
|
|
||||||
|
|
||||||
|
def test_non_successor_and_availability_restoration_are_refused() -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
_, preparation = _prepare(case)
|
||||||
|
receipt = _spend(case, preparation)
|
||||||
|
with pytest.raises(EvaluationSharedSpendError, match="successor"):
|
||||||
|
_bind(case, preparation, replace(receipt, sequence=receipt.sequence + 1))
|
||||||
|
with pytest.raises(EvaluationSharedSpendError, match="available to spent"):
|
||||||
|
_bind(case, preparation, replace(receipt, status=custody.PopulationStatus.AVAILABLE))
|
||||||
|
|
||||||
|
|
||||||
|
def test_later_current_head_change_invalidates_the_once_winning_receipt() -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
_, preparation = _prepare(case)
|
||||||
|
winner = _spend(case, preparation)
|
||||||
|
context_case = case[0]
|
||||||
|
population = preparation.label_release_request.population_digest
|
||||||
|
current = context_case.port.current_log(population)
|
||||||
|
later_request = custody.PopulationUseRequest(
|
||||||
|
population,
|
||||||
|
"post-spend-audit",
|
||||||
|
custody.LineagePurpose.PROMOTION,
|
||||||
|
False,
|
||||||
|
_digest("post-spend-artifact"),
|
||||||
|
current.log_digest,
|
||||||
|
)
|
||||||
|
later = custody.record_population_use(
|
||||||
|
later_request,
|
||||||
|
current,
|
||||||
|
current.custodian_identity,
|
||||||
|
context_case.context.evidence.custody_policy,
|
||||||
|
)
|
||||||
|
context_case.port.logs[population] = custody.append_population_use(
|
||||||
|
current,
|
||||||
|
later,
|
||||||
|
context_case.context.evidence.custody_policy,
|
||||||
|
)
|
||||||
|
with pytest.raises(EvaluationSharedSpendError, match="successor"):
|
||||||
|
_bind(case, preparation, winner)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("mutation", ("broken_chain", "restored_available"))
|
||||||
|
def test_broken_or_availability_restored_current_log_is_refused(mutation: str) -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
_, preparation = _prepare(case)
|
||||||
|
receipt = _spend(case, preparation)
|
||||||
|
context_case = case[0]
|
||||||
|
population = preparation.label_release_request.population_digest
|
||||||
|
current = context_case.port.current_log(population)
|
||||||
|
if mutation == "broken_chain":
|
||||||
|
broken_head = replace(current.receipts[-1], predecessor_digest=_digest("broken-predecessor"))
|
||||||
|
changed = replace(current, receipts=(*current.receipts[:-1], broken_head))
|
||||||
|
else:
|
||||||
|
changed = replace(current, status=custody.PopulationStatus.AVAILABLE)
|
||||||
|
context_case.port.logs[population] = changed
|
||||||
|
with pytest.raises(EvaluationSharedSpendError):
|
||||||
|
_bind(case, preparation, receipt)
|
||||||
|
|
||||||
|
|
||||||
|
def test_two_stale_preparations_allow_only_the_external_cas_winner() -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
_, first = _prepare(case)
|
||||||
|
_, second = _prepare(case)
|
||||||
|
assert first == second
|
||||||
|
winner = _spend(case, first)
|
||||||
|
with pytest.raises(EvaluationSharedSpendError, match="stale|unavailable"):
|
||||||
|
_spend(case, second)
|
||||||
|
assert _bind(case, first, winner).planned_baseline_digests == first.planned_baseline_digests
|
||||||
|
|
||||||
|
|
||||||
|
def test_subclasses_and_forged_binding_digest_are_refused() -> None:
|
||||||
|
case = _authority_case()
|
||||||
|
_, preparation = _prepare(case)
|
||||||
|
receipt = _spend(case, preparation)
|
||||||
|
binding = _bind(case, preparation, receipt)
|
||||||
|
with pytest.raises(EvaluationSharedSpendError, match="canonical"):
|
||||||
|
replace(binding, binding_digest=_digest("forged-binding"))
|
||||||
|
|
||||||
|
class DerivedAuthority(EvaluationAdmissionAuthorityV3):
|
||||||
|
pass
|
||||||
|
|
||||||
|
derived = object.__new__(DerivedAuthority)
|
||||||
|
with pytest.raises((EvaluationSharedSpendError, TypeError, ValueError)):
|
||||||
|
EvaluationPreparationV3(
|
||||||
|
derived,
|
||||||
|
preparation.label_release_request,
|
||||||
|
preparation.planned_baseline_digests,
|
||||||
|
"",
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user