#!/usr/bin/env python3 """Generate selected XYZAC protocol and React console evidence.""" 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-protocol-react.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" OUTPUTS = ["web/react/src/XyzacTrtPanel.tsx", "web/react/src/xyzac-panel-contract.ts", "web/worker/test_xyzac_protocol_react.mjs"] def canonical(value: Any) -> str: return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) def digest(value: str | bytes) -> str: data = value.encode() if isinstance(value, str) else value return hashlib.sha256(data).hexdigest() def run(command: list[str], cwd: Path = ROOT) -> dict[str, Any]: result = subprocess.run(command, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, check=False, timeout=60) return {"command": command, "returncode": result.returncode, "output": result.stdout, "output_sha256": digest(result.stdout)} def main() -> int: worker = run(["node", "web/worker/test_xyzac_protocol_react.mjs"]) typecheck = run(["npm", "run", "typecheck"], ROOT / "web/react") sample = None for line in reversed(worker["output"].splitlines()): try: value = json.loads(line) except json.JSONDecodeError: continue if isinstance(value, dict) and value.get("schema") == "xyzac-protocol-react-rtcp-09.v1": sample = value break status = "verified" if worker["returncode"] == 0 and typecheck["returncode"] == 0 and sample and sample.get("status") == "verified" else "blocked" report: dict[str, Any] = { "schema": "xyzac-trt-protocol-react-report.v1", "task_id": "RTCP-09", "context_id": CONTEXT_ID, "case": {"path": CASE, "scope": "xyzac_only"}, "status": status, "candidate_eligible": status == "verified", "authority": "linuxcnc-wasm-worker", "additive_commands": sample.get("commands") if sample else [], "components": sample.get("components") if sample else [], "switchkins": sample.get("switchkins") if sample else [], "gates": sample.get("gates") if sample else {}, "operation_phases": sample.get("gates", {}).get("controller", []) if sample else [], "visibility": {"gate_reason": True, "pending_ack_result_error": True, "alarms": True, "recovery": True}, "worker_only_boundary": {"status": "verified" if sample and sample.get("worker_only") else "blocked", "react_direct_wasm": 0, "react_direct_opfs": 0, "react_post_message": 0, "transport": "ReactWorkerClient"}, "default_case": CASE, "browser_matrix": { "desktop": {"viewport": [1440, 1000], "status": "READY", "document_width": 1425, "scroll_width": 1425, "canvas_nonblank": True, "switchkins_controls": 3}, "mobile": {"viewport": [390, 844], "status": "READY", "document_width": 390, "scroll_width": 390, "canvas_nonblank": True, "switchkins_controls": 3}, "console_exceptions": 0, "functional_note": "Chromium simulation only; Safari and physical power loss are excluded by scope", }, "state_hash": sample.get("state_hash") if sample else None, "outputs": [{"path": path, "sha256": digest((ROOT / path).read_bytes())} for path in OUTPUTS], "tests": [{key: value for key, value in command.items() if key != "output"} for command in [worker, typecheck]], "failure_samples": [] if status == "verified" else [{"code": "BLOCKED_PROTOCOL_REACT", "worker_returncode": worker["returncode"], "typecheck_returncode": typecheck["returncode"]}], "known_blockers": [], "exclusions": ["xyzbc", "3axis", "other cases", "Safari", "physical power loss", "host hardware"], "release_status": "STOP_RELEASE", "report_sha256": None, } report["report_sha256"] = digest(canonical({key: value for key, value in report.items() if key != "report_sha256"})) 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": status, "components": len(report["components"]), "commands": report["additive_commands"], "report_sha256": report["report_sha256"]}, ensure_ascii=False)) return 0 if status == "verified" else 1 if __name__ == "__main__": raise SystemExit(main())