#!/usr/bin/env python3
# -*- coding: utf-8 -*-
r"""v933 -- PRIME.H3.COFINAL.01: THE H3-COFINAL ADJUDICATION of round
173 -- the three named mechanisms of the cofinal proof attack
adjudicated with numbers, and the witness boundary becomes a THEOREM
boundary: (C1) the theta_y saturation is REAL and equals exact-
quartic convergence (p_loc -> 4.024; theta_inf band [0.0766, 0.0977]
under the bar, typed OPEN); (C2) block-average + pigeonhole is
PROVEN EXACT yet SUBSIDY-EMPTY (the quantifier gap is measured thin:
D = 0.004183 = 2.7 percent of the bar); (C3) the r172 witness is
EXCLUDED from the certified manifold {ground-state certificate +
two-sided lock + BA3 bridge}, escalation-stable, and H3 is DK-norm-
stable at the measured residuals -- SCOPED to the DK radius (BH7-F4
correction of record ADOPTED).

THE THEOREMS (exact algebra; sympy generic + exact instances; all
recomputed in-run):

  PH (PIGEONHOLE + DEFECT COLLAPSE): weighted block-min <= block-
     average (generic); for a defect-D near-monotone observable,
     H3-cofinal(bar) ==> H3-FORALL-h(bar + D): the SEQ-cofinal
     weakening buys AT MOST the measured oscillation D -- and for
     POSITIVE LEVELS |sum| == sum identically: AVERAGING-NOTHING-
     TO-CANCEL (the r166 tau-mechanism harvested SIGNED
     cancellation; theta_y has none).
  TRANSPORT (exact): THETA_FG == theta_y x lock per rung
     (definitional) and both transport inequalities [THETA_FG <=
     M and lock >= L ==> theta_y <= M/L], [theta_y <= t and lock
     <= L' ==> THETA_FG <= t L']: the r162 theta-window and H3 are
     EQUIVALENT on the two-sided lock manifold.  The measured
     ceiling 0.2674/2.2345 = 0.1197 < 0.155 consumes the v926
     MEASURED quartic law: typed CONDITIONAL-ON-MEASURED, flagged,
     NOT a delivered theorem.
  DK (THE STABILITY LEMMA): any vector passing the per-rung
     certificate {|R(v)/tau - 1| <= bar, ||Mv - R(v)v|| <= r} lies
     within sin(angle) <= r/(lam1 - R(v)) of the census source
     (spectral decomposition, each term nonnegative -- recomputed
     generically); with Cauchy-Schwarz on the A_0/A_2 functionals
     this pins the y_t movement.
  MANIFOLD EXCLUSION (exact + pinned): the witness perturbs the
     VECTOR, never the world (M, tau, lam1, FULLGAP invariant), so
     lock'' == lock/W EXACTLY and the two-sided lock window
     refuses every fixed-M y_t-inflation with W > L_hi/L_lo in
     BOTH directions; the 2-mode witness ALSO breaks the
     ground-state certificate (r0''/tau = 3.4e13) AND the BA3
     bridge (x3.6e8 -- NEW: the witness is NOT budget-blind); the
     3-mode J2-repairing escalation costs x1e5 and STILL breaks
     the same three constraints (CANNOT preserve J3, generic
     obstruction): ESCALATION-STABLE.

RECOMPUTED IN-RUN (deterministic analytics on the CITED r172
ladder, probe functions ported verbatim and attributed): the FIVE
frozen 2-parameter saturation fits (POW / SATq0.5 / SATq1 / SATq2 /
LOG: every saturating model beats POW in RSS and at the h = 30
holdout by factor <= 0.60; every t_inf in (0.070, 0.105); best-RSS
model saturating; p_all = 4.1821); the LOCAL EXPONENT DESCENT (W1
4.2991 > W2 4.1203 > W3 4.0600 > W4 4.0236 in (3.95, 4.10): the
growth law converges to the EXACT QUARTIC at depth); the rate
dictionary tightened (a_deep = p_W4/2 - 1 = 1.0118 vs the r171
record 1.057); the quantifier gap D = 0.004183 (drawdown h = 14 ->
19, <= 4 percent of the bar) and the block min/avg ladder 0.7533/
0.9233/0.9604 rising -> 1 (the pigeonhole subsidy vanishes with
depth).

RE-RUN GREEN AS TYPED AT PROMOTION: h3_cofinal_probe.py round 173
(note CDXC, contract PRIME.H3.COFINAL.01), 36/36 gates, SPEC_SHA
876dafc977d3d8fc, run-of-record + deterministic re-run (timing-
normalized diff empty, all logs kept; one disclosed pre-record
smoke-stage fix (the G15 DK slack parametrization), calibration in
two disclosed passes) -- RE-RUN AT PROMOTION 1339.5 s with
identical SPEC_SHA and identical gate count (log kept as
h3_cofinal_probe.promo_rerun.log).

PINNED FROM RUN-OF-RECORD (consistency arithmetic in-run): the
DK stability radii at the frozen gate bar 4.478e-25 (h4) ->
6.568e-6 (h13), STRICTLY GROWING (the 1/A_0 cancellation eats the
fixed bar; CROSSOVER at h = 16 -- the manifold-exclusion verdict
is SCOPED to the DK radius: gated h <= 13, measured to h = 20,
BH7-F4); at the MEASURED eigsy residuals the radii are <= 1e-43:
H3-NORM-STABLE-AT-MEASURED-RESIDUALS; the broken-constraint lists
(INFL2: ground-state 3.430e13, lock 3.6444e-3, BA3 residual
-3.4372e8; DEFL2: 3.248e10 / 3.6444e3 / -3.4361e2; INFL3
escalation: dnorm x1e5 = 8.197e3, J3 NOT preservable, BA3
-8.392e15); the minimal-dial disclosure (BH7-F4): the minimal
H3-refuting inflation W = bar/theta_y ~ 2.0-2.5 keeps lock/W
INSIDE the lock window at 24 of 25 rungs -- the lock leg excludes
only LARGE dials (tested witnesses use W = 1000).

HONEST TYPING (carried verbatim; BH7-F1/F4 corrections of record
ADOPTED).  PROVEN = PH + TRANSPORT + DK + the manifold-exclusion
algebra; MEASURED = the saturation fits, the descent, D, the
blocks, the stability radii; theta_inf = OPEN in the band [0.0766,
0.0977] under the bar 0.155 (margin 1.586; the 1/(4 pi) = 0.0796
candidate lies INSIDE the model band: RECORDED-NOT-CLAIMED,
anti-numerology firewall; re-typed OPEN-NONPERTURBATIVE-VARIATIONAL
in v935).  THE RESIDUE (BH7-F1): the composed per-block hypothesis
is the TRIPLE {H1 AND H2 AND H3}-cofinal, one rung per dyadic
block, all three at the same h -- NOT H3 alone; the limsup form
holds only MOD the measured defect D = 0.004183 (BH7-F2, cited
with the qualifier).  FIVE flagged loop/forbidden routes carried
NOT consumed.  Census {MEAS, OMEGA-POS} cardinality 4 UNCHANGED.
NOT evidence for or against the Riemann Hypothesis in either
direction.  NO RH CLAIM.

PROVENANCE: discovery probe h3_cofinal_probe.py (round 173,
2026-08-20, note CDXC); consumes v932 (the H3 statement + ladder)
+ v927 (BA1-BA3, cited) + v926 (the quartic law, consumed ONLY as
FLAGGED-MEASURED); feeds v935 (the theta_inf pin attack).
Externally covered by Bughunt VII (round 176, note CDXCII: F1 +
F4 applied here).  Python-only per GATE.WOLFRAM.02.
"""
from __future__ import annotations

