mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 00:13:28 +00:00
Sessions: canonicalize mixed-case session keys
This commit is contained in:
@@ -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,
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user