#!/usr/bin/env bash
# dryrun.sh — run the WHOLE pipeline on a copy of a real project, spending nothing.
#
# THIS IS THE CHEAPEST TEST IN THE LANE AND IT SHOULD BE THE FIRST THING YOU REACH FOR.
# The half of the pipeline after `render-all` had never once executed — both earlier
# films died before it and it was hand-built each time. Running it on a copy, while the
# real film was still rendering, found FIVE separate defects in an afternoon, every one
# of which would have stranded a film whose renders were already paid for:
#
#   sync-gate could never complete        mark sat after gate(), and gate() exits
#   render-all could never complete       same shape, and it is where the hours are
#   bookends named an impossible file     lost the -v1 rebuilding a versioned name
#   the film was cropped TWICE            portrait -> landscape master -> portrait film
#   deliver shipped the body, subs early  wrong input, and captions timed for the film
#
# None was visible in the code or in a green test suite. All five were obvious within
# seconds of the stage actually running.
#
# Why a COPY of a real project rather than a fresh slug: a real project already has the
# song, the transcript, the styles, the plan and some landed clips. A fresh slug has
# none of that and cannot reach the stages that break.
#
# Usage:
#   dryrun.sh <pack> <slug> [--keep] [--synthetic]
#
# --synthetic: generate the clips instead of requiring landed ones — testsrc2 over the
# real song slice, at the plan's own window lengths and dimensions, first four windows. This is what lets a FRESH pack prove the
# whole back half BEFORE its first real render: the preflight stage runs it once per
# pack x runner-version and refuses to spend a render slot on a pipeline that cannot
# reach `deliver`. The seven bugs that motivated this were all invisible in code review
# and a green suite, and all obvious within seconds of the stage actually running.
#
# It NEVER submits: the render stage is marked complete in the copy before anything runs.
set -uo pipefail

PACK="${1:-}"; SLUG="${2:-}"
# --stamp stands alone; everything else needs a pack (or --fixture) and a slug (or kind)
if [ -z "$PACK" ] || { [ "$PACK" != "--stamp" ] && [ -z "$SLUG" ]; }; then
  echo "usage: dryrun.sh <pack> <slug> [--keep] [--synthetic] [--all-windows] | --fixture solo|duet [--all-windows] | --stamp" >&2
  exit 2
fi
shift; [ $# -gt 0 ] && shift
KEEP=""; SYNTHETIC=0; ALL_WINDOWS=0
for a in "$@"; do
  case "$a" in
    --keep) KEEP="--keep";;
    --synthetic) SYNTHETIC=1;;
    --all-windows) ALL_WINDOWS=1;;
    *) echo "dryrun: unknown flag $a" >&2; exit 2;;
  esac
done
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MAIN="$(cd "$HERE/../../.." && pwd)"
W="${VCLAW_WORKSPACE:-${VIDEOCLAW_WORKSPACE:-$HOME/videoclaw}}"

