Skip to content

The agent that grows with you

refactor(gateway): move streaming-TTS types to base_streaming_tts; split busy-path helpers

Teknium710c70a6f9d5
diff --git a/gateway/platforms/base.py b/gateway/platforms/base.py
index 6fea2962c3f..83c5d9886df 100644
--- a/gateway/platforms/base.py
+++ b/gateway/platforms/base.py
@@ -417,7 +417,7 @@ def is_host_excluded_by_no_proxy(hostname: str, no_proxy_value: str | None = Non
import dataclasses
-from dataclasses import dataclass, field
+from dataclasses import dataclass
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional, Any, Callable, Awaitable, Tuple, Union
@@ -428,6 +428,8 @@ from gateway.platforms.helpers import fence_state_after
from gateway.platforms.base_exec_approval import (
approval_timeout_seconds, ea_action_labels, ea_default_reason_text, ea_header_text,
ea_reason_label_text, ea_smart_deny_line_text, format_approval_deadline_line)
+from gateway.platforms.base_streaming_tts import (
+ AudioFormat, StreamingTTSHandle, streaming_tts_should_skip_whole_file, streaming_tts_turn_key)
from gateway.platforms.event import MessageEvent, MessageType, ProcessingOutcome
from gateway.warning_notifications import diagnostic_wake_muted
from hermes_cli.observability.shared_metrics_gateway import records_delivery, stop_reply_clock
@@ -439,46 +441,6 @@ from agent.provider_media import GENERATED_SUBDIR, MEDIA_CACHE_MAX_AGE_HOURS
if TYPE_CHECKING:
from agent.display import ToolPreview
-@dataclass
-# --------------------------------------------------------------------------- Streaming TTS format
-# descriptor and handle (#60671) ---------------------------------------------------------------------------
-class AudioFormat:
- """Declared PCM format for a streaming-TTS session: every ``write_streaming_tts``
- chunk must be raw little-endian PCM at this rate / channels / sample width."""
- sample_rate: int = 24000
- channels: int = 1
- sample_width: int = 2 # bytes per sample (int16 = 2)
-
-
-@dataclass
-class StreamingTTSHandle:
- """Opaque handle returned by ``begin_streaming_tts``; adapters may extend it with
- platform state. The base fields are consumer bookkeeping / cancellation."""
- chat_id: str = ""
- audio_format: AudioFormat = field(default_factory=AudioFormat)
- # True once the first PCM chunk is written: a later failure then ends cleanly instead of
- # falling back to whole-file TTS (don't replay already-audible output).
- audible: bool = False
- aborted: bool = False # set by abort_streaming_tts; late chunks are dropped
-
-
-def streaming_tts_turn_key(session_key: str | None, turn_marker: Any = None, *, event: Any = None) -> str | None:
- """Per-turn streaming-TTS suppression key — turn-scoped (not chat-scoped) so
- overlapping turns in one chat can't suppress each other's fallback paths.
- ``turn_marker`` is normally the run generation, else the event's message/update id."""
- if not session_key:
- return None
- if turn_marker is None and event is not None:
- turn_marker = getattr(event, "message_id", None) or getattr(event, "platform_update_id", None)
- return None if turn_marker is None else f"{session_key}:{turn_marker}"
-
-
-def streaming_tts_should_skip_whole_file(completed_turns: set[str], session_key: str | None,
- turn_marker: Any = None, *, event: Any = None) -> bool:
- """Pure, turn-scoped auto-TTS suppression decision (testable without the adapter stack)."""
- turn_key = streaming_tts_turn_key(session_key, turn_marker, event=event)
- return bool(turn_key and turn_key in completed_turns)
-
GATEWAY_SECRET_CAPTURE_UNSUPPORTED_MESSAGE = (
"Secure secret entry is not supported over messaging. "
@@ -4077,12 +4039,33 @@ class BasePlatformAdapter(ABC):
# Guard installed synchronously BEFORE the task spawns so a second message can't race in.
event._gateway_accepted = self._start_session_processing(event, session_key)
+ def _shared_runtime_owns_input(self, event: MessageEvent) -> bool:
+ """True when a non-command input belongs to the shared runtime's session authority."""
+ runner = getattr(self._message_handler, '__self__', None)
+ return getattr(runner, 'session_authority', None) is not None and not event.get_command()
+
+ def _pending_recovery_owners(self, event: MessageEvent, session_key: str) -> list:
+ """Adapters whose pending slot may hold *event* after busy admission: this one, plus the
+ runner's current delivery adapter for its source when that replacement has no live guard."""
+ owners = [self]
+ runner = getattr(self, "gateway_runner", None)
+ if runner is not None:
+ try:
+ delivery_adapter = runner._delivery_adapter_for(event.source)
+ except Exception:
+ delivery_adapter = None
+ logger.debug("[%s] Delivery-adapter lookup failed during pending recovery",
+ self.name, exc_info=True)
+ if (delivery_adapter is not None and delivery_adapter is not self
+ and session_key not in delivery_adapter._active_sessions):
+ owners.append(delivery_adapter)
+ return owners
+
async def _handle_message_while_active(self, event: MessageEvent, session_key: str) -> None:
"""Route a message that arrived while ``session_key`` is busy: bypass
commands / clarify replies dispatch inline, everything else is queued."""
# The shared runtime's durable FIFO replaces the adapter's busy-message slot.
- runner = getattr(self._message_handler, '__self__', None)
- if getattr(runner, 'session_authority', None) is not None and not event.get_command():
+ if self._shared_runtime_owns_input(event):
from gateway.session_ingress import dispatch_shared_busy
await dispatch_shared_busy(self, event, session_key)
return
@@ -4148,20 +4131,8 @@ class BasePlatformAdapter(ABC):
# Recover from whichever slot actually holds it — this one, or the replacement —
# and start it on that slot's owner. A replacement with a live guard drains its
# own slot, so leave that one alone.
- owners = [self]
- runner = getattr(self, "gateway_runner", None)
- if runner is not None:
- try:
- delivery_adapter = runner._delivery_adapter_for(event.source)
- except Exception:
- delivery_adapter = None
- logger.debug("[%s] Delivery-adapter lookup failed during pending recovery",
- self.name, exc_info=True)
- if (delivery_adapter is not None and delivery_adapter is not self
- and session_key not in delivery_adapter._active_sessions):
- owners.append(delivery_adapter)
orphan = None
- for owner in owners:
+ for owner in self._pending_recovery_owners(event, session_key):
orphan = owner.get_pending_message(session_key)
if orphan is not None:
owner._start_session_processing(orphan, session_key)
diff --git a/gateway/platforms/base_streaming_tts.py b/gateway/platforms/base_streaming_tts.py
new file mode 100644
index 00000000000..1883ffa710f
--- /dev/null
+++ b/gateway/platforms/base_streaming_tts.py
@@ -0,0 +1,48 @@
+"""Streaming-TTS format descriptor, handle and turn-scoped suppression helpers (#60671).
+
+Re-exported from ``gateway.platforms.base``; no imports from it (base imports this module).
+"""
+
+from dataclasses import dataclass, field
+from typing import Any
+
+
+@dataclass
+# --------------------------------------------------------------------------- Streaming TTS format
+# descriptor and handle (#60671) ---------------------------------------------------------------------------
+class AudioFormat:
+ """Declared PCM format for a streaming-TTS session: every ``write_streaming_tts``
+ chunk must be raw little-endian PCM at this rate / channels / sample width."""
+ sample_rate: int = 24000
+ channels: int = 1
+ sample_width: int = 2 # bytes per sample (int16 = 2)
+
+
+@dataclass
+class StreamingTTSHandle:
+ """Opaque handle returned by ``begin_streaming_tts``; adapters may extend it with
+ platform state. The base fields are consumer bookkeeping / cancellation."""
+ chat_id: str = ""
+ audio_format: AudioFormat = field(default_factory=AudioFormat)
+ # True once the first PCM chunk is written: a later failure then ends cleanly instead of
+ # falling back to whole-file TTS (don't replay already-audible output).
+ audible: bool = False
+ aborted: bool = False # set by abort_streaming_tts; late chunks are dropped
+
+
+def streaming_tts_turn_key(session_key: str | None, turn_marker: Any = None, *, event: Any = None) -> str | None:
+ """Per-turn streaming-TTS suppression key — turn-scoped (not chat-scoped) so
+ overlapping turns in one chat can't suppress each other's fallback paths.
+ ``turn_marker`` is normally the run generation, else the event's message/update id."""
+ if not session_key:
+ return None
+ if turn_marker is None and event is not None:
+ turn_marker = getattr(event, "message_id", None) or getattr(event, "platform_update_id", None)
+ return None if turn_marker is None else f"{session_key}:{turn_marker}"
+
+
+def streaming_tts_should_skip_whole_file(completed_turns: set[str], session_key: str | None,
+ turn_marker: Any = None, *, event: Any = None) -> bool:
+ """Pure, turn-scoped auto-TTS suppression decision (testable without the adapter stack)."""
+ turn_key = streaming_tts_turn_key(session_key, turn_marker, event=event)
+ return bool(turn_key and turn_key in completed_turns)