Commit graph

2 commits

Author SHA1 Message Date
Resham Joshi
c357b6ce63
fix(guard): stop blocking normal test loops and polling, and keep the stop out of the agent's hands (#1895)
Some checks are pending
CI / semgrep (push) Waiting to run
Tests / test (22) (push) Waiting to run
Tests / test (22.15.0) (push) Waiting to run
Tests / test (24) (push) Waiting to run
Tests / test (26) (push) Waiting to run
Tests / test-platforms (macos-latest) (push) Waiting to run
Tests / test-platforms (windows-latest) (push) Waiting to run
Tests / desktop (push) Waiting to run
Tests / vscode (push) Waiting to run
* fix(guard): count consecutive failures so a test-edit loop is not stopped

A passing run resets that command's failure count and an edit resets all
of them; only back-to-back failures of one command reach the retry stop.

* fix(guard): do not count polling a background task as a loop

BashOutput, TaskOutput and Monitor repeat the same input by design while
waiting, so they never reach the identical-call warn or stop.

* fix(guard): tell only the user how to lift a stop

The deny reason reaches the model, so it no longer names 'codeburn guard
allow' or the guard.json threshold; that hint moves to the systemMessage
the user sees.

* fix(guard): allow without an id names its session and refuses when ambiguous

The cache now keeps the session's cwd. 'codeburn guard allow' prints the
session it lifts (id, cwd, cost) and exits 1 with the candidates listed
when more than one session wrote its cache in the last five minutes.

* fix(guard): prune session caches and allow markers after 30 days

writeCache checks at most once a day and removes per-session files left
untouched for 30 days; an allow marker stays while its session is live.

* fix(guard): write the session cache via temp file and rename

Parallel tool calls run PreToolUse hooks concurrently; an in-place write
could be read half-done, parse as empty and reset the warned flags.

* fix(guard): retry the cache rename on Windows sharing errors

A rename over a cache another hook has open fails with EPERM/EBUSY/EACCES
on Windows; retry with a short backoff, then write in place so the hook
never throws.

* fix(guard): keep the previous cache when the rename keeps failing

An in-place fallback write could be read torn and reset the warned flags.
After the retries the old cache stays, and readCache retries once on a
parse failure before starting over.

* fix(guard): word the Stop retry count as the longest streak

The count is the session's longest run of failures, which an edit may
since have broken. Also cite the hook contract at deny().

* test(guard): prove the rename without an inode check

ext4 can hand the freed inode number straight back on a rename, so count
rename calls and check the last payload won instead.

* fix(guard): treat a sleep-then-check command and calls whose results keep changing as waiting, not looping

A Bash command that starts with sleep is waiting on CI or a server and
never counts toward the loop. Other identical calls count only while
their tool results stay the same; a changing result is progress.

* fix(guard): exempt a sleep only when what follows is a read-only check

'sleep 0; npm test' repeated forever no longer escapes the loop counter.
The part after the sleep must be read-shaped, or a gh view/list/checks/
status/diff/watch call, which the shared read list leaves out.

* fix(guard): only a successful edit resets the retry counts

An edit is held pending until its tool result lands; one that errored,
like an old_string that did not match, changed no code and leaves the
failure counts alone.

* fix(guard): a sleep followed by a bare echo is a wait

'sleep 150 && echo waiting' is a blind timer wait. An echo with no
redirection, chaining or substitution now counts as a read-only check
after a sleep. The echo and gh read patterns also stop at a newline, so
a second command on the next line still counts.
2026-11-06 13:28:42 +01:00
Resham Joshi
5d12c6e86c
feat(guard): warn and stop identical-call loops and failing-command retries (#1857)
* feat(guard): warn and stop identical-call loops and failing-command retries

The PreToolUse hook now watches two more signals next to the dollar caps.
The same tool called with the same input (hashed as JSON with sorted keys)
4 times in a row gets a one-time warning and the 8th call is denied. A shell
command (Bash or PowerShell) that already failed 3 times in the session gets
a one-time warning and is denied once it has failed 5 times; successful runs
do not reset the count. Both pairs are guard.json settings (loopWarnAfter,
loopStopAfter, retryWarnAfter, retryStopAfter), validated like the dollar
caps, null turns one off, and guard status prints them.

The incoming call is read from the hook payload and recorded by tool_use_id,
since the transcript can lag. The session cache (version 4) keeps a rolling
window of the last 16 calls, pending shell calls and failure counts per
command, folded from the same incremental tail read as the cost. When a loop
or retry warning fired, the Stop note adds one line with the count and what
the calls after the first warning cost.

On a generated 20 MB transcript the warm hook takes about 13 ms (median of 5);
the first cold read of the whole file takes about 480 ms, as before.

* fix(guard): let allow lift loop blocks and tell the agent at the warning

codeburn guard allow now lifts the loop and retry stops as well as the hard
cap, for that session only, so polling a CI job on purpose has the same way
out. The deny reasons name it.

Loop and retry warnings now also reach the agent through PreToolUse
additionalContext, with the same sentence plus "Change approach or ask the
user." It is sent without a permissionDecision, since "allow" would also
skip the user's permission prompt. The docs now say the loop count tops out
at 17 (16 earlier calls plus the incoming one).
2026-11-04 19:29:23 +01:00