#!/usr/bin/env python3 """Generate deterministic evidence for FK feedback integration.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path INPUTS = { "feedback_header": "wasm-core/include/sim_kinematics_feedback.h", "feedback_source": "wasm-core/src/sim_kinematics_feedback.c", "kinematics_header": "wasm-core/include/sim_kinematics.h", "kinematics_source": "wasm-core/src/sim_kinematics.c", "test": "wasm-core/tests/sim_kinematics_feedback_test.c", "linuxcnc_control": "linuxcnc/src/emc/motion/control.c", "linuxcnc_motion": "linuxcnc/src/emc/motion/motion.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-feedback-samples.v1": return item raise RuntimeError("runner did not emit kinematics-feedback-samples.v1") def generate(root: Path, output: Path) -> dict[str, object]: root = root.resolve() native = run_json([str(root / "build-native/sim_kinematics_feedback_test")], root) wasm = run_json(["node", str(root / "build-wasm/sim_kinematics_feedback_wasm_test.js")], root) report = { "schema": "kinematics-feedback-report.v1", "status": "partial", "implementation_status": "verified", "source_records": {name: {"path": path, "sha256": sha256(root / path)} for name, path in INPUTS.items()}, "pipeline": {"order": ["actual_joint_feedback", "forward_kinematics", "pose_flags_commit"], "last_valid_pose_policy": "retain on error", "state_fields": ["pose", "joints", "forward_flags", "inverse_flags", "tick", "valid", "last_error", "kinematics_error"]}, "configuration_candidates": [{"id": "trivkins-xyz-feedback", "kind": "derived", "path": INPUTS["test"], "coordinates": "XYZ"}, {"id": "inverse-only-feedback-rejection", "kind": "derived", "path": INPUTS["test"], "coordinates": "XYZBC", "purpose": "forward feedback rejects unsupported mode without publishing a pose"}], "boundaries": {"stale_read": native["stale_read"], "nonfinite": native["nonfinite"], "joint_count": native["count_error"], "forward_failure": native["singular"]["status"], "forward_failure_kinematics": native["singular"]["kinematics_error"], "failed_feedback_valid": native["singular"]["valid"], "state_hash_stable": native["nominal"]["state_hash"] == native["nominal"]["repeat_hash"]}, "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["nominal"]["state_hash"] == wasm["nominal"]["state_hash"]}, "native_wasm": {"status": "passed", "native_command": "build-native/sim_kinematics_feedback_test", "wasm_command": "node build-wasm/sim_kinematics_feedback_wasm_test.js"}, "oracle": {"status": "blocked", "reason": "linuxcnc/unit_tests/kinematics is absent from the locked source", "blocked_samples": ["nominal FK feedback", "stale read", "nonfinite feedback", "joint count mismatch", "unsupported forward", "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-feedback.report.json")) args = parser.parse_args() report = generate(args.repo_root, args.repo_root / args.output) print(f"kinematics_feedback={report['status']} native_wasm={report['native_wasm']['status']} oracle={report['oracle']['status']}") return 0 if __name__ == "__main__": raise SystemExit(main())