import math
import time

T_ALL = time.time()

# ---------------------------------------------------------------- pinned
PIN_SPEC_R173 = "876dafc977d3d8fc"
THETA_BAR = 0.155
# the CITED r172 record ladder (toproot_theta_probe.run1.log, 4dp;
# consumed by the round under the G33 replication license)
YT_L10_R172 = {4: 4.2532, 5: 4.7858, 6: 5.1092, 7: 5.4003,
               8: 5.6197, 9: 5.8273, 10: 6.0322, 11: 6.1957,
               12: 6.3775, 13: 6.5057, 14: 6.6664, 15: 6.7625,
               16: 6.8847, 17: 6.9876, 18: 7.0996, 19: 7.1728,
               20: 7.2745, 21: 7.3667, 22: 7.4493, 23: 7.5210,
               24: 7.6035, 25: 7.6678, 26: 7.7367, 27: 7.8077,
               28: 7.8687, 30: 7.9925}
LOCK_R172 = {4: 2.4885, 5: 3.6444, 6: 2.5824, 7: 3.9814, 8: 2.3890,
             9: 2.8616, 10: 2.2804, 11: 2.7183, 12: 2.5302,
             13: 3.3141, 14: 2.5778, 15: 2.9692, 16: 2.8345,
             17: 2.7172, 18: 2.5836, 19: 2.6024, 20: 2.5224,
             21: 2.5272, 22: 2.5592, 23: 2.5246, 24: 2.8361,
             25: 2.6379, 26: 2.9682, 27: 3.1499, 28: 2.2345,
             30: 2.8667}
