Repository navigation
fix(telegram): deliver long answers whole, keep code blocks intact, survive rate limits - #157
Conversation
…res rendered HTML The Markdown to Telegram HTML converter mangled common model output, and there was no way to split a long answer without breaking it. Converter (markdownToTelegramHtml): - Emphasis is paired by the CommonMark delimiter rules instead of regexes. A delimiter needs a non-space character on its inside edge, `_` needs a word boundary on its outside edge, and backslash escapes are honoured. `2 * 3 * 4`, `* item` bullets, `snake_case_name` and `\*x\*` stay literal. Mis-nested markers (`**bold _both** tail_`) come out as well-formed HTML instead of crossed tags. - A single word in double underscores (`__init__`, `__main__`) stays literal. It cannot be told apart from `__bold__`, which therefore is no longer bold; `__two words__` still is. - `2*3*4` and `2**10` between numbers stay literal. - Code fences are read line by line. A block ends only on a line of backticks, so backticks inside a code line no longer end it; a longer fence can hold a shorter one; a fence that is never closed runs to the end of the text, so a code block renders as code while it streams. - Inline code can use more than one backtick. - Table cells get inline formatting and a `|` inside inline code or after a backslash no longer splits the cell. Telegram shows neither bold nor links inside <pre>, so each link in a cell is repeated under the table. Splitter (new): splitMarkdownForTelegram, takeMarkdownChunk and resumeMarkdownAt split the markdown source so that each chunk's rendered HTML, and its source, fit one Telegram message. A chunk ends at the last line break that fits. A code block that spans chunks is closed and reopened with the same opening line; a table repeats its header. A single line too long for a message is cut inside the line, at a space if there is one, never inside a surrogate pair. Chunks record the source range they cover, so concatenating them gives back the text exactly. splitMessage is unchanged except that it no longer cuts a surrogate pair.
Three ways an answer reached Telegram incomplete: 1. Text was cut at 4096 characters. The markdown was split at 3800 source characters, converted, and the HTML truncated to fit; escaping and tags push a chunk past the limit, so lines were silently dropped. The stream committed a prefix by a ratio estimate, lost lines and started the next message mid-line. Now the markdown is split so that each chunk's rendered HTML fits, and nothing is truncated. The stream closes a message at the last line break that fits and continues the next one from that exact offset. 2. A code block split across messages rendered as prose, because the converter needs both fences in one chunk. The splitter closes the block at the end of a chunk and reopens it with the same opening line. A line longer than the limit inside a block is cut inside the line. 3. After one HTTP 429 during streaming, every later delta retried at once (the throttle only counted successful edits) and the final edit failed silently, leaving the message at whatever it showed. Now a rate limit is recorded and nothing is sent before Telegram's retry-after has passed; the throttle counts failed attempts too. The last edit of a message waits for the retry-after, 30 seconds at most, and tries again. If the message still cannot be edited, the text it does not show is posted as a new message. Posting waits out a rate limit the same way instead of dropping the message. Edits are spaced 1100 ms apart (was 600), and 3100 ms in groups: the Chat SDK adapter's own floors. An edit Telegram refuses as "message is not modified" counts as delivered instead of falling back to plain text. postTelegramHtml and handleTelegramHtmlStream take an optional clock and sleep, so tests do not wait.
Telegram's <pre> may not contain other entities, and a message it refuses falls back to raw markdown for the whole chunk. Cells are read as inline markdown, so the markers are gone, but nothing is tagged inside the box. Links are still repeated under the table.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 78e4772cab
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Delivery - A rate limit is waited out for up to 90 s per answer (was 30 s per call). Telegram is never called inside a wait it asked for. Sends and edits keep separate waits, and the wait for sends is remembered per chat. - After a chunk that a rate limit holds back, later chunks are not sent; the loss is logged once with its size. - A stream that goes quiet shows the text it has when the next edit is due, instead of when the next piece arrives. - The final edit keeps the same distance from the previous one as any other. - No edit for a change that is only trailing whitespace. - A stream that fails still delivers what arrived. - The thread used for boot, cron and sub-agent notices throws when Telegram refuses a call, so retries and the plain-text fallback apply to it too. Splitting - No chunk without visible text: a code block whose first line is too long is cut inside that line, and blank lines before a long line stay with it. - A cut before a half-written table row (streaming) repeats the header. Rendering - Lines ending in CR LF. - Bold next to punctuation with a letter after it (`**Note:**text`, CJK). - Table rows with more cells than the header keep them. Tests: the fake Telegram now refuses what Telegram refuses (empty text, bad HTML, over-long text, unchanged edits).
Independent review: outcomeA second reviewer read the branch and probed it with its own fuzzers and a stricter fake of the Bot API. No blockers; every should-fix finding is fixed, each with a test that fails on the code as it was.
Two of my own fixes were wrong at first and the reviewer's fuzzers caught them before the push: treating a half-written row as a row in the converter made very wide tables unsplittable, and a chunk could consist of a line break alone. Both have tests now. Also added: a stream piece that ends in the middle of a surrogate pair is not sent until the other half arrives. Still not handled, and listed in the plan: Telegram's limit on formatted spans per message (unverified), the growing per-piece cost of very long one-character streams, and telling the owner in chat when a rate limit made roundhouse give up on part of an answer. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 901a97ae07
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
… back Posted answers already stopped at the first chunk that was held back; a streamed one went on with the next bubble once Telegram accepted messages again, so the reader got the answer with a hole in it. Now nothing is sent after the part that was held back, the stream is read to its end, and the loss is logged once with its size.
Why
Four defects in how agent output reaches Telegram, each reproduced with a failing test before it was fixed.
truncateHtmlSafe. HTML escaping and tags can push a chunk past the limit, so the cut removed text. An 11,000-character answer with a 140-line code block lost code lines on both the posting and the streaming path.2 * 3 * 4,*bullets with emphasis,__init__, mis-nested emphasis,\*escapes, and formatting inside table cells.What changes
splitMarkdownForTelegramsplits the source so each chunk's rendered HTML fits. Nothing is truncated. A fenced block that spans chunks is closed at the end of one and reopened with the same opening line in the next; a table repeats its header. A single line longer than the limit is hard-split, never dropped. Surrogate pairs and entities are not cut.<pre>may not contain other entities); links in cells are repeated under the table.Behaviour changes to know
__init__) stays literal;__two words__is still bold. The two cannot be told apart otherwise.\.in prose loses its backslash, a consequence of honouring escapes.Verification
npm test: 882 passing on this branch (withmainmerged in).npm run typecheck: clean.After the independent review (see the outcome comment below): an answer waits up to 90 s for rate limits and Telegram is never called inside a wait it asked for; a quiet stream shows its text when the next edit is due; no chunk is sent without visible text; the fake Telegram in the tests refuses what Telegram refuses.