Skip to content

fix(telegram): advance reply anchor to latest message_id when batching stacked prompts - #43787

Closed
g0rdonL wants to merge 1 commit into
NousResearch:mainfrom
g0rdonL:fix/telegram-text-batch-reply-anchor-clobber
Closed

fix(telegram): advance reply anchor to latest message_id when batching stacked prompts#43787
g0rdonL wants to merge 1 commit into
NousResearch:mainfrom
g0rdonL:fix/telegram-text-batch-reply-anchor-clobber

Conversation

@g0rdonL

@g0rdonL g0rdonL commented Jun 10, 2026

Copy link
Copy Markdown

Problem

When a user sends multiple messages quickly ("stacked prompts"), the text batching code in _enqueue_text_event merges them into a single event but never updated existing.message_id — it stayed pinned to the first message's ID. The bot's reply then anchored to that first message rather than the most recent one, which appeared to the user as the bot replying to the wrong message.

The merge path at telegram.py:

else:
    if event.text:
        existing.text = ...  # text merged
    existing._last_chunk_len = chunk_len
    # ← message_id never updated here
    if event.media_urls:
        ...

Root Cause

_enqueue_text_event was designed for Telegram's 4096-char message splits (where consecutive chunks belong to the same logical message). For that case the original message_id is fine. But the same code path runs for any two rapidly-sent messages, and in that case the reply anchor should track the last message so the reply threads to what the user most recently sent.

Fix

One line: update existing.message_id to the incoming event's message_id when merging, guarded by truthiness so a missing id is never clobbered with None.

if event.message_id:
    existing.message_id = event.message_id

Test

Added test_stacked_prompts_reply_anchor_advances_to_last_message to the existing test_telegram_text_batching.py suite. It reproduces the exact scenario — two events with distinct message_ids batched within the window — and asserts:

  1. Both texts are merged.
  2. The dispatched event carries the last message_id as the reply anchor.

All 7 tests in the file pass.

…g stacked prompts

When a user sends multiple messages quickly (stacked prompts), the text
batching code in _enqueue_text_event merges them into a single event but
never updated existing.message_id — it stayed pinned to the *first*
message.  The bot's reply then anchored to that first message instead of
the most recent one, which looked like a wrong-reply bug.

Fix: when merging a later event into an existing batch, update
existing.message_id to the incoming event's message_id (guarded by
truthiness so a missing id is not clobbered with None).

Add a regression test that reproduces the exact scenario: two stacked
prompts with distinct message_ids — asserts both texts are merged and
the dispatched event carries the last message_id as the reply anchor.
@alt-glitch alt-glitch added type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter P2 Medium — degraded but workaround exists labels Jun 10, 2026
@liuhao1024

Copy link
Copy Markdown
Contributor

Code Review Verification

Reviewed the diff (4 lines production + 35 lines test). The fix is correct and well-scoped:

  • Logic: When a user sends stacked prompts (two distinct messages within the batch window), existing.message_id now advances to the latest message. Previously the reply anchor stayed on the first message, causing the bot reply to appear under the wrong user message.
  • Scope: Only affects the batching path in _enqueue_text_event — single messages and Telegram chunk-splits are unaffected.
  • Test: test_stacked_prompts_reply_anchor_advances_to_last_message creates two events with distinct message_id values, verifies both texts merge and the dispatched event carries the last message_id. Assertions are specific and include a failure message.

No issues found. Clean fix with good test coverage.

@teknium1

Copy link
Copy Markdown
Contributor

Thanks for the focused regression test and narrow fix. The underlying behavior is still present on current main: plugins/platforms/telegram/adapter.py:7642-7650 merges text without advancing existing.message_id, while gateway/platforms/base.py:99-107 uses that event ID as the Telegram reply anchor.

Problems

  • The production hunk targets gateway/platforms/telegram.py, which was moved to plugins/platforms/telegram/adapter.py by 5600105478ffde29d7566b45421b100eaa29c4ef. The current live adapter therefore is not changed by this PR as written.

Suggested changes

  • Port the guarded existing.message_id = event.message_id assignment into plugins/platforms/telegram/adapter.py after the text merge at line 7646; the added regression test can remain in tests/gateway/test_telegram_text_batching.py.

Automated hermes-sweeper review.

@teknium1 teknium1 added sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform labels Jul 14, 2026
@g0rdonL

g0rdonL commented Jul 19, 2026

Copy link
Copy Markdown
Author

Closing in favor of #67306, which ports this fix to plugins/platforms/telegram/adapter.py per your review above — the module moved in 5600105 and this PR's diff no longer touched the live code path.

@g0rdonL g0rdonL closed this Jul 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists platform/telegram Telegram bot adapter sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants