"""RUN-040 - Round 37: the first depth-32 publication wave, and cell 33's frontier carried to depth 42.

Round 37 promotes cells 49, 64 and 66 - their trees rebuilt from durable sources, their lower bounds
migrated, their markers replayed, their certificates written as RHCERT-v0.1 and replayed from the bytes -
to PUBLICATION-CANDIDATE-SHARD, ten of 77; carries cell 33's recovered depth-36 frontier on to depths 38,
40 and 42 in sharded, hashed DFRONT segments merged by composition (the volume falls to 8.7% of its depth-36
value; the cell stays PARTIAL); and prepares the trees of cells 12, 59 and 57. Here the three certificates
are decoded and every vertex checked in this tree's integer arithmetic, each leaf's density and hull count
set against this tree's own certificate; cell 33 is continued here from this tree's own depth-36 frontier,
one depth at a time, every checkpoint and every segment file written here from this tree's boxes and set
against the package's bytes; the three prepared trees lifted, encoded and hashed.

Checks
  A0 anchors      the numbers and formulas below occur in Round 37 as written
  W1 certificates sec. 0-4: cells 49, 64, 66 - their bytes replayed here, against this tree's certificates
  W2 markers      sec. 2-4: markers 49, 64, 66 against Round 29's files and this tree's polygons
  W3 states       sec. 2-4: the dominance audits' witness-core states on this tree's lifts
  W4 chain        sec. 6-8, 10: cell 33 carried here from depth 36 to 42 against the four DFRONT checkpoints
  W5 segments     sec. 6-9: the 166 segments against this tree's continuation, run by run
  W6 trees        sec. 1, 12: the trees of cells 49, 64, 66, 12, 59, 57 lifted, encoded and hashed
  W7 stages       sec. 5, 11, 13: the stage counts; the geometry unchanged
  W8 code         what the package ships, read after the rebuild and never run
  P1 package      the shard files, the certificate and DFRONT records, the dashboard and the crystal agree
  P2 package      VALIDATION against the archive; its checks from the data
  R1 registry     Round 37's judgments reviewed, first-used statuses defined

Usage: python code/run040_round37_depth32.py [--drill] [--blind CID]
"""
import argparse
import collections
import copy
import hashlib
import io
import json
import re
import zipfile
from fractions import Fraction
from multiprocessing import Pool

import gate
import registry
import run008_round05_saturation_gate as r8
import run009_round06_witness_exchange as r9
import run019_round16_global_pilot as r19
import run023_round20_mixed_escalation as r23
import run027_round24_residual_joint as r27
import run029_round26_full_atlas as r29
import run030_round27_interval_replay as r30
import run031_round28_exact_root as r31
import run032_round29_marker_upper as r32
import run033_round30_a1_replay as r33
import run034_round31_mpfr_replay as r34
import run035_round32_bulk_wave as r35
import run036_round33_dominance as r36
import run037_round34_rhcert as r37
import run038_round35_rhcert_production as r38
import run039_round36_dfront as r39
from corpus import SOURCES, TREE, source_bytes, walk_sources
from run010_round07_minimizer_atlas import d_star, t_text

PKG = "r37.zip!/"
DOC = PKG + "AMRAL_Lebesgue_Finite_Closure_Round_37_Depth32_DFRONT_Shards_v0.1.md"
CRYSTAL = PKG + "AMRAL_LUC_FC_State_Crystal_Round_37_v0.1.md"
DASH = PKG + "AMRAL_LUC_FC_Round_37_COVERAGE_DASHBOARD.json"
BATCH = PKG + "AMRAL_LUC_FC_Round_37_DEPTH32_BATCH_STATUS.json"
CHAIN = PKG + "AMRAL_LUC_FC_Round_37_CELL33_DFRONT_CHAIN.json"
MERGE = PKG + "AMRAL_LUC_FC_Round_37_COMPOSITIONAL_DFRONT_MERGE_SPEC.json"
NEXT = PKG + "AMRAL_LUC_FC_Round_37_NEXT_DEPTH32_SHARDS.json"
VALIDATION = PKG + "AMRAL_LUC_FC_Round_37_VALIDATION.json"
CELLS = (49, 64, 66)
SHARD = {n: PKG + f"AMRAL_LUC_FC_Round_37_PUBLICATION_CANDIDATE_SHARD_{n}.json" for n in CELLS}
CERTZIP = {n: f"AMRAL_LUC_FC_Round_37_Cell{n}_RHCert.zip" for n in CELLS}
CERT = {n: PKG + CERTZIP[n] + "!/" for n in CELLS}
CZIP = "AMRAL_LUC_FC_Round_37_Cell33_DFRONT_Chain.zip"
CH = PKG + CZIP + "!/"
DFRONTS = {36: CH + "AMRAL_LUC_FC_Round_36_CELL33_DFRONT.bin", 38: CH + "AMRAL_LUC_FC_Round_37_CELL33_DFRONT_D38.bin",
           40: CH + "AMRAL_LUC_FC_Round_37_CELL33_DFRONT_D40.bin", 42: CH + "AMRAL_LUC_FC_Round_37_CELL33_DFRONT_D42.bin"}
DMANS = {36: CH + "AMRAL_LUC_FC_Round_36_CELL33_DFRONT_MANIFEST.json", 38: CH + "AMRAL_LUC_FC_Round_37_CELL33_DFRONT_D38_MANIFEST.json",
         40: CH + "AMRAL_LUC_FC_Round_37_CELL33_DFRONT_D40_MANIFEST.json", 42: CH + "AMRAL_LUC_FC_Round_37_CELL33_DFRONT_D42_MANIFEST.json"}
LOG = TREE / "data" / "gate-logs" / "run040-round37-depth32.json"
MANIFEST = TREE / "data" / "SOURCE_MANIFEST.v1.json"
R36_DFRONT = r39.D33

T = 0.835
DEPTH = 32
NEXT_CELLS = (12, 59, 57)
CHECKPOINTS = (36, 38, 40, 42)


# ---------------------------------------------------------------- one lift per cell, each leaf certified from its start band

def cert_compute(path):
    """a cell lifted here to depth 32 (RUN-030's lift) and every leaf certified on its intended box from its
    slack's start band and on the ladder (RUN-031's migration): [path, area, result] in lift order"""
    cell = r35.cell_box(path)
    scales = r31.root_scales()
    base = r33.base_cores(cell, scales)
    tn = t_text(7, d_star(r31.TM))
    seq = r31.float_sequence(tn, DEPTH)
    waxes = [a for a, _ in seq]
    with Pool(14) as pool:
        leaves = r30.leaves_with_paths(pool, cell["center"], cell["half"], DEPTH)
        assert max(len(p) for p, _, _, _ in leaves) <= len(seq)
        jobs = []
        for p, _, _, area in leaves:
            wc, wh = r31.float_box(p, seq, tn)
            wlo, whi = r31.fractions(p, waxes, 3)
            jobs.append((cell["center"], cell["half"], wc, wh, r31.directed_box(wlo, whi, r31.WITNESS, scales), base, r35.start_band(area - r30.PKG_PAD - T)))
        results = pool.map(r31.migrate, jobs, chunksize=16)
    return {"leaves": [[p, area] for p, _, _, area in leaves], "results": results}


def cert_computed(n, path):
    return r35._cached(f"run040-cell{n}.json", [cert_compute, r35.cell_box, r35.start_band, r31.migrate, r33.base_cores, r30.leaves_with_paths,
                                               r8, r9, r19, r23, r27, r30, r31, path.encode()], lambda: cert_compute(path))


def chain_compute(path, lift36):
    """cell 33 carried here from this tree's own depth-36 frontier (RUN-039's lift) one depth at a time to 42
    under the package's pad (RUN-030's continuation), the open boxes and the per-depth counts kept at every
    depth"""
    box = r35.cell_box(path)
    lift = {"per": {str(k): v for k, v in lift36["per"].items()}, "frontier": lift36["frontier"]}
    out = {str(CHECKPOINTS[0]): lift}
    with Pool(14) as pool:
        for d in range(CHECKPOINTS[0], CHECKPOINTS[-1]):
            lift = r30.resume(pool, box["center"], box["half"], lift, d, d + 1, r30.PKG_PAD)
            out[str(d + 1)] = lift
    return out


def chain_computed(path, lift36):
    return r35._cached("run040-chain33.json", [chain_compute, r30.resume, r30.split_sequence, r35.cell_box, r8, r9, r19, r23, r27, r30,
                                              json.dumps(lift36["frontier"]).encode(), path.encode()], lambda: chain_compute(path, lift36))


