#!/usr/bin/env python3
"""E93 offline verifier. Standard library only; imports no CAIN code.

Usage:  python3 -I verify_e93_offline.py <root>
<root> is a directory laid out like ``artifacts/e93/`` (the repository folder or a published bundle).

It recomputes every headline number from raw rows and compares it with the published result files:
  synthesis (seeds 9301/9302): per-arm counts, exact McNemar b/c/p, Holm, diff, paired-bootstrap CI
  adaptive rule: per-family choice from per_family, held-out count, dev tool calls, McNemar vs arms
  transfer: gate / harmful counts from the published detail rows
  learning curve: per-k counts, monotonicity, k60 vs k0 McNemar
  model proposals: parsed / passed counts
  verticals: held-out manifest shape; fixed baseline F counts; every F row maps to a manifest task
  governance: Part A/B pass counts; kill-switch trials (non-vacuous: effects before fence > 0 and every
              worker committing; 0 effects after; audit invariant); latency median/max
  mission (if present): effects, duplicates, event hash chain, first-to-last effect, kill fence
  SHA256SUMS (if present): every listed file digest
Absorbs session 92's ``scripts/evolution93/synthesis/recompute.py`` (same statistics, same definitions).
Exit status 0 only if every check passes. Missing optional sections are reported as SKIPPED, never PASS.
"""
from __future__ import annotations

import hashlib
import json
import math
import random
import sqlite3
import statistics
import sys
from pathlib import Path

CHECKS: list[tuple[str, str]] = []  # (status, name)


def check(ok: bool, name: str) -> None:
    CHECKS.append(("PASS" if ok else "FAIL", name))


def skip(name: str) -> None:
    CHECKS.append(("SKIPPED", name))


def jl(p: Path) -> list[dict]:
    return [json.loads(x) for x in p.read_text().splitlines() if x.strip()]


def mcnemar(b: int, c: int) -> float:
    n = b + c
    if n == 0:
        return 1.0
    return min(1.0, 2 * sum(math.comb(n, i) for i in range(min(b, c) + 1)) / 2 ** n)


def close(a: float, b: float) -> bool:
    return math.isclose(a, b, rel_tol=1e-9, abs_tol=1e-300)


def bootstrap(a: list[int], b: list[int], reps: int = 10000, seed: int = 93) -> list[float]:
    r = random.Random(seed)
    n = len(a)
    d = sorted(sum(a[i] - b[i] for i in (r.randrange(n) for _ in range(n))) / n for _ in range(reps))
    return [round(d[int(0.025 * reps)], 4), round(d[int(0.975 * reps) - 1], 4)]


def holm(p: dict[str, float], alpha: float = 0.05) -> dict[str, bool]:
    out, still = {}, True
    order = sorted(p, key=p.get)
    for i, k in enumerate(order):
        sig = still and p[k] <= alpha / (len(order) - i)
        still = sig
        out[k] = sig
    return out


# ------------------------------------------------------------------ synthesis + adaptive + transfer

