The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): bind the session workspace for side agents and managed-worker turns
Tekniumc4bff8aaa2fe
diff --git a/gateway/session_kanban.py b/gateway/session_kanban.pyindex d23f09477d4..aeabdf40193 100644--- a/gateway/session_kanban.py+++ b/gateway/session_kanban.py@@ -1,5 +1,6 @@"""Trusted dispatcher claims enter the ordinary owner's durable admission queue."""import asyncio+import contextlibfrom contextlib import closingfrom dataclasses import asdict, replaceimport threading@@ -226,13 +227,36 @@ def run_worker_turns(agent, frame, history):return _run_worker_turns(agent, frame, history)+def _worker_workspace(frame):+ """A managed worker's chat turn in its session's workspace, as the in-process turn runner binds it+ (#140665): the session id as the terminal task id, the route as the session key (persistent Docker+ keys the workspace container from it) and the frozen cwd as the session's mount. Yields the task+ id, or None for a frame with no session scope (nothing is bound then)."""+ session_id = (frame.get('scope') or {}).get('session_id')+ if not session_id:+ yield None+ return+ from gateway.session_context import clear_session_vars, set_session_vars+ from gateway.session_policy import bind_turn_workspace, restore_policy+ policy = restore_policy(frame['policy'])+ tokens = set_session_vars(session_key=frame.get('route') or session_id, session_id=session_id)+ try:+ bind_turn_workspace(policy, session_id)+ yield session_id+ finally:+ clear_session_vars(tokens)++def _run_worker_turns(agent, frame, history):context = json.loads(frame['policy'].get('kanban_json') or 'null')if context is None:from gateway.session_display import worker_display_kwargsauthor = frame.get('turn_author')- return agent.run_conversation(frame['text'], conversation_history=history, **worker_display_kwargs(),- **({'turn_author': author} if author is not None else {}))+ with _worker_workspace(frame) as task_id:+ return agent.run_conversation(frame['text'], conversation_history=history, **worker_display_kwargs(),+ **({'task_id': task_id} if task_id else {}),+ **({'turn_author': author} if author is not None else {}))from hermes_cli.turn_exit import credential_failure_flags, turn_exit_codecode, last_output = 1, ''try:diff --git a/gateway/session_tui_side.py b/gateway/session_tui_side.pyindex a20ebd65278..4756b1f9434 100644--- a/gateway/session_tui_side.py+++ b/gateway/session_tui_side.py@@ -59,12 +59,16 @@ def _side_scope(authority, policy, task_id):identity (approval key, HERMES_SESSION_ID) for the side agent's thread."""from gateway.session_authorities import owner_scopefrom gateway.session_context import clear_session_vars, set_session_vars- from gateway.session_policy import policy_scope+ from gateway.session_policy import bind_turn_workspace, policy_scope, unbind_turn_workspacewith 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)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).+ bind_turn_workspace(policy, task_id)yieldfinally:+ unbind_turn_workspace(task_id)clear_session_vars(tokens)diff --git a/tests/gateway/test_docker_session_workspace.py b/tests/gateway/test_docker_session_workspace.pyindex af82a8fc6e6..a25438e61eb 100644--- a/tests/gateway/test_docker_session_workspace.py+++ b/tests/gateway/test_docker_session_workspace.py@@ -214,3 +214,47 @@ def test_idle_reaper_stops_workspace_containers_and_keeps_the_profile_one_runninassert sorted(calls) == [('cleanup', 'default'), ('stop', 'default:ws:aaaa'),('stop', 'profile:beta:ws:bbbb')]assert terminal_tool._active_environments == {}++[email protected]('persistent', [False, True])+def test_side_agent_runs_in_its_sessions_workspace(home, tmp_path, persistent):+ # Review (@kshitijk4poor S2, @dokterdok 4): /bg, /btw and preview restart never bound the workspace.+ from types import SimpleNamespace+ from gateway.session_tui_side import _side_scope+ from tools import terminal_tool+ ws1 = tmp_path / 'ws1'+ ws1.mkdir()+ policy = _policy(home, ws1, persistent=persistent)+ with _turn(policy, 'local-a'):+ session_key = _plan('local-a')[0]+ authority = SimpleNamespace(runner=SimpleNamespace(), profile_id='fixture')+ with _side_scope(authority, policy, 'bg_123456'):+ key, mount = _plan('bg_123456')+ assert mount == str(ws1)+ if persistent:+ assert key == session_key # the session's own workspace container, not the profile's+ assert terminal_tool.resolve_task_overrides('bg_123456') == {} # released with the side task+++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+ from gateway.session_policy import policy_scope+ ws1 = tmp_path / 'ws1'+ ws1.mkdir()+ policy = _policy(home, ws1, persistent=True)+ seen = {}++ class Agent:+ def run_conversation(self, text, **kwargs):+ seen['task_id'] = kwargs.get('task_id')+ seen['plan'] = _plan(kwargs.get('task_id'))+ return {}++ frame = {'policy': asdict(policy), 'text': 'hi', 'route': 'agent:main:local:dm:worker-s1',+ 'scope': {'session_id': 'worker-s1'}}+ with policy_scope(policy): # agent/managed_worker.py: policy_scope(policy), no session key bound+ session_kanban.run_worker_turns(Agent(), frame, [])+ assert seen['task_id'] == 'worker-s1'+ assert ':ws:' in seen['plan'][0]+ assert seen['plan'][1] == str(ws1)