Files
jason.woltje 4ff4f44c98 lead: commit researcher's untracked evidence (1406 files: queue-101 queue-105 queue-128 queue-134 queue-143 queue-148 queue-183 queue-228)
Unclaimed files left in the canonical checkout. Inventory: canonical-cleanup-inventory.md (commit-as-evidence groups). detect-secrets scan clean on 2026-11-02: hits are sha256 pins, git object ids, test sentinels already in tracked tests and unit-file lines.
2026-11-02 15:05:14 -06:00

158 lines
11 KiB
JavaScript

#!/usr/bin/env node
// Row 148 round 3 (sage-0606): a generated probe for the Markdown preview, so no shape is hand-picked.
//
// node probe-gen.mjs run --module <markdown-preview.js> --out <dir> [--sizes 64,1024] [--timeout 20]
// [--abort-after 6] [--refine-max 40] [--filter <opener substring>]
//
// Every text is opener + filler repeated up to the size + terminator + "x" (no tail when the
// terminator is none), over the full cross product of
// openers every block marker, plus the link, image, emphasis and code openers;
// fillers letters, spaces, tabs, backticks, the opener repeated, the opener repeated with a blank;
// terminators none, LF, CR, U+2028, U+2029, NUL;
// at each size in KiB (characters). One child process per cell, one at a time, each with its own
// timeout (SIGKILL). A cell times one render() on the real module in a bare context with a stub
// document; it never touches a browser, the network or the repository.
//
// Verdict per opener family and per cell: ratio per doubling = (t_big / t_small) ^ (1 / doublings).
// Linear text is x2.0; the sage-0606 limit is x2.2, and any cell above it is flagged. A flagged cell
// that did not time out is measured again at 128, 256 and 512 KiB so a noisy small time can be told
// from real growth; the table shows both the strict flag and the refined step ratios.
//
// The source stays ASCII: the separator characters are made with String.fromCharCode.
import { spawnSync } from 'node:child_process';
import { appendFileSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { runInNewContext } from 'node:vm';
import { fileURLToPath } from 'node:url';
import { resolve } from 'node:path';
const OPENERS = [
'```', '~~~', '#', '-', '+', '*', '1.', '1)', '>', '=', '---', '***', '___', // block markers
'[', '![', '[a](', '[a](<', '**', '_', '`', '``', // link, image, emphasis, code
];
// `marker` repeats the opener as it is; `spaced` repeats it with one blank after it ("- - - ", "*a *a ").
const FILLERS = { letters: 'abcdefghij', spaces: ' ', tabs: '\t', backticks: '`', marker: null, spaced: null };
const TERMS = {
none: '', LF: '\n', CR: '\r',
U2028: String.fromCharCode(0x2028), U2029: String.fromCharCode(0x2029), NUL: String.fromCharCode(0),
};
const THRESHOLD = 2.2;
function build(opener, filler, term, chars) {
const f = filler === 'marker' ? opener : filler === 'spaced' ? opener + ' ' : FILLERS[filler];
const room = Math.max(0, chars - opener.length);
return opener + f.repeat(Math.ceil(room / f.length)).slice(0, room) + (term === 'none' ? '' : TERMS[term] + 'x');
}
// ---- cell: one render in this process -------------------------------------------------------------
function cell(modulePath, opener, filler, term, kib) {
const src = readFileSync(modulePath, 'utf8');
const window = {}; runInNewContext(src, { window });
class N { constructor() { this.c = []; } appendChild(x) { this.c.push(x); return x; } setAttribute() {} set textContent(v) {} get lastChild() { return this.c.at(-1); } }
const doc = { createElement: () => new N(), createTextNode: t => ({ t }), createDocumentFragment: () => new N() };
const text = build(opener, filler, term, kib * 1024);
const times = []; let meter = {}, limit = 5;
for (let r = 0; r < limit; r++) {
meter = {};
const t0 = performance.now();
window.MosaicMarkdown.render(text, doc, meter);
times.push(performance.now() - t0);
if (r === 0) limit = times[0] < 200 ? 5 : times[0] < 2000 ? 2 : 1;
}
times.sort((a, b) => a - b);
console.log(JSON.stringify({ ms: +times[times.length >> 1].toFixed(3), reps: times.length, length: text.length, work: meter.work, budget: meter.budget }));
}
// ---- driver ---------------------------------------------------------------------------------------
const args = process.argv.slice(2);
if (args[0] === 'cell') {
const [, modulePath, opener, filler, term, kib] = args;
cell(modulePath, opener, filler, term, Number(kib));
process.exit(0);
}
if (args[0] !== 'run') { console.error('usage: probe-gen.mjs run --module <file> --out <dir> [--sizes 64,1024] [--timeout 20] [--abort-after 6] [--refine-max 40] [--filter s]'); process.exit(2); }
const opt = (name, dflt) => { const i = args.indexOf('--' + name); return i < 0 ? dflt : args[i + 1]; };
const MODULE = resolve(opt('module')), OUT = resolve(opt('out', '.'));
const SIZES = opt('sizes', '64,1024').split(',').map(Number).sort((a, b) => a - b);
const TIMEOUT = Number(opt('timeout', '20')), ABORT_AFTER = Number(opt('abort-after', '6')), REFINE_MAX = Number(opt('refine-max', '40'));
const FILTER = opt('filter', '');
const SELF = fileURLToPath(import.meta.url);
mkdirSync(OUT, { recursive: true });
const LOG = resolve(OUT, 'cells.jsonl');
writeFileSync(LOG, '');
let timeouts = 0, aborted = false;
function measure(opener, filler, term, kib) {
if (aborted) return { status: 'not-run' };
const t0 = Date.now();
const r = spawnSync(process.execPath, [SELF, 'cell', MODULE, opener, filler, term, String(kib)], { encoding: 'utf8', timeout: TIMEOUT * 1000, killSignal: 'SIGKILL', maxBuffer: 1 << 20 });
let rec;
if (r.error && r.error.code === 'ETIMEDOUT') { timeouts++; rec = { status: 'timeout', ms: TIMEOUT * 1000 }; }
else if (r.status !== 0) rec = { status: 'error', code: r.status, signal: r.signal, stderr: (r.stderr || '').slice(0, 200) };
else rec = { status: 'ok', ...JSON.parse(r.stdout) };
rec = { opener, filler, term, kib, wall_ms: Date.now() - t0, ...rec };
appendFileSync(LOG, JSON.stringify(rec) + '\n');
if (timeouts >= ABORT_AFTER) aborted = true;
return rec;
}
const cells = [];
for (const opener of OPENERS) if (opener.includes(FILTER)) for (const filler of Object.keys(FILLERS)) for (const term of Object.keys(TERMS)) cells.push({ opener, filler, term, by: {} });
console.log(`${cells.length} cells x sizes ${SIZES.join(', ')} KiB, timeout ${TIMEOUT} s per process, abort after ${ABORT_AFTER} timeouts`);
// Smallest size first for every cell; the larger sizes run worst-first, so an abort loses the least.
const small = SIZES[0];
for (const c of cells) c.by[small] = measure(c.opener, c.filler, c.term, small);
for (const size of SIZES.slice(1)) {
const order = [...cells].sort((a, b) => (b.by[small].ms ?? 0) - (a.by[small].ms ?? 0));
for (const c of order) c.by[size] = c.by[small].status === 'ok' ? measure(c.opener, c.filler, c.term, size) : { status: 'skipped' };
}
const big = SIZES.at(-1), doublings = Math.log2(big / small);
for (const c of cells) {
const a = c.by[small], b = c.by[big];
if (a.status === 'ok' && b.status === 'ok') c.ratio = (b.ms / Math.max(a.ms, 0.001)) ** (1 / doublings);
else if (a.status === 'ok' && b.status === 'timeout') c.ratio = (b.ms / Math.max(a.ms, 0.001)) ** (1 / doublings); // a lower bound
c.flag = c.ratio !== undefined && c.ratio > THRESHOLD;
c.bound = b.status === 'timeout';
}
// Refine: flagged cells that finished get the sizes between, doubling by doubling.
const steps = [];
for (let s = small * 2; s < big; s *= 2) steps.push(s);
let refined = 0;
for (const c of cells.filter(x => x.flag && !x.bound).sort((x, y) => y.ratio - x.ratio)) {
if (refined++ >= REFINE_MAX) break;
for (const s of steps) c.by[s] = measure(c.opener, c.filler, c.term, s);
const chain = [small, ...steps, big].map(s => c.by[s]);
if (chain.every(x => x.status === 'ok')) c.chain = chain.slice(1).map((x, i) => +(x.ms / Math.max(chain[i].ms, 0.001)).toFixed(2));
}
// ---- table ----------------------------------------------------------------------------------------
const f1 = x => x === undefined ? '-' : x >= 100 ? x.toFixed(0) : x.toFixed(1);
const lines = [];
const ok = cells.filter(c => c.ratio !== undefined);
lines.push(`Generated probe, module ${MODULE}`, '', `${cells.length} cells (${OPENERS.filter(o => o.includes(FILTER)).length} openers x ${Object.keys(FILLERS).length} fillers x ${Object.keys(TERMS).length} terminators), sizes ${SIZES.join(', ')} KiB, one process per cell, timeout ${TIMEOUT} s.`);
lines.push(`Ratio per doubling = (t_${big}KiB / t_${small}KiB)^(1/${doublings}); linear is 2.0, the limit is ${THRESHOLD}. Timeouts: ${timeouts}${aborted ? ' (ABORTED: later cells not run)' : ''}. Cells measured at both ends: ${ok.length}.`, '');
const worstRatio = [...ok].sort((a, b) => b.ratio - a.ratio)[0], worstTime = [...cells].filter(c => c.by[big]?.ms !== undefined).sort((a, b) => b.by[big].ms - a.by[big].ms)[0];
const row = c => `${JSON.stringify(c.opener)} + ${c.filler} + ${c.term}: ${f1(c.by[small].ms)} ms at ${small} KiB, ${c.by[big].status === 'timeout' ? '>' + f1(c.by[big].ms) + ' (timeout)' : f1(c.by[big].ms)} ms at ${big} KiB, x${c.ratio.toFixed(2)}${c.bound ? '+' : ''} per doubling`;
if (worstRatio) lines.push(`Worst ratio: ${row(worstRatio)}`);
if (worstTime) lines.push(`Slowest at ${big} KiB: ${row(worstTime)}`);
lines.push('', `| opener | worst cell (filler + terminator) | ms at ${small} KiB | ms at ${big} KiB | ratio per doubling | slowest cell at ${big} KiB (ms) | flagged cells |`, '|---|---|---|---|---|---|---|');
for (const opener of OPENERS.filter(o => o.includes(FILTER))) {
const fam = ok.filter(c => c.opener === opener);
if (!fam.length) { lines.push(`| \`${opener}\` | no cell measured | | | | | |`); continue; }
const w = [...fam].sort((a, b) => b.ratio - a.ratio)[0], slow = [...fam].sort((a, b) => b.by[big].ms - a.by[big].ms)[0];
lines.push(`| \`${opener}\` | ${w.filler} + ${w.term} | ${f1(w.by[small].ms)} | ${w.bound ? '>' : ''}${f1(w.by[big].ms)} | x${w.ratio.toFixed(2)}${w.bound ? '+' : ''} | ${f1(slow.by[big].ms)} (${slow.filler} + ${slow.term}) | ${fam.filter(c => c.flag).length} of ${fam.length} |`);
}
const flagged = cells.filter(c => c.flag);
lines.push('', `Flagged (ratio above ${THRESHOLD}): ${flagged.length}`);
if (flagged.length) {
lines.push('', `| cell | ms at ${small} KiB | ms at ${big} KiB | ratio | step ratios ${[small, ...steps, big].join(' > ')} KiB |`, '|---|---|---|---|---|');
for (const c of flagged.sort((a, b) => b.ratio - a.ratio)) lines.push(`| ${JSON.stringify(c.opener)} + ${c.filler} + ${c.term} | ${f1(c.by[small].ms)} | ${c.bound ? '>' : ''}${f1(c.by[big].ms)} | x${c.ratio.toFixed(2)}${c.bound ? '+' : ''} | ${c.chain ? c.chain.map(x => 'x' + x).join(' ') : c.bound ? 'timed out' : 'not refined'} |`);
}
const bad = cells.filter(c => Object.values(c.by).some(b => b.status === 'error'));
if (bad.length) { lines.push('', `Cells whose process failed: ${bad.length}`); for (const c of bad.slice(0, 20)) lines.push(`- ${JSON.stringify(c.opener)} + ${c.filler} + ${c.term}: ${JSON.stringify(Object.values(c.by).find(b => b.status === 'error'))}`); }
writeFileSync(resolve(OUT, 'table.md'), lines.join('\n') + '\n');
console.log(lines.join('\n'));
process.exit(aborted || flagged.length || bad.length ? 1 : 0);