#!/usr/bin/env python3
"""E130 standalone verifier.

Usage:  python3 verify_e130_offline.py artifacts/e130

Imports no CAIN code.  It re-derives every headline number from the raw rows:

  1. the sealed answer key hashes to the committed digest;
  2. every recorded program is re-applied to the sealed query inputs and must reproduce the
     recorded predictions and the answer key;
  3. tier accuracies, solved-task counts and the emergent-task set are recomputed from rows and
     compared to the reported curves;
  4. every macro's declared primitive expansion equals the concatenation of its body;
  5. the library is monotonic across generations (or a rollback is recorded);
  6. improvement records only mention development task ids, never sealed ones;
  7. the journal is a valid SHA-256 chain;
  8. the Merkle manifest matches the bytes and the Ed25519 signature verifies.

Exit code 0 only if every check passes.
"""
from __future__ import annotations

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

PRIMS = {
    "identity": lambda v: v, "inc": lambda v: v + 1, "dec": lambda v: v - 1,
    "double": lambda v: 2 * v, "half": lambda v: v // 2, "neg": lambda v: -v,
    "abs": lambda v: abs(v), "square": lambda v: v * v, "cube": lambda v: v * v * v,
    "mod3": lambda v: v % 3, "mod5": lambda v: v % 5, "mod7": lambda v: v % 7,
    "digitsum": lambda v: sum(int(c) for c in str(abs(v))),
    "sign": lambda v: 0 if v == 0 else (1 if v > 0 else -1),
    "clamp09": lambda v: max(0, min(9, v)), "tens": lambda v: abs(v) // 10 % 10,
    "gt20": lambda v: 1 if v > 20 else 0, "parity": lambda v: v % 2,
    "add2": lambda v: v + 2, "add5": lambda v: v + 5, "sub3": lambda v: v - 3,
}

CHECKS: list[dict] = []


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


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


def sha(data: bytes) -> str:
    return hashlib.sha256(data).hexdigest()


def apply_base(base, x: int) -> int:
    v = int(x)
    for name in base:
        v = PRIMS[name](v)
    return int(v)


def load(p: Path):
    return json.loads(p.read_text())


def merkle(hashes: list[str]) -> str:
    if not hashes:
        return sha(b"")
    level = [bytes.fromhex(h) for h in hashes]
    while len(level) > 1:
        nxt = []
        for i in range(0, len(level), 2):
            a = level[i]
            b = level[i + 1] if i + 1 < len(level) else level[i]
            nxt.append(hashlib.sha256(a + b).digest())
        level = nxt
    return level[0].hex()


def verify_signature(pub_b64: str, payload, sig_b64: str) -> bool:
    try:
        from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
    except Exception:
        return False
    try:
        key = Ed25519PublicKey.from_public_bytes(base64.b64decode(pub_b64))
        key.verify(base64.b64decode(sig_b64), canon(payload))
        return True
    except Exception:
        return False


