// research/phoneme-ladders/verify-one-sound-away.mjs
// ─────────────────────────────────────────────────────────────────────────────
// The check for /strata/one-sound-away/.
//
//   node research/phoneme-ladders/verify-one-sound-away.mjs
//   node research/phoneme-ladders/verify-one-sound-away.mjs --mutate   (self-test)
//
// Offline, deterministic, no network. It re-derives with its OWN arithmetic where
// it can, and only then asserts the shipped bytes. Six families:
//
//   1. THE EDGE RULE, BRUTE FORCED. The graph is built by a deletion index that
//      nominates candidates cheaply. The index is the only place a missing edge
//      could hide, so a whole SCOWL band is rebuilt by comparing every pair of
//      pronunciations directly, index discarded, and the two edge sets must be
//      identical. This is the family that makes "no ladder exists" mean anything:
//      an absence is only evidence if the search could not have missed an edge.
//
//   2. THE GRAPH'S SHAPE, recomputed from the dictionary and checked against
//      data/census.json, data/analysis.json and data/probes.json.
//
//   3. THE DIAMETER, re-walked. The 32-rung ladder is re-derived, every rung is
//      re-checked for being exactly one edit from the last AND for being a real
//      pronunciation of a real admitted word, and the claim that no pair is
//      further apart is re-established by an exhaustive sweep, not sampled.
//
//   4. THE SHIPPED BYTES. graph.mjs under public/ must be byte-identical to the
//      one this lab imports; lexicon.txt must decode back to the same graph;
//      every SHA-256 in meta.json must match the file it names.
//
//   5. THE PROSE. Every figure of three digits or more in the page's own text
//      must appear in a register built by walking everything the lab computed.
//      A number in the prose that no computation produced fails the build. The
//      allowlist beside it is explicit and each entry carries its reason, because
//      an unexplained exemption is where a false number would live.
//
//   6. THE PRIOR ART, pinned. Each transcribed figure from Vitevitch (2008) and
//      Alderete et al. (2025) is asserted against meta.json, so that editing one
//      without the other turns this red.
// ─────────────────────────────────────────────────────────────────────────────

import fs from 'node:fs';
import path from 'node:path';
import crypto from 'node:crypto';
import { fileURLToPath } from 'node:url';
import * as L from '../rhyme-orphans/lexicon.mjs';
import { buildGraph, components, bfs, oneEdit, PHONES, bare } from './graph.mjs';

const HERE = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.resolve(HERE, '../..');
// --page= and --data= let mutate.mjs aim this verifier at a DEFECTIVE COPY of the
// artefacts. Without that the "can it go red" question is answered by assertions
// about assertions, which is not an answer.
const arg = (k, d) => { const a = process.argv.find((x) => x.startsWith(k)); return a ? a.slice(k.length) : d; };
const PAGE = path.resolve(arg('--page=', path.join(ROOT, 'public/strata/one-sound-away')));
const DATA = path.resolve(arg('--data=', path.join(HERE, 'data')));
const ENGINE = path.join(HERE, 'graph.mjs');

let pass = 0, fail = 0;
const failures = [];
function check(name, ok, detail) {
  if (ok) { pass++; return true; }
  fail++; failures.push(`${name}${detail ? ' — ' + detail : ''}`);
  return false;
}
const eq = (name, got, want) => check(name, got === want, `got ${JSON.stringify(got)}, want ${JSON.stringify(want)}`);
const near = (name, got, want, tol) => check(name, Math.abs(got - want) <= tol, `got ${got}, want ${want} ±${tol}`);
const sha = (b) => crypto.createHash('sha256').update(b).digest('hex');

const census = JSON.parse(fs.readFileSync(path.join(DATA, 'census.json'), 'utf8'));
const analysis = JSON.parse(fs.readFileSync(path.join(DATA, 'analysis.json'), 'utf8'));
const probes = JSON.parse(fs.readFileSync(path.join(DATA, 'probes.json'), 'utf8'));
const meta = JSON.parse(fs.readFileSync(path.join(PAGE, 'meta.json'), 'utf8'));
const html = fs.readFileSync(path.join(PAGE, 'index.html'), 'utf8');

const { dict, lines } = L.loadCmudict();
const scowl = L.loadScowl();
const BAND = 60;
const adm = L.admit(scowl, dict, BAND);
const g = buildGraph(dict, adm.words);
const { comp, sizes } = components(g.adj);
const giantId = sizes.indexOf(Math.max(...sizes));

// ── 1. the edge rule, brute forced ───────────────────────────────────────────
// Band 10 is small enough to compare every pair directly and large enough that a
// broken index would be caught: 4,775 pronunciations is 11,397,925 pairs.
{
  const small = L.admit(scowl, dict, 10);
  const gs = buildGraph(dict, small.words);
  const n = gs.nodes.length;
  const brute = Array.from({ length: n }, () => new Set());
  let pairs = 0, bruteEdges = 0;
  for (let i = 0; i < n; i++) {
    for (let j = i + 1; j < n; j++) {
      pairs++;
      if (oneEdit(gs.phonesOf[i], gs.phonesOf[j])) { brute[i].add(j); brute[j].add(i); bruteEdges++; }
    }
  }
  eq('brute force: same edge count', bruteEdges, gs.edges);
  let mismatched = 0;
  for (let i = 0; i < n; i++) {
    const a = gs.adj[i], b = [...brute[i]].sort((x, y) => x - y);
    if (a.length !== b.length || a.some((v, k) => v !== b[k])) mismatched++;
  }
  eq('brute force: every adjacency list identical', mismatched, 0);
  check('brute force: the comparison was actually exhaustive', pairs === (n * (n - 1)) / 2, `${pairs} pairs over ${n} nodes`);
  // record the real numbers so the prose can quote them
  global.__brute = { nodes: n, pairs, edges: bruteEdges };
}

// The metric itself, on cases chosen because they are the ones a wrong
// implementation gets wrong.
{
  const P = (s) => s.split(' ');
  eq('oneEdit: substitution', oneEdit(P('K AE T'), P('K AH T')), true);
  eq('oneEdit: addition at the front', oneEdit(P('K AE T'), P('S K AE T')), true);
  eq('oneEdit: deletion at the end', oneEdit(P('K AE T'), P('K AE')), true);
  eq('oneEdit: identity is not one edit', oneEdit(P('K AE T'), P('K AE T')), false);
  eq('oneEdit: two substitutions', oneEdit(P('K AE T'), P('D AH T')), false);
  eq('oneEdit: transposition is NOT one edit', oneEdit(P('AE K T'), P('AE T K')), false);
  eq('oneEdit: two lengths apart', oneEdit(P('K AE T'), P('S K AE T S')), false);
  eq('oneEdit: repeated phone, insertion', oneEdit(P('AH B'), P('AH AH B')), true);
}

