mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-04 10:48:19 +00:00
style(detection-core): format evaluation contracts
This commit is contained in:
@@ -19,9 +19,7 @@ from openultrasast.core.provenance import ProvenanceStamp
|
|||||||
|
|
||||||
_DIGEST = re.compile(r"[0-9a-f]{64}")
|
_DIGEST = re.compile(r"[0-9a-f]{64}")
|
||||||
_SCHEMA = "openultrasast.detection-core.evaluation-case-outcome.v3"
|
_SCHEMA = "openultrasast.detection-core.evaluation-case-outcome.v3"
|
||||||
_PROJECTION_FIELDS = frozenset(
|
_PROJECTION_FIELDS = frozenset({"case_input_digest", "policy", "source_outcome_digests", "normalized_outcome", "projection_digest"})
|
||||||
{"case_input_digest", "policy", "source_outcome_digests", "normalized_outcome", "projection_digest"}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class CaseOutcomeProjectionError(ValueError):
|
class CaseOutcomeProjectionError(ValueError):
|
||||||
|
|||||||
@@ -181,12 +181,7 @@ def _component_observations(rule: CombinationRuleV3, cases: tuple[CombinedCaseOu
|
|||||||
digest
|
digest
|
||||||
for arm_id in rule.component_arm_ids
|
for arm_id in rule.component_arm_ids
|
||||||
for digest in sorted(
|
for digest in sorted(
|
||||||
{
|
{component.observation_digest for case in cases for component in case.components if component.component_arm_id == arm_id}
|
||||||
component.observation_digest
|
|
||||||
for case in cases
|
|
||||||
for component in case.components
|
|
||||||
if component.component_arm_id == arm_id
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -263,18 +258,12 @@ def _validate_inputs(
|
|||||||
if type(declaration) is not EvaluationPlanDeclarationV3:
|
if type(declaration) is not EvaluationPlanDeclarationV3:
|
||||||
raise CombinationValidationError("combination declaration must use exact EvaluationPlanDeclarationV3")
|
raise CombinationValidationError("combination declaration must use exact EvaluationPlanDeclarationV3")
|
||||||
declaration.__post_init__()
|
declaration.__post_init__()
|
||||||
if (
|
if type(bindings) is not tuple or not bindings or any(type(item) is not ReleasedArmCaseBindingV3 for item in bindings):
|
||||||
type(bindings) is not tuple
|
|
||||||
or not bindings
|
|
||||||
or any(type(item) is not ReleasedArmCaseBindingV3 for item in bindings)
|
|
||||||
):
|
|
||||||
raise CombinationValidationError("combination bindings must be a nonempty exact tuple")
|
raise CombinationValidationError("combination bindings must be a nonempty exact tuple")
|
||||||
rows = bindings
|
rows = bindings
|
||||||
for item in rows:
|
for item in rows:
|
||||||
item.__post_init__()
|
item.__post_init__()
|
||||||
order = tuple(
|
order = tuple(sorted(rows, key=lambda item: (item.key.arm_id, item.key.scope.family_id, item.key.scope.ecosystem, item.case_id_digest)))
|
||||||
sorted(rows, key=lambda item: (item.key.arm_id, item.key.scope.family_id, item.key.scope.ecosystem, item.case_id_digest))
|
|
||||||
)
|
|
||||||
if rows != order:
|
if rows != order:
|
||||||
raise CombinationValidationError("combination bindings must use canonical release order")
|
raise CombinationValidationError("combination bindings must use canonical release order")
|
||||||
configurations = {item.arm_id: item for item in declaration.arm_configurations}
|
configurations = {item.arm_id: item for item in declaration.arm_configurations}
|
||||||
|
|||||||
@@ -435,10 +435,7 @@ def validate_evaluation_observations(
|
|||||||
_validate_header(reference, slot, configurations[slot.arm_id])
|
_validate_header(reference, slot, configurations[slot.arm_id])
|
||||||
observation = _resolve_observation(reference, resolver, trusted_store_identities, maximum_bytes)
|
observation = _resolve_observation(reference, resolver, trusted_store_identities, maximum_bytes)
|
||||||
_validate_resolved(reference, observation)
|
_validate_resolved(reference, observation)
|
||||||
if any(
|
if any(case.outcome_projection.policy is not configurations[slot.arm_id].case_outcome_policy for case in observation.cases):
|
||||||
case.outcome_projection.policy is not configurations[slot.arm_id].case_outcome_policy
|
|
||||||
for case in observation.cases
|
|
||||||
):
|
|
||||||
raise EvaluationObservationError("observation case outcome projection policy differs from the planned arm")
|
raise EvaluationObservationError("observation case outcome projection policy differs from the planned arm")
|
||||||
resolved.append(observation)
|
resolved.append(observation)
|
||||||
return tuple(resolved)
|
return tuple(resolved)
|
||||||
|
|||||||
@@ -130,9 +130,10 @@ def test_component_projection_refuses_record_subclasses_and_source_changes() ->
|
|||||||
derive_component_case_outcome_projection((tampered,))
|
derive_component_case_outcome_projection((tampered,))
|
||||||
|
|
||||||
changed = _case_record("base", NeutralOutcome.SATISFIED, change="change-b")
|
changed = _case_record("base", NeutralOutcome.SATISFIED, change="change-b")
|
||||||
assert derive_component_case_outcome_projection((record,)).case_input_digest != derive_component_case_outcome_projection(
|
assert (
|
||||||
(changed,)
|
derive_component_case_outcome_projection((record,)).case_input_digest
|
||||||
).case_input_digest
|
!= derive_component_case_outcome_projection((changed,)).case_input_digest
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_case_input_derivation_refuses_self_consistent_invalid_nested_change_identity() -> None:
|
def test_case_input_derivation_refuses_self_consistent_invalid_nested_change_identity() -> None:
|
||||||
|
|||||||
@@ -209,11 +209,7 @@ def _bundle(*, authority_case=None, projection_mutation: str | None = None):
|
|||||||
if projection_mutation == "case_input":
|
if projection_mutation == "case_input":
|
||||||
case_input = _digest("coordinated-other-case-input")
|
case_input = _digest("coordinated-other-case-input")
|
||||||
elif projection_mutation == "outcome":
|
elif projection_mutation == "outcome":
|
||||||
outcome = (
|
outcome = NeutralOutcome.VIOLATION if outcome is not NeutralOutcome.VIOLATION else NeutralOutcome.SATISFIED
|
||||||
NeutralOutcome.VIOLATION
|
|
||||||
if outcome is not NeutralOutcome.VIOLATION
|
|
||||||
else NeutralOutcome.SATISFIED
|
|
||||||
)
|
|
||||||
elif projection_mutation == "source":
|
elif projection_mutation == "source":
|
||||||
sources = (_digest("coordinated-other-record"),)
|
sources = (_digest("coordinated-other-record"),)
|
||||||
else: # pragma: no cover - fixture misuse
|
else: # pragma: no cover - fixture misuse
|
||||||
@@ -459,9 +455,7 @@ def test_canonical_release_binding_requires_planned_projection_policy(
|
|||||||
substituted_policy: CaseOutcomeProjectionPolicyV3,
|
substituted_policy: CaseOutcomeProjectionPolicyV3,
|
||||||
) -> None:
|
) -> None:
|
||||||
bundle = _bundle()
|
bundle = _bundle()
|
||||||
configurations = {
|
configurations = {item.arm_id: item for item in bundle[3].plan_manifest.declaration.arm_configurations}
|
||||||
item.arm_id: item for item in bundle[3].plan_manifest.declaration.arm_configurations
|
|
||||||
}
|
|
||||||
configuration = next(item for item in configurations.values() if item.kind is kind)
|
configuration = next(item for item in configurations.values() if item.kind is kind)
|
||||||
group = next(item for item in bundle[2] if item.arm_id == configuration.arm_id)
|
group = next(item for item in bundle[2] if item.arm_id == configuration.arm_id)
|
||||||
case = group.cases[0]
|
case = group.cases[0]
|
||||||
|
|||||||
@@ -27,9 +27,7 @@ def _projection(
|
|||||||
) -> CaseOutcomeProjectionV3:
|
) -> CaseOutcomeProjectionV3:
|
||||||
return CaseOutcomeProjectionV3(
|
return CaseOutcomeProjectionV3(
|
||||||
case_input,
|
case_input,
|
||||||
CaseOutcomeProjectionPolicyV3.DECLARATIVE_COMBINATION_TABLE
|
CaseOutcomeProjectionPolicyV3.DECLARATIVE_COMBINATION_TABLE if combined else CaseOutcomeProjectionPolicyV3.UNANIMOUS_EVIDENCE,
|
||||||
if combined
|
|
||||||
else CaseOutcomeProjectionPolicyV3.UNANIMOUS_EVIDENCE,
|
|
||||||
sources,
|
sources,
|
||||||
outcome,
|
outcome,
|
||||||
"",
|
"",
|
||||||
@@ -99,9 +97,7 @@ def test_combination_replay_joins_exact_cases_in_registered_component_order() ->
|
|||||||
rule = next(item.combination_rule for item in declaration.arm_configurations if item.arm_id == "arm-combined")
|
rule = next(item.combination_rule for item in declaration.arm_configurations if item.arm_id == "arm-combined")
|
||||||
assert rule is not None
|
assert rule is not None
|
||||||
expected_row = next(
|
expected_row = next(
|
||||||
item
|
item for item in rule.decision_rows if item.component_outcomes == (NeutralOutcome.SATISFIED, NeutralOutcome.VIOLATION)
|
||||||
for item in rule.decision_rows
|
|
||||||
if item.component_outcomes == (NeutralOutcome.SATISFIED, NeutralOutcome.VIOLATION)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
assert lineage.rule == rule
|
assert lineage.rule == rule
|
||||||
|
|||||||
@@ -146,9 +146,7 @@ def test_observation_case_policy_must_equal_planned_arm_policy(
|
|||||||
changed_observation = replace(original, cases=(changed_case,), observation_digest="")
|
changed_observation = replace(original, cases=(changed_case,), observation_digest="")
|
||||||
changed_ref, changed_content = _seal(changed_observation, f"wrong-policy-{arm_id}")
|
changed_ref, changed_content = _seal(changed_observation, f"wrong-policy-{arm_id}")
|
||||||
changed_sealed = sealed[:index] + (changed_ref,) + sealed[index + 1 :]
|
changed_sealed = sealed[:index] + (changed_ref,) + sealed[index + 1 :]
|
||||||
changed_resolver = InMemoryContentResolver(
|
changed_resolver = InMemoryContentResolver({**resolver.artifacts, changed_ref.observation.artifact_id: changed_content})
|
||||||
{**resolver.artifacts, changed_ref.observation.artifact_id: changed_content}
|
|
||||||
)
|
|
||||||
|
|
||||||
with pytest.raises(EvaluationObservationError, match="policy"):
|
with pytest.raises(EvaluationObservationError, match="policy"):
|
||||||
_validate(declaration, changed_sealed, changed_resolver)
|
_validate(declaration, changed_sealed, changed_resolver)
|
||||||
|
|||||||
@@ -100,11 +100,7 @@ def _configuration(arm_id: str, *, combined: bool = False, configuration: str =
|
|||||||
_digest("budget"),
|
_digest("budget"),
|
||||||
"container@sha256:abc",
|
"container@sha256:abc",
|
||||||
_digest("outcomes"),
|
_digest("outcomes"),
|
||||||
(
|
(CaseOutcomeProjectionPolicyV3.DECLARATIVE_COMBINATION_TABLE if combined else CaseOutcomeProjectionPolicyV3.UNANIMOUS_EVIDENCE),
|
||||||
CaseOutcomeProjectionPolicyV3.DECLARATIVE_COMBINATION_TABLE
|
|
||||||
if combined
|
|
||||||
else CaseOutcomeProjectionPolicyV3.UNANIMOUS_EVIDENCE
|
|
||||||
),
|
|
||||||
_combination_rule() if combined else None,
|
_combination_rule() if combined else None,
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user