#!/usr/bin/env python3 """Generate deterministic S3-02 remap/filter/VCP callback evidence.""" from __future__ import annotations import hashlib import json import subprocess from pathlib import Path ROOT = Path(__file__).resolve().parents[1] OUTPUT = ROOT / "coverage/python-callbacks-s3.report.json" INPUTS = ( "context-lock.json", "web/worker/python-callbacks.mjs", "web/worker/test_python_callbacks.mjs", "web/worker/test_python_callbacks_s3.mjs", "web/worker/sim-session.mjs", "linuxcnc/configs/sim/axis/remap/cycle/python/remap.py", ) def digest(data: bytes) -> str: return hashlib.sha256(data).hexdigest() def canonical(value: object) -> bytes: return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode("utf-8") def run(command: list[str]) -> dict[str, object]: result = subprocess.run(command, cwd=ROOT, text=True, capture_output=True, check=False) sample = None for line in reversed(result.stdout.splitlines()): if line.lstrip().startswith("{"): try: sample = json.loads(line) break except json.JSONDecodeError: continue return {"command": command, "returncode": result.returncode, "status": "verified" if result.returncode == 0 else "blocked", "stdout_sha256": digest(result.stdout.encode()), "stderr_sha256": digest(result.stderr.encode()), "sample": sample} def generate(output: Path = OUTPUT) -> dict[str, object]: context = json.loads((ROOT / "context-lock.json").read_text(encoding="utf-8")) callbacks = run(["node", "web/worker/test_python_callbacks.mjs"]) s3 = run(["node", "web/worker/test_python_callbacks_s3.mjs"]) report: dict[str, object] = { "schema": "python-callbacks-s3-report.v1", "task_id": "S3-02", "context_id": context["context_id"], "status": "verified" if callbacks["status"] == "verified" and s3["status"] == "verified" and (s3["sample"] or {}).get("status") == "verified" else "blocked", "callbacks": {"remap": ["prolog", "epilog"], "filter": ["source_allowlist", "budget", "exception_isolation"], "vcp": ["descriptor", "typed_pin", "callback", "rollback_on_failure"]}, "limits": {"max_callbacks": 256, "max_source_bytes": 65536, "vcp_pins": 256, "reentrant": "blocked"}, "boundaries": ["subprocess/socket/device paths/host paths are REAL-EXCLUDED", "VCP UI rendering remains outside this task", "callback handlers are Worker-local and not persisted as executable code"], "inputs": [{"path": path, "sha256": digest((ROOT / path).read_bytes())} for path in INPUTS], "tests": {"legacy": callbacks, "s3": s3}, "report_sha256": None, } report["report_sha256"] = digest(canonical({key: value for key, value in report.items() if key != "report_sha256"})) 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 if __name__ == "__main__": report = generate() print(f"python_callbacks_s3={report['status']}")