Repository navigation
fix: start the capture guard's Node process only for calls it can block - #91
Conversation
This fixes a bunch of messages in Claude Code: PreToolUse:Bash hook error Failed with non-blocking status code: /bin/sh: node: command not found * Introduced guard-capture.sh to manage plugin hooks and ensure Node.js is available for execution. * Updated hooks.json to reference the new shell script instead of the previous JavaScript file. * Modified tests to validate the functionality of the new guard mechanism and ensure proper blocking behavior without Node.js in the PATH.
|
🔱 Poseidon started reviewing this PR — central run. |
| # Only tool_input can trip a rule; the transcript path always ends in .jsonl and the cwd can hold any word. Inside a JSON string a key's quotes are escaped, so this never cuts tool_input. | ||
| printf '%s' "$input" \ | ||
| | sed -E 's/"(transcript_path|agent_transcript_path|cwd)"[[:space:]]*:[[:space:]]*"[^"]*"//g' \ | ||
| | grep -qiE 'capture|links|jsonl|kosh-reports' || exit 0 |
There was a problem hiding this comment.
The pre-filter greps the whole tool_input, so an Edit/Write/MultiEdit whose content, old_string or new_string contains links, capture or jsonl gets through to Node. guard-capture.js never reads those fields: for edit tools it only tests args.file_path (hooks/guard-capture.js:60). For the users this PR targets (no node on the hook's PATH), that call now hits exit 2. So in any project, even with kosh unused, Claude can't write a README, HTML template or JS file that mentions "links" or "capture". Before this PR the same call went through with a non-blocking error; now it's a hard block. Match only the fields the guard reads, e.g. grep -oE '"(file_path|command)"[[:space:]]*:[[:space:]]*"([^"\\]|\\.)*"' | grep -qiE 'capture|links|jsonl|kosh-reports' || exit 0. That is safe for the same reason the existing sed is: a key quoted inside a JSON string is always escaped.
🤖 Fix prompt for AI agents
Verify each finding against the current code and only fix it if
needed.
In `hooks/guard-capture.sh`:
- Line 7: The pre-filter pipes all of `tool_input` into
`grep -qiE 'capture|links|jsonl|kosh-reports'`. That includes Write
`content` and Edit/MultiEdit `old_string`/`new_string`, but
`blocked()` in hooks/guard-capture.js only tests `args.file_path`
for edit tools and `args.command` for Bash. When node is missing,
any edit whose text mentions "links" or "capture" therefore reaches
the `exit 2` branch and is refused in every project, a regression
from the old non-blocking error. Narrow the check to the
`file_path` and `command` values, for example by first extracting
them with `grep -oE '"(file_path|command)"[[:space:]]*:[[:space:]]*"([^"\\]|\\.)*"'`,
then applying the keyword grep. Keep the transcript_path/cwd
stripping, the `command -v node` fail-closed branch and the final
`node "$(dirname "$0")/guard-capture.js"` call unchanged. Add a test
to tests/guard-capture.test.js's no-node test showing that an Edit
whose new_string contains "links" exits 0.
The keyword grep runs over all of tool_input, including Write content and Edit old_string/new_string, but the guard only reads file_path for edit tools. With no node on PATH, an edit whose text mentions "links" or "capture" was now refused
There was a problem hiding this comment.
🔱 Poseidon Review
Summary — The latest push narrows the sh pre-filter to the command and file_path values, the only fields guard-capture.js reads, so edit text no longer trips the no-node block. No issues found on this pass; low risk.
Walkthrough
- 🐛 Fix —
hooks/guard-capture.shnow extracts"file_path"/"command"key-value pairs withgrep -oEbefore the keyword check, replacing thesedthat strippedtranscript_path/cwd - 🧪 Tests — no-node test adds an Edit and a Write whose text mentions captures, and expects exit 0
- 📝 Docs — AGENTS.md describes the narrower filter
♻️ Previously flagged
Resolved
- ✅ hooks/guard-capture.sh:L7 — filter now greps only
command/file_pathvalues (top-levelblocked()reads in hooks/guard-capture.js:L58-L63 are a subset), and a quoted key inside a string stays escaped, so it can't match
ℹ️ Review info
- Commit: d5443ab
- Files reviewed: AGENTS.md, hooks/guard-capture.sh, hooks/hooks.json, tests/guard-capture.test.js
- Files skipped: none
- Model: claude-opus-5-5
Why
kosh registers a
PreToolUsehook that runsnode hooks/guard-capture.jsbefore everyBash,Write,EditandMultiEditcall.Plugin hooks run in every project where kosh is enabled, so this hook runs even when kosh is not being used.
In my particular case, this was showing up a ton of errors I think because my Node version loads from
.zshrc, but not 100% sure about this:I was getting that "hook error" and failed message pretty often, almost on every interaction with Claude Code.
What changes
The hook still runs on every Bash, Write, Edit and MultiEdit call, but it is now a quick shell check. Node starts only when the command or file path names capture, links, jsonl or kosh-reports.
hooks/guard-capture.sh: This new script runs beforeguard-capture.js. It starts Node only when the call'scommandorfile_pathcontainscapture,links,jsonlorkosh-reports. Every rule in the guard needs one of these words, so the guard still blocks every case it blocked before.commandandfile_path, because the guard reads only those fields. Other fields, such as the transcript path or the text of an edit, do not start Node.hooks/hooks.json: The hook now runs the.shfile.Problems fixed:
/bin/sh, which does not read.zshrc. If a user loads Node through nvm or a similar tool in.zshrc,nodeis missing from the hook'sPATH. EveryBash,Write,EditandMultiEditcall then showsPreToolUse:Bash hook error … node: command not found.koshrun, the capture guard then checks nothing.Every call also starts a Node process, but most calls can never match a rule.
Tests
transcript_path, as Claude Code's real input does.node --test "tests/*.test.js"passes all 668 tests.claude --plugin-dir <path/to/updated/kosh>on a machine without Node on the hook'sPATH.git statusran with no hook error.Effect on users whose hooks cannot find Node"
Calls that do not involve
koshno longer show an error. Whenkoshneeds Node and cannot find it, a message tells the user to add Node to their PATH. For example, they can link it into~/.local/bin.Screenshots
Asking Claude to update a file:

For every Bash execution on Claude Code:

Note: You need
/focusdisabled to actually see the trail like the screenshots above.