Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/tools/test_base_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,27 @@ def failing_run_bash(*args, **kwargs):

assert env._snapshot_ready is False

def test_init_session_bootstraps_into_configured_cwd(self):
env = _TestableEnv(cwd="/custom/workspace")
calls = []

def mock_run_bash(cmd, *, login=False, timeout=120, stdin_data=None):
calls.append({"cmd": cmd, "login": login, "timeout": timeout})
mock = MagicMock()
mock.poll.return_value = 0
mock.returncode = 0
mock.stdout = iter([])
return mock

env._run_bash = mock_run_bash
env._wait_for_process = lambda proc, timeout=120: {"output": f"\n{env._cwd_marker}/custom/workspace{env._cwd_marker}\n", "returncode": 0}
env.init_session()

assert len(calls) == 1
assert calls[0]["login"] is True
assert "cd /custom/workspace || exit 126" in calls[0]["cmd"]
assert env.cwd == "/custom/workspace"

def test_login_flag_when_snapshot_not_ready(self):
"""When _snapshot_ready=False, execute() should pass login=True to _run_bash."""
env = _TestableEnv()
Expand Down
10 changes: 10 additions & 0 deletions tools/environments/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,23 @@ def init_session(self):
instead of running with ``bash -l``.
"""
# Full capture: env vars, functions (filtered), aliases, shell options.
# IMPORTANT: cd to the configured cwd before recording pwd. Otherwise the
# initial session cwd silently becomes the parent Python process cwd
# (e.g. a systemd WorkingDirectory or the shell dir that launched Hermes),
# which overrides TERMINAL_CWD / terminal.cwd for local sessions.
quoted_cwd = (
shlex.quote(self.cwd)
if self.cwd != "~" and not self.cwd.startswith("~/")
else self.cwd
)
bootstrap = (
f"export -p > {self._snapshot_path}\n"
f"declare -f | grep -vE '^_[^_]' >> {self._snapshot_path}\n"
f"alias -p >> {self._snapshot_path}\n"
f"echo 'shopt -s expand_aliases' >> {self._snapshot_path}\n"
f"echo 'set +e' >> {self._snapshot_path}\n"
f"echo 'set +u' >> {self._snapshot_path}\n"
f"cd {quoted_cwd} || exit 126\n"
f"pwd -P > {self._cwd_file} 2>/dev/null || true\n"
f"printf '\\n{self._cwd_marker}%s{self._cwd_marker}\\n' \"$(pwd -P)\"\n"
)
Expand Down