#!/usr/bin/env python3 """Run the active simulator-free 6400 manual soak and emit a receipt.""" from __future__ import annotations import hashlib import json import os from pathlib import Path import subprocess import sys import time ROOT = Path(__file__).resolve().parents[1] CLOSURE = ROOT / "products/6400-front-rear/source-closure.json" def sha256(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as stream: for block in iter(lambda: stream.read(1024 * 1024), b""): digest.update(block) return digest.hexdigest() def git(*args: str) -> str: return subprocess.check_output(["git", *args], cwd=ROOT, text=True).strip() def main() -> int: dirty = git("status", "--porcelain") if dirty: raise SystemExit("soak requires a clean source worktree") commit, tree = git("rev-parse", "HEAD"), git("rev-parse", "HEAD^{tree}") closure = json.loads(CLOSURE.read_text(encoding="utf-8")) sources = [ROOT / item for item in closure["sources"] if item.endswith(".v")] test = ROOT / "tests/manual_6400_soak.py" inputs = [CLOSURE, test, *sources] source_manifest = hashlib.sha256( json.dumps({str(p.relative_to(ROOT)): sha256(p) for p in inputs}, sort_keys=True, separators=(",", ":")).encode() ).hexdigest() run_id = "6400-manual-soak-" + commit[:8] run_root = Path(os.environ.get("PROD2U_SOAK_RUN_ROOT", "/data/prod2u/runs")) / run_id run_root.mkdir(parents=True, exist_ok=False) result_json = run_root / "soak-result.json" env = os.environ.copy() env["PROD2U_SOAK_RESULT"] = str(result_json) env.setdefault("PROD2U_SOAK_ITERATIONS", "2048") env.setdefault("PROD2U_SOAK_SEED", "64006400") started = time.monotonic() completed = subprocess.run([sys.executable, "-B", str(test)], cwd=ROOT, env=env, text=True, capture_output=True, check=False) status = "PASS" if completed.returncode == 0 and result_json.is_file() else "FAIL" receipt = { "schema": "prod2u-6400-manual-soak-receipt-v1", "run_id": run_id, "status": status, "commit": commit, "tree": tree, "clean": not bool(dirty), "command": "python3 tools/run_6400_soak.py", "duration_seconds": round(time.monotonic() - started, 3), "source_manifest_sha256": source_manifest, "iterations": int(env["PROD2U_SOAK_ITERATIONS"]), "seed": int(env["PROD2U_SOAK_SEED"]), "result": json.loads(result_json.read_text()) if result_json.is_file() else None, "stdout": completed.stdout[-4000:], "stderr": completed.stderr[-4000:], "hardware": "NOT_RUN", } receipt_path = run_root / "soak-receipt.json" receipt_path.write_text(json.dumps(receipt, indent=2, sort_keys=True) + "\n", encoding="utf-8") print(receipt_path) print("MANUAL_SOAK_PASS" if status == "PASS" else "MANUAL_SOAK_FAIL") return 0 if status == "PASS" else 1 if __name__ == "__main__": raise SystemExit(main())