GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Kuiper, cosmos and drawing engines / galaxies-wafers / iterations/41-golden-angle

Iterations — 42 Counts That Say Themselves and 1 more file

A JavaScript module in Galaxies Wafers that draws blocks, families and provenance, and uses data from GitHub API and the stars and elements repositories. Served with the page at https://ventusltd.github.io/galaxies-wafers/iterations/42-counts-that-say-themselves/.

691 lines of JavaScript from galaxies-wafers, in 2 files. Topic: Kuiper, cosmos and drawing engines. Section 1223 of 2044.

Cut 2026-12-06T03:07:51+00:00. A copy for reading; the repository at the stated commit is the source.

In the State lines below, domain stage, users and canonical status are rule-based first approximations from code and path evidence (rules explore-agent-rules-v1), not observed facts: the rules.

In this section

  1. Law transform · galaxies-wafers / iterations/41-golden-angle/law.mjs lines 1–237
  2. Iterations — 42 Counts That Say Themselves interaction · galaxies-wafers / iterations/42-counts-that-say-themselves/app.mjs lines 1–454

Law

A JavaScript module in the iterations folder of Galaxies Wafers that counts rings (`countRings`), places linear (`placeLinear`), counts most (`countAtMost`) and calculates planar distances. Served with the page at https://ventusltd.github.io/galaxies-wafers/iterations/41-golden-angle/.

transformgalaxies-wafers / iterations/41-golden-angle/law.mjs · lines 1–237 of 237 · pattern bands (shared by 3 files)

What it does
It calculates its inputs. Defines 18 functions or classes, including integers, placeWith, sameBits, digitsAgree, s.
Downstream impact
Used directly by 1 file: Iterations — 41 Golden Angle (galaxies-wafers, https://ventusltd.github.io/galaxies-wafers/iterations/41-golden-angle/). Reached from 1 other entry point, including Iterations — 41 Golden Angle Page (galaxies-wafers, https://ventusltd.github.io/galaxies-wafers/iterations/41-golden-angle/).
What it achieves
It counts rings (`countRings`), places linear (`placeLinear`) and counts most (`countAtMost`). The mathematics of The Placement Law, with no DOM and no fetch.
Why it matters
Imported by Iterations — 41 Golden Angle.
Application
galaxies-wafers · iterations/41-golden-angle · JavaScript module
Uses
Galaxies Wafers — Lib (import)
Used by
imported by Iterations — 41 Golden Angle (import)
Produces
TAU, PHI, integers, placeWith, sameBits, digitsAgree, goldenCheck, continuedFraction, signedFrac, nearest, arms, ringBounds
Defines here
integers, placeWith, sameBits, digitsAgree, s, goldenCheck, continuedFraction, signedFrac, nearest, cx, cy_cx, arms, ringBounds, countRings, placeLinear, countAtMost, gaps, bands
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
galaxies-wafers@5814793812add74605660fa821e6a39b8de80588 iterations/41-golden-angle/law.mjs:1-237 · blob 843106954d508a642f0925c291715ee15769ef04 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: transform · effects: none found · domain stage: unknown · users: none · canonical: unknown
/* law.mjs — the mathematics of The Placement Law, with no DOM and no fetch.
 *
 * Every constant here is read from ../../lib.mjs, the file every wafer page
 * imports. Nothing is retyped: the golden angle, the spacing and the law itself
 * come from lib.mjs, and the golden panel is drawn with lib.mjs placeAll()
 * itself. The only variant is placeWith(), the same law with the angle
 * replaced so the reader can see what another angle would do; it is checked
 * bit for bit against placeAll() when the angle is lib.mjs GOLDEN.
 */
import { GOLDEN, SPACING, place, placeAll } from '../../lib.mjs';

export { GOLDEN, SPACING, place, placeAll };

export const TAU = 2 * Math.PI;
export const PHI = (1 + Math.sqrt(5)) / 2;

/* 1, 2, 3 … n as a Uint32Array: the integers, issued or not. One buffer, grown
   only when a larger n is asked for, and handed out as a view. */
let ints = new Uint32Array(0);
export function integers(n) {
  if (n > ints.length) {
    const next = new Uint32Array(Math.max(n, ints.length * 2));
    for (let i = 0; i < next.length; i++) next[i] = i + 1;
    ints = next;
  }
  return ints.subarray(0, n);
}

/* The law with the angle swapped. r = SPACING·sqrt(key) exactly as lib.mjs. */
export function placeWith(keys, theta) {
  const n = keys.length, p = new Float32Array(n * 2);
  for (let i = 0; i < n; i++) {
    const k = keys[i], r = SPACING * Math.sqrt(k), t = k * theta;
    p[i * 2] = r * Math.cos(t);
    p[i * 2 + 1] = r * Math.sin(t);
  }
  return p;
}

export function sameBits(a, b) {
  if (a.length !== b.length) return false;
  for (let i = 0; i < a.length; i++) if (!Object.is(a[i], b[i])) return false;
  return true;
}

/* How many leading significant digits two numbers share when both are printed
   to 17 significant figures (the precision that round-trips a double). */
export function digitsAgree(a, b) {
  const s = x => x.toPrecision(17).replace('.', '').replace(/^-?0+/, '');
  const A = s(a), B = s(b);
  let d = 0;
  while (d < A.length && A[d] === B[d]) d++;
  return d;
}

export function goldenCheck() {
  const formula = TAU * (1 - 1 / PHI);
  const alt = Math.PI * (3 - Math.sqrt(5));
  return {
    lib: GOLDEN, formula, alt, phi: PHI,
    diff: GOLDEN - formula,
    digits: digitsAgree(GOLDEN, formula),
    degrees: GOLDEN * 180 / Math.PI,
    turn: GOLDEN / TAU
  };
}

/* Continued fraction of x, computed in double precision. Stops when the
   remainder is below tol (x was, to this precision, rational) or after max
   terms. Double precision loses roughly one digit every term or two, so terms
   beyond the first dozen or so are not to be trusted; the caller says so. */
export function continuedFraction(x, max = 14, tol = 1e-9) {
  const terms = [];
  let v = x;
  for (let i = 0; i < max; i++) {
    const a = Math.floor(v);
    terms.push(a);
    const f = v - a;
    if (f < tol) return { terms, exhausted: true };
    v = 1 / f;
    if (v > 1e9) return { terms, exhausted: true };
  }
  return { terms, exhausted: false };
}

/* Signed distance of x from the nearest integer, in (-0.5, 0.5]. */
export const signedFrac = x => x - Math.round(x);

/* ── nearest neighbours through a grid bucket, not all pairs ────────────────
   Points go into square cells of side `cell`. For each point its own cell and
   then successive square rings of cells are searched; the search stops once
   the best distance found is no more than ring·cell, because every point in a
   further ring is at least that far away. Returns the minimum nearest-
   neighbour distance over all points, the pair that attains it, the median,
   and for the outer half of the keys (key > n/2) the histogram of the key
   difference to the nearest neighbour. */
export function nearest(pos, keys, cell = SPACING) {
  const n = keys.length;
  const out = { n, min: Infinity, minA: 0, minB: 0, median: NaN, modal: 0, modalShare: 0, visits: 0, ms: 0 };
  if (n < 2) return out;
  const t0 = performance.now();
  let R = 0;
  for (let i = 0; i < n * 2; i++) R = Math.max(R, Math.abs(pos[i]));
  const G = Math.max(1, Math.ceil((2 * R) / cell) + 1);
  const cx = x => Math.min(G - 1, Math.max(0, Math.floor((x + R) / cell)));
  const cellOf = new Int32Array(n), count = new Int32Array(G * G + 1);
  for (let i = 0; i < n; i++) { const c = cy_cx(i); cellOf[i] = c; count[c + 1]++; }
  function cy_cx(i) { return cx(pos[i * 2 + 1]) * G + cx(pos[i * 2]); }
  for (let c = 0; c < G * G; c++) count[c + 1] += count[c];
  const fill = count.slice(0, G * G), order = new Int32Array(n);
  for (let i = 0; i < n; i++) order[fill[cellOf[i]]++] = i;

  const nn = new Float32Array(n), hist = new Map();
  const half = n / 2;
  let visits = 0, outer = 0;
  for (let i = 0; i < n; i++) {
    const x = pos[i * 2], y = pos[i * 2 + 1];
    const gx = cx(x), gy = cx(y);
    let best = Infinity, bj = -1;
    for (let ring = 0; ring < G; ring++) {
      const x0 = gx - ring, x1 = gx + ring, y0 = gy - ring, y1 = gy + ring;
      for (let yy = y0; yy <= y1; yy++) {
        if (yy < 0 || yy >= G) continue;
        const edgeRow = yy === y0 || yy === y1;
        for (let xx = x0; xx <= x1; xx += edgeRow ? 1 : (x1 - x0 || 1)) {
          if (xx >= 0 && xx < G) {
            const c = yy * G + xx;
            for (let s = count[c], e = count[c + 1]; s < e; s++) {
              const j = order[s];
              if (j === i) continue;
              const dx = pos[j * 2] - x, dy = pos[j * 2 + 1] - y, d = dx * dx + dy * dy;
              visits++;
              if (d < best) { best = d; bj = j; }
            }
          }
          if (x1 === x0) break;
        }
      }
      if (bj >= 0 && Math.sqrt(best) <= ring * cell) break;
    }
    const d = Math.sqrt(best);
    nn[i] = d;
    if (d < out.min) { out.min = d; out.minA = keys[i]; out.minB = keys[bj]; }
    if (keys[i] > half) {
      const dk = Math.abs(keys[bj] - keys[i]);
      hist.set(dk, (hist.get(dk) || 0) + 1);
      outer++;
    }
  }
  const sorted = nn.slice().sort();
  out.median = sorted[n >> 1];
  let mk = 0, mv = -1;
  for (const [k, v] of hist) if (v > mv || (v === mv && k < mk)) { mk = k; mv = v; }
  out.modal = mk; out.modalShare = outer ? mv / outer : 0;
  out.visits = visits;
  out.ms = performance.now() - t0;
  return out;
}

/* Arms from the modal key difference d: keys k, k+d, k+2d … lie along one arm.
   Each step turns the arm by the signed fractional part of d·(angle/2π) of a
   turn. Across the drawn disc an arm takes about n/d steps, so its total turn is
   (n/d)·that. The page calls the arms straight spokes when that total turn is
   less than half the angular gap between neighbouring arms (half of 1/d of a
   turn); otherwise they are spiral arms. The rule is stated on the page. */
export function arms(theta, n, d) {
  if (!d) return { d: 0, stepTurn: 0, totalDeg: 0, spokes: false };
  const stepTurn = signedFrac(d * (theta / TAU));
  const totalDeg = Math.abs((n / d) * stepTurn * 360);
  return { d, stepTurn, totalDeg, gapDeg: 360 / d, spokes: totalDeg < 360 / d / 2 };
}

/* ── equal-area rings ───────────────────────────────────────────────────────
   m rings between radius 0 and Rout, boundaries Rout·sqrt(i/m), so every ring
   has area π·Rout²/m. Points are assigned by their measured radius, hypot(x,y)
   of the placed point, against those boundaries: the counting is geometric,
   done on positions, not on the key. */
export function ringBounds(Rout, m) {
  const b = new Float64Array(m + 1);
  for (let i = 0; i <= m; i++) b[i] = Rout * Math.sqrt(i / m);
  return b;
}

export function countRings(pos, bounds, limit = Infinity) {
  const m = bounds.length - 1, counts = new Uint32Array(m);
  const Rout = bounds[m], tol = Rout * 1e-6;
  const n = Math.min(pos.length / 2, limit);
  let outside = 0;
  for (let i = 0; i < n; i++) {
    const r = Math.hypot(pos[i * 2], pos[i * 2 + 1]);
    if (r > Rout + tol) { outside++; continue; }
    let lo = 0, hi = m - 1;
    while (lo < hi) { const mid = (lo + hi) >> 1; if (r < bounds[mid + 1]) hi = mid; else lo = mid + 1; }
    counts[lo]++;
  }
  return { counts, outside };
}

/* Contrast: the same keys under r ∝ key instead of r ∝ sqrt(key), scaled so key
   n lands on the same outer radius. Returns a position array. */
export function placeLinear(keys, n) {
  const Rout = SPACING * Math.sqrt(n), p = new Float32Array(keys.length * 2);
  for (let i = 0; i < keys.length; i++) {
    const k = keys[i], r = Rout * (k / n), t = k * GOLDEN;
    p[i * 2] = r * Math.cos(t); p[i * 2 + 1] = r * Math.sin(t);
  }
  return p;
}

/* Number of issued keys ≤ n in a sorted key list (binary search). */
export function countAtMost(keys, n) {
  let lo = 0, hi = keys.length;
  while (lo < hi) { const m = (lo + hi) >> 1; if (keys[m] <= n) lo = m + 1; else hi = m; }
  return lo;
}

/* Gaps in a sorted key list starting from 1: numbers never issued. */
export function gaps(keys) {
  let absent = 0, longest = 0, longestFrom = 0, runs = 0, prev = 0;
  for (let i = 0; i < keys.length; i++) {
    const g = keys[i] - prev - 1;
    if (g > 0) { absent += g; runs++; if (g > longest) { longest = g; longestFrom = prev + 1; } }
    prev = keys[i];
  }
  return { issued: keys.length, max: prev, absent, runs, longest, longestFrom };
}

export function bands(keys, max, m) {
  const issued = new Uint32Array(m), size = new Uint32Array(m);
  const w = max / m;
  for (let b = 0; b < m; b++) {
    const lo = Math.floor(b * w) + 1, hi = b === m - 1 ? max : Math.floor((b + 1) * w);
    size[b] = hi - lo + 1;
    issued[b] = countAtMost(keys, hi) - countAtMost(keys, lo - 1);
  }
  return { issued, size };
}

Iterations — 42 Counts That Say Themselves

A JavaScript module in Galaxies Wafers that draws blocks, families and provenance, and uses data from GitHub API and the stars and elements repositories. Served with the page at https://ventusltd.github.io/galaxies-wafers/iterations/42-counts-that-say-themselves/.

interactiongalaxies-wafers / iterations/42-counts-that-say-themselves/app.mjs · lines 1–454 of 454 · Entry point · pattern fillRow (shared by 2 files)

What it does
It draws blocks, families, provenance and elements. Defines 27 functions or classes, including rel, $, el, link, blob. Side effects: changes the web page (DOM); makes network requests.
Why it exists in the grid workflow
README: "The page (`index.html`, `app.mjs`, `lib.mjs`) is inherited unchanged from testcode/202609151339."
Inputs & outputs
In: data source: stars data; data source: elements data; data source: GitHub API; JSON file blocks.json; JSON file families.json; JSON file index.json; JSON file provenance.json; JSON file apps.json.
Downstream impact
Used directly by 1 file: Iterations — 42 Counts That Say Themselves Page (galaxies-wafers, https://ventusltd.github.io/galaxies-wafers/iterations/42-counts-that-say-themselves/). It is itself an entry point, published at https://ventusltd.github.io/galaxies-wafers/iterations/42-counts-that-say-themselves/.
What it achieves
It fetches queue (`FetchQueue`), fetches once (`fetchOnce`) and counts lines md (`countLinesMd`). Loading in Grid Atlas grammar: every section exists from the start, nothing is fetched until the reader opens it, one queue of 4, a 15 s timeout on every fetch, one promise per URL. It responds to toggle events.
Why it matters
Loaded by the page of the same name at https://ventusltd.github.io/galaxies-wafers/iterations/42-counts-that-say-themselves/.
Application
galaxies-wafers · iterations/42-counts-that-say-themselves · JavaScript module
Uses
Audit (import), Iterations — 42 Counts That Say Themselves Page (new URL import.meta.url), Stars — Index Page (estate URL), GlobalGrid2050 — Testcode Page (estate URL)
Used by
loaded by Iterations — 42 Counts That Say Themselves Page (script src)
Produces
rel, $, el, link, blob, cite, FetchQueue, withTimeout, fetchOnce, tryGet, countLinesMd, addRow
Defines here
rel, $, el, link, blob, cite, FetchQueue, withTimeout, fetchOnce, tryGet, countLinesMd, addRow, fillRow, failRow, paintTally, paintMachine, label, hydrate, starsData, nounRule, scanTyped, computedRow, famLine, lineOf, row, arr, ruleText
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
galaxies-wafers@5814793812add74605660fa821e6a39b8de80588 iterations/42-counts-that-say-themselves/app.mjs:1-454 · blob 3cb11dadf730e10109ea0f621bfbcb4a8d536147 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: dom, network · domain stage: unknown · users: none · canonical: unknown
/* Counts That Say Themselves — the page. Loading in Grid Atlas grammar: every
   section exists from the start, nothing is fetched until the reader opens it,
   one queue of 4, a 15 s timeout on every fetch, one promise per URL, and only
   the changed row or label is touched. The rules live in audit.mjs. */
import * as A from './audit.mjs';

const STARS = 'https://ventusltd.github.io/stars/';
const GG = 'https://globalgrid2050.com/testcode/';
const ELEMENTS = 'https://raw.githubusercontent.com/Ventusltd/elements/main/catalogue/';
const API = 'https://api.github.com/repos/Ventusltd/';
const HERE = new URL('./', import.meta.url);
const SITE = new URL('../../', HERE);            // galaxies-wafers root as served
const rel = p => new URL(p, SITE).href;
const TIMEOUT = 15000, CONCURRENCY = 4;

const $ = id => document.getElementById(id);
const el = (tag, cls, text) => { const e = document.createElement(tag); if (cls) e.className = cls; if (text != null) e.textContent = text; return e; };
const link = (href, text) => { const a = el('a', null, text); a.href = href; a.rel = 'noopener'; return a; };

/* where a served file lives in its repository, for a file-and-line citation */
function blob(url, line) {
  const L = line ? `#L${line}` : '';
  if (url.startsWith(STARS)) return { label: `stars/${url.slice(STARS.length)}`, href: `https://github.com/Ventusltd/stars/blob/main/${url.slice(STARS.length)}${L}` };
  if (url.startsWith(GG)) return { label: `globalgrid2050/testcode/${url.slice(GG.length)}`, href: `https://github.com/Ventusltd/globalgrid2050/blob/main/testcode/${url.slice(GG.length)}${L}` };
  if (url.startsWith(ELEMENTS)) return { label: `elements/catalogue/${url.slice(ELEMENTS.length)}`, href: `https://github.com/Ventusltd/elements/blob/main/catalogue/${url.slice(ELEMENTS.length)}${L}` };
  if (url.startsWith(SITE.href)) return { label: `galaxies-wafers/${url.slice(SITE.href.length)}`, href: `https://github.com/Ventusltd/galaxies-wafers/blob/main/${url.slice(SITE.href.length)}${L}` };
  return { label: url, href: url };
}
const cite = (url, line) => { const b = blob(url, line); return { label: line ? `${b.label}:${line}` : b.label, href: b.href }; };

/* ── loading, as Grid Atlas does it ─────────────────────────────────────── */
class FetchQueue {
  constructor(n) { this.n = n; this.active = 0; this.wait = []; this.peak = 0; }
  async add(task) {
    if (this.active >= this.n) await new Promise(r => this.wait.push(r));
    this.active++; this.peak = Math.max(this.peak, this.active);
    try { return await task(); } finally { this.active--; if (this.wait.length) this.wait.shift()(); }
  }
}
const queue = new FetchQueue(CONCURRENCY);
const urlCache = new Map();
const M = { fetches: 0, failed: 0, bytes: 0, timeouts: 0 };

function withTimeout(run) {
  return queue.add(async () => {
    const ctl = new AbortController();
    const timer = setTimeout(() => ctl.abort(), TIMEOUT);
    M.fetches++; paintMachine();
    try { return await run(ctl.signal, ctl); }
    catch (err) {
      M.failed++;
      if (err && err.name === 'AbortError') { M.timeouts++; throw new Error('timed out after 15 s'); }
      throw new Error((err && err.message) || String(err));
    } finally { clearTimeout(timer); paintMachine(); }
  });
}

function fetchOnce(url, as = 'json') {
  const key = as + ' ' + url;
  if (urlCache.has(key)) return urlCache.get(key);
  let p;
  if (as === 'head') {
    p = withTimeout(async signal => {
      const res = await fetch(url, { method: 'HEAD', signal, cache: 'no-cache' });
      return { ok: res.ok, status: res.status };
    });
  } else if (as === 'first2k') {
    /* the first bytes of a large file, then the stream is cancelled */
    p = withTimeout(async (signal, ctl) => {
      const res = await fetch(url, { signal, cache: 'no-cache' });
      if (!res.ok) throw new Error(`HTTP ${res.status}`);
      if (!res.body || !res.body.getReader) { const t = await res.text(); M.bytes += t.length; return t.slice(0, 2048); }
      const reader = res.body.getReader(); const dec = new TextDecoder(); let t = '';
      while (t.length < 2048) { const { done, value } = await reader.read(); if (done) break; M.bytes += value.length; t += dec.decode(value, { stream: true }); }
      try { await reader.cancel(); } catch (_) { /* already closed */ }
      ctl.abort();
      return t.slice(0, 2048);
    });
  } else {
    p = withTimeout(async signal => {
      const res = await fetch(url, { signal, cache: 'no-cache' });
      if (!res.ok) throw new Error(`HTTP ${res.status}`);
      const t = await res.text();
      M.bytes += t.length;
      if (as === 'text') return t;
      try { return JSON.parse(t); } catch (e) { throw new Error('not valid JSON'); }
    });
  }
  p = p.catch(err => { urlCache.delete(key); throw err; });
  urlCache.set(key, p);
  return p;
}
/* a value, or null with the reason kept */
async function tryGet(url, as) { try { return { v: await fetchOnce(url, as) }; } catch (e) { return { v: null, why: `${blob(url).label}: ${e.message}` }; } }

/* the whole of LINES.md, streamed through the row counter; nothing is kept */
let linesCount = null;
function countLinesMd(onProgress) {
  if (linesCount) return linesCount;
  linesCount = withTimeout(async signal => {
    const res = await fetch(STARS + 'LINES.md', { signal, cache: 'no-cache' });
    if (!res.ok) throw new Error(`HTTP ${res.status}`);
    const c = A.lineRowCounter(); const dec = new TextDecoder(); const reader = res.body.getReader();
    let bytes = 0, last = 0;
    for (;;) {
      const { done, value } = await reader.read();
      if (done) break;
      bytes += value.length; M.bytes += value.length;
      c.push(dec.decode(value, { stream: true }));
      if (bytes - last > 1 << 20) { last = bytes; onProgress(bytes, c.rows); }
    }
    return { ...c.end(), bytes };
  }).catch(e => { linesCount = null; throw e; });
  return linesCount;
}

/* ── rows: created once, re-filled one at a time ─────────────────────────── */
const ROWS = new Map();      // id -> { li, parts, status, typed, sec }
function addRow(sec, id, title) {
  if (ROWS.has(id)) return ROWS.get(id);
  const li = el('li', 'row unknown');
  const head = el('div', 'rhead');
  const st = el('span', 'st', 'WAIT');
  const t = el('span', 'rt', title);
  head.append(st, t);
  const served = el('code', 'served', '');
  const where = el('div', 'where small');
  const nums = el('div', 'nums');
  const rule = el('div', 'rule small dim');
  li.append(head, served, where, nums, rule);
  $('rows-' + sec).append(li);
  const row = { li, st, t, served, where, nums, rule, status: null, typed: false, sec, whereLabel: '' };
  ROWS.set(id, row);
  return row;
}
function fillRow(id, r) {
  const row = ROWS.get(id);
  const status = r.status || A.statusOf(r.stated, r.recount);
  row.status = status; row.typed = !!r.typed; row.whereLabel = r.where ? r.where.label : '';
  row.li.className = 'row ' + (status === A.STATUS.MATCH ? 'match' : status === A.STATUS.DIFFERS ? 'differs' : 'unknown');
  row.st.textContent = status;
  if (r.title) row.t.textContent = r.title;
  row.served.textContent = r.served ? r.served.trim().slice(0, 420) : '(not found in the served text)';
  row.where.replaceChildren();
  if (r.where) row.where.append(link(r.where.href, r.where.label));
  row.where.append(' · ');
  row.where.append(el('span', r.typed ? 'tag typed' : 'tag', r.kind));
  row.nums.replaceChildren();
  const s = el('span', null, 'states '); s.append(el('b', null, A.fmt(r.stated)));
  const c = el('span', null, ' · recomputed '); c.append(el('b', null, A.fmt(r.recount)));
  row.nums.append(s, c);
  row.rule.replaceChildren();
  row.rule.append('rule: ' + r.rule);
  if (r.ruleCite) { row.rule.append(' · '); row.rule.append(link(r.ruleCite.href, r.ruleCite.label)); }
  if (r.note) row.rule.append(el('div', 'note', r.note));
  paintTally();
}
function failRow(id, title, reason) {
  fillRow(id, { title, served: '', kind: 'not read', stated: null, recount: null, rule: 'not yet known', note: reason, status: A.STATUS.UNKNOWN });
}

function paintTally() {
  const n = { MATCH: 0, DIFFERS: 0, UNKNOWN: 0, typed: 0 };
  const differs = [];
  for (const r of ROWS.values()) {
    if (!r.status) continue;
    if (r.status === A.STATUS.MATCH) n.MATCH++; else if (r.status === A.STATUS.DIFFERS) { n.DIFFERS++; differs.push(r.whereLabel); } else n.UNKNOWN++;
    if (r.typed) n.typed++;
  }
  const total = n.MATCH + n.DIFFERS + n.UNKNOWN;
  $('summary').textContent = total
    ? `${total} counts checked in the sections opened · ${n.MATCH} MATCH · ${n.DIFFERS} DIFFERS · ${n.UNKNOWN} NOT YET KNOWN · ${n.typed} typed rather than computed`
    : 'WAIT: nothing is fetched until you open a section.';
  const uniq = [...new Set(differs.filter(Boolean))];
  $('q-next').textContent = total
    ? (uniq.length ? `The next thing a count leads to is the file that states it wrongly: ${uniq.slice(0, 12).join('; ')}${uniq.length > 12 ? ` and ${uniq.length - 12} more` : ''}. From there, the data file the sentence names.` : 'Every count checked so far matches its data, so it leads to the data file it names (the row\'s rule link); no correction is established.')
    : 'not yet known: open a section.';
  paintMachine();
}
function paintMachine() {
  const m = $('machine'); if (!m) return;
  m.textContent = `Machine detail · inputs: served page text and JSON data files (bytes, UTF-8), fetched at run time · outputs: counts (whole numbers, no unit), each marked MATCH, DIFFERS or NOT YET KNOWN, and "typed" where the digits are written into the source · refusals: HTTP status, "timed out after 15 s", "sentence not found in the served text", GitHub listing refused · queue ${CONCURRENCY} (  … [line shortened here: 720 characters; full line on GitHub]
}

/* ── sections ───────────────────────────────────────────────────────────── */
const RT = {};
function label(sec, state, detail) {
  const span = $('lbl-' + sec);
  span.textContent = `${span.getAttribute('data-base-label')} [${detail ? state + ' · ' + detail : state}]`;
}
function hydrate(sec) {
  const rt = RT[sec] || (RT[sec] = { loaded: false, loading: null });
  if (rt.loaded) return rt.loading || Promise.resolve(true);
  if (rt.loading) return rt.loading;
  label(sec, 'LOAD');
  rt.loading = (async () => {
    try {
      await HYDRATE[sec]();
      rt.loaded = true;
      const rows = [...ROWS.values()].filter(r => r.sec === sec);
      label(sec, rows.length ? 'OK' : 'EMPTY', rows.length ? `${rows.length} counts` : 'no count sentence found');
      return true;
    } catch (err) {
      label(sec, 'FAIL', err.message + ' · close and open to retry');
      return false;
    } finally { rt.loading = null; }
  })();
  return rt.loading;
}

/* the stars data every stars-derived sentence counts */
async function starsData() {
  const [b, f, i, h] = await Promise.all([tryGet(STARS + 'blocks/blocks.json'), tryGet(STARS + 'blocks/families.json'), tryGet(STARS + 'code/index.json'), tryGet(STARS + 'LINES.md', 'first2k')]);
  const C = A.starsCounts(b.v, f.v, i.v);
  const header = A.parseLinesHeader(h.v);
  const why = [b.why, f.why, i.why].filter(Boolean).join('; ');
  return { C, header, why, headerWhy: h.why || (header ? '' : 'LINES.md header not found in its first 2 kB'), blocksDoc: b.v };
}

/* recount by noun, under the rule of the code family the sentence belongs to */
function nounRule(noun, S, family, tableNamedCite, famCite) {
  const C = S.C;
  const bj = cite(STARS + 'blocks/blocks.json'), fj = cite(STARS + 'blocks/families.json');
  switch (noun) {
    case 'named blocks':
      if (family === 'old-lens') return { value: C.blocksAll, rule: 'the lens\'s own countsLine counts every entry of blocks.json blocks[] as a named block', cite: famCite, note: `blocks.json marks ${A.fmt(C.blocksAuto)} of those entries kind "auto"; table.html names only ${A.fmt(C.blocksNamedByKind)}.` };
      return { value: C.blocksNamedByKind, rule: 'blocks.json blocks[] whose kind is not "auto"', cite: tableNamedCite, note: `blocks.json holds ${A.fmt(C.blocksAll)} blocks in all.` };
    case 'blocks': return { value: C.blocksAll, rule: 'length of blocks.json blocks[]', cite: bj };
    case 'categories': return { value: C.categories, rule: 'length of blocks.json categories[]', cite: bj };
    case 'groups': return { value: C.groupKeys, rule: 'keys of blocks/families.json (block or unnamed group → family numbers)', cite: fj, note: `${A.fmt(C.unnamedGroups)} of those keys are not a block symbol.` };
    case 'families': case 'function families': return { value: C.familiesDistinct, rule: 'distinct family numbers across every list in blocks/families.json', cite: fj };
    case 'unique lines': case 'unique numbered lines': return { value: S.header ? S.header.unique : null, rule: 'the LINES.md header (its rows are recounted in the LINES.md section)', cite: cite(STARS + 'LINES.md', 3), note: S.headerWhy || '' };
    case 'repositories': return { value: C.repositories, rule: 'distinct names in blocks.json blocks[].repos', cite: bj };
    default: return { value: null, rule: 'no rule for this noun', cite: null };
  }
}

async function scanTyped(sec, url, S, family, tableNamedCite, famCite) {
  const got = await tryGet(url, 'text');
  if (got.v == null) { const id = `${sec}:${url}:read`; addRow(sec, id, blob(url).label); failRow(id, blob(url).label, got.why); return; }
  const found = A.findTyped(got.v);
  for (const f of found) {
    const id = `${sec}:${url}:${f.line}:${f.number}:${f.noun}`;
    addRow(sec, id, `"${A.fmt(f.number)} ${f.noun}"`);
    const r = nounRule(f.noun, S, family, tableNamedCite, famCite);
    fillRow(id, {
      served: f.text, where: cite(url, f.line), typed: true, kind: f.comment ? 'typed (in a comment)' : 'typed',
      stated: f.number, recount: r.value, rule: r.rule, ruleCite: r.cite,
      note: [r.note, r.value == null ? (S.why || 'the data is not available') : ''].filter(Boolean).join(' '),
    });
  }
}

/* a sentence built by code: its expression evaluated on the data now, and the recount */
function computedRow(sec, id, title, url, text, re, stated, recount, rule, ruleCite, note) {
  addRow(sec, id, title);
  const at = A.locate(text, re);
  if (!at) { failRow(id, title, `sentence not found in the served text of ${blob(url).label}`); return; }
  fillRow(id, { served: at.text, where: cite(url, at.line), typed: false, kind: 'computed in the browser', stated, recount, rule, ruleCite, note });
}

const HYDRATE = {
  async stars() {
    const [S, start, table, code] = await Promise.all([starsData(), tryGet(STARS + 'start.html', 'text'), tryGet(STARS + 'table.html', 'text'), tryGet(STARS + 'code.html', 'text')]);
    const namedAt = A.locate(table.v, /const named = data\.blocks\.filter\(b => b\.kind !== 'auto'\)/);
    const namedCite = namedAt ? cite(STARS + 'table.html', namedAt.line) : cite(STARS + 'table.html');
    for (const u of ['start.html', 'table.html', 'code.html']) await scanTyped('stars', STARS + u, S, 'stars', namedCite, null);
    const C = S.C, T = table.v, K = code.v;
    if (T) {
      computedRow('stars', 'st:named', 'table.html status · named blocks', STARS + 'table.html', T, /\$\{named\.length\} named blocks/, C.blocksNamedByKind, C.blocksNamedByKind, 'same rule recomputed: blocks.json blocks[] whose kind is not "auto"', namedCite, S.why);
      computedRow('stars', 'st:auto', 'table.html status · blocks found automatically', STARS + 'table.html', T, /\$\{autos\.length\} blocks found automatically/, C.blocksAuto, C.blocksAuto, 'same rule recomputed: blocks.json blocks[] whose kind is "auto"', namedCite, S.why);
      computedRow('stars', 'st:fns', 'table.html status · functions underneath', STARS + 'table.html', T, /functions underneath/, C.functionsSum, C.familiesPerBlockSum, 'states the sum of blocks[].functions; recomputed as the sum, over every block, of the length of its list in blocks/families.json', cite(STARS + 'blocks/families.json'), S.why);
    } else { addRow('stars', 'st:table', 'table.html'); failRow('st:table', 'table.html', table.why); }
    if (K) {
      computedRow('stars', 'cd:fam', 'code.html status · functions with a permanent number', STARS + 'code.html', K, /functions with a permanent number/, C.indexFamilies, C.familiesDistinct, 'states code/index.json families; recomputed as the distinct family numbers across blocks/families.json', cite(STARS + 'blocks/families.json'), S.why);
      addRow('stars', 'cd:el', 'code.html status · functions and classes in the code');
      const at = A.locate(K, /functions and classes in the code/);
      fillRow('cd:el', { served: at ? at.text : '', where: at ? cite(STARS + 'code.html', at.line) : null, kind: 'computed in the browser', stated: C.indexElements, recount: null, rule: 'not yet known', note: 'code/index.json elements has no published list beside it; a recount needs every family bucket code/f/*.json, which this page does not fetch at once.', status: A.STATUS.UNKNOWN });
      computedRow('stars', 'cd:lines', 'code.html status · unique lines', STARS + 'code.html', K, /unique lines · updated/, C.indexLines, S.header ? S.header.unique : null, 'states code/index.json lines; compared with the LINES.md header (rows recounted in the LINES.md section)', cite(STARS + 'LINES.md', 3), S.headerWhy || S.why);
    } else { addRow('stars', 'st:code', 'code.html'); failRow('st:code', 'code.html', code.why); }
  },

  async lines() {
    const [i, lm] = await Promise.all([tryGet(STARS + 'code/index.json'), tryGet(STARS + 'modular-star/lines.mjs', 'text')]);
    const genAt = A.locate(lm.v, /unique lines, numbered 1 to/);
    const genCite = genAt ? cite(STARS + 'modular-star/lines.mjs', genAt.line) : cite(STARS + 'modular-star/lines.mjs');
    const ids = [['ln:unique', 'LINES.md header · unique lines'], ['ln:last', 'LINES.md header · numbered to'], ['ln:first', 'LINES.md header · numbered from'], ['ln:index', 'code/index.json lines (printed by code.html and every countsLine)']];
    for (const [id, t] of ids) addRow('lines', id, t);
    let res;
    try { res = await countLinesMd((bytes, rows) => label('lines', 'LOAD', `${(bytes / 1048576).toFixed(1)} MB read · ${rows.toLocaleString('en-GB')} rows so far`)); }
    catch (e) { for (const [id, t] of ids) failRow(id, t, `LINES.md: ${e.message}; the rows could not be counted.`); return; }
    const h = A.parseLinesHeader(res.header);
    const where = cite(STARS + 'LINES.md', 3);
    const kind = 'written by a build (typed into the served file)';
    const note = `${(res.bytes / 1048576).toFixed(1)} MB streamed; ${res.badOrder} rows out of ascending order.`;
    fillRow('ln:unique', { served: res.header, where, kind, typed: false, stated: h && h.unique, recount: res.rows, rule: 'rows of the form number<TAB>code, counted in full', ruleCite: genCite, note });
    fillRow('ln:last', { served: res.header, where, kind, stated: h && h.last, recount: res.max, rule: 'the highest row number', ruleCite: genCite, note });
    fillRow('ln:first', { served: res.header, where, kind, stated: h && h.first, recount: res.min, rule: 'the lowest row number', ruleCite: genCite });
    fillRow('ln:index', { served: i.v ? `"lines": ${i.v.lines}` : '', where: cite(STARS + 'code/index.json'), kind: 'a published data field', stated: i.v ? i.v.lines : null, recount: res.rows, rule: 'rows of LINES.md counted in full', ruleCite: genCite, note: i.why || '' });
  },

  async lenses() {
    const S = await starsData();
    const L1350 = GG + '202609141350/', L1754 = GG + '202609141754/code-map/', L2225 = GG + '202609142225/';
    const texts = await Promise.all([L1350 + 'core.js', L1754 + 'core.js', L2225 + 'core.js'].map(u => tryGet(u, 'text')));
    const famLine = (t, u) => { const at = A.locate(t, /function countsLine\(\)/); return at ? cite(u, at.line) : cite(u); };
    const oldCite1350 = famLine(texts[0].v, L1350 + 'core.js'), oldCite1754 = famLine(texts[1].v, L1754 + 'core.js');
    const at2225 = A.locate(texts[2].v, /const named = U\.blocks\.filter\(b => b\.kind !== 'auto'\)/);
    const named2225 = at2225 ? cite(L2225 + 'core.js', at2225.line) : cite(L2225 + 'core.js');
    const C = S.C;
    /* computed count sentences */
    for (const [t, u, c, tag] of [[texts[0], L1350 + 'core.js', oldCite1350, '1350'], [texts[1], L1754 + 'core.js', oldCite1754, '1754']]) {
      if (!t.v) { addRow('lenses', 'cl:' + tag, blob(u).label); failRow('cl:' + tag, blob(u).label, t.why); continue; }
      const re = /\$\{U\.blocks\.length\} named blocks/;
      computedRow('lenses', `cl:${tag}:named`, `countsLine · named blocks (${tag})`, u, t.v, re, C.blocksAll, C.blocksNamedByKind, 'states blocks.json blocks[].length; recomputed as the blocks table.html and the newer countsLine call named: kind not "auto"', named2225, `The word differs from what the code counts: ${A.fmt(C.blocksAuto)} of the ${A.fmt(C.blocksAll)} are kind "auto". ${S.why}`);
      computedRow('lenses', `cl:${tag}:unnamed`, `countsLine · unnamed groups (${tag})`, u, t.v, /\$\{U\.unnamed\.length\} unnamed groups/, C.unnamedGroups, C.unnamedGroups, 'same rule recomputed: keys of blocks/families.json that are not a block symbol', c, S.why);
      computedRow('lenses', `cl:${tag}:fam`, `countsLine · function families (${tag})`, u, t.v, /function families/, C.indexFamilies, C.familiesDistinct, 'states code/index.json families; recomputed as distinct family numbers in blocks/families.json', cite(STARS + 'blocks/families.json'), S.why);
      computedRow('lenses', `cl:${tag}:lines`, `countsLine · unique numbered lines (${tag})`, u, t.v, /unique numbered lines/, C.indexLines, S.header ? S.header.unique : null, 'states code/index.json lines; compared with the LINES.md header', cite(STARS + 'LINES.md', 3), S.headerWhy || S.why);
    }
    if (texts[2].v) {
      const u = L2225 + 'core.js', t = texts[2].v;
      computedRow('lenses', 'cl:2225:table', 'countsLine · blocks on the table (GRAMMAR core)', u, t, /blocks on the table \(/, C.blocksAll, C.blocksAll, 'same rule recomputed: length of blocks.json blocks[]', named2225, S.why);
      computedRow('lenses', 'cl:2225:named', 'countsLine · named (GRAMMAR core)', u, t, /blocks on the table \(/, C.blocksNamedByKind, C.blocksNamedByKind, 'same rule recomputed: kind not "auto"', named2225, S.why);
      computedRow('lenses', 'cl:2225:off', 'countsLine · groups off the table (GRAMMAR core)', u, t, /groups off the table/, C.unnamedGroups, C.unnamedGroups, 'same rule recomputed: keys of blocks/families.json that are not a block symbol', named2225, S.why);
      computedRow('lenses', 'cl:2225:repos', 'countsLine · repositories (GRAMMAR core)', u, t, /repositories · data/, C.repositories, C.repositories, 'same rule recomputed: distinct blocks[].repos', named2225, S.why);
    } else { addRow('lenses', 'cl:2225', blob(L2225 + 'core.js').label); failRow('cl:2225', blob(L2225 + 'core.js').label, texts[2].why); }
    /* typed counts in the lens pages */
    const typedFiles = [
      [L1350 + 'core.js', 'old-lens', oldCite1350], [L1350 + 'index.html', 'old-lens', oldCite1350], [L1350 + 'v02-radial-sunburst/index.html', 'old-lens', oldCite1350],
      [L1350 + 'v03-particle-universe/index.html', 'old-lens', oldCite1350], [L1350 + 'v04-periodic-arrows/index.html', 'old-lens', oldCite1350],
      [L1754 + 'core.js', 'old-lens', oldCite1754], [L1754 + 'index.html', 'old-lens', oldCite1754], [L1754 + 'v02-radial-hierarchy/index.html', 'old-lens', oldCite1754],
      [L1754 + 'v03-block-map/index.html', 'old-lens', oldCite1754], [L1754 + 'v04-block-register-links/index.html', 'old-lens', oldCite1754],
      [L2225 + 'core.js', 'grammar', named2225], [L2225 + 'lenses/column.js', 'grammar', named2225],
    ];
    await Promise.all(typedFiles.map(([u, fam, c]) => scanTyped('lenses', u, S, fam, named2225, c)));
  },

  async iter() {
    const idxUrl = rel('iterations/index.json'), buildUrl = rel('build/build_iterations_index.py');
    const [idx, py, list] = await Promise.all([tryGet(idxUrl), tryGet(buildUrl, 'text'), tryGet(API + 'galaxies-wafers/contents/iterations?ref=main')]);
    const at = A.locate(py.v, /'count': len\(rows\)/);
    const ruleCite = at ? cite(buildUrl, at.line) : cite(buildUrl);
    const served = idx.v ? `"count": ${idx.v.count}` : '';
    const where = idx.v ? cite(idxUrl, 3) : cite(idxUrl);
    const kind = 'written by a build';
    const stated = idx.v ? idx.v.count : null;
    const rows = idx.v && Array.isArray(idx.v.iterations) ? idx.v.iterations : null;
    addRow('iter', 'it:rows', 'iterations/index.json count · rows in the same file');
    fillRow('it:rows', { served, where, kind, stated, recount: rows ? rows.length : null, rule: 'length of iterations[]', ruleCite, note: idx.why || '' });
    addRow('iter', 'it:repo', 'iterations/index.json count · directories in the repository');
    let dirs = null;
    if (Array.isArray(list.v)) dirs = list.v.filter(x => x.type === 'dir' && A.ITER_DIR.test(x.name)).map(x => x.name);
    const inIdx = new Set((rows || []).map(r => r.dir));
    const extra = dirs ? dirs.filter(d => !inIdx.has(d)) : [], missing = dirs ? [...inIdx].filter(d => !dirs.includes(d)) : [];
    fillRow('it:repo', { served, where, kind, stated, recount: dirs ? dirs.length : null, rule: 'directories named NN-slug under iterations/ in the GitHub listing of main, read now (the build also requires an index.html)', ruleCite,
      note: dirs ? `${extra.length ? 'In the repository, not in the index: ' + extra.join(', ') + '. ' : ''}${missing.length ? 'In the index, not in the repository: ' + missing.join(', ') + '.' : ''}` : (list.why || 'the listing was refused') });
    addRow('iter', 'it:served', 'iterations/index.json count · directories served');
    if (!rows) { failRow('it:served', 'iterations/index.json count · directories served', idx.why); return; }
    let done = 0, ok = 0; const absent = [];
    await Promise.all(rows.map(async r => {
      try { const h = await fetchOnce(rel(`iterations/${r.dir}/index.html`), 'head'); if (h.ok) ok++; else absent.push(`${r.dir} (HTTP ${h.status})`); }
      catch (e) { absent.push(`${r.dir} (${e.message})`); }
      done++; label('iter', 'LOAD', `${done}/${rows.length} directories checked`);
    }));
    fillRow('it:served', { served, where, kind, stated, recount: absent.some(a => /timed out|fetch/i.test(a)) ? null : ok, rule: 'index entries whose <dir>/index.html answers HTTP 2xx from the host serving this page', ruleCite,
      note: absent.length ? 'Not served: ' + absent.join(', ') : `Host: ${SITE.host}.` });
  },

  async layers() {
    const manUrl = rel('layers/manifest.json'), buildUrl = rel('build/build_layers.py');
    const [man, py, l1, l2] = await Promise.all([tryGet(manUrl), tryGet(buildUrl, 'text'), tryGet(API + 'galaxies-wafers/contents/layers?ref=main'), tryGet(API + 'galaxies-wafers/contents/layers/modules?ref=main')]);
    const at = A.locate(py.v, /"features": len\(d\["features"\]\)/);
    const ruleCite = at ? cite(buildUrl, at.line) : cite(buildUrl);
    const layers = man.v && Array.isArray(man.v.layers) ? man.v.layers : null;
    const stated = layers ? layers.length : null;
    const where = cite(manUrl);
    const served = layers ? `"layers": [ … ${layers.length} entries … ]` : '';
    addRow('layers', 'ly:repo', 'layers/manifest.json layers · JSON files in the repository');
    let files = null;
    if (Array.isArray(l1.v) && Array.isArray(l2.v)) files = [...l1.v, ...l2.v].filter(x => x.type === 'file' && /\.json$/.test(x.name) && x.path !== 'layers/manifest.json').map(x => x.path);
    const inMan = new Set((layers || []).map(l => l.file));
    const extra = files ? files.filter(f => !inMan.has(f)) : [], missing = files ? [...inMan].filter(f => !files.includes(f)) : [];
    fillRow('ly:repo', { served, where, kind: 'written by a build (no count field; the list is the statement)', stated, recount: files ? files.length : null,
      rule: 'JSON files in layers/ and layers/modules/ of main (manifest.json excluded; layers/tiles/ holds tiles, not layers)', ruleCite,
      note: files ? `${extra.length ? 'Files not in the manifest: ' + extra.join(', ') + '. ' : ''}${missing.length ? 'Manifest entries with no file: ' + missing.slice(0, 20).join(', ') + '.' : ''}` : [l1.why, l2.why].filter(Boolean).join('; ') || 'the listing was refused' });
    addRow('layers', 'ly:served', 'layers/manifest.json layers · files served');
    if (!layers) { failRow('ly:served', 'layers/manifest.json layers · files served', man.why); return; }
    let done = 0, ok = 0; const absent = [];
    await Promise.all(layers.map(async l => {
      try { const h = await fetchOnce(rel(l.file), 'head'); if (h.ok) ok++; else absent.push(`${l.file} (HTTP ${h.status})`); }
      catch (e) { absent.push(`${l.file} (${e.message})`); }
      done++; if (done % 10 === 0 || done === layers.length) label('layers', 'LOAD', `${done}/${layers.length} files checked`);
    }));
    fillRow('ly:served', { served, where, kind: 'written by a build', stated, recount: absent.some(a => /timed out|fetch/i.test(a)) ? null : ok, rule: 'manifest entries whose file answers HTTP 2xx from the host serving this page', ruleCite, note: absent.length ? 'Not served: ' + absent.slice(0, 20).join(', ') : `Host: ${SITE.host}.` });
    /* per-layer features: one file at a time, only when asked */
    const sel = $('layer-pick');
    sel.replaceChildren(...layers.map((l, k) => { const o = el('option', null, `${l.id} · ${A.fmt(l.features)} features · ${(l.bytes / 1024).toFixed(0)} kB`); o.value = String(k); return o; }));
    $('layer-check').disabled = false;
    $('layer-check').onclick = async () => {
      const l = layers[+sel.value]; const id = 'ly:f:' + l.file;
      addRow('layers', id, `${l.file} · features`);
      ROWS.get(id).st.textContent = 'LOAD';
      const got = await tryGet(rel(l.file));
      const n = got.v && Array.isArray(got.v.features) ? got.v.features.length : null;
      fillRow(id, { served: `"features": ${l.features}`, where, kind: 'written by a build', stated: l.features, recount: n, rule: 'length of features[] in the layer file', ruleCite, note: got.why || '' });
      const rows = [...ROWS.values()].filter(r => r.sec === 'layers');
      label('layers', 'OK', `${rows.length} counts`);
    };
  },

  async elements() {
    const pUrl = ELEMENTS + 'provenance.json';
    const [pt, S] = await Promise.all([tryGet(pUrl, 'text'), starsData()]);
    if (pt.v == null) throw new Error(pt.why);
    let P; try { P = JSON.parse(pt.v); } catch (e) { throw new Error('provenance.json is not valid JSON'); }
    const counts = P.counts || {};
    const lineOf = key => { const a = A.locate(pt.v, new RegExp(`"${key}":\\s*(\\d+)`)); return a ? a : null; };
    const row = (id, title, key, recount, rule, ruleCite, note) => {
      addRow('elements', id, title);
      const a = lineOf(key);
      fillRow(id, { served: a ? a.text : '', where: cite(pUrl, a && a.line), kind: 'written by a build', stated: counts[key] ?? null, recount, rule, ruleCite, note });
    };
    const arr = async name => { const g = await tryGet(ELEMENTS + name); return { n: Array.isArray(g.v) ? g.v.length : null, v: g.v, why: g.why || (g.v && !Array.isArray(g.v) ? `${name} is not an array` : '') }; };
    const [apps, els, surf] = await Promise.all([arr('apps.json'), arr('elements.json'), arr('surfaces.json')]);
    const ruleText = k => (P.rule && P.rule[k]) ? `"${P.rule[k]}"` : '';
    row('el:elements', 'catalogue elements · elements.json', 'elements', els.n, `length of elements.json; the file's rule: ${ruleText('elements')}`, cite(ELEMENTS + 'elements.json'), els.why);
    row('el:apps', 'catalogue apps · apps.json', 'apps', apps.n, `length of apps.json; the file's rule: ${ruleText('apps')}`, cite(ELEMENTS + 'apps.json'), apps.why);
    row('el:surfaces', 'catalogue surfaces · surfaces.json', 'surfaces', surf.n, `length of surfaces.json; the file's rule: ${ruleText('surfaces')}`, cite(ELEMENTS + 'surfaces.json'), surf.why);
    row('el:reg', 'catalogue elements · the live register now', 'elements', S.C.blocksAll, 'length of stars blocks/blocks.json blocks[] as served now', cite(STARS + 'blocks/blocks.json'), `Catalogue built from the register generated ${P.register ? P.register.generated_utc : 'not stated'}; the register served now was generated ${S.blocksDoc ? S.blocksDoc.generated_utc : 'not yet known'}. ${S.why}`)  … [line shortened here: 401 characters; full line on GitHub]
    row('el:regapps', 'catalogue apps · the live register now', 'apps', S.C.apps, 'stars blocks/blocks.json blocks[] whose kind is "app", as served now', cite(STARS + 'blocks/blocks.json'), S.why);
    const fn = await arr('functions.json');
    row('el:fns', 'catalogue functions over 10 lines · functions.json', 'functions_over_10_lines', fn.n, 'length of functions.json', cite(ELEMENTS + 'functions.json'), fn.why);
    const nd = (P.numbered_database || []).find(x => /\/families\.json$/.test(x.url || ''));
    if (nd) {
      const g = await tryGet(nd.url);
      const list = Array.isArray(g.v) ? g.v : null;
      row('el:famtot', 'catalogue families_total · the numbered database', 'families_total', list ? list.length : null, 'length of the numbered database families.json the catalogue names', cite(nd.url), g.why || '');
      row('el:over10', 'catalogue functions over 10 lines · recomputed by its rule', 'functions_over_10_lines', list ? list.filter(f => f.lineCount > 10).length : null, `entries of that families.json with lineCount above 10; the file's rule: ${ruleText('functions')}`, cite(nd.url), g.why || '');
    } else {
      row('el:famtot', 'catalogue families_total', 'families_total', null, 'not yet known', null, 'provenance.json names no numbered-database families.json');
    }
    const own = A.parseLinesHeader(P.lines_source && P.lines_source.header);
    row('el:lines-own', 'catalogue numbered_lines · the header it copied', 'numbered_lines', own ? own.unique : null, 'the LINES.md header held in provenance.json lines_source.header', cite(pUrl), '');
    row('el:lines-live', 'catalogue numbered_lines · LINES.md served now', 'numbered_lines', S.header ? S.header.unique : null, 'the LINES.md header as served now', cite(STARS + 'LINES.md', 3), S.headerWhy || 'A difference here means the catalogue is a snapshot of an older LINES.md, not that either file miscounts.');
    row('el:max-live', 'catalogue highest_line_key · LINES.md served now', 'highest_line_key', S.header ? S.header.last : null, 'the highest key in the LINES.md header as served now', cite(STARS + 'LINES.md', 3), S.headerWhy || '');
  },
};

/* ── wiring ─────────────────────────────────────────────────────────────── */
document.querySelectorAll('details.sec').forEach(d => {
  d.addEventListener('toggle', () => { if (d.open) hydrate(d.dataset.sec); });
});
window.__counts = { rows: () => [...ROWS.entries()].map(([id, r]) => ({ id, sec: r.sec, title: r.t.textContent, status: r.status, typed: r.typed, where: r.whereLabel, nums: r.nums.textContent, note: r.rule.textContent })), M, queue, RT };
paintTally();