Skip to content

fix: add CWD existence guard in LocalShell._run_bash() to prevent terminal bricking on deleted directories#17707

Closed
alexwangzheng74 wants to merge 1 commit into
NousResearch:mainfrom
alexwangzheng74:local-fixes
Closed

fix: add CWD existence guard in LocalShell._run_bash() to prevent terminal bricking on deleted directories#17707
alexwangzheng74 wants to merge 1 commit into
NousResearch:mainfrom
alexwangzheng74:local-fixes

Conversation

@alexwangzheng74

Copy link
Copy Markdown

Problem

When a session's working directory is deleted mid-session (e.g. rm -rf ~/projects/some-project from one command), subsequent terminal() and execute_code() calls permanently fail for the rest of the session.

Root cause: local._run_bash() calls subprocess.Popen(..., cwd=self.cwd). When self.cwd points to a deleted directory, Popen throws FileNotFoundError before the shell even starts — the builtin cd || exit 126 wrapper in _wrap_command() never gets a chance to run. Every subsequent tool call hits the same error, and the only recovery is a full agent restart.

Fix

Add an os.path.isdir(self.cwd) check in _run_bash() right before subprocess.Popen. If the directory no longer exists, fall back to $HOME and log a warning. This lets the agent recover gracefully without a restart.

12-line change with a comment explaining the edge case. Zero impact on normal operation — the check is a single os.path.isdir() call that passes almost instantly.

Testing

  • Manual simulation: confirmed that Popen(cwd=deleted) raises FileNotFoundError, and fallback to $HOME succeeds
  • Existing test suite: 60 passed, 1 pre-existing flaky (test_wait_for_process_kills_subprocess_on_keyboardinterrupt — unrelated drain thread race)
  • Normal operation unaffected: os.path.isdir() on a valid directory returns in microseconds

When the session's CWD is deleted mid-session (e.g.  on a project
dir), subprocess.Popen(cwd=deleted_dir) fails with FileNotFoundError before
the shell starts, permanently bricking terminal and execute_code tools.

Add a guard that checks os.path.isdir(self.cwd) before spawning the shell
and falls back to /Users/alexwang if the directory no longer exists. This allows the
agent to recover without a restart.
@alt-glitch alt-glitch added type/bug Something isn't working backend/local Local shell execution tool/terminal Terminal execution and process management P2 Medium — degraded but workaround exists labels Apr 30, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #17569 — same root cause (deleted CWD breaks Popen in _run_bash) and same fallback-to-safe-directory fix.

@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #17569 — same root cause: LocalEnvironment._run_bash raises FileNotFoundError when self.cwd is deleted. Both PRs add a fallback guard before Popen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend/local Local shell execution P2 Medium — degraded but workaround exists tool/terminal Terminal execution and process management type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants