You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(telegram): port MarkdownV2-safe truncation helpers (chat@4.27.0)
The previous truncation in ``TelegramAdapter.truncate_message`` /
``truncate_caption`` called ``_truncate_to_utf16`` with a literal
``...`` ellipsis, which Telegram's MarkdownV2 parser rejects: ``.`` is
a reserved character; the slice can leave an orphan trailing ``\``;
and the slice can cut through a paired entity (``*bold*``,
`` `code` ``, ``[label](url)``) leaving it unclosed. Any MarkdownV2
message exceeding 4096 chars (or 1024 for captions) triggered
``Bad Request: can't parse entities``.
Port of upstream b9a1961 (chat@4.27.0) plus the streaming-chunk safety
trim from f46a6fb (chat#446):
- ``truncate_for_telegram(text, limit, parse_mode)``: MarkdownV2 path
uses an escaped ``\.\.\.`` ellipsis and walks back past unbalanced
entity delimiters or orphan backslashes before appending. Plain text
keeps the literal ``...``.
- ``find_unescaped_positions(text, marker)``: scans for unescaped
occurrences of an entity marker, accounting for arbitrary runs of
escape backslashes.
- ``ends_with_orphan_backslash(text)``: True when the trailing run of
``\`` has odd parity.
- ``_find_unescaped_positions_outside_code``: skips occurrences inside
fenced and inline code spans (Telegram treats markers there as
literal text).
- ``_trim_to_markdown_v2_safe_boundary``: best-effort backwards walk
past unbalanced delimiters / orphan backslashes / unmatched ``[``.
``truncate_message`` / ``truncate_caption`` now accept an optional
``parse_mode``; ``post_message`` / ``edit_message`` / ``send_document``
plumb it through. Plain-mode behaviour is unchanged (still UTF-16
aware).
Tests: 8 length-limit tests on the MarkdownV2 path (escaped ellipsis,
orphan backslash, unclosed bold / code / open bracket, all-special
input, balanced no-op, plain passthrough); 4 streaming-chunk safety
trims; 4 ``find_unescaped_positions`` tests; 5
``ends_with_orphan_backslash`` tests; 3 adapter-level integration
tests verifying ``truncate_message`` / ``truncate_caption`` dispatch
on parse_mode. Each docstring includes "What to fix if this fails:"
pointing at the relevant helper.
https://claude.ai/code/session_01FyMxQn2BEAzmwKS1GZczKj
0 commit comments