#!/usr/bin/env python3 """Generate deterministic S5-05 HAL/.comp source adapter 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_hal_s5.mjs") NATIVE_COMMAND = ("build-native/sim_hal_test",) WASM_COMMAND = ("node", "build-wasm/sim_hal_wasm_test.js") CASE_IDS = { "source-test:linuxcnc/tests/abs.0/test.hal", "source-test:linuxcnc/tests/edge/rising/test.hal", "source-test:linuxcnc/tests/edge/rising-starting-high/test.hal", "source-test:linuxcnc/tests/halcompile/names/test.sh", "source-test:linuxcnc/tests/halcompile/relative-header/test.sh", "source-test:linuxcnc/tests/halcompile/userspace-count-names/test.sh", "source-test:linuxcnc/tests/symbols.0/test.sh", "source-test:linuxcnc/tests/symbols.1/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]: native = command(root, NATIVE_COMMAND) wasm = command(root, WASM_COMMAND) worker = command(root, WORKER_COMMAND) ctest = command(root, ("ctest", "--test-dir", "build-native", "-R", "^sim_hal_test$", "--output-on-failure")) worker_json = parse_json(worker) cases = worker_json.get("cases", []) if worker_json else [] case_ids = {item.get("id") for item in cases} 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 ) golden_equal = native["returncode"] == 0 and wasm["returncode"] == 0 and native["stdout"] == wasm["stdout"] worker_ok = worker["returncode"] == 0 and worker_json is not None and case_ids == CASE_IDS and fixture_ok adapter_ok = golden_equal and worker_ok and ctest["returncode"] == 0 report: dict[str, Any] = { "schema": "linuxcnc-source-hal-execution.v1", "task_id": "S5-05", "context_id": CONTEXT_ID, "status": "verified" if adapter_ok else "blocked", "scope": ["HAL regression/functional", "static SimHAL components", ".comp declaration preflight"], "native_wasm": { "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 golden_equal else "FAILED", "byte_identical": golden_equal, "native_stdout_sha256": native["stdout_sha256"], "wasm_stdout_sha256": wasm["stdout_sha256"], "first_difference": None if golden_equal else "sim_hal_test stdout differs", }, }, "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 HAL expected/checkresult files are retained and hashed; host halrun/sampler logs are unavailable to this static Worker adapter", }, "boundaries": [ "sampler/streamer/loadusr and realtime compiler paths remain explicit host/resource boundaries", "userspace and dynamic .comp execution is HOST_EXCLUDED; custom component compile and relative headers are SOURCE_BLOCKED", "native/WASM SimHAL golden is exact for the static component behavior exercised by the Worker 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-hal-s5.report.json")) args = parser.parse_args() report = generate(args.repo_root.resolve(), args.repo_root.resolve() / args.report) print(f"linuxcnc_source_hal_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())