From 66696e9a3e9d97d47a50c941fb774a69bdd1eeb2 Mon Sep 17 00:00:00 2001 From: bchanot Date: Thu, 5 Nov 2026 16:52:21 +0100 Subject: [PATCH] feat(memory): known-error line on failing commands --- mods/memory/hooks/register.test.ts | 197 +++++++++++++++++++++++++++++ mods/memory/hooks/register.ts | 76 +++++++++-- 2 files changed, 264 insertions(+), 9 deletions(-) diff --git a/mods/memory/hooks/register.test.ts b/mods/memory/hooks/register.test.ts index 5743f97..6806c0a 100644 --- a/mods/memory/hooks/register.test.ts +++ b/mods/memory/hooks/register.test.ts @@ -1716,6 +1716,7 @@ test('SR7: no registry read, no process run, no unavailable note', async ( test('SR8: HOME unset, status unavailable, fails open', async ($, on) => { const w = await boot($, on, { files: REGS }) await failing($, w, EXIT1 + SR_LINE) + w.runs = [] // the capture's known-line step ran the reader once w.home = undefined const text = await pending($) expect(w.runs).toEqual([]) @@ -2564,3 +2565,199 @@ test('LG5: two appends fired together keep both records', async ( expect((w.kv.get(`l:${ROOT}`) as LogRecord[]).map(r => r.at)) .toEqual([1, 2]) }) + +// ---- known-error line on failing commands ------------------------------ + +const KN_SPAN = 'fatal: unable to access remote host' +const KN_ENTRY = entryText('BLK-002', 'remote access', 'Solution', + 'Check the VPN first.') + `seen: \`${KN_SPAN}\`\n` +const KN_LINE = 'memory: known BLK-002 remote access — ' + + 'Check the VPN first.' + +/** A world whose BLK-002 is open and matches KN_SPAN. */ +async function bootKnown($: Engine, on: On, o: Opts = {}) { + const w = await boot($, on, { files: { [BLK]: KN_ENTRY }, ...o }) + w.proc = { stdout: rowsOut([['BLK-002', 'remote access']]) } + return w +} + +const asAgent = ($: Engine, command = 'make') => + $.tool.call({ tool: 'Bash', command, agentId: 'a1' } as never) + +test('TE1: a known failure gains the context line, keeps its shape', + async ($, on) => { + const w = await bootKnown($, on) + const out = await failing($, w, EXIT1 + KN_SPAN) + expect(out).toEqual({ ...(w.answer as object), context: [KN_LINE] }) + }) + +test('TE2: an ENOENT line adds no context key', async ($, on) => { + const text = ER_ENTRY + + 'seen: `cat: x: No such file or directory (os error 2)`\n' + const w = await boot($, on, { files: { [LRN]: text } }) + w.proc = { stdout: '| LRN-009 | 2026-01-01 | e | open |' } + const out = await failing($, w, + EXIT1 + "ls: cannot access '/tmp/zz': No such file or directory") + expect(out).toEqual(w.answer) + expect('context' in (out as object)).toBe(false) +}) + +test('TE3: a sub-agent and the main loop each get the line', async ( + $, on, +) => { + const w = await bootKnown($, on) + failWith(w, EXIT1 + KN_SPAN) + const sub = await asAgent($) + expect((sub as { context?: string[] }).context).toEqual([KN_LINE]) + const main = await bash($, 'make') + expect((main as { context?: string[] }).context).toEqual([KN_LINE]) + const again = await asAgent($) + expect('context' in (again as object)).toBe(false) +}) + +test('TE4: the same failure 20 times in one context injects once', + async ($, on) => { + const w = await bootKnown($, on) + failWith(w, EXIT1 + KN_SPAN) + let injected = 0 + for (let i = 0; i < 20; i++) { + const out = await bash($, 'make') as { context?: string[] } + if (out.context !== undefined) injected++ + } + expect(injected).toBe(1) + }) + +test('TE4b: two concurrent failing calls inject one line', async ( + $, on, +) => { + const w = await bootKnown($, on) + failWith(w, EXIT1 + KN_SPAN) + const outs = await Promise.all([bash($, 'make'), bash($, 'make')]) as + { context?: string[] }[] + expect(outs.filter(o => o.context !== undefined)).toHaveLength(1) + expect(await loadLog(storeApi(w), ROOT)).toHaveLength(1) +}) + +test('TE5: "recall": false adds no line, the capture is stored', + async ($, on) => { + const w = await bootKnown($, on, { + switchText: '{"enabled": true, "recall": false}' }) + const out = await failing($, w, EXIT1 + KN_SPAN) + expect(out).toEqual(w.answer) + expect(failuresOf(w)).toHaveLength(1) + }) + +test('TE6: a superseded entry injects nothing', async ($, on) => { + const w = await bootKnown($, on) + w.proc = { stdout: [ + '| BLK-002 | 2026-01-01 | remote access | superseded |'].join('\n') } + const out = await failing($, w, EXIT1 + KN_SPAN) + expect(out).toEqual(w.answer) +}) + +test('TE6b: an unavailable status reader injects nothing', async ( + $, on, +) => { + const w = await bootKnown($, on) + w.proc = 'exit2' + const out = await failing($, w, EXIT1 + KN_SPAN) + expect(out).toEqual(w.answer) +}) + +test('TE7: at 1448 chars spent, a 200-char line is refused', async ( + $, on, +) => { + // four lines of 362 chars (57 title + 280 lesson) = 1448 spent + const names = ['alpha', 'bravo', 'charlie', 'delta', 'echo'] + const spanOf = (n: string) => `fatal: ${n} cluster is down` + const body = (i: number, title: number, lesson: number) => entryText( + `BLK-01${i}`, 't'.repeat(title - 1) + i, 'Solution', + 'l'.repeat(lesson - 1) + '.') + `seen: \`${spanOf(names[i] ?? '')}\`\n` + const texts = [0, 1, 2, 3].map(i => body(i, 57, 280)) + texts.push(body(4, 50, 125)) // 14 + 8 + 50 + 3 + 125 = 200 + const rows = texts.map((_t, i): [string, string] => + [`BLK-01${i}`, 't'.repeat(i === 4 ? 49 : 56) + i]) + const w = await boot($, on, { files: { [BLK]: texts.join('\n') } }) + w.proc = { stdout: rowsOut(rows) } + for (let i = 0; i < 4; i++) { + const out = await failing($, w, EXIT1 + spanOf(names[i] ?? '')) as + { context?: string[] } + expect(out.context?.[0]?.length).toBe(362) + } + const last = await failing($, w, EXIT1 + spanOf('echo')) + expect(last).toEqual(w.answer) +}) + +test('TE8: a failing known-line step returns the answer unchanged', + async ($, on) => { + const w = await bootKnown($, on) + w.proc = 'throw' + const out = await failing($, w, EXIT1 + KN_SPAN) + expect(out).toEqual(w.answer) + expect(failuresOf(w)).toHaveLength(1) + }) + +test('TE9: a recall of ~1160 chars, an ordinary prompt, then a ~395 known ' + + 'line is injected', async ($, on) => { + const span = 'fatal: alpha cluster is down' + const known = entryText('BLK-004', 't'.repeat(89) + '4', 'Solution', + 'alpha '.repeat(50).slice(0, 279) + '.') + `seen: \`${span}\`\n` + const { w, seen } = await bootNotes($, on, { files: { [BLK]: + [...[1, 2, 3].map(longEntry), known].join('\n') } }) + w.proc = { stdout: rowsOut([1, 2, 3, 4].map((n): [string, string] => + [`BLK-00${n}`, 't'.repeat(89) + n])) } + w.model = { text: 'BLK-001, BLK-002, BLK-003' } + await start($) + await say($, 'design alpha now') + expect(seen[0]?.[0]?.length).toBe(1160) // 14 + 3 x (381 + 1) + await say($, 'nothing special at all') + const out = await failing($, w, EXIT1 + span) as { context?: string[] } + // 1160 + 395 > 1500: only the per-prompt reset lets it through + expect(out.context?.[0]?.length).toBe(395) +}) + +test('RB8: a first pick the budget refuses leaves spent equal to the block', + async ($, on) => { + const w = await boot($, on, { files: { [BLK]: [ + longEntry(1), + entryText('BLK-002', 'beta', 'Solution', 'alpha b.'), + ].join('\n') } }) + w.proc = { stdout: rowsOut([ + ['BLK-001', 't'.repeat(89) + '1'], ['BLK-002', 'beta']]) } + w.model = { text: 'BLK-001, BLK-002' } + const st = switchedOn() + // another caller spends 1150 while the rerank is in flight + const api = Object.assign(fakeApi(w), { model: { complete: async () => { + claim(st, 's1:main', ['X'], [1150]) + return { isAnswered: true, text: 'BLK-001, BLK-002' } + } } }) as unknown as Api + onContextStart(st, 's1') + const [block] = await promptContext(api, + st, { text: 'design alpha now', origin: { kind: 'composer' } }) + expect(block).toBe('memory recall:\nBLK-002 beta — alpha b.') + expect(st.spent.get('s1:main')).toBe(1150 + (block?.length ?? 0)) + expect(st.seen.get('s1:main')?.has('BLK-001')).toBe(false) + }) + +test('PC1: a throwing note still lets the recall attach', async ($, on) => { + const { w, seen } = await bootRecall($, on) + await start($) + await run($, w, 'git commit -m "fix: y"') + w.getsFail = true + await say($, GO) + expect(seen[0]).toEqual([BLOCK1]) +}) + +test('PC2: a throwing recall still lets the note attach', async ($, on) => { + const w = await boot($, on, { files: REC_FILES }) + w.proc = { stdout: REC_INDEX } + const st = switchedOn() + st.pending = ['commit fix: y'] + const api = Object.assign(fakeApi(w), { model: { complete: async () => ({ + get isAnswered(): boolean { throw new Error('boom') }, + }) } }) as unknown as Api + const added = await promptContext(api, st, { text: GO, + origin: { kind: 'composer' } }) + expect(added).toHaveLength(1) + expect(added[0]).toContain('memory: commit fix: y') +}) diff --git a/mods/memory/hooks/register.ts b/mods/memory/hooks/register.ts index 5d3e7cf..ab65b5b 100644 --- a/mods/memory/hooks/register.ts +++ b/mods/memory/hooks/register.ts @@ -1285,17 +1285,19 @@ async function captureFailure( at: Where, e: { command?: unknown; agentId?: unknown }, out: { isError?: boolean; text?: string }, -): Promise { +): Promise { try { - if (out.isError !== true) return + if (out.isError !== true) return undefined const agent = e.agentId !== undefined const f = buildFailure( String(out.text ?? ''), String(e.command ?? ''), agent, await $.clock.now(), ) if (f !== undefined) await queueWrite(st, () => recordFailure($, at, f)) + return f } catch { warnOnce(st, $, 'Bash capture', 'store') + return undefined } } @@ -1388,6 +1390,52 @@ async function captureMilestones( } } +/** + * The known-error line of a failure: `memory: known ` when a live + * BLK or LRN matches and the context has not seen it nor spent its budget. + * '' otherwise. Claims after the last await. + */ +async function knownLine( + $: Api, + st: State, + at: Where, + f: Failure, + agent: string | undefined, +): Promise { + if (!(await ensureRecall($, st))) return '' + const rx = await loadRegistries($, at.root, FAILURE_FILES) + if (rx.status === undefined) return '' + const id = matchOf(rx.index, f.key) + const entry = rx.entries.find(e => e.id === id) + if (entry === undefined) return '' + const line = `memory: known ${recallLine(entry)}` + const key = contextKey(at.id, agent) + if (claim(st, key, [entry.id], [line.length]).length === 0) return '' + const record = { at: await $.clock.now(), session: at.id, ids: [entry.id] } + await appendLog($, st, at.root, { + ...record, agent: agent ?? 'main', channel: 'tool', + }).catch(() => undefined) + return line +} + +/** The known line of a failing answer; '' on any failure (fail open). */ +async function knownContext( + $: Api, + st: State, + at: Where, + e: BashEvent, + f: Failure | undefined, +): Promise { + if (f === undefined) return '' + try { + const agent = typeof e.agentId === 'string' ? e.agentId : undefined + return await knownLine($, st, at, f, agent) + } catch { + warnOnce(st, $, 'Bash known', 'recall') + return '' + } +} + function registerBash(on: On, st: State): void { on('tool.call', { tool: 'Bash' }, async ($, e, next) => { if (!(await ensureEnabled($, st))) return next(e) @@ -1395,9 +1443,11 @@ function registerBash(on: On, st: State): void { const at = { root: await $.session.root(), id: await $.session.id() } if (at.root === '') return next(e) const out = await next(e) - await captureFailure($, st, at, e, out) + const f = await captureFailure($, st, at, e, out) await captureMilestones($, st, at, e, out as BashOut) - return out + const line = await knownContext($, st, at, e, f) + if (line === '') return out + return { ...out, context: [...(out.context ?? []), line] } }).catch(($, e, next) => { warnOnce(st, $, 'Bash', next.error.kind) return next(e) @@ -1534,10 +1584,12 @@ async function recallFor( const picked = parseIds(answer ?? '', cands, MAX_PICKS) .flatMap(id => byId.get(id) ?? []) const when = await $.clock.now() - // the header is charged to the first line of the block - const costs = picked.map((e, i) => - recallLine(e).length + 1 + (i === 0 ? RECALL_HEADER.length : 0)) - const taken = claim(st, key, picked.map(e => e.id), costs) + const taken: string[] = [] + for (const e of picked) { + // the header is charged to the first line actually taken + const header = taken.length === 0 ? RECALL_HEADER.length : 0 + taken.push(...claim(st, key, [e.id], [recallLine(e).length + 1 + header])) + } const block = recallBlock( picked.filter(e => taken.includes(e.id)).map(recallLine), RECALL_CAP) if (block === '') return '' @@ -1558,7 +1610,13 @@ async function additions( wantRecall: boolean, ): Promise { const rx = await loadRegistries($, at.root, ALL_FILES) - const note = st.pending.length > 0 ? await noteFor($, st, at, rx) : '' + // separate failures: a throw in one never drops the other + const note = st.pending.length > 0 + ? await noteFor($, st, at, rx).catch(() => { + warnOnce(st, $, 'prompt.submit', 'note') + return '' + }) + : '' const block = wantRecall ? await recallFor($, st, at, text, rx).catch(() => '') : ''