#16783: Fix cross-channel routing in iMessage monitor
channel: imessage
stale
size: XS
Cluster:
Network Error Handling Improvements
## Problem
When iMessage receives a message in a cross-channel conversation (e.g., Telegram user sending to iMessage contact), agent responses were incorrectly routed to iMessage instead of back to the original channel (Telegram).
**Example scenario:**
1. User on Telegram uses message tool to send to iMessage contact
2. Contact replies on iMessage
3. ❌ **Bug**: Agent's response goes to iMessage contact (should go back to Telegram user)
4. ✅ **Fix**: Agent's response now goes back to Telegram user
## Solution
This PR fixes the routing by:
- Loading the session entry to check for `deliveryContext`
- Using `deliveryContext` for `OriginatingChannel`/`OriginatingTo` when present and different from current channel
- Falling back to normal iMessage routing when `deliveryContext` is not set
**Files changed:**
- `src/imessage/monitor/monitor-provider.ts`: Added session loading and deliveryContext check before setting OriginatingChannel
## Testing
Tested with Telegram → iMessage → Reply scenario:
- ✅ Agent responses correctly route back to Telegram
- ✅ Direct iMessage conversations still work normally
- ✅ Cross-channel routing respects session deliveryContext
## Notes
The same pattern could be applied to other channel monitors (Telegram, Discord, Slack, Signal) if they have similar cross-channel routing issues.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR fixes cross-channel routing in the iMessage monitor so that when a message arrives on iMessage as part of a conversation that originated on another channel (e.g., Telegram), agent replies are routed back to the originating channel rather than being sent via iMessage.
- Loads session `deliveryContext` before building the inbound context to detect cross-channel conversations
- Overrides `OriginatingChannel` and `OriginatingTo` when the stored delivery context points to a different channel, leveraging the existing `dispatch-from-config` cross-channel routing infrastructure
- Falls back to normal iMessage routing when no cross-channel context exists or when the session load fails
- Includes an unrelated lockfile cleanup removing `proper-lockfile` from the `extensions/msteams` importer (catching up with a prior package.json change)
- No tests were added for the new cross-channel routing behavior in the iMessage monitor — the PR relies on manual testing
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge with low risk — it adds a well-guarded read-only check with proper fallback behavior
- The change is well-contained (single file with logic changes), uses existing infrastructure correctly (loadSessionStore, deliveryContext, OriginatingChannel pattern), has proper error handling with try/catch fallback, and doesn't modify any existing behavior when no cross-channel context is present. The lack of unit tests is a minor gap offset by the targeted nature of the change and manual testing confirmation.
- Pay attention to `src/imessage/monitor/monitor-provider.ts` for potential stale deliveryContext edge cases in long-lived sessions
<sub>Last reviewed commit: 4ff4efb</sub>
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#17337: fix(delivery): keep route fields paired to channel during context m...
by Glucksberg · 2026-02-15
79.8%
#6850: fix: support direct channel:account:peer format in session key extr...
by toboto · 2026-02-02
77.3%
#15863: fix: route agent-to-agent Slack messages to bound sessions
by MisterGuy420 · 2026-02-14
76.7%
#17527: fix(gateway): allow WebChat to attach to main session regardless of...
by Glucksberg · 2026-02-15
75.8%
#2799: fix(imessage): prevent self-chat and outbound echo loops (#2585)
by Tfh-Yqf · 2026-01-27
75.3%
#7353: fix: prevent silent message drops after config.patch restart
by 18-RAJAT · 2026-02-02
74.9%
#3300: changed chat session selection logic in iMessage
by KimEJ · 2026-01-28
74.8%
#13580: fix(telegram): skip updateLastRoute when dmScope isolates DM sessions
by lailoo · 2026-02-10
74.6%
#18664: fix(imessage): honor groupConfig regardless of allowlistEnabled
by brandonwise · 2026-02-16
74.5%
#11123: Fix webchat→external channel cross-delivery
by jingkang0822 · 2026-02-07
74.3%