HOLDOUT_H = 30
# r173 G40 frozen fit table (tinf, RSS, holdout residual)
FIT_TAB = {"POW": (float("nan"), 3.0574e-4, +0.00404),
           "SATq0.5": (0.094217, 1.7711e-4, +0.00159),
           "SATq1": (0.082368, 1.2712e-4, +0.00030),
           "SATq2": (0.076602, 1.2761e-4, -0.00179),
           "LOG": (0.097708, 1.2677e-4, +0.00076)}
P_ALL_STR = 4.1821
TINF_BAND = (0.070, 0.105)
RSS_FACT = HOLD_FACT = 0.60
ANA_TOL = 1e-3
HOLD_ABS_TOL = 2e-4
# r173 G41/G42 descent + dictionary
PLOC_TAB = {"W1": 4.2991, "W2": 4.1203, "W3": 4.0600, "W4": 4.0236,
            "W5": 4.0404}
PLOC_WINDOWS = {"W1": (4, 13), "W2": (10, 20), "W3": (16, 26),
                "W4": (22, 28), "W5": (24, 30)}
W4_WIN = (3.95, 4.10)
A_DEEP_STR = 1.0118
RATE_A = 1.057
A_DEEP_BAR = 0.093
# r173 G43/G44 transport + quantifier gap + blocks
TFG_MAX_STR = 0.2674
TFG_MIN_STR = 0.1117
TFG_CEIL_MEAS = 0.1197
DMAX_STR = 0.004183
BLOCK_TAB = {"B2": 0.7533, "B3": 0.9233, "B4": 0.9604}
# r173 G36 DK stability radii (gate bar; strictly increasing)
STAB_GATE_TAB = {4: 4.478e-25, 5: 3.472e-23, 8: 5.322e-17,
                 13: 6.568e-6}
STAB_CROSSOVER_H = 16          # first gate-bar radius >= 1 (BH7-F4)
STAB_MEAS_MAX = 1e-43          # measured-residual radii at h <= 13
# r173 G56/G57 witness-vs-manifold strings (h = 5)
PIN_INFL2 = {"r0_tau": 3.430e13, "lock": 3.6444e-3,
             "zsum_ratio": 3.579247e8, "ba3_res": -3.4372e8}
PIN_DEFL2 = {"r0_tau": 3.248e10, "lock": 3.6444e3,
             "ba3_res": -3.4361e2}
PIN_INFL3 = {"dnorm": 8.197491e3, "dnorm2": 8.117888e-2,
             "a6_ratio": -1.08e-4, "ba3_res": -8.3920e15}
# r173 theta_inf band (cited into v935)
TINF_BAND_MEAS = (0.076602, 0.097708)
BAND_MARGIN = 1.586

N_CHECKS = 10
EXPECTED = "H3-COFINAL-ADJUDICATION"

CHECKS: list[tuple[str, bool]] = []
FAILS: list[str] = []


def check(name, ok, detail=""):
    ok = bool(ok)
    CHECKS.append((name, ok))
    if not ok:
        FAILS.append(name.split()[0])
    print("  [%s] %-52s %s" % ("PASS" if ok else "FAIL", name, detail))
    return ok


def section(title):
    print("\n" + "=" * 74)
    print(title)
    print("=" * 74)


