claude-code - 💡(How to fix) Fix EnterWorktree intermittently returns false subagent isolation error from main session [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#52538Fetched 2026-04-24 06:04:30
View on GitHub
Comments
0
Participants
1
Timeline
4
Reactions
0
Participants
Timeline (top)
labeled ×4

EnterWorktree(name=...) intermittently fails with a false subagent isolation error when called from the main session (not a subagent). The exact same call -- identical parameters -- succeeds on retry without any environmental change.

Error Message

EnterWorktree(name=...) intermittently fails with a false subagent isolation error when called from the main session (not a subagent). The exact same call -- identical parameters -- succeeds on retry without any environmental change. The runtime subagent isolation detection appears to be checking stale state or incorrectly inheriting a flag. The error is transient -- the identical call succeeds after 2 failures with no meaningful environmental change between attempts.

Root Cause

EnterWorktree(name=...) intermittently fails with a false subagent isolation error when called from the main session (not a subagent). The exact same call -- identical parameters -- succeeds on retry without any environmental change.

RAW_BUFFERClick to expand / collapse

Summary

EnterWorktree(name=...) intermittently fails with a false subagent isolation error when called from the main session (not a subagent). The exact same call -- identical parameters -- succeeds on retry without any environmental change.

Reproduction

  1. Start a session in a git repo main checkout (not inside a worktree)
  2. Call EnterWorktree(name=some-worktree-name)
  3. First call fails with: EnterWorktree cannot be called from a subagent with a cwd override (isolation: worktree or explicit cwd) -- it would mutate the parent session process-wide working directory. This agent is already isolated in its own working copy.
  4. Retry the exact same call -- same failure
  5. Retry again (or after user interrupts and re-prompts) -- succeeds

Evidence

Observed in 2 independent sessions on the same repo, same pattern:

Session 1 (7537afaf):

  • Line 537: EnterWorktree(name=slice-e8-s29-a-pdf-generation) -- FAIL
  • Line 550: same call -- FAIL
  • Line 567: same call -- SUCCESS

Session 2 (915024db):

  • Line 516: EnterWorktree(name=slice-e8-pr31-c-notifications-ui) -- FAIL
  • Line 528: same call -- FAIL
  • Line 578: same call -- SUCCESS (after deleting an unrelated branch and returning to dev)

In all cases:

  • pwd confirms the main repo root (not inside .claude/worktrees/)
  • git rev-parse --git-common-dir returns .git (not a linked worktree)
  • No isolation: worktree parameter was passed -- only name
  • No prior Agent(isolation: worktree) calls in the session
  • No active worktrees at time of call (worktrees are removed after each use via ExitWorktree)

Environment

  • Claude Code: 2.1.118
  • macOS Darwin 24.6.0

Expected behavior

EnterWorktree(name=...) should succeed on the first call when invoked from the main session at the repo root with no active worktree context.

Notes

The runtime subagent isolation detection appears to be checking stale state or incorrectly inheriting a flag. The error is transient -- the identical call succeeds after 2 failures with no meaningful environmental change between attempts.

extent analysis

TL;DR

The issue can be mitigated by retrying the EnterWorktree call after an initial failure, as the error is transient and the identical call succeeds after 2-3 attempts.

Guidance

  • Investigate the subagent isolation detection mechanism to understand why it's checking stale state or incorrectly inheriting a flag, causing the initial failure.
  • Verify that the EnterWorktree call is being made from the main session at the repo root with no active worktree context, as expected.
  • Consider adding a retry mechanism to the EnterWorktree call to automatically retry the operation after an initial failure, given the transient nature of the error.
  • Review the code changes and environment settings between versions to identify any potential regressions or changes that may have introduced this issue.

Example

No code snippet is provided as the issue does not imply a specific code change, but rather an investigation into the subagent isolation detection mechanism.

Notes

The issue appears to be related to the runtime subagent isolation detection, which may be checking stale state or incorrectly inheriting a flag. The error is transient, and the identical call succeeds after 2-3 attempts, suggesting a potential issue with the detection mechanism.

Recommendation

Apply workaround: given the transient nature of the error, implementing a retry mechanism for the EnterWorktree call may be a suitable workaround until the root cause of the issue is identified and addressed.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

EnterWorktree(name=...) should succeed on the first call when invoked from the main session at the repo root with no active worktree context.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix EnterWorktree intermittently returns false subagent isolation error from main session [1 participants]