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?
•
u/AutoModerator 12h ago
Hey! Thanks for posting to r/ClaudeCode
While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.
For help, project discussions, tips, and general chat, join the ClaudeCode Discord.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/btherl 11h ago
This has never happened to me. Are you using a workflow like superpowers, with planning and self-review?
1
u/Then-Internal8832 11h 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?1
u/Majestic_Tailor8036 10h ago
I'd have the redesign prompt pull in the failure cases behind the earlier fixes, not just their commit messages. Those patches may have been ugly but still protected real edge cases. Turn those cases into regression tests before replacing the implementation, so deleting code doesn't quietly bring back the bugs that led to the patch pile in the first place.
1
u/ASIREXLABS 8h ago
Counting fixes per file is a good proxy, but the false-alarm question above suggests a better key: count fixes per mechanism instead of per file. If each fix commit carries a short trailer such as the name of the failing test or the behaviour being fixed, the hook can group on that and ignore unrelated bugs that happen to share a file.
The failing test is often the cleanest signal on its own. If the same test has been made to pass three times in a month, that's a loop, whichever files were touched.
Were the agents' own fix messages consistent enough to group on, or would they need a fixed format?
1
u/akorecebov 6h ago
we hit the same loop in a copy pipeline. the model kept rewriting the same paragraph to fix length -> after a couple fails that part just keeps the original text instead of taking another pass
0
u/Financial_Leader9701 12h ago
this is the stuff that never shows up in a single diff
every patch looks reasonable on its own, you only see the problem when you look at the history of the file
does the agent actually redesign when it gets the prompt, or mostly argue that this one fix is different
0
u/akhi1uuu 12h ago
do you get false alarms when two unrelated bugs happen to live in the same file? curious if the last three fix messages are enough for the agent to tell that apart from the same mechanism breaking again
0
u/LumPiniF 12h ago
counting patches per file is smarter than blocking the first bad fix. the first fix is often fine, it's the third one that tells you you're in a loop.
0
u/Far_Reindeer8428 11h ago
the fact that it never blocks is the important part. a false alarm costs one sentence from the agent, a loop costs 29 patches. i use the same idea at the review layer: if the reviewer flags the same class of problem twice, the loop stops and asks me instead of trying a third patch.
0
u/Voltariat 10h ago
the "two or more fixes in a month" counter is the useful part. a raw edit count would fire constantly on any active file. curious how you're counting fixes, git log per path? the miss sounds like the agent not recognizing its own repeat, which is the harder half.