#!/usr/bin/env python3
"""Standalone verifier for the E85 frontier evidence bundle. No CAIN imports."""
import base64, hashlib, json, math, sys
from pathlib import Path
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey

def canon(o): return json.dumps(o, sort_keys=True, separators=(",", ":"), default=str).encode()
def sha(o): return hashlib.sha256(o if isinstance(o,bytes) else canon(o)).hexdigest()
def merkle(leaves):
    def mth(xs):
        if not xs: return hashlib.sha256(b"").digest()
        if len(xs)==1: return hashlib.sha256(b"\x00"+xs[0]).digest()
        k=1
        while k*2<len(xs): k*=2
        return hashlib.sha256(b"\x01"+mth(xs[:k])+mth(xs[k:])).digest()
    return mth(list(leaves)).hex()
def wilson(k,n,z=1.96):
    p=k/n; d=1+z*z/n; c=p+z*z/(2*n); h=z*math.sqrt(p*(1-p)/n+z*z/(4*n*n))
    return [round((c-h)/d,4), round((c+h)/d,4)]
def mcnemar(b,c):
    n=b+c
    if n==0: return 1.0
    m=min(b,c); tail=sum(math.comb(n,k) for k in range(m+1))/(2**n)
    return round(min(1.0,2*tail),8)

def main():
    B=Path(sys.argv[1]).resolve(); C=[]
    def check(n,ok,d=""): C.append((n,bool(ok),d))
    man=json.loads((B/"manifest.json").read_text()); body=man["body"]
    dg=sha(body)
    try:
        Ed25519PublicKey.from_public_bytes(base64.b64decode(man["public_key_b64"])).verify(
            base64.b64decode(man["signature_b64"]), canon(body)); check("manifest_signature_valid",True)
    except Exception: check("manifest_signature_valid",False)
    bad=[f["path"] for f in body["files"] if not (B/f["path"]).is_file() or
         hashlib.sha256((B/f["path"]).read_bytes()).hexdigest()!=f["sha256"]]
    check("every_file_matches_digest",not bad,str(bad[:3]))
    leaves=[f"{f['path']}:{f['sha256']}".encode() for f in sorted(body["files"],key=lambda x:x["path"])]
    check("merkle_root_recomputed",merkle(leaves)==body["merkle_root"])
    check("no_private_key_material",not any(b"PRIVATE KEY" in (B/f["path"]).read_bytes() or b"sk-ant-" in (B/f["path"]).read_bytes()
          for f in body["files"] if not f["path"].endswith(".py")))
    rows=[json.loads(x) for x in (B/"eval/rows.jsonl").read_text().splitlines() if x.strip()]
    held=[r for r in rows if r["split"]=="heldout"]
    by={}
    for r in held: by.setdefault(r["arm"],{})[r["task_id"]]=r
    summ=json.loads((B/"summary.json").read_text())
    for arm,d in summ["arms"].items():
        rs=list(by.get(arm,{}).values()); k=sum(r["correct"] for r in rs)
        check(f"{arm}_accuracy_recomputed", k==d["correct"] and len(rs)==d["n"] and wilson(k,len(rs))==d["wilson95"],
              f"{k}/{len(rs)}")
    ids=sorted(set(by["A_single"])&set(by["B_learned"]))
    b=sum(by["B_learned"][i]["correct"] and not by["A_single"][i]["correct"] for i in ids)
    a=sum(by["A_single"][i]["correct"] and not by["B_learned"][i]["correct"] for i in ids)
    check("paired_mcnemar_recomputed", (b,a,mcnemar(a,b))==(summ["comparison"]["b_only"],summ["comparison"]["a_only"],summ["comparison"]["mcnemar_p"]),
          f"{b}/{a} p={mcnemar(a,b)}")
    calls=[json.loads(x) for x in (B/"eval/model_calls.jsonl").read_text().splitlines() if x.strip()]
    classes={c.get("execution_class") for c in calls}
    check("all_calls_are_real_remote", classes=={"REAL_REMOTE_MODEL"}, str(classes))
    check("single_model_declared", {c.get("model") for c in calls}=={summ["model"]}, str({c.get("model") for c in calls}))
    pre=json.loads((B/"eval/preregistration.json").read_text())
    first=min(c["utc"] for c in calls)
    check("preregistered_before_first_call", pre["registered_utc"]<=first)
    check("cost_within_cap", summ["estimated_cost_usd"]<=8.0, f"${summ['estimated_cost_usd']}")
    rep=(B/"FRONTIER_REPORT.md").read_text()
    check("report_states_the_numbers", f"{summ['arms']['B_learned']['correct']}/100" in rep and
          f"{summ['arms']['A_single']['correct']}/100" in rep and "not AGI" in rep)
    check("no_agi_claim", "not AGI" in rep and "not general intelligence" in rep)
    for n,ok,d in C: print(("PASS " if ok else "FAIL ")+n+(f"  ({d})" if d else ""))
    print(f"\n{sum(1 for _,o,_ in C if o)}/{len(C)} checks passed")
    return 0 if all(o for _,o,_ in C) else 1
if __name__=="__main__": sys.exit(main())
