From 17df4741df608066e2a4ae0b37a39486efcfa770 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 2 Sep 2026 15:42:26 -0700 Subject: [PATCH] refactor(terminal): extract scope-aware TERMINAL_* config readers/cwd sanitizers to terminal_tool_config --- tools/terminal_tool.py | 101 ++++----------------------------- tools/terminal_tool_config.py | 103 ++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 89 deletions(-) create mode 100644 tools/terminal_tool_config.py diff --git a/tools/terminal_tool.py b/tools/terminal_tool.py index b98e6caf7a..f45c5b2473 100644 --- a/tools/terminal_tool.py +++ b/tools/terminal_tool.py @@ -43,6 +43,18 @@ def _redact_terminal_error_text(value: Any) -> str: # kill long-running subprocesses instead of blocking until timeout. from tools.interrupt import is_interrupted, _interrupt_event # noqa: F401 — re-exported from tools.registry import tool_error +from tools.terminal_tool_config import ( # noqa: F401 (re-exported; tests patch tools.terminal_tool.) + _CONTAINER_BACKENDS, + _HOST_CWD_PREFIXES, + _get_plugin_env_provider, + _is_container_backend, + _is_unusable_container_cwd, + _parse_env_var, + _plugin_env_flag, + _safe_getcwd, + _tenv, + _tenv_bool, +) from tools.terminal_tool_backends import ( # noqa: F401 (re-exported; tests patch tools.terminal_tool.) _ENV_BUILDERS, _REQUIREMENT_CHECKERS, @@ -627,95 +639,6 @@ def _resolve_task_host_cwd(config: Dict[str, Any], task_id: Optional[str]) -> Op return candidate -def _parse_env_var(name: str, default: str, converter: Any = int, type_label: str = "integer"): - """Parse an env var with *converter*, raising a clear ValueError on bad - values (e.g. TERMINAL_TIMEOUT=5m) instead of an opaque crash. TERMINAL_* - names are read scope-aware via :func:`_tenv`.""" - raw = os.getenv(name, default) - if name.startswith("TERMINAL_"): - raw = _tenv(name, default) - try: - return converter(raw) - except (ValueError, json.JSONDecodeError): - raise ValueError( - f"Invalid value for {name}: {raw!r} (expected {type_label}). " - f"Check ~/.hermes/.env or environment variables." - ) - - -def _safe_getcwd() -> str: - """``os.getcwd()`` tolerant of a deleted cwd (FileNotFoundError) or a macOS - TCC-protected one without Full Disk Access (PermissionError); falls back - to TERMINAL_CWD, then the home directory.""" - try: - return os.getcwd() - except (FileNotFoundError, PermissionError): - return _tenv("TERMINAL_CWD") or os.path.expanduser("~") - - -# Host-cwd prefixes that cannot exist inside a container sandbox (POSIX user -# dirs and Windows drive paths as they leak toward a Linux ``-w`` flag). -_HOST_CWD_PREFIXES = ("/Users/", "/home/", "C:\\", "C:/") - -_CONTAINER_BACKENDS = frozenset({"docker", "singularity", "modal", "daytona", "vercel_sandbox"}) - - -def _plugin_env_flag(env_type: str, attr: str, default=False): - """Classification flag of a plugin-registered backend. Fail-soft: *default* - when the registry is unavailable, the backend unknown, or the provider - raises — a misbehaving plugin must never take the terminal tool down.""" - if not env_type or env_type in _CONTAINER_BACKENDS or env_type in {"local", "ssh", "managed_modal"}: - return default - try: - from agent.terminal_env_registry import provider_flag - - return provider_flag(env_type, attr, default) - except Exception: - return default - - -def _is_container_backend(env_type: str) -> bool: - """True for built-in container backends and plugins declaring ``is_container``.""" - return env_type in _CONTAINER_BACKENDS or _plugin_env_flag(env_type, "is_container") - - -def _get_plugin_env_provider(env_type: str): - """Return the registered plugin provider for *env_type*, or None.""" - if not env_type or env_type in _CONTAINER_BACKENDS or env_type in {"local", "ssh", "managed_modal"}: - return None - try: - from agent.terminal_env_registry import get_provider - - return get_provider(env_type) - except Exception: - return None - - -def _is_unusable_container_cwd(cwd: str) -> bool: - """True if *cwd* is a host or relative path that can't be a container - workdir: ``docker run -w`` needs an absolute in-sandbox path, otherwise the - container fails to start (exit 125). Windows drive paths aren't ``isabs`` - on POSIX, so they're caught by the prefix check.""" - if not cwd: - return False - return cwd.startswith(_HOST_CWD_PREFIXES) or not os.path.isabs(cwd) - - -def _tenv(name: str, default: str = "") -> str: - """Scope-aware read of a ``TERMINAL_*`` variable. Every terminal setting - must go through this: under gateway multiplexing the active profile's - config arrives via a per-turn scope, and a raw ``os.getenv`` would read - whatever a previous turn pinned into the process env (cross-profile leak).""" - from tools.terminal_scope import terminal_env - - return terminal_env(name, default) - - -def _tenv_bool(name: str, default: str) -> bool: - """Scope-aware boolean ``TERMINAL_*`` read: true/1/yes (case-insensitive).""" - return _tenv(name, default).lower() in {"true", "1", "yes"} - - # One-shot guard for the config-fallback bridge: after the first attempt # either TERMINAL_ENV is set or the import failed, so retrying is wasted work. _terminal_config_bridge_attempted = False diff --git a/tools/terminal_tool_config.py b/tools/terminal_tool_config.py new file mode 100644 index 0000000000..5e3e21bdf2 --- /dev/null +++ b/tools/terminal_tool_config.py @@ -0,0 +1,103 @@ +"""Terminal backend configuration: scope-aware TERMINAL_* reads, env-var parsing, container-cwd sanity checks, plugin-backend classification and the resolved config dict (_get_env_config). + +Split out of ``tools/terminal_tool.py``; every public/patched name is re-imported there, +so ``tools.terminal_tool.`` keeps resolving (and monkeypatching) as before. +""" + +import logging +import json +import os +from typing import Any + +# Log-record parity with the origin module. +logger = logging.getLogger("tools.terminal_tool") + + +def _parse_env_var(name: str, default: str, converter: Any = int, type_label: str = "integer"): + """Parse an env var with *converter*, raising a clear ValueError on bad + values (e.g. TERMINAL_TIMEOUT=5m) instead of an opaque crash. TERMINAL_* + names are read scope-aware via :func:`_tenv`.""" + raw = os.getenv(name, default) + if name.startswith("TERMINAL_"): + raw = _tenv(name, default) + try: + return converter(raw) + except (ValueError, json.JSONDecodeError): + raise ValueError( + f"Invalid value for {name}: {raw!r} (expected {type_label}). " + f"Check ~/.hermes/.env or environment variables." + ) + + +def _safe_getcwd() -> str: + """``os.getcwd()`` tolerant of a deleted cwd (FileNotFoundError) or a macOS + TCC-protected one without Full Disk Access (PermissionError); falls back + to TERMINAL_CWD, then the home directory.""" + try: + return os.getcwd() + except (FileNotFoundError, PermissionError): + return _tenv("TERMINAL_CWD") or os.path.expanduser("~") + + +# Host-cwd prefixes that cannot exist inside a container sandbox (POSIX user +# dirs and Windows drive paths as they leak toward a Linux ``-w`` flag). +_HOST_CWD_PREFIXES = ("/Users/", "/home/", "C:\\", "C:/") + + +_CONTAINER_BACKENDS = frozenset({"docker", "singularity", "modal", "daytona", "vercel_sandbox"}) + + +def _plugin_env_flag(env_type: str, attr: str, default=False): + """Classification flag of a plugin-registered backend. Fail-soft: *default* + when the registry is unavailable, the backend unknown, or the provider + raises — a misbehaving plugin must never take the terminal tool down.""" + if not env_type or env_type in _CONTAINER_BACKENDS or env_type in {"local", "ssh", "managed_modal"}: + return default + try: + from agent.terminal_env_registry import provider_flag + + return provider_flag(env_type, attr, default) + except Exception: + return default + + +def _is_container_backend(env_type: str) -> bool: + """True for built-in container backends and plugins declaring ``is_container``.""" + return env_type in _CONTAINER_BACKENDS or _plugin_env_flag(env_type, "is_container") + + +def _get_plugin_env_provider(env_type: str): + """Return the registered plugin provider for *env_type*, or None.""" + if not env_type or env_type in _CONTAINER_BACKENDS or env_type in {"local", "ssh", "managed_modal"}: + return None + try: + from agent.terminal_env_registry import get_provider + + return get_provider(env_type) + except Exception: + return None + + +def _is_unusable_container_cwd(cwd: str) -> bool: + """True if *cwd* is a host or relative path that can't be a container + workdir: ``docker run -w`` needs an absolute in-sandbox path, otherwise the + container fails to start (exit 125). Windows drive paths aren't ``isabs`` + on POSIX, so they're caught by the prefix check.""" + if not cwd: + return False + return cwd.startswith(_HOST_CWD_PREFIXES) or not os.path.isabs(cwd) + + +def _tenv(name: str, default: str = "") -> str: + """Scope-aware read of a ``TERMINAL_*`` variable. Every terminal setting + must go through this: under gateway multiplexing the active profile's + config arrives via a per-turn scope, and a raw ``os.getenv`` would read + whatever a previous turn pinned into the process env (cross-profile leak).""" + from tools.terminal_scope import terminal_env + + return terminal_env(name, default) + + +def _tenv_bool(name: str, default: str) -> bool: + """Scope-aware boolean ``TERMINAL_*`` read: true/1/yes (case-insensitive).""" + return _tenv(name, default).lower() in {"true", "1", "yes"}