#!/usr/bin/env python3 """Generate deterministic S5-04 interp/remap/O-word Worker 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" RUNNER = ("node", "web/worker/test_source_interpreter_s5.mjs") EXPECTED_CASES = { "source-test:linuxcnc/tests/interp/oword-bug315/test.sh", "source-test:linuxcnc/tests/oword/sub.0/test.sh", "source-test:linuxcnc/tests/interp/oword-unwind/test.sh", "source-test:linuxcnc/tests/interp/do-while-break/test.sh", "source-test:linuxcnc/tests/interp/g6164/test.sh", "source-test:linuxcnc/tests/remap/oword-pycall/test.sh", "source-test:linuxcnc/tests/remap/fail/body-py/test.sh", "source-test:linuxcnc/tests/remap/remap-io/test-py.ini", } 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) -> dict[str, Any]: result = subprocess.run(RUNNER, cwd=root, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) return { "command": list(RUNNER), "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 generate(root: Path, output: Path) -> dict[str, Any]: execution = run(root) parsed = None parse_error = None try: parsed = json.loads(execution["stdout"]) except json.JSONDecodeError as exc: parse_error = str(exc) cases = parsed.get("cases", []) if isinstance(parsed, dict) else [] case_ids = {item.get("id") for item in cases} pending = parsed.get("pending", []) if isinstance(parsed, dict) else [] fixture_files = [] for item in cases: fixture_files.extend(item.get("fixture", [])) fixture_paths = {item.get("path") for item in fixture_files} all_fixtures_valid = all( isinstance(item, dict) and isinstance(item.get("path"), str) and (root / item["path"]).is_file() and digest((root / item["path"]).read_bytes()) == item.get("sha256") and (root / item["path"]).stat().st_size == item.get("bytes") for item in fixture_files ) expected_comparison = { "status": "SOURCE_BLOCKED", "reason": "locked source expected/checkresult files are hashed and retained; upstream canonical logger is unavailable in this Worker adapter", "fixture_hashes_verified": all_fixtures_valid, "compared_byte_for_byte": False, } adapter_ok = execution["status"] == "PASSED" and parsed is not None and case_ids == EXPECTED_CASES and all_fixtures_valid report: dict[str, Any] = { "schema": "linuxcnc-source-interpreter-execution.v1", "task_id": "S5-04", "context_id": CONTEXT_ID, "status": "verified" if adapter_ok else "blocked", "scope": ["tests/interp/**", "tests/remap/**", "tests/oword/**", "filter capability boundary"], "execution": {key: value for key, value in execution.items() if key not in {"stdout", "stderr"}}, "worker_golden": parsed, "wasm_core": {"status": "PASSED" if adapter_ok else "BLOCKED", "runner": "build-wasm/linuxcnc-sim-core.wasm", "logical_tick": False}, "expected_checkresult_comparison": expected_comparison, "counts": { "selected_cases": len(cases), "passed": parsed.get("counts", {}).get("PASSED", 0) if isinstance(parsed, dict) else 0, "worker_task_motion_pending": parsed.get("counts", {}).get("WORKER_TASK_MOTION_PENDING", 0) if isinstance(parsed, dict) else 0, "fixture_files": len(fixture_paths), "pending": len(pending), }, "pending_adapters": pending, "boundaries": [ "upstream canonical logger parity remains SOURCE_BLOCKED; DO/ENDDO and G64/G61 now have Worker adapters", "remap/filter Python is represented only through allowlisted PythonCallbacks; host imports remain REAL-EXCLUDED", "source expected/checkresult hashes are evidence inputs, not an unverified parity claim", ], } report["parse_error"] = parse_error 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-interp-s5.report.json")) args = parser.parse_args() report = generate(args.repo_root.resolve(), args.repo_root.resolve() / args.report) print(f"linuxcnc_source_interpreter_s5={report['status']} cases={report['counts']['selected_cases']} pending={report['counts']['pending']}") return 0 if report["status"] == "verified" else 1 if __name__ == "__main__": raise SystemExit(main())