run_verification_matrix.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. """Run the full Prod2U CPLD offline verification matrix.
  2. This script is the canonical Team C runner. It executes the software, HDL,
  3. formal, and package lanes against an exact commit and writes machine-readable
  4. receipts. Missing toolchains produce `blocked` or `not_run` status, never a
  5. fabricated pass.
  6. """
  7. from __future__ import annotations
  8. import argparse
  9. import json
  10. import os
  11. import subprocess
  12. import sys
  13. import time
  14. from pathlib import Path
  15. from typing import Any
  16. _repo_root = Path(__file__).resolve().parents[1]
  17. if str(_repo_root) not in sys.path:
  18. sys.path.insert(0, str(_repo_root))
  19. from tools import codex_harness, prod2u_p4_harness
  20. REQUIRED_LANES = [
  21. "python",
  22. "icarus",
  23. "verilator",
  24. "formal",
  25. "cocotb",
  26. ]
  27. def _run(cmd: list[str], cwd: Path) -> subprocess.CompletedProcess[str]:
  28. print(" ".join(cmd))
  29. return subprocess.run(cmd, cwd=cwd, text=True, capture_output=True)
  30. def run_python_unit(repo: Path, work_root: Path, run_id: str) -> dict[str, Any]:
  31. receipt_path = work_root / "python-unit-receipt.json"
  32. receipt_path.parent.mkdir(parents=True, exist_ok=True)
  33. result = _run(
  34. [
  35. sys.executable,
  36. "-B",
  37. "-m",
  38. "pytest",
  39. "-q",
  40. "-p",
  41. "no:cacheprovider",
  42. "tests/unit",
  43. "tests/test_project_structure.py",
  44. ],
  45. repo,
  46. )
  47. status = "pass" if result.returncode == 0 else "fail"
  48. full_commit = prod2u_p4_harness.current_commit()
  49. return {
  50. "schema": "prod2u-p4-normal-v3-python-unit-receipt-v1",
  51. "status": status,
  52. "verification_run_id": run_id,
  53. "commit": full_commit[:8],
  54. "commit_full": full_commit,
  55. "project_config_sha256": prod2u_p4_harness.sha256_file(repo / "projects" / "prod2u-door-temp" / "project.json"),
  56. "td_source_manifest_sha256": prod2u_p4_harness.sha256_file(repo / "src" / "generated" / "td-source-manifest.json"),
  57. "command": "python -B -m pytest -q -p no:cacheprovider tests/unit tests/test_project_structure.py",
  58. "tool_version": f"Python {sys.version.split()[0]}",
  59. "returncode": result.returncode,
  60. "stdout_tail": result.stdout[-2048:] if result.stdout else "",
  61. "stderr_tail": result.stderr[-2048:] if result.stderr else "",
  62. "report_path": str(receipt_path),
  63. "authoritative": status == "pass",
  64. "placeholder": False,
  65. }
  66. def _run_harness_lane(repo: Path, work_root: Path, run_id: str, lane: str) -> dict[str, Any]:
  67. receipt_path = work_root / f"{lane}-receipt.json"
  68. backend = "icarus" if lane == "icarus" else "verilator" if lane == "verilator" else "cocotb"
  69. cmd = [
  70. sys.executable,
  71. "-B",
  72. "tools/prod2u_p4_harness.py",
  73. "hdl-sim",
  74. "--run",
  75. "--backend",
  76. backend,
  77. "--work-root",
  78. str(work_root),
  79. "--verification-run-id",
  80. run_id,
  81. "--report-file",
  82. str(receipt_path),
  83. ]
  84. result = _run(cmd, repo)
  85. try:
  86. receipt = json.loads(receipt_path.read_text(encoding="utf-8-sig"))
  87. except (OSError, json.JSONDecodeError):
  88. receipt = {
  89. "schema": "prod2u-p4-normal-v3-hdl-sim-receipt-v1",
  90. "status": "blocked",
  91. "verification_run_id": run_id,
  92. "failures": [f"receipt_unreadable:{lane}"],
  93. }
  94. return receipt
  95. def run_formal(repo: Path, work_root: Path, run_id: str) -> dict[str, Any]:
  96. receipt_path = work_root / "formal-receipt.json"
  97. cmd = [
  98. sys.executable,
  99. "-B",
  100. "tools/prod2u_p4_harness.py",
  101. "formal",
  102. "--run",
  103. "--work-root",
  104. str(work_root),
  105. "--verification-run-id",
  106. run_id,
  107. "--report-file",
  108. str(receipt_path),
  109. ]
  110. result = _run(cmd, repo)
  111. try:
  112. receipt = json.loads(receipt_path.read_text(encoding="utf-8-sig"))
  113. except (OSError, json.JSONDecodeError):
  114. receipt = {
  115. "schema": "prod2u-p4-normal-v3-formal-receipt-v1",
  116. "status": "blocked",
  117. "verification_run_id": run_id,
  118. "failures": ["receipt_unreadable:formal"],
  119. }
  120. return receipt
  121. def main() -> int:
  122. parser = argparse.ArgumentParser(description="Run the verification matrix")
  123. parser.add_argument("--work-root-prefix", required=True)
  124. parser.add_argument("--verification-run-id", required=True)
  125. parser.add_argument("--report-file", required=True)
  126. parser.add_argument("--lanes", default=",".join(REQUIRED_LANES))
  127. parser.add_argument("--stop-on-fail", action="store_true")
  128. args = parser.parse_args()
  129. repo = Path(__file__).resolve().parents[1]
  130. run_id = args.verification_run_id
  131. lanes = [l.strip() for l in args.lanes.split(",") if l.strip()]
  132. results: dict[str, Any] = {}
  133. all_pass = True
  134. for lane in lanes:
  135. work_root = Path(f"{args.work_root_prefix}-{lane}-{run_id[:8]}")
  136. if lane == "python":
  137. receipt = run_python_unit(repo, work_root, run_id)
  138. elif lane in ("icarus", "verilator", "cocotb"):
  139. receipt = _run_harness_lane(repo, work_root, run_id, lane)
  140. elif lane == "formal":
  141. receipt = run_formal(repo, work_root, run_id)
  142. else:
  143. receipt = {"status": "blocked", "failures": [f"unknown_lane:{lane}"]}
  144. results[lane] = receipt
  145. if receipt.get("status") != "pass":
  146. all_pass = False
  147. if args.stop_on_fail:
  148. break
  149. full_commit = prod2u_p4_harness.current_commit()
  150. matrix = {
  151. "schema": "prod2u-p4-normal-v3-verification-matrix-v1",
  152. "generated_at": prod2u_p4_harness.utc_now(),
  153. "verification_run_id": run_id,
  154. "commit": full_commit[:8],
  155. "commit_full": full_commit,
  156. "project_config_sha256": prod2u_p4_harness.sha256_file(repo / "projects" / "prod2u-door-temp" / "project.json"),
  157. "td_source_manifest_sha256": prod2u_p4_harness.sha256_file(repo / "src" / "generated" / "td-source-manifest.json"),
  158. "status": "pass" if all_pass else "fail",
  159. "lanes": results,
  160. "failures": [f"{lane}:{r['status']}" for lane, r in results.items() if r.get("status") != "pass"],
  161. }
  162. report_path = Path(args.report_file)
  163. report_path.parent.mkdir(parents=True, exist_ok=True)
  164. report_path.write_text(json.dumps(matrix, indent=2, sort_keys=True) + "\n", encoding="utf-8")
  165. print(json.dumps({"status": matrix["status"], "report": str(report_path)}, indent=2))
  166. return 0 if all_pass else 1
  167. if __name__ == "__main__":
  168. sys.exit(main())