Elffuss BraidHub

utopia-ia/elffuss public

main · 4.4 KiB · executableRawPermalink
1#!/usr/bin/env node
2/**
3 * Fact-recall benchmark for context packers.
4 *
5 * WHAT IT MEASURES, and why it is built this way:
6 *
7 * Agent sessions are generated over a REAL project — every tool result is the
8 * genuine output of reading, listing or searching real files, not synthetic
9 * text. Halfway through the session the agent read a definition. At the end it
10 * is asked about it. The benchmark's only question is:
11 *
12 * is that definition still inside the packed context?
13 *
14 * There is NO model judging. It is a presence check: deterministic and
15 * reproducible. That matters more than it looks: scoring the SAME answers,
16 * three different judges gave us 0%, ~100% and something measurable, depending
17 * on the model and the prompt. A benchmark whose result depends on which judge
18 * you pick is no use for deciding between two packers.
19 *
20 * Every condition gets the SAME token budget. The only thing that changes is
21 * the selection policy.
22 *
23 * USAGE
24 * node run.mjs --root <path-to-a-repo> [--ext .js] [--budget 3000]
25 * [--seeds 8] [--packer <file.js>]
26 *
27 * --packer must export `packHistory(history, budgetTokens)`. By default it uses
28 * this repository's own core, so the README numbers reproduce with nothing to
29 * configure.
30 */
31import { createRequire } from 'module';
32import path from 'path';
33import { fileURLToPath } from 'url';
34const require = createRequire(import.meta.url);
35const HERE = path.dirname(fileURLToPath(import.meta.url));
36const SG = require(path.join(HERE, 'sessiongen.js'));
37
38const argv = process.argv.slice(2);
39const arg = (k, d) => { const i = argv.indexOf('--' + k); return i >= 0 ? argv[i + 1] : d; };
40const ROOT = arg('root');
41const EXT = arg('ext', '.js');
42const BUDGET = parseInt(arg('budget', '3000'), 10);
43const SEEDS = parseInt(arg('seeds', '8'), 10);
44const PACKER = path.resolve(arg('packer', path.join(HERE, '..', 'core', 'context.js')));
45
46if (!ROOT) {
47 console.error('missing --root <path-to-a-repo>. E.g.: node run.mjs --root ../core --ext .js');
48 process.exit(2);
49}
50
51// The core reads flags from localStorage, which does not exist in Node. It is
52// given an empty stand-in: that way the benchmark measures the DEFAULT
53// behaviour, which is what has to be measured — not some configuration
54// somebody left switched on.
55if (typeof globalThis.localStorage === 'undefined') {
56 const mem = new Map();
57 globalThis.localStorage = {
58 getItem: k => (mem.has(k) ? mem.get(k) : null),
59 setItem: (k, v) => mem.set(k, String(v)),
60 removeItem: k => mem.delete(k),
61 };
62}
63
64const { packHistory } = await import('file://' + PACKER);
65const estTok = s => Math.ceil((s || '').length / 4) + 4;
66
67// CONDITIONS. `tail` is the bar that has to be cleared: keep the last messages
68// until the budget is full and throw away the rest. If a packer does not beat
69// this, everything else you measure about it is beside the point.
70const tail = (history, budget) => {
71 const out = []; let used = 0;
72 for (let i = history.length - 1; i >= 0; i--) {
73 const t = estTok(history[i].content);
74 if (used + t > budget) break;
75 out.unshift(history[i]); used += t;
76 }
77 return out.length ? out : [history[history.length - 1]];
78};
79
80const CONDS = {
81 full: h => h,
82 tail: h => tail(h, BUDGET),
83 packer: h => packHistory(h, BUDGET),
84};
85
86const acc = {}; for (const c of Object.keys(CONDS)) acc[c] = { hit: 0, n: 0, tok: 0 };
87let sessions = 0;
88
89for (let seed = 1; seed <= SEEDS; seed++) {
90 const s = SG.buildSession({ root: ROOT, seed: seed * 7919, steps: 26, exts: [EXT] });
91 const probes = SG.selectProbes(s, SG.mulberry32(seed * 104729), 8, 'old', 'ident');
92 if (probes.length < 3) continue;
93 sessions++;
94 for (const p of probes) {
95 const h = [...s.history, { role: 'user', content: p.query }];
96 for (const [name, fn] of Object.entries(CONDS)) {
97 const out = await fn(h, BUDGET);
98 const txt = out.map(m => m.content).join('\n');
99 acc[name].n++;
100 acc[name].tok += out.reduce((a, m) => a + estTok(m.content), 0);
101 if (txt.includes(p.ident) && txt.includes(p.value)) acc[name].hit++;
102 }
103 }
104}
105
106console.log(`\n ${ROOT} · ${sessions} sessions · budget ${BUDGET} tokens\n`);
107console.log(` ${'condition'.padEnd(12)}${'recall'.padStart(10)}${'tokens'.padStart(10)}`);
108for (const [name, a] of Object.entries(acc)) {
109 if (!a.n) continue;
110 console.log(` ${name.padEnd(12)}${(100 * a.hit / a.n).toFixed(1).padStart(9)}%${Math.round(a.tok / a.n).toString().padStart(10)}`);
111}
112console.log(`\n n = ${acc.packer.n} probes\n`);