Skip to content

fix(gateway): format-then-chunk in Telegram overflow split - #43470

Closed
annguyenNous wants to merge 1 commit into
NousResearch:mainfrom
annguyenNous:fix/telegram-overflow-format-order
Closed

fix(gateway): format-then-chunk in Telegram overflow split#43470
annguyenNous wants to merge 1 commit into
NousResearch:mainfrom
annguyenNous:fix/telegram-overflow-format-order

Conversation

@annguyenNous

Copy link
Copy Markdown
Contributor

Fixes #43441

Problem

In Telegram bound/forum topics, final replies arrive showing raw Markdown markers literally. The root cause: _edit_overflow_split() chunks raw text first, then formats each chunk with MarkdownV2. The escaping inflates each chunk past the 4096 limit, Telegram rejects every formatted attempt, and the fallback delivers raw markers.

Fix

  • When finalize=True: format content first, then chunk (matching send() order)
  • Escape (1/N) chunk indicators for MarkdownV2
  • Strip MarkdownV2 escapes in all plain-text fallback paths

Testing

  • Syntax verified: ast.parse() passes
  • finalize=False path unchanged

_edit_overflow_split() chunked raw text first, then formatted each
chunk with MarkdownV2. The escaping inflated each chunk past the
4096 UTF-16 limit, Telegram rejected every formatted attempt with
MESSAGE_TOO_LONG, and the fallbacks delivered raw Markdown markers
literally.

Align with send() which formats first and then chunks the already-
formatted text. Continuation chunks and fallback paths now strip
MarkdownV2 escapes before sending as plain text.

Fixes NousResearch#43441
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter duplicate This issue or pull request already exists labels Jun 10, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #43458 — same fix (format with MarkdownV2 before chunking in _edit_overflow_split) for the same issue #43441. #43458 is the earliest open PR with this approach. See also #43463 (broader scope, also touches the stream-consumer cancellation path).

@liuhao1024

Copy link
Copy Markdown
Contributor

Code Review: Single-chunk fallback re-formats already-formatted content

Thanks for the PR! The format-then-chunk approach is correct — formatting raw text before chunking prevents MarkdownV2 escapes from inflating past MAX_MESSAGE_LENGTH.

However, there's a double-formatting issue in the single-chunk fallback path. When finalize=True and len(chunks) <= 1, the code does:

# Line 18: content is formatted once
formatted = self.format_message(content)
chunks = self.truncate_message(formatted, ...)

# Line 36: single-chunk fallback re-formats the ORIGINAL content
chunks = [content if not finalize else self.format_message(content)]

self.format_message(content) is called twice on the same input. The first call (line 18) formats the text and the chunks list already contains the formatted result. The single-chunk fallback then reformats the original content instead of reusing the already-formatted formatted variable.

For MarkdownV2, format_message() escapes literal characters (e.g. *\*). Double-formatting produces double-escaped output (e.g. \*\\*), which renders with visible backslashes in Telegram.

Fix: Use formatted instead of re-formatting:

if len(chunks) <= 1:
    chunks = [formatted if finalize else content]

This correctly reuses the already-formatted text when finalize=True, and the raw text when finalize=False (unchanged behavior).

@austinpickett

Copy link
Copy Markdown
Collaborator

Closing as duplicate — #43458 (liuhao1024) covers the same format-before-chunk fix for the Telegram overflow split and is already under review.

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 duplicate This issue or pull request already exists P2 Medium — degraded but workaround exists platform/telegram Telegram bot adapter type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Telegram bound topic final replies render raw Markdown instead of formatted text

4 participants