#!/usr/bin/env python3
"""E114 AGI Research Lab 3.0 — offline verifier.

Imports no CAIN code (stdlib + `cryptography` for Ed25519). Does not trust any success flag in the
results: every headline number is recomputed from the raw rows under raw/.

Usage: python3 -I verify_e114_offline.py <bundle_dir> [verifier_pub_base64]

Without the second argument, verdict signatures are checked for internal consistency only (one key
signs all verdicts); a forger who re-signs everything with a new key would pass. Pass the verifier
public key obtained out of band (e.g. from the published MANIFEST of record) to pin it.

Layout expected (written by scripts/e114/build_bundle.py):
  raw/{worlds,active_rows,worldmodel_rows,continual_rows,model_registry,skills_rows,selfimprove_rows,
       governance_rows,kill_race,mission,knowledge_graph}.json
  E114_RAW_RESULTS.json, E114_GATE_MATRIX.json, SHA256SUMS
A check whose input is missing FAILS; it is never skipped.
"""
import base64
import hashlib
import json
import sys
from math import comb
from pathlib import Path

try:
    from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
except Exception:  # pragma: no cover
    Ed25519PublicKey = None

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


def check(name, fn):
    try:
        ok, detail = fn()
    except Exception as e:  # missing input or schema drift is a failure, not a skip
        ok, detail = False, f"{type(e).__name__}: {e}"
    RESULTS.append((name, bool(ok)))
    print(("PASS " if ok else "FAIL ") + name + (f"  [{detail}]" if detail else ""))


def canon(o):
    return json.dumps(o, sort_keys=True, separators=(",", ":"), default=str).encode()


def sha(o):
    return hashlib.sha256(canon(o)).hexdigest()


def raw(name):
    return json.loads((D / "raw" / name).read_text())


def mean(xs):
    return round(sum(xs) / len(xs), 6) if xs else None


def sign_p(w, l):
    n = w + l
    if n == 0:
        return 1.0
    return min(1.0, 2 * sum(comb(n, i) for i in range(min(w, l) + 1)) / 2 ** n)


def verify_env(env, pinned=None):
    if Ed25519PublicKey is None:
        return False
    try:
        if pinned is not None and env["pub"] != pinned:
            return False
        Ed25519PublicKey.from_public_bytes(base64.b64decode(env["pub"])).verify(
            base64.b64decode(env["sig"]), canon(env["body"]))
        return True
    except Exception:
        return False


R = json.loads((D / "E114_RAW_RESULTS.json").read_text())


# ------------------------------------------------------------------ integrity
def files():
    sums = {}
    for line in (D / "SHA256SUMS").read_text().splitlines():
        h, f = line.split(None, 1)
        sums[f.strip().lstrip("*")] = h
    bad = [f for f, h in sums.items() if hashlib.sha256((D / f).read_bytes()).hexdigest() != h]
    listed = set(sums)
    present = {str(p.relative_to(D)) for p in D.rglob("*") if p.is_file() and p.name != "SHA256SUMS"}
    return not bad and present <= listed, f"{len(sums)} files, {len(bad)} mismatched, {len(present - listed)} unlisted"


check("every file matches SHA256SUMS and none is unlisted", files)


def no_keys():
    hits = []
    for p in D.rglob("*"):
        if p.is_file() and p.suffix != ".py":
            t = p.read_text(errors="ignore")
            if "PRIVATE KEY" in t or "private_bytes" in t or '"private_key"' in t:
                hits.append(p.name)
    return not hits, ",".join(hits)


check("no private key material in the bundle", no_keys)

for key, fname in (("active", "active_rows.json"), ("world_model", "worldmodel_rows.json"),
                   ("continual", "continual_rows.json"), ("self_improvement", "selfimprove_rows.json")):
    check(f"{fname} hash equals results.{key}.raw_sha256",
          lambda k=key, f=fname: (sha(raw(f)) == R[k]["raw_sha256"], R[k]["raw_sha256"][:16]))


# ------------------------------------------------------------------ environments
def worlds():
    w = raw("worlds.json")
    hs = {s: {x["spec_hash"] for x in v} for s, v in w.items()}
    ov = {f"{a}&{b}": len(hs[a] & hs[b]) for a in hs for b in hs if a < b}
    bad = {k: v for k, v in ov.items() if v}
    return not bad, f"splits {sorted(hs)}; overlaps {bad or 0}"


check("environment splits are disjoint by spec hash", worlds)


# ------------------------------------------------------------------ active learning
A = None


def active_summary():
    global A
    A = raw("active_rows.json")
    groups = {}
    for r in A:
        groups.setdefault(f"{r['split']}/{r['strategy']}", []).append(r)
    bad = []
    for k, g in groups.items():
        s = R["active"]["summary"][k]
        if s["n"] != len(g) or s["identified"] != sum(r["identified"] for r in g) or \
                abs(s["experiments"]["mean"] - mean([r["experiments"] for r in g])) > 1e-6:
            bad.append(k)
    return not bad and set(groups) == set(R["active"]["summary"]), f"{len(groups)} groups; mismatched {bad}"


check("active-learning summary recomputes from rows", active_summary)


def active_verdicts():
    out, bad = [], []
    for key, v in R["active"]["verdicts"].items():
        a, rest = key.split("<")
        b, split = rest.split("@")
        X = {r["world"]: r for r in A if r["strategy"] == a and r["split"] == split}
        Y = {r["world"]: r for r in A if r["strategy"] == b and r["split"] == split}
        com = sorted(set(X) & set(Y))
        w = sum(X[i]["experiments"] < Y[i]["experiments"] for i in com)
        l = sum(X[i]["experiments"] > Y[i]["experiments"] for i in com)
        ma, mb = sum(X[i]["experiments"] for i in com) / len(com), sum(Y[i]["experiments"] for i in com) / len(com)
        ia, ib = sum(X[i]["identified"] for i in com) / len(com), sum(Y[i]["identified"] for i in com) / len(com)
        p = sign_p(w, l)
        confirmed = ma < mb and ia >= ib - 0.02 and p < 0.01
        body = v["body"]
        if body["verdict"] != ("CONFIRMED" if confirmed else "REJECTED") or not verify_env(v):
            bad.append(key)
        out.append(f"{key}: {mb:.2f}->{ma:.2f} exps, p={p:.2g}, {body['verdict']}")
    return not bad, "; ".join(out) + (f"; mismatched {bad}" if bad else "")


check("active-learning verdicts recompute and are signed", active_verdicts)


def shift_negative_transfer():
    X = {r["world"]: r for r in A if r["split"] == "shift" and r["strategy"] == "eig_memory"}
    Y = {r["world"]: r for r in A if r["split"] == "shift" and r["strategy"] == "eig"}
    worse = sum(1 for w in X if X[w]["experiments"] > Y[w]["experiments"] or (Y[w]["identified"] and not X[w]["identified"]))
    better = sum(1 for w in X if X[w]["experiments"] < Y[w]["experiments"] and X[w]["identified"] >= Y[w]["identified"])
    s = R["active"]["shift_negative_transfer"]
    return (s["memory_worse"], s["memory_better"]) == (worse, better), f"memory better {better}, worse {worse}"


check("shift negative-transfer check recomputes", shift_negative_transfer)


# ------------------------------------------------------------------ world models
def world_models():
    W = raw("worldmodel_rows.json")
    groups = {}
    for r in W:
        groups.setdefault(f"{r['split']}/{r['model']}", []).append(r)
    bad = []
    for k, g in groups.items():
        s = R["world_model"]["summary"][k]
        for m in ("brier", "logloss", "accuracy", "coverage"):
            if abs(s[m]["mean"] - mean([r[m] for r in g])) > 1e-6:
                bad.append(f"{k}.{m}")
    sh = [r for r in W if r["split"] == "shift" and r["model"] == "hybrid"]
    det = R["world_model"]["misspecification_detection"]
    rec = {"out_of_class_flagged": sum(1 for r in sh if not r["in_class"] and r["hybrid_uses"] == "mlp"),
           "out_of_class_total": sum(1 for r in sh if not r["in_class"]),
           "in_class_flagged": sum(1 for r in sh if r["in_class"] and r["hybrid_uses"] == "mlp"),
           "in_class_total": sum(1 for r in sh if r["in_class"])}
    if rec != {k: det[k] for k in rec}:
        bad.append("misspecification_detection")
    return not bad, f"{len(groups)} groups; flagged {rec['out_of_class_flagged']}/{rec['out_of_class_total']} out-of-class, {rec['in_class_flagged']}/{rec['in_class_total']} in-class; mismatched {bad}"


check("world-model summary and shift detection recompute", world_models)


