#16441: feat: add topicId matching to bindings for Telegram forum topics
channel: telegram
stale
size: S
Cluster:
Session Management Enhancements
## Summary
Add an optional `topicId` field to the binding match schema, enabling per-topic agent routing in Telegram forum groups.
## Use Case
In multi-agent setups, different Telegram forum topics in the same group should route to different agents. Currently, bindings can match on `channel`, `peer`, `guildId`, `teamId`, and `roles` — but not on Telegram forum topic IDs.
## Changes
- **`src/config/types.agents.ts`**: Add optional `topicId: string` to `AgentBinding.match`
- **`src/routing/resolve-route.ts`**:
- Add `topicId` to `ResolveAgentRouteInput`, `NormalizedBindingMatch`, and `BindingScope`
- New `binding.peer+topic` tier ranked above `binding.peer` for higher specificity
- `matchesBindingScope` checks `topicId` when present on a binding
- **`src/telegram/bot-message-context.ts`**, **`bot-handlers.ts`**, **`bot-native-commands.ts`**: Pass `topicId` (from `resolvedThreadId`) to `resolveAgentRoute`
- **`src/routing/resolve-route.test.ts`**: 3 new tests covering topic match, topic mismatch, and no-topic fallback
## Example Config
```json
{
"agentId": "cfo",
"match": {
"channel": "telegram",
"peer": { "kind": "group", "id": "-1003888463743" },
"topicId": "315"
}
}
```
## Backward Compatibility
When `topicId` is absent from a binding, behavior is unchanged — matches all topics in the group as before.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR adds optional `topicId`-based routing for Telegram forum topics, enabling per-topic agent binding in multi-agent setups. The routing logic correctly introduces a new `binding.peer+topic` tier above `binding.peer`, and `matchesBindingScope` properly prevents topic-constrained bindings from matching at lower tiers when the topic doesn't match. The Telegram handler files consistently pass `resolvedThreadId` as `topicId`. Tests cover the key scenarios.
- **Critical:** `topicId` is missing from the Zod validation schema in `src/config/zod-schema.agents.ts`. The `match` object uses `.strict()`, so any config with `topicId` in a binding will be **rejected at config load time**, making the feature unusable until the schema is updated.
- **Minor:** Debug log lines in `resolve-route.ts` do not include `topicId`, which will make it harder to troubleshoot topic-based routing issues.
<h3>Confidence Score: 2/5</h3>
- The feature will fail at config validation due to a missing Zod schema update — merging as-is will prevent anyone from using topicId in bindings.
- The routing logic itself is well-implemented and the tests are solid, but the missing `topicId` in the Zod `BindingsSchema` (.strict() mode) means the feature is broken at the config layer. This is a blocking issue that must be fixed before merge.
- `src/config/zod-schema.agents.ts` — needs `topicId: z.string().optional()` added to the binding match schema.
<sub>Last reviewed commit: 210c2f1</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
#7224: feat(telegram): inherit forum topic bindings from parent group
by Buywatermelon · 2026-02-02
82.6%
#23656: fix(routing): trust binding agentId even when not in agents.list
by SleuthCo · 2026-02-22
73.5%
#10383: feat(hooks): add agentId to hook mappings
by quentintou · 2026-02-06
72.0%
#7058: feat(telegram): add channel_post handler for broadcast channels
by waifu7498173 · 2026-02-02
71.3%
#13477: routing: normalize account ID matching for agent bindings
by davidahmann · 2026-02-10
71.3%
#10663: feat(hooks): add agentId support for webhook routing
by nityatrish · 2026-02-06
70.9%
#8310: feat(telegram): Add allowBots support for groups (parity with Disco...
by vishaltandale00 · 2026-02-03
70.7%
#6084: Add dynamic Telegram agent routing for DM and groups
by Alfonsxh · 2026-02-01
70.6%
#15244: feat: Thread-session binding — route sub-agent I/O through platform...
by spk-alex · 2026-02-13
70.5%
#16249: fix(sessions): allow cross-agent session paths in multi-agent bindings
by 0xbrak · 2026-02-14
70.4%