Skip to content

In a worktree-isolated Claude Code session, any rtk … command with a git-named operand is refused #4591

Open

Description

@KuSh

Claude Code's worktree guard refuses a command whose launcher it does not model when an operand matches ^git(?:\.exe|\.real|-[a-z][\w-]*)?$ (2.1.301). rtk is such a launcher, so beyond the rtk git … case fixed by #3879:

  • Hook rewrites: grep -n git README.md → rtk grep -n git README.md, refused ("… this command runs rtk with a git command among its operands …"). Reproduced in claude -p --worktree with fix(hooks): leave git unrewritten in Claude Code managed worktrees #3879 merged on develop f16beca. Any rewritten command whose arguments contain the bare word git (a search pattern, a path component spelled git) hits it.
  • Commands the model types from rtk's own guidance: rtk bypass git status and rtk git status are refused the same way. rtk's awareness text and recovery hints tell the model to re-run with rtk bypass <cmd>, which cannot succeed for git in an isolated session.

Plain grep -n git README.md runs in the same session. A fix on the rewrite side would leave a segment unrewritten when the session is isolated and the rewrite would put a git-named operand after rtk; the hint side needs a spelling the guard accepts, or no hint, in that case. Detecting isolation is the same open question as #4490: a .git file also marks a linked worktree a plain, non-isolated session runs in, where the guard never applies.

Activity

  1. rtk-wshm-sync-bot commented on Oct 16, 2026

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 88%

    Summary

    Worktree guard over-matches: any operand spelled like 'git' (even a grep pattern or rtk's own bypass hint) is refused in isolated sessions, extending beyond the #3879 fix.

    Suggested Actions

    1. Resolve the isolation-detection question shared with fix(hooks): detect linked worktrees by .git shape, not path convention #4490 before picking a fix direction
    2. Tighten the guard regex so it only matches git when it is the launcher/subcommand, not any operand
    3. Fix rtk's own bypass/recovery hint text so it doesn't suggest 'rtk bypass git ...' in isolated sessions
    4. Add a regression test for grep -n git README.md rewritten under claude -p --worktree

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  2. gfreschi commented on Oct 20, 2026

    @gfreschi

    Hit this too, on rtk 1.0.1 (Homebrew) with Claude Code subagents launched with isolation: worktree. 1.0.1 doesn't have #3879 yet, so for me even a plain git status was refused:

    This agent is isolated in the worktree /Users/.../.claude/worktrees/agent-..., but this command runs rtk with a git command among its operands ... Refusing to run it

    git rev-parse went through fine, only because rtk doesn't rewrite it, which confirms the guard only cares about what ends up after rtk.

    Until a release has #3879 plus a fix for the operand case here, I've put a small wrapper in front of rtk hook claude in settings.json. When cwd is under /.claude/worktrees/, it drops rtk's answer if any word of the rewritten command looks like a git binary (the same shape as the guard's regex: git, git-*, git.exe, git.real, with or without a path). Everything else passes through unchanged:

    #!/usr/bin/env bash
    INPUT=$(cat)
    case "$(printf '%s' "$INPUT" | jq -r '.cwd // empty' 2>/dev/null)" in
      */.claude/worktrees/*) ;;
      *) printf '%s' "$INPUT" | rtk hook claude; exit ;;
    esac
    OUT=$(printf '%s' "$INPUT" | rtk hook claude); RC=$?
    REWRITE=$(printf '%s' "$OUT" | jq -r '.hookSpecificOutput.updatedInput.command // empty' 2>/dev/null)
    if [ "$RC" -eq 0 ] && printf '%s' "$REWRITE" | tr -d "\"'\\\\" | tr -s ' \t;&|()<>' '\n' |
      grep -Eq '^(.*/)?git(\.exe|\.real|-[a-z][[:alnum:]_-]*)?$'; then
      exit 0
    fi
    printf '%s' "$OUT"; exit "$RC"

    With that in place, an isolated agent could run git status, commit on a scratch branch, and run grep -n git AGENTS.md, while ls in the same worktree still got rtk's compact output.

    One side effect worth knowing: rtk init -g / the "Hook outdated" check sees the wrapper as an outdated hook, so anyone copying this shouldn't re-run init.

    A rewrite-side fix in decide_for_agent_at that does the same check on the rewritten segment would let me drop the wrapper. Happy to test a build from develop if that helps.

  3. alponme commented on Oct 28, 2026

    @alponme

    Still reproducible with the 1.1.0 release (Windows 11, Git Bash, Claude Code 2.1.312, subagent with isolation: worktree).

    With #3879 in the release, plain git now passes: git status --short, git diff --stat, git show --stat HEAD and timeout 5 git status --short all run. The operand case remains, and a quoted search pattern hits a second message:

    Command as typed Guard
    grep -c git FILE (rewritten to rtk grep …) refused: "…this command runs rtk with a git command among its operands…"
    rg -c git FILE (rewritten to rtk rg …) refused, same message
    grep -c "git push" FILE (rewritten to rtk grep …) refused: "…this command runs rtk with the text git push in a plain command, so what it runs cannot be shown not to be git…"
    cat .gitignore (rewritten to rtk read .gitignore) accepted

    So an operand containing the text git push (a search pattern) is refused too, not only an operand spelled git.

  4. HyeonggeonMin commented on Nov 5, 2026

    @HyeonggeonMin

    Starting an investigation and implementation for this issue.

    Additional checks on macOS with RTK 1.1.0:

    • Invoked the configured selective PreToolUse wrapper with payloads for a main checkout, a sibling linked worktree, and a worktree subdirectory. Simple ls, rg, and docker compose ps commands were rewritten successfully in all three locations.
    • The current local workaround deliberately leaves Git-bearing commands and compound shell commands unrewritten, irrespective of checkout location. This preserves ordinary Git execution while retaining RTK for other simple commands.
    • Running rtk git status directly in a linked worktree outside a Claude worktree-isolated session succeeded. A linked worktree alone therefore does not reproduce the isolation-guard failure; these checks do not constitute a fresh reproduction inside Claude's isolated session.
    • Windows native and WSL execution have not been verified. In particular, the local workaround targets the Bash tool, not PowerShell, and Windows/WSL hook installations must be evaluated independently.

    I will inspect the current rewrite paths and recovery/awareness hints, check the isolation-detection limitations discussed in #4490, and add focused regression coverage. The aim is to avoid producing commands that the worktree guard will refuse, while retaining rewriting for commands the guard can accept. No claim of a cross-platform fix or completed isolated-session reproduction yet.

  5. HyeonggeonMin commented on Nov 5, 2026

    @HyeonggeonMin

    Local implementation is now prepared on fix/hook-worktree-git-operands (not yet submitted as a PR).

    • In a detected Claude-managed worktree, inspect the proposed rewrite using the existing quote-aware shell lexer. If an RTK launcher would contain a Git-named argument/path component (git, git.exe, git.real, or git-*), defer the whole command to Claude unchanged. This conservative whole-command fallback avoids reconstructing shell syntax; unaffected segments in that particular command also remain uncompressed.
    • Preserve existing behavior for git status && cargo build → git status && rtk cargo build, ordinary checkouts, configured exclusions, and deny decisions.
    • Update all three awareness levels to run these commands directly in isolated worktrees, and use RTK_DISABLED=1 <cmd> for unfiltered recovery instead of wrapping Git with rtk bypass.

    Validation on macOS:

    • Confirmed the new regression test fails before the fix and passes afterward.
    • cargo fmt --all, cargo clippy --all-targets (no warnings), cargo test --all: 5,511 passed, 0 failed, 8 ignored.
    • Separate proxy-workspace formatting check passed.
    • Invoked the built rtk hook claude with isolated configuration: Git search operand deferred inside the managed worktree, still rewritten outside it, the mixed Git/Cargo chain retained its existing rewrite, and RTK_DISABLED=1 git status stayed untouched.

    Scope remains limited to the existing Claude-managed worktree detection. I reviewed the closure rationale for #4490 and have not introduced .git-file detection: custom-location isolation remains #4634. Native Windows/WSL and a fresh Claude isolated-session end-to-end run are still unverified; the binary-payload checks above are not a claim that those environments were tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions