Worktree-isolation guard refuses plain git commands after a PreToolUse hook wraps them in a launcher (rtk)
Version: 2.1.291 (Claude Code), rtk 0.49.0
Platform: macOS, zsh via the Bash tool
Related: #93592 (same guard, same refusal for jj git <subcommand>)
Summary
In a worktree-isolated session, the model writes a plain git command, but the guard refuses it. The cause is the user's own PreToolUse hook, which rewrote the command into rtk git ... before the guard ran. So the refusal is about a command the model never wrote.
rtk is a token-saving output filter. Its hook returns updatedInput.command with the git command wrapped in rtk.
Setup
"PreToolUse": [{ "matcher": "Bash", "hooks": [{ "type": "command", "command": "rtk hook claude" }] }]
What the hook does to each command, from feeding it PreToolUse payloads directly:
| Command written by the model | After the hook | In an isolated worktree |
|---|---|---|
git diff |
rtk git diff |
Refused |
git -C <wt> diff |
rtk git -C <wt> diff |
Refused |
git add a |
rtk git add a |
Refused |
git add a 2>&1 | cat |
rtk git add a 2>&1 | cat |
Refused |
git push origin b |
rtk git push origin b |
Refused |
git switch b |
unchanged | Runs |
git rebase origin/master |
unchanged | Runs |
git update-index --add a |
unchanged | Runs |
git diff > f |
unchanged | Runs |
/usr/bin/git push origin b |
unchanged | Runs |
Repro
- Install the hook above.
- Enter a worktree with
EnterWorktree. - Run
git diff origin/master -- package.jsonvia the Bash tool.
Expected: the command runs. It is a git command in the session's own worktree.
Actual:
This session is isolated in the worktree <wt>, but this command runs rtk with a git command among its operands: what runs it, and from which directory or root, cannot be read here (name git right after the launcher and its options) in a plain command, so what it runs cannot be shown not to be git. Refusing to run it — a worktree-isolated session's git operations must target its own worktree. Run the plain command from <wt>.
The remedy is not actionable. The model already ran the plain command from <wt>, and the hook rewrites every retry the same way. The only way through is to dodge the user's own hook, by calling /usr/bin/git or by redirecting output. Resolving a rebase conflict in a worktree took several refused round-trips for that reason.
Proposal (either one)
- Check the command the model wrote. When a PreToolUse hook only prepends a launcher to a command, run the isolation check on the command before the rewrite.
- Allow-list transparent launchers. Add a setting such as
"worktree": { "transparentLaunchers": ["rtk"] }. The guard strips the launcher and checks the remaininggit ...like a plain command. This would also cover #93592'sjj gitif jj were listed.
Impact
Every user of rtk, or of any similar wrapper hook, loses git diff, status, add, checkout, show and push inside EnterWorktree sessions and isolation: "worktree" agents.
Same here on Claude Code 2.1.285 with rtk 0.44.2, in both isolation: "worktree" subagents and EnterWorktree sessions. Adding two things that may narrow the fix.
The guard already has a launcher set, and rtk fits it
The guard reads through some launchers and checks the git command after them. We ran each of these as its own Bash call in an isolated subagent, with commands rtk does not rewrite:
| Command | Result |
|---|---|
time git status |
Runs |
nice git status |
Runs |
nice -n 5 git status |
Runs |
nohup git status |
Runs |
stdbuf -o0 git status |
Runs |
caffeinate -i git status |
Refused: "runs caffeinate with a git command among its operands" |
rtk git status / rtk proxy git status |
Refused, same message |
So the refusal isn't about launchers in general. rtk just isn't on the list the guard knows (time, nice, nohup, env, stdbuf, timeout, sudo, …), each parsed with its own option grammar.
Narrow ask: add rtk to that set. Its grammar is small:
rtk [-v|-vv|-vvv] [--ultra-compact] [--skip-env] git <git args>
rtk [-v…] proxy git <git args>
rtk git takes git's own global options (-C, -c, --git-dir, --work-tree, --no-pager, …) with git's meaning and passes them through. The guard's existing git-redirect checks can then run unchanged on the git … that follows. rtk adds output-format flags only. It never changes the directory or repository git runs in.
A user-configurable list of transparent launchers would be the general fix for rtk, caffeinate and similar wrappers. The rtk entry alone would unblock most people hitting this today.
What works meanwhile
The guard accepts rtk on the right side of a pipe, where it only filters stdout:
git status | rtk pipe -f git-status
git log -3 | rtk pipe -f git-log
git diff | rtk pipe -f git-diff
rtk hook claude leaves those unchanged. A PreToolUse hook can therefore return updatedInput in this shape in isolated worktrees and keep most of the compression. The cost is a hidden git exit status and only three rtk pipe filters, so it doesn't work for commit, push or diff --exit-code. The real fix is still on the guard side.
Still refused on 2.1.312 (macOS, rtk 0.44.1). In local transcripts from the last two weeks, 83 sessions hit this refusal. Most were the very first command of a worktree subagent, a plain git log -1 --oneline or git status that the rtk hook rewrote to rtk git .... In one subagent every git command was refused, including git status and git add -A, so it stopped with its work uncommitted.
As noted above, the refusal's hint ("name git right after the launcher and its options") is already what the rewrite produces (rtk git log -1 --oneline), so following the hint does not clear it.
For others who hit this: rtk v1.1.0 leaves git unrewritten in Claude Code managed worktrees (rtk-ai/rtk#3879). With it, plain git status and git log run in an agent worktree without being refused. The guard itself still refuses rtk git ..., so older rtk versions and other launchers still hit this.
On older rtk versions, a workaround is to stop rtk from rewriting git at all: add exclude_commands = ["git"] under [hooks] in rtk's config.toml (on macOS, ~/Library/Application Support/rtk/config.toml). This also turns off rtk's git output filtering in every repo.
Same class on 2.1.316 (macOS, Apple Silicon, Bash tool), with a hook that does not wrap the command in a launcher but prepends a shell prologue. The data point may help scope the fix: it is the updatedInput text, not rtk's launcher as such, that the guard judges.
Setup
A settings PreToolUse hook on Bash returns updatedInput.command as a one-line prologue followed by the model's command on the next line. The prologue sets a file-size limit for a subagent's writes:
if [ -n "${BASH_VERSION-}" ] && { case ":${SHELLOPTS-}:" in *:posix:*) [ "${BASH_VERSINFO-0}" -lt 5 ];; esac; }; then ulimit -HSf 1048576; else ulimit -HSf 2097152; fi 2>/dev/null
pwd
What happens
In an EnterWorktree session, and in every subagent it spawns whose calls the hook rewrites, the guard refuses the result, for pwd, ls, date -u alike:
This session is isolated in the worktree
<wt>, but this command is too complex to verify that it stays inside the worktree. Refusing to run it — a worktree-isolated session's git operations must target its own worktree. Split it into plain, separate commands and run them from<wt>.
Over four days, 0 of 1001 Bash calls of the subagents the hook rewrote ran; subagents the hook left alone ran 7379 of 7642 calls in the same sessions; the same subagents outside isolated worktrees saw no refusal. The model's command was pwd. The hint ("split it into plain, separate commands") cannot be followed: the construct the guard objects to was added after the model wrote the command, and every retry is rewritten the same way.
Bisection of the prologue, each as its own Bash call in the isolated session
Prologue before pwd |
Result |
|---|---|
if [ -n "${BASH_VERSION-}" ] && { case ":${SHELLOPTS-}:" in *:posix:*) [ "${BASH_VERSINFO-0}" -lt 5 ];; esac; }; then ulimit -HSf N; else ulimit -HSf M; fi 2>/dev/null |
Refused |
ulimit -HSf N 2>/dev/null (own line, or ; pwd) |
Runs |
if [ -n "${BASH_VERSION-}" ]; then ulimit -HSf N; else ulimit -HSf M; fi 2>/dev/null |
Runs |
case ":${SHELLOPTS-}:" in *:posix:*) ulimit -HSf N;; *) ulimit -HSf M;; esac 2>/dev/null |
Runs |
if [ -n "${BASH_VERSION-}" ]; then case ":${SHELLOPTS-}:" in *:posix:*) if [ "${BASH_VERSINFO-0}" -lt 5 ]; then ulimit -HSf N; else ulimit -HSf M; fi;; *) ulimit -HSf N;; esac; else ulimit -HSf M; fi 2>/dev/null |
Runs |
So the [ ... ] && { case ...; } brace group is what the guard cannot verify; the nested if/case with the same meaning passes. We changed our hook to the last form, which cures it on our side.
Ask, in addition to the proposals above
When the guard refuses a command that a PreToolUse hook changed, the refusal should say so and show the text it judged (or run the isolation check on the command the model wrote, as proposed above). Today the model is told to fix a command it never wrote, and a hook author sees nothing in the session that points at the hook.
Hey I'm with the Claude Code team. Plain git should work in worktree sessions even if a hook rewrites the command, as of 2.1.321. Could you update and let me know if it works with your hook?
This was fixed in Claude Code 2.1.321, so we're closing this issue. If you disagree, let us know here and we can re-open it. Thanks for reporting!