Skip to content

The agent that grows with you

fix(gateway): key a secondary profile's side agents and worker turns by that profile

David Dudok de Wit11898243de27
diff --git a/gateway/session_kanban.py b/gateway/session_kanban.py
index 0eb71e59077..999580b5750 100644
--- a/gateway/session_kanban.py
+++ b/gateway/session_kanban.py
@@ -238,10 +238,19 @@ def _worker_workspace(frame):
if not session_id:
yield None
return
+ from gateway.session_authorities import served_profile_name
from gateway.session_context import clear_session_vars, set_session_vars
from gateway.session_policy import bind_turn_workspace, restore_policy
+ from hermes_state_keys import profile_from_session_key
policy = restore_policy(frame['policy'])
- tokens = set_session_vars(session_key=frame.get('route') or session_id, session_id=session_id)
+ route = frame.get('route') or session_id
+ # The profile its in-process turns bind, so persistent Docker keys the container they and the
+ # session's side agents use (#140665): a served secondary's name, which its routes carry
+ # (``agent:<name>:``); none for the launch home, whatever its name (``hermes -p beta gateway``
+ # keys ``agent:main:``, and only the default profile runs the multiplexer).
+ profile = served_profile_name(frame.get('home'))
+ tokens = set_session_vars(session_key=route, session_id=session_id,
+ profile=profile if profile_from_session_key(route) == profile else '')
try:
bind_turn_workspace(policy, session_id)
yield session_id
diff --git a/gateway/session_tui_side.py b/gateway/session_tui_side.py
index 00892989e52..76d745efbad 100644
--- a/gateway/session_tui_side.py
+++ b/gateway/session_tui_side.py
@@ -56,11 +56,17 @@ def _admit(connection, ref, params, prefix, required='text', optional=()):
def _side_scope(authority, policy, task_id):
"""The owning profile's home/secrets, the session's frozen policy, and the task's own session
identity (approval key, HERMES_SESSION_ID) for the side agent's thread."""
- from gateway.session_authorities import owner_scope
+ from gateway.session_authorities import owner_scope, served_profile_name
from gateway.session_context import clear_session_vars, set_session_vars
from gateway.session_policy import bind_turn_workspace, policy_scope, unbind_turn_workspace
+ from tools.environments.local import _is_routed_home
+ # The owning profile, as its own turns bind it: a served secondary by its name, so persistent
+ # Docker keys ITS workspace container (never the launch profile's live one for the same folder);
+ # the launch home binds none, like its ``agent:main`` turns.
+ home = authority.profile_id
+ profile = served_profile_name(home) if _is_routed_home(home) else ''
with owner_scope(authority, hydrate_secrets=True), policy_scope(policy, authority=authority):
- tokens = set_session_vars(session_key=task_id, session_id=task_id, cwd=policy.cwd)
+ tokens = set_session_vars(session_key=task_id, session_id=task_id, cwd=policy.cwd, profile=profile)
try:
# The session's workspace, as its own turns get it (#140665): mounted at /workspace in a
# per-task container, or the session's own persistent workspace container (same digest).
diff --git a/tests/gateway/test_docker_session_workspace.py b/tests/gateway/test_docker_session_workspace.py
index a25438e61eb..fad26ffefd8 100644
--- a/tests/gateway/test_docker_session_workspace.py
+++ b/tests/gateway/test_docker_session_workspace.py
@@ -236,6 +236,51 @@ def test_side_agent_runs_in_its_sessions_workspace(home, tmp_path, persistent):
assert terminal_tool.resolve_task_overrides('bg_123456') == {} # released with the side task
+def test_secondary_profiles_side_agent_never_gets_the_launch_profiles_workspace_container(home, tmp_path, monkeypatch):
+ # Review (@dokterdok, round 2): beta's /bg in a folder with a live default session got default's env.
+ from types import SimpleNamespace
+ from gateway.session_tui_side import _side_scope
+ from tools import terminal_tool, terminal_tool_lifecycle
+ from tools.terminal_tool import workspace_key_for
+ ws1 = tmp_path / 'ws1'
+ ws1.mkdir()
+ beta = home / 'profiles' / 'beta'
+ beta.mkdir(parents=True)
+ monkeypatch.setattr(terminal_tool, '_active_environments', {})
+ monkeypatch.setattr(terminal_tool, '_last_activity', {})
+ policy = _policy(home, ws1, persistent=True)
+ with _turn(policy, 'local-a'):
+ launch_key = _plan('local-a')[0]
+ terminal_tool._active_environments[launch_key] = launch_env = object()
+ authority = SimpleNamespace(profile_id=str(beta),
+ runner=SimpleNamespace(config=SimpleNamespace(multiplex_profiles=True)))
+ with _side_scope(authority, policy, 'bg_123456'):
+ key = _plan('bg_123456')[0]
+ env = terminal_tool_lifecycle.get_active_env('bg_123456')
+ # Beta's own workspace container, keyed as beta's routed turns key it; never default's live one.
+ assert key == f'profile:beta:ws:{workspace_key_for(str(ws1))}'
+ assert env is not launch_env
+
+
+def test_named_launch_profiles_side_agent_keeps_its_sessions_workspace_container(home, tmp_path, monkeypatch):
+ # The launch home's own turns bind no profile (``agent:main``), whatever its name: its side agent
+ # must key the same container as they do, not ``profile:<name>``.
+ from types import SimpleNamespace
+ from gateway.session_tui_side import _side_scope
+ ws1 = tmp_path / 'ws1'
+ ws1.mkdir()
+ beta = home / 'profiles' / 'beta'
+ beta.mkdir(parents=True)
+ monkeypatch.setenv('HERMES_HOME', str(beta)) # `hermes -p beta gateway`
+ policy = _policy(beta, ws1, persistent=True)
+ with _turn(policy, 'local-a'):
+ session_key = _plan('local-a')[0]
+ authority = SimpleNamespace(profile_id=str(beta),
+ runner=SimpleNamespace(config=SimpleNamespace(multiplex_profiles=True)))
+ with _side_scope(authority, policy, 'bg_123456'):
+ assert _plan('bg_123456')[0] == session_key
+
+
def test_managed_worker_turn_runs_in_its_sessions_workspace(home, tmp_path, monkeypatch):
# Review (@kshitijk4poor S2): out-of-process (managed worker) turns never bound the workspace.
from gateway import session_kanban
@@ -258,3 +303,55 @@ def test_managed_worker_turn_runs_in_its_sessions_workspace(home, tmp_path, monk
assert seen['task_id'] == 'worker-s1'
assert ':ws:' in seen['plan'][0]
assert seen['plan'][1] == str(ws1)
+
+
+def test_secondary_profiles_managed_worker_turn_keys_its_own_workspace_container(home, tmp_path, monkeypatch):
+ from gateway import session_kanban
+ from gateway.session_policy import policy_scope
+ from tools.terminal_tool import workspace_key_for
+ ws1 = tmp_path / 'ws1'
+ ws1.mkdir()
+ beta = home / 'profiles' / 'beta'
+ beta.mkdir(parents=True)
+ monkeypatch.setenv('HERMES_HOME', str(beta)) # the worker process runs under its owning home
+ policy = _policy(beta, ws1, persistent=True)
+ seen = {}
+
+ class Agent:
+ def run_conversation(self, text, **kwargs):
+ seen['key'] = _plan(kwargs['task_id'])[0]
+ return {}
+
+ frame = {'home': str(beta), 'policy': asdict(policy), 'text': 'hi', 'route': 'agent:beta:local:dm:worker-s1',
+ 'scope': {'session_id': 'worker-s1'}}
+ with policy_scope(policy):
+ session_kanban.run_worker_turns(Agent(), frame, [])
+ # The key beta's in-gateway turns and side agents use, not the `default:ws:` one.
+ assert seen['key'] == f'profile:beta:ws:{workspace_key_for(str(ws1))}'
+
+
+def test_named_launch_profiles_managed_worker_turn_keeps_its_sessions_workspace_container(home, tmp_path, monkeypatch):
+ # `hermes -p beta gateway` (a named profile never multiplexes): its routes are `agent:main`, its
+ # in-process turns and side agents bind no profile, and its worker turns must key the same container.
+ from gateway import session_kanban
+ from gateway.session_policy import policy_scope
+ ws1 = tmp_path / 'ws1'
+ ws1.mkdir()
+ beta = home / 'profiles' / 'beta'
+ beta.mkdir(parents=True)
+ monkeypatch.setenv('HERMES_HOME', str(beta))
+ policy = _policy(beta, ws1, persistent=True)
+ with _turn(policy, 'local-a'):
+ session_key = _plan('local-a')[0]
+ seen = {}
+
+ class Agent:
+ def run_conversation(self, text, **kwargs):
+ seen['key'] = _plan(kwargs['task_id'])[0]
+ return {}
+
+ frame = {'home': str(beta), 'policy': asdict(policy), 'text': 'hi', 'route': 'agent:main:local:dm:worker-s1',
+ 'scope': {'session_id': 'worker-s1'}}
+ with policy_scope(policy):
+ session_kanban.run_worker_turns(Agent(), frame, [])
+ assert seen['key'] == session_key