"""RUN-027 - Round 24: the portfolio residual core and joint-witness admission.

Round 24 deepens B7 on Round 23's four hardest cells instead of opening a joint multi-witness
tree: cells 0, 1 and 2 close (depths 30, 32, 32), cell 3 is left at depth 30 with 1.49e-7 of
open volume, "contracting". It publishes the first complete-edge certificate (cell 0, B7, depth
30: 18555 nodes, 9278 CERT leaves, a preorder topology), proves joint dominance
J_B >= max_K J_K and a pointwise joint-necessity theorem, and adds an admission state machine
REDUNDANT / DEFERRED-CONTRACTING / CANDIDATE / NECESSARY. Here the certificate is rebuilt bit for
bit, every tail number is traced to the tree it comes from, cell 3 is run on to closure, and the
theorems are checked.

Checks
  A0 anchors      the numbers and formulas below occur in Round 24 as written
  W1 theorems     sec. 1-4, 11-13, 17-18: joint dominance, pointwise necessity, the joint lower
                  hull and the joint counterexample
  W2 tails        sec. 6, 9: each tail value traced to its tree - the 96-direction pilot or the
                  exact-core lift
  W3 certificate  sec. 7: cell 0's B7 tree to depth 30 - counts, topology bytes, slack, closest call
  W4 cells        sec. 8-10: cells 1-3 at depth 30 and to closure
  W5 residual     sec. 15, 22-23: the admission states and the residual at depth 36
  W6 H19          the recorded H19 depth-24 edge on cell 0
  W7 recorded     what the package records and ships
  P1 package      the residual core, the references, the spec, the crystal and the text agree
  P2 package      VALIDATION against the archive; its checks from the data
  R1 registry     Round 24's judgments reviewed, first-used statuses defined

Usage: python code/run027_round24_residual_joint.py [--drill] [--blind CID]
"""
import argparse
import copy
import hashlib
import inspect
import json
import math
from multiprocessing import Pool

import numpy as np

import gate
import registry
import run008_round05_saturation_gate as r8
import run009_round06_witness_exchange as r9
import run011_round08_conditional_lift as r11
import run012_round09_adaptive_atlas as r12
import run013_round10_reference_dry_run as r13
import run019_round16_global_pilot as r19
import run020_round17_d3_cpg as r20
import run022_round19_necessity_lift as r22
import run023_round20_mixed_escalation as r23
import run025_round22_fourier_pareto as r25
import run026_round23_portfolio_closure as r26
from corpus import SOURCES, TREE, source_bytes, walk_sources
from run009_round06_witness_exchange import placed_disc, placed_reuleaux
from run010_round07_minimizer_atlas import d_star, t_text
from run011_round08_conditional_lift import placed_vertices
from run012_round09_adaptive_atlas import core_pieces

PKG = "main.zip!/AMRAL_LUC_FC_Round_24_Package_v0.1.zip!/"
DOC = PKG + "AMRAL_Lebesgue_Finite_Closure_Round_24_Residual_Joint_Admission_v0.1.md"
CRYSTAL = PKG + "AMRAL_LUC_FC_State_Crystal_Round_24_v0.1.md"
RESIDUAL = PKG + "AMRAL_LUC_FC_Round_24_RESIDUAL_CORE.json"
ADMISSION = PKG + "AMRAL_LUC_FC_Round_24_JOINT_ADMISSION_SPEC.json"
VALIDATION = PKG + "AMRAL_LUC_FC_Round_24_VALIDATION.json"
REF = PKG + "AMRAL_LUC_FC_Round_24_reference/"
EDGE = REF + "cell0_B7_complete_edge/"
LOG = TREE / "data" / "gate-logs" / "run027-round24-residual-joint.json"
CACHE = TREE / "data" / "cache" / "run027-residual.json"
MANIFEST = TREE / "data" / "SOURCE_MANIFEST.v1.json"

T, TM = 0.835, 0.8365
HARD, EXACT_MAX, SEED = 4, 36, 20260925

ANCHORS = {
    "W1": [r"J_K(H(q))=\min_g\operatorname{Area}\operatorname{conv}(H(q)\cupgK).", r"J_{\mathcalB}(H(q))\geJ_K(H(q)).",
           r"J_{\mathcalB}(H(q))\ge\max_{K\in\mathcalB}J_K(H(q)).", r"\text{jointwitnessismathematicallyredundant}.",
           r"\operatorname{Area}\operatorname{conv}(H(q)\cupg_iK_i)<T,", r"J_{K_i}(H(q))<T\quad\foralli.",
           r"\text{cellhasnostrictedge}\not\Rightarrow\text{jointnecessary}.", r"L_1L_2",
           r"L_{\rmjoint}=\operatorname{Area}\operatorname{conv}(G_{\rmbase}\cupG_K\cupG_L).", r"U(H(q)\cupg_KK\cupg_LL)<T,",
           r"\rho_{d,\Delta}(C,K)=\frac{V_{C,K}^{(d+\Delta)}}{V_{C,K}^{(d)}}.", r"\text{JOINT-WITNESSADMISSIONLOGIC:CLOSED}"],
    "W2": [r"9.34\times10^{-4}.", r"5.17\times10^{-5}.", r"6.94\times10^{-6}.", r"1.77\times10^{-6}.", r"9.39\times10^{-8}.",
           r"9.70\times10^{-4}.", r"5.85\times10^{-5}.", r"\boxed{1.49\times10^{-7}.}", r"3.93\times10^{2}"],
    "W3": [r"I_{0,B_7}^{(30)}=1", r"18555\text{nodes},", r"9278\text{CERTleaves},", r"0\text{PENDING}.", r"18555=2(9278)-1.",
           r"\text{FIRSTREFERENCESTRICTLIFTEDGE:CLOSED}"],
    "W4": [r"\boxed{\text{COMPLETEatdepth}30};", r"\boxed{\text{COMPLETEatdepth}32};", r"\boxed{1.49\times10^{-7}},",
           r"6.79\times10^{-10}.", r"$$152$$", r"2.06\times10^{-8}", r"I_{1,B_7}^{(32)}=I_{2,B_7}^{(32)}=1."],
    "W5": [r"\boxed{3/4}", r"\{0,1,2,3\}\to\{3\}", r"\boxed{75\%}", r"\boxed{0\text{strictCOMPLETEedges}.}",
           r"\text{JOINTNECESSITYONTESTEDCELLS:NOTESTABLISHED}"],
}

CLAIMS = {
    # the tail values and the tree each comes from: 96 = Round 23's 96-direction pilot, exact = the exact-core lift
    "tail_sources": {"cell0": {"16": "96", "22": "96", "24": "exact", "26": "exact", "28": "exact", "30": "exact"},
                     "cell3": {"16": "96", "22": "96", "30": "exact"}},
    "cell3_ratio_exact_only": 217.3,
    "certificate": {"nodes": 18555, "cert": 9278, "pending": 0, "topology_matches": True, "slack": 4.1174202158877904e-08},
    "closest_call_above": 1e-9,
    "cells": {"1": {"d30_open": 5, "closes_at": 31, "nodes": 22009}, "2": {"d30_open": 152, "closes_at": 31, "nodes": 26189},
              "3": {"d30_open": 1098, "d30_nodes": 31797, "closes_at": 34, "nodes": 36193}},
    "residual_at_36": [],
    # this tree's H19 at depth 24 less the package's (nodes, open): RUN-026's unresolved harmonic convention
    "h19_d24_gap": [22, -2],
    "recorded_facts": {"ships_code": False, "states_lower_rule": True, "states_pad": True, "pad": 1e-10},
}

# ---------------------------------------------------------------- the exact-core B7 lift, keeping the tree and every bound

def _exact(args):
    c, h, wc, wh = args
    base = r19.cores(np.asarray(c, dtype=float), np.asarray(h, dtype=float))
    rho = 1 - r23.delta_n(7, wh)
    if rho <= r23.rn(7) or base[0] is None or base[1] is None:
        return None
    kn = core_pieces(placed_vertices(7, wc[0], wc[1:3]), rho)[0]
    return r19.exact_area(placed_disc() + base[0] + base[1] + kn)


def exact_lift(pool, c, h, max_depth, keep_tree):
    """RUN-023's exact-core B7 lift (the bound less the pilot's 1e-9 pad), recording the least
    CERT slack and the closest call - the least |area - T| over every box it bounded"""
    tn = t_text(7, d_star(TM))
    level = [("", (math.pi / 7, 0.0, 0.0), (math.pi / 7, tn, tn))]
    out = {"per": {}, "slack": None, "closest": None, "tree": {} if keep_tree else None}
    nodes = closed = 0
    for d in range(max_depth + 1):
        areas = pool.map(_exact, [(list(c), list(h), wc, wh) for _, wc, wh in level], chunksize=8)
        nodes += len(level)
        nxt, opened = [], 0
        for (p, wc, wh), area in zip(level, areas):
            lb = math.pi / 4 if area is None else area - r19.PAD
            if area is not None:
                gap = abs(area - T)
                out["closest"] = gap if out["closest"] is None else min(out["closest"], gap)
            if lb >= T:
                closed += 1
                out["slack"] = lb - T if out["slack"] is None else min(out["slack"], lb - T)
                if keep_tree:
                    out["tree"][p] = "C"
                continue
            opened += 1
            if keep_tree:
                out["tree"][p] = "P" if d == max_depth else "I"
            if d == max_depth:
                continue
            trial = []
            for a in range(3):
                hh = list(wh)
                hh[a] /= 2
                trial.append(r23.delta_n(7, hh))
            a = int(np.argmin(trial))
            hh = list(wh)
            hh[a] /= 2
            lo, hi = list(wc), list(wc)
            lo[a] -= hh[a]
            hi[a] += hh[a]
            nxt += [(p + "0", tuple(lo), tuple(hh)), (p + "1", tuple(hi), tuple(hh))]
        out["per"][d] = [nodes, closed, opened]
        level = nxt
        if not level:
            break
    return out


