claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

Agent tool's isolation:'worktree' binds the worktree's base repo to the caller's Bash cwd at dispatch time, not the target repo

Open thebenjaminlee opened this issue 3 months ago • 3 comments

Summary

When calling the Agent tool with isolation: 'worktree', the isolated worktree's base repository appears to be determined by the dispatching session's current Bash-tool working directory at the moment Agent is called — not by anything in the prompt, and there is no parameter on the tool to specify the target repo explicitly. This causes silent cross-repo mis-dispatch whenever the caller works across multiple git repos in one session (a very common pattern for a multi-repo monorepo-adjacent codebase).

Reproduction (observed this session, 7 Agent(isolation:'worktree') calls across a multi-repo task)

  1. Sent 4 Agent(isolation:'worktree') calls in a single batched message (one message, multiple tool_use blocks), targeting 4 different repos (A, B, C, D). The Bash cwd at send time was repo C.
    • Result: all four landed with their isolated worktree rooted in repo A (not C, not their individually-intended targets). Two of the four happened to actually need repo A, so those "worked" by coincidence; the other two (needing B and D) failed immediately — each correctly detected the mismatch via git rev-parse --show-toplevel and self-reported rather than attempting a -C/GIT_DIR/GIT_WORK_TREE workaround (which the sandbox also independently blocked when the agents tried, as a defense-in-depth check).
  2. Retried the repo-B task alone: ran cd <repo B> via Bash, then immediately (no other tool call in between) issued a single Agent(isolation:'worktree') call.
    • Result: correctly landed in repo B.
  3. Immediately retried the repo-D task alone, but issued right after the repo-B retry above with no intervening cd.
    • Result: incorrectly landed in repo B again (inherited from the previous dispatch's cwd, not the cwd at the time of this call — actually in this case it was the same cwd, since nothing changed it, confirming cwd-at-call-time is the mechanism, and it persists across calls until explicitly changed).
  4. Retried repo D a third time, with cd <repo D> issued immediately before, single call, nothing in between.
    • Result: correctly landed in repo D.

Conclusion

The base repo for isolation: 'worktree' is bound to whatever directory the orchestrating session's Bash tool was last cd'd to, evaluated at Agent-call time, and this state is NOT scoped per-call or per-batch — it's ambient session state that leaks across unrelated tool calls, including across calls sent in the same message/batch (which all appear to share one cwd snapshot rather than each specifying their own).

This makes isolation: 'worktree' unsafe to use for cross-repo work without an extremely fragile discipline: never batch multiple isolated dispatches targeting different repos, and always cd to the exact target repo immediately before each single dispatch with zero other tool calls in between. Any orchestration pattern that fans out N agents across N repos in parallel (a natural use case for background agent dispatch) is currently broken by this.

Suggested fix

Add an explicit repo/path parameter to Agent (e.g. cwd or repoPath) that determines the isolated worktree's base repository, independent of the dispatching session's own shell state. This removes the ambient-state footgun entirely and makes cross-repo parallel dispatch safe to batch.

Environment

  • Claude Code CLI, multi-repo local development environment (several sibling git repos under one parent directory)
  • Observed via the Agent tool with isolation: 'worktree', subagent_type: 'general-purpose', dispatched in both batched and single-call forms in the same session

thebenjaminlee avatar Aug 10 '26 07:08 thebenjaminlee

repoPath on the call is the right fix. I'd also have the child report the canonical Git root in its first metadata event and fail before the first tool call if it doesn't match. That way a stale or batched dispatch can't quietly start in the wrong repo even if the parent routing regresses.

I work on BitFun; we keep workspace identity in dispatch state instead of prompt text for exactly this reason. The four-repo batch above would make a very good regression test.

bobleer avatar Aug 10 '26 08:08 bobleer

Closing for now — inactive for too long. Please open a new issue if this is still relevant.

github-actions[bot] avatar Oct 04 '26 22:10 github-actions[bot]

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.

github-actions[bot] avatar Nov 28 '26 14:11 github-actions[bot]