#16501: feat(telegram): add inbound subagent queue with prefork progress
channel: telegram
stale
size: L
Cluster:
Telegram History and Features
## Summary
This PR adds a built-in Telegram inbound subagent queue with prefork-oriented dispatch and user-visible progress updates.
## Why
Before this change, Telegram inbound messages on the subagent path had no dedicated built-in queue worker and no clear lifecycle feedback.
Users could not reliably tell whether a message was queued, actively processing, or no longer running.
This was especially painful during development and operations: after service/container restarts, users had no clear signal to distinguish “still processing slowly” from “processing was interrupted and is no longer progressing.”
By adding a built-in queue worker with explicit queued/processing progress states and prefork dispatch, we improve both perceived responsiveness and operational clarity.
## Problem solved
1. Unclear waiting state for Telegram users (queued vs processing vs stalled).
2. Extra startup latency before subagent dispatch.
3. No dedicated per-chat/global concurrency control for this Telegram inbound subagent path.
4. Weak continuity for queued turns across follow-up processing.
## What changed
1. Add TelegramInboundSubagentQueue for Telegram inbound -> subagent execution.
2. Queue messages and dispatch subagent runs in prefork mode.
3. Add global/per-chat inflight limits for fairer and safer burst handling.
4. Add Telegram queue memory persistence to improve context continuity.
5. Add progress/status feedback for queued and processing states.
6. Add prefork timing test coverage.
## Benefits
1. Faster time-to-dispatch.
2. Better Telegram UX with clear processing feedback.
3. Improved robustness under traffic bursts.
4. Better context continuity for queued conversations.
5. Better observability via queue timing logs.
## Validation
- `pnpm build` passed
- `pnpm vitest src/telegram/inbound-subagent-queue.prefork.test.ts src/telegram/bot-message-dispatch.test.ts` passed
- `pnpm check` has unrelated existing baseline errors outside this change
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR introduces a Telegram inbound subagent queue with prefork dispatch and progress updates. The implementation adds a dedicated queue worker (`TelegramInboundSubagentQueue`) that manages inbound Telegram messages, dispatching them to subagents with improved UX through status updates and concurrency controls.
**Key changes:**
- New `TelegramInboundSubagentQueue` class with global/per-chat concurrency limits (3 each)
- Queue memory persistence in session store for better context continuity
- Prefork dispatch pattern - starts agent execution before sending reactions/status
- Live progress updates showing queued → processing states with typing indicators
- Test coverage for prefork timing behavior
**Implementation approach:**
- Messages are enqueued when gateway is reachable and valid messageId/body exist
- Queue uses `pump()` pattern to dispatch tasks respecting inflight limits
- Each task sends status message → loads memory → dispatches subagent → polls for completion → updates with final response
- Progress updates occur every 4s (typing heartbeat) with snippet from latest assistant reply
- Queue disabled in vitest (`process.env.VITEST === "true"`) to avoid affecting existing tests
<h3>Confidence Score: 4/5</h3>
- This PR is generally safe to merge with comprehensive test coverage and good error handling, though one edge case warrants attention.
- The implementation is well-structured with proper concurrency controls, error handling, and test coverage. The queue disabled in tests via environment check prevents disruption to existing test suite. One minor concern: the `pump()` method has a potential edge case where rapid concurrent calls could theoretically lead to redundant pump cycles, though the `pumping` flag mitigates this in practice.
- Pay close attention to `src/telegram/inbound-subagent-queue.ts` - review the pump() concurrency logic and ensure the inflight counter management is correct under all scenarios.
<sub>Last reviewed commit: e34c946</sub>
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#8166: fix(telegram): lifecycle fixes for duplicate messages and auto-reco...
by cheenu1092-oss · 2026-02-03
78.9%
#14367: feat(telegram): add message read via inbound message store
by michaelquinlan88 · 2026-02-12
78.2%
#15975: feat(telegram): add Business Mode for personal chat access
by scomma · 2026-02-14
76.9%
#21346: [AI-assisted] Telegram: add reaction state machine with fallback an...
by Archie818 · 2026-02-19
76.4%
#19800: fix(telegram): add INFO-level logging at inbound message drop paths
by katalabut · 2026-02-18
76.2%
#16102: Fix: Telegram Inline Button Support for Exec Approvals (builds on #...
by RoguePhoenix117 · 2026-02-14
75.9%
#3186: fix(telegram): sanitize update offset + lock polling
by daxiong888 · 2026-01-28
75.1%
#22095: feat: add Telegram inline buttons to exec approval requests
by AIflow-Labs · 2026-02-20
75.0%
#7751: Telegram: persist inbound history (SQLite) + add read action
by welttowelt · 2026-02-03
75.0%
#3045: [AI-Assisted] fix: preserve pending tasks when subagent completes
by sid1943 · 2026-01-28
74.8%