#!/usr/bin/env python3 """Generate the self-hashed RS-274/MDI report for the selected tutorial case.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path from typing import Any ROOT = Path(__file__).resolve().parents[1] REPORT = Path("coverage/axis3-tutorial-program.report.json") CASE_ROOT = Path("linuxcnc/configs/sim/axis/vismach/3axis-tutorial") INPUTS = ( CASE_ROOT / "3axis-tutorial.ini", CASE_ROOT / "3axis-tutorial.hal", Path("wasm-core/include/sim_interpreter.h"), Path("wasm-core/src/sim_interpreter.c"), Path("wasm-core/tests/sim_interpreter_test.c"), Path("web/worker/program-runner.mjs"), Path("web/worker/sim-session.mjs"), Path("web/worker/test_axis3_tutorial_program.mjs"), ) def canonical(value: Any) -> str: return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) def sha256(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def run_node() -> tuple[dict[str, Any] | None, dict[str, Any]]: result = subprocess.run( ["node", "web/worker/test_axis3_tutorial_program.mjs"], cwd=ROOT, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=False, ) payload = None for line in reversed(result.stdout.splitlines()): try: candidate = json.loads(line) except json.JSONDecodeError: continue if isinstance(candidate, dict) and candidate.get("schema") == "axis3-tutorial-program.v1": payload = candidate break return payload, { "status": "verified" if result.returncode == 0 and payload is not None else "blocked", "command": ["node", "web/worker/test_axis3_tutorial_program.mjs"], "returncode": result.returncode, "stdout_sha256": hashlib.sha256(result.stdout.encode()).hexdigest(), "stderr_sha256": hashlib.sha256(result.stderr.encode()).hexdigest(), } def run_native() -> dict[str, Any]: binary = ROOT / "build-native/sim_interpreter_test" if not binary.is_file(): return {"status": "blocked", "reason": "native_interpreter_binary_missing", "path": binary.as_posix()} result = subprocess.run([str(binary)], cwd=ROOT, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) return { "status": "verified" if result.returncode == 0 else "blocked", "path": "build-native/sim_interpreter_test", "returncode": result.returncode, "stdout_sha256": hashlib.sha256(result.stdout).hexdigest(), "stderr_sha256": hashlib.sha256(result.stderr).hexdigest(), } def generate(output: Path = REPORT) -> dict[str, Any]: node_payload, node = run_node() native = run_native() report: dict[str, Any] = { "schema": "axis3-tutorial-program-report.v1", "task_id": "TUTORIAL-05", "context_id": "linuxcnc-2.10.0-pre0-wasm-m1-py-c1", "case": "linuxcnc/configs/sim/axis/vismach/3axis-tutorial", "entrypoint": "linuxcnc/configs/sim/axis/vismach/3axis-tutorial/3axis-tutorial.ini", "status": "verified" if node["status"] == "verified" and native["status"] == "verified" else "blocked", "inputs": [{"path": path.as_posix(), "sha256": sha256(ROOT / path)} for path in INPUTS], "native_interpreter": native, "worker": node, "checks": { "rs274": { "units": "G21/mm", "distance_modes": ["G90/absolute", "G91/incremental"], "motion": ["G0", "G1"], "feed_words": ["F120", "F60"], "program_end": "M2", }, "program_control": ["PROGRAM_LOAD", "PROGRAM_RUN", "PROGRAM_PAUSE", "PROGRAM_RESUME", "PROGRAM_STEP", "PROGRAM_ABORT", "PROGRAM_RESTART"], "mdi": {"active_program_rejected": "mdi_while_program_active", "after_abort_recovery": "verified"}, "determinism": {"state_hash": node_payload.get("final_hash") if node_payload else None}, }, "observed": node_payload, "known_boundaries": [ "native evidence is the locked interpreter regression, not a realtime hardware oracle", "MDI is rejected while a program is paused/running and is accepted after abort/recovery", "full LinuxCNC canonical trajectory/oracle remains outside this case slice", ], } report["report_sha256"] = hashlib.sha256(canonical(report).encode()).hexdigest() destination = output if output.is_absolute() else ROOT / output destination.parent.mkdir(parents=True, exist_ok=True) destination.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() parser.add_argument("--report", type=Path, default=REPORT) args = parser.parse_args() report = generate(args.report) print(f"axis3_tutorial_program={report['status']} report_sha256={report['report_sha256']}") return 0 if report["status"] == "verified" else 1 if __name__ == "__main__": raise SystemExit(main())