feat(memory): milestones and one note at the next prompt

This commit is contained in:
bchanot committed 2026-11-04 12:28:54 +01:00
1 parent 929c5dc50c
commit 86fac26390
2 files changed
+458 -4

No files matched your search

+283
View File
@@ -4,6 +4,7 @@ import type { On } from 'claude-code'
import {
cap, tail, redact, normalize, failureLine, sessionOf, addFailure,
entriesOf, isSuperseded, grams, buildIndex, matchOf,
segments, suiteKeyOf, commitLabel, finishLabel,
} from './register'
const HOME = '/home/t'
@@ -839,3 +840,285 @@ test('a throwing exists probe names the registry, keeps the other', async (
'index: read learnings.md; skipped blockers.md (unreadable)')
expect(text).toContain('- known LRN-002 \u00d71')
})
// Task 5: milestones and the one-line note.
type Stored = {
milestones: { kind: string; label: string; at: number }[]
red: string[]
}
const stored = (w: World): Stored =>
(projectOf(w)?.sessions[0] ?? { milestones: [], red: [] }) as
unknown as Stored
const kinds = (w: World) => stored(w).milestones.map(m => m.kind)
const labels = (w: World) => stored(w).milestones.map(m => m.label)
const OK = { result: { stdout: 'ok', stderr: '' } }
const RED = { result: { stdout: '', stderr: '' }, isError: true,
text: 'Exit code 2\nmake: *** [test] Error 1' }
const FINISH_CMD = 'bash "$HOME/.claude/lib/gitflow.sh" finish'
/** Bash calls whose answer is `answer`, one command at a time. */
async function run(
$: Engine, w: World, command: string, answer: unknown = OK,
): Promise<void> {
w.answer = answer
await bash($, command)
}
test('segments splits on && || ; and newline', () => {
expect(segments('a && b||c;d\ne')).toEqual(['a', 'b', 'c', 'd', 'e'])
})
test('suiteKeyOf: a make test segment, never a mention', () => {
expect(suiteKeyOf('cd x && make test suite=a'))
.toBe('cd x && make test suite=a')
expect(suiteKeyOf('grep "make test" notes.md')).toBeUndefined()
expect(suiteKeyOf('make build')).toBeUndefined()
expect(suiteKeyOf('X_TOKEN=abc make test'))
.toBe('X_TOKEN=<redacted> make test')
})
test('commitLabel: feat and fix only, any segment, -am and env', () => {
expect(commitLabel('git add a && git commit -m "fix(x): y"'))
.toBe('fix(x): y')
expect(commitLabel("git -C /r commit -qm 'feat!: big one'"))
.toBe('feat: big one')
expect(commitLabel('A=1 git commit -am "feat: z"')).toBe('feat: z')
expect(commitLabel('git commit -m "chore: z"')).toBeUndefined()
expect(commitLabel('echo git commit -m "fix: z"')).toBeUndefined()
expect(commitLabel('git commit -m "fix: ' + 'w'.repeat(200) + '"'))
.toBe('fix: ' + 'w'.repeat(75))
})
test('finishLabel: type and name, bare finish, chore excluded', () => {
expect(finishLabel(FINISH_CMD)).toBe('finish')
expect(finishLabel('bash ~/.claude/lib/gitflow.sh finish feature x'))
.toBe('feature x')
expect(finishLabel('bash ~/.claude/lib/gitflow.sh finish chore x'))
.toBeUndefined()
expect(finishLabel('echo gitflow.sh finish')).toBeUndefined()
})
test('MS1: a fix commit success stores one commit milestone', async (
$, on,
) => {
const w = await boot($, on)
await run($, w, 'git add a && git commit -m "fix(x): y"')
expect(kinds(w)).toEqual(['commit'])
expect(labels(w)).toEqual(['fix(x): y'])
expect(stored(w).milestones[0]?.at).toBe(w.clock.now())
})
test('MS2: a chore commit stores nothing', async ($, on) => {
const w = await boot($, on)
await run($, w, 'git commit -m "chore: z"')
expect(w.kv.size).toBe(0)
})
test('MS3 MS4: finish stores a milestone, finish chore none', async (
$, on,
) => {
const w = await boot($, on)
await run($, w, 'bash ~/.claude/lib/gitflow.sh finish chore x')
expect(w.kv.size).toBe(0)
await run($, w, FINISH_CMD)
expect(kinds(w)).toEqual(['finish'])
expect(labels(w)).toEqual(['finish'])
})
test('MS5: red then green gives one green; green alone none', async (
$, on,
) => {
const w = await boot($, on)
await run($, w, 'make test suite=a')
expect(w.kv.size).toBe(0)
await run($, w, 'make test suite=a', RED)
expect(stored(w).red).toEqual(['make test suite=a'])
await run($, w, 'make test suite=a')
expect(kinds(w)).toEqual(['green'])
expect(labels(w)).toEqual(['make test suite=a'])
expect(stored(w).red).toEqual([])
await run($, w, 'make test suite=a')
expect(kinds(w)).toEqual(['green'])
})
test('MS5b: a red of another suite does not turn green', async ($, on) => {
const w = await boot($, on)
await run($, w, 'make test suite=a', RED)
await run($, w, 'make test suite=b')
expect(kinds(w)).toEqual([])
})
test('MS6: a sub-agent commit and red store no milestone', async (
$, on,
) => {
const w = await boot($, on)
w.answer = OK
await $.tool.call({ tool: 'Bash', command: 'git commit -m "fix: a"',
agentId: 'a1' })
w.answer = RED
await $.tool.call({ tool: 'Bash', command: 'make test', agentId: 'a1' })
expect(stored(w).red).toEqual([])
expect(kinds(w)).toEqual([])
})
test('a backgrounded, timed out or interrupted run is no success', async (
$, on,
) => {
const w = await boot($, on)
await run($, w, 'make test', RED)
for (const result of [{ backgroundTaskId: 'b1' }, { timedOutAfterMs: 9 },
{ interrupted: true }]) {
await run($, w, 'make test', { result: { stdout: '', ...result } })
}
w.answer = OK
await $.tool.call({ tool: 'Bash', command: 'make test',
run_in_background: true })
expect(kinds(w)).toEqual([])
await run($, w, 'make test')
expect(kinds(w)).toEqual(['green'])
})
test('a deny answer on a commit stores no milestone', async ($, on) => {
const w = await boot($, on)
await run($, w, 'git commit -m "fix: a"', { deny: 'blocked' })
expect(kinds(w)).toEqual([])
})
test('a red key is stored redacted', async ($, on) => {
const w = await boot($, on)
await run($, w, 'X_TOKEN=abc make test', RED)
expect(stored(w).red).toEqual(['X_TOKEN=<redacted> make test'])
})
test('a commit label is stored redacted', async ($, on) => {
const w = await boot($, on)
await run($, w, `git commit -m "fix: ${'sk_live_' + 'a'.repeat(12)}"`)
expect(labels(w)).toEqual(['fix: <redacted:stripe>'])
})
test('a red answer without Exit code is no red', async ($, on) => {
const w = await boot($, on)
await run($, w, 'make test', { ...RED, text: 'denied by a guard' })
expect(w.kv.size).toBe(0)
})
test('milestones keep 20, red keeps 10 of 300 chars', async ($, on) => {
const w = await boot($, on)
for (let i = 0; i < 22; i++) {
await run($, w, `git commit -m "fix: n${i}"`)
}
expect(labels(w)).toHaveLength(20)
expect(labels(w)[0]).toBe('fix: n2')
for (let i = 0; i < 12; i++) {
await run($, w, `make test suite=${i}`, RED)
}
expect(stored(w).red).toHaveLength(10)
await run($, w, `make test ${'x'.repeat(400)}`, RED)
expect(stored(w).red.at(-1)).toHaveLength(300)
})
test('a stored commit milestone shows in the memory_pending answer', async (
$, on,
) => {
const w = await boot($, on)
await run($, w, 'git commit -m "fix(x): y"')
expect(await pending($)).toBe([
'memory_pending (project app, 1 session(s))',
'index: skipped blockers.md (missing), learnings.md (missing)',
'milestones (this session):',
'- commit fix(x): y (00:00)',
].join('\n'))
})
/** Boots with a bottom prompt hook that records the context it gets. */
async function bootNotes(
$: Engine, on: On, o: Opts = {},
): Promise<{ w: World; seen: (readonly string[] | undefined)[] }> {
const seen: (readonly string[] | undefined)[] = []
on('prompt.submit', (_a, e) => {
seen.push(e.context)
return { text: e.text }
})
return { w: await boot($, on, o), seen }
}
const composer = ($: Engine, turnId?: string) => $.prompt.submit({
text: 'next', wait: turnId !== undefined, origin: { kind: 'composer' },
...(turnId === undefined ? {} : { turnId }),
})
test('NT1 NT2: one note after a milestone, none after', async ($, on) => {
const { w, seen } = await bootNotes($, on)
await failing($, w, EXIT1 + 'fatal: bad thing', 'git push')
await run($, w, 'git commit -m "fix(x): y"')
await composer($)
expect(seen).toEqual([[
'memory: commit fix(x): y \u2014 1 new failure mode(s) this session;' +
' /capitalize when ready.',
]])
await composer($)
expect(seen[1]).toBeUndefined()
})
test('NT1b: a known failure is not counted as new', async ($, on) => {
const { w, seen } = await bootNotes($, on, { files: REGS })
await failing($, w, EXIT1 + 'fatal: unable to access remote host', 'git')
await run($, w, 'git commit -m "fix: y"')
await composer($)
expect(seen[0]?.[0]).toContain('\u2014 0 new failure mode(s)')
})
test('NT3: a mid-turn prompt gets no note and keeps the milestone', async (
$, on,
) => {
const { w, seen } = await bootNotes($, on)
await run($, w, 'git commit -m "fix: y"')
await composer($, 'u1')
expect(seen[0]).toBeUndefined()
await composer($)
expect(seen[1]).toHaveLength(1)
})
test('NT4: no milestone, no context added', async ($, on) => {
const { seen } = await bootNotes($, on)
await composer($)
expect(seen).toEqual([undefined])
})
test('a prompt that is not from the composer gets no note', async (
$, on,
) => {
const { w, seen } = await bootNotes($, on)
await run($, w, 'git commit -m "fix: y"')
await $.prompt.submit({ text: 'x', wait: false, origin: { kind: 'sdk' } })
expect(seen[0]).toBeUndefined()
await composer($)
expect(seen[1]).toHaveLength(1)
})
test('an existing context is kept and the note appended', async ($, on) => {
const { w, seen } = await bootNotes($, on)
await run($, w, 'git commit -m "fix: y"')
await $.prompt.submit({ text: 'x', wait: false, context: ['keep'],
origin: { kind: 'composer' } })
expect(seen[0]?.[0]).toBe('keep')
expect(seen[0]).toHaveLength(2)
})
test('session.end drops the unnoted milestones', async ($, on) => {
const { w, seen } = await bootNotes($, on)
await run($, w, 'git commit -m "fix: y"')
await endSession($)
await composer($)
expect(seen[0]).toBeUndefined()
})
test('MTOFF: switched off, no milestone and no note', async ($, on) => {
const { w, seen } = await bootNotes($, on,
{ switchText: '{"enabled": false}' })
await run($, w, 'git commit -m "fix: y"')
await composer($)
expect(w.kv.size).toBe(0)
expect(seen[0]).toBeUndefined()
})
+175 -4
View File
@@ -503,14 +503,76 @@ function queueWrite(st: State, job: () => Promise<void>): Promise<void> {
type Where = { root: string; id: string }
async function recordFailure($: Api, at: Where, f: Failure): Promise<void> {
/** Load, change the current session, save; sweeps when it is created. */
async function mutateSession(
$: Api,
at: Where,
now: number,
change: (s: Session) => void,
): Promise<void> {
const p = await loadProject($, at.root)
const created = !p.sessions.some(s => s.id === at.id)
addFailure(sessionOf(p, at.id, f.at), f)
change(sessionOf(p, at.id, now))
await saveProject($, at.root, p)
if (created) await sweepStore($, storeKey(at.root), at.id)
}
const recordFailure = ($: Api, at: Where, f: Failure) =>
mutateSession($, at, f.at, s => addFailure(s, f))
// Milestones: a commit, a gitflow finish, a suite gone from red to green.
// Regexes read at most 64 KiB of one command and use bounded repeats.
const MAX_LABEL = 80
const ENV_PREFIX = '(?:[A-Za-z_]\\w{0,60}=\\S{0,200}[ \\t]{1,5}){0,10}'
const SUITE_SEGMENT = new RegExp(
`^${ENV_PREFIX}make\\b[^;&|]{0,1000}\\btest\\b`,
)
const COMMIT_SEGMENT = new RegExp(
`^${ENV_PREFIX}git(?:[ \\t]{1,5}-C[ \\t]{1,5}\\S{1,200})?` +
'[ \\t]{1,5}commit\\b',
)
const COMMIT_MESSAGE = new RegExp(
'-[a-z]{0,5}m[ \\t]{1,5}(["\']?)(feat|fix)(\\([^)\\n]{0,80}\\))?!?:' +
'[ \\t]{0,5}([^"\'\\n]{0,80})',
)
const FINISH = new RegExp(
'(?:^|[\\s;&|])(?:bash|sh)[ \\t]{1,5}\\S{0,200}gitflow\\.sh"?' +
'[ \\t]{1,5}finish\\b(?:[ \\t]{1,5}([a-z]{1,20})[ \\t]{1,5}' +
'([\\w./-]{1,60}))?',
)
const FINISH_CHORE = /\bfinish[ \t]{1,5}chore\b/
/** The `;` `&&` `||` newline separated parts of a command. */
export function segments(command: string): string[] {
return cap(command, MAX_REGEX_INPUT).split(/&&|\|\||;|\n/)
.map(s => s.trim())
}
const label = (text: string) => redact(text).slice(0, MAX_LABEL)
/** The redacted, whitespace-normalized command when it runs a suite. */
export function suiteKeyOf(command: string): string | undefined {
if (!segments(command).some(s => SUITE_SEGMENT.test(s))) return undefined
return redact(cap(command, MAX_REGEX_INPUT)).replace(/\s+/g, ' ').trim()
.slice(0, MAX_RED_LEN)
}
/** `<type><scope>: <subject>` of a feat/fix commit, else `undefined`. */
export function commitLabel(command: string): string | undefined {
if (!segments(command).some(s => COMMIT_SEGMENT.test(s))) return undefined
const m = COMMIT_MESSAGE.exec(cap(command, MAX_REGEX_INPUT))
if (m === null) return undefined
return label(`${m[2]}${m[3] ?? ''}: ${(m[4] ?? '').trim()}`)
}
/** `<type> <name>` after a gitflow finish, `finish` alone, or undefined. */
export function finishLabel(command: string): string | undefined {
const text = cap(command, MAX_REGEX_INPUT)
const m = FINISH.exec(text)
if (m === null || FINISH_CHORE.test(text)) return undefined
return m[1] === undefined ? 'finish' : label(`${m[1]} ${m[2]}`)
}
function newState(): State {
return {
enabled: null,
@@ -641,6 +703,90 @@ async function captureFailure(
}
}
type BashEvent = {
command?: unknown
agentId?: unknown
run_in_background?: unknown
}
type BashOut = {
isError?: boolean
deny?: unknown
text?: string
result?: {
backgroundTaskId?: unknown
timedOutAfterMs?: unknown
interrupted?: unknown
}
}
/** A finished foreground command that was neither denied nor failed. */
function isSuccess(e: BashEvent, out: BashOut): boolean {
const r = out.result
return out.isError !== true && out.deny === undefined &&
r?.backgroundTaskId === undefined && r?.timedOutAfterMs === undefined &&
r?.interrupted !== true && e.run_in_background !== true
}
/** The suite a failed command ran, remembered as red until it passes. */
function recordRed($: Api, st: State, at: Where, key: string, now: number) {
return queueWrite(st, () => mutateSession($, at, now, s => {
if (!s.red.includes(key)) s.red = [...s.red, key]
}))
}
/** Commit and finish milestones of a success; green when the suite was red. */
function recordSuccess(
$: Api,
st: State,
at: Where,
command: string,
now: number,
): Promise<void> {
const found = [
['commit', commitLabel(command)],
['finish', finishLabel(command)],
].filter((m): m is [Milestone['kind'], string] => m[1] !== undefined)
const key = suiteKeyOf(command)
return queueWrite(st, async () => {
const cur = (await loadProject($, at.root)).sessions
.find(s => s.id === at.id)
const green = key !== undefined && cur?.red.includes(key) === true
if (found.length === 0 && !green) return
const added = found.map(([kind, text]) => ({ kind, label: text, at: now }))
if (green) added.push({ kind: 'green', label: label(key), at: now })
await mutateSession($, at, now, s => {
s.milestones = [...s.milestones, ...added]
if (green) s.red = s.red.filter(r => r !== key)
})
st.pending.push(...added.map(m => `${m.kind} ${m.label}`))
})
}
/** Milestones of a main-loop Bash answer; never throws. */
async function captureMilestones(
$: Api,
st: State,
at: Where,
e: BashEvent,
out: BashOut,
): Promise<void> {
try {
if (e.agentId !== undefined) return
const command = String(e.command ?? '')
const now = await $.clock.now()
const key = suiteKeyOf(command)
if (out.isError === true) {
if (key !== undefined && EXIT_CODE.test(String(out.text ?? ''))) {
await recordRed($, st, at, key, now)
}
} else if (isSuccess(e, out)) {
await recordSuccess($, st, at, command, now)
}
} catch {
warnOnce(st, $, 'Bash milestones', 'store')
}
}
function registerBash(on: On, st: State): void {
on('tool.call', { tool: 'Bash' }, async ($, e, next) => {
if (!(await ensureEnabled($, st))) return next(e)
@@ -649,6 +795,7 @@ function registerBash(on: On, st: State): void {
if (at.root === '') return next(e)
const out = await next(e)
await captureFailure($, st, at, e, out)
await captureMilestones($, st, at, e, out as BashOut)
return out
}).catch(($, e, next) => {
warnOnce(st, $, 'Bash', next.error.kind)
@@ -656,10 +803,34 @@ function registerBash(on: On, st: State): void {
})
}
/** Failures of the current session no registry entry knows. */
async function newFailureCount($: Api, st: State, at: Where): Promise<number> {
await st.writes
const p = await loadProject($, at.root)
const { index } = await loadIndex($, at.root)
const cur = p.sessions.find(s => s.id === at.id)
return (cur?.failures ?? []).filter(isFailure)
.filter(f => matchOf(index, f.key) === undefined).length
}
/** The note for the oldest unnoted milestone; clears what it covers. */
async function noteFor($: Api, st: State, at: Where): Promise<string> {
const first = st.pending[0] ?? ''
st.pending = []
const n = await newFailureCount($, st, at)
return `memory: ${first} \u2014 ${n} new failure mode(s) this session;` +
' /capitalize when ready.'
}
function registerPrompt(on: On, st: State): void {
on('prompt.submit', async ($, e, next) => {
await ensureEnabled($, st)
return next(e)
if (!(await ensureEnabled($, st))) return next(e)
const mine = e.origin.kind === 'composer' && e.turnId === undefined
if (!mine || st.pending.length === 0) return next(e)
const at = { root: await $.session.root(), id: await $.session.id() }
if (at.root === '') return next(e)
const note = await noteFor($, st, at)
return next({ ...e, context: [...(e.context ?? []), note] })
}).catch(($, e, next) => {
warnOnce(st, $, 'prompt.submit', next.error.kind)
return next(e)