From a916a8709182a5e2ffcb4ed153ac20a1f16296b9 Mon Sep 17 00:00:00 2001 From: Marius Ciepluch <11855163+norandom@users.noreply.github.com> Date: Fri, 6 Nov 2026 17:38:55 +0100 Subject: [PATCH] feat(detection-core): close combined-arm configuration contract --- .kiro/specs/detection-core/tasks.md | 3 +- .../core/_evaluation_arm_configuration.py | 4 +- .../test_core_evaluation_arm_configuration.py | 201 ++++++++++++++++++ 3 files changed, 205 insertions(+), 3 deletions(-) create mode 100644 tests/test_core_evaluation_arm_configuration.py diff --git a/.kiro/specs/detection-core/tasks.md b/.kiro/specs/detection-core/tasks.md index 728fb48f..3a1aed26 100644 --- a/.kiro/specs/detection-core/tasks.md +++ b/.kiro/specs/detection-core/tasks.md @@ -487,7 +487,7 @@ contract work and use synthetic/in-memory workflow boundaries only. - _Depends: 12.1, 12.3_ - [ ] 13. Register reviewed multi-arm plans and exact preflight -- [ ] 13.1 Define component, combined-arm, and declarative combination configurations +- [x] 13.1 Define component, combined-arm, and declarative combination configurations - Represent component and combined arm configurations with exact scopes, policies, thresholds, sufficiency rules, outcome mapping, and a closed declarative combination rule. - Require component identities and a finite normalized decision table for every combined arm, with identical-component-candidate-only proof retention. - Refuse missing components, duplicate arms, cross-key policies, and arbitrary executable combination behavior. @@ -711,6 +711,7 @@ contract work and use synthetic/in-memory workflow boundaries only. - Task 12.2 moved the seven closed metric identities and bit-exact Wilson arithmetic into `_metric_algebra.py`; V2 retains all schemas, validators, serialization, and error ownership. - Task 12.3 established exact V3 arm/scope measurement, sufficiency, immutable arm-identity, scoped Wilson-result, and rational aggregate-weight contracts behind an acyclic public facade; all malformed public-factory inputs fail through `EvaluationMeasurementError` before arithmetic. - Task 12.4 reserved the exact V3 evaluation-admission identity, added closed extensible V3 contract routing, and made partial artifacts structurally non-authorizing across inheritance, wrapped aliases, and post-definition/shared-base mutation while leaving V2 routes unchanged. +- Task 13.1 closed the arm-configuration gap by requiring at least two canonical non-self component identities for a combined arm and proving its complete finite decision table, proof-retention policy, and every configuration input remain digest-bound without executable behavior. - 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. - 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. diff --git a/src/openultrasast/core/_evaluation_arm_configuration.py b/src/openultrasast/core/_evaluation_arm_configuration.py index 588c8464..61cdd4c2 100644 --- a/src/openultrasast/core/_evaluation_arm_configuration.py +++ b/src/openultrasast/core/_evaluation_arm_configuration.py @@ -74,10 +74,10 @@ class CombinationRuleV3: _text(self.combined_arm_id, "combined_arm_id") if ( type(self.component_arm_ids) is not tuple - or not self.component_arm_ids + or len(self.component_arm_ids) < 2 or any(type(item) is not str or not item for item in self.component_arm_ids) ): - raise EvaluationMeasurementError("component arm ids must be a nonempty tuple") + raise EvaluationMeasurementError("component arm ids must contain at least two exact identities") canonical_ids = tuple(sorted(set(self.component_arm_ids))) if self.component_arm_ids != canonical_ids or self.combined_arm_id in self.component_arm_ids: raise EvaluationMeasurementError("component arm ids must be unique and in canonical order") diff --git a/tests/test_core_evaluation_arm_configuration.py b/tests/test_core_evaluation_arm_configuration.py new file mode 100644 index 00000000..e54588fd --- /dev/null +++ b/tests/test_core_evaluation_arm_configuration.py @@ -0,0 +1,201 @@ +"""Component and combined-arm configuration contracts for evaluation V3.""" + +from __future__ import annotations + +from dataclasses import replace +from itertools import product + +import pytest + +from openultrasast.core import canonical +from openultrasast.core.evaluation_measurement import ( + ArmConfigurationV3, + ArmKindV3, + ArmScopeKey, + CombinationDecisionRowV3, + CombinationOperatorV3, + CombinationRuleV3, + Comparator, + DenominatorPolicyV3, + EvaluationMeasurementError, + IntervalMethod, + IntervalPrecisionRuleV3, + MetricId, + NeutralOutcome, + ProofCandidatePolicyV3, + ScopedThresholdV3, + ScopeKey, + ScopeSufficiencyRuleV3, + Statistic, + standard_scoped_metric_rules, +) + + +def _digest(label: str) -> str: + return canonical.content_digest(label) + + +def _rule( + component_arm_ids: tuple[str, ...] = ("arm-a", "arm-b"), + *, + combined_arm_id: str = "arm-combined", +) -> CombinationRuleV3: + rows = ( + tuple( + CombinationDecisionRowV3( + outcomes, + NeutralOutcome.VIOLATION if NeutralOutcome.VIOLATION in outcomes else outcomes[0], + "", + ) + for outcomes in product(tuple(NeutralOutcome), repeat=len(component_arm_ids)) + ) + if component_arm_ids + else () + ) + return CombinationRuleV3( + combined_arm_id, + component_arm_ids, + CombinationOperatorV3.DECLARATIVE_TABLE, + rows, + ProofCandidatePolicyV3.IDENTICAL_COMPONENT_ONLY, + "", + ) + + +def _configuration( + *, + scope: ScopeKey | None = None, + threshold_target: float = 0.5, + minimum_pairs: int = 4, + outcome_mapping: str = "outcomes", + combination_rule: CombinationRuleV3 | None = None, +) -> ArmConfigurationV3: + if scope is None: + scope = ScopeKey("access_control", "python") + arm_id = "arm-combined" + key = ArmScopeKey(arm_id, scope) + thresholds = tuple( + ScopedThresholdV3( + key, + metric_id, + (Statistic.PER_100 if metric_id is MetricId.ORDINARY_FALSE_ALARMS_PER_100 else Statistic.RATE), + ( + Comparator.AT_MOST + if metric_id + in { + MetricId.FIXED_FALSE_ALARM_RATE, + MetricId.ORDINARY_FALSE_ALARMS_PER_100, + } + else Comparator.AT_LEAST + ), + threshold_target, + "", + ) + for metric_id in MetricId + ) + precision = tuple(IntervalPrecisionRuleV3(metric_id, 0.25, 4, "") for metric_id in MetricId) + return ArmConfigurationV3( + arm_id=arm_id, + kind=ArmKindV3.COMBINED, + exact_scopes=(scope,), + denominator_policies=(DenominatorPolicyV3(key, standard_scoped_metric_rules(key), IntervalMethod.WILSON_95, ""),), + thresholds=thresholds, + sufficiency_rules=(ScopeSufficiencyRuleV3(key, minimum_pairs, 8, precision, ""),), + judgment_vocabulary_digest=_digest("judgments"), + analyzer_identity="joern-4.0+ousast-v3", + fact_set_digest=_digest("facts"), + configuration_digest=_digest("configuration"), + registered_budget_digest=_digest("budget"), + execution_identity="container@sha256:abc", + outcome_mapping_digest=_digest(outcome_mapping), + combination_rule=_rule() if combination_rule is None else combination_rule, + configuration_identity_digest="", + ) + + +def test_combined_arm_requires_at_least_two_component_identities() -> None: + with pytest.raises(EvaluationMeasurementError, match="at least two"): + _rule(("arm-a",)) + + +@pytest.mark.parametrize( + "component_arm_ids", + ( + (), + ("arm-a", "arm-a"), + ("arm-b", "arm-a"), + ("arm-a", "arm-combined"), + ), +) +def test_combined_arm_refuses_missing_duplicate_noncanonical_or_self_components( + component_arm_ids: tuple[str, ...], +) -> None: + with pytest.raises(EvaluationMeasurementError, match="component arm ids"): + _rule(component_arm_ids) + + +def test_combination_rule_requires_the_complete_normalized_decision_table() -> None: + rule = _rule() + first, second = rule.decision_rows[:2] + mutations = ( + rule.decision_rows[:-1], + (first, first) + rule.decision_rows[2:], + (second, first) + rule.decision_rows[2:], + (replace(first, component_outcomes=(NeutralOutcome.VIOLATION,), row_digest=""),) + rule.decision_rows[1:], + ) + for rows in mutations: + with pytest.raises(EvaluationMeasurementError, match="canonical|every component outcome"): + replace(rule, decision_rows=rows, rule_digest="") + + +def test_combination_rule_refuses_executable_or_nonclosed_behavior() -> None: + rule = _rule() + for field, value in ( + ("operator", lambda outcomes: outcomes[0]), + ("candidate_policy", "identical_component_only"), + ("decision_rows", lambda outcomes: outcomes[0]), + ): + with pytest.raises(EvaluationMeasurementError): + replace(rule, **{field: value, "rule_digest": ""}) + + +def test_each_combined_configuration_input_changes_its_identity() -> None: + base = _configuration() + base_rule = base.combination_rule + assert base_rule is not None + + changed_components = _configuration(combination_rule=_rule(("arm-a", "arm-c"))) + changed_rows = list(base_rule.decision_rows) + changed_rows[0] = replace( + changed_rows[0], + combined_outcome=NeutralOutcome.INDETERMINATE, + row_digest="", + ) + changed_rule_row = _configuration( + combination_rule=replace(base_rule, decision_rows=tuple(changed_rows), rule_digest=""), + ) + changed_scope_and_policy = _configuration(scope=ScopeKey("injection", "python")) + changed_threshold = _configuration(threshold_target=0.6) + changed_sufficiency = _configuration(minimum_pairs=5) + changed_outcome_mapping = _configuration(outcome_mapping="outcomes-v2") + + identities = { + item.configuration_identity_digest + for item in ( + base, + changed_components, + changed_rule_row, + changed_scope_and_policy, + changed_threshold, + changed_sufficiency, + changed_outcome_mapping, + ) + } + assert len(identities) == 7 + + +def test_identical_component_candidate_policy_is_the_only_proof_retention_policy() -> None: + rule = _rule() + assert rule.candidate_policy is ProofCandidatePolicyV3.IDENTICAL_COMPONENT_ONLY + with pytest.raises(EvaluationMeasurementError, match="closed operator and candidate policy"): + replace(rule, candidate_policy=object(), rule_digest="")