fix(guard): read the deny shape only when no live mark exists

A genuine failure whose text matched a deny reason exactly was dropped even in
a live session, where the hook knows which calls it denied. Calls the hook
recorded itself are now flagged live, and the deny-shape match only applies to
calls without a live record or a pending deny (a rebuild or a call out of the
window).
This commit is contained in:
Resham Joshi 2026-11-06 19:34:17 -08:00
parent 9a6de518e3
commit 18b1502fc0
3 changed files with 20 additions and 5 deletions

View file

@ -214,7 +214,7 @@ async function handlePreToolUse(input: unknown, opts: HookOpts): Promise<string>
// 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

View file

@ -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<string>()
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))

View file

@ -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[] = []