"""Promotion gate for copyright-safe book rule packets. Admission to ``runtime_packet_ids`` is a provenance-reference status only. A ``validated_reference`` packet may become runtime-visible only when ALL of the following are true: * ``validation.status`` is one of the validated receipts (``fixture_passed`` / ``engine_cross_checked`` / ``case_calibrated``); * ``validation.validation_kind`` is a formula-grade kind (``fixture_numeric`` / ``engine_cross_checked`` / ``case_calibrated``) -- a status alone is never trusted; * ``validation.validated_assertions`` is a non-empty list of concrete assertions the fixture actually checks; * ``validation.test_path`` matches the expected ``tests/...py[::node]`` format AND the referenced test file exists in the main repo. Packets that only wire source paths, boundary text, or unrelated legacy tests must stay ``candidate_mapped`` / ``reference_only`` with ``validation.status: not_validated``; they never enter ``runtime_packet_ids``. """ from __future__ import annotations import re from pathlib import Path from typing import Any VALIDATION_RECEIPTS = { "fixture_passed", "engine_cross_checked", "case_calibrated", } # Formula-grade validation kinds: real numeric/formula assertions with a # verifiable receipt. Wiring/boundary-text checks are not validation. VALIDATED_KINDS = { "fixture_numeric", "engine_cross_checked", "case_calibrated", } TEST_PATH_PATTERN = re.compile(r"^tests/[A-Za-z0-9_./\-]+\.py(::[A-Za-z0-9_]+){0,2}$") BOUNDARY = "Admission state never implies Technique Audit execution." ROOT = Path(__file__).resolve().parents[1] def _test_path_reason(test_path: Any) -> str | None: """Return a failure reason when the test path is not a verifiable receipt.""" if not isinstance(test_path, str) or not test_path.strip(): return "validated_reference_requires_test_path" if not TEST_PATH_PATTERN.match(test_path.strip()): return "invalid_test_path_format" file_part = test_path.strip().split("::", 1)[0] if not (ROOT / file_part).is_file(): return f"test_file_missing:{file_part}" return None def _packet_validation_failure(packet: dict[str, Any]) -> str | None: """Return the reason a validated_reference packet fails the gate, if any.""" validation = packet.get("validation") if not isinstance(validation, dict): return "validated_reference_requires_validation_block" status = validation.get("status") if status not in VALIDATION_RECEIPTS: return "validated_reference_requires_validation_receipt" kind = validation.get("validation_kind") if kind not in VALIDATED_KINDS: return ( "validated_reference_requires_formula_grade_validation_kind" + (f":{kind}" if kind is not None else "") ) assertions = validation.get("validated_assertions") if not isinstance(assertions, list) or not assertions: return "validated_reference_requires_non_empty_validated_assertions" test_path_reason = _test_path_reason(validation.get("test_path")) if test_path_reason is not None: return test_path_reason return None def build_report(cards_document: dict[str, Any], packets_document: dict[str, Any]) -> dict[str, Any]: """Return the packets permitted as provenance references, never execution records.""" del cards_document # Cards are supplied for the shared report contract, not promotion. runtime_packet_ids: list[str] = [] blocked_packet_ids: list[str] = [] failures: list[dict[str, str]] = [] for packet in packets_document.get("packets", []): rule_id = packet.get("rule_id", "") admission_state = packet.get("admission_state") if admission_state == "blocked": blocked_packet_ids.append(rule_id) continue if admission_state != "validated_reference": continue failure_reason = _packet_validation_failure(packet) if failure_reason is None: runtime_packet_ids.append(rule_id) else: failures.append( { "id": "invalid_validated_packet", "rule_id": rule_id, "reason": failure_reason, } ) return { "runtime_packet_ids": sorted(runtime_packet_ids), "blocked_packet_ids": sorted(blocked_packet_ids), "failures": sorted(failures, key=lambda failure: failure["rule_id"]), "boundary": BOUNDARY, }