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).
This commit is contained in:
Ivan Casco 2026-01-25 22:38:07 +00:00
parent 0b1c8db0ca
commit d80ec4ca31
2 changed files with 30 additions and 1 deletions

View File

@ -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", () => {

View File

@ -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}`;
}