mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-03 10:18:18 +00:00
feat(valid-detection-benchmark): draw representative ordinary changes
This commit is contained in:
@@ -0,0 +1,342 @@
|
||||
"""Canonical contracts for ordinary-change selection and contamination review."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from collections import Counter
|
||||
from collections.abc import Sequence
|
||||
from dataclasses import dataclass
|
||||
from enum import StrEnum
|
||||
from typing import Protocol
|
||||
|
||||
from benchmarks.valid.ecosystem_review import CanonicalEcosystem, RepositoryEcosystemReviewV1
|
||||
from benchmarks.valid.fix_site_catalog import FixSiteCandidateV1
|
||||
from benchmarks.valid.ground_truth_review import GroundTruthReviewV1
|
||||
from openultrasast.core.canonical import JsonValue, canonical_bytes, content_digest
|
||||
|
||||
ORDINARY_SELECTION_SCHEMA = "openultrasast.valid-detection-benchmark.ordinary-selection.v1"
|
||||
CONTAMINATION_REVIEW_SCHEMA = "openultrasast.valid-detection-benchmark.ordinary-contamination-review.v1"
|
||||
_DIGEST = re.compile(r"[0-9a-f]{64}")
|
||||
_REVISION = re.compile(r"(?:[0-9a-f]{40}|[0-9a-f]{64})")
|
||||
|
||||
|
||||
class OrdinarySelectionError(ValueError):
|
||||
"""Ordinary history or reviewed contamination cannot authorize a draw."""
|
||||
|
||||
|
||||
class ContaminationTiming(StrEnum):
|
||||
PRE_FREEZE = "pre_freeze"
|
||||
POST_FREEZE = "post_freeze"
|
||||
|
||||
|
||||
class OrdinaryExclusionReason(StrEnum):
|
||||
REVIEWED_SECURITY_DEFECT = "reviewed_security_defect"
|
||||
|
||||
|
||||
class OrdinaryHistoryReader(Protocol):
|
||||
def parents(self, revision: str) -> Sequence[str]: ...
|
||||
|
||||
def change_bytes(self, revision: str) -> bytes | None: ...
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ReviewedPositiveInput:
|
||||
candidate: FixSiteCandidateV1
|
||||
ecosystem_review: RepositoryEcosystemReviewV1
|
||||
ground_truth_review: GroundTruthReviewV1
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class OrdinaryHistorySource:
|
||||
reader: OrdinaryHistoryReader
|
||||
canonical_repository: str
|
||||
network_identity_digest: str
|
||||
source_digest: str
|
||||
|
||||
|
||||
@dataclass(frozen=True, order=True)
|
||||
class OrdinaryPushV1:
|
||||
canonical_repository: str
|
||||
network_identity_digest: str
|
||||
ecosystem: CanonicalEcosystem
|
||||
base_revision: str
|
||||
tip_revision: str
|
||||
change_byte_length: int
|
||||
change_content_digest: str
|
||||
history_source_digest: str
|
||||
push_digest: str
|
||||
|
||||
|
||||
@dataclass(frozen=True, order=True)
|
||||
class OrdinaryDrawAssignmentV1:
|
||||
positive_change_digest: str
|
||||
network_identity_digest: str
|
||||
ecosystem: CanonicalEcosystem
|
||||
push_digest: str
|
||||
|
||||
|
||||
@dataclass(frozen=True, order=True)
|
||||
class OrdinaryExclusionV1:
|
||||
network_identity_digest: str
|
||||
tip_revision: str
|
||||
reason: OrdinaryExclusionReason
|
||||
contamination_review_digest: str
|
||||
|
||||
|
||||
@dataclass(frozen=True, order=True)
|
||||
class OrdinaryFixExclusionV1:
|
||||
network_identity_digest: str
|
||||
fixed_revision: str
|
||||
positive_change_digest: str
|
||||
ground_truth_review_digest: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class OrdinaryContaminationReviewV1:
|
||||
network_identity_digest: str
|
||||
tip_revision: str
|
||||
timing: ContaminationTiming
|
||||
evidence_digest: str
|
||||
producer_identity: str
|
||||
reviewer_identity: str
|
||||
conflict_disclosed: bool
|
||||
policy_digest: str
|
||||
review_digest: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class OrdinarySelectionV1:
|
||||
required_per_positive: int
|
||||
policy_digest: str
|
||||
positive_change_digests: tuple[str, ...]
|
||||
fix_exclusions: tuple[OrdinaryFixExclusionV1, ...]
|
||||
pushes: tuple[OrdinaryPushV1, ...]
|
||||
assignments: tuple[OrdinaryDrawAssignmentV1, ...]
|
||||
exclusions: tuple[OrdinaryExclusionV1, ...]
|
||||
provenance_digest: str
|
||||
selection_digest: str
|
||||
|
||||
|
||||
def _digest(value: object, field: str) -> str:
|
||||
if not isinstance(value, str) or _DIGEST.fullmatch(value) is None:
|
||||
raise OrdinarySelectionError(f"{field} must be a full lowercase SHA-256 digest")
|
||||
return value
|
||||
|
||||
|
||||
def _revision(value: object, field: str) -> str:
|
||||
if not isinstance(value, str) or _REVISION.fullmatch(value) is None:
|
||||
raise OrdinarySelectionError(f"{field} must be a full Git object identity")
|
||||
return value
|
||||
|
||||
|
||||
def _text(value: object, field: str) -> str:
|
||||
if not isinstance(value, str) or not value.strip():
|
||||
raise OrdinarySelectionError(f"{field} must be nonempty text")
|
||||
return value.strip()
|
||||
|
||||
|
||||
def _push_value(push: OrdinaryPushV1, *, include_digest: bool) -> dict[str, JsonValue]:
|
||||
value: dict[str, JsonValue] = {
|
||||
"canonical_repository": _text(push.canonical_repository, "canonical repository"),
|
||||
"network_identity_digest": _digest(push.network_identity_digest, "network identity digest"),
|
||||
"ecosystem": CanonicalEcosystem(push.ecosystem).value,
|
||||
"base_revision": _revision(push.base_revision, "ordinary base revision"),
|
||||
"tip_revision": _revision(push.tip_revision, "ordinary tip revision"),
|
||||
"change_byte_length": push.change_byte_length,
|
||||
"change_content_digest": _digest(push.change_content_digest, "ordinary change digest"),
|
||||
"history_source_digest": _digest(push.history_source_digest, "history source digest"),
|
||||
}
|
||||
if push.base_revision == push.tip_revision or push.change_byte_length <= 0:
|
||||
raise OrdinarySelectionError("ordinary push byte receipt or revisions are invalid")
|
||||
if include_digest:
|
||||
value["push_digest"] = _digest(push.push_digest, "ordinary push digest")
|
||||
return value
|
||||
|
||||
|
||||
def _assignment_value(item: OrdinaryDrawAssignmentV1) -> dict[str, JsonValue]:
|
||||
return {
|
||||
"positive_change_digest": _digest(item.positive_change_digest, "positive change digest"),
|
||||
"network_identity_digest": _digest(item.network_identity_digest, "network identity digest"),
|
||||
"ecosystem": CanonicalEcosystem(item.ecosystem).value,
|
||||
"push_digest": _digest(item.push_digest, "ordinary push digest"),
|
||||
}
|
||||
|
||||
|
||||
def _exclusion_value(item: OrdinaryExclusionV1) -> dict[str, JsonValue]:
|
||||
return {
|
||||
"network_identity_digest": _digest(item.network_identity_digest, "network identity digest"),
|
||||
"tip_revision": _revision(item.tip_revision, "excluded tip revision"),
|
||||
"reason": OrdinaryExclusionReason(item.reason).value,
|
||||
"contamination_review_digest": _digest(item.contamination_review_digest, "contamination review digest"),
|
||||
}
|
||||
|
||||
|
||||
def _fix_exclusion_value(item: OrdinaryFixExclusionV1) -> dict[str, JsonValue]:
|
||||
return {
|
||||
"network_identity_digest": _digest(item.network_identity_digest, "network identity digest"),
|
||||
"fixed_revision": _revision(item.fixed_revision, "accepted fixed revision"),
|
||||
"positive_change_digest": _digest(item.positive_change_digest, "positive change digest"),
|
||||
"ground_truth_review_digest": _digest(item.ground_truth_review_digest, "ground-truth review digest"),
|
||||
}
|
||||
|
||||
|
||||
def _selection_value(selection: OrdinarySelectionV1, *, include_digest: bool) -> dict[str, JsonValue]:
|
||||
value: dict[str, JsonValue] = {
|
||||
"schema_version": ORDINARY_SELECTION_SCHEMA,
|
||||
"required_per_positive": selection.required_per_positive,
|
||||
"policy_digest": _digest(selection.policy_digest, "ordinary selection policy digest"),
|
||||
"positive_change_digests": selection.positive_change_digests,
|
||||
"fix_exclusions": tuple(_fix_exclusion_value(item) for item in selection.fix_exclusions),
|
||||
"pushes": tuple(_push_value(push, include_digest=True) for push in selection.pushes),
|
||||
"assignments": tuple(_assignment_value(item) for item in selection.assignments),
|
||||
"exclusions": tuple(_exclusion_value(item) for item in selection.exclusions),
|
||||
"provenance_digest": _digest(selection.provenance_digest, "ordinary provenance digest"),
|
||||
}
|
||||
if include_digest:
|
||||
value["selection_digest"] = _digest(selection.selection_digest, "ordinary selection digest")
|
||||
return value
|
||||
|
||||
|
||||
def _contamination_value(review: OrdinaryContaminationReviewV1, *, include_digest: bool) -> dict[str, JsonValue]:
|
||||
value: dict[str, JsonValue] = {
|
||||
"schema_version": CONTAMINATION_REVIEW_SCHEMA,
|
||||
"network_identity_digest": _digest(review.network_identity_digest, "network identity digest"),
|
||||
"tip_revision": _revision(review.tip_revision, "contaminated tip revision"),
|
||||
"timing": ContaminationTiming(review.timing).value,
|
||||
"evidence_digest": _digest(review.evidence_digest, "contamination evidence digest"),
|
||||
"producer_identity": _text(review.producer_identity, "producer identity"),
|
||||
"reviewer_identity": _text(review.reviewer_identity, "reviewer identity"),
|
||||
"conflict_disclosed": review.conflict_disclosed,
|
||||
"policy_digest": _digest(review.policy_digest, "contamination policy digest"),
|
||||
}
|
||||
if review.producer_identity == review.reviewer_identity or review.conflict_disclosed is not True:
|
||||
raise OrdinarySelectionError("independent contamination review and disclosure are required")
|
||||
if include_digest:
|
||||
value["review_digest"] = _digest(review.review_digest, "contamination review digest")
|
||||
return value
|
||||
|
||||
|
||||
def reviewed_contamination(
|
||||
*,
|
||||
network_identity_digest: str,
|
||||
tip_revision: str,
|
||||
timing: ContaminationTiming | str,
|
||||
evidence_digest: str,
|
||||
producer_identity: str,
|
||||
reviewer_identity: str,
|
||||
conflict_disclosed: bool,
|
||||
policy_digest: str,
|
||||
) -> OrdinaryContaminationReviewV1:
|
||||
draft = OrdinaryContaminationReviewV1(
|
||||
network_identity_digest,
|
||||
tip_revision,
|
||||
ContaminationTiming(timing),
|
||||
evidence_digest,
|
||||
producer_identity,
|
||||
reviewer_identity,
|
||||
conflict_disclosed,
|
||||
policy_digest,
|
||||
"",
|
||||
)
|
||||
digest = content_digest(_contamination_value(draft, include_digest=False))
|
||||
review = OrdinaryContaminationReviewV1(**{**vars(draft), "review_digest": digest})
|
||||
contamination_review_bytes(review)
|
||||
return review
|
||||
|
||||
|
||||
def contamination_review_bytes(review: OrdinaryContaminationReviewV1) -> bytes:
|
||||
expected = content_digest(_contamination_value(review, include_digest=False))
|
||||
if review.review_digest != expected:
|
||||
raise OrdinarySelectionError("contamination review digest does not match")
|
||||
return canonical_bytes(_contamination_value(review, include_digest=True))
|
||||
|
||||
|
||||
def _validate_canonical_collections(selection: OrdinarySelectionV1) -> None:
|
||||
if selection.required_per_positive < 20:
|
||||
raise OrdinarySelectionError("ordinary selection minimum is below 20")
|
||||
if selection.positive_change_digests != tuple(sorted(set(selection.positive_change_digests))):
|
||||
raise OrdinarySelectionError("ordinary positive identities are not canonical")
|
||||
for digest in selection.positive_change_digests:
|
||||
_digest(digest, "positive change digest")
|
||||
if selection.fix_exclusions != tuple(sorted(set(selection.fix_exclusions))):
|
||||
raise OrdinarySelectionError("accepted fix exclusions are not canonical")
|
||||
for item in selection.fix_exclusions:
|
||||
_fix_exclusion_value(item)
|
||||
if selection.pushes != tuple(sorted(set(selection.pushes), key=lambda item: item.push_digest)):
|
||||
raise OrdinarySelectionError("ordinary pushes are not canonical")
|
||||
if selection.assignments != tuple(sorted(set(selection.assignments))):
|
||||
raise OrdinarySelectionError("ordinary assignments are not canonical")
|
||||
if selection.exclusions != tuple(sorted(set(selection.exclusions))):
|
||||
raise OrdinarySelectionError("ordinary exclusions are not canonical")
|
||||
|
||||
|
||||
def _validate_pushes(selection: OrdinarySelectionV1) -> dict[str, OrdinaryPushV1]:
|
||||
pushes = {push.push_digest: push for push in selection.pushes}
|
||||
for push in selection.pushes:
|
||||
if push.push_digest != content_digest(_push_value(push, include_digest=False)):
|
||||
raise OrdinarySelectionError("ordinary push digest does not match")
|
||||
return pushes
|
||||
|
||||
|
||||
def _validate_assignments(selection: OrdinarySelectionV1, pushes: dict[str, OrdinaryPushV1]) -> None:
|
||||
counts = Counter(item.positive_change_digest for item in selection.assignments)
|
||||
for item in selection.assignments:
|
||||
push = pushes.get(item.push_digest)
|
||||
if push is None or (item.network_identity_digest, item.ecosystem) != (push.network_identity_digest, push.ecosystem):
|
||||
raise OrdinarySelectionError("ordinary assignment crosses repository or ecosystem")
|
||||
selected_keys = {(push.network_identity_digest, push.tip_revision) for push in selection.pushes}
|
||||
fixed_keys = {(item.network_identity_digest, item.fixed_revision) for item in selection.fix_exclusions}
|
||||
excluded_keys = {(item.network_identity_digest, item.tip_revision) for item in selection.exclusions}
|
||||
if selected_keys & (excluded_keys | fixed_keys):
|
||||
raise OrdinarySelectionError("reviewed contaminated or accepted fixed push remains selected")
|
||||
fixed_positives = tuple(sorted(item.positive_change_digest for item in selection.fix_exclusions))
|
||||
if fixed_positives != selection.positive_change_digests:
|
||||
raise OrdinarySelectionError("accepted fix exclusions do not bind the exact positive set")
|
||||
if set(counts) != set(selection.positive_change_digests):
|
||||
raise OrdinarySelectionError("ordinary assignments do not cover the exact positive set")
|
||||
if any(count != selection.required_per_positive for count in counts.values()):
|
||||
raise OrdinarySelectionError("ordinary assignment count does not match policy")
|
||||
|
||||
|
||||
def _validate_provenance(selection: OrdinarySelectionV1) -> None:
|
||||
provenance = content_digest(
|
||||
{
|
||||
"history_sources": tuple(
|
||||
sorted({(push.canonical_repository, push.network_identity_digest, push.history_source_digest) for push in selection.pushes})
|
||||
),
|
||||
"accepted_fix_exclusions": tuple(_fix_exclusion_value(item) for item in selection.fix_exclusions),
|
||||
"policy_digest": selection.policy_digest,
|
||||
}
|
||||
)
|
||||
if selection.provenance_digest != provenance:
|
||||
raise OrdinarySelectionError("ordinary provenance digest does not match sources and policy")
|
||||
|
||||
|
||||
def ordinary_selection_bytes(selection: OrdinarySelectionV1) -> bytes:
|
||||
_validate_canonical_collections(selection)
|
||||
pushes = _validate_pushes(selection)
|
||||
_validate_assignments(selection, pushes)
|
||||
_validate_provenance(selection)
|
||||
expected = content_digest(_selection_value(selection, include_digest=False))
|
||||
if selection.selection_digest != expected:
|
||||
raise OrdinarySelectionError("ordinary selection digest does not match")
|
||||
return canonical_bytes(_selection_value(selection, include_digest=True))
|
||||
|
||||
|
||||
__all__ = [
|
||||
"ContaminationTiming",
|
||||
"OrdinaryContaminationReviewV1",
|
||||
"OrdinaryDrawAssignmentV1",
|
||||
"OrdinaryExclusionReason",
|
||||
"OrdinaryExclusionV1",
|
||||
"OrdinaryFixExclusionV1",
|
||||
"OrdinaryHistoryReader",
|
||||
"OrdinaryHistorySource",
|
||||
"OrdinaryPushV1",
|
||||
"OrdinarySelectionError",
|
||||
"OrdinarySelectionV1",
|
||||
"ReviewedPositiveInput",
|
||||
"contamination_review_bytes",
|
||||
"ordinary_selection_bytes",
|
||||
"reviewed_contamination",
|
||||
]
|
||||
@@ -0,0 +1,366 @@
|
||||
"""Deterministic first-parent ordinary-change draws for accepted positives."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Iterable, Mapping
|
||||
|
||||
from benchmarks.valid.ecosystem_review import CanonicalEcosystem
|
||||
from benchmarks.valid.ground_truth_review import GroundTruthStatus, ground_truth_review_bytes, project_accepted_ground_truth
|
||||
from benchmarks.valid.lifecycle import (
|
||||
PopulationLifecycleProjectionV1,
|
||||
PopulationLifecycleState,
|
||||
RetirementReason,
|
||||
reviewed_retirement_event,
|
||||
)
|
||||
from benchmarks.valid.lifecycle_projection import projection_bytes
|
||||
from benchmarks.valid.ordinary_change_contract import (
|
||||
ContaminationTiming,
|
||||
OrdinaryContaminationReviewV1,
|
||||
OrdinaryDrawAssignmentV1,
|
||||
OrdinaryExclusionReason,
|
||||
OrdinaryExclusionV1,
|
||||
OrdinaryFixExclusionV1,
|
||||
OrdinaryHistoryReader,
|
||||
OrdinaryHistorySource,
|
||||
OrdinaryPushV1,
|
||||
OrdinarySelectionError,
|
||||
OrdinarySelectionV1,
|
||||
ReviewedPositiveInput,
|
||||
_digest,
|
||||
_fix_exclusion_value,
|
||||
_push_value,
|
||||
_revision,
|
||||
_selection_value,
|
||||
contamination_review_bytes,
|
||||
ordinary_selection_bytes,
|
||||
reviewed_contamination,
|
||||
)
|
||||
from openultrasast.core.canonical import bytes_digest, content_digest
|
||||
|
||||
|
||||
def _validate_positive(item: ReviewedPositiveInput, ecosystem_policy: str, ground_truth_policy: str) -> tuple[str, str, CanonicalEcosystem]:
|
||||
encoded = ground_truth_review_bytes(
|
||||
item.ground_truth_review,
|
||||
candidate=item.candidate,
|
||||
ecosystem_review=item.ecosystem_review,
|
||||
expected_ecosystem_policy_digest=ecosystem_policy,
|
||||
expected_ground_truth_policy_digest=ground_truth_policy,
|
||||
)
|
||||
if not encoded or item.ground_truth_review.status is not GroundTruthStatus.ACCEPTED:
|
||||
raise OrdinarySelectionError("ordinary draw requires accepted ground truth")
|
||||
ecosystem = item.ecosystem_review.ecosystem
|
||||
if ecosystem is None:
|
||||
raise OrdinarySelectionError("ordinary draw requires accepted ecosystem")
|
||||
return item.ground_truth_review.change_identity_digest, item.candidate.network_identity_digest, ecosystem
|
||||
|
||||
|
||||
def _contaminations(
|
||||
reviews: Iterable[OrdinaryContaminationReviewV1],
|
||||
) -> dict[tuple[str, str], OrdinaryContaminationReviewV1]:
|
||||
result: dict[tuple[str, str], OrdinaryContaminationReviewV1] = {}
|
||||
for review in reviews:
|
||||
contamination_review_bytes(review)
|
||||
if review.timing is not ContaminationTiming.PRE_FREEZE:
|
||||
raise OrdinarySelectionError("post-freeze contamination cannot rewrite an ordinary draw")
|
||||
key = (review.network_identity_digest, review.tip_revision)
|
||||
existing = result.get(key)
|
||||
if existing is not None and existing != review:
|
||||
raise OrdinarySelectionError("one ordinary push has conflicting contamination reviews")
|
||||
result[key] = review
|
||||
return result
|
||||
|
||||
|
||||
def _ordinary_push(
|
||||
item: ReviewedPositiveInput,
|
||||
ecosystem: CanonicalEcosystem,
|
||||
source_digest: str,
|
||||
base: str,
|
||||
tip: str,
|
||||
content: bytes,
|
||||
) -> OrdinaryPushV1:
|
||||
if not isinstance(content, bytes) or not content:
|
||||
raise OrdinarySelectionError("ordinary change bytes are empty")
|
||||
draft = OrdinaryPushV1(
|
||||
item.candidate.canonical_repository,
|
||||
item.candidate.network_identity_digest,
|
||||
ecosystem,
|
||||
base,
|
||||
tip,
|
||||
len(content),
|
||||
bytes_digest(content),
|
||||
source_digest,
|
||||
"",
|
||||
)
|
||||
return OrdinaryPushV1(**{**vars(draft), "push_digest": content_digest(_push_value(draft, include_digest=False))})
|
||||
|
||||
|
||||
def _draw_one(
|
||||
item: ReviewedPositiveInput,
|
||||
source: OrdinaryHistorySource,
|
||||
identity: tuple[str, str, CanonicalEcosystem],
|
||||
accepted_fixes: frozenset[tuple[str, str]],
|
||||
contamination: Mapping[tuple[str, str], OrdinaryContaminationReviewV1],
|
||||
required: int,
|
||||
) -> tuple[list[OrdinaryPushV1], list[OrdinaryExclusionV1]]:
|
||||
_, network_digest, ecosystem = identity
|
||||
fixed = item.candidate.fixed.revision
|
||||
parents = tuple(source.reader.parents(fixed))
|
||||
if not parents or parents[0] != item.candidate.vulnerable.revision:
|
||||
raise OrdinarySelectionError("history does not bind the exact fix parent")
|
||||
tip = parents[0]
|
||||
pushes: list[OrdinaryPushV1] = []
|
||||
exclusions: list[OrdinaryExclusionV1] = []
|
||||
seen = {fixed}
|
||||
while len(pushes) < required:
|
||||
if tip in seen:
|
||||
raise OrdinarySelectionError("first-parent history contains a cycle")
|
||||
seen.add(tip)
|
||||
ancestors = tuple(source.reader.parents(tip))
|
||||
if not ancestors:
|
||||
break
|
||||
base = _revision(ancestors[0], "ordinary first parent")
|
||||
if (network_digest, tip) in accepted_fixes:
|
||||
tip = base
|
||||
continue
|
||||
content = source.reader.change_bytes(tip)
|
||||
if content is None:
|
||||
raise OrdinarySelectionError("ordinary change bytes are unreadable")
|
||||
review = contamination.get((network_digest, tip))
|
||||
if review is None:
|
||||
pushes.append(_ordinary_push(item, ecosystem, source.source_digest, base, tip, content))
|
||||
else:
|
||||
exclusions.append(
|
||||
OrdinaryExclusionV1(
|
||||
network_digest,
|
||||
tip,
|
||||
OrdinaryExclusionReason.REVIEWED_SECURITY_DEFECT,
|
||||
review.review_digest,
|
||||
)
|
||||
)
|
||||
tip = base
|
||||
if len(pushes) < required:
|
||||
raise OrdinarySelectionError(f"ordinary first-parent history supplies {len(pushes)} of {required} required pushes")
|
||||
return pushes, exclusions
|
||||
|
||||
|
||||
def _validated_positives(
|
||||
positives: Iterable[ReviewedPositiveInput],
|
||||
ecosystem_policy: str,
|
||||
ground_truth_policy: str,
|
||||
) -> tuple[ReviewedPositiveInput, ...]:
|
||||
values = tuple(positives)
|
||||
candidates = {item.candidate.candidate_digest: item.candidate for item in values}
|
||||
ecosystems = {item.ecosystem_review.review_digest: item.ecosystem_review for item in values}
|
||||
project_accepted_ground_truth(
|
||||
(item.ground_truth_review for item in values),
|
||||
candidates_by_digest=candidates,
|
||||
ecosystem_reviews_by_digest=ecosystems,
|
||||
expected_ecosystem_policy_digest=ecosystem_policy,
|
||||
expected_ground_truth_policy_digest=ground_truth_policy,
|
||||
)
|
||||
if not values:
|
||||
raise OrdinarySelectionError("ordinary draw requires accepted positives")
|
||||
return values
|
||||
|
||||
|
||||
def _accepted_fixes(
|
||||
positives: tuple[ReviewedPositiveInput, ...],
|
||||
) -> tuple[tuple[OrdinaryFixExclusionV1, ...], frozenset[tuple[str, str]]]:
|
||||
exclusions: set[OrdinaryFixExclusionV1] = set()
|
||||
revisions: set[tuple[str, str]] = set()
|
||||
for item in positives:
|
||||
review = item.ground_truth_review
|
||||
exclusion = OrdinaryFixExclusionV1(
|
||||
item.candidate.network_identity_digest,
|
||||
item.candidate.fixed.revision,
|
||||
review.change_identity_digest,
|
||||
review.review_digest,
|
||||
)
|
||||
exclusions.add(exclusion)
|
||||
revisions.add((exclusion.network_identity_digest, exclusion.fixed_revision))
|
||||
ordered = tuple(sorted(exclusions))
|
||||
if len(ordered) != len(positives):
|
||||
raise OrdinarySelectionError("accepted positives do not have unique fix authorities")
|
||||
return ordered, frozenset(revisions)
|
||||
|
||||
|
||||
def _collect_draws(
|
||||
positives: tuple[ReviewedPositiveInput, ...],
|
||||
history_by_network: Mapping[str, OrdinaryHistorySource],
|
||||
accepted_fixes: frozenset[tuple[str, str]],
|
||||
contamination: Mapping[tuple[str, str], OrdinaryContaminationReviewV1],
|
||||
required: int,
|
||||
ecosystem_policy: str,
|
||||
ground_truth_policy: str,
|
||||
) -> tuple[
|
||||
dict[str, OrdinaryPushV1],
|
||||
set[OrdinaryDrawAssignmentV1],
|
||||
set[OrdinaryExclusionV1],
|
||||
set[str],
|
||||
set[tuple[str, str, str]],
|
||||
]:
|
||||
pushes_by_digest: dict[str, OrdinaryPushV1] = {}
|
||||
assignments: set[OrdinaryDrawAssignmentV1] = set()
|
||||
exclusions: set[OrdinaryExclusionV1] = set()
|
||||
positive_digests: set[str] = set()
|
||||
source_identities: set[tuple[str, str, str]] = set()
|
||||
for item in positives:
|
||||
identity = _validate_positive(item, ecosystem_policy, ground_truth_policy)
|
||||
change_digest, network_digest, ecosystem = identity
|
||||
if change_digest in positive_digests:
|
||||
raise OrdinarySelectionError("ordinary draw contains a duplicate positive change identity")
|
||||
positive_digests.add(change_digest)
|
||||
source = history_by_network.get(network_digest)
|
||||
if source is None:
|
||||
raise OrdinarySelectionError("ordinary history source is missing for repository network")
|
||||
if (source.canonical_repository, source.network_identity_digest) != (
|
||||
item.candidate.canonical_repository,
|
||||
network_digest,
|
||||
):
|
||||
raise OrdinarySelectionError("ordinary history source crosses repository identity")
|
||||
source_digest = _digest(source.source_digest, "history source digest")
|
||||
source_identities.add((source.canonical_repository, network_digest, source_digest))
|
||||
drawn, excluded = _draw_one(item, source, identity, accepted_fixes, contamination, required)
|
||||
for push in drawn:
|
||||
existing = pushes_by_digest.get(push.push_digest)
|
||||
if existing is not None and existing != push:
|
||||
raise OrdinarySelectionError("same ordinary push identity has different content")
|
||||
pushes_by_digest[push.push_digest] = push
|
||||
assignments.add(OrdinaryDrawAssignmentV1(change_digest, network_digest, ecosystem, push.push_digest))
|
||||
exclusions.update(excluded)
|
||||
supplied_reviews = {review.review_digest for review in contamination.values()}
|
||||
applied_reviews = {item.contamination_review_digest for item in exclusions}
|
||||
if supplied_reviews != applied_reviews:
|
||||
raise OrdinarySelectionError("contamination review does not identify the selected first-parent histories")
|
||||
return pushes_by_digest, assignments, exclusions, positive_digests, source_identities
|
||||
|
||||
|
||||
def draw_ordinary_selection(
|
||||
positives: Iterable[ReviewedPositiveInput],
|
||||
*,
|
||||
history_by_network: Mapping[str, OrdinaryHistorySource],
|
||||
contamination_reviews: Iterable[OrdinaryContaminationReviewV1] = (),
|
||||
required_per_positive: int = 20,
|
||||
policy_digest: str,
|
||||
expected_ecosystem_policy_digest: str,
|
||||
expected_ground_truth_policy_digest: str,
|
||||
) -> OrdinarySelectionV1:
|
||||
if isinstance(required_per_positive, bool) or required_per_positive < 20:
|
||||
raise OrdinarySelectionError("ordinary draw requires at least 20 pushes per positive")
|
||||
positives = _validated_positives(positives, expected_ecosystem_policy_digest, expected_ground_truth_policy_digest)
|
||||
policy = _digest(policy_digest, "ordinary selection policy digest")
|
||||
contamination = _contaminations(contamination_reviews)
|
||||
fix_exclusions, accepted_fixes = _accepted_fixes(positives)
|
||||
pushes, assignments, exclusions, positive_digests, source_identities = _collect_draws(
|
||||
positives,
|
||||
history_by_network,
|
||||
accepted_fixes,
|
||||
contamination,
|
||||
required_per_positive,
|
||||
expected_ecosystem_policy_digest,
|
||||
expected_ground_truth_policy_digest,
|
||||
)
|
||||
draft = OrdinarySelectionV1(
|
||||
required_per_positive,
|
||||
policy,
|
||||
tuple(sorted(positive_digests)),
|
||||
fix_exclusions,
|
||||
tuple(pushes[key] for key in sorted(pushes)),
|
||||
tuple(sorted(assignments)),
|
||||
tuple(sorted(exclusions)),
|
||||
content_digest(
|
||||
{
|
||||
"history_sources": tuple(sorted(source_identities)),
|
||||
"accepted_fix_exclusions": tuple(_fix_exclusion_value(item) for item in fix_exclusions),
|
||||
"policy_digest": policy,
|
||||
}
|
||||
),
|
||||
"",
|
||||
)
|
||||
selection = OrdinarySelectionV1(**{**vars(draft), "selection_digest": content_digest(_selection_value(draft, include_digest=False))})
|
||||
ordinary_selection_bytes(selection)
|
||||
return selection
|
||||
|
||||
|
||||
def validate_ordinary_selection(
|
||||
selection: OrdinarySelectionV1,
|
||||
positives: Iterable[ReviewedPositiveInput],
|
||||
*,
|
||||
history_by_network: Mapping[str, OrdinaryHistorySource],
|
||||
contamination_reviews: Iterable[OrdinaryContaminationReviewV1] = (),
|
||||
expected_ecosystem_policy_digest: str,
|
||||
expected_ground_truth_policy_digest: str,
|
||||
) -> bytes:
|
||||
"""Reproduce one draw from exact review, history, contamination, and policy inputs."""
|
||||
expected = draw_ordinary_selection(
|
||||
positives,
|
||||
history_by_network=history_by_network,
|
||||
contamination_reviews=contamination_reviews,
|
||||
required_per_positive=selection.required_per_positive,
|
||||
policy_digest=selection.policy_digest,
|
||||
expected_ecosystem_policy_digest=expected_ecosystem_policy_digest,
|
||||
expected_ground_truth_policy_digest=expected_ground_truth_policy_digest,
|
||||
)
|
||||
if expected != selection:
|
||||
raise OrdinarySelectionError("ordinary selection does not reproduce from exact inputs")
|
||||
return ordinary_selection_bytes(selection)
|
||||
|
||||
|
||||
def post_freeze_contamination_retirement(
|
||||
selection: OrdinarySelectionV1,
|
||||
review: OrdinaryContaminationReviewV1,
|
||||
lifecycle: PopulationLifecycleProjectionV1,
|
||||
) -> bytes:
|
||||
before = ordinary_selection_bytes(selection)
|
||||
contamination_review_bytes(review)
|
||||
projection_bytes(lifecycle)
|
||||
if review.timing is not ContaminationTiming.POST_FREEZE:
|
||||
raise OrdinarySelectionError("retirement requires post-freeze contamination review")
|
||||
matches = [
|
||||
push
|
||||
for push in selection.pushes
|
||||
if (push.network_identity_digest, push.tip_revision) == (review.network_identity_digest, review.tip_revision)
|
||||
]
|
||||
if len(matches) != 1:
|
||||
raise OrdinarySelectionError("post-freeze contamination does not identify one selected push")
|
||||
if lifecycle.state is not PopulationLifecycleState.REPLACEMENT_COMPLETE or not lifecycle.admission_eligible:
|
||||
raise OrdinarySelectionError("post-freeze contamination requires an active replacement lifecycle")
|
||||
if lifecycle.population_digest is None or lifecycle.freeze_digest is None:
|
||||
raise OrdinarySelectionError("active replacement lifecycle lacks population identity")
|
||||
evidence = content_digest({"selection_digest": selection.selection_digest, "review_digest": review.review_digest})
|
||||
event = reviewed_retirement_event(
|
||||
predecessor_digest=lifecycle.head_event_digest,
|
||||
population_digest=lifecycle.population_digest,
|
||||
freeze_digest=lifecycle.freeze_digest,
|
||||
evidence_digest=evidence,
|
||||
reason_code=RetirementReason.CONTAMINATED_NEGATIVE.value,
|
||||
producer_identity=review.producer_identity,
|
||||
reviewer_identity=review.reviewer_identity,
|
||||
conflict_disclosed=review.conflict_disclosed,
|
||||
policy_digest=review.policy_digest,
|
||||
)
|
||||
if ordinary_selection_bytes(selection) != before:
|
||||
raise OrdinarySelectionError("post-freeze contamination rewrote the ordinary selection")
|
||||
return event
|
||||
|
||||
|
||||
__all__ = [
|
||||
"ContaminationTiming",
|
||||
"OrdinaryContaminationReviewV1",
|
||||
"OrdinaryDrawAssignmentV1",
|
||||
"OrdinaryExclusionReason",
|
||||
"OrdinaryExclusionV1",
|
||||
"OrdinaryFixExclusionV1",
|
||||
"OrdinaryHistoryReader",
|
||||
"OrdinaryHistorySource",
|
||||
"OrdinaryPushV1",
|
||||
"OrdinarySelectionError",
|
||||
"OrdinarySelectionV1",
|
||||
"ReviewedPositiveInput",
|
||||
"contamination_review_bytes",
|
||||
"draw_ordinary_selection",
|
||||
"ordinary_selection_bytes",
|
||||
"post_freeze_contamination_retirement",
|
||||
"reviewed_contamination",
|
||||
"validate_ordinary_selection",
|
||||
]
|
||||
Reference in New Issue
Block a user