#!/usr/bin/env python3
# -*- coding: utf-8 -*-
r"""v824 -- PRIME.FLOOR.SKELETON.01: the three-piece uniform certified lower-bound skeleton of the sector floor lambda tau on the deployed battery, with the deep-tail coherence repair closing Piece 1 at fixed citation depth for ALL h, ONE module from two probes (12/12 + 9/9 checks, ~35 s; discovery probes prime_floor_theorem_probe.py FLOOR-PARTIAL and prime_tail_envelope_probe.py TAIL-CLOSED-ALL-H, both 2026-08-06).  PART 1, THE ASSEMBLY (three pieces, honest grades): PIECE 2 UNIFORM-VERIFIED -- the ANALYTIC h -> infinity limit of the fixed pair, X_inf(alpha) = 16 alpha pi^2 sin(alpha gamma_1) sinh(alpha/2) [bracket], with the Dirichlet closed form == direct sum at 5.7e-13 and the closed-form pipeline reproducing the deployed X^2 at 8.5e-13; ZERO sign flips over 14 alphas x 10 synthetic h; the explicit bound L(h) = X_inf^2(alpha)(1 - C(alpha)/h) holds for h > h0(alpha) = C(alpha) (measured C(alpha) <= 499.6; 13/14 rungs in-domain, the excluded h = 149 stands on the per-rung certificate); honest limitation typed: 25 alpha-NODES of sin(alpha gamma_1) on [2, 12] (spacing pi/gamma_1 = 0.2223; deployed rungs keep |sin| >= 0.331) -- h-uniform per alpha, NOT ladder-uniform in alpha; at nodes the family takes over.  PIECE 3 UNIFORM-VERIFIED -- FAMILY EXHAUSTION: the pole family carries >= 0.99996 of the pair total on every rung; the certified top-100 (pole x zero) family bound carries median 0.952 (min 0.931, max 0.974) of the floor det Ahat2, all carriers on-line by verified computation (<= 2e4); the residue (the > 2e4 remainder's det share) is 0.015..0.049 with trend slope +0.010 (truncation depth, not a structural carrier); the ANALYTIC family limit F_inf(alpha) stays positive across all fixed-pair nodes (min 1.62e-4 at alpha = 2.68) -- the family never dies simultaneously.  PIECE 1 (per part 1) PER-RUNG-ONLY: the crude coherence-discarding envelope closes only 4/14 rungs (h in {184, 210, 218, 434}, T_need 5.9e12..9.9e14) -- superseded by part 2.  PART 2, THE TAIL CLOSURE (TAIL-CLOSED-ALL-H, 9/9): the autopsy names the coherence discard [D1] (sup|csinc|^2 x sup|FF| multiplies sups attained at OPPOSITE points -- F is coherent exactly at the alias points where the csinc weight VANISHES; measured band slack x2.7e3..2.4e5); the repair = the PRODUCT SUP of the true per-zero envelope (exact Dirichlet closed form, grid-converged with doubling ward + x1.35 slack; the rigorous Lipschitz certificate is a NAMED elementary step, typed not claimed) combined with Abel summation against RvM counting with EXPLICIT unconditional constants (Trudgian-grade |Q(t)| <= 0.112 log t + 0.278 loglog t + 3.4; factor 2.13); the VK zero-free region typed honestly USELESS (e^{-2 alpha/(57.54 ell)} ~ 0.985); THE CLOSURE: pert_c = 1.5e-9..8.4e-7 vs X^2 ~ 1e-3 -- ALL 14 rungs close at the FIXED verified horizon T_ver = 3e12 with margins 10^3..10^6, and the secondary top-100 family gate closes 14/14 too; THE LAW CHANGE (the asymptotic answer): log h coefficient +1.98 -> +0.13 (bar 0.25) -- the h e^alpha growth was an ARTIFACT of the coherence discard, the sharpened law is h-FREE at fixed alpha; the remaining growth is alpha-only (q = +1.79, the [A1] off-line allowance e^{2 alpha delta}), re-crossing X_inf^2 near the validity horizon alpha* ~ 11.2 -- far beyond the deployed battery (alpha <= 6.146) but finite and honestly typed.  NET: lambda tau = det Ahat2 >= X^2(pole, gamma_1) - pert(h) > 0 at citation grade on ALL 14 rungs -- no per-rung eigencheck, no zero-location input beyond the [A1] strip; exact SOS identity (v823) + analytic fixed-pair bound + certified 0.93..0.97 family exhaustion + psd remainder closed for all h up to alpha* ~ 11.  Controls fire in both parts (synthetic OFF-line pairs break the psd chain 4/4 exactly at the off-line locus AND stay inside both envelopes; the scramble kills the family structure, lambda_min(R_scr) = -686..-19; the regression ward reproduces the old {184, 210, 218, 434} closure boundary).  Feeds PRIME.FLOOR.RATIO.01 [O] (narrowed, NOT closed: the floor bound is battery-relative and alpha-bounded; necessary-side evidence -- the detector-inversion direction is where floor bounds convert to zero-exclusion).  NO RH claim.  Python-only per GATE.WOLFRAM.02.

PROVENANCE: discovery probes prime_floor_theorem_probe.py (2026-08-06,
12/12 checks, ~12 s, FLOOR-PARTIAL with grades P1 PER-RUNG-ONLY / P2
UNIFORM-VERIFIED / P3 UNIFORM-VERIFIED) + prime_tail_envelope_probe.py
(2026-08-06, 9/9 checks, ~18 s, TAIL-CLOSED-ALL-H); both re-run
identically at promotion.  Merged per the v518/v668 precedent: part 1
verbatim at module level (the probes' read-only parent import
'prime_lagrange_pair_probe as pp' resolves to v823_prime_lagrange_floor,
which carries the identical zero_list/components_of machinery at module
level; the probe's run() renamed _probe_run(); a _LAST1 verdict capture
inserted, v791 precedent); part 2 verbatim inside an isolated function
scope (its module-level names are function-local; its 'global'
declarations become 'nonlocal'; a _LAST2 verdict capture inserted);
numbers unchanged; run() encodes the expected patterns (v757
precedent).  DOWNSCOPING: none -- the deployed 14-window ladder and the
2e4 RS zero scan are rebuilt in-process per the v677/v772 precedent
(~35 s total, predeclared).

Original prime_floor_theorem_probe.py docstring (verbatim):
prime_floor_theorem_probe -- the floor-theorem assembly attempt.
Maximal honesty about which pieces reach theorem grade vs
verified-per-rung grade.

CONTEXT (prime_lagrange_pair_probe -> prime_lagrange_budget_probe,
PAIR-CERTIFIED): lambda tau = det Ahat2 >= det(G_Z+P) >= X^2(pole,g1)
per rung via the psd-remainder chain.  Three named gaps attacked here.

PIECE 1 -- PSD REMAINDER AS THEOREM.  Replace the per-rung eigencheck
  lambda_min(R) > 0 by the citation-grade decomposition
    R = R_band(2e4 < gamma <= 3e12; psd BY CITATION, zeros on-line
        by verified computation [Platt-Trudgian 2021])
      + R_deep(gamma > 3e12; UNKNOWN location inside the strip
        0 < Re s < 1 = [A1], worst-case displacement |delta| <= 1/2),
  with the EXPLICIT elementary envelope (per rung, all sums finite):
    E_i = 2 sum_j |t_ij| e^{(h-j-1/2) D/2}   (worst off-line leg),
    |layer entry| <= 4 cosh^2(D/4) E_i E_j / (D gamma^2),
    sum_{gamma > T} gamma^-2 <= (log T + 1)/(pi T)   (Abel with
      N(t) <= t log t / 2pi),
    eps_deep = 4 cosh^2(D/4) max(E_i E_j)/D x (log T_RH + 1)/(pi T_RH)
  plus the master-identity defect budget: eps_arch = the measured
  GL-48 -> GL-96 shift of the arch lags (x ARCH_SLACK) and float.
  Chain: det Ahat2 >= det(G_Z+P) - pert(eps_deep + eps_arch + eps_f),
  pert(e) = e (|a11|+|a22|+2|a12|) + 2 e^2.  PASS per rung iff
  pert < X^2.  KILL/type: where the envelope's h-growth beats X^2,
  the required citation horizon T_need(h) is printed -- the precise
  RH-content re-entry point (zero location beyond the strip is never
  used; only DEPTH of the verified on-line band).

PIECE 2 -- UNIFORM h-ASYMPTOTIC OF THE FIXED PAIR.  The exact closed
  form (Dirichlet kernels; hD = alpha exactly in the deployed frame):
    S_k(phi) = (1/sqrt N)[T1 - T2],  T_m = cos(c_m + s_m (h-1)/2)
               sin(h s_m / 2)/sin(s_m / 2),
    s_1 = om + phi, c_1 = om - (h-1/2) phi;  s_2 = om - phi,
    c_2 = om + (h-1/2) phi;  om = 2 pi k / N, N = 2h+1,
  and the ANALYTIC h -> infinity limit (derived, then verified):
    G_k(u)  = pi k sin(u) / (pi^2 k^2 - u^2),        u = alpha gamma_1,
    Gp_k    = pi k sinh(alpha/2) / (pi^2 k^2 + alpha^2/4),
    X_inf(alpha) = 8 alpha [G_1(u) Gp_2 - G_2(u) Gp_1]
      = 16 alpha pi^2 sin(u) sinh(alpha/2)
        [ ((pi^2-u^2)(4pi^2+alpha^2/4))^-1
        - ((4pi^2-u^2)(pi^2+alpha^2/4))^-1 ].
  Deliverable: L(h) = X_inf^2(alpha)(1 - C(alpha)/h) with C(alpha)
  measured on a synthetic h-ladder AT EACH DEPLOYED alpha (x2
  slack; C blows up near the sin-nodes), valid on the explicit
  domain h > h0(alpha) = C(alpha); wards: closed form == direct sum
  (machine), the limit matches all rungs within C(alpha)/h,
  L(h) <= measured X^2 in-domain.  KILL test: sign flips of X
  in h at fixed alpha (expected NONE); the sin(alpha gamma_1) factor
  gives alpha-NODES (spacing pi/gamma_1 ~ 0.2223) -- typed: the fixed
  pair is h-uniform per rung but canNOT be ladder-uniform in alpha;
  at nodes the family (piece 3) takes over.

PIECE 3 -- FAMILY EXTENSION (pole x zeros, the alias carriers).
  Per rung: the family total sum_k X_k^2 vs the pair total
  det(G_Z+P) (Lagrange split: det(G_Z+P) = det(G_Z) + pole-family
  total EXACTLY); the certified family fraction of the floor
  (top-K = 100, same chain, all carriers <= 2e4 on-line by
  computation); the residue 1 - det(G_Z+P)/det(Ahat2) (= the >2e4
  remainder's det contribution) share + trend -- exhaustion iff the
  residue is small with non-growing trend (it is truncation depth,
  not a structural carrier); the family limit F_inf(alpha) =
  sum_zeros X_inf^2(gamma_k; alpha) on a dense alpha grid: its
  minimum must stay positive across the fixed-pair nodes (the
  family never dies simultaneously).

CONTROLS: CT1 synthetic OFF-LINE zero pairs (frozen grid; a single
  off-line pair layer has det < 0 STRUCTURALLY -- Re of a complex
  rank-one -- though the negative eigenvalue can be subleading when
  the two parity legs share the dominant phase) -- the chain must
  break exactly at the off-line locus AND every layer must respect
  the piece-1 envelope; CT2 family/identity structure
  (lambda_min(R_scr) < 0, from the declared scramble); CT3 the L(h)
  ward (piece 2).

SYNTHESIS: per-piece status PROVEN / UNIFORM-VERIFIED /
  PER-RUNG-ONLY / DEAD (frozen rules in code), the strongest
  supported floor assertion verbatim, and the single separating
  object from tau > 0 for all h -- with the honesty note: floor
  positivity on the deployed battery is NECESSARY-side evidence,
  NOT RH; the detector-inversion direction is where floor bounds
  convert to zero-exclusion.

VERDICT (frozen): FLOOR-SKELETON-COMPLETE (all three pieces at least
  UNIFORM-VERIFIED) / FLOOR-PARTIAL (typed per piece) /
  FLOOR-BLOCKED (a piece DEAD, blocker named).

FIREWALL: v563 / v684 / v692 / parent probes READ-ONLY; zero values
used openly (on-line by computation <= 2e4, citation <= 3e12); RNG
only in v563's declared scramble.

Original prime_tail_envelope_probe.py docstring (verbatim):
prime_tail_envelope_probe -- attack the deep-tail envelope (piece 1).

CONTEXT (prime_floor_theorem_probe, FLOOR-PARTIAL): the citation-grade
psd-remainder chain closes only to h = 434 because the deep-tail
envelope eps_deep = 4 cosh^2(D/4) E_i E_j / D x (log T + 1)/(pi T)
grows ~ h e^alpha against the verified-zeros horizon T_ver = 3e12
(Platt-Trudgian), with T_need = 5.9e12 .. 9.9e14 on the failing rungs.

AUTOPSY (S1) -- where the h e^alpha enters.  Two triangle
inequalities discard structure:
  [D1] COHERENCE: |F_i(Dz)| <= E_i = 2 sum_j |t_ij| e^{(h-j-1/2) D
       delta} assumes all j-phases align.  They DO align -- but only
       at the alias points D gamma = 2 pi k, where the csinc weight
       sin^2(D gamma / 2) VANISHES.  Taking sup|csinc|^2 x sup|F F|
       independently multiplies two sups attained at OPPOSITE points.
  [D2] OSCILLATION ACROSS ZEROS: term-by-term absolute values.  With
       zero POSITIONS unknown beyond T_ver this is not exploitable
       unconditionally (recovering it = Landau/Gonek = the explicit
       formula again); measured on the accessible band and typed as
       unexploitable slack.
  The band [1e4, 2e4] (verified on-line zeros in hand) gives the
  measured slack decomposition per rung.

ATTACKS (S2; predeclared order a -> b -> c, stop at first success):
  (a) PARTIAL SUMMATION: Abel against the Riemann-von Mangoldt
      counting N(t) = (t/2pi) log(t/2pi e) + Q(t) with EXPLICIT
      unconditional |Q(t)| <= 0.112 log t + 0.278 loglog t + 3.4
      (Trudgian-2014-grade constants, rounded conservatively; the
      7/8 + arctan tail folded in).  Result:
        sum_{gamma > T} gamma^-2 <= (log(T/2pi) + 1)/(2 pi T)
                                    + (2 Q(T) + 0.2)/T^2
      -- an analytic factor ~ 2 (log T + 1)/(log(T/2pi) + 1) ~ 2.13
      over the crude N(t) <= t log t / 2pi chain.
  (b) UNCONDITIONAL ZERO-FREE REGION (Vinogradov-Korobov, Ford's
      explicit constant): beta <= 1 - 1/(57.54 ell(T)), ell =
      (log T)^{2/3} (loglog T)^{1/3}, so delta <= 1/2 - 1/(57.54
      ell(T)).  STRUCTURAL ANSWER TYPED: the tail couples to the
      displacement EXPONENTIALLY (e^{2 alpha delta}) -- it is NOT
      count-only -- but the explicit VK constant gives only
      e^{-2 alpha / (57.54 ell)} ~ 0.985: numerically useless.
  (c) THE PRODUCT SUP (kernel structure; the [D1] repair): bound the
      per-zero layer by its TRUE envelope
        |T_ij(gamma + i delta)| <= (4 / (D gamma^2)) NUM_ij,
        NUM_ij = sup_{u in [0,4pi), delta in [0,1/2]}
                 [ (sin^2(u/2) + sinh^2(D delta / 2))
                   x 4 |S_i(u + i D delta)| |S_j(u + i D delta)| ],
      exact via the Dirichlet closed form of S (periodic in u = D
      gamma with period 4pi; conjugate pair delta -> -delta gives
      |T| equal, so delta >= 0 suffices).  The sup is computed on a
      grid with a slack factor and a grid-doubling convergence ward
      (a fully rigorous Lipschitz/Bernstein certificate of the sup
      is a named elementary step -- typed, not claimed).  Combined
      with (a):  eps_c = (4/D) NUM_max x shSum(T_ver).

CLOSURE MAP (S3): per rung old/new pert vs X^2(pole, gamma_1)
  (frozen primary gate, comparable to the parent probe) and vs the
  top-100 family sum (secondary, the stronger floor); which attack
  closes each rung; new T_need for any still failing; the strongest
  uniform floor theorem now supported, verbatim, all inputs named.

ASYMPTOTIC QUESTION (S4, frozen): fit log(eps s1) on (log h, alpha)
  for the old and sharpened envelopes -- does the LAW change from
  ~ h e^alpha to h-free, or only the constant?  If h-free (|log h
  coefficient| <= 0.25) AND all rungs close at T_ver: a fixed T_ver
  covers all h at the deployed alphas -- state loudly, with full
  honesty (necessary-side floor on a frozen battery, NOT RH).
  The alpha-horizon alpha* of the sharpened law is typed.

CONTROLS: CTA envelope wards on the accessible band (every envelope,
  crude and sharpened, at delta = 0, must majorize the measured band
  sums; the Abel bound must majorize the measured band zero-count
  sum); CTB regression (the old envelope must reproduce the h = 434
  closure boundary, set {184, 210, 218, 434}); CTC the synthetic
  off-line control (unchanged grid, must fire).

VERDICT (frozen): TAIL-CLOSED-ALL-H (all 14 rungs close at T_ver AND
  the sharpened law is h-free -- floor theorem complete on the
  deployed battery; alpha* typed) / TAIL-EXTENDED (boundary pushed
  past h = 434, new h_max typed) / TAIL-IRREDUCIBLE (envelope tight;
  the verification-depth dependence is the honest boundary).

FIREWALL: v563 / v684 / v692 / parent probes READ-ONLY; zeros used
openly; RNG none.
"""

import math
import os
import sys
import time

import numpy as np

_here = os.path.dirname(os.path.abspath(__file__))
for _cand in (os.path.join(_here, "..", "..", "verification"), _here):
    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 v684_rank3_zeroside as zp             # noqa: E402 (READ-ONLY)
import v692_rank3_lockgram as lg             # noqa: E402 (READ-ONLY)
import v823_prime_lagrange_floor as pp       # noqa: E402 (READ-ONLY;
#   carries the probes' parent machinery zero_list/components_of)

T0 = time.time()
FAILS = []
N_CHK = 0
_LAST1 = {}
_LAST2 = {}

# ------------------------------------------------- frozen bars / constants
T_RH = 3.0e12           # Platt-Trudgian 2021 (cited on-line horizon)
DELTA_OFF = 0.5         # worst-case off-line displacement ([A1] strip)
GL_FINE = 96            # refined arch quadrature (vs deployed 48)
ARCH_SLACK = 10.0       # slack on the measured GL shift as eps_arch
CHAIN_FAC = 100.0       # float chain budget factor
WARD_CF = 1.0e-9        # closed form vs direct sum, relative
SYN_HS = (128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536)
C_SLACK = 2.0           # slack on the measured 1/h-correction constant
K_FAM = 100             # certified family size
FAM_PAIR_TOT = 0.999    # pole family share of the pair total
RES_MAX = 0.06          # residue share bar (exhaustion)
RES_SLOPE_MAX = 0.05    # residue trend bar (vs log h)
FAM_CERT_MED = 0.90     # median certified family fraction of floor
N_GRID = 2001           # alpha grid for the node / family-limit scan
ALPHA_LO, ALPHA_HI = 2.0, 12.0
N_ZLIM = 2000           # zeros in the family-limit sum
# CT1 off-line control grid (calibrated once, then frozen): a single
# off-line pair layer is asymptotically degenerate-indefinite (det < 0
# structurally but the negative eigenvalue can be subleading), so the
# control uses a small grid and fires on the worst configuration
CT1_GRID = ((20.0, 0.3), (20.0, 0.45), (80.0, 0.3), (80.0, 0.45))
CT1_NEG = -1.0e-3       # worst min-eig/scale must be below this
SCRAMBLE_SEED = 20260806
EPSM = float(np.finfo(float).eps)
EPS_JS = 1.0e-300
G1REF = 14.134725141734695   # gamma_1 (cache-verified vs zetazero(1))


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 eig2_min(M):
    tr = M[0, 0] + M[1, 1]
    dif = M[0, 0] - M[1, 1]
    rad = math.sqrt(0.25 * dif * dif + M[0, 1] * M[0, 1])
    return 0.5 * tr - rad


# ---------------------------------------------------------- closed forms
def parity_t(k, h):
    N = 2 * h + 1
    jj = np.arange(h)
    return (2.0 / math.sqrt(N)) * np.sin(
        2.0 * math.pi * k * (jj + 1.0) / N)


def S_direct(k, h, D, g):
    jj = np.arange(h)
    return float(parity_t(k, h) @ np.sin((h - jj - 0.5) * D * g))


def S_closed(k, h, D, g):
    """Exact Dirichlet-kernel closed form of S_k (piece 2)."""
    N = 2 * h + 1
    om = 2.0 * math.pi * k / N
    phi = D * g
    out = 0.0
    for sgn in (1.0, -1.0):
        s = om + sgn * phi
        c = om - sgn * (h - 0.5) * phi
        if abs(math.sin(0.5 * s)) < 1e-14:
            term = h * math.cos(c)          # degenerate Dirichlet
        else:
            term = (math.cos(c + 0.5 * s * (h - 1.0))
                    * math.sin(0.5 * h * s) / math.sin(0.5 * s))
        out += sgn * term
    return out / math.sqrt(N)


def pair_x2_closed(alpha, h, g1):
    """X^2(pole, gamma_1) from the exact closed forms (any alpha, h)."""
    D = alpha / h
    phi = D * g1
    wg = D * (math.sin(0.5 * phi) / (0.5 * phi)) ** 2
    a1 = 2.0 * math.sqrt(wg) * S_closed(1, h, D, g1)
    b1 = 2.0 * math.sqrt(wg) * S_closed(2, h, D, g1)
    jj = np.arange(h)
    e = np.sinh((h - jj - 0.5) * D / 2.0)
    sp1 = float(parity_t(1, h) @ e)
    sp2 = float(parity_t(2, h) @ e)
    cp = 2.0 * math.sqrt(D) * (math.sinh(D / 4.0) / (D / 4.0))
    p1, p2 = cp * sp1, cp * sp2
    x = a1 * p2 - p1 * b1
    return x * x, x


def x_inf(alpha, g):
    """The ANALYTIC h -> infinity limit of X(pole, gamma) (piece 2)."""
    u = alpha * g
    pi2 = math.pi ** 2
    a24 = 0.25 * alpha * alpha
    br = (1.0 / ((pi2 - u * u) * (4.0 * pi2 + a24))
          - 1.0 / ((4.0 * pi2 - u * u) * (pi2 + a24)))
    return (16.0 * alpha * pi2 * math.sin(u)
            * math.sinh(0.5 * alpha) * br)


# ------------------------------------------------- refined arch (piece 1)
_GX96, _GW96 = np.polynomial.legendre.leggauss(GL_FINE)


def arch_A_fine(sv, D):
    """core.arch_A re-evaluated with GL-96 nodes (identical integrand)."""
    sv = np.abs(np.asarray(sv, dtype=float))
    out = np.empty(sv.shape[0])
    far = sv >= D
    if far.any():
        s = sv[far].reshape(-1, 1)
        acc = np.zeros(s.shape[0])
        for lo, hi in ((s - D, s), (s, s + D)):
            mid, half = 0.5 * (lo + hi), 0.5 * (hi - lo)
            w = mid + half * _GX96[None, :]
            val = ((1.0 - np.abs(s - w) / D) * np.exp(-0.5 * w)
                   / (-np.expm1(-2.0 * w)))
            acc -= half[:, 0] * (val @ _GW96)
        out[far] = acc
    for i in np.nonzero(~far)[0]:
        s = float(sv[i])
        tri_s = max(0.0, 1.0 - s / D)
        W = s + D
        pts = sorted({0.0, s, D - s, W})
        pts = [p for p in pts if 0.0 <= p <= W]
        tot = 0.0
        for lo, hi in zip(pts[:-1], pts[1:]):
            if hi <= lo:
                continue
            mid, half = 0.5 * (lo + hi), 0.5 * (hi - lo)
            w = mid + half * _GX96
            tri = np.maximum(0.0, 1.0 - np.abs(s - w) / D)
            trr = np.maximum(0.0, 1.0 - np.abs(s + w) / D)
            val = ((tri_s * np.exp(-2.0 * w)
                    - 0.5 * (tri + trr) * np.exp(-0.5 * w))
                   / (-np.expm1(-2.0 * w)))
            tot += half * float(np.dot(_GW96, val))
        out[i] = (-(core.EULER + core.LOG_PI) * tri_s + 2.0 * tot
                  + tri_s * (-math.log1p(-math.exp(-2.0 * W))))
    return out