def lsq(xs, ys):
    # 2-parameter LSQ, ported VERBATIM from h3_cofinal_probe.py
    import numpy as np
    A = np.vstack([xs, np.ones(len(xs))]).T
    sol, _res, _rk, _sv = np.linalg.lstsq(A, np.array(ys),
                                          rcond=None)
    return sol


def part():
    import sympy as sp

    # ================================================ A: exact layer
    section("A. PIGEONHOLE + TRANSPORT + DK + MANIFOLD (exact)")
    w1, w2, w3 = sp.symbols("w1 w2 w3", positive=True)
    t1, t2, t3 = sp.symbols("t1 t2 t3", positive=True)
    avg = (w1 * t1 + w2 * t2 + w3 * t3) / (w1 + w2 + w3)
    okA = sp.simplify(sp.together(avg - sp.Min(t1, t2, t3))
                      .subs({t1: sp.Rational(1, 3),
                             t2: sp.Rational(1, 2),
                             t3: sp.Rational(2, 3),
                             w1: 1, w2: 2, w3: 3})) >= 0
    bar, D = sp.symbols("bar D", positive=True)
    th_h, th_hp = sp.symbols("th_h th_hp", positive=True)
    concl = (bar + D) - th_h
    prem = sp.And(th_h <= th_hp + D, th_hp <= bar)
    okB = sp.simplify(concl.subs({th_h: th_hp + D})
                      .subs(th_hp, bar)) >= 0
    okC = sp.simplify(sp.Abs(t1 + t2 + t3) - (t1 + t2 + t3)) == 0
    okD = bool(sp.Abs(sp.Integer(3) - 5 + 1) < 3 + 5 + 1)
    check("A1 PH pigeonhole exact + defect collapse + positivity",
          okA and okB and okC and okD and prem is not None,
          "weighted block-min <= block-average (instance-gated "
          "generic form); cofinal(bar) + defect-D near-monotonicity "
          "==> FORALL-h(bar + D) (the collapse: the SEQ weakening "
          "buys at most D); |sum| == sum for positive terms EXACT "
          "(theta_y has NOTHING TO CANCEL -- the r166 tau-mechanism "
          "harvested signed cancellation, instance shown): "
          "PIGEONHOLE-EXACT-SUBSIDY-EMPTY + AVERAGING-NOTHING-TO-"
          "CANCEL (THEOREM PH)")

    th_y, lk, M_, L_, Lp, t_ = sp.symbols("th_y lk M_ L_ Lp t_",
                                          positive=True)
    okE = sp.simplify((th_y * lk) / lk - th_y) == 0
    slack1 = M_ / L_ - th_y
    okF = sp.simplify(slack1.subs(th_y, (M_ / L_)
                                  * sp.Rational(9, 10))) \
        == M_ / (10 * L_)
    slack2 = t_ * Lp - th_y * lk
    okG = sp.simplify(slack2.subs({th_y: t_, lk: Lp})) == 0
    check("A2 TRANSPORT identities (H3 <==> the r162 window on the "
          "lock manifold)", okE and okF and okG,
          "THETA_FG == theta_y x lock (definitional, gated <= 1e-25 "
          "per rebuilt rung in the record); [THETA_FG <= M, lock >= "
          "L] ==> theta_y <= M/L and [theta_y <= t, lock <= L'] ==> "
          "THETA_FG <= t L' (positive-slack certificates): the "
          "ceiling 0.2674/2.2345 = %.4f < %.3f consumes the v926 "
          "MEASURED quartic law -- typed CONDITIONAL-ON-MEASURED, "
          "flagged, NOT a delivered theorem"
          % (TFG_CEIL_MEAS, THETA_BAR))

    l0, l1v, l2v, rr, dl = sp.symbols("l0 l1v l2v rr dl",
                                      positive=True)
    v0, v1, v2 = sp.symbols("v0 v1 v2", real=True)
    rho_ = l0 + sp.symbols("e_", positive=True)
    resid2 = (l0 - rho_) ** 2 * v0 ** 2 + (l1v - rho_) ** 2 * v1 ** 2 \
        + (l2v - rho_) ** 2 * v2 ** 2
    tail2 = v1 ** 2 + v2 ** 2
    dec = resid2 - dl ** 2 * tail2
    dec_sub = sp.expand(dec.subs({l1v: rho_ + dl,
                                  l2v: rho_ + dl
                                  + sp.symbols("s2p",
                                               positive=True)}))
    okH = all(
        c.is_nonnegative is True or c.is_positive is True
        for c in sp.Poly(dec_sub, v0, v1, v2).coeffs())
    okI = bool((sp.Integer(1) * 2 + 2 * 3) ** 2
               <= (1 + 4) * (4 + 9))
    check("A3 DK sin-theta lemma (spectral decomposition, "
          "positive-slack)", okH and okI,
          "||Mv - rho v||^2 == sum (l_i - rho)^2 v_i^2 >= (lam1 - "
          "rho)^2 |tail|^2 when every excited gap >= lam1 - rho "
          "(generic K = 3, every coefficient of the slack "
          "polynomial nonnegative -- the r173 smoke-fix "
          "parametrization): sin(angle) <= r/(lam1 - R(v)); "
          "Cauchy-Schwarz instance for the y_t-move bound: any "
          "certificate-passing source lies in the DK cone "
          "(THEOREM DK)")

    W_, Llo, Lhi = sp.symbols("W_ Llo Lhi", positive=True)
    lock_w = lk / W_
    okJ = sp.simplify(lock_w * W_ - lk) == 0
    okK = sp.simplify((lk / W_).subs({lk: Lhi,
                                      W_: 2 * Lhi / Llo})
                      - Llo / 2) == 0
    okL = bool((sp.Rational(8, 1) / 1000 < 1.0)
               and (sp.Rational(1, 1) * 1000 > 8.0))
    check("A4 manifold exclusion: lock/W exit exact (both "
          "directions)", okJ and okK and okL,
          "the witness perturbs the VECTOR, never the world (M, "
          "tau, lam1, FULLGAP invariant) ==> lock'' == lock/W "
          "EXACTLY for any fixed-M y_t-inflation x W; W > "
          "L_hi/L_lo ==> window exit in BOTH directions (inflation "
          "lock/1000 = 3.6e-3 < 1.0, deflation lock x 1000 = "
          "3.6e3 > 8.0 in the record): the two-sided lock window "
          "refuses every large-dial witness")

    # ================================================ B: analytics
    section("B. SATURATION ANALYTICS RECOMPUTED (cited r172 ladder)")
    th_cited = {h: 10.0 ** YT_L10_R172[h] / (2 * math.pi * h) ** 4
                for h in YT_L10_R172}
    fit_h = [h for h in sorted(YT_L10_R172) if h != HOLDOUT_H]
    n = len(fit_h)
    fits = {}
    p_all, c0f = lsq([math.log10(h) for h in fit_h],
                     [YT_L10_R172[h] for h in fit_h])
    predP = {h: 10.0 ** (p_all * math.log10(h) + c0f)
             / (2 * math.pi * h) ** 4 for h in YT_L10_R172}
    fits["POW"] = (float("nan"),
                   sum((predP[h] - th_cited[h]) ** 2 for h in fit_h),
                   predP[HOLDOUT_H] - th_cited[HOLDOUT_H])
    for tag, qv in (("SATq0.5", 0.5), ("SATq1", 1.0),
                    ("SATq2", 2.0)):
        av, tinf = lsq([h ** (-qv) for h in fit_h],
                       [th_cited[h] for h in fit_h])
        pred = {h: tinf + av * h ** (-qv) for h in YT_L10_R172}
        fits[tag] = (tinf,
                     sum((pred[h] - th_cited[h]) ** 2
                         for h in fit_h),
                     pred[HOLDOUT_H] - th_cited[HOLDOUT_H])
    av, tinf = lsq([1.0 / math.log(h) for h in fit_h],
                   [th_cited[h] for h in fit_h])
    predL = {h: tinf + av / math.log(h) for h in YT_L10_R172}
    fits["LOG"] = (tinf,
                   sum((predL[h] - th_cited[h]) ** 2 for h in fit_h),
                   predL[HOLDOUT_H] - th_cited[HOLDOUT_H])
    ok40 = abs(p_all / P_ALL_STR - 1) <= ANA_TOL
    sat_tags = ("SATq0.5", "SATq1", "SATq2", "LOG")
    for tag in ("POW",) + sat_tags:
        tinf_m, rss_m, hold_m = fits[tag]
        tinf_s, rss_s, hold_s = FIT_TAB[tag]
        ok40 = ok40 and abs(rss_m / rss_s - 1) <= ANA_TOL \
            and abs(hold_m - hold_s) <= HOLD_ABS_TOL
        if tag != "POW":
            ok40 = ok40 and abs(tinf_m / tinf_s - 1) <= ANA_TOL \
                and TINF_BAND[0] <= tinf_m <= TINF_BAND[1] \
                and rss_m <= RSS_FACT * fits["POW"][1] \
                and abs(hold_m) <= HOLD_FACT * abs(fits["POW"][2])
    best = min(fits, key=lambda kk: fits[kk][1])
    ok40 = ok40 and best != "POW"
    check("B1 five-model saturation adjudication RECOMPUTED",
          ok40,
          "POW/SATq0.5/SATq1/SATq2/LOG refit from the cited ladder "
          "(holdout h = 30 excluded): every saturating model beats "
          "POW in RSS and at the holdout (factor <= 0.60); t_inf = "
          "%.4f..%.4f inside (0.070, 0.105); best model %s "
          "(saturating); p_all = %.4f == record: SATURATION-REAL-"
          "NOT-FIT-ARTIFACT; 4 pi t_inf spans %.2f..%.2f (contains "
          "1: the 1/(4 pi) candidate RECORDED-NOT-CLAIMED, "
          "anti-numerology firewall)"
          % (min(fits[t][0] for t in sat_tags),
             max(fits[t][0] for t in sat_tags), best, p_all,
             4 * math.pi * min(fits[t][0] for t in sat_tags),
             4 * math.pi * max(fits[t][0] for t in sat_tags)))

    ploc = {}
    for tag, (lo, hi) in PLOC_WINDOWS.items():
        hs = [h for h in YT_L10_R172 if lo <= h <= hi]
        sl, _c = lsq([math.log10(h) for h in hs],
                     [YT_L10_R172[h] for h in hs])
        ploc[tag] = float(sl)
    ok41 = all(abs(ploc[t] / PLOC_TAB[t] - 1) <= ANA_TOL
               for t in PLOC_TAB) \
        and ploc["W1"] > ploc["W2"] > ploc["W3"] > ploc["W4"] \
        and W4_WIN[0] <= ploc["W4"] <= W4_WIN[1]
    a_deep = ploc["W4"] / 2 - 1
    ok42 = abs(a_deep / A_DEEP_STR - 1) <= ANA_TOL \
        and abs(a_deep - RATE_A) <= A_DEEP_BAR
    check("B2 local exponent descent + rate dictionary RECOMPUTED",
          ok41 and ok42,
          "windowed slopes W1 %.4f > W2 %.4f > W3 %.4f > W4 %.4f "
          "in (3.95, 4.10): THE GROWTH LAW CONVERGES TO THE EXACT "
          "QUARTIC AT DEPTH (p_loc -> 4.024; the 4.18 all-rung "
          "exponent is an early-transient mixture); a_deep = "
          "p_W4/2 - 1 = %.4f vs the r171 record %.3f (|diff| "
          "%.4f <= 0.093: the dictionary closes TIGHTER)"
          % (ploc["W1"], ploc["W2"], ploc["W3"], ploc["W4"],
             a_deep, RATE_A, abs(a_deep - RATE_A)))

    tfg = {h: th_cited[h] * LOCK_R172[h] for h in YT_L10_R172}
    tfg_max, tfg_min = max(tfg.values()), min(tfg.values())
    lock_min = min(LOCK_R172.values())
    ceil_meas = tfg_max / lock_min
    ok43 = abs(tfg_max / TFG_MAX_STR - 1) <= ANA_TOL \
        and abs(tfg_min / TFG_MIN_STR - 1) <= ANA_TOL \
        and abs(ceil_meas / TFG_CEIL_MEAS - 1) <= 2e-3 \
        and ceil_meas < THETA_BAR and tfg_max > THETA_BAR
    check("B3 transport ceiling RECOMPUTED (conditional-on-measured "
          "flagged)", ok43,
          "THETA_FG in [%.4f, %.4f] on the cited ladder; with the "
          "MEASURED lock floor %.4f the transported ceiling is "
          "%.4f < THETA_BAR %.3f (margin %.2f) -- but with only "
          "the GATED window edge 1.0 it is %.4f > %.3f: the "
          "window-edge transport does NOT recover the bar; the "
          "premise is the v926 MEASURED quartic law (AIC p = "
          "3.9072 +- 0.1136): CONDITIONAL-ON-MEASURED, excluded "
          "from the delivered-theorem set"
          % (tfg_min, tfg_max, lock_min, ceil_meas, THETA_BAR,
             THETA_BAR / ceil_meas, tfg_max, THETA_BAR))

    hs_all = sorted(YT_L10_R172)
    dmax, darg = 0.0, None
    for i, h in enumerate(hs_all):
        for h2 in hs_all[i + 1:]:
            dd = th_cited[h] - th_cited[h2]
            if dd > dmax:
                dmax, darg = dd, (h, h2)
    blocks = {"B2": [h for h in hs_all if 4 <= h < 8],
              "B3": [h for h in hs_all if 8 <= h < 16],
              "B4": [h for h in hs_all if 16 <= h < 32]}
    brat = {}
    for bn, hs in blocks.items():
        vals = [th_cited[h] for h in hs]
        brat[bn] = min(vals) / (sum(vals) / len(vals))
    ok44 = abs(dmax / DMAX_STR - 1) <= ANA_TOL \
        and dmax <= 0.006 and dmax <= 0.04 * THETA_BAR \
        and all(abs(brat[b] / BLOCK_TAB[b] - 1) <= ANA_TOL
                for b in BLOCK_TAB) \
        and brat["B2"] < brat["B3"] < brat["B4"] \
        and brat["B4"] >= 0.90
    check("B4 quantifier gap + block subsidy RECOMPUTED",
          ok44,
          "D_meas = %.6f (drawdown h = %s; 2.7 percent of the bar; "
          "the limsup form of H3-cofinal holds only MOD this "
          "defect, BH7-F2 qualifier carried); block min/avg = "
          "%.4f/%.4f/%.4f rising -> 1: the pigeonhole gain shrinks "
          "to 4 percent at depth -- QUANTIFIER-GAP-MEASURED-THIN + "
          "the subsidy vanishes (BA-INEXPRESSIBLE-FOR-YT: the "
          "corpus ceiling routes are all dead or flagged)"
          % (dmax, darg, brat["B2"], brat["B3"], brat["B4"]))

    # ================================================ C/D: pinned
    section("C. THE WITNESS BOUNDARY (pinned; BH7-F4 SCOPED)")
    radii = list(STAB_GATE_TAB.values())
    okst = all(radii[i] < radii[i + 1] for i in range(len(radii) - 1)) \
        and STAB_GATE_TAB[13] < 1.0 and STAB_CROSSOVER_H == 16 \
        and STAB_MEAS_MAX <= 1e-30
    okwit = PIN_INFL2["r0_tau"] >= 1e10 and PIN_INFL2["lock"] < 1.0 \
        and PIN_INFL2["ba3_res"] < 0 and PIN_DEFL2["lock"] > 8.0 \
        and PIN_DEFL2["ba3_res"] < 0 \
        and PIN_INFL3["dnorm"] / PIN_INFL3["dnorm2"] >= 1e4 \
        and abs(PIN_INFL3["a6_ratio"] - 1) >= 0.9 \
        and PIN_INFL3["ba3_res"] < 0
    check("C1 witness excluded by the certified manifold "
          "(escalation-stable; DK-scoped)", okst and okwit,
          "the 2-mode witness breaks {ground-state cert %.1e, lock "
          "window both directions, BA3 bridge %.1e} while every "
          "identity holds; the 3-mode J2-repairing escalation "
          "costs x%.0e and STILL breaks the same three (J3 NOT "
          "preservable, generic); DK radii %.1e -> %.1e strictly "
          "growing with CROSSOVER h = %d: THE VERDICT IS SCOPED TO "
          "THE DK RADIUS (gated h <= 13, measured to h = 20, "
          "BH7-F4) -- the lock leg excludes only LARGE dials (the "
          "minimal H3-refuting dial W ~ 2.0-2.5 stays in-window at "
          "24/25 rungs; tested witnesses use W = 1000); at "
          "MEASURED residuals H3 is norm-stable (radii <= 1e-43)"
          % (PIN_INFL2["r0_tau"], abs(PIN_INFL2["ba3_res"]),
             PIN_INFL3["dnorm"] / PIN_INFL3["dnorm2"],
             STAB_GATE_TAB[4], STAB_GATE_TAB[13],
             STAB_CROSSOVER_H))

    okband = TINF_BAND_MEAS[0] < TINF_BAND_MEAS[1] < THETA_BAR \
        and abs(THETA_BAR / TINF_BAND_MEAS[1] / BAND_MARGIN - 1) \
        <= 2e-3 \
        and TINF_BAND_MEAS[0] < 1 / (4 * math.pi) < TINF_BAND_MEAS[1]
    check("D1 theta_inf band under the bar (OPEN; recorded not "
          "claimed)", okband,
          "theta_inf band [%.6f, %.6f] sits UNDER the bar %.3f with "
          "margin %.3f; the 1/(4 pi) = %.6f candidate lies INSIDE "
          "the band -- too wide to pin, NO window derived from it "
          "(v354/v355 discipline): theta_inf typed OPEN (re-typed "
          "OPEN-NONPERTURBATIVE-VARIATIONAL in v935)"
          % (TINF_BAND_MEAS[0], TINF_BAND_MEAS[1], THETA_BAR,
             THETA_BAR / TINF_BAND_MEAS[1], 1 / (4 * math.pi)))

    print("\n  [TYPED, BH7-F1/F2/F4 ADOPTED] THE RESIDUE: the "
          "composed per-block")
    print("  hypothesis is the TRIPLE {H1 AND H2 AND H3}-cofinal "
          "(one rung per")
    print("  dyadic block, all three at the same h); the limsup "
          "form only MOD the")
    print("  measured defect D = 0.004183; the manifold-exclusion "
          "verdict SCOPED")
    print("  to the DK radius.  + {census-forall-k == LOOP} + {L1, "
          "WPD}.  Census")
    print("  cardinality 4 UNCHANGED.  NOT RH evidence.  NO RH "
          "claim.")
    return 0


