Claude Code Worktree-Isolation Guard
When a Claude Code session is isolated in a worktree (EnterWorktree, isolation: "worktree" subagents, background jobs), the harness statically checks every Bash command before it runs. If it cannot prove that git operations stay inside the session’s own worktree, it refuses the command with “Refusing to run it — a worktree-isolated session’s git operations must target its own worktree.” Since 2.1.222 this applies to every session type, and it cannot be switched off. Evidence: Claude Code Worktree Guard Issues.
The guard is conservative by design: it does not run or simulate the command, so anything whose target directory or executable is only known at runtime counts as unverifiable.
Refusal classes (as of 2.1.304)
No longer refused (fixed 2.1.257/2.1.259): plain loops, heredocs,
$VAR reads and "$(…)" that never touch git. &&, pipes and redirects pass on their own.
Workaround matrix
Removal criteria
These are bandaids for specific bugs. Remove each one when its bug is fixed:- rtk stable release containing PR #3879 → drop the hook wrapper and the
/usr/bin/gitrule. - claude-code#88550 closed → drop the “no
~” rule. - The other rules (no git outside own worktree, no
source) are by design and stay.
Related
- rtk (Rust Token Killer) — the PreToolUse rewrite that collides with the guard.
- Worktree Isolation — the isolation model the guard enforces.
- Agent Subagents —
isolation: "worktree"subagents. - Agentic Sandbox Controls — the broader sandboxing picture.
- Local record:
mistakes/2026-10-20-claude-code-worktree-guard-compound-bash.md; dotfiles workflowdocs/workflows/worktree-guard-bandaid.md.