def _probe_run():
    global N_CHK, FAILS
    N_CHK = 0
    FAILS = []
    print("=" * 78)
    print("THE FLOOR-THEOREM ASSEMBLY -- three pieces, honest grades")
    print("(part 1 == prime_floor_theorem_probe verbatim, no RH claim)")
    print("=" * 78)

    # ============================================================== S0
    print("\nS0 -- rebuild (parent machinery)")
    gam, n_rvm = pp.zero_list()
    check("S0.Z zero list: %d zeros to T = %.0f (RvM dev %.2f <= 3)"
          % (len(gam), zp.T_SCAN, abs(len(gam) - n_rvm)),
          abs(len(gam) - n_rvm) <= 3.0)
    KZ = core.frame_a_zones()
    L15 = len(KZ)
    fam5 = [0, (L15 - 1) // 4, L15 // 2, (3 * (L15 - 1)) // 4, L15 - 1]
    inter = []
    for (lo_i, hi_i), n_in in zip(zip(fam5[:-1], fam5[1:]), (2, 3, 3, 2)):
        for j in range(1, n_in + 1):
            inter.append(lo_i + j * (hi_i - lo_i) // (n_in + 1))
    idx15 = sorted(set(fam5 + inter))
    wins = [lg.lock_block(KZ[i]) for i in idx15]
    wins = [w for w in wins if w["complete"]]
    wins.sort(key=lambda w: w["alpha"])
    g1 = float(gam[0])
    for w in wins:
        a, b, meta = pp.components_of(w, gam)
        M2 = np.array([[float(a @ a), float(a @ b)],
                       [float(a @ b), float(b @ b)]])
        x = a[0] * b[-1] - a[-1] * b[0]
        w.update(a=a, b=b, meta=meta, M2=M2,
                 det_m2=float(np.linalg.det(M2)),
                 det_a2=float(np.linalg.det(w["A2"])),
                 x2=float(x * x))
    print("    %d windows; gamma_1 = %.15f (cache; |cache - "
          "zetazero(1)| = 5.3e-15 measured in the parent probe)"
          % (len(wins), g1))

    # ========================================================== PIECE 1
    print("\nP1 -- PSD REMAINDER AS THEOREM (citation band + explicit "
          "deep envelope)")
    print("    %5s %6s | %9s %9s %9s | %9s %11s | %6s %9s"
          % ("h", "alpha", "eps_deep", "eps_arch", "eps_f", "pert",
             "X^2", "close?", "T_need"))
    n_close, close_flags = 0, []
    for w in wins:
        D, hz, Mz, al = w["D"], w["h"], w["M"], w["alpha"]
        jj = np.arange(hz)
        t1v = parity_t(1, hz)
        t2v = parity_t(2, hz)
        grow = np.exp((hz - jj - 0.5) * D * DELTA_OFF)
        E1 = 2.0 * float(np.abs(t1v) @ grow)
        E2 = 2.0 * float(np.abs(t2v) @ grow)
        ee = max(E1 * E1, E1 * E2, E2 * E2)
        ch2 = math.cosh(D / 4.0) ** 2
        eps_deep = 4.0 * ch2 * ee / D \
            * (math.log(T_RH) + 1.0) / (math.pi * T_RH)
        # master-identity defect: measured GL-48 -> GL-96 arch shift
        c48 = core.arch_lags(Mz, D)
        c96_idx = np.arange(Mz) * D
        c96 = arch_A_fine(c96_idx, D)
        dc = c96 - c48
        W11 = core.lag_weights_from_v(t1v, hz)
        W22 = core.lag_weights_from_v(t2v, hz)
        Wpp = core.lag_weights_from_v(t1v + t2v, hz)
        W12 = 0.5 * (Wpp - W11 - W22)
        dA2 = max(abs(float(dc @ W11)), abs(float(dc @ W22)),
                  abs(float(dc @ W12)))
        eps_arch = ARCH_SLACK * dA2
        eps_f = CHAIN_FAC * EPSM * float(np.linalg.norm(w["A2"])) ** 2
        eps = eps_deep + eps_arch + eps_f
        s1 = (abs(w["A2"][0, 0]) + abs(w["A2"][1, 1])
              + 2.0 * abs(w["A2"][0, 1]))
        pert = eps * s1 + 2.0 * eps * eps
        close = pert < w["x2"]
        close_flags.append(close)
        n_close += int(close)
        t_need = float("nan")
        if not close:
            # smallest citation horizon that would close this rung
            tt = T_RH
            for _ in range(400):
                tt *= 1.25
                e_d = 4.0 * ch2 * ee / D \
                    * (math.log(tt) + 1.0) / (math.pi * tt)
                e_t = e_d + eps_arch + eps_f
                if e_t * s1 + 2.0 * e_t * e_t < w["x2"]:
                    t_need = tt
                    break
        w.update(pert=pert, eps_deep=eps_deep, eps_arch=eps_arch,
                 p1_close=close, t_need=t_need)
        print("    %5d %6.3f | %9.2e %9.2e %9.2e | %9.2e %11.4e | "
              "%6s %9s"
              % (hz, al, eps_deep, eps_arch, eps_f, pert, w["x2"],
                 "yes" if close else "NO",
                 ("-" if close else "%.1e" % t_need)))
    p1_uniform = n_close == len(wins)
    if p1_uniform:
        p1_grade = "UNIFORM-VERIFIED"
    elif n_close > 0:
        p1_grade = "PER-RUNG-ONLY (beyond h of rung %d)" \
            % ([w["h"] for w, c in zip(wins, close_flags) if c][-1])
    else:
        p1_grade = "PER-RUNG-ONLY (envelope never closes)"
    check("P1.CLOSE the citation-grade chain pert < X^2 closes on "
          "%d/%d rungs -- %s; the deep envelope uses ONLY the [A1] "
          "strip + the cited on-line depth (no zero-location input); "
          "where it fails, the printed T_need is the exact "
          "citation-depth re-entry point"
          % (n_close, len(wins), p1_grade), n_close > 0)

    # ========================================================== PIECE 2
    print("\nP2 -- UNIFORM h-ASYMPTOTIC of the fixed pair")
    # ward 1: closed form == direct sum on all rungs
    cf_dev = 0.0
    for w in wins:
        for k in (1, 2):
            sc = S_closed(k, w["h"], w["D"], g1)
            sd = S_direct(k, w["h"], w["D"], g1)
            cf_dev = max(cf_dev, abs(sc - sd) / max(abs(sd), 1e-12))
    check("P2.CF the Dirichlet closed form of S_k equals the direct "
          "sum on all rungs (max rel dev %.1e <= %.0e)"
          % (cf_dev, WARD_CF), cf_dev <= WARD_CF)
    # ward 2: the closed-form pipeline reproduces the measured X^2
    pipe_dev = max(abs(pair_x2_closed(w["alpha"], w["h"], g1)[0]
                       - w["x2"]) / w["x2"] for w in wins)
    check("P2.PIPE the closed-form pair pipeline reproduces the "
          "deployed X^2 on all rungs (max rel dev %.1e <= 1e-6)"
          % pipe_dev, pipe_dev <= 1e-6)
    # synthetic h-ladder study AT EACH DEPLOYED alpha: the 1/h
    # correction constant C(alpha) (it blows up near the sin-nodes),
    # sign stability in h, and the per-alpha uniform bound
    print("    synthetic h-ladders at the deployed alphas "
          "(h in %d..%d):" % (SYN_HS[0], SYN_HS[-1]))
    print("    %5s %6s | %8s %8s | %10s %10s | %6s %6s"
          % ("h", "alpha", "C(alpha)", "h0", "L(h)", "X^2",
             "in-dom", "L<=X2"))
    flips, lim_ok, lh_ok, n_dom = 0, True, True, 0
    for w in wins:
        al = w["alpha"]
        xi = x_inf(al, g1)
        c_al, signs = 0.0, []
        for h_s in SYN_HS:
            x2s, xs = pair_x2_closed(al, h_s, g1)
            c_al = max(c_al, h_s * abs(x2s / xi ** 2 - 1.0))
            signs.append(math.copysign(1.0, xs))
        flips += sum(1 for s0, s1_ in zip(signs[:-1], signs[1:])
                     if s0 != s1_)
        c_al *= C_SLACK
        h0_al = c_al
        dev_h = abs(w["x2"] / xi ** 2 - 1.0) * w["h"]
        lim_ok = lim_ok and dev_h <= c_al
        lh = xi ** 2 * (1.0 - c_al / w["h"])
        in_dom = w["h"] > h0_al
        if in_dom:
            n_dom += 1
            lh_ok = lh_ok and (lh <= w["x2"]) and lh > 0.0
        w.update(lh=lh, c_al=c_al, h0_al=h0_al, in_dom=in_dom)
        print("    %5d %6.3f | %8.1f %8.0f | %10.3e %10.4e | "
              "%6s %6s"
              % (w["h"], al, c_al, h0_al, lh, w["x2"],
                 "yes" if in_dom else "no",
                 "ok" if (not in_dom or lh <= w["x2"]) else "NO"))
    check("P2.SIGN no sign flips of X in h at fixed alpha on the "
          "synthetic ladders (%d flips over 14 alphas x %d h) -- "
          "the KILL branch does NOT fire in h"
          % (flips, len(SYN_HS)), flips == 0)
    check("P2.LIM the ANALYTIC limit X_inf^2(alpha) matches the "
          "deployed X^2 within the per-alpha measured 1/h "
          "correction (x%.0f slack) on all rungs" % C_SLACK, lim_ok)
    small = [w["h"] for w in wins if not w["in_dom"]]
    c_l = max(w["c_al"] for w in wins)
    h0 = max(w["h0_al"] for w in wins)
    check("P2.L(h) the per-alpha uniform bound L(h) = "
          "X_inf^2(alpha)(1 - C(alpha)/h) is positive and below "
          "the measured X^2 on every rung in its explicit domain "
          "h > h0(alpha) (%d/%d rungs in-domain; excluded small "
          "rungs %s stand on the per-rung certificate) (CT3 ward)"
          % (n_dom, len(wins), small), lh_ok and n_dom >= 10)
    # the alpha-node structure (typed, the honest limitation)
    aa = np.linspace(ALPHA_LO, ALPHA_HI, N_GRID)
    xg = np.array([x_inf(al, g1) ** 2 for al in aa])
    thr = 1e-3 * float(np.median(xg))
    nodes = int(np.sum((xg[1:-1] < xg[:-2]) & (xg[1:-1] < xg[2:])
                       & (xg[1:-1] < thr)))
    min_sin = min(abs(math.sin(w["alpha"] * g1)) for w in wins)
    print("    ALPHA-NODES (typed): X_inf^2 has %d near-zeros on "
          "alpha in [%.0f, %.0f] (sin(alpha gamma_1) nodes, spacing "
          "pi/gamma_1 = %.4f); deployed rungs keep |sin| >= %.3f -- "
          "the fixed pair is h-UNIFORM per rung but NOT "
          "ladder-uniform in alpha; at nodes the family takes over"
          % (nodes, ALPHA_LO, ALPHA_HI, math.pi / g1, min_sin))
    p2_grade = ("UNIFORM-VERIFIED (per-alpha, h > h0(alpha) <= %.0f)"
                % h0
                if (cf_dev <= WARD_CF and pipe_dev <= 1e-6
                    and lim_ok and flips == 0
                    and lh_ok and n_dom >= 10) else "PER-RUNG-ONLY")

    # ========================================================== PIECE 3
    print("\nP3 -- FAMILY EXTENSION (pole x zeros)")
    print("    %5s %6s | %8s %8s %8s | %8s %8s | %8s"
          % ("h", "alpha", "fam/pt", "cert100", "cert/fl", "zz/pt",
             "residue", "med_alias"))
    fam_ok, res_list, cert_fracs, lam_r_min = True, [], [], np.inf
    for w in wins:
        a, b = w["a"], w["b"]
        x_pz = a[:-1] * b[-1] - b[:-1] * a[-1]
        fam_tot = float(np.sum(x_pz ** 2))
        fam_share_pt = fam_tot / w["det_m2"]
        zz_share = 1.0 - fam_share_pt
        top = np.sort(x_pz ** 2)[::-1]
        cert100 = float(np.sum(top[:K_FAM]))
        # per-rung chain (the PAIR-CERTIFIED grade): psd remainder
        # verified per rung + the float budget -- NOT piece 1's
        # citation-grade pert (that distinction is piece 1's grade)
        lam_r = eig2_min(w["A2"] - w["M2"])
        lam_r_min = min(lam_r_min, lam_r)
        bud_pr = CHAIN_FAC * EPSM * (
            float(np.linalg.norm(w["A2"])) ** 2
            + float(np.linalg.norm(w["M2"])) ** 2)
        cert_frac = (cert100 - bud_pr) / w["det_a2"]
        residue = 1.0 - w["det_m2"] / w["det_a2"]
        res_list.append(residue)
        cert_fracs.append(cert_frac)
        fam_ok = fam_ok and fam_share_pt >= FAM_PAIR_TOT
        # alias identity of the top-10 family carriers
        idx10 = np.argsort(x_pz ** 2)[::-1][:10]
        r = gam[idx10] * w["D"] / (2.0 * math.pi)
        med_alias = float(np.median(np.abs(r - np.round(r))))
        print("    %5d %6.3f | %8.5f %8.5f %8.4f | %8.1e %8.4f | "
              "%8.4f"
              % (w["h"], w["alpha"], fam_share_pt,
                 cert100 / w["det_m2"], cert_frac, zz_share,
                 residue, med_alias))
    sl_res = float(np.polyfit(np.log([w["h"] for w in wins]),
                              res_list, 1)[0])
    exhaust = (max(res_list) <= RES_MAX
               and sl_res <= RES_SLOPE_MAX and fam_ok)
    check("P3.EXH the pole family carries >= %.3f of the pair total "
          "on every rung; the residue (the >2e4 remainder's det "
          "share) is %.4f..%.4f with trend slope %+.3f vs log h "
          "(bars %.2f / %.2f) -- %s"
          % (FAM_PAIR_TOT, min(res_list), max(res_list), sl_res,
             RES_MAX, RES_SLOPE_MAX,
             "EXHAUSTION: the floor reduces to pieces 1+2 plus the "
             "family sum" if exhaust else
             "persistent residue (typed)"), exhaust)
    check("P3.CERT the certified top-%d family bound (per-rung "
          "chain: lambda_min(R) = %.1e >= 0 on all rungs, float "
          "budget only) carries a median %.3f of the floor (bar >= "
          "%.2f; min %.3f), all carriers on-line by computation"
          % (K_FAM, lam_r_min, float(np.median(cert_fracs)),
             FAM_CERT_MED, min(cert_fracs)),
          float(np.median(cert_fracs)) >= FAM_CERT_MED
          and lam_r_min >= 0.0)
    # the family limit across the fixed-pair nodes
    gz_lim = np.asarray(gam[:N_ZLIM], dtype=float)
    f_min, f_min_al = float("inf"), float("nan")
    for al in aa[::4]:
        u = al * gz_lim
        pi2 = math.pi ** 2
        a24 = 0.25 * al * al
        br = (1.0 / ((pi2 - u * u) * (4.0 * pi2 + a24))
              - 1.0 / ((4.0 * pi2 - u * u) * (pi2 + a24)))
        xv = (16.0 * al * pi2 * np.sin(u)
              * math.sinh(0.5 * al) * br)
        fv = float(np.sum(xv * xv))
        if fv < f_min:
            f_min, f_min_al = fv, float(al)
    check("P3.FAM-LIM the ANALYTIC family limit F_inf(alpha) = "
          "sum_k X_inf^2(gamma_k) stays positive across the "
          "fixed-pair nodes (min %.3e at alpha = %.2f > 0): the "
          "family never dies simultaneously" % (f_min, f_min_al),
          f_min > 0.0)
    p3_grade = ("UNIFORM-VERIFIED" if (exhaust and f_min > 0.0
                and float(np.median(cert_fracs)) >= FAM_CERT_MED)
                else "PER-RUNG-ONLY")

    # ========================================================== CONTROLS
    print("\nCT -- controls")
    w_mid = wins[len(wins) // 2]
    D, hz = w_mid["D"], w_mid["h"]
    jj = np.arange(hz)
    worst_rel, n_detneg, env_ok = 0.0, 0, True
    for gs, de in CT1_GRID:
        zc = complex(gs, de)
        L_off = np.empty((2, 2))
        pairs = {(0, 0): (w_mid["f1"], w_mid["f1"]),
                 (1, 1): (w_mid["f2"], w_mid["f2"]),
                 (0, 1): (w_mid["f1"], w_mid["f2"])}
        for (i, j), (fa, fb) in pairs.items():
            tp = lg.T_pair(fa, fb, D, np.array([zc, np.conj(zc)]))
            L_off[i, j] = L_off[j, i] = float(np.real(np.sum(tp)))
        sc = float(np.max(np.abs(L_off)))
        worst_rel = min(worst_rel, eig2_min(L_off) / sc)
        n_detneg += int(float(np.linalg.det(L_off)) < 0.0)
        grow_s = np.exp((hz - jj - 0.5) * D * de)
        E1s = 2.0 * float(np.abs(parity_t(1, hz)) @ grow_s)
        E2s = 2.0 * float(np.abs(parity_t(2, hz)) @ grow_s)
        env = 8.0 * math.cosh(D / 4.0) ** 2 \
            * max(E1s * E1s, E1s * E2s, E2s * E2s) / (D * gs * gs)
        env_ok = env_ok and sc <= env
    check("CT1 [must-fire] synthetic OFF-line pairs (grid %s) break "
          "the psd chain exactly at the off-line locus: det < 0 on "
          "%d/%d configurations (the structural signature) and the "
          "worst min-eig/scale = %.2f <= %.0e; all layers respect "
          "the piece-1 envelope: %s"
          % (str(CT1_GRID), n_detneg, len(CT1_GRID), worst_rel,
             CT1_NEG, "ok" if env_ok else "VIOLATED"),
          n_detneg == len(CT1_GRID) and worst_rel <= CT1_NEG
          and env_ok)
    lam_scr = []
    for si in (0, len(wins) // 2, len(wins) - 1):
        w = wins[si]
        rr_s = core.build_window(w["kz"], scramble_seed=SCRAMBLE_SEED)
        lam_scr.append(eig2_min(rr_s["Ah_dir"] - w["M2"]))
    check("CT2 [must-fire] scramble kills the family/identity "
          "structure: lambda_min(R_scr) = %.1f..%.1f < 0 (the psd "
          "chain refuses the scrambled comb)"
          % (min(lam_scr), max(lam_scr)), max(lam_scr) < 0.0)

    # ============================================================== V
    print("\n" + "=" * 78)
    print("V -- synthesis: the theorem-status table + verdict")
    print("=" * 78)
    grades = {"P1 psd remainder": p1_grade,
              "P2 uniform h-asymptotic": p2_grade,
              "P3 family extension": p3_grade}
    all_uniform = all(g.startswith("UNIFORM") for g in grades.values())
    any_dead = any(g == "DEAD" for g in grades.values())
    if all_uniform:
        verdict = "FLOOR-SKELETON-COMPLETE"
    elif any_dead:
        verdict = "FLOOR-BLOCKED"
    else:
        verdict = "FLOOR-PARTIAL"
    _LAST1["verdict"] = verdict
    _LAST1["grades"] = (p1_grade, p2_grade, p3_grade)
    print("\n  THEOREM-STATUS TABLE:")
    for k_, v_ in grades.items():
        print("    %-28s %s" % (k_, v_))
    hs_close = [w["h"] for w, c in zip(wins, close_flags) if c]
    print("""
  VERDICT: %s

  STRONGEST SUPPORTED FLOOR ASSERTION (verbatim):
    'On every rung of the deployed ladder, lambda tau = det Ahat2
     >= sum over the top-%d (pole x zero) pairs of X_k^2 minus the
     explicit budget pert(h): a certified %.2f..%.2f of the floor,
     all carriers on-line by verified computation (<= 2e4).  The
     fixed pair (pole, gamma_1) satisfies X^2(alpha, h) >=
     X_inf^2(alpha)(1 - C(alpha)/h) for h > h0(alpha) = C(alpha)
     (measured C(alpha) <= %.1f, worst h0 = %.0f), X_inf ANALYTIC
     (= 16 alpha pi^2 sin(alpha gamma_1) sinh(alpha/2) [bracket]);
     h-uniform at every deployed alpha in-domain.  The chain
     upgrades from per-rung eigencheck to
     citation-grade (strip + on-line depth only) on rungs with h <=
     %s, where the deep-tail envelope closes: pert < X^2.'

  THE SINGLE SEPARATING OBJECT from tau > 0 for all h:
    the deep-tail envelope grows ~ h e^alpha against the FIXED
    citation horizon T_RH = 3e12 -- beyond the printed T_need the
    psd of the remainder needs verified on-line DEPTH (never
    location outside the strip); plus the alpha-nodes of the fixed
    pair, where the certified brick must hand over to the family
    (P3: the analytic family limit stays positive, min %.1e).
    HONESTY: floor positivity on the deployed battery is
    NECESSARY-side evidence, NOT RH; the detector-inversion
    direction is where floor bounds convert to zero-exclusion.
""" % (verdict, K_FAM, min(cert_fracs), max(cert_fracs), c_l, h0,
       (str(max(hs_close)) if hs_close else "NONE"), f_min))

    dt = time.time() - T0
    print("-" * 78)
    print("checks: %d run, %d failed%s | runtime %.1f min"
          % (N_CHK, len(FAILS),
             (" [" + ", ".join(FAILS) + "]") if FAILS else "",
             dt / 60.0))
    print("NO RH claim (part 1).")


# ======================================================================
# PART 2 -- prime_tail_envelope_probe.py verbatim, isolated scope
# (its module-level names are function-local; its 'global' declarations
# become 'nonlocal'; pp = v823, v791/v818 precedent)
# ======================================================================
def _part2():
    T0 = time.time()
    FAILS = []
    N_CHK = 0

    # --------------------------------------------- frozen bars / constants
    T_VER = 3.0e12          # Platt-Trudgian verified on-line horizon
    BAND = (1.0e4, 2.0e4)   # accessible band for the measured-slack autopsy
    Q_A, Q_B, Q_C = 0.112, 0.278, 3.4   # |N - main| <= Q (Trudgian-grade)
    C_VK = 57.54            # Ford's explicit VK zero-free constant (cited)
    N_U = 65536             # u-grid for the product sup (period 4 pi)
    N_DELTA = 33            # delta-grid on [0, 1/2]
    SUP_SLACK = 1.35        # slack on the grid sup (Bernstein-style)
    GRID_CONV = 0.02        # grid-doubling convergence bar for the sup
    CHAIN_FAC = 100.0       # float chain budget factor
    H_EXP_BAR = 0.25        # |log h coefficient| bar for the h-free law
    K_FAM = 100             # secondary gate: top-K family sum
    ALPHA_STAR_MAX = 60.0   # horizon scan limit
    CT1_GRID = ((20.0, 0.3), (20.0, 0.45), (80.0, 0.3), (80.0, 0.45))
    CT1_NEG = -1.0e-3
    OLD_CLOSE_SET = {184, 210, 218, 434}   # CTB regression target
    EPSM = float(np.finfo(float).eps)

    def check(name, ok, detail=""):
        nonlocal 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 eig2_min(M):
        tr = M[0, 0] + M[1, 1]
        dif = M[0, 0] - M[1, 1]
        rad = math.sqrt(0.25 * dif * dif + M[0, 1] * M[0, 1])
        return 0.5 * tr - rad

    def parity_t(k, h):
        N = 2 * h + 1
        jj = np.arange(h)
        return (2.0 / math.sqrt(N)) * np.sin(
            2.0 * math.pi * k * (jj + 1.0) / N)

    def S_closed_c(k, h, phi):
        """Dirichlet closed form of S_k at COMPLEX phi (vectorized)."""
        N = 2 * h + 1
        om = 2.0 * math.pi * k / N
        out = np.zeros_like(phi, dtype=complex)
        for sgn in (1.0, -1.0):
            s = om + sgn * phi
            c = om - sgn * (h - 0.5) * phi
            out += sgn * (np.cos(c + 0.5 * s * (h - 1.0))
                          * np.sin(0.5 * h * s) / np.sin(0.5 * s))
        return out / math.sqrt(N)

    def q_rvm(t):
        """Explicit unconditional |N(t) - main(t)| bound."""
        return Q_A * math.log(t) + Q_B * math.log(math.log(t)) + Q_C

    def sh_sum(T):
        """Sharpened Abel bound of sum_{gamma > T} gamma^-2 (a)."""
        main = (math.log(T / (2.0 * math.pi)) + 1.0) \
            / (2.0 * math.pi * T)
        return main + (2.0 * q_rvm(T) + 0.2) / (T * T)

    def sh_sum_lower(T):
        """Certified LOWER bound of the same tail (band differences)."""
        main = (math.log(T / (2.0 * math.pi)) + 1.0) \
            / (2.0 * math.pi * T)
        return max(0.0, main - (2.0 * q_rvm(T) + 0.2) / (T * T))

    def crude_sum(T):
        """The parent probe's crude tail bound (regression)."""
        return (math.log(T) + 1.0) / (math.pi * T)

    def env_E(w, delta):
        """Coherent per-leg bound E_i(delta) ([D1]-discarding)."""
        hz, D = w["h"], w["D"]
        jj = np.arange(hz)
        grow = np.exp((hz - jj - 0.5) * D * delta)
        e1 = 2.0 * float(np.abs(parity_t(1, hz)) @ grow)
        e2 = 2.0 * float(np.abs(parity_t(2, hz)) @ grow)
        return e1, e2

    def num_crude(w, delta):
        """Crude per-zero numerator: cosh^2(D/4) E_i E_j (worst)."""
        e1, e2 = env_E(w, delta)
        return (math.cosh(w["D"] / 4.0) ** 2
                * max(e1 * e1, e1 * e2, e2 * e2))

    def num_sup(w, delta_max, n_u=N_U, n_delta=N_DELTA):
        """Attack (c): the true product sup NUM_max over (u, delta)."""
        hz, D = w["h"], w["D"]
        u = (np.arange(n_u) + 0.31) * (4.0 * math.pi / n_u)
        dl = np.linspace(0.0, delta_max, n_delta)
        phi = u[None, :] + 1j * (D * dl)[:, None]
        s1 = np.abs(S_closed_c(1, hz, phi))
        s2 = np.abs(S_closed_c(2, hz, phi))
        pref = (np.sin(0.5 * u[None, :]) ** 2
                + np.sinh(0.5 * D * dl)[:, None] ** 2)
        return float(np.max(pref * 4.0 * np.maximum(
            np.maximum(s1 * s1, s2 * s2), s1 * s2))) * SUP_SLACK

    def pert_of(env, s1n):
        return env * s1n + 2.0 * env * env

    def run():
        nonlocal N_CHK, FAILS
        N_CHK = 0
        FAILS = []
        print("=" * 78)
        print("THE DEEP-TAIL ENVELOPE ATTACK -- autopsy, sharpening, "
              "closure")
        print("(part 2 == prime_tail_envelope_probe verbatim, no RH "
              "claim)")
        print("=" * 78)

        # ========================================================== S0
        print("\nS0 -- rebuild (parent machinery)")
        gam, n_rvm = pp.zero_list()
        check("S0.Z zero list: %d zeros to T = %.0f (RvM dev %.2f "
              "<= 3)" % (len(gam), zp.T_SCAN, abs(len(gam) - n_rvm)),
              abs(len(gam) - n_rvm) <= 3.0)
        KZ = core.frame_a_zones()
        L15 = len(KZ)
        fam5 = [0, (L15 - 1) // 4, L15 // 2, (3 * (L15 - 1)) // 4,
                L15 - 1]
        inter = []
        for (lo_i, hi_i), n_in in zip(zip(fam5[:-1], fam5[1:]),
                                      (2, 3, 3, 2)):
            for j in range(1, n_in + 1):
                inter.append(lo_i + j * (hi_i - lo_i) // (n_in + 1))
        idx15 = sorted(set(fam5 + inter))
        wins = [lg.lock_block(KZ[i]) for i in idx15]
        wins = [w for w in wins if w["complete"]]
        wins.sort(key=lambda w: w["alpha"])
        g1 = float(gam[0])
        band_mask = (gam > BAND[0]) & (gam <= BAND[1])
        for w in wins:
            a, b, meta = pp.components_of(w, gam)
            x = a[0] * b[-1] - a[-1] * b[0]
            x_pz = a[:-1] * b[-1] - b[:-1] * a[-1]
            top = np.sort(x_pz ** 2)[::-1]
            w.update(a=a, b=b, x2=float(x * x),
                     cert100=float(np.sum(top[:K_FAM])),
                     s1n=(abs(w["A2"][0, 0]) + abs(w["A2"][1, 1])
                          + 2.0 * abs(w["A2"][0, 1])),
                     eps_f=CHAIN_FAC * EPSM
                     * float(np.linalg.norm(w["A2"])) ** 2)
        print("    %d windows; band (%.0f, %.0f]: %d verified on-line "
              "zeros" % (len(wins), BAND[0], BAND[1],
                         int(np.sum(band_mask))))

        # ========================================================== S1
        print("\nS1 -- ENVELOPE AUTOPSY (the two discarded structures "
              "+ measured slack on the band)")
        print("    [D1] coherence: sup|csinc|^2 x sup|FF| multiplies "
              "sups attained at OPPOSITE points")
        print("         (F coherent exactly at alias points D gamma = "
              "2 pi k, where sin^2(D gamma/2) = 0)")
        print("    [D2] oscillation across zeros: unexploitable "
              "without positions (Landau/Gonek = the identity again)")
        # measured band tail sum of gamma^-2 vs the Abel machinery
        g_band = np.asarray(gam[band_mask], dtype=float)
        meas_g2 = float(np.sum(g_band ** -2.0))
        band_up = sh_sum(BAND[0]) - sh_sum_lower(BAND[1])
        band_crude = crude_sum(BAND[0]) - 0.0
        check("S1.ABEL the sharpened Abel bound majorizes the "
              "measured band sum_gamma^-2 (measured %.4e <= band "
              "bound %.4e; crude one-sided %.4e)"
              % (meas_g2, band_up, band_crude), meas_g2 <= band_up)
        print("    %5s %6s | %9s %9s %9s | %8s %8s | %9s"
              % ("h", "alpha", "meas_abs", "env0_cr", "env0_sup",
                 "slk_coh", "slk_osc", "off_allow"))
        slk_c_list = []
        for w in wins:
            a, b = w["a"][:-1], w["b"][:-1]     # zero legs only
            ab_abs = np.maximum(np.maximum(a * a, b * b),
                                np.abs(a * b))
            meas_abs = float(np.sum(ab_abs[band_mask[:len(a)]]))
            meas_osc = max(
                abs(float(np.sum(a[band_mask[:len(a)]] ** 2))),
                abs(float(np.sum(b[band_mask[:len(a)]] ** 2))),
                abs(float(np.sum((a * b)[band_mask[:len(a)]]))))
            env0_cr = (4.0 / w["D"]) * num_crude(w, 0.0) * band_up
            n0 = num_sup(w, 0.0, n_u=N_U, n_delta=1)
            env0_sup = (4.0 / w["D"]) * n0 * band_up
            e1h, e2h = env_E(w, 0.5)
            e10, e20 = env_E(w, 0.0)
            off_allow = (e1h * e2h) / (e10 * e20)
            slk_coh = env0_cr / env0_sup
            slk_c_list.append(slk_coh)
            w.update(band_meas_abs=meas_abs, band_env0_cr=env0_cr,
                     band_env0_sup=env0_sup)
            print("    %5d %6.3f | %9.2e %9.2e %9.2e | %8.1f %8.1f | "
                  "%9.1f"
                  % (w["h"], w["alpha"], meas_abs, env0_cr, env0_sup,
                     slk_coh, meas_abs / max(meas_osc, 1e-300),
                     off_allow))
        check("S1.SLACK the coherence-discard [D1] carries a real "
              "slack factor %.0f..%.0f (crude/product-sup on the "
              "band, delta = 0); the off-line allowance "
              "(E(1/2)/E(0))^2 = e^{~alpha} is the honest [A1] "
              "price; the oscillation slack [D2] is typed "
              "unexploitable"
              % (min(slk_c_list), max(slk_c_list)),
              min(slk_c_list) > 2.0)

        # ========================================================== S2
        print("\nS2 -- SHARPENING ATTACKS (order a -> b -> c, stop at "
              "first success per rung)")
        fac_a = crude_sum(T_VER) / sh_sum(T_VER)
        print("    (a) Abel/RvM explicit: tail-sum factor %.2f "
              "(%.3e -> %.3e at T_ver)"
              % (fac_a, crude_sum(T_VER), sh_sum(T_VER)))
        ell = (math.log(T_VER) ** (2.0 / 3.0)
               * math.log(math.log(T_VER)) ** (1.0 / 3.0))
        d_vk = 0.5 - 1.0 / (C_VK * ell)
        print("    (b) VK zero-free region: delta_max = 1/2 - "
              "1/(%.2f x %.2f) = %.6f" % (C_VK, ell, d_vk))
        print("        STRUCTURAL ANSWER: the tail couples to the "
              "displacement EXPONENTIALLY (e^{2 alpha delta}),")
        print("        NOT count-only -- but the explicit constant "
              "gives factor e^{-2 alpha/(%.1f ell)} ~ %.3f: "
              "numerically useless (typed)"
              % (C_VK, math.exp(-2.0 * 6.15 / (C_VK * ell))))
        print("    (c) product sup NUM (grid %d x %d, slack x%.2f, "
              "doubling ward)" % (N_U, N_DELTA, SUP_SLACK))
        conv_dev = 0.0
        for w in (wins[0], wins[len(wins) // 2], wins[-1]):
            n1 = num_sup(w, 0.5)
            n2 = num_sup(w, 0.5, n_u=2 * N_U, n_delta=N_DELTA)
            conv_dev = max(conv_dev, abs(n2 - n1) / n1)
        check("S2.GRID the product-sup grid is converged (doubling "
              "changes NUM by %.4f <= %.2f on 3 test rungs; the "
              "rigorous Lipschitz certificate of the sup is a NAMED "
              "elementary step, not claimed)" % (conv_dev, GRID_CONV),
              conv_dev <= GRID_CONV)

        # ========================================================== S3
        print("\nS3 -- THE CLOSURE MAP (gate: pert < X^2, frozen as "
              "in the parent probe)")
        print("    %5s %6s | %9s %9s %9s %9s | %6s | %9s %9s"
              % ("h", "alpha", "pert_old", "pert_a", "pert_b",
                 "pert_c", "closes", "T_need", "vs_fam"))
        n_old, n_new, close_at = 0, 0, []
        for w in wins:
            D, s1n = w["D"], w["s1n"]
            e1, e2 = env_E(w, 0.5)
            nc_half = num_crude(w, 0.5)
            env_old = (4.0 / D) * nc_half * crude_sum(T_VER) \
                + w["eps_f"]
            env_a = (4.0 / D) * nc_half * sh_sum(T_VER) + w["eps_f"]
            env_b = (4.0 / D) * num_crude(w, d_vk) * sh_sum(T_VER) \
                + w["eps_f"]
            w["num_c"] = num_sup(w, 0.5)
            env_c = (4.0 / D) * w["num_c"] * sh_sum(T_VER) \
                + w["eps_f"]
            p_old = pert_of(env_old, s1n)
            p_a = pert_of(env_a, s1n)
            p_b = pert_of(env_b, s1n)
            p_c = pert_of(env_c, s1n)
            w.update(p_old=p_old, p_a=p_a, p_b=p_b, p_c=p_c,
                     env_c=env_c)
            n_old += int(p_old < w["x2"])
            which = "-"
            for tag, pv in (("a", p_a), ("b", p_b), ("c", p_c)):
                if pv < w["x2"]:
                    which = tag
                    break
            close_at.append(which)
            n_new += int(which != "-")
            t_need = float("nan")
            if which == "-":
                tt = T_VER
                for _ in range(600):
                    tt *= 1.25
                    ev = (4.0 / D) * w["num_c"] * sh_sum(tt) \
                        + w["eps_f"]
                    if pert_of(ev, s1n) < w["x2"]:
                        t_need = tt
                        break
            print("    %5d %6.3f | %9.2e %9.2e %9.2e %9.2e | %6s | "
                  "%9s %9s"
                  % (w["h"], w["alpha"], p_old, p_a, p_b, p_c, which,
                     ("-" if which != "-" else "%.1e" % t_need),
                     "yes" if p_c < w["cert100"] else "NO"))
        hs_new = [w["h"] for w, c in zip(wins, close_at) if c != "-"]
        h_max_new = max(hs_new) if hs_new else 0
        check("S3.CLOSE the sharpened chain closes %d/14 rungs at "
              "T_ver = %.0e (old: %d/14, boundary h = 434); new "
              "boundary h_max = %d; secondary family gate (pert < "
              "top-%d sum) closes %d/14"
              % (n_new, T_VER, n_old, h_max_new, K_FAM,
                 sum(1 for w in wins if w["p_c"] < w["cert100"])),
              n_new > n_old)

        # ========================================================== S4
        print("\nS4 -- THE ASYMPTOTIC QUESTION (law change, frozen)")
        lh = np.log([w["h"] for w in wins])
        av = np.array([w["alpha"] for w in wins])
        A_ = np.column_stack([np.ones_like(lh), lh, av])
        co_old, *_ = np.linalg.lstsq(
            A_, np.log([w["p_old"] for w in wins]), rcond=None)
        co_c, *_ = np.linalg.lstsq(
            A_, np.log([w["p_c"] for w in wins]), rcond=None)
        print("    fitted log(pert) = c0 + p log h + q alpha:")
        print("      old envelope: p = %+.2f, q = %+.2f  (the h "
              "e^alpha law)" % (co_old[1], co_old[2]))
        print("      sharpened  : p = %+.2f, q = %+.2f" % (co_c[1],
                                                           co_c[2]))
        h_free = abs(co_c[1]) <= H_EXP_BAR
        check("S4.LAW the sharpened envelope changed the GROWTH LAW, "
              "not just the constant: log h coefficient %+.2f -> "
              "%+.2f (bar |p| <= %.2f); X^2 is h-free analytically "
              "(piece 2), so closure is %s"
              % (co_old[1], co_c[1], H_EXP_BAR,
                 "h-INDEPENDENT at fixed alpha" if h_free
                 else "still h-dependent"), h_free)
        # the alpha-horizon of the sharpened law at fixed T_ver
        pi2 = math.pi ** 2
        alpha_star = float("nan")
        for al in np.arange(2.0, ALPHA_STAR_MAX, 0.05):
            u2 = (al * g1) ** 2
            a24 = 0.25 * al * al
            br = (1.0 / ((pi2 - u2) * (4.0 * pi2 + a24))
                  - 1.0 / ((4.0 * pi2 - u2) * (pi2 + a24)))
            x2_typ = (16.0 * al * pi2 * math.sinh(0.5 * al)
                      * br) ** 2 \
                * 0.5                      # sin^2 -> typical 1/2
            p_law = math.exp(co_c[0] + co_c[1] * math.log(500.0)
                             + co_c[2] * al)
            if p_law >= x2_typ:
                alpha_star = float(al)
                break
        print("    alpha-horizon of the sharpened law at fixed T_ver "
              "(vs X_inf^2 at sin^2 = 1/2): alpha* ~ %.1f "
              "(deployed battery tops at alpha = %.3f)"
              % (alpha_star, wins[-1]["alpha"]))

        # ========================================================== CT
        print("\nCT -- controls")
        band_ok = all(w["band_meas_abs"] <= w["band_env0_sup"]
                      and w["band_meas_abs"] <= w["band_env0_cr"]
                      for w in wins)
        check("CTA envelope ward: on the accessible band every "
              "envelope (crude AND product-sup, delta = 0) majorizes "
              "the measured tail sums on all rungs", band_ok)
        old_set = {w["h"] for w in wins if w["p_old"] < w["x2"]}
        check("CTB regression ward: the old envelope reproduces the "
              "h = 434 closure boundary (set %s == %s; eps_arch <= "
              "2e-14 measured in the parent probe, negligible)"
              % (sorted(old_set), sorted(OLD_CLOSE_SET)),
              old_set == OLD_CLOSE_SET)
        w_mid = wins[len(wins) // 2]
        D, hz = w_mid["D"], w_mid["h"]
        jj = np.arange(hz)
        worst_rel, n_detneg, env_ok = 0.0, 0, True
        for gs, de in CT1_GRID:
            zc = complex(gs, de)
            L_off = np.empty((2, 2))
            pairs = {(0, 0): (w_mid["f1"], w_mid["f1"]),
                     (1, 1): (w_mid["f2"], w_mid["f2"]),
                     (0, 1): (w_mid["f1"], w_mid["f2"])}
            for (i, j), (fa, fb) in pairs.items():
                tp = lg.T_pair(fa, fb, D, np.array([zc, np.conj(zc)]))
                L_off[i, j] = L_off[j, i] = float(np.real(np.sum(tp)))
            sc = float(np.max(np.abs(L_off)))
            worst_rel = min(worst_rel, eig2_min(L_off) / sc)
            n_detneg += int(float(np.linalg.det(L_off)) < 0.0)
            env_here = (4.0 / (D * gs * gs)) * num_sup(
                w_mid, de, n_u=N_U, n_delta=9)
            env_ok = env_ok and sc <= 2.0 * env_here
        check("CTC [must-fire] synthetic OFF-line pairs break the "
              "psd chain (det < 0 on %d/%d, worst min-eig/scale %.2f "
              "<= %.0e) AND the SHARPENED per-zero envelope still "
              "contains them: %s"
              % (n_detneg, len(CT1_GRID), worst_rel, CT1_NEG,
                 "ok" if env_ok else "VIOLATED"),
              n_detneg == len(CT1_GRID) and worst_rel <= CT1_NEG
              and env_ok)

        # ========================================================== V
        print("\n" + "=" * 78)
        print("V -- verdict")
        print("=" * 78)
        if n_new == len(wins) and h_free:
            verdict = "TAIL-CLOSED-ALL-H"
        elif h_max_new > 434:
            verdict = "TAIL-EXTENDED"
        else:
            verdict = "TAIL-IRREDUCIBLE"
        _LAST2["verdict"] = verdict
        print("""
  VERDICT: %s

  STRONGEST UNIFORM FLOOR THEOREM NOW SUPPORTED (verbatim):
    'Inputs: [A1] (all zeta zeros in 0 < Re s < 1, unconditional);
     verified on-line zeros to T_ver = 3e12 (Platt-Trudgian, cited);
     the RvM counting function with Trudgian-grade explicit
     constants (cited); the analytic pair limit X_inf^2(alpha)
     (piece 2); the product-sup per-zero envelope NUM (numerically
     certified sup of an explicit closed form; its Lipschitz
     certificate is a named elementary step).  Then on %s of the
     deployed battery: lambda tau = det Ahat2 >= X^2(pole, gamma_1)
     - pert(h) > 0 at citation grade -- the psd remainder needs NO
     per-rung eigencheck and NO zero-location input beyond the
     strip; the deep tail (gamma > T_ver, worst-case off-line
     displacement) is absorbed by an envelope whose growth law is
     h-free: p(log h) = %+.2f.  The same chain bounds the top-%d
     family sum on the rungs marked vs_fam.'

  THE ASYMPTOTIC ANSWER: the law changed, not just the constant --
    old p(log h) = %+.2f (the h e^alpha artifact of the coherence
    discard [D1]) -> sharpened p = %+.2f; a FIXED T_ver covers the
    battery independent of h at the deployed alphas.  The remaining
    growth is in alpha only (q = %+.2f, from the [A1] off-line
    allowance e^{2 alpha delta}): the sharpened chain re-crosses
    X_inf^2 near alpha* ~ %.0f -- far beyond the deployed range
    (alpha <= %.2f) but finite: the verification-depth dependence
    re-enters there, honestly typed.
  NOT PROVEN: this is a necessary-side floor on a frozen battery --
    NOT RH; zero-location content beyond [A1] + citation depth is
    never used, and no statement about zeros off the line is made
    or implied.
""" % (verdict,
       ("ALL 14 rungs" if n_new == len(wins)
        else "%d/14 rungs (h <= %d)" % (n_new, h_max_new)),
       co_c[1], K_FAM, co_old[1], co_c[1], co_c[2], alpha_star,
       wins[-1]["alpha"]))

        dt = time.time() - T0
        print("-" * 78)
        print("checks: %d run, %d failed%s | runtime %.1f min"
              % (N_CHK, len(FAILS),
                 (" [" + ", ".join(FAILS) + "]") if FAILS else "",
                 dt / 60.0))
        print("NO RH claim (part 2).")

    run()
    return list(FAILS), N_CHK


def run():
    """run_all entry point (v757 precedent): expected patterns 12/12
    checks with verdict FLOOR-PARTIAL and grades P1 PER-RUNG-ONLY /
    P2 UNIFORM-VERIFIED / P3 UNIFORM-VERIFIED (part 1 -- the crude P1
    envelope closes only 4/14, exactly what part 2 repairs), then 9/9
    checks with verdict TAIL-CLOSED-ALL-H (part 2)."""
    _probe_run()
    fails1 = list(FAILS)
    n1 = N_CHK
    v1 = _LAST1.get("verdict", "?")
    g1_, g2_, g3_ = _LAST1.get("grades", ("?", "?", "?"))
    print()
    fails2, n2 = _part2()
    v2 = _LAST2.get("verdict", "?")
    ok = (n1 == 12 and fails1 == [] and v1 == "FLOOR-PARTIAL"
          and g1_.startswith("PER-RUNG-ONLY")
          and g2_.startswith("UNIFORM-VERIFIED")
          and g3_ == "UNIFORM-VERIFIED"
          and n2 == 9 and fails2 == []
          and v2 == "TAIL-CLOSED-ALL-H")
    print("\n[%s] PATTERN GATE: expected 12/12 (FLOOR-PARTIAL; grades "
          "PER-RUNG-ONLY / UNIFORM-VERIFIED / UNIFORM-VERIFIED) + 9/9 "
          "(TAIL-CLOSED-ALL-H); got %d checks (fails %s, %s; %s / %s "
          "/ %s) + %d checks (fails %s, %s)"
          % ("PASS" if ok else "FAIL", n1, fails1 or "none", v1,
             g1_, g2_, g3_, n2, fails2 or "none", v2))
    return 0 if ok else 1


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