def compute():
    out = {}
    with Pool(14) as pool:
        _, cells = r26.twelve(pool)
        out["cells"] = cells[:HARD]
        e0 = exact_lift(pool, cells[0]["center"], cells[0]["half"], 30, True)
        topo, tags = r23.emit(e0["tree"], True, True, 0)
        out["certificate"] = {"per": e0["per"], "slack": e0["slack"], "closest": e0["closest"], "topology": topo.hex(),
                              "tags": tags.hex()}
        out["exact"] = []
        out["pilot96"] = []
        for cell in cells[:HARD]:
            r = exact_lift(pool, cell["center"], cell["half"], EXACT_MAX, False)
            out["exact"].append({"per": r["per"], "slack": r["slack"], "closest": r["closest"]})
            out["pilot96"].append(r26.lift(pool, "B7", cell["center"], cell["half"], 22, r26.N_PILOT))
        out["h19_d24"] = r26.lift(pool, "H19", cells[0]["center"], cells[0]["half"], 24, r26.N_PILOT)
    return out


def _code_key(files):
    digest = hashlib.sha256()
    for fn in (_exact, exact_lift, compute):
        digest.update(inspect.getsource(fn).encode())
    for mod in (r8, r9, r11, r12, r13, r19, r20, r22, r23, r25, r26):
        digest.update(open(mod.__file__, "rb").read())
    digest.update(files)
    digest.update(repr((HARD, EXACT_MAX, SEED)).encode())
    return digest.hexdigest()


_COMPUTED = {}


def computed(key_bytes):
    key = _code_key(key_bytes)
    if key not in _COMPUTED:
        if CACHE.exists():
            cached = json.loads(CACHE.read_text(encoding="utf-8"))
            if cached.get("key") == key:
                _COMPUTED[key] = cached["results"]
        if key not in _COMPUTED:
            _COMPUTED[key] = json.loads(json.dumps(compute()))
            CACHE.parent.mkdir(parents=True, exist_ok=True)
            CACHE.write_text(json.dumps({"key": key, "results": _COMPUTED[key]}) + "\n", encoding="utf-8")
    return _COMPUTED[key]


# ---------------------------------------------------------------- checks

def near(a, b, tol):
    return abs(a - b) <= tol


def at(per, d):
    return r23.at(per, d)


ROOT7 = 2 * math.pi / 7 * (2 * t_text(7, d_star(TM))) ** 2


def w1_theorems(w):
    """joint dominance and the joint lower hull are monotonicity of the hull; pointwise necessity
    is a statement about single-witness values only"""
    out = []
    rng = np.random.default_rng(SEED)
    dominance, core_ok = np.inf, True
    for _ in range(16):
        q = [rng.uniform(-0.01, 0.01), rng.uniform(-0.01, 0.01), rng.uniform(0, 1.2), rng.uniform(-0.02, 0.02), rng.uniform(-0.02, 0.02)]
        g1 = [rng.uniform(0, 1), rng.uniform(-0.02, 0.02), rng.uniform(-0.02, 0.02)]
        g2 = [rng.uniform(0, 1), rng.uniform(-0.02, 0.02), rng.uniform(-0.02, 0.02)]
        base = [p for body in r25.base_bodies(["cell", q]) for p in body]
        k1, k2 = placed_reuleaux(7, g1[0], g1[1:3]), placed_reuleaux(11, g2[0], g2[1:3])
        both = r19.exact_area(base + k1 + k2)
        dominance = min(dominance, both - max(r19.exact_area(base + k1), r19.exact_area(base + k2)))
        # the joint lower hull: eroded cores sit inside their bodies, so their hull is never larger
        c1 = core_pieces(placed_vertices(7, g1[0], g1[1:3]), 0.99)[0]
        c2 = core_pieces(placed_vertices(11, g2[0], g2[1:3]), 0.99)[0]
        core_ok &= r19.exact_area(base + c1 + c2) <= both + 1e-15
    spec = w["admission"]
    if dominance < 0 or not core_ok or spec["definitions"]["monotonicity"] != "J_B(H(q)) >= max_K J_K(H(q)) for a fixed portfolio B" \
            or "does not prove the joint portfolio succeeds" not in spec["pointwise_joint_necessity_relative_to_portfolio"]["warning"]:
        out.append(f"dominance least gain {dominance:.2e}; cores inside {core_ok}")
    w["w1"] = {"dominance_least_gain": dominance, "joint_core_hull_below": core_ok}
    return out


