The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): messaging /stop and /new drop the durable queued follow-up
Teknium2132a9e2eb46
diff --git a/gateway/run_inbound.py b/gateway/run_inbound.pyindex 3a77cac420c..2c94ac0d4b4 100644--- a/gateway/run_inbound.py+++ b/gateway/run_inbound.py@@ -573,6 +573,8 @@ class GatewayInboundMixin(GatewayInboundHooksMixin, GatewayPluginInjectionMixin)_denied = self._check_slash_access(source, _cmd_def_inner.name)if _denied is not None:return True, _denied+ if _cmd_def_inner.name in ("stop", "new"):+ await self._hm_cancel_queued_followups(_quick_key)# Any recognized slash command dispatches per its declared busy_policy (dispatch /# interrupt_then_dispatch / reject). Unrecognized commands and plain text fall through.return True, await self._dispatch_busy_slash_command(event, _cmd_def_inner, _quick_key, source)@@ -585,6 +587,27 @@ class GatewayInboundMixin(GatewayInboundHooksMixin, GatewayPluginInjectionMixin)return True, Nonereturn False, None+ async def _hm_cancel_queued_followups(self, _quick_key: str) -> None:+ """Busy /stop and /new drop the human follow-up queued behind the running turn, as main+ dropped the adapter slot; under session authority it sits in the durable FIFO instead.+ Only still-queued ``messaging:*`` rows of this route's logical owner: automation wakes,+ LOCAL/API input and ``unknown`` rows are untouched. Settled silently (no pause notice)."""+ from gateway.session_authorities import active_authority+ authority = active_authority(self)+ sid = authority.logical_owner(self.session_store.peek_session_id(_quick_key)) if authority else None+ if not sid or sid not in authority.sessions:+ return+ from gateway.session_contract import Principal, SessionRef+ from hermes_state_runtime import RuntimeStoreError, list_session_admissions+ ref = SessionRef(authority.profile_id, sid)+ actor = Principal('messaging-control', authority.profile_id,+ frozenset({'session:submit', 'session:control'}), 'busy-command')+ for row in await asyncio.to_thread(list_session_admissions, authority.db, session_id=sid):+ if row['status'] == 'queued' and row['principal_id'].startswith('messaging:'):+ # Claimed meanwhile (stale_generation): it runs as main's post-claim race does.+ with suppress(RuntimeStoreError):+ await authority.cancel_queued(actor, ref, row['admission_id'])+def _hm_busy_telegram_grace_queue(self, event: MessageEvent, source: SessionSource, _quick_key: str, effective_busy_input_mode: str) -> bool:diff --git a/tests/gateway/test_messaging_stop_new_cancel_queued.py b/tests/gateway/test_messaging_stop_new_cancel_queued.pynew file mode 100644index 00000000000..cbfa922d8b7--- /dev/null+++ b/tests/gateway/test_messaging_stop_new_cancel_queued.py@@ -0,0 +1,71 @@+"""Messaging /stop and /new drop the durable queued follow-up, as main dropped the adapter slot.++Under session authority every busy follow-up is admitted to the durable FIFO, so clearing the+adapter's pending slot no longer reached it: /stop let the drain RUN the message the user meant+to cancel, and /new stranded it queued on the abandoned session. Real GatewayRunner busy+dispatch, real DiscordAdapter, real authority over a real SessionDB; only the turn executor is+recorded.+"""+import asyncio++import pytest++[email protected]('command', ['/stop', '/new'])+async def test_busy_stop_and_new_cancel_the_queued_messaging_followup(tmp_path, monkeypatch, command):+ from gateway.config import GatewayConfig, Platform, PlatformConfig+ from gateway.platforms.event import MessageEvent+ from gateway.run import GatewayRunner+ from gateway.session_authority import SessionAuthority, initialize_session_authority+ from gateway.session_ingress import admit_message+ from gateway.session_ingress_context import native_callback+ from hermes_constants import get_hermes_home+ from hermes_state_runtime import list_session_admissions+ from plugins.platforms.discord.adapter import DiscordAdapter++ monkeypatch.setattr(SessionAuthority, '_schedule', lambda self, ref: None)+ monkeypatch.setenv('DISCORD_ALLOWED_USERS', '42')+ runner = GatewayRunner(GatewayConfig())+ adapter = DiscordAdapter(PlatformConfig(enabled=True, token='fixture-token', typing_indicator=False))+ runner.adapters = {Platform.DISCORD: adapter}+ authority = await initialize_session_authority(runner, profile_id='default', instance_id='owner')+ runner._wire_adapter_handlers(adapter)+ source = adapter.build_source(chat_id='42', chat_type='dm', user_id='42')++ # Turn A is running; follow-up B is committed to the durable FIFO behind it.+ follow_up = MessageEvent(text='B', source=source, message_id='B')+ with native_callback(runner, follow_up, get_hermes_home()):+ waiter = asyncio.create_task(admit_message(authority, follow_up))+ for _ in range(200):+ if authority.waiters:+ break+ await asyncio.sleep(0.01)+ key = runner.session_store._generate_session_key(source)+ owner = runner.session_store.peek_session_id(key)+ assert [r['status'] for r in list_session_admissions(authority.db, session_id=owner)] == ['queued']++ class RunningAgent:+ interrupted = []++ def interrupt(self, message=None):+ self.interrupted.append(message)++ runner._session_state(key).turn.agent = RunningAgent()+ with native_callback(runner, MessageEvent(text=command, source=source), get_hermes_home()):+ reply = await runner._handle_message(MessageEvent(text=command, source=source))+ assert reply is not None++ # B is settled silently (no pause notice, no reply) and never runs.+ assert await asyncio.wait_for(waiter, 5) is None+ rows = list_session_admissions(authority.db, session_id=owner, pending_only=False)+ assert [(r['request_id'], r['status'], r['outcome']) for r in rows] == [('B', 'terminal', 'cancelled')]+ executed = []++ async def handle(event):+ executed.append(event.text)+ return 'ran'+ monkeypatch.setattr(runner, '_handle_message', handle)+ from gateway.session_contract import SessionRef+ await SessionAuthority._drain(authority, SessionRef(authority.profile_id, owner))+ assert executed == []