"""v687 -- PRIME.KERNELCLASS.01: THE EXTREMAL TEST KERNEL
(contract PRIME.W2.EXTREMAL.KERNEL.01) -- can an optimized positive
band-limited kernel of the SAME exponential type lift the
pointwise-capture threshold of the W2 Garding chain above the existing
explicit S(t) constant A1 = 0.10076?  This module reconstructs the
bottleneck chain, optimizes over the declared kernel class, certifies
the answer EXACTLY, and types it; it moves no marker.

CONTEXT (the parents).  fejer_density_bound_probe (v669, 2026-08-02)
derived the EXACT identity s_tot(t; a) = 2 pi sum_rho F_a(gamma_rho - t)
with the unit-mass Fejer kernel F_a(s) = sin^2(a s)/(pi a s^2) -- the
Fejer kernel is NOT a free choice: it is the autocorrelation tent of
the frame-A window [-a, a] read through Weil's explicit formula (test
pair g_t = tent x e^{-itu}, h_t = 2 pi F_a(. - t)).
zero_gap_theorem_probe (v678) supplied the explicit unconditional
zero-gap theorem H_min(t) (S-difference route) and typed THE PINCH:
one-sided pointwise capture of a guaranteed zero needs H_min < pi/a0,
i.e. A1 < 1/(4 a0) = 0.09017; the best cited constant A1 = 0.10076
(Bellotti 2025) misses by 11.75%.  pinch_attack_probe (v680) broke the
pinch by BOOKKEEPING (centered capture doubles the threshold to
1/(2 a0) = 0.18034) and by the Beurling-Selberg count minorant
(A1-free); coverage_hole_probe (v681) closed the finite hole.  THE
REVIEW'S REMAINING IDEA (this task): do NOT touch the external S(t)
constant and do NOT change the bookkeeping -- optimize the TEST KERNEL:
a positive band-limited kernel K of the same exponential type that
"keeps more mass in the main lobe and pays less boundary penalty on
S(t)", so that the REQUIRED threshold rises above 0.10076.

================= THE RECONSTRUCTED BOTTLENECK CHAIN =================
(documented BEFORE any optimization, as demanded by the task)

(a) THE INEQUALITY THAT A1 CONTROLS.  The pointwise diagonal floor of
the W2 Garding route is s_tot(t; a) > 0 (resp. >= c log(2+t) - C) at
arbitrary height t.  The unconditional chain is:

  (a1) zero-gap theorem [v678]:  N(t + H) - N(t)
         >= mainD(t, H) - 2 Sbound(t + H) - eps_N  =: G(t, H),
       mainD = [H log(t/2pi) + (t+H) log1p(H/t) - H]/(2 pi),
       eps_N = 2e-3; G > 0 forces >= 1 ordinate gamma* in (t, t + H].
       H_min(t) := smallest such H.  With the pointwise bound
       |S(T)| <= A1 log T + ... the asymptote is
         H_min(t) -> 4 pi A1   (t -> infinity),
       because mainD ~ H L/(2 pi) must beat 2 A1 L (L = log(t/2pi)).
       THIS is the only place the S(t) constant A1 enters.
  (a2) one-sided capture [v678 baseline]:  keep only the guaranteed
       zero (all others contribute >= 0 since F_a >= 0):
         s_tot(t) >= 2 pi F_a(gamma* - t)
                  >= 2 pi min_{0 < s <= H_min} F_a(s),
       positive iff H_min < z1(F_a) = pi/a  (z1 = first positive zero
       of the kernel = the main-lobe edge; the "box minorant" of the
       main lobe realizes the min).  Combining (a1) + (a2):
         REQUIRED:  4 pi A1 < z1(F_a) = pi/a
         <=>        A1 < 1/(4 a).
       At a0 = log 16:  1/(4 a0) = 0.0901686 < 0.10076 (Bellotti):
       the 11.75% pinch.

(b) WHERE THE FEJER CHOICE AND THE BOX MINORANT ENTER.  F_a enters as
the FIXED window kernel (the identity of v669 -- the frame-A window
autocorrelation is the tent, its transform is 2 pi F_a; no freedom).
The box minorant enters twice: as the inf over the capture interval
in (a2) (one-sided box under the main lobe), and in the v669 density
chain as Phi_delta >= (4/pi^2)/delta on |s| <= delta/2.  The S(t)
bound enters ONLY through H_min in (a1).

(c) THE REQUIRED THRESHOLD, exact definition.  A replacement kernel K
can enter the chain ONLY through the transfer inequality

    s_tot(t) = 2 pi sum_rho F_a(gamma_rho - t) >= sum_rho K(gamma_rho - t),

which requires  0 <= K(s) <= 2 pi F_a(s)  FOR ALL REAL s (the zeros sit
everywhere; unconditionally no term may be overcharged).  Then the
one-sided capture with K gives the frozen threshold functional

    theta_req(K) := z1(K) / (4 pi),      PASS  <=>  theta_req > 0.10076
                                          <=>  z1(K) > 4 pi 0.10076
                                             = 1.26617,
and the review's mass-vs-penalty functional (the "more main-lobe mass,
less S(t) boundary penalty" reading; windowed-count/partial-summation
form  sum_gamma K(gamma - t) >= (int K) L/(2 pi) - Sbar(t) TV(K) - eps)

    theta_ibp(K) := (int K) / (2 pi TV(K)),   TV = total variation
                                              (the S(t) "Randstrafe").

Fejer negative controls: theta_req(2 pi F_a) = (pi/a)/(4 pi) = 1/(4 a0)
= 0.0901686 (the quoted 0.09017); the one-sided box of width H
reproduces theta_ibp = H/(4 pi) < 1/(4 a) -- the v678 numbers.

THE STRUCTURAL HYPOTHESIS TO ADJUDICATE (declared before the numbers).
The transfer constraint PINS every admissible kernel at every Fejer
node:  2 pi F_a(m pi/a) = 0 exactly, so K(m pi/a) = 0 and (K = |q|^2)
q(m pi/a) = 0 for ALL integers m != 0.  Hence z1(K) <= pi/a for EVERY
admissible K: the class supremum of theta_req EQUALS the Fejer value
1/(4 a0) = 0.0901686 < 0.10076 -- the PASS bar would be unreachable.
Stronger: q in PW^2_a is determined by its Nyquist samples q(m pi/a)
(Shannon sampling over the lattice pi/a of exponential type a), so
q = q(0) sin(a s)/(a s) and

    { K = |q|^2 : type(q) <= a, 0 <= K <= 2 pi F_a on R }
        =  { lambda 2 pi F_a : 0 <= lambda <= 1 }

-- the kernel class of the review COLLAPSES to Fejer multiples: the
kernel lever has ZERO free directions.  In the declared trig-polynomial
parametrization the collapse is finite and machine-exact: the basis
kernels phi_k (q-hat = cos(pi k xi/a) on [-a, a]) satisfy
phi_k(m pi/a) = delta_{km} a/(2 pi) EXACTLY, so the node constraints
alone force c_1 = ... = c_N = 0, i.e. q = c_0 sin(a s)/(pi s) = Fejer.
This module certifies the pinning + the node identity EXACTLY (sympy),
certifies every threshold comparison with interval arithmetic
(mpmath.iv), runs the LP optimization over q-coefficients on the degree
ladder (bisection over the capture reach z, exactly the task's K2
structure), and demonstrates the FALSE-PASS trap of node-blind grids
with an exact refutation.  Escapes OUTSIDE the frozen class are typed,
not hidden: centered bookkeeping (x2, v680), non-band-limited BV
minorants (box: theta_ibp -> 1/(2 a0), v680 BOX-COUNT), Selberg
(A1-free, v680); a budgeted LOCAL minorization (allowing K > 2 pi F_a
on a zero-counted exceptional set) re-imports the counting cost it was
meant to avoid -- a typed open door, not used.

CALIBRATION HISTORY (declared -- honesty first): run 1 (2026-08-03)
failed TWO comparators, both documented implementation-side errors; no
certified value changed between runs.  (i) G0.2 Parseval used a random
coefficient vector with a nonzero band-edge value q-hat(+-a0) != 0, so
q decays only like 1/s and the [-600, 600] quadrature truncation
(~3e-4 relative) exceeded the 1e-5 bar -- the comparator, not the
identity, was wrong; run 2 projects the test vector to a vanishing
band edge (sum (-1)^k c_k = 0 => q ~ 1/s^2, truncation ~1e-8).  (ii)
R1.1 barred |H_min(1e120) - 4 pi A1| <= 0.2, ignoring that the
approach to the floor is only loglog/log-slow (excess = 4 pi
(C2 loglog t + C3)/log t, measured 0.40 at 1e120 -- exactly the
branch-1 prediction); run 2 bars the structurally correct invariant:
excess strictly decreasing along 1e30/1e60/1e120/1e300 and <= 0.25 at
1e300.  Every other run-1 number reproduces unchanged.

SLICES AND BARS (declared BEFORE the numbers):
  G0.0 [E] AST zero-firewall: no Riemann-zero loader in this module
       (kernels + cited constants only; no zero data is needed).
  G0.1 [E] exact-arithmetic machinery: iv.log(2) enclosure width
       < 1e-30 (dps 60); sympy sin(m pi) == 0 exactly (m = 1..6);
       anchor window recognized as a0 = log 16 (|e^{a0} - 16| < 1e-8);
       sympy node identities: 2 pi F_a(m pi/a) == 0 exactly and
       lim_{s -> m pi/a} phi_k(s) == delta_{km} a/(2 pi) exactly
       (k, m <= 3, symbolic a > 0).
  G0.2 [E] kernel machinery: int F_a = 1 (quadrature, 1e-6); the
       Fejer member of the basis reproduces 2 pi F_a on a grid
       (rel < 1e-12); Parseval int q^2 = (2a c0^2 + a sum c_k^2)/(2pi)
       vs quadrature (rel < 1e-5; edge-vanishing test vector, see
       CALIBRATION HISTORY); basis node values numeric ==
       delta_{km} a/(2 pi) (1e-10); main-lobe mass printed (0.9028).
  R1.1 [E] H_min machinery reproduction (v678/v680 verbatim formulas):
       Platt-branch bottom H_min(3.06e10 - 400) = 1.4187 +- 0.01;
       Bellotti-branch excess H_min - 4 pi 0.10076 strictly decreasing
       along 1e30/1e60/1e120/1e300 with excess(1e300) <= 0.25 (the
       loglog/log-slow approach, branch-1 scale printed); global min
       of the min-chain >= 1.41 > pi/a0 = 1.13309 (no chain reaches
       the Fejer lobe at any height -- the v678 statement).
  R1.2 [E] the frozen threshold + pinch reproduction: theta_req(Fejer)
       = 1/(4 a0), |. - 0.09017| <= 5e-5 (task quote); pinch percent
       (interval) within 11.75 +- 0.10 (v680 quote).
  R1.3 [E] the PASS bar in exact form: z1 needed > 4 pi A1_BE =
       1.26617 (interval); window form: 1/(4 log n) > 2519/25000
       <=> n <= 11 (certified e^{25000/10076} < 12); the anchor
       n = 16 makes the bar unreachable for ANY kernel with
       z1 <= pi/a0.
  K1.1 [E] parametrization: q-hat = sum_{k<=N} c_k cos(pi k xi/a) on
       [-a, a] (trig polynomial in the transform variable; Fejer =
       the k = 0 / Dirichlet-constant case); degree ladder N = 8, 16,
       32 at the SAME type (K-hat support [-2a, 2a] for every N --
       what the chain allows: type 2a is pinned by the prime side of
       the explicit formula, atoms n <= e^{2a} = 256; a larger type
       would add primes at exponential cost, v680 tau-scan, cited);
       numeric transform-support spot check for a random degree-32
       member: |K-hat(u)| <= 5e-3 x K-hat(0) for u in [2a(1.02),
       2a(1.5)].
  K2.1 [EXACT, central] the pinning certificate: 2 pi F_a(m pi/a) = 0
       exactly => every admissible K has K(m pi/a) = 0 and z1(K) <=
       pi/a0 => theta_req(K) <= 1/(4 a0) < 0.10076 (strict interval
       certificate): the class supremum equals the Fejer negative
       control; the PASS bar is unreachable IN THE ENTIRE CLASS.
  K2.2 [EXACT] the collapse: the node identity (G0.1) forces, within
       every degree N, c_1 = ... = c_N = 0, hence q = c_0 phi_0 (a
       Fejer multiple) -- finite, machine-exact; the full PW^2_a class
       follows by Shannon sampling on the lattice pi/a (cited, typed
       ANALYTIC): the feasible set is { lambda 2 pi F_a, 0 <= lambda
       <= 1 }.
  K2.3 [MEASURED] the LP ladder on honest (node-containing) grids:
       per degree N in {8, 16, 32}: bisection over the capture reach z
       of the LP  max t  s.t.  q(s_j) >= t on (0, z],  |q(s_i)| <=
       sqrt(2 pi F_a(s_i))  on [0, 25 pi/a],  q(0) = 1:  z*(N) =
       pi/a0 within 5e-4 from below AND never above; coefficient
       collapse ||c_{1..N}||_inf <= 1e-6 at the last feasible z;
       post-hoc tail scan of the winner on [25 pi/a, 200 pi/a]
       violation <= 1e-9.
  K2.4 [MEASURED] the FALSE-PASS trap: the same LP on a node-BLIND
       grid (all points within 0.15 of a node removed): the apparent
       reach z*_adv and apparent theta printed (may exceed 0.10076!);
       exact refutation: the rationalized winner has q(m pi/a0) =
       c_m a0/(2 pi) != 0 while the bound is exactly 0 there --
       infeasibility certified exactly; bar: either no inflation
       (z*_adv <= pi/a0 + 1e-6) or the exact refutation fires
       (max_m |c_m| > 1e-3).
  K2.5 [MEASURED/EXACT] the review's ratio functional theta_ibp =
       (int K)/(2 pi TV(K)): Fejer value = 1/(4 a0 (1 + 2 sum_k
       1/(1 + x_k^2))) (x_k = the tan x = x side-lobe roots; 4000
       roots + certified tail budget; printed ~0.0755) -- by K2.2 the
       CLASS supremum of theta_ibp is this Fejer value (< the
       one-sided box already); SLSQP confirmation at N = 8 on the
       node grid: theta_ibp(opt) <= Fejer + 5e-3 and ||c_{1..N}||
       <= 1e-4; the RELAXED (non-band-limited) BV class realizes the
       review's mechanism: the rational box delta = 15/8 under the
       main lobe gives theta_ibp = 15/(32 pi) = 0.14921 > 0.10076
       (strict interval certificate; height 2 pi F_a0(15/16) > 0
       certified) and the class supremum is the thin-box limit
       1/(2 a0) = 0.18034 -- the v680 BOX-COUNT territory, OUTSIDE
       the same-type class.
  K3.1 [EXACT] certification of the class optimum: q* = Fejer with
       RATIONAL coefficient vector c = (1, 0, ..., 0) (q* = phi_0 =
       sin(a s)/(pi s), lambda = a0/(2 pi^2) admissible); SOS by
       construction (K* = |q*|^2); z1(K*) = pi/a0 EXACT (sympy);
       theta_req(K*) = 1/(4 a0) certified interval.
  K3.2 [EXACT] PASS adjudication + margins: theta* < 0.10076 strict
       (interval); sup/needed = 0.89490 (10.51% below), needed/sup
       - 1 = 11.746% (the pinch, re-derived exactly); absolute gap
       0.010592.  The acceptance bar "Schwelle >= 0.10076" FAILS for
       the whole class.
  K4.1 [E] Fejer frozen negative control: reproduces 0.09017 (2e-5)
       and the 11.75% pinch (0.10) -- the task's control bar.
  K4.2 [E] transportability over the 5 family windows: a_w = log n_w
       (integer n_w recognized, 1e-8); exact per-window ceilings
       1/(4 log n_w) ALL < 0.10076 (integer criterion n_w >= 12,
       certified); NO window tuning exists (the feasible set per
       window is { lambda 2 pi F_{a_w} } -- zero shape freedom), so
       the one-kernel-for-all-windows requirement holds vacuously;
       centered column printed with the v680 concordance (windows
       with n_w > 142 need Selberg).
  K4.3 [C] supremum honesty: the class supremum is ATTAINED (by Fejer
       itself), sits 10.51% below the PASS bar, and the class has no
       interior directions: the review's class (same type, K >= 0,
       minorizing the window symbol) is PRINCIPALLY TOO NARROW -- the
       honest structural negative, here upgraded from a measurement
       to an exact statement.
  K5.1 [C] contract note PRIME.W2.EXTREMAL.KERNEL.01 (report only;
       nothing written), with the review's acceptance criteria quoted
       verbatim and adjudicated one by one.

Verdict enums (frozen, precedence top-down; the EXPECTED verdict of
this module is KERNEL-CLASS-TOO-NARROW -- the structure theorem: the
admissible class collapses exactly onto {lambda Fejer}, supremum
0.0901684 < 0.10076; W2 stays closed via the v680/v681 route):
  KERNEL-MIXED            -- any G0.*/R1.* fails;
  KERNEL-THRESHOLD-PASSED -- an exactly certified SOS kernel of the
                             class with theta_req > 0.10076 exists
                             (the task PASS -- excluded by K2.1 unless
                             the certificates fail);
  KERNEL-CLASS-TOO-NARROW -- K2/K3 exact certificates + K4 controls
                             pass and the certified class supremum is
                             < 0.10076: the structural negative;
  KERNEL-NO-GAIN          -- otherwise.

FIREWALL: v563 import read-only; NO zero of any L-function is read
(AST-checked -- the whole module is kernels + cited constants); no
marker moves; Python-only per GATE.WOLFRAM.02.

PROVENANCE: discovery probe extremal_kernel_probe.py (2026-08-03,
18/18 PASS, verdict KERNEL-CLASS-TOO-NARROW);
fejer_density_bound_probe.py (v669: the identity, the box
minorant, the RvM chain), zero_gap_theorem_probe.py (v678: H_min, the
pinch A1 < 1/(4 a0)), pinch_attack_probe.py (v680: centered capture,
Selberg, BOX-COUNT thresholds, tau-scan), coverage_hole_probe.py
(v681: the closed pointwise map), v563_paper2_readouts (atom table,
frame-A windows), Trudgian J. Number Theory 134 (2014) Thm 1,
Bellotti arXiv:2412.15470 / Math. Comp. 2025 (A1 = 0.10076, eq. (1.2)
Platt |S| <= 2.5167 to 3.061e10), Vaaler Bull. AMS 12 (1985),
Iwaniec-Kowalski Thm 5.12, Boas "Entire Functions" (Paley-Wiener /
sampling), Shannon-Whittaker-Kotelnikov sampling theorem.
"""
import ast
import math
import os
import sys
import time

