Skip to content

The agent that grows with you

fix: refuse a messaging model pick on a stopped chat instead of rotating it

Teknium58bd671bcc2f
diff --git a/gateway/session_messaging_picks.py b/gateway/session_messaging_picks.py
index 52c4fda274a..2e0e4eca783 100644
--- a/gateway/session_messaging_picks.py
+++ b/gateway/session_messaging_picks.py
@@ -40,6 +40,11 @@ async def _pick_model(authority, owner, live, payload, row):
from gateway.slash_commands_model import _ModelSwitchContext
from hermes_constants import get_hermes_home
runner = authority.runner
+ # A /stop'd chat starts a new session on its next access, and recording the switch is such an
+ # access: a viewer's pick would end the chat's session with no message from anyone in it.
+ entry = runner.session_store.lookup_by_session_key(live.route)
+ if entry is None or entry.suspended:
+ raise RuntimeStoreError('not_found', detail='chat is stopped; its next message starts a new session')
ctx = _ModelSwitchContext(session_key=live.route, source=live.source,
config_path=get_hermes_home() / 'config.yaml', persist_global=False)
ctx.read_config()
diff --git a/tests/gateway/test_messaging_session_picks.py b/tests/gateway/test_messaging_session_picks.py
index be47d34d3a7..5f6cf62f164 100644
--- a/tests/gateway/test_messaging_session_picks.py
+++ b/tests/gateway/test_messaging_session_picks.py
@@ -168,3 +168,21 @@ async def test_a_messaging_chats_effort_wire_is_clamped_for_its_api_mode(tmp_pat
finally:
await viewer.close()
runner.session_store.close_all_db_handles()
+
+
+async def test_a_model_pick_on_a_stopped_chat_is_refused_without_rotating_it(tmp_path):
+ """Recording a switch on a /stop'd chat started its new session: a viewer's pick ended the chat's
+ session with no message in it. The pick is refused and the route keeps its session."""
+ runner, _source, route, viewer, root, fences = await _compressed_chat(tmp_path)
+ try:
+ assert runner.session_store.suspend_session(route)
+ before = runner.session_store.lookup_by_session_key(route).session_id
+
+ picked = await viewer.dispatch(_mutate(root, fences, 'model', {'model': 'picked'}, 'stopped-pick'))
+
+ assert picked.get('error', {}).get('data', {}).get('reason') == 'not_found', picked
+ assert runner.session_store.lookup_by_session_key(route).session_id == before
+ finally:
+ await viewer.close()
+ runner.session_store.close_all_db_handles()