#17337: fix(delivery): keep route fields paired to channel during context merge
size: S
experienced-contributor
Cluster:
Network Error Handling Improvements
Fixes #13332
## Problem
When Telegram and webchat share the same session (`agent:main:main`), `mergeDeliveryContext()` would inherit route fields (`to`, `accountId`, `threadId`) from the fallback context even when the primary and fallback contexts named **different channels**. This caused a Telegram-originated reply to pick up webchat's `to`/`accountId` values, mis-routing the response to the webchat UI instead of back to Telegram.
## Fix
Added a `channelsConflict` guard in `mergeDeliveryContext()` — when both primary and fallback specify different channel names, route fields (`to`, `accountId`, `threadId`) are **not** inherited from the fallback. They remain paired to the selected channel.
This is a 13-line logic change in `src/utils/delivery-context.ts`.
## Changed files
- **`src/utils/delivery-context.ts`** — added channel-conflict check before fallback inheritance
- **`src/utils/delivery-context.test.ts`** — 3 new test cases:
- Cross-channel fields are NOT inherited (telegram vs discord)
- Same-channel fields ARE inherited normally
- Missing channel in fallback still merges normally
- Updated `normalizeSessionDeliveryFields` test to verify cross-channel carryover is blocked
## Tests
All 3 new unit tests pass. Existing delivery-context tests updated and passing.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Adds cross-channel conflict detection to `mergeDeliveryContext()` to prevent route field mis-pairing when Telegram and webchat share the same session. When primary and fallback contexts specify different channels, route fields (`to`, `accountId`, `threadId`) are not inherited from fallback, keeping them paired to the selected channel.
- Fixes mis-routing issue where Telegram replies inherited webchat's route fields
- Logic change is minimal (13 lines) and well-tested with 3 new test cases
- Preserves existing behavior when channels match or when either context lacks a channel
- Updated integration test verifies cross-channel carryover is blocked
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk
- The fix is targeted, well-tested, and solves a specific cross-channel routing bug. The logic is sound: it only blocks route field inheritance when both contexts explicitly specify different channels, preserving all existing behavior for same-channel and missing-channel scenarios. Three new unit tests directly validate the fix, and an integration test confirms the end-to-end behavior.
- No files require special attention
<sub>Last reviewed commit: 4248f11</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#17527: fix(gateway): allow WebChat to attach to main session regardless of...
by Glucksberg · 2026-02-15
83.1%
#13580: fix(telegram): skip updateLastRoute when dmScope isolates DM sessions
by lailoo · 2026-02-10
80.6%
#16783: Fix cross-channel routing in iMessage monitor
by fengwen2013 · 2026-02-15
79.8%
#23727: Fix Telegram channel resolution drift across announce + message sen...
by SmithLabsLLC · 2026-02-22
78.2%
#19213: Telegram: preserve DM topic thread in direct replies
by Kemalau · 2026-02-17
78.0%
#18801: fix(routing): use fresh config in resolveAgentRoute to prevent stal...
by mcaxtr · 2026-02-17
77.9%
#23804: fix(slack): preserve string thread context in queue + DM route
by vincentkoc · 2026-02-22
76.9%
#5391: fix(commands): prevent webchat/tui from inheriting messaging channe...
by Glucksberg · 2026-01-31
76.8%
#23238: fix(telegram): account named "default" silently breaks inbound polling
by anillBhoi · 2026-02-22
76.8%
#3368: fix: sessions navigation, DM thread display, and DM thread delivery...
by Lukavyi · 2026-01-28
76.4%