From a7adc99d318afd47fa4f3cc3051c7519ed90d550 Mon Sep 17 00:00:00 2001 From: Marius Ciepluch <11855163+norandom@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:41:42 +0100 Subject: [PATCH] feat(valid-detection-benchmark): 9.1 route catalog, verification, and assembly through new authorities --- .../specs/valid-detection-benchmark/tasks.md | 2 +- benchmarks/valid/assemble.py | 46 +- benchmarks/valid/catalog.py | 34 ++ benchmarks/valid/verify.py | 73 +++ ...test_valid_benchmark_legacy_integration.py | 521 ++++++++++++++++++ 5 files changed, 674 insertions(+), 2 deletions(-) create mode 100644 tests/test_valid_benchmark_legacy_integration.py diff --git a/.kiro/specs/valid-detection-benchmark/tasks.md b/.kiro/specs/valid-detection-benchmark/tasks.md index 5c4a2ad1..bf6c6a91 100644 --- a/.kiro/specs/valid-detection-benchmark/tasks.md +++ b/.kiro/specs/valid-detection-benchmark/tasks.md @@ -280,7 +280,7 @@ - _Depends: 2.3, 7.2, 8.1, 8.2_ - [ ] 9. Integrate legacy benchmark seams and the operator entry point -- [ ] 9.1 Route catalog, verification, and assembly through the new authorities +- [x] 9.1 Route catalog, verification, and assembly through the new authorities - Adapt existing fix-site cataloging and optional model-assistance records to the review authority without treating prior model or spot-check fields as accepted labels. - Make legacy assembly a development-only facade that delegates to reservation, review, population, and artifact authorities and cannot emit an admission-class legacy manifest. - Preserve historical readers and bytes while refusing old pilot identities as replacement inputs. diff --git a/benchmarks/valid/assemble.py b/benchmarks/valid/assemble.py index d8daefa8..8987cfb4 100644 --- a/benchmarks/valid/assemble.py +++ b/benchmarks/valid/assemble.py @@ -1,4 +1,8 @@ -"""Freeze verified positives and deterministic ordinary draws with a held-out guard.""" +"""Development-only legacy assembly of verified positives and deterministic ordinary draws. + +Replacement admission freezes delegate to the population authority through +:func:`replacement_freeze`; this facade never emits an admission-class manifest +of its own, and its freeze records carry a development-only admission class.""" from __future__ import annotations @@ -15,8 +19,11 @@ from benchmarks.unseen.eligibility import normalize from benchmarks.unseen.journal import digest from .catalog import local_git +from .population_freeze import PopulationFreezeV3, freeze_population from .verify import verdict +LEGACY_FREEZE_ADMISSION_CLASS = "legacy_development_only" + def negatives( rows: list[dict], fixes: set[str], functions: set[tuple[str, str]], *, count: int, seed: int, floor: int = extract.ORDINARY_FLOOR @@ -163,6 +170,7 @@ def freeze( ordinary_floor=floor, history_limit=history_limit, label_check_counts={"confirmed": sum(len(v) for v in retained.values()), "unchecked": 0, "unclear": 0, "refactor": 0}, + admission_class=LEGACY_FREEZE_ADMISSION_CLASS, ) payloads = [ (directory / "pool-valid.toml", text.encode()), @@ -178,6 +186,42 @@ def freeze( return record +def replacement_freeze( + positives, + *, + ordinary_selection, + reservation_census, + reservation_authority, + scope_minimums=(), + exclusion_counts=(), + source_provenance_digest, + freeze_policy_digest, + expected_ecosystem_policy_digest, + expected_ground_truth_policy_digest, + forbidden_historical_identities=(), +) -> PopulationFreezeV3: + """Delegate replacement freezing to the population authority. + + The legacy facade holds no freezing logic of its own: reviewed positives, the + ordinary selection, the reservation census, and the artifact contract stay with + their authorities, so an admission-class artifact can never be produced from + legacy assembly output. + """ + return freeze_population( + positives, + ordinary_selection=ordinary_selection, + reservation_census=reservation_census, + reservation_authority=reservation_authority, + scope_minimums=scope_minimums, + exclusion_counts=exclusion_counts, + source_provenance_digest=source_provenance_digest, + freeze_policy_digest=freeze_policy_digest, + expected_ecosystem_policy_digest=expected_ecosystem_policy_digest, + expected_ground_truth_policy_digest=expected_ground_truth_policy_digest, + forbidden_historical_identities=forbidden_historical_identities, + ) + + def main(argv=None): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("catalog", type=Path) diff --git a/benchmarks/valid/catalog.py b/benchmarks/valid/catalog.py index 5fb9ca57..757637d3 100644 --- a/benchmarks/valid/catalog.py +++ b/benchmarks/valid/catalog.py @@ -30,6 +30,8 @@ from benchmarks.valid.fix_site_catalog import ( build_fix_site_candidate, fix_site_candidate_bytes, ) +from benchmarks.valid.reservation import RepositoryIdentityResolver +from openultrasast.core.canonical import bytes_digest, content_digest LANGUAGES = { ".py": "python", @@ -150,6 +152,37 @@ def write_catalog(path: Path, cases: list[dict[str, Any]]) -> None: path.write_text(text, encoding="utf-8") +def candidate_request( + case: Mapping[str, Any], + *, + resolver: RepositoryIdentityResolver, + package_evidence: PackageEcosystemEvidence, + license_evidence: LicenseEvidence, +) -> FixSiteRequest: + """Route one legacy fix-site case into the review authority's candidate request. + + Repository identity comes from the reservation authority's reviewed network + registry, the advisory remains the source evidence with its own content digest, + and package plus license evidence must be supplied from reviewed repository + material because the legacy catalog never recorded them. + """ + advisory_text = str(case.get("advisory_text", "")) + if not advisory_text.strip(): + raise ValueError("advisory text is required for source evidence") + return FixSiteRequest( + repository=resolver.resolve(str(case["url"])), + vulnerable_parent=str(case["parent"]), + fixed_revision=str(case["commit"]), + path=str(case["path"]), + function=str(case["function"]), + observed_language=str(case["language"]), + package_evidence=package_evidence, + license_evidence=license_evidence, + source_evidence=SourceEvidence("security_advisory", str(case["advisory"]), bytes_digest(advisory_text.encode("utf-8"))), + change_inputs_digest=content_digest({"parent": case["parent"], "fixed": case["commit"]}), + ) + + def main(argv: Sequence[str] | None = None) -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("sources", type=Path, help="JSON array of resolved Candidate dictionaries with CWE and advisory_text") @@ -182,6 +215,7 @@ __all__ = [ "SourceEvidence", "build_cases", "build_fix_site_candidate", + "candidate_request", "fix_site_candidate_bytes", "local_git", "main", diff --git a/benchmarks/valid/verify.py b/benchmarks/valid/verify.py index 0730f966..8c03bcdb 100644 --- a/benchmarks/valid/verify.py +++ b/benchmarks/valid/verify.py @@ -6,15 +6,30 @@ import argparse import json import math import tomllib +from collections.abc import Mapping from datetime import date from pathlib import Path +from openultrasast.core.canonical import content_digest from openultrasast.model.endpoint import DEFAULT_DETECTOR_MODEL, DeepSeekChatClient, resolve_chat_endpoint from openultrasast.ruleset.frameworks import load_frameworks from .catalog import local_git, read_functions +from .ground_truth_review import GroundTruthAttestationV1 CHECKS = ("vulnerable_pre_fix", "fix_addresses_it", "reachable_in_repo") +MODEL_ASSISTANCE_SCHEMA = "openultrasast.valid-detection-benchmark.model-assistance.v1" +_HUMAN_ATTESTATION_FIELDS = ( + "producer_identity", + "reviewer_identity", + "conflict_disclosed", + "policy_digest", + "policy_satisfied", + "defect_present", + "fix_addresses", + "in_repository_reachable", + "verified_scopes", +) def verdict(record: dict) -> str: @@ -95,6 +110,64 @@ def spot_check(record: dict, decision: str, reviewer: str, checked_date: str) -> return updated +def model_assistance(record: Mapping[str, object]) -> dict[str, object]: + """Record the model's checks as separately identified non-authoritative assistance.""" + return { + "schema_version": MODEL_ASSISTANCE_SCHEMA, + "case_id": record.get("id"), + "assistance_kind": "model_review_assistance", + "authoritative": False, + "model": record.get("model"), + "checks": {key: record.get(key) for key in (*CHECKS, "advisory_cited")}, + "entry_point": record.get("entry_point"), + "confidence": record.get("confidence"), + } + + +def model_assistance_digest(record: Mapping[str, object]) -> str: + """Bind model assistance to its own content identity, never to a label.""" + return content_digest(model_assistance(record)) + + +def review_attestation( + record: Mapping[str, object], + *, + candidate_digest: str, + ecosystem_review_digest: str, + human_judgment: Mapping[str, object], +) -> GroundTruthAttestationV1: + """Adapt one legacy verification record to the review authority. + + The record's model checks, confidence, and any legacy ``human_spot_check`` fields are + routed only as separately identified non-authoritative assistance; they can never + satisfy the required human judgment. Every authoritative judgment, reviewer identity, + disclosure, and policy value must arrive through the externally governed + ``human_judgment`` mapping, which must carry exactly the review-authority + attestation fields; the review authority itself then validates their semantics. + """ + if not isinstance(human_judgment, Mapping): + raise ValueError("human judgment is required; a legacy verification record cannot supply it") + fields = set(human_judgment) + if fields != set(_HUMAN_ATTESTATION_FIELDS): + missing = sorted(set(_HUMAN_ATTESTATION_FIELDS) - fields) + unknown = sorted(fields - set(_HUMAN_ATTESTATION_FIELDS)) + raise ValueError(f"human judgment must carry exactly the attestation fields: missing={missing}, unknown={unknown}") + return GroundTruthAttestationV1( + candidate_digest=candidate_digest, + ecosystem_review_digest=ecosystem_review_digest, + producer_identity=str(human_judgment["producer_identity"]), + reviewer_identity=str(human_judgment["reviewer_identity"]), + conflict_disclosed=human_judgment["conflict_disclosed"], + policy_digest=str(human_judgment["policy_digest"]), + policy_satisfied=human_judgment["policy_satisfied"], + defect_present=human_judgment["defect_present"], + fix_addresses=human_judgment["fix_addresses"], + in_repository_reachable=human_judgment["in_repository_reachable"], + verified_scopes=tuple(human_judgment["verified_scopes"]), + model_assistance_digest=model_assistance_digest(record), + ) + + def write_records(path: Path, records: list[dict]) -> None: if len({row["id"] for row in records}) != len(records): raise ValueError("duplicate verification id") diff --git a/tests/test_valid_benchmark_legacy_integration.py b/tests/test_valid_benchmark_legacy_integration.py new file mode 100644 index 00000000..0ae2da63 --- /dev/null +++ b/tests/test_valid_benchmark_legacy_integration.py @@ -0,0 +1,521 @@ +"""Legacy catalog, verification, and assembly routed through the new authorities (task 9.1).""" + +from __future__ import annotations + +import hashlib +import json +import os +import subprocess +import sys +import tomllib +from dataclasses import replace +from pathlib import Path + +import pytest + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +from benchmarks.unseen import extract, replay # noqa: E402 +from benchmarks.valid import assemble, catalog, verify # 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, + LicenseEvidence, + PackageEcosystemEvidence, + build_fix_site_candidate, +) +from benchmarks.valid.ground_truth_review import ( # noqa: E402 + GroundTruthExclusionReason, + GroundTruthStatus, + VerifiedScope, + review_ground_truth, +) +from benchmarks.valid.historical_authorization import ( # noqa: E402 + HISTORICAL_REVIEW_SCHEMA, + PINNED_HISTORICAL_DIGESTS, + HistoricalAuthorizationError, + accept_reviewed_withdrawal, + authorize_historical_path, + build_withdrawal_candidate, + pinned_historical_inventory, + require_current_qualification, +) +from benchmarks.valid.ordinary_change_contract import ( # noqa: E402 + OrdinaryDrawAssignmentV1, + OrdinaryFixExclusionV1, + OrdinaryPushV1, + OrdinarySelectionV1, + ReviewedPositiveInput, + _fix_exclusion_value, + _push_value, + _selection_value, +) +from benchmarks.valid.population_freeze import ( # noqa: E402 + PopulationClass, + PopulationClassificationReason, + PopulationFreezeError, + population_freeze_bytes, +) +from benchmarks.valid.population_freeze_contract import accept_population_freeze # noqa: E402 +from benchmarks.valid.reservation import ( # noqa: E402 + RepositoryIdentityResolver, + ReservationAuthority, + legacy_reservation_source, +) +from benchmarks.valid.reservation_identity import build_network_identity # noqa: E402 +from openultrasast.core.canonical import bytes_digest, canonical_bytes, content_digest # noqa: E402 +from openultrasast.tool_hunter import ChatResponse # noqa: E402 + +ROOT = Path(__file__).resolve().parents[1] +PILOT_POOL = ROOT / "benchmarks/valid/pilot/pool-valid.toml" +PILOT_FREEZE = ROOT / "benchmarks/valid/pilot/freeze-valid.json" +TRAIN_CATALOG = ROOT / "benchmarks/valid/train/catalog.toml" +ECOSYSTEM_POLICY = content_digest({"policy": "ecosystem-review-fixture"}) +GROUND_TRUTH_POLICY = content_digest({"policy": "ground-truth-review-fixture"}) +FREEZE_POLICY = content_digest({"policy": "population-freeze-fixture"}) +DRAW_POLICY = content_digest({"policy": "ordinary-first-parent-fixture", "required": 20}) +PROVENANCE = content_digest({"source": "legacy-integration-fixture"}) +PACKAGE_EVIDENCE = PackageEcosystemEvidence("python", EvidenceRef("requirements.txt", content_digest({"manifest": 1}), 27)) +LICENSE_EVIDENCE = LicenseEvidence("MIT", EvidenceRef("LICENSE", content_digest({"license": 1}), 21)) + + +@pytest.fixture(autouse=True) +def offline(monkeypatch): + def forbidden(*args, **kwargs): + pytest.fail("network or SZZ was invoked") + + import socket + + monkeypatch.setattr(socket, "create_connection", forbidden) + monkeypatch.setattr(socket.socket, "connect", forbidden) + monkeypatch.setattr(extract, "introducing", forbidden) + monkeypatch.setattr(extract.Git, "blame", forbidden) + for var in ("GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES"): + monkeypatch.delenv(var, raising=False) + + +@pytest.fixture +def pair(tmp_path): + commit_count = 0 + + def runner(argv, **kwargs): + nonlocal commit_count + assert not {"fetch", "clone", "pull", "push", "blame"} & set(argv) + assert not any(arg.startswith("-L") for arg in argv) + if "commit" in argv: + timestamp = f"@{1600000000 + commit_count * 86400} +0000" + kwargs["env"] = {**os.environ, "GIT_AUTHOR_DATE": timestamp, "GIT_COMMITTER_DATE": timestamp} + commit_count += 1 + return subprocess.run(argv, **kwargs) + + git = extract.Git(tmp_path, runner=runner) + git.run("init", "-q") + git.run("config", "user.name", "Fixture") + git.run("config", "user.email", "fixture@example.invalid") + pre = 'from flask import request\n@app.route("/run")\ndef handler():\n return eval(request.args["code"])\n' + post = pre.replace('eval(request.args["code"])', 'int(request.args["code"])') + commits = [] + for text in (pre, post): + (tmp_path / "app.py").write_text(text) + git.run("add", "app.py") + git.run("commit", "-qm", "fixture") + commits.append(git.run("rev-parse", "HEAD").strip()) + source = dict( + repository="fixture/app", + url="https://github.com/fixture/app", + parent=commits[0], + fix=commits[1], + advisory="GHSA-fixture", + cwe="CWE-94", + family="injection", + advisory_text="Request code is evaluated.", + ) + (case,) = catalog.build_cases(git, source) + return git, case + + +class Stub: + def __init__(self, **changes): + self.calls = [] + self.answer = dict( + vulnerable_pre_fix=True, + fix_addresses_it=True, + reachable_in_repo=True, + entry_point="Flask route /run using request.args", + confidence=0.86, + advisory_cited=True, + **changes, + ) + + def complete(self, **kwargs): + self.calls.append(kwargs) + return ChatResponse(content=json.dumps(self.answer)) + + +def ordinary_rows(n=90): + return [ + dict( + head=f"{i + 10:040x}", + parents=[f"{i + 9:040x}"], + timestamp=i, + author="Fixture", + message="ordinary edit", + files=[dict(path="other.py", added=1, removed=1, head_lines=[[2, 2]], functions=["other"], generated=False)], + ) + for i in range(n) + ] + + +def accepted(case, **changes): + record = dict( + id=case["id"], + vulnerable_pre_fix=True, + fix_addresses_it=True, + reachable_in_repo=True, + entry_point="Flask route /run", + confidence=0.86, + model="stub", + advisory_cited=True, + human_spot_check=dict(decision="accept", reviewer="AB", date="2026-10-12"), + verdict="accepted", + ) + record.update(changes) + return record + + +def human_judgment(**changes): + values = { + "producer_identity": "ground-truth-producer@example.invalid", + "reviewer_identity": "ground-truth-reviewer@example.invalid", + "conflict_disclosed": True, + "policy_digest": GROUND_TRUTH_POLICY, + "policy_satisfied": True, + "defect_present": True, + "fix_addresses": True, + "in_repository_reachable": True, + "verified_scopes": (VerifiedScope("injection", "python"),), + } + values.update(changes) + return values + + +def _resolver(*networks): + return RepositoryIdentityResolver(networks or (build_network_identity("https://github.com/fixture/app"),)) + + +def _candidate(git, case, resolver): + request = catalog.candidate_request(case, resolver=resolver, package_evidence=PACKAGE_EVIDENCE, license_evidence=LICENSE_EVIDENCE) + return build_fix_site_candidate(git, request) + + +def _ecosystem(candidate): + attestation = EcosystemReviewAttestationV1( + candidate.candidate_digest, + candidate.source_evidence.evidence_digest, + "ecosystem-producer@example.invalid", + "ecosystem-reviewer@example.invalid", + True, + ECOSYSTEM_POLICY, + True, + EcosystemReviewDecision.PYTHON, + ) + return review_repository_ecosystem(candidate, attestation, expected_policy_digest=ECOSYSTEM_POLICY) + + +def _reviewed_positive(git, case, record, *, resolver, human=None): + candidate = _candidate(git, case, resolver) + ecosystem = _ecosystem(candidate) + attestation = verify.review_attestation( + record, + candidate_digest=candidate.candidate_digest, + ecosystem_review_digest=ecosystem.review_digest, + human_judgment=human or human_judgment(), + ) + review = review_ground_truth( + candidate, + ecosystem, + attestation, + expected_ecosystem_policy_digest=ECOSYSTEM_POLICY, + expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY, + ) + return ReviewedPositiveInput(candidate, ecosystem, review) + + +def _selection(positive): + network = positive.candidate.network_identity_digest + repository = positive.candidate.canonical_repository + pushes = [] + for index in range(20): + draft = OrdinaryPushV1( + repository, + network, + CanonicalEcosystem.PYTHON, + f"{9000 + index * 2:040x}", + f"{9001 + index * 2:040x}", + 8, + content_digest({"change": index}), + content_digest({"history": network}), + "", + ) + pushes.append(replace(draft, push_digest=content_digest(_push_value(draft, include_digest=False)))) + change = positive.ground_truth_review.change_identity_digest + fix_exclusion = OrdinaryFixExclusionV1( + network, + positive.candidate.fixed.revision, + change, + positive.ground_truth_review.review_digest, + ) + assignments = tuple(OrdinaryDrawAssignmentV1(change, push.network_identity_digest, push.ecosystem, push.push_digest) for push in pushes) + provenance = content_digest( + { + "history_sources": ((repository, network, pushes[0].history_source_digest),), + "accepted_fix_exclusions": tuple(_fix_exclusion_value(item) for item in (fix_exclusion,)), + "policy_digest": DRAW_POLICY, + } + ) + selection = OrdinarySelectionV1( + 20, + DRAW_POLICY, + (change,), + (fix_exclusion,), + tuple(sorted(pushes, key=lambda item: item.push_digest)), + tuple(sorted(assignments)), + (), + provenance, + "", + ) + return replace(selection, selection_digest=content_digest(_selection_value(selection, include_digest=False))) + + +def _replacement_arguments(positive, *, authority, census): + return dict( + ordinary_selection=_selection(positive), + reservation_census=census, + reservation_authority=authority, + source_provenance_digest=PROVENANCE, + freeze_policy_digest=FREEZE_POLICY, + expected_ecosystem_policy_digest=ECOSYSTEM_POLICY, + expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY, + ) + + +def test_model_assistance_is_separate_and_never_an_accepted_label(pair): + git, case = pair + record = verify.spot_check(verify.verify_case(git, case, client=Stub()), "accept", "AB", "2026-10-12") + assert record["verdict"] == "accepted" + + assistance = verify.model_assistance(record) + assert assistance["authoritative"] is False + assert set(assistance["checks"]) == {*verify.CHECKS, "advisory_cited"} + assert "human_spot_check" not in assistance and "verdict" not in assistance + + candidate = _candidate(git, case, _resolver()) + ecosystem = _ecosystem(candidate) + attestation = verify.review_attestation( + record, + candidate_digest=candidate.candidate_digest, + ecosystem_review_digest=ecosystem.review_digest, + human_judgment=human_judgment(defect_present=None, fix_addresses=None, in_repository_reachable=None), + ) + assert attestation.model_assistance_digest == verify.model_assistance_digest(record) + review = review_ground_truth( + candidate, + ecosystem, + attestation, + expected_ecosystem_policy_digest=ECOSYSTEM_POLICY, + expected_ground_truth_policy_digest=GROUND_TRUTH_POLICY, + ) + assert review.status is GroundTruthStatus.EXCLUDED + assert review.exclusion_reason is GroundTruthExclusionReason.MISSING_HUMAN_JUDGMENT + assert review.model_assistance_digest == verify.model_assistance_digest(record) + + +def test_legacy_record_and_spot_check_shapes_cannot_supply_the_human_judgment(pair): + git, case = pair + record = verify.spot_check(verify.verify_case(git, case, client=Stub()), "accept", "AB", "2026-10-12") + candidate = _candidate(git, case, _resolver()) + ecosystem = _ecosystem(candidate) + keywords = dict(candidate_digest=candidate.candidate_digest, ecosystem_review_digest=ecosystem.review_digest) + + for legacy_shape in (None, record, record["human_spot_check"], accepted(case) | {"decision": "accept"}): + with pytest.raises(ValueError, match="human judgment"): + verify.review_attestation(record, human_judgment=legacy_shape, **keywords) + + +def test_only_the_human_judgment_decides_acceptance(pair): + git, case = pair + disagreeing = Stub() + for key in verify.CHECKS: + disagreeing.answer[key] = False + record = verify.spot_check(verify.verify_case(git, case, client=disagreeing), "reject", "AB", "2026-10-12") + assert record["verdict"] == "rejected" + + positive = _reviewed_positive(git, case, record, resolver=_resolver()) + assert positive.ground_truth_review.status is GroundTruthStatus.ACCEPTED + assert positive.ground_truth_review.verified_scopes == (VerifiedScope("injection", "python"),) + + rejected = _reviewed_positive( + git, + case, + record, + resolver=_resolver(), + human=human_judgment(defect_present=False), + ) + assert rejected.ground_truth_review.status is GroundTruthStatus.EXCLUDED + assert rejected.ground_truth_review.exclusion_reason is GroundTruthExclusionReason.JUDGMENT_REJECTED + + self_reviewed = _reviewed_positive( + git, + case, + record, + resolver=_resolver(), + human=human_judgment(reviewer_identity="ground-truth-producer@example.invalid"), + ) + assert self_reviewed.ground_truth_review.status is GroundTruthStatus.EXCLUDED + assert self_reviewed.ground_truth_review.exclusion_reason is GroundTruthExclusionReason.SELF_REVIEW + + +def test_catalog_routes_identity_language_license_and_source_evidence(pair): + git, case = pair + resolver = _resolver(build_network_identity("https://github.com/fixture/app", aliases=("https://github.com/fixture-mirror/app",))) + + request = catalog.candidate_request(case, resolver=resolver, package_evidence=PACKAGE_EVIDENCE, license_evidence=LICENSE_EVIDENCE) + assert request.repository.canonical_repository == "https://github.com/fixture/app" + candidate = build_fix_site_candidate(git, request) + + identity = resolver.resolve(case["url"]) + assert candidate.canonical_repository == identity.canonical_repository + assert candidate.network_identity_digest == identity.network_identity_digest + assert resolver.resolve("https://github.com/FIXTURE-MIRROR/app.git").network_identity_digest == identity.network_identity_digest + assert candidate.observed_language == case["language"] == "python" + assert candidate.license_evidence.expression == "MIT" + assert candidate.package_evidence.ecosystem_hint == "python" + assert candidate.source_evidence.source_kind == "security_advisory" + assert candidate.source_evidence.source_locator == case["advisory"] + assert candidate.source_evidence.evidence_digest == bytes_digest(case["advisory_text"].encode("utf-8")) + assert candidate.vulnerable.content_digest != candidate.fixed.content_digest + assert "eval(" in candidate.vulnerable.content and "int(" in candidate.fixed.content + + +def test_legacy_assembly_is_development_only_and_refused_as_admission_root(pair, tmp_path, monkeypatch): + git, case = pair + monkeypatch.setattr(extract, "ordinary_history", lambda *args: ordinary_rows()) + record = assemble.freeze( + tmp_path / "pool", + cases=[case], + verifications=[accepted(case)], + repos={case["repo"]: git}, + used=set(), + seed=7, + ) + + assert assemble.LEGACY_FREEZE_ADMISSION_CLASS == "legacy_development_only" + assert record["admission_class"] == assemble.LEGACY_FREEZE_ADMISSION_CLASS + assert record["admission_class"] not in {item.value for item in PopulationClass} + # The exact validator the V3 admission preflight freeze gate calls refuses the legacy record. + with pytest.raises((AttributeError, TypeError, PopulationFreezeError)): + population_freeze_bytes(record) + with pytest.raises((AttributeError, TypeError, PopulationFreezeError)): + population_freeze_bytes(json.loads((tmp_path / "pool/freeze-valid.json").read_text())) + # The development workflow itself stays usable: the legacy pool still replays. + loaded = replay.load_slice(tmp_path / "pool/pool-valid.toml", 1) + assert loaded and loaded[0]["case_id"] == case["id"] + + +def test_replacement_freeze_delegates_to_the_population_and_artifact_authorities(pair): + git, case = pair + record = verify.verify_case(git, case, client=Stub()) + positive = _reviewed_positive(git, case, record, resolver=_resolver()) + authority = ReservationAuthority(_resolver()) + census = authority.current_census() + + freeze = assemble.replacement_freeze((positive,), **_replacement_arguments(positive, authority=authority, census=census)) + + assert freeze.positive_count == 1 + assert freeze.population_class is PopulationClass.INSUFFICIENT + assert PopulationClassificationReason.BELOW_MINIMUM_POSITIVES in freeze.classification_reasons + assert freeze.population_class is not PopulationClass.ADMISSION + assert freeze.reservation_census_digest == census.census_digest + content = accept_population_freeze(freeze) + assert content == population_freeze_bytes(freeze) and len(content) > 0 + # New identities must never be historical pilot identities (requirement 4.9). + with pytest.raises(PopulationFreezeError, match="historical identity"): + assemble.replacement_freeze( + (positive,), + forbidden_historical_identities=(freeze.population_digest, freeze.freeze_digest), + **_replacement_arguments(positive, authority=authority, census=census), + ) + + +def test_withdrawn_pilot_identities_are_refused_as_replacement_inputs(pair): + git, case = pair + pilot_repositories = sorted(row["repository"] for row in tomllib.loads(PILOT_POOL.read_text())["repository"]) + assert len(pilot_repositories) == 12 + pilot = pilot_repositories[0] + owner, name = pilot.split("/") + pilot_url = f"https://github.com/{pilot}" + network = build_network_identity(pilot_url, aliases=(f"https://github.com/{owner}-mirror/{name}",)) + resolver = _resolver(network) + legacy = legacy_reservation_source(resolver, repository=pilot_url, source_digest=content_digest({"pilot": pilot})) + authority = ReservationAuthority(resolver, legacy_sources=(legacy,)) + census = authority.current_census() + + assert authority.is_reserved(pilot_url, census) + assert authority.is_reserved(f"https://github.com/{owner}-mirror/{name}", census) + assert not authority.is_reserved("https://github.com/fixture/app", census) + + pilot_source = dict( + repository=pilot, + url=pilot_url, + parent=case["parent"], + fix=case["commit"], + advisory="GHSA-pilot", + cwe="CWE-94", + family="injection", + advisory_text="Historical pilot defect.", + ) + (pilot_case,) = catalog.build_cases(git, pilot_source) + record = verify.verify_case(git, pilot_case, client=Stub()) + positive = _reviewed_positive(git, pilot_case, record, resolver=resolver) + + with pytest.raises(PopulationFreezeError, match="permanently reserved"): + assemble.replacement_freeze((positive,), **_replacement_arguments(positive, authority=authority, census=census)) + + # The withdrawn pilot itself stays auditable exactly, but never as current qualification. + inventory = pinned_historical_inventory(ROOT) + withdrawal_candidate = build_withdrawal_candidate(inventory) + review = { + "schema_version": HISTORICAL_REVIEW_SCHEMA, + "subject_digest": withdrawal_candidate.candidate_digest, + "decision": "authorize_exact_historical_audit", + "producer_identity": "custodian", + "reviewer_identity": "historical-reviewer", + "conflict_disclosed": True, + "policy_digest": content_digest({"policy": "historical-review-fixture"}), + } + review["review_digest"] = content_digest(review) + withdrawal = accept_reviewed_withdrawal(withdrawal_candidate, canonical_bytes(review)) + with pytest.raises(HistoricalAuthorizationError, match="non-admissible"): + require_current_qualification(withdrawal) + pool_digest = PINNED_HISTORICAL_DIGESTS["benchmarks/valid/pilot/pool-valid.toml"] + audited = authorize_historical_path(ROOT, "benchmarks/valid/pilot/pool-valid.toml", pool_digest, withdrawal) + assert hashlib.sha256(audited).hexdigest() == pool_digest + + +def test_historical_pilot_and_train_readers_remain_byte_identical_and_usable(): + for relative in ("benchmarks/valid/pilot/pool-valid.toml", "benchmarks/valid/pilot/freeze-valid.json"): + content = (ROOT / relative).read_bytes() + assert content and hashlib.sha256(content).hexdigest() == PINNED_HISTORICAL_DIGESTS[relative] + + rows = replay.load_slice(PILOT_POOL, 1) + assert rows and sum(1 for row in rows if row["kind"] == "vulnerability") >= 1 + freeze = json.loads(PILOT_FREEZE.read_text()) + assert freeze["freeze_digest"] and freeze["negative_ratio"] == 20 + + train_cases = tomllib.loads(TRAIN_CATALOG.read_text())["case"] + assert train_cases and all(row["language"] and row["cwe"] and row["repo"] for row in train_cases[:50])