Skip to content

fix(telegram): truncate mid-stream overflow instead of splitting - #48663

Closed
liuhao1024 wants to merge 2 commits into
NousResearch:mainfrom
liuhao1024:fix/48648-telegram-overflow
Closed

fix(telegram): truncate mid-stream overflow instead of splitting#48663
liuhao1024 wants to merge 2 commits into
NousResearch:mainfrom
liuhao1024:fix/48648-telegram-overflow

Conversation

@liuhao1024

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes an infinite message duplication loop in the Telegram adapter when streamed content exceeds the 4096 UTF-16 code-unit limit. During streaming (finalize=False), the pre-flight overflow check unconditionally called _edit_overflow_split, which spawned continuation messages and updated the active message_id. On the next token, the full accumulated text (still > 4096) was edited to the new message, triggering another split — creating an infinite chain of duplicate replies.

The fix: when finalize=False and content exceeds the limit, truncate the preview to 4000 code-units (safe margin below 4096) and fall through to the normal edit path. The real split only happens when finalize=True (stream complete).

Related Issue

Fixes #48648

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • gateway/platforms/telegram.py: In edit_message(), when finalize=False and content exceeds MAX_MESSAGE_LENGTH, truncate to 4000 code-units instead of calling _edit_overflow_split. The split path is preserved for finalize=True.
  • tests/gateway/test_telegram_overflow_truncation.py: 3 regression tests — mid-stream truncation, finalize-True split, and under-limit passthrough.

How to Test

  1. Run python -m pytest tests/gateway/test_telegram_overflow_truncation.py -v — all 3 tests should pass
  2. On Telegram: start a streaming response that grows past 4096 chars. Before this fix, you'd see infinite nested replies. After: the preview truncates at 4000 chars and the final message splits cleanly.

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Code Intelligence

⚠️ GitNexus unavailable — grep-based fallback used.

  • Analyzed: gateway/platforms/telegram.py::edit_message (callers: stream_consumer, gateway run.py)
  • Blast radius: LOW — only affects mid-stream overflow path; finalize-True and under-limit paths unchanged
  • Related patterns: _edit_overflow_split at L2804 handles chunked delivery on finalize; this fix defers to it

When streamed content exceeds Telegram's 4096 UTF-16 code-unit limit,
edit_message() unconditionally called _edit_overflow_split — even during
streaming (finalize=False).  The split spawns continuation messages and
updates the active message_id; the next token edit carries the full
accumulated text (still > limit), triggering another split, creating an
infinite chain of duplicate replies.

Fix: when finalize=False, truncate the preview to 4000 code-units (safe
margin below 4096) and fall through to the normal edit path.  The real
split only fires on finalize=True (stream complete).

Fixes NousResearch#48648
@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 18, 2026
The PR changes mid-stream overflow from splitting into continuations to
truncating the preview (to prevent infinite reply chains). Update the
existing tests in test_telegram_format.py to match the new behavior:
- finalize=False: truncate, no continuations
- finalize=True: split into continuations with topic metadata
@teknium1

Copy link
Copy Markdown
Contributor

Closing as superseded by #51736 (merged to main in 73a20a6ad), which fixed #48648 by gating the overflow split on finalize. Your fix targeted gateway/platforms/telegram.py, which no longer exists — the Telegram adapter was relocated to plugins/platforms/telegram/adapter.py in a bundled-plugin refactor, so the change landed there instead. The salvaged PR (#50408 by @Tranquil-Flow) hit the current path. Thanks for the fix and the report — credited in the issue.

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 type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(telegram): Infinite streamed message duplication loop during 4096-char overflow

3 participants