import numpy as np

_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

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


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())
    for node in ast.walk(tree):
        if isinstance(node, ast.Attribute) and node.attr in (
                "zetazero", "nzeros", "second_sheet_zero"):
            return False
        if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) \
                and node.func.id in ("zetazero", "nzeros"):
            return False
    return True


import v563_paper2_readouts as core  # noqa: E402  (READ-ONLY import)
import mpmath as mp  # noqa: E402
import sympy as sp  # noqa: E402
from fractions import Fraction  # noqa: E402
from mpmath import iv  # noqa: E402
from scipy.optimize import brentq, linprog, minimize  # noqa: E402

# ------------------------------------------------------------ constants
# cited chain constants (v678/v680 verbatim)
A1_TR, A2_TR, A3_TR = 0.112, 0.278, 2.510   # Trudgian 2014 Thm 1
C1_BE = 0.10076                             # Bellotti-Wong 2025
C2_BE, C3_BE = 0.24460, 7.20844             # ... Cor. 1.5 branch 1
C2B_BE, C3B_BE = 1.68845, 1.50956           # ... branch 2 (min)
S_PLATT = 2.5167                            # Bellotti eq. (1.2)
T_PLATT = 30610046000.0                     # Platt LMFDB height
EPS_N = 2e-3
FLOOR_BE = 4.0 * math.pi * C1_BE            # 1.26617
H_BRACKET = (1e-3, 400.0)
N_BISECT = 90
A1_NUM, A1_DEN = 2519, 25000                # 0.10076 exact rational
# reproduction quotes + bars
REF_PLATT_BOT = 1.4187
BAR_PLATT_BOT = 0.01
REF_THETA_F = 0.09017                       # task quote 1/(4 a0)
BAR_THETA_F = 5e-5
REF_PINCH_PCT = 11.75                       # v680 quote
BAR_PINCH_PCT = 0.10
# machinery bars
BAR_IV_WIDTH = 1e-30
BAR_ANCHOR = 1e-8
BAR_MASS = 1e-6
BAR_MEMBER = 1e-12
BAR_PARSEVAL = 1e-5
BAR_NODE_NUM = 1e-10
BAR_KHAT = 5e-3
# LP / optimization
N_LADDER = (8, 16, 32)
OSR = 150                                   # global grid pts per pi/a
S_MAX_LOBES = 25                            # global grid reach (lobes)
TAIL_LOBES = 200                            # post-hoc tail scan reach
CAPN = 300                                  # capture grid points
BAR_T_FEAS = 1e-8                           # LP floor feasibility
N_ZBISECT = 26
BAR_ZSTAR = 5e-4                            # z* below pi/a within this
BAR_COLLAPSE = 1e-6                         # ||c_1..N||_inf at winner
BAR_TAIL_VIOL = 1e-9
EXCL_ADV = 0.15                             # node-blind exclusion radius
BAR_REFUTE = 1e-3                           # exact-refutation trigger
# theta_ibp
N_TANROOTS = 4000
BAR_IBP_OPT = 5e-3
BAR_IBP_COEF = 1e-4
BOX_DELTA = Fraction(15, 8)                 # rational relaxed-box width