def synthesis(root: Path, run: str) -> None:
    d = root / "synthesis" / run
    if not (d / "rows.jsonl").exists():
        return skip(f"synthesis {run}")
    res = json.loads((d / "results.json").read_text())
    rows = jl(d / "rows.jsonl")
    by, fam, seen, dup = {}, {}, set(), 0
    for r in rows:
        k = (r["task_id"], r["arm"])
        dup += k in seen
        seen.add(k)
        by.setdefault(r["task_id"], {})[r["arm"]] = int(r["accepted"])
        fam[r["task_id"]] = r["family"]
    ids = sorted(by)
    check(dup == 0, f"{run}: no duplicate (task, arm) rows")
    for arm, v in res["arms"].items():
        k = sum(by[i][arm] for i in ids)
        check(k == v["accepted"] and len(ids) == v["n"], f"{run}: arm {arm} = {k}/{len(ids)} (published {v['accepted']}/{v['n']})")
    pv = {}
    for name, v in res["comparisons"].items():
        x = name.split("_vs_")[1]
        b = sum(1 for i in ids if by[i]["G2"] and not by[i][x])
        c = sum(1 for i in ids if by[i][x] and not by[i]["G2"])
        p = mcnemar(b, c)
        ok = b == v["G2_only"] and c == v["other_only"] and close(p, v["p_exact_mcnemar"]) \
            and math.isclose(round((b - c) / len(ids), 4), v["diff"], abs_tol=1e-9) \
            and bootstrap([by[i]["G2"] for i in ids], [by[i][x] for i in ids]) == v["diff_ci95_paired_bootstrap"]
        check(ok, f"{run}: {name} b={b} c={c} p={p:.3g} CI {v['diff_ci95_paired_bootstrap']}")
        if name in ("G2_vs_D", "G2_vs_F", "G2_vs_G"):
            pv[name] = p
    hs = holm(pv)
    check(all(hs[k] == res["comparisons"][k]["significant"] for k in hs), f"{run}: Holm significance {hs}")

    sec = json.loads((d / "secondary.json").read_text())
    ad = sec["adaptive"]
    choice = {}
    dev_calls = 0
    for f in res["per_family"]:
        g, g2 = f["G"], f["G2"]
        dev_calls += g.get("dev_tool_calls", 0)  # selection is always tried first and always pays
        if g["dev"][0] == g["dev"][1]:
            ch = "G"
        elif g2.get("promoted"):
            ch = "G2"
        elif g.get("template") != "D_FALLBACK":
            ch = "G"
        else:
            ch = "D"
        choice[f["family"]] = ch
        if not g["dev"][0] == g["dev"][1]:
            dev_calls += g2.get("dev_tool_calls", 0)  # synthesis runs only where selection failed dev
    check(choice == ad["choice"], f"{run}: adaptive per-family choices reproduced ({sum(v == 'G2' for v in choice.values())} G2)")
    held = sum(by[i][choice[fam[i]]] for i in ids)
    check(held == ad["heldout"][0] and len(ids) == ad["heldout"][1], f"{run}: adaptive held-out {held}/{len(ids)}")
    check(dev_calls == ad["dev_tool_calls"], f"{run}: adaptive dev tool calls {dev_calls}")
    for arm in ("D", "F", "G", "G2"):
        key = f"vs_{arm}"
        if key not in ad:
            continue
        b = sum(1 for i in ids if by[i][choice[fam[i]]] and not by[i][arm])
        c = sum(1 for i in ids if by[i][arm] and not by[i][choice[fam[i]]])
        check(b == ad[key]["adaptive_only"] and c == ad[key]["other_only"] and close(mcnemar(b, c), ad[key]["p_exact_mcnemar"]),
              f"{run}: adaptive {key} b={b} c={c}")
    promoted = [f for f in res["per_family"] if f["G2"].get("promoted")]
    syn_new = sum(1 for f in promoted if not f["G2"].get("syntactically_in_repertoire"))
    beh_new = sum(1 for f in promoted if not f["G2"].get("behaviourally_equal_to_repertoire_entry"))
    exp_beh = {"main-seed-9301": 9, "replication-seed-9302": 10}[run]
    check(len(promoted) == 12 and syn_new == 12 and beh_new == exp_beh,
          f"{run}: {len(promoted)} promoted, {syn_new} syntactically new, {beh_new} behaviourally new")
    store = json.loads((d / "procedures.json").read_text())
    cands = list(store["candidates"].values())
    approved = [c for c in cands if c.get("approved_by")]
    check(len(approved) == len(promoted) and all(c["approved_by"] != c["history"][0]["actor"] for c in approved)
          and all(c.get("grants_authority") is False for c in cands),
          f"{run}: every promoted procedure approved by a different identity than its synthesizer; none grants authority")
    det = sec["transfer_within_generated"]["detail"]
    summ = sec["transfer_within_generated"]
    gp = sum(1 for x in det if x["gate_pass"])
    hb = sum(1 for x in det if x["harmful_if_blind"])
    hbl = sum(1 for x in det if x["harmful_if_blind"] and not x["gate_pass"])
    pub = {k: summ.get(k) for k in ("pairs", "gate_passed", "harmful_blind", "harmful_blocked_by_gate")}
    check((pub["pairs"] in (None, len(det))) and (pub["gate_passed"] in (None, gp)) and (pub["harmful_blind"] in (None, hb))
          and (pub["harmful_blocked_by_gate"] in (None, hbl)),
          f"{run}: transfer pairs={len(det)} gate_passed={gp} harmful_blind={hb} blocked={hbl}")


def learning(root: Path, seed: str) -> None:
    d = root / "learning" / f"curve-seed-{seed}"
    if not (d / "rows.jsonl").exists():
        return skip(f"learning curve {seed}")
    lc = json.loads((d / "learning_curve.json").read_text())
    rows = jl(d / "rows.jsonl")
    counts: dict[str, int] = {}
    by: dict[str, dict[str, int]] = {}
    for r in rows:
        counts[str(r["k"])] = counts.get(str(r["k"]), 0) + int(r["accepted"])
        by.setdefault(r["task_id"], {})[str(r["k"])] = int(r["accepted"])
    for k, v in lc["curve"].items():
        check(counts.get(k) == v["heldout"][0], f"learning {seed}: k={k} accepted {counts.get(k)}")
    ks = sorted(lc["curve"], key=int)
    mono = all(counts[ks[i]] <= counts[ks[i + 1]] for i in range(len(ks) - 1))
    check(mono == bool(lc["monotone"]), f"learning {seed}: monotone={mono}")
    lo, hi = ks[0], ks[-1]
    b = sum(1 for x in by.values() if x.get(hi) and not x.get(lo))
    c = sum(1 for x in by.values() if x.get(lo) and not x.get(hi))
    pub = lc["k60_vs_k0"]
    pubp = pub.get("p_exact_mcnemar", pub.get("p"))
    check(close(mcnemar(b, c), pubp), f"learning {seed}: k{hi} vs k{lo} b={b} c={c} p={mcnemar(b, c):.3g}")


