#19141: fix(telegram): serialize per-chat sends and await block reply flush before tools
channel: telegram
agents
size: XS
Cluster:
Tool Result Handling Improvements
## Problem
When an agent sends text (block reply) followed by a tool-initiated send (e.g., message tool sending a PDF), messages can arrive out of order on Telegram.
## Root Cause
1. **No per-chat send ordering**: sendMessageTelegram calls from block replies and the message tool fire concurrently
2. **Block reply buffer not flushed before tools**: The coalescer holds buffered text when a tool starts
## Fix
### Per-chat send serializer (src/telegram/send.ts)
- chatSendChains Map keyed by accountId:chatId
- serializeChatSend chains concurrent sends via promise chaining
- Wraps sendMessageTelegram for FIFO ordering per chat
### Await block reply flush (src/agents/pi-embedded-subscribe.handlers.tools.ts)
- flushBlockReplyBuffer() + await onBlockReplyFlush() before tool execution
- Ensures pending text is delivered before tool-initiated sends
## Changes
- src/telegram/send.ts — per-chat send serializer (37 lines)
- src/agents/pi-embedded-subscribe.handlers.tools.ts — flush + await (3 lines)
Most Similar PRs
#22886: fix: await onBlockReplyFlush before tool execution to preserve mess...
by botverse · 2026-02-21
76.5%
#19518: fix: await block reply flush before tool execution
by katalabut · 2026-02-17
71.5%
#15920: fix: await coalescer flush before tool execution
by Bridgerz · 2026-02-14
67.2%
#22994: fix(gateway): flush text buffer before tool events to prevent data ...
by youngkent · 2026-02-21
66.9%
#15996: fix(agents): messages arrive out of order — tool output beats narra...
by yinghaosang · 2026-02-14
66.7%
#9171: Fix: Route tool result deliveries through BlockReplyPipeline for pr...
by vishaltandale00 · 2026-02-04
65.8%
#16275: fix(agents): prevent rapid-fire duplicate messages during tool exec...
by heyhudson · 2026-02-14
65.4%
#19102: Fix Telegram per-message link preview override
by Clawborn · 2026-02-17
64.6%
#19235: fix(telegram): tool error warnings no longer overwrite streamed rep...
by gatewaybuddy · 2026-02-17
64.2%
#5681: fix(gateway): flush text buffer before tool events in webchat
by MaudeBot · 2026-01-31
63.2%