#!/usr/bin/env python3 """Generate deterministic S7-06 canonical INTRO_GRAPHIC resource 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_resource_intro_graphic_s7_06.mjs") INPUTS = ("coverage/configuration-corexy-s7-05.report.json", "coverage/configuration-resource-remediation.report.json", "linuxcnc/linuxcnc.gif", "linuxcnc/src/emc/usr_intf/stepconf/build_INI.py", "web/workspace/resource-resolver.mjs", "web/worker/test_resource_intro_graphic_s7_06.mjs") 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 artifact(root: Path, path: str) -> dict[str, Any]: value = root / path return {"path": path, "status": "missing"} if not value.is_file() else {"path": path, "bytes": value.stat().st_size, "sha256": digest(value.read_bytes())} def generate(root: Path, output: Path) -> dict[str, Any]: root = root.resolve(); result = subprocess.run(RUNNER, cwd=root, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False); sample = None for line in reversed(result.stdout.decode("utf-8", errors="replace").splitlines()): if line.lstrip().startswith("{"): try: sample = json.loads(line); break except json.JSONDecodeError: pass passed = ( result.returncode == 0 and sample and sample.get("status") == "verified" and sample.get("source_generator", {}).get("statement") == 'print("INTRO_GRAPHIC = linuxcnc.gif", file=file)' and sample.get("resource", {}).get("path") == "linuxcnc/linuxcnc.gif" and sample.get("resource_graph", {}).get("deterministic") is True and sample.get("wrong_key", {}).get("status") == "blocked" and sample.get("wrong_section", {}).get("status") == "blocked" ) report: dict[str, Any] = {"schema": "resource-intro-graphic-s7-06-report.v1", "task_id": "S7-06", "context_id": CONTEXT_ID, "status": "verified" if passed else "blocked", "implementation_status": "verified" if passed else "blocked", "execution": {"command": list(RUNNER), "returncode": result.returncode, "status": "PASSED" if result.returncode == 0 else "FAILED", "stdout_sha256": digest(result.stdout), "stderr_sha256": digest(result.stderr), "sample": sample}, "scope": {"key": "DISPLAY.INTRO_GRAPHIC", "raw_value": "linuxcnc.gif", "canonical_path": "linuxcnc/linuxcnc.gif", "mapping_policy": "explicit-key-only"}, "checks": {"source_generator_basis": {"status": "verified" if passed else "blocked", "source": "linuxcnc/src/emc/usr_intf/stepconf/build_INI.py", "statement": sample.get("source_generator", {}).get("statement") if sample else None, "source_sha256": sample.get("source_generator", {}).get("sha256") if sample else None}, "canonical_asset_resolution": {"status": "verified" if passed else "blocked"}, "generic_basename_guard": {"status": "verified" if passed else "blocked"}, "deterministic_graph": {"status": "verified" if passed else "blocked", "state_hash": sample.get("resource_graph", {}).get("state_hash") if sample else None, "repeated_state_hash": sample.get("resource_graph", {}).get("repeated_state_hash") if sample else None}}, "inputs": [artifact(root, path) for path in INPUTS], "boundaries": ["only DISPLAY.INTRO_GRAPHIC linuxcnc.gif uses this canonical shared asset", "tool/parameter/HAL/program basename fallback remains blocked", "208 configuration entries and official oracle remain independent blockers"]} report["report_sha256"] = hashlib.sha256(canonical({key: value for key, value in report.items() if key != "report_sha256"}).encode()).hexdigest(); 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/resource-intro-graphic-s7-06.report.json")); args = parser.parse_args(); root = args.repo_root.resolve(); output = args.report if args.report.is_absolute() else root / args.report; report = generate(root, output); print(f"resource_intro_graphic_s7_06={report['status']} mapping={report['scope']['mapping_policy']}"); return 0 if report["status"] == "verified" else 1 if __name__ == "__main__": raise SystemExit(main())