Files
linuxcncWebWASM/scripts/generate_authoritative_sim_task_s6.py

105 lines
5.7 KiB
Python

#!/usr/bin/env python3
"""Generate S6-02 authoritative sim_task fixed-tick integration 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"
WORKER = ("node", "web/worker/test_authoritative_sim_task_s6.mjs")
NATIVE = {
"task": ("build-native/sim_task_test",),
"scheduler": ("build-native/sim_scheduler_test",),
"kinematics_motion": ("build-native/sim_kinematics_motion_test",),
"hal": ("build-native/sim_hal_test",),
"io": ("build-native/sim_io_test",),
}
WASM = {
"kinematics_motion": ("node", "build-wasm/sim_kinematics_motion_wasm_test.js"),
"hal": ("node", "build-wasm/sim_hal_wasm_test.js"),
"io": ("node", "build-wasm/sim_io_wasm_test.js"),
}
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 run(root: Path, args: tuple[str, ...]) -> dict[str, Any]:
result = subprocess.run(args, cwd=root, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)
return {"command": list(args), "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 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 parse(result: dict[str, Any]) -> dict[str, Any] | None:
try:
return json.loads(result["stdout"])
except json.JSONDecodeError:
return None
def artifact(root: Path, path: Path) -> dict[str, Any]:
value = root / path
return {"path": path.as_posix(), "bytes": value.stat().st_size, "sha256": digest(value.read_bytes())} if value.is_file() else {"path": path.as_posix(), "status": "missing"}
def generate(root: Path, output: Path) -> dict[str, Any]:
worker_result = run(root, WORKER)
worker = parse(worker_result)
native = {name: run(root, command) for name, command in NATIVE.items()}
wasm = {name: run(root, command) for name, command in WASM.items()}
ctest = run(root, ("ctest", "--test-dir", "build-native", "-R", "^(sim_task_test|sim_scheduler_test|sim_kinematics_motion_test|sim_hal_test|sim_io_test)$", "--output-on-failure"))
component_golden = {}
for name in WASM:
if name in NATIVE:
equal = native[name]["returncode"] == 0 and wasm[name]["returncode"] == 0 and native[name]["stdout"] == wasm[name]["stdout"]
component_golden[name] = {"status": "PASSED" if equal else "FAILED", "byte_identical": equal, "native_stdout_sha256": native[name]["stdout_sha256"], "wasm_stdout_sha256": wasm[name]["stdout_sha256"]}
worker_ok = worker_result["returncode"] == 0 and worker is not None and worker.get("status") == "verified" and all(worker.get("parity", {}).get(key, {}).get("equal") for key in ("mode", "one_x_recovery", "fast_recovery"))
native_ok = all(result["returncode"] == 0 for result in native.values())
wasm_ok = all(result["returncode"] == 0 for result in wasm.values())
report: dict[str, Any] = {
"schema": "authoritative-sim-task-s6.v1",
"task_id": "S6-02",
"context_id": CONTEXT_ID,
"status": "verified" if worker_ok and native_ok and wasm_ok and ctest["returncode"] == 0 and all(item["byte_identical"] for item in component_golden.values()) else "blocked",
"scope": {"kinematics": "trivkins", "axes": ["x", "y", "z"], "phases": worker.get("supported", {}).get("fixed_tick_phases", []) if worker else []},
"worker": {"execution": no_output(worker_result), "sample": worker},
"native": {name: no_output(result) for name, result in native.items()},
"wasm": {name: no_output(result) for name, result in wasm.items()},
"native_wasm_component_golden": component_golden,
"ctest": no_output(ctest),
"inputs": [artifact(root, Path("coverage/full-linuxcnc-simulation-gap-audit.report.json")), artifact(root, Path("coverage/sim-task.report.json")), artifact(root, Path("coverage/sim-scheduler.report.json")), artifact(root, Path("wasm-core/include/sim_kinematics_motion.h")), artifact(root, Path("web/worker/sim-session.mjs")), artifact(root, Path("web/worker/authoritative-runtime.mjs"))],
"boundaries": ["non-trivkins kinematics remain S6-03+", "full LinuxCNC canonical/native oracle remains explicitly blocked", "authoritative runtime state is now checkpointed and endpoint-compared for the supported XYZ/trivkins slice"],
}
report["report_sha256"] = digest(canonical({key: value for key, value in report.items() if key != "report_sha256"}).encode("utf-8"))
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-sim-task-s6.report.json"))
args = parser.parse_args()
report = generate(args.repo_root.resolve(), args.repo_root.resolve() / args.report)
print(f"authoritative_sim_task_s6={report['status']} kinematics={report['scope']['kinematics']} phase_count={len(report['scope']['phases'])}")
return 0 if report["status"] == "verified" else 1
if __name__ == "__main__":
raise SystemExit(main())