mirror of
https://github.com/norandom/OpenUltraSAST.git
synced 2026-12-03 02:08:18 +00:00
feat(valid-detection-benchmark): draw representative ordinary changes
This commit is contained in:
@@ -98,7 +98,7 @@
|
||||
- _Boundary: Review Authority_
|
||||
|
||||
- [ ] 4. Freeze a reproducible label-isolated replacement population
|
||||
- [ ] 4.1 Draw and validate representative ordinary pushes
|
||||
- [x] 4.1 Draw and validate representative ordinary pushes
|
||||
- Draw at least twenty eligible first-parent ordinary pushes per accepted positive from the same canonical repository and exclude the security fix.
|
||||
- Store each underlying ordinary push once with its reviewed ecosystem and deterministic draw/provenance identity.
|
||||
- Exclude reviewed pre-freeze defects with a reason and route verified post-freeze contamination to lifecycle retirement rather than rewriting a result.
|
||||
@@ -421,3 +421,4 @@
|
||||
- Task 3.1 builds non-authoritative fix-site candidates from only the fixed revision and its exact parent, binding nonzero pre/post function bytes and repository/package/language/license/source/change provenance without blame or introducing-commit inference.
|
||||
- Task 3.2 revalidates externally governed ecosystem reviews against exact candidate/evidence/policy identities, derives closed accepted/insufficient outcomes, and enforces a repository↔network bijection before projecting canonical Python or JavaScript ecosystems.
|
||||
- Task 3.3 revalidates exact candidate and ecosystem authorities, derives accepted/excluded ground truth from explicit independent human judgments, keeps model assistance non-authoritative, and projects each change once while preserving all verified exact scopes.
|
||||
- Task 4.1 deterministically draws at least twenty same-network first-parent ordinary pushes per accepted positive, globally excludes every accepted fix revision, stores shared pushes once, and routes reviewed contamination to exclusion or terminal retirement according to freeze timing.
|
||||
|
||||
@@ -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",
|
||||
]
|
||||
@@ -0,0 +1,423 @@
|
||||
"""Deterministic ordinary-change selection and contamination lifecycle tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
from benchmarks.valid import lifecycle as _lifecycle_module # noqa: E402, F401
|
||||
from benchmarks.valid import lifecycle_projection as lifecycle_projection_module # noqa: E402
|
||||
from benchmarks.valid import ordinary_changes as ordinary # noqa: E402
|
||||
from benchmarks.valid.ecosystem_review import ( # noqa: E402
|
||||
CanonicalEcosystem,
|
||||
EcosystemReviewAttestationV1,
|
||||
EcosystemReviewDecision,
|
||||
review_repository_ecosystem,
|
||||
)
|
||||
from benchmarks.valid.fix_site_catalog import ( # noqa: E402
|
||||
EvidenceRef,
|
||||
FixSiteRequest,
|
||||
LicenseEvidence,
|
||||
PackageEcosystemEvidence,
|
||||
SourceEvidence,
|
||||
build_fix_site_candidate,
|
||||
)
|
||||
from benchmarks.valid.ground_truth_review import ( # noqa: E402
|
||||
GroundTruthAttestationV1,
|
||||
VerifiedScope,
|
||||
review_ground_truth,
|
||||
)
|
||||
from benchmarks.valid.lifecycle import ( # noqa: E402
|
||||
PopulationLifecycleProjectionV1,
|
||||
PopulationLifecycleState,
|
||||
decode_lifecycle_event,
|
||||
)
|
||||
from benchmarks.valid.reservation_identity import build_network_identity # noqa: E402
|
||||
from openultrasast.core.canonical import content_digest # noqa: E402
|
||||
|
||||
PARENT = "a" * 40
|
||||
FIXED = "b" * 40
|
||||
SECOND_FIXED = "c" * 40
|
||||
ECOSYSTEM_POLICY = content_digest({"policy": "ecosystem-v1"})
|
||||
GROUND_TRUTH_POLICY = content_digest({"policy": "ground-truth-v1"})
|
||||
DRAW_POLICY = content_digest({"policy": "ordinary-first-parent-v1", "required": 20})
|
||||
SOURCE = content_digest({"source": "fixture-first-parent-history"})
|
||||
CONTAMINATION_POLICY = content_digest({"policy": "contamination-review-v1"})
|
||||
|
||||
|
||||
class _FixReader:
|
||||
def __init__(self, fixed: str, parent: str) -> None:
|
||||
self.fixed = fixed
|
||||
self.parent = parent
|
||||
|
||||
def parents(self, revision: str) -> tuple[str, ...]:
|
||||
return (self.parent,) if revision == self.fixed else ()
|
||||
|
||||
def blob(self, revision: str, path: str) -> str | None:
|
||||
if path != "src/app.py":
|
||||
return None
|
||||
return {
|
||||
self.parent: 'def handle():\n return "before"\n',
|
||||
self.fixed: f'def handle():\n return "{self.fixed[:1]}"\n',
|
||||
}.get(revision)
|
||||
|
||||
|
||||
class _History:
|
||||
def __init__(
|
||||
self,
|
||||
count: int,
|
||||
*,
|
||||
merge_at: int | None = None,
|
||||
unreadable_at: int | None = None,
|
||||
fixed_parents: dict[str, str] | None = None,
|
||||
) -> None:
|
||||
tips = [PARENT, *(_revision(index) for index in range(1, count))]
|
||||
root = _revision(count)
|
||||
self.parents_by_tip: dict[str, tuple[str, ...]] = {}
|
||||
for index, tip in enumerate(tips):
|
||||
parent = tips[index + 1] if index + 1 < len(tips) else root
|
||||
parents = (parent, "f" * 40) if merge_at == index else (parent,)
|
||||
self.parents_by_tip[tip] = parents
|
||||
self.unreadable = tips[unreadable_at] if unreadable_at is not None else None
|
||||
self.fixed_parents = fixed_parents or {FIXED: PARENT, SECOND_FIXED: PARENT}
|
||||
|
||||
def parents(self, revision: str) -> tuple[str, ...]:
|
||||
if revision in self.fixed_parents:
|
||||
return (self.fixed_parents[revision],)
|
||||
return self.parents_by_tip.get(revision, ())
|
||||
|
||||
def change_bytes(self, revision: str) -> bytes | None:
|
||||
if revision == self.unreadable:
|
||||
return None
|
||||
return f"change:{revision}".encode()
|
||||
|
||||
|
||||
def _revision(index: int) -> str:
|
||||
return f"{index + 1:040x}"
|
||||
|
||||
|
||||
def _positive(*, fixed: str = FIXED, parent: str = PARENT) -> ordinary.ReviewedPositiveInput:
|
||||
network = build_network_identity("https://github.com/example/application")
|
||||
candidate = build_fix_site_candidate(
|
||||
_FixReader(fixed, parent),
|
||||
FixSiteRequest(
|
||||
repository=network,
|
||||
vulnerable_parent=parent,
|
||||
fixed_revision=fixed,
|
||||
path="src/app.py",
|
||||
function="handle",
|
||||
observed_language="python",
|
||||
package_evidence=PackageEcosystemEvidence(
|
||||
"python",
|
||||
EvidenceRef("pyproject.toml", content_digest({"manifest": "python"}), 17),
|
||||
),
|
||||
license_evidence=LicenseEvidence(
|
||||
"Apache-2.0",
|
||||
EvidenceRef("LICENSE", content_digest({"license": "Apache-2.0"}), 11),
|
||||
),
|
||||
source_evidence=SourceEvidence(
|
||||
"security_advisory",
|
||||
f"fixture://advisory/{fixed[:1]}",
|
||||
content_digest({"advisory": fixed}),
|
||||
),
|
||||
change_inputs_digest=content_digest({"parent": parent, "fixed": fixed}),
|
||||
),
|
||||
)
|
||||
ecosystem = review_repository_ecosystem(
|
||||
candidate,
|
||||
EcosystemReviewAttestationV1(
|
||||
candidate.candidate_digest,
|
||||
candidate.source_evidence.evidence_digest,
|
||||
"[email protected]",
|
||||
"[email protected]",
|
||||
True,
|
||||
ECOSYSTEM_POLICY,
|
||||
True,
|
||||
EcosystemReviewDecision.PYTHON,
|
||||
),
|
||||
expected_policy_digest=ECOSYSTEM_POLICY,
|
||||
)
|
||||
ground_truth = review_ground_truth(
|
||||
candidate,
|
||||
ecosystem,
|
||||
GroundTruthAttestationV1(
|
||||
candidate.candidate_digest,
|
||||
ecosystem.review_digest,
|
||||
"[email protected]",
|
||||
"[email protected]",
|
||||
True,
|
||||
GROUND_TRUTH_POLICY,
|
||||
True,
|
||||
True,
|
||||
True,
|
||||
True,
|
||||
(VerifiedScope("injection", CanonicalEcosystem.PYTHON),),
|
||||
),
|
||||
expected_ecosystem_policy_digest=ECOSYSTEM_POLICY,
|
||||
expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY,
|
||||
)
|
||||
return ordinary.ReviewedPositiveInput(candidate, ecosystem, ground_truth)
|
||||
|
||||
|
||||
def _draw(
|
||||
positives: tuple[ordinary.ReviewedPositiveInput, ...] = (),
|
||||
*,
|
||||
history: _History | None = None,
|
||||
reviews: tuple[ordinary.OrdinaryContaminationReviewV1, ...] = (),
|
||||
required: int = 20,
|
||||
) -> ordinary.OrdinarySelectionV1:
|
||||
values = tuple(positives) or (_positive(),)
|
||||
source = ordinary.OrdinaryHistorySource(
|
||||
history or _History(21, merge_at=3),
|
||||
values[0].candidate.canonical_repository,
|
||||
values[0].candidate.network_identity_digest,
|
||||
SOURCE,
|
||||
)
|
||||
return ordinary.draw_ordinary_selection(
|
||||
values,
|
||||
history_by_network={values[0].candidate.network_identity_digest: source},
|
||||
contamination_reviews=reviews,
|
||||
required_per_positive=required,
|
||||
policy_digest=DRAW_POLICY,
|
||||
expected_ecosystem_policy_digest=ECOSYSTEM_POLICY,
|
||||
expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY,
|
||||
)
|
||||
|
||||
|
||||
def _contamination(tip: str, timing: ordinary.ContaminationTiming) -> ordinary.OrdinaryContaminationReviewV1:
|
||||
return ordinary.reviewed_contamination(
|
||||
network_identity_digest=_positive().candidate.network_identity_digest,
|
||||
tip_revision=tip,
|
||||
timing=timing,
|
||||
evidence_digest=content_digest({"contamination": tip}),
|
||||
producer_identity="[email protected]",
|
||||
reviewer_identity="[email protected]",
|
||||
conflict_disclosed=True,
|
||||
policy_digest=CONTAMINATION_POLICY,
|
||||
)
|
||||
|
||||
|
||||
def _active_lifecycle(population: str, freeze: str) -> PopulationLifecycleProjectionV1:
|
||||
draft = PopulationLifecycleProjectionV1(
|
||||
PopulationLifecycleState.REPLACEMENT_COMPLETE,
|
||||
content_digest({"withdrawal": 1}),
|
||||
population,
|
||||
freeze,
|
||||
content_digest({"head": 1}),
|
||||
(content_digest({"root": 1}), content_digest({"head": 1})),
|
||||
(),
|
||||
True,
|
||||
"",
|
||||
)
|
||||
return replace(
|
||||
draft,
|
||||
projection_digest=content_digest(lifecycle_projection_module._projection_value(draft, include_digest=False)),
|
||||
)
|
||||
|
||||
|
||||
def test_draws_twenty_first_parent_pushes_deterministically_and_excludes_fix() -> None:
|
||||
first = _draw()
|
||||
second = _draw()
|
||||
|
||||
assert ordinary.ordinary_selection_bytes(first) == ordinary.ordinary_selection_bytes(second)
|
||||
assert len(first.pushes) == len(first.assignments) == 20
|
||||
assert all(push.tip_revision != FIXED for push in first.pushes)
|
||||
merge_tip = _revision(3)
|
||||
merge = next(push for push in first.pushes if push.tip_revision == merge_tip)
|
||||
assert merge.base_revision == _revision(4)
|
||||
|
||||
|
||||
def test_nineteen_eligible_pushes_are_insufficient() -> None:
|
||||
with pytest.raises(ordinary.OrdinarySelectionError, match="19 of 20"):
|
||||
_draw(history=_History(19))
|
||||
|
||||
|
||||
def test_shared_underlying_pushes_are_stored_once_but_assigned_per_positive() -> None:
|
||||
selection = _draw((_positive(), _positive(fixed=SECOND_FIXED)))
|
||||
|
||||
assert len(selection.positive_change_digests) == 2
|
||||
assert len(selection.pushes) == 20
|
||||
assert len(selection.assignments) == 40
|
||||
|
||||
|
||||
def test_every_accepted_fix_is_excluded_from_linear_same_network_draws() -> None:
|
||||
positives = (_positive(), _positive(fixed=SECOND_FIXED, parent=FIXED))
|
||||
history = _History(21, fixed_parents={FIXED: PARENT, SECOND_FIXED: FIXED})
|
||||
|
||||
selection = _draw(positives, history=history)
|
||||
|
||||
tips = {push.tip_revision for push in selection.pushes}
|
||||
pushes_by_digest = {push.push_digest: push for push in selection.pushes}
|
||||
assigned_tips = {pushes_by_digest[item.push_digest].tip_revision for item in selection.assignments}
|
||||
assert FIXED not in tips
|
||||
assert SECOND_FIXED not in tips
|
||||
assert not ({FIXED, SECOND_FIXED} & assigned_tips)
|
||||
assert {item.fixed_revision for item in selection.fix_exclusions} == {FIXED, SECOND_FIXED}
|
||||
assert len(selection.pushes) == 20
|
||||
assert len(selection.assignments) == 40
|
||||
source = ordinary.OrdinaryHistorySource(
|
||||
history,
|
||||
positives[0].candidate.canonical_repository,
|
||||
positives[0].candidate.network_identity_digest,
|
||||
SOURCE,
|
||||
)
|
||||
assert ordinary.validate_ordinary_selection(
|
||||
selection,
|
||||
positives,
|
||||
history_by_network={positives[0].candidate.network_identity_digest: source},
|
||||
expected_ecosystem_policy_digest=ECOSYSTEM_POLICY,
|
||||
expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY,
|
||||
)
|
||||
changed_fix = replace(
|
||||
selection.fix_exclusions[0],
|
||||
ground_truth_review_digest=content_digest({"review": "substituted"}),
|
||||
)
|
||||
forged = replace(selection, fix_exclusions=tuple(sorted((changed_fix, *selection.fix_exclusions[1:]))))
|
||||
with pytest.raises(ordinary.OrdinarySelectionError, match="provenance"):
|
||||
ordinary.ordinary_selection_bytes(forged)
|
||||
|
||||
|
||||
def test_reviewed_pre_freeze_contamination_is_excluded_with_reason() -> None:
|
||||
review = _contamination(PARENT, ordinary.ContaminationTiming.PRE_FREEZE)
|
||||
selection = _draw(history=_History(21), reviews=(review,))
|
||||
|
||||
assert len(selection.pushes) == 20
|
||||
assert selection.exclusions == (
|
||||
ordinary.OrdinaryExclusionV1(
|
||||
review.network_identity_digest,
|
||||
review.tip_revision,
|
||||
ordinary.OrdinaryExclusionReason.REVIEWED_SECURITY_DEFECT,
|
||||
review.review_digest,
|
||||
),
|
||||
)
|
||||
assert all(push.tip_revision != PARENT for push in selection.pushes)
|
||||
|
||||
|
||||
def test_post_freeze_contamination_cannot_rewrite_draw_and_emits_retirement() -> None:
|
||||
selection = _draw()
|
||||
review = _contamination(selection.pushes[0].tip_revision, ordinary.ContaminationTiming.POST_FREEZE)
|
||||
before = ordinary.ordinary_selection_bytes(selection)
|
||||
lifecycle = _active_lifecycle(content_digest({"population": 1}), content_digest({"freeze": 1}))
|
||||
|
||||
event = decode_lifecycle_event(ordinary.post_freeze_contamination_retirement(selection, review, lifecycle))
|
||||
|
||||
assert event.reason_code == "contaminated_negative"
|
||||
assert (event.population_digest, event.freeze_digest) == (lifecycle.population_digest, lifecycle.freeze_digest)
|
||||
assert ordinary.ordinary_selection_bytes(selection) == before
|
||||
with pytest.raises(ordinary.OrdinarySelectionError, match="post-freeze"):
|
||||
_draw(reviews=(review,))
|
||||
|
||||
|
||||
def test_assignment_ecosystem_substitution_is_refused() -> None:
|
||||
selection = _draw()
|
||||
forged_assignment = replace(selection.assignments[0], ecosystem=CanonicalEcosystem.JAVASCRIPT)
|
||||
forged = replace(selection, assignments=(forged_assignment, *selection.assignments[1:]))
|
||||
|
||||
with pytest.raises(ordinary.OrdinarySelectionError, match="crosses repository or ecosystem"):
|
||||
ordinary.ordinary_selection_bytes(forged)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("history,message", ((_History(20, unreadable_at=2), "unreadable"),))
|
||||
def test_unreadable_ordinary_change_refuses_clean_draw(history: _History, message: str) -> None:
|
||||
with pytest.raises(ordinary.OrdinarySelectionError, match=message):
|
||||
_draw(history=history)
|
||||
|
||||
|
||||
def test_empty_ordinary_change_refuses_clean_draw() -> None:
|
||||
class EmptyHistory(_History):
|
||||
def change_bytes(self, revision: str) -> bytes | None:
|
||||
return b"" if revision == PARENT else super().change_bytes(revision)
|
||||
|
||||
with pytest.raises(ordinary.OrdinarySelectionError, match="empty"):
|
||||
_draw(history=EmptyHistory(20))
|
||||
|
||||
|
||||
def test_source_policy_and_order_are_identity_bearing() -> None:
|
||||
selection = _draw()
|
||||
reversed_pushes = replace(selection, pushes=tuple(reversed(selection.pushes)))
|
||||
changed_policy = replace(selection, policy_digest=content_digest({"policy": "changed"}))
|
||||
|
||||
with pytest.raises(ordinary.OrdinarySelectionError, match="canonical"):
|
||||
ordinary.ordinary_selection_bytes(reversed_pushes)
|
||||
with pytest.raises(ordinary.OrdinarySelectionError, match="digest"):
|
||||
ordinary.ordinary_selection_bytes(changed_policy)
|
||||
|
||||
|
||||
def test_selection_must_reproduce_from_exact_history_and_review_inputs() -> None:
|
||||
positive = _positive()
|
||||
history = _History(21)
|
||||
source = ordinary.OrdinaryHistorySource(
|
||||
history, positive.candidate.canonical_repository, positive.candidate.network_identity_digest, SOURCE
|
||||
)
|
||||
selection = _draw((positive,), history=history)
|
||||
|
||||
assert ordinary.validate_ordinary_selection(
|
||||
selection,
|
||||
(positive,),
|
||||
history_by_network={positive.candidate.network_identity_digest: source},
|
||||
expected_ecosystem_policy_digest=ECOSYSTEM_POLICY,
|
||||
expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY,
|
||||
)
|
||||
changed_source = ordinary.OrdinaryHistorySource(
|
||||
history,
|
||||
positive.candidate.canonical_repository,
|
||||
positive.candidate.network_identity_digest,
|
||||
content_digest({"source": "changed"}),
|
||||
)
|
||||
with pytest.raises(ordinary.OrdinarySelectionError, match="reproduce"):
|
||||
ordinary.validate_ordinary_selection(
|
||||
selection,
|
||||
(positive,),
|
||||
history_by_network={positive.candidate.network_identity_digest: changed_source},
|
||||
expected_ecosystem_policy_digest=ECOSYSTEM_POLICY,
|
||||
expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY,
|
||||
)
|
||||
|
||||
|
||||
def test_unrelated_contamination_review_cannot_disappear_from_selection() -> None:
|
||||
review = _contamination("e" * 40, ordinary.ContaminationTiming.PRE_FREEZE)
|
||||
|
||||
with pytest.raises(ordinary.OrdinarySelectionError, match="does not identify"):
|
||||
_draw(reviews=(review,))
|
||||
|
||||
|
||||
def test_history_source_cannot_cross_repository_identity() -> None:
|
||||
positive = _positive()
|
||||
source = ordinary.OrdinaryHistorySource(
|
||||
_History(20),
|
||||
"https://github.com/other/application",
|
||||
positive.candidate.network_identity_digest,
|
||||
SOURCE,
|
||||
)
|
||||
|
||||
with pytest.raises(ordinary.OrdinarySelectionError, match="crosses repository"):
|
||||
ordinary.draw_ordinary_selection(
|
||||
(positive,),
|
||||
history_by_network={positive.candidate.network_identity_digest: source},
|
||||
policy_digest=DRAW_POLICY,
|
||||
expected_ecosystem_policy_digest=ECOSYSTEM_POLICY,
|
||||
expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY,
|
||||
)
|
||||
|
||||
|
||||
def test_contamination_requires_independent_disclosed_exact_review() -> None:
|
||||
with pytest.raises(ordinary.OrdinarySelectionError, match="independent"):
|
||||
ordinary.reviewed_contamination(
|
||||
network_identity_digest=_positive().candidate.network_identity_digest,
|
||||
tip_revision=PARENT,
|
||||
timing=ordinary.ContaminationTiming.PRE_FREEZE,
|
||||
evidence_digest=content_digest({"contamination": PARENT}),
|
||||
producer_identity="[email protected]",
|
||||
reviewer_identity="[email protected]",
|
||||
conflict_disclosed=True,
|
||||
policy_digest=CONTAMINATION_POLICY,
|
||||
)
|
||||
|
||||
review = _contamination(PARENT, ordinary.ContaminationTiming.PRE_FREEZE)
|
||||
with pytest.raises(ordinary.OrdinarySelectionError, match="digest"):
|
||||
ordinary.contamination_review_bytes(replace(review, evidence_digest=content_digest({"changed": 1})))
|
||||
Reference in New Issue
Block a user