The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(terminal): share the force_remove-aware env teardown between cleanup_vm and the probe
kshitijk4poor09fa7533661c
diff --git a/agent/prompt_builder.py b/agent/prompt_builder.pyindex 9714aac5ecd..24ea2b17967 100644--- a/agent/prompt_builder.py+++ b/agent/prompt_builder.py@@ -1296,26 +1296,17 @@ def _probe_remote_backend(env_type: str) -> str | None:_BACKEND_PROBE_CACHE[cache_key] = ""return Nonefinally:- # The probe only needs a one-shot `uname`. Without this teardown the+ # The probe only needs a one-shot `uname`; without teardown the# 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 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.+ # running for the whole process lifetime next to the agent's own one.+ # ssh is left alone: it has no task-scoped sandbox and its cleanup()+ # closes a ControlMaster socket (keyed by user@host:port) shared with+ # the agent's real environment; ControlPersist expires it anyway.if env is not None and env_type != "ssh":try:- import inspect+ from tools.terminal_tool import _cleanup_env- if "force_remove" in inspect.signature(env.cleanup).parameters:- env.cleanup(force_remove=True)- else:- env.cleanup()+ _cleanup_env(env, force_remove=True)except Exception:logger.debug("Backend probe cleanup failed", exc_info=True)diff --git a/tools/terminal_tool.py b/tools/terminal_tool.pyindex 4b16c7f0bd4..e5a7103a624 100644--- a/tools/terminal_tool.py+++ b/tools/terminal_tool.py@@ -2431,6 +2431,26 @@ def cleanup_all_environments():return cleaned+def _cleanup_env(env, *, force_remove: bool = False) -> None:+ """Tear down one environment, passing ``force_remove`` only when accepted.++ ``DockerEnvironment.cleanup(force_remove=...)`` (issue #20561) diverges+ from the base ``cleanup(self)``; other backends expose ``stop`` /+ ``terminate`` instead. Shared by ``cleanup_vm`` and the prompt-time+ backend probe so the signature check lives in one place.+ """+ if hasattr(env, 'cleanup'):+ import inspect+ if "force_remove" in inspect.signature(env.cleanup).parameters:+ env.cleanup(force_remove=force_remove)+ else:+ env.cleanup()+ elif hasattr(env, 'stop'):+ env.stop()+ elif hasattr(env, 'terminate'):+ env.terminate()++def cleanup_vm(task_id: str, *, force_remove: bool = False):"""Manually clean up a specific environment by task_id.@@ -2475,19 +2495,7 @@ def cleanup_vm(task_id: str, *, force_remove: bool = False):returntry:- if hasattr(env, 'cleanup'):- # Pass force_remove only if the env's cleanup() accepts it- # (DockerEnvironment after issue #20561; other backends don't).- import inspect- sig = inspect.signature(env.cleanup)- if "force_remove" in sig.parameters:- env.cleanup(force_remove=force_remove)- else:- env.cleanup()- elif hasattr(env, 'stop'):- env.stop()- elif hasattr(env, 'terminate'):- env.terminate()+ _cleanup_env(env, force_remove=force_remove)logger.info("Manually cleaned up environment for task: %s", task_id)