#!/usr/bin/env node
// verify-two-hundred-and-forty-reasons-a-second.mjs
//
//   node research/two-hundred-and-forty-reasons-a-second/verify-two-hundred-and-forty-reasons-a-second.mjs
//   ... --raw <dir>     also re-derive any of the seventeen archives found in <dir>
//
// The single offline gate for /strata/two-hundred-and-forty-reasons-a-second/. It
// re-derives the counts from the Commission's raw bytes, checks them against two anchors
// the Commission published and this page did not choose, runs the same protocol through
// three separate implementations in two languages, feeds each check something known to be
// wrong and requires it to go red, and finally pulls every figure out of the rendered HTML
// and requires it to equal a number computed in this process.
//
// Six of the seventeen archives ship with the page, so a clean checkout re-derives six and
// SKIPS eleven. Skips are counted and printed; they never pass quietly.

import fs from 'node:fs';
import path from 'node:path';
import crypto from 'node:crypto';
import { spawnSync } from 'node:child_process';
import { measure, entropy, normField, FORM, PROSE, PROSE_CAP, US } from './derive.mjs';
import { csvShards, entries, eachRow, read as zipRead } from './zipread.mjs';

let REST_COLUMNS = null;
/** Read just the header line of a CSV shard. */
function eachRowHeader(text, cb) { eachRow(text, () => {}, cb); }
import { buildIsland, islandOf, PAGE, CLASSES } from './island.mjs';

const HERE = path.dirname(new URL(import.meta.url).pathname);
const ROOT = path.join(HERE, '..', '..');
const SLUG = 'two-hundred-and-forty-reasons-a-second';
const PUBDATA = path.join(ROOT, 'public', 'strata', SLUG, 'data');
const RESDATA = path.join(HERE, 'data');
const MD = path.join(ROOT, 'src', 'content', 'strata', SLUG + '.md');

let pass = 0, fail = 0, skipped = 0;
const failures = [];
function ok(label, cond, detail) {
  if (cond) { pass++; console.log('PASS ' + String(pass + fail).padStart(3, '0') + '  ' + label); }
  else {
    fail++; failures.push(label);
    console.log('FAIL ' + String(pass + fail).padStart(3, '0') + '  ' + label
      + (detail === undefined ? '' : '  ' + JSON.stringify(detail)));
  }
}
function skip(label, why) {
  skipped++; console.log('SKIP      ' + label + '  (' + why + ')');
}
const near = (a, b, t) => Math.abs(a - b) <= t;
const sha1 = b => crypto.createHash('sha1').update(b).digest('hex');

// ─────────────────────────────────────────────────────────────────────────────────────
// 0. the artefacts
// ─────────────────────────────────────────────────────────────────────────────────────
const manifest = JSON.parse(fs.readFileSync(path.join(HERE, 'manifest.json'), 'utf8'));
const classes = JSON.parse(fs.readFileSync(CLASSES, 'utf8'));
const census = JSON.parse(fs.readFileSync(path.join(RESDATA, 'census-2026-08-30.json'), 'utf8'));
const battery = JSON.parse(fs.readFileSync(path.join(RESDATA, 'mutation-battery-2026-08-30.json'), 'utf8'));
const L = census.landing, S = census.schema;
const html = fs.readFileSync(PAGE, 'utf8');

const rawArg = process.argv.indexOf('--raw');
const extraDir = rawArg > 0 ? process.argv[rawArg + 1] : null;
function locate(zip) {
  for (const dir of [PUBDATA, path.join(RESDATA, 'raw'), extraDir].filter(Boolean)) {
    const p = path.join(dir, zip);
    if (fs.existsSync(p)) return p;
  }
  return null;
}

console.log('== 1. the two published anchors, per archive ==========================');
console.log('   (the Commission\'s own SHA-1 beside the file, and its own advertised row');
console.log('    count beside it on the download listing; neither chosen by this page)');

// The full seventeen decompress to 2 GB of CSV (Facebook alone is 650 MB), so nothing here
// holds an archive: each is read, hashed, measured and released, and any later section that
// needs the bytes again reads them from disk with bufOf(). Keeping the buffers in this map
// is what made a --raw run over all seventeen die with an out-of-memory abort.
const derived = new Map();   // zip -> {path, m, d, sha1, csvBytes}
const bufOf = zip => fs.readFileSync(derived.get(zip).path);
let anchored = 0;
for (const d of manifest.days) {
  const p = locate(d.zip);
  if (!p) { skip(d.platform + ' ' + d.date + ' (' + d.zip + ')', 'archive not in this checkout'); continue; }
  const buf = fs.readFileSync(p);
  const h = sha1(buf);
  ok(d.platform + ' ' + d.date + ': bytes match the Commission\'s published SHA-1',
     h === d.publishedSha1, { got: h, published: d.publishedSha1 });
  const m = measure(buf);
  let csvBytes = 0;
  for (const sh2 of csvShards(buf)) csvBytes += sh2.text.length;
  derived.set(d.zip, { path: p, m, d, sha1: h, csvBytes });
  ok(d.platform + ' ' + d.date + ': recount ' + m.n + ' equals the advertised ' + d.advertised,
     m.n === d.advertised, { recount: m.n, advertised: d.advertised });
  ok(d.platform + ' ' + d.date + ': no malformed records', m.malformed === 0, { malformed: m.malformed });
  anchored++;
}
console.log('   re-derived ' + anchored + ' of ' + manifest.days.length
  + ' archives from raw bytes; ' + (manifest.days.length - anchored) + ' skipped (not in this checkout).');
// Not "derived + skipped adds up", which is arithmetic on itself. The thing that can go
// wrong is an archive sitting on disk that never reached the derivation, so ask the
// filesystem again, independently of the loop above.
const onDisk = manifest.days.filter(d => locate(d.zip) !== null).map(d => d.zip);
ok('every archive present on disk was re-derived, and nothing else was',
   onDisk.length === derived.size && onDisk.every(z => derived.has(z)),
   { onDisk: onDisk.length, derived: derived.size,
     missed: onDisk.filter(z => !derived.has(z)) });
ok('the six archives the page ships are all on disk and all re-derived',
   manifest.days.filter(d => d.shipped).every(d => derived.has(d.zip))
   && manifest.days.filter(d => d.shipped).length === 6, { anchored });

console.log('== 2. the shipped class table equals the re-derivation ================');
for (const [zip, { m, d }] of derived) {
  const row = classes.days.find(r => r.zip === zip);
  ok(d.platform + ' ' + d.date + ': class table matches the fresh re-derivation',
     !!row && row.n === m.n && row.distinctProse === m.distinctProse
     && row.distinctFacts === m.distinctFacts
     && row.distinctProseNorm === m.distinctProseNorm && row.distinctForm === m.distinctForm
     && row.distinctJoint === m.distinctJoint && row.formMultiProse === m.formMultiProse
     && row.inversions === m.inversions && row.placeholder === m.placeholder
     && row.maxFacts === m.maxFacts && row.medianFacts === m.medianFacts
     && row.maxProseAny === m.maxProseAny && row.topFactsCount === m.topFactsCount
     && row.maxProseRows === m.maxProseRows
     && JSON.stringify(row.maxByProse) === JSON.stringify(m.maxByProse)
     && row.sha1 === derived.get(zip).sha1,
     row && { table: [row.n, row.distinctProse, row.distinctForm, row.distinctJoint],
              fresh: [m.n, m.distinctProse, m.distinctForm, m.distinctJoint] });
}
for (const row of classes.days) {
  ok(row.platform + ' ' + row.date + ': class sizes sum to the row count',
     row.formCounts.reduce((a, b) => a + b, 0) === row.n
     && row.jointCounts.reduce((a, b) => a + b, 0) === row.n
     && row.prose.reduce((a, r) => a + r[0], 0) === row.n,
     { n: row.n });
  ok(row.platform + ' ' + row.date + ': row count equals the advertised count',
     row.n === row.advertised, { n: row.n, advertised: row.advertised });
  // Invariants a class table must satisfy whatever the archive says. They cannot catch a
  // wrong derivation carried consistently into every field, which is why the page says so
  // in as many words, but they do catch a table edited in one place and not another.
  // A list that showed the same sentence twice with two different counts beside it would be
  // reporting a defect that is not in the data. Nine pairs did exactly that at a flat cut.
  {
    const texts = row.prose.map(r => r[1]);
    ok(row.platform + ' ' + row.date + ': no two classes display as the same string',
       new Set(texts).size === texts.length,
       { classes: texts.length, distinctStrings: new Set(texts).size });
    // An ellipsis means "cut here", so it may only appear on a string that reached the cut.
    // A short value ending in one would be telling the reader something was removed when
    // nothing was. (The converse cannot be checked from the table alone: a long string is
    // either a whole value or one widened past 420 to stay distinct from its neighbour, and
    // only the re-derivation in section 2 can tell those apart, which it does.)
    ok(row.platform + ' ' + row.date + ': an ellipsis appears only on a string long enough '
       + 'to have been cut',
       texts.every(t => !t.endsWith('…') || t.length >= 420),
       { shortWithEllipsis: texts.filter(t => t.endsWith('…') && t.length < 420) });
  }
  ok(row.platform + ' ' + row.date + ': the distinct counts equal the lengths of their own '
     + 'class vectors, and the joint partition refines both halves',
     row.distinctForm === row.formCounts.length
     && row.distinctJoint === row.jointCounts.length
     && row.distinctProse === row.prose.length
     && row.distinctJoint >= row.distinctForm && row.distinctJoint >= row.distinctProse
     && row.distinctJoint <= row.distinctForm * row.distinctProse
     && row.distinctProseNorm <= row.distinctProse
     && row.distinctFacts <= row.distinctProse
     && (row.formMultiProse === 0) === (row.distinctJoint === row.distinctForm),
     { form: [row.distinctForm, row.formCounts.length],
       joint: [row.distinctJoint, row.jointCounts.length],
       prose: [row.distinctProse, row.prose.length],
       formMultiProse: row.formMultiProse });
}

{
  let anchorRows = 0;
  for (const d of manifest.days) {
    const row = classes.days.find(r => r.zip === d.zip);
    ok(d.platform + ' ' + d.date + ': the shipped table carries the Commission\'s own SHA-1, '
       + 'byte length and advertised count from the manifest',
       !!row && row.sha1 === d.publishedSha1 && row.bytes === d.bytes
       && row.advertised === d.advertised && row.fileId === d.fileId,
       row && { tableSha1: row.sha1, manifestSha1: d.publishedSha1,
                tableBytes: row.bytes, manifestBytes: d.bytes,
                tableAdv: row.advertised, manifestAdv: d.advertised });
    anchorRows++;
  }
  ok('the published anchor was checked for all ' + manifest.days.length
     + ' platform-days, not only the ones whose bytes are in this checkout',
     anchorRows === manifest.days.length && classes.days.length === manifest.days.length,
     { anchorRows, tableRows: classes.days.length, manifestRows: manifest.days.length });
}

console.log('== 3. an independent route: the page\'s own two browser engines, offline ==');
// The engines are lifted out of the served HTML, not re-typed here, so this exercises the
// code the reader runs. One is a character state machine counting into Maps; the other
// splits records on quote parity and counts by sorting and run-length.
const engineSrc = [...html.matchAll(/<script>([\s\S]*?)<\/script>/g)]
  .map(m => m[1]).find(s => s.includes('g.AWDSA'));
ok('the page\'s engine block was found in the served HTML', !!engineSrc);
let AWDSA = null;
if (engineSrc) { new Function(engineSrc)(); AWDSA = globalThis.AWDSA; }
ok('the engine block exports both engines',
   !!AWDSA && typeof AWDSA.engineA === 'function' && typeof AWDSA.engineB === 'function');

if (AWDSA) {
  // Every archive present, not only the six the browser can reach. The engines are the
  // page's own code and they are an INDEPENDENT implementation of the same specification,
  // so restricting them to the shipped six threw away the only independent route over the
  // eleven that carry the page's headline numbers, for no reason but habit.
  //
  // The engines take every shard of a day as one array of strings, the way the browser
  // hands them over, so the whole day's CSV is resident at once and Facebook's is 650 MB
  // (1.3 GB as UTF-16). Above this bound the run is SKIPPED BY NAME with its size, never
  // passed over quietly: the Python cross-check in section 4 still covers those days.
  const ENGINE_MAX_CSV = 200e6;
  for (const [zip, { m, d, csvBytes }] of derived) {
    if (csvBytes > ENGINE_MAX_CSV) {
      skip(d.platform + ' ' + d.date + ': the page\'s two engines',
           (csvBytes / 1e6).toFixed(0) + ' MB of CSV is past the '
           + (ENGINE_MAX_CSV / 1e6) + ' MB in-memory bound this gate sets for them');
      continue;
    }
    const buf = bufOf(zip);
    const shards = [...csvShards(buf)].map(s => s.text);
    const a = AWDSA.engineA(shards);
    const b = AWDSA.engineB(shards);
    const keys = ['n', 'malformed', 'distinctProse', 'distinctProseNorm', 'distinctForm',
                  'distinctJoint', 'formMultiProse', 'topClass'];
    ok(d.platform + ': engine A and engine B agree on all ' + keys.length + ' counts',
       keys.every(k => a[k] === b[k]), { A: a, B: b });
    ok(d.platform + ': both browser engines agree with the Node derivation',
       keys.every(k => a[k] === m[k]), { A: a, node: keys.reduce((o, k) => (o[k] = m[k], o), {}) });
  }
}

console.log('== 4. a third implementation, in a second language ===================');
const pyFiles = [...derived.values()].map(v => locate(v.d.zip));
const py = spawnSync('python3', [path.join(HERE, 'derive-crosscheck.py'), ...pyFiles],
                     { encoding: 'utf8', maxBuffer: 1 << 28 });
if (py.error || py.status !== 0) {
  skip('python3 cross-check over ' + pyFiles.length + ' archives',
       py.error ? String(py.error.message) : 'python3 exited ' + py.status);
} else {
  const P = JSON.parse(py.stdout).files;
  const INTS = ['n', 'malformed', 'distinctFacts', 'distinctProse', 'distinctProseNorm',
                'distinctForm', 'distinctJoint', 'formMultiProse', 'medianFacts', 'maxFacts',
                'placeholder', 'inversions', 'dated', 'topClass'];
  let comparisons = 0;
  for (const [zip, { m, d }] of derived) {
    const p = P[zip];
    if (!p) { ok('python saw ' + zip, false); continue; }
    const bad = INTS.filter(k => p[k] !== m[k]);
    const badF = ['hProse', 'hForm', 'hJoint', 'twins'].filter(k => !near(p[k], m[k], 1e-9));
    comparisons += INTS.length + 4;
    ok(d.platform + ' ' + d.date + ': Python agrees with Node on all ' + (INTS.length + 4) + ' fields',
       bad.length === 0 && badF.length === 0, { ints: bad, floats: badF });
  }
  console.log('   ' + comparisons + ' cross-language field comparisons over ' + derived.size + ' archives.');
}

console.log('== 5. the answer, put to controls that must come back RED =============');

// A minimal STORED-only zip writer, so a doctored archive can be handed to the SAME
// unmodified measure() the page and the table are built from. Nothing about the counting
// is re-implemented here; only the container is rebuilt.
function crc32(buf) {
  let c, table = crc32.t;
  if (!table) {
    table = crc32.t = new Int32Array(256);
    for (let n = 0; n < 256; n++) { c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xEDB88320 ^ (c >>> 1) : c >>> 1; table[n] = c; }
  }
  c = -1;
  for (let i = 0; i < buf.length; i++) c = (c >>> 8) ^ table[(c ^ buf[i]) & 0xFF];
  return (c ^ -1) >>> 0;
}
function makeZip(files) {                    // files: [{name, data:Buffer}]
  const locals = [], central = [];
  let off = 0;
  for (const f of files) {
    const nm = Buffer.from(f.name, 'utf8');
    const lh = Buffer.alloc(30);
    lh.writeUInt32LE(0x04034b50, 0); lh.writeUInt16LE(20, 4); lh.writeUInt16LE(0, 6);
    lh.writeUInt16LE(0, 8); lh.writeUInt32LE(crc32(f.data), 14);
    lh.writeUInt32LE(f.data.length, 18); lh.writeUInt32LE(f.data.length, 22);
    lh.writeUInt16LE(nm.length, 26); lh.writeUInt16LE(0, 28);
    const ch = Buffer.alloc(46);
    ch.writeUInt32LE(0x02014b50, 0); ch.writeUInt16LE(20, 4); ch.writeUInt16LE(20, 6);
    ch.writeUInt16LE(0, 10); ch.writeUInt32LE(crc32(f.data), 16);
    ch.writeUInt32LE(f.data.length, 20); ch.writeUInt32LE(f.data.length, 24);
    ch.writeUInt16LE(nm.length, 28); ch.writeUInt32LE(off, 42);
    locals.push(lh, nm, f.data); central.push(ch, nm);
    off += 30 + nm.length + f.data.length;
  }
  const cd = Buffer.concat(central);
  const eo = Buffer.alloc(22);
  eo.writeUInt32LE(0x06054b50, 0); eo.writeUInt16LE(files.length, 8);
  eo.writeUInt16LE(files.length, 10); eo.writeUInt32LE(cd.length, 12); eo.writeUInt32LE(off, 16);
  return Buffer.concat([...locals, cd, eo]);
}
/** Rebuild a platform-day archive from a transform applied to each shard's text. */
function rebuild(buf, fn) {
  const inners = [];
  for (const oe of entries(buf).sort((a, b) => (a.name < b.name ? -1 : 1))) {
    const innerBuf = zipRead(buf, oe);
    const shards = entries(innerBuf).sort((a, b) => (a.name < b.name ? -1 : 1))
      .map(ie => ({ name: ie.name, data: Buffer.from(fn(zipRead(innerBuf, ie).toString('utf8'), ie.name), 'utf8') }));
    inners.push({ name: oe.name, data: makeZip(shards) });
  }
  return makeZip(inners);
}
function splitCSV(text) {                    // header line + record lines, quote-aware
  const lines = []; let start = 0, q = false;
  for (let i = 0; i < text.length; i++) {
    const c = text.charCodeAt(i);
    if (c === 34) q = !q;
    else if (c === 10 && !q) { lines.push(text.slice(start, i)); start = i + 1; }
  }
  if (start < text.length) lines.push(text.slice(start));
  return lines;
}

// One constant, used by the control that plants the rows AND by the assertion that the
// page describes it correctly. Two separate literals made "the page says 50" compare 50
// with 50: changing the control to plant 60 would have left the page's "50" green.
const PLANT = 50;

const controlMeta = derived.get('sor-reddit-2026-08-28-full.zip');
const control = controlMeta ? { ...controlMeta, buf: bufOf('sor-reddit-2026-08-28-full.zip') } : null;
if (!control) {
  skip('the five poison controls', 'the Reddit archive they run on is not in this checkout');
} else {
  const base = measure(rebuild(control.buf, t => t));
  ok('CONTROL 0: rebuilding the container without touching a byte of CSV changes nothing',
     base.n === control.m.n && base.distinctProse === control.m.distinctProse
     && base.distinctForm === control.m.distinctForm && base.distinctJoint === control.m.distinctJoint,
     { rebuilt: base.n, original: control.m.n });

  // --- CONTROL 1: plant 50 rows carrying prose that occurs nowhere in the real file.
  let planted = 0;
  const poisonedBuf = rebuild(control.buf, (t, name) => {
    if (planted >= PLANT) return t;
    const lines = splitCSV(t);
    const hdr = lines[0].split(',');
    const ix = {}; hdr.forEach((h, i) => { ix[h] = i; });
    const model = lines[1];
    const out = [lines[0]];
    for (let i = 1; i < lines.length; i++) out.push(lines[i]);
    while (planted < PLANT) {
      const cells = [];
      let cur = '', q = false;
      for (let i = 0; i < model.length; i++) {
        const ch = model[i];
        if (ch === '"') { q = !q; cur += ch; }
        else if (ch === ',' && !q) { cells.push(cur); cur = ''; }
        else cur += ch;
      }
      cells.push(cur);
      cells[ix.decision_facts] = '"PLANTED CONTROL ROW ' + planted + ', not a real statement"';
      out.push(cells.join(','));
      planted++;
    }
    return out.join('\n') + '\n';
  });
  const poisoned = measure(poisonedBuf);
  ok('CONTROL 1: ' + PLANT + ' planted rows raise the statement count by exactly ' + PLANT,
     poisoned.n === base.n + PLANT, { base: base.n, poisoned: poisoned.n });
  ok('CONTROL 1: ' + PLANT + ' planted rows raise the distinct free-text count by exactly ' + PLANT,
     poisoned.distinctProse === base.distinctProse + PLANT,
     { base: base.distinctProse, poisoned: poisoned.distinctProse });
  ok('CONTROL 1: and the distinct full-description count by exactly ' + PLANT,
     poisoned.distinctJoint === base.distinctJoint + PLANT,
     { base: base.distinctJoint, poisoned: poisoned.distinctJoint });
  const restored = measure(rebuild(control.buf, t => t));
  ok('CONTROL 1: the undoctored archive is unchanged afterwards',
     restored.n === base.n && restored.distinctProse === base.distinctProse,
     { restored: restored.n, base: base.n });

  // --- CONTROL 2: the advertised-count anchor must reject a file with rows removed.
  let dropped = 0;
  const shortBuf = rebuild(control.buf, t => {
    if (dropped >= 10) return t;
    const lines = splitCSV(t);
    const keep = [lines[0]];
    for (let i = 1; i < lines.length; i++) {
      if (dropped < 10 && lines[i].trim()) { dropped++; continue; }
      keep.push(lines[i]);
    }
    return keep.join('\n') + '\n';
  });
  const short = measure(shortBuf);
  ok('CONTROL 2: deleting ten rows makes the advertised-count anchor FAIL',
     short.n !== control.d.advertised && short.n === base.n - 10,
     { short: short.n, advertised: control.d.advertised });

  // --- CONTROL 3: the SHA-1 anchor must reject a single flipped byte.
  const anchorOK = (b, published) => sha1(b) === published;
  const bent = Buffer.from(control.buf); bent[Math.floor(bent.length / 2)] ^= 0xFF;
  ok('CONTROL 3: the SHA-1 anchor passes the real bytes and FAILS one flipped byte',
     anchorOK(control.buf, control.d.publishedSha1) && !anchorOK(bent, control.d.publishedSha1));

  // --- CONTROL 4: the load-bearing statistic itself. H(prose | form) is the whole second
  //     layer. Break the coupling between the two halves and it must RISE; make the prose
  //     a strict function of the form and it must fall to exactly zero.
  const realAdds = control.m.hJoint - control.m.hForm;
  let lcg = 1;
  const permutedBuf = rebuild(control.buf, t => {
    const lines = splitCSV(t);
    const hdr = lines[0].split(',');
    const ix = {}; hdr.forEach((h, i) => { ix[h] = i; });
    const recs = lines.slice(1).filter(l => l.trim());
    const cellsOf = (line) => {
      const cells = []; let cur = '', q = false;
      for (let i = 0; i < line.length; i++) {
        const ch = line[i];
        if (ch === '"') { q = !q; cur += ch; }
        else if (ch === ',' && !q) { cells.push(cur); cur = ''; }
        else cur += ch;
      }
      cells.push(cur); return cells;
    };
    const rows = recs.map(cellsOf);
    const col = rows.map(r => PROSE.map(k => r[ix[k]]));
    for (let i = col.length - 1; i > 0; i--) {          // deterministic Fisher-Yates
      lcg = (lcg * 1103515245 + 12345) % 2147483648;
      const j = lcg % (i + 1);
      const t2 = col[i]; col[i] = col[j]; col[j] = t2;
    }
    rows.forEach((r, i) => PROSE.forEach((k, c) => { r[ix[k]] = col[i][c]; }));
    return [lines[0], ...rows.map(r => r.join(','))].join('\n') + '\n';
  });
  const permuted = measure(permutedBuf);
  const permAdds = permuted.hJoint - permuted.hForm;
  ok('CONTROL 4a: shuffling the free text between rows makes it add MORE bits, not the same',
     permAdds > realAdds + 1.0 && permuted.n === base.n,
     { real: +realAdds.toFixed(4), shuffled: +permAdds.toFixed(4), Hprose: +control.m.hProse.toFixed(4) });
  const flatBuf = rebuild(control.buf, t => {
    const lines = splitCSV(t);
    const hdr = lines[0].split(',');
    const ix = {}; hdr.forEach((h, i) => { ix[h] = i; });
    const out = [lines[0]];
    for (let i = 1; i < lines.length; i++) {
      if (!lines[i].trim()) continue;
      const cells = []; let cur = '', q = false;
      for (let k = 0; k < lines[i].length; k++) {
        const ch = lines[i][k];
        if (ch === '"') { q = !q; cur += ch; }
        else if (ch === ',' && !q) { cells.push(cur); cur = ''; }
        else cur += ch;
      }
      cells.push(cur);
      // prose becomes a strict function of the coded half: one sentence per category
      const key = cells[ix.category];
      PROSE.forEach(k => { cells[ix[k]] = ''; });
      cells[ix.decision_facts] = '"this decision was taken under ' + key + '"';
      out.push(cells.join(','));
    }
    return out.join('\n') + '\n';
  });
  const flat = measure(flatBuf);
  ok('CONTROL 4b: prose made a strict function of a coded field adds exactly 0.000 bits',
     Math.abs(flat.hJoint - flat.hForm) < 1e-9 && flat.formMultiProse === 0 && flat.n === base.n,
     { adds: flat.hJoint - flat.hForm, formMultiProse: flat.formMultiProse });
}

// The whole scheme rests on two characters never occurring inside a field value. Assert it
// rather than assume it: if a platform ever files one, every joined key becomes ambiguous.
for (const [zip, { d }] of derived) {
  let sawSeparator = false;
  for (const shard of csvShards(bufOf(zip))) {
    if (shard.text.includes(US) || shard.text.includes('\u241e')) { sawSeparator = true; break; }
  }
  ok(d.platform + ' ' + d.date + ': no field value contains the U+241F or U+241E join characters',
     !sawSeparator);
}

console.log('== 5b. the normaliser itself, on inputs built to make it fold ========');
// Section 6 below can only run where a Pinterest archive is present, and on ALL SIX of the
// archives that ship, distinctProse === distinctProseNorm. So in a clean checkout a
// normaliser that did nothing at all would pass every assertion in this file: the exact and
// the folded counts would agree because they agree anyway. That is a check that cannot
// fail, so here is one that can. The page names the normalisation as "NFC, whitespace
// collapsing and case folding, and nothing else", and each clause is required to bite, and
// required NOT to bite on a difference it is not supposed to erase.
{
  const base = 'Advertising Guidelines';
  const folds = [
    ['case', 'advertising guidelines'],
    ['a run of spaces', 'Advertising    Guidelines'],
    ['a tab and a newline', 'Advertising\t\nGuidelines'],
    ['leading and trailing space', '   Advertising Guidelines   '],
    ['a non-breaking space', 'Advertising\u00a0Guidelines'],
  ];
  const b = normField(base);
  for (const [what, variant] of folds) {
    ok('the normaliser folds ' + what, normField(variant) === b,
       { base: b, variant: normField(variant) });
  }
  // NFC is its own clause and needs a character that actually decomposes.
  ok('the normaliser folds a decomposed accent onto its composed form (NFC)',
     normField('cafe\u0301 guidelines') === normField('caf\u00e9 Guidelines'),
     { decomposed: normField('cafe\u0301 guidelines'), composed: normField('caf\u00e9 Guidelines') });
  // and the other direction: it must not erase a difference that is a real difference
  for (const [what, variant] of [['a different word', 'Advertising Guideline'],
                                 ['a different letter', 'Advertising Guidelinez'],
                                 ['an extra clause', 'Advertising Guidelines, section 2']]) {
    ok('the normaliser does NOT fold ' + what, normField(variant) !== b);
  }
  // and the whole pipeline, not just the field-level helper: two records differing only in
  // case must be one class after folding and two before it.
  const exact = new Set([base, 'advertising guidelines']);
  const folded = new Set([...exact].map(normField));
  ok('two values differing only in case are 2 exact classes and 1 folded class',
     exact.size === 2 && folded.size === 1, { exact: exact.size, folded: folded.size });
}

console.log('== 5c. the column split the page describes ============================');
// The page prints "20 coded, 5 free text, and the remaining 13". An earlier version put
// territorial_scope in the remaining 13 while also counting it among the 20, i.e. one
// column on both sides of a partition. A partition is checkable, so check it.
{
  const anyZip = [...derived.keys()][0];
  if (!anyZip) { skip('the 38-column partition against a real archive', 'no archive in this checkout'); }
  else {
    let header = null;
    for (const shard of csvShards(bufOf(anyZip))) {
      eachRowHeader(shard.text, h => { header = h; });
      break;
    }
    ok('a real archive has exactly ' + S.columns + ' columns, as the page says',
       !!header && header.length === S.columns, header && { got: header.length, said: S.columns });
    if (header) {
      ok('every FORM and PROSE column named in derive.mjs exists in the real header',
         FORM.every(k => header.includes(k)) && PROSE.every(k => header.includes(k)),
         { missing: [...FORM, ...PROSE].filter(k => !header.includes(k)) });
      const rest = header.filter(h => !FORM.includes(h) && !PROSE.includes(h));
      ok('FORM, PROSE and the rest partition the header: '
         + FORM.length + ' + ' + PROSE.length + ' + ' + rest.length + ' = ' + header.length,
         FORM.length + PROSE.length + rest.length === header.length
         && new Set([...FORM, ...PROSE, ...rest]).size === header.length,
         { form: FORM.length, prose: PROSE.length, rest: rest.length, total: header.length });
      ok('territorial_scope is one of the coded 20 and is NOT in the remaining '
         + rest.length + ', so the page may not describe it as both',
         FORM.includes('territorial_scope') && !rest.includes('territorial_scope'));
      ok('the two date columns the page reads are in the remaining ' + rest.length
         + ', which is why the page does not call them explanations',
         rest.includes('content_date') && rest.includes('application_date'));
      REST_COLUMNS = rest;
    }
  }
}

console.log('== 6. the normalisation gap is exactly what the page says it is =======');
// The page claims the ONLY thing case-and-whitespace folding merges, on either Pinterest
// day, is "Advertising Guidelines" against "Advertising guidelines". So: take the exact
// free-text classes the production measure() found, fold that one word by hand, and the
// count must land on the normalised count to the digit. Anything else hiding inside that
// gap, a stray space or a second difference of case, leaves a residue and turns this red.
for (const zip of ['sor-pinterest-2026-08-28-full.zip', 'sor-pinterest-2026-08-27-full.zip']) {
  const got = derived.get(zip);
  if (!got) { skip('the Pinterest normalisation gap on ' + zip, 'archive not in this checkout'); continue; }
  const m = got.m;
  const oneWord = new Set(m.prose.map(([t]) => t.replace(/Advertising Guidelines/g, 'Advertising guidelines')));
  ok(got.d.date + ': folding one word closes the whole gap, ' + m.distinctProse
     + ' exact to ' + oneWord.size + ' folded, against ' + m.distinctProseNorm + ' normalised',
     oneWord.size === m.distinctProseNorm && m.distinctProse > m.distinctProseNorm,
     { exact: m.distinctProse, afterOneWord: oneWord.size, normalised: m.distinctProseNorm });
  const other = new Set(m.prose.map(([t]) => t));
  ok(got.d.date + ': and the gap is real, not an artefact of the count ('
     + (m.distinctProse - m.distinctProseNorm) + ' classes merge)',
     other.size === m.distinctProse && m.distinctProse - m.distinctProseNorm > 0);
}

console.log('== 7. the JSON island inside the page is in sync with the derivation ==');
const wantIsland = buildIsland(classes);
const gotIsland = islandOf(html);
ok('the island in index.html is byte-identical to buildIsland(classes.json)',
   gotIsland === wantIsland, { pageBytes: gotIsland.length, derivedBytes: wantIsland.length });
let island = null;
try { island = JSON.parse(gotIsland); } catch (e) { /* reported below */ }
ok('the island parses as JSON', !!island && Array.isArray(island.days) && island.days.length === 17,
   island && { days: island.days.length });
if (island) {
  for (const d of island.days) {
    ok(d.p + ' ' + d.d + ': island class sizes sum to n and n equals the advertised count',
       d.pc.reduce((a, b) => a + b, 0) === d.n && d.fc.reduce((a, b) => a + b, 0) === d.n
       && d.jc.reduce((a, b) => a + b, 0) === d.n && d.n === d.adv, { n: d.n, adv: d.adv });
  }
  ok('the island marks exactly six archives as shipped, and all six are on disk',
     island.days.filter(d => d.ship).length === 6
     && island.days.filter(d => d.ship).every(d => fs.existsSync(path.join(PUBDATA, d.zip))),
     { shipped: island.days.filter(d => d.ship).map(d => d.p) });
  ok('H(prose | form) is never negative beyond floating-point noise',
     island.days.every(d => (entropy(d.jc) - entropy(d.fc)) > -1e-9),
     island.days.map(d => +(entropy(d.jc) - entropy(d.fc)).toFixed(12)));
}

console.log('== 8. the census: 366 platform counts against a separately published total ==');
const colFor = day => census.columns.indexOf(day);
for (const day of ['2026-08-28', '2026-08-27']) {
  const c = colFor(day);
  const sum = census.rows.reduce((a, r) => a + r[c], 0);
  ok(day + ': the 366 per-platform counts sum to the advertised global total ' + census.globalAdvertised[day],
     sum === census.globalAdvertised[day], { sum, advertised: census.globalAdvertised[day] });
}
for (const [zip, { d }] of derived) {
  const c = colFor(d.date);
  if (c < 0) continue;
  const row = census.rows.find(r => r[0] === d.platformId);
  ok(d.platform + ' ' + d.date + ': the census agrees with the file listing',
     !!row && row[c] === d.advertised, row && { census: row[c], listing: d.advertised });
}

console.log('== 9. every figure the reader can see, string-matched ================');
// Nothing below reads a number out of the page and calls it verified. Each entry computes
// a value HERE and then requires the page's rendered text to carry exactly that string.
// The final two assertions close the loop in both directions, so a figure added to the
// prose without a computation, or a computation quietly dropped from the prose, is red.
const D = {};
for (const r of classes.days) {
  D[r.platform + '|' + r.date] = r;
}
const H = a => entropy(a);
const adds = r => { const v = H(r.jointCounts) - H(r.formCounts); return Math.abs(v) < 1e-9 ? 0 : v; };
const twins = r => r.jointCounts.reduce((a, c) => a + c * c, 0) / r.n;
const N = n => n.toLocaleString('en-US');
const pin28 = D['Pinterest|2026-08-28'], pin27 = D['Pinterest|2026-08-27'];
const fb = D['Facebook|2026-08-28'], eb = D['eBay|2026-08-28'], sh = D['Shopify|2026-08-28'];
const sn = D['Snapchat|2026-08-28'], rd = D['Reddit|2026-08-28'], rd27 = D['Reddit|2026-08-27'];
const li = D['LinkedIn|2026-08-28'], gh = D['GitHub|2026-08-28'], dm = D['Dailymotion|2026-08-28'];
const sc = D['SoundCloud|2026-08-28'], dc = D['Discord|2026-08-28'], fn = D['Fortnite|2026-08-28'];

const c28 = colFor('2026-08-28');
const total28 = census.rows.reduce((a, r) => a + r[c28], 0);
const ranked = census.rows.slice().sort((a, b) => b[c28] - a[c28]);
const gshop = ranked[0][c28];
const days = census.dailyGlobal, gsDays = census.dailyGoogleShopping;
const both = Object.keys(days).filter(k => k in gsDays).sort();
const gsVals = both.map(k => gsDays[k]), gVals = both.map(k => days[k]);
const shares = both.map(k => gsDays[k] / days[k]).sort((a, b) => a - b);
const longestRow = classes.days.slice().sort((a, b) => b.maxProseAny - a.maxProseAny)[0];
const longestCol = longestRow.maxProseCol;
ok('the longest free-text value in the seventeen is in ' + longestCol + ' (' + longestRow.maxProseAny
   + ' characters, ' + longestRow.platform + ' ' + longestRow.date + '), and it is longer than the '
   + 'longest decision_facts (' + Math.max(...classes.days.map(r => r.maxFacts)) + ')',
   longestRow.maxProseAny > Math.max(...classes.days.map(r => r.maxFacts))
   && longestCol !== 'decision_facts' && PROSE_CAP[longestCol] < PROSE_CAP.decision_facts,
   { col: longestCol, len: longestRow.maxProseAny, cap: PROSE_CAP[longestCol] });
ok('the longest free-text value in the seventeen was filed more than once, so the page may '
   + 'not present it as a one-off', longestRow.maxProseRows > 1,
   { col: longestCol, len: longestRow.maxProseAny, filed: longestRow.maxProseRows });
ok('the per-column caps the page prints are the schema\'s, and they are not all 5,000',
   PROSE_CAP.decision_facts === 5000 && PROSE_CAP.incompatible_content_explanation === 2000
   && PROSE_CAP.incompatible_content_ground === 500 && PROSE.every(k => k in PROSE_CAP),
   PROSE_CAP);
ok('every value filed in every column is inside that column\'s published cap',
   classes.days.every(r => PROSE.every(k => r.maxByProse[k] <= PROSE_CAP[k])),
   classes.days.flatMap(r => PROSE.filter(k => r.maxByProse[k] > PROSE_CAP[k])
     .map(k => r.platform + ' ' + k + ' ' + r.maxByProse[k] + ' > ' + PROSE_CAP[k])));
// the daily mean implied by the front page block is below EVERY day the census carries, so
// the block lags the files it summarises. This is the page's claim and it is checkable here.
ok('the front page total implies a daily mean below every one of the '
   + Object.keys(days).length + ' days the census carries, so it lags the files',
   L.readings[0].statementsOfReasons / L.windowDays < Math.min(...Object.values(days)),
   { impliedDailyMean: Math.round(L.readings[0].statementsOfReasons / L.windowDays),
     lowestDayMeasured: Math.min(...Object.values(days)) });
const median = shares[Math.floor(shares.length / 2)];

// Each row is [ the string the reader must see, what computed it, the phrase it must sit
// inside ]. The phrase matters: an earlier version matched bare numbers, and a mutation
// test caught it out. Two different quantities that happen to share a string ("14 platform
// days with no date inversion" and "14 archives from 2026-08-28") were interchangeable, so
// changing one to the other's value passed. Context is now REQUIRED of every figure short
// enough to collide, and that requirement is itself asserted below.
const EXPECT = [
  [N(L.readings[0].statementsOfReasons), 'the front page total at the first reading', 'page reported {} statements of reasons'],
  [(L.readings[0].statementsOfReasons / (L.windowDays * 86400)).toFixed(1), 'that total as a rate', 'which is {} a second'],
  [N(L.readings[1].statementsOfReasons), 'the front page total 1h45m later', 'same block reported {} .'],
  [(L.readings[1].statementsOfReasons / (L.windowDays * 86400)).toFixed(1), 'the second reading as a rate', 'or {} a second'],
  [N(L.readings[1].statementsOfReasons - L.readings[0].statementsOfReasons), 'what it moved by in between', 'a move of {} in an hour and three quarters'],
  [String(L.activePlatforms), 'active platforms on the front page', 'averaged over six months, from {} platforms'],
  // the listing's own row count, not the landing block's 180-day "active platforms". The two
  // are both 366 today and are different quantities; the sentence is about the listing.
  [String(census.rows.length), 'the rows the download listing carries', 'listing carries a row for {} platforms'],
  [String(census.rows.filter(r => r[c28] > 0).length), 'platforms that filed anything on the day', 'only {} of them filed anything at all'],
  [N(Math.round(L.readings[0].statementsOfReasons / L.windowDays)), 'the front page total as a daily mean', 'is {} statements a day, which is fewer'],
  [String(L.windowDays), 'the window, where the block is compared with the files', '3,761,203,712 over {} days is'],
  [N(L.readings[0].statementsOfReasons), 'the front page total, where it is compared with the files', 'behind the files it summarises: {} over 180 days'],
  [String(both.length), 'the days it is compared against', 'any single one of the {} days counted lower down'],
  [String(S.decisionFactsCharLimit.toLocaleString('en-US')), 'the schema limit on decision_facts', 'up to {} characters'],
  [String(S.statementCategories), 'statement categories in the schema', 'are {} statement categories'],
  [String(S.keywordValues), 'keyword values in the schema', '{} keyword values'],
  [String(S.contentTypes), 'content types in the schema', '{} content types'],
  [String(S.restrictionCodes), 'restriction codes in the schema', '{} restriction codes'],
  [String(S.decisionGrounds), 'decision grounds in the schema', '{} decision grounds'],
  [String(S.territorialCodes), 'territorial codes in the schema', 'subset of {} countries'],
  [String(S.columns), 'columns in a statement of reasons', 'statement of reasons has {} columns'],
  [String(S.columns - FORM.length - PROSE.length), 'columns that are neither form nor prose', 'The remaining {} are identifiers'],
  // the number in the page's own title, rounded off the measured rate rather than asserted
  [String(Math.round(L.readings[0].statementsOfReasons / (L.windowDays * 86400) / 10) * 10),
   'the rate rounded to the number in the title', '{} moderation decisions a second'],
  [String(fb.fileId), 'the Commission file identifier the quoted Facebook sentence came from', 'Facebook, 2026-08-28, file {}'],
  [String(sh.distinctJoint), 'the count on a one-class platform-day', 'every count is {} and every entropy'],
  [H(sh.jointCounts).toFixed(3), 'the entropy on a one-class platform-day', 'every entropy is {} , and the conditional-entropy'],
  [N(total28), 'the whole database on 2026-08-28', 'the whole database took {} statements'],
  [N(total28), 'and the same total from the 366 platform counts', 'per-platform counts sum to {} exactly'],
  [N(gshop), 'Google Shopping on 2026-08-28', 'Google Shopping filed {} of them'],
  [(100 * gshop / total28).toFixed(2) + '%', 'Google Shopping as a share of the day', 'which is {} of the day'],
  [(100 * gshop / total28).toFixed(2) + '%', 'the share of the day that is taken on trust', 'largest filer, {} of it, is taken on trust'],
  [(100 * ranked.slice(0, 6).reduce((a, r) => a + r[c28], 0) / total28).toFixed(2) + '%', 'the top six as a share', 'largest filers are {} of it'],
  [String(census.rows.filter(r => r[c28] === 0).length), 'platforms that filed nothing', 'the other {} filed nothing'],
  [String(both.length), 'days on which both series have a file', 'across the {} days sampled here, the most recent page'],
  [N(Math.min(...gsVals)), 'Google Shopping low over that window', 'sits between {} and'],
  [N(Math.max(...gsVals)), 'Google Shopping high over that window', 'sits between 16,838,647 and {} : a spread'],
  [Math.round(100 * (Math.max(...gsVals) - Math.min(...gsVals)) / Math.min(...gsVals)) + '%', 'that spread', 'a spread of about {} over seven weeks'],
  [N(Math.min(...gVals)), 'the database low over that window', 'total swings from {} to'],
  [N(Math.max(...gVals)), 'the database high over that window', 'swings from 22,904,281 to {} .'],
  [(100 * Math.min(...shares)).toFixed(2) + '%', 'lowest Google Shopping share', 'share of the day runs from {} to'],
  [(100 * Math.max(...shares)).toFixed(2) + '%', 'highest Google Shopping share', 'runs from 38.06% to {} with'],
  [(100 * median).toFixed(2) + '%', 'median Google Shopping share', 'with a median of {} ,'],
  [N(census.dailyGlobal['2026-08-22']), 'the day missing from the paired series', 'listing shows {} statements that day'],
  [N(sh.n), 'Shopify statements, in the opening paragraph', "Shopify's {} decisions on 2026-08-28 carry one"],
  [N(sh.n), 'Shopify statements, in the closing paragraph', 'ordinary Friday, that is {} decisions arriving as one'],
  [String(sh.distinctJoint), 'Shopify distinct public descriptions', 'left in the table with its {} rather than dropped'],
  [N(pin28.n), 'Pinterest statements, in the opening paragraph', "Pinterest's {} carry 206"],
  [N(pin28.n), 'Pinterest statements, in the free-choice paragraph', "Pinterest's {} decisions on 2026-08-28 carry exactly"],
  [String(pin28.distinctFacts), 'Pinterest distinct decision_facts values', 'carry exactly {} distinct values of it'],
  // The sentence this guards is scoped to decision_facts alone ("carry exactly 200 distinct
  // values of IT"), so the check must be the commonest decision_facts, not the commonest
  // five-column prose class, which is a strictly finer partition that happens to coincide.
  [N(pin28.topFactsCount), 'the commonest Pinterest decision_facts value', 'commonest one is filed {} times'],
  [(100 * pin28.topFactsCount / pin28.n).toFixed(1) + '%', 'that sentence as a share of the day', 'which is {} of the platform'],
  [String(pin28.distinctProse), 'Pinterest free-text classes, opening paragraph', 'carry {} , or 201 once case'],
  [String(pin28.distinctProse), 'Pinterest free-text classes, free-choice paragraph', 'five free-text columns and it is {} .'],
  [String(pin28.distinctProseNorm), 'Pinterest free-text classes folded', 'or {} once case and spacing'],
  [String(pin28.distinctProse), 'Pinterest exact count in the check panel', '{} against 201 , and 235'],
  [String(pin28.distinctProseNorm), 'Pinterest folded count in the check panel', '206 against {} , and 235 against 228'],
  [String(pin27.distinctProse), 'Pinterest 08-27 exact', 'and {} against 228'],
  [String(pin27.distinctProseNorm), 'Pinterest 08-27 folded', 'and 235 against {} .'],
  [String(pin28.distinctProse - pin28.distinctProseNorm), 'the 08-28 folding gap', 'count runs {} ahead of the meaning-preserving one'],
  [String(pin27.distinctProse - pin27.distinctProseNorm), 'the 08-27 folding gap', 'on the first day and {} on the second'],
  [String(pin28.distinctJoint), 'Pinterest distinct full public descriptions', 'arrive in the record as {} distinguishable'],
  // The twins figure counts the statement itself, so the number of OTHERS is one fewer.
  // The readout says "itself included" and prints twins; the prose says "others" and prints
  // twins minus one. An earlier version printed the readout's number after the word others.
  [N(Math.round(twins(pin28) - 1)), 'Pinterest twins as others, one fewer than the readout', 'indistinguishable, on that record, from {} others'],
  [H(pin28.jointCounts).toFixed(2), 'Pinterest bits per statement', 'the record carries {} .'],
  [adds(eb).toFixed(3), 'the bits eBay adds, first mention', 'eBay adds {} , Pinterest'],
  [adds(eb).toFixed(3), 'the bits eBay adds, second mention', 'let eBay put {} bits there'],
  [adds(pin28).toFixed(3), 'the bits Pinterest adds', 'Pinterest {} , LinkedIn'],
  [adds(li).toFixed(3), 'the bits LinkedIn adds', 'Pinterest 1.931 , LinkedIn {} .'],
  [adds(fb).toFixed(3), 'the bits Facebook, Shopify and Fortnite add', 'Fortnite add {} bits'],
  [adds(fb).toFixed(3), 'the same figure where the mechanism is explained', 'why the number is {} and not merely small'],
  [adds(fn).toFixed(3), 'the figure that is forced on Fortnite', 'coded ones, where {} is forced'],
  [adds(fb).toFixed(3), 'the bits Facebook adds, in the vacuity note', 'holding two of them, and {} bits added'],
  [adds(fb).toFixed(3), 'what the flattening control must produce', 'must fall to exactly {} ,'],
  [adds(gh).toFixed(3), 'the bits GitHub adds, the lowest non-zero row', 'near-zero rows, at {} ,'],
  [adds(sc).toFixed(3), 'the bits SoundCloud adds', 'near-zero rows, at 0.003 , {} ,'],
  [adds(dc).toFixed(3), 'the bits Discord adds', 'near-zero rows, at 0.003 , 0.008 , {} ,'],
  [adds(rd27).toFixed(3), 'the bits Reddit adds on 08-27', '0.018 , {} and'],
  [adds(rd).toFixed(3), 'the bits Reddit adds on 08-28', '0.011 and {} bits'],
  [String(sn.distinctForm), 'Snapchat coded classes', 'its day into {} classes'],
  [String(sn.distinctProse), 'Snapchat free-text classes', 'separate it into {} , and'],
  [String(eb.distinctForm), 'eBay coded classes', 'the opposite, {} against 279'],
  [String(eb.distinctProse), 'eBay free-text classes', '87 against {} .'],
  [N(fb.n), 'Facebook statements', 'Its {} statements that day carry'],
  [String(fb.distinctProse), 'Facebook free-text classes', 'carry {} distinct free-text descriptions'],
  [String(fb.distinctForm), 'Facebook coded classes', 'across {} distinct coded ones'],
  [N(fb.n), 'Facebook statements, in the vacuity note', 'bits added over {} statements'],
  [String(fb.distinctProse), 'Facebook free-text classes, in the vacuity note', '{} free-text classes spread over'],
  [String(fb.distinctForm), 'Facebook coded classes, in the vacuity note', 'spread over {} coded ones'],
  [N(fb.prose[0][0]), 'Facebook largest free-text class', 'The largest of the seven, filed {} times'],
  [String(eb.distinctProse), 'eBay free-text classes, in the lookup-table note', "eBay's {} free-text classes cover"],
  [N(eb.n), 'eBay statements', 'classes cover {} statements'],
  [String(Math.round(eb.n / eb.distinctProse)), 'eBay statements per sentence', 'about {} statements per sentence'],
  [String(eb.maxFacts), 'the longest decision_facts eBay filed', 'that day is {} characters'],
  [N(gh.inversions), 'GitHub statements, all of them date-inverted', 'all {} of GitHub'],
  [String(dm.inversions), 'Dailymotion date inversions', 'Dailymotion has {} of'],
  [N(dm.n), 'Dailymotion statements', 'Dailymotion has 165 of {} ;'],
  [String(sn.inversions), 'Snapchat date inversions', 'Snapchat has {} of'],
  [N(sn.n), 'Snapchat statements', 'Snapchat has 1 of {} ;'],
  [String(classes.days.filter(r => r.inversions === 0).length), 'platform-days with no date inversion', 'the other {} platform-days have none'],
  [String(rd.placeholder), 'Reddit statements carrying an unfilled placeholder', '{} of Reddit'],
  [N(rd.n), 'Reddit statements', "Reddit's {} statements"],
  [String(rd27.placeholder), 'Reddit placeholders the day before', 'and {} of the 1,402'],
  [N(rd27.n), 'Reddit statements the day before', 'of the {} it filed the day before'],
  [String(fn.distinctForm), 'Fortnite coded classes', 'one prose class over {} coded ones'],
  [String(50), 'the rows the poison control plants', 'verifier plants {} synthetic rows'],
  [String(50), 'the rise it requires in the distinct count', 'distinct count to rise by exactly {} and'],
  [String(50), 'the rise it requires in the statement count', 'statement count to rise by exactly {} ;'],
  [String(classes.days.filter(r => r.date === '2026-08-28').length), 'archives from 2026-08-28', '{} from 2026-08-28 and, for Pinterest'],
  [String(L.fullyAutomatedPercent) + '%', 'the front page automation figure', 'reports {} fully automated decisions'],
  [(100 * pin28.automated.AUTOMATED_DECISION_PARTIALLY / pin28.n).toFixed(1) + '%', 'Pinterest PARTIALLY', "Pinterest's day is {} PARTIALLY"],
  [(100 * rd.automated.AUTOMATED_DECISION_FULLY / rd.n).toFixed(1) + '%', 'Reddit FULLY', "Reddit's is {} FULLY"],
  [String(Math.round(census.googleShopping.zipBytes / 1e6)), 'the weight of the Google Shopping archive in MB', 'Its {} MB daily archive'],
  [String(battery.caught), 'mutations the gate caught', 'it caught {} of 20'],
  [String(battery.mutations), 'mutations it was fed', 'it caught 20 of {} ,'],
  [N(L.readings[0].statementsOfReasons), 'the front page total, restated in the scale section', 'summary block: {} statements over the trailing'],
  [(L.readings[0].statementsOfReasons / (L.windowDays * 86400)).toFixed(1), 'the rate, restated in the scale section', '180 days that block covers is {} a second, averaged'],
  [N(L.readings[1].statementsOfReasons), 'the second reading, in the scale section', 'in between it rose to {} , or 242.7'],
  [String(S.decisionFactsCharLimit.toLocaleString('en-US')), 'the schema limit, where eBay is measured against it', '366 characters against the {} the schema allows'],
  [String(L.activePlatforms), 'the platform count, in the check panel', 'days are here. The {} per-platform counts on the listing'],
  [String(S.decisionFactsCharLimit.toLocaleString('en-US')), 'the room the schema gives for the facts', 'free-text field with {} characters to write in'],
  [N(classes.days.reduce((a2, r) => a2 + r.n, 0)), 'the statements counted in all seventeen archives', 'platform-days counted below, {} statements'],
  // 638 is the longest decision_facts, NOT the longest thing anybody filed. The longest
  // free-text value in these seventeen archives is more than twice that, in a different
  // column with a different cap, and the page prints both against their own caps.
  [String(Math.max(...classes.days.map(r => r.maxFacts))), 'the longest decision_facts anybody filed', 'longest decision_facts anybody filed is {} characters'],
  [N(Math.max(...classes.days.map(r => r.maxProseAny))), 'the longest free-text value of any column', 'filed in any of the five is {} characters of'],
  [N(PROSE_CAP[longestCol]), 'the schema cap on the column that longest value is in', 'which the schema caps at {} .'],
  [N(longestRow.maxProseRows), 'how many times the longest free-text value was filed', 'longest account was filed {} times, word for word'],
  [N(Math.max(...classes.days.map(r => r.medianFacts))), 'the largest of the seventeen median decision_facts lengths', 'median lengths, is {} .'],
  [(100 * classes.days.filter(r => r.date === '2026-08-28').reduce((a, r) => a + r.n, 0) / total28).toFixed(2) + '%',
   'the share of 2026-08-28 this page actually counted', 'covers {} of 2026-08-28 by volume'],
  [String(S.decisionFactsCharLimit.toLocaleString('en-US')), 'the schema limit, where 638 is measured against it', 'against a schema limit of {} ;'],
  [String(pin28.distinctProse), 'Pinterest exact prose count, in the honest edges', "free-text count goes from {} to 235"],
  [String(pin27.distinctProse), 'Pinterest 08-27 prose count, in the honest edges', 'goes from 206 to {} ,'],
  [String(both.length), 'the days the concentration figures are checked against', 'most recent {} days of the listing'],
  [String(L.windowDays), 'the window the front page summarises', 'over the trailing {} days that block covers'],
  [String(L.windowDays), 'the window a statement stays searchable', 'searchable for {} days and keeps'],
  [String(S.decisionFactsCharLimit.toLocaleString('en-US')), 'the room the schema gives, in the honest edges', 'circumstances in up to {} characters'],
];

const text = html
  .replace(/<script[\s\S]*?<\/script>/gi, ' ')
  .replace(/<style[\s\S]*?<\/style>/gi, ' ')
  .replace(/<!--[\s\S]*?-->/g, ' ')
  .replace(/<[^>]*>/g, ' ')
  .replace(/&middot;/g, '·').replace(/&nbsp;/g, ' ').replace(/&amp;/g, '&')
  .replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&#39;/g, "'")
  .replace(/\s+/g, ' ');
for (const [value, what, ctx] of EXPECT) {
  const phrase = ctx.replace('{}', value);
  ok('page shows ' + what + ' as ' + value, text.includes(phrase), { phrase });
}
// The guard that keeps the collision from coming back: no figure may be checked by its bare
// string, and no context may be so loose that it fails to pin the number to its own sentence.
ok('every checked figure is pinned to a phrase, not matched as a bare number',
   EXPECT.every(e => typeof e[2] === 'string' && e[2].includes('{}') && e[2].length > e[0].length + 8),
   EXPECT.filter(e => !(typeof e[2] === 'string' && e[2].includes('{}') && e[2].length > e[0].length + 8)).map(e => e[1]));
ok('no two checked figures share a context phrase',
   new Set(EXPECT.map(e => e[2].replace('{}', e[0]))).size === EXPECT.length,
   EXPECT.map(e => e[2].replace('{}', e[0])).filter((p, i, a) => a.indexOf(p) !== i));

// ── the sweep ──────────────────────────────────────────────────────────────────────────
// Everything above checks the figures the page MARKED UP as figures, which is a check that
// cannot fail on an unmarked number: an audit proved it by writing "has 39 columns", "the
// whole 48-day series" and "file 224238" into a scratch copy of this page and watching the
// gate stay green. So sweep every run of digits in the readable text instead, and require
// each one to be either a value this run recomputed or a member of a SHORT, NAMED list of
// things that are not measurements. Anything else is an unchecked number and fails.
//
// The list is deliberately narrow and each entry says what it exempts. Adding to it is the
// only way to put an unchecked number on this page, which is the point.
const computed = new Set(EXPECT.map(e => e[0]));

// Spans that are a date or a time rather than a quantity. Matched whole, so the tokenizer
// cannot split "2026-08-28" into three unexplained numbers.
const DATE_SPANS = [
  /\d{4}-\d{2}-\d{2}/g,
  /\d{2}:\d{2}/g,
  /\d{1,2}\s+(?:January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{4}/g,
];
const NOT_A_MEASUREMENT = [
  ['a numbered Article, paragraph or point of the Regulation', /^\d{1,2}$/,
   /(?:Article|Articles|paragraph|point)\s+[\d()a-z,\s]*$/],
  ['the Regulation\'s or a Recommendation\'s own number', /^(?:2022|2065|2003|361)$/,
   /(?:EU\)\s*|\/)$|Recommendation\s+$/],
  ['a CC BY licence version', /^4\.0$/, /CC BY\s+$/],
  ['the number of hours in a day', /^24$/, /same\s+$/],
  ['the calendar year the download window reaches', /^2031$/, /resolve into\s+$/],
];
/** Every digit run in `str` that nothing on this page accounts for. */
function unaccountedNumbers(str) {
  const covered = [];
  for (const re of DATE_SPANS) {
    for (const m of str.matchAll(re)) covered.push([m.index, m.index + m[0].length]);
  }
  const inDate = (a, b) => covered.some(([x, y]) => a >= x && b <= y);
  const out = [];
  // A thousands-separated figure, or a plain number, optionally decimal, optionally percent.
  // Written so a trailing comma of ordinary punctuation never lands inside the token.
  for (const m of str.matchAll(/\d{1,3}(?:,\d{3})+(?:\.\d+)?%?|\d+(?:\.\d+)?%?/g)) {
    const tok = m[0], a = m.index, b = a + tok.length;
    if (computed.has(tok)) continue;                       // a figure this run recomputed
    if (inDate(a, b)) continue;                            // part of a date or a clock time
    const before = str.slice(Math.max(0, a - 90), a);
    if (NOT_A_MEASUREMENT.some(([, tokRe, ctxRe]) => tokRe.test(tok) && ctxRe.test(before))) continue;
    out.push(tok + '  <<in>>  ...' + before.slice(-58) + '[' + tok + ']');
  }
  return out;
}
{
  const unaccounted = unaccountedNumbers(text);
  const swept = [...text.matchAll(/\d{1,3}(?:,\d{3})+(?:\.\d+)?%?|\d+(?:\.\d+)?%?/g)].length;
  ok('every run of digits in the readable text is either a figure this run computed or a '
     + 'named non-measurement (' + swept + ' swept)',
     unaccounted.length === 0, { unaccounted: unaccounted.slice(0, 40) });
  // The sweep must be able to go red, or it is decoration. Three plants, one of each shape
  // the audit actually used against the old gate.
  for (const [what, plant] of [
    ['a bare count written without the marker', ' the record has 9182736455 columns.'],
    ['a plausible near-miss of a real figure', " Shopify's 199,336 decisions that day."],
    ['a wrong file identifier', ' incompatible_content_explanation, Facebook, file 224238 .'],
  ]) {
    ok('the sweep catches ' + what,
       unaccountedNumbers(text + plant).length === unaccounted.length + 1,
       { withPlant: unaccountedNumbers(text + plant).length, without: unaccounted.length });
  }
}

const rendered = [...html.slice(html.indexOf('<body>')).replace(/<script[\s\S]*?<\/script>/gi, ' ')
  .matchAll(/<span class="num">([^<]*)<\/span>/g)].map(m => m[1]);
const orphans = [...new Set(rendered)].filter(v => !computed.has(v));
ok('every figure marked as a number in the page body is one this run computed ('
   + rendered.length + ' occurrences, ' + new Set(rendered).size + ' distinct)',
   orphans.length === 0, { orphans });
{
  // Which value is short a row: count page occurrences against EXPECT rows, per value, so a
  // failure names the figure instead of only the totals.
  const pageCount = {}, expCount = {};
  for (const v of rendered) pageCount[v] = (pageCount[v] || 0) + 1;
  for (const e of EXPECT) expCount[e[0]] = (expCount[e[0]] || 0) + 1;
  const unbalanced = [...new Set([...Object.keys(pageCount), ...Object.keys(expCount)])]
    .filter(v => (pageCount[v] || 0) !== (expCount[v] || 0))
    .map(v => v + ': ' + (pageCount[v] || 0) + ' in page, ' + (expCount[v] || 0) + ' checked');
  ok('every marked figure is checked in its own place, not merely somewhere on the page',
     rendered.length === EXPECT.length && unbalanced.length === 0,
     { markedInPage: rendered.length, checkedHere: EXPECT.length, unbalanced });
}

console.log('== 9b. the instrument itself, driven against a stub DOM ===========');
// node --check only proves the controller parses. This runs it: a minimal DOM stands in for
// the browser, the page's own controller block executes against the island it ships, and the
// numbers it WRITES INTO THE READOUTS are compared with numbers computed here. Every figure
// checked in section 9 is static text; these are the ones only a running page produces.
const ctlSrc = [...html.matchAll(/<script>([\s\S]*?)<\/script>/g)]
  .map(m => m[1]).find(t => t.includes("getElementById('dsa-data')"));
ok('the page\'s controller block was found in the served HTML', !!ctlSrc);
if (ctlSrc) {
  const made = new Map();
  const mkEl = (tag) => {
    const el = {
      tagName: tag, textContent: '', className: '', disabled: false, value: '',
      children: [], attrs: {}, handlers: {},
      appendChild(c) { this.children.push(c); return c; },
      setAttribute(k, v) { this.attrs[k] = String(v); },
      getAttribute(k) { return k in this.attrs ? this.attrs[k] : null; },
      addEventListener(t, f) { (this.handlers[t] = this.handlers[t] || []).push(f); },
      fire(t) { (this.handlers[t] || []).forEach(f => f.call(this, {})); },
      querySelectorAll(sel2) { return this.children.filter(c => c.tagName === sel2); },
    };
    return el;
  };
  const idsInMarkup = [...html.matchAll(/id="([^"]+)"/g)].map(m => m[1]);
  for (const id of idsInMarkup) made.set(id, mkEl(id === 'keyseg' ? 'fieldset' : 'div'));
  made.get('dsa-data').textContent = gotIsland;
  const seg = made.get('keyseg');
  for (const k of ['prose', 'form', 'joint']) {
    const b = mkEl('button');
    b.setAttribute('data-key', k);
    b.setAttribute('aria-pressed', k === 'prose' ? 'true' : 'false');
    seg.appendChild(b);
  }
  const doc = {
    getElementById: id => made.get(id) || null,
    createElement: mkEl,
    addEventListener() {},
  };
  const win = { matchMedia: () => ({ matches: false }), addEventListener() {}, AW: undefined };
  let ran = true, err = null;
  try {
    new Function('document', 'window', 'AW', 'DecompressionStream', ctlSrc)(doc, win, undefined, undefined);
  } catch (e) { ran = false; err = e; }
  ok('the controller runs to completion against a stub DOM', ran, err && String(err.stack).split('\n').slice(0, 3));

  if (ran) {
    const seln = made.get('pick');
    const readout = () => ({
      n: made.get('ro-n').textContent, d: made.get('ro-d').textContent,
      h: made.get('ro-h').textContent, t: made.get('ro-t').textContent,
      head: made.get('headline').textContent,
    });
    const pickIndex = (p, dt) => island.days.findIndex(x => x.p === p && x.d === dt);
    ok('the platform menu offers all seventeen platform-days',
       seln.children.length === 17, { options: seln.children.length });

    // Default selection. It must NOT be a platform-day where the three keys give the same
    // answer, because then the page's only comparative control does nothing on the reader's
    // first press. Shopify 2026-08-28 is exactly that day and used to be the default.
    const r0 = readout();
    ok('on load the instrument reads out Facebook\'s ' + N(fb.n) + ' statements and '
       + fb.distinctProse + ' free-text classes',
       r0.n === N(fb.n) && r0.d === String(fb.distinctProse), r0);
    ok('the default platform-day is NOT one where all three keys give the same answer',
       !(fb.distinctProse === fb.distinctForm && fb.distinctForm === fb.distinctJoint),
       { prose: fb.distinctProse, form: fb.distinctForm, joint: fb.distinctJoint });
    ok('and Shopify, the degenerate day, is the one this default was moved off',
       sh.distinctProse === 1 && sh.distinctForm === 1 && sh.distinctJoint === 1);
    ok('the fourth readout carries H(prose | form) for the selected day, to three places',
       made.get('ro-a').textContent === adds(fb).toFixed(3),
       { got: made.get('ro-a').textContent, want: adds(fb).toFixed(3) });

    // switch to Pinterest and to the joint key, and require the live numbers to move
    seln.value = String(pickIndex('Pinterest', '2026-08-28'));
    seln.fire('change');
    const rp = readout();
    ok('selecting Pinterest moves every readout to Pinterest\'s own numbers',
       rp.n === N(pin28.n) && rp.d === String(pin28.distinctProse)
       && rp.h === H(pin28.prose.map(x => x[0])).toFixed(2), rp);
    seg.children.find(b => b.getAttribute('data-key') === 'joint').fire('click');
    const rj = readout();
    ok('pressing "both" gives Pinterest\'s ' + pin28.distinctJoint
       + ' full public descriptions, ' + H(pin28.jointCounts).toFixed(2) + ' bits and '
       + N(Math.round(twins(pin28))) + ' twins',
       rj.d === String(pin28.distinctJoint) && rj.h === H(pin28.jointCounts).toFixed(2)
       && rj.t === N(Math.round(twins(pin28))), rj);
    ok('and the headline sentence names the advertised count as the Commission\'s',
       rj.head.includes(N(pin28.n)) && rj.head.includes(String(pin28.fileId)), { head: rj.head });
    seg.children.find(b => b.getAttribute('data-key') === 'form').fire('click');
    const rf = readout();
    ok('pressing "the form" gives Pinterest\'s ' + pin28.distinctForm + ' coded descriptions',
       rf.d === String(pin28.distinctForm), rf);
    ok('the three keys give three different answers on the same day',
       new Set([rp.d, rj.d, rf.d]).size === 3, { prose: rp.d, joint: rj.d, form: rf.d });

    // the depth-layer table
    const rows = made.get('gridbody').children;
    ok('the second-layer table has one row per platform-day', rows.length === 17, { rows: rows.length });
    const headerCells = (html.match(/<th scope="col">/g) || []).length;
    ok('every table row carries exactly as many cells as the table has headers ('
       + headerCells + ')', rows.every(r => r.children.length === headerCells),
       { headers: headerCells, widths: [...new Set(rows.map(r => r.children.length))] });
    const cellText = r => r.children.map(c => c.textContent);
    const ebRow = rows.find(r => cellText(r)[0] === 'eBay');
    ok('the table\'s top row is eBay at ' + adds(eb).toFixed(3) + ' bits added',
       cellText(rows[0])[0] === 'eBay' && cellText(rows[0]).at(-1) === adds(eb).toFixed(3),
       cellText(rows[0]));
    const ghRow = rows.find(r => cellText(r)[0] === 'GitHub');
    ok('the impossible-dates column carries GitHub\'s ' + N(gh.inversions),
       cellText(ghRow)[6] === N(gh.inversions), cellText(ghRow));
    const zeroRows = rows.filter(r => cellText(r).at(-1) === '0.000').map(r => cellText(r)[0]);
    ok('exactly three rows read 0.000 bits added, and they are Facebook, Shopify and Fortnite',
       zeroRows.length === 3 && ['Facebook', 'Shopify', 'Fortnite'].every(p => zeroRows.includes(p)),
       { zeroRows });
    ok('no row prints a negative number of bits added',
       rows.every(r => !cellText(r).at(-1).startsWith('-')),
       rows.map(r => cellText(r).at(-1)).filter(v => v.startsWith('-')));
    ok('the table\'s own summary sentence names the same three',
       ['Facebook', 'Shopify', 'Fortnite'].every(p => made.get('grid-summary').textContent.includes(p))
       && made.get('grid-summary').textContent.includes(adds(eb).toFixed(3)),
       { summary: made.get('grid-summary').textContent });
    // The summary's two tallies are generated by the page at run time, so they escape the
    // marked-figure sweep by construction and nothing used to check them.
    const summary = made.get('grid-summary').textContent;
    const flipA = classes.days.filter(r => r.distinctForm > r.distinctProse).length;
    const flipB = classes.days.filter(r => r.distinctProse > r.distinctForm).length;
    ok('the summary\'s "coded half is finer" tally is ' + flipA + ', recomputed here',
       summary.includes('On ' + flipA + ' of the 17 the coded half'), { summary, flipA });
    ok('the summary\'s "free text is finer" tally is ' + flipB + ', recomputed here',
       summary.includes('and on ' + flipB + ' it is the other way'), { summary, flipB });
    ok('the two tallies and the ties account for all seventeen platform-days',
       flipA + flipB + classes.days.filter(r => r.distinctForm === r.distinctProse).length === 17,
       { flipA, flipB });
    ok('the summary\'s range is the range of the column',
       summary.includes('between ' + Math.min(...classes.days.map(adds)).toFixed(3) + ' and '
                        + Math.max(...classes.days.map(adds)).toFixed(3)), { summary });
  }
}

console.log('== 9c. the refusal branches, which print words nobody had asserted =====');
// showLive() is the only place the page tells a reader what the Commission's checksum did.
// It used to be handed a two-valued sha1ok, so a browser with no crypto.subtle (any
// file:// open, any non-secure context) was told the checksum DID NOT MATCH when nothing
// had been hashed at all. Nothing exercised that branch. This drives the real function,
// lifted out of the served HTML, through all three states.
if (ctlSrc) {
  const at = ctlSrc.indexOf('function showLive(');
  let depth = 0, end = -1;
  for (let i = ctlSrc.indexOf('{', at); i < ctlSrc.length; i++) {
    if (ctlSrc[i] === '{') depth++;
    else if (ctlSrc[i] === '}') { depth--; if (depth === 0) { end = i + 1; break; } }
  }
  ok('showLive was found whole in the served HTML', at >= 0 && end > at);
  if (at >= 0 && end > at) {
    const src = ctlSrc.slice(at, end);
    const els = { rstat: { textContent: '', className: '' }, concur: { textContent: '' } };
    const stub = new Function('$', 'AW', 'fmt', src + '; return showLive;')(
      id => els[id],
      { concur: { render() {}, guard: (rec, k) => rec[k] } },
      x => x.toLocaleString('en-US'));
    const day = { adv: rd.n, dp: rd.distinctProse, df: rd.distinctForm, dj: rd.distinctJoint };
    const rec = { n: rd.n, distinctProse: rd.distinctProse, distinctForm: rd.distinctForm,
                  distinctJoint: rd.distinctJoint };
    const goodSha = 'a'.repeat(40);
    stub(day, { rec, sha1: goodSha, sha1ok: true });
    ok('with a matching checksum the panel says it matches',
       /matches the checksum/.test(els.rstat.textContent) && els.rstat.className === 'status ok',
       { text: els.rstat.textContent, cls: els.rstat.className });
    stub(day, { rec, sha1: goodSha, sha1ok: false });
    ok('with a failing checksum the panel says DOES NOT MATCH and goes bad',
       /DOES NOT MATCH/.test(els.rstat.textContent) && els.rstat.className === 'status bad',
       { text: els.rstat.textContent, cls: els.rstat.className });
    stub(day, { rec, sha1: null, sha1ok: null });
    ok('with NO checksum computed the panel says so and does NOT claim a failure',
       /was not computed/.test(els.rstat.textContent)
       && !/DOES NOT MATCH/.test(els.rstat.textContent)
       && !/\bmatches\b/.test(els.rstat.textContent)
       && els.rstat.className !== 'status ok',
       { text: els.rstat.textContent, cls: els.rstat.className });
    // and the disagreement refusal, which prints no recounted number at all
    const split = { n: [rd.n, rd.n + 1], distinctProse: rd.distinctProse,
                    distinctForm: rd.distinctForm, distinctJoint: rd.distinctJoint };
    const guardNull = { concur: { render() {}, guard: (r, k) => (Array.isArray(r[k]) ? null : r[k]) } };
    const stub2 = new Function('$', 'AW', 'fmt', src + '; return showLive;')(
      id => els[id], guardNull, x => x.toLocaleString('en-US'));
    stub2(day, { rec: split, sha1: goodSha, sha1ok: true });
    ok('when the two engines part, no recounted number is printed',
       /engines disagree/.test(els.rstat.textContent)
       && !els.rstat.textContent.includes(N(rd.n)), { text: els.rstat.textContent });

    // The three states above prove showLive HANDLES a three-valued sha1ok. They say nothing
    // about whether its CALLER ever produces the third value, and a mutation that put
    // `sha1ok = false` back into the caller sailed through them: a check on one half of a
    // contract. So drive the caller's own checksum block, lifted out of the same source.
    const ci = ctlSrc.indexOf('var sha1 =');
    const cj = ctlSrc.indexOf("$('rstat').textContent = 'Unzipping", ci);
    ok('the recount handler\'s checksum block was found in the served HTML', ci >= 0 && cj > ci);
    if (ci >= 0 && cj > ci) {
      const csum = ctlSrc.slice(ci, cj);
      const run = new Function('window', 'buf', 'd', 'hex',
        'return (async () => { ' + csum + ' return [sha1, sha1ok]; })();');
      const bytes = fs.readFileSync(path.join(PUBDATA, 'sor-linkedin-2026-08-28-full.zip'));
      const ab = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
      const hexOf = b => Buffer.from(b).toString('hex');
      const real = classes.days.find(r => r.zip === 'sor-linkedin-2026-08-28-full.zip').sha1;
      const [s1, ok1] = await run({ crypto: globalThis.crypto }, ab, { sha1: real }, hexOf);
      ok('with crypto.subtle present the handler computes the real SHA-1 and matches it',
         s1 === real && ok1 === true, { got: s1, want: real, ok: ok1 });
      const [s2, ok2] = await run({ crypto: globalThis.crypto }, ab, { sha1: 'f'.repeat(40) }, hexOf);
      ok('and reports a mismatch as false when the checksum really does not match',
         ok2 === false && s2 === real);
      const [s3, ok3] = await run({ crypto: undefined }, ab, { sha1: real }, hexOf);
      ok('with NO crypto.subtle the handler leaves the checksum UNCOMPUTED, not failed: '
         + 'sha1ok must not be false here, or the page prints a failure that never ran',
         ok3 !== false && s3 !== '' && (ok3 === null || ok3 === undefined),
         { sha1: s3, sha1ok: ok3 });
    }
  }
}

console.log('== 10. the served surface: head, structure, and the closing tags ======');
const ld = html.match(/<script type="application\/ld\+json">([\s\S]*?)<\/script>/);
ok('the split the page describes is the split the code uses: 38 columns, 20 coded, 5 prose',
   S.columns === 38 && FORM.length === 20 && PROSE.length === 5,
   { columns: S.columns, form: FORM.length, prose: PROSE.length });
ok('the JSON-LD block is present', !!ld);
let LD = null;
try { LD = JSON.parse(ld[1]); } catch (e) { /* reported */ }
ok('the JSON-LD parses and carries the Article fields',
   !!LD && LD['@type'] === 'Article' && !!LD.headline && !!LD.description
   && LD.url === 'https://artwaste.land/strata/' + SLUG + '/',
   LD && { type: LD['@type'], url: LD.url });
ok('the JSON-LD is dated 2026-08-30',
   !!LD && LD.datePublished === '2026-08-30' && LD.dateModified === '2026-08-30');
for (const [label, re] of [
  ['canonical link', /<link rel="canonical" href="https:\/\/artwaste\.land\/strata\/two-hundred-and-forty-reasons-a-second\/"/],
  ['meta description', /<meta name="description" content="/],
  ['self-hosted fonts', /<link rel="stylesheet" href="\/fonts\/fonts\.css"/],
  ['the site banner script', /<script src="\/media\/home-banner\.js" defer><\/script>/],
  ['a reduced-motion guard in its stylesheet', /@media \(prefers-reduced-motion: reduce\)\s*\{/],
  ['a wide table inside its own horizontal scroller', /\.tablewrap\s*\{[^}]*overflow-x:\s*auto/],
  ['a bounded main column', /main\s*\{[^}]*max-width:\s*4[4-8]rem/],
  ['a focus-visible rule', /:focus-visible/],
  ['a live region on the readout', /role="status" aria-live="polite"/],
]) ok('the page carries a ' + label, re.test(html));
ok('the page ends with the load-bearing closing tags',
   /<\/script>\s*<\/body>\s*<\/html>/.test(html));

// ── horizontal overflow, measured rather than eyeballed ────────────────────────────────
// At a 390px viewport the main column is 390 - 2 * 1.15rem = about 353px. A token with no
// break opportunity that is wider than that pushes the whole page sideways, and an audit
// found one: a 61-character file path in .mono, in a footer list item. Estimating whether a
// given token fits needs font metrics this gate does not have, so the property checked here
// is the structural one that makes the question moot: EVERY element holding a token long
// enough to be a risk must be covered by a rule that allows a break inside a word. Both
// halves are checked, the markup and the stylesheet, so adding a long token in a new place
// fails, and so does deleting the rule that lets it wrap.
{
  // Comments first: a /* ... */ before a rule otherwise lands inside the selector text and
  // silently disqualifies the rule, which is how .mono came back "uncovered" while carrying
  // the very declaration this looks for.
  const styleBlock = ((html.match(/<style>([\s\S]*?)<\/style>/) || [])[1] || '')
    .replace(/\/\*[\s\S]*?\*\//g, ' ');
  // Descendant selectors only, which is all this stylesheet uses for these rules. Each is
  // kept as its full chain, so "blockquote p" does NOT make every paragraph breakable: an
  // earlier version of this check took the last simple selector and did exactly that, and
  // its own red-on-purpose test caught it.
  const chains = [];
  for (const m of styleBlock.matchAll(/([^{}]+)\{([^}]*)\}/g)) {
    if (!/overflow-wrap\s*:\s*(anywhere|break-word)|word-break\s*:\s*break-all/.test(m[2])) continue;
    for (const sel of m[1].split(',')) {
      const parts = sel.trim().split(/\s+/).filter(x => x && !/^[>+~]$/.test(x));
      if (!parts.length || parts.some(x => /[>+~:[]/.test(x))) continue;
      chains.push(parts.map(x => {
        const tag = (x.match(/^[a-zA-Z0-9]+/) || [''])[0].toLowerCase();
        return { tag, classes: [...x.matchAll(/\.([A-Za-z0-9_-]+)/g)].map(c => c[1]) };
      }));
    }
  }
  ok('the stylesheet carries rules that allow a break inside a word, read as whole chains',
     chains.length >= 3, { chains: chains.map(c => c.map(x => x.tag + x.classes.map(y => '.' + y).join('')).join(' ')) });
  const matchesSimple = (el, sim) =>
    (!sim.tag || sim.tag === el.tag) && sim.classes.every(c => el.classes.includes(c));
  /** Does any breaking rule match this element, given its ancestor stack? */
  const breaks = (stack) => chains.some(chain => {
    if (!stack.length || !matchesSimple(stack[stack.length - 1], chain[chain.length - 1])) return false;
    let si = stack.length - 2;
    for (let ci = chain.length - 2; ci >= 0; ci--) {
      while (si >= 0 && !matchesSimple(stack[si], chain[ci])) si--;
      if (si < 0) return false;
      si--;
    }
    return true;
  });
  const LONG = 25;                      // characters; ~250px of Martian Mono at .9rem
  /** Long tokens in `frag` that sit where no rule lets them break. */
  const longUncovered = (frag) => {
    const stack = [], out = [];
    for (const m of frag.matchAll(/<(\/?)([a-zA-Z0-9]+)([^>]*)>|([^<]+)/g)) {
      if (m[4] !== undefined) {
        for (const tok of m[4].replace(/&[a-z#0-9]+;/gi, 'x').split(/\s+/)) {
          if (tok.length <= LONG || breaks(stack)) continue;
          out.push({ token: tok.slice(0, 50),
                     inside: stack.map(e => e.tag + e.classes.map(c => '.' + c).join('')).slice(-3).join(' ') });
        }
      } else if (m[1] === '/') stack.pop();
      else if (!/^(br|img|meta|link|input|hr|source)$/i.test(m[2])) {
        const cm = /class="([^"]*)"/.exec(m[3]);
        stack.push({ tag: m[2].toLowerCase(), classes: cm ? cm[1].split(/\s+/).filter(Boolean) : [] });
      }
    }
    return out;
  };
  const bodyHtml = html.slice(html.indexOf('<body>')).replace(/<script[\s\S]*?<\/script>/gi, ' ');
  const uncovered = longUncovered(bodyHtml);
  ok('every word longer than ' + LONG + ' characters sits inside an element the stylesheet '
     + 'lets break, so nothing can push the page sideways at 390px',
     uncovered.length === 0, { uncovered: uncovered.slice(0, 12) });
  // and it must go red: the audit's own token, in the one paragraph that is a plain <p>
  const plantedOne = bodyHtml.replace('<p class="dek">',
    '<p class="dek">research/two-hundred-and-forty-reasons-a-second/manifest.json ');
  ok('and it goes red on an unbreakable path planted in a plain paragraph',
     longUncovered(plantedOne).length === uncovered.length + 1,
     { withPlant: longUncovered(plantedOne).length, without: uncovered.length });
  // and it must NOT go red when the same token is planted where a rule does let it break
  const plantedTwo = bodyHtml.replace('<span class="mono">decision_facts</span>',
    '<span class="mono">research/two-hundred-and-forty-reasons-a-second/manifest.json</span>');
  ok('and it stays green when the same token is planted inside .mono, which can break',
     longUncovered(plantedTwo).length === uncovered.length,
     { withPlant: longUncovered(plantedTwo).length, without: uncovered.length });
}

console.log('== 11. the frontmatter, and the words on the shareable surface =======');
if (!fs.existsSync(MD)) { ok('src/content/strata/' + SLUG + '.md exists', false); }
else {
  const md = fs.readFileSync(MD, 'utf8');
  const fmBlock = md.split('---')[1] || '';
  const field = k => (fmBlock.match(new RegExp('^' + k + ': "([\\s\\S]*?)"$', 'm')) || [])[1];
  ok('frontmatter is dated 2026-08-30', /^date: 2026-08-30$/m.test(fmBlock));
  ok('frontmatter is not a draft', /^draft: false$/m.test(fmBlock));
  ok('frontmatter carries the closed-vocabulary seam "commons"', /^seam: "commons"$/m.test(fmBlock));
  ok('frontmatter href points at this page', /^href: "\/strata\/two-hundred-and-forty-reasons-a-second\/"$/m.test(fmBlock));
  ok('frontmatter is tagged immersive and interactive',
     /"immersive"/.test(fmBlock) && /"interactive"/.test(fmBlock));
  const plain = field('plain');
  ok('the plain placard is one double-quoted line of 90 to 420 characters',
     !!plain && plain.length >= 90 && plain.length <= 420, plain && { length: plain.length });
  ok('the plain placard is at most three sentences and carries no raw quote or em dash',
     !!plain && (plain.match(/[.!?](\s|$)/g) || []).length <= 3
     && !plain.includes('"') && !plain.includes('\u2014'),
     plain && { sentences: (plain.match(/[.!?](\s|$)/g) || []).length });
  const relates = [...fmBlock.matchAll(/^\s*-\s*to:\s*"([^"]+)"/gm)].map(m => m[1]);
  ok('there are 2 to 4 relates edges', relates.length >= 2 && relates.length <= 4, { relates });
  for (const r of relates) {
    ok('relates target exists on disk: ' + r,
       fs.existsSync(path.join(ROOT, 'src', 'content', 'strata', r + '.md')));
  }
  ok('every relates edge is an object with a note',
     relates.length === [...fmBlock.matchAll(/^\s*note:\s*"/gm)].length, { relates: relates.length });
  // ── the shareable surface ────────────────────────────────────────────────────────
  // The dek, the placard, the SEO fields, the social cards and the structured-data block
  // travel WITHOUT the page: they are what a link preview, a search result and the corpus
  // map show. Only three dek figures were ever checked here, and an audit proved the hole
  // by writing "938 characters", "2,999,999 statements", "Facebook's 803,293 carry nine"
  // and a JSON-LD "Pinterest's 596,796 carry 306" onto this surface with the gate green.
  // Every one of them now goes through the same sweep the page body gets.
  const attr = (re) => (html.match(re) || [])[1] || '';
  const LDo = LD || {};
  const surfaces = [
    ['the frontmatter dek', field('dek') || ''],
    ['the plain placard', field('plain') || ''],
    ['the frontmatter seoTitle', field('seoTitle') || ''],
    ['the frontmatter seoDescription', field('seoDescription') || ''],
    ['the page title element', (html.match(/<title>([\s\S]*?)<\/title>/) || [])[1] || ''],
    ['the meta description', attr(/<meta name="description" content="([^"]*)"/)],
    ['the og:title', attr(/<meta property="og:title" content="([^"]*)"/)],
    ['the og:description', attr(/<meta property="og:description" content="([^"]*)"/)],
    ['the og:image:alt', attr(/<meta property="og:image:alt" content="([^"]*)"/)],
    ['the twitter:title', attr(/<meta name="twitter:title" content="([^"]*)"/)],
    ['the twitter:description', attr(/<meta name="twitter:description" content="([^"]*)"/)],
    ['the JSON-LD name', LDo.name || ''],
    ['the JSON-LD headline', LDo.headline || ''],
    ['the JSON-LD description', LDo.description || ''],
  ];
  for (const [what, str] of surfaces) {
    ok(what + ' exists', str.length > 0);
    const bad = unaccountedNumbers(str);
    ok('every number on ' + what + ' is one this run computed or a named non-measurement',
       bad.length === 0, { bad, str });
  }
  // and the sweep of this surface must be able to fail, in the shape the audit used
  for (const [what, plant] of [
    ['a wrong distinct count', " Pinterest's 596,796 carry 306."],
    ['an invented character figure', ' the longest anybody filed is 938 characters.'],
    ['a rounded-off total', ' 2,999,999 statements in all.'],
  ]) {
    ok('the shareable-surface sweep catches ' + what,
       unaccountedNumbers((field('dek') || '') + plant).length
       === unaccountedNumbers(field('dek') || '').length + 1);
  }
  // Figures the surface states IN WORDS. The digit sweep is blind to these by construction,
  // so "Facebook's 803,293 carry seven" could become "carry nine" with every check green.
  // Each one is a computed quantity spelled out, and is required to be the right word.
  const WORD = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
    'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen',
    'seventeen', 'eighteen', 'nineteen', 'twenty'];
  const WORD_FIGURES = [
    [fb.distinctProse, "Facebook's free-text classes, spelled out in the dek", /Facebook's 803,293 carry (\w+)/],
    [classes.days.length, 'the number of platform-days, spelled out', /(\w+) whole platform-days were re-derived/],
    [classes.days.filter(r => r.shipped).length, 'the archives that ship, spelled out', /(\w+) of the seventeen archives ship beside the page/],
    [classes.days.length, 'the number of platform-days, in the seventeen-archives clause', /Six of the (\w+) archives ship/],
    [classes.days.filter(r => r.shipped).length, 'the archives that ship, spelled out in the placard', /(\w+) of the seventeen days are recounted/],
    [classes.days.length, 'the number of platform-days, in the placard', /of the (\w+) days are recounted/],
  ];
  for (const [value, what, re] of WORD_FIGURES) {
    const hit = surfaces.map(([, str]) => str.match(re)).find(Boolean);
    ok(what + ' is stated on the shareable surface and reads "' + WORD[value] + '"',
       !!hit && hit[1].toLowerCase() === WORD[value],
       { found: hit && hit[1], want: WORD[value], value });
  }
  // the surface must also not have gone SILENT on the figures that carry it
  const dek = field('dek') || '';
  for (const [v, what] of [[N(sh.n), 'Shopify\'s statement count'],
                           [N(pin28.n), 'Pinterest\'s statement count'],
                           [String(pin28.distinctProse), 'Pinterest\'s free-text count'],
                           [N(classes.days.reduce((a, r) => a + r.n, 0)), 'the statements counted'],
                           [String(Math.max(...classes.days.map(r => r.maxFacts))), 'the longest decision_facts']]) {
    ok('the dek still carries ' + what + ', ' + v, dek.includes(v), { dek });
  }
  // the 638 wording is the one that was wrong on every surface at once: it is the longest
  // decision_facts, not the longest thing filed, and both the dek and the structured data
  // said the latter. Require the qualifier wherever the number appears.
  for (const [what, str] of surfaces) {
    const n638 = String(Math.max(...classes.days.map(r => r.maxFacts)));
    if (!str.includes(n638)) continue;
    ok(what + ' names decision_facts wherever it prints ' + n638,
       /decision_facts[^.]*\b638\b|\b638\b[^.]*decision_facts/.test(str), { str });
  }
  ok('no em dash in the frontmatter', !md.includes('\u2014'));
}
// Figures the page states IN WORDS, which the digit sweep cannot see by construction. The
// sentence "Fifteen deliberate corruptions ... it caught 20 of 20" sat on this page with
// every other check green, because one half of it was digits and the other half was English.
{
  const WORDS = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
    'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen',
    'seventeen', 'eighteen', 'nineteen', 'twenty'];
  const BODY_WORD_FIGURES = [
    [battery.mutations, 'the corruptions fed to the gate', /(\w+)\s+deliberate corruptions of the finished files/],
    [classes.days.filter(r => r.distinctProse === r.distinctProseNorm).length,
     'the days on which the exact and folded prose counts agree', /On (\w+) of the seventeen days the two agree exactly/],
    [classes.days.length, 'the platform-days, in the folding sentence', /On fifteen of the (\w+) days the two agree/],
    [classes.days.filter(r => r.shipped).length, 'the archives that ship, in the run-it paragraph', /(\w+) of the seventeen archives ship with this page/],
    [classes.days.length - classes.days.filter(r => r.shipped).length,
     'the archives that do not ship', /the other (\w+) are re-derived if you download them/],
    [FORM.length, 'the coded columns', /(\w+) of them are coded: the Commission's schema/],
    [FORM.length, 'the coded columns, where Snapchat is described', /Snapchat's (\w+) coded columns separate its day/],
    [PROSE.length, 'the free-text columns', /(\w+) of them are free text, and those are the ones/],
    [S.columns - FORM.length - PROSE.length, 'the remaining columns', /None of the (\w+) is an explanation/],
    [fb.distinctProse, "Facebook's free-text classes, where the largest is quoted", /The largest of the (\w+), filed 366,522 times/],
    [Math.round(Math.log2(pin28.n)), 'the bits it would take to address one Pinterest statement',
     /(\w+) bits of address would be needed/],
    [classes.days.filter(r => r.shipped).length, 'the archives that ship, in the recount panel', /(\w+) of these days ship the original zip archive/],
  ];
  for (const [value, what, re] of BODY_WORD_FIGURES) {
    const hit = text.match(re);
    ok('the page states ' + what + ' in words, and it reads "' + WORDS[value] + '"',
       !!hit && hit[1].toLowerCase() === WORDS[value],
       { found: hit && hit[1], want: WORDS[value], value });
  }
}

ok('the page discloses that a fresh checkout cannot re-derive the counts for the archives '
   + 'it does not ship, rather than implying all seventeen are checked every run',
   /cannot do is re-derive the[\s\S]{0,120}counts[\s\S]{0,400}rest on the derivation until you download/.test(
     html.replace(/<[^>]*>/g, ' ').replace(/\s+/g, ' ')),
   { });
ok('no em dash in the page', !html.includes('\u2014'));
const readme = path.join(HERE, 'README.md');
ok('the research README exists', fs.existsSync(readme));
if (fs.existsSync(readme)) ok('no em dash in the README', !fs.readFileSync(readme, 'utf8').includes('\u2014'));

console.log('');
console.log('re-derived ' + anchored + ' of ' + manifest.days.length + ' archives from the Commission\'s raw bytes.');
const notDerived = manifest.days.filter(d => !derived.has(d.zip));
if (notDerived.length) {
  console.log('the distinct counts for these ' + notDerived.length + ' were NOT re-derived in '
    + 'this run and rest on classes.json (their published SHA-1, byte length, advertised count');
  console.log('and file id WERE checked against manifest.json): '
    + notDerived.map(d => d.platform + ' ' + d.date).join(', '));
  console.log('pass --raw <dir> with the archives to re-derive them.');
}
console.log(pass + ' pass · ' + fail + ' fail · ' + skipped + ' SKIPPED');
if (fail) { console.log('failures: ' + failures.join(' | ')); process.exit(1); }
process.exit(0);
