Summary
TelegramChannel at 1516 lines combines Telegram API client, message formatting, option parsing, sub-agent tree formatting, message queue with backpressure, topic management, polling loop, callback routing, and progress card rendering.
Details
- File:
src/channels/telegram.ts (1516 lines)
- Severity: High
- Duplicate HTML escaping functions:
esc(), bold(), code(), italic() defined identically in both telegram.ts (lines 118-135) and telegram-style.ts (lines 18-35). Local definitions shadow imports.
- Global mutable
rateLimitUntil (line 65) shared across all instances — breaks multi-instance and test isolation
pollLoop is fire-and-forget with no shutdown guarantee — can process updates 10s after destroy()
- Poll loop has no backoff on persistent errors — hammers Telegram API every 5s forever
tgApi retries non-transient 4xx errors (400 Bad Request, 403 Forbidden) unnecessarily
preTopicBuffer never evicted for orphaned sessions (sessions where topic creation fails)
- In-flight counter race condition on concurrent sends
- Message truncation inside HTML tags produces broken markup
- Hardcoded Italian button labels ("Chiudi", "Riavvia", etc.)
md2html can double-escape URLs with ampersands
Suggested Fix
Split into:
telegram-api.ts — tgApi + rate limiting
telegram-format.ts — expand existing telegram-style.ts to cover all formatting
telegram-queue.ts — message queuing, backpressure, read grouping
- Remove duplicate escaping functions, use imports from telegram-style.ts
- Move
rateLimitUntil to instance property
- Add exponential backoff + max retries for poll loop
- Add TTL-based eviction for preTopicBuffer
Summary
TelegramChannel at 1516 lines combines Telegram API client, message formatting, option parsing, sub-agent tree formatting, message queue with backpressure, topic management, polling loop, callback routing, and progress card rendering.
Details
src/channels/telegram.ts(1516 lines)esc(),bold(),code(),italic()defined identically in bothtelegram.ts(lines 118-135) andtelegram-style.ts(lines 18-35). Local definitions shadow imports.rateLimitUntil(line 65) shared across all instances — breaks multi-instance and test isolationpollLoopis fire-and-forget with no shutdown guarantee — can process updates 10s afterdestroy()tgApiretries non-transient 4xx errors (400 Bad Request, 403 Forbidden) unnecessarilypreTopicBuffernever evicted for orphaned sessions (sessions where topic creation fails)md2htmlcan double-escape URLs with ampersandsSuggested Fix
Split into:
telegram-api.ts— tgApi + rate limitingtelegram-format.ts— expand existing telegram-style.ts to cover all formattingtelegram-queue.ts— message queuing, backpressure, read groupingrateLimitUntilto instance property