fix(telegram): complete 3-layer defense against invalid thread_id in non-forum DMs - #3283
Closed
MacroAnarchy wants to merge 1 commit into
Closed
fix(telegram): complete 3-layer defense against invalid thread_id in non-forum DMs#3283MacroAnarchy wants to merge 1 commit into
MacroAnarchy wants to merge 1 commit into
Conversation
…non-forum DMs Layer 1 — Source: Drop spurious message_thread_id in _build_message_event() when no forum topic was resolved. Telegram sets this field on reply chains in non-forum DMs, but it cannot be used as message_thread_id when sending. Layer 2 — Send fallback: When send() gets 'Message thread not found', clear effective_thread_id and retry immediately instead of retrying 3x with the same broken value. Catches both Exception and NetworkError paths. Layer 3 — Progress messages: Make the event_message_id fallback in _progress_thread_id platform-aware. Only Slack/Discord use message IDs as thread identifiers. Telegram uses dedicated forum topic IDs — falling back to a regular message ID causes 'Message thread not found' on every progress update. Without Layer 3, progress/status messages in run.py re-introduce invalid thread IDs even when Layers 1-2 correctly handle the send path. Fixes NousResearch#3206
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Comprehensive fix for Telegram DM sends failing with
Message thread not foundwhen replying to messages in non-forum chats. Addresses the root cause at three layers for defense-in-depth.Related Issue
Fixes #3206
Type of Change
🐛 Bug fix (non-breaking change that fixes an issue)
Root Cause
Telegram sets
message_thread_idon incoming reply-chain messages in non-forum DMs. This value is not valid for outgoingsend_message()calls — Telegram rejects it withBadRequest: Message thread not found. The error cascades through streaming, progress messages, and all send types, causing 1000+ errors per session.Changes Made — 3 Layers
Layer 1 — Source (
telegram.py: _build_message_event)Drop spurious
message_thread_idwhen no forum topic was resolved. Prevents invalid thread IDs from entering the system.Layer 2 — Send fallback (
telegram.py: send)When
send_message()gets "Message thread not found", cleareffective_thread_idand retry immediately — instead of retrying 3x with the same broken value. Catches bothExceptionandNetworkErrorpaths.Layer 3 — Progress messages (
run.py: _progress_thread_id)The
event_message_idfallback in_progress_thread_idwas designed for Slack (where message IDs are valid thread identifiers). On Telegram, this re-introduces invalid thread IDs even when Layers 1-2 correctly handle the send path. Now platform-aware:source.thread_id or event_message_id(correct for their threading model)source.thread_idonly (no message ID fallback)Without Layer 3, the bug persists — progress/status messages bypass the telegram.py fixes entirely via
run.py.Why this supersedes #3207 and #3218
run.pyprogress message path that re-introduces invalid thread IDs from a Slack-specific fallbackThis PR combines all three layers into a single coherent fix.
How to Test
message_thread_id)Checklist