"""v683 -- PRIME.RANK3FUNC.01: OFFENSIVE 2 -- THE ARITHMETIC
BOTTLENECK, LIFTED EXACTLY.

The parity-Toeplitz classification (Paper II, thm:rank3) compresses the
prime influence on the load-bearing 2x2 Gram block into THREE linear
functionals of the comb weights:

    S_j(a) = sum_{p^m <= e^{2a}} (log p / p^{m/2}) * What_j(m log p; a),
    j in {11, 22, 12},

where What_j is the TWO-POINT SPLINE READ (prop:split) of the CLOSED
parity lag weights:

  diagonal (lem:closedweight / v587, machine-exact):
    W_kk(d) = (2/N) [ (N - d) cos(w_k d) + cot(w_k) sin(w_k d) ],  d >= 1,
    W_kk(0) = 1,  N = 2h + 1,  w_k = 2 pi k / N;
  cross (v576 C1, machine-exact):
    W_12(d) = (2/N) [ sin w_2 sin(d w_1) - sin w_1 sin(d w_2) ]
              / (cos w_1 - cos w_2),  d >= 0.

Positivity of the block Ahat_2 = B - S runs through the exact
polarisation det Ahat = det B - D(B,S) + det S (prop:polarisation), so
the LOAD-BEARING inequality on the prime block is

    det S(a)  >=  R_arch(a) := D(B, S)(a) - det B(a),

with margin det Ahat = (1 - r12^2) a11 a22 (prob:R1 scale) -- a
razor-thin three-term cancellation, NOT a coarse sign inequality.

FIVE SLICES (bars and enums declared BEFORE any number):

R1 [EXTRACTION, EXACT].  Rebuild the three functionals END TO END from
   the closed formulas + an INDEPENDENT von Mangoldt sieve + the
   two-point read, and compare against the v563 window assembly
   (build_window, read-only import):
     bar R1.W:    closed weights vs corpus lag_weights_from_v,
                  max abs dev <= TOL_WFORM = 1e-10 (all lags);
     bar R1.ASM:  assembly identity (independent sieve + independent
                  read, CORPUS weight arrays) vs the v563 S-matrix,
                  residuum / max(1, ||rho||_1) <= TOL_ASM = 1e-13;
     bar R1.E2E:  the full closed rebuild vs the v563 S-matrix on ALL
                  windows, residuum <= TOL_EXACT = 1e-12 (the cross
                  formula's denominator is evaluated by the product
                  identity 2 sin(3pi/N) sin(pi/N) -- the naive
                  cos w1 - cos w2 loses ~2.5e-11 to cancellation);
     bar R1.EXACT: both pipelines vs an mpmath 30-digit reference of
                  the SAME definition (exact weights, exact rho,
                  identical cell selection) on three declared windows
                  (smallest family, v563 reference, deepest):
                  residuum <= TOL_EXACT = 1e-12 each.
   Tabulate S_j(a) on 5 family windows + 10 intermediate a values
   (DECLARED choice: the 5 family windows are the quintile elements of
   the v563 frame-A candidate list -- the middle one IS the v563
   reference window h = 540 -- and the 10 intermediates are evenly
   spaced list indices inside the four quintile gaps, split 2/3/3/2).

R2 [THE TARGET INEQUALITY].  (i) sign and trend of det S over a;
   (ii) the archimedean absorption remainder R_arch(a) = D(B,S) - det B
   exactly per window (v563 B matrices); (iii) the two target forms:
     T-A (task's literal form):  det S >= -c a^{-eta}   and
     T-B (load-bearing form):    det S - R_arch >= 0, margin
          1 - r12^2 = det Ahat/(a11 a22) ~ c_h h^{-eta_h} (measured fit
          on the complete-comb windows).

R3 [CIRCULARITY CHECK -- MANDATORY BEFORE ANY TOOL WORK].  Per
   functional, S_j = M_j + F_j with the smooth main term
   M_j = int_{U0}^{2a} What_j(u) e^{u/2} du (the program's calibrated
   pole-term model, U0 = 2 log(-C/4), C = -2 (zeta'/zeta)(1/2),
   v583/v587 convention).  TWO fluctuation envelopes:
     (sch)  the naive-RH yardstick, Schoenfeld 1976
            |psi(x)-x| < sqrt(x) log^2 x/(8 pi) for x >= 73.2 (YARDSTICK
            ONLY, no RH assumed; exact table envelope below 73.2);
     (ex)   the EXACT |psi(x)-x| on the table (the strongest possible
            entry-wise input on this surface -- stronger than RH here).
   Per-functional budget RHB_j^env = boundary + int |What_j' -
   What_j/2|(u) Env(u) e^{u/2}-normalised du (numeric 4-pt Gauss per
   lag cell, declared an estimate).  Determinant-level budgets
   (triangle inequality on the J-pairing):
     Delta_quad = RHB11 RHB22 + RHB12^2,
     Delta_A = |M11| RHB22 + |M22| RHB11 + 2 |M12| RHB12 + Delta_quad,
     Delta_B = same with the measured Ahat entries in place of M,
     Delta_C = RHB(K_M) + Delta_quad   [THE LIFTED PAIRING: the whole
       first-order determinant fluctuation is ONE linear functional of
       the comb, D(M, F) = sum_n rho_n Khat(u_n) - int Khat e^{u/2} du
       with the closed kernel K_M = M11 W22 + M22 W11 - 2 M12 W12 --
       identity checked to 1e-10].
   CLASS VERDICT per target and envelope (bars declared): with
   ratio = (guaranteed positive part) / (budget),
     class (a) iff min ratio >= CLASS_HI = 3,
     class (b) iff min ratio in [CLASS_LO, CLASS_HI) = [1/3, 3),
     class (c) iff min ratio <  CLASS_LO = 1/3   (the roundabout).
   The DECISIVE reading is (sch) = what a naive RH insertion delivers;
   the (ex) reading separates arithmetic loss from the structural
   triangle-inequality loss.  If the det-level targets land in (c),
   the declared WEAKENING CASCADE is applied and documented:
     T-ENTRY:   |F_j| <= RHB_j (class read off the measured usage),
     T-PAIR:    |D(M,F)| <= RHB(K_M) (one functional, closed kernel),
     T-DET:     |D(M,F) + det F| <= kappa det M, kappa < 1 (the
                uniform-constant form; measured kappa reported).

R4 [TOOL SOUNDING -- gate: the pointwise Cauchy-Schwarz test always
    runs (a win would be UNCONDITIONAL, no RH involved); the
    pretentious scan runs against the WEAKENED target chain iff that
    chain reaches class (a)/(b) at some level].
   (ii, FIRST -- the five-line-win test): are the three sums moments
       <f,f>, <g,g>, <f,g> of ONE inner product w.r.t. the positive
       comb measure?  Necessary and sufficient pointwise structure:
       What11(u) >= 0, What22(u) >= 0, What12^2 <= What11 What22
       (then S11 S22 - S12^2 >= 0 is Cauchy-Schwarz, unconditional).
       Test on ALL atoms + a dense u grid (N_GRID = 200000).
       Enum: CS-POINTWISE-WIN / CS-POINTWISE-FAIL.
   (i) pretentious test: worst multiplicative phase
       rho_n -> rho_n Re(e^{i phi} n^{i tau}) = rho_n cos(tau u_n + phi).
       Exact reduction: S_j(tau, phi) = v(phi)^T z_j(tau) with
       z_j(tau) = sum_n rho_n e^{i tau u_n} What_j(u_n) in R^2 and
       v(phi) = (cos phi, -sin phi), so det(tau, phi) = v^T Q(tau) v,
       Q = (z1 z2^T + z2 z1^T)/2 - z3 z3^T, and the min over ALL
       phases at fixed tau is lambda_min(Q(tau)) in closed form.
       Scan tau in [0, TAU_MAX] step TAU_STEP + refinement; also the
       NONNEGATIVE pretentious family rho_n (1 + cos(tau u_n + phi))/2
       (does positivity of the measure alone save the determinant?).
       Enums: PRET-ROBUST / PRET-BREAKS (worst tau, phi, magnitudes;
       position of the worst tau vs the dual points gamma_k printed).

R5 [CONTRACT NOTE] printed at the end: the exact functionals, the
   target inequality with measured (c, eta), the circularity verdict,
   the Cauchy-Schwarz finding.  NO file outside experiments/ is
   touched; no marker moves; the ledger is untouched.

FIREWALL: v563 import strictly read-only (same pattern as
margin_link_probe.py); an INDEPENDENT sieve cross-checks the atom
table bit for bit; no zero of any L-function is read (AST-checked;
mpmath is used ONLY for values: zeta'(1/2)/zeta(1/2) and the R1
reference evaluation, per v587); deterministic, no RNG.  Radical
honesty: the razor-thin margin of T-B, any Cauchy-Schwarz failure and
any class-(c) verdict are reported as they fall -- the elegant
roundabout is the named failure mode, and the truncated-comb window
(det S < 0, the v619 flip set) is shown, not hidden.

PROVENANCE: discovery probe rank3_functionals_probe.py (2026-08-03,
19/19 PASS, circularity verdict: T-A/T-PAIR/T-B class (c), T-ENTRY
class (a), CS-POINTWISE-FAIL, PRET-BREAKS);
parity_toeplitz_classification.tex (prop:split,
prop:polarisation, thm:rank3, lem:closedweight, lem:dualpoint,
prob:R1, meas:refwindow); v563_paper2_readouts.py (assembly, S1/S4);
v576_cheb_loewner_edge.py (cross formula); v587_w_closed_form.py
(diagonal formula, U0 cutoff); margin_link_probe.py (T163 pairing,
import pattern); v618/v619 (flip-set context for the truncated comb).
"""
import ast
import math
import os
import sys
import time

