#!/usr/bin/env python3
"""E124 standalone verifier: 6/6 CAIN-native long-horizon pilot, honest ceiling."""
from __future__ import annotations
import hashlib, json, sys
from pathlib import Path
C=[]
def check(n,ok,d=""): C.append((n,bool(ok),str(d)))
def main():
    root=Path(sys.argv[1] if len(sys.argv)>1 else ".")
    import ast
    tree=ast.parse(Path(__file__).read_text()); banned=[]
    for node in ast.walk(tree):
        if isinstance(node,ast.Import): banned += [a.name for a in node.names if a.name.split(".")[0]=="cain"]
        elif isinstance(node,ast.ImportFrom) and (node.module or "").split(".")[0]=="cain": banned.append(node.module)
    check("verifier imports no CAIN code", not banned, banned)
    man=json.loads((root/"MANIFEST.json").read_text())
    bad=[r for r,h in man["files"].items() if not (root/r).exists() or hashlib.sha256((root/r).read_bytes()).hexdigest()!=h]
    check(f"manifest hashes ({len(man['files'])})", not bad, bad[:5])
    d=json.loads((root/"E124_SUITE.json").read_text())
    rows=d["rows"]
    check("six tasks",len(rows)==6,len(rows))
    check("18 attempts", d.get("attempts")==18, d.get("attempts"))
    check("at least 10 successes", sum(r["successes"] for r in rows)>=10, sum(r["successes"] for r in rows))
    check("model recorded", "Llama-3.3-70B" in d["model"], d["model"])
    check("finite p50 computed", isinstance(d["fit"]["p50_minutes"], (int,float)), d["fit"])
    check("p50 is bounded", 1 < d["fit"]["p50_minutes"] < 10000, d["fit"])
    check("limits say not comparable", any("not comparable" in x for x in d["limits"]), d["limits"])
    passed=sum(1 for _,ok,_ in C if ok)
    for n,ok,x in C: print(("PASS " if ok else "FAIL ")+n+(f" [{x}]" if (not ok and x) else ""))
    print(f"\n{passed}/{len(C)} checks passed")
    return 0 if passed==len(C) else 1
if __name__=="__main__": raise SystemExit(main())
