Shell cd is silently reset when navigating outside allowed directories (--add-dir)

Resolved 💬 2 comments Opened Mar 24, 2026 by kouui Closed Mar 24, 2026

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

  1. Launch Claude Code with an additional directory:

``bash
claude --add-dir ~/src/projects/other-repo
``

  1. In the Bash tool, cd to the added directory — works fine:

``bash
cd ~/src/projects/other-repo && pwd
# /home/user/src/projects/other-repo
``

  1. 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
``

  1. 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 cd command itself succeeds (exit code 0, pwd shows 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:

  1. Block the cd with a clear error message (e.g., Error: directory /home/user/src/projects is outside allowed working directories)
  2. 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-dir flag
  • 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)

View original on GitHub ↗

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