#!/usr/bin/env python3 """Generate the exact XYZAC startup evidence report for RTCP-02.""" 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-startup.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 sha256_file(path: Path) -> str | None: return hashlib.sha256(path.read_bytes()).hexdigest() if path.is_file() else None def run_test() -> dict[str, Any]: command = ["node", "web/worker/test_xyzac_startup.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-trt-startup-rtcp-02.v1": sample = value break return {"command": command, "returncode": result.returncode, "output_sha256": hashlib.sha256(result.stdout.encode()).hexdigest(), "status": "verified" if result.returncode == 0 and sample and sample.get("status") == "verified" else "blocked", "sample": sample} def main() -> int: # Python has no optional chaining; keep the report generator explicit and deterministic. command = ["node", "web/worker/test_xyzac_startup.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-trt-startup-rtcp-02.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-trt-startup-rtcp-02.v1", "task_id": "RTCP-02", "context_id": CONTEXT_ID, "case": {"path": CASE, "sha256": sha256_file(ROOT / CASE)}, "scope": "xyzac_only", "status": status, "candidate_eligible": status == "verified", "startup_states": ["TASK_NEW", "CONFIG_RESOLVED", "HAL_READY", "RUNTIME_READY", "TASK_READY"], "command_trace": ["CASE_LOAD", "INI_PARSE", "RESOURCE_RESOLVE", "HAL_PREFLIGHT", "CORE_CREATE", "TASK_READY"], "tick_trace": [0], "state_hash": sample.get("state_hash") if sample else None, "startup": sample.get("startup") if sample else None, "exact_resource_count": sample.get("exact_resource_count") if sample else 0, "fetched_count": sample.get("fetched_count") if sample else 0, "missing_case": {"status": sample.get("missing_case_status") if sample else "blocked", "expected": "blocked"}, "tests": [{"command": command, "status": status, "returncode": result.returncode, "output_sha256": hashlib.sha256(result.stdout.encode()).hexdigest()}], "failure_samples": [] if status == "verified" else [{"code": "BLOCKED_STARTUP", "detail": "exact XYZAC startup test did not verify"}], "accepted_scope_exclusions": ["xyzbc", "3axis", "other cases"], "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"], "exact_resource_count": report["exact_resource_count"]}, ensure_ascii=False)) return 0 if status == "verified" else 1 if __name__ == "__main__": raise SystemExit(main())