Skip to content

fix(hooks): read PostToolUse payload from stdin instead of $TOOL_INPUT env var#598

Open
amitvijapur wants to merge 1 commit into
Egonex-AI:mainfrom
amitvijapur:fix/posttooluse-hook-stdin
Open

fix(hooks): read PostToolUse payload from stdin instead of $TOOL_INPUT env var#598
amitvijapur wants to merge 1 commit into
Egonex-AI:mainfrom
amitvijapur:fix/posttooluse-hook-stdin

Conversation

@amitvijapur

Copy link
Copy Markdown

Fixes #594.

Root cause

The PostToolUse auto-update hook in understand-anything-plugin/hooks/hooks.json detects git commit/merge/cherry-pick/rebase by reading "$TOOL_INPUT". Claude Code doesn't set a TOOL_INPUT environment variable for hook processes — hook input is delivered as JSON on stdin (tool_name, tool_input, cwd, ...). So the grep -qE always tests an empty string, the && chain short-circuits on the first check, and the trailing || true exits clean — the per-commit auto-update silently never fires, on any OS. The SessionStart staleness hook is unaffected (it only reads files + git rev-parse), so in practice this degrades freshness from per-commit to session-start granularity with no visible error.

Fix

Capture stdin into INPUT, extract tool_input.command via a small node -e script, then run the same grep check against that instead of the env var. This mirrors the JSON-parsing style the SessionStart hook already uses (node -p) and avoids adding a jq dependency, which isn't guaranteed present on Windows/Git Bash hosts. Everything downstream — UA_DIR resolution, config.json/knowledge-graph.json gating — is untouched.

Verification

Piped a realistic PostToolUse JSON payload (tool_name: "Bash", tool_input.command: "git commit -m ...") into both the old and new hook command via stdin, with no TOOL_INPUT env var set (matching real Claude Code invocation):

  • Before: empty stdout, hook never fires.
  • After: [understand-anything] Commit detected with auto-update enabled. You MUST read the file at .../hooks/auto-update-prompt.md ... — hook fires correctly.

Also checked: git merge/cherry-pick/rebase trigger correctly; non-matching commands and non-Bash tool payloads (no tool_input.command) stay silent; commit messages with embedded quotes don't break JSON parsing; malformed stdin doesn't crash the hook; autoUpdate: false and missing knowledge-graph.json still correctly suppress the hook.

Testing

This is a config-only change (no TypeScript/JS source touched, and no test suite references hooks.json or TOOL_INPUT), so I verified by exercising the actual shell command directly against simulated hook payloads rather than through pnpm test. JSON validity of the edited file confirmed.

…T env var

The PostToolUse auto-update hook read "$TOOL_INPUT" to detect git
commit/merge/cherry-pick/rebase commands, but Claude Code never sets a
TOOL_INPUT environment variable for hook processes — hook input is
delivered as JSON on stdin (fields tool_name, tool_input, cwd, ...).
The grep therefore always tested an empty string, the && chain
short-circuited, and the trailing || true swallowed the failure: the
per-commit auto-update silently never fired, on any OS.

Capture stdin into INPUT, then extract tool_input.command via a small
node -e script (matching the JSON-parsing style already used by the
SessionStart hook's node -p call, and requiring no new dependency —
jq isn't guaranteed present on Windows/Git Bash hosts). The extracted
command is piped into the same grep check as before; everything
downstream (UA_DIR resolution, config.json/knowledge-graph.json gating)
is unchanged.

Verified by piping a realistic PostToolUse JSON payload into both the
old and new hook commands: the old command produces no output (bug
reproduced), the new one prints the auto-update instruction. Also
checked git merge/cherry-pick/rebase trigger correctly, non-matching
commands and non-Bash tool payloads (no tool_input.command field) stay
silent, embedded quotes in commit messages don't break JSON parsing,
and the autoUpdate:false / missing knowledge-graph.json gates still
suppress the hook as before.

Fixes Egonex-AI#594.
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.

PostToolUse auto-update hook never fires on Claude Code: reads $TOOL_INPUT env var, but hook input arrives on stdin

1 participant