#!/usr/bin/env python3
# -*- coding: utf-8 -*-
r"""v805 -- E8.SYNDROME.ALGEBRA.01: the honest kill -- E8 is NOT an error-correction hull in the sharpest sense, one probe (13/13 checks incl. the 3 must-fire controls, ~6 s; discovery probe e8_syndrome_algebra_probe.py, 2026-08-06, verdict SYNDROME-DEAD, the kill fired exactly as preregistered).  THE QUESTION: do the 240 root operators U_r |x> = i^{c(r).x} |x XOR v> plus the 16 neutral kernels form a 256-dimensional mu4-twisted V-graded basis of End(S+)?  THE KILL FIRES: the frozen mu4-linear-lift realization family reaches max rank 222/256 over ALL FOUR frozen phase conventions (Im/Re 222, Re+-Im 160; sympy-exact over Q(i)) -- with a STRUCTURAL obstruction: any phase rule Z-linear in the root gives c(-r) = -c(r), so +-r pairs with c in {0,2}^4 produce PROPORTIONAL operators (collision witness printed; 240 - 228 = 12 c-vector collisions), unreachable for the ENTIRE diagonal-phase x translation family; TWO post-hoc diagnostics score WORSE (nonlinear BFS transversal 215, zeta8-linear float 160) -- the death is robust, not a convention artifact; a successor would need root-individual (non-linear-in-r) operator data.  WHAT SURVIVES: the syndrome-table counting 16 + 240 = 256 = 1x16 + 15x16, the neutral sector (the 16 v112 kernels ARE the diagonal maximal abelian subalgebra; same-class products land in A_0 with mu4 coefficients EXACTLY), the fine (class, d, k) groupoid composition law (associative on all 3375 section triples), and the W(3,2) commutation skeleton (scalar commutators 15.1%, matching (-1)^hbar on only 51.5% -- the class-level symplectic reduction fails in this realization).  NO scalar V-cocycle exists (section closure 0/210).  Controls fire (scrambled classes, wrong cocycle i^hbar fails the 2-cocycle identity on 960/4096 triples, random c-assignments).  The sharpest E8-is-a-hull form (twisted V-graded basis) is DEAD as frozen; successor criterion named.  No matter semantics (v775 ROOTCLASS-MIXED stands).  No marker move, NO RH claim.  Python-only per GATE.WOLFRAM.02.

PROVENANCE: discovery probe e8_syndrome_algebra_probe.py (2026-08-06, 13/13, ~6 s, SYNDROME-DEAD; no spec corrections disclosed); re-run identically at promotion.  Promoted verbatim, the script-style probe body wrapped in a function scope (v789/v795 precedent; the wrap indents the multiline RESULT print literal by four spaces -- cosmetic, numbers unchanged); a module-level _LAST verdict capture inserted at the verdict assignment (v791 precedent); numbers unchanged; run() encodes the pattern (v757 precedent).

Original e8_syndrome_algebra_probe.py docstring (verbatim):
e8_syndrome_algebra_probe -- E8.SYNDROME.ALGEBRA.01: is E8 an
error-correction hull in the sharpest sense -- do the 240 root operators
plus the 16 neutral kernels form a 256-dimensional mu4-TWISTED V-graded
basis of End(S+)?

CORPUS ANCHORS (read-only):
  v111_quadratic_transport -- dim End(S+) = 16^2 = 256 = 1 + 45 + 210;
      the 45 quadratic Clifford words (= so(10)) generate the FULL
      operator algebra End(S+) (rank-256 certificate).
  v112_selfcounting_channel -- the self-counting channel has EXACTLY one
      Cartan-neutral kernel per code state: 2^{g-1} = 16 neutral kernels
      at g = 5, Pascal-graded (1, 5, 10).
  v752_projective_hamming_incidence -- the concrete lattice: h = H/4 is
      Z[i]-valued with hermitian Gram determinant 1; V = L/(1+i)L = F2^4;
      240 roots in 15 nonzero classes x 16; hbar alternating,
      nondegenerate (symplectic).
  The counting 16 + 240 = 256 = 16 x 16 is the syndrome-table form:
      1 x 16 neutral + 15 x 16 charged.

THE HYPOTHESIS [H neu] (frozen): with A_0 := span of the 16 neutral
kernels and A_v := span of the 16 class-v root operators (v != 0), the
256 operators are a basis of End(S+) and A_v . A_w <= A_{v+w} holds with
mu4 structure constants (twisted group/groupoid algebra over V).

THE FROZEN REALIZATION (predeclared; the lattice vertex-operator
truncation in Frenkel--Kac style, transplanted to the (1+i)-torsor):
  * S+ := C[V], |x> indexed by the 16 classes x in V, written in the
    corpus family/anchor bit coordinates (F1, F2, F3, ANC basis, v752/
    v689 machinery); the 16 basis states are the 16 code states of the
    v112 self-counting reading.
  * FROZEN TRANSVERSAL: X_x := sum_i x_i B_i (Z-linear lift over the
    family/anchor basis reps B_1..B_4).  This makes every root phase
    Z4-LINEAR by construction -- the entire algebra becomes exact Z4
    arithmetic (no floats anywhere).
  * ROOT OPERATORS: for a root r with class v = [r],
        U_r |x> := i^{c(r).x mod 4} |x XOR v>,
        c_i(r) := phi(h(r, B_i)) mod 4,
    with h(y,z) := hermC(y,z)/2 the Z[i]-valued unimodular hermitian
    form and phi the frozen phase functional.  CANDIDATE CONVENTIONS
    (enumerated BEFORE running, selection rule frozen: the convention
    with maximal exact rank wins, ties to the first):
        phi in { Im (primary), Re, Re+Im, Re-Im }.
    NOTE c is Z-LINEAR in r (c(r+r') = c(r)+c(r'), c(ir) rotates
    Im->Re): the mu4 deck acts on the phase data, the realization is
    mu4-twisted by construction.
  * NEUTRAL KERNELS: the 16 projectors |x><x| (one per code state,
    the v112 neutral kernels).

FROZEN GATES:
  G1 RANK: the 256 operators span End(S+) -- exact rank 256 (per class:
     rank of the 16x16 phase matrix Phi_v[j,x] = i^{c_j.x} over Q(i),
     sympy exact; blocks for different v have disjoint matrix support,
     so rank = 16 + sum_v rank Phi_v).
  G2 GRADED MULTIPLICATION (15x15 census, all 16x16 root pairs per
     cell): U_r U_r' has translation v+w by construction, so subspace
     closure A_v A_w <= A_{v+w} is equivalent to G1; the SHARP census
     is the mu4-coefficient form: for v+w != 0, is the product a SINGLE
     mu4 multiple of a basis root operator (exists r'' in class v+w
     with c(r'') = d(r,r'), where d_i = c_i(r') + c_i(r)(1-2 w_i) mod 4
     and omega = i^{c(r).w})?  For v = w the product is diagonal --
     it decomposes in the kernel basis with coefficients i^{q(x)} in
     mu4 EXACTLY (the zero-syndrome sector).
  G3 COCYCLE: (i) section cocycle: with the frozen section s_v :=
     lex-least root of class v, test whether S_v S_w = omega(v,w)
     S_{v+w} scalar-closes; if yes verify the 2-cocycle identity on
     all 16^3 triples; if not, the cocycle is typed as C(V,mu4)-valued
     (groupoid twist) and the fine composition law (v,c,k)-data is
     verified associative on all section triples.  (ii) PREDECLARED
     CANDIDATE: commutators U_r U_r' = (-1)^{hbar(v,w)} U_r' U_r
     whenever the commutator is scalar (the symplectic-form meaning);
     census of scalar fraction + match fraction.  (iii) the classical
     fact: rank 256 + trivial center => the algebra IS M_16 = End(S+)
     (center computed exactly: translation-invariant diagonals).
  G4 NEUTRAL SECTOR: A_0 = the diagonal maximal abelian subalgebra
     (commutant argument exact); same-class products land in A_0
     (zero syndrome); A_0 A_v <= A_v (module property via G1).

KILL (frozen): rank < 256 in ALL four enumerated conventions, or
products leak outside A_{v+w} beyond the central structure.

CONTROLS (must fire):
  C1 scrambled class assignment (frozen transposition of the two
     lex-first nonzero classes) breaks the grading census;
  C2 the wrong cocycle candidate omega'(v,w) = i^{hbar(v,w)} fails the
     2-cocycle identity (count > 0 over 16^3 triples);
  C3 random c-assignments (frozen seed) fail the rank/grading census
     jointly.

VERDICT ENUM (frozen):
  SYNDROME-ALGEBRA-EXACT : rank 256 + 100% single-element mu4 products
      (v+w != 0) + cocycle identified (section or typed-fine, with the
      symplectic commutator census exact) + neutral gate + controls.
  SYNDROME-PARTIAL : rank 256 (E8 spans End(S+) in the graded sense)
      but the mu4-single-element census or the cocycle identification
      is incomplete (fractions reported).
  SYNDROME-DEAD : rank < 256 in all enumerated conventions.

FENCES: EXPLORATION ONLY (experiments/tfpt-discovery); no matter
semantics -- the v775 ROOTCLASS-MIXED kill (root-level code->matter
reading dead) stands; this is operator-algebra structure only.  Exact
integer/Z4/cyclotomic arithmetic in every load-bearing check.
"""

_LAST = {}


import itertools
import random
import time
from collections import Counter
from fractions import Fraction as Fr

import numpy as np
import sympy as sp

T0 = time.time()
CHECKS = []


def check(name, ok, detail=""):
    CHECKS.append((name, bool(ok)))
    print("[%s] %s%s" % ("PASS" if ok else "FAIL", name,
                         (" -- " + detail) if detail else ""), flush=True)


def section(title):
    print("=" * 78)
    print(title)
    print("=" * 78, flush=True)


# ---------------------------------------------------------------- Z[i]
def gadd(a, b):
    return (a[0] + b[0], a[1] + b[1])


def gmul(a, b):
    return (a[0] * b[0] - a[1] * b[1], a[0] * b[1] + a[1] * b[0])


def gconj(a):
    return (a[0], -a[1])


def hermC(z, w):
    s = (0, 0)
    for a, b in zip(z, w):
        s = gadd(s, gmul(a, gconj(b)))
    return s


# ------------------------------------------- corpus lattice machinery
G_NAIVE = [(1, 0, 0, 0, 0, 1, 1, 1),
           (0, 1, 0, 0, 1, 0, 1, 1),
           (0, 0, 1, 0, 1, 1, 0, 1),
           (0, 0, 0, 1, 1, 1, 1, 0)]
C_NAIVE = frozenset(tuple(sum(m[k] * G_NAIVE[k][j] for k in range(4)) % 2
                          for j in range(8))
                    for m in itertools.product((0, 1), repeat=4))
PI_J = (1, 0, 3, 2, 5, 4, 7, 6)
PI_SIG = (4, 5, 0, 1, 2, 3, 6, 7)


def apply_perm(c, p):
    return tuple(c[p[k]] for k in range(8))


def code_image(code, p):
    return frozenset(apply_perm(c, p) for c in code)


def mat_det_inv(rows):
    n = len(rows)
    A = [[Fr(v) for v in r] for r in rows]
    I = [[Fr(1 if i == j else 0) for j in range(n)] for i in range(n)]
    for col in range(n):
        piv = next(r for r in range(col, n) if A[r][col] != 0)
        if piv != col:
            A[col], A[piv] = A[piv], A[col]
            I[col], I[piv] = I[piv], I[col]
        inv = 1 / A[col][col]
        A[col] = [a * inv for a in A[col]]
        I[col] = [a * inv for a in I[col]]
        for r in range(n):
            if r != col and A[r][col] != 0:
                f = A[r][col]
                A[r] = [a - f * b for a, b in zip(A[r], A[col])]
                I[r] = [a - f * b for a, b in zip(I[r], I[col])]
    return I


def vec_mat(x, M):
    n = len(M)
    return tuple(sum(x[i] * M[i][j] for i in range(n)) for j in range(n))


def row_hnf(rows):
    M = [list(map(int, r)) for r in rows]
    m = len(M)
    for col in range(m):
        piv = next(r for r in range(col, m) if M[r][col] != 0)
        M[col], M[piv] = M[piv], M[col]
        for r in range(col + 1, m):
            while M[r][col] != 0:
                q = M[col][col] // M[r][col]
                M[col] = [a - q * b for a, b in zip(M[col], M[r])]
                M[col], M[r] = M[r], M[col]
        if M[col][col] < 0:
            M[col] = [-a for a in M[col]]
    return M


def hnf_reduce(c, H):
    c = list(c)
    for i in range(len(H)):
        q = c[i] // H[i][i]
        if q:
            c = [a - q * b for a, b in zip(c, H[i])]
    return tuple(c)


def J_vec(x):
    out = []
    for k in range(0, 8, 2):
        out += [-x[k + 1], x[k]]
    return tuple(out)


def sig_vec(x):
    return (x[4], x[5], x[0], x[1], x[2], x[3], x[6], x[7])


def add_vec(x, y):
    return tuple(a + b for a, b in zip(x, y))


def f2_rref(words):
    rows = [list(w) for w in sorted(words, reverse=True) if any(w)]
    basis, pivots = [], []
    for r in rows:
        r = r[:]
        for b, pv in zip(basis, pivots):
            if r[pv]:
                r = [(a + c) % 2 for a, c in zip(r, b)]
        if any(r):
            basis.append(r)
            pivots.append(next(i for i, a in enumerate(r) if a))
    return basis, pivots


def main():
    """Probe body, promoted verbatim in a function scope (v789/v795 precedent; the probe is script-style)."""
    section("P0: corpus rebuild -- lattice, 240 = 15 x 16, family/anchor "
            "basis, hbar")

    all_placements = set()
    for p in itertools.permutations(range(8)):
        all_placements.add(code_image(C_NAIVE, p))
    both_inv = [c for c in sorted(all_placements, key=lambda c: sorted(c))
                if code_image(c, PI_J) == c and code_image(c, PI_SIG) == c]
    W0246 = tuple(1 if i in (0, 2, 4, 6) else 0 for i in range(8))
    CSTAR = [c for c in both_inv if W0246 in c][0]

    cb, pivots = f2_rref(CSTAR)
    BROWS = [tuple(r) for r in cb]
    BROWS += [tuple(2 if i == j else 0 for i in range(8))
              for j in range(8) if j not in pivots]
    Binv = mat_det_inv(BROWS)


    def coords(x):
        c = vec_mat(x, Binv)
        assert all(v.denominator == 1 for v in c)
        return tuple(int(v) for v in c)


    HNF = row_hnf([coords(add_vec(b, J_vec(b))) for b in BROWS])


    def label(x):
        return hnf_reduce(coords(x), HNF)


    ROOTS = [x for x in itertools.product(range(-2, 3), repeat=8)
             if sum(v * v for v in x) == 4
             and tuple(v % 2 for v in x) in CSTAR]
    ROOTS.sort()
    ZERO = label((0,) * 8)

    REPS = {ZERO: (0,) * 8}
    frontier = [(0,) * 8]
    while frontier:
        v = frontier.pop()
        for b in BROWS:
            w = add_vec(v, b)
            l = label(w)
            if l not in REPS:
                REPS[l] = w
                frontier.append(w)

    root_label = {r: label(r) for r in ROOTS}
    cls_census = Counter(root_label.values())
    check("P0.1 240 roots, 15 nonzero classes x 16 (v752 census)",
          len(ROOTS) == 240 and len(cls_census) == 15
          and set(cls_census.values()) == {16} and len(REPS) == 16)


    def chart(r):
        return ((r[0], r[1]), (r[2], r[3]), (r[4], r[5]), (r[6], r[7]))


    def h_form(y, z):
        s = hermC(chart(y), chart(z))
        assert s[0] % 2 == 0 and s[1] % 2 == 0, "h not Z[i]-valued"
        return (s[0] // 2, s[1] // 2)


    def sig_label(lb):
        return label(sig_vec(REPS[lb]))


    def family_anchor_basis():
        fixed_labels = [lb for lb in REPS if sig_label(lb) == lb]
        for lb in REPS:
            if lb == ZERO or sig_label(lb) == lb:
                continue
            o1, o2 = lb, sig_label(lb)
            o3 = sig_label(o2)
            s = label(add_vec(add_vec(REPS[o1], REPS[o2]), REPS[o3]))
            if s == ZERO:
                continue
            span3 = set()
            for bits in itertools.product((0, 1), repeat=3):
                w = (0,) * 8
                for bit, l2 in zip(bits, (o1, o2, o3)):
                    if bit:
                        w = add_vec(w, REPS[l2])
                span3.add(label(w))
            if len(span3) != 8:
                continue
            anchor = next(l2 for l2 in fixed_labels
                          if l2 != ZERO and l2 not in span3)
            return (o1, o2, o3, anchor, s)
        raise AssertionError


    F1L, F2L, F3L, ANCL, FSUML = family_anchor_basis()
    BASIS_L = (F1L, F2L, F3L, ANCL)
    B_REPS = [REPS[lb] for lb in BASIS_L]
    BITS_V = {}
    for bits in itertools.product((0, 1), repeat=4):
        w = (0,) * 8
        for bit, l2 in zip(bits, BASIS_L):
            if bit:
                w = add_vec(w, REPS[l2])
        BITS_V[label(w)] = bits
    ALLX = list(itertools.product((0, 1), repeat=4))
    NZV = [b for b in ALLX if any(b)]


    def bxor(a, b):
        return tuple((x + y) % 2 for x, y in zip(a, b))


    def hbar(y, z):
        s = h_form(y, z)
        return (s[0] + s[1]) % 2


    GRAM = [[hbar(B_REPS[i], B_REPS[j]) for j in range(4)] for i in range(4)]


    def hbar_bits(a, b):
        return sum(a[i] * GRAM[i][j] * b[j]
                   for i in range(4) for j in range(4)) % 2


    gr = sp.Matrix(GRAM)
    check("P0.2 h(r,r) = 2 on all roots; hbar alternating, nondegenerate "
          "(Gram rank 4 over F2) -- the v752 symplectic structure",
          all(h_form(r, r) == (2, 0) for r in ROOTS)
          and all(GRAM[i][i] == 0 for i in range(4))
          and sp.Matrix(GRAM).det() % 2 == 1)

    class_roots = {}
    for r in ROOTS:
        class_roots.setdefault(BITS_V[root_label[r]], []).append(r)

    # ================================================================== P1
    section("P1 (G1): the frozen realization and the exact RANK census")


    def c_vec(r, phi):
        out = []
        for B in B_REPS:
            z = h_form(r, B)
            val = {"Im": z[1], "Re": z[0],
                   "Re+Im": z[0] + z[1], "Re-Im": z[0] - z[1]}[phi]
            out.append(val % 4)
        return tuple(out)


    PHIS = ["Im", "Re", "Re+Im", "Re-Im"]
    I4 = sp.I
    rank_results = {}
    for phi in PHIS:
        total = 16          # A_0: the 16 projectors (diagonal block)
        per_class = []
        for v in NZV:
            cs = [c_vec(r, phi) for r in class_roots[v]]
            M = sp.Matrix([[sp.I ** (sum(ci * xi for ci, xi in zip(c, x)) % 4)
                            for x in ALLX] for c in cs])
            rk = M.rank()
            per_class.append(rk)
            total += rk
        rank_results[phi] = (total, per_class)
        print("    phi = %-6s -> total rank %d (per-class ranks %s)"
              % (phi, total, sorted(Counter(per_class).items())), flush=True)

    best_phi = max(PHIS, key=lambda p: (rank_results[p][0], -PHIS.index(p)))
    RANK, PER_CLASS = rank_results[best_phi]
    RANK_GATE = (RANK == 256)
    check("P1.1 RANK GATE ADJUDICATED: best convention phi = %s reaches "
          "rank %d of 256 -- the frozen kill 'rank < 256 in all four "
          "enumerated conventions' %s (census exact, sympy over Q(i))"
          % (best_phi, RANK,
             "FIRES: the mu4-linear-lift realization family CANNOT span "
             "End(S+)" if not RANK_GATE else "does not fire"),
          RANK <= 256 and all(rank_results[p][0] <= 256 for p in PHIS))

    CV = {v: [c_vec(r, best_phi) for r in class_roots[v]] for v in NZV}
    CSET = {v: {c: k for k, c in enumerate(CV[v])} for v in NZV}
    n_distinct = sum(len(set(CV[v])) for v in NZV)
    mod2_cosets = sum(1 for v in NZV
                      if len({tuple(ci % 2 for ci in c) for c in CV[v]}) == 1)
    wit = None
    for v in NZV:
        seen = {}
        for r, c in zip(class_roots[v], CV[v]):
            if c in seen:
                wit = (seen[c], r, c)
                break
            seen[c] = r
        if wit:
            break
    print("    c-vector census: %d/240 distinct within classes; classes "
          "whose 16 c-vectors form a single {0,2}^4-coset: %d/15"
          % (n_distinct, mod2_cosets), flush=True)
    if wit:
        print("    COLLISION WITNESS (kills independence): roots %s and %s "
              "share c = %s -- their operators are proportional"
              % (wit[0], wit[1], wit[2]), flush=True)

    # ================================================================== P2
    section("P2 (G2): the graded multiplication census (15 x 15 cells)")


    def product_data(c_r, v, c_rp, w):
        """U_r U_r' with [r] = v, [r'] = w: returns (target class, d, k):
        U_r U_r' |x> = i^{k + d.x} |x XOR v XOR w>, exact Z4."""
        d = tuple((c_rp[i] + c_r[i] * (1 - 2 * w[i])) % 4 for i in range(4))
        k = sum(c_r[i] * w[i] for i in range(4)) % 4
        return bxor(v, w), d, k


    hit_cells = 0
    tot_pairs = 0
    hit_pairs = 0
    cell_report = {}
    diag_cells_ok = True
    for v in NZV:
        for w in NZV:
            tgt = bxor(v, w)
            hits = 0
            for c_r in CV[v]:
                for c_rp in CV[w]:
                    _, d, k = product_data(c_r, v, c_rp, w)
                    if tgt == (0, 0, 0, 0):
                        hits += 1          # diagonal: mu4 coefficients exact
                    elif d in CSET[tgt]:
                        hits += 1
            cell_report[(v, w)] = hits
            tot_pairs += 256
            hit_pairs += hits
            if hits == 256:
                hit_cells += 1
    frac = hit_pairs / float(tot_pairs)
    n_offdiag = sum(1 for v in NZV for w in NZV if bxor(v, w) != (0, 0, 0, 0))
    perfect_offdiag = sum(1 for (v, w), h in cell_report.items()
                          if bxor(v, w) != (0, 0, 0, 0) and h == 256)
    diag_cells = [(v, w) for v in NZV for w in NZV
                  if bxor(v, w) == (0, 0, 0, 0)]
    check("P2.1 GRADING CENSUS (finding): translation grading v+w is "
          "structural; subspace closure A_v A_w <= A_{v+w} would need "
          "rank 16 per class (fails with G1); SHARP mu4-single-element "
          "census: %d/%d root pairs (%.1f%%), perfect cells %d/%d "
          "off-diagonal + %d/15 diagonal (diagonal cells are A_0-valued "
          "with mu4 coefficients EXACTLY, by construction)"
          % (hit_pairs, tot_pairs, 100 * frac, perfect_offdiag, n_offdiag,
             len(diag_cells)),
          len(cell_report) == 225 and tot_pairs == 57600,
          "gate outcome adjudicated in the verdict")
    SINGLE_OK = (perfect_offdiag == n_offdiag)

    # worst / best cells
    by_hits = sorted(cell_report.items(), key=lambda kv: kv[1])
    print("    worst cells: %s" % [(h) for _, h in by_hits[:5]])
    print("    best  cells: %s" % [(h) for _, h in by_hits[-5:]], flush=True)

    # ================================================================== P3
    section("P3 (G3): the cocycle -- section, commutators, M16")

    SEC = {v: CV[v][0] for v in NZV}      # lex-least root per class (frozen)
    sec_scalar = 0
    sec_omega = {}
    for v in NZV:
        for w in NZV:
            tgt = bxor(v, w)
            if tgt == (0, 0, 0, 0):
                continue
            _, d, k = product_data(SEC[v], v, SEC[w], w)
            if d == SEC[tgt]:
                sec_scalar += 1
                sec_omega[(v, w)] = k
    n_sec = 15 * 14
    check("P3.1 SECTION COCYCLE: the frozen section scalar-closes on "
          "%d/%d class pairs -- %s" %
          (sec_scalar, n_sec,
           "omega: V x V -> mu4 exists on the section" if sec_scalar == n_sec
           else "NO scalar V-cocycle; the twist is C(V,mu4)-valued "
           "(groupoid cocycle), typed"),
          True, "existence census reported; identity tested where defined")

    # fine associativity: (U_r U_r') U_r'' = U_r (U_r' U_r'') on all
    # section triples, exact composition of (class, d, k) data
    assoc_fail = 0
    for u in NZV:
        cu = SEC[u]
        for v in NZV:
            cv2 = SEC[v]
            t1, d1, k1 = product_data(cu, u, cv2, v)
            for w in NZV:
                cw = SEC[w]
                # left: (uv) then w ... compose product operator with U_w
                # (t1, d1, k1) applied after U_w:
                dl = tuple((cw[i] + d1[i] * (1 - 2 * w[i])) % 4
                           for i in range(4))
                kl = (k1 + sum(d1[i] * w[i] for i in range(4))) % 4
                tl = bxor(t1, w)
                # right: u after (vw)
                t2, d2, k2 = product_data(cv2, v, cw, w)
                dr = tuple((d2[i] + cu[i] * (1 - 2 * t2[i])) % 4
                           for i in range(4))
                kr = (k2 + sum(cu[i] * t2[i] for i in range(4))) % 4
                tr = bxor(u, t2)
                if (tl, dl, kl) != (tr, dr, kr):
                    assoc_fail += 1
    check("P3.2 FINE COMPOSITION LAW associative on all %d section triples "
          "(exact (class, d, k) arithmetic; %d failures) -- the twisted "
          "groupoid structure is consistent" % (15 ** 3, assoc_fail),
          assoc_fail == 0)

    # commutator census over all 240 x 240 root pairs (vectorized)
    allC = np.array([c for v in NZV for c in CV[v]], dtype=np.int64)
    allV = np.array([v for v in NZV for c in CV[v]], dtype=np.int64)
    N = 240
    sgn_w = 1 - 2 * allV                     # (N,4)
    d12 = (allC[None, :, :] + allC[:, None, :] * sgn_w[None, :, :]) % 4
    d21 = (allC[:, None, :] + allC[None, :, :] * sgn_w[:, None, :]) % 4
    scalar_mask = np.all(d12 == d21, axis=2)
    k12 = (allC[:, None, :] * allV[None, :, :]).sum(axis=2) % 4
    k21 = (allC[None, :, :] * allV[:, None, :]).sum(axis=2) % 4
    lam = (k12 - k21) % 4                    # commutator exponent
    hb = np.array([[hbar_bits(tuple(a), tuple(b)) for b in allV[:, :]]
                   for a in allV[:, :]], dtype=np.int64) % 2
    n_scalar = int(scalar_mask.sum())
    match = int(((lam == 2 * hb) & scalar_mask).sum())
    lam_even = int((((lam % 2) == 0) & scalar_mask).sum())
    check("P3.3 COMMUTATOR CENSUS (predeclared candidate: U U' = "
          "(-1)^hbar U' U where scalar): scalar commutators %d/57600 "
          "(%.1f%%); of these, lambda = (-1)^hbar exactly: %d (%.1f%%); "
          "lambda always in {+-1} (mu2): %s"
          % (n_scalar, n_scalar / 576.0, match,
             100.0 * match / max(n_scalar, 1), lam_even == n_scalar),
          True, "candidate adjudicated in verdict")
    COCYCLE_OK = (n_scalar > 0 and match == n_scalar)

    # center: rank 256 => algebra = End(S+); trivial center check via
    # transitivity: a diagonal invariant under all translations is constant
    trans_transitive = all(any(bxor(x, v) == y for v in [(0, 0, 0, 0)] + NZV)
                           for x in ALLX for y in ALLX)
    check("P3.4 M16 STRUCTURE (conditional fact): IF the span were full "
          "(rank 256), it would be End(S+) = M_16 (v111) with trivial "
          "center (V transitive on itself: %s; hbar nondegenerate: %s); "
          "the frozen realization reaches only rank %d, so E8's operators "
          "here generate a PROPER graded subalgebra basis-wise"
          % (trans_transitive, gr.det() % 2 == 1, RANK),
          trans_transitive and gr.det() % 2 == 1)

    # ================================================================== P4
    section("P4 (G4): the neutral sector A_0")

    # maximal abelian: an operator commuting with all projectors is
    # diagonal: [P_x, E_ab] = delta_xa E_ab - delta_xb E_ab != 0 for a != b
    offdiag_killed = all(any((1 if x == a else 0) != (1 if x == b else 0)
                             for x in ALLX)
                         for a in ALLX for b in ALLX if a != b)
    zero_syndrome = all(bxor(v, v) == (0, 0, 0, 0) for v in NZV)
    check("P4.1 A_0 = span{16 projectors |x><x|} (the v112 neutral "
          "kernels, one per code state) IS the diagonal maximal abelian "
          "subalgebra: commutant-of-projectors = diagonals (matrix-unit "
          "argument exact: %s); same-class products land in A_0 (zero "
          "syndrome, P2 diagonal cells: exact); the module property "
          "A_0 A_v <= A_v holds only for full-rank classes (%d/15 have "
          "rank 16) -- the neutral gate passes, the charged spans do not"
          % (offdiag_killed, sum(1 for r in PER_CLASS if r == 16)),
          offdiag_killed and zero_syndrome)

    # ================================================================= P4b
    section("P4b: POST-HOC DIAGNOSTICS (observational, NOT gates -- "
            "successor-hypothesis pointers only)")

    INV_BITS = {v: k for k, v in BITS_V.items()}
    # (i) nonlinear corpus transversal (label_group BFS reps), mu4 phases
    ph_total = 16
    for v in NZV:
        rows = []
        for r in class_roots[v]:
            row = []
            for x in ALLX:
                z = h_form(r, REPS[INV_BITS[x]])
                row.append(sp.I ** (z[1] % 4))
            rows.append(row)
        ph_total += sp.Matrix(rows).rank()
    print("    (i) NONLINEAR corpus transversal (BFS reps, phi = Im, "
          "mu4): total rank %d/256 (exact)" % ph_total, flush=True)

    # (ii) zeta8-phases on the linear transversal (float diagnostic ONLY)
    z8 = np.exp(1j * np.pi / 4)
    z8_total = 16
    for v in NZV:
        rows = []
        for r in class_roots[v]:
            ms = [(h_form(r, B)[0] + h_form(r, B)[1]) % 8 for B in B_REPS]
            rows.append([z8 ** (sum(m * xi for m, xi in zip(ms, x)) % 8)
                         for x in ALLX])
        s = np.linalg.svd(np.array(rows), compute_uv=False)
        z8_total += int((s > 1e-9).sum())
    print("    (ii) zeta8-linear phases (metaplectic/half-deck flavour, "
          "FLOAT diagnostic): total rank %d/256 -- if > mu4 ranks, the "
          "missing span lives in the zeta8 (missing-coset) direction, "
          "matching the sibling modular-S finding" % z8_total, flush=True)
    check("P4b.1 post-hoc diagnostics recorded (typed observational; no "
          "gate, no verdict influence): nonlinear-mu4 rank %d, zeta8 "
          "float rank %d, frozen-family rank %d"
          % (ph_total, z8_total, RANK), True)

    # ================================================================== P5
    section("P5: CONTROLS (must fire)")

    # C1: scrambled class assignment (frozen transposition)
    va, vb = sorted(NZV)[0], sorted(NZV)[1]


    def scramble(v):
        return vb if v == va else va if v == vb else v


    s_hit = 0
    s_tot = 0
    for v in NZV:
        for w in NZV:
            tgt_claimed = bxor(scramble(v), scramble(w))
            if tgt_claimed == (0, 0, 0, 0):
                continue
            for c_r in CV[v]:
                for c_rp in CV[w]:
                    _, d, k = product_data(c_r, v, c_rp, w)
                    s_tot += 1
                    # true translation is v+w; claimed target is scrambled:
                    if bxor(v, w) == tgt_claimed and d in CSET.get(
                            tgt_claimed, {}):
                        s_hit += 1
    check("P5.1 C1 FIRES: scrambled class assignment (transposition of "
          "the two lex-first classes) breaks the grading census: %d/%d "
          "hits under scrambling vs %d/%d true (translation grading "
          "mismatches the scrambled targets)"
          % (s_hit, s_tot, hit_pairs, tot_pairs),
          s_hit < hit_pairs)

    # C2: wrong cocycle candidate omega'(v,w) = i^{hbar(v,w)}
    c2_fail = 0
    for u in ALLX:
        for v in ALLX:
            for w in ALLX:
                lhs = (hbar_bits(u, bxor(v, w)) + hbar_bits(v, w)) % 4
                rhs = (hbar_bits(bxor(u, v), w) + hbar_bits(u, v)) % 4
                if lhs != rhs:
                    c2_fail += 1
    check("P5.2 C2 FIRES: the wrong candidate omega' = i^hbar fails the "
          "2-cocycle identity on %d/4096 triples (i^hbar is only mu2-"
          "bilinear mod 2, not a mu4 2-cocycle)" % c2_fail, c2_fail > 0)

    # C3: random c-assignments (frozen seed)
    rng = random.Random(20260806)
    CV_R = {v: [tuple(rng.randrange(4) for _ in range(4)) for _ in range(16)]
            for v in NZV}
    r_rank = 16
    for v in NZV:
        M = sp.Matrix([[sp.I ** (sum(ci * xi for ci, xi in zip(c, x)) % 4)
                        for x in ALLX] for c in CV_R[v]])
        r_rank += M.rank()
    CSET_R = {v: set(CV_R[v]) for v in NZV}
    r_hit = 0
    r_tot = 0
    for v in NZV:
        for w in NZV:
            tgt = bxor(v, w)
            if tgt == (0, 0, 0, 0):
                continue
            for c_r in CV_R[v]:
                for c_rp in CV_R[w]:
                    _, d, k = product_data(c_r, v, c_rp, w)
                    r_tot += 1
                    if d in CSET_R[tgt]:
                        r_hit += 1
    true_offdiag_frac = ((hit_pairs - 256 * len(diag_cells))
                         / float(tot_pairs - 256 * len(diag_cells)))
    check("P5.3 C3 FIRES: random c-assignments (seed frozen): rank %d, "
          "single-element census %d/%d (%.2f%%) vs true %.2f%% -- the "
          "rank/grading stack fails jointly"
          % (r_rank, r_hit, r_tot, 100.0 * r_hit / r_tot,
             100.0 * true_offdiag_frac),
          (r_rank < 256) or (r_hit / float(r_tot) < 0.5 * true_offdiag_frac
                             if true_offdiag_frac > 0 else r_hit == 0))

    # ============================================================ VERDICT
    section("SUMMARY / VERDICT (frozen enum)")
    n_pass = sum(1 for _, ok in CHECKS if ok)
    print("%d/%d checks passed" % (n_pass, len(CHECKS)))
    controls = all(ok for n, ok in CHECKS if n.startswith("P5"))
    if not controls:
        verdict = "TEST-VOID"
    elif RANK == 256 and SINGLE_OK and COCYCLE_OK and assoc_fail == 0:
        verdict = "SYNDROME-ALGEBRA-EXACT"
    elif RANK == 256:
        verdict = "SYNDROME-PARTIAL"
    else:
        verdict = "SYNDROME-DEAD"
    _LAST["verdict"] = verdict
    print("VERDICT: %s" % verdict)
    print("""
    RESULT:
      * realization (frozen): S+ = C[V], Z-linear family/anchor
        transversal, root phases Z4-linear c_i(r) = %s h(r, B_i) mod 4
        (best of the frozen 4-convention enumeration; ranks %s).
      * RANK: %d/256 -- THE FROZEN KILL FIRES: the mu4-linear-lift
        realization family cannot span End(S+); 240 - %d = %d c-vector
        collisions inside classes witness proportional operators.
      * grading: mu4-single-element census %.1f%% of root pairs (%d/%d
        off-diagonal cells perfect) -- no twisted group basis here.
      * cocycle: NO scalar V-cocycle (section closure %d/%d); the fine
        (class,d,k) groupoid law IS associative (0/3375 failures); scalar
        commutators %.1f%%, matching (-1)^hbar on only %.1f%% -- the
        class-level symplectic reduction FAILS in this realization.
      * neutral sector: PASSES -- the 16 v112 kernels are the diagonal
        maximal abelian subalgebra, and same-class products land in A_0
        with mu4 coefficients exactly (the zero-syndrome sector is real).
      * POST-HOC (observational): BOTH escape directions do WORSE
        (nonlinear transversal 215, zeta8-linear 160 vs 222) -- the death
        is robust, not a convention artifact.  STRUCTURAL OBSTRUCTION:
        any phase rule Z-linear in the root gives c(-r) = -c(r), so +-r
        pairs with c in {0,2}^4 produce PROPORTIONAL operators (witness
        printed in P1) -- 240 independent operators are unreachable in
        the entire diagonal-phase x translation family; a successor would
        need root-individual (non-linear-in-r) operator data.
      * CONNECTION: what SURVIVES is the syndrome-table counting (16 +
        240 = 256 = 1x16 + 15x16), the zero-syndrome sector, and the
        W(3,2) commutation skeleton -- the sharpest E8-is-a-hull form
        (twisted V-graded basis) is DEAD as frozen.
      * FENCES: no matter semantics (v775 ROOTCLASS-MIXED stands).
    """ % (best_phi, {p: rank_results[p][0] for p in PHIS}, RANK,
           n_distinct, 240 - n_distinct,
           100 * frac, perfect_offdiag, n_offdiag, sec_scalar, n_sec,
           n_scalar / 576.0, 100.0 * match / max(n_scalar, 1)))
    print("elapsed: %.1f s" % (time.time() - T0))
    print("ALL CHECKS PASSED" if n_pass == len(CHECKS)
          else "SOME CHECKS FAILED")
    return 0 if n_pass == len(CHECKS) else 1


def run():
    """run_all entry point (v757 precedent): expected pattern 13/13 with
    verdict SYNDROME-DEAD (the preregistered kill)."""
    rc = main()
    n_pass = sum(1 for _n, ok in CHECKS if ok)
    fails = [n for n, ok in CHECKS if not ok]
    v = _LAST.get("verdict", "")
    ok = (rc == 0 and n_pass == len(CHECKS) == 13 and not fails
          and v == "SYNDROME-DEAD")
    print("\n[%s] PATTERN GATE: expected 13/13 with verdict "
          "SYNDROME-DEAD; got %d/%d, fails: %s, verdict: %s"
          % ("PASS" if ok else "FAIL", n_pass, len(CHECKS),
             fails or "none", v))
    print("\nCOMBINED ADJUDICATION: %s -- SYNDROME-DEAD (the "
          "preregistered kill fires): the mu4-linear-lift realization "
          "family cannot span End(S+) -- max rank 222/256 over all four "
          "frozen phase conventions, with the +-r proportionality "
          "obstruction structural for the whole diagonal-phase family "
          "and both post-hoc escape directions scoring worse (215/160). "
          " The syndrome-table counting 16 + 240 = 256, the neutral "
          "diagonal MASA and the exact A_0-valued same-class products "
          "survive; the twisted V-graded basis reading is dead as "
          "frozen; a successor needs root-individual operator data.  "
          "NO RH claim."
          % ("PASS" if ok else "FAIL"))
    return 0 if ok else 1


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