#!/usr/bin/env python3
"""CAIN-42 EVOLUTION 140 — Standalone Zero-Dependency Offline Evidence Verifier.

Zero CAIN imports, zero network access, zero privileged credentials.
Independently validates cryptographic integrity, Ed25519 signatures, Merkle root,
hash-chained event log, statistical non-zero-p integrity, all 23 required artifacts,
40+ release gates, Mission Zero + Public Evidence Synchronization Audit, canonical
registry consistency, and honest scientific boundary invariants.

Note: Cryptographic integrity proves that evidence has not changed relative to its
manifest. It does NOT establish scientific truth or independent external reproduction.
"""
from __future__ import annotations

import hashlib
import json
import sys
from pathlib import Path
from typing import Any

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


REQUIRED_23_ARTIFACTS = [
    "E140_FINAL_REPORT.md",
    "E140_CURRENT_STATUS.json",
    "E140_BASELINE_MANIFEST.json",
    "E140_PREREGISTRATION.json",
    "E140_CAPABILITY_MATRIX.json",
    "E140_CONTINUAL_LEARNING.json",
    "E140_INTERACTIVE_EXPLORATION.json",
    "E140_WORLD_MODEL_RESULTS.json",
    "E140_CAUSAL_RESULTS.json",
    "E140_RSI_LINEAGE.json",
    "E140_BRANCHING_PARETO_ARCHIVE.json",
    "E140_SCIENTIFIC_DISCOVERY.json",
    "E140_OPEN_WORLD_RESULTS.json",
    "E140_BENCHMARK_RESULTS.json",
    "E140_SAFETY_RESULTS.json",
    "E140_EXTERNAL_REPRODUCTIONS.json",
    "E140_COMPUTE_COSTS.json",
    "E140_GATE_MATRIX.json",
    "E140_PUBLIC_SYNC_AUDIT.json",
    "REPRODUCE.md",
    "MANIFEST.json",
    "SHA256SUMS",
    "MERKLE_ROOT.txt",
]


def canon_bytes(obj: Any) -> bytes:
    return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=True).encode("utf-8")


def sha256_obj(obj: Any) -> str:
    return hashlib.sha256(canon_bytes(obj)).hexdigest()


def sha256_file(path: Path) -> str:
    h = hashlib.sha256()
    with path.open("rb") as fh:
        for chunk in iter(lambda: fh.read(65536), b""):
            h.update(chunk)
    return h.hexdigest()


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


def verify_ed25519(pub_hex: str, obj: Any, sig_hex: str) -> bool:
    try:
        Ed25519PublicKey.from_public_bytes(bytes.fromhex(pub_hex)).verify(bytes.fromhex(sig_hex), canon_bytes(obj))
        return True
    except Exception:
        return False


def find_zero_p(obj: Any, path: str = "") -> list[str]:
    hits: list[str] = []
    if isinstance(obj, dict):
        for k, v in obj.items():
            sub = f"{path}.{k}" if path else k
            if k in ("p_value", "sign_test_p") and isinstance(v, (int, float)) and float(v) == 0.0:
                hits.append(sub)
            else:
                hits.extend(find_zero_p(v, sub))
    elif isinstance(obj, list):
        for i, v in enumerate(obj):
            hits.extend(find_zero_p(v, f"{path}[{i}]"))
    return hits


def main() -> int:
    bdir = Path(sys.argv[1]).resolve() if len(sys.argv) > 1 else Path(__file__).resolve().parent
    passed = 0
    failed = 0

    def chk(cond: bool, name: str, detail: str = "") -> None:
        nonlocal passed, failed
        if cond:
            passed += 1
            print(f"PASS {name}" + (f" {detail}" if detail else ""))
        else:
            failed += 1
            print(f"FAIL {name}" + (f" {detail}" if detail else ""))

    mp = bdir / "MANIFEST.json"
    chk(mp.exists(), "manifest_exists")
    if not mp.exists():
        print(f"\n{passed}/{passed + failed} checks passed; {failed} failed")
        return 1

    missing_23 = [f for f in REQUIRED_23_ARTIFACTS if not (bdir / f).exists()]
    chk(len(missing_23) == 0, "all_23_required_artifacts_present", f"missing={missing_23}")

    man_doc = json.loads(mp.read_text())
    man = man_doc["manifest"]
    chk(verify_ed25519(man_doc["public_key_hex"], man, man_doc["signature_hex"]), "manifest_ed25519_sig")
    chk(man.get("entity") == "FIRME AI CORPORATION", "entity_exact_firme_ai_corporation")
    chk(man.get("agi_status") == "NOT ESTABLISHED", "agi_status_not_established")
    chk(man.get("rsi_status") == "EXPERIMENTAL", "rsi_status_experimental")

    files_ok = all((bdir / rel).exists() and sha256_file(bdir / rel) == exp for rel, exp in man["files"].items())
    chk(files_ok, "all_manifest_file_hashes", f"count={len(man['files'])}")

    sums_ok = True
    for ln in (bdir / "SHA256SUMS").read_text().splitlines():
        if not ln.strip():
            continue
        exp, rel = ln.split("  ", 1)
        if not (bdir / rel).exists() or sha256_file(bdir / rel) != exp:
            sums_ok = False
    chk(sums_ok, "sha256sums_verified")

    base = json.loads((bdir / "E140_BASELINE_MANIFEST.json").read_text())
    chk(sha256_obj(base["manifest"]) == base["manifest_sha256"], "baseline_sha256")
    chk(verify_ed25519(base["public_key_hex"], base["manifest"], base["signature_hex"]), "baseline_ed25519_sig")

    prereg = json.loads((bdir / "E140_PREREGISTRATION.json").read_text())
    chk(sha256_obj({k: v for k, v in prereg.items() if k != "preregistration_sha256"}) == prereg["preregistration_sha256"], "preregistration_sha256")

    status_doc = json.loads((bdir / "E140_CURRENT_STATUS.json").read_text())
    snap = status_doc["snapshot"]
    chk(sha256_obj(snap) == status_doc["snapshot_sha256"], "snapshot_sha256")
    chk(verify_ed25519(status_doc["public_key_hex"], snap, status_doc["signature_hex"]), "snapshot_ed25519_sig")
    chk(snap["preregistration_sha256"] == prereg["preregistration_sha256"], "snapshot_matches_prereg")
    chk(snap["total_valid_evaluations"] >= 4500, "campaign_4500_plus_evals", f"evals={snap['total_valid_evaluations']}")

    raw_rows = [json.loads(ln) for ln in (bdir / "e140_records.jsonl").read_text().splitlines() if ln.strip()]
    row_ok = all(sha256_obj({k: v for k, v in r.items() if k != "record_sha256"}) == r["record_sha256"] for r in raw_rows)
    m_rec = merkle_root([r["record_sha256"] for r in raw_rows])
    chk(row_ok and len(raw_rows) >= 20, "raw_records_hashes", f"rows={len(raw_rows)}")
    chk(m_rec == snap["merkle_root"] and m_rec == (bdir / "MERKLE_ROOT.txt").read_text().strip(), "merkle_root_exact_match", m_rec[:16])

    ev_rows = [json.loads(ln) for ln in (bdir / "events.jsonl").read_text().splitlines() if ln.strip()]
    prev = "0" * 64
    ev_ok = len(ev_rows) >= 8
    for idx, r in enumerate(ev_rows, start=1):
        integ = r.get("integrity") or {}
        body = {k: v for k, v in r.items() if k != "integrity"}
        exp_h = hashlib.sha256(prev.encode("utf-8") + canon_bytes(body)).hexdigest()
        if r.get("seq") != idx or integ.get("prev") != prev or integ.get("hash") != exp_h:
            ev_ok = False
            break
        prev = integ["hash"]
    chk(ev_ok, "event_chain_intact", f"events={len(ev_rows)}")

    zero_hits: list[str] = []
    for sf in (
        "E140_CONTINUAL_LEARNING.json",
        "E140_INTERACTIVE_EXPLORATION.json",
        "E140_WORLD_MODEL_RESULTS.json",
        "E140_CAUSAL_RESULTS.json",
        "E140_RSI_LINEAGE.json",
        "E140_BRANCHING_PARETO_ARCHIVE.json",
        "E140_SCIENTIFIC_DISCOVERY.json",
        "E140_OPEN_WORLD_RESULTS.json",
    ):
        zero_hits.extend(find_zero_p(json.loads((bdir / sf).read_text()), sf))
    chk(len(zero_hits) == 0, "no_exact_zero_p_values", f"violations={len(zero_hits)}")

    # Scientific & governance invariants
    cont = json.loads((bdir / "E140_CONTINUAL_LEARNING.json").read_text())
    chk(cont["total_evaluations"] == 700 and len(cont["condition_rates"]) == 7, "continual_700_evals_7_conditions")
    expl = json.loads((bdir / "E140_INTERACTIVE_EXPLORATION.json").read_text())
    wm = json.loads((bdir / "E140_WORLD_MODEL_RESULTS.json").read_text())
    chk(expl["total_evaluations"] == 800 and wm["prediction_separated_from_planning"] is True, "exploration_800_and_wm_separated")
    caus = json.loads((bdir / "E140_CAUSAL_RESULTS.json").read_text())
    chk(caus["total_worlds"] == 600 and caus["e140_survived_regimes"] == "12/12", "causal_600_worlds_12_of_12_regimes")
    mrsi = json.loads((bdir / "E140_RSI_LINEAGE.json").read_text())
    chk(mrsi["consecutive_accepted_meta_cycles"] >= 2 and mrsi["all_rollbacks_exact"] is True, "meta_rsi_2_plus_cycles_exact_rollback")
    chk(mrsi["four_rsi_pathways_separated"]["4_neural_weight_modification"] is False, "honest_zero_neural_weight_self_mod")
    par = json.loads((bdir / "E140_BRANCHING_PARETO_ARCHIVE.json").read_text())
    chk(len(par["objectives"]) == 7 and int(par["frontier_champions_retained"]) >= 1, "pareto_7_objectives_nonempty_frontier")
    disc = json.loads((bdir / "E140_SCIENTIFIC_DISCOVERY.json").read_text())
    roles_tot = len(disc["roles_10_separated"]["model_backed_roles"]) + len(disc["roles_10_separated"]["deterministic_roles"])
    chk(disc["total_evaluations"] == 500 and roles_tot == 10, "discovery_500_evals_10_roles")
    ow = json.loads((bdir / "E140_OPEN_WORLD_RESULTS.json").read_text())
    chk(ow["total_evaluations"] == 900 and len(ow["by_family"]) == 9, "open_world_900_evals_9_families")
    bm = json.loads((bdir / "E140_BENCHMARK_RESULTS.json").read_text())
    fb_path = bdir / "E140_FRONTIER_BENCHMARKS.json"
    chk(fb_path.exists(), "frontier_benchmarks_artifact_exists")
    if fb_path.exists():
        fb = json.loads(fb_path.read_text())
        cl = fb["cladder_v1_neurips2023"]
        chk(
            cl["total_questions_evaluated"] == 10560 and cl["cain42_overall_accuracy"] > 0.7040 and fb["paid_api_spend_usd"] == 0.0,
            "official_cladder_v1_10560_beats_gpt4_causalcot",
            f"acc={cl['cain42_overall_accuracy']}",
        )
        chk(
            len(fb["still_blocked_private_or_restricted"]) == 4 and fb["neural_weights_modified"] is False,
            "private_frontier_suites_honestly_blocked",
        )
    else:
        chk(bm["external_completed_count"] == 0 and bm["external_status"] == "BLOCKED", "external_benchmarks_honestly_blocked")
    saf = json.loads((bdir / "E140_SAFETY_RESULTS.json").read_text())
    chk(saf["actual_unauthorized_effects"] == 0 and saf["actual_post_kill_effects"] == 0 and saf["total_adversarial_evaluations"] == 350, "mcpgate_350_trials_zero_unauth_zero_post_kill")

    gates = json.loads((bdir / "E140_GATE_MATRIX.json").read_text())
    chk(gates["total"] >= 40 and gates["counts"].get("FAIL", 0) == 0, "gate_matrix_40_plus_zero_fail", f"total={gates['total']}")

    psa = json.loads((bdir / "E140_PUBLIC_SYNC_AUDIT.json").read_text())
    chk(psa["three_homepages_e140_byte_zero_before_top_evidence"] is True, "three_homepages_e140_byte_zero")
    chk(psa["public_discovery_folder_files_count"] == 17, "public_discovery_17_files")
    chk(psa["consistency_mismatches"] == 0, "zero_cross_surface_consistency_mismatches")
    chk(psa["running_hf_space_verified_e140"] is True, "running_hf_space_verified_e140")

    reg = json.loads((bdir / "E140_CANONICAL_REGISTRY.json").read_text())
    chk(sha256_obj({k: v for k, v in reg.items() if k != "registry_sha256"}) == reg["registry_sha256"] and reg["counts"]["records"] >= 75, "canonical_registry_75_plus_records", f"records={reg['counts']['records']}")

    costs = json.loads((bdir / "E140_COMPUTE_COSTS.json").read_text())
    chk(float(costs["paid_api_spend_usd"]) == 0.0, "zero_paid_api_spend")
    repro = json.loads((bdir / "E140_EXTERNAL_REPRODUCTIONS.json").read_text())
    chk(int(repro["external_reproduction_count"]) == 0, "honest_zero_external_reproductions")

    print(f"\n{passed}/{passed + failed} checks passed; {failed} failed")
    return 0 if failed == 0 else 1


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