Skip to content

Commit 03a48b5

Browse files
authored
fix(agents): preserve prompt cache boundary keys (#126380)
1 parent 0606e31 commit 03a48b5

2 files changed

Lines changed: 27 additions & 18 deletions

File tree

‎src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.test.ts‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ describe("resolveSessionBoundaryPromptCacheKey", () => {
1010
sessionId: "session-1",
1111
});
1212

13-
expect(resolve(0)).toBe(resolve(0));
14-
expect(resolve(1)).not.toBe(resolve(0));
15-
expect(resolve(2)).not.toBe(resolve(1));
13+
expect([resolve(0), resolve(0), resolve(1), resolve(2)]).toEqual([
14+
"session-1:0",
15+
"session-1:0",
16+
"session-1:1",
17+
"session-1:2",
18+
]);
1619
});
1720

1821
it("preserves an explicit caller cache key", () => {
@@ -26,15 +29,21 @@ describe("resolveSessionBoundaryPromptCacheKey", () => {
2629
).toBe("caller-key");
2730
});
2831

29-
it("clamps derived keys from long internal session ids to OpenAI's 64-char limit", () => {
30-
const longSessionId = `internal-session-effects-session-companion-${"a".repeat(50)}`;
31-
const key = resolveSessionBoundaryPromptCacheKey({
32-
api: "openai-responses",
33-
boundaryCount: 0,
34-
sessionId: longSessionId,
35-
});
36-
expect(key).toBeDefined();
37-
expect(Array.from(key ?? "").length).toBeLessThanOrEqual(64);
38-
expect(key?.startsWith("internal-session-effects-session-companion-")).toBe(true);
32+
it("keeps long Unicode derived keys distinct across boundaries within the 64-char limit", () => {
33+
const longSessionId = `internal-session-effects-${"🦞".repeat(64)}`;
34+
const keys = [0, 1, 2].map((boundaryCount) =>
35+
resolveSessionBoundaryPromptCacheKey({
36+
api: "openai-responses",
37+
boundaryCount,
38+
sessionId: longSessionId,
39+
}),
40+
);
41+
42+
expect(new Set(keys).size).toBe(keys.length);
43+
for (const [boundaryCount, key] of keys.entries()) {
44+
expect(key).toBeDefined();
45+
expect(Array.from(key ?? "").length).toBeLessThanOrEqual(64);
46+
expect(key?.endsWith(`:${boundaryCount}`)).toBe(true);
47+
}
3948
});
4049
});

‎src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { clampOpenAIPromptCacheKey } from "@openclaw/ai/providers";
1+
import { OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH } from "@openclaw/ai/providers";
22

33
export function resolveSessionBoundaryPromptCacheKey(params: {
44
api: string;
@@ -17,8 +17,8 @@ export function resolveSessionBoundaryPromptCacheKey(params: {
1717
if (!usesOpenAIPromptCacheKey) {
1818
return undefined;
1919
}
20-
// Clamp at derivation, not only in provider param builders: proxy runtimes
21-
// serialize this key verbatim, and long internal-effects session ids
22-
// (companion/btw) otherwise exceed OpenAI's 64-char prompt_cache_key limit.
23-
return clampOpenAIPromptCacheKey(`${params.sessionId}:${params.boundaryCount}`);
20+
// Reserve the lifecycle suffix inside OpenAI's 64-code-point limit for proxy runtimes.
21+
const suffix = `:${params.boundaryCount}`;
22+
const maxSessionIdLength = OPENAI_PROMPT_CACHE_KEY_MAX_LENGTH - suffix.length;
23+
return `${Array.from(params.sessionId).slice(0, maxSessionIdLength).join("")}${suffix}`;
2424
}

0 commit comments

Comments
 (0)