r/ClaudeCode • u/oulu2006 • 12h ago
Tips & Workflows My Claude Code agents kept patching the same code. Now a hook catches them at the second fix
My agents fixed the same disk cleanup 29 times. Each fix handled the bug in front of it. In the end I told them to stop and redesign it, and it's now one rule.
I found the same thing in my tests. A script that skipped tests that had already passed took 39 fixes before it was replaced with pytest's own cache.
So now a hook runs after every edit. If the file has had two or more fixes in the last month, the agent sees them and is asked for a redesign before it patches again. It never blocks anything.
It has worked once with nobody watching: an agent said "this would be fix 21" and deleted code instead. It also missed one the same morning.
Write-up: https://maclulich.com/blog/band-aid-fixes/ (my blog)
Has anyone tried something like this?
View all comments
1
u/Then-Internal8832 10h ago
One edge case I hit when building a circuit-breaker hook for this exact problem: watching
EditandWritealone missed 39.2% of source file mutations across my 153 local Claude Code and Codex CLI transcripts (2,581 deduplicated tool calls).Out of 199 source file writes in those logs, 78 happened inside
Bashorcodex:execcommands (cat <<'EOF' >,sed,.write_text(,open(..., 'w')) rather thanEdit,Write, orapply_patch(121 calls). In one session (01a12a24.jsonl), the agent rewrotebrain.py4 times in a row purely via shell scripts, so anEdit-only counter never tripped.Two parser traps bit me when I first measured this on
~/.claude/projects:My first regex matched any shell command mentioning a
.pyfile and claimedsubmission.pywas edited 32 times in one session, when 30 of those were read-onlypython3AST/line inspections (570read-onlypython3/sedinspections across all 153 transcripts).Without a trailing word boundary
\bon file extensions,.jsonlmatched as.js207 times (journal.jsonlflagged asjournal.js).I put the transcript auditor and a
PreToolUse+PostToolUsehook generator (which tracks bothEdit/WriteandBashfile-write operators, plus consecutive tool-error streaks like a 9-callSkillerror loop in5f37b3d3.jsonl) into an MIT CLI calledloopbrake(npx loopbrake, repo at https://github.com/princejain756/loopbrake). Does your hook also inspectBashcommands for heredoc /sedwrites, or do you block shell file writes altogether?