Skip to content

fix: read tool_response instead of tool_output in PostToolUse hook#561

Merged
rohitg00 merged 2 commits into
rohitg00:mainfrom
faraz152:fix/post-tool-use-tool-response-field
May 20, 2026
Merged

fix: read tool_response instead of tool_output in PostToolUse hook#561
rohitg00 merged 2 commits into
rohitg00:mainfrom
faraz152:fix/post-tool-use-tool-response-field

Conversation

@faraz152
Copy link
Copy Markdown

@faraz152 faraz152 commented May 20, 2026

Root cause

src/hooks/post-tool-use.ts (and its compiled counterpart plugin/scripts/post-tool-use.mjs) read the tool output from data.tool_output:

const { imageData, cleanOutput } = extractImageData(data.tool_output);

But Claude Code's PostToolUse hook payload uses tool_response, not tool_output. This means cleanOutput is always undefined, the observe request reaches the server with no output value, and mem::compress fails schema validation on every real tool call — narrative and facts are empty so the XML schema check fails.

Captured real PostToolUse payload from Claude Code (from #539):

{
  "hook_event_name": "PostToolUse",
  "tool_name": "Bash",
  "tool_input": { "command": "echo hi" },
  "tool_response": { "stdout": "hi\n", "stderr": "", "interrupted": false }
}

Fix

Read data.tool_response with data.tool_output as a fallback so any older integration still using the legacy field name keeps working:

const { imageData, cleanOutput } = extractImageData(
  data.tool_response ?? data.tool_output,
);

Same one-liner applied to the compiled plugin/scripts/post-tool-use.mjs.

Impact

Without this fix mem::compress silently fails on ~100% of real Claude Code tool calls, degrading memory_recall output and starving the consolidation pipeline of compressed observations.

Fixes #539

Summary by CodeRabbit

  • Bug Fixes
    • Improved tool response processing to prefer the newer response field when present, falling back to legacy output otherwise.
    • Ensures image data and cleaned output are correctly extracted and included in downstream observations, improving compatibility and reliability across response formats.

Review Change Stack

Claude Code's PostToolUse payload sends the field as `tool_response`,
not `tool_output`. The hook was reading `data.tool_output` which is
always undefined, so `cleanOutput` was undefined, the observe request
contained no `tool_output` value, and mem::compress consistently failed
its XML schema validation (requires narrative >= 10 chars + facts >= 1).

Fix: read `data.tool_response` with `data.tool_output` as a fallback
so older integrations that emit the legacy field name keep working.

Fixes rohitg00#539
@vercel
Copy link
Copy Markdown

vercel Bot commented May 20, 2026

@faraz152 is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b932002d-5979-44c7-96d3-59c93a65b8b6

📥 Commits

Reviewing files that changed from the base of the PR and between bb51ec0 and d0dc2ac.

📒 Files selected for processing (1)
  • src/hooks/post-tool-use.ts
💤 Files with no reviewable changes (1)
  • src/hooks/post-tool-use.ts

📝 Walkthrough

Walkthrough

Both the TypeScript hook and JavaScript script now use data.tool_response ?? data.tool_output when extracting image data and cleaning tool output, ensuring the code reads Claude Code's tool_response field while preserving legacy compatibility.

Changes

Tool Response Compatibility

Layer / File(s) Summary
Tool response field fallback with backward compatibility
src/hooks/post-tool-use.ts, plugin/scripts/post-tool-use.mjs
Both files now use data.tool_response ?? data.tool_output as the input to extractImageData, switching which field is scanned and sanitized for embedded/base64 image data while keeping backward compatibility with legacy payloads.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A rabbit finds a field named two,
response first, then output too.
It sniffs for pictures, cleans the rest,
Hops on quietly — backward-compatible best.
🐇✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: fixing the PostToolUse hook to read tool_response instead of tool_output, which directly addresses the primary bug in the PR.
Linked Issues check ✅ Passed The PR fully satisfies issue #539 by implementing the proposed patch to use data.tool_response with data.tool_output as fallback in both source and compiled files, fixing the mem::compress failure.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the tool_response/tool_output field-name mismatch in the PostToolUse hook; no unrelated modifications are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/hooks/post-tool-use.ts (1)

35-36: ⚡ Quick win

Remove the explanatory WHAT comment and rely on clear code instead.

The fallback expression is already self-explanatory; please drop the inline explanatory comment to align with repository style rules.

As per coding guidelines, "Avoid code comments explaining WHAT — use clear naming instead".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/hooks/post-tool-use.ts` around lines 35 - 36, Remove the explanatory
inline comment and rely on the clear fallback expression itself: delete the
comment line that describes "Claude Code's PostToolUse payload uses
`tool_response`; fall back to `tool_output`…" and keep the existing fallback
expression that references `tool_response` and `tool_output` unchanged so the
code reads cleanly without a WHAT-style comment.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/hooks/post-tool-use.ts`:
- Around line 35-36: Remove the explanatory inline comment and rely on the clear
fallback expression itself: delete the comment line that describes "Claude
Code's PostToolUse payload uses `tool_response`; fall back to `tool_output`…"
and keep the existing fallback expression that references `tool_response` and
`tool_output` unchanged so the code reads cleanly without a WHAT-style comment.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 035c737b-679f-494d-a29e-af86185049f8

📥 Commits

Reviewing files that changed from the base of the PR and between 1838f4d and bb51ec0.

📒 Files selected for processing (2)
  • plugin/scripts/post-tool-use.mjs
  • src/hooks/post-tool-use.ts

@rohitg00 rohitg00 merged commit 3cb7f90 into rohitg00:main May 20, 2026
1 of 2 checks passed
@rohitg00
Copy link
Copy Markdown
Owner

Thanks @faraz152 — clean root-cause writeup with a captured payload, exactly the kind of repro that makes review easy. Merged.

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.

[BUG] PostToolUse hook reads data.tool_output but Claude Code sends tool_response — compresses fail XML schema (~47% loss)

2 participants