The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): keep an idle slash command's stop epoch from before its rewrite
Teknium928530047621
diff --git a/gateway/run_busy.py b/gateway/run_busy.pyindex 329f34836b7..e20696b4b35 100644--- a/gateway/run_busy.py+++ b/gateway/run_busy.py@@ -1247,13 +1247,13 @@ class GatewayBusySessionMixin:return eventasync def _admit_command_turn(self, event: MessageEvent, source, session_key: str,- stop_epoch: Optional[int] = None) -> tuple[bool, Optional[str]]:+ stop_epoch: int) -> tuple[bool, Optional[str]]:"""Inline admission (the idle path, a mid-run skill reaching the runner): ``(True, reply)``- like ``_hm_authority_admission``, else ``(False, None)`` to stay in-process."""+ like ``_hm_authority_admission``, else ``(False, None)`` to stay in-process. *stop_epoch* is+ the route's /stop and /new epoch read when the command arrived, before any rewrite."""if not self._command_turn_admitted(event, session_key):return False, None- epoch = self._route_stop_epoch(session_key) if stop_epoch is None else stop_epoch- return await self._hm_authority_admission(self._as_fifo_turn(event, epoch), source, session_key, False)+ return await self._hm_authority_admission(self._as_fifo_turn(event, stop_epoch), source, session_key, False)async def _queue_command_turn(self, session_key: str, command: MessageEvent, event: MessageEvent, adapter,ack: Optional[str] = None, stop_epoch: Optional[int] = None) -> bool:diff --git a/gateway/run_inbound.py b/gateway/run_inbound.pyindex 9f2accb7051..7cb57141dbd 100644--- a/gateway/run_inbound.py+++ b/gateway/run_inbound.py@@ -1146,7 +1146,10 @@ class GatewayInboundMixin(GatewayInboundHooksMixin, GatewayInboundTranscriptionMasync def _hm_dispatch_idle_commands(self, event: MessageEvent, source: SessionSource, _quick_key: str) -> tuple[bool, Optional[str]]:- """Idle path: resolve + dispatch slash commands; rewriting commands fall through to the agent."""+ """Idle path: resolve + dispatch slash commands; rewriting commands fall through to the agent.+ A command rewritten into a turn keeps the /stop and /new epoch it arrived under, read before+ the (slow, off-loop) rewrite, so a reset during it drops the command, as on the busy path."""+ stop_epoch = self._route_stop_epoch(_quick_key)_handled, _result, command, canonical = await self._hm_resolve_command(event, source, _quick_key)if not _handled:_handled, _result = await self._hm_dispatch_canonical_command(event, source, _quick_key, canonical)@@ -1161,7 +1164,7 @@ class GatewayInboundMixin(GatewayInboundHooksMixin, GatewayInboundTranscriptionMself._hm_skill_slash_rewrite, event, source, _quick_key, command)_handled = _result is not Noneif not _handled and command: # rewritten into a turn: the authority's FIFO, like text- _handled, _result = await self._admit_command_turn(event, source, _quick_key)+ _handled, _result = await self._admit_command_turn(event, source, _quick_key, stop_epoch)return _handled, _resultdef _hm_rescue_orphaned_fifo(diff --git a/tests/gateway/test_messaging_slash_command_fifo.py b/tests/gateway/test_messaging_slash_command_fifo.pyindex 9638b7d8b8f..763f074ad50 100644--- a/tests/gateway/test_messaging_slash_command_fifo.py+++ b/tests/gateway/test_messaging_slash_command_fifo.py@@ -352,3 +352,55 @@ async def test_queued_approval_word_is_a_turn_never_an_approval(monkeypatch):monkeypatch.setattr(GatewayRunner, '_handle_approve_command', approve)order = await _run_a_then(monkeypatch, 'queue', '/queue yes')assert approved == [] and 'C' in [m for m, _ in order], (approved, order)++[email protected]('reset', ['/new', '/stop'])+async def test_reset_during_an_idle_skill_rewrite_drops_it(monkeypatch, reset):+ """Idle chat: ``/demo C``'s (slow) rewrite runs off the loop; a ``/new`` or ``/stop`` landing during it+ must drop the command, which keeps the epoch it arrived under. Read after the rewrite, the epoch+ already counts the reset and the old command commits into the fresh conversation."""+ import threading+ import agent.skill_commands as skills+ from gateway.platforms.event import MessageEvent, MessageType+ from gateway.session_authority import initialize_session_authority+ from gateway.session_ingress import executing_admission+ from gateway.session_results import execution_result++ _skill(monkeypatch)+ started, gate = threading.Event(), threading.Event()++ def slow(text):+ started.set()+ gate.wait(10)+ return f'[demo skill] {text}'+ monkeypatch.setattr(skills, 'build_skill_invocation_message', lambda key, text='', **_: slow(text))+ runner, adapter, _sent = _owner(monkeypatch)+ 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')+ order = []++ async def turn(event, source, quick_key, run_generation):+ order.append((event.message_id, executing_admission.get()))+ captured = execution_result.get()+ if captured is not None:+ captured['result'] = {'final_response': 'ok', 'messages': []}+ return 'ok'+ monkeypatch.setattr(runner, '_handle_message_with_agent', turn)+ await adapter.handle_message(MessageEvent(text='A', source=source, message_id='A'))+ await async_wait_until(lambda: order == [('A', True)] and not runner._is_session_running(+ runner.session_store._generate_session_key(source)), timeout=15, what='A to run and settle')+ command = MessageEvent(text='/demo C', message_type=MessageType.COMMAND, source=source, message_id='C')+ task = asyncio.create_task(runner._handle_message(command))+ await async_wait_until(lambda: started.is_set() or task.done(), what='the idle rewrite to start')+ assert not task.done()+ await runner._handle_message(MessageEvent(text=reset, message_type=MessageType.COMMAND,+ source=source, message_id='S'))+ gate.set()+ await asyncio.wait_for(task, 15)+ key = runner.session_store._generate_session_key(source)+ from hermes_state_runtime import list_session_admissions+ rows = [row['request_id'] for row in list_session_admissions(+ authority.db, session_id=runner.session_store.peek_session_id(key), pending_only=False)]+ assert 'C' not in rows and 'C' not in [m for m, _ in order], (rows, order)