def w2_tails(w):
    """sec. 6 and 9: each recorded tail value against the 96-direction pilot and the exact tree"""
    out, comp, res = [], w["computed"], w["residual"]
    text_vals = {"cell0": {"16": 9.34e-4, "22": 5.17e-5, "24": 6.94e-6, "26": 1.77e-6, "28": 9.39e-8, "30": 0.0},
                 "cell3": {"16": 9.70e-4, "22": 5.85e-5, "30": 1.49e-7}}
    sources = {}
    for cell, i in (("cell0", 0), ("cell3", 3)):
        sources[cell] = {}
        for d, v in text_vals[cell].items():
            dd = int(d)
            ex = at(comp["exact"][i]["per"], dd)[2] / 2 ** dd * ROOT7
            s96 = at(comp["pilot96"][i], dd)[2] / 2 ** dd * ROOT7 if dd <= 22 else None
            fits = [name for name, x in (("96", s96), ("exact", ex)) if x is not None and (near(x, v, 5e-3 * v) if v else x == 0)]
            sources[cell][d] = fits[0] if len(fits) == 1 else fits
    rec = {r["cell"]: r for r in res["first_four_cells"]}
    for i in range(HARD):
        s96 = [at(comp["pilot96"][i], d)[2] / 2 ** d * ROOT7 for d in (16, 22)]
        if not near(s96[0], rec[i]["B7_unresolved_volume_d16"], 1e-12 * s96[0]) or not near(s96[1], rec[i]["B7_unresolved_volume_d22"], 1e-12 * s96[1]):
            out.append(f"cell {i}: the recorded depth-16/22 volumes are not the 96-direction pilot's")
    exact_ratio = (at(comp["exact"][3]["per"], 22)[2] / 2 ** 22) / (at(comp["exact"][3]["per"], 30)[2] / 2 ** 30)
    if sources != w["tail_sources"] or not near(exact_ratio, w["cell3_ratio_exact_only"], 0.05):
        out.append(f"tail sources {sources}; cell 3 exact-only ratio {exact_ratio:.1f}")
    w["w2"] = {"sources": sources, "cell3_ratio_mixed": 5.85e-5 / 1.49e-7, "cell3_ratio_exact_only": exact_ratio,
               "cell0_exact_d16_d22": [at(comp["exact"][0]["per"], d)[2] / 2 ** d * ROOT7 for d in (16, 22)]}
    return out


def w3_certificate(w):
    """sec. 7: cell 0's B7 tree to depth 30, rebuilt and re-emitted"""
    out, cert, man = [], w["computed"]["certificate"], w["edge_manifest"]
    nodes, closed, opened = at(cert["per"], 30)
    topo = bytes.fromhex(cert["topology"])
    got = {"nodes": nodes, "cert": closed, "pending": opened,
           "topology_matches": topo == w["topology"] and hashlib.sha256(topo).hexdigest() == man["topology"]["sha256"],
           "slack": cert["slack"]}
    cells = w["computed"]["cells"]
    if max(abs(a - b) for a, b in zip(cells[0]["center"] + cells[0]["half"], man["base_center"] + man["base_halfwidth"])) > 1e-15 \
            or man["base_path"] != cells[0]["path"]:
        out.append("the certificate's base cell is not Round 23's cell 0")
    if {k: v for k, v in got.items() if k != "slack"} != {k: v for k, v in w["certificate"].items() if k != "slack"} \
            or not near(got["slack"], w["certificate"]["slack"], 1e-15) or nodes != 2 * closed - 1 \
            or (nodes, closed, opened) != (man["nodes"], man["cert_leaves"], man["pending_leaves"]) or man["topology"]["bits"] != nodes \
            or not near(cert["slack"] + r19.PAD - 1e-10, man["worst_reference_slack"], 1e-15):
        # this tree's pad is the pilot's 1e-9, the manifest's 1e-10: its slack is 9e-10 higher
        out.append(f"the certificate: {got}")
    if not cert["closest"] > w["closest_call_above"]:
        out.append(f"a box within {cert['closest']:.1e} of T: the pad could decide it")
    w["w3"] = {**got, "closest_call": cert["closest"], "topology_bytes": len(topo)}
    return out


def w4_cells(w):
    """sec. 8-10: cells 1-3 at depth 30 and to closure"""
    out, comp = [], w["computed"]
    got = {}
    for i in (1, 2, 3):
        per = comp["exact"][i]["per"]
        last = max(per, key=int)
        n30, _, o30 = at(per, 30)
        got[str(i)] = {"d30_open": o30, "closes_at": int(last), "nodes": per[last][0]}
        if i == 3:
            got[str(i)]["d30_nodes"] = n30
        if per[last][2] != 0:
            out.append(f"cell {i}: open at depth {last}")
    refs = {1: w["ref"]["r24_cell1_b7_d32.json"], 2: w["ref"]["r24_cell2_b7_d32.json"], 3: w["ref"]["r24_cell3_b7_d30.json"]}
    for i, r in refs.items():
        n, cl, o = at(comp["exact"][i]["per"], r["depth"])
        if (n, cl, o) != (r["nodes"], r["cert_leaves"], r["pending_leaves"]) or not near(r["unresolved_volume"], o / 2 ** r["depth"] * ROOT7, 1e-12 * max(r["unresolved_volume"], 1e-30)):
            out.append(f"cell {i} at depth {r['depth']}: {n, cl, o} against {r['nodes'], r['cert_leaves'], r['pending_leaves']}")
    r0 = w["ref"]["r24_cell0_b7_d30.json"]
    if at(comp["exact"][0]["per"], 30) != (r0["nodes"], r0["cert_leaves"], r0["pending_leaves"]):
        out.append("cell 0 at depth 30")
    if got != w["cells"]:
        out.append(f"cells 1-3: {got}")
    w["w4"] = got
    return out


def w5_residual(w):
    """sec. 15, 22-23: the admission states, and the residual when the exact trees are let run"""
    out, comp, res = [], w["computed"], w["residual"]
    tested = {r["cell"]: r for r in res["first_four_cells"]}
    at_budget = sorted(i for i in range(HARD) if at(comp["exact"][i]["per"], tested[i]["deep_budget"])[2] > 0)
    at36 = sorted(i for i in range(HARD) if at(comp["exact"][i]["per"], EXACT_MAX)[2] > 0)
    states = {i: "REDUNDANT" if at(comp["exact"][i]["per"], tested[i]["deep_budget"])[2] == 0 else "DEFERRED-CONTRACTING" for i in range(HARD)}
    if at_budget != res["first_four_residual_after_tested_budgets"] or at36 != w["residual_at_36"] \
            or any(states[i] != tested[i]["joint_admission"] for i in range(HARD)):
        out.append(f"residual at the tested budgets {at_budget}, at depth {EXACT_MAX} {at36}, states {states}")
    w["w5"] = {"residual_at_tested_budgets": at_budget, "residual_at_36": at36, "states_at_tested_budgets": states}
    return out


def w6_h19(w):
    """the recorded H19 depth-24 edge on cell 0 (the 96-direction pilot)"""
    comp, r = w["computed"], w["ref"]["r24_cell0_d24_b7_h19.json"]
    rec = {x["witness"]: x for x in r}
    n, _, o = at(comp["h19_d24"], 24)
    gap = [n - rec["H19"]["nodes"], o - rec["H19"]["pending_leaves"]]
    w["w6"] = {"this_tree": [n, o], "package": [rec["H19"]["nodes"], rec["H19"]["pending_leaves"]], "gap": gap,
               "b7_record_is_exact_tree": at(comp["exact"][0]["per"], 24) == (rec["B7"]["nodes"], rec["B7"]["cert_leaves"], rec["B7"]["pending_leaves"])}
    return [] if gap == w["h19_d24_gap"] and w["w6"]["b7_record_is_exact_tree"] else [f"H19 at depth 24: {w['w6']}"]


def w7_recorded(w):
    man = w["edge_manifest"]
    facts = {"ships_code": any(p.endswith(".py") for p in w["package_files"]),
             "states_lower_rule": "exact-inner-hull" in man["lower_rule"],
             "states_pad": "1e-10" in man["lower_rule"], "pad": 1e-10 if "1e-10" in man["lower_rule"] else None}
    w["w7"] = {**facts, "lower_rule": man["lower_rule"], "split_rule": man["split_rule"]}
    return [] if facts == w["recorded_facts"] else [f"recorded: {facts}"]


def p1_package(w):
    out, text, res, ref = [], gate.squash(w["text"]), w["residual"], w["ref"]
    rec = {r["cell"]: r for r in res["first_four_cells"]}
    pairs = {"9.34": rec[0]["B7_unresolved_volume_d16"], "5.17": rec[0]["B7_unresolved_volume_d22"],
             "6.94": ref["r24_cell0_d24_b7_h19.json"][0]["unresolved_volume"], "1.49": rec[3]["deep_unresolved_volume"],
             "9.70": rec[3]["B7_unresolved_volume_d16"], "5.85": rec[3]["B7_unresolved_volume_d22"]}
    for s, v in pairs.items():
        if f"{v:.2e}"[:4] != s:
            out.append(f"the text's {s} against {v}")
    edges = [(e["cell"], e["budget_depth"]) for e in res["strict_edges_added"]]
    if edges != [(0, 30), (1, 32), (2, 32)] or [ref[f"r24_cell{i}_b7_d{d}.json"]["status"] for i, d in edges] != ["COMPLETE"] * 3 \
            or ref["r24_cell3_b7_d30.json"]["status"] != "PARTIAL" or res["untested_deeper_cells"] != list(range(4, 12)):
        out.append("the residual core against the references")
    st = w["admission"]["current_reference_status"]
    if st != {"cell0": "JOINT-REDUNDANT via B7 depth30", "cell1": "JOINT-REDUNDANT via B7 depth32",
              "cell2": "JOINT-REDUNDANT via B7 depth32", "cell3": "JOINT-DEFERRED-CONTRACTING at B7 depth30"}:
        out.append("the admission spec's current status")
    if w["replay"]["nodes"] != w["edge_manifest"]["nodes"] or not w["replay"]["matches_manifest"]:
        out.append("the replay summary against the manifest")
    crystal = gate.squash(w["crystal"])
    for frag in ("-18555nodes", "-9278CERTleaves", "-0pending", "-cell1:B7COMPLETEatdepth32", "-cell2:B7COMPLETEatdepth32", "~1.49e-7",
                 "-cell3DEFERRED-CONTRACTING"):
        if frag not in crystal:
            out.append(f"crystal: {frag}")
    w["p1"] = {"edges": edges}
    return out


