Skip to content

The agent that grows with you

fix(voice): one Discord bot's transcript dedup no longer drops another bot's turn

John Paul Soliva9eaf235b7df8
diff --git a/gateway/run.py b/gateway/run.py
index c2733ae906d..8b3c0999361 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -3759,8 +3759,9 @@ class GatewayRunner(
self.hooks = ProfileHookRegistries()
# Per-chat voice reply mode: "off" | "voice_only" | "all"
self._voice_mode: Dict[str, str] = self._load_voice_modes()
- # Per-(guild,user) transcript dedup: the voice/STT pipeline can emit one utterance twice.
- self._recent_voice_transcripts: Dict[tuple[int, int], List[tuple[float, str]]] = {}
+ # Per-(bot, guild, text channel, user) transcript dedup: the voice/STT pipeline can emit one
+ # utterance twice.
+ self._recent_voice_transcripts: Dict[tuple, List[tuple[float, str]]] = {}
# Background tasks kept referenced so they are not garbage-collected mid-execution.
self._background_tasks: set = set()
# Event-loop liveness heartbeat: rewritten every 30s while the loop dispatches; supervisors use
diff --git a/gateway/run_voice.py b/gateway/run_voice.py
index 44b759e11e8..f04ee0a0f5e 100644
--- a/gateway/run_voice.py
+++ b/gateway/run_voice.py
@@ -211,13 +211,15 @@ class GatewayVoiceMixin:
profile=getattr(adapter, "_owner_profile", None))
self._apply_voice_mode(adapter, key, chat_id, "off")
- def _is_duplicate_voice_transcript(self, guild_id: int, user_id: int, transcript: str) -> bool:
+ def _is_duplicate_voice_transcript(self, binding: tuple, user_id: int, transcript: str) -> bool:
"""Suppress repeated STT outputs for one recent utterance (voice capture can emit it twice a
- few seconds apart -> a second queued run and overlapping spoken replies)."""
+ few seconds apart -> a second queued run and overlapping spoken replies). ``binding`` is
+ (receiving bot's profile, guild, bound text channel): the store is runner-wide, and another
+ bot's copy of the utterance, or a new binding, is a separate turn."""
normalized = re.sub(r"[^\w\s]", "", re.sub(r"\s+", " ", transcript).strip().lower())
if not normalized:
return False
- now, key = time.monotonic(), (guild_id, user_id)
+ now, key = time.monotonic(), (*binding, user_id)
if not isinstance(recent_store := getattr(self, "_recent_voice_transcripts", None), dict):
recent_store = self._recent_voice_transcripts = {}
recent = [(ts, txt) for ts, txt in recent_store.get(key, []) if now - ts <= 12.0]
@@ -287,7 +289,8 @@ class GatewayVoiceMixin:
if not self._is_user_authorized_for_source(source):
logger.debug("Unauthorized voice input from user %d, ignoring", user_id)
return
- if self._is_duplicate_voice_transcript(guild_id, user_id, transcript):
+ binding = (getattr(adapter, "_owner_profile", None), guild_id, text_ch_id)
+ if self._is_duplicate_voice_transcript(binding, user_id, transcript):
logger.info("Suppressing duplicate voice transcript for guild=%s user=%s: %s",
guild_id, user_id, transcript[:100])
return
diff --git a/tests/gateway/test_voice_transcript_dedup_scope.py b/tests/gateway/test_voice_transcript_dedup_scope.py
new file mode 100644
index 00000000000..83621ccae73
--- /dev/null
+++ b/tests/gateway/test_voice_transcript_dedup_scope.py
@@ -0,0 +1,47 @@
+"""Voice-transcript dedup belongs to the bot that heard the utterance and the conversation it is bound to.
+
+The runner keeps one dedup store for every Discord bot it serves. Keyed by guild and speaker alone, a
+second bot (another profile) in the same guild dropped its own copy of an utterance because the first
+bot had just recorded it, and a rebind to another text channel inherited the old binding's history.
+"""
+
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock
+
+import pytest
+
+import gateway.run as gateway_run
+from gateway.config import Platform
+
+_GUILD, _USER = 1, 42
+
+
+class _Bot:
+ """What the voice callback reads from a Discord adapter (weakref-able)."""
+
+ def __init__(self, profile):
+ self._owner_profile = profile
+ self._voice_text_channels, self._voice_sources = {_GUILD: 700}, {}
+ self._client = SimpleNamespace(get_channel=lambda _id: None, get_guild=lambda _id: None)
+ self.handle_message = AsyncMock()
+
+
+async def test_dedup_is_scoped_to_the_receiving_bot_and_its_binding():
+ bot_a, bot_b = _Bot(None), _Bot("bot-b")
+ runner = object.__new__(gateway_run.GatewayRunner)
+ runner.adapters = {Platform.DISCORD: bot_a}
+ runner._voice_mode, runner._session_db, runner.session_store = {}, None, MagicMock()
+ runner._canonicalize = lambda source, **_kw: object()
+ runner._is_user_authorized = lambda source: True
+
+ async def say(bot, text="what broke on the ingest box"):
+ await runner._handle_voice_channel_input(_GUILD, _USER, text, adapter=bot)
+
+ await say(bot_a)
+ await say(bot_b)
+ await say(bot_a) # the same capture emitted twice: still suppressed
+ assert (bot_a.handle_message.await_count, bot_b.handle_message.await_count) == (1, 1)
+ bot_a._voice_text_channels[_GUILD] = 800 # /voice join from another text channel
+ await say(bot_a)
+ assert bot_a.handle_message.await_count == 2