|
| 1 | +/** |
| 2 | + * 3-arm guard eval generator (task: do IDS rows earn their ~290 image tokens/block |
| 3 | + * on the Fable path, on top of the text fact sheet?). |
| 4 | + * |
| 5 | + * Arms (rendered here; queried by run.sh): |
| 6 | + * A = production: PNG rendered from appendIdsBlock(text), fact sheet in prompt |
| 7 | + * B = fact-sheet only: plain PNG, fact sheet in prompt |
| 8 | + * C = none: plain PNG, no fact sheet |
| 9 | + * |
| 10 | + * Corpus: synthetic session-log JSONL (12-hex ids + unique dur_ms), 2 pages, |
| 11 | + * each < DENSE_CONTENT_CHARS_PER_IMAGE so one PNG per page. Query mirrors |
| 12 | + * eval/verbatim-15: "which id has dur_ms=X" — association must come from the |
| 13 | + * image; guards can only help as exact-spelling correction, which is exactly |
| 14 | + * the mechanism under test. |
| 15 | + * |
| 16 | + * Strata per page (4 golds sampled from each): |
| 17 | + * ids+sheet — id is in the in-image IDS block (and therefore in the sheet) |
| 18 | + * sheet-only — id is in the 96-token fact sheet but not the 16-row IDS block |
| 19 | + * |
| 20 | + * No "uncovered" stratum: Fable dense pages cap at 91 rows (728px / 8px cells), so a |
| 21 | + * single-image block holds ≤91 log lines and the 96-token sheet budget covers every id |
| 22 | + * in it. Uncovered ids only exist on multi-image blocks (one sheet per block, >96 ids). |
| 23 | + * Body is capped at 72 lines/page so arm A (body + 17 IDS rows) stays single-image. |
| 24 | + * |
| 25 | + * Known confound (noted, accepted): tier-0 ranking is length-desc then lexical-asc, |
| 26 | + * so strata correlate with leading hex chars. The decision-relevant contrast |
| 27 | + * (A vs B, paired per trial) is unaffected — same golds, same strata. |
| 28 | + * |
| 29 | + * Deterministic: seeded PRNG, no Date/random in emitted content. |
| 30 | + * Run: npx tsx eval/ab/guards-3arm/gen.mts |
| 31 | + */ |
| 32 | +import { writeFileSync } from 'node:fs'; |
| 33 | +import { dirname, join } from 'node:path'; |
| 34 | +import { fileURLToPath } from 'node:url'; |
| 35 | +import { |
| 36 | + appendIdsBlock, |
| 37 | + extractFactSheetEntries, |
| 38 | + factSheetText, |
| 39 | +} from '../../../src/core/factsheet.js'; |
| 40 | +import { |
| 41 | + DENSE_CONTENT_CHARS_PER_IMAGE, |
| 42 | + DENSE_CONTENT_COLS, |
| 43 | + DENSE_RENDER_STYLE, |
| 44 | + renderTextToPngsWithCharLimit, |
| 45 | + shrinkColsToContent, |
| 46 | +} from '../../../src/core/render.js'; |
| 47 | + |
| 48 | +const OUT = join(dirname(fileURLToPath(import.meta.url)), 'work'); |
| 49 | + |
| 50 | +function mulberry32(seed: number): () => number { |
| 51 | + let a = seed >>> 0; |
| 52 | + return () => { |
| 53 | + a = (a + 0x6d2b79f5) | 0; |
| 54 | + let t = Math.imul(a ^ (a >>> 15), 1 | a); |
| 55 | + t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; |
| 56 | + return ((t ^ (t >>> 14)) >>> 0) / 4294967296; |
| 57 | + }; |
| 58 | +} |
| 59 | +const rnd = mulberry32(0x3a7f00d); |
| 60 | +const randInt = (lo: number, hi: number) => lo + Math.floor(rnd() * (hi - lo + 1)); |
| 61 | +const HEX = '0123456789abcdef'; |
| 62 | +/** 12-hex id guaranteed to contain ≥1 digit (extraction pattern requires it). */ |
| 63 | +function id12(): string { |
| 64 | + for (;;) { |
| 65 | + let s = ''; |
| 66 | + for (let i = 0; i < 12; i++) s += HEX[Math.floor(rnd() * 16)]; |
| 67 | + if (/\d/.test(s)) return s; |
| 68 | + } |
| 69 | +} |
| 70 | +function shuffle<T>(xs: T[]): T[] { |
| 71 | + const a = xs.slice(); |
| 72 | + for (let i = a.length - 1; i > 0; i--) { |
| 73 | + const j = Math.floor(rnd() * (i + 1)); |
| 74 | + [a[i], a[j]] = [a[j], a[i]]; |
| 75 | + } |
| 76 | + return a; |
| 77 | +} |
| 78 | + |
| 79 | +const OPS = ['render', 'upload', 'fetch', 'decode', 'verify', 'commit', 'snap', 'probe', 'merge', 'flush']; |
| 80 | +const PAGES = 3; |
| 81 | +const LINES_PER_PAGE = 72; // 72 body + 17 IDS rows ≤ 91-row page → arm A stays single-image |
| 82 | +const GOLDS_PER_STRATUM = 4; |
| 83 | + |
| 84 | +interface Rec { page: number; id: string; dur: number } |
| 85 | +const durs = new Set<number>(); |
| 86 | +function uniqDur(): number { |
| 87 | + for (;;) { |
| 88 | + const d = randInt(500, 9499); |
| 89 | + if (!durs.has(d)) { durs.add(d); return d; } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +const trials: Array<{ i: number; page: number; stratum: string; dur: number; gold: string }> = []; |
| 94 | +const meta: Record<string, unknown>[] = []; |
| 95 | + |
| 96 | +for (let page = 0; page < PAGES; page++) { |
| 97 | + const recs: Rec[] = []; |
| 98 | + const lines: string[] = []; |
| 99 | + let secs = 4 * 3600 + page * 1200; |
| 100 | + for (let n = 0; n < LINES_PER_PAGE; n++) { |
| 101 | + const id = id12(); |
| 102 | + const dur = uniqDur(); |
| 103 | + secs += randInt(1, 9); |
| 104 | + const hh = String(Math.floor(secs / 3600)).padStart(2, '0'); |
| 105 | + const mm = String(Math.floor((secs % 3600) / 60)).padStart(2, '0'); |
| 106 | + const ss = String(secs % 60).padStart(2, '0'); |
| 107 | + const ms = String(randInt(0, 999)).padStart(3, '0'); |
| 108 | + const line = |
| 109 | + `{"ts": "2026-07-12T${hh}:${mm}:${ss}.${ms}Z", "id": "${id}", "op": "${OPS[randInt(0, OPS.length - 1)]}", ` + |
| 110 | + `"dur_ms": ${dur}, "ok": ${rnd() < 0.9}, "lane": ${randInt(1, 8)}, "bytes": ${randInt(1000, 99999)}}`; |
| 111 | + lines.push(line); |
| 112 | + recs.push({ page, id, dur }); |
| 113 | + } |
| 114 | + const pageText = lines.join('\n'); |
| 115 | + |
| 116 | + // Guard sets, computed with the real production code paths. |
| 117 | + const armAText = appendIdsBlock(pageText); |
| 118 | + const idsIdx = armAText.indexOf('\nIDS\n'); |
| 119 | + if (idsIdx < 0) throw new Error(`page ${page}: appendIdsBlock added no IDS block`); |
| 120 | + const idsTokens = new Set( |
| 121 | + armAText.slice(idsIdx + 5).trim().split('\n').map((l) => l.trim().split(/\s+/).pop()!), |
| 122 | + ); |
| 123 | + const sheetTokens = new Set(extractFactSheetEntries(pageText).map((e) => e.token)); |
| 124 | + for (const t of idsTokens) { |
| 125 | + if (!sheetTokens.has(t)) console.warn(`page ${page}: IDS token not in sheet (unexpected): ${t}`); |
| 126 | + } |
| 127 | + const sheet = factSheetText(pageText); |
| 128 | + |
| 129 | + // Strata. |
| 130 | + const s1 = recs.filter((r) => idsTokens.has(r.id)); |
| 131 | + const s2 = recs.filter((r) => sheetTokens.has(r.id) && !idsTokens.has(r.id)); |
| 132 | + const s3 = recs.filter((r) => !sheetTokens.has(r.id)); |
| 133 | + for (const [name, pool] of [['ids+sheet', s1], ['sheet-only', s2], ['uncovered', s3]] as const) { |
| 134 | + for (const r of shuffle(pool).slice(0, GOLDS_PER_STRATUM)) { |
| 135 | + trials.push({ i: trials.length, page, stratum: name, dur: r.dur, gold: r.id }); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // Render both variants with the exact production dense single-col path |
| 140 | + // (transform.ts:1362 minus/plus the appendIdsBlock wrap). |
| 141 | + const renderOne = async (text: string, file: string) => { |
| 142 | + const imgs = await renderTextToPngsWithCharLimit( |
| 143 | + text, |
| 144 | + shrinkColsToContent(text, DENSE_CONTENT_COLS), |
| 145 | + DENSE_CONTENT_CHARS_PER_IMAGE, |
| 146 | + DENSE_RENDER_STYLE, |
| 147 | + ); |
| 148 | + if (imgs.length !== 1) throw new Error(`${file}: expected 1 png, got ${imgs.length}`); |
| 149 | + writeFileSync(join(OUT, file), imgs[0].png); |
| 150 | + return { w: imgs[0].width, h: imgs[0].height }; |
| 151 | + }; |
| 152 | + const dimA = await renderOne(armAText, `pageA${page}.png`); |
| 153 | + const dimP = await renderOne(pageText, `pageP${page}.png`); |
| 154 | + |
| 155 | + writeFileSync(join(OUT, `factsheet${page}.txt`), sheet); |
| 156 | + writeFileSync(join(OUT, `corpus${page}.txt`), pageText); |
| 157 | + meta.push({ |
| 158 | + page, |
| 159 | + lines: lines.length, |
| 160 | + chars: pageText.length, |
| 161 | + idsTokens: [...idsTokens], |
| 162 | + idsHexCount: [...idsTokens].filter((t) => /^[0-9a-f]{12}$/.test(t)).length, |
| 163 | + sheetTokenCount: sheetTokens.size, |
| 164 | + sheetHexCount: [...sheetTokens].filter((t) => /^[0-9a-f]{12}$/.test(t)).length, |
| 165 | + sheetChars: sheet.length, |
| 166 | + strata: { 'ids+sheet': s1.length, 'sheet-only': s2.length, uncovered: s3.length }, |
| 167 | + dims: { A: dimA, P: dimP }, |
| 168 | + }); |
| 169 | +} |
| 170 | + |
| 171 | +writeFileSync(join(OUT, 'trials.json'), JSON.stringify(trials, null, 1)); |
| 172 | +writeFileSync(join(OUT, 'meta.json'), JSON.stringify(meta, null, 1)); |
| 173 | +console.log(JSON.stringify(meta, null, 1)); |
| 174 | +console.log(`trials: ${trials.length}`); |
0 commit comments