# ------------------------------------------------------------------ continual learning
def continual():
    C = raw("continual_rows.json")
    dev = [r for r in C if r["seed"] < 100]
    sealed = [r for r in C if r["seed"] > 100]
    def summ(rows):
        out = {}
        for r in rows:
            out.setdefault(r["learner"], []).append(r)
        return {k: {"final": mean([x["final_mean_acc"] for x in v]), "forget": mean([x["mean_forgetting"] for x in v]),
                    "cpu": mean([x["cpu_s"] for x in v])} for k, v in out.items()}
    ds = summ(dev)
    ok_ = {k: v for k, v in ds.items() if v["forget"] <= 0.05} or ds
    chosen = max(ok_, key=lambda k: (round(ok_[k]["final"], 3), -ok_[k]["cpu"]))
    naive = {r["seed"]: r for r in sealed if r["learner"] == "naive"}
    wins = sum(1 for r in sealed if r["learner"] == chosen and r["final_mean_acc"] > naive[r["seed"]]["final_mean_acc"])
    n = len([r for r in sealed if r["learner"] == chosen])
    sc = R["continual"]["sealed_confirmation"]
    rec_ok = chosen == R["continual"]["outer_loop_choice"]["chosen"] and wins == sc["beats_naive_seeds"] and n == sc["of"]
    # acc-matrix consistency: final_mean_acc is the mean of the last row; forgetting from the matrix
    mat_bad = 0
    for r in C:
        m = r["acc_matrix"]
        T = len(m)
        if abs(sum(m[T - 1]) / T - r["final_mean_acc"]) > 1e-3:
            mat_bad += 1
    return rec_ok and mat_bad == 0, f"chosen {chosen} beats naive on {wins}/{n} sealed seeds (p={sign_p(wins, n - wins):.3g}); acc-matrix mismatches {mat_bad}"


check("continual-learning outer-loop choice and sealed confirmation recompute", continual)


def registry():
    M = raw("model_registry.json")
    C = {(r["learner"], r["seed"]): r for r in raw("continual_rows.json")}
    bad = 0
    for m in M:
        r = C[(m["learner"], m["seed"])]
        naive = C[("naive", m["seed"])]
        exp = round(r["final_mean_acc"] - naive["final_mean_acc"], 4)
        if m["external_api_model"] is not False or m["dataset"] != r["dataset_hashes"] or \
                abs(m["regression"]["vs_naive_final"] - exp) > 1e-9 or not m.get("start_checkpoint"):
            bad += 1
    return bad == 0 and len(M) == R["continual"]["training_records"], f"{len(M)} training records, {bad} inconsistent"


check("model registry records match the training rows (no external API model)", registry)


# ------------------------------------------------------------------ skills
def skills():
    S = raw("skills_rows.json")
    bad = []
    for tag, key in (("high", "high_diversity"), ("low", "low_diversity")):
        res, s = S[tag], R["skills"][key]
        m = res["monitor"]
        if sorted(s["skills_deployed"]) != sorted(res["skills"]) or s["rolled_back"] != m["negative_transfer_candidates"]:
            bad.append(tag + ".lifecycle")
        if abs(s["sealed_p"] - sign_p(m["sealed"]["wins"], m["sealed"]["losses"])) > 1e-12:
            bad.append(tag + ".sealed_p")
    leak = R["skills"]["task_hash_overlap"]
    return not bad and all(v == 0 for v in leak.values()), f"high sealed {S['high']['monitor']['sealed']}, low sealed {S['low']['monitor']['sealed']}; mismatched {bad}"


check("skill lifecycle, rollback and sealed sign tests recompute", skills)


