#!/usr/bin/env python3 """Generate deterministic canonical queue/TP blend evidence for P3-03.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path from typing import Any OUTPUTS = ( Path("wasm-core/include/sim_motion_queue.h"), Path("wasm-core/src/sim_motion_queue.c"), Path("wasm-core/tests/sim_motion_queue_test.c"), ) def canonical(value: Any) -> str: return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) def sha256(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as handle: for chunk in iter(lambda: handle.read(1024 * 1024), b""): digest.update(chunk) return digest.hexdigest() def command(root: Path, args: list[str]) -> dict[str, Any]: result = subprocess.run(args, cwd=root, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) return {"status": "verified" if result.returncode == 0 else "blocked", "command": " ".join(args), "returncode": result.returncode, "stdout": result.stdout.decode("utf-8", errors="replace")[-2000:], "stderr": result.stderr.decode("utf-8", errors="replace")[-2000:]} def generate(root: Path, report_path: Path) -> dict[str, Any]: native = command(root, [str(root / "build-native/sim_motion_queue_test")]) wasm_binary = root / "build-wasm/sim_motion_queue_wasm_test.js" wasm = command(root, ["node", "build-wasm/sim_motion_queue_wasm_test.js"]) if wasm_binary.is_file() else {"status": "blocked", "command": "node build-wasm/sim_motion_queue_wasm_test.js", "reason": "wasm_test_missing"} outputs = [{"path": path.as_posix(), "bytes": (root / path).stat().st_size, "sha256": sha256(root / path)} for path in OUTPUTS] report: dict[str, Any] = { "schema": "tp-blend-report.v1", "task_id": "P3-03", "context_id": "linuxcnc-2.10.0-pre0-wasm-m1-py-c1", "status": "verified" if native["status"] == "verified" and wasm["status"] == "verified" else "blocked", "profiles": {"blend": {"lookahead": "bounded min velocity over queued segments", "junction": "deterministic velocity carry"}, "exact_stop": {"lookahead": "disabled at junction", "junction": "zero velocity"}}, "queue": {"capacity": 64, "peek": True, "segment_order": "FIFO", "plan_hash": "fnv1a64 profile/lookahead/queued planned velocities"}, "determinism": {"fixed_tick": True, "wall_clock": False, "random": False, "native_wasm_state_hash": True}, "outputs": outputs, "native": native, "wasm": wasm, "known_boundaries": ["full kinematics IK/motion adapter is P3-05/P3-06", "official LinuxCNC TP oracle remains source-gated"], } 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/tp-blend.report.json")) args = parser.parse_args() report = generate(args.repo_root.resolve(), args.repo_root.resolve() / args.report) print(f"tp_blend={report['status']} native={report['native']['status']} wasm={report['wasm']['status']}") return 0 if report["status"] == "verified" else 1 if __name__ == "__main__": raise SystemExit(main())