#!/usr/bin/env python3
"""Standalone verifier for the E115 INTERIM bundle. Standard library only; imports no CAIN code.
Recomputes every summary from raw rows and checks the pins. Usage: python3 -I verify_e115_interim.py <bundle_dir>"""
import hashlib
import json
import sys
from pathlib import Path

D = Path(sys.argv[1] if len(sys.argv) > 1 else ".")
R = []


def check(name, fn):
    try:
        ok, detail = fn()
    except Exception as e:  # a crash is a failure, never a pass
        ok, detail = False, f"{type(e).__name__}: {e}"
    R.append(ok)
    print(("PASS " if ok else "FAIL ") + name + (f"  [{detail}]" if detail else ""))


def sha_file(p):
    return hashlib.sha256(Path(p).read_bytes()).hexdigest()


def load(n):
    return json.loads((D / n).read_text())


def sums():
    bad = []
    for line in (D / "SHA256SUMS").read_text().splitlines():
        h, f = line.split(None, 1)
        if sha_file(D / f) != h:
            bad.append(f)
    return not bad, f"{len((D / 'SHA256SUMS').read_text().splitlines())} files, mismatched {bad}"


def pins():
    st = load("INTERIM_STATUS.json")
    p = st["pins"]
    ok = sha_file(D / "raw/E115_PREREGISTRATION.json") == p["preregistration_sha256"] and \
        sha_file(D / "raw/agi_benchmark_status.json") == p["agi_benchmark_status_sha256"]
    return ok, f"prereg {p['preregistration_sha256'][:12]}, benchmark status {p['agi_benchmark_status_sha256'][:12]}"


def track_a():
    st = load("INTERIM_STATUS.json")["trackA_diagnostic"]
    mism = []
    for name, claimed in st.items():
        rows = load(f"raw/{name}.json")["rows"]
        for arm, c in claimed["summary"].items():
            rs = [r for r in rows if r["arm"] == arm]
            nr = round(sum(r["novel_recall"] for r in rs) / len(rs), 4)
            fh = round(sum(r["full_hit"] for r in rs) / len(rs), 4)
            if (len(rs), nr, fh) != (c["questions"], c["novel_recall"], c["full_hit"]):
                mism.append(f"{name}/{arm}")
        if load(f"raw/{name}.json").get("model_calls") != 0:
            mism.append(f"{name}: model_calls != 0")
    return not mism, f"{len(st)} runs recomputed; mismatches {mism}"


def track_b():
    st = load("INTERIM_STATUS.json")["trackB_attack_acceptance"]
    mism = []
    for name, claimed in st.items():
        audits = load(f"raw/{name}.json")["audits"]
        for arm, c in claimed.items():
            a = [x for x in audits if x["arm"] == arm]
            got = {"catchable_accepted": sum(x["catchable_accepted"] for x in a), "residual_accepted": sum(x["residual_accepted"] for x in a)}
            if got != {k: c[k] for k in got}:
                mism.append(f"{name}/{arm}")
    return not mism, f"mismatches {mism}"


def track_c():
    d = load("raw/trackC_sealed_P0P1.json")
    summ = {}
    for r in d["rows"]:
        k = f"{r['split']}/{r['proposer']}"
        s = summ.setdefault(k, {"n": 0, "identified": 0, "false_confirmations": 0, "accepted": 0})
        s["n"] += 1; s["identified"] += r["identified"]; s["false_confirmations"] += r["false_confirmation"]
        s["accepted"] += r["status"] == "ACCEPTED"
    claimed = load("INTERIM_STATUS.json")["trackC_sealed"]
    fc_ok = all(r["false_confirmation"] == (r["status"] == "ACCEPTED" and not r["identified"]) for r in d["rows"])
    return summ == claimed and fc_ok and d["model_calls"] == 0, json.dumps(summ)


def blocked_is_honest():
    st = load("INTERIM_STATUS.json")
    led = (D / "raw/spend.jsonl").read_text().splitlines() if (D / "raw/spend.jsonl").exists() else []
    paid_ok = [json.loads(l) for l in led if json.loads(l)["status"] == 200]
    return st["paid_model_arms"] == "BLOCKED" and not paid_ok, f"{len(led)} proxy calls, {len(paid_ok)} succeeded (credit exhausted)"


def no_overclaim():
    t = (D / "INTERIM_STATUS.json").read_text().lower() + (D / "README.md").read_text().lower()
    bad = [p for p in ("agi achieved", "achieved agi", "is agi.", "passed the benchmark") if p in t]
    b = load("raw/agi_benchmark_status.json")
    ran = [x["name"] for x in b["benchmarks"] if x["status"] == "RAN"]
    return not bad and "not yet agi" in t, f"forbidden phrases {bad}; benchmarks marked RAN: {ran}"


check("SHA256SUMS match", sums)
check("preregistration and benchmark-status pins", pins)
check("Track A diagnostic summaries recompute from rows (0 model calls)", track_a)
check("Track B attack acceptance recomputes from audits", track_b)
check("Track C sealed summaries recompute; false confirmation = accepted and not identified", track_c)
check("paid arms honestly BLOCKED (no successful paid call)", blocked_is_honest)
check("no AGI overclaim; 'not yet AGI' stated", no_overclaim)
n = sum(R)
print(f"\n{n}/{len(R)} checks passed")
sys.exit(0 if n == len(R) else 1)
