The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): drop a refused watch notice pinned to a session /new closed
Kurt VanDusen3123de441253
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.pyindex 07c7c3b6f42..67a2e75c7c4 100644--- a/gateway/run_notifications.py+++ b/gateway/run_notifications.py@@ -854,7 +854,8 @@ class GatewayNotificationsMixin(GatewayUpdateNotificationsMixin, GatewayNotificaRouting comes from the queued event, never the active foreground message. Returns``True`` on adapter acceptance, ``False`` on retryable adapter failure, ``None`` with no- gateway route. Not transactional: a crash after acceptance can replay (at-least-once).+ gateway route or a refused notice pinned to a permanently-gone session (``_watch_pin_gone``).+ Not transactional: a crash after acceptance can replay (at-least-once)."""from gateway.wake import WakeNotAccepted, adapter_supports_push, admit_internal_eventsource = await asyncio.to_thread(self._build_process_event_source, evt)@@ -931,6 +932,10 @@ class GatewayNotificationsMixin(GatewayUpdateNotificationsMixin, GatewayNotificaawait admit_internal_event(adapter, synth_event)return Trueexcept WakeNotAccepted as exc:+ # Durable callers ran the pre-flight already; a watch notice is classified only once+ # refused, and one pinned to a gone session is dropped instead of requeued.+ if not raise_not_accepted and await self._watch_pin_gone(evt, str(exc)):+ return Nonelogger.warning("Watch notification not admitted for %s chat=%s: %s",platform_name, source.chat_id, exc)# Durable callers refund the claim; ordinary watch callers just requeue.diff --git a/gateway/run_notifications_ownership.py b/gateway/run_notifications_ownership.pyindex 7528ae1e779..9a78b487d6b 100644--- a/gateway/run_notifications_ownership.py+++ b/gateway/run_notifications_ownership.py@@ -138,6 +138,30 @@ class GatewayNotificationOwnershipMixin:session_id = parentreturn "terminal", session_id, None+ async def _watch_pin_gone(self, evt: dict, refusal: str) -> bool:+ """True (logged once) when a refused watch notice is pinned to a permanently-gone session.++ The session authority admits a notice only into its pinned session or that session's+ compression continuation, so after /new it refuses the notice on every retry and the drain+ would spin on it (#141562). This is the completion pre-flight's verdict, applied after the+ refusal: a /resume reopens the pinned session, which delivers again.+ """+ pinned = str(evt.get("parent_session_id") or "").strip()+ if not pinned:+ return False+ session_key = str(evt.get("session_key") or "")+ verdict, _owner, _target = await cast(Any, self)._classify_completion_owner(pinned, session_key)+ if verdict != "terminal":+ return False+ logger.warning(+ "Dropping %s notification of process %s: pinned session %s is permanently gone (user "+ "boundary such as /new); the route now runs %s and refuses it (%s). Output remains "+ "available via process(action='log').",+ evt.get("type") or "watch", evt.get("session_id") or "<unknown>", pinned,+ cast(Any, self).session_store.peek_session_id(session_key) or "<none>", refusal,+ )+ return True+async def _resolve_async_delegation_session(self, session_entry: SessionEntry, pinned_session_id: str, proven_session_id: str = "",completion_ids: Sequence[str] = (),diff --git a/tests/gateway/test_watch_notification_reset_pin.py b/tests/gateway/test_watch_notification_reset_pin.pynew file mode 100644index 00000000000..8f43fc7790d--- /dev/null+++ b/tests/gateway/test_watch_notification_reset_pin.py@@ -0,0 +1,178 @@+"""A watch notice pinned to a session that ended at a user boundary (/new) is dropped, not requeued+forever: the session authority refuses it ``admission_conflict`` on every drain tick (#141562).+Real GatewayRunner, DiscordAdapter, SessionAuthority and drain over SQLite; no agent turn runs."""+import logging+import queue+import time++import pytest++from tests.waiting import async_wait_until+++async def _gateway(monkeypatch):+ from gateway.config import GatewayConfig, Platform, PlatformConfig+ from gateway.platforms.base import SendResult+ from gateway.platforms.event import MessageEvent+ from gateway.run import GatewayRunner+ from gateway.session_results import execution_result+ from gateway.session_authority import SessionAuthority, initialize_session_authority+ 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))+ adapter.gateway_runner = runner+ runner.adapters = {Platform.DISCORD: adapter}+ authority = await initialize_session_authority(runner, profile_id='default', instance_id='owner')+ runner._wire_adapter_handlers(adapter)++ async def send(chat_id, content, reply_to=None, metadata=None):+ return SendResult(success=True, message_id='sent')++ async def turn(event, source, quick_key, run_generation):+ captured = execution_result.get()+ if captured is not None:+ captured['result'] = {'final_response': 'reply', 'messages': []}+ return 'reply'+ monkeypatch.setattr(adapter, 'send', send)+ monkeypatch.setattr(runner, '_handle_message_with_agent', turn)+ source = adapter.build_source(chat_id='42', chat_type='dm', user_id='42')+ # One real inbound turn commits the route a notice is admitted through.+ await adapter.handle_message(MessageEvent(text='start the build', source=source, message_id='m1'))+ key = runner.session_store._generate_session_key(source)+ await async_wait_until(lambda: _settled(authority, runner.session_store.peek_session_id(key)),+ timeout=15, what='the inbound turn to settle')+ # Admitted notices stay queued from here on: these tests assert the admission, not its turn.+ monkeypatch.setattr(SessionAuthority, '_schedule', lambda self, ref: None)+ return runner, authority, runner.session_store.lookup_by_session_key(key)+++def _settled(authority, session_id):+ from hermes_state_runtime import list_session_admissions+ rows = list_session_admissions(authority.db, session_id=session_id, pending_only=False) if session_id else []+ return bool(rows) and all(row['status'] not in ('queued', 'started') for row in rows)+++def _notice(entry, pinned_session_id, kind='watch_match'):+ return {+ 'type': kind, 'event_id': f'{kind}-1', 'session_id': 'proc_watch', 'started_at': 1.0,+ 'session_key': entry.session_key, 'parent_session_id': pinned_session_id,+ 'platform': 'discord', 'chat_type': 'dm', 'chat_id': '42', 'user_id': '42',+ 'command': 'tail -f build.log', 'pattern': 'READY', 'output': 'READY', 'suppressed': 0,+ 'seq': 1, 'interval': 60, 'elapsed': 60, 'omitted': 0,+ }+++def _queued_admissions(authority, session_id):+ from hermes_state_runtime import list_session_admissions+ return [row for row in list_session_admissions(authority.db, session_id=session_id, pending_only=False)+ if row['principal_id'].startswith('automation:')]++[email protected]('kind', ['watch_match', 'heartbeat'])+async def test_notice_pinned_to_a_reset_session_is_dropped_once(monkeypatch, caplog, kind):+ runner, authority, entry = await _gateway(monkeypatch)+ try:+ retired = entry.session_id+ live = runner.session_store.reset_session(entry.session_key).session_id+ assert live != retired+ pending = queue.Queue()+ pending.put(_notice(entry, retired, kind))++ with caplog.at_level(logging.WARNING, logger='gateway.run'):+ await runner._drain_watch_notifications(pending)++ assert pending.empty(), 'a notice the closed session can never admit must not be requeued'+ warnings = [r.getMessage() for r in caplog.records if r.levelno >= logging.WARNING]+ assert len(warnings) == 1, warnings+ assert 'permanently gone' in warnings[0]+ assert retired in warnings[0] and live in warnings[0] and 'admission_conflict' in warnings[0]+ assert not _queued_admissions(authority, live)+ finally:+ authority.db.close()+++async def test_notice_pinned_to_a_compression_ancestor_still_admits(monkeypatch, caplog):+ runner, authority, entry = await _gateway(monkeypatch)+ try:+ parent, child = entry.session_id, entry.session_id + '-compressed'+ runner.session_store._db_for_key(entry.session_key).publish_compression_child(+ parent_session_id=parent, child_session_id=child, source='discord',+ require_compression_lease=False, model='offline', model_config={},+ system_prompt='offline', messages=[{'role': 'user', 'content': 'retained'}],+ )+ assert runner.session_store.advance_compression_session(entry.session_key, parent, child)+ pending = queue.Queue()+ pending.put(_notice(entry, parent))++ with caplog.at_level(logging.WARNING, logger='gateway.run'):+ await runner._drain_watch_notifications(pending)++ assert pending.empty()+ assert not [r for r in caplog.records if r.levelno >= logging.WARNING]+ assert _queued_admissions(authority, parent) + _queued_admissions(authority, child)+ finally:+ authority.db.close()+++async def _foreign_pin(runner, adapter_source_chat='43'):+ """A live session of another chat: the authority refuses the notice, but nothing proves the pin+ gone, so the refusal stays retryable."""+ from gateway.config import Platform+ adapter = runner.adapters[Platform.DISCORD]+ other = adapter.build_source(chat_id=adapter_source_chat, chat_type='dm', user_id='42')+ return runner.session_store.get_or_create_session(other).session_id+++async def test_unproven_refusal_requeues_with_backoff_until_the_cap(monkeypatch, caplog):+ from gateway import run_notifications+ from gateway.run_notifications import _MAX_COMPLETION_INJECTION_ATTEMPTS+ runner, authority, entry = await _gateway(monkeypatch)+ try:+ pending = queue.Queue()+ pending.put(_notice(entry, await _foreign_pin(runner)))+ # Only ever forward: the event loop keeps its own timers on the same clock.+ skew = {'s': 0.0}+ real = time.monotonic+ monkeypatch.setattr(run_notifications.time, 'monotonic', lambda: real() + skew['s'])++ with caplog.at_level(logging.WARNING, logger='gateway.run'):+ await runner._drain_watch_notifications(pending)+ assert not pending.empty(), 'an unproven refusal is retried'+ for _ in range(200):+ if pending.empty():+ break+ skew['s'] += 60+ await runner._drain_watch_notifications(pending)++ messages = [r.getMessage() for r in caplog.records]+ assert pending.empty()+ assert sum('not admitted' in m for m in messages) == _MAX_COMPLETION_INJECTION_ATTEMPTS+ assert sum('dropping the queued event' in m for m in messages) == 1+ assert not [m for m in messages if 'permanently gone' in m]+ finally:+ authority.db.close()+++async def test_unproven_heartbeat_refusal_requeues_the_original_tick(monkeypatch, caplog):+ """A liveness tick keeps its own lifecycle: a refusal the pre-flight cannot prove permanent puts+ the original tick back (a later tick folds from it); only a gone pin drops it."""+ runner, authority, entry = await _gateway(monkeypatch)+ try:+ tick = _notice(entry, await _foreign_pin(runner), 'heartbeat')+ pending = queue.Queue()+ pending.put(tick)++ with caplog.at_level(logging.WARNING, logger='gateway.run'):+ await runner._drain_watch_notifications(pending)++ assert pending.get_nowait() is tick+ assert not [r for r in caplog.records if 'permanently gone' in r.getMessage()]+ assert not _queued_admissions(authority, entry.session_id)+ finally:+ authority.db.close()