Skip to content

The agent that grows with you

fix(discord): pre-seed the starter dedup before awaiting mark_async

kshitijk4poorb9ec39939b97
diff --git a/plugins/platforms/discord/adapter.py b/plugins/platforms/discord/adapter.py
index 9b6f3e18915..12a7aab1fa9 100644
--- a/plugins/platforms/discord/adapter.py
+++ b/plugins/platforms/discord/adapter.py
@@ -6006,11 +6006,13 @@ class DiscordAdapter(DiscordMediaMixin, BasePlatformAdapter):
parent_channel_id = str(message.channel.id)
is_thread = True
thread_id = str(thread.id)
- auto_threaded_channel = thread
- await self._threads.mark_async(thread_id)
# Pre-seed dedup: message.create_thread() fires a second MESSAGE_CREATE for the
# starter (id == thread.id, maybe type=default); mark it so it can't trigger a rerun.
+ # Must run before the first await below: mark_async yields to the loop, and the
+ # echo's _discord_message_admission would otherwise claim the id first.
self._dedup.is_duplicate(str(thread.id))
+ auto_threaded_channel = thread
+ await self._threads.mark_async(thread_id)
else:
# Auto-threading is the routing target; do NOT fall back to an inline parent-channel
# reply (dumps the task into a shared channel). Surface an error and skip the run.
diff --git a/tests/gateway/test_discord_double_dispatch.py b/tests/gateway/test_discord_double_dispatch.py
index d326f6d21ca..c5108cad819 100644
--- a/tests/gateway/test_discord_double_dispatch.py
+++ b/tests/gateway/test_discord_double_dispatch.py
@@ -172,6 +172,10 @@ class TestThreadStarterDedup:
monkeypatch.setattr(adapter, "_auto_create_thread", fake_auto_create_thread)
+ async def fake_mark_async(tid): # first yield point after thread creation
+ assert str(tid) in adapter._dedup._seen, "dedup pre-seed must precede await mark_async"
+ monkeypatch.setattr(adapter._threads, "mark_async", fake_mark_async)
+
# 1) Original user message arrives → triggers thread creation + dispatch
user_msg = _make_message(msg_id=42, channel=channel, content="hello bot")
await adapter._handle_message(user_msg)