#!/usr/bin/env python3 """Generate deterministic evidence for spindle, tool changer and probe IO.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path INPUTS = { "io_header": "wasm-core/include/sim_io.h", "io_source": "wasm-core/src/sim_io.c", "hal_header": "wasm-core/include/sim_hal.h", "hal_source": "wasm-core/src/sim_hal.c", "test": "wasm-core/tests/sim_io_test.c", "linuxcnc_motion": "linuxcnc/src/emc/motion/motion.c", "linuxcnc_control": "linuxcnc/src/emc/motion/control.c", "linuxcnc_iocontrol": "linuxcnc/src/emc/iotask/ioControl.cc", "linuxcnc_iocontrol_v2": "linuxcnc/src/emc/iotask/ioControl_v2.cc", "linuxcnc_sim_spindle": "linuxcnc/src/hal/components/sim_spindle.comp", } 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") == "io-samples.v1": return item raise RuntimeError("runner did not emit io-samples.v1") def canonical(value: object) -> str: return json.dumps(value, sort_keys=True, separators=(",", ":")) def generate(root: Path, output: Path) -> dict[str, object]: root = root.resolve() native = run_json([str(root / "build-native/sim_io_test")], root) wasm = run_json(["node", str(root / "build-wasm/sim_io_wasm_test.js")], root) report = { "schema": "io-runtime-report.v1", "status": "partial", "implementation_status": "verified", "source_records": {name: {"path": path, "sha256": sha256(root / path)} for name, path in INPUTS.items()}, "registration": { "spindle_capacity": 8, "tool_pocket_capacity": 1001, "fixed_tick": True, "hal_pins": ["spindle.0.*", "spindle.1.*", "iocontrol.0.tool-*", "motion.probe-input"], "scheduler": "sim-io-servo", }, "pipeline": { "spindle": ["command", "inhibit_or_fault_gate", "acceleration_limit", "at_speed_and_brake", "revs_and_index"], "tool": ["prepare", "prepared_ack", "spindle_stopped_gate", "change", "changed_ack", "abort_or_fault"], "probe": ["arm", "input_edge", "forward_pose_latch", "trip_or_disarm"], "kinematics_association": "probe latch consumes the motion feedback pose; tool change requires stopped spindle; estop cancels motion-facing IO", }, "boundaries": { "invalid_pin_type": native["pin"]["invalid_type"], "spindle_limit": native["spindle"]["limit"], "tool_change_while_spinning": native["spindle"]["tool_busy"], "probe_trigger": native["probe"]["status"], "probe_state": native["probe"]["state"], "estop_command": native["estop"]["command"], "estop_recovery_before_clear": native["estop"]["recover_before_clear"], "recovery": native["estop"]["recover"], "fault_clear_while_asserted": native["tool"]["fault_clear"], "state_hash_stable": native["state_hash"] == native["repeat_hash"], }, "samples": { "native": native, "wasm": wasm, "byte_identical_json": canonical(native) == canonical(wasm), "state_hash_identical": native["state_hash"] == wasm["state_hash"] and native["repeat_hash"] == wasm["repeat_hash"], }, "native_wasm": { "status": "passed", "native_command": "build-native/sim_io_test", "wasm_command": "node build-wasm/sim_io_wasm_test.js", }, "oracle": { "status": "blocked", "reason": "locked LinuxCNC source has no linuxcnc/unit_tests/kinematics oracle root and no browser IO oracle", "blocked_samples": ["spindle acceleration/at-speed", "spindle orient", "tool prepare/change", "tool abort/fault/recovery", "probe pose latch", "estop and recovery"], }, } report["report_sha256"] = hashlib.sha256(canonical(report).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/io-runtime.report.json")) args = parser.parse_args() report = generate(args.repo_root, args.repo_root / args.output) print(f"io_runtime={report['status']} native_wasm={report['native_wasm']['status']} oracle={report['oracle']['status']}") return 0 if __name__ == "__main__": raise SystemExit(main())