#!/usr/bin/env python3 """Generate deterministic evidence for home and limit integration.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path INPUTS = { "home_header": "wasm-core/include/sim_kinematics_home_limit.h", "home_source": "wasm-core/src/sim_kinematics_home_limit.c", "kinematics_header": "wasm-core/include/sim_kinematics.h", "kinematics_source": "wasm-core/src/sim_kinematics.c", "test": "wasm-core/tests/sim_kinematics_home_limit_test.c", "linuxcnc_homing": "linuxcnc/src/emc/motion/homing.c", "linuxcnc_homemod": "linuxcnc/src/emc/motion/homemod.c", } def sha256(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def run_json(command: list[str], root: Path) -> dict[str, object]: result = subprocess.run(command, cwd=root, check=False, text=True, capture_output=True) if result.returncode: raise RuntimeError(f"{command[0]} exited {result.returncode}: {result.stderr.strip()}") for line in result.stdout.splitlines(): if line.lstrip().startswith("{"): item = json.loads(line) if item.get("schema") == "kinematics-home-limit-samples.v1": return item raise RuntimeError("runner did not emit kinematics-home-limit-samples.v1") def generate(root: Path, output: Path) -> dict[str, object]: root = root.resolve() native = run_json([str(root / "build-native/sim_kinematics_home_limit_test")], root) wasm = run_json(["node", str(root / "build-wasm/sim_kinematics_home_limit_wasm_test.js")], root) report = { "schema": "kinematics-home-limit-report.v1", "status": "partial", "implementation_status": "verified", "source_records": {name: {"path": path, "sha256": sha256(root / path)} for name, path in INPUTS.items()}, "pipeline": {"order": ["home_sequence", "kinematics_home", "limit_check", "forward_feedback"], "state_fields": ["homed", "home_sequence", "tick", "pose", "joints", "last_error", "limit_error", "kinematics_error"], "failed_limit_policy": "retain last valid pose and joints"}, "configuration_candidates": [{"id": "trivkins-xyz-home-limits", "kind": "derived", "path": INPUTS["test"], "coordinates": "XYZ", "limits": "[-5,5] per axis"}], "boundaries": {"unhomed": native["before_home"], "sequence": native["sequence_error"], "home": native["home_status"], "limit": native["limit_status"], "nonfinite": native["nonfinite_status"], "joint_count": native["count_status"], "state_hash_stable": native["state"]["hash"] == native["state"]["repeat_hash"], "retains_valid_pose": native["state"]["pose"] == [1.0, 2.0, 3.0]}, "samples": {"native": native, "wasm": wasm, "byte_identical_json": json.dumps(native, sort_keys=True, separators=(",", ":")) == json.dumps(wasm, sort_keys=True, separators=(",", ":")), "state_hash_identical": native["state"]["hash"] == wasm["state"]["hash"]}, "native_wasm": {"status": "passed", "native_command": "build-native/sim_kinematics_home_limit_test", "wasm_command": "node build-wasm/sim_kinematics_home_limit_wasm_test.js"}, "oracle": {"status": "blocked", "reason": "linuxcnc/unit_tests/kinematics is absent from the locked source", "blocked_samples": ["unhomed check", "home sequence", "kinematics home", "joint limit", "nonfinite joint", "joint count", "state hash"]}, } report["report_sha256"] = hashlib.sha256(json.dumps(report, sort_keys=True, separators=(",", ":")).encode()).hexdigest() 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(description=__doc__) parser.add_argument("--repo-root", type=Path, default=Path(__file__).resolve().parents[1]) parser.add_argument("--output", type=Path, default=Path("coverage/kinematics-home-limit.report.json")) args = parser.parse_args() report = generate(args.repo_root, args.repo_root / args.output) print(f"kinematics_home_limit={report['status']} native_wasm={report['native_wasm']['status']} oracle={report['oracle']['status']}") return 0 if __name__ == "__main__": raise SystemExit(main())