Files
linuxcncWebWASM/scripts/generate_authoritative_kinematics_s7_03.py

92 lines
6.1 KiB
Python

#!/usr/bin/env python3
"""Generate deterministic S7-03 authoritative linear Delta evidence."""
from __future__ import annotations
import argparse
import hashlib
import json
import subprocess
from pathlib import Path
from typing import Any
CONTEXT_ID = "linuxcnc-2.10.0-pre0-wasm-m1-py-c1"
RUNNER = ("node", "web/worker/test_authoritative_kinematics_s7_03.mjs")
PAIRS = {"kinematics": (("build-native/sim_kinematics_test",), ("node", "build-wasm/sim_kinematics_wasm_test.js")), "kinematics_motion": (("build-native/sim_kinematics_motion_test",), ("node", "build-wasm/sim_kinematics_motion_wasm_test.js"))}
INPUTS = (
"coverage/simulation-alignment-s7.report.json",
"coverage/delta-kinematics.report.json",
"coverage/kinematics-registry.report.json",
"coverage/source-g6164-s7-02.report.json",
"linuxcnc/configs/sim/axis/ldelta.ini",
"linuxcnc/src/emc/kinematics/lineardeltakins.c",
"wasm-core/src/sim_kinematics.c",
"web/worker/authoritative-runtime.mjs",
"web/worker/sim-session.mjs",
"web/worker/test_authoritative_kinematics_s7_03.mjs",
)
def canonical(value: Any) -> str: return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
def digest(value: bytes) -> str: return hashlib.sha256(value).hexdigest()
def artifact(root: Path, path: str) -> dict[str, Any]:
value = root / path
return {"path": path, "status": "missing"} if not value.is_file() else {"path": path, "bytes": value.stat().st_size, "sha256": digest(value.read_bytes())}
def run(root: Path, command: tuple[str, ...]) -> dict[str, Any]:
result = subprocess.run(command, cwd=root, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)
return {"command": list(command), "returncode": result.returncode, "status": "PASSED" if result.returncode == 0 else "FAILED", "stdout_sha256": digest(result.stdout), "stderr_sha256": digest(result.stderr), "stdout": result.stdout.decode("utf-8", errors="replace"), "stderr": result.stderr.decode("utf-8", errors="replace")}
def parse(result: dict[str, Any]) -> dict[str, Any] | None:
for line in reversed(result["stdout"].splitlines()):
if line.lstrip().startswith("{"):
try: return json.loads(line)
except json.JSONDecodeError: pass
return None
def no_output(result: dict[str, Any]) -> dict[str, Any]: return {key: value for key, value in result.items() if key not in {"stdout", "stderr"}}
def generate(root: Path, output: Path, profile: dict[str, Any] | None = None) -> dict[str, Any]:
profile = profile or {}
runner = profile.get("runner", RUNNER)
supported_type = profile.get("supported_type", "lineardelta")
worker_result = run(root, runner); worker = parse(worker_result)
native_wasm = {}; pair_ok = True
for name, (native_command, wasm_command) in PAIRS.items():
native = run(root, native_command); wasm = run(root, wasm_command)
equal = native["returncode"] == 0 and wasm["returncode"] == 0 and native["stdout"] == wasm["stdout"]
pair_ok = pair_ok and equal
native_wasm[name] = {"native": no_output(native), "wasm": no_output(wasm), "golden_diff": {"status": "PASSED" if equal else "FAILED", "byte_identical": equal, "native_stdout_sha256": native["stdout_sha256"], "wasm_stdout_sha256": wasm["stdout_sha256"]}}
ctest = run(root, ("ctest", "--test-dir", "build-native", "-R", "^(sim_kinematics_test|sim_kinematics_motion_test)$", "--output-on-failure"))
worker_ok = worker_result["returncode"] == 0 and worker and worker.get("status") == "verified" and worker.get("supported", {}).get("type") == supported_type and worker.get("parity", {}).get("mode", {}).get("equal") and worker.get("parity", {}).get("recovery", {}).get("equal")
report: dict[str, Any] = {
"schema": "authoritative-kinematics-s7-03.v1",
"task_id": "S7-03",
"context_id": CONTEXT_ID,
"status": "verified" if worker_ok and pair_ok and ctest["returncode"] == 0 else "blocked",
"scope": {"selected_family": "parallel_linear_delta", "kinematics": "lineardelta", "source_module": "lineardeltakins", "pose": [0, 0, 64.636584194], "joint_count": 3, "geometry": {"radius": 130.25, "rod_length": 269, "tower_layout": "120-degree equilateral"}, "unsupported_error": "authoritative_kinematics_unsupported", "range_error": "authoritative_kinematics_range"},
"worker": {"execution": no_output(worker_result), "sample": worker},
"native_wasm": native_wasm,
"ctest": no_output(ctest),
"inputs": [artifact(root, path) for path in INPUTS],
"selection": {"basis": ["delta-kinematics report has real lineardelta configuration candidates and passed native/WASM samples", "lineardelta preserves the existing authoritative XYZ plus three-joint shape", "no ABI/protocol/schema expansion is required"], "not_promoted": ["TRT", "serial_robot", "rotary_delta", "Hexapod", "Pentapod", "switch", "user", "official_oracle"]},
"boundaries": ["official kinematics oracle remains SOURCE_BLOCKED", "no-solution mapping fails closed as authoritative_kinematics_range", "missing ldelta HAL resources remain configuration gaps", "TRT/robot/Hexapod/Pentapod/switch/user remain queued"],
}
report["report_sha256"] = hashlib.sha256(canonical({key: value for key, value in report.items() if key != "report_sha256"}).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("--report", type=Path, default=Path("coverage/authoritative-kinematics-s7-03.report.json")); args = parser.parse_args(); root = args.repo_root.resolve(); output = args.report if args.report.is_absolute() else root / args.report; report = generate(root, output); print(f"authoritative_kinematics_s7_03={report['status']} family={report['scope']['selected_family']}"); return 0 if report["status"] == "verified" else 1
if __name__ == "__main__": raise SystemExit(main())