DEV Community

Cover image for One Shell Command Kept Calling Itself Until My PC Froze: Stopping Known Mistakes with Claude Code Hooks

One Shell Command Kept Calling Itself Until My PC Froze: Stopping Known Mistakes with Claude Code Hooks

maruhana on November 28, 2026

A backtick inside double quotes ran a Markdown file as a shell script, which ran itself. I moved known mistakes out of the notes and into PreToolUs...
Collapse
 
fxdgif profile image
Fred 🍓 •

Replaying the rules over old commands before trusting them is the part I'd copy. A related trap sits on the hook entry itself rather than the commands it checks: if a hook runs inline PowerShell, Git Bash expands the $ before PowerShell sees it, same family as your ps-dollar-in-bash-dq rule. The exec form (command plus an args array, pointing at a .ps1 file) skips the shell entirely. I also keep those scripts ASCII-only, because Windows PowerShell 5.1 reads a file without a BOM as ANSI, and one typographic dash inside a string was enough to break the parse for me. Are your hooks in shell form or exec form?

Collapse
 
maruhana profile image
maruhana •

Shell form, but nothing in the hook entry gets expanded. Each entry is just node with a quoted path to a .mjs file, e.g. node "C:/Users//.claude/hooks/pitfall-guard.mjs", and the command to check arrives as JSON on stdin. So no inline PowerShell, and no $ for Git Bash to touch. Thanks for the BOM tip; my scripts are JavaScript with Japanese text in them, which Node reads as UTF-8 without trouble, but it's worth keeping in mind for any .ps1.

Collapse
 
fxdgif profile image
Fred 🍓 •

That's the safe version, nothing left for Bash to expand. The part I didn't expect is what the shell itself costs per call. I timed one hook on Windows: the program alone took 16 ms, through Git Bash 42 ms, through cmd /c 45 ms, and through PowerShell 418 ms. Shell form goes through Git Bash every time, so the exec form ("command": "node" plus the .mjs path in "args") would drop roughly 25 ms from each of your 8,522 calls. Node's own startup is probably most of your 0.1 s, though.

Thread Thread
 
maruhana profile image
maruhana • • Edited

Thanks for timing it. All I have is the total, about 0.1 s per check, so I can't say how much of it is Node's startup and how much is the shell. That's worth timing before I change anything.

Thread Thread
 
fxdgif profile image
Fred 🍓 •

Easy way to split it: time node -e "0" on its own, which is the startup floor, then your hook script with a sample JSON piped to stdin, then the same call through the real hook entry. In my test the bare program was 16 ms and Git Bash added about 26 ms on top, so if most of your 0.1 s is the first number, switching to exec form won't change much. Curious what you find.

Collapse
 
skillselion profile image
Skillselion •

Replaying the rules over 24,374 old commands before trusting them is the step most hook write-ups skip, and the drop from 587 hits to 181 shows why it matters. You say the hook returns the reason and the fix. Is that exit code 2 with the text on stderr? For a PreToolUse hook, Claude Code feeds that stderr back to the model, so the agent can retry with single quotes or a file for the py -3 -c body instead of stopping cold. We would be curious how many of the 7 got a working retry on the next turn and how many needed you.

Collapse
 
maruhana profile image
maruhana •

Yes. Both hooks write the reason and the fix to stderr and exit with code 2, and Claude Code passes that text back to the model as the result of the blocked call.

I hadn't counted the retries, so I went back to the session logs for those 7 blocks. Every one was followed by a working command within 3 to 12 seconds, with no message from me in between:

  • 6 of 7 worked on the very next call, mostly by doing what the message suggested: the backtick cases moved the Python into a file, and the PowerShell $_ case moved the command into a .ps1 file.
  • 1 took two tries. It first switched to single quotes, which broke the WMI filter's own quoting, and then wrote a .ps1 file, which worked.

So none of the 7 needed me. "Working" here means the next command ran and returned the output it was after; I read each pair, but it's 7 cases on one PC, so I wouldn't read much into the rate.

Collapse
 
crisstiank profile image
Cristian K •

Running the rules against commands from past sessions is the part worth copying. Going from 587 hits to 181 shows that a hook is only as useful as its false-positive rate, since noisy blocks tend to get switched off. One addition: keep the replayed commands as a small must-block and must-allow list and re-run it whenever you edit a rule, so a tweak that quiets a noisy pattern doesn't quietly reopen the original hole.

