#!/usr/bin/env python3
# MQG_STEP2_ROBUSTNESS  (marker used to find this cell's source when run from a notebook)
"""
Step 2 of the v3 revision: does the late quantum excess survive (a) a non-Gaussian classical sampling and
(b) other mixing angles?  Two-beam neutrino model, exact block solver and truncated Wigner approximation (TWA)
identical to Stage 4 (mqg_breaktime_test.py); only the TWA initial sampling and theta are varied.

HOW TO RUN (Google Colab, free CPU runtime)
  Paste this whole file into ONE cell and run it. Nothing to install (numpy, scipy, matplotlib).
  QUICK = True: N = 640, 1280 only (about 5-10 min). QUICK = False: N = 640, 1280, 2560 (about 20-40 min).
  At the end a zip with every output is downloaded to your computer (DOWNLOAD = True).

WHAT IS COMPUTED
  mu = 2.0, T = 1600 (grid 0.5), TWA 8000 samples (8 groups), RK4 dt = 0.0833.
  theta = 0.05, 0.10, 0.20 in the unstable ordering; theta = 0.10 also in the stable control ordering.
  Two classical samplings of the same initial coherent states:
    'gaussian' - transverse components Gaussian with variance j/2 per beam (exactly as Stage 4, seeds 7000 + N);
    'discrete' - each neutrino's transverse isospin components are +-1/2 with equal probability (discrete
                 truncated Wigner sampling); the beam components are then binomial, with the same variance j/2
                 but non-Gaussian higher cumulants (seeds 9000 + N).
  Spectral time t_spec = 2 pi / <|Delta^2 E|> from the exact block spectra (population-weighted second differences
  of the levels populated by the initial state), as in the step-1 spectral test.
  Running excess r(t) = <QFI/N>_exact / <QFI/N>_TWA - 1 over a centred 30-unit window; break time t_b = first
  t > 15 with r > 0.05 (also 0.03, 0.08); peak = largest r with 0.2 <= t/t_spec <= 1.0; FWHM in units of t_spec.

PRE-REGISTERED (fixed 2026-10-22, before any output of this script was seen)
  Checks
    V1  Gaussian sampling, theta = 0.10, mu = 2, unstable: t_b reproduces Stage 4 (N = 640: 64.5, 1280: 157.5,
        2560: 352.5, 5120: 758.0) to within 1.0 time unit for every N run.
    V2  norm conserved to < 1e-10 for every exact run.
    V3  discrete sampling: sample variance of the transverse beam components within 3% of j/2.
  S1  sampling (theta = 0.10, mu = 2, unstable): discrete vs Gaussian TWA changes t_b by <= 10% for every N,
      and, for every N >= 1280, t_peak by <= 5% and the peak height by <= 0.03.   PASS: the break and the peak are
      not artefacts of Gaussian sampling.
  S2  spectral collapse at new angles (Gaussian sampling, mu = 2, unstable), for theta = 0.05 and 0.20 separately,
      using the two largest N run:
      (a) a peak is found with 0.2 <= t/t_spec <= 1.0, not at the end of the run, for both N;
      (b) t_peak/t_spec differs between the two N by <= 5%;
      (c) the exponent of t_peak between the two N lies in [0.85, 1.15];
      (d) the peak height lies in [0.10, 0.30] for both N.
      PASS for a theta: the step-1 picture (a revival-type peak at a fixed fraction of t_spec, t_peak ~ N, height
      independent of N) holds at that angle.
  Information only (no criterion)
    I1  kappa = t_peak/t_spec at the largest N for theta = 0.05, 0.10, 0.20 (does the prefactor depend on theta?)
    I2  S1 repeated at theta = 0.05 and 0.20
    I3  control ordering (theta = 0.10): r(t), peak position in units of t_spec, height; also the largest r over
        the whole run and its position in units of t_spec (added after the smoke test below; information only)
    I4  late classical QFI/N (t >= 800): discrete / Gaussian - 1
  A code smoke test (N = 320, 640, T = 200) was run on 2026-10-22 only to check that the script works; its numbers
  are not results, and the criteria above were not changed after it. It showed that the control's first 5%
  crossing is seed-dependent at early times, which is why the whole-run maximum was added to I3.

OUTPUTS (folder mqg_step2_<UTC time>/, also zipped)
  cases.csv          one row per (theta, ordering, sampling, N): t_spec, t_b at 3/5/8%, peak time/height/width, ...
  summary.json       settings, checks, criteria, verdicts and all numbers
  series.npz         time series: exact and classical QFI/N, running excess
  fig_step2.png      running excess against t/t_spec for every case
  run_log.txt        everything printed
  run_metadata.json  start/end time, duration, Python/NumPy/SciPy versions, platform, SHA-256 of this script
  mqg_step2_robustness.py   exact copy of the code that produced the run
"""
import csv
import hashlib
import json
import multiprocessing as mproc
import os
import platform
import shutil
import sys
import time
import zipfile
from concurrent.futures import ProcessPoolExecutor
from datetime import datetime, timezone

import numpy as np
import scipy
from scipy.linalg import eigh_tridiagonal

# ---------------------------------------------------------------- settings (pre-registered)
QUICK = False
INCLUDE_5120 = False                     # True adds N = 5120 (roughly doubles the run time)
DOWNLOAD = True                          # Colab: download the zipped outputs at the end
N_LIST = [640, 1280] if QUICK else [640, 1280, 2560]
if INCLUDE_5120 and not QUICK:
    N_LIST = N_LIST + [5120]
MU = 2.0
THETAS = [0.05, 0.10, 0.20]
CONTROL_THETA = 0.10
SAMPLINGS = ('gaussian', 'discrete')
T_MAX, DT = 1600.0, 0.5
WIDTH, LEVEL, T_MIN = 30.0, 0.05, 15.0
LEVELS = (0.03, 0.05, 0.08)
M_TWA, N_GROUPS = 8000, 8
TWA_SUBSTEPS = 6
BLOCK_TOL = 1e-22
W_MIN_SPEC = 1e-10
SEED_GAUSS, SEED_DISCRETE = 7000, 9000
STAGE4_TB = {640: 64.5, 1280: 157.5, 2560: 352.5, 5120: 758.0, 320: 25.0}   # theta 0.10, mu 2, level 5%

if os.environ.get('MQG_SMOKE'):          # developer smoke test only (tiny run); never used for results
    N_LIST, T_MAX = [320, 640], 200.0
TIMES = np.round(np.arange(0.0, T_MAX + DT / 2, DT), 10)

# ---------------------------------------------------------------- logging
_LOG = []


def log(*a):
    s = ' '.join(str(x) for x in a)
    print(s, flush=True)
    _LOG.append(s)


def alpha_of(b, theta):
    """angle between flavour z and B; in the B frame flavour z = (-sin alpha, 0, cos alpha)."""
    return np.arctan2(np.sin(2 * theta), b * np.cos(2 * theta))


# ---------------------------------------------------------------- exact block solver (unchanged from Stage 4)
def entropy_bloch(r):
    r = np.clip(r, 0.0, 1.0)
    out = np.zeros_like(r)
    for sgn in (1, -1):
        p = np.clip((1 + sgn * r) / 2, 1e-15, 1.0)
        out -= p * np.log(p)
    return out


def measures(rA, rB, Jmean, Jcov, N):
    lam = np.linalg.eigvalsh(Jcov)[..., -1]
    C = (entropy_bloch(np.linalg.norm(rA, axis=-1)) + entropy_bloch(np.linalg.norm(rB, axis=-1))) / 2 / np.log(2)
    I = 4 * lam / N**2
    P = np.linalg.norm(Jmean, axis=-1) / (N / 2)
    return {'S1': C, 'QFI_N': 4 * lam / N, 'P': P, 'Q': np.maximum(C * I * P, 0.0) ** (1 / 3)}


def block_structure(N):
    j = N / 4
    d = int(round(2 * j + 1))
    jj = j * (j + 1)
    blocks = []
    for k in range(2 * d - 1):
        K = -2 * j + k
        lo = max(-j, K - j)
        mA = np.arange(lo, min(j, K + j) + 0.5, 1.0)
        mB = K - mA
        idx = np.round(j - mA).astype(np.int64) * d + np.round(j - mB).astype(np.int64)
        blocks.append({'K': K, 'mA': mA, 'mB': mB, 'idx': idx, 'lo': lo})
    for k in range(len(blocks) - 1):            # raising maps block k -> k+1 for J_A+ and J_B+
        s, t = blocks[k], blocks[k + 1]
        mA, mB = s['mA'], s['mB']
        okA, okB = mA < j - 1e-9, mB < j - 1e-9
        for key, ok, dst, m in (('rA', okA, mA[okA] + 1 - t['lo'], mA), ('rB', okB, mA[okB] - t['lo'], mB)):
            src = np.nonzero(ok)[0]
            dst = np.round(dst).astype(int)
            assert np.all(np.diff(src) == 1) and np.all(np.diff(dst) == 1)
            s[key] = (int(src[0]), int(dst[0]), len(src), np.sqrt(jj - m[ok] * (m[ok] + 1)))
    return {'N': N, 'j': j, 'd': d, 'blocks': blocks}


def initial_states(N, alphas):
    """product coherent states in the B frame: beam A along flavour +z, beam B along flavour -z."""
    j = N / 4
    m = np.arange(j, -j - 1, -1)
    jp = np.diag(np.sqrt(j * (j + 1) - m[1:] * (m[1:] + 1)), 1)
    Jy = (jp - jp.T) / 2j
    lam, U = np.linalg.eigh(Jy)
    cols = []
    for al in alphas:
        ph = np.exp(1j * al * lam)
        a = U @ (ph * U[0].conj())
        b = U @ (ph * U[-1].conj())
        cols.append(np.kron(a, b))
    return np.stack(cols, 1)


def _dot(x, y):
    return np.einsum('ij,ij->j', x.conj(), y)


def exact_moments(struct, mu, alphas, times):
    N, blocks = struct['N'], struct['blocks']
    P0 = initial_states(N, alphas)
    na, nt = len(alphas), len(times)
    dt = times[1] - times[0]
    assert np.allclose(np.diff(times), dt) and times[0] == 0.0

    def psi_block(k):
        s = blocks[k]
        n = len(s['mA'])
        diag = (s['mA'] - s['mB']) + (mu / N) * (2 * struct['j'] * (struct['j'] + 1) + 2 * s['mA'] * s['mB'] - 0.75 * N)
        if n == 1:
            E, V = diag, np.ones((1, 1))
        else:
            mA, mB = s['mA'][:-1], s['mB'][:-1]
            jj = struct['j'] * (struct['j'] + 1)
            off = (mu / N) * np.sqrt(jj - mA * (mA + 1)) * np.sqrt(np.maximum(jj - mB * (mB - 1), 0.0))
            E, V = eigh_tridiagonal(diag, off)
        a = V.T @ P0[s['idx']]
        ph = np.empty((n, nt), complex)
        ph[:, 0] = 1.0
        np.cumprod(np.broadcast_to(np.exp(-1j * E * dt)[:, None], (n, nt - 1)), axis=1, out=ph[:, 1:])
        C = (a[:, :, None] * ph[:, None, :]).reshape(n, -1)
        return (V @ C.view(np.float64)).view(np.complex128)

    cols = na * nt
    norm, Jz, Jz2, JAz, JBz, JmJp = (np.zeros(cols) for _ in range(6))
    JAp, JBp, Jp2, XY = (np.zeros(cols, complex) for _ in range(4))
    wK = np.array([(np.abs(P0[s['idx']])**2).sum(0).max() for s in blocks])
    keep = np.nonzero(wK > BLOCK_TOL)[0]
    k_lo, k_hi = int(keep[0]), int(keep[-1])
    skipped = wK[:k_lo].sum() + wK[k_hi + 1:].sum()
    prev, cur = None, psi_block(k_lo)
    for k in range(k_lo, k_hi + 1):
        nxt = psi_block(k + 1) if k < k_hi else None
        s = blocks[k]
        K, n = s['K'], len(s['mA'])
        w = cur.real**2 + cur.imag**2
        ws = w.sum(0)
        norm += ws
        Jz += K * ws
        Jz2 += K * K * ws
        JAz += s['mA'] @ w
        JBz += s['mB'] @ w
        if prev is not None:
            (sa, da, la, ca), (sb, db, lb, cb) = blocks[k - 1]['rA'], blocks[k - 1]['rB']
            tA = ca[:, None] * prev[sa:sa + la]
            tB = cb[:, None] * prev[sb:sb + lb]
            pA, pB = _dot(cur[da:da + la], tA), _dot(cur[db:db + lb], tB)
            JAp += pA
            JBp += pB
            XY += (2 * K - 1) * (pA + pB)
            u = np.zeros((n, cols), complex)
            u[da:da + la] = tA
            u[db:db + lb] += tB
            JmJp += (u.real**2 + u.imag**2).sum(0)
            if nxt is not None:
                (sa, da, la, ca), (sb, db, lb, cb) = s['rA'], s['rB']
                Jp2 += _dot(ca[:, None] * nxt[da:da + la], u[sa:sa + la])
                Jp2 += _dot(cb[:, None] * nxt[db:db + lb], u[sb:sb + lb])
        prev, cur = cur, nxt

    rs = lambda x: x.reshape(na, nt)
    norm, Jz, Jz2, JAz, JBz, JmJp, JAp, JBp, Jp2, XY = map(rs, (norm, Jz, Jz2, JAz, JBz, JmJp, JAp, JBp, Jp2, XY))
    Jp = JAp + JBp
    JpJm = JmJp + 2 * Jz
    mean = np.stack([Jp.real, Jp.imag, Jz], -1)
    S = np.empty((na, nt, 3, 3))
    S[..., 0, 0] = (2 * Jp2.real + JpJm + JmJp) / 4
    S[..., 1, 1] = (-2 * Jp2.real + JpJm + JmJp) / 4
    S[..., 2, 2] = Jz2
    S[..., 0, 1] = S[..., 1, 0] = Jp2.imag / 2
    S[..., 0, 2] = S[..., 2, 0] = XY.real / 2
    S[..., 1, 2] = S[..., 2, 1] = XY.imag / 2
    cov = S - mean[..., :, None] * mean[..., None, :]
    rA = 4 * np.stack([JAp.real, JAp.imag, JAz], -1) / N
    rB = 4 * np.stack([JBp.real, JBp.imag, JBz], -1) / N
    out = []
    for i in range(na):
        d = measures(rA[i], rB[i], mean[i], cov[i], N)
        d['norm'] = norm[i]
        d['skipped'] = skipped
        out.append(d)
    return out


# ---------------------------------------------------------------- classical TWA (Stage 4 + sampling option)
def twa_qfi(N, params, sampling, M=M_TWA, groups=N_GROUPS, times=TIMES, substeps=TWA_SUBSTEPS, seed=0):
    """params: list of (mu, b, theta). sampling 'gaussian' reproduces Stage 4 exactly for the same seed;
    'discrete' draws each neutrino's transverse components as +-1/2 (binomial beam sums, same variance)."""
    g = np.random.default_rng(seed)
    j = N / 4
    P = len(params)
    half_mu = np.array([p[0] / 2 for p in params])[:, None]
    Bx = np.array([np.sin(2 * p[2]) for p in params])[:, None]
    Bz = np.array([p[1] * np.cos(2 * p[2]) for p in params])[:, None]
    if sampling == 'gaussian':
        sd = np.sqrt(1 / (2 * j))
        noise = g.normal(scale=sd, size=(4, M))
    elif sampling == 'discrete':
        n = N // 2                                                   # neutrinos per beam
        noise = (2.0 * g.binomial(n, 0.5, size=(4, M)) - n) / 2.0 / j
    else:
        raise ValueError(sampling)
    a = np.empty((3, P, M))
    c = np.empty((3, P, M))
    a[0], a[1], a[2] = noise[0], noise[1], 1.0
    c[0], c[1], c[2] = noise[2], noise[3], -1.0

    def f(a, c):
        hx, hy, hz = half_mu * (a[0] + c[0]), half_mu * (a[1] + c[1]), half_mu * (a[2] + c[2])
        ax_, az_ = hx + Bx, hz + Bz
        cx_, cz_ = hx - Bx, hz - Bz
        da = np.stack([hy * a[2] - az_ * a[1], az_ * a[0] - ax_ * a[2], ax_ * a[1] - hy * a[0]])
        dc = np.stack([hy * c[2] - cz_ * c[1], cz_ * c[0] - cx_ * c[2], cx_ * c[1] - hy * c[0]])
        return da, dc

    q_all = np.empty((P, len(times)))

    def record(it, a, c):
        s = (a + c) * j
        d = s - s.mean(2, keepdims=True)
        cov = np.einsum('ipm,kpm->pik', d, d) / (M - 1)
        q_all[:, it] = 4 * np.linalg.eigvalsh(cov)[:, -1] / N

    dt = (times[1] - times[0]) / substeps
    record(0, a, c)
    for it in range(1, len(times)):
        for _ in range(substeps):
            k1 = f(a, c)
            k2 = f(a + 0.5 * dt * k1[0], c + 0.5 * dt * k1[1])
            k3 = f(a + 0.5 * dt * k2[0], c + 0.5 * dt * k2[1])
            k4 = f(a + dt * k3[0], c + dt * k3[1])
            a = a + dt / 6 * (k1[0] + 2 * k2[0] + 2 * k3[0] + k4[0])
            c = c + dt / 6 * (k1[1] + 2 * k2[1] + 2 * k3[1] + k4[1])
        record(it, a, c)
    var_check = float(np.var(noise) * 2 * j)                         # should be 1 (variance j/2 per component)
    return q_all, var_check


# ---------------------------------------------------------------- spectral time (step 1)
def spectral_time(N, mu, b, theta):
    st = block_structure(N)
    P0 = initial_states(N, [alpha_of(b, theta)])[:, 0]
    j = st['j']
    jj = j * (j + 1)
    num = den = 0.0
    for s in st['blocks']:
        a0 = P0[s['idx']]
        if np.sum(np.abs(a0)**2) < W_MIN_SPEC or len(s['mA']) < 4:
            continue
        diag = (s['mA'] - s['mB']) + (mu / N) * (2 * jj + 2 * s['mA'] * s['mB'] - 0.75 * N)
        mA, mB = s['mA'][:-1], s['mB'][:-1]
        off = (mu / N) * np.sqrt(jj - mA * (mA + 1)) * np.sqrt(np.maximum(jj - mB * (mB - 1), 0.0))
        E, V = eigh_tridiagonal(diag, off)
        p = np.abs(V.T @ a0)**2
        num += np.sum(p[:-2] * np.abs(np.diff(E, 2)))
        den += np.sum(p[:-2])
    return 2 * np.pi * den / num


# ---------------------------------------------------------------- analysis
def running_excess(q_ex, q_tw, width=WIDTH, times=TIMES):
    w = int(round(width / (times[1] - times[0])))
    k = np.ones(w) / w
    r = np.convolve(q_ex, k, 'valid') / np.convolve(q_tw, k, 'valid') - 1
    return times[w // 2: w // 2 + len(r)], r


def break_time(tc, r, level=LEVEL, t_min=T_MIN):
    hit = np.nonzero((r > level) & (tc > t_min))[0]
    return float(tc[hit[0]]) if len(hit) else float('nan')


def peak(tc, r, ts):
    x = tc / ts
    m = (x >= 0.2) & (x <= 1.0)
    if not m.any():
        return dict(t_peak=float('nan'), r_peak=float('nan'), x_peak=float('nan'), fwhm_x=float('nan'), at_edge=True)
    i = int(np.nonzero(m)[0][np.argmax(r[m])])
    at_edge = bool(tc[i] >= tc[-1] - 0.01 * T_MAX)
    half = r[i] / 2
    lo, hi = i, i
    while lo > 0 and r[lo] > half:
        lo -= 1
    while hi < len(r) - 1 and r[hi] > half:
        hi += 1
    fwhm = (tc[hi] - tc[lo]) / ts if (lo > 0 and hi < len(r) - 1) else float('nan')
    return dict(t_peak=float(tc[i]), r_peak=float(r[i]), x_peak=float(x[i]), fwhm_x=float(fwhm), at_edge=at_edge)


# ---------------------------------------------------------------- parallel jobs
def _exact_job(args):
    N, theta = args
    bs = (1, -1) if abs(theta - CONTROL_THETA) < 1e-12 else (1,)
    res = exact_moments(block_structure(N), MU, [alpha_of(b, theta) for b in bs], TIMES)
    return N, theta, {b: {'QFI_N': r['QFI_N'], 'norm': r['norm'], 'skipped': r['skipped']} for b, r in zip(bs, res)}


def _twa_job(args):
    N, sampling = args
    params = [(MU, 1, th) for th in THETAS] + [(MU, -1, CONTROL_THETA)]
    seed = (SEED_GAUSS if sampling == 'gaussian' else SEED_DISCRETE) + N
    q, var_check = twa_qfi(N, params, sampling, seed=seed)
    return N, sampling, {(p[1], p[2]): q[i] for i, p in enumerate(params)}, var_check


def _spec_job(args):
    N, b, theta = args
    return N, b, theta, spectral_time(N, MU, b, theta)


def _map(fn, jobs, pool):
    if pool is not None:
        try:
            return list(pool.map(fn, jobs, chunksize=1))
        except Exception as exc:
            log('   (parallel run unavailable, running serially:', type(exc).__name__, ')')
    return [fn(j) for j in jobs]


def _source_text():
    try:
        return open(__file__, encoding='utf-8').read()
    except Exception:
        pass
    try:
        ip = get_ipython()                                            # noqa: F821  (defined in notebooks)
        for cell in reversed(ip.user_ns.get('In', [])):
            if 'MQG_STEP2_ROBUSTNESS' in cell and 'def _source_text' in cell:
                return cell
    except Exception:
        pass
    return None


# ---------------------------------------------------------------- main
def main():
    t0 = time.time()
    start = datetime.now(timezone.utc)
    outdir = 'mqg_step2_' + start.strftime('%Y%m%dT%H%M%SZ')
    os.makedirs(outdir, exist_ok=True)
    log(f'MQG step 2 robustness test   N = {N_LIST}, mu = {MU}, theta = {THETAS}, T = {T_MAX:.0f}, '
        f'TWA {M_TWA} samples   -> {outdir}/')
    ncpu = os.cpu_count() or 1
    pool = None
    if ncpu > 1:
        try:
            pool = ProcessPoolExecutor(max_workers=min(ncpu, 2), mp_context=mproc.get_context('fork'))
        except Exception:
            pool = None
    exact, twa, tspec, var_checks = {}, {}, {}, {}
    try:
        jobs = [(N, th) for N in N_LIST[::-1] for th in THETAS]
        for N, th, res in _map(_exact_job, jobs, pool):
            for b, r in res.items():
                exact[(th, b, N)] = r
        log(f'   exact runs done ({time.time() - t0:.0f} s)')
        for N, smp, res, vc in _map(_twa_job, [(N, s) for N in N_LIST[::-1] for s in SAMPLINGS], pool):
            for (b, th), q in res.items():
                twa[(th, b, N, smp)] = q
            var_checks[(N, smp)] = vc
        log(f'   classical runs done ({time.time() - t0:.0f} s)')
        sjobs = [(N, 1, th) for N in N_LIST for th in THETAS] + [(N, -1, CONTROL_THETA) for N in N_LIST]
        for N, b, th, ts in _map(_spec_job, sjobs, pool):
            tspec[(th, b, N)] = ts
        log(f'   spectral times done ({time.time() - t0:.0f} s)')
    finally:
        if pool is not None:
            pool.shutdown()

    # ---- checks
    V = {}
    dev = {N: abs(break_time(*running_excess(exact[(0.10, 1, N)]['QFI_N'], twa[(0.10, 1, N, 'gaussian')]))
                  - STAGE4_TB[N]) for N in N_LIST if N in STAGE4_TB}
    V['V1'] = bool(dev) and all(d <= 1.0 for d in dev.values())
    log(f"[{'PASS' if V['V1'] else 'FAIL'}] V1 Gaussian TWA reproduces Stage-4 t_b (theta 0.10): "
        + ', '.join(f'N={N}: |dev| {d:.1f}' for N, d in dev.items()))
    ndev = max(float(np.abs(r['norm'] - 1).max()) for r in exact.values())
    V['V2'] = ndev < 1e-10
    log(f"[{'PASS' if V['V2'] else 'FAIL'}] V2 norm: max |norm - 1| = {ndev:.1e}")
    vd = {N: var_checks[(N, 'discrete')] for N in N_LIST}
    V['V3'] = all(abs(v - 1) <= 0.03 for v in vd.values())
    log(f"[{'PASS' if V['V3'] else 'FAIL'}] V3 discrete sampling variance / (j/2): "
        + ', '.join(f'N={N}: {v:.4f}' for N, v in vd.items()))

    # ---- per-case analysis
    rows, series = [], {'times': TIMES}
    cases = [(th, 1) for th in THETAS] + [(CONTROL_THETA, -1)]
    late = TIMES >= T_MAX / 2
    for th, b in cases:
        for N in N_LIST:
            qe = exact[(th, b, N)]['QFI_N']
            series[f'exact_th{th}_b{b}_N{N}'] = qe
            for smp in SAMPLINGS:
                qt = twa[(th, b, N, smp)]
                tc, r = running_excess(qe, qt)
                ts = tspec[(th, b, N)]
                pk = peak(tc, r, ts)
                x = tc / ts
                post = r[(x >= 0.8) & (x <= 1.0)]
                after_min = tc > T_MIN
                i_max = int(np.nonzero(after_min)[0][np.argmax(r[after_min])])
                row = dict(theta=th, ordering='unstable' if b == 1 else 'control', sampling=smp, N=N, t_spec=ts,
                           **{f't_b_{int(lv * 100)}pct': break_time(tc, r, lv) for lv in LEVELS}, **pk,
                           r_max_run=float(r[i_max]), x_max_run=float(x[i_max]),
                           post_peak_mean_x08_10=float(post.mean()) if post.size > 20 else float('nan'),
                           twa_late_mean=float(qt[late].mean()), exact_late_mean=float(qe[late].mean()))
                rows.append(row)
                series[f'twa_th{th}_b{b}_N{N}_{smp}'] = qt
                series[f'r_th{th}_b{b}_N{N}_{smp}'] = r
                series[f'tc_th{th}_b{b}_N{N}_{smp}'] = tc
    R = {(w['theta'], w['ordering'], w['sampling'], w['N']): w for w in rows}

    log('\n   theta  ordering  sampling     N   t_spec    t_b(5%)  [3% / 8%]        t_peak  t_peak/t_spec  height  FWHM/t_spec')
    for w in rows:
        log(f"   {w['theta']:.2f}   {w['ordering']:8s}  {w['sampling']:9s} {w['N']:5d} {w['t_spec']:8.1f}  "
            f"{w['t_b_5pct']:8.1f}  [{w['t_b_3pct']:7.1f} / {w['t_b_8pct']:7.1f}]  {w['t_peak']:8.1f}  "
            f"{w['x_peak']:10.3f}  {w['r_peak']:7.3f}  {w['fwhm_x']:8.3f}" + ('  (peak at end of run)' if w['at_edge'] else ''))

    # ---- S1: sampling at theta = 0.10 (and I2 at other theta)
    def sampling_test(th):
        ok, lines = True, []
        for N in N_LIST:
            g, d = R[(th, 'unstable', 'gaussian', N)], R[(th, 'unstable', 'discrete', N)]
            dtb = abs(d['t_b_5pct'] / g['t_b_5pct'] - 1) if np.isfinite(g['t_b_5pct']) and np.isfinite(d['t_b_5pct']) else np.inf
            c = dtb <= 0.10
            txt = f'N={N}: t_b {g["t_b_5pct"]:.1f} vs {d["t_b_5pct"]:.1f} ({dtb:.1%})'
            if N >= 1280:
                valid = not (g['at_edge'] or d['at_edge']) and np.isfinite(g['t_peak']) and np.isfinite(d['t_peak'])
                dtp = abs(d['t_peak'] / g['t_peak'] - 1) if valid else np.inf
                dh = abs(d['r_peak'] - g['r_peak']) if valid else np.inf
                c = c and dtp <= 0.05 and dh <= 0.03
                txt += f'; t_peak {g["t_peak"]:.1f} vs {d["t_peak"]:.1f} ({dtp:.1%}); height {g["r_peak"]:.3f} vs {d["r_peak"]:.3f}'
            ok &= bool(c)
            lines.append(txt)
        return ok, lines

    S1, s1_lines = sampling_test(0.10)
    log('')
    for ln in s1_lines:
        log('   S1 ' + ln)
    log(f"[{'PASS' if S1 else 'FAIL'}] S1 break and peak unchanged by discrete (non-Gaussian) sampling, theta = 0.10")
    I2 = {}
    for th in [t for t in THETAS if abs(t - 0.10) > 1e-12]:
        ok, lines = sampling_test(th)
        I2[th] = ok
        for ln in lines:
            log(f'   (info I2, theta {th}) ' + ln)
        log(f'   (info I2) theta {th}: sampling criteria {"met" if ok else "not met"}')

    # ---- S2: spectral collapse at new angles
    S2 = {}
    N1, N2 = N_LIST[-2], N_LIST[-1]
    for th in [t for t in THETAS if abs(t - 0.10) > 1e-12]:
        a, b_ = R[(th, 'unstable', 'gaussian', N1)], R[(th, 'unstable', 'gaussian', N2)]
        found = all(np.isfinite(w['t_peak']) and not w['at_edge'] for w in (a, b_))
        dx = abs(b_['x_peak'] / a['x_peak'] - 1) if found else np.inf
        ex = np.log(b_['t_peak'] / a['t_peak']) / np.log(N2 / N1) if found else np.nan
        hts = [a['r_peak'], b_['r_peak']]
        ok = found and dx <= 0.05 and 0.85 <= ex <= 1.15 and all(0.10 <= h <= 0.30 for h in hts)
        S2[th] = bool(ok)
        log(f"[{'PASS' if ok else 'FAIL'}] S2 theta = {th}: peak found {found}; t_peak/t_spec {a['x_peak']:.3f} -> "
            f"{b_['x_peak']:.3f} ({dx:.1%}); exponent {ex:.3f}; heights {hts[0]:.3f}, {hts[1]:.3f}")

    # ---- info
    I1 = {th: R[(th, 'unstable', 'gaussian', N_LIST[-1])]['x_peak'] for th in THETAS}
    log('   (info I1) kappa = t_peak/t_spec at N = %d: ' % N_LIST[-1]
        + ', '.join(f'theta {th}: {k:.3f}' for th, k in I1.items()))
    for N in N_LIST:
        w = R[(CONTROL_THETA, 'control', 'gaussian', N)]
        log(f"   (info I3) control N={N}: t_spec {w['t_spec']:.0f}, t_b {w['t_b_5pct']:.1f}, peak at "
            f"{w['x_peak']:.3f} t_spec, height {w['r_peak']:.3f}" + (' (at end of run)' if w['at_edge'] else '')
            + f"; largest r over the run {w['r_max_run']:+.3f} at {w['x_max_run']:.3f} t_spec")
    I4 = {f'{th}_{b}_{N}': R[(th, 'unstable' if b == 1 else 'control', 'discrete', N)]['twa_late_mean']
          / R[(th, 'unstable' if b == 1 else 'control', 'gaussian', N)]['twa_late_mean'] - 1
          for th, b in cases for N in N_LIST}
    log('   (info I4) late classical QFI/N, discrete/gaussian - 1: '
        + ', '.join(f'{k}: {v:+.2%}' for k, v in I4.items()))

    allV = all(V.values())
    verdict = (f"checks {'all pass' if allV else 'FAILED - verdict not valid'}; S1 {'PASS' if S1 else 'FAIL'}; "
               + '; '.join(f"S2 theta={th} {'PASS' if ok else 'FAIL'}" for th, ok in S2.items()))
    log(f'\nVERDICT ({time.time() - t0:.0f} s): ' + verdict)

    # ---- outputs
    with open(os.path.join(outdir, 'cases.csv'), 'w', newline='') as fh:
        wr = csv.DictWriter(fh, fieldnames=list(rows[0]))
        wr.writeheader()
        wr.writerows(rows)
    np.savez_compressed(os.path.join(outdir, 'series.npz'), **series)
    summary = dict(settings=dict(N_LIST=N_LIST, MU=MU, THETAS=THETAS, CONTROL_THETA=CONTROL_THETA, T_MAX=T_MAX, DT=DT,
                                 WIDTH=WIDTH, LEVEL=LEVEL, T_MIN=T_MIN, M_TWA=M_TWA, TWA_SUBSTEPS=TWA_SUBSTEPS,
                                 SEED_GAUSS=SEED_GAUSS, SEED_DISCRETE=SEED_DISCRETE, QUICK=QUICK),
                   checks=V, V1_deviation=dev, V2_norm=ndev, V3_variance=vd, S1=S1, S1_lines=s1_lines, S2=S2,
                   I1_kappa=I1, I2=I2, I4=I4, verdict=verdict, cases=rows)
    json.dump(summary, open(os.path.join(outdir, 'summary.json'), 'w'), indent=1, default=float)
    try:
        import matplotlib
        if 'google.colab' not in sys.modules:
            matplotlib.use('Agg')
        import matplotlib.pyplot as plt
        blues = ['#86b6ef', '#3987e5', '#1c5cab', '#0d366b'][-len(N_LIST):]
        fig, axs = plt.subplots(2, 2, figsize=(11, 7.5))
        panels = [((th, 1), f'theta = {th}, unstable') for th in THETAS] + [((CONTROL_THETA, -1), 'theta = 0.10, control')]
        for ax, ((th, b), title) in zip(axs.flat, panels):
            for N, col in zip(N_LIST, blues):
                for smp, ls in zip(SAMPLINGS, ('-', '--')):
                    tc = series[f'tc_th{th}_b{b}_N{N}_{smp}']
                    ax.plot(tc / tspec[(th, b, N)], series[f'r_th{th}_b{b}_N{N}_{smp}'], color=col, ls=ls, lw=1,
                            label=f'N={N}, {smp}')
            ax.axhline(LEVEL, color='grey', ls=':', lw=0.8)
            ax.axhline(0, color='lightgrey', lw=0.8)
            ax.set_xlim(0, 2.0 if b == -1 else 1.6)
            ax.set_xlabel('t / t_spec')
            ax.set_ylabel('running excess r')
            ax.set_title(title, fontsize=10)
        axs[0, 0].legend(fontsize=7, ncol=2)
        fig.tight_layout()
        fig.savefig(os.path.join(outdir, 'fig_step2.png'), dpi=150)
        plt.show()
    except Exception as exc:
        log('plot skipped:', exc)

    src = _source_text()
    end = datetime.now(timezone.utc)
    meta = dict(start_utc=start.isoformat(), end_utc=end.isoformat(), duration_s=round(time.time() - t0, 1),
                python=sys.version.split()[0], numpy=np.__version__, scipy=scipy.__version__,
                platform=platform.platform(), cpu_count=ncpu, in_colab='google.colab' in sys.modules,
                script_sha256=hashlib.sha256(src.rstrip('\n').encode() + b'\n').hexdigest() if src else None,
                smoke_test=bool(os.environ.get('MQG_SMOKE')))
    if src:
        open(os.path.join(outdir, 'mqg_step2_robustness.py'), 'w', encoding='utf-8').write(src.rstrip('\n') + '\n')
    else:
        log('   (could not find the script source; code copy not saved)')
    json.dump(meta, open(os.path.join(outdir, 'run_metadata.json'), 'w'), indent=1)
    open(os.path.join(outdir, 'run_log.txt'), 'w').write('\n'.join(_LOG) + '\n')
    zpath = shutil.make_archive(outdir, 'zip', '.', outdir)
    log(f'\nall outputs in {outdir}/ and {zpath}')
    if DOWNLOAD and 'google.colab' in sys.modules:
        try:
            from google.colab import files
            files.download(zpath)
        except Exception as exc:
            log('download failed (use the Files panel on the left):', exc)
    return summary


if __name__ == '__main__':
    summary = main()