import numpy as np

# ---------------------------------------------------------------- imports
_here = os.path.dirname(os.path.abspath(__file__))
for _cand in (_here, os.path.join(_here, "..", "..", "verification")):
    if os.path.exists(os.path.join(_cand, "v563_paper2_readouts.py")):
        sys.path.insert(0, os.path.abspath(_cand))
        break

import v563_paper2_readouts as core  # noqa: E402  (READ-ONLY import)
from mpmath import mp, mpf, zeta, diff, cos as mcos, sin as msin, \
    log as mlog, sqrt as msqrt, pi as mpi  # noqa: E402  (VALUES only)

T0 = time.time()
FAILS = []
N_CHK = 0

# ------------------------------------------------- declared bars / constants
TOL_WFORM = 1.0e-10     # closed weights vs corpus, max abs dev, all lags
TOL_ASM = 1.0e-13       # assembly identity (corpus weights), scaled
TOL_EXACT = 1.0e-12     # closed rebuild vs mpmath reference, scaled
TOL_IDENT = 1.0e-10     # det S - det M == D(M,F) + det F, relative
TOL_PAIR = 1.0e-10      # D(M,F) == the single K_M-functional, relative
N_FAM = 5               # family windows (quintiles of the frame-A list)
N_INTER = 10            # intermediate a values (gaps split 2/3/3/2)
INTER_SPLIT = (2, 3, 3, 2)
SCHOENFELD_X0 = 73.2    # Schoenfeld 1976 validity floor
EIGHT_PI = 8.0 * math.pi
CLASS_HI = 3.0          # class (a) bar: min ratio >= 3
CLASS_LO = 1.0 / 3.0    # class (c) bar: min ratio < 1/3
ENTRY_USE_BAR = 0.5     # T-ENTRY class (a) iff max budget usage <= this
N_GRID_CS = 200000      # dense u grid for the pointwise CS test
TAU_MAX = 8.0           # pretentious scan range (dual points ~0.62/1.24)
TAU_STEP = 0.01
N_PHI = 720             # phase grid for the nonnegative family
REFINE_HALF = 0.05      # local refinement half-width around minima
REFINE_STEP = 2.0e-4
FLOAT_FLOOR = 1.0e-12   # 'zero' floor for the CS / pretentious verdicts
N_QUAD_CELL = 4         # Gauss points per lag cell in the budgets
MP_DPS = 30             # mpmath reference precision

mp.dps = MP_DPS


def check(name, ok, detail=""):
    global N_CHK
    N_CHK += 1
    if not ok:
        FAILS.append(name.split()[0])
    print("[%s] %s%s" % ("PASS" if ok else "FAIL", name,
                         (": " + detail) if detail else ""))


def ast_zero_firewall(src_path):
    with open(src_path, "r", encoding="utf-8") as fh:
        tree = ast.parse(fh.read())
    hits = []
    for node in ast.walk(tree):
        if isinstance(node, ast.Call):
            f = node.func
            if isinstance(f, ast.Attribute) and f.attr in (
                    "zetazero", "nzeros", "second_sheet_zero"):
                hits.append(f.attr)
            if isinstance(f, ast.Name) and f.id in ("zetazero", "nzeros"):
                hits.append(f.id)
    return not hits


# ------------------------------------------------- independent sieve (R1)
def independent_von_mangoldt(n_max):
    """A second, independently coded von Mangoldt table (cross-check)."""
    spf = np.zeros(n_max + 1, dtype=np.int64)
    for i in range(2, n_max + 1):
        if spf[i] == 0:
            spf[i::i] = np.where(spf[i::i] == 0, i, spf[i::i])
    lam = np.zeros(n_max + 1)
    for p in range(2, n_max + 1):
        if spf[p] == p:
            lp = math.log(p)
            q = p
            while q <= n_max:
                lam[q] = lp
                q *= p
    return lam


# ------------------------------------------------- closed weights (R1)
def closed_weights(hz):
    """The three closed lag-weight vectors on d = 0..M-1.

    Diagonal: lem:closedweight (Paper II) / v587 (equivalent form).
    Cross:    v576 C1 (exact).
    """
    N = 2 * hz + 1
    Mz = 2 * hz
    d = np.arange(Mz, dtype=float)
    w1 = 2.0 * math.pi / N
    w2 = 4.0 * math.pi / N
    W11 = (2.0 / N) * ((N - d) * np.cos(w1 * d)
                       + (math.cos(w1) / math.sin(w1)) * np.sin(w1 * d))
    W22 = (2.0 / N) * ((N - d) * np.cos(w2 * d)
                       + (math.cos(w2) / math.sin(w2)) * np.sin(w2 * d))
    W11[0] = 1.0
    W22[0] = 1.0
    # denominator via the product identity (no catastrophic cancellation):
    # cos w1 - cos w2 = 2 sin((w1+w2)/2) sin((w2-w1)/2) = 2 sin(3pi/N) sin(pi/N)
    den12 = 2.0 * math.sin(3.0 * math.pi / N) * math.sin(math.pi / N)
    W12 = (2.0 / N) * (math.sin(w2) * np.sin(d * w1)
                       - math.sin(w1) * np.sin(d * w2)) / den12
    return W11, W22, W12


def spline_read_vec(W, u, D):
    """Vectorised two-point read (prop:split); reflection branch is
    asserted dead (u_min > D on every window of the surface)."""
    Mz = len(W)
    q = np.asarray(u, dtype=float) / D
    i0 = np.floor(q).astype(np.int64)
    f = q - i0
    v = np.zeros_like(q)
    ok0 = (i0 >= 0) & (i0 < Mz)
    v[ok0] = (1.0 - f[ok0]) * W[i0[ok0]]
    ok1 = (i0 + 1 >= 0) & (i0 + 1 < Mz)
    v[ok1] += f[ok1] * W[i0[ok1] + 1]
    return v


def mp_closed_S(r, nn_atoms, uu_atoms):
    """mpmath 30-digit reference: the closed weights at the needed lags
    only, exact rho_n = log p / p^{m/2}, identical float64 cell
    selection (i0, f from the SAME float64 u/D as the corpus)."""
    hz, D = r["h"], r["D"]
    N = 2 * hz + 1
    Mz = 2 * hz
    w1 = 2 * mpi / N
    w2 = 4 * mpi / N
    cot1 = mcos(w1) / msin(w1)
    cot2 = mcos(w2) / msin(w2)
    den12 = 2 * msin(3 * mpi / N) * msin(mpi / N)

    cache = {}

    def w_at(d):
        if d not in cache:
            if d == 0:
                cache[d] = (mpf(1), mpf(1), mpf(0))
            else:
                dm = mpf(d)
                c1, s1 = mcos(w1 * dm), msin(w1 * dm)
                c2, s2 = mcos(w2 * dm), msin(w2 * dm)
                cache[d] = (
                    (2 / mpf(N)) * ((N - dm) * c1 + cot1 * s1),
                    (2 / mpf(N)) * ((N - dm) * c2 + cot2 * s2),
                    (2 / mpf(N)) * (msin(w2) * s1 - msin(w1) * s2) / den12)
        return cache[d]

    def smallest_prime_factor(n):
        if n % 2 == 0:
            return 2
        f = 3
        while f * f <= n:
            if n % f == 0:
                return f
            f += 2
        return n

    tot = [mpf(0), mpf(0), mpf(0)]
    for n, u in zip(nn_atoms, uu_atoms):
        n = int(n)
        u = float(u)                    # the corpus float64 u_n, verbatim
        q = u / D                       # identical float64 division
        i0 = int(math.floor(q))
        f = mpf(q) - i0
        # exact rho_n = Lambda(n)/sqrt(n): base p = smallest prime factor
        p = smallest_prime_factor(n)
        t = n
        while t % p == 0:
            t //= p
        assert t == 1, "atom %d is not a prime power" % n
        rho = mlog(mpf(p)) / msqrt(mpf(n))
        for lag, wt in ((i0, 1 - f), (i0 + 1, f)):
            if 0 <= lag < Mz:
                W3 = w_at(lag)
                for j in range(3):
                    tot[j] += rho * wt * W3[j]
    return tot


# ------------------------------------------------- model + budgets (R3)
def model_entry(W, D, Mz, u_lo, u_hi):
    """int_{u_lo}^{u_hi} What(u) e^{u/2} du, EXACT per lag cell
    (What is linear on [iD, (i+1)D] with node values W[i], W[M] := 0)."""
    Wn = np.append(np.asarray(W, dtype=float), 0.0)
    i_arr = np.arange(Mz)
    lo = np.maximum(i_arr * D, u_lo)
    hi = np.minimum((i_arr + 1) * D, u_hi)
    act = hi > lo
    lo, hi, ii = lo[act], hi[act], i_arr[act]
    A = Wn[ii]
    Bsl = (Wn[ii + 1] - Wn[ii]) / D
    u0c = ii * D

    def prim(u):
        return (2.0 * (A + Bsl * (u - u0c)) - 4.0 * Bsl) * np.exp(0.5 * u)

    return float(np.sum(prim(hi) - prim(lo)))


_GLXQ, _GLWQ = np.polynomial.legendre.leggauss(N_QUAD_CELL)


def fluct_budget(W, D, Mz, u_lo, u_hi, env_fun):
    """|F| <= |g(u_lo)| |E|(u_lo) + |g(u_hi)| |E|(u_hi)
             + int |What' - What/2|(u) env(u) du,
    with g = What e^{-u/2} and env(u) = |E|(e^u) e^{-u/2} supplied by
    env_fun (vectorised in u).  Numeric 4-pt Gauss per lag cell."""
    Wn = np.append(np.asarray(W, dtype=float), 0.0)
    i_arr = np.arange(Mz)
    lo = np.maximum(i_arr * D, u_lo)
    hi = np.minimum((i_arr + 1) * D, u_hi)
    act = hi > lo
    lo, hi, ii = lo[act], hi[act], i_arr[act]
    A = Wn[ii]
    Bsl = (Wn[ii + 1] - Wn[ii]) / D
    u0c = ii * D
    mid = 0.5 * (lo + hi)
    half = 0.5 * (hi - lo)
    uu = mid[:, None] + half[:, None] * _GLXQ[None, :]
    what = A[:, None] + Bsl[:, None] * (uu - u0c[:, None])
    gprime = np.abs(Bsl[:, None] - 0.5 * what)
    env = env_fun(uu)
    integral = float(np.sum(half[:, None] * gprime * env * _GLWQ[None, :]))
    w_lo = float(spline_read_vec(Wn[:-1], np.array([u_lo]), D)[0])
    w_hi = float(spline_read_vec(Wn[:-1], np.array([u_hi - 1e-12]), D)[0])
    bnd = (abs(w_lo) * float(env_fun(np.array([[u_lo]]))[0, 0])
           + abs(w_hi) * float(env_fun(np.array([[u_hi - 1e-12]]))[0, 0]))
    return bnd + integral


def mixed_D(P11, P22, P12, Q11, Q22, Q12):
    """D(P, Q) = P11 Q22 + P22 Q11 - 2 P12 Q12 (the det polarisation)."""
    return P11 * Q22 + P22 * Q11 - 2.0 * P12 * Q12


def ols_loglog(x, y):
    """OLS fit log|y| = b log x + q; returns (b, e^q, R^2)."""
    lx, ly = np.log(np.asarray(x)), np.log(np.abs(np.asarray(y)))
    b, q = np.polyfit(lx, ly, 1)
    pred = b * lx + q
    ss = 1.0 - np.sum((ly - pred) ** 2) / max(np.sum((ly - ly.mean()) ** 2),
                                              1e-300)
    return float(b), float(math.exp(q)), float(ss)


def lam_min_2x2(q11, q22, q12):
    tr = q11 + q22
    disc = math.sqrt((q11 - q22) ** 2 + 4.0 * q12 ** 2)
    return 0.5 * (tr - disc)


def class_of(rmin):
    if rmin >= CLASS_HI:
        return "a"
    if rmin >= CLASS_LO:
        return "b"
    return "c"


def run():
    global N_CHK, FAILS
    N_CHK = 0
    FAILS = []
    print("=" * 78)
    print("OFFENSIVE 2 -- the rank-3 functionals, extracted exactly "
          "(rank3_functionals_probe)")
    print("=" * 78)

    # ============================================================== S0
    print("\nS0 -- firewall + the one arithmetic input")
    check("S0.AST no zeta-zero loader in this module",
          ast_zero_firewall(__file__))
    lam_ind = independent_von_mangoldt(core.ATOM_MAX)
    check("S0.SIEVE independent von Mangoldt table equals the v563 table "
          "BIT FOR BIT (%d entries)" % (core.ATOM_MAX + 1),
          np.array_equal(lam_ind, core.LAM_TAB))
    nn_ind = np.nonzero(lam_ind > 0.0)[0]
    u_ind = np.log(nn_ind.astype(float))
    rho_ind = lam_ind[nn_ind] / np.sqrt(nn_ind.astype(float))
    psi_tab = np.cumsum(lam_ind[nn_ind])

    # exact |psi(t)-t| envelope below X0 (sup at jumps, both sides)
    e_small = 0.0
    prev = 0.0
    for x_val, ps in zip(nn_ind.astype(float), psi_tab):
        if x_val > SCHOENFELD_X0:
            break
        e_small = max(e_small, abs(prev - x_val), abs(ps - x_val))
        prev = ps
    e_small = max(e_small, abs(prev - SCHOENFELD_X0))
    print("    E_small (exact |psi(x)-x| envelope below x = %.1f): %.4f"
          % (SCHOENFELD_X0, e_small))

    def env_schoenfeld(uu):
        """|E|(e^u) e^{-u/2}: u^2/(8 pi) above X0, exact envelope below."""
        return np.where(np.exp(uu) >= SCHOENFELD_X0,
                        uu ** 2 / EIGHT_PI,
                        e_small * np.exp(-0.5 * uu))

    def env_exact(uu):
        """pointwise-exact |psi(x)-x| e^{-u/2} on the table (unconditional
        on this surface; numeric-estimate quadrature, declared)."""
        x = np.exp(uu)
        idx = np.searchsorted(nn_ind.astype(float), x, side="right") - 1
        ps = np.where(idx >= 0, psi_tab[np.maximum(idx, 0)], 0.0)
        return np.abs(ps - x) * np.exp(-0.5 * uu)

    c_th = float(-2 * diff(lambda s: zeta(s), 0.5) / zeta(0.5))
    u0_cut = 2.0 * math.log(-c_th / 4.0)
    print("    U0 = 2 log(-C/4) = %.6f  (C = -2 (zeta'/zeta)(1/2) = %.6f; "
          "v583/v587 convention)" % (u0_cut, c_th))

    # ============================================================== R1
    print("\nR1 -- extraction, exact (closed weights + independent comb "
          "vs the v563 assembly)")
    KZ = core.frame_a_zones()
    L = len(KZ)
    fam5 = [0, (L - 1) // 4, L // 2, (3 * (L - 1)) // 4, L - 1]
    inter = []
    for (lo, hi), n_in in zip(zip(fam5[:-1], fam5[1:]), INTER_SPLIT):
        for j in range(1, n_in + 1):
            inter.append(lo + j * (hi - lo) // (n_in + 1))
    idx = sorted(set(fam5 + inter))
    check("R1.PICK declared window set: %d family (quintiles of %d frame-A "
          "candidates; middle = v563 reference) + %d intermediates = %d "
          "distinct windows" % (N_FAM, L, N_INTER, len(idx)),
          len(idx) == N_FAM + N_INTER and fam5[2] == L // 2)

    rows = []
    dev_w_all = 0.0
    res_asm_all = 0.0
    res_closed_all = 0.0
    for kz in idx:
        r = core.build_window(KZ[kz])
        W11c, W22c, W12c = closed_weights(r["h"])
        dev_w = max(float(np.max(np.abs(W11c - r["W11"]))),
                    float(np.max(np.abs(W22c - r["W22"]))),
                    float(np.max(np.abs(W12c - r["W12"]))))
        dev_w_all = max(dev_w_all, dev_w)
        ka = len(r["uu"])
        n_sel = int(np.searchsorted(u_ind, 2.0 * r["alpha"] + 1.0e-14,
                                    side="right"))
        uu = u_ind[:n_sel]
        rho = rho_ind[:n_sel]
        assert n_sel == ka and np.array_equal(uu, r["uu"]), \
            "independent atom table disagrees with v563"
        assert float(uu[0]) > r["D"], "reflection branch would be live"
        scale = max(1.0, float(np.sum(rho)))
        # (i) assembly identity with the CORPUS weight arrays
        sc11 = float(rho @ spline_read_vec(r["W11"], uu, r["D"]))
        sc22 = float(rho @ spline_read_vec(r["W22"], uu, r["D"]))
        sc12 = float(rho @ spline_read_vec(r["W12"], uu, r["D"]))
        res_asm = max(abs(sc11 - r["S"][0, 0]), abs(sc22 - r["S"][1, 1]),
                      abs(sc12 - r["S"][0, 1])) / scale
        res_asm_all = max(res_asm_all, res_asm)
        # (ii) end-to-end with the closed formulas
        s11 = float(rho @ spline_read_vec(W11c, uu, r["D"]))
        s22 = float(rho @ spline_read_vec(W22c, uu, r["D"]))
        s12 = float(rho @ spline_read_vec(W12c, uu, r["D"]))
        res_closed = max(abs(s11 - r["S"][0, 0]), abs(s22 - r["S"][1, 1]),
                         abs(s12 - r["S"][0, 1])) / scale
        res_closed_all = max(res_closed_all, res_closed)
        detS = r["S"][0, 0] * r["S"][1, 1] - r["S"][0, 1] ** 2
        detB = r["B"][0, 0] * r["B"][1, 1] - r["B"][0, 1] ** 2
        dbs = mixed_D(r["B"][0, 0], r["B"][1, 1], r["B"][0, 1],
                      r["S"][0, 0], r["S"][1, 1], r["S"][0, 1])
        rows.append(dict(kz=kz, r=r, fam=(kz in fam5),
                         complete=(r["n_zone"] ** 2 <= core.ATOM_MAX + 0.5),
                         nn_at=nn_ind[:n_sel],
                         Sc=(s11, s22, s12),
                         S11=r["S"][0, 0], S22=r["S"][1, 1],
                         S12=r["S"][0, 1], detS=detS, detB=detB, dbs=dbs,
                         detA=r["det"], onem=r["onem"]))
    check("R1.W closed weight formulas (diagonal lem:closedweight, cross "
          "v576) reproduce the corpus lag weights on ALL %d windows, ALL "
          "lags: max abs dev %.2e <= %.0e" % (len(idx), dev_w_all, TOL_WFORM),
          dev_w_all <= TOL_WFORM)
    check("R1.ASM assembly identity (INDEPENDENT sieve + INDEPENDENT "
          "two-point read, corpus weight arrays) reproduces the v563 "
          "S-matrix on ALL %d windows: residuum %.2e <= %.0e "
          "(scale max(1, ||rho||_1))" % (len(idx), res_asm_all, TOL_ASM),
          res_asm_all <= TOL_ASM)

    check("R1.E2E end-to-end rebuild (closed formulas + INDEPENDENT sieve "
          "+ two-point read) reproduces the v563 S-matrix on ALL %d "
          "windows: residuum %.2e <= %.0e (scale max(1, ||rho||_1))"
          % (len(idx), res_closed_all, TOL_EXACT),
          res_closed_all <= TOL_EXACT)

    # (iii) the mpmath reference on three declared windows
    ref_row = next((w for w in rows if w["r"]["h"] == core.Q_H_REF),
                   rows[len(rows) // 2])
    res_ex_closed = 0.0
    res_ex_corpus = 0.0
    mp_hs = []
    for w in (rows[0], ref_row, rows[-1]):
        r = w["r"]
        mp_hs.append(r["h"])
        tot = mp_closed_S(r, w["nn_at"], r["uu"])
        scale = max(1.0, float(np.sum(rho_ind[:len(w["nn_at"])])))
        got = w["Sc"]
        cor = (r["S"][0, 0], r["S"][1, 1], r["S"][0, 1])
        res_ex_closed = max(res_ex_closed,
                            max(abs(float(tot[j] - got[j]))
                                for j in range(3)) / scale)
        res_ex_corpus = max(res_ex_corpus,
                            max(abs(float(tot[j] - cor[j]))
                                for j in range(3)) / scale)
    check("R1.EXACT against the mpmath %d-digit reference (exact weights, "
          "exact rho, identical cell selection) on the three declared "
          "windows h = %s: closed rebuild residuum %.2e <= %.0e, corpus "
          "float64 S residuum %.2e -- both pipelines sit at their float64 "
          "rounding floor around the same exact values"
          % (MP_DPS, mp_hs, res_ex_closed, TOL_EXACT, res_ex_corpus),
          res_ex_closed <= TOL_EXACT and res_ex_corpus <= TOL_EXACT)

    print("\n    the three functionals over a  (F = family window, "
          "I = intermediate; C = complete comb, T = truncated)")
    print("    %-2s %-2s %5s %6s %8s %10s %12s %12s %12s %14s"
          % ("t", "C", "h", "nz", "a", "X", "S11", "S22", "S12", "detS"))
    for w in rows:
        r = w["r"]
        print("    %-2s %-2s %5d %6d %8.4f %10.4g %12.6f %12.6f %12.6f "
              "%14.6f"
              % ("F" if w["fam"] else "I", "C" if w["complete"] else "T",
                 r["h"], r["n_zone"], r["alpha"], r["X"],
                 w["S11"], w["S22"], w["S12"], w["detS"]))

    # ============================================================== R2
    print("\nR2 -- the target inequality (det S vs the archimedean "
          "absorption remainder)")
    print("    %-2s %5s %8s %12s %12s %14s %14s %12s"
          % ("C", "h", "a", "detB", "D(B,S)", "R_arch", "detAhat",
             "1-r12^2"))
    for w in rows:
        r = w["r"]
        print("    %-2s %5d %8.4f %12.6f %12.6f %14.6f %14.6e %12.4e"
              % ("C" if w["complete"] else "T", r["h"], r["alpha"],
                 w["detB"], w["dbs"], w["dbs"] - w["detB"], w["detA"],
                 w["onem"]))
    comp = [w for w in rows if w["complete"]]
    trunc = [w for w in rows if not w["complete"]]
    if trunc:
        print("    NOTE: the truncated-comb window(s) %s carry det S < 0 "
              "and det Ahat < 0 -- the v619 flip mechanism (data-boundary "
              "artifact), excluded from every fit below, shown not hidden"
              % ([t["r"]["h"] for t in trunc]))
    check("R2.SIGN det S > 0 on ALL %d complete-comb windows of the "
          "declared set (min %.4f); the task's literal form "
          "det S >= -c a^{-eta} is satisfied with STRICTLY POSITIVE and "
          "GROWING left side -- it is far weaker than the load-bearing "
          "inequality" % (len(comp), min(w["detS"] for w in comp)),
          all(w["detS"] > 0.0 for w in comp))
    a_arr = np.array([w["r"]["alpha"] for w in comp])
    h_arr = np.array([float(w["r"]["h"]) for w in comp])
    dS_arr = np.array([w["detS"] for w in comp])
    sl_a = np.polyfit(a_arr, np.log(dS_arr), 1)
    print("    trend: log det S vs a slope = %.4f (pure e^a would be 1)"
          % sl_a[0])
    onem_arr = np.array([w["onem"] for w in comp])
    marg_arr = np.array([w["detA"] for w in comp])
    all_pos = bool(np.all(marg_arr > 0.0))
    eta_h, c_h, r2_h = ols_loglog(h_arr, onem_arr)
    check("R2.MARGIN the load-bearing margin det Ahat = det S - R_arch is "
          "> 0 on all %d complete windows and razor thin: 1 - r12^2 = "
          "c h^{-eta} with MEASURED eta = %.3f, c = %.3g (R^2 = %.3f) -- "
          "the target inequality is det S >= R_arch(a) with relative "
          "slack %.1e..%.1e of det S itself"
          % (len(comp), -eta_h, c_h, r2_h,
             float(np.min(marg_arr / dS_arr)),
             float(np.max(marg_arr / dS_arr))),
          all_pos and r2_h > 0.5)
    print("    TO BE PROVEN (T-B, prob:R1): 0 <= det S - R_arch "
          "<= C_eps h^{-3+eps} a11 a22; measured margin range "
          "[%.3e, %.3e]" % (float(np.min(marg_arr)), float(np.max(marg_arr))))

    # ============================================================== R3
    print("\nR3 -- the circularity check (naive-RH yardstick per "
          "functional and for the determinant)")
    print("    (Schoenfeld 1976 as YARDSTICK only -- no RH assumed, no "
          "zero read; 'exact' = pointwise |psi-x| on the table, the "
          "strongest possible entry-wise input on this surface)")
    id_worst = 0.0
    pair_worst = 0.0
    for w in comp:
        r = w["r"]
        D, Mz, a2 = r["D"], r["M"], 2.0 * r["alpha"]
        u_lo = u0_cut
        Wk = [r["W11"], r["W22"], r["W12"]]
        MM = [model_entry(Wk[j], D, Mz, u_lo, a2) for j in range(3)]
        RBs = [fluct_budget(Wk[j], D, Mz, u_lo, a2, env_schoenfeld)
               for j in range(3)]
        RBx = [fluct_budget(Wk[j], D, Mz, u_lo, a2, env_exact)
               for j in range(3)]
        FF = [w["S11"] - MM[0], w["S22"] - MM[1], w["S12"] - MM[2]]
        detM = MM[0] * MM[1] - MM[2] ** 2
        dMF = mixed_D(MM[0], MM[1], MM[2], FF[0], FF[1], FF[2])
        detF = FF[0] * FF[1] - FF[2] ** 2
        ident = abs(w["detS"] - detM - dMF - detF) / max(abs(w["detS"]),
                                                         1e-300)
        id_worst = max(id_worst, ident)
        # THE LIFTED PAIRING: one linear functional with closed kernel
        K_M = MM[0] * np.asarray(r["W22"]) + MM[1] * np.asarray(r["W11"]) \
            - 2.0 * MM[2] * np.asarray(r["W12"])
        uu = r["uu"]
        rho = r["lam"]
        pair_direct = float(rho @ spline_read_vec(K_M, uu, D)) \
            - model_entry(K_M, D, Mz, u_lo, a2)
        pair_worst = max(pair_worst,
                         abs(pair_direct - dMF) / max(abs(dMF), 1e-300))
        RBK_s = fluct_budget(K_M, D, Mz, u_lo, a2, env_schoenfeld)
        RBK_x = fluct_budget(K_M, D, Mz, u_lo, a2, env_exact)

        def dets(RB):
            quad = RB[0] * RB[1] + RB[2] ** 2
            dA = (abs(MM[0]) * RB[1] + abs(MM[1]) * RB[0]
                  + 2.0 * abs(MM[2]) * RB[2] + quad)
            dB = (abs(r["a11"]) * RB[1] + abs(r["a22"]) * RB[0]
                  + 2.0 * abs(r["a12"]) * RB[2] + quad)
            return quad, dA, dB

        quad_s, dA_s, dB_s = dets(RBs)
        quad_x, dA_x, dB_x = dets(RBx)
        w.update(MM=MM, RBs=RBs, RBx=RBx, FF=FF, detM=detM, dMF=dMF,
                 detF=detF, dA_s=dA_s, dA_x=dA_x, dB_s=dB_s, dB_x=dB_x,
                 dC_s=RBK_s + quad_s, dC_x=RBK_x + quad_x,
                 RBK_s=RBK_s, RBK_x=RBK_x,
                 kappa=abs(dMF + detF) / detM)
    check("R3.IDENT det S - det M = D(M,F) + det F holds as an identity "
          "on all complete windows (worst rel %.1e <= %.0e)"
          % (id_worst, TOL_IDENT), id_worst <= TOL_IDENT)
    check("R3.PAIR the first-order determinant fluctuation IS one linear "
          "functional of the comb: D(M,F) = sum_n rho_n Khat_M(u_n) - "
          "int Khat_M e^{u/2} du with the closed kernel K_M = M11 W22 + "
          "M22 W11 - 2 M12 W12 (worst rel dev %.1e <= %.0e) -- the "
          "rank-3 compression lifts the whole first-order question into "
          "ONE explicit prime sum" % (pair_worst, TOL_PAIR),
          pair_worst <= TOL_PAIR)

    print("\n    reference window h = %d, per functional:" % ref_row["r"]["h"])
    print("    %6s %12s %12s %14s %14s"
          % ("j", "M_j", "F_j = S-M", "RHB_sch", "RHB_exact"))
    for j, nm in enumerate(("11", "22", "12")):
        print("    %6s %12.4f %12.4f %14.4f %14.4f"
              % (nm, ref_row["MM"][j], ref_row["FF"][j],
                 ref_row["RBs"][j], ref_row["RBx"][j]))
    use_s = max(abs(w["FF"][j]) / w["RBs"][j] for w in comp for j in range(3))
    use_x = max(abs(w["FF"][j]) / w["RBx"][j] for w in comp for j in range(3))
    check("R3.ENTRY measured entry deviations sit INSIDE both budgets on "
          "all complete windows: max usage %.4f (Schoenfeld) / %.4f "
          "(exact envelope) <= %.1f -- T-ENTRY is class (a): weaker than "
          "what a naive RH insertion delivers per entry"
          % (use_s, use_x, ENTRY_USE_BAR),
          use_s <= ENTRY_USE_BAR and use_x <= ENTRY_USE_BAR)

    print("\n    determinant-level budgets over the complete surface:")
    print("    %5s %10s | %10s %8s | %10s %8s | %10s %8s | %10s %8s "
          "| %9s"
          % ("h", "detM", "dA_sch", "ratA_s", "dA_ex", "ratA_x",
             "dC_sch", "ratC_s", "dC_ex", "ratC_x", "kappa"))
    for w in comp:
        print("    %5d %10.4f | %10.1f %8.4f | %10.1f %8.4f | %10.1f "
              "%8.4f | %10.1f %8.4f | %9.5f"
              % (w["r"]["h"], w["detM"], w["dA_s"], w["detM"] / w["dA_s"],
                 w["dA_x"], w["detM"] / w["dA_x"],
                 w["dC_s"], w["detM"] / w["dC_s"],
                 w["dC_x"], w["detM"] / w["dC_x"], w["kappa"]))
    rA_s = min(w["detM"] / w["dA_s"] for w in comp)
    rA_x = min(w["detM"] / w["dA_x"] for w in comp)
    rC_s = min(w["detM"] / w["dC_s"] for w in comp)
    rC_x = min(w["detM"] / w["dC_x"] for w in comp)
    rB_s = min(w["detA"] / w["dB_s"] for w in comp)
    rB_x = min(w["detA"] / w["dB_x"] for w in comp)
    kap_max = max(w["kappa"] for w in comp)
    print("    class bars: (a) min ratio >= %.1f, (b) in [%.3f, %.1f), "
          "(c) < %.3f" % (CLASS_HI, CLASS_LO, CLASS_HI, CLASS_LO))
    check("R3.CLASS-TA target T-A (det S >= 0 via per-entry budgets): "
          "min det M/Delta_A = %.4f (Schoenfeld) --> CLASS (%s); with the "
          "EXACT |psi-x| envelope %.4f --> CLASS (%s)%s"
          % (rA_s, class_of(rA_s), rA_x, class_of(rA_x),
             " -- even the strongest entry-wise input fails: the loss is "
             "STRUCTURAL (triangle inequality on the J-pairing), not "
             "arithmetic" if class_of(rA_x) == "c" else ""), True)
    check("R3.CLASS-TC weakened target T-PAIR (the lifted single-"
          "functional pairing): min det M/Delta_C = %.4f (Schoenfeld) "
          "--> CLASS (%s); exact envelope %.4f --> CLASS (%s)"
          % (rC_s, class_of(rC_s), rC_x, class_of(rC_x)), True)
    check("R3.CLASS-TB load-bearing target T-B (det Ahat >= 0): min "
          "margin/Delta_B = %.3e (Schoenfeld) --> CLASS (%s); exact "
          "envelope %.3e --> CLASS (%s) -- THE ROUNDABOUT CONFIRMED at "
          "the absorption level: the razor-thin margin demands ~1e6 x "
          "more than ANY entry-wise input delivers; a proof must use the "
          "collective (locked) cancellation, i.e. prob:R1 itself"
          % (rB_s, class_of(rB_s), rB_x, class_of(rB_x)), True)
    gains = [w["dA_s"] / max(abs(w["dMF"] + w["detF"]), 1e-300)
             for w in comp]
    print("    measured collective-cancellation gain Delta_A_sch / "
          "|D(M,F)+det F|: min %.1f, median %.1f, max %.1f"
          % (float(np.min(gains)), float(np.median(gains)),
             float(np.max(gains))))
    eta_p, c_p, r2_p = ols_loglog(
        h_arr, np.array([w["kappa"] for w in comp]))
    print("    T-DET (uniform-constant form): |D(M,F)+det F| <= kappa "
          "det M with measured kappa_max = %.5f << 1; trend kappa ~ "
          "%.3g h^{%.3f} (R^2 = %.3f)" % (kap_max, c_p, eta_p, r2_p))

    best_class = min(class_of(rA_s), class_of(rC_s))  # 'a' < 'b' < 'c'

    # ============================================================== R4
    run_pret = best_class in ("a", "b") or use_s <= ENTRY_USE_BAR
    print("\nR4 -- tool sounding (pointwise CS always runs -- a win would "
          "be UNCONDITIONAL; pretentious scan gated on the weakened "
          "chain: %s)"
          % ("OPEN (T-ENTRY class (a); the scan probes what any "
             "entry-to-determinant route must exclude)" if run_pret
             else "CLOSED"))

    # ---- R4-ii: the five-line-win test (pointwise Cauchy-Schwarz) --------
    ref_r = ref_row["r"]
    detS_ref = ref_row["detS"]
    Xn = ref_r["Xn"]
    dX_at = Xn[:, 0] * Xn[:, 1] - Xn[:, 2] ** 2
    frac_neg_at = float(np.mean(dX_at < 0.0))
    ug = np.linspace(u0_cut, 2.0 * ref_r["alpha"] - 1e-9, N_GRID_CS)
    g11 = spline_read_vec(ref_r["W11"], ug, ref_r["D"])
    g22 = spline_read_vec(ref_r["W22"], ug, ref_r["D"])
    g12 = spline_read_vec(ref_r["W12"], ug, ref_r["D"])
    dX_gr = g11 * g22 - g12 ** 2
    frac_neg_gr = float(np.mean(dX_gr < 0.0))
    min_diag = min(float(np.min(g11)), float(np.min(g22)),
                   float(np.min(Xn[:, 0])), float(np.min(Xn[:, 1])))
    cs_win = (min(float(np.min(dX_at)), float(np.min(dX_gr)))
              >= -FLOAT_FLOOR) and (min_diag >= -FLOAT_FLOOR)
    psd_frac = float(np.mean((dX_at >= 0.0) & (Xn[:, 0] >= 0.0)
                             & (Xn[:, 1] >= 0.0)))
    nsd_frac = float(np.mean((dX_at >= 0.0) & (Xn[:, 0] <= 0.0)
                             & (Xn[:, 1] <= 0.0)))
    neg_mass = float(np.sum(ref_r["lam"] ** 2 * np.minimum(dX_at, 0.0)))
    check("R4.CS the five-line-win test on the reference window h = %d: "
          "pointwise W12^2 <= W11 W22 with W11, W22 >= 0 %s -- atom reads: "
          "%.1f%% have det X_n < 0 (min %.3e, rho^2-weighted negative "
          "diagonal mass %.3e vs det S = %.3f); dense grid: %.1f%% "
          "negative (min %.3e); min diagonal read %.3e.  %s"
          % (ref_r["h"],
             "HOLDS" if cs_win else "FAILS",
             100.0 * frac_neg_at, float(np.min(dX_at)), neg_mass,
             detS_ref,
             100.0 * frac_neg_gr, float(np.min(dX_gr)), min_diag,
             "CS-POINTWISE-WIN: det S >= 0 follows UNCONDITIONALLY by "
             "Cauchy-Schwarz" if cs_win else
             "CS-POINTWISE-FAIL: the three sums are NOT moments of one "
             "inner product w.r.t. the comb measure -- no five-line win; "
             "consistent with lem:wedge (near-rank-one reads of BOTH "
             "signs) and the v576 negative edge coefficient"),
          True)
    print("    wedge signature of the atom reads X_n: PSD %.1f%% / NSD "
          "%.1f%% / indefinite %.1f%%"
          % (100.0 * psd_frac, 100.0 * nsd_frac,
             100.0 * (1.0 - psd_frac - nsd_frac)))

    # ---- R4-i: the pretentious phase scan --------------------------------
    if run_pret:
        mid = next((w for w in comp if w["r"]["h"] == core.Q_H_REF),
                   comp[len(comp) // 2])
        pick, seen = [], set()
        for w in (comp[0], mid, comp[-1]):
            if w["r"]["h"] not in seen:
                seen.add(w["r"]["h"])
                pick.append(w)
        for w in pick:
            r = w["r"]
            uu, rho, Xw = r["uu"], r["lam"], r["Xn"]
            gam1 = 2.0 * math.pi / ((2 * r["h"] + 1) * r["D"])
            rx = [rho * Xw[:, j] for j in range(3)]

            def zvec(tau):
                ph = np.exp(1j * tau * uu)
                return np.array([complex(ph @ rx[0]),
                                 complex(ph @ rx[1]),
                                 complex(ph @ rx[2])])

            def lam_min_of(z):
                zv = np.stack([z.real, z.imag], axis=1)
                Qm = 0.5 * (np.outer(zv[0], zv[1])
                            + np.outer(zv[1], zv[0])) \
                    - np.outer(zv[2], zv[2])
                return lam_min_2x2(Qm[0, 0], Qm[1, 1], Qm[0, 1])

            taus = np.arange(0.0, TAU_MAX + TAU_STEP, TAU_STEP)
            zs = [zvec(t) for t in taus]
            vals = np.array([lam_min_of(z) for z in zs])
            i_min = int(np.argmin(vals))
            t_best, v_best = float(taus[i_min]), float(vals[i_min])
            t_ref = np.arange(max(0.0, t_best - REFINE_HALF),
                              t_best + REFINE_HALF, REFINE_STEP)
            v_ref = np.array([lam_min_of(zvec(t)) for t in t_ref])
            j = int(np.argmin(v_ref))
            t_best, v_best = float(t_ref[j]), float(v_ref[j])
            # nonnegative pretentious family rho (1 + cos(tau u + phi))/2
            phis = np.linspace(0.0, 2.0 * math.pi, N_PHI, endpoint=False)
            cph, sph = np.cos(phis), np.sin(phis)
            best_nn, t_nn, p_nn = np.inf, 0.0, 0.0
            for t, z in zip(taus, zs):
                rr = (cph[:, None] * z.real[None, :]
                      - sph[:, None] * z.imag[None, :])
                s1 = 0.5 * (w["S11"] + rr[:, 0])
                s2 = 0.5 * (w["S22"] + rr[:, 1])
                s3 = 0.5 * (w["S12"] + rr[:, 2])
                dv = s1 * s2 - s3 ** 2
                k = int(np.argmin(dv))
                if dv[k] < best_nn:
                    best_nn, t_nn, p_nn = float(dv[k]), float(t), \
                        float(phis[k])
            breaks = v_best < -FLOAT_FLOOR * max(1.0, w["detS"])
            breaks_nn = best_nn < -FLOAT_FLOOR * max(1.0, w["detS"])
            check("R4.PRET h = %d: worst PURE pretentious phase "
                  "rho_n cos(tau u_n + phi): min det = %.4f at tau = %.4f "
                  "(dual points gamma_1 = %.4f, gamma_2 = %.4f) -- "
                  "det/detS(true) = %.3f; worst NONNEGATIVE pretentious "
                  "measure rho_n(1+cos)/2: min det = %.4f at tau = %.4f, "
                  "phi = %.3f (det/detS = %.3f).  %s"
                  % (r["h"], v_best, t_best, gam1, 2.0 * gam1,
                     v_best / w["detS"], best_nn, t_nn, p_nn,
                     best_nn / w["detS"],
                     "PRET-BREAKS: even a NONNEGATIVE pretentious measure "
                     "drives det S < 0 -- positivity + size of the comb "
                     "CANNOT prove det S >= 0; the needed input is a "
                     "pretentious-distance statement (no n^{i tau} "
                     "correlation) at the dual points" if breaks_nn else
                     ("PRET-BREAKS(pure only): the unimodular twist "
                      "breaks the sign, the nonnegative family does not "
                      "-- positivity of the measure is load-bearing"
                      if breaks else
                      "PRET-ROBUST: no scanned phase breaks the sign")),
                  True)
    else:
        print("    [gate closed -- no pretentious scan]")

    # ============================================================== R5
    print("\n" + "=" * 78)
    print("R5 -- CONTRACT NOTE (chat report is the deliverable; no file "
          "outside experiments/ is touched)")
    print("=" * 78)
    print("""
  THE THREE FUNCTIONALS (exact; R1: formulas %.1e, assembly %.1e,
  mpmath reference %.1e):
    S_j(a) = sum_{p^m <= e^(2a)} (log p / p^(m/2)) What_j(m log p; a),
    What_j = two-point read of the closed parity weights
      W_11(d) = (2/N)[(N-d) cos(w1 d) + cot(w1) sin(w1 d)]
      W_22(d) = (2/N)[(N-d) cos(w2 d) + cot(w2) sin(w2 d)]
      W_12(d) = (2/N)[sin w2 sin(d w1) - sin w1 sin(d w2)]
                / (cos w1 - cos w2),   w_k = 2 pi k / (2h+1).

  THE TARGET INEQUALITY (load-bearing form T-B):
    det S(a) >= R_arch(a) = D(B,S)(a) - det B(a),
    margin det Ahat = (1 - r12^2) a11 a22,
    1 - r12^2 ~ %.3g h^{-%.3f} MEASURED (R^2 = %.3f); the literal form
    T-A (det S >= -c a^{-eta}) is trivially true (det S > 0, ~ e^a).

  THE CIRCULARITY VERDICT (Schoenfeld / exact-envelope):
    T-A  class (%s)/(%s)   [min det M/Delta_A  = %.4f / %.4f]
    T-PAIR class (%s)/(%s) [min det M/Delta_C  = %.4f / %.4f]
    T-B  class (%s)/(%s)   [min margin/Delta_B = %.2e / %.2e]
    T-ENTRY class (a)      [max budget usage   = %.4f / %.4f]
    T-DET uniform-constant form: kappa_max = %.5f (measured), the
    surviving det-level conjecture |D(M,F)+det F| <= kappa det M.
""" % (dev_w_all, res_asm_all, res_ex_closed,
       c_h, -eta_h, r2_h,
       class_of(rA_s), class_of(rA_x), rA_s, rA_x,
       class_of(rC_s), class_of(rC_x), rC_s, rC_x,
       class_of(rB_s), class_of(rB_x), rB_s, rB_x,
       use_s, use_x, kap_max))

    print("[TIME] %.1f s   [CHECKS] %d run, %d failed%s"
          % (time.time() - T0, N_CHK, len(FAILS),
             ("  " + ",".join(FAILS)) if FAILS else ""))
    return 0 if not FAILS else 1


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