"""Run the full Prod2U CPLD offline verification matrix. This script is the canonical Team C runner. It executes the software, HDL, formal, and package lanes against an exact commit and writes machine-readable receipts. Missing toolchains produce `blocked` or `not_run` status, never a fabricated pass. """ from __future__ import annotations import argparse import json import os import subprocess import sys import time from pathlib import Path from typing import Any _repo_root = Path(__file__).resolve().parents[1] if str(_repo_root) not in sys.path: sys.path.insert(0, str(_repo_root)) from tools import codex_harness, prod2u_p4_harness REQUIRED_LANES = [ "python", "icarus", "verilator", "formal", "cocotb", ] def _run(cmd: list[str], cwd: Path) -> subprocess.CompletedProcess[str]: print(" ".join(cmd)) return subprocess.run(cmd, cwd=cwd, text=True, capture_output=True) def run_python_unit(repo: Path, work_root: Path, run_id: str) -> dict[str, Any]: receipt_path = work_root / "python-unit-receipt.json" receipt_path.parent.mkdir(parents=True, exist_ok=True) result = _run( [ sys.executable, "-B", "-m", "pytest", "-q", "-p", "no:cacheprovider", "tests/unit", "tests/test_project_structure.py", ], repo, ) status = "pass" if result.returncode == 0 else "fail" full_commit = prod2u_p4_harness.current_commit() receipt = { "schema": "prod2u-p4-normal-v3-python-unit-receipt-v1", "status": status, "verification_run_id": run_id, "commit": full_commit, "commit_full": full_commit, "project_config_sha256": prod2u_p4_harness.sha256_file(repo / "projects" / "prod2u-door-temp" / "project.json"), "td_source_manifest_sha256": prod2u_p4_harness.sha256_file(repo / "src" / "generated" / "td-source-manifest.json"), "command": "python -B -m pytest -q -p no:cacheprovider tests/unit tests/test_project_structure.py", "tool_version": f"Python {sys.version.split()[0]}", "returncode": result.returncode, "stdout_tail": result.stdout[-2048:] if result.stdout else "", "stderr_tail": result.stderr[-2048:] if result.stderr else "", "report_path": str(receipt_path), "authoritative": status == "pass", "placeholder": False, } receipt_path.write_text(json.dumps(receipt, indent=2, sort_keys=True) + "\n", encoding="utf-8") return receipt def _run_harness_lane(repo: Path, work_root: Path, run_id: str, lane: str) -> dict[str, Any]: receipt_path = work_root / f"{lane}-receipt.json" backend = "icarus" if lane == "icarus" else "verilator" if lane == "verilator" else "cocotb" cmd = [ sys.executable, "-B", "tools/prod2u_p4_harness.py", "hdl-sim", "--run", "--backend", backend, "--work-root", str(work_root), "--verification-run-id", run_id, "--report-file", str(receipt_path), ] result = _run(cmd, repo) try: receipt = json.loads(receipt_path.read_text(encoding="utf-8-sig")) except (OSError, json.JSONDecodeError): receipt = { "schema": "prod2u-p4-normal-v3-hdl-sim-receipt-v1", "status": "blocked", "verification_run_id": run_id, "failures": [f"receipt_unreadable:{lane}"], } return receipt def run_formal(repo: Path, work_root: Path, run_id: str) -> dict[str, Any]: receipt_path = work_root / "formal-receipt.json" cmd = [ sys.executable, "-B", "tools/prod2u_p4_harness.py", "formal", "--run", "--work-root", str(work_root), "--verification-run-id", run_id, "--report-file", str(receipt_path), ] result = _run(cmd, repo) try: receipt = json.loads(receipt_path.read_text(encoding="utf-8-sig")) except (OSError, json.JSONDecodeError): receipt = { "schema": "prod2u-p4-normal-v3-formal-receipt-v1", "status": "blocked", "verification_run_id": run_id, "failures": ["receipt_unreadable:formal"], } return receipt def main() -> int: parser = argparse.ArgumentParser(description="Run the verification matrix") parser.add_argument("--work-root-prefix", required=True) parser.add_argument("--verification-run-id", required=True) parser.add_argument("--report-file", required=True) parser.add_argument("--lanes", default=",".join(REQUIRED_LANES)) parser.add_argument("--stop-on-fail", action="store_true") args = parser.parse_args() repo = Path(__file__).resolve().parents[1] run_id = args.verification_run_id lanes = [l.strip() for l in args.lanes.split(",") if l.strip()] results: dict[str, Any] = {} all_pass = True for lane in lanes: work_root = Path(f"{args.work_root_prefix}-{lane}-{run_id[:8]}") if lane == "python": receipt = run_python_unit(repo, work_root, run_id) elif lane in ("icarus", "verilator", "cocotb"): receipt = _run_harness_lane(repo, work_root, run_id, lane) elif lane == "formal": receipt = run_formal(repo, work_root, run_id) else: receipt = {"status": "blocked", "failures": [f"unknown_lane:{lane}"]} results[lane] = receipt if receipt.get("status") != "pass": all_pass = False if args.stop_on_fail: break full_commit = prod2u_p4_harness.current_commit() matrix = { "schema": "prod2u-p4-normal-v3-verification-matrix-v1", "generated_at": prod2u_p4_harness.utc_now(), "verification_run_id": run_id, "commit": full_commit[:8], "commit_full": full_commit, "project_config_sha256": prod2u_p4_harness.sha256_file(repo / "projects" / "prod2u-door-temp" / "project.json"), "td_source_manifest_sha256": prod2u_p4_harness.sha256_file(repo / "src" / "generated" / "td-source-manifest.json"), "status": "pass" if all_pass else "fail", "lanes": results, "failures": [f"{lane}:{r['status']}" for lane, r in results.items() if r.get("status") != "pass"], } report_path = Path(args.report_file) report_path.parent.mkdir(parents=True, exist_ok=True) report_path.write_text(json.dumps(matrix, indent=2, sort_keys=True) + "\n", encoding="utf-8") print(json.dumps({"status": matrix["status"], "report": str(report_path)}, indent=2)) return 0 if all_pass else 1 if __name__ == "__main__": sys.exit(main())