diff --git a/src/api/providers/__tests__/openai-native-usage.spec.ts b/src/api/providers/__tests__/openai-native-usage.spec.ts index b79145efdd..9f823ffca5 100644 --- a/src/api/providers/__tests__/openai-native-usage.spec.ts +++ b/src/api/providers/__tests__/openai-native-usage.spec.ts @@ -3,6 +3,22 @@ import { createHash } from "crypto" import { OpenAiNativeHandler } from "../openai-native" import { OpenAiServiceTier, openAiNativeModels, type ModelInfo } from "@roo-code/types" import type { ApiStreamUsageChunk } from "../../transform/stream" +import { asyncStreamFrom, collectStream } from "../../../test-utils/stream" + +// The streaming entry point reaches the SDK client, so stub the client surface the +// handler actually calls. Without this the real SDK would attempt a network request. +const mockResponsesCreate = vitest.fn() + +vitest.mock("openai", () => ({ + __esModule: true, + default: vitest.fn().mockImplementation(function () { + return { + responses: { + create: mockResponsesCreate, + }, + } + }), +})) describe("OpenAiNativeHandler - normalizeUsage", () => { let handler: OpenAiNativeHandler @@ -478,10 +494,11 @@ describe("OpenAiNativeHandler - normalizeUsage", () => { expect(first.prompt_cache_key).not.toBe(second.prompt_cache_key) }) - it("should fall back to the handler session id when no task id is supplied", () => { + it("should omit the key when no task id is supplied", () => { + // No task-scoped identity means no accumulating prefix, so the field is omitted + // rather than filled with a per-call random value that would override default routing. const body = buildRequestBody("gpt-5.1") - expect(body.prompt_cache_key).toBe(handler["sessionId"]) - expect(body.prompt_cache_key).toEqual(expect.any(String)) + expect(body).not.toHaveProperty("prompt_cache_key") }) it("should omit the key when the model does not support prompt caching", () => { @@ -492,6 +509,32 @@ describe("OpenAiNativeHandler - normalizeUsage", () => { expect(body.prompt_cache_key).toBeUndefined() }) + it("should omit the key for a non-caching model through the streaming entry point", async () => { + // The builder-level test above cannot see the streaming path: createMessage forwards + // metadata into buildRequestBody, so a key added anywhere along that path would still + // pass it. Drive the public entry point with a task id to pin the model gate there too. + const modelId = "codex-mini-latest" + expect(openAiNativeModels[modelId as keyof typeof openAiNativeModels].supportsPromptCache).toBe(false) + + mockResponsesCreate.mockClear() + mockResponsesCreate.mockResolvedValue(asyncStreamFrom([])) + + const streamingHandler = new OpenAiNativeHandler({ + openAiNativeApiKey: "test-key", + apiModelId: modelId, + }) + + await collectStream( + streamingHandler.createMessage("You are a helpful assistant.", [{ role: "user", content: "Hello!" }], { + taskId: "task-abc", + }), + ) + + expect(mockResponsesCreate).toHaveBeenCalledTimes(1) + const [body] = mockResponsesCreate.mock.calls[0] + expect(body).not.toHaveProperty("prompt_cache_key") + }) + it("should clamp an over-length task id to OpenAI's 64-character limit", () => { const longTaskId = "t".repeat(80) const body = buildRequestBody("gpt-5.1", longTaskId) diff --git a/src/api/providers/__tests__/openai-native.spec.ts b/src/api/providers/__tests__/openai-native.spec.ts index fa32c85dd9..a0003c8324 100644 --- a/src/api/providers/__tests__/openai-native.spec.ts +++ b/src/api/providers/__tests__/openai-native.spec.ts @@ -2386,14 +2386,13 @@ describe("OpenAiNativeHandler", () => { expect(handler.getEncryptedContent()).toBeUndefined() }) - it("should scope the prompt_cache_key to the handler session", async () => { + it("should omit the prompt_cache_key because no task-scoped identity exists", async () => { mockResponsesCreate.mockResolvedValue({ output: [] }) await handler.completePrompt("Test prompt") const [request] = mockResponsesCreate.mock.calls[0] - expect(request.prompt_cache_key).toBe(handler["sessionId"]) - expect(request.prompt_cache_key).toEqual(expect.any(String)) + expect(request).not.toHaveProperty("prompt_cache_key") }) it("should omit the prompt_cache_key when the model does not support prompt caching", async () => { @@ -3396,19 +3395,55 @@ describe("OpenAiNativeHandler", () => { it("sends the task-scoped prompt_cache_key through the SDK path", async () => { mockResponsesCreate.mockResolvedValue(asyncStreamFrom([])) + ;(OpenAI as unknown as ReturnType).mockClear() const cacheHandler = new OpenAiNativeHandler({ ...mockOptions, apiModelId: "gpt-5.1" }) - for (const taskId of ["sdk-task-one", "sdk-task-one", "sdk-task-two"]) { - await collectStream(cacheHandler.createMessage(systemPrompt, messages, { taskId })) + // The second turn of the same task carries a longer conversation. Reusing one + // messages array would let a key derived from the conversation pass, so the two + // calls must differ in input while the key holds. + const followUp: Anthropic.Messages.MessageParam[] = [ + ...messages, + { role: "assistant", content: "Hello! How can I help?" }, + { role: "user", content: "Tell me about prompt caching." }, + ] + const turns: Array<{ taskId: string; messages: Anthropic.Messages.MessageParam[] }> = [ + { taskId: "sdk-task-one", messages }, + { taskId: "sdk-task-one", messages: followUp }, + { taskId: "sdk-task-two", messages }, + ] + + for (const turn of turns) { + await collectStream(cacheHandler.createMessage(systemPrompt, turn.messages, { taskId: turn.taskId })) } const bodies = mockResponsesCreate.mock.calls.map(([body]) => body) expect(bodies[0].prompt_cache_key).toBe("sdk-task-one") + expect(bodies[1].input).not.toEqual(bodies[0].input) expect(bodies[1].prompt_cache_key).toBe(bodies[0].prompt_cache_key) expect(bodies[2].prompt_cache_key).toBe("sdk-task-two") expect(bodies[0].prompt_cache_retention).toBe("24h") expect(mockResponsesCreate.mock.calls[0][1].headers).not.toHaveProperty("session_id") + // Per-request headers are only half the surface: the SDK constructor also takes + // defaultHeaders, and a restored affinity header there would ship on every request + // while the assertion above still passed. Cache affinity is a body field, so neither + // the header name nor the body field name may appear in the constructor's defaults. + const constructorArgs = (OpenAI as unknown as ReturnType).mock.calls[0][0] + expect(constructorArgs.defaultHeaders).not.toHaveProperty("session_id") + expect(constructorArgs.defaultHeaders).not.toHaveProperty("prompt_cache_key") + }) + + it("omits the prompt_cache_key through createMessage when no metadata is supplied", async () => { + mockResponsesCreate.mockResolvedValue(asyncStreamFrom([])) + + const cacheHandler = new OpenAiNativeHandler({ ...mockOptions, apiModelId: "gpt-5.1" }) + + // No metadata at all: the streaming entry point must not synthesize a fallback + // task id, or the field would replace OpenAI's default routing for no affinity. + await collectStream(cacheHandler.createMessage(systemPrompt, messages)) + + const [body] = mockResponsesCreate.mock.calls[0] + expect(body).not.toHaveProperty("prompt_cache_key") }) it("keys completePrompt off the task id when the caller supplies one", async () => { @@ -3430,17 +3465,6 @@ describe("OpenAiNativeHandler", () => { expect(bodies[1].prompt_cache_key).toBe(bodies[0].prompt_cache_key) }) - it("falls back to the handler session id when completePrompt gets no task id", async () => { - mockResponsesCreate.mockResolvedValue({ output: [] }) - - const cacheHandler = new OpenAiNativeHandler({ ...mockOptions, apiModelId: "gpt-5.1" }) - - await cacheHandler.completePrompt("enhance me") - - const [body] = mockResponsesCreate.mock.calls[0] - expect(body.prompt_cache_key).toBe(cacheHandler["sessionId"]) - }) - it("should provide helpful error messages for different error codes", async () => { const testCases = [ { status: 400, expectedMessage: "Invalid request to Responses API" }, diff --git a/src/api/providers/openai-native.ts b/src/api/providers/openai-native.ts index 267eab96e7..1530c62b31 100644 --- a/src/api/providers/openai-native.ts +++ b/src/api/providers/openai-native.ts @@ -1,6 +1,5 @@ import * as os from "os" import { createHash } from "crypto" -import { v7 as uuidv7 } from "uuid" import { Anthropic } from "@anthropic-ai/sdk" import OpenAI from "openai" @@ -50,8 +49,6 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio protected options: ApiHandlerOptions private client: OpenAI private readonly providerName = "OpenAI Native" - // Session ID for request tracking (persists for the lifetime of the handler) - private readonly sessionId: string /** * Some Responses streams emit tool-call argument deltas without stable call id/name. * Track the last observed tool identity from output_item events so we can still @@ -100,8 +97,6 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio constructor(options: ApiHandlerOptions) { super() this.options = options - // Generate a session ID for request tracking - this.sessionId = uuidv7() // Default to including reasoning.summary: "auto" for models that support Responses API // reasoning summaries unless explicitly disabled. if (this.options.enableResponsesReasoningSummary === undefined) { @@ -1713,8 +1708,12 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio * machine) and differ between tasks (so unrelated prefixes do not compete for one * routing bucket). `metadata.taskId` is the longest-lived identifier the handler * receives: it is the persisted task id, stable across turns, restarts and - * rehydration, whereas `this.sessionId` is regenerated per handler instance. - * The handler session id is the fallback for callers that send no task id. + * rehydration. + * + * The key is sent ONLY when a task-scoped identity exists. With no task id there is + * no conversation whose prefix could accumulate, so a per-call random key would buy + * no affinity while replacing whatever routing OpenAI applies by default. The + * fail-safe is therefore to omit the field and leave default routing untouched. */ private getPromptCacheKey( model: OpenAiNativeModel, @@ -1722,13 +1721,13 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio ): string | undefined { if (!model.info.supportsPromptCache) return undefined - const key = metadata?.taskId || this.sessionId - if (!key) return undefined + const taskId = metadata?.taskId + if (!taskId) return undefined // Hash rather than slice: a 64-char prefix drops the distinguishing tail, so two // distinct task ids that share a long prefix would land in one routing bucket. // sha256 hex is 64 characters, so the result fits the cap without truncation. - return key.length > PROMPT_CACHE_KEY_MAX_LENGTH ? createHash("sha256").update(key).digest("hex") : key + return taskId.length > PROMPT_CACHE_KEY_MAX_LENGTH ? createHash("sha256").update(taskId).digest("hex") : taskId } /** @@ -1871,9 +1870,11 @@ export class OpenAiNativeHandler extends BaseProvider implements SingleCompletio requestBody[SERVICE_TIER_KEY] = serviceTier } - // Cache routing affinity key. A caller that knows the task passes it, so repeated - // completions for one task share a bucket across handler instances; otherwise the - // handler session id scopes the key to this instance. + // Cache routing affinity key, sent only when a task-scoped identity exists: a + // caller that knows the task passes it, so repeated completions for one task + // share a bucket across handler instances. With no task id there is no + // conversation whose prefix could accumulate, so the field is omitted and + // OpenAI's default routing applies. const promptCacheKey = this.getPromptCacheKey( model, options?.taskId ? { taskId: options.taskId } : undefined,