#!/usr/bin/env python3 """Package-time verification_tip → actual delivery HEAD closure (no self-hash loop). Tracked commits must not embed their own final commit hash. This helper is intended to run at package time against a clean exact HEAD and emit package-generated metadata (ZIP meta/), not a pre-committed self-referential receipt. """ from __future__ import annotations import argparse import json import subprocess import sys from datetime import datetime, timezone from pathlib import Path from typing import Any ROOT = Path(__file__).resolve().parents[1] # Round-9 strict tip→delivery policy: # After the clean verification tip + unified matrix, ONLY immutable run receipts / # five-audit outputs and narrowly scoped release-note files may land before package. # Package/provenance/selftest/crosscheck/unit-test/allowlist/active-doc changes # after the tip MUST fail closure and force a new tip + matrix re-run. ALLOWED_PREFIXES = ( "receipts/", "evidence/", "CHANGE-TEST-NOTES", ) # Explicitly forbidden production / verification / release-logic inputs. FORBIDDEN_PREFIXES = ( "src/", "constraints/", "td/", "projects/prod2u-door-temp/", "tests/formal/", "tests/icarus/", "tests/cocotb/", "tests/unit/", "tools/", "config/", "docs/", "package_for_agent_analysis.py", "AGENTS.md", "README", "PLAN.md", "OPEN_QUESTIONS", ) def _git(args: list[str], *, cwd: Path) -> str: return subprocess.check_output(["git", *args], cwd=cwd, text=True).strip() def _classify(path: str) -> str: """Classify tip→delivery path. Packaging/docs/tests after tip = forbidden.""" lowered = path.replace("\\", "/") for prefix in FORBIDDEN_PREFIXES: if lowered == prefix or lowered.startswith(prefix): return "forbidden" for prefix in ALLOWED_PREFIXES: if lowered == prefix or lowered.startswith(prefix): return "allowed" return "forbidden" def build_closure( *, repo: Path, verification_tip: str, delivery_commit: str | None = None, matrix_run_id: str | None = None, tool_version: str = "prod2u-mse-verification-to-delivery-closure-v3", ) -> dict[str, Any]: head = delivery_commit or _git(["rev-parse", "HEAD"], cwd=repo) tip = verification_tip.strip() if len(tip) < 7: raise ValueError("verification_tip required") changed = [] if tip != head: out = _git(["diff", "--name-only", f"{tip}..{head}"], cwd=repo) changed = [line for line in out.splitlines() if line.strip()] classified = [{"path": p, "class": _classify(p)} for p in changed] forbidden = [row["path"] for row in classified if row["class"] == "forbidden"] closure_ok = len(forbidden) == 0 return { "schema": tool_version, "generated_utc": datetime.now(timezone.utc).isoformat(), "matrix_verification_tip": tip, "verification_tip": tip, # alias for older consumers "delivery_commit": head, "actual_package_head": head, "changed_paths": changed, "classified_paths": classified, "forbidden_changed_paths": forbidden, "allowed_path_prefixes": list(ALLOWED_PREFIXES), "forbidden_path_prefixes": list(FORBIDDEN_PREFIXES), "closure_ok": closure_ok, "matrix_run_id": matrix_run_id, "tool_version": tool_version, "policy": "round9-strict-receipts-only-after-tip", "note": ( "Package-time actual-HEAD closure (Round-9 strict). After verification tip, " "only receipts/evidence/narrow release notes may change. Package scripts, " "identity builders, selftests, unit tests, allowlists, and active docs after " "the tip force closure_ok=false and a new tip+matrix. delivery_commit must " "equal ZIP meta/git/HEAD.txt. Do not pre-commit this receipt inside the same tip." ), "hardware_validation": "NOT RUN", "dev_merge": "FORBIDDEN", } def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--repo", type=Path, default=ROOT) parser.add_argument("--verification-tip", required=True) parser.add_argument("--delivery-commit", default="") parser.add_argument("--matrix-run-id", default="") parser.add_argument("--output", type=Path, required=True) args = parser.parse_args(argv) payload = build_closure( repo=args.repo.resolve(), verification_tip=args.verification_tip, delivery_commit=args.delivery_commit or None, matrix_run_id=args.matrix_run_id or None, ) args.output.parent.mkdir(parents=True, exist_ok=True) args.output.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8") print(json.dumps({"closure_ok": payload["closure_ok"], "output": str(args.output)})) return 0 if payload["closure_ok"] else 1 if __name__ == "__main__": sys.exit(main())