def p2_package(w):
    import re
    out, v = [], w["validation"]
    leaves = {row[0]: row for row in w["manifest"]["leaves"]}
    for key, leaf_id, text in (("round24", DOC, w["text"]), ("state_crystal", CRYSTAL, w["crystal"])):
        rec, (_, size, digest) = v[key], leaves[leaf_id]
        n = text.count("$$")
        if rec["sha256"] != digest or rec["bytes"] != size or not rec["utf8"] \
                or rec["double_dollar_count"] != n or rec["double_dollar_even"] != (n % 2 == 0) \
                or rec["contains_unicode_escape_sequence"] != bool(re.search(r"\\u[0-9a-fA-F]{4}", text)) \
                or rec["contains_bracket_math_delimiter"] != bool(re.search(r"\\[\[\]()]", text)):
            out.append(f"VALIDATION {key} against the archive and the document")
    for key, leaf_id in (("residual_core_sha256", RESIDUAL), ("joint_admission_spec_sha256", ADMISSION),
                         ("complete_edge_manifest_sha256", EDGE + "manifest.json"), ("complete_edge_replay_sha256", EDGE + "replay_summary.json")):
        if v[key] != leaves[leaf_id][2]:
            out.append(f"VALIDATION {key} against the archive")
    rec = {r["cell"]: r for r in w["residual"]["first_four_cells"]}
    man = w["edge_manifest"]
    kc = {"cell0_complete": rec[0]["deep_status"] == "COMPLETE", "cell1_complete": rec[1]["deep_status"] == "COMPLETE",
          "cell2_complete": rec[2]["deep_status"] == "COMPLETE",
          "cell3_contracting_partial": rec[3]["deep_status"] == "PARTIAL" and rec[3]["joint_admission"] == "DEFERRED-CONTRACTING",
          "certificate_nodes_match_replay": man["nodes"] == w["replay"]["nodes"], "certificate_pending_zero": man["pending_leaves"] == 0,
          "forest_identity": man["nodes"] == 2 * man["cert_leaves"] - 1, "no_global_claim": man["global_lebesgue_claim"] is False}
    if kc != v["checks"] or v["status"] != "PASS":
        out.append(f"VALIDATION checks {kc}")
    w["p2"] = kc
    return out


R24_DEFINITIONS = {
    "JOINT-CANDIDATE": ("in-line", "sec. 14: single-witness tails plateau, or the main single witnesses have negative evidence - "
                                   "joint cost and synergy may be estimated"),
    "JOINT-DEFERRED-CONTRACTING": ("in-line", "sec. 14: no strict edge yet, but a non-disqualified single-witness tail still "
                                              "contracts strongly with budget"),
    "CONTRACTING": ("in-line", "sec. 9: an unresolved volume still falling fast with depth, with no plateau"),
    "JOINT-NECESSARY-RELATIVE-TO-PORTFOLIO": ("in-line", "sec. 11, 14: at one exact base point every portfolio witness has a "
                                                         "verified counterexample - single-witness reduction is impossible there"),
    "JOINT-REDUNDANT": ("in-line", "sec. 14: a strict single-witness edge already exists for the cell"),
    "REFERENCE-COMPLETE-EDGE-PILOT": ("in-line", "sec. 7: a complete lift tree replayed deterministically in the pilot's floating "
                                                 "arithmetic - not a publication-grade interval certificate"),
}
R24_REVIEWS = [
    ("PASS", "7. First actual complete-edge certificate", "computation", "PASS",
     "the replay stands: this tree's exact-core lift gives the same 18555 nodes, 9278 CERT leaves and the same topology bytes (W3)"),
    ("CLOSED", "FIRST REFERENCE STRICT LIFT EDGE", "computation", "PASS",
     "rebuilt bit for bit, least slack 4.2e-8, no box within 1e-9 of T (W3) - floating arithmetic, as the manifest says"),
    ("CLOSED", "JOINT-WITNESS ADMISSION LOGIC", "analytic", "PASS",
     "joint dominance and the joint lower hull are monotonicity of the hull; pointwise necessity speaks of single values only (W1)"),
    ("COMPLETE", "", "computation", "PASS", "cell 1: the exact tree closes at depth 31 with 22009 nodes, inside the depth-32 budget (W4)"),
    ("COMPLETE", "", "computation", "PASS", "cell 2: the exact tree closes at depth 31 with 26189 nodes (W4)"),
    ("NOT ESTABLISHED", "JOINT NECESSITY ON TESTED CELLS", "computation", "PASS",
     "stronger than stated: let run, the exact tree closes cell 3 too, at depth 34 - no tested cell needs a joint witness (W5)"),
    ("PARTIAL", "9. Cell 3", "computation", "PASS", "at depth 30, 1098 boxes stay open, 1.49e-7 of volume (W4)"),
    ("JOINT-CANDIDATE", "", "n/a", "n/a", "a state of sec. 14, defined there; not a judgment"),
    ("JOINT-CANDIDATE", "20. Plateau gate", "n/a", "n/a", "the state sec. 20's gate leads to; not a judgment"),
    ("JOINT-DEFERRED-CONTRACTING", "", "n/a", "n/a", "a state of sec. 14, defined there; not a judgment"),
    ("JOINT-DEFERRED-CONTRACTING", "19. Residual Collapse Principle", "n/a", "n/a", "the state sec. 19's gate leads to; not a judgment"),
    ("JOINT-DEFERRED-CONTRACTING", "", "computation", "PARTIAL",
     "cell 3's state at depth 30 holds, but its evidence mixes two trees: 5.85e-5 at depth 22 is the 96-direction pilot's, "
     "1.49e-7 at 30 the exact tree's (x393; exact alone x217); and at depth 34 the cell is REDUNDANT (W2, W4)"),
    ("CONTRACTING", "9. Cell 3", "computation", "PARTIAL",
     "it contracts, but the quoted x393 compares the 96-direction pilot with the exact tree; exact alone x217 (W2)"),
    ("JOINT-NECESSARY-RELATIVE-TO-PORTFOLIO", "", "n/a", "n/a", "a state of sec. 14, defined there; its theorem is sec. 11 (W1)"),
    ("JOINT-REDUNDANT", "", "n/a", "n/a", "a state of sec. 14, defined there; not a judgment"),
    ("JOINT-REDUNDANT", "", "computation", "PASS", "cell 0, via the rebuilt certificate (W3)"),
    ("JOINT-REDUNDANT", "", "computation", "PASS", "cell 1, via the exact tree's closure at depth 31 (W4)"),
    ("JOINT-REDUNDANT", "", "computation", "PASS", "cell 2, via the exact tree's closure at depth 31 (W4)"),
    ("STILL COMPUTE-DEFERRED", "a_{Leb}≥0.8350", "n/a", "n/a", "sec. 0's boxed summary; read from census v5 on (RUN-003)"),
    ("REFERENCE-COMPLETE-EDGE-PILOT", "", "computation", "PASS", "sec. 7's certificate, rebuilt bit for bit (W3)"),
]


