fix(hooks): read PostToolUse payload from stdin instead of $TOOL_INPUT env var#598
Open
amitvijapur wants to merge 1 commit into
Open
fix(hooks): read PostToolUse payload from stdin instead of $TOOL_INPUT env var#598amitvijapur wants to merge 1 commit into
amitvijapur wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #594.
Root cause
The
PostToolUseauto-update hook inunderstand-anything-plugin/hooks/hooks.jsondetectsgit commit/merge/cherry-pick/rebaseby reading"$TOOL_INPUT". Claude Code doesn't set aTOOL_INPUTenvironment variable for hook processes — hook input is delivered as JSON on stdin (tool_name,tool_input,cwd, ...). So thegrep -qEalways tests an empty string, the&&chain short-circuits on the first check, and the trailing|| trueexits clean — the per-commit auto-update silently never fires, on any OS. TheSessionStartstaleness 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, extracttool_input.commandvia a smallnode -escript, then run the samegrepcheck against that instead of the env var. This mirrors the JSON-parsing style theSessionStarthook already uses (node -p) and avoids adding ajqdependency, which isn't guaranteed present on Windows/Git Bash hosts. Everything downstream —UA_DIRresolution,config.json/knowledge-graph.jsongating — is untouched.Verification
Piped a realistic
PostToolUseJSON payload (tool_name: "Bash",tool_input.command: "git commit -m ...") into both the old and new hook command via stdin, with noTOOL_INPUTenv var set (matching real Claude Code invocation):[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/rebasetrigger correctly; non-matching commands and non-Bash tool payloads (notool_input.command) stay silent; commit messages with embedded quotes don't break JSON parsing; malformed stdin doesn't crash the hook;autoUpdate: falseand missingknowledge-graph.jsonstill correctly suppress the hook.Testing
This is a config-only change (no TypeScript/JS source touched, and no test suite references
hooks.jsonorTOOL_INPUT), so I verified by exercising the actual shell command directly against simulated hook payloads rather than throughpnpm test. JSON validity of the edited file confirmed.