mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-03 18:28:19 +00:00
feat(detection-core): reconcile ordinary push coverage
This commit is contained in:
@@ -150,7 +150,7 @@ def _contract_catalog_context(context_case):
|
||||
return replace(context_case, context=context)
|
||||
|
||||
|
||||
def _rekey_configuration(configuration, scope: ScopeKey):
|
||||
def _rekey_configuration(configuration, scope: ScopeKey, *, minimum_ordinary_pushes=None, relaxed_precision=False):
|
||||
key = ArmScopeKey(configuration.arm_id, scope)
|
||||
policies = tuple(
|
||||
replace(
|
||||
@@ -162,7 +162,24 @@ def _rekey_configuration(configuration, scope: ScopeKey):
|
||||
for policy in configuration.denominator_policies
|
||||
)
|
||||
thresholds = tuple(replace(row, key=key, threshold_digest="") for row in configuration.thresholds)
|
||||
sufficiency = tuple(replace(row, key=key, rule_digest="") for row in configuration.sufficiency_rules)
|
||||
sufficiency = tuple(
|
||||
replace(
|
||||
row,
|
||||
key=key,
|
||||
minimum_ordinary_pushes=(row.minimum_ordinary_pushes if minimum_ordinary_pushes is None else minimum_ordinary_pushes),
|
||||
precision_rules=tuple(
|
||||
replace(
|
||||
precision,
|
||||
maximum_wilson_width=100.0 if relaxed_precision else precision.maximum_wilson_width,
|
||||
minimum_denominator=1 if relaxed_precision else precision.minimum_denominator,
|
||||
rule_digest="",
|
||||
)
|
||||
for precision in row.precision_rules
|
||||
),
|
||||
rule_digest="",
|
||||
)
|
||||
for row in configuration.sufficiency_rules
|
||||
)
|
||||
return replace(
|
||||
configuration,
|
||||
exact_scopes=(scope,),
|
||||
@@ -173,16 +190,17 @@ def _rekey_configuration(configuration, scope: ScopeKey):
|
||||
)
|
||||
|
||||
|
||||
def _reviewed_scoped_census(plan, scopes):
|
||||
def _reviewed_scoped_census(plan, scopes, *, ordinary_count=None, whole_count=None):
|
||||
template_row = plan.census.scope_rows[0]
|
||||
scope_rows = tuple(replace(template_row, scope=scope) for scope in sorted(set(scopes.values())))
|
||||
ecosystems = tuple(sorted({scope.ecosystem for scope in scopes.values()}))
|
||||
ordinary_count = plan.census.whole_underlying_push_count // len(ecosystems)
|
||||
ordinary_rows = tuple(EcosystemOrdinaryCensusV3(ecosystem, ordinary_count) for ecosystem in ecosystems)
|
||||
per_ecosystem = plan.census.whole_underlying_push_count // len(ecosystems) if ordinary_count is None else ordinary_count
|
||||
ordinary_rows = tuple(EcosystemOrdinaryCensusV3(ecosystem, per_ecosystem) for ecosystem in ecosystems)
|
||||
census_draft = replace(
|
||||
plan.census,
|
||||
scope_rows=scope_rows,
|
||||
ordinary_rows=ordinary_rows,
|
||||
whole_underlying_push_count=(plan.census.whole_underlying_push_count if whole_count is None else whole_count),
|
||||
payload_digest="0" * 64,
|
||||
census_digest="0" * 64,
|
||||
)
|
||||
@@ -200,8 +218,24 @@ def _reviewed_scoped_census(plan, scopes):
|
||||
return replace(census_reviewed, census_digest=derive_evaluation_census_digest(census_reviewed))
|
||||
|
||||
|
||||
def _reviewed_scoped_plan(plan, scopes):
|
||||
configurations = tuple(_rekey_configuration(row, scopes[row.arm_id]) for row in plan.declaration.arm_configurations)
|
||||
def _reviewed_scoped_plan(
|
||||
plan,
|
||||
scopes,
|
||||
*,
|
||||
ordinary_count=None,
|
||||
whole_count=None,
|
||||
minimum_ordinary_pushes=None,
|
||||
relaxed_precision=False,
|
||||
):
|
||||
configurations = tuple(
|
||||
_rekey_configuration(
|
||||
row,
|
||||
scopes[row.arm_id],
|
||||
minimum_ordinary_pushes=minimum_ordinary_pushes,
|
||||
relaxed_precision=relaxed_precision,
|
||||
)
|
||||
for row in plan.declaration.arm_configurations
|
||||
)
|
||||
inputs = tuple(sorted({slot.input_identity_digest for slot in plan.declaration.expected_observation_slots}))
|
||||
declaration = create_evaluation_plan_declaration(
|
||||
population_digest=plan.declaration.population_digest,
|
||||
@@ -214,7 +248,7 @@ def _reviewed_scoped_plan(plan, scopes):
|
||||
plan,
|
||||
declaration=declaration,
|
||||
baselines=derive_evaluation_baselines(declaration),
|
||||
census=_reviewed_scoped_census(plan, scopes),
|
||||
census=_reviewed_scoped_census(plan, scopes, ordinary_count=ordinary_count, whole_count=whole_count),
|
||||
payload_digest="0" * 64,
|
||||
manifest_digest="0" * 64,
|
||||
)
|
||||
@@ -232,10 +266,24 @@ def _reviewed_scoped_plan(plan, scopes):
|
||||
return replace(plan_reviewed, manifest_digest=derive_evaluation_plan_manifest_digest(plan_reviewed))
|
||||
|
||||
|
||||
def _scoped_authority_case(scopes):
|
||||
def _scoped_authority_case(
|
||||
scopes,
|
||||
*,
|
||||
ordinary_count=None,
|
||||
whole_count=None,
|
||||
minimum_ordinary_pushes=None,
|
||||
relaxed_precision=False,
|
||||
):
|
||||
context_case, plan, readiness = authority_fixtures._reviewed_plan()
|
||||
context_case = _contract_catalog_context(context_case)
|
||||
multi_plan = _reviewed_scoped_plan(plan, scopes)
|
||||
multi_plan = _reviewed_scoped_plan(
|
||||
plan,
|
||||
scopes,
|
||||
ordinary_count=ordinary_count,
|
||||
whole_count=whole_count,
|
||||
minimum_ordinary_pushes=minimum_ordinary_pushes,
|
||||
relaxed_precision=relaxed_precision,
|
||||
)
|
||||
original = authority_fixtures._reviewed_plan
|
||||
authority_fixtures._reviewed_plan = lambda: (context_case, multi_plan, readiness)
|
||||
try:
|
||||
@@ -259,6 +307,20 @@ def _multi_scope_authority_case():
|
||||
)
|
||||
|
||||
|
||||
def _ordinary_projection_authority_case():
|
||||
return _scoped_authority_case(
|
||||
{
|
||||
"arm-a": ScopeKey("reference-family", "python"),
|
||||
"arm-b": ScopeKey("reference-family", "javascript"),
|
||||
"arm-combined": ScopeKey("reference-family", "python"),
|
||||
},
|
||||
ordinary_count=1,
|
||||
whole_count=6,
|
||||
minimum_ordinary_pushes=1,
|
||||
relaxed_precision=True,
|
||||
)
|
||||
|
||||
|
||||
def _contract_for_scope(context_case, scope: ScopeKey, *, family_version: str | None = None, family_id: str | None = None):
|
||||
selected_family = family_id or scope.family_id
|
||||
selected_version = family_version or ("2" if scope.ecosystem == "javascript" else "1")
|
||||
@@ -583,6 +645,7 @@ def _bundle(
|
||||
projection_mutation: str | None = None,
|
||||
evidence_mutation: str | None = None,
|
||||
summary_mutation: str | None = None,
|
||||
shared_underlying_by_ecosystem: bool = False,
|
||||
):
|
||||
setup = _setup()
|
||||
authority_case = authority_case or _reference_authority_case()
|
||||
@@ -591,6 +654,14 @@ def _bundle(
|
||||
draft_cases, extra_artifacts = _draft_cases(
|
||||
setup, authority_case, configurations, projection_mutation, evidence_mutation, summary_mutation
|
||||
)
|
||||
if shared_underlying_by_ecosystem:
|
||||
draft_cases = {
|
||||
arm_id: tuple(
|
||||
replace(case, underlying_push_digest=_digest(f"underlying-push-{case.key.scope.ecosystem}-{index}"))
|
||||
for index, case in enumerate(cases)
|
||||
)
|
||||
for arm_id, cases in draft_cases.items()
|
||||
}
|
||||
|
||||
artifacts = {**authority_case[5].artifacts, **setup.resolver.artifacts, **extra_artifacts}
|
||||
sealed, observations, observation_by_arm = _sealed_observations(manifest, configurations, draft_cases, artifacts)
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
"""Ecosystem-scoped ordinary projection and whole-push deduplication for V3."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import replace
|
||||
|
||||
import pytest
|
||||
from test_core_evaluation_case_validation import (
|
||||
_bundle,
|
||||
_digest,
|
||||
_multi_scope_authority_case,
|
||||
_ordinary_projection_authority_case,
|
||||
)
|
||||
|
||||
from openultrasast.core.evaluation_measurement import ArmScopeKey, ScopeKey
|
||||
from openultrasast.core.evaluation_plan import EcosystemOrdinaryCensusV3
|
||||
|
||||
|
||||
def _project(bundle):
|
||||
from openultrasast.core.evaluation_ordinary_projection import project_ordinary_pushes_v3
|
||||
|
||||
reviewed_case = bundle[5]
|
||||
return project_ordinary_pushes_v3(
|
||||
bundle[3],
|
||||
bundle[6],
|
||||
bundle[7],
|
||||
bundle[8],
|
||||
bundle[9],
|
||||
bundle[1],
|
||||
bundle[2],
|
||||
reviewed_case[0].context,
|
||||
reviewed_case[0].port,
|
||||
bundle[4],
|
||||
)
|
||||
|
||||
|
||||
def _valid_bundle():
|
||||
return _bundle(
|
||||
authority_case=_ordinary_projection_authority_case(),
|
||||
shared_underlying_by_ecosystem=True,
|
||||
)
|
||||
|
||||
|
||||
def _identity(key: ArmScopeKey, label: str, push: str):
|
||||
from openultrasast.core.evaluation_ordinary_projection import OrdinaryPushCaseV3
|
||||
|
||||
return OrdinaryPushCaseV3(key, _digest(f"case-{label}"), _digest(f"binding-{label}"), _digest(push), "")
|
||||
|
||||
|
||||
def _derive(census, keys, identities, whole):
|
||||
from openultrasast.core.evaluation_ordinary_projection import _derive_ordinary_push_projection_v3
|
||||
|
||||
return _derive_ordinary_push_projection_v3(census, keys, identities, whole)
|
||||
|
||||
|
||||
def test_reviewed_projection_reconciles_each_scope_and_deduplicates_whole_pushes() -> None:
|
||||
bundle = _valid_bundle()
|
||||
projection = _project(bundle)
|
||||
|
||||
assert tuple(row.key for row in projection.scoped_projections) == (
|
||||
ArmScopeKey("arm-a", ScopeKey("reference-family", "python")),
|
||||
ArmScopeKey("arm-b", ScopeKey("reference-family", "javascript")),
|
||||
ArmScopeKey("arm-combined", ScopeKey("reference-family", "python")),
|
||||
)
|
||||
assert all(row.ordinary_push_count == 1 for row in projection.scoped_projections)
|
||||
python_rows = tuple(row for row in projection.scoped_projections if row.key.scope.ecosystem == "python")
|
||||
javascript_row = next(row for row in projection.scoped_projections if row.key.scope.ecosystem == "javascript")
|
||||
assert {row.cases[0].underlying_push_digest for row in python_rows} == {_digest("underlying-push-python-2")}
|
||||
assert javascript_row.cases[0].underlying_push_digest == _digest("underlying-push-javascript-2")
|
||||
assert projection.whole_population.underlying_push_count == 6
|
||||
assert len(projection.whole_population.underlying_push_digests) == 6
|
||||
assert projection.census_digest == bundle[3].plan_manifest.census.census_digest
|
||||
|
||||
|
||||
def test_one_arm_projects_the_same_ordinary_push_once_into_each_same_ecosystem_family() -> None:
|
||||
bundle = _valid_bundle()
|
||||
census = replace(
|
||||
bundle[3].plan_manifest.census,
|
||||
scope_rows=bundle[3].plan_manifest.census.scope_rows[:1],
|
||||
ordinary_rows=(EcosystemOrdinaryCensusV3("python", 1),),
|
||||
whole_underlying_push_count=1,
|
||||
)
|
||||
first = ArmScopeKey("arm-a", ScopeKey("family-a", "python"))
|
||||
second = ArmScopeKey("arm-a", ScopeKey("family-b", "python"))
|
||||
push = "same-ecosystem-ordinary"
|
||||
|
||||
projection = _derive(
|
||||
census,
|
||||
(first, second),
|
||||
(_identity(first, "a", push), _identity(second, "b", push)),
|
||||
(_digest(push),),
|
||||
)
|
||||
|
||||
assert tuple(row.ordinary_push_count for row in projection.scoped_projections) == (1, 1)
|
||||
assert projection.whole_population.underlying_push_count == 1
|
||||
|
||||
|
||||
def test_same_ecosystem_scopes_must_project_the_identical_eligible_push_set() -> None:
|
||||
bundle = _valid_bundle()
|
||||
census = replace(
|
||||
bundle[3].plan_manifest.census,
|
||||
scope_rows=bundle[3].plan_manifest.census.scope_rows[:1],
|
||||
ordinary_rows=(EcosystemOrdinaryCensusV3("python", 1),),
|
||||
whole_underlying_push_count=2,
|
||||
)
|
||||
first = ArmScopeKey("arm-a", ScopeKey("family-a", "python"))
|
||||
second = ArmScopeKey("arm-b", ScopeKey("family-b", "python"))
|
||||
|
||||
with pytest.raises(ValueError, match="eligible ordinary push set"):
|
||||
_derive(
|
||||
census,
|
||||
(first, second),
|
||||
(_identity(first, "a", "push-a"), _identity(second, "b", "push-b")),
|
||||
(_digest("push-a"), _digest("push-b")),
|
||||
)
|
||||
|
||||
|
||||
def test_underlying_ordinary_push_cannot_project_into_another_ecosystem() -> None:
|
||||
bundle = _valid_bundle()
|
||||
census = replace(
|
||||
bundle[3].plan_manifest.census,
|
||||
ordinary_rows=(EcosystemOrdinaryCensusV3("javascript", 1), EcosystemOrdinaryCensusV3("python", 1)),
|
||||
whole_underlying_push_count=2,
|
||||
)
|
||||
javascript = ArmScopeKey("arm-a", ScopeKey("family-a", "javascript"))
|
||||
python = ArmScopeKey("arm-b", ScopeKey("family-b", "python"))
|
||||
|
||||
with pytest.raises(ValueError, match="another ecosystem"):
|
||||
_derive(
|
||||
census,
|
||||
(javascript, python),
|
||||
(_identity(javascript, "js", "shared-push"), _identity(python, "py", "shared-push")),
|
||||
tuple(sorted((_digest("shared-push"), _digest("other-push")))),
|
||||
)
|
||||
|
||||
|
||||
def test_scoped_and_whole_counts_must_match_the_reviewed_census() -> None:
|
||||
bundle = _valid_bundle()
|
||||
key = ArmScopeKey("arm-a", ScopeKey("family-a", "python"))
|
||||
census = replace(
|
||||
bundle[3].plan_manifest.census,
|
||||
scope_rows=bundle[3].plan_manifest.census.scope_rows[:1],
|
||||
ordinary_rows=(EcosystemOrdinaryCensusV3("python", 2),),
|
||||
whole_underlying_push_count=2,
|
||||
)
|
||||
identity = _identity(key, "a", "push-a")
|
||||
|
||||
with pytest.raises(ValueError, match="ordinary census"):
|
||||
_derive(census, (key,), (identity,), (_digest("push-a"), _digest("push-b")))
|
||||
with pytest.raises(ValueError, match="whole-population census"):
|
||||
_derive(replace(census, ordinary_rows=(EcosystemOrdinaryCensusV3("python", 1),)), (key,), (identity,), (_digest("push-a"),))
|
||||
|
||||
|
||||
def test_full_release_with_unreconciled_census_is_refused() -> None:
|
||||
bundle = _bundle(authority_case=_multi_scope_authority_case())
|
||||
|
||||
with pytest.raises(ValueError, match="census"):
|
||||
_project(bundle)
|
||||
|
||||
|
||||
def test_projection_contracts_are_non_authorizing_and_require_exact_collections() -> None:
|
||||
from openultrasast.core.evaluation_ordinary_projection import EvaluationOrdinaryProjectionV3
|
||||
|
||||
projection = _project(_valid_bundle())
|
||||
assert not {"metrics", "gate", "decision", "authorization"} & EvaluationOrdinaryProjectionV3.__dataclass_fields__.keys()
|
||||
with pytest.raises(ValueError, match="exact tuple"):
|
||||
_derive(
|
||||
_valid_bundle()[3].plan_manifest.census,
|
||||
list(row.key for row in projection.scoped_projections), # type: ignore[arg-type]
|
||||
(),
|
||||
(),
|
||||
)
|
||||
|
||||
|
||||
def test_v2_whole_population_metric_contract_is_unchanged() -> None:
|
||||
from openultrasast.core.admission_metrics import AdmissionMetricEvidence
|
||||
|
||||
assert "whole_population_case_count" in AdmissionMetricEvidence.__dataclass_fields__
|
||||
assert "whole_population_funnel" in AdmissionMetricEvidence.__dataclass_fields__
|
||||
Reference in New Issue
Block a user