Skip to content

fix(telegram): complete 3-layer defense against invalid thread_id in non-forum DMs - #3283

Closed
MacroAnarchy wants to merge 1 commit into
NousResearch:mainfrom
MacroAnarchy:fix/telegram-thread-id-complete
Closed

fix(telegram): complete 3-layer defense against invalid thread_id in non-forum DMs#3283
MacroAnarchy wants to merge 1 commit into
NousResearch:mainfrom
MacroAnarchy:fix/telegram-thread-id-complete

Conversation

@MacroAnarchy

Copy link
Copy Markdown
Contributor

What does this PR do?

Comprehensive fix for Telegram DM sends failing with Message thread not found when 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_id on incoming reply-chain messages in non-forum DMs. This value is not valid for outgoing send_message() calls — Telegram rejects it with BadRequest: 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_id when 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", 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 (run.py: _progress_thread_id)

The event_message_id fallback in _progress_thread_id was 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:

  • Slack/Discord: source.thread_id or event_message_id (correct for their threading model)
  • Telegram & others: source.thread_id only (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

This PR combines all three layers into a single coherent fix.

How to Test

  1. Open a Telegram DM (non-forum chat) with the bot
  2. Reply to an existing message (creates a reply chain with message_thread_id)
  3. Trigger the bot to respond (streaming or normal)
  4. Verify: no "Message thread not found" errors in logs, messages send successfully

Checklist

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits
  • I searched for existing PRs before opening this one
  • Only 2 files changed, minimal diff, no unrelated changes

…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
@teknium1

Copy link
Copy Markdown
Contributor

The core issue (invalid thread_id causing 'Message thread not found' failures) is already fixed on main via PR #3390 (commit 41d9d08). That fix handles BadRequest at the send layer, clears thread_id, and retries. Thanks for the thorough defense-in-depth approach though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Telegram DM sends fail with 'Message thread not found' — spurious thread_id from reply chains

2 participants