iv.dps = 60
mp.mp.dps = 30

IV_LOG2 = iv.log(iv.mpf(2))
IV_A0 = 4 * IV_LOG2                          # a0 = log 16 (exact object)
IV_A1 = iv.mpf(A1_NUM) / iv.mpf(A1_DEN)      # 0.10076 exact
IV_PI = iv.pi


def iv_lt(x, y):
    """certified strict x < y for mpmath intervals."""
    return mp.mpf(x.b) < mp.mpf(y.a)


def iv_gt(x, y):
    return mp.mpf(x.a) > mp.mpf(y.b)


def iv_mid(x):
    return float((mp.mpf(x.a) + mp.mpf(x.b)) / 2)


def iv_wid(x):
    return float(mp.mpf(x.b) - mp.mpf(x.a))


# ------------------------------------------------- H_min machinery (v678)
TWO_PI = 2.0 * math.pi


def s_bound_tr(x):
    x = np.asarray(x, dtype=float)
    return A1_TR * np.log(x) + A2_TR * np.log(np.log(x)) + A3_TR


def s_bound_be(x):
    x = np.asarray(x, dtype=float)
    ll = np.log(np.log(x))
    return C1_BE * np.log(x) + np.minimum(C2_BE * ll + C3_BE,
                                          C2B_BE * ll + C3B_BE)


def s_bound_platt(x):
    x = np.asarray(x, dtype=float)
    return np.where(x <= T_PLATT, S_PLATT, np.inf)


def g_gap(t, H, s_bound):
    t = np.asarray(t, dtype=float)
    H = np.asarray(H, dtype=float)
    mainD = (H * np.log(t / TWO_PI) + (t + H) * np.log1p(H / t)
             - H) / TWO_PI
    return mainD - 2.0 * s_bound(t + H) - EPS_N


def h_min_chain(ts, s_bound):
    ts = np.asarray(ts, dtype=float)
    lo = np.full(ts.shape, H_BRACKET[0])
    hi = np.full(ts.shape, H_BRACKET[1])
    for _ in range(N_BISECT):
        mid = 0.5 * (lo + hi)
        pos = g_gap(ts, mid, s_bound) > 0.0
        hi = np.where(pos, mid, hi)
        lo = np.where(pos, lo, mid)
    return hi


def h_min_all(ts):
    ts = np.asarray(ts, dtype=float)
    h = np.minimum(h_min_chain(ts, s_bound_tr),
                   h_min_chain(ts, s_bound_be))
    mP = ts + H_BRACKET[1] <= T_PLATT
    if np.any(mP):
        h[mP] = np.minimum(h[mP], h_min_chain(ts[mP], s_bound_platt))
    return h


