Files
linuxcncWebWASM/scripts/generate_linuxcnc_source_io_s5.py

99 lines
5.9 KiB
Python

#!/usr/bin/env python3
"""Generate deterministic S5-06 IO/startup/tool/limit 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_COMMAND = ("node", "web/worker/test_source_io_s5.mjs")
PAIRS = {
"sim_io": (("build-native/sim_io_test",), ("node", "build-wasm/sim_io_wasm_test.js")),
"sim_device_io": (("build-native/sim_device_io_test",), ("node", "build-wasm/sim_device_io_wasm_test.js")),
}
CASE_IDS = {
"source-test:linuxcnc/tests/hard-limits/test.sh",
"source-test:linuxcnc/tests/limit3/limit-max-velocity/test.hal",
"source-test:linuxcnc/tests/toolchanger/m61/test.sh",
"source-test:linuxcnc/tests/io-startup/nonrandom/no-tool-in-p0/test.sh",
"source-test:linuxcnc/tests/io-startup/nonrandom/tool-in-p0/test.sh",
"source-test:linuxcnc/tests/motion-logger/startup-gcode-abort/test.sh",
"source-test:linuxcnc/tests/classicladder/estop/test.sh",
}
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 command(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), "status": "PASSED" if result.returncode == 0 else "FAILED", "returncode": result.returncode, "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_json(result: dict[str, Any]) -> dict[str, Any] | None:
try:
return json.loads(result["stdout"])
except json.JSONDecodeError:
return None
def generate(root: Path, output: Path) -> dict[str, Any]:
worker = command(root, WORKER_COMMAND)
worker_json = parse_json(worker)
cases = worker_json.get("cases", []) if worker_json else []
fixture_rows = [file for item in cases for file in item.get("fixture", [])]
fixture_ok = all(isinstance(item, dict) and isinstance(item.get("path"), str) and (root / item["path"]).is_file() and (root / item["path"]).stat().st_size == item.get("bytes") and digest((root / item["path"]).read_bytes()) == item.get("sha256") for item in fixture_rows)
native_wasm: dict[str, Any] = {}
pairs_equal = True
for name, (native_command, wasm_command) in PAIRS.items():
native = command(root, native_command)
wasm = command(root, wasm_command)
equal = native["returncode"] == 0 and wasm["returncode"] == 0 and native["stdout"] == wasm["stdout"]
pairs_equal = pairs_equal and equal
native_wasm[name] = {"native": {key: value for key, value in native.items() if key not in {"stdout", "stderr"}}, "wasm": {key: value for key, value in wasm.items() if key not in {"stdout", "stderr"}}, "golden_diff": {"status": "PASSED" if equal else "FAILED", "byte_identical": equal, "native_stdout_sha256": native["stdout_sha256"], "wasm_stdout_sha256": wasm["stdout_sha256"], "first_difference": None if equal else f"{name} stdout differs"}}
ctest = command(root, ("ctest", "--test-dir", "build-native", "-R", "^(sim_io_test|sim_device_io_test)$", "--output-on-failure"))
case_ids = {item.get("id") for item in cases}
adapter_ok = worker["returncode"] == 0 and worker_json is not None and case_ids == CASE_IDS and fixture_ok and pairs_equal and ctest["returncode"] == 0
report: dict[str, Any] = {
"schema": "linuxcnc-source-io-execution.v1",
"task_id": "S5-06",
"context_id": CONTEXT_ID,
"status": "verified" if adapter_ok else "blocked",
"scope": ["IO", "startup tool table", "toolchanger ack", "hard limits", "limit3", "motion abort", "ClassicLadder boundary"],
"native_wasm": native_wasm,
"ctest": {key: value for key, value in ctest.items() if key not in {"stdout", "stderr"}},
"worker": {"execution": {key: value for key, value in worker.items() if key not in {"stdout", "stderr"}}, "golden": worker_json, "fixture_hashes_verified": fixture_ok},
"counts": {"selected_cases": len(cases), "fixture_files": len({item.get("path") for item in fixture_rows}), **(worker_json.get("counts", {}) if worker_json else {})},
"expected_checkresult_comparison": {"status": "SOURCE_BLOCKED", "compared_byte_for_byte": False, "fixture_hashes_verified": fixture_ok, "reason": "source IO/startup/tool expected/checkresult files are retained and hashed; host GUI/configuration oracle is unavailable to the Worker adapter"},
"boundaries": ["startup tool table resolution remains SOURCE_BLOCKED until full Task/configuration startup is available", "motion logger abort remains WORKER_TASK_MOTION_PENDING", "ClassicLadder and realtime hardware paths remain HOST_EXCLUDED", "native/WASM IO and device outputs are byte-identical for static fixtures"],
}
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/linuxcnc-source-io-s5.report.json"))
args = parser.parse_args()
report = generate(args.repo_root.resolve(), args.repo_root.resolve() / args.report)
print(f"linuxcnc_source_io_s5={report['status']} cases={report['counts']['selected_cases']} fixtures={report['counts']['fixture_files']}")
return 0 if report["status"] == "verified" else 1
if __name__ == "__main__":
raise SystemExit(main())