claude-code - 💡(How to fix) Fix [BUG] Subagents inherit server cwd instead of parent session worktree directory [3 comments, 3 participants]

Official PRs (…)
ON THIS PAGE

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#48306Fetched 2026-04-16 07:03:31
View on GitHub
Comments
3
Participants
3
Timeline
7
Reactions
0
Author
Timeline (top)
labeled ×4commented ×3

Error Message

Error Messages/Logs

Fix Action

Fix / Workaround

Workaround: PreToolUse hook on Bash that compares cwd (from hook JSON payload) against process cwd and warns via stderr. Companion .claude/rules/ file teaches agents to self-correct.

RAW_BUFFERClick to expand / collapse

Preflight Checklist

  • I have searched existing issues and this hasn't been reported yet
  • This is a single bug report (please file separate reports for different bugs)
  • I am using the latest version of Claude Code

What's Wrong?

When using claude remote-control –spawn worktree, each session correctly gets its own git worktree as its working directory. However, subagents spawned via the Agent tool from within those sessions inherit the claude remote-control server process's cwd instead of the parent session's worktree path.

The subagent's system prompt correctly shows the worktree as "Primary working directory", but the actual shell cwd for Bash commands points to the server's original directory.

This causes subagents to read/write files in the wrong directory silently — edits land in the main clone instead of the worktree branch.

What Should Happen?

Subagents should inherit the parent session's working directory (the worktree), not the server process's cwd.

Error Messages/Logs

Steps to Reproduce

  1. Start claude remote-control –spawn worktree –no-create-session-in-dir from /data/repo
  2. Create a session — it correctly gets worktree /data/repo/.claude/worktrees/<session-id>
  3. From that session, use the Agent tool to spawn a subagent
  4. In the subagent, run pwd via Bash tool
  5. Result: pwd returns /data/repo (server cwd) instead of the worktree path

Claude Model

Not sure / Multiple models

Is this a regression?

I don't know

Last Working Version

No response

Claude Code Version

2.1.92 (Claude Code)

Platform

Anthropic API

Operating System

Ubuntu/Debian Linux

Terminal/Shell

Non-interactive/CI environment

Additional Information

Setup: claude remote-control –spawn worktree –no-create-session-in-dir in Docker (node:22-slim), multiple concurrent sessions per server.

Workaround: PreToolUse hook on Bash that compares cwd (from hook JSON payload) against process cwd and warns via stderr. Companion .claude/rules/ file teaches agents to self-correct.

This issue was drafted with agent assistance (Claude Opus 4.6)

extent analysis

TL;DR

The subagents spawned via the Agent tool should inherit the parent session's working directory, which is the git worktree path, instead of the server process's cwd.

Guidance

  • Verify that the claude remote-control server process is correctly setting the working directory for each session using the --spawn worktree option.
  • Check the Agent tool's configuration to ensure it is properly inheriting the parent session's working directory.
  • Investigate the use of the PreToolUse hook on Bash to compare the cwd from the hook JSON payload against the process cwd and warn via stderr as a potential workaround.
  • Review the .claude/rules/ file to ensure it is correctly teaching agents to self-correct.

Example

No code snippet is provided as the issue does not contain sufficient information to create a specific example.

Notes

The issue may be related to the way the claude remote-control server process handles the working directory for each session, or how the Agent tool inherits this directory. Further investigation is needed to determine the root cause.

Recommendation

Apply workaround: Use the PreToolUse hook on Bash to compare the cwd from the hook JSON payload against the process cwd and warn via stderr, and review the .claude/rules/ file to ensure it is correctly teaching agents to self-correct. This workaround can help mitigate the issue until a permanent fix is found.

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…

Still need to ship something?

×6

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

Back to top recommendations

TRENDING