claude-code - 💡(How to fix) Fix Plugin PostToolUse command hook (exit 2) output not injected into model context [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#46376Fetched 2026-04-11 06:21:55
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Timeline (top)
labeled ×4commented ×1

A command-type PostToolUse hook defined in a plugin's hooks.json detects a keyword in Bash output and exits with code 2 + a message. The message is not injected into the model's context, so the model proceeds without acting on it.

Error Message

The model proceeds to summarize the PR check results without invoking the skill. No hook output or error is shown in the transcript for the PostToolUse event. The hook appears to either not fire or its output is silently discarded.

  • A separate agent-type PreToolUse hook error ("Messages are required for agent hooks") does appear in the same session on the first Bash call. That error is from a different hook (not this plugin) and is tracked in #39184. It's unclear whether the agent hook error on PreToolUse prevents command-type PostToolUse hooks from running on subsequent Bash calls.

Root Cause

A command-type PostToolUse hook defined in a plugin's hooks.json detects a keyword in Bash output and exits with code 2 + a message. The message is not injected into the model's context, so the model proceeds without acting on it.

Code Example

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "${CLAUDE_PLUGIN_ROOT}/hooks/detect-sonar-in-output.sh"
          }
        ]
      }
    ]
  }
}

---

#!/usr/bin/env bash
input=$(cat)
stdout=$(echo "$input" | jq -r '.tool_result.stdout // empty')
stderr=$(echo "$input" | jq -r '.tool_result.stderr // empty')
combined="$stdout $stderr"

if echo "$combined" | grep -qi "sonarqube"; then
  echo "SonarQube detected in command output. You MUST invoke the /sonarqube skill."
  exit 2
fi
RAW_BUFFERClick to expand / collapse

Description

A command-type PostToolUse hook defined in a plugin's hooks.json detects a keyword in Bash output and exits with code 2 + a message. The message is not injected into the model's context, so the model proceeds without acting on it.

Environment

  • Claude Code 2.1.x (latest)
  • macOS Darwin 25.4.0
  • Opus 4.6 (1M context)
  • Hook defined in a plugin installed via the plugin marketplace

Plugin hooks.json

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "${CLAUDE_PLUGIN_ROOT}/hooks/detect-sonar-in-output.sh"
          }
        ]
      }
    ]
  }
}

Hook script (detect-sonar-in-output.sh)

#!/usr/bin/env bash
input=$(cat)
stdout=$(echo "$input" | jq -r '.tool_result.stdout // empty')
stderr=$(echo "$input" | jq -r '.tool_result.stderr // empty')
combined="$stdout $stderr"

if echo "$combined" | grep -qi "sonarqube"; then
  echo "SonarQube detected in command output. You MUST invoke the /sonarqube skill."
  exit 2
fi

Steps to reproduce

  1. Install a plugin with the above PostToolUse hook
  2. Ask about a PR that has a SonarQube check run: "What details can you find on this PR? https://github.com/org/repo/pull/229"
  3. The model runs gh pr checks 229 via Bash, which returns output containing "SonarQube Code Analysis"
  4. The PostToolUse hook should fire, detect "sonarqube" in the output, and exit 2 with a message

Expected behavior

The hook's stdout message is injected into the conversation as additional context, and the model acts on it (invokes the sonarqube skill).

Actual behavior

The model proceeds to summarize the PR check results without invoking the skill. No hook output or error is shown in the transcript for the PostToolUse event. The hook appears to either not fire or its output is silently discarded.

Notes

  • A separate agent-type PreToolUse hook error ("Messages are required for agent hooks") does appear in the same session on the first Bash call. That error is from a different hook (not this plugin) and is tracked in #39184. It's unclear whether the agent hook error on PreToolUse prevents command-type PostToolUse hooks from running on subsequent Bash calls.
  • The plugin's PreToolUse hook on WebFetch|Fetch (also command-type) is not triggered in this flow since the model uses Bash, not WebFetch.
  • Command-type PostToolUse hooks are documented as working in #39184's compatibility table, so this may be specific to plugin-defined hooks vs settings-defined hooks.

extent analysis

TL;DR

The PostToolUse hook's output is not being injected into the conversation, likely due to the hook's exit code or the model's handling of command-type hooks.

Guidance

  • Verify that the detect-sonar-in-output.sh script is being executed by adding a logging statement or checking the system logs for any errors.
  • Check the Claude Code documentation for any specific requirements or limitations on command-type PostToolUse hooks, particularly in relation to plugin-defined hooks.
  • Investigate whether the agent-type PreToolUse hook error is interfering with the command-type PostToolUse hook, potentially by testing with the agent hook disabled.
  • Consider modifying the detect-sonar-in-output.sh script to handle errors or exceptions differently, such as returning a different exit code or output format.

Example

# Add logging to detect-sonar-in-output.sh to verify execution
echo "Hook executed with input: $input" >&2

Notes

The issue may be specific to the interaction between plugin-defined hooks and the model's handling of command-type PostToolUse hooks. Further investigation is needed to determine the root cause.

Recommendation

Apply workaround: Modify the detect-sonar-in-output.sh script to handle errors or exceptions differently, such as returning a different exit code or output format, to see if this resolves the issue.

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…

FAQ

Expected behavior

The hook's stdout message is injected into the conversation as additional context, and the model acts on it (invokes the sonarqube skill).

Still need to ship something?

×6

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

Back to top recommendations

TRENDING