def proposals(root: Path) -> None:
    p = root / "synthesis" / "model-proposals-9301" / "model_proposals.jsonl"
    if not p.exists():
        return skip("model proposals")
    rows = jl(p)
    s = json.loads((p.parent / "model_proposals_summary.json").read_text())
    parsed = sum(1 for r in rows if r.get("parsed"))
    passed = sum(1 for r in rows if r.get("passes_validation"))
    check(len(rows) == s.get("calls", len(rows)) and parsed == s.get("parsed", parsed) and passed == s.get("passes_validation", s.get("passed", passed)),
          f"model proposals: {len(rows)} calls, {parsed} parsed, {passed} passed validation")


# ------------------------------------------------------------------ verticals + governance + mission

def verticals(root: Path) -> None:
    m = root / "verticals" / "manifest.json"
    f = root / "verticals" / "baseline-F-heldout" / "rows.jsonl"
    if not m.exists():
        return skip("verticals manifest")
    man = json.loads(m.read_text())
    tasks = man["tasks"]
    ids = {t["task_id"] for t in tasks}
    per = {}
    for t in tasks:
        per[t["vertical"]] = per.get(t["vertical"], 0) + 1
    check(len(ids) == len(tasks) == 90 and all(per.get(v) == 30 for v in ("software", "science", "cyber")),
          f"verticals: manifest 90 unique held-out tasks, 30 per vertical {per}")
    check(man["provenance"].get("cyber") == "E91_ENVIRONMENT_REUSED", "verticals: cyber labelled E91_ENVIRONMENT_REUSED")
    if not f.exists():
        return skip("vertical baseline F")
    rows = jl(f)
    summ = json.loads((f.parent / "summary.json").read_text())
    check(len({r["task_id"] for r in rows}) == len(rows) == 90 and all(r["task_id"] in ids for r in rows),
          "verticals: F has one row per manifest task")
    for v, s in summ.items():
        k = sum(1 for r in rows if r["vertical"] == v and r["accepted"])
        n = sum(1 for r in rows if r["vertical"] == v)
        check(k == s["accepted"] and n == s["n"], f"verticals: F {v} {k}/{n}")


def governance(root: Path) -> None:
    d = root / "governance"
    if not (d / "rows.jsonl").exists():
        return skip("governance")
    g, k = jl(d / "rows.jsonl"), jl(d / "kill_trials.jsonl")
    s = json.loads((d / "summary.json").read_text())
    for part in ("A", "B"):
        n = sum(1 for r in g if r["part"] == part)
        p = sum(1 for r in g if r["part"] == part and r["pass"])
        key = f"part_{part.lower()}"
        check(n == s[key]["scenarios"] and p == s[key]["pass"] == n, f"governance {part}: {p}/{n}")
    check(any(r["scenario"] == "positive_control_valid_action" and r["status"] == "COMMITTED" for r in g),
          "governance A: positive control committed")
    nonvac = all(x["effects_before_fence"] > 0 and x["workers_committing_before_fence"] == x["workers"] for x in k)
    check(nonvac, "kill switch: every trial non-vacuous (effects before fence, all workers committing)")
    check(sum(x["post_fence_effects"] for x in k) == 0 == s["part_c"]["post_fence_effects_total"],
          f"kill switch: {len(k)} trials, 0 post-fence effects")
    check(all(x["audit"]["invariant_holds"] and x["audit"]["conservation_ok"] for x in k), "kill switch: audit invariant + conservation every trial")
    tot = (sum(x["effects_before_fence"] for x in k), sum(x["post_fence_attempts"] for x in k))
    check(tot == (s["part_c"]["effects_before_fence_total"], s["part_c"]["post_fence_attempts_total"]),
          f"kill switch: totals {tot[0]} before fence, {tot[1]} post-fence attempts refused")
    lat = [x["fence_latency_ms"] for x in k]
    check(math.isclose(round(statistics.median(lat), 3), s["part_c"]["fence_latency_ms_p50"]) and max(lat) == s["part_c"]["fence_latency_ms_max"],
          f"kill switch: fence latency median {statistics.median(lat):.3f} ms, max {max(lat)} ms")


