Files
linuxcncWebWASM/scripts/generate_coupled_kinematics_report.py

112 lines
5.6 KiB
Python

#!/usr/bin/env python3
"""Generate native/WASM evidence for the CoreXY, Rose, MaxKins and RotateKins slice."""
from __future__ import annotations
import argparse
import hashlib
import json
import subprocess
from pathlib import Path
SOURCES = {
"corexy": "linuxcnc/src/emc/kinematics/corexykins.c",
"rose": "linuxcnc/src/emc/kinematics/rosekins.c",
"maxkins": "linuxcnc/src/emc/kinematics/maxkins.c",
"rotatekins": "linuxcnc/src/emc/kinematics/rotatekins.c",
}
CONFIGS = {
"corexy_kins": "linuxcnc/configs/sim/axis/corexy/corexy_by_kins.ini",
"corexy_hal": "linuxcnc/configs/sim/axis/corexy/corexy_by_hal.ini",
"rose": "linuxcnc/configs/sim/axis/rose_engine/rose_engine.ini",
"maxkins": "linuxcnc/configs/sim/axis/vismach/5axis/max5/max5kins.ini",
"rotatekins": "linuxcnc/configs/sim/axis/rotate/rotatekins.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()):
line = line.strip()
if line.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()
closure = json.loads((root / "coverage/kinematics-source-closure.report.json").read_text(encoding="utf-8"))
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_text = json.dumps(native, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
wasm_text = json.dumps(wasm, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
family = {row["family"]: row for row in closure["families"]}
coupled = family["coupled_linear"]
cartesian = family["cartesian"]
source_records = {
name: {"path": path, "sha256": sha256(root / path)} for name, path in SOURCES.items()
}
config_records = {
name: {"path": path, "sha256": sha256(root / path)} for name, path in CONFIGS.items()
}
report = {
"schema": "coupled-kinematics-report.v1",
"context_id": closure["context_id"],
"status": "partial",
"implementation_status": "verified",
"source_records": source_records,
"configuration_candidates": config_records,
"configuration_candidate_origin": {"rotatekins": "workspace_derived_fixture; LinuxCNC locked source has no shipped rotatekins config"},
"families": {
"corexy": {"forward": "X=0.5*(J0+J1), Y=0.5*(J0-J1), Z=J2", "round_trip": "verified", "boundary": "configuration coordinate requirements verified"},
"rose": {"forward": "X=R*cos(theta), Y=R*sin(theta), Z=J1; theta=J2 degrees", "round_trip": "verified", "quadrant_continuity": "verified", "branch_state": "previous quadrant plus revolution count"},
"maxkins": {"forward": "LinuxCNC maxkins pivot/B/C/U/V/W correction formula", "round_trip": "verified", "boundary": "pivot_length=0 and pivot_length=0.666 verified"},
"rotatekins": {"forward": "XY rotation by -C degrees; Z and optional axes direct", "round_trip": "verified", "boundary": "90 degree rotation verified"},
},
"samples": {"native": native, "wasm": wasm, "byte_identical_json": native_text == wasm_text},
"state_hash": {
"native": {name: native[name]["state_hash"] for name in ("corexy", "rose", "maxkins", "rotatekins")},
"wasm": {name: wasm[name]["state_hash"] for name in ("corexy", "rose", "maxkins", "rotatekins")},
"stable": native_text == wasm_text,
},
"corexy_entry_distinction": {
"kins_configuration": CONFIGS["corexy_kins"],
"hal_configuration": CONFIGS["corexy_hal"],
"hal_component": "linuxcnc/src/hal/components/corexy_by_hal.comp",
"distinct": True,
},
"closure": {
"coupled_linear_source_ids": coupled["source_ids"],
"max_rotate_source_ids": [source_records["maxkins"]["path"], source_records["rotatekins"]["path"]],
"official_oracle": "blocked",
"blocking_reason": "linuxcnc/unit_tests/kinematics is absent from the locked source",
"cartesian_candidate_ids": cartesian["configuration_ids"][:8],
},
"native_wasm": {"native_command": "build-native/sim_kinematics_test", "wasm_command": "node build-wasm/sim_kinematics_wasm_test.js", "status": "passed"},
}
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/coupled-kinematics.report.json"))
args = parser.parse_args()
report = generate(args.repo_root, args.repo_root / args.output)
print(f"coupled_kinematics={report['status']} native_wasm={report['native_wasm']['status']} oracle={report['closure']['official_oracle']}")
return 0
if __name__ == "__main__":
raise SystemExit(main())