Skip to content

fix(telegram): split long messages and sanitize markdown instead of truncating#141

Merged
xlabtg merged 3 commits intoxlabtg:mainfrom
konard:issue-137-ecbdab2b6259
Apr 5, 2026
Merged

fix(telegram): split long messages and sanitize markdown instead of truncating#141
xlabtg merged 3 commits intoxlabtg:mainfrom
konard:issue-137-ecbdab2b6259

Conversation

@konard
Copy link
Copy Markdown

@konard konard commented Apr 4, 2026

Fixes #137

Root Causes & Fixes

1. Truncated responses (Priority 🔴)

Root cause: handlers.ts simply sliced messages longer than max_message_length (default 4096) with "...", discarding all content after the limit.

Fix: Replaced truncation with splitMessageForTelegram() — a new utility that splits at natural break points (paragraph → line → word boundary) while avoiding cuts inside fenced code blocks. Each part is sent as a separate message.

// Before (handlers.ts)
if (responseText.length > this.config.max_message_length) {
  responseText = responseText.slice(0, this.config.max_message_length - 3) + "..."; // ❌ loses content
}

// After
const parts = splitMessageForTelegram(sanitized, this.config.max_message_length);
for (const part of parts) { await bridge.sendMessage(...); } // ✅ all content delivered

2. Empty rectangles / broken formatting (Priority 🔴)

Root cause: Empty fenced code blocks (```python\n```) render as blank rectangles in Telegram. Unclosed fences cause everything after them to be misrendered.

Fix: New sanitizeMarkdownForTelegram() utility (applied before every send):

  • Removes empty code blocks
  • Appends missing closing ``` when fence count is odd
  • Normalizes 4+-backtick fences (unsupported by Telegram) to ```

New Files

File Purpose
src/telegram/message-splitter.ts splitMessageForTelegram(text, maxLength?)
src/telegram/sanitize-markdown.ts sanitizeMarkdownForTelegram(text)
src/telegram/__tests__/message-splitter.test.ts 10 unit tests
src/telegram/__tests__/sanitize-markdown.test.ts 13 unit tests

Modified Files

File Change
src/telegram/handlers.ts Replace truncation with split+sanitize pipeline
src/agent/tools/telegram/messaging/send-message.ts Apply sanitization before send
src/agent/tools/telegram/messaging/edit-message.ts Apply sanitization before convert+edit
src/telegram/__tests__/handlers.test.ts Update test for new splitting behaviour

Test Results

All 23 new tests pass. All 50 existing handlers.test.ts tests pass. Pre-existing failures (@teleton-agent/sdk package unbuilt, schema version mismatch) are unrelated to this PR.

Backward Compatibility

  • Short messages (≤ 4096 chars) take the same code path as before — single send, no change in behavior.
  • sanitizeMarkdownForTelegram is a no-op on well-formed markdown.
  • The max_message_length config option continues to control the per-part limit.

This PR was created automatically by the AI issue solver

konard and others added 2 commits April 4, 2026 11:27
Adding .gitkeep for PR creation (default mode).
This file will be removed when the task is complete.

Issue: xlabtg#137
…runcating

Fixes xlabtg#137

Root causes addressed:
1. Long responses (>4096 chars) were truncated with "..." — now split into
   multiple parts at natural break points (paragraph, line, word boundaries)
   while avoiding splits inside fenced code blocks.
2. Empty fenced code blocks rendered as blank rectangles in Telegram — now
   removed before sending.
3. Unclosed code fences caused Telegram to misrender the rest of the message —
   now auto-closed with a warning log entry.
4. 4+-backtick fences unsupported by Telegram — normalized to ```.

New files:
- src/telegram/message-splitter.ts  — splitMessageForTelegram()
- src/telegram/sanitize-markdown.ts — sanitizeMarkdownForTelegram()
- src/telegram/__tests__/message-splitter.test.ts  (10 tests)
- src/telegram/__tests__/sanitize-markdown.test.ts (13 tests)

Modified files:
- src/telegram/handlers.ts          — use split+sanitize instead of truncation
- src/agent/tools/telegram/messaging/send-message.ts  — apply sanitization
- src/agent/tools/telegram/messaging/edit-message.ts  — apply sanitization
- src/telegram/__tests__/handlers.test.ts — update truncation test for new behaviour

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@konard konard changed the title [WIP] 🐛 [Telegram] Truncated messages and empty gaps in chat responses fix(telegram): split long messages and sanitize markdown instead of truncating Apr 4, 2026
@konard konard marked this pull request as ready for review April 4, 2026 11:38
@konard
Copy link
Copy Markdown
Author

konard commented Apr 4, 2026

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $2.304122
  • Calculated by Anthropic: $2.304122 USD
  • Difference: $0.000000 (+0.00%)

📊 Context and tokens usage:

Claude Sonnet 4.6:

  • Max context window: 93.4K / 1M input tokens (9%)
  • Max output tokens: 20.6K / 64K output tokens (32%)

Total input tokens: 88.8K + 5.0M cached
Total output tokens: 20.6K output
Cost: $2.140765

Claude Haiku 4.5:

  • Context window: 630.1K / 200K tokens (315%)
  • Max output tokens: 4.2K / 64K output tokens (7%)

Total input tokens: 69.4K + 560.7K cached
Total output tokens: 4.2K output
Cost: $0.163357

🤖 Models used:

  • Tool: Anthropic Claude Code
  • Requested: sonnet
  • Main model: Claude Sonnet 4.6 (claude-sonnet-4-6)
  • Additional models:
    • Claude Haiku 4.5 (claude-haiku-4-5-20251001)

📎 Log file uploaded as Gist (1708KB)


Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard
Copy link
Copy Markdown
Author

konard commented Apr 4, 2026

✅ Ready to merge

This pull request is now ready to be merged:

  • CI workflows exist but were not triggered for this commit
  • No merge conflicts
  • No pending changes

Monitored by hive-mind with --auto-restart-until-mergeable flag

@xlabtg xlabtg merged commit 0427bf3 into xlabtg:main Apr 5, 2026
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.

🐛 [Telegram] Truncated messages and empty gaps in chat responses

2 participants