#!/usr/bin/env python3 """Generate deterministic Worker Python bridge evidence.""" from __future__ import annotations import argparse import hashlib import json import subprocess from pathlib import Path from typing import Any CONTEXT = Path("context-lock.json") MANIFEST = Path("python-runtime/manifest.json") RUNTIME_REPORT = Path("coverage/python-runtime.report.json") SESSION_REPORT = Path("coverage/workspace-session-integration.report.json") GATE = Path("coverage/release-candidate-gate.report.json") OUTPUT = Path("coverage/python-worker-bridge.report.json") def canonical(value: Any) -> str: return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) def digest(value: Any) -> str: return hashlib.sha256(canonical(value).encode("utf-8")).hexdigest() def artifact(root: Path, path: Path, role: str) -> dict[str, Any]: full = root / path if not full.is_file(): return {"path": path.as_posix(), "role": role, "status": "missing"} return {"path": path.as_posix(), "role": role, "status": "present", "bytes": full.stat().st_size, "sha256": hashlib.sha256(full.read_bytes()).hexdigest()} def run(root: Path) -> tuple[dict[str, Any], dict[str, Any]]: runs = [] for _ in range(2): result = subprocess.run(["node", "web/worker/test_python_bridge.mjs"], cwd=root, check=False, capture_output=True, text=True) runs.append({"status": "passed" if result.returncode == 0 else "blocked", "exit_code": result.returncode, "stdout": result.stdout.strip(), "stdout_sha256": digest(result.stdout), "stderr_sha256": digest(result.stderr)}) return runs[0], runs[1] def generate(root: Path, output: Path = OUTPUT) -> dict[str, Any]: root = root.resolve() lock = json.loads((root / CONTEXT).read_text(encoding="utf-8")) manifest = json.loads((root / MANIFEST).read_text(encoding="utf-8")) runtime = json.loads((root / RUNTIME_REPORT).read_text(encoding="utf-8")) session = json.loads((root / SESSION_REPORT).read_text(encoding="utf-8")) gate = json.loads((root / GATE).read_text(encoding="utf-8")) first, second = run(root) checks = { "manifest_pin": {"status": "verified" if manifest.get("runtime") == "Pyodide" and manifest.get("worker_only") is True and runtime.get("status") == "passed" else "blocked", "version": manifest.get("version")}, "session_dependency": {"status": "verified" if session.get("implementation_status") == "verified" else "blocked"}, "virtual_api_and_denial": {"status": "verified" if first.get("status") == "passed" and "python_bridge=passed" in first.get("stdout", "") else "blocked"}, "repeat_determinism": {"status": "verified" if first == second else "blocked"}, "release_stop_preserved": {"status": "verified" if gate.get("release_action") == "STOP_RELEASE" and gate.get("candidate_eligible") is False else "blocked", "release_action": gate.get("release_action")}, } checks_ok = all(value["status"] == "verified" for value in checks.values()) report: dict[str, Any] = { "schema": "python-worker-bridge.v1", "context_id": lock.get("context_id"), "task_id": "M3-PY-01", "status": "partial" if checks_ok else "blocked", "implementation_status": "verified" if checks_ok else "blocked", "candidate_eligible": False, "release_action": "STOP_RELEASE", "inputs": [artifact(root, CONTEXT, "context_lock"), artifact(root, MANIFEST, "python_manifest"), artifact(root, RUNTIME_REPORT, "python_runtime"), artifact(root, SESSION_REPORT, "workspace_session_integration"), artifact(root, GATE, "release_candidate_gate")], "checks": checks, "runtime": manifest, "sample": {"first": first, "second": second}, "next_transition": "execute FE-M1 minimal real frontend vertical slice", "report_sha256": None, } report["report_sha256"] = digest({key: value for key, value in report.items() if key != "report_sha256"}) destination = output if output.is_absolute() else root / output destination.parent.mkdir(parents=True, exist_ok=True) destination.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("--output", type=Path, default=OUTPUT) args = parser.parse_args() report = generate(args.repo_root.resolve(), args.output) print(f"python_worker_bridge={report['status']} denial={report['checks']['virtual_api_and_denial']['status']}") return 0 if __name__ == "__main__": raise SystemExit(main())