claude-code - 💡(How to fix) Fix PreToolUse hook with matcher: "Agent" does not fire on parent Agent tool dispatch [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#56151Fetched 2026-05-05 05:56:51
View on GitHub
Comments
1
Participants
2
Timeline
6
Reactions
0
Timeline (top)
labeled ×5commented ×1

Fix Action

Fix / Workaround

A PreToolUse hook registered with matcher: "Agent" is never invoked when the main session dispatches the Agent tool to spawn a subagent. The hook script does not execute at all — not "fires but updatedInput ignored" (#39814) and not "fires but exit code ignored" (#40580). The script simply never runs.

PreToolUse hooks matching the Agent tool should be invoked before the subagent is dispatched, the same way PreToolUse:Bash hooks fire before Bash commands.

Cannot enforce a model/prompt gate on subagent dispatch from the parent session. In our case: a hook that injects `model: "haiku"` and a `CONFIDENCE:` suffix for `Explore` subagent calls, plus a TSV telemetry row per dispatch. None of it executes.

Code Example

#!/usr/bin/env bash
   set -euo pipefail
   date -u '+%FT%TZ' >> /tmp/agent-hook-fired.log
   input=$(cat)
   # ... rest of hook ...

---

"PreToolUse": [
     { "matcher": "Agent",
       "hooks": [{ "type": "command", "command": "/path/to/script.sh" }] }
   ]
RAW_BUFFERClick to expand / collapse

Filed by Claude Code (claude-opus-4-7, 1M context) on behalf of the user.

What's Wrong?

A PreToolUse hook registered with matcher: "Agent" is never invoked when the main session dispatches the Agent tool to spawn a subagent. The hook script does not execute at all — not "fires but updatedInput ignored" (#39814) and not "fires but exit code ignored" (#40580). The script simply never runs.

What Should Happen?

PreToolUse hooks matching the Agent tool should be invoked before the subagent is dispatched, the same way PreToolUse:Bash hooks fire before Bash commands.

Repro

  1. Hook script with an unconditional sentinel write at the very top, before any matcher logic, before reading stdin:

    #!/usr/bin/env bash
    set -euo pipefail
    date -u '+%FT%TZ' >> /tmp/agent-hook-fired.log
    input=$(cat)
    # ... rest of hook ...
  2. Register in .claude/settings.json:

    "PreToolUse": [
      { "matcher": "Agent",
        "hooks": [{ "type": "command", "command": "/path/to/script.sh" }] }
    ]
  3. Reset sentinel: : > /tmp/agent-hook-fired.log

  4. From the main session, spawn a subagent: Agent(subagent_type="Explore", prompt="...")

  5. Inspect /tmp/agent-hook-fired.log0 bytes. Script never ran.

Manual invocation works: echo '{"tool_name":"Agent","tool_input":{"subagent_type":"Explore","prompt":"x"}}' | bash script.sh → writes sentinel, returns proper `updatedInput` JSON, exit 0.

Variations Tested

  • `matcher: "Agent"` — does not fire
  • `matcher: "Task"` (legacy tool name) — does not fire
  • Project-level settings (`.claude/settings.json`) — does not fire
  • Same project's `PreToolUse:Bash` hook fires reliably in the same session, so hook infrastructure itself is healthy.

Impact

Cannot enforce a model/prompt gate on subagent dispatch from the parent session. In our case: a hook that injects `model: "haiku"` and a `CONFIDENCE:` suffix for `Explore` subagent calls, plus a TSV telemetry row per dispatch. None of it executes.

Environment

  • macOS Darwin 25.2.0
  • Claude Code, model: claude-opus-4-7 (1M context)
  • Hook command paths use `$CLAUDE_PROJECT_DIR`, settings file mtime predates session start (config was loaded at SessionStart).

Related

  • #39814 — `updatedInput` ignored for Agent (hook fires; my hook does not fire)
  • #40580 — exit code ignored for subagent's own tool calls (subagent-internal; mine is parent-side)
  • #44534 — deny not enforced for Agent (similar surface, different symptom)

This issue was drafted and filed by Claude Code (claude-opus-4-7, 1M context) at the user's request, after empirically reproducing the bug in their session.

extent analysis

TL;DR

The PreToolUse hook with a matcher for "Agent" is not being invoked when spawning a subagent, suggesting a potential issue with the hook registration or the tool dispatch mechanism.

Guidance

  • Verify that the .claude/settings.json file is correctly formatted and loaded by the system, ensuring the PreToolUse hook is properly registered.
  • Check if there are any other hooks or settings that might be interfering with the PreToolUse hook for the "Agent" tool.
  • Test the hook with a different matcher, such as "Bash", to see if the issue is specific to the "Agent" tool.
  • Investigate if there are any known issues or limitations with the PreToolUse hook mechanism in the Claude Code system.

Example

No code snippet is provided as the issue seems to be related to the hook registration or the tool dispatch mechanism rather than a code-specific problem.

Notes

The issue might be related to the specific version of Claude Code (claude-opus-4-7) or the environment (macOS Darwin 25.2.0) being used. Further investigation is needed to determine the root cause.

Recommendation

Apply a workaround by testing the hook with a different matcher or tool to isolate the issue, as the problem seems to be specific to the "Agent" tool.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix PreToolUse hook with matcher: "Agent" does not fire on parent Agent tool dispatch [1 comments, 2 participants]