Skip to content

The agent that grows with you

fix(tools): coerce restored SSH session working directories

Gillea789f874e2ee
diff --git a/tests/tools/test_ssh_session_cwd.py b/tests/tools/test_ssh_session_cwd.py
new file mode 100644
index 00000000000..71b3a80bb69
--- /dev/null
+++ b/tests/tools/test_ssh_session_cwd.py
@@ -0,0 +1,85 @@
+"""Late session cwd updates must stay in the SSH peer's namespace.
+
+The creation path already translates the Hermes subprocess home to remote ~.
+These regressions exercise an existing environment and a later command reading
+the raw session record, neither of which creates a new environment.
+"""
+
+import pytest
+
+import hermes_constants
+import tools.terminal_tool as terminal_tool
+from tools.file_operations import ShellFileOperations
+
+
+HOST_HOME = "/srv/hermes-host/home"
+
+
+class RecordingEnvironment:
+ def __init__(self, env_type):
+ self.env_type = env_type
+ self.cwd = "~/initial"
+ self.calls = []
+
+ def execute(self, command, **kwargs):
+ self.calls.append((command, kwargs))
+ return {"output": "probe", "returncode": 0}
+
+
[email protected](autouse=True)
+def isolated_session_state(monkeypatch):
+ monkeypatch.setattr(hermes_constants, "get_subprocess_home", lambda: HOST_HOME)
+ monkeypatch.setattr(hermes_constants, "get_real_home", lambda: "/srv/os-user")
+ for name in ("_session_cwd", "_task_env_overrides", "_active_environments", "_container_aliases"):
+ monkeypatch.setattr(terminal_tool, name, {})
+
+
+CASES = [
+ ("ssh", HOST_HOME, "~"),
+ ("ssh", f"{HOST_HOME}/project", "~/project"),
+ ("ssh", "/remote/project", "/remote/project"),
+ ("ssh", "~other/project", "~other/project"),
+ ("ssh", f"{HOST_HOME}work", f"{HOST_HOME}work"),
+ ("local", HOST_HOME, HOST_HOME),
+]
+
+
[email protected]("env_type, raw_cwd, backend_cwd", CASES)
+def test_late_registration_keeps_file_execution_in_backend_namespace(
+ env_type, raw_cwd, backend_cwd
+):
+ env = RecordingEnvironment(env_type)
+ task_id = "session-example"
+ terminal_tool._active_environments[task_id] = env
+ file_ops = ShellFileOperations(env)
+
+ terminal_tool.register_task_env_overrides(task_id, {"cwd": raw_cwd})
+
+ # The UI's workspace record stays raw. Only execution crosses namespaces.
+ assert terminal_tool.get_session_cwd(task_id) == raw_cwd
+ assert env.cwd == backend_cwd
+ result = file_ops._exec("probe")
+ assert result.exit_code == 0
+ assert env.calls[-1][1]["cwd"] == backend_cwd
+
+
[email protected]("env_type, raw_cwd, backend_cwd", CASES)
+def test_recorded_session_cwd_is_coerced_without_changing_explicit_remote_workdir(
+ env_type, raw_cwd, backend_cwd
+):
+ task_id = "session-example"
+ terminal_tool.record_session_cwd(task_id, raw_cwd)
+
+ assert terminal_tool._resolve_command_cwd(
+ workdir=None,
+ default_cwd="~/default",
+ session_key=task_id,
+ env_type=env_type,
+ ) == backend_cwd
+ assert terminal_tool.get_session_cwd(task_id) == raw_cwd
+ assert terminal_tool._resolve_command_cwd(
+ workdir="/remote/explicit",
+ default_cwd="~/default",
+ session_key=task_id,
+ env_type=env_type,
+ ) == "/remote/explicit"
diff --git a/tools/terminal_tool.py b/tools/terminal_tool.py
index 474b64e8926..44ca2a41806 100644
--- a/tools/terminal_tool.py
+++ b/tools/terminal_tool.py
@@ -287,10 +287,12 @@ def _sanitize_cwd_for_live_env(env: Any, new_cwd: str) -> Optional[str]:
are already rejected on the creation paths. This write classifies the
directory mounted at ``/workspace`` as unusable before that prefix
heuristic, then remaps the match (or a child of it) to its container mount
- instead of storing the host path. Non-container backends apply the override
- verbatim (ACP project-root switching must keep working).
+ instead of storing the host path. SSH maps the Hermes subprocess home onto
+ the peer's home, as environment creation already does. Other backends apply
+ the override verbatim (ACP project-root switching must keep working).
"""
env_type = getattr(env, "env_type", None)
+ new_cwd = coerce_ssh_remote_cwd(new_cwd, env_type)
if not env_type or not _is_container_backend(env_type):
return new_cwd
host_mount = getattr(env, "host_cwd", None)
@@ -960,7 +962,7 @@ def _resolve_command_cwd(
recorded, env_type, default_cwd,
)
return _container_visible_default(default_cwd, env_type, env)
- return recorded or coerce_ssh_remote_cwd(_container_visible_default(default_cwd, env_type, env), env_type)
+ return coerce_ssh_remote_cwd(recorded or _container_visible_default(default_cwd, env_type, env), env_type)
def _error_json(error: str, *, exit_code: int = -1, status: Optional[str] = None, **extra) -> str: