From b127b2859b8f9bc7214b4f077d0815bed16a45ee Mon Sep 17 00:00:00 2001 From: bchanot Date: Sun, 8 Nov 2026 17:33:20 +0100 Subject: [PATCH] feat(memory-feedback): join log records to transcript turns --- lib/memory-feedback/transcripts.test.ts | 101 +++++++++++++ lib/memory-feedback/transcripts.ts | 190 ++++++++++++++++++++++++ 2 files changed, 291 insertions(+) create mode 100644 lib/memory-feedback/transcripts.test.ts create mode 100644 lib/memory-feedback/transcripts.ts diff --git a/lib/memory-feedback/transcripts.test.ts b/lib/memory-feedback/transcripts.test.ts new file mode 100644 index 0000000..c014137 --- /dev/null +++ b/lib/memory-feedback/transcripts.test.ts @@ -0,0 +1,101 @@ +import { afterAll, expect, test } from 'bun:test' +import * as fs from 'node:fs' +import * as os from 'node:os' +import * as path from 'node:path' +import type { LogRecord } from '../../mods/memory/hooks/register.ts' +import { + composerText, joinPrompt, joinTool, records, sessionFiles, slugOf, + type Rec, +} from './transcripts.ts' + +const dirs: string[] = [] +afterAll(() => dirs.forEach(d => fs.rmSync(d, { recursive: true }))) +const T0 = Date.parse('2026-11-10T10:00:00.000Z') +const iso = (ms: number) => new Date(T0 + ms).toISOString() +const user = (ms: number, text: string): Rec => ({ type: 'user', + timestamp: iso(ms), origin: { kind: 'human' }, message: { content: text } }) +const said = (ms: number, id: string, text: string): Rec => ({ + type: 'assistant', timestamp: iso(ms), + message: { id, content: [{ type: 'text', text }] } }) +const hook = (ms: number, name: string, lines: string[], use?: string) => ({ + type: 'attachment', timestamp: iso(ms), attachment: { + type: 'hook_additional_context', hookName: name, content: lines, + ...(use ? { toolUseID: use } : {}) } }) +const rec = (over: Partial): LogRecord => ({ at: T0, session: 's', + agent: 'main', channel: 'prompt', ids: ['BDR-001'], trigger: 'fresh', + outcome: 'injected', ...over }) +const JOIN_LATE = 10001 // one ms past JOIN_MS + +test('X1: slug, the main or one sub-agent file, torn lines counted', () => { + expect(slugOf('/home/a.b/c_d')).toBe('-home-a-b-c-d') + const p = fs.mkdtempSync(path.join(os.tmpdir(), 'mf-')) + dirs.push(p) + const dir = path.join(p, slugOf('/r')) + const sub = path.join(dir, 's', 'subagents') + fs.mkdirSync(sub, { recursive: true }) + fs.writeFileSync(path.join(dir, 's.jsonl'), '{"a":1}\n{"torn') + fs.writeFileSync(path.join(sub, 'agent-a1.jsonl'), '{"b":2}\n') + fs.writeFileSync(path.join(sub, 'agent-a2.jsonl'), '{"c":3}\n') + const main = sessionFiles(p, '/r', 's', 'main') + expect(main.map(f => path.basename(f))).toEqual(['s.jsonl']) + expect(sessionFiles(p, '/r', 's', 'a1').map(f => path.basename(f))) + .toEqual(['agent-a1.jsonl']) + expect(records(main[0]!)).toEqual({ recs: [{ a: 1 }], torn: 1 }) + expect(sessionFiles(p, '/r', 'gone', 'main')).toEqual([]) +}) + +test('X2: composer prompts only', () => { + expect(composerText(user(0, 'fix the gate'))).toBe('fix the gate') + expect(composerText(user(0, '/close'))).toBeUndefined() + expect(composerText(user(0, 'x'))).toBeUndefined() + expect(composerText({ ...user(0, 'x'), origin: { kind: 'sdk' } })) + .toBeUndefined() + expect(composerText({ ...user(0, 'x'), isSidechain: true })) + .toBeUndefined() +}) + +test('X3: an injected ask joins prompt, block and deduped reply', () => { + const recs = [ + user(-100, 'design the gate'), + hook(800, 'prompt.submit', ['memory: note', 'memory recall:\nBDR-001 x']), + said(2000, 'm1', 'see BDR-001'), said(2000, 'm1', 'see BDR-001'), + said(2100, 'm1', 'done'), + user(9000, 'next ask'), said(9500, 'm2', 'later'), + ] + expect(joinPrompt(recs, rec({}))).toEqual({ prompt: 'design the gate', + injected: 'memory recall:\nBDR-001 x', reply: 'see BDR-001\ndone' }) +}) + +test('X4: a non-injected ask joins its prompt; a missing turn is lost', + () => { + const recs = [user(-3000, 'plan the reader')] + expect(joinPrompt(recs, rec({ outcome: 'timeout', ids: [] }))) + .toEqual({ prompt: 'plan the reader', injected: '', reply: '' }) + expect(joinPrompt([], rec({ outcome: 'none', ids: [] }))) + .toBeUndefined() + const late = [user(0, 'x'), hook(JOIN_LATE, 'prompt.submit', + ['memory recall:\nBDR-001 x'])] + expect(joinPrompt(late, rec({}))).toBeUndefined() + }) + +test('X5: a tool record joins through the -context id, replays ignored', + () => { + const use = (ms: number, id: string, command?: string): Rec => ({ + type: 'assistant', timestamp: iso(ms), message: { id: `a${id}`, + content: [{ type: 'tool_use', id, name: 'Bash', + input: command ? { command } : {} }] } }) + const result = (ms: number, id: string, err: boolean, type = 'user') => + ({ type, timestamp: iso(ms), message: { content: [ + { type: 'tool_result', tool_use_id: id, is_error: err }] } }) + const recs: Rec[] = [ + use(0, 'u1', 'make test'), result(5, 'u1', true), + hook(10, 'tool.call', ['memory: known BLK-003 — x'], 'u1-context'), + result(15, 'u1', true, 'api-request-blob'), // a replay, not a call + use(20, 'u2'), result(25, 'u2', false), + result(30, 'u2', false), // a duplicate result of the same call + ] + const r = rec({ channel: 'tool', ids: ['BLK-003'], trigger: 'tool' }) + expect(joinTool(recs, r)).toEqual({ command: 'make test', + known: 'memory: known BLK-003 — x', next: [false] }) + expect(joinTool(recs, { ...r, ids: ['BLK-009'] })).toBeUndefined() + }) diff --git a/lib/memory-feedback/transcripts.ts b/lib/memory-feedback/transcripts.ts new file mode 100644 index 0000000..127587d --- /dev/null +++ b/lib/memory-feedback/transcripts.ts @@ -0,0 +1,190 @@ +// lib/memory-feedback/transcripts.ts: reads Claude Code transcripts and +// joins one injection-log record to the turn it served (phase 4d). +import * as fs from 'node:fs' +import * as path from 'node:path' +import type { LogRecord } from '../../mods/memory/hooks/register.ts' + +export type Rec = Record +export type Loaded = { recs: Rec[]; torn: number } +export const JOIN_MS = 10000 // attachment written at most this after `at` +const NEAR_MS = 60000 // composer prompt at most this far from the ask +const WRAPPERS = ['-context + +/** The transcript folder name of a project root. */ +export const slugOf = (root: string) => root.replace(/[^A-Za-z0-9]/g, '-') + +/** The transcript of a session's main thread, or of one sub-agent + * (`subagents/agent-.jsonl`, the id the log keeps in `agent`). */ +export function sessionFiles( + projects: string, root: string, session: string, agent: string, +): string[] { + const dir = path.join(projects, slugOf(root)) + const file = agent === 'main' + ? path.join(dir, `${session}.jsonl`) + : path.join(dir, session, 'subagents', `agent-${agent}.jsonl`) + return fs.existsSync(file) ? [file] : [] +} + +/** Parsed lines of a transcript; torn or partial lines are counted. */ +export function records(file: string): Loaded { + const recs: Rec[] = [] + let torn = 0 + for (const raw of fs.readFileSync(file, 'utf8').split('\n')) { + if (raw.trim() === '') continue + try { recs.push(JSON.parse(raw)) } catch { torn += 1 } + } + return { recs, torn } +} + +const tsOf = (r: Rec) => Date.parse(String(r.timestamp ?? '')) + +function blocksOf(r: Rec): Rec[] { + const c = r.message?.content + return Array.isArray(c) ? c.filter(b => b && typeof b === 'object') : [] +} + +/** A typed composer prompt (recall-bench's rule); else undefined. */ +export function composerText(r: Rec): string | undefined { + if (r.type !== 'user' || r.isSidechain === true) return undefined + if (r.isMeta === true || r.isCompactSummary === true) return undefined + const c = r.message?.content + if (typeof c !== 'string' || r.origin?.kind !== 'human') return undefined + const t = c.trim() + if (t.startsWith('/')) return undefined + return WRAPPERS.some(w => t.startsWith(w)) ? undefined : t +} + +/** The joined text of a `hook` context attachment; else undefined. */ +function hookText(r: Rec, hook: string): string | undefined { + const a = r.attachment + if (r.type !== 'attachment' || a?.type !== 'hook_additional_context') { + return undefined + } + if (a.hookName !== hook) return undefined + return Array.isArray(a.content) ? a.content.map(String).join('\n') + : String(a.content ?? '') +} + +/** The earliest `hook` attachment holding `needle` in [at, at+JOIN_MS]. */ +function attachmentAfter( + recs: Rec[], at: number, hook: string, needle: string, +): Rec | undefined { + let best: Rec | undefined + for (const r of recs) { + const t = tsOf(r) + if (!(t >= at && t <= at + JOIN_MS)) continue + if (!hookText(r, hook)?.includes(needle)) continue + if (best === undefined || t < tsOf(best)) best = r + } + return best +} + +type Prompt = { t: number; text: string } + +/** The composer prompt nearest `ts`, within NEAR_MS. */ +function promptNear(recs: Rec[], ts: number): Prompt | undefined { + let best: Prompt | undefined + for (const r of recs) { + const text = composerText(r) + const t = tsOf(r) + if (text === undefined || Math.abs(t - ts) > NEAR_MS) continue + if (best === undefined || Math.abs(t - ts) < Math.abs(best.t - ts)) { + best = { t, text } + } + } + return best +} + +/** The first composer prompt after `ts`; Infinity when none. */ +function nextPromptAt(recs: Rec[], ts: number): number { + let end = Infinity + for (const r of recs) { + const t = tsOf(r) + if (t > ts && t < end && composerText(r) !== undefined) end = t + } + return end +} + +/** Main-thread assistant text after `ts` up to the next prompt. */ +function replyAfter(recs: Rec[], ts: number): string { + const end = nextPromptAt(recs, ts) + const seen = new Set() + const parts: string[] = [] + for (const r of recs) { + const t = tsOf(r) + if (r.type !== 'assistant' || r.isSidechain === true) continue + if (!(t > ts && t < end)) continue + for (const b of blocksOf(r).filter(b => b.type === 'text')) { + const key = `${r.message?.id}:${b.text}` // EVAL-037 + if (!seen.has(key)) parts.push(String(b.text)) + seen.add(key) + } + } + return parts.join('\n') +} + +export type PromptJoin = { prompt: string; injected: string; reply: string } + +/** The turn a prompt-channel record served; undefined when lost. */ +export function joinPrompt( + recs: Rec[], rec: LogRecord, +): PromptJoin | undefined { + if (rec.outcome !== 'injected') { + const p = promptNear(recs, rec.at) + return p && { prompt: p.text, injected: '', reply: '' } + } + const att = attachmentAfter(recs, rec.at, 'prompt.submit', RECALL_HEAD) + if (att === undefined) return undefined + const t = tsOf(att) + const p = promptNear(recs, t) + const text = hookText(att, 'prompt.submit') ?? '' + return { prompt: p?.text ?? '', + injected: text.slice(text.indexOf(RECALL_HEAD)), + reply: replyAfter(recs, Math.max(t, p?.t ?? t)) } +} + +/** Blocks of real calls and results: blob replays are skipped. */ +const callBlocks = (recs: Rec[]) => recs + .filter(r => r.type === 'assistant' || r.type === 'user') + .flatMap(blocksOf) + +/** The command of a Bash tool_use. */ +function bashCommand(blocks: Rec[], useId: string): string { + const b = blocks.find(x => x.type === 'tool_use' && x.id === useId) + return String(b?.input?.command ?? '') +} + +/** Whether each of the next 3 Bash results after `useId` failed. */ +function nextResults(blocks: Rec[], useId: string): boolean[] { + const bash = new Set() + const done = new Set() + const out: boolean[] = [] + let after = false + for (const b of blocks) { + if (b.type === 'tool_use' && b.name === 'Bash') bash.add(b.id) + if (b.type !== 'tool_result' || !bash.has(b.tool_use_id)) continue + if (done.has(b.tool_use_id)) continue + done.add(b.tool_use_id) + if (after && out.length < 3) out.push(b.is_error === true) + if (b.tool_use_id === useId) after = true + } + return out +} + +export type ToolJoin = { command: string; known: string; next: boolean[] } + +/** The failure a known line answered and the next 3 Bash results. */ +export function joinTool(recs: Rec[], rec: LogRecord): ToolJoin | undefined { + const needle = KNOWN_HEAD + (rec.ids[0] ?? '') + const att = attachmentAfter(recs, rec.at, 'tool.call', needle) + const raw = att?.attachment?.toolUseID + if (att === undefined || typeof raw !== 'string') return undefined + const useId = raw.replace(CONTEXT_SUFFIX, '') + const blocks = callBlocks(recs) + return { command: bashCommand(blocks, useId), + known: hookText(att, 'tool.call') ?? '', + next: nextResults(blocks, useId) } +}