From 1ed859246715ce1a27e2159f7067cfaf9ac7947f Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 22 Jun 2026 09:10:16 -0700 Subject: [PATCH] fix(agents): keep fallback routes account-bound --- src/utils/delivery-context.shared.ts | 13 ++++++++----- src/utils/delivery-context.test.ts | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/utils/delivery-context.shared.ts b/src/utils/delivery-context.shared.ts index a7dcce48f4a..8948d7c8f75 100644 --- a/src/utils/delivery-context.shared.ts +++ b/src/utils/delivery-context.shared.ts @@ -243,17 +243,20 @@ export function mergeDeliveryContext( normalizedPrimary?.channel && normalizedFallback?.channel && normalizedPrimary.channel !== normalizedFallback.channel; + const accountsConflict = + normalizedPrimary?.accountId && + normalizedFallback?.accountId && + normalizedPrimary.accountId !== normalizedFallback.accountId; + const routesConflict = channelsConflict || accountsConflict; return normalizeDeliveryContext({ channel: normalizedPrimary?.channel ?? normalizedFallback?.channel, // Keep route fields paired to their channel; avoid crossing fields between // unrelated channels during session context merges. - to: channelsConflict - ? normalizedPrimary?.to - : (normalizedPrimary?.to ?? normalizedFallback?.to), - accountId: channelsConflict + to: routesConflict ? normalizedPrimary?.to : (normalizedPrimary?.to ?? normalizedFallback?.to), + accountId: routesConflict ? normalizedPrimary?.accountId : (normalizedPrimary?.accountId ?? normalizedFallback?.accountId), - threadId: channelsConflict + threadId: routesConflict ? normalizedPrimary?.threadId : (normalizedPrimary?.threadId ?? normalizedFallback?.threadId), }); diff --git a/src/utils/delivery-context.test.ts b/src/utils/delivery-context.test.ts index bae7a0576e8..8b64b97ab58 100644 --- a/src/utils/delivery-context.test.ts +++ b/src/utils/delivery-context.test.ts @@ -53,6 +53,20 @@ describe("delivery context helpers", () => { }); }); + it("does not inherit route fields from a different account on the same channel", () => { + const merged = mergeDeliveryContext( + { channel: "telegram", accountId: "bot-a" }, + { channel: "telegram", to: "123", accountId: "bot-b", threadId: "99" }, + ); + + expect(merged).toEqual({ + channel: "telegram", + to: undefined, + accountId: "bot-a", + }); + expect(merged?.threadId).toBeUndefined(); + }); + it("uses fallback route fields when fallback has no channel", () => { const merged = mergeDeliveryContext( { channel: "demo-channel" },