Skip to content

The agent that grows with you

refactor(tools): unify docker probe/start helpers, collapse egress arg parser, compact env module docstrings

Teknium132d67645c82
diff --git a/tools/environments/base.py b/tools/environments/base.py
index dbe848248ff..ad0577afb9b 100644
--- a/tools/environments/base.py
+++ b/tools/environments/base.py
@@ -22,31 +22,18 @@ from typing import Callable, Iterable
from hermes_constants import get_hermes_home
from tools.interrupt import is_interrupted, is_thread_interrupted
from tools.environments.base_output import ( # noqa: F401
- ProcessHandle,
- _BoundedOutputCollector,
- _ThreadedProcessHandle,
- _UNBOUNDED_CAPTURE_CHARS,
- _finalize_wait_result,
- _new_output_collector,
- _pipe_stdin,
- _popen_bash,
- _start_drain_thread)
+ ProcessHandle, _BoundedOutputCollector, _ThreadedProcessHandle, _UNBOUNDED_CAPTURE_CHARS,
+ _finalize_wait_result, _new_output_collector, _pipe_stdin, _popen_bash, _start_drain_thread,
+)
from tools.environments.base_session_env import ( # noqa: F401
- _SHELL_ENV_NAME_RE,
- _SNAP_TMP,
- _SNAP_TMP_SUFFIX,
- _SNAPSHOT_EXCLUDED_ENV_REGEX,
- _cwd_marker,
- _export_dump_excluding_session_vars,
- _snapshot_bootstrap_script,
- _split_cwd_marker,
- _wrap_command_script)
+ _SHELL_ENV_NAME_RE, _SNAP_TMP, _SNAP_TMP_SUFFIX, _SNAPSHOT_EXCLUDED_ENV_REGEX, _cwd_marker,
+ _export_dump_excluding_session_vars, _snapshot_bootstrap_script, _split_cwd_marker,
+ _wrap_command_script,
+)
from tools.environments.base_wait import _WaitTrace
from tools.environments.path_utils import ( # noqa: F401
- _SANDBOX_DIR_HASH_LEN,
- _SANDBOX_DIR_MAX_LEN,
- _SANDBOX_DIR_UNSAFE_RE,
- sanitize_task_id_for_path)
+ _SANDBOX_DIR_HASH_LEN, _SANDBOX_DIR_MAX_LEN, _SANDBOX_DIR_UNSAFE_RE, sanitize_task_id_for_path,
+)
logger = logging.getLogger(__name__)
@@ -183,10 +170,7 @@ class BaseEnvironment(ABC):
# so execute() must fall back to non-login ``bash -c``, not ``bash -l``.
self._prefer_nonlogin = False
- # ------------------------------------------------------------------
- # Abstract methods
- # ------------------------------------------------------------------
-
+ # --- Abstract methods ---
def _run_bash(
self, cmd_string: str, *, login: bool = False, timeout: int = 120, stdin_data: str | None = None,
) -> ProcessHandle:
@@ -198,10 +182,7 @@ class BaseEnvironment(ABC):
"""Release backend resources (container, instance, connection)."""
...
- # ------------------------------------------------------------------
- # Session snapshot (init_session)
- # ------------------------------------------------------------------
-
+ # --- Session snapshot (init_session) ---
def _additional_profile_scoped_passthrough_names(self) -> Iterable[str]:
"""Return backend-specific names that must not persist in snapshots."""
return ()
@@ -258,8 +239,7 @@ class BaseEnvironment(ABC):
self._session_id, exc)
else:
logger.warning(
- "init_session failed (session=%s): %s — "
- "falling back to bash -l per command",
+ "init_session failed (session=%s): %s — falling back to bash -l per command",
self._session_id, detail)
def _probe_nonlogin_fallback(self, detail: str) -> tuple[bool, str]:
@@ -275,10 +255,7 @@ class BaseEnvironment(ABC):
except Exception as probe_exc:
return False, f"{detail}; non-login probe: {probe_exc}"
- # ------------------------------------------------------------------
- # Command wrapping
- # ------------------------------------------------------------------
-
+ # --- Command wrapping ---
@staticmethod
def _quote_cwd_for_cd(cwd: str) -> str:
"""Quote a ``cd`` target while preserving ``~`` expansion (``~/...``
@@ -310,29 +287,24 @@ class BaseEnvironment(ABC):
delimiter = f"HERMES_STDIN_{uuid.uuid4().hex[:12]}"
return f"{command} << '{delimiter}'\n{stdin_data}\n{delimiter}"
- # ------------------------------------------------------------------
- # Process lifecycle
- # ------------------------------------------------------------------
-
+ # --- Process lifecycle ---
def _wait_for_process(
self, proc: ProcessHandle, timeout: int = 120, *,
bounded_capture: bool = False, watch_interrupt_tid: int | None = None) -> dict:
"""Poll-based wait with interrupt checking and stdout draining (shared, not overridden).
-
``bounded_capture=True`` (foreground terminal-tool path only) retains at most
``tool_output.max_bytes`` in a head/tail window so a verbose subprocess cannot OOM the
- process; the default keeps full fidelity for internal consumers where truncation
- corrupts data. Fires the activity callback every 10s so the gateway's inactivity
- timeout doesn't kill long commands. ``watch_interrupt_tid`` is the tool-worker thread
- that submitted this wait: ``execute()`` may move the wait onto a ``run_bounded_sync``
- worker while ``/stop`` still interrupts the original tid, so both bits are honored.
- ``KeyboardInterrupt``/``SystemExit`` mid-poll kills the process first — the local
- backend spawns into its own process group, so an unkilled child would be orphaned.
- """
+ process; the default keeps full fidelity for internal consumers. Fires the activity
+ callback every 10s so the gateway's inactivity timeout doesn't kill long commands.
+ ``watch_interrupt_tid`` is the tool-worker thread that submitted this wait: ``execute()``
+ may move the wait onto a ``run_bounded_sync`` worker while ``/stop`` still interrupts the
+ original tid, so both bits are honored. ``KeyboardInterrupt``/``SystemExit`` mid-poll
+ kills the process first — the local backend spawns into its own process group, so an
+ unkilled child would be orphaned."""
output = _new_output_collector(proc, bounded_capture)
drain_thread = _start_drain_thread(proc, output)
- deadline = time.monotonic() + timeout
_now = time.monotonic()
+ deadline = _now + timeout
_activity_state = {"last_touch": _now, "start": _now}
trace = _WaitTrace(proc, timeout, enabled=_DEBUG_INTERRUPT, logger=logger)
trace.enter()
@@ -388,10 +360,8 @@ class BaseEnvironment(ABC):
stdin_thread.join(timeout=5)
rendered = output.render()
result = self._finalize_wait_result(output, rendered, proc.returncode)
- stdin_errors = getattr(proc, "_hermes_stdin_errors", None)
- if stdin_errors:
- err = str(stdin_errors[0])
- result["stdin_error"] = err
+ if stdin_errors := getattr(proc, "_hermes_stdin_errors", None):
+ result["stdin_error"] = err = str(stdin_errors[0])
result["output"] = rendered + f"\n[stdin write failed: {err}]"
return result
@@ -404,10 +374,7 @@ class BaseEnvironment(ABC):
except (ProcessLookupError, PermissionError, OSError):
pass
- # ------------------------------------------------------------------
- # CWD extraction
- # ------------------------------------------------------------------
-
+ # --- CWD extraction ---
def _update_cwd(self, result: dict):
"""Extract CWD from command output. Override for local file-based read."""
self._extract_cwd_from_output(result)
@@ -428,19 +395,13 @@ class BaseEnvironment(ABC):
result["cwd"] = cwd_path
result["output"] = cleaned
- # ------------------------------------------------------------------
- # Hooks
- # ------------------------------------------------------------------
-
+ # --- Hooks ---
def _before_execute(self) -> None:
"""Hook before each command. Remote backends (SSH, Modal, Daytona)
trigger their FileSyncManager here; bind-mount backends and Local don't."""
pass
- # ------------------------------------------------------------------
- # Unified execute()
- # ------------------------------------------------------------------
-
+ # --- Unified execute() ---
def execute(
self,
command: str,
@@ -450,15 +411,12 @@ class BaseEnvironment(ABC):
stdin_data: str | None = None,
rewrite_compound_background: bool = True,
bounded_capture: bool = False) -> dict:
- """Execute a command, return {"output": str, "returncode": int}.
-
- ``bounded_capture=True`` caps retention at ``tool_output.max_bytes`` WHILE draining;
- only the foreground terminal tool may set it — internal full-fidelity consumers
- (file-op ``cat`` reads feeding the patch engine, RPC reads, log reads) MUST leave it
- False or data is corrupted. The wait is bounded by ``agent.deadline.run_bounded_sync``
- so a wedged poll loop cannot hang past ``timeout`` and silently disable every asyncio
- timer in the process.
- """
+ """Execute a command, return {"output": str, "returncode": int}. ``bounded_capture=True``
+ caps retention at ``tool_output.max_bytes`` WHILE draining; only the foreground terminal
+ tool may set it — internal full-fidelity consumers (file-op ``cat`` reads feeding the
+ patch engine, RPC reads, log reads) MUST leave it False or data is corrupted. The wait is
+ bounded by ``agent.deadline.run_bounded_sync`` so a wedged poll loop cannot hang past
+ ``timeout`` and silently disable every asyncio timer in the process."""
self._before_execute()
exec_command, sudo_stdin = self._prepare_command(command)
@@ -508,10 +466,10 @@ class BaseEnvironment(ABC):
from agent.deadline import run_bounded_sync
try:
- bound_s = float(effective_timeout) + _EXECUTE_WAIT_BOUND_GRACE_S
+ bound_s = float(effective_timeout)
except (TypeError, ValueError):
- # A non-numeric timeout must not disable the backstop; use the 120s default.
- bound_s = 120.0 + _EXECUTE_WAIT_BOUND_GRACE_S
+ bound_s = 120.0 # a non-numeric timeout must not disable the backstop
+ bound_s += _EXECUTE_WAIT_BOUND_GRACE_S
try:
bounded = run_bounded_sync(
@@ -520,11 +478,9 @@ class BaseEnvironment(ABC):
_on_timeout()
raise
- if bounded.timed_out:
- timeout_msg = f"\n[Command timed out after {effective_timeout}s]"
- result = {"output": timeout_msg.lstrip(), "returncode": 124}
- else:
- result = bounded.value
+ result = (
+ {"output": f"[Command timed out after {effective_timeout}s]", "returncode": 124}
+ if bounded.timed_out else bounded.value)
self._update_cwd(result)
return result
@@ -543,10 +499,7 @@ class BaseEnvironment(ABC):
except Exception:
logger.debug("terminal wait-bound kill_process_tree failed", exc_info=True)
- # ------------------------------------------------------------------
- # Shared helpers
- # ------------------------------------------------------------------
-
+ # --- Shared helpers ---
def stop(self):
"""Alias for cleanup (compat with older callers)."""
self.cleanup()
diff --git a/tools/environments/base_output.py b/tools/environments/base_output.py
index c7d8aab9bbf..460568cd25c 100644
--- a/tools/environments/base_output.py
+++ b/tools/environments/base_output.py
@@ -128,8 +128,7 @@ class _BoundedOutputCollector:
chunk = text[start:]
self._tail.append(chunk)
self._tail_chars += len(chunk)
- while self._tail_chars > self._tail_limit:
- excess = self._tail_chars - self._tail_limit
+ while (excess := self._tail_chars - self._tail_limit) > 0:
first = self._tail[0]
if len(first) <= excess:
self._tail.popleft()
@@ -154,9 +153,7 @@ class _BoundedOutputCollector:
# the notice length; iterate to a fixed point (converges quickly).
notice = ""
for _ in range(4):
- content_budget = max(0, available - len(notice))
- head_chars = int(content_budget * 0.4)
- omitted = max(0, self._total_chars - content_budget)
+ omitted = max(0, self._total_chars - max(0, available - len(notice)))
updated = (
f"\n\n... [OUTPUT TRUNCATED - {omitted:,} chars omitted "
f"out of {self._total_chars:,} total] ...\n\n")
@@ -211,14 +208,9 @@ def _finalize_wait_result(collector: _BoundedOutputCollector, rendered: str, ret
return result
-# ---------------------------------------------------------------------------
-# Stdin / spawn helpers
-# ---------------------------------------------------------------------------
-
-
+# --- Stdin / spawn helpers ---
def _pipe_stdin(proc: subprocess.Popen, data: str) -> None:
"""Write *data* to proc.stdin on a daemon thread to avoid pipe-buffer deadlocks.
-
Writes go through ``proc.stdin.buffer`` as UTF-8 bytes we encode ourselves: Windows
text-mode stdin would translate ``\\n`` -> ``\\r\\n`` and corrupt every write_file/patch
payload. Encoding uses ``surrogateescape`` (exact inverse of the read-side decode);
@@ -275,11 +267,7 @@ def _popen_bash(cmd: list[str], stdin_data: str | None = None, **kwargs) -> subp
return proc
-# ---------------------------------------------------------------------------
-# ProcessHandle protocol
-# ---------------------------------------------------------------------------
-
-
+# --- ProcessHandle protocol ---
class ProcessHandle(Protocol):
"""Duck type every backend's _run_bash() must return. subprocess.Popen satisfies this
natively; SDK backends (Modal, Daytona) return _ThreadedProcessHandle."""
@@ -304,7 +292,6 @@ class _ThreadedProcessHandle:
self._cancel_fn = cancel_fn
self._done = threading.Event()
self._returncode: int | None = None
- self._error: Exception | None = None
# Pipe for stdout — the drain thread in _wait_for_process reads the read end.
read_fd, write_fd = os.pipe()
@@ -319,8 +306,7 @@ class _ThreadedProcessHandle:
os.write(self._write_fd, output.encode("utf-8", errors="replace"))
except OSError:
pass
- except Exception as exc:
- self._error = exc
+ except Exception:
self._returncode = 1
finally:
try:
@@ -354,14 +340,9 @@ class _ThreadedProcessHandle:
return self._returncode
-# ---------------------------------------------------------------------------
-# Stdout drain thread
-# ---------------------------------------------------------------------------
-
-
+# --- Stdout drain thread ---
def _drain_stdout(proc: ProcessHandle, output: _BoundedOutputCollector) -> None:
"""Drain ``proc.stdout`` into *output* until EOF or shortly after exit.
-
``for line in proc.stdout`` would block on ``readline()`` until EOF, and a backgrounded
grandchild (``cmd &``, ``setsid cmd & disown``) inherits the pipe's write end — so the
tool would hang for the grandchild's lifetime. Instead we ``select()`` with a short poll
@@ -376,10 +357,9 @@ def _drain_stdout(proc: ProcessHandle, output: _BoundedOutputCollector) -> None:
if stream is None:
return
decoder = codecs.getincrementaldecoder("utf-8")(errors="replace")
- fileno = getattr(stream, "fileno", None)
try:
- fd = fileno() if callable(fileno) else None
- except Exception:
+ fd = stream.fileno()
+ except Exception: # mocks / in-memory adapters without a real descriptor
fd = None
try:
if not isinstance(fd, int) or fd < 0:
diff --git a/tools/environments/base_session_env.py b/tools/environments/base_session_env.py
index ff29649b6f6..12a8f85b479 100644
--- a/tools/environments/base_session_env.py
+++ b/tools/environments/base_session_env.py
@@ -36,50 +36,42 @@ def _cwd_marker_printf(marker: str) -> str:
def _export_dump_excluding_session_vars(tmp_path: str, excluded_names: Iterable[str] = ()) -> str:
- """Shell snippet dumping ``export -p`` to *tmp_path* minus the per-session bridged vars
- (see ``_SNAPSHOT_EXCLUDED_ENV_REGEX``) and *excluded_names*.
-
- The vars are ``unset`` in a subshell BEFORE ``export -p``: a line-based ``grep -vE`` is
- unsafe because bash 3.2 prints a value containing a newline as a multi-line ``declare -x``
- block, so smuggled continuation lines would survive into the snapshot and execute on the
- next ``source``. ``|| true`` keeps the success contract. The dump is a brace group with the
- redirection on the group: *tmp_path* is usually a shell-variable expansion, and a redirect
- on a pipeline segment would expand it inside that segment's subshell, inconsistently with
- the parent that expands the follow-up ``mv``.
- """
+ """Shell snippet dumping ``export -p`` to *tmp_path* minus the per-session bridged vars (see
+ ``_SNAPSHOT_EXCLUDED_ENV_REGEX``) and *excluded_names*. The vars are ``unset`` in a subshell
+ BEFORE ``export -p``: a line-based ``grep -vE`` is unsafe because bash 3.2 prints a value
+ containing a newline as a multi-line ``declare -x`` block, so smuggled continuation lines would
+ survive into the snapshot and execute on the next ``source``. ``|| true`` keeps the success
+ contract. The dump is a brace group with the redirection on the group: *tmp_path* is usually a
+ shell-variable expansion, and a redirect on a pipeline segment would expand it inside that
+ segment's subshell, inconsistently with the parent that expands the follow-up ``mv``."""
# ${!PREFIX*} is bash 3.2+ name-prefix expansion; empty matches are ignored
# under 2>/dev/null. Caller names are quoted so malformed config can never
# become shell syntax (valid names stay unquoted by shlex.quote()).
safe_names = {name for name in excluded_names if isinstance(name, str) and name}
extra_unset = "".join(f" {shlex.quote(name)}" for name in sorted(safe_names))
return (
- "{ ( "
- "unset ${!HERMES_SESSION_*} ${!HERMES_CRON_AUTO_DELIVER_*} "
+ "{ ( unset ${!HERMES_SESSION_*} ${!HERMES_CRON_AUTO_DELIVER_*} "
"${!HERMES_BROWSER_CONTROL_*} "
# AI_AGENT / HERMES_AGENT are per-command attribution markers re-exported
# by every wrapper with ${VAR:-default} semantics; persisting them would
# let the FIRST command's value override a later outer-harness value.
"AI_AGENT HERMES_AGENT "
f"HERMES_UI_SESSION_ID{extra_unset} 2>/dev/null; "
- "export -p; "
- ") || true; } "
+ "export -p; ) || true; } "
f"> {tmp_path}")
def _snapshot_bootstrap_script(
*, quoted_cwd: str, quoted_snap: str, snap_tmp_template: str, excluded_names: Iterable[str], cwd_marker: str,
) -> str:
- """Login-shell bootstrap that captures env/functions/aliases into the snapshot.
-
- Atomic publish: assemble in a ``mktemp`` file, then ``mv`` over the final path so a
- concurrent ``source`` never reads a half-written snapshot (``$$`` is the parent PID in
- ``&``-launched subshells and macOS bash 3.2 lacks ``$BASHPID``, so only ``mktemp`` is
- portable). Functions are filtered by NAME via ``declare -F`` (a line-based
- ``declare -f | grep -v`` strips the header and leaves an orphaned body that breaks every
- sourced command); the non-empty guard matters because bare ``declare -f`` dumps ALL
- functions. The trailing ``cd`` restores the configured cwd after profile scripts
- (e.g. ``cd ~``) so ``pwd -P`` reports terminal.cwd, not the profile's directory.
- """
+ """Login-shell bootstrap that captures env/functions/aliases into the snapshot. Atomic publish:
+ assemble in a ``mktemp`` file, then ``mv`` over the final path so a concurrent ``source`` never
+ reads a half-written snapshot (``$$`` is the parent PID in ``&``-launched subshells and macOS
+ bash 3.2 lacks ``$BASHPID``, so only ``mktemp`` is portable). Functions are filtered by NAME via
+ ``declare -F`` (a line-based ``declare -f | grep -v`` strips the header and leaves an orphaned
+ body that breaks every sourced command); the non-empty guard matters because bare ``declare -f``
+ dumps ALL functions. The trailing ``cd`` restores the configured cwd after profile scripts (e.g.
+ ``cd ~``) so ``pwd -P`` reports terminal.cwd, not the profile's directory."""
return (
"umask 077\n"
f"__hermes_snap_tmp=$(mktemp {snap_tmp_template}) || exit 1\n"
@@ -116,7 +108,6 @@ def _wrap_command_script(
command: str, *, quoted_cwd: str, quoted_snap: str, snap_tmp_template: str,
passthrough_names: Iterable[str], snapshot_ready: bool, cwd_marker: str) -> str:
"""Per-command bash script: source snapshot, cd, run, re-dump env, emit CWD marker.
-
``source`` stdout goes to /dev/null because macOS bash 3.2 / some Homebrew builds echo
``declare -x`` lines when sourcing. AI_AGENT/HERMES_AGENT advertise the harness to remote
backends (whose env is not inherited); ``${VAR:-default}`` never clobbers an outer harness.
diff --git a/tools/environments/docker.py b/tools/environments/docker.py
index a07e6182917..ce2bc690d39 100644
--- a/tools/environments/docker.py
+++ b/tools/environments/docker.py
@@ -20,18 +20,12 @@ import uuid
from pathlib import Path
from typing import Optional
-from tools.environments.base import BaseEnvironment, EnvironmentConnectionError, _popen_bash
+from tools.environments.base import BaseEnvironment, EnvironmentConnectionError, _SHELL_ENV_NAME_RE, _popen_bash
from tools.environments.docker_egress import ( # noqa: F401 — re-exported for tests/patch targets
- _EGRESS_LABEL_KEY,
- _critical_egress_env_names,
- _egress_enforce_on_docker,
- _egress_proxy_args_for_docker,
- _egress_reuse_fingerprint,
- _extra_args_egress_collisions,
- check_docker_env_collisions,
- check_extra_args_collisions,
- check_forward_env_collisions,
- merge_egress_env)
+ _EGRESS_LABEL_KEY, _critical_egress_env_names, _egress_enforce_on_docker, _egress_proxy_args_for_docker,
+ _egress_reuse_fingerprint, _extra_args_egress_collisions, check_docker_env_collisions,
+ check_extra_args_collisions, check_forward_env_collisions, merge_egress_env,
+)
from tools.environments.path_utils import sanitize_task_id_for_path
from tools.environments.remote_common import bash_argv, run_capture
from tools.environments.local import _HERMES_PROVIDER_ENV_BLOCKLIST, _is_hermes_internal_secret
@@ -41,12 +35,11 @@ logger = logging.getLogger(__name__)
# Docker Desktop install paths checked when 'docker' is not in PATH
# (macOS Intel / Apple Silicon Homebrew / app bundle).
_DOCKER_SEARCH_PATHS = [
- "/usr/local/bin/docker",
- "/opt/homebrew/bin/docker",
- "/Applications/Docker.app/Contents/Resources/bin/docker"]
+ "/usr/local/bin/docker", "/opt/homebrew/bin/docker", "/Applications/Docker.app/Contents/Resources/bin/docker",
+]
_docker_executable: Optional[str] = None # resolved once, cached
-_ENV_VAR_NAME_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
+_ENV_VAR_NAME_RE = _SHELL_ENV_NAME_RE
def _normalize_forward_env_names(forward_env: list[str] | None) -> list[str]:
@@ -122,13 +115,10 @@ def _get_active_profile_name() -> str:
def _container_identity(shared_key: str = "") -> str:
- """Profile label used for container reuse and orphan reaping.
-
- Profiles are isolated by default; an explicit shared key lets trusted profiles share
- one Docker identity. Label sanitization is lossy and reuse is label-keyed, so a digest
- of the raw key disambiguates colliding keys. Plain profile names keep their historical
- un-suffixed labels.
- """
+ """Profile label used for container reuse and orphan reaping. Profiles are isolated by default; an
+ explicit shared key lets trusted profiles share one Docker identity. Label sanitization is lossy
+ and reuse is label-keyed, so a digest of the raw key disambiguates colliding keys. Plain profile
+ names keep their historical un-suffixed labels."""
if not shared_key:
return _sanitize_label_value(_get_active_profile_name())
digest = hashlib.sha256(shared_key.encode("utf-8")).hexdigest()[:12]
@@ -149,13 +139,10 @@ def reap_orphan_containers(
if profile_filter:
filters.extend(["--filter", f"label=hermes-profile={_sanitize_label_value(profile_filter)}"])
- try:
- listing = run_capture([docker, "ps", "-a", *filters, "--format", "{{.ID}}"], timeout=15)
- except (subprocess.TimeoutExpired, OSError) as e:
- logger.debug("orphan reaper docker ps failed: %s", e)
- return 0
- if listing.returncode != 0:
- logger.debug("orphan reaper docker ps returned %d: %s", listing.returncode, listing.stderr.strip())
+ listing = _docker_query(
+ [docker, "ps", "-a", *filters, "--format", "{{.ID}}"], timeout=15,
+ fail="orphan reaper docker ps failed: %s", nonzero="orphan reaper docker ps returned %d: %s")
+ if listing is None:
return 0
# Per-container inspect keeps the failure blast radius to one container.
@@ -168,29 +155,28 @@ def reap_orphan_containers(
age = (now - finished_at).total_seconds()
if age < max_age_seconds:
continue
- try:
- result = run_capture([docker, "rm", "-f", cid], timeout=30)
- if result.returncode == 0:
- removed += 1
- logger.info("Reaped orphan container %s (exited %d seconds ago)", cid[:12], int(age))
- else:
- logger.debug("docker rm -f %s failed: %s", cid[:12], result.stderr.strip())
- except (subprocess.TimeoutExpired, OSError) as e:
- logger.debug("orphan reaper docker rm %s failed: %s", cid[:12], e)
+ result = _docker_query(
+ [docker, "rm", "-f", cid], timeout=30, fail="orphan reaper docker rm %s failed: %s", fail_args=(cid[:12],))
+ if result is None:
+ continue
+ if result.returncode == 0:
+ removed += 1
+ logger.info("Reaped orphan container %s (exited %d seconds ago)", cid[:12], int(age))
+ else:
+ logger.debug("docker rm -f %s failed: %s", cid[:12], result.stderr.strip())
return removed
def _container_finished_at(docker_exe: str, container_id: str):
"""``docker inspect`` FinishedAt as an aware datetime; ``None`` (= don't reap) when
missing/unparseable or Docker's never-finished zero value ``0001-01-01T00:00:00Z``."""
- try:
- result = run_capture(
- [docker_exe, "inspect", "--format", "{{.State.FinishedAt}}", container_id], timeout=10)
- except (subprocess.TimeoutExpired, OSError) as e:
- logger.debug("orphan reaper docker inspect %s failed: %s", container_id[:12], e)
+ result = _docker_query(
+ [docker_exe, "inspect", "--format", "{{.State.FinishedAt}}", container_id], timeout=10,
+ fail="orphan reaper docker inspect %s failed: %s", fail_args=(container_id[:12],))
+ if result is None or result.returncode != 0:
return None
raw = result.stdout.strip()
- if result.returncode != 0 or not raw or raw.startswith("0001-01-01"):
+ if not raw or raw.startswith("0001-01-01"):
return None
# Docker emits RFC3339 with nanoseconds; fromisoformat only takes microseconds.
raw = re.sub(r"(\.\d{6})\d+", r"\1", raw).replace("Z", "+00:00")
@@ -205,6 +191,24 @@ def _is_executable(path: str) -> bool:
return os.path.isfile(path) and os.access(path, os.X_OK)
+def _docker_query(
+ argv: list[str], *, timeout: float, fail: str, fail_args: tuple = (), nonzero: str | None = None,
+ exc_types: tuple = (subprocess.TimeoutExpired, OSError),
+):
+ """Best-effort ``run_capture`` for probes: logs *fail* (``*fail_args, exc``) at debug and
+ returns ``None`` when the CLI raises; with *nonzero* set, a nonzero exit also logs
+ (``*fail_args, returncode, stderr``) and returns ``None``."""
+ try:
+ result = run_capture(argv, timeout=timeout)
+ except exc_types as e:
+ logger.debug(fail, *fail_args, e)
+ return None
+ if nonzero is not None and result.returncode != 0:
+ logger.debug(nonzero, *fail_args, result.returncode, result.stderr.strip())
+ return None
+ return result
+
+
def find_docker() -> Optional[str]:
"""Locate the docker/podman CLI (cached): ``HERMES_DOCKER_BINARY`` override, ``docker``
on PATH, ``podman`` on PATH, then macOS Docker Desktop locations; ``None`` if absent."""
@@ -281,18 +285,12 @@ def _image_uses_init_entrypoint(docker_exe: str, image: str) -> bool:
"""True if ``image``'s entrypoint is the s6-overlay ``/init`` (own PID 1: incompatible
with ``--init`` and a noexec /run). Any inspection failure, including an image not yet
pulled, returns False and keeps the hardened defaults."""
- try:
- result = run_capture(
- [docker_exe, "image", "inspect", image, "--format", "{{json .Config.Entrypoint}}"], timeout=15)
- except (subprocess.SubprocessError, OSError) as e:
- logger.debug("Docker: could not inspect entrypoint for %s: %s", image, e)
- return False
- if result.returncode != 0:
- logger.debug(
- "Docker: image inspect for %s returned %d (stderr=%s)",
- image, result.returncode, result.stderr.strip())
- return False
- raw = (result.stdout or "").strip()
+ result = _docker_query(
+ [docker_exe, "image", "inspect", image, "--format", "{{json .Config.Entrypoint}}"], timeout=15,
+ fail="Docker: could not inspect entrypoint for %s: %s", fail_args=(image,),
+ nonzero="Docker: image inspect for %s returned %d (stderr=%s)",
+ exc_types=(subprocess.SubprocessError, OSError))
+ raw = (result.stdout or "").strip() if result is not None else ""
if not raw or raw == "null":
return False
try:
@@ -348,8 +346,7 @@ def _cgroup_limits_available(image: str) -> bool:
"Cgroup resource limits (--cpus/--memory/--pids-limit) not "
"available in this environment. Containers will run without "
"CPU, memory or PID limits. To enable, delegate the cpu, "
- "memory and pids cgroup controllers to this container. "
- "Probe stderr: %s",
+ "memory and pids cgroup controllers to this container. Probe stderr: %s",
(result.stderr or "").strip()[:500])
except Exception as e:
_cgroup_limits_ok = False
@@ -368,8 +365,7 @@ def _ensure_docker_available() -> None:
if not docker_exe:
raise _docker_unavailable(
"Docker backend selected but no docker executable was found in PATH "
- "or known install locations. Install Docker Desktop and ensure the "
- "CLI is available.",
+ "or known install locations. Install Docker Desktop and ensure the CLI is available.",
error="Docker executable not found in PATH or known install locations. "
"Install Docker and ensure the 'docker' command is available.",
hint="Install Docker (or fix PATH) and retry, or switch terminal.backend to 'local'.")
@@ -414,29 +410,23 @@ _RO_MOUNT_SOURCES = (
def _readonly_skill_mount_args() -> list[str]:
- """``-v host:container:ro`` args for credential files, skill dirs and cache dirs.
-
- Read-only so the container can authenticate/read but never modify host state. Missing
- or wrong-kind sources are skipped with a warning (Docker-in-Docker auto-creates a
- missing file source as a directory, which would exit 125).
- """
+ """``-v host:container:ro`` args for credential files, skill dirs and cache dirs. Read-only so the
+ container can authenticate/read but never modify host state. Missing or wrong-kind sources are
+ skipped with a warning (Docker-in-Docker auto-creates a missing file source as a directory,
+ which would exit 125)."""
args: list[str] = []
try:
import tools.credential_files as cf
for getter, expects_file, noun in _RO_MOUNT_SOURCES:
for entry in getattr(cf, getter)():
src = Path(entry["host_path"])
- if expects_file and src.is_dir():
- logger.warning(
- "Docker: skipping credential mount — source is a directory "
- "(likely Docker-in-Docker auto-creation): %s", src)
- continue
- if expects_file and not src.is_file():
- logger.warning("Docker: skipping credential mount — source not found: %s", src)
- continue
- if not expects_file and not src.is_dir():
- logger.warning("Docker: skipping %s mount — source is not a directory: %s",
- noun.split()[0], src)
+ if expects_file:
+ problem = ("source is a directory (likely Docker-in-Docker auto-creation)" if src.is_dir()
+ else None if src.is_file() else "source not found")
+ else:
+ problem = None if src.is_dir() else "source is not a directory"
+ if problem:
+ logger.warning("Docker: skipping %s mount — %s: %s", noun.split()[0], problem, src)
continue
args.extend(["-v", f"{entry['host_path']}:{entry['container_path']}:ro"])
logger.info("Docker: mounting %s %s -> %s", noun, entry["host_path"], entry["container_path"])
@@ -456,8 +446,7 @@ def _host_user_args(run_as_host_user: bool) -> list[str]:
return ["--user", user_spec]
logger.warning(
"docker_run_as_host_user is enabled but this platform does "
- "not expose POSIX uid/gid; container will start as its "
- "image default user.")
+ "not expose POSIX uid/gid; container will start as its image default user.")
return []
@@ -568,10 +557,7 @@ class DockerEnvironment(BaseEnvironment):
self._init_env_args = self._build_init_env_args()
self.init_session()
- # ------------------------------------------------------------------
- # __init__ helpers
- # ------------------------------------------------------------------
-
+ # --- __init__ helpers ---
def _egress_and_env_args(self, extra_args) -> tuple[str, list[str], list[str], list[str], list[str]]:
"""Egress credential-injection proxy plumbing (CA mount + HTTPS_PROXY/CA-bundle env so
outbound traffic routes through the host-side proxy and the sandbox receives proxy tokens
@@ -603,11 +589,11 @@ class DockerEnvironment(BaseEnvironment):
def _resource_args(self, image, cpu, memory, disk, network, shm_size, extra_args) -> list[str]:
"""cgroup-gated CPU/memory/pids limits, shm size, disk quota and network mode."""
args: list[str] = []
- if cpu > 0 and _cgroup_limits_available(image):
- args.extend(["--cpus", str(cpu)])
- if memory > 0 and _cgroup_limits_available(image):
- args.extend(["--memory", f"{memory}m"])
if _cgroup_limits_available(image):
+ if cpu > 0:
+ args.extend(["--cpus", str(cpu)])
+ if memory > 0:
+ args.extend(["--memory", f"{memory}m"])
args.extend(["--pids-limit", _DEFAULT_PIDS_LIMIT])
# --shm-size is a tmpfs option, not cgroup-gated. Skipped when the user
# sets it in docker_extra_args or opts out with empty/"0".
@@ -629,7 +615,6 @@ class DockerEnvironment(BaseEnvironment):
"""``(volume_args, writable_args)`` for user volumes, host cwd and /workspace,/root.
Persistent mode bind-mounts from TERMINAL_SANDBOX_DIR (default ~/.hermes/sandboxes/)."""
volume_args: list[str] = []
- workspace_explicitly_mounted = False
for vol in (volumes or []):
if not isinstance(vol, str):
logger.warning("Docker volume entry is not a string: %r", vol)
@@ -641,8 +626,7 @@ class DockerEnvironment(BaseEnvironment):
logger.warning("Docker volume '%s' missing colon, skipping", vol)
continue
volume_args.extend(["-v", vol])
- if ":/workspace" in vol:
- workspace_explicitly_mounted = True
+ workspace_explicitly_mounted = any(":/workspace" in v for v in volume_args)
host_cwd_abs = os.path.abspath(os.path.expanduser(host_cwd)) if host_cwd else ""
bind_host_cwd = (
@@ -666,8 +650,7 @@ class DockerEnvironment(BaseEnvironment):
os.makedirs(self._workspace_dir, exist_ok=True)
writable_args += ["-v", f"{self._workspace_dir}:/workspace"]
else:
- if mount_workspace:
- writable_args += ["--tmpfs", "/workspace:rw,exec,size=10g"]
+ writable_args += ["--tmpfs", "/workspace:rw,exec,size=10g"] if mount_workspace else []
writable_args += ["--tmpfs", "/home:rw,exec,size=1g", "--tmpfs", "/root:rw,exec,size=1g"]
if bind_host_cwd:
@@ -693,8 +676,7 @@ class DockerEnvironment(BaseEnvironment):
logger.warning(
"Existing container %s has NetworkMode=%s but "
"docker_network=false requests an air-gapped "
- "container — removing it and starting fresh "
- "(task=%s, profile=%s).",
+ "container — removing it and starting fresh (task=%s, profile=%s).",
container_id[:12], actual_mode or "unknown", task_label, profile_name)
try:
run_capture([self._docker_exe, "rm", "-f", container_id], timeout=30)
@@ -703,13 +685,12 @@ class DockerEnvironment(BaseEnvironment):
return False
if state != "running":
- try:
- run_capture([self._docker_exe, "start", container_id], timeout=30, check=True)
- except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as e:
+ err = self._start_container(container_id)
+ if err is not None:
logger.warning(
"Failed to start existing container %s (state=%s): "
"%s — falling back to a fresh container.",
- container_id[:12], state, e)
+ container_id[:12], state, err)
return False
self._container_id = container_id
logger.info(
@@ -717,6 +698,14 @@ class DockerEnvironment(BaseEnvironment):
container_id[:12], task_label, profile_name, state)
return True
+ def _start_container(self, container_id: str) -> Exception | None:
+ """``docker start`` a stopped container; returns the failure instead of raising."""
+ try:
+ run_capture([self._docker_exe, "start", container_id], timeout=30, check=True)
+ return None
+ except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as e:
+ return e
+
def _run_command(self, name: str, workdir: str) -> list[str]:
"""``docker run -d`` argv for a fresh ``sleep infinity`` container (idle reaper handles
lifetime). s6-overlay images already provide PID 1, so ``--init`` is skipped for them."""
@@ -752,10 +741,7 @@ class DockerEnvironment(BaseEnvironment):
logger.info("Started container %s (%s)", container_name, container_id[:12])
return container_id
- # ------------------------------------------------------------------
- # Env forwarding
- # ------------------------------------------------------------------
-
+ # --- Env forwarding ---
def _docker_client_env(self, values: dict[str, str]) -> dict[str, str] | None:
"""Env for the docker-client subprocess carrying forwarded values (pairs with name-only
``-e KEY`` flags to keep secrets out of cmdline); ``None`` = inherit when empty."""
@@ -783,9 +769,8 @@ class DockerEnvironment(BaseEnvironment):
is_global_env = lambda _name: False # noqa: E731
try:
from tools.env_passthrough import get_all_passthrough, resolve_passthrough_value
- from agent.secret_scope import _is_global_env, is_multiplex_active as _is_multiplex_active
- is_global_env = _is_global_env
- multiplex_active = _is_multiplex_active()
+ from agent.secret_scope import _is_global_env as is_global_env, is_multiplex_active
+ multiplex_active = is_multiplex_active()
passthrough_keys = set(get_all_passthrough())
except Exception:
pass
@@ -832,19 +817,12 @@ class DockerEnvironment(BaseEnvironment):
if unset_names:
quoted_names = " ".join(shlex.quote(name) for name in unset_names)
cmd_string = f"unset {quoted_names} 2>/dev/null || true\n{cmd_string}"
-
- cmd.append(self._container_id)
- cmd.extend(bash_argv(cmd_string, login))
+ cmd += [self._container_id, *bash_argv(cmd_string, login)]
client_env = self._docker_client_env(env_values)
- if client_env is not None:
- return _popen_bash(cmd, stdin_data, env=client_env)
- return _popen_bash(cmd, stdin_data)
-
- # ------------------------------------------------------------------
- # "No such container" recovery
- # ------------------------------------------------------------------
+ return _popen_bash(cmd, stdin_data, env=client_env) if client_env is not None else _popen_bash(cmd, stdin_data)
+ # --- "No such container" recovery ---
_NO_CONTAINER_PATTERNS = ("No such container", "is not running", "no such container")
def _is_container_gone(self, output: str) -> bool:
@@ -866,13 +844,11 @@ class DockerEnvironment(BaseEnvironment):
if state == "running":
self._container_id = cid
logger.info("Recovery: reusing running container %s", cid[:12])
+ elif (err := self._start_container(cid)) is None:
+ self._container_id = cid
+ logger.info("Recovery: restarted container %s", cid[:12])
else:
- try:
- run_capture([self._docker_exe, "start", cid], timeout=30, check=True)
- self._container_id = cid
- logger.info("Recovery: restarted container %s", cid[:12])
- except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as e:
- logger.warning("Recovery: failed to start container %s: %s", cid[:12], e)
+ logger.warning("Recovery: failed to start container %s: %s", cid[:12], err)
if not self._container_id:
if not self._image:
@@ -937,17 +913,10 @@ class DockerEnvironment(BaseEnvironment):
def _container_network_mode(self, container_id: str) -> Optional[str]:
"""``HostConfig.NetworkMode`` of a container, or ``None`` when inspection fails (callers
treat ``None`` as a mismatch under lockdown, so a failed inspect fails closed)."""
- try:
- result = run_capture(
- [self._docker_exe, "inspect", "--format", "{{.HostConfig.NetworkMode}}", container_id],
- timeout=10)
- except (subprocess.TimeoutExpired, OSError) as e:
- logger.debug("docker inspect NetworkMode failed: %s", e)
- return None
- if result.returncode != 0:
- logger.debug("docker inspect NetworkMode returned %d: %s", result.returncode, result.stderr.strip())
- return None
- return result.stdout.strip() or None
+ result = _docker_query(
+ [self._docker_exe, "inspect", "--format", "{{.HostConfig.NetworkMode}}", container_id], timeout=10,
+ fail="docker inspect NetworkMode failed: %s", nonzero="docker inspect NetworkMode returned %d: %s")
+ return (result.stdout.strip() or None) if result is not None else None
def _find_reusable_container(
self, task_label: str, profile_label: str, egress_label: str) -> Optional[tuple[str, str]]:
@@ -966,15 +935,11 @@ class DockerEnvironment(BaseEnvironment):
else:
filters.extend(["--filter", f"label={_EGRESS_LABEL_KEY}={egress_label}"])
fmt = "{{.ID}}\t{{.State}}"
- try:
- result = run_capture([self._docker_exe, "ps", "-a", *filters, "--format", fmt], timeout=10)
- except (subprocess.TimeoutExpired, OSError) as e:
- logger.debug("docker ps probe failed: %s — will start a fresh container", e)
- return None
- if result.returncode != 0:
- logger.debug(
- "docker ps probe returned %d: %s — will start a fresh container",
- result.returncode, result.stderr.strip())
+ result = _docker_query(
+ [self._docker_exe, "ps", "-a", *filters, "--format", fmt], timeout=10,
+ fail="docker ps probe failed: %s — will start a fresh container",
+ nonzero="docker ps probe returned %d: %s — will start a fresh container")
+ if result is None:
return None
# Multiple matches can happen after a crash mid-cleanup: prefer a running
# one, else the first listed; stale duplicates are the orphan reaper's job.
diff --git a/tools/environments/docker_egress.py b/tools/environments/docker_egress.py
index 1f555f96aa1..4778e6ec193 100644
--- a/tools/environments/docker_egress.py
+++ b/tools/environments/docker_egress.py
@@ -146,27 +146,20 @@ def _critical_egress_env_names(env_overrides: dict[str, str]) -> set[str]:
def _extra_args_egress_collisions(extra_args: list[str], critical_names: set[str]) -> list[str]:
"""Return docker_extra_args entries that can override egress controls."""
collisions: list[str] = []
- env_flags = {"-e", "--env", "--env-file"}
- network_flags = {"--network", "--net"}
i = 0
while i < len(extra_args):
arg = extra_args[i]
- nxt = extra_args[i + 1] if i + 1 < len(extra_args) else ""
- if arg in env_flags:
- if arg == "--env-file":
- collisions.append(arg)
- elif nxt.split("=", 1)[0] in critical_names:
- collisions.append(nxt.split("=", 1)[0])
- i += 2
+ flag, sep, inline_value = arg.partition("=") # ``-e NAME=v`` vs ``-e=NAME=v`` / ``--env-file=f``
+ if flag in ("-e", "--env", "--env-file"):
+ value = inline_value if sep else (extra_args[i + 1] if i + 1 < len(extra_args) else "")
+ name = value.split("=", 1)[0]
+ if flag == "--env-file":
+ collisions.append(flag)
+ elif name in critical_names:
+ collisions.append(name)
+ i += 1 if sep else 2
continue
- if any(arg.startswith(f"{flag}=") for flag in env_flags):
- if arg.startswith("--env-file="):
- collisions.append("--env-file")
- else:
- name = arg.split("=", 1)[1].split("=", 1)[0]
- if name in critical_names:
- collisions.append(name)
- elif arg in network_flags or any(arg.startswith(f"{flag}=") for flag in network_flags):
+ if flag in ("--network", "--net"):
collisions.append(arg)
i += 1
return sorted(set(collisions))
diff --git a/tools/environments/file_sync.py b/tools/environments/file_sync.py
index cee07113e23..c9cb048e582 100644
--- a/tools/environments/file_sync.py
+++ b/tools/environments/file_sync.py
@@ -211,10 +211,7 @@ class FileSyncManager:
self._delete_fn(to_delete)
logger.debug("file_sync: deleted %s", to_delete)
- # ------------------------------------------------------------------
- # Sync-back: pull remote changes to host on teardown
- # ------------------------------------------------------------------
-
+ # --- Sync-back: pull remote changes to host on teardown ---
def sync_back(self, hermes_home: Path | None = None) -> None:
"""Pull remote changes back to the host: download the remote ``.hermes/`` as a tar and
apply only files whose SHA-256 differs from what was pushed. SIGINT is deferred until
@@ -360,8 +357,7 @@ class FileSyncManager:
if pushed_hash is not None and os.path.exists(host_path) and _sha256_file(host_path) != pushed_hash:
logger.warning(
"sync_back: conflict on %s — host modified "
- "since push, remote also changed. Applying "
- "remote version (last-write-wins).",
+ "since push, remote also changed. Applying remote version (last-write-wins).",
remote_path)
os.makedirs(os.path.dirname(host_path), exist_ok=True)