#!/usr/bin/env python3
"""Evolution 79 standalone offline verifier (v2).

    python3 scripts/evolution79/verify_e79_v2_offline.py artifacts/e79 [--artifacts artifacts/e79]

Imports no CAIN code: standard library plus ``cryptography`` for Ed25519.

What it does NOT do: it does not trust the summary JSON. Every headline number is
recomputed from the raw per-task rows in ``bench/*.jsonl`` and ``bench_replication/*.jsonl``,
and the recomputed value must equal the value inside the signed bundle.

It verifies a record. It cannot make the record true, and it is not a third-party audit.
"""
from __future__ import annotations

import base64
import hashlib
import json
import math
import sys
from pathlib import Path
from typing import Any, Dict, List, Optional, Sequence, Tuple

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

FIELDS = (
    "phase0_baseline", "model_runtime_and_providers", "specialist_registry_and_identities",
    "collective_benchmark", "benchmark_raw_rows", "tool_execution", "collaboration_transcript",
    "protocol_iterations", "synthesis_mission",
    "research_lab", "memory_evaluation", "agentic75_matrix", "self_improvement",
    "durable_execution_drill", "security_lab", "governance", "clawx_state",
    "cost_and_resource_accounting", "replication", "off_host_anchor", "artifact_hashes",
    "final_acceptance",
)


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


def sha_obj(obj: Any) -> str:
    return hashlib.sha256(canonical(obj)).hexdigest()


