Skip to content

Commit 9e89c41

Browse files
committed
fix(claude): preserve native cache session affinity
1 parent 8cc4747 commit 9e89c41

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

‎src/server/claude-messages.ts‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* internal Request, so routing/OAuth/account-pool/failover/sidecars are inherited
77
* unchanged. The Responses output (SSE or JSON) is converted back to Anthropic shape.
88
*/
9+
import { createHash } from "node:crypto";
910
import { FORWARD_HEADERS } from "../adapters/openai-responses";
1011
import { enforceAnthropicImageLimits } from "../adapters/anthropic-image-guard";
1112
import { normalizeAnthropicImages } from "../adapters/anthropic-image-normalize";
@@ -104,6 +105,11 @@ function uuidFromHex(hex32: string): string {
104105
return `${h.slice(0, 8)}-${h.slice(8, 12)}-4${h.slice(13, 16)}-8${h.slice(17, 20)}-${h.slice(20, 32)}`;
105106
}
106107

108+
function claudeMetadataUserId(body: unknown): string | undefined {
109+
if (!isRec(body) || !isRec(body.metadata)) return undefined;
110+
return typeof body.metadata.user_id === "string" ? body.metadata.user_id : undefined;
111+
}
112+
107113
function anthropicUsageToOcx(usage: Rec | undefined): { inputTokens: number; outputTokens: number; cachedInputTokens?: number; cacheReadInputTokens?: number; cacheCreationInputTokens?: number } | undefined {
108114
if (!usage) return undefined;
109115
const num = (v: unknown) => typeof v === "number" ? v : 0;
@@ -649,15 +655,16 @@ export async function handleClaudeMessages(
649655
headers.set("authorization", `Bearer ${token.accessToken}`);
650656
headers.set("chatgpt-account-id", token.chatgptAccountId);
651657
}
652-
// ChatGPT-backend prompt-cache affinity rides the session_id HEADER (codex
653-
// clients always send their session uuid; devlog 090 follow-up: body-level
654-
// prompt_cache_key alone still yielded cached_tokens:0). Claude Code never sends
655-
// the header, so synthesize a stable per-session uuid from the same cache key —
656-
// but ONLY for a real per-session key (metadata.user_id). The system-hash fallback
657-
// key is shared across Desktop conversations, and a shared session_id's backend
658-
// semantics are unproven (audit 133 R2#3): body prompt_cache_key only there.
659-
if (cacheKeySource === "metadata" && !headers.has("session_id") && typeof internalBody.prompt_cache_key === "string") {
660-
headers.set("session_id", uuidFromHex(internalBody.prompt_cache_key));
658+
// ChatGPT-backend prompt-cache affinity rides the session_id HEADER. Keep it
659+
// session-scoped even when prompt_cache_key is content-first: metadata.user_id
660+
// identifies the Claude Code session, while the content hash is shared across
661+
// equivalent conversations and must never become a shared session identifier.
662+
const metadataUserId = claudeMetadataUserId(anthropicBody);
663+
if (!headers.has("session_id") && metadataUserId) {
664+
headers.set(
665+
"session_id",
666+
uuidFromHex(createHash("sha256").update(metadataUserId).digest("hex").slice(0, 32)),
667+
);
661668
}
662669
}
663670
const internalReq = new Request("http://localhost/v1/responses", {

‎tests/claude-messages-endpoint.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { afterEach, beforeEach, expect, test } from "bun:test";
2+
import { createHash } from "node:crypto";
23
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
34
import { tmpdir } from "node:os";
45
import { join } from "node:path";
@@ -530,6 +531,7 @@ test("native openai-responses route carries prompt_cache_key + synthesized sessi
530531
body: JSON.stringify({
531532
model: "native/gpt-test",
532533
max_tokens: 128,
534+
system: "cache-stable instructions",
533535
messages: [{ role: "user", content: "hi" }],
534536
metadata: { user_id: "user_abc123_account__session_11111111-2222-3333-4444-555555555555" },
535537
thinking: { type: "adaptive", display: "omitted" },
@@ -545,7 +547,10 @@ test("native openai-responses route carries prompt_cache_key + synthesized sessi
545547
expect(capture.body?.user).toBeUndefined();
546548
expect(capture.body?.max_output_tokens).toBeUndefined();
547549
expect(capture.body?.reasoning?.effort).toBe("high");
548-
expect(capture.headers?.["session_id"]).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-8[0-9a-f]{3}-[0-9a-f]{12}$/);
550+
const metadataUserId = "user_abc123_account__session_11111111-2222-3333-4444-555555555555";
551+
const h = createHash("sha256").update(metadataUserId).digest("hex").slice(0, 32);
552+
const expectedSessionId = `${h.slice(0, 8)}-${h.slice(8, 12)}-4${h.slice(13, 16)}-8${h.slice(17, 20)}-${h.slice(20, 32)}`;
553+
expect(capture.headers?.["session_id"]).toBe(expectedSessionId);
549554
} finally {
550555
server.stop(true);
551556
upstream.stop(true);

0 commit comments

Comments
 (0)