def main(root: str, allow_unsigned: bool = False) -> int:
    b = Path(root)
    if not b.exists():
        print(json.dumps({"ok": False, "error": f"not found: {b}"}))
        return 1

    # 1. commitment ---------------------------------------------------------
    answers = load(b / "answers.json")
    pre = load(b / "preregistration.json")
    commitment = sha(canon(answers))
    check("answer_key_commitment", commitment == pre["commitment_sha256"],
          f"{commitment[:16]} vs {pre['commitment_sha256'][:16]}")

    # 2/3. re-apply every program, recompute every number -------------------
    sealed = {t["task_id"]: t for t in load(b / "sealed_public.json")}
    rows = [json.loads(line) for line in (b / "rows.jsonl").read_text().splitlines() if line.strip()]
    check("rows_present", len(rows) > 0, f"{len(rows)} rows")

    grouped: dict[tuple[str, int, int], dict[str, int]] = {}
    emergent = set()
    gen0_solved = set()
    for r in rows:
        task = sealed.get(r["task_id"])
        if task is None:
            check(f"task_known:{r['task_id']}", False)
            continue
        query_x = [q["x"] for q in task["query"]]
        expanded = r.get("expanded") or []
        pred = [apply_base(expanded, x) for x in query_x] if expanded else []
        check(f"pred_recompute:{r['arm']}:{r['generation']}:{r['task_id']}",
              pred == r["predictions"], f"{len(pred)} predictions")
        key = answers[r["task_id"]]
        correct = sum(1 for p, q in zip(pred, key) if p == q) if pred else 0
        check(f"score_recompute:{r['arm']}:{r['generation']}:{r['task_id']}",
              correct == r["correct"] and len(key) == r["total"])
        slot = grouped.setdefault((r["arm"], r["generation"], r["tier"]),
                                  {"correct": 0, "total": 0, "solved": 0, "tasks": 0})
        slot["correct"] += correct
        slot["total"] += len(key)
        slot["tasks"] += 1
        slot["solved"] += 1 if correct == len(key) else 0
        if correct == len(key) and r["arm"] == "main":
            if r["generation"] == 0:
                gen0_solved.add(r["task_id"])
            else:
                emergent.add(r["task_id"])

    # compare with per-arm summaries and results.json
    results = load(b / "results.json")
    ok_curves = True
    for arm_dir in sorted((b / "arms").glob("*")):
        arm = arm_dir.name
        summary = load(arm_dir / "summary.json")
        for gen, hist in enumerate(summary["history"]):
            for tier, reported in hist["sealed"]["by_tier"].items():
                got = grouped.get((arm, gen, int(tier)))
                if got is None:
                    ok_curves = False
                    continue
                acc = round(got["correct"] / got["total"], 6) if got["total"] else 0.0
                if got["solved"] != reported["solved"] or got["correct"] != reported["correct"]:
                    ok_curves = False
                if abs(acc - reported["accuracy"]) > 1e-9:
                    ok_curves = False
        reported_solved = [h["sealed"]["solved_tasks"] for h in summary["history"]]
        got_solved = [sum(s["solved"] for (a, g, _t), s in grouped.items() if a == arm and g == gen)
                      for gen in range(len(summary["history"]))]
        if reported_solved != got_solved:
            ok_curves = False
    check("curves_recomputed", ok_curves, "per-arm, per-generation, per-tier")

    reported_emergent = sorted(set(results.get("emergent_tasks", [])))
    check("emergent_recomputed", sorted(emergent - gen0_solved) == reported_emergent,
          f"{len(emergent - gen0_solved)} tasks")

    # 4. macro expansion identity ------------------------------------------
    ok_macro = True
    for arm_dir in sorted((b / "arms").glob("*")):
        for gf in sorted(arm_dir.glob("generation_*.json")):
            snap = load(gf)["library"]["entries"]
            by_name = {e["name"]: e for e in snap}
            for e in snap:
                if e["kind"] == "primitive":
                    if e["body"] != [e["name"]] or e["base"] != [e["name"]]:
                        ok_macro = False
                    continue
                if any(member not in by_name for member in e["body"]):
                    ok_macro = False
                    continue
                expanded = []
                for member in e["body"]:
                    expanded.extend(by_name[member]["base"])
                if expanded != e["base"]:
                    ok_macro = False
                for x in (-17, -1, 0, 1, 2, 9, 31):
                    if apply_base(e["base"], x) != apply_base(
                            [p for m in e["body"] for p in by_name[m]["base"]], x):
                        ok_macro = False
    check("macro_expansion_identity", ok_macro, "every macro expands to its declared base")

    # 4b. the library each generation actually ran on is bound to its record -----
    ok_used = True
    for arm_dir in sorted((b / "arms").glob("*")):
        prev_post = None
        for gf in sorted(arm_dir.glob("generation_*.json")):
            d = load(gf)
            used = d.get("library_used")
            if not used:
                ok_used = False
                continue
            if used["digest"] != d["record"]["library_digest"]:
                ok_used = False
            if len(used["entries"]) != d["record"]["library_size"]:
                ok_used = False
            if prev_post is not None and used["entries"] != prev_post["entries"]:
                ok_used = False
            prev_post = d["library"]
    check("library_used_binding", ok_used, "digest, size and generation-to-generation continuity")

    # 5. monotonic library / recorded rollback ------------------------------
    journal_path = b / "journal_main.jsonl"
    journal_rows = [json.loads(line) for line in journal_path.read_text().splitlines() if line.strip()]
    rollback = any(rec["record"].get("event") == "ROLLED_BACK" for rec in journal_rows)
    snaps = [load(p)["library"]["entries"] for p in sorted((b / "arms/main").glob("generation_*.json"))]
    mono = True
    for prev, nxt in zip(snaps, snaps[1:]):
        prev_names = {e["name"] for e in prev}
        if not prev_names.issubset({e["name"] for e in nxt}) and not rollback:
            mono = False
    check("library_monotonic", mono, "prior entries preserved" + (" (rollback recorded)" if rollback else ""))

    # 6. improvement uses development tasks only ---------------------------
    ok_scope = True
    sealed_ids = set(sealed)
    for arm_dir in sorted((b / "arms").glob("*")):
        for gf in sorted(arm_dir.glob("generation_*.json")):
            rec = load(gf)["record"]
            imp = rec.get("improvement", {})
            for chain in imp.get("research_chains", []):
                if isinstance(chain, dict) and chain.get("task_id") in sealed_ids:
                    ok_scope = False
            for tid in imp.get("research_task_ids", []):
                if tid in sealed_ids:
                    ok_scope = False
            if imp.get("mode") in {"directed", "flat"} and imp.get("dev_tasks", 0) < 1:
                ok_scope = False
    check("improvement_scope_dev_only", ok_scope, "no sealed task id in any improvement record")

    # 6b. meta-selection is recorded and internally consistent ----------------
    ok_meta = True
    for gf in sorted((b / "arms/main").glob("generation_*.json")):
        rec = load(gf)["record"]
        imp = rec.get("improvement", {})
        if imp.get("mode") != "meta":
            continue
        scores = imp.get("strategy_scores") or {}
        selected = imp.get("selected")
        if selected not in scores:
            ok_meta = False
            continue
        def key(v):
            return (v.get("solved", -1), -v.get("evaluations", 10**9))
        if key(scores[selected]) != max(key(v) for v in scores.values()):
            ok_meta = False
    check("meta_selection_consistent", ok_meta,
          "selected strategy has the best development-probe score")

    # 7. journal chain ------------------------------------------------------
    head = "0" * 64
    ok_chain = True
    for rec in journal_rows:
        expect = sha(head.encode() + canon(rec["record"]))
        if rec["prev"] != head or rec["link"] != expect:
            ok_chain = False
            break
        head = rec["link"]
    check("journal_chain", ok_chain and bool(journal_rows), f"{len(journal_rows)} records")

    # 8. manifest + signature ----------------------------------------------
    man_path = b / "manifest.json"
    if man_path.exists():
        man = load(man_path)
        ok_files = True
        for f in man["files"]:
            p = b / f["path"]
            if not p.exists() or sha(p.read_bytes()) != f["sha256"]:
                ok_files = False
        check("manifest_files", ok_files, f"{len(man['files'])} files")
        check("manifest_merkle", merkle([f["sha256"] for f in man["files"]]) == man["merkle_root"],
              man["merkle_root"][:16])
        sig = (b / "manifest.sig").read_text().strip()
        check("manifest_signature", verify_signature(man["public_key_b64"], man, sig),
              "Ed25519 over canonical manifest")
        listed = {f["path"] for f in man["files"]} | {"manifest.json", "manifest.sig"}
        extra = sorted(str(p.relative_to(b)) for p in b.rglob("*")
                       if p.is_file() and str(p.relative_to(b)) not in listed
                       and not p.name.endswith(".tmp"))
        check("no_unlisted_files", not extra, ", ".join(extra[:5]) or "none")
    elif allow_unsigned:
        check("manifest_files", True, "pre-sign bundle: manifest intentionally absent")
    else:
        check("manifest_files", False, "manifest.json missing")

    ok = all(c["ok"] for c in CHECKS)
    out = {
        "schema": "cain.e130.verification.v1",
        "ok": ok,
        "checks": len(CHECKS),
        "passed": sum(1 for c in CHECKS if c["ok"]),
        "failed": [c for c in CHECKS if not c["ok"]][:20],
        "headline_recomputed": {
            "rows": len(rows),
            "emergent_tasks": len(emergent - gen0_solved),
            "main_solved_by_generation": [
                sum(s["solved"] for (a, g, _t), s in grouped.items() if a == "main" and g == gen)
                for gen in sorted({g for (a, g, _t) in grouped if a == "main"})
            ],
        },
        "agi_status": "NOT ACHIEVED",
    }
    print(json.dumps(out, indent=1))
    return 0 if ok else 1


if __name__ == "__main__":
    args = [a for a in sys.argv[1:] if not a.startswith("--")]
    sys.exit(main(args[0] if args else "artifacts/e130", allow_unsigned="--allow-unsigned" in sys.argv))
