Skip to content

The agent that grows with you

fix(gateway): drop a session's workspace binding on a backend switch and on delete

Tekniumfa39f9f23568
diff --git a/gateway/session_mutations.py b/gateway/session_mutations.py
index 3eaf712558e..1c1845cf294 100644
--- a/gateway/session_mutations.py
+++ b/gateway/session_mutations.py
@@ -360,8 +360,9 @@ def retire_live_sessions(authority, session_ids, *, require_fence=False):
strip(session_ids)
except Exception:
logger.warning('could not strip retired routes from sessions.json', exc_info=True)
- from gateway.session_policy import release_launch_secrets
+ from gateway.session_policy import release_launch_secrets, release_session_workspaces
release_launch_secrets(authority, session_ids)
+ release_session_workspaces(authority, session_ids)
from tools.async_delegation import allow_for_session
for sid in session_ids:
# A closed session's refusal of new delegations (session_viewer_close) goes with its live state.
diff --git a/gateway/session_policy.py b/gateway/session_policy.py
index 1cd1d6ab088..619c44dc067 100644
--- a/gateway/session_policy.py
+++ b/gateway/session_policy.py
@@ -942,6 +942,9 @@ def bind_turn_workspace(policy, task_id):
return
terminal = json.loads(policy.terminal_json)
if str(terminal.get('TERMINAL_ENV') or '').strip().lower() != 'docker' or not terminal_uses_host_cwd(terminal):
+ # The session moved off a host-cwd Docker mount (e.g. to SSH with its own terminal.cwd): the
+ # binding an earlier turn registered would otherwise win in _plan_execution on the new backend.
+ unbind_turn_workspace(task_id)
return
from tools.terminal_tool import register_task_env_overrides, resolve_task_overrides, workspace_key_for
current = resolve_task_overrides(task_id)
@@ -951,6 +954,31 @@ def bind_turn_workspace(policy, task_id):
'workspace_key': workspace_key_for(policy.cwd)})
+def unbind_turn_workspace(task_id):
+ """Drop the workspace override :func:`bind_turn_workspace` registered for *task_id*, and the cwd
+ record it seeded. Only its own entry (tagged ``workspace_key``): an ACP/TUI workspace or an RL
+ image override registered by someone else under the same id stays."""
+ if not task_id:
+ return
+ from tools.terminal_tool import _qualify_task_key, _task_env_overrides, clear_session_cwd, get_session_cwd
+ key = _qualify_task_key(task_id)
+ current = _task_env_overrides.get(key) or {}
+ if 'workspace_key' not in current:
+ return
+ _task_env_overrides.pop(key, None)
+ if get_session_cwd(task_id) == current.get('cwd'):
+ clear_session_cwd(task_id)
+
+
+def release_session_workspaces(authority, session_ids):
+ """Session teardown: drop each session's workspace binding under its owning profile's scope (the
+ key the turn bound it under), so the override table does not grow with every closed session."""
+ from gateway.session_authorities import owner_scope
+ with owner_scope(authority):
+ for sid in session_ids:
+ unbind_turn_workspace(sid)
+
+
@contextmanager
def profile_read_scope(policy, authority):
"""Hold ONE read of a following session's profile for every resolution inside, as a turn's
diff --git a/gateway/session_policy_refresh.py b/gateway/session_policy_refresh.py
index d13f58ca24c..19139fcb1c5 100644
--- a/gateway/session_policy_refresh.py
+++ b/gateway/session_policy_refresh.py
@@ -237,9 +237,12 @@ def _drop_terminal(authority, ref):
from tools.terminal_tool_lifecycle import cleanup_vm
ids = {ref.session_id, local_receipt(authority.db, ref.session_id)['entry']['session_id']}
route = authority.sessions[ref.session_id].route
+ from gateway.session_policy import unbind_turn_workspace
with owner_scope(authority):
keys = {_qualify_task_key(key) for key in (f'session:{route}', *ids)}
for key in (route, *ids):
+ # The turn's workspace binding goes with them: it wins over the new binding's cwd too.
+ unbind_turn_workspace(key)
clear_session_cwd(key)
for key in keys:
cleanup_vm(key)
diff --git a/tests/gateway/test_docker_session_workspace.py b/tests/gateway/test_docker_session_workspace.py
index e1af9fb7b47..5882cec313a 100644
--- a/tests/gateway/test_docker_session_workspace.py
+++ b/tests/gateway/test_docker_session_workspace.py
@@ -147,3 +147,42 @@ def test_workspace_container_key_is_idempotent_and_lazy_setup_creates_the_sessio
for key in [k for k in terminal_tool._active_environments if k not in saved]:
terminal_tool._active_environments.pop(key, None)
terminal_tool._active_environments.update(saved)
+
+
+def test_switching_away_from_mounted_docker_drops_the_session_workspace_override(home, tmp_path):
+ # Review probe (@kshitijk4poor W3, @dokterdok 3, Greptile): Docker -> SSH kept the old host cwd.
+ from gateway.session_policy import bind_turn_workspace, build_policy, policy_scope, restore_policy
+ from tools import terminal_tool
+ ws1 = tmp_path / 'ws1'
+ ws1.mkdir()
+ with _turn(_policy(home, ws1, persistent=True), 'local-a'):
+ assert terminal_tool.resolve_task_overrides('local-a')['cwd'] == str(ws1)
+ (home / 'config.yaml').write_text(json.dumps({'terminal': {'backend': 'ssh', 'cwd': '/srv/project'}}))
+ ssh = restore_policy(asdict(build_policy({'source': 'cli', 'cwd': str(ws1), 'model': 'm'}, {})))
+ with policy_scope(ssh):
+ bind_turn_workspace(ssh, 'local-a')
+ assert terminal_tool.resolve_task_overrides('local-a') == {}
+ assert terminal_tool.get_session_cwd('local-a') != str(ws1)
+
+
+def test_unbind_keeps_an_override_the_session_workspace_binding_did_not_register(home, tmp_path):
+ from gateway.session_policy import unbind_turn_workspace
+ from tools import terminal_tool
+ terminal_tool.register_task_env_overrides('acp-1', {'cwd': str(tmp_path), 'cwd_source': 'session'})
+ unbind_turn_workspace('acp-1')
+ assert terminal_tool.resolve_task_overrides('acp-1')['cwd'] == str(tmp_path)
+
+
+def test_deleting_a_session_releases_its_workspace_binding(home, tmp_path):
+ # Review (@kshitijk4poor W3): the bindings were never cleared on session close.
+ from types import SimpleNamespace
+ from gateway.session_mutations import retire_live_sessions
+ from tools import terminal_tool
+ ws1 = tmp_path / 'ws1'
+ ws1.mkdir()
+ with _turn(_policy(home, ws1, persistent=True), 'local-a'):
+ pass
+ assert terminal_tool.resolve_task_overrides('local-a')['cwd'] == str(ws1)
+ authority = SimpleNamespace(runner=SimpleNamespace(), sessions={}, profile_id='fixture', instance_id='i', epoch=1)
+ retire_live_sessions(authority, ['local-a'])
+ assert terminal_tool.resolve_task_overrides('local-a') == {}