Sessions: canonicalize mixed-case session keys

This commit is contained in:
Vignesh Natarajan
2026-02-22 23:26:32 -08:00
parent 1be8897339
commit 9ea740afb6
5 changed files with 229 additions and 15 deletions

View File

@@ -75,4 +75,32 @@ describe("recordInboundSession", () => {
}),
);
});
it("normalizes mixed-case session keys before recording and route updates", async () => {
const { recordInboundSession } = await import("./session.js");
await recordInboundSession({
storePath: "/tmp/openclaw-session-store.json",
sessionKey: "Agent:Main:Telegram:1234:Thread:42",
ctx,
updateLastRoute: {
sessionKey: "agent:main:telegram:1234:thread:42",
channel: "telegram",
to: "telegram:1234",
},
onRecordError: vi.fn(),
});
expect(recordSessionMetaFromInboundMock).toHaveBeenCalledWith(
expect.objectContaining({
sessionKey: "agent:main:telegram:1234:thread:42",
}),
);
expect(updateLastRouteMock).toHaveBeenCalledWith(
expect.objectContaining({
sessionKey: "agent:main:telegram:1234:thread:42",
ctx,
}),
);
});
});

View File

@@ -6,6 +6,10 @@ import {
updateLastRoute,
} from "../config/sessions.js";
function normalizeSessionStoreKey(sessionKey: string): string {
return sessionKey.trim().toLowerCase();
}
export type InboundLastRouteUpdate = {
sessionKey: string;
channel: SessionEntry["lastChannel"];
@@ -24,9 +28,10 @@ export async function recordInboundSession(params: {
onRecordError: (err: unknown) => void;
}): Promise<void> {
const { storePath, sessionKey, ctx, groupResolution, createIfMissing } = params;
const canonicalSessionKey = normalizeSessionStoreKey(sessionKey);
void recordSessionMetaFromInbound({
storePath,
sessionKey,
sessionKey: canonicalSessionKey,
ctx,
groupResolution,
createIfMissing,
@@ -36,9 +41,10 @@ export async function recordInboundSession(params: {
if (!update) {
return;
}
const targetSessionKey = normalizeSessionStoreKey(update.sessionKey);
await updateLastRoute({
storePath,
sessionKey: update.sessionKey,
sessionKey: targetSessionKey,
deliveryContext: {
channel: update.channel,
to: update.to,
@@ -46,7 +52,7 @@ export async function recordInboundSession(params: {
threadId: update.threadId,
},
// Avoid leaking inbound origin metadata into a different target session.
ctx: update.sessionKey === sessionKey ? ctx : undefined,
ctx: targetSessionKey === canonicalSessionKey ? ctx : undefined,
groupResolution,
});
}