Skip to content

The agent that grows with you

fix(terminal): do not cd an ssh session into the container home

xxxigm666681cbf3d3
diff --git a/tests/tools/test_ssh_remote_cwd.py b/tests/tools/test_ssh_remote_cwd.py
index 2152e05436f..219a04732fc 100644
--- a/tests/tools/test_ssh_remote_cwd.py
+++ b/tests/tools/test_ssh_remote_cwd.py
@@ -36,6 +36,25 @@ def test_ssh_keeps_a_real_remote_directory(monkeypatch):
assert str(resolved) == "/home/ubuntu/COMPRESS/cwd_probe.txt"
+def test_ssh_command_cwd_drops_container_home(monkeypatch):
+ monkeypatch.setattr(terminal_tool, "_session_cwd", {"sess": "/opt/data/home"})
+ with patch("hermes_constants.get_subprocess_home", return_value="/opt/data/home"):
+ cwd = terminal_tool._resolve_command_cwd(
+ workdir=None, default_cwd="~", session_key="sess", env_type="ssh")
+ explicit = terminal_tool._resolve_command_cwd(
+ workdir="/opt/data/home", default_cwd="~", session_key="sess", env_type="ssh")
+ assert cwd == "~"
+ assert explicit == "~"
+
+
+def test_ssh_command_cwd_keeps_remote_directory(monkeypatch):
+ monkeypatch.setattr(terminal_tool, "_session_cwd", {"sess": "/home/ubuntu"})
+ with patch("hermes_constants.get_subprocess_home", return_value="/opt/data/home"):
+ cwd = terminal_tool._resolve_command_cwd(
+ workdir=None, default_cwd="~", session_key="sess", env_type="ssh")
+ assert cwd == "/home/ubuntu"
+
+
def test_local_tilde_still_uses_subprocess_home(monkeypatch, tmp_path):
home = tmp_path / "profile_home"
home.mkdir()
diff --git a/tools/file_tools.py b/tools/file_tools.py
index 91c2fe03b44..3ee5fc1511b 100644
--- a/tools/file_tools.py
+++ b/tools/file_tools.py
@@ -264,7 +264,7 @@ _file_ops_cache: dict = {}
def _create_terminal_env_for_file_ops(raw_task_id: str, task_id: str):
"""Build the terminal environment for *task_id* via the shared ``_create_configured_env``,
so a file tool that runs before any terminal command still gets the configured backend."""
- from tools.terminal_tool_config import _is_container_backend
+ from tools.terminal_tool_config import _is_container_backend, coerce_ssh_remote_cwd
from tools.terminal_tool import (
_create_configured_env, _get_env_config, _is_unusable_container_cwd,
_resolve_task_host_cwd, _select_image, get_session_cwd, resolve_task_overrides)
@@ -276,7 +276,7 @@ def _create_terminal_env_for_file_ops(raw_task_id: str, task_id: str):
recorded_cwd = get_session_cwd(raw_task_id)
except Exception:
recorded_cwd = None
- cwd = overrides.get("cwd") or recorded_cwd or config["cwd"]
+ cwd = coerce_ssh_remote_cwd(overrides.get("cwd") or recorded_cwd or config["cwd"], env_type)
# Re-apply the container cwd guard: a gateway/TUI/ACP override is a raw HOST
# path and ``docker run -w <host-path>`` makes search_files & co silently
# return nothing. Valid in-container overrides (/workspace, /root) pass.
diff --git a/tools/terminal_tool.py b/tools/terminal_tool.py
index 5a88943b650..21d89110542 100644
--- a/tools/terminal_tool.py
+++ b/tools/terminal_tool.py
@@ -45,6 +45,7 @@ from tools.terminal_tool_lifecycle import (
)
from tools.terminal_tool_config import (
_is_container_backend, _is_host_cwd, _is_unusable_container_cwd, _parse_env_var,
+ coerce_ssh_remote_cwd,
_plugin_env_flag, _quiet, _safe_getcwd, _tenv, _tenv_bool,
)
from tools.terminal_tool_backends import (
@@ -845,7 +846,7 @@ def _resolve_command_cwd(
Same guard class as the env-creation sanitizers (#50636, #54447); this is the per-command sibling site.
"""
if workdir:
- return workdir
+ return coerce_ssh_remote_cwd(workdir, env_type)
recorded = get_session_cwd(session_key)
if recorded and _is_container_backend(env_type) and _is_unusable_container_cwd(recorded):
logger.info(
@@ -854,7 +855,7 @@ def _resolve_command_cwd(
recorded, env_type, default_cwd,
)
return default_cwd
- return recorded or default_cwd
+ return coerce_ssh_remote_cwd(recorded or default_cwd, env_type)
def _error_json(error: str, *, exit_code: int = -1, status: Optional[str] = None, **extra) -> str:
@@ -1006,7 +1007,8 @@ def _plan_execution(
overrides = resolve_task_overrides(task_id)
image = _select_image(env_type, overrides, config)
- cwd = overrides.get("cwd") or get_session_cwd(task_id) or config["cwd"]
+ cwd = coerce_ssh_remote_cwd(
+ overrides.get("cwd") or get_session_cwd(task_id) or config["cwd"], env_type)
host_cwd = _resolve_task_host_cwd(config, task_id)
# config["cwd"] was sanitized for container backends in _get_env_config
# but an override / session record is raw: a host path would reach