run_verification_matrix.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. receipt = {
  50. "schema": "prod2u-p4-normal-v3-python-unit-receipt-v1",
  51. "status": status,
  52. "verification_run_id": run_id,
  53. "commit": full_commit,
  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. receipt_path.write_text(json.dumps(receipt, indent=2, sort_keys=True) + "\n", encoding="utf-8")
  67. return receipt
  68. def _run_harness_lane(repo: Path, work_root: Path, run_id: str, lane: str) -> dict[str, Any]:
  69. receipt_path = work_root / f"{lane}-receipt.json"
  70. backend = "icarus" if lane == "icarus" else "verilator" if lane == "verilator" else "cocotb"
  71. cmd = [
  72. sys.executable,
  73. "-B",
  74. "tools/prod2u_p4_harness.py",
  75. "hdl-sim",
  76. "--run",
  77. "--backend",
  78. backend,
  79. "--work-root",
  80. str(work_root),
  81. "--verification-run-id",
  82. run_id,
  83. "--report-file",
  84. str(receipt_path),
  85. ]
  86. result = _run(cmd, repo)
  87. try:
  88. receipt = json.loads(receipt_path.read_text(encoding="utf-8-sig"))
  89. except (OSError, json.JSONDecodeError):
  90. receipt = {
  91. "schema": "prod2u-p4-normal-v3-hdl-sim-receipt-v1",
  92. "status": "blocked",
  93. "verification_run_id": run_id,
  94. "failures": [f"receipt_unreadable:{lane}"],
  95. }
  96. return receipt
  97. def run_formal(repo: Path, work_root: Path, run_id: str) -> dict[str, Any]:
  98. receipt_path = work_root / "formal-receipt.json"
  99. cmd = [
  100. sys.executable,
  101. "-B",
  102. "tools/prod2u_p4_harness.py",
  103. "formal",
  104. "--run",
  105. "--work-root",
  106. str(work_root),
  107. "--verification-run-id",
  108. run_id,
  109. "--report-file",
  110. str(receipt_path),
  111. ]
  112. result = _run(cmd, repo)
  113. try:
  114. receipt = json.loads(receipt_path.read_text(encoding="utf-8-sig"))
  115. except (OSError, json.JSONDecodeError):
  116. receipt = {
  117. "schema": "prod2u-p4-normal-v3-formal-receipt-v1",
  118. "status": "blocked",
  119. "verification_run_id": run_id,
  120. "failures": ["receipt_unreadable:formal"],
  121. }
  122. return receipt
  123. def main() -> int:
  124. parser = argparse.ArgumentParser(description="Run the verification matrix")
  125. parser.add_argument("--work-root-prefix", required=True)
  126. parser.add_argument("--verification-run-id", required=True)
  127. parser.add_argument("--report-file", required=True)
  128. parser.add_argument("--lanes", default=",".join(REQUIRED_LANES))
  129. parser.add_argument("--stop-on-fail", action="store_true")
  130. args = parser.parse_args()
  131. repo = Path(__file__).resolve().parents[1]
  132. run_id = args.verification_run_id
  133. lanes = [l.strip() for l in args.lanes.split(",") if l.strip()]
  134. results: dict[str, Any] = {}
  135. all_pass = True
  136. for lane in lanes:
  137. work_root = Path(f"{args.work_root_prefix}-{lane}-{run_id[:8]}")
  138. if lane == "python":
  139. receipt = run_python_unit(repo, work_root, run_id)
  140. elif lane in ("icarus", "verilator", "cocotb"):
  141. receipt = _run_harness_lane(repo, work_root, run_id, lane)
  142. elif lane == "formal":
  143. receipt = run_formal(repo, work_root, run_id)
  144. else:
  145. receipt = {"status": "blocked", "failures": [f"unknown_lane:{lane}"]}
  146. results[lane] = receipt
  147. if receipt.get("status") != "pass":
  148. all_pass = False
  149. if args.stop_on_fail:
  150. break
  151. full_commit = prod2u_p4_harness.current_commit()
  152. matrix = {
  153. "schema": "prod2u-p4-normal-v3-verification-matrix-v1",
  154. "generated_at": prod2u_p4_harness.utc_now(),
  155. "verification_run_id": run_id,
  156. "commit": full_commit[:8],
  157. "commit_full": full_commit,
  158. "project_config_sha256": prod2u_p4_harness.sha256_file(repo / "projects" / "prod2u-door-temp" / "project.json"),
  159. "td_source_manifest_sha256": prod2u_p4_harness.sha256_file(repo / "src" / "generated" / "td-source-manifest.json"),
  160. "status": "pass" if all_pass else "fail",
  161. "lanes": results,
  162. "failures": [f"{lane}:{r['status']}" for lane, r in results.items() if r.get("status") != "pass"],
  163. }
  164. report_path = Path(args.report_file)
  165. report_path.parent.mkdir(parents=True, exist_ok=True)
  166. report_path.write_text(json.dumps(matrix, indent=2, sort_keys=True) + "\n", encoding="utf-8")
  167. print(json.dumps({"status": matrix["status"], "report": str(report_path)}, indent=2))
  168. return 0 if all_pass else 1
  169. if __name__ == "__main__":
  170. sys.exit(main())