I found a gap in the hooks I use to keep coding agents away from a few dangerous commands, and since the change that closed it was small, both fit in one post. The guards are three Python scripts that Claude Code runs as PreToolUse hooks on the Bash tool (the setup is close to the one in the post about blocking force pushes), and between them they deny git commands that would throw away uncommitted edits, a foreground git commit, and raw gh issue create outside the skill meant for it. I had been treating that as covering everything an agent runs, which turned out not to be true.
What PreToolUse doesn’t see
A Claude Code skill (or a slash command, since custom commands were merged into skills) can run shell commands while it loads, with !`cmd` inline or a ```! block, and put their output into the prompt. That’s handy for pulling git status or a diff into a skill before the model reads it, but those commands are run by the skill loader, not by the Bash tool, so a PreToolUse hook with a Bash matcher never hears about them.
They aren’t completely unchecked. Each one goes through your permission rules first, a deny rule aborts the whole skill, and outside auto mode anything that isn’t allowed aborts it too, which is why skills that run git inline pre-approve it with allowed-tools. So the gap is in commands your rules allow, which is where the guards are supposed to catch the dangerous variants. We can see it with a throwaway repo that has one edited file, and a throwaway skill that does something the guards should stop:
---
name: preskill-deny
description: Throwaway test skill for inline shell guards
allowed-tools: Bash(git:*)
---
Print each RESULT line below exactly as it appears, then stop. Do not run any tools.
RESULT-DISCARD: !`git -C /tmp/preskill-test/repo checkout -- f.txt && echo DISCARD-RAN`
RESULT-COMMIT: !`git -C /tmp/preskill-test/repo commit --dry-run -m x && echo COMMIT-RAN`
RESULT-ECHO: !`echo ECHO-RAN`
With only the PreToolUse hooks registered, invoking /preskill-deny failed to load at all, because the commit --dry-run couldn’t take git’s index.lock while another git process (most likely the checkout) held it. The edit in f.txt was gone anyway, so the skill erroring out didn’t save it, and no hook fired at any point. The same command sent through the Bash tool gets denied every time, so the guard was fine, it just wasn’t being asked.
A skill can run any command your permission rules allow before the model sees a single line of it, and your Bash hooks are not in that path.
The PreSkillShellCommand event
Claude Code 2.1.332 added a hook event for exactly this, PreSkillShellCommand, which fires before each inline command that passed the permission check. I expected to need a small adapter script, since a new event usually means a new payload, but the hooks docs say the command is in the same tool_input.command field and the hook can answer with the same JSON a PreToolUse hook returns. The payload has a few extra fields (command_name, command_source, invoked_by) and no run_in_background, but the guards only read the command, so registering the same three scripts a second time was the whole fix:
"PreSkillShellCommand": [
{
"matcher": "Bash|PowerShell",
"hooks": [
{"type": "command", "command": "python3 \"$HOME/.claude/hooks/guard-git-discard.py\"", "timeout": 15},
{"type": "command", "command": "python3 \"$HOME/.claude/hooks/guard-gh-issue-writes.py\"", "timeout": 10},
{"type": "command", "command": "python3 \"$HOME/.claude/hooks/guard-run-hazards.py\"", "timeout": 10}
]
}
]
This goes under hooks in a settings file (~/.claude/settings.json here), and a plugin works too, but a skill’s own frontmatter or an agent definition doesn’t, since the docs say those hooks are skipped on a skill’s first load in a session, inside subagents and for forked skills. The matcher includes PowerShell because a skill’s inline commands run through either the Bash tool or the PowerShell tool, depending on its shell setting and what’s installed, and the guards parse the command text the same way for both.
Keep the PreToolUse entry as well. In auto mode an inline command that would need approval doesn’t run while the skill loads, and the skill instead tells Claude to run it, which then goes through the normal tool call and PreToolUse, so we need both.
Running /preskill-deny again with this in place, the discard and the commit came back as
Not run in /preskill-deny: git -C /tmp/preskill-test/repo checkout -- f.txt && echo DISCARD-RAN
(blocked by hook: Blocked: this would discard uncommitted changes in files that are currently modified: - f.txt ...)
and the hook didn’t block the echo in the same skill, or a second skill with only git status and echo in it. The cost is small, too. Three Python guards running in parallel take about 33 ms per inline command on my machine (median of 30 runs), and 16.5 ms of each guard’s 22-27 ms is just Python starting up.
What behaves differently on this event
The scripts are the same, but a few of their answers land differently while a skill is loading.
An ask is a block. The discard guard answers ask to a force-push rather than deny, so that I can approve one I really meant. A loading skill has nowhere to show that prompt, so Claude Code treats ask (and defer) like deny, and the message for a skill with git push --force in it says the hook asked for approval, which a skill can’t wait for. If you have a hook that relies on asking, it won’t ask here.
Every inline git commit is blocked. The run-hazards guard only lets a commit through when it runs in the background, and an inline command has no way to ask for that, so on this event the guard denies every commit. That’s intended (commit through a normal Bash tool call instead), and it’s the same rule that blocked my own foreground git commit while setting up the throwaway repo :|
A hook that crashes or times out lets the command run, and when you invoke the skill from the terminal you’ll see a Ran anyway in /<skill> line, unless the hook entry sets onFailure: "block", which the docs recommend for a hook that has to block. My guards fail open on purpose, since a broken Python install shouldn’t stop every skill from loading, so I left it off, but if your guard is a security control, set it. Note that it only covers a hook that fails or times out, so a script that catches its own exceptions and exits 0 still fails open whatever onFailure says.
Skills from an MCP server don’t run ! commands at all, so there’s nothing to guard there.
Older versions ignore it
A version before 2.1.332 runs inline commands without calling the hook, and opens each interactive terminal session with an Unknown hook event warning, so at least you’ll notice in the terminal. The docs also mention a requiredMinimumVersion setting for enforcing the floor. The place this is easy to miss is the Claude desktop app, which runs its own bundled Claude Code that can lag behind the CLI on your PATH (the same split as with AGENTS.md). On the day I set this up it was still on 2.1.331, and its settings validation refused the new event name outright, so I applied the settings by hand and did the test runs with the CLI.
Testing both payloads
Since the scripts didn’t change, what could break later is a guard edit that starts reading hook_event_name or one of the new fields and stops matching the skill payload. So it’s worth running each guard’s deny and allow cases twice, once with a PreToolUse payload and once with a PreSkillShellCommand one, expecting the same answer except for the backgrounded commit, which flips to deny. If the matcher includes PowerShell, give some of those rows that tool name too, since an edit keyed on tool_name would otherwise pass both.
A hook only covers the path it’s registered on, and “the agent ran a command” has more than one path in Claude Code now. Registering the same guards on PreSkillShellCommand, in a settings file, closes the skill-loading path. The parts that surprised me were that an ask turns into a block there, and that whichever Claude Code a session runs, desktop app included, has to be new enough to know about the event at all.