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

+25
View File
@@ -62,6 +62,29 @@ if [[ $RC -eq 0 && "$OUT" == stub-output ]]; then pass; else fail "want rc 0 and
if [[ "$(cat "$WORK/stub-args" 2> /dev/null)" == "hook claude" ]]; then pass; else fail "stub args: $(cat "$WORK/stub-args" 2> /dev/null)"; fi
if [[ "$(cat "$WORK/stub-stdin" 2> /dev/null)" == "$(payload "git status")" ]]; then pass; else fail "stub stdin: $(cat "$WORK/stub-stdin" 2> /dev/null)"; fi
# stub_rtk <name> <body>: writes a stub rtk with <body> to $WORK/<name>/rtk.
stub_rtk() {
mkdir -p "$WORK/$1"
printf '#!/usr/bin/env bash\ncat > /dev/null\n%s\n' "$2" > "$WORK/$1/rtk"
chmod +x "$WORK/$1/rtk"
}
stub_rtk fail2-bin "echo 'error: unrecognized subcommand' >&2; exit 2"
stub_rtk fail1-bin "exit 1"
stub_rtk silent-bin "exit 0"
echo "== rtk exits 2 (usage error): exit 0, no stdout, never blocks"
run "$HOOK" "$WORK/fail2-bin:$WORK/base-bin" "git status"
if [[ $RC -eq 0 && -z "$OUT" ]]; then pass; else fail "rtk exit 2: want exit 0 and no stdout, got $RC: $OUT"; fi
echo "== rtk exits 1: exit 0, no stdout"
run "$HOOK" "$WORK/fail1-bin:$WORK/base-bin" "git status"
if [[ $RC -eq 0 && -z "$OUT" ]]; then pass; else fail "rtk exit 1: want exit 0 and no stdout, got $RC: $OUT"; fi
echo "== rtk prints nothing: exit 0, no stdout at all (not even a newline)"
RC=0
payload "git status" | PATH="$WORK/silent-bin:$WORK/base-bin" "$TIMEOUT" 10 "$HOOK" > "$WORK/raw-out" 2> "$WORK/stderr" || RC=$?
if [[ $RC -eq 0 && ! -s "$WORK/raw-out" ]]; then pass; else fail "silent rtk: want exit 0 and zero-byte stdout, got $RC: $(od -c "$WORK/raw-out")"; fi
echo "== rtk absent: exit 0, no output"
run "$HOOK" "$WORK/base-bin" "git status"
if [[ $RC -eq 0 && -z "$OUT" && ! -s "$WORK/stderr" ]]; then pass; else fail "want silent exit 0, got $RC: $OUT $(cat "$WORK/stderr")"; fi
@@ -78,6 +101,8 @@ else
REAL_PATH="$(dirname "$REAL_RTK"):$WORK/base-bin"
run "$HOOK" "$REAL_PATH" "git status"
if [[ $RC -eq 0 && "$OUT" == *'"command":"rtk git status"'* ]]; then pass; else fail "git status: want rewrite, got $RC: $OUT"; fi
# Depends on rtk's built-in rules as of 0.42.4: `echo` has no rewrite rule,
# so rtk passes it through with no output. A later rtk may add one.
run "$HOOK" "$REAL_PATH" "echo hello"
if [[ $RC -eq 0 && -z "$OUT" ]]; then pass; else fail "echo hello: want no output, got $RC: $OUT"; fi
fi