fix(rtk): never block a Bash call when rtk itself fails

The hook ran `exec rtk hook claude`, so rtk's exit code became the hook's.
Claude Code treats a PreToolUse exit 2 as "block this tool call", and rtk
(clap) exits 2 on a usage error, e.g. an older rtk without `hook claude`.
On such a machine every Bash call would have been blocked.

The hook now captures rtk's stdout and always exits 0. On any non-zero rtk
exit it prints nothing, so the call proceeds unchanged. On success it prints
rtk's output only when non-empty, with no stray blank line. stdin still
reaches rtk.

Tests cover a stub rtk exiting 2 with stderr, exiting 1, and exiting 0 with
no output, and note that the real-rtk pass-through case depends on rtk
0.42.4's built-in rules.

Refs: #179

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014uGZTNugst7K9Bd2FTR4Mi
This commit is contained in:
Defame1297andClaude Opus 5.5 committed 2026-12-06 07:55:22 +00:00
1 parent fe7184f4f3
commit cc1d396d47
2 files changed
+33 -2

No files matched your search

+8 -2
View File
@@ -1,7 +1,10 @@
#!/usr/bin/env bash
# PreToolUse hook: route a Bash tool call through `rtk hook claude`, which may
# rewrite the command to its rtk-prefixed form. rtk's output is passed through
# unchanged. Without rtk on PATH it does nothing. Limits: the rtk plugin's README.
# when rtk succeeds. The hook always exits 0: a PreToolUse exit 2 blocks the tool
# call, and rtk exits 2 on a usage error (e.g. an rtk without `hook claude`), so
# any rtk failure is swallowed and the call proceeds unchanged. Without rtk on
# PATH it does nothing. Limits: the rtk plugin's README.
set -euo pipefail
# apm could deploy this file to other runtimes, where it must do nothing.
@@ -13,4 +16,7 @@ esac
command -v rtk > /dev/null 2>&1 || exit 0
exec rtk hook claude
# stdin (the hook payload) is inherited by rtk inside the substitution.
out="$(rtk hook claude)" || exit 0
[[ -z "$out" ]] || printf '%s\n' "$out"
exit 0