Skip to content

The agent that grows with you

fix(docker): identify idle-stopped workspace containers by a dedicated label

Tekniumf2e178388b9c
diff --git a/tests/tools/test_docker_environment.py b/tests/tools/test_docker_environment.py
index 8f99c597a74..1cc01ca8ac4 100644
--- a/tests/tools/test_docker_environment.py
+++ b/tests/tools/test_docker_environment.py
@@ -1897,3 +1897,36 @@ def test_docker_env_warnings_never_echo_values(caplog):
with caplog.at_level(logging.WARNING, logger="tools.environments.docker"):
docker_env._normalize_env_dict({"TOKEN": ["sk-live-value"], "OK": "1"})
assert "TOKEN" in caplog.text and "sk-live-value" not in caplog.text
+
+
[email protected]("base", ["default", "profile:research-assistant-for-the-quarterly-ops"])
+def test_workspace_container_labels_identify_it_to_the_orphan_reaper(monkeypatch, base):
+ """The orphan reaper must recognise an idle-STOPPED workspace container from the labels the
+ create path actually stamps. ``hermes-task-id`` is truncated to 63 chars, so a profile name of
+ 36+ chars cut the ``_ws_<digest>`` suffix and the next sweep ``docker rm``-ed the container,
+ losing everything installed outside the mounts."""
+ monkeypatch.setattr(docker_env, "find_docker", lambda: "/usr/bin/docker")
+ calls = _mock_subprocess_run(monkeypatch)
+ _make_dummy_env(task_id=f"{base}:ws:0123456789abcdef")
+ labels = dict(lbl.split("=", 1) for lbl in _labels_in_run_args(_run_args_from_calls(calls)))
+
+ def _inspect(cmd, **kwargs):
+ key = re.search(r'Labels "([^"]+)"', cmd[cmd.index("--format") + 1]).group(1)
+ return subprocess.CompletedProcess(cmd, 0, stdout=labels.get(key, "<no value>") + "\n", stderr="")
+
+ monkeypatch.setattr(docker_env.subprocess, "run", _inspect)
+ assert docker_env._is_idle_workspace_container("/usr/bin/docker", "cid"), labels
+
+
+def test_non_workspace_container_is_not_an_idle_workspace(monkeypatch):
+ monkeypatch.setattr(docker_env, "find_docker", lambda: "/usr/bin/docker")
+ calls = _mock_subprocess_run(monkeypatch)
+ _make_dummy_env(task_id="profile:research-assistant-for-the-quarterly-ops")
+ labels = dict(lbl.split("=", 1) for lbl in _labels_in_run_args(_run_args_from_calls(calls)))
+
+ def _inspect(cmd, **kwargs):
+ key = re.search(r'Labels "([^"]+)"', cmd[cmd.index("--format") + 1]).group(1)
+ return subprocess.CompletedProcess(cmd, 0, stdout=labels.get(key, "<no value>") + "\n", stderr="")
+
+ monkeypatch.setattr(docker_env.subprocess, "run", _inspect)
+ assert not docker_env._is_idle_workspace_container("/usr/bin/docker", "cid"), labels
diff --git a/tools/environments/docker.py b/tools/environments/docker.py
index 2be3f39c6c3..e8d9b16dd81 100644
--- a/tools/environments/docker.py
+++ b/tools/environments/docker.py
@@ -48,6 +48,11 @@ _DOCKER_SEARCH_PATHS = [
_docker_executable: Optional[str] = None # resolved once, cached
_ENV_VAR_NAME_RE = _SHELL_ENV_NAME_RE
_ENVIRONMENT_LABEL_KEY = "hermes-environment"
+# Stamped at create time on per-workspace persistent containers (task key ``<base>:ws:<digest>``)
+# with the digest as its value. The orphan reaper reads it to spare a container the idle reaper
+# stopped on purpose; ``hermes-task-id`` cannot serve, it is truncated to 63 chars.
+_WORKSPACE_LABEL_KEY = "hermes-workspace"
+_WORKSPACE_TASK_RE = re.compile(r":ws:([0-9a-f]{16})$")
def _normalize_forward_env_names(forward_env: list[str] | None) -> list[str]:
@@ -222,15 +227,17 @@ def reap_orphan_containers(
def _is_idle_workspace_container(docker_exe: str, container_id: str) -> bool:
- """A persistent per-workspace container (task key ``<base>:ws:<digest>``, label ``<base>_ws_<digest>``)
- that the idle reaper STOPPED on purpose (``DockerEnvironment.stop_idle``): it is that workspace's
- sandbox, restarted on next use, not an orphan. Unknown (inspect failed) reads as not one."""
+ """A persistent per-workspace container (task key ``<base>:ws:<digest>``, stamped with the
+ ``hermes-workspace=<digest>`` label at create time) that the idle reaper STOPPED on purpose
+ (``DockerEnvironment.stop_idle``): it is that workspace's sandbox, restarted on next use, not an
+ orphan. Reads the dedicated label, never the 63-char-truncated ``hermes-task-id``, whose
+ ``_ws_<digest>`` suffix a long profile name cuts. Unknown (inspect failed) reads as not one."""
result = _docker_query(
- [docker_exe, "inspect", "--format", '{{index .Config.Labels "hermes-task-id"}}', container_id], timeout=10,
- fail="orphan reaper docker inspect %s failed: %s", fail_args=(container_id[:12],))
+ [docker_exe, "inspect", "--format", f'{{{{index .Config.Labels "{_WORKSPACE_LABEL_KEY}"}}}}', 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 False
- return re.search(r"_ws_[0-9a-f]{16}$", result.stdout.strip()) is not None
+ return re.fullmatch(r"[0-9a-f]{16}", result.stdout.strip()) is not None
def _container_finished_at(docker_exe: str, container_id: str):
@@ -747,6 +754,8 @@ class DockerEnvironment(BaseEnvironment):
"hermes-task-id": task_label,
"hermes-profile": profile_name,
_EGRESS_LABEL_KEY: egress_label}
+ if workspace := _WORKSPACE_TASK_RE.search(task_id or ""):
+ self._labels[_WORKSPACE_LABEL_KEY] = workspace.group(1)
# Explicit sharing opts into the first creator's settings. Otherwise,
# changed image/mount/home configuration must start a fresh container.
if not shared_container_key: