From d80ec4ca31fa9c3d54de826466ae4bb35ed01979 Mon Sep 17 00:00:00 2001 From: Ivan Casco Date: Sun, 25 Jan 2026 22:38:07 +0000 Subject: [PATCH] fix: identityLinks shares context across channels for per-channel-peer When dmScope is per-channel-peer with identityLinks configured, linked identities now share context across channels. Previously, the session key always included the channel name even when identityLinks resolved to a canonical identity, preventing cross-channel context sharing. Now when a linked identity is found, the channel is omitted from the session key (e.g., agent:main:dm:alice instead of agent:main:discord:dm:alice). --- src/routing/resolve-route.test.ts | 28 +++++++++++++++++++++++++++- src/routing/session-key.ts | 3 +++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/routing/resolve-route.test.ts b/src/routing/resolve-route.test.ts index 6a3366e97..afcaf37d1 100644 --- a/src/routing/resolve-route.test.ts +++ b/src/routing/resolve-route.test.ts @@ -77,7 +77,33 @@ describe("resolveAgentRoute", () => { accountId: null, peer: { kind: "dm", id: "222222222222222222" }, }); - expect(route.sessionKey).toBe("agent:main:discord:dm:alice"); + expect(route.sessionKey).toBe("agent:main:dm:alice"); + }); + + test("identityLinks per-channel-peer shares context across channels", () => { + const cfg: ClawdbotConfig = { + session: { + dmScope: "per-channel-peer", + identityLinks: { + alice: ["telegram:111111111", "discord:222222222222222222"], + }, + }, + }; + const telegramRoute = resolveAgentRoute({ + cfg, + channel: "telegram", + accountId: null, + peer: { kind: "dm", id: "111111111" }, + }); + const discordRoute = resolveAgentRoute({ + cfg, + channel: "discord", + accountId: null, + peer: { kind: "dm", id: "222222222222222222" }, + }); + expect(telegramRoute.sessionKey).toBe("agent:main:dm:alice"); + expect(discordRoute.sessionKey).toBe("agent:main:dm:alice"); + expect(telegramRoute.sessionKey).toBe(discordRoute.sessionKey); }); test("peer binding wins over account binding", () => { diff --git a/src/routing/session-key.ts b/src/routing/session-key.ts index 7f9f209ed..79cb6d24b 100644 --- a/src/routing/session-key.ts +++ b/src/routing/session-key.ts @@ -132,6 +132,9 @@ export function buildAgentPeerSessionKey(params: { if (linkedPeerId) peerId = linkedPeerId; peerId = peerId.toLowerCase(); if (dmScope === "per-channel-peer" && peerId) { + if (linkedPeerId) { + return `agent:${normalizeAgentId(params.agentId)}:dm:${peerId}`; + } const channel = (params.channel ?? "").trim().toLowerCase() || "unknown"; return `agent:${normalizeAgentId(params.agentId)}:${channel}:dm:${peerId}`; }