#!/usr/bin/env python3 """Generate deterministic native/WASM evidence for linear and rotary Delta.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path SOURCES = { "linear_common": "linuxcnc/src/emc/kinematics/lineardeltakins-common.h", "linear_c": "linuxcnc/src/emc/kinematics/lineardeltakins.c", "linear_cc": "linuxcnc/src/emc/kinematics/lineardeltakins.cc", "rotary_common": "linuxcnc/src/emc/kinematics/rotarydeltakins-common.h", "rotary_c": "linuxcnc/src/emc/kinematics/rotarydeltakins.c", "rotary_cc": "linuxcnc/src/emc/kinematics/rotarydeltakins.cc", "implementation": "wasm-core/src/sim_kinematics.c", "header": "wasm-core/include/sim_kinematics.h", "test": "wasm-core/tests/sim_kinematics_test.c", } CONFIGS = { "linear_axis": "linuxcnc/configs/sim/axis/ldelta.ini", "linear_demo": "linuxcnc/configs/sim/axis/ldelta_demo.ini", "linear_tklinuxcnc": "linuxcnc/configs/sim/tklinuxcnc/ldelta.ini", "rotary_axis": "linuxcnc/configs/sim/axis/rdelta.ini", } def sha256(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def run_json(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.lstrip().startswith("{"): return json.loads(line) raise RuntimeError(f"{command[0]} did not emit a JSON sample") def generate(root: Path, output: Path) -> dict[str, object]: root = root.resolve() native = run_json([str(root / "build-native/sim_kinematics_test")], root) wasm = run_json(["node", str(root / "build-wasm/sim_kinematics_wasm_test.js")], root) native_delta = {key: native[key] for key in ("lineardelta", "rotarydelta", "delta_edges")} wasm_delta = {key: wasm[key] for key in ("lineardelta", "rotarydelta", "delta_edges")} native_text = json.dumps(native_delta, sort_keys=True, separators=(",", ":")) wasm_text = json.dumps(wasm_delta, sort_keys=True, separators=(",", ":")) source_records = {name: {"path": path, "sha256": sha256(root / path)} for name, path in SOURCES.items()} config_records = {} missing_hal = [] for name, path in CONFIGS.items(): text = (root / path).read_text(encoding="utf-8") hal_refs = [line.split("=", 1)[1].strip() for line in text.splitlines() if line.strip().startswith("HALFILE") and "=" in line] missing = [str((Path(path).parent / ref).as_posix()) for ref in hal_refs if not (root / Path(path).parent / ref).is_file()] missing_hal.extend(missing) config_records[name] = { "path": path, "sha256": sha256(root / path), "kinematics": "rotarydeltakins" if "rdelta" in path else "lineardeltakins", "hal_files": hal_refs, "missing_hal_files": sorted(missing), } edges = native_delta["delta_edges"] report = { "schema": "delta-kinematics-report.v1", "status": "partial", "implementation_status": "verified", "source_records": source_records, "configuration_candidates": config_records, "configuration_candidate_count": len(config_records), "geometry": { "linear": {"radius": 130.25, "rod_length": 269.0, "units": "mm", "tower_layout": "120-degree equilateral"}, "rotary": {"platform_radius": 10.0, "thigh_length": 10.0, "shin_length": 14.0, "foot_radius": 6.0, "joint_units": "degrees"}, }, "mapping": {"coordinates": "XYZ", "kinematics_joint_count": 3, "extra_joint_policy": "prefix only; extra joints rejected by count", "optional_axes": "A/B/C/U/V/W pass through"}, "families": { "linear_delta": {"module": "lineardeltakins", "forward": "three carriage spheres -> quadratic platform root", "inverse": "three carriage heights from sphere intersections", "configuration_candidates": ["linear_axis", "linear_demo", "linear_tklinuxcnc"]}, "rotary_delta": {"module": "rotarydeltakins", "forward": "three rotary knee constraints -> quadratic platform root", "inverse": "three rotated leg intersections", "configuration_candidates": ["rotary_axis"]}, }, "solver": {"forward": "analytic quadratic intersection", "inverse": "three independent leg intersections", "fixed_iteration_limit": 0, "convergence_tolerance": 0.0, "default_root": 0, "alternate_root_flag": "SIM_KIN_DELTA_ALTERNATE_ROOT bit 0"}, "workspace_and_boundaries": { "linear": {"nominal": native_delta["lineardelta"]["pose"], "alternate_root_pose": edges["lineardelta"]["alternate_pose"], "no_solution_error": edges["lineardelta"]["no_solution"], "joint_limit_error": edges["lineardelta"]["limit_error"], "geometry_error": edges["lineardelta"]["geometry_error"], "root_branch": edges["lineardelta"]["branch"], "alternate_branch": edges["lineardelta"]["alternate_branch"], "iterations": edges["lineardelta"]["iterations"]}, "rotary": {"nominal": native_delta["rotarydelta"]["pose"], "alternate_root_pose": edges["rotarydelta"]["alternate_pose"], "singular_error": edges["rotarydelta"]["singular"], "geometry_error": edges["rotarydelta"]["geometry_error"], "root_branch": edges["rotarydelta"]["branch"], "alternate_branch": edges["rotarydelta"]["alternate_branch"], "iterations": edges["rotarydelta"]["iterations"]}, }, "samples": {"native": native_delta, "wasm": wasm_delta, "byte_identical_json": native_text == wasm_text, "state_hash_identical": native_delta["lineardelta"]["state_hash"] == wasm_delta["lineardelta"]["state_hash"] and native_delta["rotarydelta"]["state_hash"] == wasm_delta["rotarydelta"]["state_hash"]}, "native_wasm": {"status": "passed", "native_command": "build-native/sim_kinematics_test", "wasm_command": "node build-wasm/sim_kinematics_wasm_test.js"}, "oracle": {"status": "blocked", "reason": "linuxcnc/unit_tests/kinematics is absent from the locked source", "blocked_samples": ["linear nominal/root", "linear alternate root", "linear no-solution", "linear joint-limit", "rotary nominal/root", "rotary alternate root", "rotary z=0 singular"]}, "resource_gaps": {"missing_hal_files": sorted(set(missing_hal)), "policy": "retained as missing inputs; no configuration smoke is promoted to numeric golden"}, } 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/delta-kinematics.report.json")) args = parser.parse_args() report = generate(args.repo_root, args.repo_root / args.output) print(f"delta_kinematics={report['status']} native_wasm={report['native_wasm']['status']} oracle={report['oracle']['status']}") return 0 if __name__ == "__main__": raise SystemExit(main())