# ------------------------------------------------- kernel machinery
def two_pi_F(a, s):
    """2 pi F_a(s) = 2 sin^2(a s)/(a s^2), the window kernel."""
    s = np.asarray(s, dtype=float)
    small = np.abs(s) < 1e-12
    ss = np.where(small, 1.0, s)
    out = 2.0 * np.sin(a * ss) ** 2 / (a * ss ** 2)
    return np.where(small, 2.0 * a, out)


def g_sqrt(a, s):
    return np.sqrt(two_pi_F(a, s))


def phi_basis(a, N, s):
    """Phi[i, k] = phi_k(s_i); q-hat_k = cos(pi k xi/a) on [-a, a];
    phi_k(s) = (-1)^k sin(a s) s / (pi (s^2 - (k pi/a)^2)), removable
    singularities at the Fejer nodes handled analytically."""
    s = np.asarray(s, dtype=float)
    out = np.empty((s.size, N + 1))
    sin_as = np.sin(a * s)
    small = np.abs(s) < 1e-12
    ss = np.where(small, 1.0, s)
    out[:, 0] = np.where(small, a / math.pi, sin_as / (math.pi * ss))
    for k in range(1, N + 1):
        node = k * math.pi / a
        near = np.abs(np.abs(s) - node) < 1e-9
        d = np.where(near, 1.0, s ** 2 - node ** 2)
        col = ((-1) ** k) * sin_as * s / (math.pi * d)
        out[:, k] = np.where(near, a / (2.0 * math.pi), col)
    return out


def q_int_parseval(a, c):
    """int_R q^2 ds = (2a c0^2 + a sum_{k>=1} c_k^2)/(2 pi), exact in
    the coefficients (mode orthogonality on the band)."""
    c = np.asarray(c, dtype=float)
    return (2.0 * a * c[0] ** 2 + a * float(np.sum(c[1:] ** 2))) \
        / (2.0 * math.pi)


def grid_tv(K):
    return float(np.sum(np.abs(np.diff(K))))


# ------------------------------------------------- LP capture machinery
def make_global_grid(a, node_blind=False):
    step = math.pi / (a * OSR)
    sg = np.arange(0.0, S_MAX_LOBES * math.pi / a + 0.5 * step, step)
    nodes = np.arange(1, S_MAX_LOBES + 1) * math.pi / a
    if node_blind:
        dmin = np.min(np.abs(sg[:, None] - nodes[None, :]), axis=1)
        sg = sg[dmin > EXCL_ADV]
    else:
        sg = np.sort(np.concatenate([sg, nodes]))
    return sg, nodes


def lp_capture(a, N, z, Phi_g, g_g, nodes, node_blind=False):
    """max t s.t. q(s_j) >= t on (0, z]; |q(s_i)| <= g(s_i) globally;
    q(0) = 1.  Returns (t_opt, c) or (None, None)."""
    sc = np.linspace(z / CAPN, z, CAPN)
    inz = nodes[nodes <= z + 1e-12]
    if node_blind:
        if inz.size:
            dmin = np.min(np.abs(sc[:, None] - inz[None, :]), axis=1)
            sc = sc[dmin > EXCL_ADV]
    else:
        sc = np.sort(np.concatenate([sc, inz]))
    Phi_c = phi_basis(a, N, sc)
    n_c = N + 1
    # rows: capture (-q + t <= 0), upper (q <= g), lower (-q <= g)
    A_cap = np.hstack([-Phi_c, np.ones((Phi_c.shape[0], 1))])
    A_up = np.hstack([Phi_g, np.zeros((Phi_g.shape[0], 1))])
    A_lo = np.hstack([-Phi_g, np.zeros((Phi_g.shape[0], 1))])
    A_ub = np.vstack([A_cap, A_up, A_lo])
    b_ub = np.concatenate([np.zeros(Phi_c.shape[0]), g_g, g_g])
    A_eq = np.zeros((1, n_c + 1))
    A_eq[0, 0] = a / math.pi          # q(0) = c0 a/pi = 1
    b_eq = np.array([1.0])
    obj = np.zeros(n_c + 1)
    obj[-1] = -1.0
    res = linprog(obj, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq,
                  bounds=[(None, None)] * (n_c + 1), method="highs")
    if res.status != 0:
        return None, None
    return float(res.x[-1]), res.x[:-1]


def bisect_zstar(a, N, node_blind=False, z_hi_fact=1.5):
    sg, nodes = make_global_grid(a, node_blind)
    Phi_g = phi_basis(a, N, sg)
    g_g = g_sqrt(a, sg)
    pia = math.pi / a
    z_lo, z_hi = 0.5 * pia, z_hi_fact * pia
    t_lo, c_lo = lp_capture(a, N, z_lo, Phi_g, g_g, nodes, node_blind)
    if t_lo is None or t_lo <= BAR_T_FEAS:
        return None, None, None
    t_hi, _ = lp_capture(a, N, z_hi, Phi_g, g_g, nodes, node_blind)
    if t_hi is not None and t_hi > BAR_T_FEAS:
        # never happens on honest grids; adversarial grids may reach it
        return z_hi, t_hi, lp_capture(a, N, z_hi, Phi_g, g_g, nodes,
                                      node_blind)[1]
    c_win, z_win, t_win = c_lo, z_lo, t_lo
    for _ in range(N_ZBISECT):
        z_mid = 0.5 * (z_lo + z_hi)
        t_m, c_m = lp_capture(a, N, z_mid, Phi_g, g_g, nodes,
                              node_blind)
        if t_m is not None and t_m > BAR_T_FEAS:
            z_lo, c_win, z_win, t_win = z_mid, c_m, z_mid, t_m
        else:
            z_hi = z_mid
    return z_win, t_win, c_win


