#!/usr/bin/env python3 """Generate deterministic evidence for the bounded O-word runtime.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path from typing import Any OUTPUTS = ( Path("web/worker/program-runner.mjs"), Path("web/worker/test_program_control.mjs"), ) def canonical(value: Any) -> str: return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) def sha256(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as handle: for chunk in iter(lambda: handle.read(1024 * 1024), b""): digest.update(chunk) return digest.hexdigest() def generate(root: Path, report_path: Path) -> dict[str, Any]: test = subprocess.run(["node", "web/worker/test_program_control.mjs"], cwd=root, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) outputs = [{"path": path.as_posix(), "bytes": (root / path).stat().st_size, "sha256": sha256(root / path)} for path in OUTPUTS] report: dict[str, Any] = { "schema": "o-word-runtime-report.v1", "task_id": "P3-02", "context_id": "linuxcnc-2.10.0-pre0-wasm-m1-py-c1", "status": "verified" if test.returncode == 0 else "blocked", "implementation": "web/worker/program-runner.mjs", "features": ["O SUB/ENDSUB", "O CALL/RETURN", "M98 P/L and M99", "IF/ELSEIF/ELSE/ENDIF", "WHILE/ENDWHILE", "REPEAT/ENDREPEAT", "BREAK", "CONTINUE"], "budgets": {"max_control_depth": 32, "max_control_steps": 100000, "max_expanded_lines": 100000, "max_program_bytes": 1048576, "max_program_lines": 10000}, "diagnostics": {"missing_subroutine": "program_subroutine_missing", "unclosed_structure": "program_control_unclosed", "loop_budget": "program_loop_budget", "host_filter": "blocked_without_host_execution"}, "filter_policy": {"host_process": "blocked", "network": "blocked", "device_path": "blocked", "dynamic_module": "blocked", "pure_worker_filter": "reserved_for_P5-03"}, "outputs": outputs, "test": {"status": "verified" if test.returncode == 0 else "blocked", "command": "node web/worker/test_program_control.mjs", "stdout": test.stdout.decode("utf-8", errors="replace")[-4000:], "stderr": test.stderr.decode("utf-8", errors="replace")[-4000:]}, "known_boundaries": ["full Python/remap/filter callback lifecycle is P5-03", "canonical/TP integration is P3-03"], } report["report_sha256"] = hashlib.sha256(canonical(report).encode("utf-8")).hexdigest() report_path.parent.mkdir(parents=True, exist_ok=True) report_path.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/o-word-runtime.report.json")) args = parser.parse_args() report = generate(args.repo_root.resolve(), args.repo_root.resolve() / args.report) print(f"o_word_runtime={report['status']} test={report['test']['status']}") return 0 if report["status"] == "verified" else 1 if __name__ == "__main__": raise SystemExit(main())