def lifts_computed(paths):
    """the trees of the cells named, lifted here to depth 32 (leaf paths and areas)"""
    def compute():
        with Pool(14) as pool:
            return {n: [[p, area] for p, _, _, area in r30.leaves_with_paths(pool, *(lambda b: (b["center"], b["half"]))(r35.cell_box(path)), DEPTH)]
                    for n, path in sorted(paths.items())}
    return r35._cached("run040-lifts.json", [lifts_computed, r30.leaves_with_paths, r35.cell_box, r8, r9, r19, r23, r27, r30,
                                            json.dumps(sorted(paths.items())).encode()], compute)


def markers_computed(markers):
    return r35._cached("run040-markers.json", [r36.marker_compute, r32.certify, r32.half_planes, r32.outer_area, r32.support_hi,
                                              r34.marker_polygons, r34.full_area, r34.package_normals, r8, r9, r19, r30, r31,
                                              json.dumps({str(n): [m["q_rational"], m["final"]["density"]] for n, m in sorted(markers.items())}).encode()],
                       lambda: {str(n): r36.marker_compute(m) for n, m in sorted(markers.items())})


def load_inputs():
    blobs = {n: source_bytes(n) for n in SOURCES}
    want = {r29.LEDGER} | {r32.REF + f"marker_{c:02d}.json" for c in CELLS}
    got = {leaf.id: d for leaf, d in walk_sources(blobs, keep=lambda l: l.id.startswith(PKG) or l.id in want) if d is not None}
    sha = lambda b: hashlib.sha256(b).hexdigest()
    outer = zipfile.ZipFile(io.BytesIO(source_bytes("r37.zip")))
    certs = {}
    for n in CELLS:
        z = outer.read(CERTZIP[n])
        rec = [k for k in got if k.startswith(CERT[n]) and k.endswith("_RHCERT_REPLAY.json")][0]
        certs[str(n)] = {"rhcert": got[CERT[n] + f"cell{n}.rhcert"], "rhidx": got[CERT[n] + f"cell{n}.rhidx"], "manifest": json.loads(got[CERT[n] + "rhcert_manifest.json"]),
                         "record": json.loads(got[rec]), "zip": [len(z), sha(z)], "digests": [sha(got[CERT[n] + f"cell{n}.rhcert"]), sha(got[CERT[n] + f"cell{n}.rhidx"])]}
    cz = outer.read(CZIP)
    certs["49"]["main_copy"] = sha(zipfile.ZipFile(io.BytesIO(blobs["main.zip"])).read(CERTZIP[49]))
    pys = [(k.split("!/")[-1].split("/")[-1], v) for k, v in got.items() if k.endswith(".py")]
    segs = {}
    for k, v in got.items():
        if k.startswith(CH) and "_segments/" in k:
            name, ext = k[len(CH):].rsplit(".", 1)
            segs.setdefault(name, {})["manifest" if ext == "json" else ext] = json.loads(v) if ext == "json" else v
    return {"text": got[DOC].decode("utf-8"), "crystal": got[CRYSTAL].decode("utf-8"), "dashboard": json.loads(got[DASH]), "batch": json.loads(got[BATCH]),
            "chain": json.loads(got[CHAIN]), "merge": json.loads(got[MERGE]), "next": json.loads(got[NEXT]), "validation": json.loads(got[VALIDATION]),
            "shards": {str(n): json.loads(got[p]) for n, p in SHARD.items()}, "certs": certs,
            "dfronts": {str(d): got[p] for d, p in DFRONTS.items()}, "dmans": {str(d): json.loads(got[p]) for d, p in DMANS.items()},
            "segments": segs, "chain_zip": [len(cz), sha(cz)],
            "merge_copy_equal": got[MERGE] == got[CH + "AMRAL_LUC_FC_Round_37_COMPOSITIONAL_DFRONT_MERGE_SPEC.json"],
            "markers": {n: json.loads(got[r32.REF + f"marker_{n:02d}.json"]) for n in CELLS}, "ledger26": json.loads(got[r29.LEDGER]),
            "code": {k.split("!/")[-1].split("/")[-1]: v.decode("utf-8") for k, v in got.items() if k.endswith(".py")},
            "code_copies": {b: [sum(x == b for x, _ in pys), len({v for x, v in pys if x == b})] for b in sorted({x for x, _ in pys})},
            "code_digests": {k.split("!/")[-1].split("/")[-1]: sha(v) for k, v in got.items() if k.endswith(".py")},
            "manifest": json.loads(MANIFEST.read_text(encoding="utf-8"))}


def replay_compute(cert, cell):
    """RUN-038's replay, its split sequence carried to depth 32 (RUN-038's stops at 30, where its cells close:
    the boxes of deeper leaves would be their depth-30 ancestors')"""
    scales = r31.root_scales()
    tn = t_text(7, d_star(r31.TM))
    waxes = [a for a, _ in r31.float_sequence(tn, DEPTH)]
    _, leaves = r37.decode(cert)
    paths = [p for p, internal in r31.decode_topology(cell["topology"], cell["manifest"]["nodes"]) if not internal]
    assert max(map(len, paths)) <= len(waxes) and len(paths) == len(leaves)
    base = r33.base_cores(r35.cell_box(cell["manifest"]["base_path"]), scales)
    jobs = []
    for (_, _, verts), p in zip(leaves, paths):
        wlo, whi = r31.fractions(p, waxes, 3)
        jobs.append((verts, r31.directed_box(wlo, whi, r31.WITNESS, scales), base))
    with Pool(14) as pool:
        return pool.map(r37.replay, jobs, chunksize=16)


def replay_on(cert, path, lift):
    """the replay above; no tree bits are shipped for these cells, so the leaf order is this tree's own lift
    in the package's preorder (whose digest W6 checks against the shard file's)"""
    top, _ = r39.encode([p for p, _ in lift["leaves"]], DEPTH)
    return replay_compute(cert, {"topology": r39.pack(top), "manifest": {"nodes": len(top), "base_path": path}})


def unique_states(paths):
    """RUN-036's witness-core states, the split sequence carried to depth 32"""
    tn = t_text(7, d_star(r31.TM))
    seq = r31.float_sequence(tn, DEPTH)
    assert max(map(len, paths)) <= len(seq)
    return len({(wc[0], tuple(wh)) for wc, wh in (r31.float_box(p, seq, tn) for p in paths)})


def world(inputs):
    c33 = r39.lift33_computed(inputs["dmans"]["36"]["base_path"])
    paths = {c["cell"]: c["path"] for c in inputs["ledger26"]["cells"]}
    mine = {str(n): cert_computed(n, paths[n]) for n in CELLS}
    return {**inputs, "paths": {str(n): paths[n] for n in CELLS + NEXT_CELLS}, "mine": mine,
            "replay": {str(n): r35._cached(f"run040-replay{n}.json", [r37.decode, r37.uvarint, r37.replay, replay_compute, replay_on, r39.encode, r39.pack,
                                                                      r33.base_cores, r35.cell_box, r8, r9, r19, r30, r31, inputs["certs"][str(n)]["rhcert"],
                                                                      json.dumps(sorted(p for p, _ in mine[str(n)]["leaves"])).encode()],
                                           lambda n=n: replay_on(inputs["certs"][str(n)]["rhcert"], paths[n], mine[str(n)])) for n in CELLS},
            "chain_mine": chain_computed(inputs["dmans"]["36"]["base_path"], c33),
            "next_lifts": lifts_computed({str(n): paths[n] for n in NEXT_CELLS}),
            "marker_mine": markers_computed(inputs["markers"])}


ANCHORS = {
    "W1": [r"9986/9986\text{PASS}.", r"-256:811-512:3139-1024:4127-2048:1592-4096:259-8192:47-16384:11", r"3.15312672\times10^{-8}>0.", r"2{,}441{,}597\text{vertices}",
           r"10539/10539\text{PASS}.", r"1.50693499\times10^{-8}>0.", r"2{,}624{,}186.", r"10636/10636\text{PASS}.", r"6.86089458\times10^{-9}>0.", r"2{,}646{,}674."],
    "W2": [r"0.8349813779061498<0.835.", r"1.86221\times10^{-5}.", r"0.8349716675759491.", r"0.8348951300993619.", r"1.048699\times10^{-4}."],
    "W3": [r"1659\text{uniquestates},\quad0\text{failures}.", r"1702\text{states},\quad0\text{failures}.", r"1710\text{states},\quad0\text{failures}."],
    "W4": [r"2.9748\times10^{-8}\to1.4338\times10^{-8}\to5.4258\times10^{-9}\to2.6015\times10^{-9}.", r"V_{42}/V_{36}\approx0.08745.", r"14027\to27042\to40934\to78507,",
           r"\boxed{8.745\%}.", r"27042\text{pending}", r"V_{38}=1.4337598723\times10^{-8}.", r"V_{38}/V_{36}=0.481963.", r"40934\text{pending}",
           r"V_{40}=5.4257753323\times10^{-9}.", r"V_{40}/V_{38}=0.378430.", r"78507\text{pending}", r"V_{42}=2.6015130699\times10^{-9}.", r"V_{42}/V_{40}=0.479473.",
           r"V_{42}/V_{36}=0.087451."],
    "W5": [r"14027\text{pending}.$$分成：$$29$$immutablerefinementsegments。", r"d38frontier進一步分：$$55$$segments。", r"d40frontier：$$82$$segments。",
           r"i\nej\Rightarrow\operatorname{int}(Q_i)\cap\operatorname{int}(Q_j)=\varnothing."],
    "W6": [r"19971\text{nodes},\qquad9986\text{leaves}.", r"21077\text{nodes},\qquad10539\text{leaves}.", r"21271\text{nodes},\qquad10636\text{leaves}.",
           r"\text{node-countmatch}=PASS.", r"21789\text{nodes},\qquad10895\text{leaves}.", r"22009\text{nodes},\qquad11005\text{leaves}.",
           r"22261\text{nodes},\qquad11131\text{leaves}."],
    "W7": [r"\{47,48,49,64,66,68,69,70,75,76\}", r"10/77\approx12.99\%.", r"10/66\approx15.15\%.", r"\boxed{56.}", r"3/12\text{publication-candidate}."],
    "P1": [r"a_{\mathrm{Leb}}\ge0.835\text{NOTCERTIFIED}."],
}

_CKPT = {"boxes_equal": True, "bytes_equal": True, "volume": [True, True, True], "digest": True, "bytes": True, "parent": [True, True]}
CLAIMS = {
    "certs_claim": {
        "49": {"header": ["L34RHC01", 18, 9986], "consumed": True, "index": [True, True, True], "mine": [9986, 9986, 2441597], "replay": [9986, 9986, 0],
               "least": [3257, "01000100100100101111101", 3.153126719767511e-08], "is_record": [True, True], "to_mine": 2.3e-16,
               "density_counts": {"256": 811, "512": 3139, "1024": 4127, "2048": 1592, "4096": 259, "8192": 47, "16384": 11}, "manifest": [True, True, True]},
        "64": {"header": ["L34RHC01", 18, 10539], "consumed": True, "index": [True, True, True], "mine": [10539, 10539, 2624186], "replay": [10539, 10539, 0],
               "least": [3073, "00111011001001010111010000111", 1.506934987941895e-08], "is_record": [True, True], "to_mine": 1.9e-16,
               "density_counts": {"256": 835, "512": 3390, "1024": 4383, "2048": 1516, "4096": 340, "8192": 61, "16384": 14}, "manifest": [True, True, True]},
        "66": {"header": ["L34RHC01", 18, 10636], "consumed": True, "index": [True, True, True], "mine": [10636, 10636, 2646674], "replay": [10636, 10636, 0],
               "least": [9173, "1101011011010010110101010110", 6.860894579634212e-09], "is_record": [True, True], "to_mine": 1.6e-16,
               "density_counts": {"256": 828, "512": 3448, "1024": 4406, "2048": 1550, "4096": 325, "8192": 66, "16384": 13}, "manifest": [True, True, True]}},
    "markers_claim": {
        "49": {"record": [True], "every_constraint": [0.8349813779061498, 5156, 79813, 79814], "full_is_file": True, "is_round29": True,
               "mine": [256, 2.4825925315990006e-05, 4.181835201322315e-05]},
        "64": {"record": [True], "every_constraint": [0.8349716675759491, 2597, 42915, 42915], "full_is_file": True, "is_round29": True,
               "mine": [128, 4.629620107005173e-06, 6.552849864416288e-05]},
        "66": {"record": [True], "every_constraint": [0.8348951300993619, 1312, 22918, 22918], "full_is_file": True, "is_round29": True,
               "mine": [128, 0.00010979458897914895, 0.00017084801269917627]}},
    "states_claim": {"49": [1659, 1659, "PASS", 0], "64": [1702, 1702, "PASS", 0], "66": [1710, 1710, "PASS", 0]},
    "chain_claim": {
        "36": {**_CKPT, "pending": [14027, 14027, 14027, 14027]},
        "38": {**_CKPT, "pending": [27042, 27042, 27042, 27042], "counts": [63980, 18975], "manifest": [True, True], "ratio": [True, True],
               "least_new_slack": [2.604509941e-10, 2e-16]},
        "40": {**_CKPT, "pending": [40934, 40934, 40934, 40934], "counts": [118666, 45441], "manifest": [True, True], "ratio": [True, True],
               "least_new_slack": [2.743697491e-10, 7e-16]},
        "42": {**_CKPT, "pending": [78507, 78507, 78507, 78507], "counts": [205928, 65391], "manifest": [True, True], "ratio": [True, True],
               "least_new_slack": [1.131750249e-11, 1e-15]},
        "ratio_42_36": True, "open_at_42": 78507},
    "segments_claim": {
        "38": {"segments": 29, "parents": [14027, 14027], "sizes": [500, 27], "merged": True, "in_slice": [27042, 27042],
               "each": {"bytes_equal": 29, "counts": 29, "depth": 29, "empty": 3, "pending": 29, "tiled": 29, "volume": 29}},
        "40": {"segments": 55, "parents": [27042, 27042], "sizes": [500, 42], "merged": True, "in_slice": [40934, 40934],
               "each": {"bytes_equal": 55, "counts": 55, "depth": 55, "empty": 9, "pending": 55, "tiled": 55, "volume": 55}},
        "42": {"segments": 82, "parents": [40934, 40934], "sizes": [500, 434], "merged": True, "in_slice": [78507, 78507],
               "each": {"bytes_equal": 82, "counts": 82, "depth": 82, "empty": 8, "pending": 82, "tiled": 82, "volume": 82}},
        "requirements_example": {"requirements": True, "union_disjoint": False, "contained": False}},
    "trees_claim": {
        "49": {"counts": [19971, 9986, 19971, 9986, 19971], "digests": [True, True], "slack": 3e-16},
        "64": {"counts": [21077, 10539, 21077, 10539, 21077], "digests": [True, True], "slack": 0.0},
        "66": {"counts": [21271, 10636, 21271, 10636, 21271], "digests": [True, True], "slack": 1e-15},
        "12": {"counts": [21789, 10895, 21789, 10895, 21789], "digests": [True, True], "slack": 3e-16},
        "59": {"counts": [22009, 11005, 22009, 11005, 22009], "digests": [True, True], "slack": 1e-15},
        "57": {"counts": [22261, 11131, 22261, 11131, 22261], "digests": [True, True], "slack": 3e-16}},
    "stages_claim": {"cells": [47, 48, 49, 64, 66, 68, 69, 70, 75, 76], "counts": [True, True, True, True],
                     "wave": [[49, 64, 66], [12, 57, 59], [45, 46, 58, 60, 65, 67]], "batch": [[49, 64, 66], [12, 57, 59], 12],
                     "geometry": [66, 11, 0, "PARTIAL", [36, 38, 40, 42]]},
    "code_facts": {
        "lines": {"r37_enclosure_dominance.py": 108, "r37_marker_a1_verify.py": 92, "r37_merge_dfront_hashed_segments.py": 36, "r37_mpfr_marker.py": 104,
                  "r37_mpmath_emitter_chunk.py": 121, "r37_refine_dfront_from_checkpoint.py": 37, "r37_refine_dfront_segment.py": 41, "r37_rhcert_assemble.py": 27,
                  "r37_rhcert_emit_chunk.py": 146, "r37_rhcert_verify_chunk.py": 101},
        "copies": {"r37_merge_dfront_hashed_segments.py": [2, 1]},
        "rhcert": {"start_bands": True, "ladders": ["256,512,1024,2048,4096,8192,16384,32768", "256,512,1024,2048,4096,8192,16384,32768,65536"],
                   "byte_emitter_starts_at_ledger": True, "index_counts_offsets": True, "verifier": [["40"], True, True], "nearest_decimal": True},
        "dominance_state": True,
        "markers": {"full_hulls": True, "grids": ["26", "9", ("26", "9")], "sectors_met": True, "round29_density": True},
        "refine": {"from_codes": True, "lower": True, "segment_disjoint": True, "slice_text": True},
        "merge": {"checks": [True, True, True, True, True], "reads_slice_hash": False, "parent_boxes_read": 1, "bundle": True, "volume_from_manifests": True,
                  "writes_fields_of": [False, False, True]},
        "absent": ["cell49_mpmath_leaf_ledger.json", "cell64_mpmath_leaf_ledger.json", "cell66_mpmath_leaf_ledger.json", "leaves_reference.json", "cell49_topology.bin",
                   "cell64_topology.bin", "cell66_topology.bin", "mpfr_marker.json", "Round_37_MARKER_A1_AUDIT.json", "CROSS_BACKEND_DOMINANCE_CELL49.json",
                   "CROSS_BACKEND_DOMINANCE_CELL64.json", "CROSS_BACKEND_DOMINANCE_CELL66.json"]},
}


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


