#!/usr/bin/env python3 """Generate deterministic evidence for the generalized serial DH slice.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path INPUTS = { "source": "linuxcnc/src/emc/kinematics/genserkins.c", "functions": "linuxcnc/src/emc/kinematics/genserfuncs.c", "melfa_config": "linuxcnc/configs/sim/axis/vismach/melfa-sim/melfa.ini", "melfa_dh": "linuxcnc/configs/sim/axis/vismach/melfa-sim/melfa_dh.hal", "puma_config": "linuxcnc/configs/sim/axis/vismach/puma/puma560.ini", "puma_dh": "linuxcnc/configs/sim/axis/vismach/puma/puma560_dh.hal", "implementation": "wasm-core/src/sim_kinematics.c", "header": "wasm-core/include/sim_kinematics.h", "test": "wasm-core/tests/sim_kinematics_test.c", } def sha256(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def run_sample(command: list[str], root: Path) -> dict[str, object]: completed = subprocess.run(command, cwd=root, check=False, text=True, capture_output=True) if completed.returncode != 0: raise RuntimeError(f"{command[0]} exited {completed.returncode}: {completed.stderr.strip()}") for line in reversed(completed.stdout.splitlines()): if line.startswith("{"): return json.loads(line) raise RuntimeError(f"{command[0]} did not emit a genser sample") def generate(root: Path, output: Path) -> dict[str, object]: root = root.resolve() native_all = run_sample([str(root / "build-native/sim_kinematics_test")], root) wasm_all = run_sample(["node", str(root / "build-wasm/sim_kinematics_wasm_test.js")], root) native = native_all["genser"] wasm = wasm_all["genser"] source_records = {name: {"path": path, "sha256": sha256(root / path)} for name, path in INPUTS.items()} melfa_config = (root / INPUTS["melfa_config"]).read_text(encoding="utf-8") melfa_dh = (root / INPUTS["melfa_dh"]).read_text(encoding="utf-8") puma_config = (root / INPUTS["puma_config"]).read_text(encoding="utf-8") puma_dh = (root / INPUTS["puma_dh"]).read_text(encoding="utf-8") native_json = json.dumps(native, ensure_ascii=False, sort_keys=True, separators=(",", ":")) wasm_json = json.dumps(wasm, ensure_ascii=False, sort_keys=True, separators=(",", ":")) report = { "schema": "genser-kinematics-report.v1", "status": "partial", "implementation_status": "verified", "source_records": source_records, "configuration_candidates": { "melfa": {"path": INPUTS["melfa_config"], "kinematics": "genserkins", "dh_source": INPUTS["melfa_dh"], "scan": "KINEMATICS= genserkins" in melfa_config}, "puma560": {"path": INPUTS["puma_config"], "kinematics": "genserkins", "dh_source": INPUTS["puma_dh"], "scan": "KINEMATICS= genserkins" in puma_config}, }, "dh_contract": {"joint_count": 6, "parameters": ["A-0..5", "ALPHA-0..5", "D-0..5"], "alpha_units": "radians from HAL pins", "joint_units": "degrees at public ABI, radians internally", "melfa_has_alpha_and_d": "ALPHA-1" in melfa_dh and "D-3" in melfa_dh, "puma_has_alpha_and_d": "ALPHA-1" in puma_dh and "D-3" in puma_dh}, "solver": {"forward": "Craig DH homogeneous product", "inverse": "fixed-zero seed damped numerical Jacobian with 1e-4 degree finite difference", "max_iterations": 200, "convergence_tolerance": 1e-7, "residual": native["residual"], "iterations": native["iterations"], "nonconvergence": "SIM_KIN_ERR_SINGULAR after fixed iteration limit"}, "samples": {"native": native, "wasm": wasm, "byte_identical_json": native_json == wasm_json}, "boundaries": {"fixed_iteration_limit": "verified", "state_hash": "native/WASM identical after 1e-9 quantization", "invalid_DH_nonfinite": "SIM_KIN_ERR_NONFINITE verified"}, "oracle": {"status": "blocked", "reason": "linuxcnc/unit_tests/kinematics is absent from the locked source"}, "native_wasm": {"status": "passed", "native_command": "build-native/sim_kinematics_test", "wasm_command": "node build-wasm/sim_kinematics_wasm_test.js"}, } report["report_sha256"] = hashlib.sha256(json.dumps(report, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode("utf-8")).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/genser-kinematics.report.json")) args = parser.parse_args() report = generate(args.repo_root, args.repo_root / args.output) print(f"genser={report['status']} native_wasm={report['native_wasm']['status']} oracle={report['oracle']['status']}") return 0 if __name__ == "__main__": raise SystemExit(main())