telegram: split long replies on rune boundaries, not 4000 bytes #1084

Closed
opened 2026-10-27 13:38:57 +00:00 by sam · 0 comments
Owner

splitLongMessage slices line[i:end] on byte offsets (messageMaxLen = 4000 bytes). Any line ≥ 4001 bytes containing multi-byte UTF-8 cuts mid-rune (e.g. 1334 × '界': parts[0] ends on a lead byte, parts[1] starts with continuation bytes). finalize feeds these parts to EditMessageText, and on edit rejection the fallback re-splits the same content into the same invalid chunks — the authoritative reply arrives mangled or not at all for exactly the long, non-ASCII replies the split path exists to serve. All existing tests use ASCII input only.

Failure: a model produces a long answer with CJK/emoji or a base64 blob crossing a 4000-byte boundary; Telegram rejects the malformed part and the user gets the stale live frame plus a corrupted reply.

Design

Split on rune boundaries (iterate runes, accumulate up to messageMaxLen runes). Add a test with multi-byte input crossing the boundary asserting every part is valid UTF-8 and no part exceeds the bound.

Acceptance criteria

A long CJK/emoji/base64 reply splits into valid, bounded parts; test covers non-ASCII content crossing the boundary.

Close reason

Fixed in internal/channels/telegram/live.go and telegram.go: finalize/abandon strip context cancellation before sending (mirroring each other), ToolCall escapes Markdown metacharacters via bot.EscapeMarkdown, splitLongMessage splits on rune boundaries. All new/changed tests verified red-then-green; task check clean; adversarial review pass found no blocking issues.


Imported from beads archie-core-5efv · GitHub #482 · type bug · priority P1 · created 2026-08-14 · closed 2026-08-14

splitLongMessage slices line[i:end] on byte offsets (messageMaxLen = 4000 bytes). Any line ≥ 4001 bytes containing multi-byte UTF-8 cuts mid-rune (e.g. 1334 × '界': parts[0] ends on a lead byte, parts[1] starts with continuation bytes). finalize feeds these parts to EditMessageText, and on edit rejection the fallback re-splits the same content into the same invalid chunks — the authoritative reply arrives mangled or not at all for exactly the long, non-ASCII replies the split path exists to serve. All existing tests use ASCII input only. Failure: a model produces a long answer with CJK/emoji or a base64 blob crossing a 4000-byte boundary; Telegram rejects the malformed part and the user gets the stale live frame plus a corrupted reply. ### Design Split on rune boundaries (iterate runes, accumulate up to messageMaxLen runes). Add a test with multi-byte input crossing the boundary asserting every part is valid UTF-8 and no part exceeds the bound. ### Acceptance criteria A long CJK/emoji/base64 reply splits into valid, bounded parts; test covers non-ASCII content crossing the boundary. ### Close reason Fixed in internal/channels/telegram/live.go and telegram.go: finalize/abandon strip context cancellation before sending (mirroring each other), ToolCall escapes Markdown metacharacters via bot.EscapeMarkdown, splitLongMessage splits on rune boundaries. All new/changed tests verified red-then-green; task check clean; adversarial review pass found no blocking issues. --- _Imported from beads `archie-core-5efv` · GitHub #482 · type bug · priority P1 · created 2026-08-14 · closed 2026-08-14_
sam added the highbug labels 2026-10-27 13:38:57 +00:00
sam self-assigned this 2026-10-27 13:38:57 +00:00
sam closed this issue 2026-10-27 13:38:57 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sam/archie#1084