"""Require a recorded review of the exact draft before accepting a final render.""" import json from pathlib import Path import re import runpy import subprocess def git(*args): return subprocess.check_output(["git", *args], text=True, encoding="utf-8").strip() def validate_final_review(request): path = request.get("review", "") if not isinstance(path, str) or not re.fullmatch(r"reviews/[a-z0-9-]+\.json", path): raise SystemExit("Final render needs a review in reviews/.json.") review_path = Path(path).resolve(strict=True) if not review_path.is_relative_to(Path("reviews").resolve()): raise SystemExit("Review file resolves outside reviews/.") review = json.loads(review_path.read_text(encoding="utf-8")) draft_sha = review.get("draft_sha", "") if not isinstance(draft_sha, str) or not re.fullmatch(r"[0-9a-f]{40}", draft_sha): raise SystemExit("Review must reference the reviewed draft commit.") if subprocess.run(["git", "merge-base", "--is-ancestor", draft_sha, "HEAD"]).returncode: raise SystemExit("Reviewed draft must be in this branch history.") files = git("diff-tree", "--no-commit-id", "--diff-filter=A", "-r", "--name-only", draft_sha, "--", "render-requests/").splitlines() if len(files) != 1: raise SystemExit("Draft commit must add one render request.") draft = json.loads(git("show", f"{draft_sha}:{files[0]}")) keys = ("scene", "duration", "fps", "format", "voice", "checkpoints", "motion") defaults = {"duration": 3, "fps": 30, "format": "horizontal", "voice": False} if draft.get("stage") != "draft" or any( draft.get(key, defaults.get(key)) != request.get(key, defaults.get(key)) for key in keys ): raise SystemExit("Final scene, timing, voice and checkpoints must match the reviewed draft.") if git("diff", "--name-only", draft_sha, "HEAD", "--", ".", ":(exclude)render-requests/**", ":(exclude)reviews/**", ":(exclude)README.md"): raise SystemExit("Source changed since review. Render and inspect a new draft.") assets = review.get("assets", {}) checkpoints = review.get("checkpoints", []) defects = review.get("defects") if review.get("scene") != request["scene"] or not isinstance(assets, dict) or not assets: raise SystemExit("Review must list inspected objects for the requested scene.") registered = runpy.run_path(request["scene"]).get("REVIEW_ASSETS") if not isinstance(registered, dict) or set(assets) != set(registered): raise SystemExit("Review must cover every object registered by the final scene.") if not all(re.fullmatch(r"[a-z][a-z0-9_-]{0,55}", name) and isinstance(note, str) and note.strip() for name, note in assets.items()): raise SystemExit("Each object needs its own visual observation.") if (not isinstance(checkpoints, list) or len(checkpoints) != len(request["checkpoints"]) or not all(isinstance(note, str) and note.strip() for note in checkpoints)): raise SystemExit("Review must record an observation for each motion checkpoint.") if request.get("motion"): notes = review.get("motion") if not isinstance(notes, list) or len(notes) != len(request["motion"]) or not all( isinstance(note, str) and note.strip() for note in notes ): raise SystemExit("Review must describe visible actor motion for every declared beat.") if not isinstance(defects, list) or not all( isinstance(item, dict) and item.get("status") == "verified-fixed" and all(isinstance(item.get(key), str) and item[key].strip() for key in ("where", "problem", "change", "verified")) for item in defects ): raise SystemExit("Review has unresolved defects or lacks verified fixes.")