The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): voice note merged during pending STT is transcribed
Maciej Lisiewski07139993ce6f
diff --git a/gateway/run_inbound.py b/gateway/run_inbound.pyindex 60e21baf5d9..b6c1dc22135 100644--- a/gateway/run_inbound.py+++ b/gateway/run_inbound.py@@ -1894,7 +1894,8 @@ class GatewayInboundMixin(GatewayInboundHooksMixin, GatewayPluginInjectionMixin)self, event, user_text: Optional[str] = None) -> tuple[str | None, list[str]]:"""Transcribe a pending audio event once and cache the result on the event: the interrupt- monitor and the pending-drain path both need it — one STT call and one echo per message."""+ monitor and the pending-drain path both need it — one STT call and one echo per message.+ ``user_text`` is a deliberate override of the caption; ``None`` reads ``event.text`` live."""if hasattr(event, "_gateway_pending_stt_text"):return event._gateway_pending_stt_text, list(getattr(event, "_gateway_pending_stt_transcripts", []) or [])audio_paths = self._pending_event_audio_paths(event)@@ -1902,6 +1903,12 @@ class GatewayInboundMixin(GatewayInboundHooksMixin, GatewayPluginInjectionMixin)return user_text if user_text is not None else (getattr(event, "text", None) or None), []text = user_text if user_text is not None else (getattr(event, "text", "") or "")enriched_text, successful_transcripts = await self._enrich_message_with_transcription(text, audio_paths)+ # A voice note merged in during the await dropped a cache that did not exist yet; re-run on+ # the merged media rather than publish a transcript that silently omits the new note.+ while self._pending_event_audio_paths(event) != audio_paths:+ audio_paths = self._pending_event_audio_paths(event)+ text = user_text if user_text is not None else (getattr(event, "text", "") or "")+ enriched_text, successful_transcripts = await self._enrich_message_with_transcription(text, audio_paths)event._gateway_pending_stt_text = enriched_textevent._gateway_pending_stt_transcripts = list(successful_transcripts)return enriched_text, successful_transcripts@@ -1927,11 +1934,13 @@ class GatewayInboundMixin(GatewayInboundHooksMixin, GatewayPluginInjectionMixin)) -> tuple[str, list[str]]:"""Transcribe a pending voice event and echo transcripts once → ``(enriched_text,transcripts)`` for ``agent.interrupt()`` or the pending-drain flow; ``(text, [])`` when there- is no STT-eligible media (caller owns the ``_build_media_placeholder`` fallback)."""+ is no STT-eligible media (caller owns the ``_build_media_placeholder`` fallback). ``text`` is+ only that fallback: the caption is read live from ``event.text`` so one merged in during+ the STT await is kept."""if not self._pending_event_audio_paths(event):return text, []try:- enriched_text, transcripts = await self._transcribe_pending_audio_event_once(event, text)+ enriched_text, transcripts = await self._transcribe_pending_audio_event_once(event)if metadata is _UNSET:metadata = self._thread_metadata_for_source(source, self._reply_anchor_for_event(event))await self._echo_pending_stt_transcripts_once(diff --git a/tests/gateway/test_telegram_voice_v0_regressions.py b/tests/gateway/test_telegram_voice_v0_regressions.pyindex 9bad21eda0f..1b59fbaef8e 100644--- a/tests/gateway/test_telegram_voice_v0_regressions.py+++ b/tests/gateway/test_telegram_voice_v0_regressions.py@@ -267,9 +267,9 @@ async def test_telegram_video_size_gate_rejects_oversized_media_before_download(assert "exceeds" in handled[0].text-def _voice_event(source, urls):+def _voice_event(source, urls, text=""):return MessageEvent(- text="",+ text=text,message_type=MessageType.VOICE,source=source,media_urls=list(urls),@@ -277,3 +277,77 @@ def _voice_event(source, urls):)+++async def test_voice_note_merged_during_pending_stt_is_transcribed():+ """A 2nd voice note merged into the pending event while the interrupt monitor's STT is in+ flight must not be dropped by the monitor publishing a transcript of the 1st note only."""+ from gateway.platforms.base import merge_pending_message_event++ runner = _runner()+ source = _source()+ stt_started = asyncio.Event()+ release = asyncio.Event()++ async def fake_enrich(text, paths):+ stt_started.set()+ await release.wait()+ transcripts = [f"<{Path(p).name}>" for p in paths]+ return text + " ".join(transcripts), transcripts++ runner._enrich_message_with_transcription = fake_enrich+ pending = {}+ merge_pending_message_event(pending, "k", _voice_event(source, ["/tmp/A.ogg"]))+ event = pending["k"]++ monitor = asyncio.create_task(runner._transcribe_pending_audio_event_once(event, ""))+ await stt_started.wait()+ merge_pending_message_event(pending, "k", _voice_event(source, ["/tmp/B.ogg"]))+ release.set()+ await monitor++ drain_text, drain_transcripts = await runner._transcribe_pending_audio_event_once(event, "")++ assert drain_transcripts == ["<A.ogg>", "<B.ogg>"]+ assert "<B.ogg>" in drain_text+++async def test_caption_of_voice_note_merged_during_pending_stt_is_kept():+ """The interrupt path hands over a snapshot of the caption; the merged note's caption lands on+ ``event.text`` during the STT await and must reach both the interrupt and the cached drain text."""+ from gateway.platforms.base import merge_pending_message_event++ runner = _runner()+ source = _source()+ stt_started = asyncio.Event()+ release = asyncio.Event()++ async def fake_enrich(text, paths):+ stt_started.set()+ await release.wait()+ transcripts = [f"<{Path(p).name}>" for p in paths]+ return " ".join(transcripts) + " | " + text, transcripts++ runner._enrich_message_with_transcription = fake_enrich+ pending = {}+ merge_pending_message_event(pending, "k", _voice_event(source, ["/tmp/A.ogg"], "first caption"))+ event = pending["k"]++ interrupt = asyncio.create_task(runner._transcribe_and_echo_pending_voice(+ event, None, source, event.text or "", log_context="Voice-priority-interrupt",+ ))+ await stt_started.wait()+ merge_pending_message_event(pending, "k", _voice_event(source, ["/tmp/B.ogg"], "second caption"))+ release.set()+ interrupt_text, _ = await interrupt++ drain_text, _ = await runner._transcribe_and_echo_pending_voice(+ event, None, source, event.text or "", log_context="Voice-drain",+ )++ for text in (interrupt_text, drain_text):+ assert "<B.ogg>" in text+ assert "first caption" in text+ assert "second caption" in text