// ── 2. the graph's shape ─────────────────────────────────────────────────────
{
  const c = census.bands.find((b) => b.band === BAND);
  eq('census: admitted words', adm.words.length, c.lexicon.admitted);
  eq('census: distinct pronunciations', g.nodes.length, c.nodes);
  eq('census: edges', g.edges, c.edges);
  eq('census: components', sizes.length, c.components);
  eq('census: continent', sizes[giantId], c.giant.size);
  const iso = g.adj.reduce((n, a) => n + (a.length === 0 ? 1 : 0), 0);
  eq('census: isolated sounds', iso, c.isolates);
  eq('probes: isolated sounds', iso, probes.isolation.isolatedNodes);

  let isoWords = 0, inGiant = 0;
  for (const [, ids] of g.nodeOfWord) {
    if (ids.every((i) => g.adj[i].length === 0)) isoWords++;
    if (ids.some((i) => comp[i] === giantId)) inGiant++;
  }
  eq('probes: isolated words', isoWords, probes.isolation.isolatedWords);
  eq('probes: words in the continent', inGiant, probes.isolation.wordsInGiant);
  check('the continent is a continent: the runner-up is tiny',
    census.bands.find((b) => b.band === BAND).componentSizesTop[1] < sizes[giantId] / 100,
    `runner-up ${census.bands.find((b) => b.band === BAND).componentSizesTop[1]}`);

  // the dictionary disagreeing with itself, recomputed
  let sameWord = 0;
  for (let v = 0; v < g.adj.length; v++) {
    for (const u of g.adj[v]) {
      if (u < v) continue;
      const left = new Set(g.wordsOf[v]);
      if (g.wordsOf[u].some((w) => left.has(w))) sameWord++;
    }
  }
  eq('probes: same-word edges', sameWord, probes.dictionaryIndecision.sameWordEdges);
  const ahih = probes.dictionaryIndecision.contrastTable.find((r) => r.pair === 'AH~IH');
  check('AH~IH is mostly the dictionary hedging', ahih.sameWord / ahih.all > 0.65,
    `${ahih.sameWord}/${ahih.all}`);

  // the stress collapse
  let withStress = 0, without = 0;
  for (const prons of dict.values()) {
    withStress += new Set(prons.map((p) => p.join(' '))).size;
    without += new Set(prons.map((p) => p.map(bare).join(' '))).size;
  }
  eq('stress collapse: with digits', withStress, census.stressCollapse.withStress);
  eq('stress collapse: without digits', without, census.stressCollapse.withoutStress);
  eq('stress collapse: cmudict line count', lines, meta.source.cmudict.lines);

  // every named word, re-derived
  for (const [w, readings] of Object.entries(probes.named)) {
    const ids = g.nodeOfWord.get(w);
    if (readings === null) { check(`named: ${w} genuinely absent`, ids === undefined); continue; }
    check(`named: ${w} present`, ids !== undefined);
    if (!ids) continue;
    eq(`named: ${w} reading count`, ids.length, readings.length);
    ids.forEach((i, k) => {
      eq(`named: ${w} pronunciation ${k}`, g.nodes[i], readings[k].pron);
      eq(`named: ${w} degree ${k}`, g.adj[i].length, readings[k].degree);
    });
  }
  for (const w of ['even', 'angst', 'oomph', 'absurd', 'okra', 'asthma', 'puma', 'spinach', 'obtuse']) {
    const ids = g.nodeOfWord.get(w);
    check(`the page calls ${w} alone, and it is`, ids && ids.every((i) => g.adj[i].length === 0));
  }
  // and the reason `evens` does not rescue `even`
  const evens = g.nodeOfWord.get('evens'), even = g.nodeOfWord.get('even');
  check('evens is two edits from even, not one',
    evens && even && !oneEdit(g.phonesOf[even[0]], g.phonesOf[evens[0]]));

  // the letter graph, rebuilt here rather than trusted
  const lnodes = adm.words.slice();
  const lidOf = new Map(lnodes.map((w, i) => [w, i]));
  const buckets = new Map();
  for (let id = 0; id < lnodes.length; id++) {
    const w = lnodes[id];
    for (const k of [w, ...Array.from({ length: w.length }, (_, i) => w.slice(0, i) + w.slice(i + 1))]) {
      let b = buckets.get(k); if (!b) buckets.set(k, (b = [])); b.push(id);
    }
  }
  const ladj = Array.from({ length: lnodes.length }, () => new Set());
  for (const b of buckets.values()) {
    if (b.length < 2) continue;
    for (let i = 0; i < b.length; i++) for (let j = i + 1; j < b.length; j++) {
      if (oneEdit([...lnodes[b[i]]], [...lnodes[b[j]]])) { ladj[b[i]].add(b[j]); ladj[b[j]].add(b[i]); }
    }
  }
  let ledges = 0; for (const s of ladj) ledges += s.size; ledges /= 2;
  eq('letter graph: edges', ledges, analysis.letters.edges);
  const lc = components(ladj.map((s) => [...s]));
  eq('letter graph: continent', Math.max(...lc.sizes), analysis.letters.giant);
  eq('letter graph: isolates', ladj.reduce((n, s) => n + (s.size === 0 ? 1 : 0), 0), analysis.letters.isolates);
  check('sound is better connected than spelling, by word',
    probes.isolation.wordsInGiant / probes.isolation.words > analysis.letters.giant / analysis.letters.nodes);

  // the disagreement extremes, re-measured on the two named pairs
  const letterD = (a, b) => { const { dist } = bfs(ladj.map((s) => [...s].sort((x, y) => x - y)), lidOf.get(a)); return dist[lidOf.get(b)]; };
  const soundD = (a, b) => {
    let best = -1;
    for (const s of g.nodeOfWord.get(a)) { const { dist } = bfs(g.adj, s);
      for (const t of g.nodeOfWord.get(b)) if (dist[t] !== -1 && (best === -1 || dist[t] < best)) best = dist[t]; }
    return best;
  };
  const top = analysis.disagreement.farOnPageTop[0];
  eq('disagreement: the widest sound-adjacent pair is prevented/preventing', `${top.a}/${top.b}`, 'prevented/preventing');
  eq('disagreement: and it is that far on the page', letterD(top.a, top.b), top.letterSteps);
  check('disagreement: prevented and preventing really are one phoneme apart',
    g.nodeOfWord.get('prevented').some((i) => g.nodeOfWord.get('preventing').some((j) => oneEdit(g.phonesOf[i], g.phonesOf[j]))));
  const topR = analysis.disagreement.farInMouthTop[0];
  eq('disagreement: the widest letter-adjacent pair is recruited/recruiter', `${topR.a}/${topR.b}`, 'recruited/recruiter');
  eq('disagreement: and it is that far in the mouth', soundD(topR.a, topR.b), topR.soundSteps);
}

// ── 3. the diameter, re-walked ───────────────────────────────────────────────
{
  const c = census.bands.find((b) => b.band === BAND);
  const rungs = c.giant.diameterLadder;
  eq('ladder: rung count matches the claimed diameter', rungs.length - 1, c.giant.diameter);
  eq('ladder: the claimed diameter', c.giant.diameter, 32);
  eq('ladder: it starts at miscue', rungs[0].words[0], 'miscue');
  eq('ladder: it ends at recruited', rungs[rungs.length - 1].words[0], 'recruited');

  let bad = 0, notAWord = 0;
  for (let i = 0; i < rungs.length; i++) {
    const phones = rungs[i].node.split(' ');
    // every rung is a real pronunciation of a real admitted word
    const id = g.nodeOfWord.get(rungs[i].words[0]);
    if (!id || !id.some((k) => g.nodes[k] === rungs[i].node)) notAWord++;
    if (i > 0 && !oneEdit(rungs[i - 1].node.split(' '), phones)) bad++;
  }
  eq('ladder: every step is exactly one edit', bad, 0);
  eq('ladder: every rung is a real pronunciation of a real word', notAWord, 0);

  // The hard part: that nothing is further apart. Exhaustive, from every node.
  const members = [];
  for (let i = 0; i < comp.length; i++) if (comp[i] === giantId) members.push(i);
  let diameter = 0, radius = Infinity, pathSum = 0, pathPairs = 0, centre = -1;
  for (const s of members) {
    const { dist } = bfs(g.adj, s);
    let ecc = 0;
    for (const t of members) { const d = dist[t]; if (d > ecc) ecc = d; pathSum += d; pathPairs++; }
    if (ecc > diameter) diameter = ecc;
    if (ecc < radius) { radius = ecc; centre = s; }
  }
  pathPairs -= members.length;
  eq('sweep: exhaustive diameter', diameter, c.giant.diameter);
  eq('sweep: exhaustive radius', radius, c.giant.radius);
  near('sweep: mean path length', pathSum / pathPairs, c.giant.meanPath, 1e-9);
  eq('sweep: the centre is ring/wring', g.nodes[centre], 'R IH NG');
  // The page claims the longest ladder runs through that centre. Check it rather
  // than admire it.
  check('the diameter ladder passes through the centre',
    c.giant.diameterLadder.some((r) => r.node === g.nodes[centre]));
  check('and the page names the three rungs in its middle',
    ['read', 'rid', 'ring'].every((w) => c.giant.diameterLadder.some((r) => r.words.includes(w))));
  eq('sweep: the sweep covered every ordered pair', pathPairs, members.length * (members.length - 1));

  // THE TWO NUMBERS THAT ARE NOT THE SAME NUMBER. The diameter is a distance
  // between two SOUNDS. The instrument is asked about WORDS, and `recruited` has
  // a second reading one rung nearer, so it answers 31. A browser pass caught
  // this page conflating them; both are pinned here so it cannot happen twice.
  const wordLevel = (a, b) => {
    let best = -1;
    for (const s of g.nodeOfWord.get(a)) {
      const { dist } = bfs(g.adj, s);
      for (const t of g.nodeOfWord.get(b)) if (dist[t] !== -1 && (best === -1 || dist[t] < best)) best = dist[t];
    }
    return best;
  };
  eq('word level: miscue to recruited is 31, not the diameter', wordLevel('miscue', 'recruited'), 31);
  eq('sound level: the diameter is 32', c.giant.diameter, 32);
  check('the page states both numbers and says why they differ',
    /it says 31/.test(html) && /distance between two <em>sounds<\/em>/.test(html));
  eq('recruited really does have two readings', g.nodeOfWord.get('recruited').length, 2);

  // Vitevitch's illustrative ladder is four rungs; over this lexicon the shortest
  // is three, and the page says so rather than repeating his as if it were optimal.
  eq('word level: cat to dog is 3 rungs here', wordLevel('cat', 'dog'), 3);
  check('the page names the shorter cat-to-dog route', /caught/.test(html) && /<strong>three<\/strong>/.test(html));
  {
    // A word can be written more than once in the dictionary, so a rung holds if
    // ANY reading of one word is one edit from ANY reading of the next.
    const route = ['cat', 'caught', 'cog', 'dog'];
    const broken = [];
    for (let i = 1; i < route.length; i++) {
      const A = g.nodeOfWord.get(route[i - 1]) || [], B = g.nodeOfWord.get(route[i]) || [];
      const joined = A.some((x) => B.some((y) => oneEdit(g.phonesOf[x], g.phonesOf[y])));
      if (!joined) broken.push(`${route[i - 1]}>${route[i]}`);
    }
    check('and that route is a real ladder, rung by rung', broken.length === 0,
      broken.length ? `not joined: ${broken.join(', ')}` : route.join(' > '));
  }

  // the stability claim
  const d35 = census.bands.find((b) => b.band === 35).giant.diameter;
  const d95 = census.bands.find((b) => b.band === 95).giant.diameter;
  eq('stability: diameter at band 35', d35, 32);
  eq('stability: diameter at band 95', d95, 32);
  const w35 = census.bands.find((b) => b.band === 35).lexicon.admitted;
  const w95 = census.bands.find((b) => b.band === 95).lexicon.admitted;
  near('stability: the lexicon really does grow ~80% across that range', 100 * (w95 / w35 - 1), 80, 1.5);
  const g35 = census.bands.find((b) => b.band === 35).giant.size;
  const g95 = census.bands.find((b) => b.band === 95).giant.size;
  near('stability: the continent grows ~71%', 100 * (g95 / g35 - 1), 71, 1.5);
  check('stability: no band between 35 and 95 strays more than one rung',
    census.bands.filter((b) => b.band >= 35).every((b) => Math.abs(b.giant.diameter - 32) <= 1));
}

