The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(terminal): move _call_in_flight into the lifecycle sibling
unsupportedpastelsebb840e8cdb9
diff --git a/tools/code_execution_tool.py b/tools/code_execution_tool.pyindex 6333d6c5442..365645b04f6 100644--- a/tools/code_execution_tool.py+++ b/tools/code_execution_tool.py@@ -662,7 +662,8 @@ def _execute_remote(code: str, task_id: Optional[str], enabled_tools: Optional[l"""Run code on the remote terminal backend: the owner's persistent remote session kernel(tools/code_kernel_remote.py) first, else the per-call script ship — the fail-open route whena kernel cannot be spawned and the only route for hosts that cannot sustain a background process."""- from tools.terminal_tool import _call_in_flight, _resolve_container_task_id+ from tools.terminal_tool import _resolve_container_task_id+ from tools.terminal_tool_lifecycle import _call_in_flight_cfg = _load_config()timeout, max_tool_calls = _cfg.get("timeout", DEFAULT_TIMEOUT), _cfg.get("max_tool_calls", DEFAULT_MAX_TOOL_CALLS)sandbox_tools, effective_task_id = _sandbox_tools_for(enabled_tools), task_id or "default"diff --git a/tools/terminal_tool.py b/tools/terminal_tool.pyindex f6b75a6398b..b3cd0ada3b9 100644--- a/tools/terminal_tool.py+++ b/tools/terminal_tool.py@@ -26,7 +26,6 @@ import sysimport timeimport threadingimport atexit-from contextlib import contextmanagerfrom dataclasses import dataclassfrom typing import Optional, Dict, Any, List@@ -42,7 +41,7 @@ def _redact_terminal_error_text(value: Any) -> str:from tools.registry import tool_errorfrom tools.terminal_tool_lifecycle import (- _cleanup_inactive_envs, _create_configured_env,+ _call_in_flight, _cleanup_inactive_envs, _create_configured_env,_evict_environment_for_task, cleanup_all_environments, ensure_task_env,)from tools.terminal_tool_config import (@@ -1301,31 +1300,6 @@ def _acquire_env(plan: _ExecPlan, task_id: Optional[str]) -> Any:return new_env-@contextmanager-def _call_in_flight(*keys: Optional[str]):- """Hold the env cached under *keys* against the idle reaper for the duration of a call.-- Only background processes refresh ``_last_activity`` mid-run, so a foreground command- longer than ``lifetime_seconds`` had its sandbox reaped out from under it. Refcounted- (parallel calls share a key); exit restamps, so the idle countdown starts at call end.- """- held = [k for k in dict.fromkeys(keys) if k]- with _env_lock:- for k in held:- _calls_in_flight[k] = _calls_in_flight.get(k, 0) + 1- try:- yield- finally:- with _env_lock:- now = time.time()- for k in held:- left = _calls_in_flight.pop(k) - 1- if left:- _calls_in_flight[k] = left- if k in _last_activity:- _last_activity[k] = now--def _yield_kwargs(command: str, **ctx) -> dict:"""``env.execute`` kwargs enabling yield-to-background (local backend only)."""handler = yield_to_background_handler(command=command, **ctx)diff --git a/tools/terminal_tool_lifecycle.py b/tools/terminal_tool_lifecycle.pyindex 613bc846526..5bb9d6137f3 100644--- a/tools/terminal_tool_lifecycle.py+++ b/tools/terminal_tool_lifecycle.py@@ -11,6 +11,7 @@ import loggingimport inspectimport threadingimport time+from contextlib import contextmanagerfrom typing import Any, Dict, Optionalfrom tools.terminal_tool_backends import (_container_config_from_config,@@ -104,6 +105,33 @@ def _unregister_env(task_id: str):return env+@contextmanager+def _call_in_flight(*keys: Optional[str]):+ """Hold the env cached under *keys* against the idle reaper for the duration of a call.++ Only background processes on workspace keys refresh ``_last_activity`` mid-run, so a+ foreground command longer than ``lifetime_seconds`` had its sandbox reaped out from under+ it. Refcounted (parallel calls share a key); exit restamps, so the idle countdown starts at+ call end.+ """+ from tools.terminal_tool import _calls_in_flight, _env_lock, _last_activity+ held = [k for k in dict.fromkeys(keys) if k]+ with _env_lock:+ for k in held:+ _calls_in_flight[k] = _calls_in_flight.get(k, 0) + 1+ try:+ yield+ finally:+ with _env_lock:+ now = time.time()+ for k in held:+ left = _calls_in_flight.pop(k) - 1+ if left:+ _calls_in_flight[k] = left+ if k in _last_activity:+ _last_activity[k] = now++def _cleanup_inactive_envs(lifetime_seconds: int = 300):"""Clean up environments that have been inactive for longer than lifetime_seconds."""from tools.terminal_tool import (