#!/usr/bin/env python3
"""v728 -- PRIME.K1BPIN.01: K1b SLICE -- WHY do the Gauss nodes
sit ~900x below the classical capture radius (K1a verdict
K1-LEMMA-CAPTURE-ONLY, certified d_pred ~ 1.4-2.4 tau vs measured
0.0003-0.03)?

CANDIDATE MECHANISM UNDER TEST (the memo's classical explanation):
  (1) LOCALITY: the K-point global Gauss rule around a peak
      coincides with the small n-point Gauss rule of the LOCAL
      measure on a window of a few node spacings (n = number of
      global nodes in the window) -- Christoffel localization at
      scale 1/K' ~ 4 node spacings.
      CALIBRATION HISTORY (declared, v678 pattern): run 1 tested
      locality on SINGLE cells (between adjacent symbol minima) --
      FAILED honestly (corr 0.039, median residual 0.131 vs node
      scatter 0.034): with ~1 node per cell the 1-point local rule
      is the full-mass cell centroid, but the node does NOT
      represent its cell's flank background (that mass is shared
      with the gap nodes).  Recalibrated ONCE to multi-spacing
      windows matched to the localization length, with an R-ladder
      (R = 1..5 node spacings) reported as the measured
      localization profile; the primary bar sits at R = 3.
      Numerics moved from Hankel/Cholesky to Chebyshev modified
      moments (Wheeler) for local sizes n up to 12.
  (2) SYMMETRY: for the SYMMETRIZED local measure (odd local moments
      removed) the local rule is symmetric about the peak -- a
      central node is pinned at the center to higher order; the node
      error is driven by the ASYMMETRY (odd moments), not the width.
  (3) BUDGET: the odd moments have exactly two sources, (a) the
      smooth-layer density gradient through the cell, (b) neighbor-
      peak imbalance/leakage -- both measurable from the symbol,
      zeta-free, with their own h-scaling.

DIAGNOSTIC TYPING (hard, E4 pattern): all predictions (cells, local
moments, local-rule nodes, symmetrized-rule nodes, centroids) are
functions of the window symbol and the frozen node set only; SHA256
printed BEFORE the declared target load (e4.declared_zero_targets).

MEASUREMENTS (bars declared before any number):
  B1  [E, zeta-free identity] LOCALITY on the real windows: local-
      rule node nearest each peak (window +-3 node spacings) vs the
      actual global node -- corr >= 0.9, slope in [0.7, 1.3],
      median |local - global| <= 0.3 x median |node - peak|
      (largest window); the R-ladder corr(R), R = 1..5, printed as
      the measured localization profile.
  B2.1 [M] SYMMETRY MECHANISM: median |node_S - peak| (symmetrized
      local rule) vs |node_F - peak| (full local rule); the
      asymmetry activation corr(node_F - node_S, measured node -
      peak); odd/even local node parity split reported.
  B2.2 [M] TARGET TEST (after the load): corr + slope of the
      predicted signed error (local node - gamma) vs the measured
      (global node - gamma) over all band zeros, largest window.
  B2.3 [M] THE RATE: ladder slopes (first 20 zeros) of measured
      |node - gamma| (E4: -1.61) vs predicted |local node - gamma|
      vs the bg-subtracted peak centroid; classical iff |slope_pred
      - slope_meas| <= 0.5.
  B2.4 [M] CENTROID COMPARISON: the perturbation picture must
      reproduce the K1a side-finding (bg-subtracted centroid beats
      the node): medians |node-g| / |pred-g| / |centroid-g|.
  B2.5 [M] ASYMMETRY BUDGET: per-cell odd moment m1 split into
      density-gradient part (linear background through the cell-edge
      minima) and neighbor/leakage rest; medians + ladder h-slopes
      of both -- the honest answer to "how does each source fall
      with h".
  B2.6 [M] DIRECT FIRST-ORDER TEST (fair to the memo, no local-rule
      detour): corr of the measured signed error with the raw
      asymmetry functionals (odd moment m1, gradient part, centroid
      shift) -- if these are ~0 too, the asymmetry mechanism is
      dead independently of the locality model.
  B2.7 [M] ATOM-PINNING ALTERNATIVE (the competing classical
      mechanism): the raw window measure is near-atomic (atoms at
      the zeros + smooth layer; the Fejer symbol is only the
      smoothed visualization).  Synthetic identity lab: atoms +
      smooth background with EXACT moments, K-ladder -> measured
      pinning rate |node - atom|(K) and node-weight ratio; plus the
      real-data correlate |node - gamma| vs local background/mass.
  B3  [M, toy] OFF-LINE BRIDGE: one synthetic peak's moment channel
      modulated by cosh(delta d / mF) (the lag signature of an
      off-line pair): Levinson breakdown depth vs delta (must be
      nonincreasing) + pre-breakdown node shift -- the falsifier
      direction quantified (declared toy, NOT the v688 form).
  VERDICT: K1B-CLASSICAL iff B1 bars pass AND B2.2 corr >= 0.8 AND
      the B2.3 rate matches within 0.5 (the memo mechanism);
      K1B-ATOM-PINNING if the memo mechanism fails but the atom
      lab pins with median |node - atom| <= 1e-3 at K = 400 and a
      K-rate <= -1.3 (carries the super-resolution classically);
      K1B-OPEN otherwise.

PROVENANCE: discovery probe k1b_superresolution_probe.py (2026-08-03,
11/13 PASS, verdict K1B-ATOM-PINNING; the two failures B1.1 (memo
locality) and B2.2 (asymmetry target) are PREREGISTERED memo bars that
die honestly -- the memo's classical locality/symmetry mechanism is
dead (corr ~ 0), the competing classical mechanism CARRIES the scale:
atom pinning of the raw measure (synthetic lab rate K^-1.84, node
weight ratio -> 1.03, real-data correlate 0.64 of log|node - gamma|
with log(eps sp/m)); off-line pairs break POSITIVITY (Levinson death)
instead of mislocating nodes.  Promoted verbatim except run(): the
preregistered failure set {B1.1, B2.2} with verdict K1B-ATOM-PINNING
is the EXPECTED promoted outcome (exit 0); any other pattern fails.
Sibling imports now point at v696/v718/v721; numbers unchanged.
"""
import ast
import hashlib
import math
import os
import sys
import time

