Shell cd is silently reset when navigating outside allowed directories (--add-dir)
Summary
When using --add-dir to add additional working directories, cd into directories outside the allowed scope silently succeeds momentarily but then gets forcefully reset back to the primary working directory. There is no warning or error message shown to the user.
Reproduction Steps
- Launch Claude Code with an additional directory:
``bash``
claude --add-dir ~/src/projects/other-repo
- In the Bash tool, cd to the added directory — works fine:
``bash``
cd ~/src/projects/other-repo && pwd
# /home/user/src/projects/other-repo
- cd to a subdirectory of the added directory — works fine:
``bash``
cd ~/src/projects/other-repo/sub && pwd
# /home/user/src/projects/other-repo/sub
- cd to the parent of the added directory (outside scope):
``bash``
cd ~/src/projects && pwd
# /home/user/src/projects
# Shell cwd was reset to /home/user/src/projects/main-repo
Observed Behavior
- The
cdcommand itself succeeds (exit code 0,pwdshows the new path) - But immediately after, the shell working directory is silently reset to the primary working directory
- The reset message
Shell cwd was reset to ...only appears in the tool output, not as a visible warning to the user/agent
Expected Behavior
Either:
- Block the
cdwith a clear error message (e.g.,Error: directory /home/user/src/projects is outside allowed working directories) - Or warn the agent explicitly that the directory is out of scope before resetting
Context
The allowed directories are:
- Primary working directory (where Claude Code was launched)
- Any directories added via
--add-dirflag - Subdirectories of both
This behavior is by design for security sandboxing, but the silent reset without a clear error can be confusing — especially for agents that may not notice the reset happened and proceed with incorrect assumptions about the current working directory.
Environment
- Claude Code CLI
- Linux (WSL2)
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