def sig(x, n=4):
    return float(f"{x:.{n - 1}e}")


def sha(b):
    return hashlib.sha256(b).hexdigest()


def preorder(lift):
    """a lift's leaf paths in the package's order (the topology's preorder, lower child first)"""
    leaves = set(p for p, _ in lift["leaves"])
    out, stack = [], [""]
    while stack:
        p = stack.pop()
        if p in leaves:
            out.append(p)
        else:
            stack.extend((p + "1", p + "0"))
    return out


def coded(frontier, depth):
    """RUN-039's dyadic codes of open boxes, each with its path as an integer (a leading 1, 0 the lower child),
    in the codes' canonical order"""
    seq = r30.split_sequence(depth)
    out = []
    for x in frontier:
        p, k, m = int(x, 16), [0, 0, 0], [0, 0, 0]
        for i, bit in enumerate(bin(p)[3:]):
            a = seq[i][0]
            k[a] += 1
            m[a] = 2 * m[a] + int(bit)
        out.append(((k[0], m[0], k[1], m[1], k[2], m[2]), p))
    return sorted(out)


def w1_certs(w):
    """sec. 0-4: cells 49, 64, 66 - each certificate decoded and every vertex checked here, each leaf's density
    and hull count against this tree's own certificate from the leaf's start band; the least margin and each
    chunk's against the replay record, its path the manifest's; the density counts the manifest's"""
    out, got = [], {}
    for n in map(str, CELLS):
        c, res, mine = w["certs"][n], w["replay"][n], w["mine"][n]
        head, leaves = r37.decode(c["rhcert"])
        ih, offs = r37.decode_index(c["rhidx"])
        paths = preorder(mine)
        at = r38.mine_at_bands(mine, paths)
        rec = c["record"]
        k = min(range(len(res)), key=lambda i: res[i]["margin"])
        got[n] = {"header": [head["magic"], head["exponent"], head["count"]], "consumed": head["end"] == len(c["rhcert"]),
                  "index": [ih["count"] == head["count"] + 1, ih["size_matches"], offs == [s for s, _, _ in leaves] + [head["end"]]],
                  "mine": [sum(d == m[0] for (_, d, _), m in zip(leaves, at)), sum(len(v) == m[1] for (_, _, v), m in zip(leaves, at)), sum(len(v) for _, _, v in leaves)],
                  "replay": [sum(r["pass"] for r in res), sum(r["convex"] for r in res), sum(r["refused"] for r in res)],
                  "least": [k, paths[k], res[k]["margin"]],
                  "is_record": [res[k]["margin"] == rec["minimum_margin_decimal"], all(min(r["margin"] for r in res[ch["start"]:ch["end"]]) == ch["minimum_margin_decimal"]
                                                                                  for ch in rec["chunks"])],
                  "to_mine": sig(max(abs(r["margin"] - m[2]) for r, m in zip(res, at)), 2),
                  "density_counts": {str(a): b for a, b in sorted(collections.Counter(d for _, d, _ in leaves).items())}}
        got[n]["manifest"] = [got[n]["density_counts"] == c["manifest"]["density_counts"], paths[k] == c["manifest"]["minimum_margin_path"],
                              got[n]["mine"][2] == c["manifest"]["vertex_count_total"]]
    if any(not v["consumed"] or not all(v["index"]) or v["mine"][0] != v["header"][2] or v["mine"][1] != v["header"][2] or v["replay"][0] != v["header"][2]
           or v["replay"][2] or not all(v["is_record"]) or not all(v["manifest"]) for v in got.values()) or got != w["certs_claim"]:
        out.append(f"the certificates: {got}")
    w["w1"] = got
    return out


def w2_markers(w):
    """sec. 2-4: markers 49, 64, 66 - the shard files' figures against Round 29's files; the full polygons under
    the package's normals and ceiling, and the float-reduced ones Round 29's; this tree's certificates"""
    out, got = [], {}
    for n in map(str, CELLS):
        f, mu, mine = w["markers"][int(n)]["final"], w["shards"][n]["marker_upper"], w["marker_mine"][n]
        poly = mine["polygons"]
        got[n] = {"record": [mu["independent_A1_margin_decimal"] == mu["MPFR_margin_decimal"] == f["margin_decimal"]], "every_constraint": poly["full"],
                  "full_is_file": poly["full"][0] == f["outer_area_decimal"],
                  "is_round29": poly["reduced"] == [f["outer_area_decimal"], f["active_constraints"], f["outer_area_num_bits"], f["outer_area_den_bits"]],
                  "mine": [mine["certificate"]["ladder"]["N"], mine["certificate"]["ladder"]["margin"], T - mine["certificate"]["exact"]]}
    if any(not all(v["record"]) or not v["full_is_file"] or not v["is_round29"] for v in got.values()) or got != w["markers_claim"]:
        out.append(f"the markers: {got}")
    w["w2"] = got
    return out


def w3_states(w):
    """sec. 2-4: the dominance audits' unique witness-core states on this tree's lifts; the audits' MPFR results
    recorded, not recomputed"""
    got = {n: [unique_states([p for p, _ in w["mine"][n]["leaves"]]), w["shards"][n]["cross_backend_dominance"]["unique_witness_core_states"],
               w["shards"][n]["cross_backend_dominance"]["status"], w["shards"][n]["cross_backend_dominance"]["failures"]] for n in map(str, CELLS)}
    w["w3"] = got
    return [] if all(v[0] == v[1] for v in got.values()) and got == w["states_claim"] else [f"the audits' states: {got}"]


def w4_chain(w):
    """sec. 6-8, 10: cell 33 carried here from this tree's own depth-36 frontier to 42 - at every checkpoint the
    same boxes as the DFRONT file, one for one, and the file itself written here from the spec byte for byte,
    its header naming the parent checkpoint's digest; the pending count, volume and ratio of the chain record
    and of the manifest; the nodes evaluated and newly certified since the parent checkpoint and the least
    new slack, from this tree's per-depth counts"""
    out, got, chain = [], {}, {c["depth"]: c for c in w["chain"]["checkpoints"]}
    mine = w["chain_mine"]
    for d in CHECKPOINTS:
        head, boxes = r39.decode_dfront(w["dfronts"][str(d)])
        codes = r39.codes(mine[str(d)]["frontier"], d)
        vol = len(codes) * r30.ROOT7 / 2 ** d
        rec, man = chain[d], w["dmans"][str(d)]
        parent = sha(w["dfronts"][str(d - 2)]) if d > CHECKPOINTS[0] else "0" * 64
        row = {"boxes_equal": codes == boxes, "bytes_equal": r39.encode_dfront(codes, 33, 40, man["dependency_hash_sha256"], parent) == w["dfronts"][str(d)],
               "pending": [len(codes), head["count"], rec["pending_count"], man["pending_count"]],
               "volume": [near(vol, rec["unresolved_volume"], 1e-22), near(vol, man["unresolved_volume"], 1e-22), str(Fraction(len(codes), 2 ** d)) == man["relative_volume_fraction"]],
               "digest": sha(w["dfronts"][str(d)]) == rec["frontier_sha256"] == man["frontier_sha256"],
               "bytes": len(w["dfronts"][str(d)]) == rec["frontier_bytes"] == man["frontier_bytes"],
               "parent": [head["parent"] == parent, man.get("parent_frontier_sha256", "0" * 64) == parent]}
        if d > CHECKPOINTS[0]:
            per, n0 = mine[str(d)]["per"], len(mine[str(d - 2)]["frontier"])
            ev, ce = per[str(d)][0] - per[str(d - 2)][0], per[str(d)][1] - per[str(d - 2)][1]
            slack = min(per[str(x)][3] for x in (d - 1, d) if per[str(x)][3] is not None)
            ratio = len(codes) / (4 * n0)
            row.update({"counts": [ev, ce], "manifest": [ev == man["evaluated_descendant_nodes"], ce == man["new_certified_nodes"]],
                        "ratio": [near(ratio, rec["ratio_vs_parent"], 1e-15), near(ratio, man["contraction_ratio_vs_d36" if d == 38 else "contraction_ratio_vs_parent"], 1e-15)],
                        "least_new_slack": [sig(slack, 10), sig(abs(slack - man["worst_new_cert_slack"]), 1)]})
        got[str(d)] = row
    got["ratio_42_36"] = near(len(mine["42"]["frontier"]) / (64 * len(mine["36"]["frontier"])), w["chain"]["volume_ratio_d42_vs_d36"], 1e-15)
    got["open_at_42"] = len(mine["42"]["frontier"])
    ok = all(v["boxes_equal"] and v["bytes_equal"] and len(set(v["pending"])) == 1 and all(v["volume"]) and v["digest"] and v["bytes"] and all(v["parent"])
             and all(v.get("manifest", [True])) and all(v.get("ratio", [True])) and v.get("least_new_slack", [0, 0])[1] <= 1e-14
             for k, v in got.items() if k.isdigit()) and got["ratio_42_36"] and got["open_at_42"] > 0
    if not ok or got != w["chain_claim"]:
        out.append(f"cell 33's chain: {got}")
    w["w4"] = got
    return out


