111 lines
5.9 KiB
Python
111 lines
5.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate deterministic native/WASM evidence for switchkins integration."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import re
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
|
|
INPUTS = {
|
|
"switch_source": "linuxcnc/src/emc/kinematics/switchkins.c",
|
|
"switch_header": "linuxcnc/src/emc/kinematics/switchkins.h",
|
|
"module_source": "linuxcnc/src/emc/kinematics/xyzac-trt-kins.c",
|
|
"userk_source": "linuxcnc/src/emc/kinematics/userkfuncs.c",
|
|
"config": "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini",
|
|
"postgui_hal": "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/switchkins_postgui.hal",
|
|
"implementation": "wasm-core/src/sim_switchkins.c",
|
|
"kinematics_header": "wasm-core/include/sim_kinematics.h",
|
|
"kinematics_implementation": "wasm-core/src/sim_kinematics.c",
|
|
"test": "wasm-core/tests/sim_switchkins_test.c",
|
|
}
|
|
|
|
|
|
def sha256(path: Path) -> str:
|
|
return hashlib.sha256(path.read_bytes()).hexdigest()
|
|
|
|
|
|
def run_json(command: list[str], root: Path, schema: str) -> 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") == schema:
|
|
return item
|
|
raise RuntimeError(f"runner did not emit {schema}")
|
|
|
|
|
|
def parse_config(path: Path) -> dict[str, object]:
|
|
text = path.read_text(encoding="utf-8")
|
|
match = re.search(r"^KINEMATICS\s*=\s*([^\s]+)(?:\s+sparm=([^\s]+))?", text, re.MULTILINE)
|
|
joints = re.search(r"^\s*JOINTS\s*=\s*(\d+)", text, re.MULTILINE)
|
|
coords = re.search(r"^\s*COORDINATES\s*=\s*([^\s]+)", text, re.MULTILINE)
|
|
return {
|
|
"path": INPUTS["config"],
|
|
"kinematics": match.group(1) if match else None,
|
|
"sparm": match.group(2) if match and match.group(2) else None,
|
|
"joints": int(joints.group(1)) if joints else None,
|
|
"coordinates": coords.group(1) if coords else None,
|
|
"identityfirst": bool(match and match.group(2) and "identityfirst" in match.group(2)),
|
|
"switch_pin": "motion.switchkins-type" in text,
|
|
"modes": {"0": "identity", "1": "xyzac-trt", "2": "userk"},
|
|
}
|
|
|
|
|
|
def generate(root: Path, output: Path) -> dict[str, object]:
|
|
root = root.resolve()
|
|
native = run_json([str(root / "build-native/sim_switchkins_test")], root, "switchkins-samples.v1")
|
|
wasm = run_json(["node", str(root / "build-wasm/sim_switchkins_wasm_test.js")], root, "switchkins-samples.v1")
|
|
config = parse_config(root / INPUTS["config"])
|
|
report = {
|
|
"schema": "switchkins-kinematics-report.v1",
|
|
"status": "partial",
|
|
"implementation_status": "verified",
|
|
"source_records": {name: {"path": path, "sha256": sha256(root / path)} for name, path in INPUTS.items()},
|
|
"configuration_candidates": [
|
|
{**config, "id": "shipped-xyzac-trt-identityfirst", "kind": "real", "provenance": "shipped LinuxCNC simulation"},
|
|
{"id": "mode-transition-pose-continuity", "kind": "derived", "path": INPUTS["test"], "modes": ["identity", "xyzac-trt"], "purpose": "old-mode forward pose to new-mode inverse remap"},
|
|
{"id": "userk-slot-missing-module", "kind": "derived", "path": INPUTS["test"], "modes": ["userk"], "purpose": "mode 2 remains fail-closed until static userk is registered"},
|
|
],
|
|
"mode_registry": {"slots": [0, 1, 2], "names": {"0": "identity", "1": "xyzac-trt", "2": "userk"}, "startup_mode": 0, "identityfirst": config["identityfirst"]},
|
|
"state_and_errors": {
|
|
"pose_continuity": {"identity_to_trt": True, "trt_to_identity": True},
|
|
"state_hash_stable": native["returned_hash"] == native["returned_repeat_hash"] and native["returned_hash"] != 0,
|
|
"invalid_mode_error": -707,
|
|
"missing_module_error": -708,
|
|
"switch_interrupted_error": -709,
|
|
"abort_restores_active_mode": native["active_mode"] == 0,
|
|
},
|
|
"samples": {
|
|
"native": native,
|
|
"wasm": wasm,
|
|
"byte_identical_json": json.dumps(native, sort_keys=True, separators=(",", ":")) == json.dumps(wasm, sort_keys=True, separators=(",", ":")),
|
|
"state_hash_identical": [native[key] for key in ("initial_hash", "switched_hash", "returned_hash", "returned_repeat_hash")] == [wasm[key] for key in ("initial_hash", "switched_hash", "returned_hash", "returned_repeat_hash")],
|
|
},
|
|
"native_wasm": {"status": "passed", "native_command": "build-native/sim_switchkins_test", "wasm_command": "node build-wasm/sim_switchkins_wasm_test.js"},
|
|
"oracle": {"status": "blocked", "reason": "linuxcnc/unit_tests/kinematics is absent from the locked source", "blocked_samples": ["type 0 identity", "type 1 xyzac-trt", "type 2 userk", "invalid mode", "missing module", "interrupted switch", "abort recovery"]},
|
|
}
|
|
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/switchkins-kinematics.report.json"))
|
|
args = parser.parse_args()
|
|
report = generate(args.repo_root, args.repo_root / args.output)
|
|
print(f"switchkins={report['status']} native_wasm={report['native_wasm']['status']} oracle={report['oracle']['status']}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|