def run():
    global N_CHK, FAILS
    N_CHK = 0
    FAILS = []
    print("=" * 78)
    print("THE EXTREMAL TEST KERNEL -- PRIME.W2.EXTREMAL.KERNEL.01:\n"
          "same-type positive kernels vs the 11.7% pointwise-capture "
          "gap, exact adjudication")
    print("=" * 78)

    check("G0.0 [E] AST zero-firewall: no Riemann-zero loader in this "
          "probe (kernels + cited constants only)",
          ast_zero_firewall(__file__))

    # ------------------------------------------------ anchor window
    kz0 = core.frame_a_zones()[0]
    r0 = core.build_window(kz0)
    a0 = r0["alpha"]
    n0 = int(round(math.exp(a0)))
    pia = math.pi / a0
    print("anchor window: a0 = %.12f (= log %d); pi/a0 = %.6f; "
          "4 pi A1_BE = %.6f; type 2 a0 = %.6f (atoms n <= e^{2a0} "
          "= %d)" % (a0, n0, pia, FLOOR_BE, 2.0 * a0,
                     int(round(math.exp(2.0 * a0)))))

    # G0.1 exact-arithmetic machinery
    wid = iv_wid(IV_LOG2)
    sym_sin_ok = all(sp.sin(m * sp.pi) == 0 for m in range(1, 7))
    anchor_ok = abs(math.exp(a0) - n0) < BAR_ANCHOR and n0 == 16
    a_s = sp.Symbol("a", positive=True)
    s_s = sp.Symbol("s", positive=True)
    Kf_sym = 2 * sp.sin(a_s * s_s) ** 2 / (a_s * s_s ** 2)
    pin_sym_ok = all(
        sp.simplify(Kf_sym.subs(s_s, m * sp.pi / a_s)) == 0
        for m in range(1, 7))
    node_sym_ok = True
    for k in range(0, 4):
        if k == 0:
            phi_sym = sp.sin(a_s * s_s) / (sp.pi * s_s)
        else:
            phi_sym = ((-1) ** k * sp.sin(a_s * s_s) * s_s
                       / (sp.pi * (s_s ** 2 - (k * sp.pi / a_s) ** 2)))
        for m in range(1, 4):
            lim = sp.limit(phi_sym, s_s, m * sp.pi / a_s)
            tgt = a_s / (2 * sp.pi) if m == k else sp.Integer(0)
            if sp.simplify(lim - tgt) != 0:
                node_sym_ok = False
    check("G0.1 [E] exact machinery: iv.log(2) width %.1e < %.0e; "
          "sympy sin(m pi) == 0 (m <= 6) %s; anchor = log 16 (dev "
          "%.1e); EXACT node identities: 2 pi F_a(m pi/a) == 0 "
          "(m <= 6) %s and lim phi_k(m pi/a) == delta_km a/(2 pi) "
          "(k <= 3, m <= 3, symbolic a) %s"
          % (wid, BAR_IV_WIDTH, sym_sin_ok,
             abs(math.exp(a0) - n0), pin_sym_ok, node_sym_ok),
          wid < BAR_IV_WIDTH and sym_sin_ok and anchor_ok
          and pin_sym_ok and node_sym_ok)

    # G0.2 kernel machinery
    sq = np.linspace(-600.0, 600.0, 2400001)
    mass = float(np.trapezoid(two_pi_F(a0, sq), sq)) / (2.0 * math.pi)
    # tail of int F_a beyond 600: <= 2/(a s) / pi ~ 2/(pi a 600)
    tail_budget = 2.0 / (math.pi * a0 * 600.0)
    c_fej = np.zeros(9)
    c_fej[0] = math.pi * math.sqrt(2.0 / a0)
    st = np.linspace(0.0, 20.0, 40001)
    q_mem = phi_basis(a0, 8, st) @ c_fej
    dev_mem = float(np.max(np.abs(q_mem ** 2 - two_pi_F(a0, st)))) \
        / (2.0 * a0)
    rng = np.random.default_rng(11)
    c_rnd = rng.normal(size=9)
    # project to a vanishing band edge q-hat(+-a0) = sum (-1)^k c_k = 0
    # so that q ~ 1/s^2 and the quadrature truncation is negligible
    sgn = np.array([(-1.0) ** k for k in range(9)])
    c_rnd -= sgn * float(sgn @ c_rnd) / 9.0
    q_rnd = phi_basis(a0, 8, sq) @ c_rnd
    int_quad = float(np.trapezoid(q_rnd ** 2, sq))
    int_par = q_int_parseval(a0, c_rnd)
    dev_par = abs(int_quad - int_par) / int_par
    nodes8 = np.arange(1, 9) * math.pi / a0
    Phi_n = phi_basis(a0, 8, nodes8)
    tgt = np.zeros((8, 9))
    for m in range(1, 9):
        tgt[m - 1, m] = a0 / (2.0 * math.pi)
    dev_node = float(np.max(np.abs(Phi_n - tgt)))
    ml = np.linspace(-pia, pia, 200001)
    ml_mass = float(np.trapezoid(two_pi_F(a0, ml), ml)) / (2.0 * math.pi)
    check("G0.2 [E] kernel machinery: int F_a0 = %.8f (bar 1 +- %.0e "
          "+ tail %.1e); Fejer member |q|^2 == 2 pi F_a0 rel dev "
          "%.1e < %.0e; Parseval rel dev %.1e < %.0e; basis node "
          "values dev %.1e < %.0e; main-lobe mass %.4f (printed)"
          % (mass, BAR_MASS, tail_budget, dev_mem, BAR_MEMBER,
             dev_par, BAR_PARSEVAL, dev_node, BAR_NODE_NUM, ml_mass),
          abs(mass - 1.0) < BAR_MASS + tail_budget
          and dev_mem < BAR_MEMBER and dev_par < BAR_PARSEVAL
          and dev_node < BAR_NODE_NUM)

    # ------------------------------------------------ R1 reconstruction
    print("\nR1 -- the reconstructed bottleneck chain (formulas in the "
          "docstring; numbers here)")
    platt_bot = float(h_min_chain(np.array([T_PLATT - H_BRACKET[1]]),
                                  s_bound_platt)[0])
    h_hts = np.array([1e30, 1e60, 1e120, 1e300])
    h_be = h_min_chain(h_hts, s_bound_be)
    exc = h_be - FLOOR_BE
    exc_dec = bool(np.all(np.diff(exc) < 0.0) and np.all(exc > 0.0))
    # branch-1 prediction of the loglog/log-slow approach (printed)
    L300 = math.log(1e300 / TWO_PI)
    exc_pred = 4.0 * math.pi * (C2_BE * math.log(L300) + C3_BE) / L300
    tgrid = np.geomspace(10.0, 1e150, 400)
    h_global_min = float(np.min(h_min_all(tgrid)))
    print("   H_min asymptote (Bellotti branch): excess over the "
          "floor 4 pi A1 = %.5f: %s at t = 1e30/1e60/1e120/1e300 "
          "(branch-1 prediction at 1e300: %.4f)"
          % (FLOOR_BE, ["%.4f" % e for e in exc], exc_pred))
    check("R1.1 [E] H_min machinery (v678/v680 verbatim): Platt "
          "bottom H_min(3.06e10 - 400) = %.4f (quote %.4f +- %.2f); "
          "Bellotti-branch excess strictly decreasing to the floor "
          "(loglog/log-slow, excess(1e300) = %.4f <= 0.25, matches "
          "the branch-1 scale %.4f); global min over ALL chains/"
          "heights = %.4f >= 1.41 > pi/a0 = %.5f -- NO chain reaches "
          "the Fejer main lobe at any height (the v678 pinch "
          "statement)"
          % (platt_bot, REF_PLATT_BOT, BAR_PLATT_BOT, exc[-1],
             exc_pred, h_global_min, pia),
          abs(platt_bot - REF_PLATT_BOT) <= BAR_PLATT_BOT and exc_dec
          and exc[-1] <= 0.25
          and h_global_min >= 1.41 and h_global_min > pia)

    IV_THETA_F = 1 / (4 * IV_A0)
    IV_PINCH = (IV_A1 / IV_THETA_F - 1) * 100
    theta_f = iv_mid(IV_THETA_F)
    check("R1.2 [E] the frozen threshold: theta_req(Fejer) = z1/(4 pi)"
          " = 1/(4 a0) = %.7f (interval width %.1e), |. - %.5f| = "
          "%.1e <= %.0e (task quote); pinch = A1_BE/theta - 1 = "
          "%.4f%% (quote %.2f +- %.2f)"
          % (theta_f, iv_wid(IV_THETA_F), REF_THETA_F,
             abs(theta_f - REF_THETA_F), BAR_THETA_F, iv_mid(IV_PINCH),
             REF_PINCH_PCT, BAR_PINCH_PCT),
          abs(theta_f - REF_THETA_F) <= BAR_THETA_F
          and abs(iv_mid(IV_PINCH) - REF_PINCH_PCT) <= BAR_PINCH_PCT)

    IV_Z_NEED = 4 * IV_PI * IV_A1
    IV_Z_CEIL = IV_PI / IV_A0
    IV_NCRIT = iv.exp(iv.mpf(A1_DEN) / iv.mpf(4 * A1_NUM))
    check("R1.3 [E] the PASS bar, exact form: needs z1(K) > 4 pi A1 = "
          "%.6f; window form 1/(4 log n) > 2519/25000 <=> n < "
          "e^{25000/10076} = %.4f (certified < 12); anchor n = 16 "
          ">= 12 => the bar needs z1 > pi/a0 = %.6f, i.e. a kernel "
          "reaching PAST the first Fejer node"
          % (iv_mid(IV_Z_NEED), iv_mid(IV_NCRIT), iv_mid(IV_Z_CEIL)),
          iv_lt(IV_NCRIT, iv.mpf(12)) and iv_lt(IV_Z_CEIL, IV_Z_NEED))

    # ------------------------------------------------ K1 parametrization
    print("\nK1 -- the kernel class: K = |q|^2, q-hat = trig polynomial"
          " on [-a0, a0] (degree ladder %s, same type)"
          % (list(N_LADDER),))
    xw = np.linspace(-600.0, 600.0, 1200001)
    q32 = phi_basis(a0, 32, xw) @ rng.normal(size=33)
    K32 = q32 ** 2
    k0 = float(np.trapezoid(K32, xw))
    supp_dev = 0.0
    for u in (2.0 * a0 * 1.02, 2.0 * a0 * 1.2, 2.0 * a0 * 1.5):
        supp_dev = max(supp_dev, abs(float(
            np.trapezoid(K32 * np.cos(u * xw), xw))) / k0)
    check("K1.1 [E] parametrization: Fejer = the k = 0 (Dirichlet-"
          "constant) case (G0.2); degree ladder refines q-hat on the "
          "FIXED band [-a0, a0] -- K-hat support [-2a0, 2a0] for "
          "every N (random N = 32 member: |K-hat(u)|/K-hat(0) <= "
          "%.1e < %.0e for u/2a0 in {1.02, 1.2, 1.5}); the chain "
          "PINS the type at 2 a0 = %.4f (prime atoms n <= e^{2a0} = "
          "%d; larger type = exponentially more primes, v680 "
          "tau-scan, cited)"
          % (supp_dev, BAR_KHAT, 2.0 * a0,
             int(round(math.exp(2.0 * a0)))), supp_dev < BAR_KHAT)

    # ------------------------------------------------ K2 the adjudication
    print("\nK2 -- the pinning, the collapse, and the optimizer")
    IV_THETA_CEIL = IV_Z_CEIL / (4 * IV_PI)   # = 1/(4 a0)
    pin_ok = pin_sym_ok and iv_lt(IV_THETA_CEIL, IV_A1)
    check("K2.1 [EXACT, central] the pinning certificate: 2 pi F_a0("
          "m pi/a0) = 0 EXACTLY (sympy, G0.1) => every admissible K "
          "(0 <= K <= 2 pi F_a0 on R) has K(m pi/a0) = 0 => z1(K) <= "
          "pi/a0 => theta_req(K) <= 1/(4 a0) = %.7f < 0.10076 "
          "(strict interval certificate) -- the class supremum EQUALS"
          " the Fejer negative control; the PASS bar is unreachable "
          "in the ENTIRE class (any type <= 2a0, any degree)"
          % iv_mid(IV_THETA_CEIL), pin_ok)

    check("K2.2 [EXACT] the collapse: phi_k(m pi/a0) = delta_km a0/"
          "(2 pi) EXACTLY (sympy, G0.1) => the node constraints alone"
          " force c_1 = ... = c_N = 0 within EVERY degree N: q = c_0 "
          "phi_0 = Fejer multiple (finite, machine-exact); full class"
          " { q in PW^2_a0 } via Shannon sampling on the lattice "
          "pi/a0 (cited, typed ANALYTIC): feasible set = { lambda "
          "2 pi F_a0 : 0 <= lambda <= 1 } -- ZERO free directions",
          node_sym_ok and pin_sym_ok)

    # K2.3 LP ladder, honest grids
    print("   LP ladder (honest node-containing grids; bisection over"
          " the capture reach z):")
    lp_ok = True
    for N in N_LADDER:
        t1 = time.time()
        z_st, t_st, c_st = bisect_zstar(a0, N, node_blind=False)
        if z_st is None:
            lp_ok = False
            print("   N = %2d: LP infeasible at z_lo -- unexpected"
                  % N)
            continue
        coef_tail = float(np.max(np.abs(c_st[1:]))) if N >= 1 else 0.0
        # post-hoc tail scan of the winner
        s_tail = np.linspace(S_MAX_LOBES * pia, TAIL_LOBES * pia,
                             200001)
        q_tail = phi_basis(a0, N, s_tail) @ c_st
        viol_tail = float(np.max(q_tail ** 2 - two_pi_F(a0, s_tail)))
        ok_N = (pia - z_st <= BAR_ZSTAR and z_st <= pia + 1e-9
                and coef_tail <= BAR_COLLAPSE
                and viol_tail <= BAR_TAIL_VIOL)
        lp_ok = lp_ok and ok_N
        print("   N = %2d: z* = %.9f (pi/a0 - z* = %+.2e), floor t* ="
              " %.2e, ||c_1..N||_inf = %.1e, tail viol %.1e  [%.1f s]"
              % (N, z_st, pia - z_st, t_st, coef_tail, viol_tail,
                 time.time() - t1))
    check("K2.3 [MEASURED] LP ladder on honest grids: z*(N) = pi/a0 "
          "from below (within %.0e) for all N in %s; coefficient "
          "collapse ||c_1..N||_inf <= %.0e (the node constraints "
          "force Fejer); winner tail violation <= %.0e on [%d, %d] "
          "lobes -- the optimizer CONFIRMS the exact collapse: no "
          "degree buys a single micron past the first Fejer node"
          % (BAR_ZSTAR, list(N_LADDER), BAR_COLLAPSE, BAR_TAIL_VIOL,
             S_MAX_LOBES, TAIL_LOBES), lp_ok)

    # K2.4 the false-PASS trap (node-blind grid)
    print("   node-BLIND grid (all points within %.2f of a node "
          "removed) at N = 8:" % EXCL_ADV)
    z_adv, t_adv, c_adv = bisect_zstar(a0, 8, node_blind=True,
                                       z_hi_fact=2.6)
    if z_adv is None:
        adv_ok = True
        print("   adversarial LP infeasible -- no inflation")
    else:
        theta_adv = z_adv / (4.0 * math.pi)
        infl = z_adv > pia + 1e-6
        print("   apparent z*_adv = %.6f (pi/a0 = %.6f), apparent "
              "theta = %.6f vs 0.10076 -> apparent %s"
              % (z_adv, pia, theta_adv,
                 "PASS (!)" if theta_adv > C1_BE else "fail"))
        if infl:
            c_rat = [Fraction(float(x)).limit_denominator(10 ** 9)
                     for x in c_adv]
            viol = [abs(float(c_rat[m])) for m in range(1, 9)]
            m_max = int(np.argmax(viol)) + 1
            v_max = max(viol)
            # exact statement: q(m pi/a0) = c_m a0/(2 pi) with the
            # RATIONALIZED c_m; the bound there is exactly 0 (sympy).
            q_node = float(c_rat[m_max]) * a0 / (2.0 * math.pi)
            print("   exact refutation: rationalized c_%d = %s != 0 "
                  "=> q(%d pi/a0) = c_%d a0/(2 pi) = %.6f, K(node) = "
                  "%.3e > 0 = 2 pi F_a0(node) EXACTLY (sympy) -- the "
                  "apparent pass is an artifact of the blind grid"
                  % (m_max, str(c_rat[m_max].limit_denominator(10**4)),
                     m_max, m_max, q_node, q_node ** 2))
            adv_ok = v_max > BAR_REFUTE
        else:
            adv_ok = True
    check("K2.4 [MEASURED] the false-PASS trap: a node-blind "
          "constraint grid inflates the apparent reach%s; the "
          "rationalized winner is EXACTLY infeasible at a pinned "
          "node (K(m pi/a0) > 0 = bound) -- exact re-verification of "
          "the target bounds kills every apparent gain, as the task "
          "demands" % ("" if z_adv is None else
                       " to z* = %.3f" % z_adv), adv_ok)

    # K2.5 the review's ratio functional theta_ibp
    print("\n   theta_ibp = (int K)/(2 pi TV(K)) -- the review's "
          "mass-vs-S(t)-penalty functional:")

    def tanroot(k):
        f = lambda x: np.sin(x) - x * np.cos(x)
        return brentq(f, k * math.pi + 0.5,
                      k * math.pi + 0.5 * math.pi - 1e-12,
                      xtol=1e-14)
    roots = np.array([tanroot(k) for k in range(1, N_TANROOTS + 1)])
    side_sum = float(np.sum(1.0 / (1.0 + roots ** 2)))
    tail_sum = 1.0 / (math.pi ** 2 * N_TANROOTS)   # sum_{k>K} < int
    tv_fact = 1.0 + 2.0 * side_sum
    theta_ibp_f = 1.0 / (4.0 * a0 * tv_fact)
    theta_ibp_f_lo = 1.0 / (4.0 * a0 * (1.0 + 2.0 * (side_sum
                                                     + tail_sum)))
    print("   Fejer: TV factor 1 + 2 sum 1/(1+x_k^2) = %.8f (tail "
          "budget %.1e); theta_ibp(2 pi F_a0) = %.7f in [%.7f, %.7f]"
          % (tv_fact, tail_sum, theta_ibp_f, theta_ibp_f_lo,
             theta_ibp_f))
    # SLSQP confirmation at N = 8 on the node grid
    sg_o, nodes_o = make_global_grid(a0)
    keep = sg_o <= 12.0 * pia
    sg_o = sg_o[keep]
    Phi_o = phi_basis(a0, 8, sg_o)
    g_o = g_sqrt(a0, sg_o)
    s_tv = np.linspace(0.0, 12.0 * pia, 24001)
    Phi_tv = phi_basis(a0, 8, s_tv)

    def neg_theta(cf):
        c = np.concatenate([[math.pi / a0], cf])
        K = (Phi_tv @ c) ** 2
        tv = 2.0 * grid_tv(K)      # even kernel: full-line TV
        return -q_int_parseval(a0, c) / (2.0 * math.pi * max(tv, 1e-14))

    cons = ({"type": "ineq",
             "fun": lambda cf: g_o - np.abs(
                 Phi_o @ np.concatenate([[math.pi / a0], cf]))},)
    res_o = minimize(neg_theta, 0.02 * rng.normal(size=8),
                     method="SLSQP", constraints=cons,
                     options={"maxiter": 300, "ftol": 1e-12})
    theta_opt = -float(res_o.fun)
    coef_opt = float(np.max(np.abs(res_o.x)))
    print("   SLSQP (N = 8, node grid): theta_ibp(opt) = %.7f "
          "(Fejer %.7f), ||c_1..8||_inf = %.1e -- collapse confirmed"
          % (theta_opt, theta_ibp_f, coef_opt))
    # relaxed BV class: the rational box instance
    IV_BOX_TH = iv.mpf(BOX_DELTA.numerator) \
        / (iv.mpf(BOX_DELTA.denominator) * 4 * IV_PI)
    IV_BOX_H = 2 * iv.sin(IV_A0 * iv.mpf(15) / iv.mpf(16)) ** 2 \
        / (IV_A0 * (iv.mpf(15) / iv.mpf(16)) ** 2)
    IV_THETA_C = 1 / (2 * IV_A0)
    box_ok = iv_gt(IV_BOX_TH, IV_A1) and mp.mpf(IV_BOX_H.a) > 0 \
        and iv_gt(IV_THETA_C, IV_A1)
    print("   relaxed BV class (minorant NOT band-limited -- the "
          "review's mechanism realized): box delta = %s: theta_ibp ="
          " delta/(4 pi) = %.6f > 0.10076 (certified), height 2 pi "
          "F_a0(15/16) = %.6f > 0 (certified); thin-box supremum = "
          "1/(2 a0) = %.6f = the v680 BOX-COUNT threshold; centered "
          "capture (bookkeeping x2, v680): 1/(2 a0) > 0.10076 "
          "(certified)"
          % (BOX_DELTA, iv_mid(IV_BOX_TH), iv_mid(IV_BOX_H),
             iv_mid(IV_THETA_C)))
    check("K2.5 [MEASURED/EXACT] theta_ibp: class supremum = the "
          "Fejer value %.5f (by the K2.2 collapse; BELOW even the "
          "one-sided box %.5f -- the raw Fejer pays side-lobe TV); "
          "SLSQP confirms (opt %.5f <= Fejer + %.0e, coef collapse "
          "%.1e <= %.0e); the review's mechanism DOES work but only "
          "OUTSIDE the class: rational box certificate %.5f > "
          "0.10076 and supremum 1/(2 a0) -- i.e. the already-"
          "promoted v680 route, not a same-type kernel"
          % (theta_ibp_f, theta_f, theta_opt, BAR_IBP_OPT, coef_opt,
             BAR_IBP_COEF, iv_mid(IV_BOX_TH)),
          theta_opt <= theta_ibp_f + BAR_IBP_OPT
          and coef_opt <= BAR_IBP_COEF and box_ok)

    # ------------------------------------------------ K3 certification
    print("\nK3 -- exact certification of the class optimum")
    # q* = phi_0 with RATIONAL coefficient vector (1, 0, ..., 0)
    lam_star = a0 / (2.0 * math.pi ** 2)
    z1_sym = sp.pi / a_s   # first positive zero of sin(a s)/(pi s)
    q_star_sym = sp.sin(a_s * s_s) / (sp.pi * s_s)
    z1_exact_ok = sp.simplify(q_star_sym.subs(s_s, z1_sym)) == 0
    # admissibility of q*^2: sin^2/(pi^2 s^2) <= 2 sin^2/(a s^2)
    # <=> a <= 2 pi^2: exact for a0 = log 16 < 2 pi^2
    adm_ok = iv_lt(IV_A0, 2 * IV_PI ** 2)
    check("K3.1 [EXACT] the class optimum certified: q* = phi_0 = "
          "sin(a0 s)/(pi s), RATIONAL coefficients c = (1, 0, ..., 0)"
          " (SOS by construction, K* = |q*|^2 = lambda 2 pi F_a0, "
          "lambda = a0/(2 pi^2) = %.5f, admissible since a0 < 2 pi^2"
          " certified); z1(K*) = pi/a0 EXACT (sympy: q*(pi/a0) = 0); "
          "theta_req(K*) = 1/(4 a0) = %.7f (interval, width %.1e)"
          % (lam_star, theta_f, iv_wid(IV_THETA_F)),
          z1_exact_ok and adm_ok)

    IV_RATIO = IV_THETA_CEIL / IV_A1
    IV_GAP = IV_A1 - IV_THETA_CEIL
    pass_bar_met = iv_gt(IV_THETA_CEIL, IV_A1)   # False expected
    check("K3.2 [EXACT] PASS adjudication: class supremum theta = "
          "%.7f vs required 0.10076: %s (strict interval); margins: "
          "sup/needed = %.5f (%.2f%% below), needed/sup - 1 = %.3f%%"
          " (the 11.7%% pinch re-derived exactly), absolute gap "
          "%.7f -- the acceptance bar 'Schwelle >= 0.10076' FAILS "
          "for the WHOLE class"
          % (iv_mid(IV_THETA_CEIL),
             "PASS" if pass_bar_met else "FAIL",
             iv_mid(IV_RATIO), 100.0 * (1.0 - iv_mid(IV_RATIO)),
             iv_mid(IV_PINCH), iv_mid(IV_GAP)),
          iv_lt(IV_THETA_CEIL, IV_A1))

    # ------------------------------------------------ K4 controls
    print("\nK4 -- controls")
    check("K4.1 [E] Fejer frozen negative control: theta_req(Fejer) "
          "= %.7f reproduces the old threshold %.5f (dev %.1e <= "
          "%.0e) and the pinch %.3f%% (quote %.2f +- %.2f) -- the "
          "task's control bar"
          % (theta_f, REF_THETA_F, abs(theta_f - REF_THETA_F),
             BAR_THETA_F, iv_mid(IV_PINCH), REF_PINCH_PCT,
             BAR_PINCH_PCT),
          abs(theta_f - REF_THETA_F) <= BAR_THETA_F
          and abs(iv_mid(IV_PINCH) - REF_PINCH_PCT) <= BAR_PINCH_PCT)

    # family selection (v680 B2 verbatim)
    zones = core.frame_a_zones()
    fam = []
    for kz in zones:
        alpha = float(core.U_ALL[kz])
        complete = math.exp(2.0 * alpha) <= core.ATOM_MAX + 0.5
        D_k = 0.5 * float(core.G_ALL[kz]) / float(core.NU_MAIN)
        Mz = int(math.ceil(alpha / D_k - 1.0e-9)) + 1
        if Mz % 2:
            Mz += 1
        fam.append((kz, alpha, Mz // 2, complete))
    comp = [t_ for t_ in fam if t_[3]]
    hs_c = np.array([t_[2] for t_ in comp], float)
    picks = [comp[0]]
    for qq in (0.25, 0.5, 0.75, 1.0):
        tgt_q = float(np.quantile(hs_c, qq))
        cand = min(comp, key=lambda t_: abs(t_[2] - tgt_q))
        if all(cand[0] != p[0] for p in picks):
            picks.append(cand)
    picks = sorted(picks, key=lambda t_: t_[2])
    IV_NCRIT_C = iv.exp(iv.mpf(A1_DEN) / iv.mpf(2 * A1_NUM))
    print("   per-window exact ceilings (a_w = log n_w; ceiling = "
          "1/(4 log n_w); centered = 1/(2 log n_w), v680 concordance"
          " n_w < e^{12500/2519} = %.2f):" % iv_mid(IV_NCRIT_C))
    fam_ok = True
    n_fail_c = 0
    for (kz, aw, hw, _c) in picks:
        nw = int(round(math.exp(aw)))
        if abs(math.exp(aw) - nw) > 1e-6 * nw:
            fam_ok = False
        IV_AW = iv.log(iv.mpf(nw))
        IV_TH_W = 1 / (4 * IV_AW)
        IV_TH_WC = 1 / (2 * IV_AW)
        ceil_fail = iv_lt(IV_TH_W, IV_A1)      # must be True (< 0.10076)
        cen_pass = iv_gt(IV_TH_WC, IV_A1)
        if not cen_pass:
            n_fail_c += 1
        fam_ok = fam_ok and ceil_fail
        print("   n = %4d (a = %.4f, h = %4d): ceiling %.6f < "
              "0.10076 certified %s | centered %.6f -> %s"
              % (nw, aw, hw, iv_mid(IV_TH_W), ceil_fail,
                 iv_mid(IV_TH_WC),
                 "passes" if cen_pass else "FAILS (Selberg-only, "
                 "v680)"))
    check("K4.2 [E] transportability: the pinning + collapse hold "
          "verbatim per window (feasible set = { lambda 2 pi F_{a_w}"
          " }); exact per-window ceilings 1/(4 log n_w) ALL < "
          "0.10076 (integer criterion n_w >= 12, all n_w >= 16); NO "
          "window tuning is possible (zero shape freedom), so the "
          "one-kernel-for-all-windows requirement holds vacuously; "
          "%d window(s) also fail the CENTERED threshold (v680 "
          "concordance: Selberg-only territory)" % n_fail_c, fam_ok)

    check("K4.3 [C] supremum honesty (the task's K4 question 'wie "
          "nah kommt man?'): the class supremum is ATTAINED -- by "
          "Fejer itself, exactly at the old threshold %.7f; distance"
          " to the PASS bar: -%.2f%%; the class has ZERO interior "
          "directions ({ lambda Fejer } only) -- the kernel class "
          "(same type, K >= 0, minorizing the window symbol) is "
          "PRINCIPALLY TOO NARROW: the anticipated structural "
          "negative, upgraded from measurement to exact statement"
          % (theta_f, 100.0 * (1.0 - iv_mid(IV_RATIO))), True)

    # ------------------------------------------------ verdict + note
    guards_ok = not any(f.startswith(("G0", "R1")) for f in FAILS)
    exact_ok = not any(f.startswith(("K2.1", "K2.2", "K3.1", "K3.2"))
                       for f in FAILS)
    ctrl_ok = not any(f.startswith(("K4.1", "K4.2")) for f in FAILS)
    if not guards_ok:
        VERDICT = "KERNEL-MIXED (guards failed)"
    elif pass_bar_met:
        VERDICT = "KERNEL-THRESHOLD-PASSED"
    elif exact_ok and ctrl_ok:
        VERDICT = "KERNEL-CLASS-TOO-NARROW"
    else:
        VERDICT = "KERNEL-NO-GAIN"

    check("K5.1 [C] typed reading: (i) the bottleneck is EXACTLY "
          "reconstructed (one-sided zero-gap capture into the Fejer "
          "main lobe; A1 enters only through H_min -> 4 pi A1; "
          "required threshold theta_req(K) = z1(K)/(4 pi)); (ii) the"
          " kernel lever is EMPTY: the transfer constraint 0 <= K <="
          " 2 pi F_a pins K at every Fejer node (exact), Shannon "
          "sampling collapses the class to { lambda 2 pi F_a } -- "
          "supremum = 1/(4 a0) = %.5f < 0.10076, certified; (iii) "
          "the optimizer confirms at every degree and the node-blind"
          " apparent pass is exactly refuted; (iv) the review's "
          "mass-vs-penalty mechanism is real but lives in the "
          "NON-band-limited minorant class (box: %.5f > 0.10076 "
          "certified, sup 1/(2 a0) = %.5f) = the v680 BOX-COUNT/"
          "centered route, already promoted; (v) W2 pointwise "
          "remains closed via v680/v681 (centered + Selberg + scan);"
          " THIS class cannot re-close it single-handedly; no marker"
          " move" % (theta_f, iv_mid(IV_BOX_TH), iv_mid(IV_THETA_C)),
          True)

    print("\nVERDICT: %s" % VERDICT)
    print("""
CONTRACT-NOTE TEXT (report only -- nothing is written by this module):

  PRIME.W2.EXTREMAL.KERNEL.01 (2026-08-03): the review's extremal-
  kernel attack on the 11.7%% W2 pointwise gap was set up EXACTLY and
  adjudicated EXACTLY.  Bottleneck chain (reconstructed, R1): the
  one-sided zero-gap capture s_tot(t) >= 2 pi min_(0,H] F_a needs
  H_min(t) -> 4 pi A1 < z1(kernel); with the Fejer kernel z1 = pi/a0,
  threshold A1 < 1/(4 a0) = %.5f vs Bellotti A1 = 0.10076 (miss
  11.75%%).  THE KERNEL LEVER IS EMPTY -- EXACT: any replacement
  kernel K = |q|^2 of type <= 2 a0 must minorize the window symbol
  (0 <= K <= 2 pi F_a0 on R, else the transfer to s_tot fails on
  adversarial zeros), and 2 pi F_a0 vanishes at every node m pi/a0
  EXACTLY, so K is pinned there, z1(K) <= pi/a0, and by Nyquist/
  Shannon sampling on the lattice pi/a0 the whole class collapses to
  { lambda 2 pi F_a0 : 0 <= lambda <= 1 } -- ZERO free directions;
  the supremum of the required threshold over the class is the Fejer
  value itself, %.5f, i.e. 10.51%% BELOW the needed 0.10076 (strict
  interval certificates; SOS trivially by construction).  The LP
  optimizer over q-hat trig-polynomial coefficients (degrees 8/16/32,
  bisection over the capture reach) confirms: z* = pi/a0 to 5e-4 at
  every degree, coefficient collapse to Fejer at 1e-7; a node-blind
  constraint grid produces an APPARENT pass which the demanded exact
  re-verification refutes at the first pinned node (K(pi/a0) > 0 =
  bound, exact).  The review's acceptance criteria, adjudicated:
  'exakte SOS-Zertifizierung' -- PASS (the class optimum is certified
  with rational coefficients); 'Schwelle >= 0.10076' -- FAIL, exactly
  and for the whole class (sup = %.5f); 'komplette punktweise
  Garding-Schranke' -- NOT through this class (it stands via
  v680/v681: centered capture + Selberg + scan map); 'keine Fenster-
  Anpassung' -- vacuously satisfied (the class has no shape freedom;
  per-window ceilings 1/(4 log n_w) all < 0.10076, exact integer
  criterion n_w >= 12).  The review's MECHANISM ('more main-lobe
  mass, less S(t) boundary penalty') is real but lives in the
  non-band-limited BV-minorant class, where its optimum is the thin
  main-lobe box: theta = delta/(4 pi) -> 1/(2 a0) = %.5f, certified
  rational instance delta = 15/8: theta = 15/(32 pi) = %.5f > 0.10076
  -- exactly the v680 BOX-COUNT/centered route already promoted.
  TYPE: exact structural theorem (pinning + sampling collapse) +
  interval certificates + LP confirmation; the 11.7%% gap is NOT
  closable by same-type kernel optimization -- it was closed by
  bookkeeping and non-band-limited minorants (v680/v681), and W2
  pointwise stays closed on that route, unchanged.  An honest typed
  door remains: budgeted LOCAL minorization (allowing K > 2 pi F_a
  on a zero-counted exceptional set) -- it re-imports the counting
  cost and is NOT covered by this module.  VERDICT %s.
""" % (theta_f, theta_f, theta_f, iv_mid(IV_THETA_C),
       iv_mid(IV_BOX_TH), VERDICT))

    print("checks: %d, failures: %d %s" % (N_CHK, len(FAILS),
                                           FAILS or ""))
    print("elapsed: %.1f s" % (time.time() - T0))
    return len(FAILS)


if __name__ == "__main__":
    raise SystemExit(1 if run() else 0)
