Skip to content

Architecture: telegram.ts monolith (1516 lines, 5+ concerns) #352

@OneStepAt4time

Description

@OneStepAt4time

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions