#!/usr/bin/env python3 """Generate deterministic P8-03 repeated execution evidence.""" from __future__ import annotations import argparse,hashlib,json,subprocess from pathlib import Path from typing import Any TESTS=("web/worker/test_sim_scheduler.mjs","web/worker/test_trace_export.mjs","web/worker/test_task_program_state.mjs","web/worker/test_recovery_offline_faults.mjs") OUTPUTS=tuple(Path(p) for p in TESTS) def canonical(v:Any)->str:return json.dumps(v,ensure_ascii=False,sort_keys=True,separators=(",",":")) def sha256(p:Path)->str:return hashlib.sha256(p.read_bytes()).hexdigest() def run(root:Path,path:str)->dict[str,Any]: r=subprocess.run(["node",path],cwd=root,stdout=subprocess.PIPE,stderr=subprocess.PIPE,check=False); return {"status":"verified" if r.returncode==0 else "blocked","returncode":r.returncode,"stdout_sha256":hashlib.sha256(r.stdout).hexdigest(),"stderr_sha256":hashlib.sha256(r.stderr).hexdigest()} def generate(root:Path,out:Path)->dict[str,Any]: repeats={p:[run(root,p) for _ in range(3)] for p in TESTS}; stable=all(all(x==rows[0] for x in rows) and rows[0]["status"]=="verified" for rows in repeats.values()) report={"schema":"determinism-report.v1","task_id":"P8-03","context_id":"linuxcnc-2.10.0-pre0-wasm-m1-py-c1","status":"verified" if stable else "blocked","modes":["1x","fast","single_step","recover"],"repeat_runs":3,"stable":stable,"first_divergence":None if stable else {"test":"unknown","tick":None,"field":"stdout/stderr_hash"},"tests":repeats,"outputs":[{"path":p.as_posix(),"bytes":(root/p).stat().st_size,"sha256":sha256(root/p)} for p in OUTPUTS],"known_boundaries":["P8-04 browser/performance matrix","P8-05 delivery closure"]}; report["report_sha256"]=hashlib.sha256(canonical(report).encode()).hexdigest(); out.parent.mkdir(parents=True,exist_ok=True); out.write_text(json.dumps(report,ensure_ascii=False,sort_keys=True,indent=2)+"\n"); return report def main()->int: p=argparse.ArgumentParser();p.add_argument("--repo-root",type=Path,default=Path(__file__).resolve().parents[1]);p.add_argument("--report",type=Path,default=Path("coverage/determinism.report.json"));a=p.parse_args();r=generate(a.repo_root.resolve(),a.repo_root.resolve()/a.report);print(f"determinism={r['status']} repeats={r['repeat_runs']}");return 0 if r["status"]=="verified" else 1 if __name__=="__main__":raise SystemExit(main())