Skip to content

Run Bash-matched PreToolUse hooks on monitor commands - #439

Merged
ilovepixelart merged 1 commit into
mainfrom
fix/bash-matcher-covers-monitor
Nov 15, 2026
Merged

ilovepixelart merged 1 commit into
mainfrom
fix/bash-matcher-covers-monitor

Conversation

@ilovepixelart

Copy link
Copy Markdown
Owner

A PreToolUse hook group with a Bash matcher did not run for a monitor call, so a Bash guard did not see a shell command started through monitor. Under {"matcher": "Bash", "hooks": [{"command": "guard.sh"}]} with a guard that exits 2, a bash call was blocked and monitor({command: ...}) ran its command.

A monitor call is now also offered, as a Bash call, to the PreToolUse groups whose matcher meets Bash but not Monitor:

  • The payload is tool_name: "Bash" with tool_input: {command, description}.
  • Bash|Read, ^Bash$ and Bash.* count as Bash matchers. A group whose matcher meets Monitor itself (Monitor, Bash|Monitor, *, no matcher) runs once, with the Monitor payload, as before.
  • A blocking decision from the Bash-matched hook blocks the monitor call. Its updatedInput is not applied to the monitor input.
  • In a Bash-matched group, a hook whose if names Monitor does not run.
  • A PostToolUse hook with a Bash matcher does not run for monitor (unchanged, now tested).

This is the behaviour of Claude Code 2.1.325 for the same thirteen hook configurations on one Monitor call; tests/hooks-monitor-bash.test.ts holds the table.

Hooks from the Bash pass run after the hooks matched as Monitor. An identical handler placed in both a Bash group and a Monitor group runs once per payload.

  • npm run check passes (Biome, strict tsc, Vitest)
  • Tests cover the change

A PreToolUse group with `"matcher": "Bash"` did not run for a monitor
call, so a Bash guard that exits 2 blocked `bash({command: "rm -rf x"})`
and let `monitor({command: "rm -rf x"})` start the command. Only the
`if: "Bash(...)"` filter covered monitor; the group matcher saw the
names `monitor` and `Monitor` alone.

A monitor call is now also offered to the groups whose matcher meets
Bash and not Monitor, as Claude Code does:

- The hook receives a Bash call: `tool_name` is `Bash` and `tool_input`
  holds `command` and `description`.
- Its `if` is judged as a Bash call, so `Bash(echo *)` filters on the
  command and a hook whose `if` names Monitor does not run.
- A block (exit 2, `permissionDecision: "deny"`, `decision: "block"`)
  blocks the monitor call. `updatedInput` is not applied to the monitor
  input, and `allow` changes nothing.
- A group whose matcher meets Monitor itself (`Monitor`, `Bash|Monitor`,
  `*`, no matcher) runs once, with the Monitor payload, as before.

PostToolUse is unchanged: a Bash matcher does not run for monitor.
Other tools are unchanged.
@sonarqubecloud

Copy link
Copy Markdown

@ilovepixelart
ilovepixelart merged commit 3662956 into main Nov 15, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant