Skip to content

The agent that grows with you

fix(gateway): answer voice and fast reads on a history mount; promote the mount in place on API bind

unsupportedpastels22b297098f93
diff --git a/gateway/session_api.py b/gateway/session_api.py
index 46b1d99c03f..d69898f6005 100644
--- a/gateway/session_api.py
+++ b/gateway/session_api.py
@@ -334,7 +334,10 @@ def restore_api_session(authority, session_id):
store._entries[entry.session_key] = current if current is not None and current.session_id == target else entry
from gateway.session_history_mount import is_history_mount
live = authority.sessions.get(session_id)
- if live is None or is_history_mount(live):
- # A read-only view opened while the row had no binding gives way to the bound conversation.
+ if live is None:
authority.sessions[session_id] = LiveSession(source, entry.session_key)
+ elif is_history_mount(live):
+ # A read-only view opened while the row had no binding becomes the bound conversation in place,
+ # so its viewers keep their subscriptions and event stream.
+ live.source, live.route, live.superseded, live.history_mount = source, entry.session_key, False, False
return SessionRef(authority.profile_id, session_id)
diff --git a/gateway/session_config.py b/gateway/session_config.py
index 19ef8dd8dbb..01fcb36a439 100644
--- a/gateway/session_config.py
+++ b/gateway/session_config.py
@@ -116,6 +116,8 @@ async def config_get(connection, ref, params):
def session_voice_mode(connection, ref):
"""``off`` / ``voice_only`` / ``all``: the session's ``/voice`` mode as the messaging handler stored it."""
runner, live = connection.authority.runner, connection.authority.sessions[ref.session_id]
+ if live.source is None: # a history mount: no chat for a voice mode to belong to
+ return 'off'
modes = getattr(runner, '_voice_mode', None) or {}
return modes.get(runner._voice_key_for_source(live.source), 'off')
diff --git a/gateway/session_setting_commands.py b/gateway/session_setting_commands.py
index 6e24df98107..9e7af267a68 100644
--- a/gateway/session_setting_commands.py
+++ b/gateway/session_setting_commands.py
@@ -50,7 +50,7 @@ def local_live_for_route(runner, session_key):
from gateway.session_authorities import all_authorities
for authority in all_authorities(runner):
for live in list(getattr(authority, 'sessions', {}).values()):
- if live.route == session_key and live.source.platform == Platform.LOCAL:
+ if live.route == session_key and getattr(live.source, 'platform', None) == Platform.LOCAL:
return live
return None
diff --git a/tests/gateway/test_stored_history_mount.py b/tests/gateway/test_stored_history_mount.py
index dea540596e0..c0e3a0c6eb7 100644
--- a/tests/gateway/test_stored_history_mount.py
+++ b/tests/gateway/test_stored_history_mount.py
@@ -147,6 +147,17 @@ async def test_the_reads_a_viewer_sends_for_an_open_chat_answer_on_a_mount(tmp_p
('config.get', {**sid, 'key': 'mtime'}), ('model.options', sid)):
reply = await conn.dispatch({'id': 1, 'method': method, 'params': params})
assert 'result' in reply, (method, reply)
+ from gateway.run import GatewayRunner
+ runner = conn.authority.runner
+ for name in ('_voice_key_for_source', '_voice_key', '_resolve_session_service_tier', '_resolve_session_key_or_none',
+ '_peek_session_state', '_load_service_tier'):
+ setattr(runner, name, getattr(GatewayRunner, name).__get__(runner))
+ runner._adapter_profile_for_source = lambda source: None # no adapters here
+ # Desktop's automatic voice read on open, and the TUI's /fast status read.
+ replies = {key: await conn.dispatch({'id': 1, 'method': 'config.get', 'params': {**sid, 'key': key}})
+ for key in ('voice', 'fast')}
+ assert {key: reply.get('result', {}).get('value') for key, reply in replies.items()} == {
+ 'voice': 'off', 'fast': 'normal'}, replies
for command in ('fast status', 'model'):
reply = await conn.dispatch({'id': 1, 'method': 'slash.exec', 'params': {**sid, 'command': command}})
assert reply['error']['message'] == 'session_read_only', (command, reply)
@@ -154,14 +165,23 @@ async def test_the_reads_a_viewer_sends_for_an_open_chat_answer_on_a_mount(tmp_p
@pytest.mark.asyncio
async def test_an_api_chat_opened_read_only_still_continues_through_the_api(tmp_path, monkeypatch):
- """Binding the viewed row replaced nothing: the read-only mount had no source to check."""
+ """Binding the viewed row replaced nothing: the read-only mount had no source to check. The viewer
+ that opened it keeps receiving the bound conversation's events and detaches normally."""
+ import asyncio
+ import queue
from gateway.config import Platform
from gateway.session_api import bind_api_session
- from gateway.session_history_mount import is_history_mount
conn, ref = await _connect(tmp_path, monkeypatch, 'api-1')
- await conn.resume(ref, {})
+ frames = queue.Queue()
+ conn.authority.events[conn.actor.transport_id] = SimpleNamespace(write=lambda frame: frames.put(frame) or True)
+ opened = await conn.resume(ref, {})
assert bind_api_session(conn.authority, 'api-1', caller_key='agent:main:api_server:dm:api-1') == ref
live = conn.authority.sessions['api-1']
- assert live.source.platform == Platform.API_SERVER and not is_history_mount(live) and not live.superseded
+ assert live.source.platform == Platform.API_SERVER and not live.superseded
+ live.event_stream.publish('api-1', {'text': 'bound'})
+ frame = await asyncio.to_thread(frames.get, True, 5)
+ assert frame['params']['payload'] == {'text': 'bound'}
+ detached = await conn.detach(ref, {'session_id': 'api-1', 'subscription_id': opened['subscription_id']})
+ assert detached['detached'] is True