Duyetbot Agent
Concepts

Batching & Alarms

Dual-batch prevents blocking. 500ms window collects msgs. 5s heartbeat rotations. 30s stuck -> auto-recovery.

TL;DR: pendingBatch collects (non-blocking). 500ms alarm -> activeBatch processes. 5s heartbeats. 30s no-beat -> recover.

Table of Contents

Dual-Batch

+---------------------+
| Webhook Msg         |
+---------------------+
           |
           v
+------------------------+
| pendingBatch           |
| - collecting           |
| - msg1, msg2...        |
+------------------------+
           |
           v
+--------------------+-----+
| 500ms Alarm Fires?  | No  |
+----------+----------+-----+
           | Yes             |
           v                 |
+------------------------+   |
| activeBatch = pending |   |
| pendingBatch = empty  |   |
+------------------------+   |
           |                 |
           v                 |
+----------------------+     |
| processBatch()       |     |
+----------------------+     |
           |                 |
           v                 |
+------------------------+   |
| 5s Heartbeat Loop     |   |
| Edit "Thinking..."    |   |
+------------------------+   |
                            |
                            v
+------------------------+   |
| Response Ready         |   |
| Edit Final Response    |   |
+------------------------+   |
           |                 |
           v                 |
+----------+----------+-----+ |
| New Msg? | Check Stuck?    |
+----------+----------+-----+ |
           | No      | Yes  | |
           v         v      | |
      Process Normally  Recover |
                            | |
                            v |
                      Ready!   |

Timings

EventDelayPurpose
queueMessageT+0msWebhook returns 200
onBatchAlarm500msBatch msgs -> 1 LLM
Heartbeat5s loopLiveness + UX
Stuck Check30sAuto-recovery

Webhook <6ms exit! DO independent.

Stuck Recovery

New msg checks: now - lastHeartbeat > 30s -> Clear stuck -> Process new.

Code Snippet

From architecture.md

pendingBatch: collecting (always open)
activeBatch: processing (immutable)
Stuck? -> pending promotes automatically

Quiz: Why dual-batch?
A: pending never blocks new msgs ✅

Related: Architecture -> | DOs ->

Deploy: bun run deploy:telegram -> Spam msgs -> See batching!

On this page