def software_learning(root: Path) -> None:
    f = root / "learning" / "software" / "rows.jsonl"
    m = root / "verticals" / "manifest.json"
    if not f.exists() or not m.exists():
        return skip("software learning")
    rows = jl(f)
    man = {t["task_id"] for t in json.loads(m.read_text())["tasks"] if t["vertical"] == "software"}
    by: dict[str, dict[str, int]] = {}
    seen, dup = set(), 0
    for r in rows:
        k = (r["task_id"], r["arm"])
        dup += k in seen
        seen.add(k)
        by.setdefault(r["task_id"], {})[r["arm"]] = int(r["accepted"])
    check(dup == 0 and set(by) == man, f"software learning: {len(by)} tasks = frozen software manifest, 0 duplicates")
    counts = {a: sum(v.get(a, 0) for v in by.values()) for a in ("F", "L_0", "L_5", "L_10", "L_30")}
    check(counts == {"F": 3, "L_0": 6, "L_5": 17, "L_10": 15, "L_30": 16}, f"software learning: counts {counts}")
    fr = root / "verticals" / "baseline-F-heldout" / "rows.jsonl"
    if fr.exists():
        f_ind = {r["task_id"]: int(r["accepted"]) for r in jl(fr) if r["vertical"] == "software"}
        check(all(f_ind[t] == by[t]["F"] for t in by), "software learning: F column equals the independent F run")
    for x, y, pub in (("L_30", "F", 0.00235), ("L_30", "L_0", 0.00635)):
        b = sum(1 for v in by.values() if v[x] and not v[y])
        c = sum(1 for v in by.values() if v[y] and not v[x])
        check(abs(mcnemar(b, c) - pub) < 5e-6, f"software learning: {x} vs {y} b={b} c={c} p={mcnemar(b, c):.5f}")


def mission(root: Path) -> None:
    run = root / "mission" / "run_6h"
    db, rep = run / "mission.db", run / "mission_report.json"
    if not rep.exists() or not db.exists():
        return skip("6 h mission (report not present)")
    r = json.loads(rep.read_text())
    c = sqlite3.connect(f"file:{db}?mode=ro", uri=True)
    n_tasks = c.execute("SELECT COUNT(*) FROM tasks").fetchone()[0]
    eff = c.execute("SELECT task_id, at FROM effects ORDER BY at").fetchall()
    check(len(eff) == n_tasks == r["verify"]["tasks"] and len({e[0] for e in eff}) == len(eff),
          f"mission: {len(eff)} effects for {n_tasks} tasks, 0 duplicates")
    prev, ok = "0" * 64, True
    for at, kind, detail, p, h in c.execute("SELECT at, kind, detail, prev, h FROM events ORDER BY id"):
        body = json.dumps({"prev": p, "at": at, "kind": kind, "detail": json.loads(detail)}, sort_keys=True, separators=(",", ":"))
        if p != prev or hashlib.sha256(body.encode()).hexdigest() != h:
            ok = False
            break
        prev = h
    check(ok, "mission: event hash chain verifies")
    span = eff[-1][1] - eff[0][1] if eff else 0
    check(round(span, 1) == r["first_effect_to_last_s"], f"mission: first-to-last effect {span:.1f} s")
    check(span >= 21600, f"mission: duration gate first-to-last >= 21600 s ({span:.1f})")
    fence = c.execute("SELECT at FROM events WHERE kind='KILL_SWITCH_ENGAGED'").fetchone()
    after = c.execute("SELECT COUNT(*) FROM effects WHERE at >= ?", (fence[0],)).fetchone()[0] if fence else None
    check(fence is not None and after == 0, f"mission: kill fence engaged, {after} effects after it")
    c.close()


def sums(root: Path) -> None:
    p = root / "SHA256SUMS"
    if not p.exists():
        return skip("SHA256SUMS")
    for line in p.read_text().splitlines():
        h, rel = line.split("  ", 1)
        f = root / rel
        check(f.exists() and hashlib.sha256(f.read_bytes()).hexdigest() == h, f"sha256 {rel}")


def main() -> int:
    root = Path(sys.argv[1] if len(sys.argv) > 1 else ".")
    sums(root)
    for run in ("main-seed-9301", "replication-seed-9302"):
        synthesis(root, run)
    for seed in ("9301", "9302"):
        learning(root, seed)
    proposals(root)
    verticals(root)
    governance(root)
    software_learning(root)
    mission(root)
    for st, name in CHECKS:
        print(f"{st:7s} {name}")
    p = sum(1 for s, _ in CHECKS if s == "PASS")
    f = sum(1 for s, _ in CHECKS if s == "FAIL")
    sk = sum(1 for s, _ in CHECKS if s == "SKIPPED")
    print(f"\n{p}/{p + f} checks passed; {sk} sections skipped")
    return 1 if f else 0


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