# --fixture solo|duet (as the PACK argument's place: `dryrun.sh --fixture duet`): a film
# from nothing. rap_fixture.py builds the pack, the sheet, a song with the lines spoken at
# their planned times, the portraits and the worlds; whisper is the fixture's own stub
# (whisper_stub.py on PATH, answering from the timeline by listening); the runner plans
# it through broll-plan, every window is synthesised from that plan, and the rest runs
# for real. The solo fixture is the baseline; the duet one executes the handover, the
# gap-section and the short-line paths a solo film never reaches (film #2's rows 7, 12
# and 17). ~10 min of local CPU, no renders, no spend.
# --stamp: record that BOTH fixtures reached qc on the current scripts (their hash). The
# runner's preflight refuses to spend a render slot until this stamp exists for the
# scripts it is about to run — an engine change is proved on the fixtures, ten minutes
# of CPU, before a film's five hours of lane time (npm run smoke:rap-dryrun).
proof_stamp() { cat "$HERE/make-rap-mv.sh" "$HERE"/*.py "$HERE/lib/common.sh" 2>/dev/null | md5 -q; }
if [ "$PACK" = "--stamp" ]; then
  mkdir -p "$W/tools/rap-proof"
  for k in solo duet; do
    [ -f "$W/projects/rap-fixture-$k-dryrun/build/qc-report.json" ] || { echo "dryrun: no qc report for the $k fixture — run --fixture $k first" >&2; exit 2; }
    python3 -c 'import json,sys; sys.exit(0 if json.load(open(sys.argv[1])).get("failures", 1) == 0 else 1)' "$W/projects/rap-fixture-$k-dryrun/build/qc-report.json"       || { echo "dryrun: the $k fixture's qc report has failures — not stamping" >&2; exit 2; }
  done
  date -u +%Y-%m-%dT%H:%M:%SZ > "$W/tools/rap-proof/$(proof_stamp).ok"
  echo "dryrun: engine proof stamped for scripts $(proof_stamp) (solo + duet reached qc)"
  exit 0
fi
if [ "$PACK" = "--fixture" ]; then
  KIND="$SLUG"
  case "$KIND" in solo|duet) ;; *) echo "dryrun: --fixture needs solo or duet" >&2; exit 2;; esac
  FX="$(python3 "$HERE/rap_fixture.py" --kind "$KIND" --workspace "$W")" || exit 2
  SLUG="$(python3 -c 'import json,sys; print(json.loads(sys.argv[1])["slug"])' "$FX")"
  PACK="$(python3 -c 'import json,sys; print(json.loads(sys.argv[1])["pack"])' "$FX")"
  STUB_DIR="$W/tools/whisper-stub"; mkdir -p "$STUB_DIR"
  cp "$HERE/whisper_stub.py" "$STUB_DIR/whisper" && chmod +x "$STUB_DIR/whisper"
  export PATH="$STUB_DIR:$PATH"
  export RAP_FIXTURE_VOCAL="$(python3 -c 'import json,sys; print(json.loads(sys.argv[1])["vocal"])' "$FX")"
  export RAP_FIXTURE_WORDS="$(python3 -c 'import json,sys; print(json.loads(sys.argv[1])["wordsFile"])' "$FX")"
  export UNATTENDED=1 RAP_SKIP_DRYRUN=1 VCLAW_WORKSPACE="$W"
  # The stub IS the ear here: it answers in openai-whisper's shape from the fixture's
  # timeline. WHISPER_ENGINE=cpp would put the real shim ahead of it on PATH.
  export WHISPER_ENGINE=openai
  echo "dryrun: fixture $KIND -> $SLUG ($(python3 -c 'import json,sys; d=json.loads(sys.argv[1]); print(str(d["songSeconds"]) + "s, " + str(d["lines"]) + " lines")' "$FX"))"
  ( cd "$MAIN" && bash "$HERE/make-rap-mv.sh" "$PACK" "$SLUG" --until broll-plan ) || { echo "dryrun: the fixture did not reach broll-plan — see $W/projects/$SLUG/build/" >&2; exit 2; }
  SYNTHETIC=1; ALL_WINDOWS=1
fi
SRC="$W/projects/$SLUG"
DST="$W/projects/$SLUG-dryrun"
# THE STAGE LIST IS ONE THING (film3 item 9): read from the runner's STAGES, cut at
# render-all — a second copy here drifted from it (the runner had 31, this line and the
# brief disagreed about which came before the renders).
STAGES_DONE="$(sed -n 's/^STAGES="\(.*\)"/\1/p' "$HERE/make-rap-mv.sh" | awk '{for (i = 1; i <= NF; i++) { printf "%s ", $i; if ($i == "render-all") exit } }')"
[ -n "$STAGES_DONE" ] || { echo "dryrun: could not read STAGES from make-rap-mv.sh" >&2; exit 2; }

[ -d "$SRC" ] || { echo "dryrun: no project at $SRC" >&2; exit 2; }
[ "$KEEP" = "--keep" ] || rm -rf "$DST"

# The live-job records are the ONLY thing that must not travel: they name provider jobs
# this copy has no business polling, and reconcile would happily adopt them.
# final/ stays behind: the copy exists to prove the PIPELINE, and the source's old
# deliverables only confuse the qc stage — they carry the source slug (correctly
# flagged as foreign) and the source loudness (correctly flagged as unnormalised).
# The copy earns its own final/ by running the stages.
# *.approved and lane-render.lock stay behind too: an approval is bound to ONE
# project's contract bytes and a copy must never inherit it, and a lock names a pid
# that belongs to the source's driver.
rsync -a --exclude 'build/queue/*/*-live-job.json' \
         --exclude 'build/*.pid' --exclude 'build/*.log' --exclude 'build/*.out' \
         --exclude '*.approved' --exclude 'lane-render.lock' \
         --exclude 'final/' --exclude 'YOUTUBE-UPLOAD.md' --exclude 'PUBLISHED.tsv' \
         "$SRC/" "$DST/"

# --synthetic: manufacture clips for the first FOUR windows so the trim below keeps
# them. Dimensions come from the plan (aspect x resolution), lengths from the windows
# (+0.08s, the provider's own overshoot) — so every downstream length check sees the
# real shape of a real render. The AUDIO is the real song slice at the window
# (`ffmpeg -ss startSec -t seconds -i hook.wav`), not a sine: a proof whose clips
# carry a tone proves nothing about the sync gates, the word store or the mouthing
# cover, which is most of the back half. Two windows was too few to reach a cut with
# a gap in it.
if [ "$SYNTHETIC" = "1" ]; then
  ffbin="${FFMPEG_FULL:-/opt/homebrew/opt/ffmpeg-full/bin/ffmpeg}"
  FF_BIN="$ffbin" DRYRUN_ALL_WINDOWS="$ALL_WINDOWS" python3 - "$SRC" "$DST" <<'PYSYN'
import json, os, subprocess, sys
SRC, D = sys.argv[1], sys.argv[2]
plan = json.load(open(f"{D}/build/plan.json", encoding="utf-8"))
res = {"720p": 720, "1080p": 1080}.get(plan.get("resolution", "720p"), 720)
w, h = (res, round(res * 16 / 9)) if plan.get("aspect") == "9:16" else (round(res * 16 / 9), res)
# REBASE THE QUEUE ONTO THE COPY, exactly as the trim step below does. The plan's
# queueDir is absolute and points INTO THE SOURCE — resolving it unrebased wrote the
# synthetic clips into the REAL project's render queue, where the resumable driver
# would have counted two testsrc windows as already rendered, while the copy found
# "no landed clips" and the self-proof refused a healthy pipeline.
queue = plan.get("queueDir") or f"{SRC}/build/queue/explore"
if queue.startswith(SRC):
    queue = D + queue[len(SRC):]
clips = os.path.join(queue if os.path.isabs(queue) else os.path.join(D, queue), "clips")
os.makedirs(clips, exist_ok=True)
SYN_WINDOWS = len(plan["jobs"]) if os.environ.get("DRYRUN_ALL_WINDOWS") == "1" else 4
song = f"{SRC}/build/hook.wav"
for n, j in enumerate(plan["jobs"][:SYN_WINDOWS]):
    secs = float(j.get("seconds") or 8) + 0.08
    out = os.path.join(clips, f"{j['id']}.mp4")
    start = max(0.0, float(j.get("startSec") or 0.0))
    # DISTINCT per window: identical parameters render byte-identical files, and
    # index_library dedupes by content hash — two windows then share ONE library entry
    # and select_cut correctly refuses ("--auto needs 2 distinct clips, the index has
    # 1"). Real renders are never identical; synthetic ones must be made unalike.
    # The hue differs per window and so does the slice, so they are unalike twice over.
    r = subprocess.run([os.environ["FF_BIN"], "-v", "error", "-y",
        "-f", "lavfi", "-i", f"testsrc2=size={w}x{h}:rate=24:duration={secs}",
        "-ss", f"{start:.3f}", "-t", f"{secs:.3f}", "-i", song,
        "-map", "0:v", "-map", "1:a", "-vf", f"hue=h={n * 90}",
        "-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", "-shortest", out],
        capture_output=True, text=True)
    if r.returncode:
        sys.exit(f"synthetic clip failed: {r.stderr[-300:]}")
    # THE LIVE-JOB SIDECAR TOO. index_library reads a clip's window/style/performer
    # from `<id>-live-job.json` (the lane driver writes one per real render); without
    # it every synthetic clip indexes window=None, select_cut cannot pin a window to
    # its OWN clip, and the length-based fallback pairs them wrong — the proof then
    # failed sync on w03 carrying w02's slice (2026-08-28), the same by-length defect
    # class the couple film shipped (see run-it-to-find-it). A proof must feed the
    # pipeline everything a real render feeds it.
    with open(os.path.join(os.path.dirname(clips), f"{j['id']}-live-job.json"), "w") as fh:
        json.dump({"window": j["id"], "styleId": j.get("styleId", ""),
                   "performer": j.get("performer", "a"), "startSec": start,
                   "seconds": secs, "state": "dryrun-synthetic", "draw": 1}, fh)
print(f"dryrun: synthesised {min(SYN_WINDOWS, len(plan['jobs']))} clip(s) at {w}x{h} "
      f"with the real song slice under each (+ live-job sidecars)")
# THE B-ROLL PLAN TOO. A shot-led window (no vocal under it) draws from the pool, and
# the pool is the broll-plan's cutaways, rendered LONGER than the windows on purpose.
# Synthesising only the two window clips left the pool empty, so select_cut fell back
# to a 13s performance clip against a 14s cut and the proof failed on a pipeline that
# was healthy — the first full-track film whose first two windows held a gap (2026-08-22).
bp = f"{D}/build/broll-plan.json"
if os.path.isfile(bp):
    broll = json.load(open(bp, encoding="utf-8"))
    bq = broll.get("queueDir") or f"{SRC}/build/broll"
    if bq.startswith(SRC):
        bq = D + bq[len(SRC):]
    bclips = os.path.join(bq, "clips")
    os.makedirs(bclips, exist_ok=True)
    for n, j in enumerate(broll.get("jobs", [])):
        secs = float(j.get("seconds") or broll.get("seconds") or 8) + 0.08
        out = os.path.join(bclips, f"{j['id']}.mp4")
        r = subprocess.run([os.environ["FF_BIN"], "-v", "error", "-y",
            "-f", "lavfi", "-i", f"testsrc2=size={w}x{h}:rate=24:duration={secs}",
            "-f", "lavfi", "-i", f"anoisesrc=colour=brown:amplitude=0.05:duration={secs}",
            "-vf", f"hue=h={180 + n * 45}",
            "-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", "-shortest", out],
            capture_output=True, text=True)
        if r.returncode:
            sys.exit(f"synthetic B-roll clip failed: {r.stderr[-300:]}")
        # the live-job sidecar a real render leaves: the library index copies styleId
        # from it, and select_cut.fill_gaps prefers a gap's own world by that field —
        # without the sidecar the proof never exercised that preference (2026-09-02)
        with open(os.path.join(bq, f"{j['id']}-live-job.json"), "w") as fh:
            json.dump({"window": j["id"], "styleId": j.get("styleId", ""), "startSec": 0.0,
                       "seconds": secs, "state": "dryrun-synthetic", "draw": 1, "role": "broll"}, fh)
    print(f"dryrun: synthesised {len(broll.get('jobs', []))} B-roll cutaway(s) from broll-plan.json (+ live-job sidecars)")
PYSYN
fi

DRYRUN_ALL_WINDOWS="$ALL_WINDOWS" python3 - "$SRC" "$DST" <<'PY'
import json, os, sys
SRC, D = sys.argv[1], sys.argv[2]
plan_path = f"{D}/build/plan.json"
plan = json.load(open(plan_path, encoding="utf-8"))
# The copied plan's paths still point INTO THE SOURCE PROJECT. Rebase them onto the copy
# rather than string-replacing a project name — a helper that only works for the film it
# was written on is the thing this whole script exists to stop.
queue = plan.get("queueDir") or f"{SRC}/build/queue/explore"
if queue.startswith(SRC):
    queue = D + queue[len(SRC):]
clips = os.path.join(queue, "clips")
have = {f[:-4] for f in os.listdir(clips) if f.endswith(".mp4")} if os.path.isdir(clips) else set()

# Only the windows that HAVE a clip, re-seated contiguously from zero so the film has no
# hole. Re-seating means the clips no longer sit on the bars they were rendered against,
# so the lip-sync gate will refuse them — CORRECTLY. That is not a bug to fix; pass
# RAP_SKIP_SYNC=1 to look past it at the stages underneath.
# TRIM-AWARE re-seat. A clamped final window carries trimStartSec: the planner slides
# startSec BACK by the trim so the timeline position (startSec + trim) still lands on
# the frame grid, and the covered duration is seconds - trim. Re-seating startSec alone
# shifted such a window 6 frames off-grid and select_cut refused the plan — correctly.
keep, t = [], 0.0
all_windows = os.environ.get("DRYRUN_ALL_WINDOWS") == "1"
if all_windows:
    # EVERY window has a clip (--all-windows, the fixture proof): keep the plan's real
    # geometry — the gap sections and their B-roll are exactly what the duet fixture
    # exists to exercise; re-seating contiguously would delete them from the copy.
    keep = [j for j in sorted(plan["jobs"], key=lambda j: float(j.get("startSec") or 0)) if j["id"] in have]
    hook0 = json.load(open(f"{D}/build/hook.json", encoding="utf-8"))
    t = float(hook0.get("durationSec") or 0.0)
else:
    for j in sorted(plan["jobs"], key=lambda j: float(j.get("startSec") or 0)):
        if j["id"] not in have:
            continue
        trim = float(j.get("trimStartSec") or 0.0)
        j["startSec"] = t - trim              # timeline position = startSec + trim = t
        t += float(j.get("seconds") or 0) - trim - float(j.get("tailSec") or 0.0)
        keep.append(j)
if not keep:
    sys.exit("dryrun: the source project has no landed clips yet — nothing to run on")
plan["jobs"] = keep
plan["queueDir"] = queue
# Voice slices live under the project too, and a slice read from the SOURCE would be
# correct here by luck rather than by construction.
for j in keep:
    ref = j.get("voiceRef") or ""
    if ref.startswith(SRC):
        j["voiceRef"] = D + ref[len(SRC):]
json.dump(plan, open(plan_path, "w", encoding="utf-8"), indent=1)

# The film must legitimately cover its song, or `polish` fails on coverage for a reason
# that is an artefact of the trim rather than anything real.
hook_path = f"{D}/build/hook.json"
hook = json.load(open(hook_path, encoding="utf-8"))
if not all_windows:
    hook["end"], hook["durationSec"] = float(hook.get("start", 0.0)) + t, t
    hook["reason"] = f"DRY RUN: trimmed to {t:.0f}s so {len(keep)} window(s) cover the song"
    json.dump(hook, open(hook_path, "w", encoding="utf-8"), indent=1)
    # THE BRIEF AND THE MAP ARE PART OF THE TRIM. The plan keeps four windows and
    # hook.wav is truncated to t — but the COPIED brief still places every line of the
    # whole song, and check_master reads the brief for
    # `no-singing-over-a-faceless-section`. So the proof compared 96s of lyrics against
    # a 25s film and reported 12 violations, every one of them past the film's own end
    # (baby-nimbooda, 2026-09-03; the same invariant on the real 13-window plan
    # measured 0). A proof that fails for being a proof teaches nothing, and it blocks
    # a clean film at the one gate that stands before the render slot.
    kept_ids = {j["id"] for j in keep}

    def _clamp(spans, a_key="start", b_key="end"):
        """Spans that survive a film ending at t, with their tails cut to it."""
        out = []
        for s in spans or []:
            a = float(s.get(a_key) or 0.0)
            b = float(s.get(b_key) or 0.0)
            if a >= t or b - a <= 0:
                continue
            s = dict(s)
            s[b_key] = round(min(b, t), 3)
            if s[b_key] - a <= 0.01:
                continue
            if "seconds" in s:
                s["seconds"] = round(s[b_key] - a, 3)
            out.append(s)
        return out

    bpath = f"{D}/build/vocal-brief.json"
    if os.path.isfile(bpath):
        brief = json.load(open(bpath, encoding="utf-8"))
        placed = [l for l in brief.get("lines", []) if l.get("start") is not None]
        unheard = [l for l in brief.get("lines", []) if l.get("start") is None]
        brief["lines"] = _clamp(placed) + unheard
        brief["windows"] = [w for w in brief.get("windows", []) if w.get("id") in kept_ids]
        json.dump(brief, open(bpath, "w", encoding="utf-8"), indent=1, ensure_ascii=False)
    mpath = f"{D}/build/vocal-map.json"
    if os.path.isfile(mpath):
        vmap = json.load(open(mpath, encoding="utf-8"))
        for key in ("silentStretches", "shortGaps", "energyStretches"):
            vmap[key] = _clamp(vmap.get(key))
        vmap["windows"] = [w for w in vmap.get("windows", []) if w.get("id") in kept_ids]
        for key in ("songSeconds", "vocalOut", "lyricsOut", "cutOut"):
            if isinstance(vmap.get(key), (int, float)):
                vmap[key] = round(min(float(vmap[key]), t), 3)
        json.dump(vmap, open(mpath, "w", encoding="utf-8"), indent=1, ensure_ascii=False)
print(f"dryrun: {len(keep)} window(s) {[j['id'] for j in keep]}, {t:.0f}s film" + (" (real geometry, gaps kept)" if all_windows else ""))
PY
[ $? -eq 0 ] || exit 2

ffmpeg_bin="${FFMPEG_FULL:-/opt/homebrew/opt/ffmpeg-full/bin/ffmpeg}"
song_len=$(python3 -c "import json;print(json.load(open('$DST/build/hook.json'))['durationSec'])")
"$ffmpeg_bin" -v error -y -i "$SRC/build/hook.wav" -t "$song_len" "$DST/build/hook.dry.wav" \
  && mv "$DST/build/hook.dry.wav" "$DST/build/hook.wav"

# Mark everything up to and including render-all as done. LINE-separated: the runner
# tests membership with `grep -qx`, so a space-separated file matches nothing and the
# whole pipeline re-runs from scaffold — which on a copy would re-enter the song stage.
printf '%s\n' $STAGES_DONE > "$DST/build/rap-state"

cd "$MAIN"
for pass in $(seq 1 16); do
  CAPTIONS=1 DELIVER="${DELIVER:-1080p}" CUT_AUTO=1 RAP_SKIP_SYNC="${RAP_SKIP_SYNC:-1}" \
    bash "$HERE/make-rap-mv.sh" "$PACK" "$SLUG-dryrun" >> "$DST/build/dryrun.log" 2>&1
  rc=$?
  printf 'pass %2d: exit=%-3s reached=%s\n' "$pass" "$rc" "$(tail -1 "$DST/build/rap-state")"
  [ "$rc" = "0" ] && { echo "=== DRY RUN COMPLETE — the pipeline reaches deliver"; exit 0; }
  [ "$rc" = "10" ] || { echo "=== FAILED (exit $rc is not a gate)"; tail -20 "$DST/build/dryrun.log"; exit "$rc"; }
done
echo "=== STOPPED: 16 passes without finishing — see $DST/build/dryrun.log" >&2
exit 10
