fix(terminal): ignore stale env.cwd from a different session's cd - #62408
Merged
Conversation
The terminal environment is shared process-globally (collapsed to the default key), so env.cwd tracks the LAST session that ran a command. _resolve_command_cwd() trusted env.cwd unconditionally — no ownership check — so when session A left env.cwd pointing at A's checkout, session B's first terminal command inherited A's stale cwd and ran in the wrong workspace. The file tools already solved this exact shared-env problem with _live_cwd_if_owned() checking env.cwd_owner. The terminal tool never got the same guard. Fix: capture env.cwd_owner BEFORE the current session claims it, and pass it as prev_owner to _resolve_command_cwd. When the previous owner was a different session, env.cwd is stale — fall through to default_cwd (the config/override cwd for this session) instead. Once the session has claimed the env, subsequent calls in the same session still trust env.cwd so in-session state survives.
ethernet8023
force-pushed
the
fix/terminal-stale-cwd-cross-session
branch
from
July 11, 2026 02:15
f776c1c to
6a1f40e
Compare
This was referenced Jul 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
The terminal environment is shared process-globally (collapsed to the
"default"key), soenv.cwdtracks the LAST session that ran a command._resolve_command_cwd()trustedenv.cwdunconditionally — no ownership check — so when session A'scdleftenv.cwdpointing at A's checkout, session B's first terminal command inherited A's stale cwd and ran in the wrong workspace.The file tools already solved this exact shared-env problem with
_live_cwd_if_owned()checkingenv.cwd_owner(tools/file_tools.py:272). The terminal tool never got the same guard.Fix: capture
env.cwd_ownerBEFORE the current session claims it, and pass it asprev_ownerto_resolve_command_cwd. When the previous owner was a different session,env.cwdis stale — fall through todefault_cwd(the config/override cwd for this session) instead. Once the session has claimed the env, subsequent calls in the same session still trustenv.cwdso in-sessioncdstate survives.Related Issue
Fixes new sessions in the desktop app frequently being in the wrong pwd :D
Type of Change
Changes Made
tools/terminal_tool.py—_resolve_command_cwd(): addedprev_ownerparam; when the env's previous owner was a different session,env.cwdis stale and falls through todefault_cwdtools/terminal_tool.py— call site (line ~2373): captureprev_cwd_owner = getattr(env, "cwd_owner", "")before claiming the envtools/terminal_tool.py— both_resolve_command_cwdcall sites (background + foreground): passprev_owner=prev_cwd_ownertests/tools/test_terminal_task_cwd.py— two new regression testsHow to Test
scripts/run_tests.sh tests/tools/test_terminal_task_cwd.py -v— 12 tests pass (10 existing + 2 new)scripts/run_tests.sh tests/tools/test_terminal_tool.py tests/tools/test_terminal_task_cwd.py tests/tools/test_terminal_config_env_sync.py tests/tools/test_terminal_compound_background.py tests/tools/test_terminal_exit_semantics.py -v— 114 tests pass, no regressionsscripts/run_tests.sh tests/tools/test_file_tools.py -v— 52 tests pass (file_tools share thecwd_ownerconcept)New tests:
test_stale_env_cwd_from_different_session_is_ignored— env owned by session-A, session-B calls terminal → command runs with config cwd, not session-A's staleenv.cwdtest_same_session_env_cwd_is_trusted_after_first_claim— env owned by same session →env.cwdis trusted (in-sessioncdsurvives)Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping