Skip to content

The agent that grows with you

fix(gateway): cold /discard acts on the chat's conversation, never a rotated one

Teknium0cdcf8b489c4
diff --git a/gateway/slash_commands_session.py b/gateway/slash_commands_session.py
index e5d40a4cfcd..189804f8bb9 100644
--- a/gateway/slash_commands_session.py
+++ b/gateway/slash_commands_session.py
@@ -188,11 +188,9 @@ class GatewaySessionCommandsMixin:
return t("gateway.discard.nothing")
if sid not in authority.sessions:
# A conversation /resume brought back after a restart is the chat's route but not yet
- # live: publish it through the same route seam an inbound message uses (review #141357).
- from functools import partial
- from gateway.session_runtime_workers import tracked_write
- sid = authority._publish_route(
- event.source, *await tracked_write(authority, partial(authority._reserve_route, event.source))).session_id
+ # live: publish the peeked conversation on its route (review #141357). Never through
+ # get-or-create: a suspended route would rotate to a new session and strand the lost turn.
+ authority._publish_route(event.source, sid, session_key)
return await discard_lost_turns(authority, sid)
async def _handle_reset_command(self, event: MessageEvent) -> str | EphemeralReply:
diff --git a/tests/gateway/test_messaging_discard_reach.py b/tests/gateway/test_messaging_discard_reach.py
index ffd4cf53672..8cc076c9201 100644
--- a/tests/gateway/test_messaging_discard_reach.py
+++ b/tests/gateway/test_messaging_discard_reach.py
@@ -24,7 +24,10 @@ def test_slack_pause_notice_names_the_registered_umbrella_command():
@pytest.mark.asyncio
-async def test_discard_after_resume_into_a_cold_conversation_resolves_its_lost_turn(tmp_path, monkeypatch):
[email protected]('suspended', [False, True], ids=['cold', 'cold-and-stuck-loop-suspended'])
+async def test_discard_after_resume_into_a_cold_conversation_resolves_its_lost_turn(tmp_path, monkeypatch, suspended):
+ """``suspended``: startup's stuck-loop guard suspends a route active across repeated restarts, which
+ the get-or-create lookup rotates to a NEW session; /discard must act on the peeked one instead."""
from gateway.config import GatewayConfig, Platform, PlatformConfig
from gateway.platforms.base import SendResult
from gateway.platforms.event import MessageEvent
@@ -39,6 +42,7 @@ async def test_discard_after_resume_into_a_cold_conversation_resolves_its_lost_t
monkeypatch.setenv('HERMES_HOME', str(tmp_path))
monkeypatch.setenv('TELEGRAM_ALLOWED_USERS', '42')
monkeypatch.setattr('gateway.run._hermes_home', tmp_path)
+ monkeypatch.setattr('hermes_state.DEFAULT_DB_PATH', tmp_path / 'state.db') # conftest repoints it per test
runner = GatewayRunner(GatewayConfig(multiplex_profiles=False, sessions_dir=tmp_path / 'sessions'))
adapter = _StubAdapter(PlatformConfig(enabled=True, token='fixture', typing_indicator=False), Platform.TELEGRAM)
sent = []
@@ -76,10 +80,13 @@ async def test_discard_after_resume_into_a_cold_conversation_resolves_its_lost_t
await runner._handle_message(MessageEvent(text='/resume ' + sid, source=source, message_id='back'))
assert runner.session_store.peek_session_id(key) == sid and sid not in second.sessions
assert get_session_admission(second.db, admission_id=claimed['admission_id'])['status'] == 'unknown'
+ if suspended:
+ runner.session_store._entries[key].suspended = True # as _suspend_stuck_loop_sessions does
await adapter.handle_message(MessageEvent(text='/discard', source=source, message_id='discard'))
if adapter._background_tasks:
await asyncio.wait_for(asyncio.gather(*list(adapter._background_tasks)), 10)
row = get_session_admission(second.db, admission_id=claimed['admission_id'])
assert (row['status'], row['outcome']) == ('terminal', 'interrupted'), (row, sent)
+ assert runner.session_store.peek_session_id(key) == sid # /discard never rotates the chat
finally:
runner.session_store.close_all_db_handles()