The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(terminal): reuse the profile container for sessionless workers
Peter Taras149e3f5539b8
diff --git a/tests/tools/test_docker_session_isolation.py b/tests/tools/test_docker_session_isolation.pyindex 4409c2c2ff4..51e565ffc6d 100644--- a/tests/tools/test_docker_session_isolation.py+++ b/tests/tools/test_docker_session_isolation.py@@ -71,6 +71,11 @@ def _disable_isolation(monkeypatch):class TestSessionIsolationKeying:def test_persistent_true_keeps_shared_default(self, monkeypatch):_disable_isolation(monkeypatch)+ # Default profile stays on the shared "default" key. Pin the name so a+ # named-profile test runner does not take the session-less profile path.+ monkeypatch.setattr(+ "hermes_cli.profiles.get_active_profile_name", lambda: "default"+ )assert terminal_tool._resolve_container_task_id("tui:sess-1") == "default"def test_persistent_false_keys_by_session(self, monkeypatch):diff --git a/tests/tools/test_shared_container_task_id.py b/tests/tools/test_shared_container_task_id.pyindex 98c5c59a0e3..7c040211989 100644--- a/tests/tools/test_shared_container_task_id.py+++ b/tests/tools/test_shared_container_task_id.py@@ -300,3 +300,77 @@ def test_shared_key_ignored_outside_persistent_docker(monkeypatch):assert terminal_tool._resolve_container_task_id(None) == "session:sess-A"finally:clear_session_vars(tokens)+++def _pin_profile(monkeypatch, name: str) -> None:+ monkeypatch.setattr(+ "hermes_cli.profiles.get_active_profile_name", lambda: name+ )+++def test_sessionless_default_profile_stays_default(monkeypatch):+ """Default profile, no session: CLI and workers still share "default"."""+ from gateway.session_context import reset_session_vars++ _persistent_docker(monkeypatch)+ monkeypatch.delenv("HERMES_SESSION_KEY", raising=False)+ monkeypatch.delenv("HERMES_SESSION_PROFILE", raising=False)+ reset_session_vars()+ _pin_profile(monkeypatch, "default")+ assert terminal_tool._resolve_container_task_id(None) == "default"+ assert terminal_tool._resolve_container_task_id("kanban-task") == "default"+++def test_sessionless_named_profile_reuses_profile_container(monkeypatch):+ """Named-profile process, no session: reuse profile:<name>, not "default"."""+ from gateway.session_context import reset_session_vars++ _persistent_docker(monkeypatch)+ monkeypatch.delenv("HERMES_SESSION_KEY", raising=False)+ monkeypatch.delenv("HERMES_SESSION_PROFILE", raising=False)+ monkeypatch.delenv("TERMINAL_DOCKER_SHARED_CONTAINER_KEY", raising=False)+ reset_session_vars()+ _pin_profile(monkeypatch, "tenant-a")+ assert terminal_tool._resolve_container_task_id(None) == "profile:tenant-a"+ assert terminal_tool._resolve_container_task_id("t_b865ceb0") == "profile:tenant-a"+++def test_explicit_session_profile_wins_over_process_home(monkeypatch):+ """A bound session profile is authoritative, even if the process home differs."""+ from gateway.session_context import clear_session_vars, set_session_vars++ _persistent_docker(monkeypatch)+ monkeypatch.delenv("HERMES_SESSION_KEY", raising=False)+ _pin_profile(monkeypatch, "tenant-a")+ tokens = set_session_vars(session_key="agent:main:desktop:1", profile="work")+ try:+ assert terminal_tool._resolve_container_task_id(None) == "profile:work"+ finally:+ clear_session_vars(tokens)+ tokens = set_session_vars(session_key="agent:main:desktop:2", profile="tenant-a")+ try:+ assert terminal_tool._resolve_container_task_id("subagent-1") == "profile:tenant-a"+ finally:+ clear_session_vars(tokens)+++def test_sessionless_named_profile_ignored_when_not_persistent_docker(monkeypatch):+ """The profile key is a persistent-Docker rule. Local stays "default"."""+ from gateway.session_context import reset_session_vars++ monkeypatch.setenv("TERMINAL_ENV", "local")+ monkeypatch.delenv("HERMES_SESSION_KEY", raising=False)+ reset_session_vars()+ _pin_profile(monkeypatch, "tenant-a")+ assert terminal_tool._resolve_container_task_id(None) == "default"+++def test_sessionless_custom_home_stays_default(monkeypatch):+ """A home that is not a named profile must not invent profile:custom."""+ from gateway.session_context import reset_session_vars++ _persistent_docker(monkeypatch)+ monkeypatch.delenv("HERMES_SESSION_KEY", raising=False)+ reset_session_vars()+ _pin_profile(monkeypatch, "custom")+ assert terminal_tool._resolve_container_task_id(None) == "default"diff --git a/tools/terminal_tool.py b/tools/terminal_tool.pyindex 86aea6f3625..81ebce8e870 100644--- a/tools/terminal_tool.py+++ b/tools/terminal_tool.py@@ -481,6 +481,32 @@ def _routed_home_task_key(profile_scoped: bool) -> Optional[str]:return f"home:{override}"+def _sessionless_container_key(profile_scoped: bool) -> str:+ """Container key when no session is bound (CLI, cron, kanban dispatcher workers).++ A routed home override keeps its existing key. Persistent Docker whose+ process home is a named profile reuses ``profile:<name>`` — the same key+ session-bound work of that profile uses. Dispatcher workers set+ ``HERMES_HOME`` and clear session vars, so they are not a routed override;+ without this they collapsed onto ``default`` and missed the profile sandbox.+ The default profile, non-persistent backends, and a home that is not a+ named profile stay ``default``.+ """+ routed = _routed_home_task_key(profile_scoped)+ if routed:+ return routed+ if not profile_scoped:+ return "default"+ try:+ from hermes_cli.profiles import get_active_profile_name+ profile = (get_active_profile_name() or "").strip()+ except Exception:+ return "default"+ if not profile or profile in {"default", "custom"}:+ return "default"+ return f"profile:{profile}"++def _qualify_task_key(key: str) -> str:"""Prefix a session-derived key with the routed profile/home, or return it unchanged.@@ -511,10 +537,11 @@ 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'sSSHEnvironment on the wrong host.- 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.+ 4. No session key (CLI, cron, kanban workers): ``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; persistent Docker on a+ named-profile process reuses ``profile:<name>`` (same sandbox as that+ profile's session-bound work); else ``"default"``."""if task_id and _has_isolation_overrides(task_id):return _qualify_task_key(task_id)@@ -525,8 +552,9 @@ def _resolve_container_task_id(task_id: Optional[str]) -> str:# the gateway per-message via contextvars), scope the container to it so switching profiles can't reuse# a previous profile's SSHEnvironment and silently run commands on the wrong remote host. Subagents# inherit the same session key, so they still collapse onto the parent's container (the #16177- # shared-container intent). CLI mode has no session key and falls through to "default", behaviour- # unchanged. See commit e00f940a9. This runs *after* the isolation-override and+ # shared-container intent). CLI mode has no session key. A named-profile process+ # under persistent Docker reuses ``profile:<name>`` (:func:`_sessionless_container_key`);+ # the default profile still falls through to "default". See commit e00f940a9. This runs *after* the isolation-override and# docker/container_persistent branches above: those paths already key containers per task_id, so they# stay authoritative where they apply and this only covers the cases that would otherwise collapse to# the shared "default" key (notably SSH).@@ -537,7 +565,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(scope.docker_profile_scoped) or "default"+ return _sessionless_container_key(scope.docker_profile_scoped)if not scope.docker_profile_scoped:return _qualify_task_key(f"session:{session_key}")profile = _container_profile()