// ── 4. the shipped bytes ─────────────────────────────────────────────────────
{
  const labEngine = fs.readFileSync(ENGINE);
  const pageEngine = fs.readFileSync(path.join(PAGE, 'graph.mjs'));
  check('the page runs the lab\'s engine, byte for byte', labEngine.equals(pageEngine),
    `${sha(labEngine).slice(0, 12)} vs ${sha(pageEngine).slice(0, 12)}`);
  eq('meta.json pins the engine it shipped', meta.source.engine.sha256, sha(pageEngine));

  const lex = fs.readFileSync(path.join(PAGE, 'lexicon.txt'));
  eq('meta.json pins the lexicon it shipped', meta.shipped['lexicon.txt'].sha256, sha(lex));
  eq('meta.json pins the dictionary it was built from', meta.source.cmudict.sha256,
    sha(fs.readFileSync(path.join(ROOT, 'research/prosody-workshop/cmudict.dict'))));

  // The shipped lexicon must decode back to the same graph the lab built. This is
  // the one check that proves the page is not being given a different English.
  const text = lex.toString('utf8');
  const rows = text.split('\n');
  const header = rows[0].slice(1).split(' ');
  check('lexicon.txt names the alphabet graph.mjs expects',
    header.length === PHONES.length && header.every((p, i) => p === PHONES[i]));
  const alpha = meta.alphabet;
  const decode = new Map([...alpha].map((c, i) => [c, header[i]]));
  const shipped = new Map();
  const bandOf = new Map();
  for (let i = 1; i < rows.length; i++) {
    if (!rows[i]) continue;
    const [w, b, codes] = rows[i].split('\t');
    bandOf.set(w, +b);
    shipped.set(w, codes.split(' ').map((c) => [...c].map((ch) => decode.get(ch))));
  }
  eq('lexicon.txt: word count', shipped.size, meta.shipped['lexicon.txt'].words);
  const shippedWords = [...shipped.keys()].filter((w) => bandOf.get(w) <= BAND).sort();
  const gShipped = buildGraph(shipped, shippedWords);
  eq('lexicon.txt rebuilds the same node count', gShipped.nodes.length, g.nodes.length);
  eq('lexicon.txt rebuilds the same edge count', gShipped.edges, g.edges);
  const cS = components(gShipped.adj);
  eq('lexicon.txt rebuilds the same continent', Math.max(...cS.sizes), sizes[giantId]);
}

