The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): run_turn resolves adapters through the intake/delivery seams
teknium14c132febc930
diff --git a/gateway/run_turn.py b/gateway/run_turn.pyindex ca50f13f99b..e32afc3e44c 100644--- a/gateway/run_turn.py+++ b/gateway/run_turn.py@@ -480,7 +480,7 @@ class GatewayTurnMixin:try:should_notify = reset_reason == "suspended"- adapter = self._adapter_for_source(source) if should_notify else None+ adapter = self._delivery_adapter_for(source) if should_notify else Noneif adapter:notice = ("◐ Session reset after being stopped. "@@ -826,7 +826,7 @@ class GatewayTurnMixin:async def _hmwa_hygiene_notify(self, source, meta, message, what):"""Best-effort user notice on the hygiene thread; failure is logged, never raised."""try:- _adapter = self._adapter_for_source(source)+ _adapter = self._delivery_adapter_for(source)if _adapter and source.chat_id:await _adapter.emit_warning(source.chat_id, message, metadata=meta,logical_platform=source.platform)@@ -1429,7 +1429,7 @@ class GatewayTurnMixin:"""Stop the typing indicator (never raises). Slack AI status is scoped to a thread/workspace, so preserve the routing metadata used by the response delivery path."""with suppress(Exception):- _typing_adapter = self._adapter_for_source(source)+ _typing_adapter = self._delivery_adapter_for(source)_kind = type(_typing_adapter)if _typing_adapter and callable(getattr(_kind, "_stop_typing_with_metadata", None)):await _typing_adapter._stop_typing_with_metadata(source.chat_id, self._event_thread_metadata(event, source))@@ -1855,7 +1855,7 @@ class GatewayTurnMixin:logger.info("Suppressing intentional silence marker for session %s", session_entry.session_id)response = ""- adapter = self._adapter_for_source(source)+ adapter = self._delivery_adapter_for(source)# Auto voice reply (TTS audio before the text) unless streaming TTS already delivered audio._streaming_tts_done = adapter is not None and bool(getattr(adapter, "_streaming_tts_turn_completed", lambda *_a, **_k: False)(session_key, run_generation)@@ -1956,7 +1956,7 @@ class GatewayTurnMixin:"Discarding stale agent result for %s — generation %d is no longer current",_quick_key or "?", run_generation,)- self._pop_post_delivery_callback(self._adapter_for_source(source), _quick_key, run_generation)+ self._pop_post_delivery_callback(self._delivery_adapter_for(source), _quick_key, run_generation)@dataclasses.dataclassclass _PreparedTurn:@@ -2050,7 +2050,7 @@ class GatewayTurnMixin:# Bind this run generation to the adapter so deferred post-delivery callbacks are released# by the run that registered them.- self._bind_adapter_run_generation(self._adapter_for_source(source), session_key, run_generation)+ self._bind_adapter_run_generation(self._delivery_adapter_for(source), session_key, run_generation)# Delivery IDs are only unique in their transport namespace. Keyless turns# need their own identity, even when another process writes to this session.import uuid@@ -2283,7 +2283,7 @@ class GatewayTurnMixin:toolsets dropped, not trusted)."""from hermes_cli.tools_config import _get_platform_toolstry:- adapter = self._adapter_for_source(source)+ adapter = self._delivery_adapter_for(source)override = adapter.toolsets_for_source(source) if adapter is not None else Noneexcept Exception:override = None@@ -2313,7 +2313,7 @@ class GatewayTurnMixin:from run_agent import AIAgentmedia_urls = media_urls or []media_types = media_types or []- adapter = self._adapter_for_source(source)+ adapter = self._delivery_adapter_for(source)if not adapter:logger.warning("No adapter for platform %s in background task %s", source.platform, task_id)return@@ -2633,7 +2633,7 @@ class GatewayTurnMixin:return Nonetry:from gateway.stream_consumer import GatewayStreamConsumer- _adapter = self._adapter_for_source(source)+ _adapter = self._delivery_adapter_for(source)if not _adapter:return None_consumer_cfg, _pause_typing_before_finalize = self._build_stream_consumer_config(@@ -2716,7 +2716,7 @@ class GatewayTurnMixin:)stream_task = asyncio.create_task(_stream_consumer.run()) if _stream_consumer else None- _adapter = self._adapter_for_source(source)+ _adapter = self._delivery_adapter_for(source)if _adapter and not scheduled_heartbeat:with suppress(Exception):await _adapter.send_typing(source.chat_id, metadata=_thread_metadata)@@ -2846,7 +2846,7 @@ class GatewayTurnMixin:user_config = _load_gateway_config()platform_key = _platform_config_key(source.platform)enabled_toolsets, disabled_toolsets = self._resolve_turn_toolsets(user_config, source, platform_key)- adapter = self._adapter_for_source(source)+ adapter = self._delivery_adapter_for(source)# Tool preview length (0 = no limit) and friendly tool labels (default on), per-platform.for _setter, _setting, _default, _cast in (("set_tool_preview_max_len", "tool_preview_length", 0, lambda v: int(v) if v else 0),@@ -2979,7 +2979,7 @@ class GatewayTurnMixin:_cleanup_progress = bool(disp.resolve_display_setting(disp.user_config, disp.platform_key, "cleanup_progress"))- _cleanup_adapter = self._adapter_for_source(source) if _cleanup_progress else None+ _cleanup_adapter = self._delivery_adapter_for(source) if _cleanup_progress else Noneif _cleanup_adapter is not None and getattr(type(_cleanup_adapter), "delete_message", None) in (None, BasePlatformAdapter.delete_message,):@@ -3038,7 +3038,7 @@ class GatewayTurnMixin:from gateway.run import _non_conversational_metadata, _resolve_progress_thread_idis_buzz = str(getattr(source.platform, "value", source.platform) or "").lower() == "buzz"_progress_reply_in_thread = True- _adapter = self._adapter_for_source(source) if source.platform == Platform.SLACK or is_buzz else None+ _adapter = self._delivery_adapter_for(source) if source.platform == Platform.SLACK or is_buzz else Noneif _adapter is not None:try:if is_buzz:@@ -3146,7 +3146,7 @@ class GatewayTurnMixin:# This avoids a cross-scope NameError: the outer interrupt / finalisation paths reference the# consumer via ``streaming_tts_consumer_holder[0]``. Gates: voice input, auto-TTS enabled for this# chat, adapter supports streaming, and a usable streaming TTS provider configured. See #60671.- _stts_adapter = self._adapter_for_source(source)+ _stts_adapter = self._delivery_adapter_for(source)_is_voice_input = (message_type is not Noneand str(getattr(message_type, "value", message_type)).lower() == "voice"@@ -3254,7 +3254,7 @@ class GatewayTurnMixin:await asyncio.sleep(0.2)try:# Re-resolve the adapter each iteration so reconnects don't leave a stale reference.- _adapter = self._adapter_for_source(source)+ _adapter = self._delivery_adapter_for(source)if not _adapter:continueif hasattr(_adapter, 'has_pending_interrupt') and _adapter.has_pending_interrupt(session_key):@@ -3278,7 +3278,7 @@ class GatewayTurnMixin:source, session_key = turn_ctx.source, turn_ctx.session_keyif _interrupt_detected.is_set() or not session_key:return- _backup_adapter = self._adapter_for_source(source)+ _backup_adapter = self._delivery_adapter_for(source)_backup_agent = turn_ctx.agent_holder[0]if (_backup_adapter and _backup_agentand hasattr(_backup_adapter, 'has_pending_interrupt')@@ -3402,7 +3402,7 @@ class GatewayTurnMixin:async def _run_agent_inactivity_warning(self, worker, source, _status_thread_metadata) -> None:"""Staged one-shot warning before the inactivity timeout escalates."""from gateway.run import _interim_metadata- _warn_adapter = self._adapter_for_source(source)+ _warn_adapter = self._delivery_adapter_for(source)if not _warn_adapter:returntry:@@ -3695,7 +3695,7 @@ class GatewayTurnMixin:"Interrupt recursion depth %d reached for session %s — ""queueing message instead of recursing.", _interrupt_depth, session_key,)- adapter = self._adapter_for_source(source)+ adapter = self._delivery_adapter_for(source)if adapter and pending_event:merge_pending_message_event(adapter._pending_messages, session_key, pending_event)elif adapter and hasattr(adapter, 'queue_message'):@@ -3749,7 +3749,7 @@ class GatewayTurnMixin:# Clear the prior turn's streaming-TTS completion marker so the recursive turn isn't suppressed.# See #60671.- _clear_adapter = self._adapter_for_source(source)+ _clear_adapter = self._delivery_adapter_for(source)_completed_turns = getattr(_clear_adapter, "_streaming_tts_completed_turns", None)_prior_key = getattr(_clear_adapter, "_streaming_tts_turn_key", None)if _completed_turns is not None and callable(_prior_key) and session_key and run_generation is not None:@@ -3778,7 +3778,7 @@ class GatewayTurnMixin:# Resolve the adapter from the follow-up's OWN source — a multiplexed gateway can route it to a# different profile's adapter, and only that instance holds the per-message reaction state.from gateway.run_turn_followup_ack import _followup_cancel_outcome, _run_followup_processing_hook- _hook_adapter = self._adapter_for_source(next_source) if pending_event is not None else None+ _hook_adapter = self._intake_adapter_for(next_source) if pending_event is not None else Noneawait _run_followup_processing_hook(_hook_adapter, pending_event, "on_processing_start")# The re-baseline sits inside the try: a /stop landing on its DB await must still close the marker# (the helper's own ``except Exception`` does not catch cancellation).@@ -4027,7 +4027,7 @@ class GatewayTurnMixin:turn_ctx._step_callback_sync = turn_runner._step_callback_syncturn_ctx._event_callback_sync = turn_runner._event_callback_syncturn_ctx._status_callback_sync = turn_runner._status_callback_sync- turn_ctx._status_adapter = self._adapter_for_source(source)+ turn_ctx._status_adapter = self._delivery_adapter_for(source)turn_ctx._status_chat_id = source.chat_idturn_ctx._status_thread_metadata = _status_thread_metadatareturn _status_thread_metadata@@ -4049,7 +4049,7 @@ class GatewayTurnMixin:returnsource, session_key, agent_holder = turn_ctx.source, turn_ctx.session_key, turn_ctx.agent_holder_status_thread_metadata = turn_ctx._status_thread_metadata- _notify_adapter = self._adapter_for_source(source)+ _notify_adapter = self._delivery_adapter_for(source)if not _notify_adapter:return_heartbeat_msg_id: Optional[str] = None@@ -4191,7 +4191,7 @@ class GatewayTurnMixin:# Interrupted OR queued message (/queue)?result = turn_ctx.result_holder[0]- adapter = self._adapter_for_source(source)+ adapter = self._delivery_adapter_for(source)await self._run_agent_finalize_streaming_tts(turn_ctx, adapter)pending_event, pending = await self._run_agent_drain_pending(result, adapter, source, session_key)if pending_event or pending: