#!/usr/bin/env python3
"""Offline verifier for the Evolution 85 evidence bundle.

Standard library plus `cryptography`. Imports no CAIN code. It does not trust the summaries: it
recomputes the headline numbers from the raw rows and re-derives every evaluation answer from
the task specification with its own implementation.

  python3 verify_e85_offline.py BUNDLE_DIR [--pubkey BASE64]

Exit 0 only if every check passes. A passing run means the bundle is internally consistent and
signed by the stated key. It does not make the producer independent of the verifier's author.
"""
from __future__ import annotations

import base64
import hashlib
import json
import math
import subprocess
import sys
from pathlib import Path

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

RESULTS = []


def check(name: str, ok: bool, detail: str = "") -> bool:
    RESULTS.append((name, bool(ok), detail))
    print(("PASS " if ok else "FAIL ") + name + (f"  ({detail})" if detail else ""))
    return bool(ok)


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


def sha(o) -> str:
    if isinstance(o, (bytes, bytearray)):
        return hashlib.sha256(o).hexdigest()
    if isinstance(o, str):
        return hashlib.sha256(o.encode()).hexdigest()
    return hashlib.sha256(canon(o)).hexdigest()


def sig_ok(pub_b64: str, payload, sig_b64: str) -> bool:
    try:
        Ed25519PublicKey.from_public_bytes(base64.b64decode(pub_b64)).verify(base64.b64decode(sig_b64), canon(payload))
        return True
    except Exception:
        return False


def merkle(leaves) -> str:
    """RFC 6962 tree hash over byte 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: int, n: int, z: float = 1.96):
    if n == 0:
        return [0.0, 0.0]
    p = k / n
    d = 1 + z * z / n
    c = (p + z * z / (2 * n)) / d
    h = z * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n)) / d
    return [round(max(0.0, c - h), 4), round(min(1.0, c + h), 4)]


def mcnemar(a, b) -> float:
    bo = sum(1 for x, y in zip(a, b) if (not x) and y)
    ao = sum(1 for x, y in zip(a, b) if x and (not y))
    n = bo + ao
    return 1.0 if n == 0 else round(min(1.0, 2 * sum(math.comb(n, i) for i in range(min(bo, ao) + 1)) / 2 ** n), 6)


def truth(t) -> str:
    """Third implementation of the task answers, from the machine-readable spec only."""
    s, f = t["spec"], t["family"]
    if f == "arith_finance":
        tot = 0
        for q, p in s["items"]:
            tot += q * p
        return str(tot - s["discount"])
    if f == "constraint_schedule":
        common = None
        for hours in s["availability"].values():
            common = set(hours) if common is None else common & set(hours)
        return str(sorted(common)[0])
    if f in ("dependency_impact", "transfer_supply"):
        down, grew = {s["failed"]}, True
        while grew:
            grew = False
            for node, needs in s["deps"].items():
                if node not in down and set(needs) & down:
                    down.add(node)
                    grew = True
        return str(len(down) - 1)
    if f == "contradiction":
        return "COMPLIANT" if (s["got"] >= s["need"]) == (s["kind"] == "at least") or s["got"] == s["need"] else "VIOLATION"
    if f == "access_decision":
        return "DENY" if s["act"] not in s["allow"][s["role"]] or (s["role"], s["act"]) == tuple(s["deny"]) else "ALLOW"
    if f == "plan_order":
        return [x for x in s["tasks"] if x not in s["prereq"]][0].upper()
    if f == "insufficient":
        return "UNKNOWN" if s["missing"] else str(s["a"] - s["b"] + s["c"])
    if f == "error_detect":
        left = [s["a"] * s["b"], s["shown"][0] + s["c"], s["shown"][1] - s["d"]]
        for i in range(3):
            if left[i] != s["shown"][i]:
                return str(i + 1)
        return "0"
    if f == "causal":
        return "YES" if ((s["a"] and s["d"]) if s["need_both"] else (s["a"] or s["d"])) else "NO"
    raise KeyError(f)


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


def chain_ok(events, fields) -> bool:
    prev = "0" * 64
    for e in events:
        if e["prev"] != prev or sha({k: e[k] for k in fields}) != e["hash"]:
            return False
        prev = e["hash"]
    return True


def main() -> int:
    if len(sys.argv) < 2:
        print(__doc__)
        return 2
    B = Path(sys.argv[1]).resolve()
    pinned = sys.argv[sys.argv.index("--pubkey") + 1] if "--pubkey" in sys.argv else None
    J = lambda rel: json.loads((B / rel).read_text())

    # ---- 1. manifest, signature, digests, Merkle root
    man = J("manifest.json")
    body = man["body"]
    check("manifest_signature_valid", sig_ok(man["public_key_b64"], body, man["signature_b64"]))
    check("manifest_key_matches_pinned_key", pinned is None or pinned == man["public_key_b64"],
          "no --pubkey given: signature is self-consistent only" if pinned is None else "pinned")
    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_listed_file_matches_its_digest", not bad, f"{len(body['files'])} files" if not bad else str(bad[:3]))
    listed = {f["path"] for f in body["files"]}
    extra = [str(p.relative_to(B)) for p in B.rglob("*") if p.is_file() and str(p.relative_to(B)) not in listed
             and p.name != "manifest.json" and "__pycache__" not in p.parts]
    check("no_unlisted_file_in_bundle", not extra, str(extra[: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"], body["merkle_root"][:16])
    check("no_private_key_material", not any(b"PRIVATE KEY" in (B / f["path"]).read_bytes() or
                                             b"seed_b64" in (B / f["path"]).read_bytes()
                                             for f in body["files"] if not f["path"].endswith(".py")))

    # ---- 2. evaluation: recompute everything from raw rows
    tasks = J("eval/tasks.json")
    held = {t["task_id"]: t for t in tasks["heldout"]}
    allt = tasks["train"] + tasks["validation"] + tasks["heldout"]
    check("task_answers_rederived_from_spec", all(truth(t) == t["answer"] for t in allt), f"{len(allt)} tasks")
    seen = {t["prompt"] for t in tasks["train"] + tasks["validation"]}
    check("heldout_prompts_disjoint_from_train_and_validation", not any(t["prompt"] in seen for t in tasks["heldout"]))
    check("transfer_family_absent_from_train_and_validation",
          not any(t["family"] == "transfer_supply" for t in tasks["train"] + tasks["validation"]))
    pre = J("eval/preregistration.json")
    h = hashlib.sha256()
    for t in sorted(tasks["heldout"], key=lambda x: x["task_id"]):
        h.update(f"{t['task_id']}|{t['prompt']}|{t['answer']}\n".encode())
    check("heldout_set_matches_preregistration", h.hexdigest() == pre["heldout_task_set_sha256"])
    rows = jl(B / "eval/rows.jsonl")
    calls = jl(B / "eval/model_calls.jsonl")
    first_call = min(c["utc"] for c in calls)
    check("preregistered_before_first_model_call", pre["registered_utc"] <= first_call,
          f"{round(first_call - pre['registered_utc'], 1)} s before")
    hrows = [r for r in rows if r["split"] == "heldout"]
    check("every_heldout_row_graded_against_the_rederived_answer",
          all(r["answer"] == truth(held[r["task_id"]]) and r["correct"] == (r["parsed"] == r["answer"]) for r in hrows),
          f"{len(hrows)} rows")
    summ = J("eval/summary.json")
    by = {}
    for r in hrows:
        by.setdefault(r["arm"], {})[r["task_id"]] = r
    ok = True
    for arm, a in summ["arms"].items():
        rs = list(by.get(arm, {}).values())
        k = sum(r["correct"] for r in rs)
        ok &= (k == a["correct"] and len(rs) == a["n"] and wilson(k, len(rs)) == a["wilson95"]
               and sum(r["calls"] for r in rs) == a["calls"])
    check("arm_accuracy_intervals_and_call_counts_recomputed", ok, ", ".join(
        f"{a}={v['correct']}/{v['n']}" for a, v in summ["arms"].items()))
    ok = True
    for arm, c in summ["comparisons"].items():
        ids = sorted(set(by["A_single"]) & set(by[arm]))
        x, y = [by["A_single"][i]["correct"] for i in ids], [by[arm][i]["correct"] for i in ids]
        ok &= (mcnemar(x, y) == c["mcnemar"]["p_two_sided"] and round((sum(y) - sum(x)) / len(ids), 4) == c["delta_accuracy"])
    check("paired_comparisons_recomputed", ok, f"{len(summ['comparisons'])} comparisons vs A_single")
    cls = {}
    for c in calls:
        cls[c["execution_class"]] = cls.get(c["execution_class"], 0) + 1
    check("all_model_calls_are_real_local_and_none_remote", set(cls) == {"REAL_LOCAL_MODEL"}, str(cls))
    ids = {c["request_id"] for c in calls}
    model_rows = [r for r in rows if "REAL_LOCAL_MODEL" in r["execution_classes"]]
    check("every_model_row_traces_to_logged_calls", all(set(r["request_ids"]) <= ids for r in model_rows),
          f"{len(model_rows)} rows, {len(calls)} calls")
    audit = jl(B / "eval/gate-audit.jsonl")
    # The producer hashes each event over its OWN keys (events carry different optional
    # fields), excluding only `hash`; a fixed field list from row 0 is wrong.
    lin = (all(a["seq"] == i for i, a in enumerate(audit))
           and all(a["prev"] == (audit[i - 1]["hash"] if i else "0" * 64) for i, a in enumerate(audit))
           and all(sha({k: v for k, v in a.items() if k != "hash"}) == a["hash"] for a in audit))
    check("gate_audit_is_one_linear_chain", lin, f"{len(audit)} rows")
    grants = {a["grant_id"] for a in audit if a.get("event") == "DECIDE" and a.get("decision") == "ALLOW"}
    check("every_model_call_has_an_allow_decision", all(c["grant_id"] in grants for c in calls))

    # ---- 3. learning ledger
    evs = J("eval/learning_events.json")
    lok = chain_ok(evs, ["utc", "tenant", "event", "body", "prev"]) and all(
        sig_ok(e["signer"], {"hash": e["hash"]}, e["signature_b64"]) for e in evs)
    check("learning_events_chained_and_signed", lok, f"{len(evs)} events")
    hist = J("eval/strategy_history.json")
    rule = all((h["state"] in ("ACTIVE", "SUPERSEDED", "ROLLED_BACK")) == (h["evaluation"]["gain_tasks"] >= 2)
               and h["approver"] != h["proposer"] and h["evaluation"]["baseline"]["total"] ==
               h["evaluation"]["candidate"]["total"] for h in hist if h["state"] != "CANDIDATE")
    check("promotions_follow_the_preregistered_rule", rule,
          f"{sum(h['state'] in ('ACTIVE', 'SUPERSEDED') for h in hist)} promoted, "
          f"{sum(h['state'] == 'REJECTED' for h in hist)} rejected")
    used = [e for e in evs if e["event"] == "KNOWLEDGE_ADDED"]
    check("no_heldout_task_entered_memory", not any("heldout" in e["body"].get("evidence_ref", "") for e in used))

    # ---- 4. governed execution: receipts re-verified with the bundled E84 verifier
    ver = B / "tools" / "verify_action_receipt.py"
    n_ok = n_bad = 0
    pkgs = sorted({p.parent for p in B.rglob("receipt.json")})
    for d in pkgs:
        p = subprocess.run([sys.executable, "-I", str(ver), str(d / "receipt.json"), "--keys",
                            str(d / "trust_keys.json")], capture_output=True, text=True)
        n_ok += int(p.returncode == 0 and "independent=0" in p.stdout)
        if (d / "receipt-tampered.json").is_file():
            q = subprocess.run([sys.executable, "-I", str(ver), str(d / "receipt-tampered.json"), "--keys",
                                str(d / "trust_keys.json")], capture_output=True, text=True)
            n_bad += int(q.returncode != 0)
    check("every_receipt_verifies_with_the_e84_verifier", n_ok == len(pkgs) and len(pkgs) >= 8, f"{n_ok}/{len(pkgs)}")
    check("tampered_receipts_are_rejected", n_bad >= 2, f"{n_bad} rejected")
    check("no_receipt_claims_an_independent_witness", all(
        json.loads((d / "receipt.json").read_text())["body"]["verification"]["witness_classification"] == "SIMULATED_LOCAL"
        for d in pkgs))
    lab = J("governed/lab.json")
    check("every_adversarial_scenario_blocked", all(r["blocked"] for r in lab), f"{len(lab)} scenarios")
    gov = J("governed/summary.json")
    rh = gov["remote_host"]
    check("remote_resource_ran_on_a_different_machine_and_was_cleaned_up",
          rh.get("status") == "NOT_RUN" or (rh["resource_hello"]["machine_id_sha256_12"] != rh["caller_machine_id_sha256_12"]
                                            and rh["cleanup"]["state_directory"] == "removed"
                                            and rh["independent_organization"] is False))
    mcp = J("mcp/integration.json")
    check("mcp_dev_integration_expectations_met", mcp["all_ok"] and mcp["hosted_production_proxy_touched"] is False,
          f"{len(mcp['expectations'])} expectations")

    # ---- 5. workforce, kill switch, endurance
    st = [s for s in J("swarm/stages.json")["stages"] if s.get("status") == "RUN"]
    check("stages_no_duplicate_effects", all(s["duplicate_effects"] == 0 for s in st),
          str([s["active_worker_processes"] for s in st]))
    unrec = [s for s in st if not s["effects_match_done"]]
    check("unreconciled_stages_explained_by_in_flight_commits",
          all(s["commits_after_engage"] > 0 for s in unrec),
          str([(s["active_worker_processes"], s["commits_after_engage"]) for s in unrec]))
    check("revoked_agents_executed_nothing", sum(s["tasks_done_by_revoked_agents"] for s in st) == 0)
    kfields = ["epoch", "action", "scope", "target", "reason", "operator", "nonce", "utc", "prev"]
    kfiles = sorted(B.glob("swarm/kill_events_*.json")) + [B / "endurance/kill_events.json"]
    kok = True
    for kf in kfiles:
        ev = json.loads(kf.read_text())
        kok &= bool(ev) and chain_ok(ev, kfields) and all(e["epoch"] == i + 1 for i, e in enumerate(ev)) and all(
            sig_ok(e["operator_pub"], e["control"], e["signature_b64"]) for e in ev)
    check("kill_events_chained_and_operator_signed", kok, f"{len(kfiles)} event logs")
    ids_ = J("swarm/identities.json")["stages"]
    check("identities_reported_separately_from_processes", max(s["registered_identities"] for s in ids_) >= 10000
          and all("active_worker_processes" not in s for s in ids_))
    end = J("endurance/endurance.json")
    check("endurance_duration_is_actual_wall_clock", end["clock"] == "ACTUAL" and
          end["wall_clock_seconds"] >= end["requested_seconds"], f"{end['wall_clock_seconds']} s")
    check("endurance_no_duplicates_and_checkpoints_chain", end["duplicate_effects"] == 0 and end["effects_match_done"]
          and end["checkpoints"]["chain_ok"] and end["resource_audit"]["clean"])
    samples = end["samples"]
    check("endurance_samples_cover_the_run", len(samples) >= end["wall_clock_seconds"] // 60 - 2 and
          all(b["t"] > a["t"] and b["done"] >= a["done"] for a, b in zip(samples, samples[1:])), f"{len(samples)} samples")

    # ---- 6. verticals: metrics recomputed from item rows
    vok = True
    vs = J("verticals/summary.json")["verticals"]
    for name, v in vs.items():
        r = J(f"verticals/{name}/result.json")["rows"]
        tp = sum(x["label"] and x["flagged"] for x in r); fp = sum((not x["label"]) and x["flagged"] for x in r)
        fn = sum(x["label"] and not x["flagged"] for x in r)
        m = v["metrics"]
        vok &= (tp, fp, fn, len(r)) == (m["tp"], m["fp"], m["fn"], m["n"])
        vok &= v["deliver_without_approval"] == "HOLD_FOR_APPROVAL" and v["deliver_with_approval"] == "COMPLETED"
    check("vertical_metrics_recomputed_and_delivery_held_for_approval", vok and len(vs) == 3, ", ".join(vs))

    # ---- 7. acceptance matrix honesty
    acc = J("acceptance_matrix.json")
    g = {x["id"]: x for x in acc["gates"]}
    cnt = {}
    for x in acc["gates"]:
        cnt[x["status"]] = cnt.get(x["status"], 0) + 1
    check("acceptance_counts_recomputed", cnt == acc["counts"], str(cnt))
    check("no_gate_claims_independent_verification_or_deployment",
          all(x["class"] not in ("INDEPENDENTLY VERIFIED", "DEPLOYED") for x in acc["gates"]))
    check("blocked_and_unmet_gates_are_not_reported_as_pass",
          g["G37"]["status"] == "BLOCKED" and g["G38"]["status"] == "FAIL" and g["G22"]["status"] != "PASS"
          and g["G20"]["status"] != "PASS" and g["G18"]["status"] != "PASS" and acc["decision"] == "NOT RELEASED")
    b = summ["comparisons"]["B_learned"]
    sig = b["delta_accuracy"] > 0 and b["mcnemar"]["p_two_sided"] < 0.05
    check("learning_gate_matches_the_statistics", (g["G09"]["status"] == "PASS") == sig, g["G09"]["status"])
    rep = (B / "docs" / "E85_FINAL_REPORT.md").read_text()
    a = summ["arms"]
    want = [f"{a['A_single']['correct']}/{a['A_single']['n']}", f"{a['B_learned']['correct']}/{a['B_learned']['n']}",
            f"{len(calls):,}", f"{end['wall_clock_seconds']}", "NOT RELEASED"]
    check("final_report_states_the_recomputed_numbers", all(w in rep for w in want), ", ".join(want))
    check("final_report_makes_no_agi_claim", "not AGI" in rep or "no AGI claim" in rep.lower()
          or "is not artificial general intelligence" in rep)
    check("final_report_discloses_in_flight_effects", "in-flight" in rep and "500" in rep)

    passed = sum(1 for _, o, _ in RESULTS if o)
    print(f"\n{passed}/{len(RESULTS)} checks passed; bundle Merkle root {body['merkle_root']}")
    return 0 if passed == len(RESULTS) else 1


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