import numpy as np
import scipy.linalg as sla

_here = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, _here)
sys.path.insert(0, os.path.abspath(os.path.join(_here, "..", "..",
                                                "verification")))

import v721_k1_node_capture as k1  # noqa: E402  (K1a lab, read-only)
import v718_moonshot_spectral as e4  # noqa: E402
import v696_z1_jacobi as jac  # noqa: E402

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

NG = k1.NG
BAND_LO = k1.BAND_LO
BAND_FRAC = k1.BAND_FRAC
K_FIRST = 20
DELTAS = (0.3, 0.6, 1.0, 1.5, 2.0)

BANNED = ("zetazero", "nzeros", "second_sheet_zero", "isprime",
          "primerange", "sympy")


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_firewall(src_path):
    with open(src_path, "r", encoding="utf-8") as fh:
        tree = ast.parse(fh.read())
    for node in ast.walk(tree):
        nm = ""
        if isinstance(node, ast.Name):
            nm = node.id
        elif isinstance(node, ast.Attribute):
            nm = node.attr
        if nm and any(b in nm.lower() for b in BANNED):
            return False
    return True


def cheb_moments(dens, xi, dtau, nmom):
    """Chebyshev moments t_d = int T_d(xi) dmu_loc from grid data."""
    T0_ = np.ones_like(xi)
    T1_ = xi.copy()
    out = [float(np.sum(dens * T0_) * dtau),
           float(np.sum(dens * T1_) * dtau)]
    for _d in range(2, nmom):
        T0_, T1_ = T1_, 2.0 * xi * T1_ - T0_
        out.append(float(np.sum(dens * T1_) * dtau))
    return np.array(out)


def local_gauss_cheb(t, n):
    """Nodes (xi in [-1, 1]) of the n-point Gauss rule of the local
    measure given its Chebyshev moments t_d (Wheeler; n reduced on
    rank loss)."""
    while n >= 1:
        aM, gM, kbad = jac.wheeler(t[:2 * n], n)
        if kbad is None and np.all(gM[1:n] > 0):
            ev = sla.eigh_tridiagonal(aM.copy(),
                                      np.sqrt(gM[1:n]))[0]
            return ev / 2.0
        n = (kbad if kbad is not None else n) - 1
    return None


def corr_slope(x, y):
    x = np.asarray(x, float)
    y = np.asarray(y, float)
    c = float(np.corrcoef(x, y)[0, 1])
    s = float(np.sum(x * y) / max(np.sum(x * x), 1e-300))
    return c, s


def ladder_slope(hs, vals):
    lh = np.log(hs)
    A = np.vstack([lh, np.ones_like(lh)]).T
    return float(np.linalg.lstsq(A, np.log(vals), rcond=None)[0][0])


# ------------------------------------------------- per-window analysis
R_MAIN = 3.0                 # primary locality window (node spacings)
R_LIST = (1.0, 2.0, 3.0, 4.0, 5.0)
N_LOC_CAP = 12


def local_predict(sF, tau_g, dtau, tau_n, tpk, R, sp):
    """Local-rule prediction around peak tpk with window +-R*sp:
    returns (nd_full, nd_symm, n_loc) or None."""
    W = R * sp
    i0 = int(round(tpk / dtau))
    wg = int(round(W / dtau))
    if i0 - wg < 0 or i0 + wg >= len(sF) or wg < 4:
        return None
    n_loc = int(np.sum(np.abs(tau_n - tpk) <= W))
    if n_loc < 1:
        return None
    n_use = min(n_loc, N_LOC_CAP)
    sl = slice(i0 - wg, i0 + wg + 1)
    xi = (tau_g[sl] - tpk) / W
    dens = np.maximum(sF[sl], 0.0)
    t = cheb_moments(dens, xi, dtau, 2 * n_use)
    xf = local_gauss_cheb(t, n_use)
    ts = t.copy()
    ts[1::2] = 0.0
    xs = local_gauss_cheb(ts, n_use)
    if xf is None or xs is None:
        return None
    nd_f = tpk + W * float(xf[np.argmin(np.abs(xf))])
    nd_s = tpk + W * float(xs[np.argmin(np.abs(xs))])
    return nd_f, nd_s, n_loc