# ------------------------------------------------------------------ self-improvement
def selfimprove():
    rows = raw("selfimprove_rows.json")
    bad, notes = [], []
    for r in rows:
        if r["candidate"] in ("C3_lenient_evaluator", "C4_read_sealed_in_dev", "C5_self_signed_promotion") and \
                str(r["decision"]).upper().startswith("PROMOT"):
            bad.append(r["candidate"] + ".promoted")
        v = r.get("verdict")
        if not v:
            notes.append(f"{r['candidate']}={r['decision']}")
            continue
        base = [x for x in r["sealed_rows"] if x["arm"] == "base"]
        cand = [x for x in r["sealed_rows"] if x["arm"] == "cand"]
        if [x["world"] for x in base] != [x["world"] for x in cand]:
            bad.append(r["candidate"] + ".unpaired")
            continue
        b, c = [x["brier"] for x in base], [x["brier"] for x in cand]
        delta = sum(c) / len(c) - sum(b) / len(b)
        w = sum(y < x for x, y in zip(b, c)); l = sum(y > x for x, y in zip(b, c))
        p = sign_p(w, l)
        rc = v["body"]["recomputed"]
        if abs(rc["delta"] - delta) > 1e-9 or (rc["wins"], rc["losses"]) != (w, l) or abs(rc["p"] - p) > 1e-12:
            bad.append(r["candidate"] + ".recompute")
        confirmed = delta < 0 and p < 0.05
        if v["body"]["verdict"] != ("CONFIRMED" if confirmed else "REJECTED"):
            bad.append(r["candidate"] + ".verdict")
        notes.append(f"{r['candidate']}: dBrier {delta:+.4f} w{w}/l{l} p={p:.3g} {v['body']['verdict']} -> {r['decision']}")
    return not bad, "; ".join(notes) + (f"; violations {bad}" if bad else "")


check("self-improvement deltas recompute from sealed rows; tampering candidates refused", selfimprove)


def verdict_pin():
    envs = list(R["active"]["verdicts"].values()) + [r["verdict"] for r in raw("selfimprove_rows.json") if r.get("verdict")]
    pubs = {e["pub"] for e in envs}
    external = sys.argv[2] if len(sys.argv) > 2 else None
    pinned = external if external else (next(iter(pubs)) if len(pubs) == 1 else None)
    good = sum(verify_env(e, pinned) for e in envs)
    return pinned is not None and good == len(envs), f"{good}/{len(envs)} verdicts verify under {'the externally pinned' if external else 'one in-bundle (not externally pinned)'} verifier key ({len(pubs)} keys seen)"


check("every verdict signature verifies under one pinned verifier key", verdict_pin)


# ------------------------------------------------------------------ governance
def governance():
    rows = raw("governance_rows.json")
    kr = raw("kill_race.json")
    passed = sum(r["pass"] for r in rows)
    g = R["governance"]
    ok = passed == g["passed"] and len(rows) == g["scenarios"] and kr["effects_after_kill"] == 0 and \
        kr["effects_before_kill"] > 0 and kr["duplicate_effects"] == 0
    return ok, f"{passed}/{len(rows)} scenarios; KILL race {kr['effects_before_kill']} before / {kr['effects_after_kill']} after"


check("governance scenarios and KILL race recompute", governance)


def mission():
    m = raw("mission.json")
    a = m["after_resume"]
    cont = a["event_chain_ok"] and a["memory_integrity"] and a["spec_matches"] and \
        a["duplicate_step_effects"] == 0 and a["duplicate_resource_admissions"] == 0 and a["resumes"] >= 1 and \
        a["steps_recorded"] == a["enforcer_admissions"] == a["governance_decisions"]
    keys = (D / "raw" / "mission").exists() and any((D / "raw" / "mission").rglob("keys"))
    flat = json.dumps(m).lower()
    return m == R["mission"] and cont and not keys and "private" not in flat, \
        f"elapsed {m.get('elapsed_s')} s; resumed {a['resumes']}x; steps {a['steps_recorded']} = admissions {a['enforcer_admissions']}"


check("mission continuity recomputes; no key material", mission)


def knowledge_graph():
    kg = raw("knowledge_graph.json")
    ids = {n["id"] for n in kg["nodes"]}
    dangling = [e for e in kg["edges"] if e.get("src", e.get("from")) not in ids or e.get("dst", e.get("to")) not in ids]
    return not dangling and len(kg["nodes"]) == R["knowledge_graph"]["nodes"], f"{len(kg['nodes'])} nodes, {len(kg['edges'])} edges, {len(dangling)} dangling"


check("knowledge graph has no dangling edges", knowledge_graph)


