Skip to content

[codex] add telegram topic routing config and thread-aware chat routing#226

Merged
everettjf merged 5 commits intomainfrom
microclaw-topic-based-routing
Mar 9, 2026
Merged

[codex] add telegram topic routing config and thread-aware chat routing#226
everettjf merged 5 commits intomainfrom
microclaw-topic-based-routing

Conversation

@everettjf
Copy link
Contributor

@everettjf everettjf commented Mar 9, 2026

Summary

This PR adds opt-in Telegram topic-based routing so forum threads can keep isolated session/memory context.

Closes #220.

Problem

In Telegram supergroups with forum topics, MicroClaw previously keyed Telegram sessions only by chat_id.
That merged all topics in the same group into one conversation state.

User impact:

  • cross-topic context leakage
  • "agent yapping" across unrelated topic workspaces
  • harder multi-agent workflows in one Telegram hub

Root Cause

external_chat_id for Telegram was always the raw Telegram chat_id, and message_thread_id was ignored for session routing.

Changes

  1. Add Telegram config:
  • channels.telegram.topic_routing.enabled (default false)
  1. Route Telegram session identity by topic when enabled:
  • Build external_chat_id as {chat_id}:{thread_id} when message_thread_id exists and routing is enabled
  • Keep legacy {chat_id} behavior when disabled or when no thread id is present
  1. Keep Telegram outbound compatibility for topic-scoped chats:
  • Teach Telegram adapter to parse both external_chat_id shapes:
    • {chat_id}
    • {chat_id}:{thread_id}
  • When thread id is present, send responses/attachments with message_thread_id
  1. Tests:
  • runtime context default/off behavior assertions
  • runtime context enabled parsing assertion
  • external chat id generation test for enabled/disabled/no-thread cases
  • updated existing send-message test fixture for new Telegram config field

Validation

Executed:

  • cargo test -p microclaw --lib channels::telegram::tests::test_build_telegram_runtime_contexts_topic_routing_enabled -- --nocapture
  • cargo test -p microclaw --lib channels::telegram::tests::test_telegram_external_chat_id_topic_routing -- --nocapture
  • cargo test -p microclaw --lib tools::send_message::tests::test_send_message_web_caller_cross_chat_denied -- --nocapture

All passed.

Topic Routing Config Priority

Telegram topic routing now supports both channel-level default and per-account override.

Priority order:

  1. channels.telegram.accounts.<account_id>.topic_routing.enabled (highest)
  2. channels.telegram.topic_routing.enabled
  3. false (implicit default)

This allows gradual rollout by account while keeping backward compatibility.

Migration Examples

Channel-wide enable:

channels:
  telegram:
    topic_routing:
      enabled: true

Mixed mode (channel default + account override):

channels:
  telegram:
    topic_routing:
      enabled: false
    accounts:
      main:
        enabled: true
        bot_token: "..."
        bot_username: "..."
      ops:
        enabled: true
        bot_token: "..."
        topic_routing:
          enabled: true

In the mixed mode above:

  • main follows channel default (false)
  • ops uses account override (true)

@everettjf everettjf marked this pull request as ready for review March 9, 2026 08:51
@everettjf everettjf marked this pull request as draft March 9, 2026 08:51
@everettjf everettjf marked this pull request as ready for review March 9, 2026 16:16
@everettjf everettjf merged commit 990e805 into main Mar 9, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Telegram Topic-based Routing (Telegram Mission Control for Microclaw)

1 participant