def analyze_window(w, dth, th_g):
    """Peaks, locality predictions (R ladder), centroids, asymmetry
    budget -- all zeta-free.  Stored on w."""
    K = w["M"] // 2
    th_n, w_n = k1.gauss_theta(w["p"], K)
    w["tau_n"] = th_n / w["D"]
    M = w["M"]
    dd = np.arange(M)
    sF = k1.cos_series_grid(w["p"][:M] * (1.0 - dd / M))[:NG // 2 + 1]
    tau_g = th_g / w["D"]
    dtau = dth / w["D"]
    hi = BAND_FRAC * math.pi / w["D"]
    band = (tau_g >= BAND_LO) & (tau_g <= hi)
    lobe_th = 2.0 * math.pi / M
    pk = k1.find_peaks_simple(sF, band, max(2, int(0.5 * lobe_th
                                                   / dth)))
    # refined peak positions + bg-subtracted centroids (K1a object)
    w2c = max(3, int(0.75 * lobe_th / dth))
    rows = []
    tau_n = w["tau_n"]
    for i in pk:
        de = 0.5 * (sF[i - 1] - sF[i + 1]) / \
            (sF[i - 1] - 2.0 * sF[i] + sF[i + 1])
        tpk = (i + de) * dth / w["D"]
        lo, hi_i = max(0, i - w2c), min(len(sF), i + w2c + 1)
        seg = sF[lo:hi_i] - float(np.min(sF[lo:hi_i]))
        ct = float(np.sum(seg * np.arange(lo, hi_i))
                   / max(np.sum(seg), 1e-300)) * dth / w["D"]
        # local node spacing around the peak
        j = int(np.searchsorted(tau_n, tpk))
        jl, jr = max(0, j - 4), min(len(tau_n), j + 4)
        if jr - jl < 3:
            continue
        sp = float(np.median(np.diff(tau_n[jl:jr])))
        res = local_predict(sF, tau_g, dtau, tau_n, tpk, R_MAIN, sp)
        if res is None:
            continue
        nd_f, nd_s, n_loc = res
        # R-ladder predictions (diagnostic)
        rl = {}
        for R in R_LIST:
            r2 = local_predict(sF, tau_g, dtau, tau_n, tpk, R, sp)
            rl[R] = r2[0] if r2 is not None else float("nan")
        # asymmetry budget on the +-1-spacing window
        wg1 = int(round(sp / dtau))
        i0 = int(round(tpk / dtau))
        if i0 - wg1 >= 0 and i0 + wg1 < len(sF) and wg1 >= 4:
            sl1 = slice(i0 - wg1, i0 + wg1 + 1)
            u = tau_g[sl1] - tpk
            dens1 = np.maximum(sF[sl1], 0.0)
            m0 = float(np.sum(dens1) * dtau)
            m1 = float(np.sum(dens1 * u) * dtau) / m0
            rho1 = (float(sF[i0 + wg1]) - float(sF[i0 - wg1])) \
                / (2.0 * sp)
            m1_grad = rho1 * (2.0 * sp ** 3 / 3.0) / m0
            eps_loc = float(np.min(sF[sl1]))
        else:
            m1 = m1_grad = eps_loc = float("nan")
        m_est = float(np.sum(seg)) * dtau     # bg-subtracted mass
        rows.append(dict(tpk=tpk, ct=ct, sp=sp, n_loc=n_loc,
                         nd_f=nd_f, nd_s=nd_s, rl=rl, m1=m1,
                         m1_grad=m1_grad, m1_nb=m1 - m1_grad,
                         m_est=m_est, eps=eps_loc))
    w["cells"] = rows
    w["hi"] = hi
    return sF


# ================================================================ main
def run():
    print("=" * 78)
    print("K1b SLICE -- the super-resolution mechanism (locality + "
          "symmetry perturbation)")
    print("=" * 78)
    check("B0.0 [E] AST firewall: no zero/prime loader symbol; "
          "targets only via the declared e4 section after the "
          "freeze", ast_firewall(os.path.abspath(__file__)))

    dth = 2.0 * math.pi / NG
    th_g = np.arange(NG // 2 + 1) * dth
    wins = e4.family_ext()
    print("   family: %s" % ", ".join("h=%d" % (w["M"] // 2)
                                      for w in wins))
    hsh = hashlib.sha256()
    for w in wins:
        analyze_window(w, dth, th_g)
        for r in w["cells"]:
            hsh.update(np.round(np.array(
                [r["tpk"], r["nd_f"], r["nd_s"], r["ct"], r["m1"]]),
                9).tobytes())
        hsh.update(np.round(w["tau_n"], 9).tobytes())
    n_cells = sum(len(w["cells"]) for w in wins)
    print("   %d peak cells analyzed (all windows), predictions "
          "frozen" % n_cells)

    # ---------------- B1: locality identity (zeta-free, no targets)
    print("\nB1 -- locality: local n-point rule vs actual global "
          "node (zeta-free identity test)")
    wL = wins[-1]
    dloc, dnp_ = [], []
    par_odd = 0
    for r in wL["cells"]:
        tn = wL["tau_n"]
        g_node = float(tn[np.argmin(np.abs(tn - r["tpk"]))])
        dloc.append(r["nd_f"] - g_node)
        dnp_.append(g_node - r["tpk"])
        par_odd += r["n_loc"] % 2
    dloc = np.array(dloc)
    dnp_ = np.array(dnp_)
    c_loc, s_loc = corr_slope(np.array([r["nd_f"] - r["tpk"]
                                        for r in wL["cells"]]), dnp_)
    med_dl = float(np.median(np.abs(dloc)))
    med_np = float(np.median(np.abs(dnp_)))
    n_par = len(wL["cells"])
    print("   largest window: %d cells, local sizes n_loc odd in "
          "%d/%d; median |local-rule - global node| = %.5f vs "
          "median |node - peak| = %.5f"
          % (n_par, par_odd, n_par, med_dl, med_np))
    # R-ladder: the measured localization profile
    for R in R_LIST:
        dv = np.array([r["rl"][R] - r["tpk"] for r in wL["cells"]])
        okm = np.isfinite(dv)
        cR, sR = corr_slope(dv[okm], dnp_[okm])
        rms = float(np.median(np.abs(
            dv[okm] - dnp_[okm])))
        print("   R = %.0f spacings: corr = %.3f, slope = %.3f, "
              "median |local - global| = %.5f (%d cells)"
              % (R, cR, sR, rms, int(np.sum(okm))))
    ok_b1 = (c_loc >= 0.9) and (0.7 <= s_loc <= 1.3) and \
        (med_dl <= 0.3 * med_np)
    check("B1.1 [E] LOCALITY (largest window): corr(local, global "
          "node-peak shift) = %.3f (bar >= 0.9), slope = %.3f (bar "
          "[0.7, 1.3]), median residual %.5f <= 0.3 x %.5f"
          % (c_loc, s_loc, med_dl, med_np), ok_b1)

    # ---------------- B2.1: symmetry mechanism (still zeta-free)
    nd_s_dev = np.array([abs(r["nd_s"] - r["tpk"])
                         for r in wL["cells"]])
    nd_f_dev = np.array([abs(r["nd_f"] - r["tpk"])
                         for r in wL["cells"]])
    act = np.array([r["nd_f"] - r["nd_s"] for r in wL["cells"]])
    c_act, s_act = corr_slope(act, dnp_)
    check("B2.1 [M] SYMMETRY: symmetrized local rule central-node "
          "distance -- median |node_S - peak| = %.5f vs full "
          "|node_F - peak| = %.5f (ratio %.2f); asymmetry "
          "activation corr(node_F - node_S, measured shift) = %.3f "
          "slope %.3f"
          % (float(np.median(nd_s_dev)), float(np.median(nd_f_dev)),
             float(np.median(nd_s_dev)) /
             max(float(np.median(nd_f_dev)), 1e-300), c_act, s_act),
          True)

    # ---------------- load the declared targets (AFTER the freeze)
    frozen = hsh.hexdigest()
    tau_cap = max(w["hi"] for w in wins)
    check("B2.0 [E] predictions FROZEN: SHA256 = %s..; now loading "
          "declared targets (tau <= %.1f)" % (frozen[:16], tau_cap),
          len(frozen) == 64)
    gam = e4.declared_zero_targets(tau_cap)
    print("   declared targets: %d zeros" % len(gam))

    # ---------------- B2.2: signed target test (largest window)
    gb = gam[(gam >= BAND_LO) & (gam <= wL["hi"])]
    tpk_arr = np.array([r["tpk"] for r in wL["cells"]])
    ipz = np.argmin(np.abs(tpk_arr[None, :] - gb[:, None]), axis=1)
    tn = wL["tau_n"]
    e_meas, e_pred, e_cent = [], [], []
    for z, ip in zip(gb, ipz):
        r = wL["cells"][ip]
        if abs(r["tpk"] - z) > 0.5:
            continue
        g_node = float(tn[np.argmin(np.abs(tn - z))])
        e_meas.append(g_node - z)
        e_pred.append(r["nd_f"] - z)
        e_cent.append(r["ct"] - z)
    e_meas = np.array(e_meas)
    e_pred = np.array(e_pred)
    e_cent = np.array(e_cent)
    c_t, s_t = corr_slope(e_pred, e_meas)
    check("B2.2 [M] TARGET TEST (largest window, %d matched zeros): "
          "corr(predicted signed error, measured) = %.3f (bar >= "
          "0.8), slope = %.3f; medians |pred| %.4f vs |meas| %.4f"
          % (len(e_meas), c_t, s_t, float(np.median(np.abs(e_pred))),
             float(np.median(np.abs(e_meas)))), c_t >= 0.8)

    # ---------------- B2.3 + B2.4: rate ladder + centroid compare
    g20 = gam[:K_FIRST]
    hs, dm, dp, dc = [], [], [], []
    for w in wins:
        tpk_w = np.array([r["tpk"] for r in w["cells"]])
        if len(tpk_w) == 0:
            continue
        ipz20 = np.argmin(np.abs(tpk_w[None, :] - g20[:, None]),
                          axis=1)
        mm, pp, cc = [], [], []
        for z, ip in zip(g20, ipz20):
            r = w["cells"][ip]
            tnw = w["tau_n"]
            mm.append(abs(float(tnw[np.argmin(np.abs(tnw - z))])
                          - z))
            pp.append(abs(r["nd_f"] - z))
            cc.append(abs(r["ct"] - z))
        hs.append(float(w["M"] // 2))
        dm.append(float(np.mean(mm)))
        dp.append(float(np.mean(pp)))
        dc.append(float(np.mean(cc)))
    sl_m = ladder_slope(hs, dm)
    sl_p = ladder_slope(hs, dp)
    sl_c = ladder_slope(hs, dc)
    print("   rate ladder measured |node-g|:   %s"
          % ", ".join("%.4f" % v for v in dm))
    print("   rate ladder predicted |loc-g|:   %s"
          % ", ".join("%.4f" % v for v in dp))
    print("   rate ladder centroid |ct-g|:     %s"
          % ", ".join("%.4f" % v for v in dc))
    rate_ok = abs(sl_p - sl_m) <= 0.5
    check("B2.3 [M] THE RATE: measured slope %.3f (E4: -1.61) vs "
          "predicted %.3f (bar |diff| <= 0.5) vs centroid %.3f -> "
          "%s" % (sl_m, sl_p, sl_c,
                  "perturbation theory carries the rate" if rate_ok
                  else "rate NOT carried"), True)
    check("B2.4 [M] CENTROID COMPARISON (largest window): medians "
          "|node-g| = %.4f, |local-rule-g| = %.4f, |bg-centroid-g| "
          "= %.4f -- the K1a side-finding (centroid beats node) is "
          "%s by the local picture"
          % (float(np.median(np.abs(e_meas))),
             float(np.median(np.abs(e_pred))),
             float(np.median(np.abs(e_cent))),
             "reproduced" if
             float(np.median(np.abs(e_cent))) <
             float(np.median(np.abs(e_meas))) else "NOT reproduced"),
          True)

    # ---------------- B2.5: asymmetry budget + h-scaling
    print("\nB2.5 -- asymmetry budget (odd moment m1 in tau units)")
    hsb, g_med, n_med = [], [], []
    for w in wins:
        if not w["cells"]:
            continue
        hsb.append(float(w["M"] // 2))
        g_med.append(float(np.median([abs(r["m1_grad"])
                                      for r in w["cells"]])))
        n_med.append(float(np.median([abs(r["m1_nb"])
                                      for r in w["cells"]])))
    sl_g = ladder_slope(hsb, g_med)
    sl_n = ladder_slope(hsb, n_med)
    dom = "gradient" if g_med[-1] > n_med[-1] else "neighbor"
    check("B2.5 [M] budget: median |m1_gradient| = %.5f (h-slope "
          "%.3f), median |m1_neighbor+rest| = %.5f (h-slope %.3f) "
          "on the largest window -- the %s term dominates; these "
          "slopes are the measured answer to the memo's rate "
          "derivation"
          % (g_med[-1], sl_g, n_med[-1], sl_n, dom), True)

    # ---------------- B2.6: direct first-order asymmetry test
    print("\nB2.6 -- direct first-order test (asymmetry functionals "
          "vs measured signed error, largest window)")
    f_m1, f_gr, f_ct, e_sgn = [], [], [], []
    for z, ip in zip(gb, ipz):
        r = wL["cells"][ip]
        if abs(r["tpk"] - z) > 0.5 or not math.isfinite(r["m1"]):
            continue
        g_node = float(tn[np.argmin(np.abs(tn - z))])
        e_sgn.append(g_node - z)
        f_m1.append(r["m1"])
        f_gr.append(r["m1_grad"])
        f_ct.append(r["ct"] - r["tpk"])
    c_m1 = float(np.corrcoef(f_m1, e_sgn)[0, 1])
    c_gr = float(np.corrcoef(f_gr, e_sgn)[0, 1])
    c_ct = float(np.corrcoef(f_ct, e_sgn)[0, 1])
    c_ce = float(np.corrcoef(e_cent, e_meas)[0, 1])
    check("B2.6 [M] direct correlations with the measured signed "
          "node error (%d zeros): odd moment m1 %.3f, gradient "
          "part %.3f, centroid shift %.3f; corr(centroid error, "
          "node error) = %.3f -- the first-order asymmetry "
          "mechanism is %s independently of the locality model"
          % (len(e_sgn), c_m1, c_gr, c_ct, c_ce,
             "dead" if max(abs(c_m1), abs(c_gr), abs(c_ct)) < 0.5
             else "alive"), True)

    # ---------------- B2.7: atom-pinning alternative (identity lab)
    print("\nB2.7 -- atom-pinning lab: atoms + smooth background, "
          "EXACT moments, K-ladder (synthetic, zeta-free)")
    ks_lab = (50, 100, 200, 400, 800)
    m_tot = float(sum(k1.S_MASS))
    c0 = 3.0 * m_tot / (2.0 * math.pi)        # smooth layer ~ 3x atoms
    d_lab = np.arange(2 * max(ks_lab))
    p_lab = np.zeros_like(d_lab, dtype=float)
    p_lab[0] = 2.0 * math.pi * c0
    p_lab[1] = 0.5 * math.pi * c0
    p_lab[2] = 0.3 * math.pi * c0
    for tk, mk in zip(k1.S_POS, k1.S_MASS):
        p_lab += mk * np.cos(d_lab * tk)
    pos = np.array(k1.S_POS)
    pin_med, pin_max = [], []
    wt_ratio = None
    for Kl in ks_lab:
        thl, wl = k1.gauss_theta(p_lab[:2 * Kl], Kl)
        dev = []
        wts = []
        for tk, mk in zip(k1.S_POS, k1.S_MASS):
            jn = int(np.argmin(np.abs(thl - tk)))
            dev.append(abs(float(thl[jn]) - tk))
            wts.append(float(wl[jn]) / mk)
        pin_med.append(float(np.median(dev)))
        pin_max.append(float(np.max(dev)))
        if Kl == 400:
            wt_ratio = (float(np.median(wts)), float(np.max(wts)))
        print("   K = %4d: |node - atom| median %.2e, max %.2e"
              % (Kl, pin_med[-1], pin_max[-1]))
    rate_pin = ladder_slope(np.array(ks_lab, float),
                            np.array(pin_med))
    pin_ok = (pin_med[3] <= 1e-3) and (rate_pin <= -1.3)
    check("B2.7a [M] atom pinning (lab): median pinning rate "
          "K^%.2f, at K = 400 median |node - atom| = %.2e and node "
          "weight / atom mass median %.3f (max %.3f) -- a mass "
          "point pins its node FAR below the smoothing width; "
          "carries super-resolution: %s"
          % (rate_pin, pin_med[3], wt_ratio[0], wt_ratio[1],
             pin_ok), True)
    # real-data correlate: |node - gamma| vs local background/mass
    xr, yr = [], []
    for z, ip in zip(gb, ipz):
        r = wL["cells"][ip]
        if abs(r["tpk"] - z) > 0.5 or not math.isfinite(r["eps"]) \
                or r["m_est"] <= 0 or r["eps"] <= 0:
            continue
        g_node = float(tn[np.argmin(np.abs(tn - z))])
        if abs(g_node - z) < 1e-12:
            continue
        xr.append(math.log(r["eps"] * r["sp"] / r["m_est"]))
        yr.append(math.log(abs(g_node - z)))
    c_pin = float(np.corrcoef(xr, yr)[0, 1])
    check("B2.7b [M] real-data pinning correlate (%d zeros, "
          "largest window): corr(log|node - gamma|, log(local "
          "background mass / peak mass)) = %.3f -- %s"
          % (len(xr), c_pin,
             "the error tracks the background-to-atom ratio "
             "(pinning signature)" if c_pin > 0.3 else
             "weak on a single window (reported, not adjudicated)"),
          True)

    # ---------------- B3: off-line toy (falsifier bridge)
    print("\nB3 -- off-line toy: cosh-modulated peak channel "
          "(declared toy, not the v688 form)")
    d = np.arange(k1.S_MOM + 1)
    fej = np.clip(1.0 - d / k1.S_MF, 0.0, None)
    p0 = np.zeros(k1.S_MOM + 1)
    p0[0] = 2.0 * math.pi * k1.S_EPS
    for tk, mk in zip(k1.S_POS, k1.S_MASS):
        p0 += mk * fej * np.cos(d * tk)
    j0 = 4                                        # peak at 1.80
    th0, _w0 = k1.gauss_theta(p0, k1.S_K)
    base_nd = float(th0[np.argmin(np.abs(th0 - k1.S_POS[j0]))])
    bds, shifts = [], []
    for de in DELTAS:
        pd_ = p0 + k1.S_MASS[j0] * fej * np.cos(d * k1.S_POS[j0]) \
            * (np.cosh(de * d / k1.S_MF) - 1.0)
        _ks, _es, bd = jac.levinson(pd_, 2 * k1.S_K - 1)
        bds.append(bd if bd is not None else -1)
        if bd is None or bd >= 2 * k1.S_K - 1:
            thd, _wd = k1.gauss_theta(pd_, k1.S_K)
            sh = float(thd[np.argmin(np.abs(thd - k1.S_POS[j0]))]) \
                - base_nd if thd is not None else float("nan")
            shifts.append(sh)
        else:
            shifts.append(float("nan"))
        print("   delta = %.1f: Levinson breakdown depth = %s "
              "(PD horizon), node shift at the modulated peak = %s"
              % (de, "none (PD)" if bds[-1] == -1 else str(bds[-1]),
                 "%.5f" % shifts[-1] if not math.isnan(shifts[-1])
                 else "n/a (not a state)"))
    bs = [b if b >= 0 else 10 ** 9 for b in bds]
    mono = all(bs[i + 1] <= bs[i] for i in range(len(bs) - 1))
    check("B3.1 [M] off-line signature: the PD horizon (Levinson "
          "breakdown depth) is nonincreasing in delta: %s -- an "
          "off-line channel does not mislocate the node, it "
          "destroys the state at a finite depth (the falsifier "
          "bridge, toy-typed)" % mono, mono)

    # ---------------- verdict
    print("\nB4 -- verdict + Satz")
    if ok_b1 and c_t >= 0.8 and rate_ok:
        verdict = "K1B-CLASSICAL"
    elif pin_ok:
        verdict = "K1B-ATOM-PINNING"
    else:
        verdict = "K1B-OPEN"
    check("B4.1 [M] preregistered adjudication: %s (memo locality "
          "%s, target corr %.3f, rate diff %.3f; atom lab pinning "
          "%s)" % (verdict, "holds" if ok_b1 else "BROKEN", c_t,
                   abs(sl_p - sl_m),
                   "carries" if pin_ok else "does not carry"), True)
    print("""
  ERROR ANATOMY (the honest B3 answer -- the memo mechanism does
  NOT carry):
    (i)   The memo's local model acts on the WRONG measure: the
          nodes are the Gauss rule of the RAW moment sequence
          (near-atomic: atoms at the zeros + smooth layer); the
          Fejer symbol is only its smoothed visualization.  Local
          rules of the smoothed measure cannot reproduce the raw
          nodes at ANY window size R (B1 ladder, corr ~ 0).
    (ii)  First-order asymmetry functionals of the smoothed peak
          (odd moment, gradient, centroid shift) do not correlate
          with the signed node error (B2.6) -- symmetry-
          perturbation of the Fejer peak is dead as the mechanism.
    (iii) The classical mechanism that DOES carry the scale is
          mass-point attraction (B2.7): an atom in the measure pins
          a Gauss node far below the smoothing width (Nevai-type;
          the node weight converges to the atom mass).  This is the
          super-resolution: quadrature nodes see the atoms of the
          raw measure, not the smoothed peaks.
  SATZ-BILD K1 (revised, typed):
    K1a [proof-adjacent, k1_node_capture_probe]: capture of every
        symbol peak in the certified M-S/Christoffel radius.
    K1b [this probe]: center localization = atom pinning of the
        raw measure [classical, Nevai-type mass-point attraction --
        the lab measures the rate; the THEOREM needs: raw window
        measure = atoms at gamma + smooth layer with controlled
        truncation, i.e. the explicit-formula/v669 content at the
        RAW level, NOT just the Fejer identity].
  OFF-LINE-EHRLICHKEIT: on-line zeros make the raw measure a
  POSITIVE atomic measure -- that is exactly what pinning needs;
  off-line pairs turn the lag channel cosh-modulated, and the
  state DIES at finite Levinson depth instead of mislocating the
  node (B3).  Falsifier (v688) and K1b are consistent: positivity
  is the single place where RH enters.""")

    print("\nVERDICT: %s" % verdict)
    dt = time.time() - T0
    # Promotion contract: the two preregistered memo bars B1.1 (locality)
    # and B2.2 (asymmetry target) die honestly -- that exact pattern IS
    # the promoted finding (K1B-ATOM-PINNING).  Any other failure set,
    # or a change of verdict, fails the module.
    expected_fails = ["B1.1", "B2.2"]
    if FAILS == expected_fails and verdict == "K1B-ATOM-PINNING":
        print("RESULT: %d/%d checks passed -- the preregistered memo bars "
              "%s die honestly, exactly as promoted  (%.1f s)"
              % (N_CHK - len(FAILS), N_CHK, ", ".join(FAILS), dt))
        return 0
    if FAILS:
        print("RESULT: %d/%d checks passed -- FAILURES: %s  (%.1f s)"
              % (N_CHK - len(FAILS), N_CHK, ", ".join(FAILS), dt))
        return 1
    print("RESULT: ALL %d CHECKS PASSED but the preregistered memo-bar "
          "deaths B1.1/B2.2 did NOT occur -- promoted finding changed, "
          "investigate  (%.1f s)" % (N_CHK, dt))
    return 1


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