Detect and break coding-agent edit-thrashing, shell-write bypasses & consecutive tool-error doom loops across Claude Code and Codex CLI transcripts.
If you wire a Claude Code PreToolUse hook that only watches Edit, MultiEdit, and Write to stop an agent from patching the same file 5+ times in a row, the agent routes around it through Bash / codex:exec (cat <<'EOF' > file.py, sed -i, Path(...).write_text(...), open(..., "w")).
When we audited 153 real local Claude Code (~/.claude/projects) and Codex CLI (~/.codex/sessions) transcripts (2,581 deduplicated tool calls):
- 39.2% Shell-Write Bypass (
LPB004): Out of199total source-file mutations,78(39.2%) were executed inside shell commands (Bash/codex:exec) rather thanEdit,Write, orapply_patch(121calls). In session01a12a24.jsonl, the agent rewrotebrain.py4 times in a single session entirely throughcodex:execshell scripts. - Same-File Edit Thrashing (
LPB001/LPB002/LPB003): Across153sessions,5source files were patched>= 3times inside a single session (fno.py6x,brain.py4x,test_backup_gdrive.py3x,test_fno_exit_telemetry.py3x,mcx.py3x), including overlapping hunk rewrites that reverted lines introduced two turns earlier. - 9-Turn Consecutive Tool-Error Doom Loops (
LPB008/LPB009):178failed tool calls clustered into18consecutive error streaks, peaking at 9 consecutive identical failedSkillinvocations in5f37b3d3.jsonl(<tool_use_error>Unknown skill: pony tail. Did you mean ponytail?</tool_use_error>). - Deleted-Sandbox CWD Escape (
LPB010): In2sessions where a temporary git worktree was removed while the agent was still active, the shell printedWorking directory ... was deleted; shell cwd recovered to /Users/...and the agent immediately ranrg --filesacross the user home directory. - Identical Command Retries & Empty
write_stdinSpins (LPB011/LPB012):20streaks where the exact same shell command was executed>= 3times back-to-back (48wasted calls) and3sessions spinning on emptywrite_stdinpolls.
A naive regex scanner over .jsonl transcripts grossly inflates edit-thrash numbers unless it filters three artifacts:
- Read-only
python3 -candsed -ninspections (LPB006—570calls filtered): When an agent runspython3 -c "import ast; ... open('submission.py').read()"orsed -n '1,80p' submission.py30 times to inspect a file, a naive filename matcher flagssubmission.pyas edited 32 times.loopbrakeseparates read-only inspections from actual write operators (>,tee,sed -i,perl -pi,cat <<EOF,.write_text(,open(..., 'w')). .jsonlvs.jsprefix regex collisions & doc/manifest updates (LPB007—207collisions +11doc edits filtered): A regex matching\.(js|py|ts)without a trailing word boundary matches.jsonlas.js, falsely flaggingjournal.jsonlas a JavaScript source file (journal.js). And normal end-of-task updates toREADME.md,CHANGELOG.md, andpackage.jsonshould not trigger source-code edit-thrash alarms.- Streaming
tool_use.id/call_idduplication (LPB014): Claude Code and Codex CLI emit multiple JSONL progress lines sharing the sametool_use.idorcall_idduring a single tool call. Deduplicating by call ID is mandatory before counting consecutive retries or edits.
Run directly with npx (zero dependencies, Node.js >= 18):
# Audit your local ~/.claude/projects and ~/.codex/sessions transcripts
npx loopbrake
# Run the deterministic 153-session benchmark showcase
npx loopbrake --demo
# Generate a ready-to-install Claude Code PreToolUse + PostToolUse circuit-breaker hook
npx loopbrake --emit-hook
# Audit specific log directories and fail CI if HIGH or CRITICAL loops are found
npx loopbrake ./logs --fail-on high --json| Metric | Measured Value | Notes |
|---|---|---|
| Sessions scanned | 153 |
Local Claude Code + Codex CLI .jsonl transcripts |
| Unique deduplicated tool calls | 2,581 |
Deduplicated by tool_use.id / call_id (LPB014) |
Structured file mutations (Edit / Write / apply_patch) |
121 |
60.8% of all source writes |
Shell-write bypasses (Bash / codex:exec heredoc, sed -i, .write_text) |
78 |
39.2% of all source writes (LPB004) |
Source files thrashed (>= 3 mutations in one session) |
5 |
fno.py (6x), brain.py (4x), test_backup_gdrive.py (3x), test_fno_exit_telemetry.py (3x), mcx.py (3x) |
Read-only python3 -c / sed -n inspections filtered |
570 |
Prevented 32x fake thrash count on submission.py (LPB006) |
.jsonl-vs-.js prefix collisions & doc updates excluded |
218 |
207 .jsonl prefix guards + 11 README.md/package.json edits (LPB007) |
| Total failed tool calls / consecutive error streaks | 178 / 18 |
Max streak: 9 consecutive failed calls (LPB008) |
Deleted-sandbox CWD escapes to ~ |
2 |
Worktree deleted mid-session; shell recovered to home dir (LPB010) |
Identical shell command retry loops (>= 3x) |
20 |
48 wasted duplicate command executions (LPB011) |
| Rule ID | Severity | Plane | Description |
|---|---|---|---|
LPB001 |
CRITICAL |
Thrash | Same-file edit thrashing (>= 4 mutations to one source file in a single session) |
LPB002 |
HIGH |
Thrash | Repeated band-aid patching (3 mutations to one source file in a single session) |
LPB003 |
HIGH |
Thrash | Overlapping hunk or self-revert rewrite on previously patched lines |
LPB004 |
MEDIUM |
Thrash | Shell-write bypass of structured Edit/apply_patch tools (cat <<EOF, sed -i, .write_text) |
LPB005 |
MEDIUM |
Thrash | Blind file edit invoked before any Read or inspection of target file |
LPB006 |
INFO |
Guard | Read-only python3 -c / sed -n AST & log inspections separated from file writes |
LPB007 |
INFO |
Guard | .jsonl-vs-.js prefix collisions and README.md/package.json updates excluded |
LPB008 |
CRITICAL |
Loop | Severe tool-error doom loop (>= 4 consecutive failing tool calls) |
LPB009 |
HIGH |
Loop | Consecutive tool-error streak (2 to 3 consecutive failing tool calls) |
LPB010 |
CRITICAL |
Loop | Deleted-sandbox CWD escape recovering shell into user home directory (~) |
LPB011 |
MEDIUM |
Loop | Identical shell command retry loop (>= 3 consecutive identical executions) |
LPB012 |
MEDIUM |
Loop | Empty write_stdin polling spin (>= 3 empty polls in one session) |
LPB013 |
LOW |
Loop | Duplicate Skill re-invocation in the same session |
LPB014 |
INFO |
Guard | Streaming tool_use.id / call_id progress rows deduplicated |
Run npx loopbrake --emit-hook to generate ~/.claude/hooks/loopbrake-hook.mjs and the matching .claude/settings.json configuration. Unlike basic Edit-only counters, the generated hook inspects both structured Edit/MultiEdit/Write calls and Bash write operators (sed -i, cat <<, .write_text(), plus PostToolUse error streaks, exiting with code 2 when a file hits 3 mutations or 3 consecutive tool errors so the agent is forced to stop and state the root cause.
- Heuristic shell-write parsing:
loopbrakedetects common shell write idioms (>,tee,sed -i,perl -pi,cat <<EOF,.write_text(,open(..., 'w')), but custom helper binaries or indirect build scripts that mutate source files without naming them on the command line will not be attributed to a specific source file. - Legitimate multi-step TDD refactors: A long session that intentionally implements 4 separate features inside the same large file over an hour will trigger
LPB001. Use per-turn hunk overlap (LPB003) alongsideLPB001to distinguish progressive feature additions from band-aid thrashing.
MIT © Prince Jain

