fix(core): unify session-key normalization and plugin boundary checks

This commit is contained in:
Peter Steinberger
2026-02-26 12:40:57 +00:00
parent e3385a6578
commit 4b71de384c
13 changed files with 182 additions and 34 deletions

View File

@@ -4,7 +4,11 @@ import {
getSubagentDepth,
isCronSessionKey,
} from "../sessions/session-key-utils.js";
import { classifySessionKeyShape } from "./session-key.js";
import {
classifySessionKeyShape,
parseAgentSessionKey,
toAgentStoreSessionKey,
} from "./session-key.js";
describe("classifySessionKeyShape", () => {
it("classifies empty keys as missing", () => {
@@ -93,3 +97,21 @@ describe("deriveSessionChatType", () => {
expect(deriveSessionChatType("")).toBe("unknown");
});
});
describe("session key canonicalization", () => {
it("parses agent keys case-insensitively and returns lowercase tokens", () => {
expect(parseAgentSessionKey("AGENT:Main:Hook:Webhook:42")).toEqual({
agentId: "main",
rest: "hook:webhook:42",
});
});
it("does not double-prefix already-qualified agent keys", () => {
expect(
toAgentStoreSessionKey({
agentId: "main",
requestKey: "agent:main:main",
}),
).toBe("agent:main:main");
});
});

View File

@@ -49,16 +49,17 @@ export function toAgentStoreSessionKey(params: {
mainKey?: string | undefined;
}): string {
const raw = (params.requestKey ?? "").trim();
if (!raw || raw === DEFAULT_MAIN_KEY) {
if (!raw || raw.toLowerCase() === DEFAULT_MAIN_KEY) {
return buildAgentMainSessionKey({ agentId: params.agentId, mainKey: params.mainKey });
}
const parsed = parseAgentSessionKey(raw);
if (parsed) {
return `agent:${parsed.agentId}:${parsed.rest}`;
}
const lowered = raw.toLowerCase();
if (lowered.startsWith("agent:")) {
return lowered;
}
if (lowered.startsWith("subagent:")) {
return `agent:${normalizeAgentId(params.agentId)}:${lowered}`;
}
return `agent:${normalizeAgentId(params.agentId)}:${lowered}`;
}