fix(bot): coalesce Telegram-split messages into a single prompt#180
Open
8x22b wants to merge 1 commit into
Open
fix(bot): coalesce Telegram-split messages into a single prompt#1808x22b wants to merge 1 commit into
8x22b wants to merge 1 commit into
Conversation
Telegram delivers long text (and a single paste) as several consecutive message updates, which the bot dispatched as separate prompts - one agent run per chunk. Buffer plain-text prompts per chat for MESSAGE_MERGE_WINDOW_MS (default 1500; 0 disables). Each new chunk restarts the window; when it elapses, buffered texts are joined and dispatched as one prompt. Commands and active interaction flows (question/task/rename/catalog) are unaffected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Telegram delivers long text — and a single paste — as several consecutive message updates (the ~4096-char client split, plus some clients splitting one input into multiple messages). The bot dispatches each update as its own prompt, so one long message becomes several independent agent runs and the conversation breaks.
Fix
Buffer plain-text prompts per chat for a short window (
MESSAGE_MERGE_WINDOW_MS, default1500;0disables). Each new chunk restarts the window; once it elapses with no new chunk, the buffered texts are joined (\n\n) and dispatched as a singlesession.prompt.Only the final plain-text fallback path is buffered. Commands and active interaction flows (question answers, task/model-search/rename input, command/skill arguments) keep being handled immediately and are never buffered.
Because the window restarts on every chunk, messages sent a few seconds apart are still processed independently.
Config
MESSAGE_MERGE_WINDOW_MS15000disables merging and processes every message immediatelyTradeoffs
\n\n. For a 4096-char split this adds one blank line per boundary (negligible in a long prompt); for genuinely separate quick messages it reads as separate paragraphs. Easy to change the separator if a maintainer prefers.Tests
New
tests/bot/handlers/message-merger.test.tscovers: disabled window (immediate), single buffered message, multi-chunk merge with window restart, gap-beyond-window flushes separately, per-chat independence, manualflushPendingPrompt, and latest ctx used on merge. The merger state is also reset intests/helpers/reset-singleton-state.tsso timers can't leak between tests.npm run lint,npm run build,npm test(1095 tests) pass. No OS-specific code.