Concepts
Batch Routing ✅
Queue pendingBatch 500ms -> activeBatch alarms. Dedup, heartbeats, 30s stuck auto-recovery.
TL;DR: pendingBatch collects (non-blocking). 500ms alarm fires -> activeBatch processes. 5s heartbeats. 30s no-heartbeat -> recover.
Table of Contents
Dual-Batch Pattern
Non-blocking webhook -> pending (mutable). Alarm promotes -> active (immutable).
Key Timings
| Event | Delay | Purpose |
|---|---|---|
queueMessage | T+0ms | Webhook 200 OK |
onBatchAlarm | 500ms | Batch -> 1 LLM call |
| Heartbeat | 5s loop | UX liveness |
| Stuck Check | 30s | Auto-recovery |
Stuck Detection & Recovery
New msg checks now - lastHeartbeat > 30s -> Clear stuck active -> Promote pending.
Error: BatchStuckError (handled silently).
Code Snippets
State from cloudflare-agent.ts
Batch types batch-types.ts
Decision Tree:
Quiz: Why dual-batch?
- A: pending never blocks webhook ✅
Try It Yourself
bun run deploy:telegram- Spam 5 msgs fast
- Watch "Thinking..." -> batched response!
Related: Batching Alarms | Architecture