-
-
Notifications
You must be signed in to change notification settings - Fork 52.7k
Description
Description
Block streaming replies can arrive out of order in Telegram (and likely other providers). The first paragraph of a response may appear after subsequent paragraphs.
Steps to Reproduce
- Enable block streaming (default is on)
- Send a message that triggers a multi-paragraph response
- Observe that paragraphs may arrive in wrong order
Example
Agent generates:
Clawdbot. It's a tool built into the gateway.
Linux has similar concepts (background jobs with `&`, `nohup`, `screen`, `tmux`) but the `process` tool is Clawdbot's own abstraction that:
1. Wraps bash commands in managed sessions
2. Tracks them by sessionId
...
Telegram receives:
- "Linux has similar concepts..." (paragraph 2+)
- "Clawdbot. It's a tool built into the gateway." (paragraph 1)
The first paragraph arrived last.
Root Cause Analysis
In src/auto-reply/reply/agent-runner.ts around line 309, each block reply is wrapped in an async task:
const task = (async () => {
if (!isHeartbeat) {
await typing.startTypingOnText(cleaned); // Variable delay based on text length
}
await opts.onBlockReply?.(blockPayload);
})()These tasks are added to pendingBlockTasks with void (fire-and-forget), meaning they run in parallel:
pendingBlockTasks.add(task);
void task.finally(() => pendingBlockTasks.delete(task));If Task 1 (first paragraph) has a longer typing delay than Task 2 (second paragraph), Task 2 may call sendBlockReply first, adding itself to the dispatcher chain before Task 1.
While the dispatcher uses a promise chain for serialization, the order tasks enter the chain depends on when they finish their typing delay — not when they were created.
Proposed Fix
Serialize block reply tasks instead of running in parallel:
// Instead of parallel fire-and-forget:
pendingBlockTasks.add(task);
void task.finally(...);
// Use sequential chaining:
blockReplyChain = blockReplyChain.then(() => task);Or remove the typing delay from the critical path for block replies.
Environment
- Clawdbot version: latest main
- Provider: Telegram (likely affects all providers with block streaming)
- Block streaming: enabled (default)
Workaround
Disable block streaming in config:
{
"agent": {
"blockStreamingDefault": "off"
}
}