Skip to content

The agent that grows with you

fix(docker): digest-suffix shared-container identity labels so distinct keys never collide

Tekniumd736f5d53f1d
diff --git a/tests/tools/test_docker_environment.py b/tests/tools/test_docker_environment.py
index b9adf563878..8666543227e 100644
--- a/tests/tools/test_docker_environment.py
+++ b/tests/tools/test_docker_environment.py
@@ -722,9 +722,38 @@ def test_shared_container_key_replaces_profile_identity(monkeypatch):
monkeypatch.setattr(docker_env, "_get_active_profile_name", lambda: "research")
_mock_subprocess_run(monkeypatch)
- env = _make_dummy_env(task_id="abc", shared_container_key="team/workspace")
+ a = _make_dummy_env(task_id="abc", shared_container_key="team/workspace")
+ b = _make_dummy_env(task_id="abc", shared_container_key="team/workspace")
- assert env._labels["hermes-profile"] == "team_workspace"
+ # Deterministic across processes/profiles, not the profile label, and
+ # digest-suffixed (label sanitization alone is lossy).
+ assert a._labels["hermes-profile"] == b._labels["hermes-profile"]
+ assert a._labels["hermes-profile"] != "research"
+ assert a._labels["hermes-profile"].startswith("team_workspace-")
+
+
+def test_distinct_shared_keys_never_collide(monkeypatch):
+ """Label sanitization is lossy — different raw keys MUST NOT resolve to
+ one container identity, or two 'isolated' teams silently attach to the
+ same running container (filesystem, processes, env)."""
+ monkeypatch.setattr(docker_env, "find_docker", lambda: "/usr/bin/docker")
+ monkeypatch.setattr(docker_env, "_get_active_profile_name", lambda: "research")
+ _mock_subprocess_run(monkeypatch)
+
+ # Sanitize-collision pair: both stems clean to "team_workspace".
+ a = _make_dummy_env(task_id="abc", shared_container_key="team/workspace")
+ b = _make_dummy_env(task_id="abc", shared_container_key="team_workspace")
+ assert a._labels["hermes-profile"] != b._labels["hermes-profile"]
+
+ # Truncation pair: identical first 63 chars, differ after.
+ long_a = "x" * 70 + "A"
+ long_b = "x" * 70 + "B"
+ c = _make_dummy_env(task_id="abc", shared_container_key=long_a)
+ d = _make_dummy_env(task_id="abc", shared_container_key=long_b)
+ assert c._labels["hermes-profile"] != d._labels["hermes-profile"]
+ # Both stay within Docker's 63-char label-value bound.
+ assert len(c._labels["hermes-profile"]) <= 63
+ assert len(d._labels["hermes-profile"]) <= 63
def test_empty_shared_container_key_preserves_profile_isolation(monkeypatch):
diff --git a/tools/environments/docker.py b/tools/environments/docker.py
index 0c8edca0adb..fe8f565568e 100644
--- a/tools/environments/docker.py
+++ b/tools/environments/docker.py
@@ -158,8 +158,22 @@ def _container_identity(shared_key: str = "") -> str:
Profiles remain isolated by default. An explicit shared key lets trusted
profiles that intentionally share a workspace use one Docker identity.
+
+ Shared keys are made collision-resistant the same way
+ :func:`sanitize_task_id_for_path` is: label sanitization is lossy
+ (``team/workspace`` and ``team_workspace`` both sanitize to
+ ``team_workspace``, and >63-char keys truncate), and since container
+ reuse is label-keyed, two DIFFERENT keys colliding after sanitization
+ would silently attach to the same running container. A digest of the
+ raw key disambiguates; identical raw keys still map to identical
+ labels across processes. Plain profile names keep their historical
+ un-suffixed labels for backward compatibility with existing containers.
"""
- return _sanitize_label_value(shared_key or _get_active_profile_name())
+ if not shared_key:
+ return _sanitize_label_value(_get_active_profile_name())
+ digest = hashlib.sha256(shared_key.encode("utf-8")).hexdigest()[:12]
+ stem = _sanitize_label_value(shared_key)[:50]
+ return f"{stem}-{digest}"
def reap_orphan_containers(
diff --git a/website/docs/user-guide/configuration.md b/website/docs/user-guide/configuration.md
index 358fc46281c..4bd432551b1 100644
--- a/website/docs/user-guide/configuration.md
+++ b/website/docs/user-guide/configuration.md
@@ -304,7 +304,7 @@ Edge cases worth knowing:
- **OOM kill of in-container PID 1** transitions the container to `Exited`. Next reuse will `docker start` it; filesystem state survives, bg processes do not.
- **Switching profiles** isolates containers from each other — a container labeled `hermes-profile=work` is invisible to a Hermes process running under `hermes-profile=research`. The orphan reaper is profile-scoped too, so cross-profile containers don't get reaped accidentally, but they also won't get cleaned up automatically until you start Hermes again under their original profile.
-- **Explicit cross-profile sharing** — set the same non-empty `docker_shared_container_key` under `terminal:` for profiles that intentionally collaborate in one trusted workspace. This replaces only their container identity label; task, egress, and network compatibility checks still apply. Profiles without the key remain isolated.
+- **Explicit cross-profile sharing** — set the same non-empty `docker_shared_container_key` under `terminal:` for profiles that intentionally collaborate in one trusted workspace. This replaces only their container identity label; task, egress, and network compatibility checks still apply. Profiles without the key remain isolated. The identity label is derived from the key with a short digest suffix, so similar-looking keys (`team/workspace` vs `team_workspace`) never collide into one container. **Important: a shared container is created once, by whichever profile starts it first** — that profile's `docker_image`, volumes, shm size, and other immutable Docker settings win, and later profiles attach to it as-is; differing settings in their configs are ignored until the container is removed and recreated. Profiles sharing a key should agree on image and mounts.
Parallel subagents spawned via `delegate_task(tasks=[...])` share this one container — concurrent `cd`, env mutations, and writes to the same path will collide. If a subagent needs an isolated sandbox, it must register a per-task image override via `register_task_env_overrides()`, which RL and benchmark environments (TerminalBench2, HermesSweEnv, etc.) do automatically for their per-task Docker images.