fix(agents): keep fallback routes account-bound

This commit is contained in:
Peter Steinberger
2026-06-22 09:10:16 -07:00
committed by Ayaan Zaidi
parent dc9ad35bda
commit 1ed8592467
2 changed files with 22 additions and 5 deletions

View File

@@ -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),
});

View File

@@ -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" },