Skip to content

The agent that grows with you

fix(gateway): only a lost connection orphans a session the TUI keeps live

unsupportedpastels388575e1f5a0
diff --git a/gateway/session_authority.py b/gateway/session_authority.py
index fad1f38ca8c..ea19e1ede2a 100644
--- a/gateway/session_authority.py
+++ b/gateway/session_authority.py
@@ -372,7 +372,7 @@ class SessionAuthority:
if self.events.get(member.transport_id) is transport:
del live.subscribers[subscription]
- async def detach(self, actor, subscription_id, *, end_reason=None):
+ async def detach(self, actor, subscription_id, *, end_reason=None, orphaned=False):
for session_id, live in self.sessions.items():
if subscription_id in live.subscribers:
if live.subscribers[subscription_id] != actor:
@@ -393,8 +393,9 @@ class SessionAuthority:
# A viewer that attached while the close was off the loop reopens it.
await tracked_write(self, partial(end_idle_acp_session, self, session_id),
then=partial(reopen_if_reattached, self, session_id))
- from gateway.session_viewer_close import arm_detached_orphan_close
- await arm_detached_orphan_close(self, session_id)
+ if orphaned: # a plain session.detach is a live client handing the session back
+ from gateway.session_viewer_close import arm_detached_orphan_close
+ await arm_detached_orphan_close(self, session_id)
return
raise RuntimeStoreError('not_found')
diff --git a/gateway/session_controls.py b/gateway/session_controls.py
index ff14af2dc90..9657cff622d 100644
--- a/gateway/session_controls.py
+++ b/gateway/session_controls.py
@@ -135,6 +135,7 @@ class AuthorityConnection:
bind_native_transport(authority, self.actor, identity)
self.native_owner = identity.get('native_bootstrap') is True
self.subscriptions = {}
+ self.handed_back = set() # sessions this live client detached without closing (the TUI's switcher)
self._identity, self._operator, self._siblings = identity, operator, {}
authority.events[self.actor.transport_id] = transport
@@ -617,6 +618,7 @@ class AuthorityConnection:
await refresh_resumed_policy(self.authority, ref, attached=ref.session_id in self.subscriptions)
snapshot = await self.authority.attach(self.actor, ref)
self.subscriptions[ref.session_id] = snapshot.subscription_id
+ self.handed_back.discard(ref.session_id)
self._spawn_notice(ref)
# Only local routes have a frozen local launch policy to project; with it, main's banner and
# status-bar inventory (tools, skills, MCP, version, branch) for that policy.
@@ -681,6 +683,7 @@ class AuthorityConnection:
await self.authority.detach(self.actor, subscription_id, end_reason=end_reason)
if self.subscriptions.get(session_id) == subscription_id:
del self.subscriptions[session_id]
+ (self.handed_back.discard if end_reason else self.handed_back.add)(session_id)
result['detached'] = True
return result
@@ -886,7 +889,11 @@ class AuthorityConnection:
for session_id, subscription in list(self.subscriptions.items()):
live = self.authority.sessions.get(session_id)
if live is not None and subscription in live.subscribers:
- await self.authority.detach(self.actor, subscription)
+ await self.authority.detach(self.actor, subscription, orphaned=True)
+ # Kept in the background by this client: losing it orphans them too, unless another viewer holds one.
+ from gateway.session_viewer_close import arm_detached_orphan_close
+ for session_id in self.handed_back:
+ await arm_detached_orphan_close(self.authority, session_id)
from gateway.session_local_migration import unbind_native_transport
unbind_native_transport(self.authority, self.actor)
self.subscriptions.clear()
diff --git a/tests/gateway/test_local_session_orphan_end.py b/tests/gateway/test_local_session_orphan_end.py
index e97407fd617..3b46f37fed5 100644
--- a/tests/gateway/test_local_session_orphan_end.py
+++ b/tests/gateway/test_local_session_orphan_end.py
@@ -359,3 +359,25 @@ async def test_a_zero_grace_never_ends_a_detached_session(tmp_path, monkeypatch)
await viewer.close()
assert armed == [] and authority.orphan_closes == {}
assert authority.db.get_session(sid)['ended_at'] is None
+
+
+async def test_a_session_the_tui_keeps_live_is_only_orphaned_when_its_connection_drops(tmp_path, monkeypatch):
+ armed = _capture_timers(monkeypatch)
+ authority, _ = await _owner(tmp_path, monkeypatch)
+ kept, front = _create(authority, 'kept live').session_id, _create(authority, 'in front').session_id
+ tui = _viewer(authority)
+ held = await tui.dispatch({'id': 1, 'method': 'session.resume', 'params': {'session_id': kept}})
+ await tui.dispatch({'id': 2, 'method': 'session.resume', 'params': {'session_id': front}})
+ # `/sessions new` or the live switcher: the previous session is handed back with a plain detach.
+ out = await tui.dispatch({'id': 3, 'method': 'session.detach', 'params': {
+ 'session_id': kept, 'subscription_id': held['result']['subscription_id']}})
+ assert out['result']['detached'] is True
+ assert armed == [] and authority.orphan_closes == {}
+ listed = await tui.dispatch({'id': 4, 'method': 'session.list', 'params': {}})
+ assert {row['id'] for row in listed['result']['sessions']} == {kept, front}
+
+ await tui.close() # the terminal is killed while it keeps `kept` in the background
+ assert sorted(authority.orphan_closes) == sorted([kept, front])
+ assert [delay for delay, _ in armed] == [300.0, 300.0]
+