mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-03 02:08:18 +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}")
|
||||
_SCHEMA = "openultrasast.detection-core.evaluation-case-outcome.v3"
|
||||
_PROJECTION_FIELDS = frozenset(
|
||||
{"case_input_digest", "policy", "source_outcome_digests", "normalized_outcome", "projection_digest"}
|
||||
)
|
||||
_PROJECTION_FIELDS = frozenset({"case_input_digest", "policy", "source_outcome_digests", "normalized_outcome", "projection_digest"})
|
||||
|
||||
|
||||
class CaseOutcomeProjectionError(ValueError):
|
||||
|
||||
@@ -181,12 +181,7 @@ def _component_observations(rule: CombinationRuleV3, cases: tuple[CombinedCaseOu
|
||||
digest
|
||||
for arm_id in rule.component_arm_ids
|
||||
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:
|
||||
raise CombinationValidationError("combination declaration must use exact EvaluationPlanDeclarationV3")
|
||||
declaration.__post_init__()
|
||||
if (
|
||||
type(bindings) is not tuple
|
||||
or not bindings
|
||||
or any(type(item) is not ReleasedArmCaseBindingV3 for item in bindings)
|
||||
):
|
||||
if 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")
|
||||
rows = bindings
|
||||
for item in rows:
|
||||
item.__post_init__()
|
||||
order = tuple(
|
||||
sorted(rows, key=lambda item: (item.key.arm_id, item.key.scope.family_id, item.key.scope.ecosystem, item.case_id_digest))
|
||||
)
|
||||
order = tuple(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:
|
||||
raise CombinationValidationError("combination bindings must use canonical release order")
|
||||
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])
|
||||
observation = _resolve_observation(reference, resolver, trusted_store_identities, maximum_bytes)
|
||||
_validate_resolved(reference, observation)
|
||||
if any(
|
||||
case.outcome_projection.policy is not configurations[slot.arm_id].case_outcome_policy
|
||||
for case in observation.cases
|
||||
):
|
||||
if any(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")
|
||||
resolved.append(observation)
|
||||
return tuple(resolved)
|
||||
|
||||
@@ -130,9 +130,10 @@ def test_component_projection_refuses_record_subclasses_and_source_changes() ->
|
||||
derive_component_case_outcome_projection((tampered,))
|
||||
|
||||
changed = _case_record("base", NeutralOutcome.SATISFIED, change="change-b")
|
||||
assert derive_component_case_outcome_projection((record,)).case_input_digest != derive_component_case_outcome_projection(
|
||||
(changed,)
|
||||
).case_input_digest
|
||||
assert (
|
||||
derive_component_case_outcome_projection((record,)).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:
|
||||
|
||||
@@ -209,11 +209,7 @@ def _bundle(*, authority_case=None, projection_mutation: str | None = None):
|
||||
if projection_mutation == "case_input":
|
||||
case_input = _digest("coordinated-other-case-input")
|
||||
elif projection_mutation == "outcome":
|
||||
outcome = (
|
||||
NeutralOutcome.VIOLATION
|
||||
if outcome is not NeutralOutcome.VIOLATION
|
||||
else NeutralOutcome.SATISFIED
|
||||
)
|
||||
outcome = NeutralOutcome.VIOLATION if outcome is not NeutralOutcome.VIOLATION else NeutralOutcome.SATISFIED
|
||||
elif projection_mutation == "source":
|
||||
sources = (_digest("coordinated-other-record"),)
|
||||
else: # pragma: no cover - fixture misuse
|
||||
@@ -459,9 +455,7 @@ def test_canonical_release_binding_requires_planned_projection_policy(
|
||||
substituted_policy: CaseOutcomeProjectionPolicyV3,
|
||||
) -> None:
|
||||
bundle = _bundle()
|
||||
configurations = {
|
||||
item.arm_id: item for item in bundle[3].plan_manifest.declaration.arm_configurations
|
||||
}
|
||||
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)
|
||||
group = next(item for item in bundle[2] if item.arm_id == configuration.arm_id)
|
||||
case = group.cases[0]
|
||||
|
||||
@@ -27,9 +27,7 @@ def _projection(
|
||||
) -> CaseOutcomeProjectionV3:
|
||||
return CaseOutcomeProjectionV3(
|
||||
case_input,
|
||||
CaseOutcomeProjectionPolicyV3.DECLARATIVE_COMBINATION_TABLE
|
||||
if combined
|
||||
else CaseOutcomeProjectionPolicyV3.UNANIMOUS_EVIDENCE,
|
||||
CaseOutcomeProjectionPolicyV3.DECLARATIVE_COMBINATION_TABLE if combined else CaseOutcomeProjectionPolicyV3.UNANIMOUS_EVIDENCE,
|
||||
sources,
|
||||
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")
|
||||
assert rule is not None
|
||||
expected_row = next(
|
||||
item
|
||||
for item in rule.decision_rows
|
||||
if item.component_outcomes == (NeutralOutcome.SATISFIED, NeutralOutcome.VIOLATION)
|
||||
item for item in rule.decision_rows if item.component_outcomes == (NeutralOutcome.SATISFIED, NeutralOutcome.VIOLATION)
|
||||
)
|
||||
|
||||
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_ref, changed_content = _seal(changed_observation, f"wrong-policy-{arm_id}")
|
||||
changed_sealed = sealed[:index] + (changed_ref,) + sealed[index + 1 :]
|
||||
changed_resolver = InMemoryContentResolver(
|
||||
{**resolver.artifacts, changed_ref.observation.artifact_id: changed_content}
|
||||
)
|
||||
changed_resolver = InMemoryContentResolver({**resolver.artifacts, changed_ref.observation.artifact_id: changed_content})
|
||||
|
||||
with pytest.raises(EvaluationObservationError, match="policy"):
|
||||
_validate(declaration, changed_sealed, changed_resolver)
|
||||
|
||||
@@ -100,11 +100,7 @@ def _configuration(arm_id: str, *, combined: bool = False, configuration: str =
|
||||
_digest("budget"),
|
||||
"container@sha256:abc",
|
||||
_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,
|
||||
"",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user