// ── 5. the prose ─────────────────────────────────────────────────────────────
{
  // Build a register of every figure the lab actually produced.
  const reg = new Set();
  const put = (n) => {
    if (n === null || n === undefined || !isFinite(n)) return;
    reg.add(String(n));
    if (Number.isInteger(n)) reg.add(n.toLocaleString('en-US'));
    else { reg.add(n.toFixed(1)); reg.add(n.toFixed(2)); reg.add(n.toFixed(3)); }
  };
  const putShare = (x) => { put(100 * x); put(+(100 * x).toFixed(1)); put(+(100 * x).toFixed(2)); put(Math.round(100 * x)); };
  const walk = (o) => {
    if (typeof o === 'number') { put(o); if (o > 0 && o < 1) putShare(o); return; }
    if (Array.isArray(o)) { o.forEach(walk); return; }
    if (o && typeof o === 'object') { Object.values(o).forEach(walk); return; }
  };
  walk(census); walk(analysis); walk(probes); walk(meta);
  // derived figures the prose is entitled to state
  const members = sizes[giantId];
  put(members * (members - 1));                       // the ordered-pair count
  put(Math.round((members * (members - 1)) / 1e6));   // …in millions
  put(global.__brute.pairs);
  put(Math.round(global.__brute.pairs / 1e6));
  put(+(100 * (probes.isolation.wordsInGiant / probes.isolation.words)).toFixed(1));
  put(+(100 * (analysis.letters.giant / analysis.letters.nodes)).toFixed(1));
  put(+(100 * (analysis.letters.isolates / analysis.letters.nodes)).toFixed(1));
  const ah = probes.dictionaryIndecision.contrastTable.find((r) => r.pair === 'AH~IH');
  put(Math.round(100 * ah.sameWord / ah.all));
  put(+(100 * (58347 / 32322 - 1)).toFixed(0));
  put(+(100 * (26713 / 15609 - 1)).toFixed(0));
  put(58347 - 45909);                                  // the words the wider band adds
  put(probes.isolation.isolatedWords - 3);             // "and N others", after three are named
  {                                                    // the published shares, as the prose states them
    const v = meta.priorArt.vitevitch2008;
    put(+(100 * v.hermits / v.lexicon).toFixed(1));
    put(+(100 * v.islands / v.lexicon).toFixed(1));
    put(+(100 * v.giant / v.lexicon).toFixed(1));
  }

  // Every non-derived number that may appear in the prose, each with its reason.
  const ALLOW = new Map([
    ['1879', "Carroll's Doublets, the year"],
    ['1964', 'the Merriam-Webster Pocket Dictionary Vitevitch used'],
    ['1968', 'Word Ways began'],
    ['2008', 'Vitevitch'],
    ['2020', 'Word Ways ended'],
    ['2025', 'Alderete, Mann and Tupper'],
    ['2026', 'the JSON-LD publication date'],
    ['408', 'Vitevitch page range'],
    ['422', 'Vitevitch page range'],
    ['1092', 'the Vitevitch DOI'],
    ['4388', 'the Vitevitch DOI'],
    ['030', 'the Vitevitch DOI'],
    ['3758', 'the Alderete DOI'],
    ['13428', 'the Alderete DOI'],
    ['025', 'the Alderete DOI'],
    ['02610', 'the Alderete DOI'],
    ['1200', 'the OG image width'],
    ['630', 'the OG image height'],
    ['100', 'a percentage denominator, not a measurement'],
    ['000', 'the thousands group of "about 20,000", written as an approximation'],
    ['8-16', 'the ISO date in the JSON-LD'],
    ['10.1044', 'the Vitevitch DOI prefix'],
    ['10.3758', 'the Alderete DOI prefix'],
  ]);

  // Strip everything that is not the page's own prose.
  let prose = html
    .replace(/<script[\s\S]*?<\/script>/gi, ' ')
    .replace(/<style[\s\S]*?<\/style>/gi, ' ')
    .replace(/<head[\s\S]*?<\/head>/i, ' ')
    .replace(/<[^>]+>/g, ' ')
    .replace(/&[a-z]+;/gi, ' ');

  const seen = new Map();
  for (const m of prose.matchAll(/\d[\d,]*(?:\.\d+)?/g)) {
    const raw = m[0];
    const digits = raw.replace(/,/g, '');
    if (!raw.includes(',') && !raw.includes('.') && digits.length < 3) continue; // 1..99 read as English
    if (!seen.has(raw)) seen.set(raw, 0);
    seen.set(raw, seen.get(raw) + 1);
  }
  const orphans = [];
  for (const raw of seen.keys()) {
    const plain = raw.replace(/,/g, '');
    if (reg.has(raw) || reg.has(plain) || ALLOW.has(raw) || ALLOW.has(plain)) continue;
    orphans.push(raw);
  }
  check('every figure in the prose came from a computation', orphans.length === 0,
    `unregistered: ${orphans.join(', ')}`);
  check('the prose actually contains figures to check', seen.size > 40, `${seen.size} found`);

  // The stratum's frontmatter is prose too, and it is what the homepage and the
  // search results show, so it is held to the same register.
  const mdPath = path.join(ROOT, 'src/content/strata/one-sound-away.md');
  if (fs.existsSync(mdPath)) {
    const md = fs.readFileSync(mdPath, 'utf8');
    const mdOrphans = [];
    const mdSeen = new Set();
    for (const m of md.matchAll(/\d[\d,]*(?:\.\d+)?/g)) {
      const raw = m[0], plain = raw.replace(/,/g, '');
      if (!raw.includes(',') && !raw.includes('.') && plain.length < 3) continue;
      mdSeen.add(raw);
      if (reg.has(raw) || reg.has(plain) || ALLOW.has(raw) || ALLOW.has(plain)) continue;
      mdOrphans.push(raw);
    }
    check('every figure in the frontmatter came from a computation', mdOrphans.length === 0,
      `unregistered: ${mdOrphans.join(', ')}`);
    check('the dek names the ladder and the isolates', /32 rungs/.test(md) && /no first step/.test(md));
    check('the frontmatter points at the page that exists', md.includes('href: "/strata/one-sound-away/"'));
    const plainField = md.match(/^plain: "(.*)"$/m);
    check('the plain placard exists and is inside the 420-character gate',
      plainField && plainField[1].length <= 420, plainField ? `${plainField[1].length} chars` : 'missing');
  } else {
    check('the stratum markdown exists', false, 'src/content/strata/one-sound-away.md is missing');
  }

  // The head must not overclaim beyond what the body proves.
  check('the meta description names the real word count',
    /58,347/.test(html.match(/<meta name="description"[^>]*>/)[0]));
  check('the canonical is the real slug', html.includes('https://artwaste.land/strata/one-sound-away/'));
}

