Skip to content

The agent that grows with you

fix(agent): probe teardown uses signature check and skips the shared SSH master

kshitijk4poor962b35b4c7b5
diff --git a/agent/prompt_builder.py b/agent/prompt_builder.py
index a7b881b4144..9714aac5ecd 100644
--- a/agent/prompt_builder.py
+++ b/agent/prompt_builder.py
@@ -1300,12 +1300,21 @@ def _probe_remote_backend(env_type: str) -> str | None:
# backend leaves a second idle sandbox (task_id="prompt-backend-probe")
# running for the whole process lifetime, alongside the agent's own
# "default" sandbox. force_remove overrides persist mode for this
- # throwaway env; backends whose cleanup() takes no kwargs fall back.
- if env is not None:
+ # throwaway env; backends whose cleanup() takes no kwargs get the
+ # bare call (same signature check as tools.terminal_tool.cleanup_vm).
+ #
+ # SSH is the exception: SSHEnvironment has no task-scoped sandbox —
+ # its cleanup() runs sync_back() and `ssh -O exit` on a ControlMaster
+ # socket keyed only by user@host:port, i.e. shared with the agent's
+ # real environment. Nothing leaks there (ControlPersist expires the
+ # master), so leave it alone.
+ if env is not None and env_type != "ssh":
try:
- try:
+ import inspect
+
+ if "force_remove" in inspect.signature(env.cleanup).parameters:
env.cleanup(force_remove=True)
- except TypeError:
+ else:
env.cleanup()
except Exception:
logger.debug("Backend probe cleanup failed", exc_info=True)
diff --git a/tests/agent/test_prompt_builder.py b/tests/agent/test_prompt_builder.py
index a9ddfe315ba..af338c08ffd 100644
--- a/tests/agent/test_prompt_builder.py
+++ b/tests/agent/test_prompt_builder.py
@@ -883,10 +883,10 @@ class TestEnvironmentHints:
def test_probe_remote_backend_tolerates_kwargless_cleanup(self, monkeypatch):
"""Backends that inherit the base ``cleanup(self)`` take no kwargs; the
- probe must fall back to the bare call instead of dying on TypeError."""
+ probe must use the bare call instead of dying on TypeError."""
import agent.prompt_builder as _pb
- monkeypatch.setenv("TERMINAL_ENV", "ssh")
+ monkeypatch.setenv("TERMINAL_ENV", "singularity")
_pb._clear_backend_probe_cache()
calls = []
@@ -907,9 +907,39 @@ class TestEnvironmentHints:
import tools.terminal_tool as _tt
monkeypatch.setattr(_tt, "_create_environment", lambda **kw: _LegacyEnv())
- assert _pb._probe_remote_backend("ssh") is not None
+ assert _pb._probe_remote_backend("singularity") is not None
assert calls == ["bare"]
+ def test_probe_remote_backend_does_not_tear_down_ssh(self, monkeypatch):
+ """SSH has no task-scoped sandbox: its cleanup() closes a ControlMaster
+ socket shared with the agent's real environment, so the probe must
+ leave it alone (nothing leaks — ControlPersist expires the master)."""
+ import agent.prompt_builder as _pb
+
+ monkeypatch.setenv("TERMINAL_ENV", "ssh")
+ _pb._clear_backend_probe_cache()
+
+ calls = []
+
+ class _SharedSshEnv:
+ def execute(self, cmd, timeout=None):
+ return {
+ "returncode": 0,
+ "output": (
+ "os=Linux\nkernel=6.8.0\nhome=/home/u\n"
+ "cwd=/home/u\nuser=u\n"
+ ),
+ }
+
+ def cleanup(self):
+ calls.append("cleanup")
+
+ import tools.terminal_tool as _tt
+ monkeypatch.setattr(_tt, "_create_environment", lambda **kw: _SharedSshEnv())
+
+ assert _pb._probe_remote_backend("ssh") is not None
+ assert calls == []
+
def test_environment_hint_from_env_var_is_appended(self, monkeypatch):
"""HERMES_ENVIRONMENT_HINT lets an embedder describe the runtime env."""