|
6 | 6 | * internal Request, so routing/OAuth/account-pool/failover/sidecars are inherited |
7 | 7 | * unchanged. The Responses output (SSE or JSON) is converted back to Anthropic shape. |
8 | 8 | */ |
| 9 | +import { createHash } from "node:crypto"; |
9 | 10 | import { FORWARD_HEADERS } from "../adapters/openai-responses"; |
10 | 11 | import { enforceAnthropicImageLimits } from "../adapters/anthropic-image-guard"; |
11 | 12 | import { normalizeAnthropicImages } from "../adapters/anthropic-image-normalize"; |
@@ -104,6 +105,11 @@ function uuidFromHex(hex32: string): string { |
104 | 105 | return `${h.slice(0, 8)}-${h.slice(8, 12)}-4${h.slice(13, 16)}-8${h.slice(17, 20)}-${h.slice(20, 32)}`; |
105 | 106 | } |
106 | 107 |
|
| 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 | + |
107 | 113 | function anthropicUsageToOcx(usage: Rec | undefined): { inputTokens: number; outputTokens: number; cachedInputTokens?: number; cacheReadInputTokens?: number; cacheCreationInputTokens?: number } | undefined { |
108 | 114 | if (!usage) return undefined; |
109 | 115 | const num = (v: unknown) => typeof v === "number" ? v : 0; |
@@ -649,15 +655,16 @@ export async function handleClaudeMessages( |
649 | 655 | headers.set("authorization", `Bearer ${token.accessToken}`); |
650 | 656 | headers.set("chatgpt-account-id", token.chatgptAccountId); |
651 | 657 | } |
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 | + ); |
661 | 668 | } |
662 | 669 | } |
663 | 670 | const internalReq = new Request("http://localhost/v1/responses", { |
|
0 commit comments