diff --git a/src/infra/outbound/bound-delivery-router.test.ts b/src/infra/outbound/bound-delivery-router.test.ts index abb2c1715bb..7077d92348e 100644 --- a/src/infra/outbound/bound-delivery-router.test.ts +++ b/src/infra/outbound/bound-delivery-router.test.ts @@ -122,6 +122,34 @@ describe("bound delivery router", () => { }, expectedConversationId: "thread-2", }, + { + name: "normalizes adapter binding conversations before requester matching", + bindings: [ + { + ...createDiscordBinding(TARGET_SESSION_KEY, "thread-1", 1), + conversation: { + channel: " discord ", + accountId: " runtime ", + conversationId: " thread-1 ", + }, + }, + { + ...createDiscordBinding(TARGET_SESSION_KEY, "thread-2", 2), + conversation: { + channel: " DISCORD ", + accountId: " Runtime ", + conversationId: " thread-2 ", + }, + }, + ], + requesterConversationId: "thread-2", + failClosed: true, + expected: { + mode: "bound", + reason: "requester-match", + }, + expectedConversationId: " thread-2 ", + }, { name: "falls back for invalid requester conversation values", bindings: [createDiscordBinding(TARGET_SESSION_KEY, "thread-1", 1)], diff --git a/src/infra/outbound/bound-delivery-router.ts b/src/infra/outbound/bound-delivery-router.ts index 5b837d40dcd..e3b425f7cd8 100644 --- a/src/infra/outbound/bound-delivery-router.ts +++ b/src/infra/outbound/bound-delivery-router.ts @@ -32,16 +32,20 @@ function resolveBindingForRequester( bindings: SessionBindingRecord[], ): SessionBindingRecord | null { const matchingChannelAccount = bindings.filter( - (entry) => - entry.conversation.channel === requester.channel && - entry.conversation.accountId === requester.accountId, + (entry) => { + const conversation = normalizeConversationRef(entry.conversation); + return ( + conversation.channel === requester.channel && conversation.accountId === requester.accountId + ); + }, ); if (matchingChannelAccount.length === 0) { return null; } const exactConversation = matchingChannelAccount.find( - (entry) => entry.conversation.conversationId === requester.conversationId, + (entry) => + normalizeConversationRef(entry.conversation).conversationId === requester.conversationId, ); if (exactConversation) { return exactConversation;