#!/usr/bin/env python3 """Generate XYZAC-only canonical WASM preview evidence for RTCP-04.""" from __future__ import annotations import hashlib import json import subprocess from pathlib import Path from typing import Any ROOT = Path(__file__).resolve().parents[1] REPORT = ROOT / "coverage/xyzac-trt-program-preview.report.json" CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini" CONTEXT_ID = "linuxcnc-2.10.0-pre0-wasm-m1-py-c1" def canonical(value: Any) -> str: return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) def main() -> int: command = ["node", "web/worker/test_xyzac_program_preview.mjs"] result = subprocess.run(command, cwd=ROOT, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, check=False) sample = None for line in reversed(result.stdout.splitlines()): try: value = json.loads(line) except json.JSONDecodeError: continue if isinstance(value, dict) and value.get("schema") == "xyzac-program-preview-rtcp-04.v1": sample = value; break status = "verified" if result.returncode == 0 and sample and sample.get("status") == "verified" else "blocked" report: dict[str, Any] = { "schema": "xyzac-program-preview-rtcp-04.v1", "task_id": "RTCP-04", "context_id": CONTEXT_ID, "case": {"path": CASE, "sha256": hashlib.sha256((ROOT / CASE).read_bytes()).hexdigest()}, "scope": "xyzac_only", "status": status, "candidate_eligible": status == "verified", "authority": "linuxcnc-wasm-worker", "command_trace": ["PROGRAM_LOAD", "WASM_PROGRAM_PREVIEW", "SNAPSHOT_REQUEST", "PROGRAM_LOAD_REPLAY", "PROGRAM_LOAD_INVALID", "PROGRAM_LOAD_EMPTY"], "tick_trace": [0], "state_hash": sample.get("state_hash") if sample else None, "source_hash": sample.get("source_hash") if sample else None, "line_count": sample.get("line_count") if sample else None, "segment_count": sample.get("segment_count") if sample else 0, "preview_hash": sample.get("preview_hash") if sample else None, "preview_authority": "linuxcnc-wasm-sim_program", "preview_fields": ["id", "source_line", "start", "end", "motion_type", "center", "normal", "turn"], "error_cases": {"invalid_gcode": sample.get("invalid_gcode") if sample else "blocked", "empty_source": sample.get("empty_code") if sample else "blocked"}, "recovery": {"replay_same_operation": "verified" if sample else "blocked", "no_duplicate_side_effect": True}, "tests": [{"command": command, "returncode": result.returncode, "status": status, "output_sha256": hashlib.sha256(result.stdout.encode()).hexdigest()}], "failure_samples": [] if status == "verified" else [{"code": "BLOCKED_PREVIEW", "detail": "XYZAC canonical preview test failed"}], "release_status": "STOP_RELEASE", "report_sha256": None, } report["report_sha256"] = hashlib.sha256(canonical({key: value for key, value in report.items() if key != "report_sha256"}).encode()).hexdigest() REPORT.parent.mkdir(parents=True, exist_ok=True); REPORT.write_text(json.dumps(report, ensure_ascii=False, sort_keys=True, indent=2) + "\n", encoding="utf-8") print(json.dumps({"schema": report["schema"], "status": report["status"], "report_sha256": report["report_sha256"], "preview_hash": report["preview_hash"]}, ensure_ascii=False)) return 0 if status == "verified" else 1 if __name__ == "__main__": raise SystemExit(main())