def merkle(leaves: Sequence[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:
        nxt = []
        for i in range(0, len(level), 2):
            left = level[i]
            right = level[i + 1] if i + 1 < len(level) else left
            nxt.append(hashlib.sha256(b"\x01" + left + right).digest())
        level = nxt
    return level[0].hex()


def wilson(k: int, n: int, z: float = 1.96) -> List[float]:
    if n == 0:
        return [0.0, 1.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_only: int, b_only: int) -> float:
    n = a_only + b_only
    if n == 0:
        return 1.0
    k = min(a_only, b_only)
    return round(min(1.0, 2 * sum(math.comb(n, i) for i in range(k + 1)) / (2 ** n)), 4)


def read_jsonl(path: Path) -> List[Dict[str, Any]]:
    if not path.is_file():
        return []
    return [json.loads(line) for line in path.read_text().splitlines() if line.strip()]


class Checker:
    def __init__(self) -> None:
        self.checks: List[Dict[str, Any]] = []

    def add(self, name: str, ok: bool, detail: Any = "") -> None:
        self.checks.append({"check": name, "ok": bool(ok), "detail": detail})

    def eq(self, name: str, got: Any, want: Any) -> None:
        self.add(name, got == want, f"got {got!r} want {want!r}")


def main(argv: Optional[Sequence[str]] = None) -> int:
    argv = list(sys.argv[1:] if argv is None else argv)
    pkg_path = Path(argv[0]) if argv else Path("artifacts/e79/evidence_v2.json")
    artifacts = Path(argv[1]) if len(argv) > 1 else (pkg_path.parent if pkg_path.is_file() else Path("artifacts/e79"))
    if pkg_path.is_dir():
        artifacts = pkg_path
        pkg_path = artifacts / "evidence_v2.json"
    if not pkg_path.is_file():
        print(json.dumps({"ok": False, "error": "PACKAGE_NOT_FOUND", "path": str(pkg_path)}))
        return 2

    pkg = json.loads(pkg_path.read_text())
    fields = pkg.get("fields") or {}
    manifest = pkg.get("manifest") or {}
    c = Checker()

    # ---- 1. package integrity --------------------------------------------------------
    missing = [f for f in FIELDS if f not in fields]
    c.add("all_fields_present", not missing, missing or f"{len(FIELDS)} fields")
    c.eq("bundle_sha256_matches", sha_obj(fields), manifest.get("bundle_sha256"))
    c.eq("merkle_root_matches", merkle([sha_obj({k: fields[k]}) for k in FIELDS]), manifest.get("merkle_root"))
    try:
        pub = Ed25519PublicKey.from_public_bytes(base64.b64decode(pkg["public_key_b64"]))
        pub.verify(base64.b64decode(pkg["signature_b64"]), canonical(manifest))
        sig_ok = True
    except Exception as exc:  # noqa: BLE001
        sig_ok = False
        c.add("signature_error", False, f"{type(exc).__name__}")
    c.add("manifest_signature_valid", sig_ok, pkg.get("fingerprint_sha256"))
    rot = manifest.get("key_rotation") or {}
    try:
        stmt = {k: rot[k] for k in ("event", "old_public_key_b64", "new_public_key_b64", "rotated_at_utc")}
        Ed25519PublicKey.from_public_bytes(base64.b64decode(rot["old_public_key_b64"])).verify(
            base64.b64decode(rot["signature_by_old_key_b64"]), canonical(stmt))
        Ed25519PublicKey.from_public_bytes(base64.b64decode(rot["new_public_key_b64"])).verify(
            base64.b64decode(rot["signature_by_new_key_b64"]), canonical(stmt))
        c.add("key_rotation_cross_signed", True, rot.get("event"))
    except Exception as exc:  # noqa: BLE001
        c.add("key_rotation_cross_signed", False, f"{type(exc).__name__}")

    bad = []
    for name, digest in (manifest.get("artifact_hashes") or {}).items():
        p = artifacts / name
        if not p.is_file():
            bad.append(f"{name}:MISSING")
        elif hashlib.sha256(p.read_bytes()).hexdigest() != digest:
            bad.append(f"{name}:MISMATCH")
    c.add("artifact_hashes_match", not bad, bad[:10] or f"{len(manifest.get('artifact_hashes') or {})} files")

    # ---- 2. recompute the benchmark from raw rows -----------------------------------
    bench_dir = artifacts / "bench"
    arms = {}
    for f in sorted(bench_dir.glob("*.jsonl")):
        if "model_calls" in f.name:
            continue
        arms[f.stem] = read_jsonl(f)
    reported = fields["collective_benchmark"]["arms"]
    for arm, rows in arms.items():
        if arm not in reported:
            continue
        n = len(rows)
        k = sum(1 for r in rows if r.get("correct"))
        c.eq(f"{arm}:tasks", reported[arm]["tasks"], n)
        c.eq(f"{arm}:correct", reported[arm]["correct"], k)
        c.eq(f"{arm}:accuracy", reported[arm]["accuracy"], round(k / n, 4) if n else 0.0)
        c.eq(f"{arm}:wilson", reported[arm]["wilson_95"], wilson(k, n))
        c.eq(f"{arm}:tokens", reported[arm]["tokens"], sum(int(r.get("tokens", 0)) for r in rows))
        c.eq(f"{arm}:model_calls", reported[arm]["model_calls"],
             sum(int(r.get("model_calls", 0)) for r in rows))
        ac = sum(1 for r in rows if r.get("accepted") is True)
        fa = sum(1 for r in rows if r.get("accepted") is True and not r.get("correct"))
        c.eq(f"{arm}:verified_accepted", reported[arm]["verified_accepted"], ac)
        c.eq(f"{arm}:false_accepts", reported[arm]["false_accepts"], fa)

    # subset = the task ids that the multi-agent arms cover
    subset_ids = set()
    for arm in ("D_four", "E_eight"):
        if arm in arms:
            subset_ids |= {r["task_id"] for r in arms[arm]}
    c.add("subset_defined", bool(subset_ids), f"{len(subset_ids)} tasks")
    sub_report = fields["collective_benchmark"]["subset_arms"]
    for arm, rows in arms.items():
        if not subset_ids:
            break
        srows = [r for r in rows if r["task_id"] in subset_ids]
        if not srows:
            continue
        k, n = sum(1 for r in srows if r["correct"]), len(srows)
        if arm in sub_report:
            c.eq(f"subset:{arm}:correct", sub_report[arm]["correct"], k)
            c.eq(f"subset:{arm}:accuracy", sub_report[arm]["accuracy"], round(k / n, 4))
            c.eq(f"subset:{arm}:wilson", sub_report[arm]["wilson_95"], wilson(k, n))

    # paired McNemar recomputation
    def paired(a: str, b: str) -> Optional[Dict[str, int]]:
        if a not in arms or b not in arms:
            return None
        ra = {r["task_id"]: r for r in arms[a]}
        rb = {r["task_id"]: r for r in arms[b]}
        # the report's paired tests use the collaboration subset, so the verifier must too
        ids = sorted((set(ra) & set(rb)) & subset_ids) if subset_ids else sorted(set(ra) & set(rb))
        if not ids:
            return None
        return {"a_only": sum(1 for i in ids if ra[i]["correct"] and not rb[i]["correct"]),
                "b_only": sum(1 for i in ids if rb[i]["correct"] and not ra[i]["correct"]),
                "tasks": len(ids)}

    sig = fields["collective_benchmark"].get("paired_significance") or {}
    for a, b, key in (("D_four", "B_single", "D_four_vs_B_single"),
                      ("E_eight", "B_single", "E_eight_vs_B_single"),
                      ("T_tool_single", "B_single", "T_tool_single_vs_B_single")):
        got = paired(a, b)
        if got and key in sig:
            c.eq(f"mcnemar:{key}:a_only", sig[key]["a_only_correct"], got["a_only"])
            c.eq(f"mcnemar:{key}:b_only", sig[key]["b_only_correct"], got["b_only"])
            c.eq(f"mcnemar:{key}:p", sig[key]["p_value_two_sided"], mcnemar(got["a_only"], got["b_only"]))

    # ---- 3. cost, execution class, and no-authority invariants ------------------------
    tele = fields["model_runtime_and_providers"]
    c.add("real_local_inference_present",
          tele.get("local", {}).get("calls", 0) > 0 and tele.get("local", {}).get("execution_class") == "LOCAL_MODEL_INFERENCE",
          tele.get("local"))
    c.eq("cloud_calls_zero", tele.get("cloud", {}).get("verified_calls", 0), 0)
    c.eq("cloud_status_blocked", tele.get("cloud", {}).get("status"), "BLOCKED_BILLING")
    cost = fields["cost_and_resource_accounting"]
    c.eq("model_cost_zero_usd", cost.get("model_cost_usd"), 0.0)
    c.add("no_authority_granted_everywhere", _no_authority(fields), "grants_authority must be false or absent")
    c.add("production_not_ready", fields["final_acceptance"]["production_ready"] is False, "false")
    c.eq("deployment_not_performed", fields["final_acceptance"]["deployment_performed"], False)

    # ---- 3b. protocol iterations and the grounded synthesis mission -------------------
    pi = fields["protocol_iterations"]
    t2 = [r for r in pi["T2_tool_first"].get("rows", [])] if isinstance(pi.get("T2_tool_first"), dict) else []
    c.eq("protocol_iterations:t2_tool_calls", pi["T2_tool_first"].get("tools_used"),
         sum(1 for r in t2 if r.get("tool_calls")) if t2 else pi["T2_tool_first"].get("tools_used"))
    c.add("protocol_iterations:delta_reported", "delta_vs_single" in pi, pi.get("delta_vs_single"))
    sm = fields["synthesis_mission"]
    c.add("synthesis:checker_recorded", "correction" in (sm.get("verification") or {}),
          (sm.get("verification") or {}).get("recommendation_counts"))
    c.eq("synthesis:accepted_consistent_with_checks",
         sm["accepted"], all(sm["verification"]["checks"].values()))

    # ---- 4. tools ---------------------------------------------------------------------
    tools = fields["tool_execution"]
    all_tool_rows = [tc for rows in arms.values() for r in rows for tc in (r.get("tool_calls") or [])]
    c.eq("tool_calls_match_raw_rows", tools.get("calls"), len(all_tool_rows))
    c.eq("tool_denied_match_raw_rows", tools.get("by_status", {}).get("DENIED", 0),
         sum(1 for t in all_tool_rows if t.get("status") == "DENIED"))
    c.add("tool_execution_is_deterministic_class",
          all(t.get("execution_class") == "DETERMINISTIC_TOOL_EXECUTION" for t in all_tool_rows) if all_tool_rows else True,
          "DETERMINISTIC_TOOL_EXECUTION")

    # ---- 5. memory ---------------------------------------------------------------------
    mem = fields["memory_evaluation"]["mission_memory"]
    for arm, val in mem["arms"].items():
        rows = val["rows"]
        k = sum(1 for r in rows if r["exact"])
        c.eq(f"memory:{arm}:exact_match", val["exact_match"], round(k / max(1, len(rows)), 4))
    c.add("memory_has_poison_record", mem["poisoned_records"] >= 1, mem["poisoned_records"])
    c.eq("memory_grants_no_authority", mem["authority"]["grants_authority"], False)

    # ---- 6. self-improvement ------------------------------------------------------------
    si = fields["self_improvement"]
    if si.get("heldout_evaluation"):
        he = si["heldout_evaluation"]
        rows = he["rows"]
        base = [r for r in rows if r["arm"] == "baseline"]
        cand = [r for r in rows if r["arm"] == "candidate"]
        c.eq("selfimprove:baseline_correct", he["baseline_correct"], sum(1 for r in base if r["correct"]))
        c.eq("selfimprove:candidate_correct", he["candidate_correct"], sum(1 for r in cand if r["correct"]))
        c.eq("selfimprove:delta", he["accuracy_delta"],
             round((sum(1 for r in cand if r["correct"]) - sum(1 for r in base if r["correct"])) / max(1, len(base)), 4))
    c.eq("selfimprove:not_promoted", si.get("promoted"), False)
    c.eq("selfimprove:awaiting_human", si.get("promotion", {}).get("status"), "AWAITING_HUMAN_APPROVAL")
    c.eq("selfimprove:no_production_files", si.get("production_files_modified"), [])

    # ---- 7. security ---------------------------------------------------------------------
    sec = fields["security_lab"]
    c.eq("security:blocked_count", sec["blocked"], sum(1 for r in sec["results"] if r["blocked"]))
    c.eq("security:bypassed_count", sec["bypassed"], len(sec["results"]) - sec["blocked"])
    c.eq("security:no_bypasses", sec["bypassed"], 0)
    c.add("security:scenarios_at_least_45", sec["scenarios"] >= 45, sec["scenarios"])

    # ---- 8. durable execution ------------------------------------------------------------
    drill = fields["durable_execution_drill"]
    c.eq("execution:duplicate_effects_zero", drill["duplicate_external_effects"], 0)
    c.eq("execution:all_completed_verified", drill["all_completed_verified"], True)
    c.eq("execution:event_chain_ok", drill["metrics"]["event_chain_ok"], True)
    c.add("execution:workers_killed", drill["workers_killed_by_sigkill"] >= 1, drill["workers_killed_by_sigkill"])

    # ---- 9. research consistency ----------------------------------------------------------
    rl = fields["research_lab"]
    h1 = rl["statistics"]["H1_tool_beats_single"]
    replicated_claim = bool(rl["epistemic_ledger"]["REPRODUCED_RESULT"]["replicated"])
    same_direction = (h1["direction_main"] == h1["direction_replication"] and h1["direction_main"] != "EQUAL")
    consistent = replicated_claim == same_direction
    c.add("research:replication_flag_consistent_with_directions", consistent,
          {"main": h1["direction_main"], "replication": h1["direction_replication"], "claim": replicated_claim})
    c.eq("research:no_scientific_discovery_claim", rl["scientific_discovery"], "NOT_CLAIMED")
    c.eq("research:model_interpretation_not_promoted", rl["model_interpretation"]["promoted_to_fact"], False)

    # ---- 10. ClawX -------------------------------------------------------------------------
    cl = fields["clawx_state"]
    c.eq("clawx:unauth_401", cl["auth"]["unauthenticated"], 401)
    c.eq("clawx:cross_tenant_403", cl["auth"]["cross_tenant"], 403)
    c.eq("clawx:tampered_evidence_rejected", cl["auth"]["tampered_evidence"], True)
    c.add("clawx:views_15", cl["views_count"] == 15, cl["views_count"])
    c.add("clawx:shows_actual_calls_not_registrations",
          cl["actual_executing_agents"] <= 15 and cl["model_calls"] > 0,
          {"agents_with_calls": cl["actual_executing_agents"], "calls": cl["model_calls"]})

    # ---- 11. Agentic 75 honesty --------------------------------------------------------------
    a75 = fields["agentic75_matrix"]
    c.eq("agentic75:catalogued", a75["catalogued"], 75)
    c.eq("agentic75:model_backed_count", a75["model_backed"], 0)
    c.add("agentic75:not_all_relabelled_integrated", a75["mission_integrated_this_evolution"] <= 75,
          a75["mission_integrated_this_evolution"])

    # ---- 12. phase 0 baseline ---------------------------------------------------------------
    p0 = fields["phase0_baseline"]
    c.eq("phase0:e78_commit", p0["e78_final_commit"], "e2851428d2cd7ed9ae46f165e5d9b64c72632cf4")
    c.eq("phase0:e77_verifier", p0["e77_verifier"]["passed_count"], 27)
    c.eq("phase0:e78_verifier", p0["e78_verifier"]["passed_count"], 17)
    c.eq("phase0:acceptance_tests_passed", p0["acceptance_tests"]["passed"], 36)

    # ---- 13. acceptance state ----------------------------------------------------------------
    fa = fields["final_acceptance"]
    c.eq("acceptance:state_in_bundle", manifest["final_acceptance_state"], fa["state"])
    c.add("acceptance:blockers_listed", isinstance(fa.get("blockers"), list), len(fa.get("blockers") or []))

    passed = sum(1 for x in c.checks if x["ok"])
    total = len(c.checks)
    result = {
        "schema": "cain2027.e79.offline_verification.v2",
        "package": str(pkg_path),
        "artifacts": str(artifacts),
        "verified": passed == total,
        "passed_count": passed,
        "total_checks": total,
        "failed": [x for x in c.checks if not x["ok"]],
        "checks": c.checks,
        "merkle_root": manifest.get("merkle_root"),
        "acceptance_state": fa.get("state"),
        "note": "Recomputes headline numbers from raw rows. Verifies a record, not the world. "
                "The signing key is self-generated unless its fingerprint is pinned out of band.",
    }
    print(json.dumps(result, indent=1))
    return 0 if result["verified"] else 1


def _no_authority(fields: Dict[str, Any]) -> bool:
    """Recursively assert no field claims to grant authority."""
    bad: List[str] = []

    def walk(obj: Any, path: str) -> None:
        if isinstance(obj, dict):
            if obj.get("grants_authority") is True:
                bad.append(path)
            for k, v in obj.items():
                walk(v, f"{path}.{k}")
        elif isinstance(obj, list):
            for i, v in enumerate(obj):
                walk(v, f"{path}[{i}]")

    walk(fields, "fields")
    return not bad


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