#!/usr/bin/env python3
"""Quals for index.html's account of BGPW's Symmetry Conjecture through order 11 (dreeves, 2026-11-06, of the result of
2026-11-03: on the page, "attributed to claude as usual"), the section of class symmetry: its codes, the upper bounds it
states, and its claim, which these quals recompute from the page's own sequences and the m*(n) chart's bounds.
Run: python3 -B quals/quals_symmetry.py [name ...]   (no arguments: all quals).

Each qual is a function q_*: a passing qual returns, a failing one raises.  Each qual's comment gives its replicata (the
setup) and expectata (what must happen); a failure's message or traceback gives the resultata (what happened instead).
They read numbers and math, not wording (dreeves's rule, 2026-10-26: no qual pins prose).

Vocabulary, each term defined once here:
  conjecture   BGPW's Symmetry Conjecture (their §6), C(m) at n: if some sequence of length n has order m, then some
               (-1)^m-symmetric one does (f_(n-1-i) = (-1)^m f_i).
  period(m)    the least power of 2 above m: by BGPW's Divisibility (mstar's two lemma) a length of order m is a
               multiple of it.
  wrap         p v q v s p*, s = (-1)^m, p* the reversal of p, q empty or s-symmetric: s-symmetric, of order at least
               the least of p's and q's, and so (BGPW's Symmetry: an s-symmetric sequence's exact order k has
               (-1)^k = s) at least the bump of that least order, the least k' >= it with (-1)^k' = s.
  closure      the best orders that the page's sequences give, by joins (lo) and by wraps (sym, for each s), each
               length built from shorter ones, every one used built and its order and symmetry checked exactly.
  tail         A, the least multiple of period(m - 1) from which every multiple of it up to 2A has order m - 1; b, the
               least (-1)^m-symmetric length of order m that is an odd multiple of period(m - 1), or 0 when period(m)
               is twice period(m - 1); threshold 2A + b, from which wraps give every multiple of period(m).
  excluded     a multiple of period(m) below the threshold whose upper bound on the chart (mstar.js) is below m.
  own tests    the chart's own proofs of upper bounds: the two lemma, the residue condition mod 3 in closed form, the
               norm lemma and its residue claims (mstar's two, residue3, norm and RESIDUE, which mstar.py reruns as it
               makes mstar.js).  residue3 since 2026-11-16, when it took the place of RESIDUE's 44 claims of the chain
               (3,) (quals_mstar's q_hi_reasons has dreeves's words); it holds at every even length that 3 does not
               divide, so past the chart it rules out more lengths than the norm lemma did (orders 13 and 14 keep
               their largest uncovered lengths, 3664 and 7328: scratch/mod3box-20261116/symtable.py).
"""
import sys
sys.dont_write_bytecode = True
import os, re, json, html, time, shutil, tempfile, traceback, subprocess
from functools import lru_cache
sys.setrecursionlimit(10000)                  # built()'s recipes nest a join or a wrap per level
HERE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))   # the repository's root, above quals/
sys.path.insert(0, os.path.join(HERE, 'pagegen'))   # mstar.py's directory since 2026-11-10 ([REF]); the root until then
import mstar
import quals_index as qi                      # read only: page, blank_comments, mstar_data, copy_page_dir, chrome, DATA, POW2
from quals_symcenters import table_cells
from math import cos, pi                      # by name: math, below, is the account's formulas
sys.path.insert(0, os.path.join(HERE, 'proofs'))
import symlarge                               # the objects of the proof of every sufficiently large length (2026-11-09)

SECTION = re.compile(r'<section class="symmetry" aria-labelledby="symmetry-title">(.*?)</section>', re.S)
CODE = re.compile(r'<code id="(\d+):(\d+)">([0-9A-F]+)</code>')
MAXM, N = 11, 1600                            # the account's orders, and the closure's lengths (past 1488, its last threshold)


def account(text=None):
    """The account's HTML, from index.html (or text) with its comments blanked: the one section of class symmetry."""
    found = SECTION.findall(qi.blank_comments(qi.page() if text is None else text))
    assert len(found) == 1, f'{len(found)} sections of class symmetry'
    return found[0]


def codes(sec):
    """[(n, m, hex)] for each code of the account, <code id="N:M">."""
    out = [(int(n), int(m), h) for n, m, h in CODE.findall(sec)]
    assert out and len(out) == sec.count('<code'), sec.count('<code')
    return out


def math(sec):
    """The account's math, each \\( ... \\) of it."""
    return re.findall(r'\\\((.*?)\\\)', sec, re.S)


def decode(h, m):
    """The paper's Table 5 code, decoded here on its own: each hex digit's four bits, the most significant first, 1 as
    +1 and 0 as -1, the left half; the right half the left half reversed, times (-1)^m."""
    left = [1 if int(d, 16) >> (3 - j) & 1 else -1 for d in h for j in range(4)]
    return left + [(-1) ** m * t for t in reversed(left)]


def order(a):
    """Exact order by power sums: the least k with sum_i a_i i^k != 0 (0^0 = 1)."""
    k, w = 0, [1] * len(a)
    while sum(x * y for x, y in zip(a, w)) == 0:
        k, w = k + 1, [x * i for i, x in enumerate(w)]
    return k


def order_div(a):
    """Exact order by synthetic division of sum_i a_i x^i by x - 1, the remainder being the sum of the coefficients."""
    c, k = list(a), 0
    while sum(c) == 0:
        acc, q = 0, [0] * (len(c) - 1)
        for i in range(len(c) - 1, 0, -1):
            acc += c[i]
            q[i - 1] = acc
        c, k = q, k + 1
    return k


def holds(a, m):
    """Whether a has order at least m: sum_i a_i i^k = 0 for every k < m (0^0 = 1)."""
    return all(sum(t * i ** k for i, t in enumerate(a)) == 0 for k in range(m))


def symmetry(a):
    """+1 if a is symmetric, -1 if antisymmetric, 0 if neither."""
    return next((s for s in (1, -1) if all(a[-1 - i] == s * t for i, t in enumerate(a))), 0)


def period(m): return 1 << m.bit_length()


def bump(k, s): return k if (-1) ** k == s else k + 1


def sources(text=None, N=N):
    """[(label, signs, order)]: Thue-Morse; the examples of BGPW's Tables 4 and 5 (mstar's copies, and the published
    Table 5's rows that overnight_v5.EXAMPLES holds); every code of the page (code blocks and sentences, the account's
    among them, decoded as page/codes.js decodes them); and the page's joins.  Each asserted of exactly its order."""
    text = qi.blank_comments(qi.page() if text is None else text)
    out = [(f'tau_{k}', mstar.thue_morse(k), k) for k in range(1, N.bit_length())]
    out += [(f'BGPW T4 {n}:{m}', mstar.from_rp(h, m, n), m) for m, ns, h in mstar.BGPW_T4 for n in ns]
    out += [(f'BGPW T5 {n}:{m}', mstar.from_rp(h, m, n), m) for m, n, h in mstar.BGPW_T5]
    out += [(f'BGPW T5 {n}:{m}', mstar.v5.from_hex(h, m), m) for (n, m), h in mstar.v5.EXAMPLES.items()
            if mstar.EXAMPLE_LABELS[n, m] == 'BGPW Table 5']
    coded = {f'{n}:{m}': decode(h, int(m)) for n, m, h in CODE.findall(text)}
    out += [(f'page {key}', a, int(key.split(':')[1])) for key, a in coded.items()]
    out += [(f'page join {ids}', [t for i in ids.split(' ') for t in coded[i]], int(m))
            for ids, m in re.findall(r'<span data-join="([^"]*)" data-order="(\d+)">', text)]
    bad = [(label, len(a), m) for label, a, m in out if order(a) != m]
    assert not bad, bad
    return out


def closure(src, N=N):
    """(lo, sym, built): lo[n] the best order of the sources and their joins at n (wraps included, a symmetric
    sequence being a sequence), sym[s][n] that of the s-symmetric ones by sources and wraps, -1 when none; and
    built(kind, n), the sequence behind lo (kind None) or sym (kind s), built from its recipe."""
    at = {}
    for i, (label, a, m) in enumerate(src): at.setdefault(len(a), []).append(i)
    lo, rlo = [0] * (N + 1), [('ones',)] * (N + 1)
    sym, rsym = {1: [-1] * (N + 1), -1: [-1] * (N + 1)}, {1: [None] * (N + 1), -1: [None] * (N + 1)}
    for n in range(1, N + 1):
        for s in (1, -1):
            cands = ([(src[i][2], ('src', i)) for i in at.get(n, []) if symmetry(src[i][1]) == s]
                     + [(bump(min(lo[(n - q) // 2], sym[s][q] if q else 10 ** 9), s), ('wrap', (n - q) // 2, q))
                        for q in range(n % 2, n - 1, 2) if q == 0 or sym[s][q] >= 0])
            sym[s][n], rsym[s][n] = max(cands, key=lambda c: c[0], default=(-1, None))
        cands = ([(0, ('ones',))] + [(src[i][2], ('src', i)) for i in at.get(n, [])]
                 + [(min(lo[a], lo[n - a]), ('join', a, n - a)) for a in range(1, n // 2 + 1)]
                 + [(sym[s][n], ('sym', s)) for s in (1, -1)])
        lo[n], rlo[n] = max(cands, key=lambda c: c[0])
    memo = {}
    def built(kind, n):
        if (kind, n) not in memo:
            r = rlo[n] if kind is None else rsym[kind][n]
            rule = {'ones': lambda: [1] * n, 'src': lambda: src[r[1]][1],
                    'join': lambda: built(None, r[1]) + built(None, r[2]), 'sym': lambda: built(r[1], n),
                    'wrap': lambda: built(None, r[1]) + (built(kind, r[2]) if r[2] else [])
                                    + [kind * t for t in reversed(built(None, r[1]))]}
            memo[kind, n] = rule[r[0]]()
        return memo[kind, n]
    return lo, sym, built


def own_hi(n):
    """The chart's own upper bound at n, without its citations: two, the residue condition mod 3 (at an even n that 3
    does not divide), norm and the RESIDUE claim at n."""
    return min([mstar.two(n)] + [mstar.residue3(n) for _ in [0] if n % 2 == 0 and n % 3]
               + [mstar.norm(n, p) for p in mstar.PRIMES if n % p]
               + [m - 1 for k, (m, _, _) in mstar.RESIDUE.items() if k == n])


def reduction(text=None, MAXM=MAXM, N=N):
    """For each m from 1 to MAXM (the account's 11, or 12 for its paragraph on order 12, with N the closure's lengths
    to match: q_order_12_is_down_to_528): (A, b, threshold, uncovered, needed), uncovered the multiples of period(m) below the
    threshold with neither a (-1)^m-symmetric sequence of order m in the closure (each one built and checked) nor a
    chart bound below m, and needed the (n, hi) of the excluded ones whose bound is not the chart's own (own_hi);
    the sequences of order m - 1 from A to 2A, and every multiple of period(m) from the threshold to N, built by the
    closure too, and checked."""
    cols = qi.mstar_data()['cols']
    lo, sym, built = closure(sources(text, N), N)
    out = {}
    for m in range(1, MAXM + 1):
        s, g0, g = (-1) ** m, period(m - 1), period(m)
        A = next(A for A in range(g0, N // 2, g0) if all(lo[n] >= m - 1 for n in range(A, 2 * A, g0)))
        b = 0 if g == 2 * g0 else next(b for b in range(g0, N, 2 * g0) if sym[s][b] >= m)
        for n in range(A, 2 * A, g0):                    # the tail's p: a sequence of order m - 1 at each length
            a = built(None, n)
            assert len(a) == n and holds(a, m - 1), (m, n)
        uncovered, needed = [], []
        for n in range(g, N + 1, g):
            if sym[s][n] >= m:
                a = built(s, n)
                assert len(a) == n and symmetry(a) == s and holds(a, m), (m, n)
                continue
            hi = cols[n - 1][1] if n <= mstar.NMAX else own_hi(n)
            if hi >= m: uncovered.append(n)
            elif own_hi(n) >= m: needed.append((n, hi))
        assert all(n < 2 * A + b for n in uncovered), (m, A, b, uncovered)
        out[m] = (A, b, 2 * A + b, uncovered, needed)
    return out


# ---------------------------------------------------------------------------------------------------------------------
# The quals

def q_symmetry_codes_have_their_orders_and_symmetries():
    # Replicata: each code of the account, <code id="N:M">, decoded here on its own (decode) and by overnight_v5.from_hex,
    # as mstar.py reads the page's codes; and every id of the page.
    # Expectata (the review of 2026-11-03, scratch/symreview-20261103: "A write-up must publish" the sequences the claim
    # needs): the two decodings agree; each code has N/8 hex digits and its sequence N signs; its exact order is M by power
    # sums and by synthetic division (equal sums below M, unequal at M); it is (-1)^M-symmetric; N is a multiple of
    # period(M) (BGPW's Divisibility); and its id is no other element's.
    # Resultata before: the page had no section of class symmetry.
    page_ids = re.findall(r'\sid="([^"]+)"', qi.blank_comments(qi.page()))
    for n, m, h in codes(account()):
        a = decode(h, m)
        assert a == mstar.v5.from_hex(h, m) and 8 * len(h) == n == len(a), (n, m)
        assert order(a) == m == order_div(a), (n, m, order(a), order_div(a))
        assert symmetry(a) == (-1) ** m and n % period(m) == 0, (n, m)
        assert page_ids.count(f'{n}:{m}') == 1, (n, m)


def q_symmetry_bounds_agree_with_the_chart():
    # Replicata: the account's math: each upper bound m^*(N) \le K and each range m^*(N) \in \{a, b\} it states; its
    # codes; and mstar.js's column [lo, hi] at each N up to 1024, or past it mstar's own two and norm lemmas.
    # Expectata (the task of 2026-11-06: "the account's stated bounds agree with mstar.js where they overlap"): each
    # stated bound is the chart's hi at N, each range [lo, hi]; and each code's order M is at most lo at its length (no
    # code shows the chart a stale lower bound) and, past 1024, at most two and norm there.
    # Resultata before: the page had no section of class symmetry.
    sec, cols = account(), qi.mstar_data()['cols']
    said = math(sec)
    les = [(int(n), int(k)) for f in said for n, k in re.findall(r'm\^\*\((\d+)\) \\le (\d+)', f)]
    ins = [(int(n), int(a), int(b)) for f in said for n, a, b in re.findall(r'm\^\*\((\d+)\) \\in \\\{(\d+), (\d+)\\\}', f)]
    assert les and all(n <= mstar.NMAX for n, _ in les), les
    assert [(n, k) for n, k in les if cols[n - 1][1] != k] == [], [(n, k, cols[n - 1][:2]) for n, k in les]
    assert [(n, a, b) for n, a, b in ins if cols[n - 1][:2] != [a, b]] == [], ins
    within = [(n, m) for n, m, _ in codes(sec)
              if not (m <= cols[n - 1][0] if n <= mstar.NMAX else m <= own_hi(n))]
    assert within == [], within


# The account's paragraphs on order 12 (Fable 5.2, 2026-11-11): its A, b and threshold, and the lengths it leaves.
ORDER_12 = (800, 624, 2224)
# 528 alone since the second round of 2026-11-11, when the tilted search found a symmetric sequence of order 12 at each
# of the 17 lengths prime to 3 that the first round had left (1504 to 2192); the 18 of them until then
LEFT_12 = [528]
# What the reduction leaves without the account's codes of order 12 or more, the 22 exceptions of its paragraph: 528,
# the four lengths where joins give order 12 (OWED_12 until the second round) and the 17 prime to 3
BARE_12 = [528, 1104, 1200, 1296, 1392, 1504, 1520, 1552, 1568, 1616, 1648, 1712, 1744, 1808, 1840, 1904, 1936, 2000,
           2032, 2096, 2128, 2192]


def q_order_12_is_down_to_528():
    # Replicata: the reduction carried on to order 12, with the closure's lengths to 2400 (reduction(MAXM=12, N=2400)),
    # on the page, and on the page without the account's codes of order 12 or more; and the account's paragraph
    # that states A = 800.
    # Expectata (Fable 5.2 with Codex, 2026-11-10 and 11; dreeves: "can you keep collaborating with codex (aka astra)
    # to see if you can make progress on the symmetry conjecture?"): at order 12 the reduction's A, b and threshold are
    # 800, 624 and 2224 (ORDER_12); the multiples of 16 it leaves uncovered, each with neither a symmetric sequence of
    # order 12, built and checked, nor a chart bound below 12, are LEFT_12, which is 528 alone; without the account's
    # codes of order 12 or more it leaves the 22 of BARE_12, so those codes are what covers the other 21; and the
    # paragraph lists all 22.
    # Spec change (Fable 5.2, 2026-11-11, the day's second round; q_order_12_is_down_to_18_lengths until then): LEFT_12
    # was 528 and the 17 lengths prime to 3 from 1504 to 2192, and the control dropped only the four codes of order 12
    # at 1104, 1200, 1296 and 1392, each of them then needed.  Since the round, the symmetric sequences of order 14 at
    # 1200 and 1296 cover those two lengths as well, so the control drops every code of order 12 or more.
    # Resultata before: the account said that orders 12 and up are open, and no symmetric sequence of order 12 was
    # known at those 21 lengths.
    text = qi.page()
    got = reduction(text, MAXM=12, N=2400)[12]
    assert got[:3] == ORDER_12 and got[3] == LEFT_12, got[:4]
    high = r'<code id="\d+:1[2-9]">[0-9A-F]+</code>'
    assert len(re.findall(high, account(text))) >= len(BARE_12) - len(LEFT_12)
    bare = SECTION.sub(lambda x: re.sub(high, '', x.group(0)), text)
    assert reduction(bare, MAXM=12, N=2400)[12][3] == BARE_12, reduction(bare, MAXM=12, N=2400)[12][3]
    [para] = [p for p in re.findall(r'<p>(.*?)</p>', account(text), re.S) if 'A = 800' in p]
    listed = [int(x) for x in re.findall(r'(?<![\d(^{=.-])\b(\d{3,4})\b(?![\d}-])', re.sub(r'\\\(.*?\\\)', '', para))]
    assert set(BARE_12) <= set(listed), listed


# The account's table of lengths from which each order holds (Opus 5.5, 2026-11-12), found by its caption's id: the
# tails of orders 13 and 14 behind its values, A, b and threshold, order 14's A from order 13's tail (every
# multiple of 16 from 3696 on, and 3680, the wrap of 1840:12, has an antisymmetric sequence of order 13); and the
# closure's lengths for it, one multiple of 16 past order 14's threshold.  Spec change (Opus 5.5, 2026-11-19;
# quals_index's q_sequences_past_the_chart_move_seven_firsts): with the antisymmetric sequences of order 13 at 2528, 2608,
# 2800 and 2992 in the account since then, order 14's tail has A = 3360 and threshold 7920, and FROM is 7936 (3680, 8560
# and 8576 until then); order 13's tail is as it was.
ORDER_13 = (1488, 720, 3696)
ORDER_14 = (3360, 1200, 7920)
FROM = 7936


def from_table(sec):
    """Each row's first and last orders, its numerical N, and the text of its E_m cell.
    The all-larger-orders row has no last order and no numerical N."""
    cell = lambda t: re.fullmatch(r'\\\(2\^\{(\d+)\}\\\)|([\d,]+)', t)
    value = lambda c: c and (2 ** int(c[1]) if c[1] else int(c[2].replace(',', '')))
    return [(int(re.findall(r'\d+', a)[0]), None if '\\ge' in a else int(re.findall(r'\d+', a)[-1]),
             value(cell(n)), e) for a, n, e in table_cells(sec)]


def q_symmetry_table_distinguishes_least_exact_cutoffs_from_bounds():
    # Replicata: read Table 6's E_m column as a reader choosing a particular order.
    # Expectata: exact values, intervals and upper bounds agree with their respective proofs.
    # Resultata before: the table gave only N, even when the two cutoffs differ.
    import exactcutoffs
    section = account()
    rows = from_table(section)
    finite = {m: e for a, z, _, e in rows[:-1] for m in range(a, z+1)}
    assert set(finite) == set(range(1, 32)), finite
    # Compare the statement's mathematics independently of links, digit grouping or TeX spacing.
    def normalize(e):
        e = re.sub(r'\s|\\[()]|\\left|\\right', '', e).replace(r'\,', '').replace('{,}', ',')
        return re.sub(r'\\leq?(?![a-z])', '≤', e)
    for m, E in exactcutoffs.cutoffs().items():
        assert normalize(finite[m]).replace(',', '') == str(E), (m, finite[m], E)
    for m in (13, 14):
        lo, hi = exactcutoffs.bounds(m)
        shown = normalize(finite[m]).replace(f'{lo:,}', str(lo)).replace(f'{hi:,}', str(hi))
        assert shown in (f'[{lo},{hi}]', rf'{lo}≤E_{{{m}}}≤{hi}', f'{lo}≤E_m≤{hi}'), (m, shown, lo, hi)
    # q_symmetry_table_gives_each_order_s_length independently computes the N to
    # which each upper bound refers, including those from the shorter centers.
    for first, last, n, cell in rows:
        if first >= 15:
            claim = normalize(cell).removeprefix('E_m')
            assert claim == '≤N' or (n is not None and claim.replace(',', '') == f'≤{n}'), (first, last, cell)
    a, z, _, _ = rows[-1]
    assert (a, z) == (32, None)
    bound = normalize(table_cells(section)[-1][1])
    assert bound in (r'\lfloor(27/10)^m\rfloor', r'\lfloor2.7^m\rfloor'), bound


def q_symmetry_table_gives_each_order_s_length():
    # Replicata: the account's table of lengths (its caption's id symmetry-table): each row's orders and its N; the
    # reduction carried to order 14 with the closure's lengths to FROM (reduction(MAXM=14, N=FROM)), on the page; the
    # proof's threshold_log2 (symlarge); and, as a control, the reduction to order 12 (lengths to 2240) on the page
    # without the account's code 2192:12.
    # Expectata (Opus 5.5, 2026-11-12; dreeves: "can you add a table to the end of the Symmetry Conjecture that gives,
    # for each order m, the N such that the symmetry conjecture holds for lengths n>=N"): the rows take the orders 1 to
    # 19 in turn, then every order from 20 on; for each order m up to 14, N is one more than the largest multiple of
    # period(m) that the reduction leaves uncovered, or 1 if it leaves none: 1 through order 11, 529 at 12 (528 left),
    # 3537 at 13 (3665 until 2026-11-21: with the antisymmetric sequences of order 13 at 2480 and 2512 in the account the largest length left is 3536, not
    # 3664; spec change, Fable 5.2, dreeves: "you can change thm 1 etc if you're only changing numbers") and 7121 at 14 (7329 until 2026-11-19, ORDER_14's
    # spec change), the tails of 13 and 14 being ORDER_13 and
    # ORDER_14; for each order from 15 to 19, which
    # have no tail from the page's sequences, N is 2^(threshold_log2(m) + 1), from which Fable 5.2's theorem gives a
    # (-1)^m-symmetric sequence of order m at every length that Divisibility allows.  Without 2192:12, 2192 is left at
    # order 12 too, and order 12's N would be 2193: the table's numbers follow the page's sequences.
    # Resultata before: the account had no such table; it stated where orders 9 to 12 hold from, and for orders 13 and
    # up only that they hold at every sufficiently large length.
    text = qi.page()
    got = reduction(text, MAXM=14, N=FROM)
    # (Fable 5.2, 2026-11-21, with quals_index's q_powers_of_2_on_the_page_follow_pow2_js: the tails are whatever the reduction gives, each threshold within the closure's lengths; until then held to
    # ORDER_13 and ORDER_14 above, which each new sequence of order 13 past the chart may change: order 14's was (3360, 1200, 7920) and is (3104, 1200, 7408)
    # since the one at 2432.)
    assert got[13][2] < FROM and got[14][2] < FROM, (got[13][:3], got[14][:3])
    # Spec change (2026-11-26, dreeves: "get all that on the page"): the constructive
    # proof replaces the Fourier threshold. Recompute the finite thresholds from the sequences.
    import sym15
    import symtransport
    want = [(m, max(got[m][3], default=0) + 1) for m in range(1, 15)]
    # 2026-11-26, dreeves: "Update the page and quals in batches, preserving earlier witnesses".
    want.append((15, order15_cutoff(sym15.RECIPES)))
    # Each length here has a checked source of order >=15, so any sum of them does too.
    src = sources(text)
    available = {len(a) for _, a, m in src if m >= 15}
    available |= {2*len(a) for _, a, m in src if m >= 14}
    seeds = {960, 1440, 1632, 1728, 2016, 2784, 7360, 7712, 8192}
    assert seeds <= available, seeds - available
    sums = {0}
    for n in range(32, 10048 + 960, 32):
        if any(n-k in sums for k in seeds):
            sums.add(n)
    assert 960 in seeds and all(n in sums for n in range(10048, 10048 + 960, 32))
    bound = 2*(10048 - 32) + 1440 + 1
    # 2026-11-30: the new explicit centers give smaller cutoffs. Keep checking
    # every numerical consequence of the original transport proof, now dated
    # in its exposition, and compare the current rows with the stronger proof.
    import symcenters
    current = symcenters.cutoffs()
    # 2026-12-02 (Opus 5.5): order 16's row is the center calculation's, at most the transport proof's bound (until
    # then equal to it, 21473; the symmetric sequences of order 16 at 3168, 3552, 3744 and 3936 of that day are four
    # more centers, symcenters.PAGE_CENTERS, and give 20033).
    assert current[16] <= bound
    want.append((16, current[16]))
    history = account(text).split('id="symtransport"', 1)[1].split('</blockquote>', 1)[0]
    for m in range(17, 20):
        bound = symtransport.seed_length(m) + 64*((bound - 1)//32) + 1
        assert str(bound) in history.replace('<wbr>', '').replace(',', '') and current[m] < bound
        want.append((m, current[m]))
    # Spec change (Fable 5.2, 2026-11-21; dreeves, of replacing the pins of each day's firsts by quals that hold the page to the generated data: "that's fine, i think, if it's as much of a no-brainer question as it sounds like"; quals_index's q_powers_of_2_on_the_page_follow_pow2_js): the table is held to the reduction, below,
    # whatever it gives (until then also to the literals [529, 3537, 7121] for orders 12 to 14, which each new sequence past the chart may lower).
    rows = from_table(account(text))
    # 2026-11-30: dreeves approved comparing N and E_m in Table 6. The individual
    # rows now run through 31. Keep the earlier asymptotic result in its proof.
    from centerlifts import shorter
    from symthreshold_finite import center_cutoffs
    lengths = {m: n for m, (n, _) in shorter().items()}
    want.extend(center_cutoffs(lengths).items())
    said = [(m, n) for a, z, n, _ in rows[:-1] for m in range(a, z + 1)]
    assert said == want, ([(m, n, w) for (m, n), (_, w) in zip(said, want) if n != w], said[:3], len(said))
    assert rows[-1][:2] == (32, None) and r'2^{O(m)}' in history, rows[-1]
    bare = re.sub(r'<code id="2192:12">[0-9A-F]+</code>', '', text)
    assert bare != text and reduction(bare, MAXM=12, N=2240)[12][3] == [528, 2192]


@lru_cache(maxsize=2)
def order14_sides(text=None):
    """An order-14-or-higher side for every multiple-of-16 residue modulo 960, from the codes of index.html (or of
    text, another version of it)."""
    limit = 7072   # The previous proof already gives sides of every residue by this length.
    lo, _, build = closure(sources(text, N=limit), N=limit)
    assert holds(build(None, 960), 14)
    sides = [0]   # The center alone
    for r in range(16, 960, 16):
        n = next(n for n in range(r, limit + 1, 960) if lo[n] >= 14)
        assert holds(build(None, n), 14)
        sides.append(n)
    return sides


def order15_cutoff(centers, text=None):
    """One more than the last length missed by these centers and the 60 verified sides (of index.html, or of text)."""
    odd = tuple(centers)
    assert odd and all(n % 32 == 16 for n in odd)
    wraps = [n + 2*s for n in (960, *odd) for s in order14_sides(text)]
    first = [min(n for n in wraps if n % 1920 == r) for r in range(0, 1920, 16)]
    return max(first) - 1920 + 1


def q_order_15_centers_can_cover_each_others_gaps():
    # Replicata: use one center from the earlier batch, then the two nearby centers
    # found in the next run. Expectata: each may cover a gap left by the other.
    # Resultata before: a calculation using the shortest center alone overshot the cutoff by 672.
    # 2026-12-01 (Opus 5.5; dreeves, of the pins that each batch moves: "if there's a best-of-all-worlds way to make
    # these less wolf-crying, that would be good"): the four cutoffs pinned on the codes of the morning before the
    # bandit's first batch (quals_index's BEFORE_BATCH), where 531984 covers a gap that 535440 and 531920 leave; on
    # today's codes, which only add sides, each no larger than then and none larger than any of its centers' alone
    # (807025, 547185, 543665 and 543473 that day; the pins until then).  Neither pair covers a gap of the other on
    # either day: 535440 alone gives the pair's cutoff with 795280, and 531920 alone the pair's with 535440.
    then = qi.at_commit(qi.BEFORE_BATCH, 'index.html')
    sets = ([795280], [795280, 535440], [535440, 531920], [535440, 531984, 531920])
    assert [order15_cutoff(c, then) for c in sets] == [807505, 547665, 544145, 543473]
    for c in sets:
        now = order15_cutoff(c)
        assert now <= order15_cutoff(c, then) and all(now <= order15_cutoff([n]) for n in c), (c, now)


def q_order_15_cutoff_links_the_shortest_checked_center():
    # Replicata: read the current center and the order-15 bound from the proof fold.
    # Expectata: the length links to its appendix entry and its actual downloadable signs.
    # Resultata before: the only center and download on the page were for 795280.
    import sym15
    text = account()
    n = min(sym15.RECIPES)
    line, = re.findall(r'<p id="sym15-current">(.*?)</p>', text, re.S)
    assert f'href="#entry-{n}">{n:,}</a>' in line
    assert f'href="pagedata/{n}.hex" download' in line
    cutoff, = [z for m, _, z, _ in from_table(text) if m == 15]
    # Spec change (Fable 5.2, 2026-11-27; dreeves, of this line against quals_index.q_tables_and_figures_are_numbered,
    # his rule that a link to a table ends with the table's number, which it had kept red since 2026-11-26: "sounds like
    # astra was confused about wolf-crying quals?").  The paragraph states the cutoff of the table's row of order 15,
    # as a number of its own, and links to the table.  Until then the cutoff had to be that link's words,
    # `href="#symmetry-table">{cutoff:,}</a>`: markup, where a reader is misled by a stale cutoff or a missing link.
    assert re.search(rf'(?<![\d,]){cutoff:,}(?!,?\d)', re.sub(r'<[^>]+>', '', line)), (cutoff, line)
    assert 'href="#symmetry-table"' in line, line


def q_every_known_order_has_its_symmetric_sequence():
    # Replicata: the chart's columns (mstar.js, every n it has) and the closure of the page's sequences up to the
    # chart's last length; at each n, the chart's lower bound there and the order below it, those of them that are
    # at least 1 (the conjecture has no order 0), each order m with its sign s = (-1)^m.
    # Expectata (Fable 5.2, 2026-11-11, the paragraph of the codes 960:14 and 1440:15, after Codex's review of that
    # morning: "Preserve both parities when maintaining symmetry coverage"): the closure has an s-symmetric sequence of
    # order m at n, which, built, has length n, symmetry s and order m.  An s-symmetric sequence of order m has every
    # lower order too, so this is the conjecture at n at every order up to the best known there: where the chart
    # knows a sequence of an order, a symmetric one of that order is on the page or built from the page.
    # Resultata before: 960 at order 14 and 1440 at order 15 had none; the sequences of order 15 at 960 and of order
    # 16 at 1440 have the other symmetry.
    cols = qi.mstar_data()['cols']
    lo, sym, built = closure(sources(None, len(cols)), len(cols))
    missing = []
    for n, col in enumerate(cols, 1):
        for m in range(max(1, col[0] - 1), col[0] + 1):
            s = (-1) ** m
            if sym[s][n] < m: missing.append((n, m)); continue
            a = built(s, n)
            assert len(a) == n and symmetry(a) == s and holds(a, m), (n, m)
    assert not missing, missing


def q_closure_gives_the_chart_no_new_lower_bound():
    # 2026-11-11 (Fable 5.2; dreeves, of the qual above: "yes, good qual. can you add more like that?").
    # Replicata: the chart's columns (mstar.js) and the closure of the page's sequences up to the chart's last length: joins, and wraps with the parity bump of
    # a symmetric sequence's order, which the chart's generator does not use.
    # Expectata: at no length does the closure reach a higher order than the chart's lower bound: what this section builds, the chart already shows.  It does
    # report a chart that lags: with column 2016's lower bound taken as 13, the closure's 15 there is above it.
    # Resultata before: no qual compared the two; a wrap that beat the chart would have gone unseen.
    cols = qi.mstar_data()['cols']
    lo, sym, built = closure(sources(None, len(cols)), len(cols))
    above = lambda los: [(n, lo[n], los[n - 1]) for n in range(1, len(cols) + 1) if lo[n] > los[n - 1]]
    assert above([c[0] for c in cols]) == []
    assert above([13 if n == 2016 else c[0] for n, c in enumerate(cols, 1)]) == [(2016, 15, 13)]


def q_symmetry_conjecture_holds_through_order_11():
    # Replicata: the reduction (reduction()) from the page's own sequences, Thue-Morse and BGPW's Tables 4 and 5 (the
    # closure, by joins and wraps, every claim built and checked), and the chart's upper bounds; and the account's math:
    # its A = ..., b = ..., n \ge ... and its stated upper bounds.
    # Expectata (the claim of 2026-11-03, scratch/sym2-20261103, confirmed by the review, scratch/symreview-20261103):
    # for every m <= 11, every multiple of period(m) below the threshold 2A + b has a (-1)^m-symmetric sequence of order m
    # or a chart bound below m, but for 288 at m = 10, and none past it lacks one; the account's A, b and thresholds are
    # those of orders 9 to 11; and the upper bounds it states are exactly the ones the reduction takes beyond the chart's
    # own tests (own_hi), at their chart values.
    # Resultata before: the page had no section of class symmetry; the reduction alone left 560 and 592 at order 9, 432
    # and 848 at order 10, and 816, 1040, 1072, 1136, 1168, 1232 and 1264 at order 11 (q_symmetry_red_cases).
    # Spec change (Claude, 2026-11-08; until then this qual was q_symmetry_conjecture_holds_through_order_11_but_at_288_10):
    # no case uncovered, not even 288 at m = 10, since the chart's upper bound there is 9 by the form-tree proof
    # (quals_mstar's q_288_is_settled_at_9), a bound the account now states.
    sec = account()
    got = reduction()
    open_ = [(n, m) for m, (*_, uncovered, _) in got.items() for n in uncovered]
    assert open_ == [], open_
    said = math(sec)
    tails = [got[m] for m in range(9, MAXM + 1)]
    # Since 2026-11-11 the account also states order 12's A, b and threshold (ORDER_12, Fable 5.2's paragraph), which
    # q_order_12_is_down_to_528 holds to the reduction carried one order on.
    tails.append(ORDER_12)
    # (2026-11-12: for an hour that evening Opus 5.5's paragraph before the table of lengths stated order 13's and order
    # 14's too, and this qual held them; dreeves replaced it with a sentence of his own as he committed the table, so the
    # account states none past order 12.  q_symmetry_table_gives_each_order_s_length holds the table to those tails.)
    assert sorted({int(x) for f in said for x in re.findall(r'^A = (\d+)$', f.strip())}) == sorted({A for A, *_ in tails}), said
    assert sorted({int(x) for f in said for x in re.findall(r'^b = (\d+)$', f.strip())}) == sorted({b for _, b, *_ in tails}), said
    assert sorted({int(x) for f in said for x in re.findall(r'^n \\ge (\d+)$', f.strip())}) == sorted({t for _, _, t, *_ in tails}), said
    stated = {(int(n), int(k)) for f in said for n, k in re.findall(r'm\^\*\((\d+)\) \\le (\d+)', f)}
    needed = {bound for *_, needed in got.values() for bound in needed}
    assert stated == needed, (sorted(stated - needed), sorted(needed - stated))


def q_symmetry_red_cases():
    # Replicata: the reduction of q_symmetry_conjecture_holds_through_order_11 on the page with the account's codes left
    # out (each <code id> of the section dropped from the page's text): its positive control.
    # Expectata: the reduction finds the cases those codes fill and only those: each code's (n, m) uncovered without
    # it, so that no code of the account is idle, and the reduction can fail.
    # Spec change (Claude, 2026-11-08): no longer besides 288 at m = 10, which the chart's upper bound 9 covers since
    # the form-tree proof.
    text = qi.page()
    sec = account(text)
    # Since 2026-11-11 the section also lists sequences of orders 12 to 14 (Fable 5.2's paragraph on order 12 and the
    # lower bounds found with it), which the reduction through order 11 does not need: its codes are those of order
    # at most 11.
    keys = [(n, m) for n, m, _ in codes(sec) if m <= 11]
    bare = SECTION.sub(lambda x: CODE.sub('', x.group(0)), text)
    got = reduction(bare)
    open_ = sorted((n, m) for m, (*_, uncovered, _) in got.items() for n in uncovered)
    assert open_ == sorted(keys), (open_, keys)


# In the page, as it loads: for each code of the account, [its id, its computed hyphens, and its paragraph's computed
# text-align and hyphens], written into the DOM that --dump-dom returns
LOOK = """<script>
addEventListener('load', () => setTimeout(() => {
  const style = e => getComputedStyle(e);
  const out = [...document.querySelectorAll('section.symmetry code[id]')].map(c =>
    [c.id, style(c).hyphens, style(c.closest('p')).textAlign, style(c.closest('p')).hyphens]);
  document.body.append(Object.assign(document.createElement('pre'), {id: 'symmetry-out', textContent: JSON.stringify(out)}));
}, 500));
</script>
"""


def q_symmetry_code_lines_are_ragged_and_unhyphenated():
    # Replicata: index.html with page/, mstar.js and pow2.js beside it, in headless Chrome 1280 px wide (justified
    # prose, from 40rem up), as it loads: each code of the account, its computed hyphens, and its paragraph's computed
    # text-align and hyphens.
    # Expectata: as the code blocks' lines are (p.code: "the code lines ... stay ragged", page/style.css), each code's
    # line is ragged (text-align start) and nothing in it is hyphenated (hyphens manual), so that no hyphen is drawn
    # inside a code where it wraps, and its label is not spread across the line when the code wraps to the next.
    # Resultata before (2026-11-06, the account's screenshots): text-align justify and hyphens auto, and the drawn codes
    # had hyphens inside them ("…C92C92D-" at 1040), their labels spread across the line ("n = 1040,   m = 11:").
    d = tempfile.mkdtemp(prefix='symmetry-quals-')
    try:
        open(os.path.join(d, 'index.html'), 'w', encoding='utf-8').write(qi.page().replace('</body>', LOOK + '</body>'))
        qi.copy_page_dir(d)
        for src, name in ((qi.DATA, 'mstar.js'), (qi.POW2, 'pow2.js')): shutil.copy(src, qi.data_file(d, name))
        dom = qi.chrome(d, ['--window-size=1280,1000', '--virtual-time-budget=10000', '--dump-dom',
                            'file://' + os.path.join(d, 'index.html')])
    finally:
        shutil.rmtree(d)
    m = re.search(r'<pre id="symmetry-out">(.*?)</pre>', dom, re.S)
    assert m, dom[-2000:]
    looks = json.loads(html.unescape(m.group(1)))
    assert [i for i, *_ in looks] == [f'{n}:{k}' for n, k, _ in codes(account())], looks
    bad = [look for look in looks if look[1:] != ['manual', 'start', 'manual']]
    assert bad == [], bad


CELLS = re.compile(r'href="https://github\.com/dreeves/equipow/tree/main/searches/formtree/cells/(\d+)_(\d+)"')


def q_symmetry_cited_bounds_have_form_tree_proofs():
    # Replicata: the account's links to form-tree proofs of single cells (searches/formtree/cells/N_M), each linked
    # directory's top and verdicts in the repository, proofs/formtree.py assemble run on them, and mstar.js's columns.
    # Expectata (dreeves, 2026-11-09, of the form-tree proofs of the bounds the account cites: "you're getting all this
    # recorded in index.html?"): the account links a proof for exactly the cells 96:8, 144:9, 176:8, 272:9 and 752:11
    # (m*(96) <= 7, m*(144) <= 8, m*(176) <= 7, m*(272) <= 8 and m*(752) <= 10, until then taken from earlier work, from
    # BGPW's integer linear programs and from a meet in the middle with no certificate); each assembles to "VERIFIED: no
    # sequence of length N has order M"; every task in it was read by the Python checker as well as by fkernel (each
    # verdict's py is true); and each proves the chart's upper bound at N, M - 1.
    # Resultata before: the account linked no such proof, and the proofs were in scratch/certify-20261109/ alone.
    # Spec change (dreeves, 2026-11-09: "sounds good, certify 168 and 184 next"): also 152:7, 168:7 and 184:7, by
    # tops whose chain starts with modulus 4 (the parity coset pins f(-1) = +-128); 168 and 184 were BGPW's last two
    # integer-linear-program bounds, and 152's, which Berend and Golan proved by hand, gets a second proof.
    # Resultata before that: the account linked the five cells above alone.
    sec, cols = account(), qi.mstar_data()['cols']
    cells = sorted((int(n), int(m)) for n, m in set(CELLS.findall(sec)))
    assert cells == [(96, 8), (144, 9), (152, 7), (168, 7), (176, 8), (184, 7), (272, 9), (752, 11)], cells
    for n, m in cells:
        d = os.path.join(HERE, 'searches', 'formtree', 'cells', f'{n}_{m}')
        top, verdicts = os.path.join(d, f'top_{n}_{m}.json.gz'), os.path.join(d, 'verdicts.jsonl')
        out = subprocess.run([sys.executable, '-B', '-I', '-S', os.path.join(HERE, 'proofs', 'formtree.py'), 'assemble', top, verdicts],
                             capture_output=True, text=True, timeout=600)
        assert f'VERIFIED: no sequence of length {n} has order {m}.' in out.stdout, (n, m, out.stdout[-300:], out.stderr[-300:])
        lines = [json.loads(line) for line in open(verdicts) if line.strip()]
        assert all(v['py'] is True for v in lines), (n, m, [v['start'] for v in lines if v['py'] is not True])
        assert cols[n - 1][1] == m - 1, (n, m, cols[n - 1])


# The theorem on every sufficiently large length (Fable 5.2, 2026-11-09): its proof, a fold of the account's block
# quote since 2026-11-10 (until then a page of its own, proofs/symlarge.html, which the quote linked), and the
# repository's files as the page links them.  quals_symlarge holds the fold itself.
FOLD = r'<details class="proof-exposition" id="symlarge">.*?</details>'
HUB = 'https://github.com/dreeves/equipow/blob/main/'


def large_lengths(sec):
    """The account's block quote that holds the proof of the theorem on every sufficiently large length, of which there
    is exactly one, without the proof's fold: the statement and what is said about it."""
    found = [q for q in re.findall(r'<blockquote class="claude">(.*?)</blockquote>', sec, re.S) if re.search(FOLD, q, re.S)]
    assert len(found) == 1, len(found)
    return re.sub(FOLD, '', found[0], flags=re.S)


def q_symmetry_large_lengths_holds_its_proof():
    # Replicata: the account's block quote that holds the proof's fold, and its links outside the fold.
    # Expectata (dreeves, 2026-11-09, of Fable 5.2's theorem that at each order m every sufficiently large length that
    # Divisibility allows has a (-1)^m-symmetric sequence of order m: "yes to putting it in index.html attributed
    # clearly to Fable 5.2"): one block quote of the account holds the proof, in its fold; and each of the quote's
    # links into github.com/dreeves/equipow names a file of the repository, of which there is at least one.
    # Spec change, 2026-11-10 (dreeves's README, [SYM]: "i think i'd rather have this symlarge proof in index.html,
    # but default collapsed"): until then this qual, q_symmetry_large_lengths_links_its_proof, asked that the quote link
    # the proof's page, proofs/symlarge.html, and that the page link back to the account's title.
    # Resultata before: the account had no such block quote, and the proof was in scratch/symlarge-20261109/ alone.
    quote = large_lengths(account())
    linked = re.findall('href="' + re.escape(HUB) + '([^"#]+)"', quote)
    assert linked and all(os.path.isfile(os.path.join(HERE, path)) for path in linked), linked


def q_symmetry_large_lengths_states_the_proof_s_numbers():
    # Replicata: that block quote's math outside the proof's fold: each bound n_0(M) \le 2^{E} it states, and each decimal number; and
    # proofs/symlarge.py: threshold_log2(M), the least L such that the conditions of the proof's section 5 hold at
    # every N >= 2^L, and count and gauss at order 5.
    # Expectata: each stated bound is the proof's, E = threshold_log2(M) + 1 (a length n = 2N with N >= 2^L); and each
    # decimal is one of three: 0.55 and 3.6, which bracket count / gauss at order 5 at every admissible N from 16 to 48
    # (n from 32 to 96) while 0.56 and 3.5 do not, and 0.39, cos(pi/16)^48 to two places.  It asks for none of them to
    # be there (a number dreeves cuts turns nothing red).
    # Resultata before: the account had no such block quote.
    said = math(large_lengths(account()))
    bounds = [(int(m), int(e)) for f in said for m, e in re.findall(r'n_0\((\d+)\) \\le 2\^\{(\d+)\}', f)]
    assert all(e == symlarge.threshold_log2(m) + 1 for m, e in bounds), bounds
    ratios = [symlarge.count(5, n) / symlarge.gauss(5, n) for n in range(16, 49, 4)]
    assert 0.55 <= min(ratios) < 0.56 and 3.5 < max(ratios) <= 3.6, ratios
    allowed = {'0.55', '3.6', f'{cos(pi / 16) ** 48:.2f}'}
    decimals = [x for f in said for x in re.findall(r'\d+\.\d+', f)]
    assert set(decimals) <= allowed, (decimals, allowed)


def main():
    names = sys.argv[1:] or [n for n in globals() if n.startswith('q_')]
    failed = 0
    for n in names:
        t0, c0 = time.time(), time.process_time()
        try:
            globals()[n]()
            # TODO: runner line; says the named qual passed, with its seconds and this process's CPU seconds
            print(f'Claude: "PASSED" {n} ({time.time() - t0:.1f} s, {time.process_time() - c0:.1f} CPU s)', flush=True)
        except Exception:
            failed += 1
            # TODO: runner line; says the named qual failed, with its seconds and this process's CPU seconds, followed by the traceback
            print(f'Claude: "FAILED" {n} ({time.time() - t0:.1f} s, {time.process_time() - c0:.1f} CPU s)\n' + traceback.format_exc(), flush=True)
    # TODO: runner summary; says how many quals passed out of how many
    print(f'Claude: "{len(names) - failed} of {len(names)} passed"')
    sys.exit(1 if failed else 0)


if __name__ == '__main__':
    main()