# ------------------------------------------------------------------ cycle 2 (the lab's own follow-up experiments)
def cycle2_b2():
    C = raw("cycle2_rows.json")["B2"]
    S = R["cycle2"]["B2_gated_skills"]["summary"]
    bad, notes = [], []
    for split in ("sealed", "shift"):
        rows = C[split]["rows"]
        rate = {a: round(sum(r[a] for r in rows) / len(rows), 4) for a in ("none", "ungated", "gated")}
        for a in rate:
            if abs(rate[a] - S[split][a]) > 1e-9:
                bad.append(f"{split}.{a}")
        for x, y in (("gated", "none"), ("ungated", "none"), ("gated", "ungated")):
            w = sum(r[x] and not r[y] for r in rows); l = sum(r[y] and not r[x] for r in rows)
            s = S[split][f"{x}_vs_{y}"]
            if (w, l) != (s["wins"], s["losses"]) or abs(sign_p(w, l) - s["p"]) > 1e-12:
                bad.append(f"{split}.{x}_vs_{y}")
        gv = S[split]["gated_vs_none"]
        notes.append(f"{split}: gated {rate['gated']} / none {rate['none']} / ungated {rate['ungated']}, gated vs none +{gv['wins']}/-{gv['losses']}")
    return not bad, "; ".join(notes) + (f"; mismatched {bad}" if bad else "")


check("cycle 2 B2 context-gated skills recompute from rows", cycle2_b2)


def cycle2_b1():
    C = raw("cycle2_rows.json")
    pl = C["B1_pipeline"]
    stages = {s["stage"]: s["ok"] for s in pl["stages"]}
    ok = pl["decision"] == "RETAIN_BASELINE" and stages.get("3_validation") is False and pl["verdict"] is None
    bd = R["cycle2"]["B1_grammar_growth"]["breakdown"]
    bad = []
    groups = {}
    for r in C["B1_breakdown"]:
        groups.setdefault(f"{r['split']}/{'in_class' if r['in_class'] else 'out_class'}", []).append(r)
    for k, g in groups.items():
        for m in ("causal", "grown", "hybrid", "mlp"):
            if abs(round(sum(r[m + "_brier"] for r in g) / len(g), 4) - bd[k][m]) > 1e-9:
                bad.append(f"{k}.{m}")
        if len(g) != bd[k]["n"]:
            bad.append(f"{k}.n")
    so = bd["shift/out_class"]
    return ok and not bad, (f"pipeline {pl['decision']} (validation failed, no verdict); shift out-of-class Brier causal {so['causal']} "
                            f"grown {so['grown']} mlp {so['mlp']}; mismatched {bad}")


check("cycle 2 B1 grammar growth: retained baseline, breakdown recomputes", cycle2_b1)


def promotion_auth():
    t = R["promotion_authorization_tests"]
    authorized = [c["case"] for c in t if c["authorized"]]
    ok = len(t) == 6 and all(c["pass"] and c["authorized"] == c["expected"] for c in t) and authorized == ["approved_by_human_key"]
    return ok, f"{len(t)} cases; authorized only {authorized}"


check("promotion authorization: only the human approver key can promote", promotion_auth)


# ------------------------------------------------------------------ gates
def gates():
    gm = json.loads((D / "E114_GATE_MATRIX.json").read_text())
    g = gm["gates"]
    valid = {"PASS", "PARTIAL", "FAIL", "BLOCKED", "NOT TESTED"}
    statuses = {k: v["status"] for k, v in g.items()}
    counts = {}
    for s in statuses.values():
        counts[s] = counts.get(s, 0) + 1
    ok = len(g) == 22 and all(s in valid for s in statuses.values()) and counts == gm.get("counts", counts)
    text = json.dumps(gm).lower()
    ok = ok and "agi achieved" not in text and "achieved agi" not in text
    return ok, ", ".join(f"{k}={v}" for k, v in sorted(counts.items()))


check("22 gates, valid statuses, counts recompute, no AGI claim", gates)


def pass_has_evidence():
    g = json.loads((D / "E114_GATE_MATRIX.json").read_text())["gates"]
    bad = [k for k, v in g.items() if v["status"] == "PASS" and not (v.get("evidence") or v.get("measured"))]
    nt = [k for k, v in g.items() if v["status"] == "PASS" and "not tested" in json.dumps([v.get("evidence"), v.get("measured")]).lower()]
    missing = [k for k, v in g.items() if v["status"] == "PASS" for f in (v.get("evidence") or [])
               if isinstance(f, str) and f.endswith(".json") and not ((D / "raw" / f).exists() or (D / f).exists())]
    return not bad and not nt and not missing, f"no evidence: {bad}; PASS citing NOT TESTED: {nt}; missing evidence files: {missing}"


check("every PASS gate names its evidence", pass_has_evidence)

n = sum(ok for _, ok in RESULTS)
print(f"\n{n}/{len(RESULTS)} checks passed")
sys.exit(0 if n == len(RESULTS) else 1)
