#!/usr/bin/env python3 """Generate deterministic P6-01 full snapshot/event/trace evidence.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path from typing import Any OUTPUTS = (Path("web/worker/trace-export.mjs"), Path("web/worker/sim-session.mjs"), Path("web/worker/test_trace_export.mjs"), Path("web/worker/replay.mjs"), Path("web/worker/contracts.mjs")) def canonical(value: Any) -> str: return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) def sha256(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def generate(root: Path, report_path: Path) -> dict[str, Any]: result = subprocess.run(["node", "web/worker/test_trace_export.mjs"], cwd=root, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) report: dict[str, Any] = { "schema": "trace-export-report.v1", "task_id": "P6-01", "context_id": "linuxcnc-2.10.0-pre0-wasm-m1-py-c1", "status": "verified" if result.returncode == 0 else "blocked", "trace": {"schema": "simulation-trace.v1", "fields": ["context_id", "seq", "tick", "state", "state_hash", "task", "timeline", "event_hash", "snapshot_hash", "trace_hash"], "subsystems": ["task", "scheduler", "program", "motion", "plc", "io_ack", "callbacks", "devices", "python", "workspace"]}, "validation": ["event sequence", "event tick monotonic", "payload hash", "snapshot hash/id/base", "state hash", "context", "trace hash", "nonfinite rejection"], "test": {"status": "verified" if result.returncode == 0 else "blocked", "command": "node web/worker/test_trace_export.mjs", "stdout": result.stdout.decode("utf-8", errors="replace")[-3000:], "stderr": result.stderr.decode("utf-8", errors="replace")[-2000:]}, "outputs": [{"path": path.as_posix(), "bytes": (root / path).stat().st_size, "sha256": sha256(root / path)} for path in OUTPUTS], "known_boundaries": ["journal/checkpoint reconciliation is P6-02", "OPFS migration/offline package is P6-03"], } report["report_sha256"] = hashlib.sha256(canonical(report).encode("utf-8")).hexdigest() report_path.parent.mkdir(parents=True, exist_ok=True) report_path.write_text(json.dumps(report, ensure_ascii=False, sort_keys=True, indent=2) + "\n", encoding="utf-8") return report def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--repo-root", type=Path, default=Path(__file__).resolve().parents[1]) parser.add_argument("--report", type=Path, default=Path("coverage/trace-export.report.json")) args = parser.parse_args() report = generate(args.repo_root.resolve(), args.repo_root.resolve() / args.report) print(f"trace_export={report['status']} test={report['test']['status']}") return 0 if report["status"] == "verified" else 1 if __name__ == "__main__": raise SystemExit(main())