Collapse
 
maruhana profile image
maruhana •

Agreed, and that's roughly what I ended up with. The forms the replay showed didn't need blocking (heredoc bodies, git stash push, a for-loop variable passed to wsl) became must-allow cases, next to the must-block ones. There are 77 examples now, and I run them before installing any rule change. The full replay is a separate option on the same test script.

Collapse
 
paullovelace profile image
Paul Lovelace •

1,123 nested bash processes is the kind of number that makes you laugh and then immediately check your own setup. The line that stuck with me is 'writing a rule down isn't enough when someone's in a hurry' — that's true of humans too, not just agents, and it's why the hook-before-execution approach feels right. The replay tuning is the part I'd copy though: 587 hits down to 181 by letting through the forms that didn't need blocking. Did the 10-second timeout blocks ever bite you on a legitimately slow command, or has that been quiet so far?

Collapse
 
maruhana profile image
maruhana •

The 10 seconds is a limit on the hook, not on the command, so a slow command never runs into it: the hook only reads the command string and answers before the command starts. It has fired 5 times in those 13 days, each time because the hook itself didn't finish within 10 seconds. I haven't looked into why those particular calls were slow. The command was blocked without running. In 4 of the 5 the session ran it again, or a simpler version of it, on its next call and it went through; in the other it moved on to a different check. Since the article's 13 days it has happened twice more, back to back in one session: the second call was a smaller command, and when it was blocked too, the same smaller command went through on the next call. So it has cost a retry now and then, but nothing has been lost so far.

Collapse
 
aheadkit profile image
AheadKit •

The notes part is what got me. You had the mistake written down, the list was right there for every one of them to read, and it still happened, because reading is not the same as stopping. I hit the same wall from the other side: my agent kept fixing things nobody asked it to fix, renaming a function here, rewriting a file to fix one line there, and I spent the afternoon undoing the extra work. Writing the rule down helped, but only once the rule said exactly what "done" means. The one I reach for first says change the smallest amount of code that fixes the problem properly, not the smallest amount that makes the test pass. One change, one reason. A bug fix does not also rename variables. I tried it on three small demo projects before trusting it, and the clearest before and after was a Node program that used to die on a missing config file with a raw stack dump and now exits with one plain sentence naming the problem and the fix. The full rule is free to read if it helps: aheadkit.me/free-samples/lean-code...

Which of your nine rules was the hardest to tune without it blocking real work? The listening-port one at 129 hits looks like the one that would have driven me mad.

Collapse
 
maruhana profile image
maruhana • • Edited

I never changed the listen-state rule itself. Its 129 hits weren't false alarms: they were sessions really checking a server with the method that comes back empty on this PC. In the 13 days I counted, sessions let it through 3 times with # pitfall-ok, when they meant to use it. The tuning was about forms that didn't need blocking at all, across the rules: heredoc bodies, git stash push, rm -rf with a variable assigned just before it, and a for variable passed to wsl. It's ten rules now; the tenth blocks Windows paths with backslashes inside sed or Python strings.

Collapse
 
halfcycle profile image
Halfcycle •

Moving known mistakes out of the notes and into something that runs before the command is the step most people never take: a note is advice the agent can skip. "I can't count how often a session avoided a mistake because it had read the rule" is honest, and it's the right limit to name. The next thing we'd want is for the list to travel. In Halfcycle the cause and the fix from one project reach every other project automatically, so most of these never show up again and the rest arrive with the answer attached. If you try npx halfcycle on a Windows project, we'd like to know what it misses that your ten rules catch.

Collapse
 
paul_irolla_6b5ae261d0224 profile image
Paul Irolla •

A PreToolUse hook is a local check, so the rule itself costs no model tokens. What it prints back is what the model reads, so keep the message short.

Collapse
 
maruhana profile image
maruhana •

Agreed. Mine are four lines each: what matched and why it's a problem, the fix, and then either the # pitfall-ok way through or, in the backtick hook, a line about the original accident. That last one I could probably drop.

Collapse
 
the_daily_flare profile image
The daily flare •

1,123 nested bash processes would have left my own PC with no memory to spare.