diff --git a/src/guard/hooks.ts b/src/guard/hooks.ts index d90301fc..450ddf56 100644 --- a/src/guard/hooks.ts +++ b/src/guard/hooks.ts @@ -214,7 +214,7 @@ async function handlePreToolUse(input: unknown, opts: HookOpts): Promise // Without a tool_use_id the transcript copy cannot be told apart from ours, // so the call is left for the transcript to record. - if (call?.id) recordToolCall(cache, call) + if (call?.id) recordToolCall(cache, { ...call, live: true }) if (denied && call?.id) cache.deniedPending = [...cache.deniedPending, call.id].slice(-DENIED_PENDING_MAX) await writeCache(cache, opts.base) return output diff --git a/src/guard/usage.ts b/src/guard/usage.ts index d7cc122a..5c3d1aee 100644 --- a/src/guard/usage.ts +++ b/src/guard/usage.ts @@ -29,7 +29,9 @@ import { allowPath, sessionCachePath, sessionsDir } from './store.js' // `failedCommands` counts consecutive is_error results per command hash: a // passing run resets that command, and a successful edit resets every command, // since a rerun after a code change is a new attempt. -export type ToolCallKey = { id: string; name: string; hash: string; result?: string } +// `live`: the PreToolUse hook recorded this call itself, so whether it denied +// it is known exactly and its result is never guessed from the deny text. +export type ToolCallKey = { id: string; name: string; hash: string; result?: string; live?: boolean } export type GuardCache = { version: number @@ -118,7 +120,11 @@ export function inputHash(value: unknown): string { } export function recordToolCall(cache: GuardCache, call: ToolCallKey): void { - if (call.id && cache.recentCalls.some(c => c.id === call.id)) return + const known = call.id ? cache.recentCalls.find(c => c.id === call.id) : undefined + if (known) { + if (call.live) known.live = true + return + } cache.recentCalls.push(call) if (cache.recentCalls.length > RECENT_CALLS) cache.recentCalls.shift() } @@ -170,11 +176,13 @@ function foldToolResults(cache: GuardCache, entry: JournalEntry): void { const ours = new Set() for (const b of Array.isArray(content) ? content as { type?: unknown; tool_use_id?: unknown; content?: unknown; is_error?: unknown }[] : []) { if (b?.type !== 'tool_result' || typeof b.tool_use_id !== 'string') continue - if (cache.deniedPending.includes(b.tool_use_id) || isGuardDeny(b)) { + const recent = cache.recentCalls.find(c => c.id === b.tool_use_id) + // The deny text only stands in for a mark the cache no longer holds (a + // rebuild, or a call out of the window); a live call's result is genuine. + if (cache.deniedPending.includes(b.tool_use_id) || (!recent?.live && isGuardDeny(b))) { ours.add(b.tool_use_id) continue } - const recent = cache.recentCalls.find(c => c.id === b.tool_use_id) if (recent) recent.result = inputHash(b.content ?? null) } if (ours.size > 0) cache.deniedPending = cache.deniedPending.filter(id => !ours.has(id)) diff --git a/tests/guard-loop.test.ts b/tests/guard-loop.test.ts index 4dd47f59..b1e806b1 100644 --- a/tests/guard-loop.test.ts +++ b/tests/guard-loop.test.ts @@ -413,6 +413,13 @@ describe('failing-command retry', () => { expect(await call(env, 'Bash', { command: 'npm test' })).toContain('already failed 5 times') }) + it('counts a live failure whose text has the exact deny shape', async () => { + const env = await setup({ loopWarnAfter: null, loopStopAfter: null }) + const output = '(codeburn guard) The command "npm test" already failed 1 times; this session is $0.01. Change approach or ask the user.' + for (let i = 0; i < 5; i++) await call(env, 'Bash', { command: 'npm test' }, { fails: true, output }) + expect(await call(env, 'Bash', { command: 'npm test' })).toContain('already failed 5 times') + }) + it('only counts shell tools', async () => { const env = await setup({ loopWarnAfter: null, loopStopAfter: null }) const outs: string[] = []