Skip to content

The agent that grows with you

fix(terminal): persistent Docker keys a routed profile's cron work to its profile container

teknium1bd14eb1c93ee
diff --git a/tests/tools/test_terminal_scope_multiplex.py b/tests/tools/test_terminal_scope_multiplex.py
index 58ba7c7fcb0..271f3107017 100644
--- a/tests/tools/test_terminal_scope_multiplex.py
+++ b/tests/tools/test_terminal_scope_multiplex.py
@@ -145,6 +145,30 @@ def test_profile_omitting_keys_gets_defaults_not_launch_values(tmp_path):
assert json.loads(os.environ["TERMINAL_DOCKER_VOLUMES"]) # A unchanged
+def test_persistent_docker_routed_profile_keeps_one_container(tmp_path):
+ """Persistent Docker is profile-scoped: a routed profile's session-less (cron) work must key the
+ SAME container as its session-bound work, and never another profile's."""
+ import gateway.run as gw
+ import tools.terminal_tool as tt
+ from gateway.session_context import clear_session_vars, reset_session_vars, set_session_vars
+
+ docker = "terminal:\n backend: docker\n container_persistent: true\n"
+ keys = {}
+ for name in ("bee", "wasp"):
+ home = _profile(tmp_path, name, docker)
+ with gw._profile_runtime_scope(home):
+ cron_key = tt._resolve_container_task_id(None)
+ tokens = set_session_vars(session_key=f"agent:{name}:chat", profile=name)
+ try:
+ session_key = tt._resolve_container_task_id(None)
+ finally:
+ clear_session_vars(tokens)
+ reset_session_vars()
+ assert cron_key == session_key == f"profile:{name}"
+ keys[name] = cron_key
+ assert keys["bee"] != keys["wasp"]
+
+
def test_malformed_profile_config_refuses_execution(tmp_path):
"""Unresolvable policy → refusal scope; terminal_tool refuses instead of
running under the launch process's ambient policy (fail closed)."""
diff --git a/tools/terminal_tool.py b/tools/terminal_tool.py
index 998b7fcb22a..efb397fb9e8 100644
--- a/tools/terminal_tool.py
+++ b/tools/terminal_tool.py
@@ -430,19 +430,24 @@ def _docker_session_isolation_enabled() -> bool:
return _session_scope().docker_session_isolated
-def _routed_home_task_key() -> Optional[str]:
+def _routed_home_task_key(profile_scoped: bool) -> Optional[str]:
"""Key for a session-less task serving a routed (non-launch) profile home, else None.
A multiplexed host runs every profile's cron jobs without a session key; collapsing them all onto
``"default"`` made profile B's cron tool calls reuse the environment the launch profile's job
created (its ``.env`` residue, its bridged ``TERMINAL_*``, its shell), so B ran with A's settings.
+ Persistent Docker keys the profile name exactly like B's session-bound work, so B keeps ONE
+ container instead of a second one per home path.
"""
- from hermes_constants import get_hermes_home_override
+ from hermes_constants import get_hermes_home_override, profile_name_for_home
from tools.environments.local import _is_routed_home
override = get_hermes_home_override()
if not override or not _is_routed_home(override):
return None
+ profile = profile_name_for_home(override) if profile_scoped else None
+ if profile:
+ return "default" if profile == "default" else f"profile:{profile}"
try:
return f"home:{os.path.realpath(override)}"
except OSError:
@@ -464,9 +469,10 @@ def _resolve_container_task_id(task_id: Optional[str]) -> str:
default-profile gateway sessions share ONE container; other backends key
``session:<key>`` so switching profiles can't reuse another profile's
SSHEnvironment on the wrong host.
- 4. No session key (CLI): ``shared:<key>`` when opted in (else a CLI run of a
- keyed profile would split from its gateway sessions), else ``"default"``,
- which subagent ids collapse onto to share the parent's container.
+ 4. No session key (CLI, cron): ``shared:<key>`` when opted in (else a CLI run of a
+ keyed profile would split from its gateway sessions); a routed multiplexed profile
+ keys its own home (``profile:<name>`` under persistent Docker, matching branch 3);
+ else ``"default"``, which subagent ids collapse onto to share the parent's container.
"""
if task_id and _has_isolation_overrides(task_id):
return task_id
@@ -489,7 +495,7 @@ def _resolve_container_task_id(task_id: Optional[str]) -> str:
# ONE container/cache slot (and sandbox dir) regardless of profile name (#84671).
return f"shared:{shared}"
if not session_key:
- return _routed_home_task_key() or "default"
+ return _routed_home_task_key(scope.docker_profile_scoped) or "default"
if not scope.docker_profile_scoped:
return f"session:{session_key}"
profile = _current_session_profile() or "default"