#!/usr/bin/env python3 """Run the active deterministic protocol fuzz and emit a receipt.""" from __future__ import annotations import hashlib, json, os, subprocess, sys, time from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def sha256(path: Path) -> str: h = hashlib.sha256() with path.open("rb") as stream: for block in iter(lambda: stream.read(1048576), b""): h.update(block) return h.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("fuzz requires a clean source worktree") commit, tree = git("rev-parse", "HEAD"), git("rev-parse", "HEAD^{tree}") test = ROOT / "tests/manual_6400_fuzz.py" run_root = Path(os.environ.get("PROD2U_FUZZ_RUN_ROOT", "/data/prod2u/runs")) / ("6400-manual-fuzz-" + commit[:8]) run_root.mkdir(parents=True, exist_ok=False) result = run_root / "fuzz-result.json" env = os.environ.copy(); env["PROD2U_FUZZ_RESULT"] = str(result) env.setdefault("PROD2U_FUZZ_ITERATIONS", "10000") 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.is_file() else "FAIL" receipt = {"schema":"prod2u-6400-manual-fuzz-receipt-v1","run_id":run_root.name,"status":status,"commit":commit,"tree":tree,"clean":not bool(dirty),"iterations":int(env["PROD2U_FUZZ_ITERATIONS"]),"duration_seconds":round(time.monotonic()-started,3),"result":json.loads(result.read_text()) if result.is_file() else None,"stdout":completed.stdout[-4000:],"stderr":completed.stderr[-4000:],"hardware":"NOT_RUN"} path = run_root / "fuzz-receipt.json"; path.write_text(json.dumps(receipt, indent=2, sort_keys=True) + "\n", encoding="utf-8") print(path); print("MANUAL_FUZZ_PASS" if status == "PASS" else "MANUAL_FUZZ_FAIL") return 0 if status == "PASS" else 1 if __name__ == "__main__": raise SystemExit(main())