def ancestor(c, k):
    """the dyadic box with depths k = (k_phi, k_x, k_y) holding box c, or None if c is not that deep"""
    if any(c[2 * a] < k[a] for a in range(3)):
        return None
    return tuple(x for a in range(3) for x in (k[a], c[2 * a + 1] >> (c[2 * a] - k[a])))


def contained(parents, kids):
    """how many of the kids lie in one of the parents, all parents sharing one split pattern (every box at a depth
    of one lift does)"""
    ks = {p[0::2] for p in parents}
    if len(ks) != 1:
        return None
    k, pset = ks.pop(), set(parents)
    return sum(ancestor(c, k) in pset for c in kids)


def merge_requirements(parent, segments):
    """the merge spec's requirements as listed, on parent boxes and (start, end, boxes) segments; the hash, status
    and dependency items hold by construction in an example, so what can fail is the cover, the encoding and the
    segment-level disjointness"""
    cover = [a for a, _, _ in segments] == [0] + [b for _, b, _ in segments[:-1]] and segments[-1][1] == len(parent)
    canonical = all(q == sorted(set(q)) and all(0 <= c[2 * a + 1] < 2 ** c[2 * a] for c in q for a in range(3)) for _, _, q in segments)
    return cover and canonical and all(len(q) < 2 or r39.disjoint(q) for _, _, q in segments)


def requirements_example():
    """sec. 9: the parent frontier the two halves of the phi range; each segment one box, the second's inside the
    FIRST parent - the listed requirements hold, the union overlaps, and the theorem's containment premise fails"""
    parent = [(1, 0, 0, 0, 0, 0), (1, 1, 0, 0, 0, 0)]
    segs = [(0, 1, [(2, 0, 0, 0, 0, 0)]), (1, 2, [(1, 0, 1, 0, 0, 0)])]
    return {"requirements": merge_requirements(parent, segs), "union_disjoint": r39.disjoint([q for _, _, s in segs for q in s]),
            "contained": all(contained(parent[a:b], q) == len(q) for a, b, q in segs)}


def w5_segments(w):
    """sec. 6-9: every segment of the three continuations against this tree's own - its parent slice a run of
    the parent checkpoint's boxes in canonical order, the runs tiling the parent; its file written here, byte for
    byte, from this tree's open descendants of that run at the child depth; its evaluated and newly certified
    node counts from this tree's counts under that run; its relative volume - and the segments' boxes together
    the child checkpoint's. The theorem's premise on the package's own files: every segment box inside a box of
    its slice; and the merge spec's requirements, as listed, shown not to imply it"""
    out, got, mine = [], {}, w["chain_mine"]
    for d in CHECKPOINTS[:-1]:
        rows = sorted((s for s in w["segments"].values() if s["manifest"]["parent_checkpoint_depth"] == d), key=lambda s: s["manifest"]["parent_start"])
        parents = coded(mine[str(d)]["frontier"], d)
        index = {p: i for i, (_, p) in enumerate(parents)}
        u = collections.Counter(index[int(x, 16) >> 1] for x in mine[str(d + 1)]["frontier"])
        kids = collections.defaultdict(list)
        for code, p in coded(mine[str(d + 2)]["frontier"], d + 2):
            kids[index[p >> 2]].append(code)
        dep, parent = w["dmans"][str(d)]["dependency_hash_sha256"], sha(w["dfronts"][str(d)])
        shipped = r39.decode_dfront(w["dfronts"][str(d)])[1]
        end, tally, merged, inside = 0, collections.Counter(), [], [0, 0]
        for s in rows:
            m = s["manifest"]
            a, b = m["parent_start"], m["parent_end"]
            q = sorted(c for i in range(a, b) for c in kids.get(i, ()))
            uu = sum(u.get(i, 0) for i in range(a, b))
            tally["tiled"] += a == end
            tally["depth"] += m["child_checkpoint_depth"] == d + 2
            tally["bytes_equal"] += r39.encode_dfront(q, 33, 40, dep, parent) == s["dfront"]
            tally["pending"] += m["descendant_pending_count"] == len(q)
            tally["counts"] += (m["evaluated_descendant_nodes"], m["new_certified_nodes"]) == (2 * (b - a) + 2 * uu, 2 * (b - a) + uu - len(q))
            tally["volume"] += m["descendant_relative_volume_fraction"] == str(Fraction(len(q), 2 ** (d + 2)))
            tally["empty"] += not q
            boxes = r39.decode_dfront(s["dfront"])[1]
            merged += boxes
            inside[0] += contained(shipped[a:b], boxes) or 0
            inside[1] += len(boxes)
            end = b
        sizes = [s["manifest"]["parent_count"] for s in rows]
        got[str(d + 2)] = {"segments": len(rows), "parents": [len(parents), end], "sizes": [max(sizes), min(sizes)],
                           "each": dict(sorted(tally.items())), "merged": sorted(merged) == r39.decode_dfront(w["dfronts"][str(d + 2)])[1], "in_slice": inside}
    got["requirements_example"] = requirements_example()
    ok = all(v["parents"][0] == v["parents"][1] and v["merged"] and v["in_slice"][0] == v["in_slice"][1]
             and all(n == v["segments"] for k, n in v["each"].items() if k != "empty") for k, v in got.items() if k.isdigit()) \
        and got["requirements_example"] == {"requirements": True, "union_disjoint": False, "contained": False}
    if not ok or got != w["segments_claim"]:
        out.append(f"the segments: {got}")
    w["w5"] = got
    return out


def w6_trees(w):
    """sec. 1, 12: the trees of cells 49, 64, 66 (from their certificates' lifts) and 12, 59, 57 lifted here,
    encoded in the package's convention and hashed against the shard and preparation files - node and leaf
    counts, topology and axis digests, least slack"""
    out, got = [], {}
    nxt = {c["cell"]: c for c in w["next"]["cells"]}
    L = w["ledger26"]["cells"]
    for n in CELLS + NEXT_CELLS:
        lift = w["mine"][str(n)]["leaves"] if n in CELLS else w["next_lifts"][str(n)]
        rec = w["shards"][str(n)]["geometry"] if n in CELLS else nxt[n]
        top, axes = r39.encode([p for p, _ in lift], DEPTH)
        slack = min(a - r30.PKG_PAD - T for _, a in lift)
        got[str(n)] = {"counts": [len(top), len(top) - sum(top), rec["nodes"], rec["leaves"], L[n]["nodes_at_depth36"]],
                       "digests": [hashlib.sha256(r39.pack(top)).hexdigest() == rec["topology_sha256"], hashlib.sha256(r39.pack(axes)).hexdigest() == rec["axis_sha256"]],
                       "slack": sig(abs(slack - rec["worst_reference_slack"]), 1)}
    if any(not all(v["digests"]) or not (v["counts"][0] == v["counts"][2] == v["counts"][4] and v["counts"][1] == v["counts"][3]) or v["slack"] > 1e-14
           for v in got.values()) or got != w["trees_claim"]:
        out.append(f"the trees: {got}")
    w["w6"] = got
    return out


def w7_stages(w):
    """sec. 5, 11, 13: the stage counts - ten candidates, 56 geometry-complete cells below them; the depth-32
    wave; the geometry unchanged, cell 33 still open"""
    out, d, b = [], w["dashboard"], w["batch"]
    ar, wave = d["arithmetic_pipeline"], d["depth32_wave"]
    pc = sorted(ar["publication_candidate_cells"])
    cells = b["cells"]
    got = {"cells": pc, "counts": [ar["publication_candidate_count"] == len(pc), near(ar["publication_candidate_fraction_global"], len(pc) / 77, 1e-15),
                                   near(ar["publication_candidate_fraction_of_known_geometry_complete"], len(pc) / 66, 1e-15),
                                   ar["geometry_complete_below_publication_candidate_count"] == d["geometry"]["known_complete"] - len(pc)],
           "wave": [sorted(wave["publication_candidate_cells"]), sorted(wave["durably_shardized_next_cells"]), sorted(wave["remaining_depth32_unshardized_cells"])],
           "batch": [sorted(int(k) for k, v in cells.items() if v["round37_status"] == "PUBLICATION-CANDIDATE-SHARD"),
                     sorted(int(k) for k, v in cells.items() if v["round37_status"] == "DURABLY-SHARDIZED-ARITHMETIC-PENDING"), len(cells)],
           "geometry": [d["geometry"]["known_complete"], d["geometry"]["residual"], d["geometry"]["round37_new_geometry_closures"], d["geometry"]["cell33_depth42"]["status"],
                        d["geometry"]["durable_cell33_checkpoints"]]}
    if not all(got["counts"]) or got != w["stages_claim"]:
        out.append(f"the stages: {got}")
    w["w7"] = got
    return out


def w8_code(w):
    """what the package ships, read after the rebuild and never run"""
    facts = {"lines": {k: v.count("\n") for k, v in sorted(w["code"].items())}, "copies": {k: v for k, v in w["code_copies"].items() if v[0] > 1}}
    facts.update(code_probes(w["code"], w["dmans"], {row[0] for row in w["manifest"]["leaves"]}))
    w["w8"] = facts
    return [] if facts == w["code_facts"] else [f"the scripts: {facts}"]


ABSENT = ("cell49_mpmath_leaf_ledger.json", "cell64_mpmath_leaf_ledger.json", "cell66_mpmath_leaf_ledger.json", "leaves_reference.json", "cell49_topology.bin",
          "cell64_topology.bin", "cell66_topology.bin", "mpfr_marker.json", "Round_37_MARKER_A1_AUDIT.json", "CROSS_BACKEND_DOMINANCE_CELL49.json",
          "CROSS_BACKEND_DOMINANCE_CELL64.json", "CROSS_BACKEND_DOMINANCE_CELL66.json", "wave_scheduler.py", "r25_incremental_wave.py", "r31_mpfr_backend.py",
          "Round_28_ROOT_RECONSTRUCTION.json", "Round_31_MPFR_ROOT_REPLAY.json")


def code_probes(code, dmans, archive):
    """read, never run.
    RHCERT: the mpmath emitter starts each leaf at the band of its reference slack - RUN-035's start_band, band for
    band - and the byte emitter at the mpmath ledger's density; the assembler's index counts L + 1 offsets; the
    verifier quantizes the cores outward to a 10^40 grid, demands strict convexity, and first rounds each rational
    endpoint to a 140-digit decimal to nearest. The dominance audit keys a state by both backends' rotation and
    half-widths. The two marker replays both prune the full cyclic hull of every normal at Round 29's density, on
    the 10^26 and 10^9 grids, and take the largest candidate over the sectors the angle's interval meets (a valid
    upper bound, looser at a sector boundary).
    DFRONT: the refinements take a box's floats straight from its code, bound as the Round 36 rebuild does, check
    each segment disjoint and hash its parent slice as text; the merge checks status, parent digest, contiguous
    slices, segment digests and counts, bundles the digests - and reads neither the slice hashes nor the parent
    boxes (only their number), so it never tests the theorem's containment premise; it writes exactly the depth-42
    manifest's fields"""
    sq = {k: v.replace(" ", "") for k, v in code.items()}
    emit, asm, ver, mpe, dom, seg, ref, mrg, mpfr, a1 = (sq[f"r37_{k}.py"] for k in ("rhcert_emit_chunk", "rhcert_assemble", "rhcert_verify_chunk",
                                                                                   "mpmath_emitter_chunk", "enclosure_dominance", "refine_dfront_segment",
                                                                                   "refine_dfront_from_checkpoint", "merge_dfront_hashed_segments", "mpfr_marker",
                                                                                   "marker_a1_verify"))
    bands = [(float(t), int(n)) for t, n in re.findall(r"ifsl>=([0-9.e-]+):return(\d+)", mpe)]
    keys = re.findall(r"'([a-z0-9_]+)':", mrg.split("man={")[1].split("}\n")[0])
    lower = "v=max(v,ex.hull_area_exact([r16.D,e3,e5,e7])-1e-10)"
    return {
        "rhcert": {"start_bands": len(bands) == 6 and all(r35.start_band(t) == n and r35.start_band(t * (1 - 1e-9)) != n for t, n in bands)
                   and "return16384" in mpe and r35.start_band(0.0) == 16384,
                   "ladders": re.findall(r"DENS=\[([0-9,]+)\]", mpe) + re.findall(r"DENS=\[([0-9,]+)\]", emit),
                   "byte_emitter_starts_at_ledger": "hint=int(ledger[i]['density'])" in emit,
                   "index_counts_offsets": "struct.pack('<8sHI',b'L34RHIDX',1,len(offsets))" in asm and "offsets.append(out.tell())" in asm,
                   "verifier": [re.findall(r"QC=10\*\*(\d+)", ver), "return[(fl(xl),ce(xh),fl(yl),ce(yh))forxl,xh,yl,yhinrect],fl(rho)" in ver,
                                "orient(pts[j-1],pts[j],pts[(j+1)%nv])<=0" in ver],
                   "nearest_decimal": "getcontext().prec=140" in ver and "deffs(fr):returnstr(Decimal(fr.numerator)/Decimal(fr.denominator))" in ver
                   and "defivE(e):returnmp.iv.mpf([fs(e.lo),fs(e.hi)])" in ver},
        "dominance_state": "key=(ca[0].lo,ca[0].hi,ha[0].lo,ha[0].hi,ha[1].lo,ha[1].hi,ha[2].lo,ha[2].hi,cb[0].lo,cb[0].hi,hb[0].lo,hb[0].hi,hb[1].lo,hb[1].hi,"
                           "hb[2].lo,hb[2].hi)" in dom,
        "markers": {"full_hulls": "act=cyclic_prune(range(len(ns)),ns,H)" in mpfr and "act=full_cyclic_hull(ns,H)" in a1,
                    "grids": re.findall(r"SUPPORT_DEN=10\*\*(\d+)", mpfr) + re.findall(r"NORMAL_SCALE=10\*\*(\d+)", mpfr) + re.findall(r";DEN=10\*\*(\d+);SCALE=10\*\*(\d+)", a1),
                    "sectors_met": "s0=math.floor(qq.lo);s1=math.floor(qq.hi)" in mpfr and "forsinrange(math.floor(qlo),math.floor(qhi)+1):" in a1,
                    "round29_density": "N=int(marker['final']['density'])" in mpfr and "N=d['final']['density']" in a1},
        "refine": {"from_codes": all("w=(hi-lo)/(2**k);a=lo+m*w;b=a+w;cs.append((a+b)/2);hs.append(w/2)" in x for x in (seg, ref)),
                   "lower": all(lower in x and "exceptException:pass" in x for x in (seg, ref)),
                   "segment_disjoint": all("new=sorted(new);df.verify_disjoint(new)" in x for x in (seg, ref)),
                   "slice_text": all(r"ps='\n'.join(f'{b.kp},{b.mp},{b.kx},{b.mx},{b.ky},{b.my}'forbinboxes).encode()" in x for x in (seg, ref))},
        "merge": {"checks": ["assertm['status']=='PASS'andm['parent_frontier_sha256']==pshaandm['parent_start']==cur" in mrg,
                             "asserthashlib.sha256(raw).hexdigest()==m['segment_sha256']" in mrg, "assertlen(dec['boxes'])==m['descendant_pending_count']" in mrg,
                             "assertcur==len(parent['boxes'])" in mrg, "assertlen(boxes)==len(set(boxes))" in mrg],
                  "reads_slice_hash": "parent_slice_sha256" in mrg, "parent_boxes_read": mrg.count("parent['boxes']"),
                  "bundle": r"bundle_hash=hashlib.sha256('\n'.join(seg_hashes).encode()).hexdigest()" in mrg,
                  "volume_from_manifests": "rel+=Fraction(m['descendant_relative_volume_fraction'])" in mrg,
                  "writes_fields_of": [keys == list(dmans[d]) for d in ("38", "40", "42")]},
        "absent": [name for name in ABSENT if not any(a.endswith(name) for a in archive)],
    }


def p1_package(w):
    out = []
    for n in map(str, CELLS):
        c, sh = w["certs"][n], w["shards"][n]
        m, rec, ce = c["manifest"], c["record"], sh["certificate_entity"]
        if (m["certificate_sha256"], m["index_sha256"], m["certificate_bytes"], m["index_bytes"]) != (sha(c["rhcert"]), sha(c["rhidx"]), len(c["rhcert"]), len(c["rhidx"])) \
                or (rec["certificate_sha256"], rec["leaf_count"], rec["vertex_count_total"], rec["minimum_margin_decimal"], rec["failures"], rec["status"]) \
                != (m["certificate_sha256"], m["leaf_count"], m["vertex_count_total"], m["minimum_margin_decimal"], 0, "PASS") \
                or [ch["start"] for ch in rec["chunks"]] != [0] + [ch["end"] for ch in rec["chunks"][:-1]] or rec["chunks"][-1]["end"] != m["leaf_count"] \
                or any((ch["leaf_count"], ch["failures"], ch["status"]) != (ch["end"] - ch["start"], 0, "PASS") for ch in rec["chunks"]) \
                or (ce["raw_sha256"], ce["raw_bytes"], ce["index_sha256"], ce["leaf_count"], ce["vertex_count_total"], ce["minimum_margin_decimal"], ce["compressed_bytes"], ce["compressed_sha256"]) \
                != (m["certificate_sha256"], m["certificate_bytes"], m["index_sha256"], m["leaf_count"], m["vertex_count_total"], m["minimum_margin_decimal"], c["zip"][0], c["zip"][1]) \
                or (ce["replay_status"], ce["replay_failures"], ce["compressed_package"]) != ("PASS", 0, CERTZIP[int(n)]) \
                or (sh["status"], sh["publication_candidate"], sh["global_lebesgue_claim"], sh["cell"], m["cell"]) != ("PUBLICATION-CANDIDATE-SHARD", True, False, int(n), int(n)) \
                or sh["geometry"]["base_path"] != w["paths"][n]:
            out.append(f"cell {n}: the manifest, the replay record and the shard file")
    dep, hashed = w["dmans"]["36"]["dependency_hash_sha256"], {}
    for d in CHECKPOINTS[1:]:
        segs = sorted((s for s in w["segments"].values() if s["manifest"]["parent_checkpoint_depth"] == d - 2), key=lambda s: s["manifest"]["parent_start"])
        rows = [s["manifest"] for s in segs]
        man, parent = w["dmans"][str(d)], r39.decode_dfront(w["dfronts"][str(d - 2)])[1]
        slices = sum(sha("\n".join(",".join(map(str, b)) for b in parent[r["parent_start"]:r["parent_end"]]).encode()) == r["parent_slice_sha256"] for r in rows)
        bundle = sha("\n".join(r["segment_sha256"] for r in rows).encode())
        hashed[str(d)] = [slices, len(rows), bundle == man.get("segment_hash_bundle_sha256", bundle)]
        if any((sha(s["dfront"]), s["manifest"]["parent_frontier_sha256"], s["manifest"]["dependency_hash_sha256"], s["manifest"]["status"],
                s["manifest"]["parent_count"], s["manifest"]["cell"]) != (s["manifest"]["segment_sha256"], sha(w["dfronts"][str(d - 2)]), dep, "PASS",
                                                                          s["manifest"]["parent_end"] - s["manifest"]["parent_start"], 33) for s in segs) \
                or len(rows) != man["segment_count"] or man["dependency_hash_sha256"] != dep or man["parent_checkpoint_depth"] != d - 2 \
                or (sum(r["descendant_pending_count"] for r in rows), sum(r["evaluated_descendant_nodes"] for r in rows), sum(r["new_certified_nodes"] for r in rows)) \
                != (man["pending_count"], man["evaluated_descendant_nodes"], man["new_certified_nodes"]) \
                or min(r["worst_new_cert_slack"] for r in rows) != man["worst_new_cert_slack"] or man.get("parent_slice_cover_complete", True) is not True \
                or slices != len(rows) or not hashed[str(d)][2]:
            out.append(f"depth {d}: the segment records against their files, the parent slices and the checkpoint manifest")
    ch, mg = w["chain"], w["merge"]
    if [c["depth"] for c in ch["checkpoints"]] != list(CHECKPOINTS) or ch["d42_status"] != "PARTIAL" or not w["merge_copy_equal"] \
            or (mg["applications"]["d38_to_d40"]["pending_count"], mg["applications"]["d38_to_d40"]["frontier_sha256"]) != (w["dmans"]["40"]["pending_count"], w["dmans"]["40"]["frontier_sha256"]) \
            or (mg["applications"]["d40_to_d42"]["pending_count"], mg["applications"]["d40_to_d42"]["frontier_sha256"], mg["applications"]["d40_to_d42"]["segment_count"]) \
            != (w["dmans"]["42"]["pending_count"], w["dmans"]["42"]["frontier_sha256"], w["dmans"]["42"]["segment_count"]) \
            or mg["applications"]["d40_to_d42"]["segment_hash_bundle_sha256"] != w["dmans"]["42"]["segment_hash_bundle_sha256"] \
            or [w["dmans"][str(d)]["segment_count"] for d in (38, 40, 42)] != [29, 55, 82]:
        out.append("the chain record, the merge spec and the checkpoint manifests")
    b, q = w["batch"]["cells"], {c["cell"]: c for c in w["next"]["cells"]}
    L = w["ledger26"]["cells"]
    if any((v["path"], v["nodes_at_depth32"], v["marker_margin"], v["closure_depth"], v["cell"])
           != (L[int(k)]["path"], L[int(k)]["nodes_at_depth36"], L[int(k)]["marker_margin"], L[int(k)]["known_closure_depth"], int(k)) for k, v in b.items()) \
            or any(v["closure_depth"] != DEPTH for v in b.values()) or any(q[n]["nodes"] != L[n]["nodes_at_depth36"] or not q[n]["node_count_match"] for n in NEXT_CELLS):
        out.append("the depth-32 batch and the next shards against the Round 26 ledger")
    crystal = gate.squash(w["crystal"])
    for frag in (r"-d36:14,027pending;$V=2.9748\times10^{-8}$", r"-d38:27,042;$V=1.4338\times10^{-8}$", r"-d40:40,934;$V=5.4258\times10^{-9}$",
                 r"-d42:78,507;$V=2.6015\times10^{-9}$", r"V_{42}/V_{36}\approx0.08745.", r"\boxed{10/77.}", "-9986leaves", "-MPFRdominance1659states/0failures",
                 "-10539leaves", "-dominance1702/0", "-10636leaves", "-dominance1710/0", "12/59/57havedurabletopologywithexactledgernode-countmatches."):
        if frag not in crystal:
            out.append(f"crystal: {frag}")
    r36 = {row[0]: row[2] for row in w["manifest"]["leaves"]}[R36_DFRONT]
    if r36 != sha(w["dfronts"]["36"]):
        out.append("the chain's depth-36 checkpoint is not Round 36's file")
    if w["certs"]["49"]["main_copy"] != w["certs"]["49"]["zip"][1]:
        out.append("the main archive's copy of cell 49's certificate package")
    w["p1"] = {"segments": len(w["segments"]), "slice_hashes": hashed, "depth36_is_round36": r36 == sha(w["dfronts"]["36"]),
               "cell49_copy_in_main": w["certs"]["49"]["main_copy"] == w["certs"]["49"]["zip"][1]}
    return out