def r1_registry(w):
    return registry.completeness(w["registry"], "R24", w["definitions"], w["reviews"])


CHECKS = {"A0": gate.anchors_check(ANCHORS), "W1": w1_theorems, "W2": w2_tails, "W3": w3_certificate, "W4": w4_cells,
          "W5": w5_residual, "W6": w6_h19, "W7": w7_recorded, "P1": p1_package, "P2": p2_package, "R1": r1_registry}
DEPENDS = {"A0": {"text"}, "W1": {"admission"},
           "W2": {"computed", "residual", "tail_sources", "cell3_ratio_exact_only"},
           "W3": {"computed", "edge_manifest", "topology", "certificate", "closest_call_above"},
           "W4": {"computed", "ref", "cells"},
           "W5": {"computed", "residual", "residual_at_36"},
           "W6": {"computed", "ref", "h19_d24_gap"},
           "W7": {"edge_manifest", "package_files", "recorded_facts"},
           "P1": {"text", "residual", "ref", "admission", "replay", "edge_manifest", "crystal"},
           "P2": {"validation", "manifest", "text", "crystal", "residual", "edge_manifest", "replay"},
           "R1": {"registry", "definitions", "reviews"}}


def load_inputs():
    blobs = {n: source_bytes(n) for n in SOURCES}
    got = {leaf.id: d for leaf, d in walk_sources(blobs, keep=lambda l: l.id.startswith(PKG)) if d is not None}
    ref = {k[len(REF):]: json.loads(v) for k, v in got.items() if k.startswith(REF) and k.endswith(".json") and "/cell0_B7" not in k[len(REF) - 1:]}
    return {"text": got[DOC].decode("utf-8"), "crystal": got[CRYSTAL].decode("utf-8"), "residual": json.loads(got[RESIDUAL]),
            "admission": json.loads(got[ADMISSION]), "validation": json.loads(got[VALIDATION]),
            "edge_manifest": json.loads(got[EDGE + "manifest.json"]), "replay": json.loads(got[EDGE + "replay_summary.json"]),
            "topology": got[EDGE + "topology.bin"], "ref": {k: v for k, v in ref.items() if "/" not in k},
            "key_bytes": got[RESIDUAL] + got[EDGE + "manifest.json"],
            "manifest": json.loads(MANIFEST.read_text(encoding="utf-8")),
            "package_files": sorted(k[len(PKG):] for k in got)}


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--drill", action="store_true")
    ap.add_argument("--blind", metavar="CID")
    args = ap.parse_args()
    inputs = load_inputs()
    results_computed = computed(inputs["key_bytes"])

    def build():
        return {**{k: v for k, v in inputs.items() if k != "key_bytes"}, "computed": results_computed,
                "registry": registry.load(), "definitions": dict(R24_DEFINITIONS), "reviews": list(R24_REVIEWS),
                **copy.deepcopy(CLAIMS)}

    if args.blind:
        CHECKS[args.blind] = lambda w: []
        args.drill = True
    w = build()
    results = {c: fn(w) for c, fn in CHECKS.items()}
    for c, f in results.items():
        print(f"{c} {'PASS' if not f else 'FAIL'}", *(f"\n     {x}" for x in f[:6]))
    for key in ("w1", "w2", "w3", "w4", "w5", "w6", "w7", "p1", "p2"):
        print(key, json.dumps(w.get(key), default=float, ensure_ascii=False)[:1200])
    if not args.drill:
        return
    res, text, man = inputs["residual"], inputs["text"], inputs["edge_manifest"]
    flipped = bytes([inputs["topology"][0] ^ 0x01]) + inputs["topology"][1:]
    res_d22 = copy.deepcopy(res)
    res_d22["first_four_cells"][2]["B7_unresolved_volume_d22"] += 1e-12
    res_cell3 = copy.deepcopy(res)
    res_cell3["first_four_cells"][3]["joint_admission"] = "REDUNDANT"
    ref_c1 = copy.deepcopy(inputs["ref"])
    ref_c1["r24_cell1_b7_d32.json"]["nodes"] += 2
    ref_h19 = copy.deepcopy(inputs["ref"])
    ref_h19["r24_cell0_d24_b7_h19.json"][1]["pending_leaves"] += 1
    cases = [
        ("K0-unmodified", "control", {}, set()),
        ("K1-doc-crlf", "control", {"text": text.replace("\n", "\r\n")}, set()),
        ("K2-residual-keys-sorted", "control", {"residual": json.loads(json.dumps(res, sort_keys=True))}, set()),
        ("K3-manifest-keys-sorted", "control", {"edge_manifest": json.loads(json.dumps(man, sort_keys=True))}, set()),
        ("M1-doc-ratio", "mutation", {"text": text.replace(r"3.93\times10^{2}", r"3.94\times10^{2}")}, {"A0"}),
        ("M2-admission-warning", "mutation",
         {"admission": {**inputs["admission"], "pointwise_joint_necessity_relative_to_portfolio":
                        {**inputs["admission"]["pointwise_joint_necessity_relative_to_portfolio"], "warning": "The joint family succeeds."}}}, {"W1"}),
        ("M3-tail-source", "mutation", {"tail_sources": {**CLAIMS["tail_sources"], "cell0": {**CLAIMS["tail_sources"]["cell0"], "22": "exact"}}},
         {"W2"}),
        ("M4-recorded-d22", "mutation", {"residual": res_d22}, {"W2"}),
        ("M5-topology-bit", "mutation", {"topology": flipped}, {"W3"}),
        ("M6-slack", "mutation", {"edge_manifest": {**man, "worst_reference_slack": man["worst_reference_slack"] + 1e-12}}, {"W3"}),
        ("M7-cell1-nodes", "mutation", {"ref": ref_c1}, {"W4"}),
        ("M8-cell3-closure-claim", "mutation", {"cells": {**CLAIMS["cells"], "3": {**CLAIMS["cells"]["3"], "closes_at": 33}}}, {"W4"}),
        ("M9-residual-claim", "mutation", {"residual_at_36": [3]}, {"W5"}),
        ("M10-cell3-state", "mutation", {"residual": res_cell3}, {"W5", "P2"}),
        ("M11-h19-pending", "mutation", {"ref": ref_h19}, {"W6"}),
        ("M12-ships-code", "mutation", {"recorded_facts": {**CLAIMS["recorded_facts"], "ships_code": True}}, {"W7"}),
        ("M13-crystal", "mutation", {"crystal": inputs["crystal"].replace("- 9278 CERT leaves", "- 9279 CERT leaves")}, {"P1"}),
        ("M14-validation-digest", "mutation", {"validation": {**inputs["validation"], "residual_core_sha256": "0" * 64}}, {"P2"}),
        ("M15-validation-check", "mutation",
         {"validation": {**inputs["validation"], "checks": {**inputs["validation"]["checks"], "forest_identity": False}}}, {"P2"}),
        ("M16-review-dropped", "mutation", {"reviews": list(R24_REVIEWS[1:])}, {"R1"}),
    ]
    if args.blind:
        cases = [c for c in cases if args.blind in c[3]]
    d = gate.run_drill(build, CHECKS, DEPENDS, cases)
    summary = gate.summarize(d, CHECKS)
    print(gate.report_drill(summary))
    for x in d:
        if not x["ok"]:
            print("   DRILL FAIL", x)
    if args.blind:
        n = sum(1 for x in d if args.blind in x["expected"] and args.blind not in x["tripped"])
        print(f"blind {args.blind}: drill reported it blind in {n} cases (nothing written)")
        return
    log = {"run": "RUN-027", "subject": "Round 24: the portfolio residual core and joint-witness admission",
           "document": DOC, "checks": {c: {"pass": not f, "failures": f} for c, f in results.items()},
           **{k: w.get(k) for k in ("w1", "w2", "w3", "w4", "w5", "w6", "w7", "p1", "p2")}, "drill": summary}
    LOG.write_text(json.dumps(log, ensure_ascii=False, indent=1, default=float) + "\n", encoding="utf-8")
    if not results["R1"]:
        registry.record("R24", "RUN-027", R24_DEFINITIONS, R24_REVIEWS)
        print(f"registry: {len(R24_DEFINITIONS)} definitions, {len(R24_REVIEWS)} reviews recorded for R24")


if __name__ == "__main__":
    main()