// ── 6. the prior art, pinned ─────────────────────────────────────────────────
{
  const v = meta.priorArt.vitevitch2008;
  eq('Vitevitch: lexicon size', v.lexicon, 19340);
  eq('Vitevitch: hermits', v.hermits, 10265);
  eq('Vitevitch: islands', v.islands, 2567);
  eq('Vitevitch: largest component', v.giant, 6508);
  eq('Vitevitch: the three parts add to the whole', v.hermits + v.islands + v.giant, v.lexicon);
  eq('Vitevitch: mean degree', v.meanDegreeGiant, 9.105);
  eq('Vitevitch: average path length', v.averagePathLength, 6.05);
  eq('Vitevitch: reports no diameter', v.reportsDiameter, false);
  eq('Vitevitch: the ladder he printed', v.ladderPrinted, 'cat, bat, bag, bog, dog');
  check('the page quotes that ladder', html.includes('bat, bag, and bog'));

  const a = meta.priorArt.alderete2025;
  eq('Alderete: word forms', a.wordFormNodes, 74286);
  near('Alderete: giant share', a.wordFormGiantShare, 0.4321, 1e-9);
  near('Alderete: hermit share', a.wordFormHermitShare, 0.4299, 1e-9);
  near('Alderete: the three shares add to one', a.wordFormGiantShare + a.wordFormIslandShare + a.wordFormHermitShare, 1, 1e-3);
  eq('Alderete: diameter', a.diameter, 25);
  eq('Alderete: reports no extremal pair', a.reportsExtremalPair, false);
  check('the page does not compare our diameter to theirs as if they were the same graph',
    /not comparable and are not compared/.test(html));
  check('the page names both papers in its own text',
    /Vitevitch/.test(html) && /Alderete/.test(html));

  // Our headline sits between the two published isolate shares once the
  // inflections are stripped, which is the whole point of that section.
  const t = probes.inflectionTest;
  check('the inflection result lands inside the published range',
    t.stripped.isolatedWordShare > a.wordFormHermitShare &&
    t.stripped.isolatedWordShare < v.hermits / v.lexicon,
    `${t.stripped.isolatedWordShare}`);
  check('and the random control does not',
    t.randomControl.isolatedWordShare < a.wordFormHermitShare);
  check('the inflection cut and the control removed the same number of words',
    t.stripped.words === t.randomControl.words);
}

// ── report ───────────────────────────────────────────────────────────────────
console.log(`\n${pass}/${pass + fail} checks pass`);
if (fail) {
  console.log('\nFAILURES:');
  for (const f of failures) console.log('  ✗ ' + f);
  process.exit(1);
}
console.log('one-sound-away: green.');
