Skip to content

The agent that grows with you

fix(gateway): messaging /stop and /new stop the authority's claimed turn

Tekniuma33345267310
diff --git a/gateway/run_inbound.py b/gateway/run_inbound.py
index 2c94ac0d4b4..4156596e645 100644
--- a/gateway/run_inbound.py
+++ b/gateway/run_inbound.py
@@ -574,7 +574,7 @@ class GatewayInboundMixin(GatewayInboundHooksMixin, GatewayPluginInjectionMixin)
if _denied is not None:
return True, _denied
if _cmd_def_inner.name in ("stop", "new"):
- await self._hm_cancel_queued_followups(_quick_key)
+ await self._hm_stop_owner_turn(_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)
@@ -587,11 +587,12 @@ class GatewayInboundMixin(GatewayInboundHooksMixin, GatewayPluginInjectionMixin)
return True, None
return 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)."""
+ async def _hm_stop_owner_turn(self, _quick_key: str) -> None:
+ """Messaging /stop and /new (busy, idle or confirmed) act on the authority's turn, as main
+ cancelled the adapter task that ran it and dropped its pending slot. Cancels the still-queued
+ ``messaging:*`` rows of this route's logical owner first (automation wakes, LOCAL/API input
+ and ``unknown`` rows are untouched; settled silently, no pause notice), then interrupts the
+ claimed generation: a turn still constructing its agent latches the Stop for adopt_agent."""
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
@@ -607,7 +608,11 @@ class GatewayInboundMixin(GatewayInboundHooksMixin, GatewayPluginInjectionMixin)
# 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'])
-
+ handle = authority._handle(ref)
+ if handle.execution_state == 'running':
+ # Settled meanwhile (stale_generation): there is no claimed turn left to stop.
+ with suppress(RuntimeStoreError):
+ await authority.interrupt(actor, ref, handle.execution_generation)
def _hm_busy_telegram_grace_queue(
self, event: MessageEvent, source: SessionSource, _quick_key: str, effective_busy_input_mode: str
) -> bool:
diff --git a/gateway/slash_commands.py b/gateway/slash_commands.py
index 0ea4b9e242e..4f1e5f20933 100644
--- a/gateway/slash_commands.py
+++ b/gateway/slash_commands.py
@@ -426,6 +426,7 @@ class GatewaySlashCommandsMixin(
source = event.source
session_entry = await self.async_session_store.get_or_create_session(source)
session_key = session_entry.session_key
+ await self._hm_stop_owner_turn(session_key) # also between turns, before the next claim (N53)
async def _stop(key: str, invalidation_reason: str) -> None:
await self._interrupt_and_clear_session(
diff --git a/gateway/slash_commands_session.py b/gateway/slash_commands_session.py
index c73d276566c..e9b5c301208 100644
--- a/gateway/slash_commands_session.py
+++ b/gateway/slash_commands_session.py
@@ -157,6 +157,7 @@ class GatewaySessionCommandsMixin:
"""Handle /new or /reset command."""
source = event.source
session_key = self._session_key_for_source(source)
+ await self._hm_stop_owner_turn(session_key) # idle and confirmed resets too (N53)
self._invalidate_session_run_generation(session_key, reason="session_reset")
# Evict the running-agent slot now that the generation is bumped: the in-flight run's own
# guarded release (old generation) returns False and would leave a zombie slot that silently
diff --git a/tests/gateway/test_messaging_stop_owner_turn.py b/tests/gateway/test_messaging_stop_owner_turn.py
new file mode 100644
index 00000000000..2d2821b781a
--- /dev/null
+++ b/tests/gateway/test_messaging_stop_owner_turn.py
@@ -0,0 +1,134 @@
+"""Messaging /stop and /new reach the authority's claimed turn and its queued follow-up.
+
+N45: a /stop while the claimed turn still builds its agent (the slot holds the pending sentinel)
+must latch on the authority, so the agent the turn adopts starts interrupted and never runs tools.
+N53: an idle /stop or /new (between turns: slot released, next row not yet claimed) must still
+cancel the queued messaging follow-up. Real GatewayRunner, DiscordAdapter, SessionAuthority and
+drain over a real SessionDB; only agent construction/conversation is a stand-in.
+"""
+import asyncio
+
+import pytest
+
+
+def _owner(monkeypatch):
+ from gateway.config import GatewayConfig, Platform, PlatformConfig
+ from gateway.run import GatewayRunner
+ from plugins.platforms.discord.adapter import DiscordAdapter
+
+ monkeypatch.setenv('DISCORD_ALLOWED_USERS', '42')
+ runner = GatewayRunner(GatewayConfig())
+ adapter = DiscordAdapter(PlatformConfig(enabled=True, token='fixture-token', typing_indicator=False))
+ sent = []
+
+ async def send(chat_id, content, reply_to=None, metadata=None):
+ from gateway.platforms.base import SendResult
+ sent.append(content)
+ return SendResult(success=True, message_id=f's{len(sent)}')
+ monkeypatch.setattr(adapter, 'send', send)
+ runner.adapters = {Platform.DISCORD: adapter}
+ return runner, adapter, sent
+
+
+async def _until(predicate, timeout=10):
+ async with asyncio.timeout(timeout):
+ while not predicate():
+ await asyncio.sleep(0.01)
+
+
+async def test_stop_during_agent_construction_latches_so_the_turn_runs_no_tools(monkeypatch):
+ from gateway.platforms.event import MessageEvent
+ from gateway.run_turn_runner import TurnRunner
+ from gateway.session_authority import initialize_session_authority
+ from gateway.session_results import execution_result
+ from gateway.turn_context import TurnContext
+ from hermes_state_runtime import list_session_admissions
+
+ 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')
+ key = runner.session_store._generate_session_key(source)
+ constructing, built = asyncio.Event(), asyncio.Event()
+ tools_ran = []
+
+ class Agent:
+ interrupted = False
+
+ def interrupt(self, *args, **kwargs):
+ self.interrupted = True
+
+ def clear_interrupt(self):
+ self.interrupted = False
+
+ async def turn(event, source, quick_key, run_generation):
+ # Agent construction: the slot holds the pending sentinel until the agent is adopted.
+ constructing.set()
+ await built.wait()
+ ctx = TurnContext(session_id=runner.session_store.peek_session_id(key), session_key=key, source=source)
+ owner, sid, generation = TurnRunner(runner, ctx)._approval_owner
+ agent = Agent()
+ owner.adopt_agent(sid, generation, agent) # what TurnRunner's agent wiring does
+ if agent.interrupted:
+ result = {'final_response': '', 'messages': [], 'interrupted': True}
+ else:
+ tools_ran.append(event.text) # run_conversation: the turn's tools execute
+ result = {'final_response': 'done', 'messages': []}
+ execution_result.get()['result'] = result
+ return result['final_response'] or None
+ monkeypatch.setattr(runner, '_handle_message_with_agent', turn)
+
+ await adapter.handle_message(MessageEvent(text='A', source=source, message_id='A'))
+ await asyncio.wait_for(constructing.wait(), 10)
+ await adapter.handle_message(MessageEvent(text='/stop', source=source, message_id='stop'))
+ await _until(lambda: any('Stopped' in text for text in sent))
+ built.set()
+ owner_id = runner.session_store.peek_session_id(key)
+ await _until(lambda: [r['status'] for r in list_session_admissions(
+ authority.db, session_id=owner_id, pending_only=False)] == ['terminal'])
+ rows = list_session_admissions(authority.db, session_id=owner_id, pending_only=False)
+ assert tools_ran == [], 'the stopped turn still ran its tools'
+ assert [(r['request_id'], r['outcome']) for r in rows] == [('A', 'interrupted')]
+
+
[email protected]('command', ['/stop', '/new'])
+async def test_idle_stop_and_new_cancel_the_queued_followup(monkeypatch, command):
+ from gateway.platforms.event import MessageEvent
+ from gateway.session_authority import SessionAuthority, initialize_session_authority
+ from gateway.session_contract import SessionRef
+ 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
+
+ runner, adapter, _sent = _owner(monkeypatch)
+ # The gap between turns: the slot is released and the drain has not claimed B yet.
+ monkeypatch.setattr(SessionAuthority, '_schedule', lambda self, ref: None)
+ monkeypatch.setattr(runner, '_read_user_config', lambda: {'approvals': {'destructive_slash_confirm': False}})
+ 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')
+ 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))
+ await _until(lambda: authority.waiters)
+ key = runner.session_store._generate_session_key(source)
+ owner = runner.session_store.peek_session_id(key)
+ assert not runner._is_session_running(key)
+
+ stop = MessageEvent(text=command, source=source)
+ with native_callback(runner, stop, get_hermes_home()):
+ assert await runner._handle_message(stop) is not None
+ assert await asyncio.wait_for(waiter, 5) is None, 'the cancelled follow-up must get no reply'
+ 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)
+ await SessionAuthority._drain(authority, SessionRef(authority.profile_id, owner))
+ assert executed == []