// Generates the machine-checked parts of SPACE.md: the "Fsck tree entries" section from test/space/fsck-tree.space.js // (checked by test/space/fsck-tree.test.js), and the "Push outcome" section from test/space/push-outcome.space.js: // the axes (tagged unions) and their variants, the product size, the impossible and collapsed cells, the cell count, // the expected outcomes, and the cells observed from git. test/space/push-outcome.test.js fails if SPACE.md drifts. // node scripts/space-md.mjs print the block // node scripts/space-md.mjs --write rewrite it in SPACE.md import { readFileSync, writeFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import * as space from '../test/space/push-outcome.space.js'; import { SCENARIOS, cellOf } from '../test/space/push-outcome.observed.js'; import * as fsckTree from '../test/space/fsck-tree.space.js'; import * as adv from '../test/space/advertisement.space.js'; export const START = ''; export const END = ''; const SPACE_MD = fileURLToPath(new URL('../SPACE.md', import.meta.url)); const payload = v => Object.entries(v).filter(([k]) => !['tag', 'why', 'n'].includes(k)).map(([k, x]) => `${k}=${typeof x === 'object' ? x.tag : JSON.stringify(x)}`).join(', '); const label = o => (o.tag === 'rejected' || o.tag === 'preSend' ? `${o.tag}(${o.code})` : o.tag); export function observedCells() { const by = new Map(); for (const [name, spec] of SCENARIOS) { const id = space.cellId(cellOf(spec)); by.set(id, [...(by.get(id) ?? []), name]); } return by; } export function census() { const outcomes = {}; const c = space.walk(cell => { const k = label(space.expected(cell)); outcomes[k] = (outcomes[k] ?? 0) + 1; }); return { census: c, outcomes }; } export function block({ census: c, outcomes }) { const L = [START, '']; L.push('### Axes (tagged unions)', ''); for (const a of space.AXES) { if (a.name === 'spread') { L.push(`**${a.name}** — ${a.doc} (${a.variants.length} variants)`, ''); L.push('- `uniform` — every ref has `status`'); L.push(`- \`mixed\` — at ∈ {0, 1, 2} × odd ∈ the ${space.STATUS_TAGS.length} \`status\` tags: ${a.variants.length - 1} variants (the odd ref's status overrides \`status\` for ref \`at\`)`, ''); continue; } L.push(`**${a.name}** — ${a.doc} (${a.variants.length} variants)`, ''); for (const v of a.variants) L.push(`- \`${v.tag}\`${payload(v) ? ` (${payload(v)})` : ''} — ${v.why}`); L.push(''); } L.push('### Product', ''); L.push(`Raw product: ${space.AXES.map(a => `${a.variants.length}`).join(' × ')} = **${c.raw.toLocaleString('en-US')}** (${space.AXES.map(a => a.name).join(' × ')}).`, ''); L.push('| removed | why | raw cells |', '|---|---|---:|'); space.IMPOSSIBLE.forEach((r, i) => L.push(`| impossible | ${r.why} | ${(c[`impossible:${i}`] ?? 0).toLocaleString('en-US')} |`)); space.COLLAPSE.forEach((r, i) => L.push(`| collapsed | ${r.why} (${r.axes.join(', ')} keep their first variant) | ${(c[`collapsed:${i}`] ?? 0).toLocaleString('en-US')} |`)); L.push(''); L.push(`**Cells: ${c.cells.toLocaleString('en-US')}.** Expected outcomes: ${Object.entries(outcomes).sort((a, b) => b[1] - a[1]).map(([k, n]) => `${k} ${n.toLocaleString('en-US')}`).join(', ')}.`, ''); const obs = observedCells(); L.push(`### Observed from git (${obs.size} cells; the other ${(c.cells - obs.size).toLocaleString('en-US')} are derived)`, ''); for (const [id, names] of obs) L.push(`- \`${id}\` — ${names.join('; ')}`); L.push('', END); return L.join('\n'); } export function splice(md, generated) { const a = md.indexOf(START), b = md.indexOf(END); if (a < 0 || b < 0) throw new Error('SPACE.md has no generated push-outcome block'); return md.slice(0, a) + generated + md.slice(b + END.length); } // ---- the fsck tree-entry section ---- export const FT_START = ''; export const FT_END = ''; const show = b => `"${b.toString('latin1').replace(/[^\x20-\x7e]/g, ch => `\\x${ch.charCodeAt(0).toString(16).padStart(2, '0')}`).replace(/([lm])\1{4095,}/, (m, ch) => `${ch}×${m.length}`)}"`; export function fsckTreeBlock() { const L = [FT_START, '']; L.push('### Axes (tagged unions)', ''); for (const a of fsckTree.AXES) { L.push(`**${a.name}** — ${a.doc} (${a.variants.length} variants)`, ''); for (const v of a.variants) { const what = v.raw !== undefined ? ` \`${v.raw}\`` : v.bytes ? ` ${show(v.bytes)}` : v.entry && v.entry !== 'same' ? ` \`${v.entry.mode} ${show(v.entry.name).slice(1, -1)}\`` : ''; L.push(`- \`${v.tag}\`${what} — ${v.why}`); } L.push(''); } const cells = fsckTree.product(); const raw = fsckTree.AXES.reduce((n, a) => n * a.variants.length, 1); const by = {}, ids = {}; for (const c of cells) { const w = fsckTree.expected(c); by[w.ply] = (by[w.ply] ?? 0) + 1; for (const m of w.errors) ids[m] = (ids[m] ?? 0) + 1; for (const m of w.warnings) ids[`${m} (warning)`] = (ids[`${m} (warning)`] ?? 0) + 1; } L.push('### Product', ''); L.push(`Raw product: ${fsckTree.AXES.map(a => a.variants.length).join(' × ')} = **${raw.toLocaleString('en-US')}** (${fsckTree.AXES.map(a => a.name).join(' × ')}); ${fsckTree.COLLAPSE.map(r => r.why).join('; ')}: **${cells.length.toLocaleString('en-US')} cells**.`, ''); L.push(`Expected: ply accepts ${(by.accept ?? 0).toLocaleString('en-US')}, refuses ${(by.refuse ?? 0).toLocaleString('en-US')}. Cells per msgId git reports (a cell can have several):`, ''); L.push('| msgId | cells |', '|---|---:|'); for (const [m, n] of Object.entries(ids).sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))) L.push(`| \`${m}\` | ${n.toLocaleString('en-US')} |`); L.push('', '### Decisions for the warnings git exits 0 on', ''); for (const [m, d] of Object.entries(fsckTree.WARN_DECISION)) L.push(`- \`${m}\` → ${d.ply}: ${d.why}`); L.push('', FT_END); return L.join('\n'); } export function spliceFsckTree(md, generated) { const a = md.indexOf(FT_START), b = md.indexOf(FT_END); if (a < 0 || b < 0) throw new Error('SPACE.md has no generated fsck-tree block'); return md.slice(0, a) + generated + md.slice(b + FT_END.length); } export const currentFsckTree = () => { const md = readFileSync(SPACE_MD, 'utf8'); return md.slice(md.indexOf(FT_START), md.indexOf(FT_END) + FT_END.length); }; export const current = () => { const md = readFileSync(SPACE_MD, 'utf8'); return md.slice(md.indexOf(START), md.indexOf(END) + END.length); }; // the advertisement section's block (test/space/advertisement.space.js mdBlock) export const currentAdvertisement = () => { const md = readFileSync(SPACE_MD, 'utf8'); return md.slice(md.indexOf(adv.MD_START), md.indexOf(adv.MD_END) + adv.MD_END.length); }; const spliceAdvertisement = (md, generated) => { const a = md.indexOf(adv.MD_START), b = md.indexOf(adv.MD_END); if (a < 0 || b < 0) throw new Error('SPACE.md has no generated advertisement block'); return md.slice(0, a) + generated + md.slice(b + adv.MD_END.length); }; if (process.argv[1] === fileURLToPath(import.meta.url)) { const g = block(census()), ft = fsckTreeBlock(); const ga = adv.mdBlock(adv.observedCounts()); if (process.argv.includes('--write')) writeFileSync(SPACE_MD, spliceAdvertisement(spliceFsckTree(splice(readFileSync(SPACE_MD, 'utf8'), g), ft), ga)); else console.log(`${g}\n\n${ft}\n\n${ga}`); }