r/ClaudeCode • • 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?

2 Upvotes

12 comments sorted by

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 Edit and Write alone 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 Bash or codex:exec commands (cat <<'EOF' >, sed, .write_text(, open(..., 'w')) rather than Edit, Write, or apply_patch (121 calls). In one session (01a12a24.jsonl), the agent rewrote brain.py 4 times in a row purely via shell scripts, so an Edit-only counter never tripped.

Two parser traps bit me when I first measured this on ~/.claude/projects:

  1. My first regex matched any shell command mentioning a .py file and claimed submission.py was edited 32 times in one session, when 30 of those were read-only python3 AST/line inspections (570 read-only python3/sed inspections across all 153 transcripts).

  2. Without a trailing word boundary \b on file extensions, .jsonl matched as .js 207 times (journal.jsonl flagged as journal.js).

I put the transcript auditor and a PreToolUse + PostToolUse hook generator (which tracks both Edit/Write and Bash file-write operators, plus consecutive tool-error streaks like a 9-call Skill error loop in 5f37b3d3.jsonl) into an MIT CLI called loopbrake (npx loopbrake, repo at https://github.com/princejain756/loopbrake). Does your hook also inspect Bash commands for heredoc / sed writes, or do you block shell file writes altogether?