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

118 lines
8.3 KiB
JavaScript

// Row 148 round 3 (sage-0619): Mos's rule for parser diffs, replacing the x2.2 rule. Run on a chosen
// set of cells from a probe-gen.mjs run, at 1, 2 and 4 MiB, one process per measurement.
//
// A cell FAILS if any of:
// ceiling more than 1000 ms at 1 MiB
// growth ratio per doubling over 1 to 4 MiB above x3, i.e. (t4MiB / t1MiB)^(1/2) > 3
// steps two consecutive steps of x3.5 or more (1 to 2 MiB and 2 to 4 MiB both >= 3.5)
// The 64 KiB column is information only. The no-parser control (ctl-concat.mjs) is reported beside the table.
//
// Cell set: every cell the x2.2 table flagged, plus per opener family the worst-ratio cell and the
// slowest cell at 1 MiB from the 64/1024 KiB run (both, so a "worst" that means "slowest" is covered).
//
// Usage: node criterion-mos.mjs --module F --cells probe-final/cells.jsonl --control control-mos-runs.txt --out criterion-mos.md
// [--timeout 60] [--abort-after 6] [--select all|flagged+worst]
import { readFileSync, writeFileSync, appendFileSync } from 'node:fs';
import { spawnSync } from 'node:child_process';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const args = process.argv.slice(2);
const opt = (name, dflt) => { const i = args.indexOf('--' + name); return i < 0 ? dflt : args[i + 1]; };
const MODULE = resolve(opt('module')), CELLS = resolve(opt('cells')), OUT = resolve(opt('out', 'criterion-mos.md'));
const CONTROL = resolve(opt('control'));
const TIMEOUT = Number(opt('timeout', '60')), ABORT_AFTER = Number(opt('abort-after', '6')), SELECT = opt('select', 'flagged+worst');
const SIZES = [1024, 2048, 4096], CEIL_MS = 1000, GROWTH = 3, STEP = 3.5, LIMIT_PREV = 2.2;
const SELF = resolve(dirname(fileURLToPath(import.meta.url)), 'probe-gen.mjs');
const LOG = OUT.replace(/\.md$/, '-cells.jsonl');
writeFileSync(LOG, '');
const key = c => JSON.stringify([c.opener, c.filler, c.term]);
const prev = new Map();
for (const l of readFileSync(CELLS, 'utf8').split('\n').filter(Boolean)) {
const r = JSON.parse(l);
if (r.status !== 'ok') continue;
if (!prev.has(key(r))) prev.set(key(r), { opener: r.opener, filler: r.filler, term: r.term, by: {} });
prev.get(key(r)).by[r.kib] = r.ms;
}
const all = [...prev.values()].filter(c => c.by[64] !== undefined && c.by[1024] !== undefined);
for (const c of all) c.r = (c.by[1024] / Math.max(c.by[64], 0.001)) ** 0.25;
const picked = new Map();
if (SELECT === 'all') for (const c of all) picked.set(key(c), c);
else {
for (const c of all) if (c.r > LIMIT_PREV) picked.set(key(c), Object.assign(c, { why: 'x2.2-flagged' }));
const fams = [...new Set(all.map(c => c.opener))];
for (const f of fams) {
const own = all.filter(c => c.opener === f);
const byRatio = own.reduce((a, b) => (b.r > a.r ? b : a));
const bySlow = own.reduce((a, b) => (b.by[1024] > a.by[1024] ? b : a));
for (const [c, why] of [[byRatio, 'family worst ratio'], [bySlow, 'family slowest at 1 MiB']]) {
const k = key(c);
if (picked.has(k)) picked.get(k).why = (picked.get(k).why ?? '') + ' + ' + why; else picked.set(k, Object.assign(c, { why }));
}
}
}
const set = [...picked.values()];
console.log(`${set.length} cells x ${SIZES.join(', ')} KiB, timeout ${TIMEOUT} s, abort after ${ABORT_AFTER} timeouts`);
let timeouts = 0, aborted = false;
function measure(c, kib) {
if (aborted) return { status: 'not-run' };
const t0 = Date.now();
const r = spawnSync(process.execPath, [SELF, 'cell', MODULE, c.opener, c.filler, c.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: c.opener, filler: c.filler, term: c.term, kib, wall_ms: Date.now() - t0, ...rec };
appendFileSync(LOG, JSON.stringify(rec) + '\n');
if (timeouts >= ABORT_AFTER) aborted = true;
return rec;
}
// 1 MiB for every cell first, then 2, then 4, so an abort loses the largest sizes last
for (const kib of SIZES) for (const c of set) (c.m ??= {})[kib] = c.m?.[kib] ?? measure(c, kib);
const ctl = readFileSync(CONTROL, 'utf8').split('\n').map(l => l.match(/^(\d+) KiB ([\d.]+) ms/)).filter(Boolean).map(m => ({ kib: +m[1], ms: +m[2] }));
const med = a => { const s = [...a].sort((x, y) => x - y); return s[s.length >> 1]; };
const ctlMs = k => ctl.filter(x => x.kib === k).map(x => x.ms);
const ctlRuns = ctlMs(1024).length;
const ctlRatio = (med(ctlMs(4096)) / med(ctlMs(1024))) ** 0.5;
const f1 = x => x === undefined ? '-' : x >= 100 ? x.toFixed(0) : x.toFixed(1);
for (const c of set) {
const ok = SIZES.every(s => c.m[s].status === 'ok');
c.ok = ok;
if (!ok) { c.fail = ['not measured: ' + SIZES.map(s => c.m[s].status).join('/')]; continue; }
const t = SIZES.map(s => c.m[s].ms);
c.t = t; c.ratio = (t[2] / Math.max(t[0], 0.001)) ** 0.5;
c.steps = [t[1] / Math.max(t[0], 0.001), t[2] / Math.max(t[1], 0.001)];
c.fail = [];
if (t[0] > CEIL_MS) c.fail.push('ceiling');
if (c.ratio > GROWTH) c.fail.push('growth');
if (c.steps[0] >= STEP && c.steps[1] >= STEP) c.fail.push('steps');
}
const failed = set.filter(c => c.fail.length);
const measured = set.filter(c => c.ok);
const fams = [...new Set(set.map(c => c.opener))];
const row = c => `| ${JSON.stringify(c.opener)} + ${c.filler} + ${c.term} | ${c.why ?? ''} | ${f1(prev.get(key(c)).by[64])} | ${c.ok ? c.t.map(f1).join(' | ') : '- | - | -'} | ${c.ok ? 'x' + c.ratio.toFixed(2) : '-'} | ${c.ok ? c.steps.map(x => 'x' + x.toFixed(2)).join(' ') : '-'} | ${c.fail.length ? 'FAIL ' + c.fail.join(',') : 'pass'} |`;
const L = [];
L.push('# Mos rule on the fixed module (sage-0619)', '',
`Module ${MODULE}. ${set.length} cells: every cell the x2.2 table flagged, plus per opener family the worst-ratio cell and the slowest cell at 1 MiB. Each cell is a fresh process at 1, 2 and 4 MiB (timeout ${TIMEOUT} s; timeouts ${timeouts}${aborted ? ', ABORTED' : ''}).`, '',
`A cell fails if: more than ${CEIL_MS} ms at 1 MiB (ceiling); ratio per doubling over 1 to 4 MiB above x${GROWTH} (growth); or both steps 1 to 2 MiB and 2 to 4 MiB at x${STEP} or more (steps). The 64 KiB column is information only and is the figure from the 64/1024 KiB run.`, '',
'## Verdict', '', failed.length ? `**${failed.length} of ${set.length} cells FAIL.**` : `All ${measured.length} of ${set.length} cells pass all three tests.`, '',
'## The no-parser control', '',
`ctl-concat.mjs, ${ctlRuns} runs per size (ms): 1 MiB ${ctlMs(1024).map(x => x.toFixed(1)).join(', ')}; 2 MiB ${ctlMs(2048).map(x => x.toFixed(1)).join(', ')}; 4 MiB ${ctlMs(4096).map(x => x.toFixed(1)).join(', ')}.`, '',
`Medians ${med(ctlMs(1024)).toFixed(1)} / ${med(ctlMs(2048)).toFixed(1)} / ${med(ctlMs(4096)).toFixed(1)} ms; ratio per doubling over 1 to 4 MiB **x${ctlRatio.toFixed(2)}** (steps x${(med(ctlMs(2048)) / med(ctlMs(1024))).toFixed(2)}, x${(med(ctlMs(4096)) / med(ctlMs(2048))).toFixed(2)}).`, '',
'## Per family', '', '| opener | cells | worst ratio 1 to 4 MiB | slowest at 1 MiB (ms) | slowest at 4 MiB (ms) | failing |', '|---|---|---|---|---|---|');
for (const f of fams) {
const own = measured.filter(c => c.opener === f);
if (!own.length) { L.push(`| ${JSON.stringify(f)} | ${set.filter(c => c.opener === f).length} | - | - | - | not measured |`); continue; }
const w = own.reduce((a, b) => (b.ratio > a.ratio ? b : a)), s1 = own.reduce((a, b) => (b.t[0] > a.t[0] ? b : a)), s4 = own.reduce((a, b) => (b.t[2] > a.t[2] ? b : a));
L.push(`| ${JSON.stringify(f)} | ${own.length} | x${w.ratio.toFixed(2)} (${w.filler} + ${w.term}) | ${f1(s1.t[0])} (${s1.filler} + ${s1.term}) | ${f1(s4.t[2])} (${s4.filler} + ${s4.term}) | ${own.filter(c => c.fail.length).length} |`);
}
L.push('', '## Cells', '', '| cell | why selected | ms at 64 KiB | ms at 1 MiB | ms at 2 MiB | ms at 4 MiB | ratio 1 to 4 MiB | steps | result |', '|---|---|---|---|---|---|---|---|---|');
for (const c of [...set].sort((a, b) => (b.ratio ?? 0) - (a.ratio ?? 0))) L.push(row(c));
writeFileSync(OUT, L.join('\n') + '\n');
console.log(`failed ${failed.length} of ${set.length}; timeouts ${timeouts}; control ratio x${ctlRatio.toFixed(2)}`);
process.exit(failed.length || aborted ? 1 : 0);