Bash tool CWD persistence causes cascading failures in multi-agent workflows

Resolved 💬 2 comments Opened Mar 23, 2026 by filscotthenrique Closed Mar 23, 2026

Problem

When Claude runs cd /some/path in a Bash tool call, the working directory change persists across all subsequent Bash calls in that session. This is especially problematic with subagents and the experimental teams feature.

What happens

  1. Agent runs cd server && swift build — reasonable on its own
  2. The cd persists, so the next Bash call (git add .) runs from server/, not the repo root
  3. The agent doesn't realize it's in the wrong directory → silent failures or wrong-path errors
  4. In team workflows, each agent independently develops this habit, compounding the problem

Why Claude gravitates toward cd

When documentation says "run swift build from the server directory," Claude translates that to cd server && swift build or worse, a bare cd server followed by swift build in a separate call. The Bash tool's persistent CWD makes the second pattern silently dangerous.

My use case

I run a monorepo (Next.js + Swift/Vapor + iOS) with specialized agent teams (backend-engineer, qa-engineer, committer, etc.) coordinated via the experimental teams feature. Each agent needs to run build/test/lint commands. When agents cd into subdirectories, subsequent git operations, file paths, and tool calls break.

Workarounds I've had to build

  • A PreToolUse hook that blocks bare cd commands (allows cd /path && command chains)
  • Rewrote all agent definitions to reference root-level Makefile targets instead of subdirectory commands
  • Added explicit "never use cd" instructions to CLAUDE.md and auto-memory
  • Still encounter the issue because agents don't always follow instructions

Suggested improvements

In order of impact:

1. Reset CWD between Bash calls (highest impact)

Each Bash tool invocation should start from the project root, not the CWD left by the previous call. The current persistence serves no purpose that cd /path && command chains don't already handle.

2. Built-in cd prevention setting

A first-class setting like "bashResetCwd": true or "bashBlockCd": true that doesn't require a custom hook. Many users likely hit this without understanding why their agents fail.

3. Agent-level working directory config

When defining agents in .claude/agents/, allow specifying a workingDirectory so the agent's Bash calls always execute from a defined location:

---
name: backend-engineer
workingDirectory: server/
---

4. Better default behavior

Claude's default behavior should prefer absolute paths and tool-specific flags (--package-path, --cwd, -C) over cd. The current tendency to cd is a training artifact that doesn't serve the tool-use context well.

Environment

  • Claude Code v2.1.81
  • macOS, monorepo with server/ (Swift/Vapor), web/ (Next.js), apple/ (iOS)
  • Using experimental agent teams (CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1)
  • Model: Claude Opus 4.6

Reproduction

  1. Start a session in a monorepo root
  2. Run any Bash command with cd to a subdirectory (e.g., cd server)
  3. Run a subsequent Bash command that expects to be at the root (e.g., git status)
  4. Observe that git status runs from the subdirectory, not the root

With agents, this is more insidious because the cd happens inside a subagent and the failure cascades through subsequent tool calls without clear error messages.

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