#!/usr/bin/env python3 """Generate deterministic S2-02 static HAL component runtime evidence.""" from __future__ import annotations import hashlib import json import subprocess from pathlib import Path ROOT = Path(__file__).resolve().parents[1] OUTPUT = ROOT / "coverage/hal-s2.report.json" CONTEXT = ROOT / "context-lock.json" INPUTS = ( "wasm-core/include/sim_hal.h", "wasm-core/src/sim_hal.c", "wasm-core/tests/sim_hal_test.c", "web/worker/hal-runtime.mjs", "web/worker/test_hal_runtime.mjs", "linuxcnc/src/hal/components/corexy_by_hal.comp", "linuxcnc/src/hal/components/and2.comp", "linuxcnc/src/hal/components/constant.comp", ) def digest_bytes(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def canonical(value: object) -> bytes: return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode("utf-8") def run(command: list[str]) -> dict[str, object]: completed = subprocess.run(command, cwd=ROOT, text=True, capture_output=True, check=False) output = completed.stdout sample = next((json.loads(line) for line in reversed(output.splitlines()) if line.startswith("{")), None) return { "command": command, "returncode": completed.returncode, "status": "verified" if completed.returncode == 0 else "blocked", "stdout_sha256": hashlib.sha256(completed.stdout.encode()).hexdigest(), "stderr_sha256": hashlib.sha256(completed.stderr.encode()).hexdigest(), "sample": sample, } def generate(output: Path = OUTPUT) -> dict[str, object]: context = json.loads(CONTEXT.read_text(encoding="utf-8")) native = run([str(ROOT / "build-native/sim_hal_test")]) wasm = run(["node", str(ROOT / "build-wasm/sim_hal_wasm_test.js")]) worker = run(["node", str(ROOT / "web/worker/test_hal_runtime.mjs")]) native_sample = native["sample"] wasm_sample = wasm["sample"] report: dict[str, object] = { "schema": "hal-s2-report.v1", "task_id": "S2-02", "context_id": context["context_id"], "status": "verified" if all(item["status"] == "verified" for item in (native, wasm, worker)) and native_sample == wasm_sample else "blocked", "features": [ "static .comp descriptor registration", "batch pin/parameter/function execution", "persistent typed links", "fixed-period deterministic tick", "transactional tick rollback", "nonfinite/capacity/unsupported rejection", "Worker checkpoint and replay state", ], "supported_components": ["corexy_by_hal", "and2", "or2", "xor2", "not", "abs", "abs_s32", "constant", "lowpass", "ddt", "integ", "mux2", "sum2", "mult2", "div2", "hypot", "near", "comp", "invert", "edge", "toggle", "estop_latch"], "boundaries": ["dynamic .so/loadusr/host device access remains excluded", "unsupported .comp semantics fail closed"], "capacity": {"pins": 256, "params": 256, "components": 64, "threads": 16, "max_period_ticks": 1000000}, "inputs": [{"path": path, "sha256": digest_bytes(ROOT / path)} for path in INPUTS], "tests": {"native": native, "wasm": wasm, "worker": worker, "native_wasm_byte_identical": native_sample == wasm_sample}, "report_sha256": None, } report["report_sha256"] = hashlib.sha256(canonical({key: value for key, value in report.items() if key != "report_sha256"})).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 if __name__ == "__main__": generated = generate() print(f"hal_s2={generated['status']} native_wasm={generated['tests']['native_wasm_byte_identical']}")