#!/usr/bin/env python3 """Generate S6-03 authoritative CoreXY kinematics evidence.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path from typing import Any CONTEXT_ID = "linuxcnc-2.10.0-pre0-wasm-m1-py-c1" WORKER = ("node", "web/worker/test_authoritative_kinematics_s6.mjs") PAIRS = {"kinematics": (("build-native/sim_kinematics_test",), ("node", "build-wasm/sim_kinematics_wasm_test.js")), "kinematics_motion": (("build-native/sim_kinematics_motion_test",), ("node", "build-wasm/sim_kinematics_motion_wasm_test.js"))} def canonical(value: Any) -> str: return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) def digest(value: bytes) -> str: return hashlib.sha256(value).hexdigest() def run(root: Path, args: tuple[str, ...]) -> dict[str, Any]: result = subprocess.run(args, cwd=root, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) return {"command": list(args), "returncode": result.returncode, "status": "PASSED" if result.returncode == 0 else "FAILED", "stdout_sha256": digest(result.stdout), "stderr_sha256": digest(result.stderr), "stdout": result.stdout.decode("utf-8", errors="replace"), "stderr": result.stderr.decode("utf-8", errors="replace")} def no_output(result: dict[str, Any]) -> dict[str, Any]: return {key: value for key, value in result.items() if key not in {"stdout", "stderr"}} def parse(result: dict[str, Any]) -> dict[str, Any] | None: try: return json.loads(result["stdout"]) except json.JSONDecodeError: return None def artifact(root: Path, path: Path) -> dict[str, Any]: value = root / path return {"path": path.as_posix(), "bytes": value.stat().st_size, "sha256": digest(value.read_bytes())} if value.is_file() else {"path": path.as_posix(), "status": "missing"} def generate(root: Path, output: Path) -> dict[str, Any]: worker_result = run(root, WORKER) worker = parse(worker_result) native_wasm = {} pair_ok = True for name, (native_cmd, wasm_cmd) in PAIRS.items(): native = run(root, native_cmd); wasm = run(root, wasm_cmd) equal = native["returncode"] == 0 and wasm["returncode"] == 0 and native["stdout"] == wasm["stdout"] pair_ok = pair_ok and equal native_wasm[name] = {"native": no_output(native), "wasm": no_output(wasm), "golden_diff": {"status": "PASSED" if equal else "FAILED", "byte_identical": equal, "native_stdout_sha256": native["stdout_sha256"], "wasm_stdout_sha256": wasm["stdout_sha256"]}} ctest = run(root, ("ctest", "--test-dir", "build-native", "-R", "^(sim_kinematics_test|sim_kinematics_motion_test)$", "--output-on-failure")) worker_ok = worker_result["returncode"] == 0 and worker and worker.get("status") == "verified" and worker.get("supported", {}).get("joints") == [6, 2, 3] and worker.get("parity", {}).get("mode", {}).get("equal") and worker.get("parity", {}).get("recovery", {}).get("equal") report: dict[str, Any] = {"schema": "authoritative-kinematics-s6.v1", "task_id": "S6-03", "context_id": CONTEXT_ID, "status": "verified" if worker_ok and pair_ok and ctest["returncode"] == 0 else "blocked", "scope": {"kinematics": "corexy", "pose": [4, 2, 3], "joints": [6, 2, 3], "unsupported_error": worker.get("unsupported", {}).get("code") if worker else None}, "worker": {"execution": no_output(worker_result), "sample": worker}, "native_wasm": native_wasm, "ctest": no_output(ctest), "inputs": [artifact(root, Path("coverage/authoritative-sim-task-s6.report.json")), artifact(root, Path("coverage/kinematics-motion.report.json")), artifact(root, Path("wasm-core/include/sim_kinematics_motion.h")), artifact(root, Path("wasm-core/src/sim_kinematics_motion.c")), artifact(root, Path("web/worker/authoritative-runtime.mjs"))], "boundaries": ["other non-trivkins families remain queued for later slices", "official LinuxCNC kinematics oracle remains SOURCE_BLOCKED", "full LinuxCNC canonical/native source parity remains open"]} report["report_sha256"] = digest(canonical({key: value for key, value in report.items() if key != "report_sha256"}).encode()) output.parent.mkdir(parents=True, exist_ok=True); output.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(); parser.add_argument("--repo-root", type=Path, default=Path(__file__).resolve().parents[1]); parser.add_argument("--report", type=Path, default=Path("coverage/authoritative-kinematics-s6.report.json")); args = parser.parse_args(); report = generate(args.repo_root.resolve(), args.repo_root.resolve() / args.report); print(f"authoritative_kinematics_s6={report['status']} family={report['scope']['kinematics']}"); return 0 if report["status"] == "verified" else 1 if __name__ == "__main__": raise SystemExit(main())