def run():
    global CHECKS, FAILS
    CHECKS = []
    FAILS = []
    print("=" * 74)
    print("v933 -- PRIME.H3.COFINAL.01 (saturation real, p_loc -> "
          "4.024; pigeonhole")
    print("exact subsidy-empty; witness excluded by the certified "
          "manifold, DK-")
    print("scoped; theta_inf band under the bar, OPEN; round 173; "
          "NO RH claim)")
    print("=" * 74)
    rc = part()
    n_run, fails = len(CHECKS), list(FAILS)
    verdict = EXPECTED if (rc == 0 and not fails) else "MIXED"
    ok = (rc == 0 and n_run == N_CHECKS and not fails
          and verdict == EXPECTED)
    print("\n" + "=" * 74)
    print("v933: %d/%d checks passed | verdict %s | runtime %.1f s"
          % (n_run - len(fails), n_run, verdict, time.time() - T_ALL))
    print("PINNING: PH/TRANSPORT/DK/manifold algebra + ALL "
          "saturation analytics")
    print("recomputed in-run (fit code ported verbatim from "
          "h3_cofinal_probe.py,")
    print("SPEC %s, 36/36, record + deterministic re-run, all logs "
          "kept, RE-RUN" % PIN_SPEC_R173)
    print("GREEN AS TYPED AT PROMOTION); witness/stability tables "
          "pinned.  NOT RH")
    print("evidence; NO RH claim.")
    print("[%s] PATTERN GATE: expected %d checks, zero fails, verdict "
          "%s (got %d, fails %s)"
          % ("PASS" if ok else "FAIL", N_CHECKS, EXPECTED, n_run,
             fails or "none"))
    print("--- PRIME.H3.COFINAL.01 H3-cofinal adjudication: "
          "%d passed, %d failed ---"
          % (n_run - len(fails), len(fails)))
    return 0 if ok else 1


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