def p2_package(w):
    out, v = [], w["validation"]
    leaves = {row[0]: row for row in w["manifest"]["leaves"]}
    for key, leaf_id, text in (("round37", 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["file"] != leaf_id.split("/")[-1] \
                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 (("coverage_dashboard_sha256", DASH), ("depth32_batch_sha256", BATCH), ("dfront_chain_sha256", CHAIN), ("dfront_merge_spec_sha256", MERGE),
                         ("candidate49_sha256", SHARD[49]), ("candidate64_sha256", SHARD[64]), ("candidate66_sha256", SHARD[66]), ("next_depth32_shards_sha256", NEXT)):
        if v[key] != leaves[leaf_id][2]:
            out.append(f"VALIDATION {key} against the archive")
    if [v["rhc49_zip_sha256"], v["rhc64_zip_sha256"], v["rhc66_zip_sha256"], v["dfront_chain_zip_sha256"]] \
            != [w["certs"]["49"]["zip"][1], w["certs"]["64"]["zip"][1], w["certs"]["66"]["zip"][1], w["chain_zip"][1]]:
        out.append("VALIDATION's package digests")
    ch = {c["depth"]: c for c in w["chain"]["checkpoints"]}
    kc = {"pub_count_10": w["dashboard"]["arithmetic_pipeline"]["publication_candidate_count"] == 10, "geometry_complete_66": w["dashboard"]["geometry"]["known_complete"] == 66,
          "no_global_claim": w["dashboard"]["global_lebesgue_claim"] is False,
          **{f"candidate{n}": w["shards"][str(n)]["publication_candidate"] is True for n in CELLS},
          **{f"rhc{n}_all": w["certs"][str(n)]["record"]["status"] == "PASS" and w["certs"][str(n)]["record"]["failures"] == 0 for n in CELLS},
          "marker_a1_all": all(w["shards"][str(n)]["marker_upper"]["independent_A1_pass"] for n in CELLS),
          "d38_contract": ch[38]["unresolved_volume"] < ch[36]["unresolved_volume"], "d40_contract": ch[40]["unresolved_volume"] < ch[38]["unresolved_volume"],
          "d42_contract": ch[42]["unresolved_volume"] < ch[40]["unresolved_volume"], "d42_segment_hashes": w["dmans"]["42"]["segment_content_hashes_verified"] is True,
          **{f"next{n}_match": {c["cell"]: c for c in w["next"]["cells"]}[n]["node_count_match"] is True for n in NEXT_CELLS}}
    if kc != v["checks"] or v["status"] != "PASS":
        out.append(f"VALIDATION checks {kc}")
    w["p2"] = kc
    return out


_BYTES = ("the bytes replayed here in this tree's integer arithmetic - every vertex in its tagged core, convex, the exact area - and every "
          "leaf's density and hull count this tree's own certificate's from its start band (W1)")
_LOWER = "every leaf certified here from its start band on this tree's lift, whose tree is the shard file's by digest (W1, W6)"
R37_REVIEWS = [
    ("PASS", "node-count match", "computation", "PASS", "the trees lifted here have the files' node and leaf counts and their topology and axis digests (W6)"),
    ("PASS", "9986/9986", "computation", "PASS", _LOWER),
    ("PASS", "9986/9986", "computation", "PASS", _BYTES),
    ("PASS", "10539/10539", "computation", "PASS", _LOWER),
    ("PASS", "10539/10539", "computation", "PASS", _BYTES),
    ("PASS", "10636/10636", "computation", "PASS", _LOWER),
    ("PASS", "10636/10636", "computation", "PASS", _BYTES),
    ("PASS", "MPFR marker replay", "computation", "UNMEASURED", "no MPFR record is shipped and MPFR is not on this machine; the full polygons are rebuilt here (W2)"),
    ("PARTIAL", "11. Geometry status remains conservative", "computation", "PASS",
     "true: carried here from the same depth-36 frontier, cell 33 still has 78,507 boxes open at 42, the DFRONT files' boxes one for one (W4)"),
    ("PUBLICATION-CANDIDATE-SHARD", "49,64,66 →", "computation", "PASS",
     "the bytes pass under this tree's arithmetic and are this tree's own certificates leaf for leaf (W1); the markers hold (W2); the MPFR "
     "dominance is not recomputed here"),
    ("NOT CERTIFIED", "a_{Leb}≥0.835", "reading", "PASS", "true"),
    ("NOT CERTIFIED", "a_{Leb}≥0.835", "reading", "PASS", "true"),
]


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


CHECKS = {"A0": gate.anchors_check(ANCHORS), "W1": w1_certs, "W2": w2_markers, "W3": w3_states, "W4": w4_chain, "W5": w5_segments, "W6": w6_trees, "W7": w7_stages,
          "W8": w8_code, "P1": p1_package, "P2": p2_package, "R1": r1_registry}
DEPENDS = {"A0": {"text"}, "W1": {"certs", "replay", "mine", "certs_claim"}, "W2": {"markers", "shards", "marker_mine", "markers_claim"},
           "W3": {"mine", "shards", "states_claim"}, "W4": {"chain", "chain_mine", "dfronts", "dmans", "chain_claim"},
           "W5": {"segments", "chain_mine", "dfronts", "dmans", "segments_claim"},
           "W6": {"mine", "next_lifts", "shards", "next", "ledger26", "trees_claim"}, "W7": {"dashboard", "batch", "stages_claim"}, "W8": {"code", "code_copies", "dmans", "manifest", "code_facts"},
           "P1": {"certs", "shards", "paths", "chain", "merge", "merge_copy_equal", "dmans", "segments", "dfronts", "batch", "next", "ledger26", "crystal", "manifest"},
           "P2": {"validation", "manifest", "text", "crystal", "certs", "chain_zip", "chain", "dmans", "dashboard", "shards", "next"}, "R1": {"registry", "definitions", "reviews"}}


def drill_cases(base):
    text, certs = base["text"], base["certs"]

    def edit(obj, fn):
        x = copy.deepcopy(obj)
        fn(x)
        return x

    def flip_last(b):
        return b[:-1] + bytes([b[-1] ^ 1])

    nonempty = sorted(k for k, v in base["segments"].items() if v["manifest"]["descendant_pending_count"] > 0)
    seg = [nonempty[0], nonempty[-1]]
    return [
        ("K0-unmodified", "control", {}, set()),
        ("K1-doc-crlf", "control", {"text": text.replace("\n", "\r\n")}, set()),
        ("K2-chain-keys-sorted", "control", {"chain": json.loads(json.dumps(base["chain"], sort_keys=True))}, set()),
        ("K3-batch-keys-sorted", "control", {"batch": json.loads(json.dumps(base["batch"], sort_keys=True))}, set()),
        ("M1-doc-pending", "mutation", {"text": text.replace(r"78507\text{ pending}", r"78508\text{ pending}")}, {"A0"}),
        ("M2-replay-refused", "mutation", {"replay": edit(base["replay"], lambda x: x["64"][3].update(refused=1, **{"pass": False}))}, {"W1"}),
        ("M3-index-offset", "mutation", {"certs": edit(certs, lambda x: x["66"].update(rhidx=x["66"]["rhidx"][:14 + 8 * 7] + bytes([x["66"]["rhidx"][14 + 8 * 7] ^ 1])
                                                                                     + x["66"]["rhidx"][15 + 8 * 7:]))}, {"W1", "P1"}),
        ("M4-marker-margin", "mutation", {"shards": edit(base["shards"], lambda x: x["49"]["marker_upper"].update(MPFR_margin_decimal=1.8622e-05))}, {"W2"}),
        ("M5-audit-states", "mutation", {"shards": edit(base["shards"], lambda x: x["66"]["cross_backend_dominance"].update(unique_witness_core_states=1711))}, {"W3"}),
        ("M6-dfront-box", "mutation", {"dfronts": edit(base["dfronts"], lambda x: x.update({"40": flip_last(x["40"])}))}, {"W4", "W5", "P1"}),
        ("M7-chain-volume", "mutation", {"chain": edit(base["chain"], lambda x: x["checkpoints"][2].update(unresolved_volume=5.4258e-09))}, {"W4"}),
        ("M8-segment-volume", "mutation", {"segments": edit(base["segments"], lambda x: x[seg[0]]["manifest"].update(descendant_relative_volume_fraction="1/4"))}, {"W5"}),
        ("M9-segment-box", "mutation", {"segments": edit(base["segments"], lambda x: x[seg[1]].update(dfront=flip_last(x[seg[1]]["dfront"])))}, {"W5", "P1"}),
        ("M10-next-digest", "mutation", {"next": edit(base["next"], lambda x: x["cells"][1].update(axis_sha256="0" * 64))}, {"W6"}),
        ("M11-dashboard", "mutation", {"dashboard": edit(base["dashboard"], lambda x: x["arithmetic_pipeline"].update(geometry_complete_below_publication_candidate_count=57))},
         {"W7"}),
        ("M12-code-claim", "mutation", {"code_facts": {}}, {"W8"}),
        ("M13-merge-count", "mutation", {"merge": edit(base["merge"], lambda x: x["applications"]["d40_to_d42"].update(segment_count=81))}, {"P1"}),
        ("M14-crystal", "mutation", {"crystal": base["crystal"].replace("10539 leaves", "10538 leaves")}, {"P1"}),
        ("M15-validation-digest", "mutation", {"validation": {**base["validation"], "dfront_chain_sha256": "0" * 64}}, {"P2"}),
        ("M16-validation-check", "mutation", {"validation": {**base["validation"], "checks": {**base["validation"]["checks"], "d40_contract": False}}}, {"P2"}),
        ("M17-review-dropped", "mutation", {"reviews": list(R37_REVIEWS[1:])}, {"R1"}),
    ]


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--drill", action="store_true")
    ap.add_argument("--blind", metavar="CID")
    args = ap.parse_args()
    base = world(load_inputs())

    def build():
        return {**base, "registry": registry.load(), "definitions": {}, "reviews": list(R37_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", "w8", "p1", "p2"):
        print(key, "=", json.dumps(w.get(key), default=float, ensure_ascii=False))
    if not args.drill:
        return
    cases = drill_cases(base)
    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-040", "subject": "Round 37: the first depth-32 publication wave, and cell 33's frontier carried to depth 42",
           "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", "w8", "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("R37", "RUN-040", {}, R37_REVIEWS)
        print(f"registry: {len(R37_REVIEWS)} reviews recorded for R37")


if __name__ == "__main__":
    main()

