#!/usr/bin/env python3
"""CAIN-42 E141 standalone offline verifier.

Imports no CAIN code and makes no network calls (stdlib + ``cryptography`` only).

    python3 verify_e141_offline.py <bundle_dir>

Checks
  1. every file listed in MANIFEST.json matches its SHA-256; the Merkle root recomputes;
     the manifest signature verifies against the pinned evidence key;
  2. the event log is an unbroken SHA-256 chain (each body re-hashed from its fields);
  3. every evaluator verdict carries a valid Ed25519 signature;
  4. for every verdict with per-instance scores: arm means, paired effect, wins/losses/ties
     and the one-sided exact sign-test log10 p recompute exactly; no p is ever 0;
  5. accepted RSI candidates are exactly the experiments whose verdict accepted them;
  6. E141_STATISTICS.json recomputes from experiments.jsonl;
  7. no experiment has two results or two verdicts; adversarial audits were all rejected;
  8. drills passed; gate counts are consistent; status invariants hold
     (AGI NOT ESTABLISHED, RSI EXPERIMENTAL, neural weights not modified, 0 independent reproductions).

Integrity is not truth: a valid signature shows the evidence is unchanged since signing by
this key, not that the science is right, and the key is self-generated by the same team.
"""
from __future__ import annotations

import hashlib
import json
import math
import sys
from collections import Counter, defaultdict
from pathlib import Path

from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey

GENESIS = "0" * 64
RESULTS: list[tuple[bool, str, str]] = []


def check(ok: bool, name: str, detail: str = "") -> bool:
    RESULTS.append((bool(ok), name, detail))
    return bool(ok)


def canon(obj) -> str:
    return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True, default=float)


def sha_file(p: Path) -> str:
    h = hashlib.sha256()
    with p.open("rb") as f:
        for chunk in iter(lambda: f.read(1 << 16), b""):
            h.update(chunk)
    return h.hexdigest()


def merkle(leaves: list[str]) -> str:
    if not leaves:
        return hashlib.sha256(b"").hexdigest()
    level = [hashlib.sha256(b"\x00" + bytes.fromhex(h)).digest() for h in leaves]
    while len(level) > 1:
        level = [hashlib.sha256(b"\x01" + level[i] + level[i + 1]).digest() if i + 1 < len(level) else level[i]
                 for i in range(0, len(level), 2)]
    return level[0].hex()


def ed_ok(pub_hex: str, sig_hex: str, msg: bytes) -> bool:
    try:
        Ed25519PublicKey.from_public_bytes(bytes.fromhex(pub_hex)).verify(bytes.fromhex(sig_hex), msg)
        return True
    except Exception:
        return False


def log10_sign_p(w: int, l: int) -> float:
    n = w + l
    if n == 0:
        return 0.0
    logs = [math.lgamma(n + 1) - math.lgamma(i + 1) - math.lgamma(n - i + 1) - n * math.log(2) for i in range(w, n + 1)]
    m = max(logs)
    return (m + math.log(sum(math.exp(x - m) for x in logs))) / math.log(10)


def paired(t: list[float], c: list[float], better: str) -> dict:
    s = 1.0 if better == "higher" else -1.0
    d = [s * (a - b) for a, b in zip(t, c)]
    n = len(d)
    w = sum(1 for x in d if x > 1e-12)
    l = sum(1 for x in d if x < -1e-12)
    mean = sum(d) / n if n else 0.0
    return {"n": n, "wins": w, "losses": l, "effect": round(mean, 6), "log10_p_sign": round(log10_sign_p(w, l), 4)}


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


def main() -> int:
    b = Path(sys.argv[1] if len(sys.argv) > 1 else Path(__file__).resolve().parent)
    man = json.loads((b / "MANIFEST.json").read_text())
    pub = (b / "evidence_pubkey.txt").read_text().strip()
    # 1. manifest
    bad = [f for f, h in man["files"].items() if not (b / f).exists() or sha_file(b / f) != h]
    check(not bad, "manifest_file_hashes", f"{len(man['files'])} files, mismatched: {bad[:5]}")
    root = merkle([man["files"][k] for k in sorted(man["files"])])
    check(root == man["merkle_root"] == (b / "MERKLE_ROOT.txt").read_text().strip(), "merkle_root", root)
    body = {k: man[k] for k in sorted(man) if k != "signature"}
    check(ed_ok(pub, man["signature"], canon(body).encode()), "manifest_signature", pub[:16])
    # 2. event chain
    events = jl(b / "events.jsonl")
    prev, chain_ok, where = GENESIS, True, None
    for e in events:
        eb = {k: e[k] for k in ("event_id", "ts", "type", "evolution", "experiment_id", "worker_id", "provenance",
                                "verification_state", "artifact_ref", "data", "prev_hash")}
        if e["prev_hash"] != prev or hashlib.sha256(canon(eb).encode()).hexdigest() != e["hash"]:
            chain_ok, where = False, e["seq"]
            break
        prev = e["hash"]
    check(chain_ok, "event_chain", f"{len(events)} events, head {prev[:16]}" + (f", broken at {where}" if where else ""))
    seqs = [e["seq"] for e in events]
    check(seqs == sorted(seqs) and len(set(seqs)) == len(seqs), "event_sequence_monotonic", f"{len(seqs)}")
    # 3-5. verdicts
    exps = jl(b / "experiments.jsonl")
    sig_bad, stat_bad, zero_p, recomputed = [], [], 0, 0
    for x in exps:
        v = x.get("verdict")
        if not v:
            continue
        vb = {k: v[k] for k in sorted(v) if k not in ("signature", "evaluator_pubkey")}
        if not ed_ok(v.get("evaluator_pubkey", ""), v.get("signature", ""), canon(vb).encode()) or v.get("evaluator_pubkey") != pub:
            sig_bad.append(x["id"])
        if "log10_p_sign" in v and (v["log10_p_sign"] is None or not math.isfinite(v["log10_p_sign"])):
            zero_p += 1
        if str(v.get("p_sign_display", "")).strip() in ("0", "0.0"):
            zero_p += 1
        sc = v.get("instance_scores")
        if sc and v["state"] in ("VERIFIED", "PARTIAL"):
            if x["family"] == "rsi_search":
                st = paired(sc["best_dev_child"], sc["parent"], "higher")
            else:
                st = paired(sc[v["treatment"]], sc[v["primary_control"]], v["better"])
                for arm, vals in sc.items():
                    if abs(sum(vals) / len(vals) - v["summary"][arm]) > 1e-5:
                        stat_bad.append((x["id"], "mean " + arm))
            for k in ("n", "wins", "losses", "effect", "log10_p_sign"):
                if abs(float(st[k]) - float(v[k])) > 1e-4:
                    stat_bad.append((x["id"], k))
            recomputed += 1
    check(not sig_bad, "verdict_signatures", f"{sum(1 for x in exps if x.get('verdict'))} verdicts, bad: {sig_bad[:5]}")
    check(not stat_bad, "verdict_statistics_recompute", f"{recomputed} recomputed from per-instance scores, mismatches: {stat_bad[:5]}")
    check(zero_p == 0, "no_zero_p_values", f"{zero_p} zero/infinite p values")
    cands = json.loads((b / "candidates.json").read_text())
    acc_v = {x["id"] for x in exps if x["family"] == "rsi_search" and x.get("verdict") and x["verdict"].get("candidate_accepted")}
    acc_c = {c["experiment_id"] for c in cands if c["state"] == "ACCEPTED"}
    check(acc_v == acc_c, "rsi_acceptance_matches_verdicts", f"{len(acc_c)} accepted candidates")
    # 6. statistics
    stats = json.loads((b / "E141_STATISTICS.json").read_text())
    cnt = Counter(x["state"] for x in exps)
    check(dict(cnt) == stats["by_state"], "statistics_by_state", json.dumps(dict(cnt), sort_keys=True))
    fam = defaultdict(lambda: Counter())
    for x in exps:
        v = x.get("verdict") or {}
        if x["kind"] in ("primary", "replication") and x["state"] in ("VERIFIED", "PARTIAL"):
            fam[x["family"]][v.get("outcome")] += 1
    check(all(dict(fam[f]) == stats["families"][f]["outcomes"] for f in stats["families"]), "statistics_family_outcomes",
          f"{len(stats['families'])} families")
    # 7. duplicates and adversarial audits
    comp = Counter(e["experiment_id"] for e in events if e["type"] == "experiment.completed")
    verd = Counter(e["experiment_id"] for e in events if e["type"] in ("experiment.verified", "experiment.rejected")
                   and e["worker_id"] == "evaluator")
    check(max(comp.values(), default=0) <= 1 and max(verd.values(), default=0) <= 1, "no_duplicate_results_or_verdicts",
          f"{len(comp)} completed, {len(verd)} judged")
    adv = [x for x in exps if x["kind"] == "adversarial" and x["state"] not in ("PREREGISTERED", "RUNNING", "CHECKPOINTED",
                                                                               "VERIFICATION_PENDING")]
    check(all(x["state"] == "REJECTED" for x in adv), "adversarial_fabrications_rejected", f"{len(adv)} audits")
    pre = {e["experiment_id"]: e["seq"] for e in events if e["type"] == "experiment.preregistered"}
    started = {}
    for e in events:
        if e["type"] == "experiment.started" and e["experiment_id"] not in started:
            started[e["experiment_id"]] = e["seq"]
    check(all(pre.get(k, 10 ** 12) < s for k, s in started.items()), "preregistration_precedes_start", f"{len(started)} started")
    # 8. drills, gates, invariants
    drills = jl(b / "drills.jsonl")
    check(bool(drills) and all(d.get("pass", True) for d in drills if d["drill"] != "audit"), "drills_passed",
          ", ".join(f"{d['drill']}={d.get('pass')}" for d in drills))
    gates = json.loads((b / "E141_GATE_MATRIX.json").read_text())
    c = Counter(g["status"] for g in gates["gates"])
    check(dict(c) == gates["summary"] and len(gates["gates"]) >= 50 and set(c) <= {"PASS", "PARTIAL", "BLOCKED", "FAIL"},
          "gate_matrix_consistent", json.dumps(dict(c), sort_keys=True))
    status = json.loads((b / "E141_CURRENT_STATUS.json").read_text())
    check(status["agi"] == "NOT ESTABLISHED" and status["rsi"] == "EXPERIMENTAL" and status["neural_weights_modified"] is False
          and status["independent_reproductions"] == 0 and status["paid_api_spend_usd"] == 0, "status_invariants", "")
    for ok, name, detail in RESULTS:
        print(("PASS " if ok else "FAIL ") + name + (f"  [{detail}]" if detail else ""))
    n_ok = sum(1 for r in RESULTS if r[0])
    print(f"\n{n_ok}/{len(RESULTS)} checks passed; {len(RESULTS) - n_ok} failed")
    return 0 if n_ok == len(RESULTS) else 1


if __name__ == "__main__":
    raise SystemExit(main())
