"""F4 build loop: for each pass and vendor in sample.json order, copy the frozen bundle, run the production driver in the
scratch worktree (--no-refresh), restore the worktree, run the Jev sidecar, log. Stop rules from PREREG.md.
Needs OPENROUTER_API_KEY in the environment (sidecar + spend check); never prints it.
  python run_f4.py --passes 1 [--start-pass 1] [--max-vendors N]"""
import argparse, json, os, shutil, subprocess, sys, time
from pathlib import Path

F4 = Path("out/exp/f4")
W = Path("../ys-jev-shadow-2026-09-21")
PY = "python"
EVB = Path("../ys-eval-bundle/io/runs")
BASE_USAGE = 67.228502
CAP, RESERVE, TARGET_B = 5.00, 0.25, 110


def now():
    return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())


def log(msg):
    with open(F4 / "LOG.md", "a", encoding="utf-8") as f:
        f.write(f"- {now()} {msg}\n")
    print(msg, flush=True)


def spend():
    for attempt in range(4):
        try:
            out = subprocess.run([PY, str(F4 / "scripts" / "spend.py")], capture_output=True, text=True, timeout=60).stdout
            return round(json.loads(out)["usage"] - BASE_USAGE, 6)
        except Exception:
            time.sleep(5)
    raise SystemExit("spend check failed 4 times; stopping (cannot enforce the cap)")


def read_jsonl(p):
    if not p.exists():
        return []
    return [json.loads(l) for l in p.read_text(encoding="utf-8").split("\n") if l.strip()]


def totals():
    runs = [r for r in read_jsonl(F4 / "shadow_runs.jsonl") if not r.get("dry_run")]
    return (sum(r.get("accept_B", 0) for r in runs), sum(r.get("accept_A", 0) for r in runs),
            sum(r.get("in_scope", 0) for r in runs), len(runs))


def git(*a):
    return subprocess.run(["git", "-C", str(W), *a], capture_output=True, text=True)


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--passes", type=int, default=2)
    ap.add_argument("--start-pass", type=int, default=1)
    ap.add_argument("--max-vendors", type=int, default=10**6)
    a = ap.parse_args()
    env = dict(os.environ, PYTHONDONTWRITEBYTECODE="1")
    order = json.loads((F4 / "sample.json").read_text(encoding="utf-8"))["order"]
    done = {(r["pass"], r["slug"]) for r in read_jsonl(F4 / "driver_runs.jsonl")}
    consec_fail, n_run = 0, 0
    st0 = git("status", "-s").stdout.strip()
    if st0:
        log(f"STOP before start: worktree not clean: {st0!r}")
        return 1
    for p in range(a.start_pass, a.passes + 1):
        io = F4 / f"onboarding-io-p{p}"
        for v in order:
            slug, half = v["slug"], v["half"]
            if (p, slug) in done:
                continue
            b, aa, ins, nr = totals()
            if b >= TARGET_B:
                log(f"STOP: pooled arm-B accepts = {b} >= {TARGET_B} (arm A {aa}, in scope {ins}, {nr} sidecar runs)")
                return 0
            sp = spend()
            if sp + RESERVE > CAP:
                log(f"STOP: spend so far ${sp:.4f} + reserve ${RESERVE} would pass ${CAP}")
                return 0
            if consec_fail >= 3:
                log("STOP: 3 consecutive failed runs")
                return 1
            if n_run >= a.max_vendors:
                log(f"PAUSE: --max-vendors {a.max_vendors} reached (pass {p}, next {slug})")
                return 0
            n_run += 1
            rd = io / "runs" / slug
            if rd.exists():
                log(f"STOP: run folder already exists for pass {p} {slug}; refusing to overwrite")
                return 1
            shutil.copytree(EVB / slug / "bundle", rd / "bundle")
            dlog = F4 / "raw" / f"driver-p{p}-{slug}.log"
            s0, t0 = now(), time.time()
            with open(dlog, "w", encoding="utf-8") as f:
                rc = subprocess.run([PY, "-u", "scripts/agent/onboarding/onboard_vendor.py", "--slug", slug, "--no-refresh"],
                                    cwd=str(W), env=dict(env, YS_ONBOARDING_IO=str(io)), stdout=f, stderr=subprocess.STDOUT).returncode
            wall = round(time.time() - t0)
            co = git("checkout", "--", f"content/vendor-research/{slug}.md")
            st = git("status", "-s").stdout.strip()
            rec = {"pass": p, "slug": slug, "half": half, "args": "--no-refresh", "start": s0, "end": now(), "wall_s": wall,
                   "exit": rc, "restore_rc": co.returncode, "worktree_status_after_restore": st,
                   "commit": git("rev-parse", "HEAD").stdout.strip()}
            with open(F4 / "driver_runs.jsonl", "a", encoding="utf-8") as f:
                f.write(json.dumps(rec) + "\n")
            if st:
                log(f"STOP: worktree not clean after restoring {slug} (pass {p}): {st!r}")
                return 1
            sdir = rd / "stages"
            if not ((sdir / "dossier.md").exists() and (sdir / "defects.json").exists()):
                consec_fail += 1
                log(f"pass {p} {slug} (half {half}): driver exit {rc}, NO dossier (failed run {consec_fail} in a row); "
                    f"spend so far ${spend():.4f}")
                continue
            consec_fail = 0
            setname = "primary" if (sdir / "03.json").exists() else "secondary"
            slog = F4 / "raw" / f"sidecar-p{p}-{slug}.log"
            with open(slog, "w", encoding="utf-8") as f:
                src = subprocess.run([PY, str(F4 / "sidecar" / "jev_shadow.py"), str(rd), "--exit", str(rc), "--pass", str(p),
                                      "--half", str(half), "--set", setname], cwd=str(F4), env=env, stdout=f,
                                     stderr=subprocess.STDOUT).returncode
            summ = {}
            try:
                summ = json.loads(slog.read_text(encoding="utf-8").strip().split("\n")[-1])
            except Exception:
                pass
            b, aa, ins, nr = totals()
            usage = {}
            if (sdir / "usage.json").exists():
                usage = json.loads((sdir / "usage.json").read_text(encoding="utf-8"))
            log(f"pass {p} {slug} (half {half}, {setname}): driver exit {rc}, {wall}s, pipeline ${usage.get('total_cost_usd')}; "
                f"sidecar rc {src}: in scope {summ.get('in_scope')}, accept A {summ.get('accept_A')}, accept B "
                f"{summ.get('accept_B')}, jev errors {summ.get('jev_errors')}, jev ${summ.get('sidecar_cost_usd')}; "
                f"TOTAL arm-B accepts {b}, arm-A {aa}, in scope {ins}, drafts {nr}; spend so far ${spend():.4f}")
            if src != 0:
                log(f"STOP: sidecar failed for {slug} (rc {src}); see {slog.name}")
                return 1
        log(f"pass {p} complete")
    b, aa, ins, nr = totals()
    log(f"END: passes done; arm-B accepts {b}, arm-A {aa}, in scope {ins}, drafts {nr}; spend ${spend():.4f}")
    return 0


if __name__ == "__main__":
    sys.exit(main())
