Skip to content

fix(terminal): honor configured cwd at session init#14831

Closed
zwcf5200 wants to merge 1 commit into
NousResearch:mainfrom
zwcf5200:test/upstream-pr-7802-cwd-fix
Closed

fix(terminal): honor configured cwd at session init#14831
zwcf5200 wants to merge 1 commit into
NousResearch:mainfrom
zwcf5200:test/upstream-pr-7802-cwd-fix

Conversation

@zwcf5200

Copy link
Copy Markdown
Contributor

Summary

Apply upstream PR #7802 behavior so BaseEnvironment.init_session() captures its initial snapshot after cd'ing into the configured cwd. This preserves SSH terminal.cwd instead of letting the remote login shell home directory overwrite it.

Upstream reference: #7802
Upstream commit: 12e3ef9

Why this is still needed

Main branch already has 4c136288 fix(local): respect configured cwd in init_session() which passes cwd=self.cwd to subprocess.Popen. However, this only fixes the local backend. SSH, Docker, Daytona and other remote backends use different _run_bash() implementations (remote shell execution) that don't support a cwd parameter — they rely on the bootstrap script in BaseEnvironment.init_session().

This PR fixes the root cause for all non-local backends by adding cd {quoted_cwd} to the init_session bootstrap script.

Changes

tools/environments/base.py

  • Added _quote_cwd_for_shell() static method to extract cwd quoting logic.
  • Modified init_session() bootstrap script to include cd {quoted_cwd} || exit 126 before writing pwd, so the initial snapshot captures environment from the configured working directory.
  • Refactored inline quoted_cwd computation in _wrap_command() to use the new static method.

tests/tools/test_base_environment.py

  • Added TestInitSessionFailure.test_init_session_bootstrap_changes_to_configured_cwd: verifies that init_session() runs a login shell with cd into the configured cwd and sets _snapshot_ready = True.
  • Updated existing test mocks to use _wait_for_process return values instead of raw process handles.

Testing

All 16 tests in tests/tools/test_base_environment.py pass (CI-parity via scripts/run_tests.sh).

Apply upstream PR NousResearch#7802 behavior so BaseEnvironment captures its initial snapshot after cd'ing into the configured cwd. This preserves SSH terminal.cwd instead of letting the remote login shell home directory overwrite it.

Upstream-PR: NousResearch#7802

Upstream-Commit: 12e3ef9
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/tools Tool registry, model_tools, toolsets tool/terminal Terminal execution and process management backend/ssh SSH remote execution labels Apr 24, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #9798 — same root cause: init_session() captures snapshot before cd into configured cwd. Previous attempts #14830 and #14828 were closed for the same reason. See also #7802.

@zwcf5200

Copy link
Copy Markdown
Contributor Author

Thanks for the reference to #9798. I checked its status and scope:

The root cause is similar, but the fix scope differs — we are complementary, not a duplicate. Without this PR, SSH/Docker/Daytona users still experience cwd loss during init_session().

Could you confirm if you would like me to close this PR and wait for #9798 to be merged first? Or should we keep it open as the remote-backend fix?

@teknium1

Copy link
Copy Markdown
Contributor

Thanks for the contribution @zwcf5200! Closing — the init_session cwd fix landed in #17205 (commit ac855bb) via @Tranquil-Flow's #14796, which uses the same approach (inject cd $cwd into the bootstrap before pwd -P). Both reports also overlap with the v0.11.0 fix 4c13628 (Popen cwd= argument). Issue #14044 is now resolved.

@teknium1 teknium1 closed this Apr 29, 2026
@zwcf5200 zwcf5200 deleted the test/upstream-pr-7802-cwd-fix branch April 30, 2026 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend/ssh SSH remote execution comp/tools Tool registry, model_tools, toolsets 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.

3 participants