From 834ffb4b18eec74cde86d230ea0babc25e9a95a8 Mon Sep 17 00:00:00 2001 From: Eason Liang <136036952+easonLiangWorldedtech@users.noreply.github.com> Date: Fri, 20 Nov 2026 02:22:41 +0800 Subject: [PATCH 01/11] refactor(api): bridge openrouter createMessage cancellation with the shared helper createMessage built its own AbortController and attached its own "abort" listener to the caller's signal. bridgeExternalAbortSignal owns that whole shape - the pre-abort guard, the once-only listener, and a detach handle - and anthropic.ts, xai.ts and minimax.ts already use it, so do the same here. The helper does not expose its controller, and this generator needs one: the finally block must cancel the in-flight request when the consumer abandons the generator, which is what the "cancels the in-flight stream when the consumer abandons the generator" test pins. That controller is kept, and its signal is merged with the bridged one through mergeAbortSignals, so either cancellation reaches the SDK call, the model-lookup race and the per-chunk guards. Both chat.completions.create awaits now go through settleOnAbort. openai@5.23.2 inspects the request signal before the first attempt and only after a retry sleep has already ended, so a create() parked in that backoff never settles on its own. The race is what ends the wait, and the two never-settling-create tests pin it: each fires the abort only after the create call is observed in flight, so model discovery, which has its own race, cannot answer for it. The three catch sites recognize an abort through isRequestAborted(error, signal) instead of only testing our own signal. The SDK reports cancellations of its own (APIUserAbortError, or an Error whose message is exactly "Request was aborted.") that the merged signal never saw, and requesty.ts and zoo-gateway.ts already treat those shapes as aborts. Without the shared check, a caller cancellation the SDK surfaced first became an API failure and was reported to exception telemetry. The suite grew to 136 tests to kill the mutants this unit left surviving and to close the review's regression gap: two completePrompt tests leave the model lookup pending, notify when it starts, then abort or let timeoutMs elapse, and assert the canonical AbortError with no create() call; rows that park the consumer at the yield immediately before each loop guard; a buffered error frame and a quietly ended iterator after an abort; non-string reasoning fragments; an empty catalog that must not clobber a published model record; the Gemini signature-placeholder replay; the required Gemini 2.5 Pro thinking budget read from a catalog entry; and the max_tokens presence pair. Two older tests stubbed chat on OpenAI.prototype through Object.defineProperty without "writable", which left the prototype read-only for every later test and made the suite order-dependent: the prompt-cache TTL and kimi-k3 tests failed under --sequence.shuffle. They now stub the handler's own client through the shared typed helper. This unit sits on upstream main a462937b671a as one commit, with no merge commit in its history. src/test-utils/promise.ts carries the shared settlesWithin helper the review asked for. src/eslint-suppressions.json drops 34 entries because openrouter.spec.ts no longer needs the no-explicit-any suppressions: the file total goes to 2831 against main's 2865, and no file's count grows (openrouter.ts stays at 6). Verified: openrouter.spec.ts 136 passed; with openai-format.spec.ts and abort-signal.spec.ts 277 passed in declaration order and 4 of 4 runs green under --sequence.shuffle. eslint . --ext=ts --max-warnings=0 is clean both with and without --prune-suppressions; that prune run rewrote only the key order of src/eslint-suppressions.json and left every per-file count identical, so the file was restored rather than committed. tsc --noEmit exits 0 after pnpm install --frozen-lockfile, which left pnpm-lock.yaml untouched. Every changed file is prettier-clean. --- .../providers/__tests__/openrouter.spec.ts | 3459 ++++++++++++++++- src/api/providers/openrouter.ts | 860 ++-- .../transform/__tests__/openai-format.spec.ts | 71 + src/eslint-suppressions.json | 5 - src/test-utils/promise.ts | 20 + 5 files changed, 3965 insertions(+), 450 deletions(-) create mode 100644 src/test-utils/promise.ts diff --git a/src/api/providers/__tests__/openrouter.spec.ts b/src/api/providers/__tests__/openrouter.spec.ts index fec3e4a2e2c..6c62e06a8df 100644 --- a/src/api/providers/__tests__/openrouter.spec.ts +++ b/src/api/providers/__tests__/openrouter.spec.ts @@ -17,17 +17,23 @@ const MOCK_TIMEOUT_MS = 300_000 import { Anthropic } from "@anthropic-ai/sdk" import OpenAI from "openai" -import { OPENROUTER_DEFAULT_PROVIDER_NAME, openRouterDefaultModelInfo, providerIdentifiers } from "@roo-code/types" +import { + OPENROUTER_DEFAULT_PROVIDER_NAME, + openRouterDefaultModelInfo, + providerIdentifiers, + type ModelRecord, +} from "@roo-code/types" import type { ModelInfo } from "@roo-code/types" import { applyOpenRouterMoonshotK3Profile } from "../fetchers/openrouter" import { OpenRouterHandler } from "../openrouter" import { Package } from "../../../shared/package" -import { makeApiHandlerOptions } from "../../../test-utils/api" +import { makeApiHandlerOptions, makeCreateMessageMetadata } from "../../../test-utils/api" import { asyncStreamFrom, collectStream } from "../../../test-utils/stream" import { collectStreamAndParseToolCalls } from "../../../test-utils/native-tool-call-stream" import { clearAllMocks } from "../../../test-utils/reset" +import { settlesWithin } from "../../../test-utils/promise" vitest.mock("openai") vitest.mock("delay", () => ({ @@ -133,6 +139,46 @@ vitest.mock("../fetchers/modelCache", () => ({ }), })) +const ABORT_SETTLE_MS = 150 + +/** + * Installs a per-instance `chat.completions.create` double on the handler's auto-mocked client. + * The mocked `chat` slot is read-only at the prototype, so a scenario has to redefine the instance + * property; each scenario still keeps its own response fixture and assertions inline. + */ +/** + * Chat-completion chunk shaped the way OpenRouter delivers it: reasoning fragments ride on + * `reasoning_details`, and a mid-stream failure arrives as an `error` member instead of a throw. + */ +type StreamFixtureChunk = { + id?: string + choices?: { + delta: { + content?: string + reasoning?: string + reasoning_details?: unknown[] + tool_calls?: { index: number; id?: string; function?: { name?: string; arguments?: string } }[] + } + finish_reason?: string + }[] + error?: { message?: string; code?: number; metadata?: { raw?: string } } + usage?: { prompt_tokens?: number; completion_tokens?: number } +} + +/** One guarded-emit row: how far to drain the stream, the last event seen, and the chunks behind it. */ +type GuardFixtureRow = { stopBefore: string; consume: number; last: unknown; chunks: StreamFixtureChunk[] } + +function stubCompletionsCreate(handler: OpenRouterHandler, create: (...args: never[]) => unknown) { + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts, and the + // double assertion is the one place that shape is spelled out. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof create } } } + Object.defineProperty(client, "chat", { + value: { completions: { create } }, + configurable: true, + writable: true, + }) +} + describe("OpenRouterHandler", () => { const mockOptions = makeApiHandlerOptions({ openRouterApiKey: "test-key", @@ -336,6 +382,301 @@ describe("OpenRouterHandler", () => { expect(result.reasoningEffort).toBe("high") expect(result.reasoning).toEqual({ effort: "high" }) }) + + it("keeps the newest lookup's records when an older lookup settles last", async () => { + // Two lookups overlap (a second request, or a lookup whose request was + // cancelled). The first started but settles last; publishing its records + // would roll pricing and limits back to an older fetch. + const { getModels } = await import("../fetchers/modelCache") + const mockedGetModels = vitest.mocked(getModels) + let releaseFirst!: () => void + let releaseSecond!: () => void + const firstGate = new Promise((resolve) => { + releaseFirst = resolve + }) + const secondGate = new Promise((resolve) => { + releaseSecond = resolve + }) + mockedGetModels + .mockImplementationOnce(async () => { + await firstGate + return { + "anthropic/claude-sonnet-4": { ...openRouterDefaultModelInfo, maxTokens: 111 }, + } + }) + .mockImplementationOnce(async () => { + await secondGate + return { + "anthropic/claude-sonnet-4": { ...openRouterDefaultModelInfo, maxTokens: 222 }, + } + }) + + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + openRouterApiKey: "test-key", + openRouterModelId: "anthropic/claude-sonnet-4", + }), + ) + + const first = handler.fetchModel() + const second = handler.fetchModel() + + releaseSecond() + await second + releaseFirst() + await first + + expect(handler.getModel().info.maxTokens).toBe(222) + }) + + it("keeps the previous endpoint records when a newer endpoint lookup returns none", async () => { + mockGetModelEndpoints + .mockResolvedValueOnce({ + "provider-x": { ...openRouterDefaultModelInfo, maxTokens: 333 }, + }) + // A failed endpoint fetch answers an empty map. + .mockResolvedValueOnce({}) + + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + openRouterApiKey: "test-key", + openRouterModelId: "anthropic/claude-sonnet-4", + openRouterSpecificProvider: "provider-x", + }), + ) + + await handler.fetchModel() + expect(handler.getModel().info.maxTokens).toBe(333) + + // The newer lookup saw no endpoints: the records from the successful one must survive, + // otherwise the pricing and limits fall back to the base catalogue entry. + await handler.fetchModel() + expect(handler.getModel().info.maxTokens).toBe(333) + }) + + it("publishes endpoint records even when the model catalog came back empty", async () => { + // A catalog miss must not discard endpoint data: the endpoint lookup carries the + // pricing and limits for the selected provider, and getModel() prefers the endpoint + // record over the (now missing) catalog entry. + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockResolvedValueOnce({}) + mockGetModelEndpoints.mockResolvedValueOnce({ + "provider-x": { ...openRouterDefaultModelInfo, maxTokens: 444 }, + }) + + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + openRouterApiKey: "test-key", + openRouterModelId: "anthropic/claude-sonnet-4", + openRouterSpecificProvider: "provider-x", + }), + ) + + await handler.fetchModel() + + expect(handler.getModel().info.maxTokens).toBe(444) + }) + + it("keeps the published model record when a newer lookup returns an empty catalog", async () => { + // getModel() takes supportedParameters from the published catalog entry and only the endpoint + // record's limits override it, so those fields show which publication is current: an answer + // that came back empty must not roll them back to the default record. + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + openRouterApiKey: "test-key", + openRouterModelId: "anthropic/claude-sonnet-4", + openRouterSpecificProvider: "provider-x", + }), + ) + // Registered after construction so only this test's two lookups see these answers. + const { getModels } = await import("../fetchers/modelCache") + vitest + .mocked(getModels) + .mockResolvedValueOnce({ + "anthropic/claude-sonnet-4": { ...openRouterDefaultModelInfo, supportedParameters: ["max_tokens"] }, + }) + .mockResolvedValueOnce({}) + mockGetModelEndpoints + .mockResolvedValueOnce({ "provider-x": { ...openRouterDefaultModelInfo, maxTokens: 444 } }) + .mockResolvedValueOnce({ "provider-x": { ...openRouterDefaultModelInfo, maxTokens: 555 } }) + + await handler.fetchModel() + await handler.fetchModel() + + expect(handler.getModel().info.maxTokens).toBe(555) + expect(handler.getModel().info.supportedParameters).toEqual(["max_tokens"]) + }) + + it("does not let an older endpoint answer replace a newer one when both catalogs are empty", async () => { + // With no models to publish, the model sequence never advances, so endpoint ordering + // needs its own sequence: without it the slower, older lookup would overwrite the + // endpoint records the newer lookup already installed. + const { getModels } = await import("../fetchers/modelCache") + const mockedGetModels = vitest.mocked(getModels) + mockedGetModels.mockResolvedValueOnce({}).mockResolvedValueOnce({}) + + let releaseFirst!: () => void + let releaseSecond!: () => void + const firstGate = new Promise((resolve) => { + releaseFirst = resolve + }) + const secondGate = new Promise((resolve) => { + releaseSecond = resolve + }) + mockGetModelEndpoints + .mockImplementationOnce(async () => { + await firstGate + return { "provider-x": { ...openRouterDefaultModelInfo, maxTokens: 111 } } + }) + .mockImplementationOnce(async () => { + await secondGate + return { "provider-x": { ...openRouterDefaultModelInfo, maxTokens: 222 } } + }) + + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + openRouterApiKey: "test-key", + openRouterModelId: "anthropic/claude-sonnet-4", + openRouterSpecificProvider: "provider-x", + }), + ) + + const first = handler.fetchModel() + const second = handler.fetchModel() + + releaseSecond() + await second + releaseFirst() + await first + + expect(handler.getModel().info.maxTokens).toBe(222) + }) + + it("keeps an older endpoint answer when a newer lookup publishes models without endpoints", async () => { + // The endpoint sequence advances only when endpoints publish, so a newer lookup that + // failed its endpoint fetch must not freeze the ordering: the older lookup's successful + // endpoint records may still arrive afterwards and must be published. + const { getModels } = await import("../fetchers/modelCache") + const mockedGetModels = vitest.mocked(getModels) + + let releaseFirst!: () => void + let releaseSecond!: () => void + const firstGate = new Promise((resolve) => { + releaseFirst = resolve + }) + const secondGate = new Promise((resolve) => { + releaseSecond = resolve + }) + mockedGetModels + .mockImplementationOnce(async () => { + await firstGate + return {} + }) + .mockImplementationOnce(async () => { + await secondGate + return { "anthropic/claude-sonnet-4": { ...openRouterDefaultModelInfo, maxTokens: 555 } } + }) + mockGetModelEndpoints + .mockImplementationOnce(async () => { + await firstGate + return { "provider-x": { ...openRouterDefaultModelInfo, maxTokens: 111 } } + }) + .mockResolvedValueOnce({}) + + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + openRouterApiKey: "test-key", + openRouterModelId: "anthropic/claude-sonnet-4", + openRouterSpecificProvider: "provider-x", + }), + ) + + const first = handler.fetchModel() + const second = handler.fetchModel() + + releaseSecond() + await second + releaseFirst() + await first + + expect(handler.getModel().info.maxTokens).toBe(111) + }) + + it("takes model capabilities from the newest catalog and endpoint limits from the endpoint", async () => { + // The endpoint cache copies supportsReasoningEffort and supportedParameters off the catalog + // that was current when the endpoints were fetched, and endpoint records survive a lookup + // that returned none, so a retained record's capabilities can be older than the catalog. + // The provider's own limits must not be dragged along with them. + const { getModels } = await import("../fetchers/modelCache") + const catalogRecord: ModelInfo = { + ...openRouterDefaultModelInfo, + maxTokens: 9999, + supportsReasoningEffort: ["low", "high"], + supportedParameters: ["temperature", "reasoning"], + } + const endpointRecord: ModelInfo = { + ...openRouterDefaultModelInfo, + maxTokens: 4096, + contextWindow: 131072, + description: "Provider X endpoint", + supportsReasoningEffort: ["minimal"], + supportedParameters: ["max_tokens"], + } + vitest.mocked(getModels).mockResolvedValueOnce({ "anthropic/claude-sonnet-4": catalogRecord }) + mockGetModelEndpoints.mockResolvedValueOnce({ "provider-x": endpointRecord }) + + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + openRouterApiKey: "test-key", + openRouterModelId: "anthropic/claude-sonnet-4", + openRouterSpecificProvider: "provider-x", + }), + ) + + await handler.fetchModel() + + const info = handler.getModel().info + expect(info.supportsReasoningEffort).toEqual(["low", "high"]) + expect(info.supportedParameters).toEqual(["temperature", "reasoning"]) + // Provider-specific limits and metadata still come from the endpoint record. + expect(info.maxTokens).toBe(4096) + expect(info.contextWindow).toBe(131072) + expect(info.description).toBe("Provider X endpoint") + // The capability list is a copy: rendering or a later fetch must not write through to the + // catalog record the shared model cache also holds. + info.supportedParameters?.push("include_reasoning") + expect(catalogRecord.supportedParameters).toEqual(["temperature", "reasoning"]) + }) + + it("keeps the endpoint's capability copy when the catalog has no entry for the model", async () => { + // A catalog miss must not leave the selected provider without capabilities: the endpoint + // record is the only record there is, so it is used as is. + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockResolvedValueOnce({}) + const endpointRecord: ModelInfo = { + ...openRouterDefaultModelInfo, + maxTokens: 4096, + description: "Provider X endpoint", + supportsReasoningEffort: ["minimal"], + supportedParameters: ["max_tokens"], + } + mockGetModelEndpoints.mockResolvedValueOnce({ "provider-x": endpointRecord }) + + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + openRouterApiKey: "test-key", + openRouterModelId: "anthropic/claude-sonnet-4", + openRouterSpecificProvider: "provider-x", + }), + ) + + await handler.fetchModel() + + const info = handler.getModel().info + expect(info.supportsReasoningEffort).toEqual(["minimal"]) + expect(info.supportedParameters).toEqual(["max_tokens"]) + expect(info.maxTokens).toBe(4096) + }) }) describe("applyOpenRouterMoonshotK3Profile", () => { @@ -402,9 +743,14 @@ describe("OpenRouterHandler", () => { // Mock OpenAI chat.completions.create const mockCreate = vitest.fn().mockResolvedValue(mockStream) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) const systemPrompt = "test system prompt" const messages: Anthropic.Messages.MessageParam[] = [{ role: "user" as const, content: "test message" }] @@ -438,7 +784,10 @@ describe("OpenRouterHandler", () => { temperature: 0, top_p: undefined, }), - { headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } }, + { + headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" }, + signal: expect.any(AbortSignal), + }, ) }) @@ -458,9 +807,14 @@ describe("OpenRouterHandler", () => { ]) const mockCreate = vitest.fn().mockResolvedValue(mockStream) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) const messages: Anthropic.Messages.MessageParam[] = [ { role: "user", content: "message 1" }, @@ -481,7 +835,10 @@ describe("OpenRouterHandler", () => { }), ]), }), - { headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } }, + { + headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" }, + signal: expect.any(AbortSignal), + }, ) }) @@ -599,9 +956,14 @@ describe("OpenRouterHandler", () => { const mockStream = asyncStreamFrom([{ error: { message: "API Error", code: 500 } }]) const mockCreate = vitest.fn().mockResolvedValue(mockStream) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) const generator = handler.createMessage("test", []) await expect(generator.next()).rejects.toThrow("OpenRouter API Error 500: API Error") @@ -621,9 +983,14 @@ describe("OpenRouterHandler", () => { it("captures telemetry when createMessage throws an exception", async () => { const handler = new OpenRouterHandler(mockOptions) const mockCreate = vitest.fn().mockRejectedValue(new Error("Connection failed")) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) const generator = handler.createMessage("test", []) await expect(generator.next()).rejects.toThrow() @@ -640,13 +1007,17 @@ describe("OpenRouterHandler", () => { it("passes SDK exceptions with status 429 to telemetry (filtering happens in PostHogTelemetryClient)", async () => { const handler = new OpenRouterHandler(mockOptions) - const error = new Error("Rate limit exceeded: free-models-per-day") as any - error.status = 429 + const error = Object.assign(new Error("Rate limit exceeded: free-models-per-day"), { status: 429 }) const mockCreate = vitest.fn().mockRejectedValue(error) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) const generator = handler.createMessage("test", []) await expect(generator.next()).rejects.toThrow("Rate limit exceeded") @@ -665,9 +1036,14 @@ describe("OpenRouterHandler", () => { const handler = new OpenRouterHandler(mockOptions) const error = new Error("429 Rate limit exceeded: free-models-per-day") const mockCreate = vitest.fn().mockRejectedValue(error) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) const generator = handler.createMessage("test", []) await expect(generator.next()).rejects.toThrow("429 Rate limit exceeded") @@ -686,9 +1062,14 @@ describe("OpenRouterHandler", () => { const handler = new OpenRouterHandler(mockOptions) const error = new Error("Request failed due to rate limit") const mockCreate = vitest.fn().mockRejectedValue(error) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) const generator = handler.createMessage("test", []) await expect(generator.next()).rejects.toThrow("rate limit") @@ -708,9 +1089,14 @@ describe("OpenRouterHandler", () => { const mockStream = asyncStreamFrom([{ error: { message: "Rate limit exceeded", code: 429 } }]) const mockCreate = vitest.fn().mockResolvedValue(mockStream) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) const generator = handler.createMessage("test", []) await expect(generator.next()).rejects.toThrow("OpenRouter API Error 429: Rate limit exceeded") @@ -765,9 +1151,14 @@ describe("OpenRouterHandler", () => { ]) const mockCreate = vitest.fn().mockResolvedValue(mockStream) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) const generator = handler.createMessage("test", []) const parserScope = NativeToolCallParser.createScope() @@ -872,11 +1263,8 @@ describe("OpenRouterHandler", () => { { id: "stream", choices: [{ delta: {}, finish_reason: "tool_calls", index: 0 }] }, ]), ) - Object.defineProperty(OpenAI.prototype, "chat", { - configurable: true, - value: { completions: { create: mockCreate } }, - }) const handler = new OpenRouterHandler(mockOptions) + stubCompletionsCreate(handler, mockCreate) const idlessChunks = await collectStream(handler.createMessage("idless", [])) const stoppedChunks = await collectStream(handler.createMessage("stopped", [])) @@ -944,11 +1332,8 @@ describe("OpenRouterHandler", () => { { id: "stream-b", choices: [{ delta: {}, finish_reason: "tool_calls", index: 0 }] }, ]) const mockCreate = vitest.fn().mockResolvedValueOnce(firstStream()).mockResolvedValueOnce(secondStream) - Object.defineProperty(OpenAI.prototype, "chat", { - configurable: true, - value: { completions: { create: mockCreate } }, - }) const handler = new OpenRouterHandler(mockOptions) + stubCompletionsCreate(handler, mockCreate) const firstChunksPromise = collectStreamAndParseToolCalls(handler.createMessage("first", [])) await firstStreamPaused @@ -971,115 +1356,2402 @@ describe("OpenRouterHandler", () => { { type: "tool_call_delta", id: "call_openrouter_b", name: "read_file", delta: '{"path":"b' }, ]) }) - }) - describe("completePrompt", () => { - it("returns correct response", async () => { + // The OpenRouter-shaped branch: OpenRouter can carry an error object inside the SSE stream instead + // of throwing. The SDK-thrown branch (openai@5.23.2 raises APIError before yielding the frame) is + // covered by the rejecting-create tests and by the iterator-throws test in the cancellation block. + it("throws an OpenRouter API error for an error frame carried inside the stream", async () => { const handler = new OpenRouterHandler(mockOptions) - const mockResponse = { choices: [{ message: { content: "test completion" } }] } - - const mockCreate = vitest.fn().mockResolvedValue(mockResponse) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any - - const result = await handler.completePrompt("test prompt") - - expect(result).toBe("test completion") - - expect(mockCreate).toHaveBeenCalledWith( - { - model: mockOptions.openRouterModelId, - max_tokens: 8192, - temperature: 0, - messages: [{ role: "user", content: "test prompt" }], - stream: false, - }, - { headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } }, + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { id: "1", choices: [{ delta: { content: "ok" } }] }, + { + id: "1", + error: { + message: "Upstream failed", + code: 500, + metadata: { raw: '{"message":"upstream: boom"}' }, + }, + }, + ]), ) - }) - - it("handles API errors and captures telemetry", async () => { - const handler = new OpenRouterHandler(mockOptions) - const mockError = { - error: { - message: "API Error", - code: 500, - }, - } - - const mockCreate = vitest.fn().mockResolvedValue(mockError) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) - await expect(handler.completePrompt("test prompt")).rejects.toThrow("OpenRouter API Error 500: API Error") + const stream = handler.createMessage("system", [{ role: "user" as const, content: "hi" }]) - // Verify telemetry was captured + await expect(collectStream(stream)).rejects.toThrow("OpenRouter API Error 500: upstream: boom") expect(mockCaptureException).toHaveBeenCalledWith( expect.objectContaining({ - message: "API Error", + message: "upstream: boom", provider: providerIdentifiers.openrouter, - modelId: mockOptions.openRouterModelId, - operation: "completePrompt", - errorCode: 500, - status: 500, + modelId: "anthropic/claude-sonnet-4", + operation: "createMessage", }), ) }) - - it("handles unexpected errors and captures telemetry", async () => { + it("rejects with AbortError when the external signal is pre-aborted", async () => { const handler = new OpenRouterHandler(mockOptions) - const error = new Error("Unexpected error") - const mockCreate = vitest.fn().mockRejectedValue(error) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any + const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "response" } }] }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) - await expect(handler.completePrompt("test prompt")).rejects.toThrow("Unexpected error") + // The constructor loads models in the background; the pre-aborted path must fail + // fast without starting a second discovery lookup. + const { getModels } = await import("../fetchers/modelCache") + const lookupsBefore = vitest.mocked(getModels).mock.calls.length - // Verify telemetry was captured (filtering now happens inside PostHogTelemetryClient) - expect(mockCaptureException).toHaveBeenCalledWith( - expect.objectContaining({ - message: "Unexpected error", - provider: providerIdentifiers.openrouter, - modelId: mockOptions.openRouterModelId, - operation: "completePrompt", - }), - ) + const controller = new AbortController() + controller.abort() + const metadata = makeCreateMessageMetadata({ abortSignal: controller.signal }) + + await expect( + handler.createMessage("test", [{ role: "user" as const, content: "hi" }], metadata).next(), + ).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + expect(mockCreate).not.toHaveBeenCalled() + expect(vitest.mocked(getModels).mock.calls.length).toBe(lookupsBefore) }) - it("passes SDK exceptions with status 429 to telemetry (filtering happens in PostHogTelemetryClient)", async () => { + it("rejects with AbortError when the external signal aborts during deferred model discovery", async () => { const handler = new OpenRouterHandler(mockOptions) - const error = new Error("Rate limit exceeded: free-models-per-day") as any - error.status = 429 - const mockCreate = vitest.fn().mockRejectedValue(error) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any + const controller = new AbortController() - await expect(handler.completePrompt("test prompt")).rejects.toThrow("Rate limit exceeded") + const mockCreate = vitest.fn() + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) - // captureException is called, but PostHogTelemetryClient filters out 429 errors internally - expect(mockCaptureException).toHaveBeenCalledWith( - expect.objectContaining({ - message: "Rate limit exceeded: free-models-per-day", - provider: providerIdentifiers.openrouter, - modelId: mockOptions.openRouterModelId, - operation: "completePrompt", - }), - ) + // Model discovery is deferred: capture the resolver and settle it only at the end of + // the test, so the abort deterministically lands while the lookup is still pending. + // The barrier below (instead of a fixed sleep) synchronizes on the lookup starting. + let resolveModelLookup!: (models: ModelRecord) => void + const deferredModelLookup = new Promise((resolve) => { + resolveModelLookup = resolve + }) + let notifyLookupStarted!: () => void + const lookupStarted = new Promise((resolve) => { + notifyLookupStarted = resolve + }) + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockImplementationOnce(() => { + notifyLookupStarted() + return deferredModelLookup + }) + + const metadata = makeCreateMessageMetadata({ abortSignal: controller.signal }) + const generator = handler.createMessage("test", [{ role: "user" as const, content: "hi" }], metadata) + + const nextPromise = generator.next() + await settlesWithin(lookupStarted, ABORT_SETTLE_MS) + controller.abort() + + await expect(settlesWithin(nextPromise, ABORT_SETTLE_MS)).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + expect(mockCreate).not.toHaveBeenCalled() + + // Settle the abandoned lookup so it cannot outlive the test. + resolveModelLookup({}) }) - it("passes SDK exceptions with 429 in message to telemetry (filtering happens in PostHogTelemetryClient)", async () => { + it("aborts the in-flight stream and rejects with AbortError when the external signal aborts", async () => { const handler = new OpenRouterHandler(mockOptions) - const error = new Error("429 Rate limit exceeded: free-models-per-day") - const mockCreate = vitest.fn().mockRejectedValue(error) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any + const controller = new AbortController() - await expect(handler.completePrompt("test prompt")).rejects.toThrow("429 Rate limit exceeded") + let requestSignal: AbortSignal | undefined + const mockCreate = vitest + .fn() + .mockImplementation(async (_params: unknown, options?: { signal?: AbortSignal }) => { + requestSignal = options?.signal + // Emulate the OpenAI SDK: the first chunk arrives, then the in-flight + // response body rejects once the request signal aborts. + return (async function* () { + yield { id: "1", choices: [{ delta: { content: "first" } }] } + await new Promise((resolve) => { + expect(requestSignal).toBeDefined() + if (requestSignal!.aborted) { + resolve() + } else { + requestSignal!.addEventListener("abort", () => resolve(), { once: true }) + } + }) + const abortError = new Error("The user aborted a request") + abortError.name = "AbortError" + throw abortError + })() + }) + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const metadata = makeCreateMessageMetadata({ abortSignal: controller.signal }) + const generator = handler.createMessage("test", [{ role: "user" as const, content: "hi" }], metadata) + + const chunks: unknown[] = [] + const iteration = (async () => { + for await (const chunk of generator) { + chunks.push(chunk) + if (chunk.type === "text") { + // Abort while the stream is still in flight. + controller.abort() + } + } + })() + + await expect(settlesWithin(iteration, ABORT_SETTLE_MS)).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + expect(chunks).toContainEqual({ type: "text", text: "first" }) + }) + it("cancels the in-flight stream when the consumer abandons the generator", async () => { + const handler = new OpenRouterHandler(mockOptions) + + // Emulate the OpenAI SDK: the first chunk arrives, then the response body + // stalls until the request signal aborts — no further chunk arrives on its own. + let requestSignal: AbortSignal | undefined + const mockCreate = vitest + .fn() + .mockImplementation(async (_params: unknown, options?: { signal?: AbortSignal }) => { + requestSignal = options?.signal + return (async function* () { + yield { id: "1", choices: [{ delta: { content: "first" } }] } + await new Promise((resolve) => { + expect(requestSignal).toBeDefined() + if (requestSignal!.aborted) { + resolve() + } else { + requestSignal!.addEventListener("abort", () => resolve(), { once: true }) + } + }) + yield { id: "2", choices: [{ delta: { content: "second" } }] } + })() + }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const generator = handler.createMessage("test", [{ role: "user" as const, content: "hi" }]) + + const first = await settlesWithin(generator.next(), ABORT_SETTLE_MS) + expect(first.value).toEqual({ type: "text", text: "first" }) + expect(requestSignal?.aborted).toBe(false) + + // Abandon the generator mid-stream: the finally block must abort the per-request + // controller so the in-flight stream is cancelled instead of lingering until + // the client-level timeout. + await settlesWithin(generator.return(undefined), ABORT_SETTLE_MS) + expect(requestSignal?.aborted).toBe(true) + }) + it("does not emit buffered chunks after a mid-stream abort (iterator keeps delivering)", async () => { + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + + // Simulate openai@5.23.2 delivering a buffered chunk after the abort has already + // fired, then ending the iterator normally (no throw). + let requestSignal: AbortSignal | undefined + const mockCreate = vitest + .fn() + .mockImplementation(async (_params: unknown, options?: { signal?: AbortSignal }) => { + requestSignal = options?.signal + return (async function* () { + yield { id: "1", choices: [{ delta: { content: "partial" } }] } + // Wait for the abort instead of polling: the buffered chunk is delivered + // once the request signal aborts. + await new Promise((resolve) => { + expect(requestSignal).toBeDefined() + if (requestSignal!.aborted) { + resolve() + } else { + requestSignal!.addEventListener("abort", () => resolve(), { once: true }) + } + }) + yield { id: "2", choices: [{ delta: { content: "after-abort" } }] } + })() + }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const metadata = makeCreateMessageMetadata({ abortSignal: controller.signal }) + const generator = handler.createMessage("test", [{ role: "user" as const, content: "hi" }], metadata) + + const first = await generator.next() + expect(first.value).toEqual({ type: "text", text: "partial" }) + // Abort mid-stream, after the first chunk has been yielded. + controller.abort() + + // The buffered second chunk must not be emitted, and the generator must reject + // with the provider AbortError. + await expect(settlesWithin(generator.next(), ABORT_SETTLE_MS)).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + }) + // Each row parks the consumer at the yield that immediately precedes one of the loop's abort + // guards, so the guard itself is what has to reject: without it the next() below resolves with + // the extra event and the cancellation only surfaces a step later. + const guardFixtureRows: GuardFixtureRow[] = [ + { + stopBefore: "the next reasoning_details fragment", + consume: 1, + last: { type: "reasoning", text: "A" }, + chunks: [ + { + id: "1", + choices: [ + { + delta: { + reasoning_details: [ + { index: 0, type: "reasoning.text", text: "A" }, + { index: 0, type: "reasoning.text", text: "B" }, + ], + }, + }, + ], + }, + ], + }, + { + stopBefore: "a tool_call_partial chunk", + consume: 1, + last: { type: "reasoning", text: "A" }, + chunks: [ + { + id: "1", + choices: [ + { + delta: { + reasoning_details: [{ index: 0, type: "reasoning.text", text: "A" }], + tool_calls: [{ index: 0, id: "call_1", function: { name: "f", arguments: "{}" } }], + }, + }, + ], + }, + ], + }, + { + stopBefore: "the next chunk from the SDK", + consume: 1, + last: { type: "text", text: "A" }, + chunks: [ + { id: "1", choices: [{ delta: { content: "A" } }] }, + { id: "2", choices: [{ delta: { content: "B" } }] }, + ], + }, + { + stopBefore: "the tool_call_end of a tool_calls finish", + consume: 2, + last: { type: "text", text: "A" }, + chunks: [ + { id: "1", choices: [{ delta: { tool_calls: [{ index: 0, id: "call_1" }] } }] }, + { id: "2", choices: [{ delta: { content: "A" }, finish_reason: "tool_calls" }] }, + ], + }, + ] + it.each(guardFixtureRows)( + "rejects instead of emitting $stopBefore when the caller aborts during the previous yield", + async ({ consume, last, chunks }) => { + const handler = new OpenRouterHandler(mockOptions) + stubCompletionsCreate(handler, vitest.fn().mockResolvedValue(asyncStreamFrom(chunks))) + const controller = new AbortController() + const metadata = makeCreateMessageMetadata({ abortSignal: controller.signal }) + const iterator = handler + .createMessage("system", [{ role: "user" as const, content: "hi" }], metadata) + [Symbol.asyncIterator]() + + let pending: unknown + for (let index = 0; index < consume; index++) { + pending = (await iterator.next()).value + } + expect(pending).toEqual(last) + + controller.abort() + await expect(iterator.next()).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + }, + ) + + it("reports the provider AbortError rather than an API error for an error frame buffered behind the abort", async () => { + // openai@5.23.2 can still deliver a buffered SSE frame after the abort fired, and + // OpenRouter signals failures as an error object inside the stream. The per-chunk guard + // has to reject that frame as a cancellation before the OpenRouter error mapping runs. + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { id: "1", choices: [{ delta: { content: "partial" } }] }, + { + id: "2", + error: { + message: "Upstream failed", + code: 500, + metadata: { raw: '{"message":"upstream: boom"}' }, + }, + }, + ]), + ) + stubCompletionsCreate(handler, mockCreate) + + const generator = handler.createMessage( + "system", + [{ role: "user" as const, content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + const first = await generator.next() + expect(first.value).toEqual({ type: "text", text: "partial" }) + + controller.abort() + await expect(settlesWithin(generator.next(), ABORT_SETTLE_MS)).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + // The cancellation has to win before the OpenRouter error mapping runs: reporting the buffered + // frame as an API failure would put a user-initiated abort into exception telemetry. + expect(mockCaptureException).not.toHaveBeenCalled() + expect(mockCreate).toHaveBeenCalledTimes(1) + }) + + it("rejects with the provider AbortError when the SDK iterator ends quietly after an abort", async () => { + // The SDK's stream iterator swallows a mid-stream cancellation and returns normally, so a + // request can end with no further chunk and no error. Only the post-loop check stands + // between that quiet end and a silently completed request with partial output. + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + stubCompletionsCreate( + handler, + vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { id: "1", choices: [{ delta: { content: "partial" } }] }, + { + id: "2", + choices: [{ delta: { content: "tail" }, finish_reason: "stop" }], + usage: { prompt_tokens: 3, completion_tokens: 2 }, + }, + ]), + ), + ) + + const generator = handler.createMessage( + "system", + [{ role: "user" as const, content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + expect((await generator.next()).value).toEqual({ type: "text", text: "partial" }) + expect((await generator.next()).value).toEqual({ type: "text", text: "tail" }) + + controller.abort() + await expect(settlesWithin(generator.next(), ABORT_SETTLE_MS)).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + }) + + it("settles the create await as AbortError while the SDK is parked in a retry backoff", async () => { + // openai@5.23.2 inspects the request signal before the first attempt and only after a + // retry sleep has already ended, so a create() that is asleep never settles: the race in + // settleOnAbort is what ends the wait on a cancellation or timeout. + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + let createStarted: () => void + const createAwaited = new Promise((resolve) => { + createStarted = resolve + }) + stubCompletionsCreate( + handler, + vitest.fn().mockImplementation(() => { + createStarted() + return new Promise(() => {}) + }), + ) + + const generator = handler.createMessage( + "system", + [{ role: "user" as const, content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + const next = settlesWithin(generator.next(), ABORT_SETTLE_MS) + // Only abort once the create call is in flight: model discovery has its own abort race, and + // aborting earlier would settle the generator there instead of at the create await. + await createAwaited + controller.abort() + + await expect(next).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + }) + + it("normalizes the SDK's own abort error into the provider AbortError instead of a completion error", async () => { + // The SDK reports a cancellation as APIUserAbortError("Request was aborted.") even when + // the merged request signal never fired, and that shape does not satisfy the task abort + // contract, so the create catch normalizes it and skips exception telemetry. + const handler = new OpenRouterHandler(mockOptions) + const sdkAbort = Object.assign(new Error("Request was aborted."), { + name: "APIUserAbortError", + status: 499, + }) + stubCompletionsCreate(handler, vitest.fn().mockRejectedValue(sdkAbort)) + + await expect( + collectStream(handler.createMessage("system", [{ role: "user" as const, content: "hi" }])), + ).rejects.toMatchObject({ name: "AbortError", message: "The OpenRouter request was aborted" }) + expect(mockCaptureException).not.toHaveBeenCalled() + }) + + it("propagates the error the SDK throws while iterating the stream", async () => { + // A provider failure that the SDK raises from inside the SSE iterator (rather than as an + // error frame) reaches the iteration catch, which rethrows it unchanged and reports it. + const handler = new OpenRouterHandler(mockOptions) + const iteratorError = Object.assign(new Error("upstream exploded"), { status: 500 }) + const partialThenThrow = async function* () { + yield { id: "1", choices: [{ delta: { content: "partial" } }] } + throw iteratorError + } + stubCompletionsCreate(handler, vitest.fn().mockResolvedValue(partialThenThrow())) + + await expect( + collectStream(handler.createMessage("system", [{ role: "user" as const, content: "hi" }])), + ).rejects.toBe(iteratorError) + expect(mockCaptureException).not.toHaveBeenCalled() + }) + + it("normalizes an abort the SDK reports during iteration when the request signal never fired", async () => { + // Same iterator, abort-shaped failure: without the normalization this raw SDK error (name + // "Error", message "Request was aborted.") would reach the task loop unnormalized. + const handler = new OpenRouterHandler(mockOptions) + stubCompletionsCreate( + handler, + vitest.fn().mockResolvedValue( + (async function* () { + yield { id: "1", choices: [{ delta: { content: "partial" } }] } + throw new Error("Request was aborted.") + })(), + ), + ) + + await expect( + collectStream(handler.createMessage("system", [{ role: "user" as const, content: "hi" }])), + ).rejects.toMatchObject({ name: "AbortError", message: "The OpenRouter request was aborted" }) + }) + + it("registers the external abort listener with { once: true } and removes the exact reference on completion", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const controller = new AbortController() + const addSpy = vi.spyOn(controller.signal, "addEventListener") + const removeSpy = vi.spyOn(controller.signal, "removeEventListener") + const metadata = makeCreateMessageMetadata({ abortSignal: controller.signal }) + + const stream = handler.createMessage("test", [{ role: "user" as const, content: "hi" }], metadata) + await collectStream(stream) + + // The listener must be registered once with self-removal, and the exact + // registered reference must be removed once the request completes. + const registeredListener = addSpy.mock.calls[0]?.[1] + expect(registeredListener).toBeTypeOf("function") + expect(addSpy).toHaveBeenCalledWith("abort", registeredListener, { once: true }) + expect(removeSpy).toHaveBeenCalledWith("abort", registeredListener) + addSpy.mockRestore() + removeSpy.mockRestore() + }) + + it("resets the reasoning_details accumulator between requests on the same handler", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "google/gemini-2.5-pro", + }), + ) + const mockCreate = vitest.fn() + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + // Request 1: a Gemini stream with reasoning_details populates the handler's + // accumulator when the stream completes. + mockCreate.mockResolvedValueOnce( + asyncStreamFrom([ + { + id: "1", + choices: [ + { delta: { reasoning_details: [{ index: 0, type: "reasoning.text", text: "thinking" }] } }, + ], + }, + { id: "2", choices: [{ delta: { content: "answer" } }] }, + ]), + ) + await collectStream(handler.createMessage("test", [{ role: "user" as const, content: "hi" }])) + expect(handler.getReasoningDetails()?.length ?? 0).toBeGreaterThan(0) + + // Request 2 on the same handler must start from an empty accumulator so the + // previous request's details cannot leak into the next one. + mockCreate.mockResolvedValueOnce(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + await collectStream(handler.createMessage("test", [{ role: "user" as const, content: "hi" }])) + expect(handler.getReasoningDetails()).toBeUndefined() + }) + + it("stops yielding a chunk's remaining events when the consumer aborts between them", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { + id: "1", + choices: [ + { + delta: { + reasoning_details: [{ index: 0, type: "reasoning.text", text: "thinking" }], + content: "answer", + }, + }, + ], + }, + ]), + ) + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const controller = new AbortController() + const iterator = handler + .createMessage("system", [{ role: "user" as const, content: "hi" }], { + taskId: "task", + abortSignal: controller.signal, + }) + [Symbol.asyncIterator]() + + // One SDK chunk yields reasoning BEFORE text: abort while the generator is paused + // between the two yields, so the text must not be emitted. + expect((await iterator.next()).value).toEqual({ type: "reasoning", text: "thinking" }) + controller.abort() + await expect(iterator.next()).rejects.toMatchObject({ name: "AbortError" }) + }) + + it("skips non-string reasoning detail fragments instead of coercing them", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { + id: "1", + choices: [ + { + delta: { + reasoning_details: [ + { index: 0, type: "reasoning.text", text: { not: "a string" } }, + { index: 1, type: "reasoning.summary", summary: { not: "a string" } }, + { index: 2, type: "reasoning.encrypted", data: { not: "a string" } }, + { index: 0, type: "reasoning.text", text: "ok" }, + ], + }, + }, + { id: "2", choices: [{ delta: { content: "answer" } }] }, + ], + }, + ]), + ) + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + await collectStream(handler.createMessage("system", [{ role: "user" as const, content: "hi" }])) + const details = handler.getReasoningDetails() + const text = details?.find((d) => d.type === "reasoning.text")?.text + // The object fragment is skipped and the later string accumulates from empty. + expect(text).toBe("ok") + expect(text).not.toContain("[object Object]") + // The same rule guards summary and encrypted: a stored object would be replayed to the provider + // as a summary block or as a non-string encrypted payload. + expect(details?.find((detail) => detail.type === "reasoning.summary")).toBeUndefined() + expect(details?.find((detail) => detail.type === "reasoning.encrypted")).toBeUndefined() + }) + + it.each([ + { type: "reasoning.text", field: "text", good: "ok", emitsReasoning: true }, + { type: "reasoning.summary", field: "summary", good: "s", emitsReasoning: true }, + { type: "reasoning.encrypted", field: "data", good: "enc", emitsReasoning: false }, + ])( + "keeps the valid fragment when a non-string $type fragment arrives after it at the same index", + async ({ type, field, good, emitsReasoning }) => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { + id: "1", + choices: [ + { delta: { reasoning_details: [{ index: 0, type, [field]: good }] } }, + { delta: { reasoning_details: [{ index: 0, type, [field]: { not: "a string" } }] } }, + ], + }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "hi" }]), + ) + // The valid fragment is the only displayable one; the non-string fragment yields nothing. + expect(chunks).toEqual(emitsReasoning ? [{ type: "reasoning", text: good }] : []) + + const details = handler.getReasoningDetails() + expect(details).toHaveLength(1) + expect(details?.[0]).toMatchObject({ type, index: 0, [field]: good }) + expect(JSON.stringify(details)).not.toContain("[object Object]") + }, + ) + + it("excludes reasoning for Gemini 2.5 Pro models by default", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "google/gemini-2.5-pro-preview", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const stream = handler.createMessage("system", [{ role: "user" as const, content: "hi" }]) + await collectStream(stream) + + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ reasoning: { exclude: true } }), + expect.any(Object), + ) + }) + + // The default-record branch: the mocked catalog has no google/gemini-2.5-pro entry, so getModel() + // serves openRouterDefaultModelInfo and the exclusion comes from that fallback record. The two + // cases below are the catalog-backed ones - the required budget and a configured budget. + it("excludes reasoning for Gemini 2.5 Pro when the catalog has no entry for it", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "google/gemini-2.5-pro", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const stream = handler.createMessage("system", [{ role: "user" as const, content: "hi" }]) + await collectStream(stream) + + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ reasoning: { exclude: true } }), + expect.any(Object), + ) + }) + + it("does not inject reasoning exclusion for non-Gemini models without configured reasoning", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "openai/gpt-4o", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const stream = handler.createMessage("system", [{ role: "user" as const, content: "hi" }]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { reasoning?: unknown } + expect(params.reasoning).toBeUndefined() + }) + + it("keeps user-configured reasoning for Gemini 2.5 Pro models instead of overriding it", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "google/gemini-2.5-pro", + enableReasoningEffort: true, + modelMaxThinkingTokens: 1024, + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + // The constructor already consumed the default model-cache mock; serve a Gemini + // entry that advertises reasoning-budget support to fetchModel so getModelParams + // resolves a user reasoning budget for this request. + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockImplementationOnce(async () => ({ + "google/gemini-2.5-pro": { + maxTokens: 65536, + contextWindow: 1048576, + supportsPromptCache: false, + supportsReasoningBudget: true, + }, + })) + + const stream = handler.createMessage("system", [{ role: "user" as const, content: "hi" }]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { reasoning?: { max_tokens?: number; exclude?: boolean } } + // 1024 is the user's modelMaxThinkingTokens, not the 128 Gemini 2.5 Pro default, so this pins + // the configured value reaching the request instead of a default that happens to fit. + expect(params.reasoning).toEqual({ max_tokens: 1024 }) + }) + + it("resolves the required Gemini 2.5 Pro thinking budget from a catalog entry", async () => { + // The OpenRouter fetcher marks google/gemini-2.5-pro required-reasoning-budget + // (src/api/providers/fetchers/openrouter.ts:304-306 over + // OPEN_ROUTER_REQUIRED_REASONING_BUDGET_MODELS), so a published entry already resolves a + // thinking budget by the time createMessage checks the Gemini default: the exclusion is a + // catalog-miss fallback, not this model's normal shape. + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ ...mockOptions, openRouterModelId: "google/gemini-2.5-pro" }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + stubCompletionsCreate(handler, mockCreate) + + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockImplementationOnce(async () => ({ + "google/gemini-2.5-pro": { + maxTokens: 65536, + contextWindow: 1048576, + supportsPromptCache: false, + requiredReasoningBudget: true, + }, + })) + + await collectStream(handler.createMessage("system", [{ role: "user" as const, content: "hi" }])) + + const params = mockCreate.mock.calls[0][0] as { reasoning?: { max_tokens?: number; exclude?: boolean } } + expect(params.reasoning).toEqual({ max_tokens: 128 }) + }) + + it("passes an assistant turn without tool calls through the Gemini injection untouched", async () => { + // The fake encrypted block exists only to satisfy Gemini tool-call validation, so a plain + // assistant turn must reach the request exactly as converted: injecting unconditionally would + // read the first element of a tool_calls array this message does not have. + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ ...mockOptions, openRouterModelId: "google/gemini-2.5-pro" }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + stubCompletionsCreate(handler, mockCreate) + + await collectStream( + handler.createMessage("system", [ + { role: "user" as const, content: "question" }, + { role: "assistant" as const, content: [{ type: "text" as const, text: "plain reply" }] }, + ]), + ) + + const params = mockCreate.mock.calls[0][0] as { + messages: { role: string; content: unknown; tool_calls?: unknown; reasoning_details?: unknown }[] + } + const assistant = params.messages.find((message) => message.role === "assistant") + expect(assistant?.content).toBe("plain reply") + expect(assistant && "tool_calls" in assistant).toBe(false) + expect(assistant && "reasoning_details" in assistant).toBe(false) + }) + + it("replays a Gemini assistant tool turn with the details it kept plus the signature placeholder", async () => { + // The placeholder entry exists only to get past Gemini tool-call validation on a turn whose + // kept details carry no encrypted block, and it has to be appended to those details instead of + // standing in for them. + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ ...mockOptions, openRouterModelId: "google/gemini-2.5-pro" }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + stubCompletionsCreate(handler, mockCreate) + + const assistantTurn: Anthropic.Messages.MessageParam & { + reasoning_details: { id: string; type: string; text: string; index: number }[] + } = { + role: "assistant", + content: [{ type: "tool_use", id: "call_1", name: "lookup", input: {} }], + reasoning_details: [{ id: "call_1", type: "reasoning.text", text: "thinking", index: 0 }], + } + + await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "question" }, assistantTurn]), + ) + + const params = mockCreate.mock.calls[0][0] as { + messages: { + role: string + tool_calls?: { id: string }[] + reasoning_details?: { type: string; id?: string; data?: string; text?: string }[] + }[] + } + const assistant = params.messages.find((message) => message.role === "assistant") + expect(assistant?.tool_calls?.map((toolCall) => toolCall.id)).toEqual(["call_1"]) + expect(assistant?.reasoning_details).toEqual([ + // The sanitizer consolidates the kept detail, which stamps the provider format it came + // from; only the placeholder entry is added by the injection below it. + { type: "reasoning.text", text: "thinking", id: "call_1", index: 0, format: "unknown" }, + { + type: "reasoning.encrypted", + data: "skip_thought_signature_validator", + id: "call_1", + format: "google-gemini-v1", + index: 0, + }, + ]) + }) + + it.each([ + { modelId: "anthropic/claude-sonnet-4", maxTokens: 8192, sent: 8192 }, + { modelId: "openai/gpt-4o-mini", maxTokens: undefined, sent: undefined }, + ])( + "sends max_tokens only when the resolved model has a positive output budget ($modelId)", + async ({ modelId, maxTokens, sent }) => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ openRouterApiKey: "test-key", openRouterModelId: modelId }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + stubCompletionsCreate(handler, mockCreate) + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockResolvedValueOnce({ + [modelId]: { + contextWindow: 200_000, + supportsPromptCache: true, + ...(maxTokens === undefined ? {} : { maxTokens }), + }, + }) + + await collectStream(handler.createMessage("system", [{ role: "user" as const, content: "hi" }])) + + const params = mockCreate.mock.calls[0][0] as { max_tokens?: number } + // A model without an output budget must omit the key rather than send max_tokens as + // undefined, which OpenRouter reads as its own default. + expect("max_tokens" in params).toBe(sent !== undefined) + expect(params.max_tokens).toBe(sent) + }, + ) + + it("omits the anthropic beta header for non-Anthropic models", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "openai/gpt-4o", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const stream = handler.createMessage("system", [{ role: "user" as const, content: "hi" }]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { messages: { role: string; content: unknown }[] } + expect(params.messages[0]).toEqual({ role: "system", content: "system" }) + + const options = mockCreate.mock.calls[0][1] as { headers?: Record; signal?: AbortSignal } + expect(options).not.toHaveProperty("headers") + expect(options.signal).toBeInstanceOf(AbortSignal) + }) + + it("uses user role for the system prompt with DeepSeek R1 models", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "deepseek/deepseek-r1", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const stream = handler.createMessage("system prompt", [{ role: "user" as const, content: "hi" }]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { messages: { role: string; content: unknown }[] } + expect(params.messages[0].role).toBe("user") + expect(params.messages.map((m) => m.role)).not.toContain("system") + }) + + it("uses user role for the system prompt with Perplexity Sonar Reasoning", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "perplexity/sonar-reasoning", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const stream = handler.createMessage("system prompt", [{ role: "user" as const, content: "hi" }]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { messages: { role: string; content: unknown }[] } + expect(params.messages[0].role).toBe("user") + expect(params.messages.map((m) => m.role)).not.toContain("system") + }) + + it("pins provider order and only for a specific openRouterSpecificProvider", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterSpecificProvider: providerIdentifiers.anthropic, + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const stream = handler.createMessage("system", [{ role: "user" as const, content: "hi" }]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { + provider?: { order?: string[]; only?: string[]; allow_fallbacks?: boolean } + } + expect(params.provider).toEqual({ + order: [providerIdentifiers.anthropic], + only: [providerIdentifiers.anthropic], + allow_fallbacks: false, + }) + }) + + it("omits the provider option when openRouterSpecificProvider is [default]", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterSpecificProvider: "[default]", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const stream = handler.createMessage("system", [{ role: "user" as const, content: "hi" }]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { provider?: unknown } + expect(params).not.toHaveProperty("provider") + }) + + it("omits the provider option when openRouterSpecificProvider is unset", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const stream = handler.createMessage("system", [{ role: "user" as const, content: "hi" }]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { provider?: unknown } + expect(params).not.toHaveProperty("provider") + }) + + it("injects a fake encrypted reasoning block for Gemini tool calls without encrypted reasoning", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "google/gemini-2.5-flash", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + // reasoning_details is an OpenRouter extension field round-tripped on assistant + // messages; the Anthropic SDK types do not include it, hence the structural cast. + const assistantMessage = { + role: "assistant" as const, + content: [{ type: "tool_use" as const, id: "toolu_01", name: "get_weather", input: { city: "SF" } }], + reasoning_details: [{ type: "reasoning.text", id: "toolu_01", text: "thinking", index: 0 }], + } + const stream = handler.createMessage("system", [ + assistantMessage as unknown as Anthropic.Messages.MessageParam, + ]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { + messages: { + role: string + tool_calls?: { id: string }[] + reasoning_details?: { type: string; id: string; data: string }[] + }[] + } + const assistant = params.messages.find((m) => m.role === "assistant") + expect(assistant?.tool_calls).toHaveLength(1) + const encrypted = assistant?.reasoning_details?.find((d) => d.type === "reasoning.encrypted") + expect(encrypted).toMatchObject({ + id: "toolu_01", + data: "skip_thought_signature_validator", + format: "google-gemini-v1", + index: 0, + }) + }) + + it("keeps the matching reasoning detail when sanitizing Gemini tool call messages", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "google/gemini-2.5-flash", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const assistantMessage = { + role: "assistant" as const, + content: [{ type: "tool_use" as const, id: "toolu_01", name: "get_weather", input: { city: "SF" } }], + reasoning_details: [{ type: "reasoning.text", id: "toolu_01", text: "thinking", index: 0 }], + } + const stream = handler.createMessage("system", [ + assistantMessage as unknown as Anthropic.Messages.MessageParam, + ]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { + messages: { + role: string + tool_calls?: { id: string }[] + reasoning_details?: { type: string; id?: string; text?: string }[] + }[] + } + const assistant = params.messages.find((m) => m.role === "assistant") + expect(assistant?.tool_calls).toHaveLength(1) + const textDetail = assistant?.reasoning_details?.find((d) => d.type === "reasoning.text") + expect(textDetail).toMatchObject({ id: "toolu_01", text: "thinking" }) + const encrypted = assistant?.reasoning_details?.find((d) => d.type === "reasoning.encrypted") + expect(encrypted).toMatchObject({ id: "toolu_01" }) + }) + + it("drops tool calls without reasoning details but keeps the content for Gemini messages", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "google/gemini-2.5-flash", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const assistantMessage = { + role: "assistant" as const, + content: [ + { type: "text" as const, text: "kept content" }, + { type: "tool_use" as const, id: "toolu_02", name: "get_weather", input: { city: "NY" } }, + ], + } + const stream = handler.createMessage("system", [ + assistantMessage as unknown as Anthropic.Messages.MessageParam, + ]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { + messages: { role: string; content?: unknown; tool_calls?: unknown }[] + } + const assistant = params.messages.find((m) => m.role === "assistant") + expect(assistant).toBeDefined() + expect(assistant?.content).toBe("kept content") + expect(assistant).not.toHaveProperty("tool_calls") + }) + + it("drops tool calls without a matching reasoning detail id for Gemini messages", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "google/gemini-2.5-flash", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const assistantMessage = { + role: "assistant" as const, + content: [ + { type: "tool_use" as const, id: "toolu_03", name: "get_weather", input: { city: "SF" } }, + { type: "tool_use" as const, id: "toolu_04", name: "search", input: { q: "x" } }, + ], + reasoning_details: [{ type: "reasoning.text", id: "toolu_03", text: "thinking", index: 0 }], + } + const stream = handler.createMessage("system", [ + assistantMessage as unknown as Anthropic.Messages.MessageParam, + ]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { + messages: { role: string; tool_calls?: { id: string }[] }[] + } + const assistant = params.messages.find((m) => m.role === "assistant") + expect(assistant?.tool_calls).toHaveLength(1) + expect(assistant?.tool_calls?.[0].id).toBe("toolu_03") + }) + + it("accumulates and yields reasoning_details from streamed chunks", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { id: "1", choices: [{ delta: { reasoning: "top-level thinking" } }] }, + { + id: "2", + choices: [ + { delta: { reasoning_details: [{ type: "reasoning.text", index: 0, text: "thinking " }] } }, + ], + }, + { + id: "3", + choices: [ + { + delta: { + reasoning_details: [ + { + type: "reasoning.text", + index: 0, + text: "more", + id: "r1", + format: "google-gemini-v1", + signature: "sig", + }, + ], + }, + }, + ], + }, + { + id: "4", + choices: [ + { delta: { reasoning_details: [{ type: "reasoning.summary", index: 1, summary: "sum" }] } }, + ], + }, + { id: "5", choices: [{ delta: { content: "hello" } }] }, + { + id: "6", + choices: [ + { + delta: { + reasoning_details: [{ type: "reasoning.summary", index: 1, summary: " more" }], + }, + }, + ], + }, + { + id: "7", + choices: [ + { delta: { reasoning_details: [{ type: "reasoning.encrypted", index: 2, data: "enc-" }] } }, + ], + }, + { + id: "8", + choices: [ + { + delta: { + reasoning_details: [{ type: "reasoning.encrypted", index: 2, data: "rypted" }], + }, + }, + ], + }, + ]), + ) + stubCompletionsCreate(handler, mockCreate) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "hi" }]), + ) + + // The fragments interleave reasoning_details, top-level reasoning, and content, and every one of + // them must reach the consumer in arrival order, so the whole array is asserted. + expect(chunks).toEqual([ + { type: "reasoning", text: "top-level thinking" }, + { type: "reasoning", text: "thinking " }, + { type: "reasoning", text: "more" }, + { type: "reasoning", text: "sum" }, + { type: "text", text: "hello" }, + { type: "reasoning", text: " more" }, + ]) + + const details = handler.getReasoningDetails() + expect(details).toHaveLength(3) + expect(details?.find((d) => d.type === "reasoning.summary")?.summary).toBe("sum more") + expect(details?.find((d) => d.type === "reasoning.encrypted")?.data).toBe("enc-rypted") + }) + + it("ignores chunks with empty choices", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { id: "1", choices: [] }, + { id: "1", choices: [{ delta: { content: "ok" } }] }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "hi" }]), + ) + expect(chunks).toContainEqual({ type: "text", text: "ok" }) + }) + + it("keeps id, format and signature from the first reasoning chunk on updates", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { + id: "1", + choices: [ + { + delta: { + reasoning_details: [ + { + type: "reasoning.text", + index: 0, + id: "det-1", + format: "google-gemini-v1", + signature: "sig-1", + }, + ], + }, + }, + ], + }, + { + id: "1", + choices: [ + { delta: { reasoning_details: [{ type: "reasoning.text", index: 0, text: "more" }] } }, + ], + }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + await collectStream(handler.createMessage("system", [{ role: "user" as const, content: "hi" }])) + + const details = handler.getReasoningDetails() + expect(details).toHaveLength(1) + expect(details?.[0]).toMatchObject({ + type: "reasoning.text", + text: "more", + id: "det-1", + format: "google-gemini-v1", + signature: "sig-1", + index: 0, + }) + }) + + it("initializes empty accumulator entries without undefined prefixes", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { id: "1", choices: [{ delta: { reasoning_details: [{ type: "reasoning.text", index: 0 }] } }] }, + { + id: "1", + choices: [{ delta: { reasoning_details: [{ type: "reasoning.text", index: 0, text: "t0" }] } }], + }, + { id: "1", choices: [{ delta: { reasoning_details: [{ type: "reasoning.text", index: 1 }] } }] }, + { + id: "1", + choices: [ + { delta: { reasoning_details: [{ type: "reasoning.text", index: 1, summary: "s1" }] } }, + ], + }, + { id: "1", choices: [{ delta: { reasoning_details: [{ type: "reasoning.text", index: 2 }] } }] }, + { + id: "1", + choices: [{ delta: { reasoning_details: [{ type: "reasoning.text", index: 2, data: "d2" }] } }], + }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + await collectStream(handler.createMessage("system", [{ role: "user" as const, content: "hi" }])) + + const details = handler.getReasoningDetails() + expect(details).toHaveLength(3) + expect(details?.find((d) => d.index === 0)?.text).toBe("t0") + expect(details?.find((d) => d.index === 1)?.summary).toBe("s1") + expect(details?.find((d) => d.index === 2)?.data).toBe("d2") + }) + + it("groups reasoning details without an explicit index under index 0", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { id: "1", choices: [{ delta: { reasoning_details: [{ type: "reasoning.text", text: "a" }] } }] }, + { + id: "1", + choices: [{ delta: { reasoning_details: [{ type: "reasoning.text", text: "b", index: 0 }] } }], + }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + await collectStream(handler.createMessage("system", [{ role: "user" as const, content: "hi" }])) + + const details = handler.getReasoningDetails() + expect(details).toHaveLength(1) + expect(details?.[0]?.text).toBe("ab") + expect(details?.[0]?.index).toBe(0) + }) + + it("accumulates reasoning text and summary with the same index separately", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { + id: "1", + choices: [{ delta: { reasoning_details: [{ type: "reasoning.text", index: 0, text: "a" }] } }], + }, + { + id: "1", + choices: [ + { delta: { reasoning_details: [{ type: "reasoning.summary", index: 0, summary: "s" }] } }, + ], + }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "hi" }]), + ) + // Both fragments stream live in arrival order (the summary is yielded from detail.summary), + // so a dropped summary is visible here rather than hidden by toContainEqual on one chunk. + expect(chunks).toEqual([ + { type: "reasoning", text: "a" }, + { type: "reasoning", text: "s" }, + ]) + + const details = handler.getReasoningDetails() + // consolidateReasoningDetails emits a summary entry only when no text arrived at the same + // index (src/api/transform/openai-format.ts:116), so the consolidated store keeps the text + // and drops the summary; asserting the drop is what makes this fail when that predicate + // narrows. The companion test below covers the no-text branch. The entry's type label follows the + // last fragment seen for the index (openai-format.ts:93-99, 105); a text payload carrying a + // summary label is not a shape the request replays, so it is left unasserted here. + expect(details).toHaveLength(1) + expect(details?.[0]?.text).toBe("a") + expect(details?.[0]?.index).toBe(0) + expect(details?.[0]?.summary).toBeUndefined() + }) + + it("keeps a reasoning summary entry when no text arrives at the same index", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { + id: "1", + choices: [ + { + delta: { reasoning_details: [{ type: "reasoning.summary", index: 0, summary: "s" }] }, + }, + ], + }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "hi" }]), + ) + expect(chunks).toEqual([{ type: "reasoning", text: "s" }]) + + const details = handler.getReasoningDetails() + expect(details).toHaveLength(1) + expect(details?.[0]).toMatchObject({ type: "reasoning.summary", index: 0, summary: "s" }) + }) + + it("does not yield displayable reasoning for encrypted-only details", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { + id: "1", + choices: [ + { delta: { reasoning_details: [{ type: "reasoning.encrypted", index: 0, data: "enc" }] } }, + ], + }, + { id: "1", choices: [{ delta: { content: "ok" } }] }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "hi" }]), + ) + expect(chunks).toContainEqual({ type: "text", text: "ok" }) + expect(chunks).not.toContainEqual(expect.objectContaining({ type: "reasoning" })) + + const details = handler.getReasoningDetails() + expect(details).toHaveLength(1) + expect(details?.[0]?.data).toBe("enc") + }) + + it("ignores non-array reasoning_details values", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { id: "1", choices: [{ delta: { reasoning_details: "not-an-array" } }] }, + { id: "1", choices: [{ delta: { content: "ok" } }] }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "hi" }]), + ) + expect(chunks).toContainEqual({ type: "text", text: "ok" }) + expect(handler.getReasoningDetails()).toBeUndefined() + }) + + it("ignores object reasoning_details values", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { id: "1", choices: [{ delta: { reasoning_details: { bogus: true } } }] }, + { id: "1", choices: [{ delta: { content: "ok" } }] }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "hi" }]), + ) + expect(chunks).toContainEqual({ type: "text", text: "ok" }) + expect(handler.getReasoningDetails()).toBeUndefined() + }) + + it("rejects with AbortError when the external signal aborts during request creation", async () => { + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + + // Deterministic synchronization (mirrors the Requesty test): the mock notifies the + // test when the request actually starts, so the abort lands after create() began + // instead of racing a fixed sleep that a slow runner can lose. + let notifyCreateStarted!: () => void + const createStarted = new Promise((resolve) => { + notifyCreateStarted = resolve + }) + const mockCreate = vitest + .fn() + .mockImplementation(async (_params: unknown, options?: { signal?: AbortSignal }) => { + notifyCreateStarted() + // Emulate the OpenAI SDK: the pending request rejects when the signal aborts. + await new Promise((resolve) => { + if (options?.signal?.aborted) { + resolve() + } else { + options?.signal?.addEventListener("abort", () => resolve(), { once: true }) + } + }) + const abortError = new Error("The user aborted a request") + abortError.name = "AbortError" + throw abortError + }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const metadata = makeCreateMessageMetadata({ abortSignal: controller.signal }) + const generator = handler.createMessage("system", [{ role: "user" as const, content: "hi" }], metadata) + + const nextPromise = generator.next() + // Abort only once create() has actually started. + await createStarted + controller.abort() + + await expect(nextPromise).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + }) + + it("normalizes Mistral tool call ids in tool result messages", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "Mistral/mixtral-large", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const stream = handler.createMessage("system", [ + { + role: "user" as const, + content: [ + { type: "text" as const, text: "hi" }, + { type: "tool_result" as const, tool_use_id: "toolu_123456789", content: "tool result ok" }, + ], + }, + ]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { + messages: { role: string; tool_call_id?: string; content: unknown }[] + } + const toolMessage = params.messages.find((m) => m.role === "tool") + expect(toolMessage).toMatchObject({ role: "tool", tool_call_id: "toolu1234", content: "tool result ok" }) + }) + + it("uses user role for the system prompt with suffixed DeepSeek R1 model ids", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "deepseek/deepseek-r1-0528", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const stream = handler.createMessage("system prompt", [ + { role: "assistant" as const, content: "earlier reply" }, + ]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { messages: { role: string; content: unknown }[] } + expect(params.messages[0]).toMatchObject({ role: "user", content: "system prompt" }) + expect(params.messages[1]).toMatchObject({ role: "assistant", content: "earlier reply" }) + expect(params.messages.some((m) => m.role === "system")).toBe(false) + }) + + it("adds gemini cache breakpoints and the default max tokens for google/gemini models", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "google/gemini-2.5-flash", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const stream = handler.createMessage("sys-k1", [{ role: "user" as const, content: "hello" }]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { + messages: { role: string; content: unknown }[] + max_tokens?: number + } + expect(params.messages[0]).toEqual({ + role: "system", + content: [{ type: "text", text: "sys-k1", cache_control: { type: "ephemeral" } }], + }) + expect(params.messages[1]).toEqual({ role: "user", content: [{ type: "text", text: "hello" }] }) + expect(params.max_tokens).toBe(8192) + }) + + it("skips gemini sanitization for non-google gemini model ids", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "openai/gemini-2.5-flash", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + // No reasoning_details at all: gemini sanitization would drop this tool call, so a + // surviving tool call proves the sanitization path was skipped. + const assistantMessage = { + role: "assistant" as const, + content: [{ type: "tool_use" as const, id: "toolu_k2", name: "lookup", input: {} }], + } + const stream = handler.createMessage("system", [ + assistantMessage as unknown as Anthropic.Messages.MessageParam, + ]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { + messages: { role: string; tool_calls?: unknown[]; reasoning_details?: unknown[] }[] + } + const assistant = params.messages.find((m) => m.role === "assistant") + expect(assistant?.tool_calls).toHaveLength(1) + expect(assistant).not.toHaveProperty("reasoning_details") + }) + + it("does not inject fake encrypted reasoning when the assistant already carries encrypted details", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "google/gemini-2.5-flash", + }), + ) + const mockCreate = vitest + .fn() + .mockResolvedValue(asyncStreamFrom([{ id: "1", choices: [{ delta: { content: "ok" } }] }])) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + // The first assistant mixes a text detail with an encrypted one; the second carries + // only encrypted details so the every()/==/-!== variants each diverge somewhere. + const assistantOne = { + role: "assistant" as const, + content: [ + { type: "tool_use" as const, id: "toolu_05", name: "lookup_a", input: {} }, + { type: "tool_use" as const, id: "toolu_06", name: "lookup_b", input: {} }, + ], + reasoning_details: [ + { type: "reasoning.text", id: "toolu_05", text: "t", index: 0 }, + { type: "reasoning.encrypted", id: "toolu_06", data: "orig-enc", index: 1 }, + ], + } + const assistantTwo = { + role: "assistant" as const, + content: [ + { type: "tool_use" as const, id: "toolu_07", name: "lookup_c", input: {} }, + { type: "tool_use" as const, id: "toolu_08", name: "lookup_d", input: {} }, + ], + reasoning_details: [ + { type: "reasoning.encrypted", id: "toolu_07", data: "enc-7", index: 0 }, + { type: "reasoning.encrypted", id: "toolu_08", data: "enc-8", index: 1 }, + ], + } + const stream = handler.createMessage("system", [ + assistantOne as unknown as Anthropic.Messages.MessageParam, + assistantTwo as unknown as Anthropic.Messages.MessageParam, + ]) + await collectStream(stream) + + const params = mockCreate.mock.calls[0][0] as { + messages: { + role: string + tool_calls?: { id: string }[] + reasoning_details?: { type: string; data?: string }[] + }[] + } + const assistants = params.messages.filter((m) => m.role === "assistant") + expect(assistants).toHaveLength(2) + expect(assistants[0].tool_calls).toHaveLength(2) + const firstEncrypted = + assistants[0].reasoning_details?.filter((d) => d.type === "reasoning.encrypted") ?? [] + expect(firstEncrypted).toHaveLength(1) + expect(firstEncrypted[0]).toMatchObject({ data: "orig-enc" }) + expect(assistants[1].tool_calls).toHaveLength(2) + const secondEncrypted = + assistants[1].reasoning_details?.filter((d) => d.type === "reasoning.encrypted") ?? [] + expect(secondEncrypted).toHaveLength(2) + }) + + it("keeps the last usage chunk when later chunks carry no usage", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { + id: "1", + choices: [], + usage: { + prompt_tokens: 10, + completion_tokens: 5, + cost: 2, + cost_details: { upstream_inference_cost: 3 }, + }, + }, + { id: "2", choices: [{ delta: { content: "ok" } }] }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "hi" }]), + ) + const usage = chunks.find((c) => c.type === "usage") + expect(usage).toMatchObject({ type: "usage", inputTokens: 10, outputTokens: 5, totalCost: 5 }) + }) + + it("merges id, format and signature into an existing reasoning detail", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { + id: "1", + choices: [ + { delta: { reasoning_details: [{ type: "reasoning.text", index: 0, text: "start" }] } }, + ], + }, + { + id: "2", + choices: [ + { + delta: { + reasoning_details: [ + { + type: "reasoning.text", + index: 0, + text: "more", + id: "det-1", + format: "google-gemini-v1", + signature: "sig-1", + }, + ], + }, + }, + ], + }, + { id: "3", choices: [{ delta: { content: "ok" } }] }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "hi" }]), + ) + expect(chunks).toContainEqual({ type: "reasoning", text: "start" }) + const details = handler.getReasoningDetails() + expect(details).toHaveLength(1) + expect(details?.[0]).toMatchObject({ + type: "reasoning.text", + text: "startmore", + id: "det-1", + format: "google-gemini-v1", + signature: "sig-1", + index: 0, + }) + }) + + it("ignores non-string text and summary values in reasoning details", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { + id: "1", + choices: [ + { + delta: { + reasoning_details: [ + { type: "reasoning.encrypted", index: 0, text: "secret", data: "enc" }, + { type: "reasoning.text", index: 1, text: 5 }, + { type: "reasoning.text", index: 2, summary: "leak" }, + { type: "reasoning.summary", index: 3, summary: 7 }, + { type: "reasoning.summary", index: 4, summary: "sum-ok" }, + { type: "reasoning.text", index: 5, text: "text-ok" }, + ], + }, + }, + ], + }, + { id: "2", choices: [{ delta: { content: "ok" } }] }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "hi" }]), + ) + expect(chunks.filter((c) => c.type === "reasoning")).toEqual([ + { type: "reasoning", text: "sum-ok" }, + { type: "reasoning", text: "text-ok" }, + ]) + }) + + it("yields top-level reasoning only for non-empty string values", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { id: "1", choices: [{ delta: { reasoning: 5 } }] }, + { id: "2", choices: [{ delta: { reasoning: "" } }] }, + { id: "3", choices: [{ delta: { reasoning: "top" } }] }, + { id: "4", choices: [{ delta: { content: "ok" } }] }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "hi" }]), + ) + expect(chunks.filter((c) => c.type === "reasoning")).toEqual([{ type: "reasoning", text: "top" }]) + expect(chunks).toContainEqual({ type: "text", text: "ok" }) + }) + + it("skips top-level reasoning once reasoning details have yielded displayable text", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { + id: "1", + choices: [ + { delta: { reasoning_details: [{ type: "reasoning.text", index: 0, text: "det" }] } }, + ], + }, + { id: "2", choices: [{ delta: { reasoning: "top" } }] }, + { id: "3", choices: [{ delta: { content: "ok" } }] }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "hi" }]), + ) + expect(chunks.filter((c) => c.type === "reasoning")).toEqual([{ type: "reasoning", text: "det" }]) + expect(chunks).toContainEqual({ type: "text", text: "ok" }) + }) + + it("ignores non-array tool_calls values in stream deltas", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { id: "1", choices: [{ delta: { tool_calls: "nope", content: "x" } }] }, + { id: "2", choices: [{ delta: { content: "ok" } }] }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "hi" }]), + ) + expect(chunks.some((c) => c.type === "tool_call_partial")).toBe(false) + expect(chunks).toContainEqual({ type: "text", text: "x" }) + }) + + it("emits tool_call_partial chunks for tool calls without a function payload", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue( + asyncStreamFrom([ + { id: "1", choices: [{ delta: { tool_calls: [{ index: 0, id: "c1" }], content: "x" } }] }, + { id: "2", choices: [{ delta: { content: "ok" } }] }, + ]), + ) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user" as const, content: "hi" }]), + ) + expect(chunks).toContainEqual(expect.objectContaining({ type: "tool_call_partial", id: "c1", index: 0 })) + }) + + it("reports OpenRouter structured errors in createMessage with telemetry", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockRejectedValueOnce({ + error: { + message: "Model not found", + code: 404, + metadata: { raw: '{"message":"upstream: model not found"}' }, + }, + }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const generator = handler.createMessage("system", [{ role: "user" as const, content: "hi" }]) + + await expect(generator.next()).rejects.toThrow(/completion error/) + expect(mockCaptureException).toHaveBeenCalledTimes(1) + }) + + it("prefers the raw metadata message over the SDK error message in createMessage telemetry", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockRejectedValueOnce({ + error: { + message: "Model not found", + code: 404, + metadata: { raw: '{"message":"upstream: model not found"}' }, + }, + }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const generator = handler.createMessage("system", [{ role: "user" as const, content: "hi" }]) + + await expect(generator.next()).rejects.toThrow(/completion error/) + expect(mockCaptureException).toHaveBeenCalledWith( + expect.objectContaining({ + message: "upstream: model not found", + provider: providerIdentifiers.openrouter, + modelId: "anthropic/claude-sonnet-4", + operation: "createMessage", + status: 404, + }), + ) + }) + + it("falls back to the SDK error message in createMessage telemetry when no raw metadata is present", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockRejectedValueOnce({ + error: { + message: "Model not found", + code: 404, + }, + }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const generator = handler.createMessage("system", [{ role: "user" as const, content: "hi" }]) + + await expect(generator.next()).rejects.toThrow(/completion error/) + expect(mockCaptureException).toHaveBeenCalledWith( + expect.objectContaining({ + message: "Model not found", + provider: providerIdentifiers.openrouter, + modelId: "anthropic/claude-sonnet-4", + operation: "createMessage", + }), + ) + }) + + it("falls back to an unknown error in createMessage telemetry when no message is available", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockRejectedValueOnce({ + error: { + code: 500, + }, + }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const generator = handler.createMessage("system", [{ role: "user" as const, content: "hi" }]) + + await expect(generator.next()).rejects.toThrow(/completion error/) + expect(mockCaptureException).toHaveBeenCalledWith( + expect.objectContaining({ + message: "Unknown error", + provider: providerIdentifiers.openrouter, + modelId: "anthropic/claude-sonnet-4", + operation: "createMessage", + }), + ) + }) + }) + + describe("completePrompt", () => { + it("returns correct response", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockResponse = { choices: [{ message: { content: "test completion" } }] } + + const mockCreate = vitest.fn().mockResolvedValue(mockResponse) + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const result = await handler.completePrompt("test prompt") + + expect(result).toBe("test completion") + + expect(mockCreate).toHaveBeenCalledWith( + { + model: mockOptions.openRouterModelId, + max_tokens: 8192, + temperature: 0, + messages: [{ role: "user", content: "test prompt" }], + stream: false, + }, + { headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } }, + ) + }) + + it("handles API errors and captures telemetry", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockError = { + error: { + message: "API Error", + code: 500, + }, + } + + const mockCreate = vitest.fn().mockResolvedValue(mockError) + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + await expect(handler.completePrompt("test prompt")).rejects.toThrow("OpenRouter API Error 500: API Error") + + // Verify telemetry was captured + expect(mockCaptureException).toHaveBeenCalledWith( + expect.objectContaining({ + message: "API Error", + provider: providerIdentifiers.openrouter, + modelId: mockOptions.openRouterModelId, + operation: "completePrompt", + errorCode: 500, + status: 500, + }), + ) + }) + + it("handles unexpected errors and captures telemetry", async () => { + const handler = new OpenRouterHandler(mockOptions) + const error = new Error("Unexpected error") + const mockCreate = vitest.fn().mockRejectedValue(error) + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + await expect(handler.completePrompt("test prompt")).rejects.toThrow("Unexpected error") + + // Verify telemetry was captured (filtering now happens inside PostHogTelemetryClient) + expect(mockCaptureException).toHaveBeenCalledWith( + expect.objectContaining({ + message: "Unexpected error", + provider: providerIdentifiers.openrouter, + modelId: mockOptions.openRouterModelId, + operation: "completePrompt", + }), + ) + }) + + it("passes SDK exceptions with status 429 to telemetry (filtering happens in PostHogTelemetryClient)", async () => { + const handler = new OpenRouterHandler(mockOptions) + const error = Object.assign(new Error("Rate limit exceeded: free-models-per-day"), { status: 429 }) + const mockCreate = vitest.fn().mockRejectedValue(error) + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + await expect(handler.completePrompt("test prompt")).rejects.toThrow("Rate limit exceeded") + + // captureException is called, but PostHogTelemetryClient filters out 429 errors internally + expect(mockCaptureException).toHaveBeenCalledWith( + expect.objectContaining({ + message: "Rate limit exceeded: free-models-per-day", + provider: providerIdentifiers.openrouter, + modelId: mockOptions.openRouterModelId, + operation: "completePrompt", + }), + ) + }) + + it("passes SDK exceptions with 429 in message to telemetry (filtering happens in PostHogTelemetryClient)", async () => { + const handler = new OpenRouterHandler(mockOptions) + const error = new Error("429 Rate limit exceeded: free-models-per-day") + const mockCreate = vitest.fn().mockRejectedValue(error) + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + await expect(handler.completePrompt("test prompt")).rejects.toThrow("429 Rate limit exceeded") // captureException is called, but PostHogTelemetryClient filters out 429 errors internally expect(mockCaptureException).toHaveBeenCalledWith( @@ -1096,9 +3768,14 @@ describe("OpenRouterHandler", () => { const handler = new OpenRouterHandler(mockOptions) const error = new Error("Request failed due to rate limit") const mockCreate = vitest.fn().mockRejectedValue(error) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) await expect(handler.completePrompt("test prompt")).rejects.toThrow("rate limit") @@ -1123,9 +3800,14 @@ describe("OpenRouterHandler", () => { } const mockCreate = vitest.fn().mockResolvedValue(mockError) - ;(OpenAI as any).prototype.chat = { - completions: { create: mockCreate }, - } as any + // Typed per-instance injection: a prototype mutation would outlive the test + // and hand later tests a stale mockCreate. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) await expect(handler.completePrompt("test prompt")).rejects.toThrow( "OpenRouter API Error 429: Rate limit exceeded", @@ -1143,6 +3825,477 @@ describe("OpenRouterHandler", () => { }), ) }) + it("should pass abort signal through to client", async () => { + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "response" } }] }) + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + await handler.completePrompt("test prompt", { abortSignal: controller.signal }) + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ model: expect.any(String) }), + expect.objectContaining({ signal: controller.signal }), + ) + }) + + it("should pass timeout through to client", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "response" } }] }) + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + await handler.completePrompt("test prompt", { timeoutMs: 5000 }) + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ model: expect.any(String) }), + expect.objectContaining({ timeout: 5000 }), + ) + }) + + it("normalizes the per-request timeout before handing it to the SDK", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "response" } }] }) + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + // Fractional delays fail the SDK's integer validation: round up. + await handler.completePrompt("test prompt", { timeoutMs: 1500.4 }) + expect(mockCreate.mock.calls[0][1]).toMatchObject({ timeout: 1501 }) + + // Non-finite delays are not a timeout at all: omit the option. + mockCreate.mockClear() + await handler.completePrompt("test prompt", { timeoutMs: Number.POSITIVE_INFINITY }) + expect(mockCreate.mock.calls[0][1]).not.toHaveProperty("timeout") + + // Node clamps overflowing delays to 1 ms: cap them at the uint32 max. + mockCreate.mockClear() + await handler.completePrompt("test prompt", { timeoutMs: 9_000_000_000_000 }) + expect(mockCreate.mock.calls[0][1]).toMatchObject({ timeout: 2_147_483_647 }) + }) + + it("should work without options (backward compatible)", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "response" } }] }) + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const result = await handler.completePrompt("test prompt") + expect(result).toBe("response") + }) + + it("omits the anthropic beta header for non-Anthropic models", async () => { + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "openai/gpt-4o", + }), + ) + const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "response" } }] }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + await handler.completePrompt("test prompt") + const options = mockCreate.mock.calls[0][1] as { headers?: Record } + expect(options).not.toHaveProperty("headers") + }) + + it("omits the signal and timeout options when timeoutMs is zero and no signal is provided", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "response" } }] }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + await handler.completePrompt("test prompt", { timeoutMs: 0 }) + const options = mockCreate.mock.calls[0][1] as { signal?: AbortSignal; timeout?: number } + expect(options).not.toHaveProperty("signal") + expect(options).not.toHaveProperty("timeout") + }) + + it("propagates non-abort model-discovery failures from completePrompt", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "response" } }] }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + // The constructor's background discovery consumed the default mock; make the next + // lookup (completePrompt's own fetchModel) reject with a non-abort failure. + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockRejectedValueOnce(new Error("discovery failed")) + + await expect(handler.completePrompt("test prompt")).rejects.toThrow("discovery failed") + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("propagates an abort-named model-discovery failure unchanged in completePrompt", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "response" } }] }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + // The constructor's background discovery consumed the default mock; make the next + // lookup (completePrompt's own fetchModel) reject with an abort-named failure. + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockRejectedValueOnce(Object.assign(new Error("aborted"), { name: "AbortError" })) + + // An abort-shaped discovery failure with no caller cancellation is a + // discovery failure: it keeps its original identity instead of being + // reported as a cancellation. + await expect(handler.completePrompt("test prompt")).rejects.toMatchObject({ + name: "AbortError", + message: "aborted", + }) + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("rejects with AbortError when the external signal aborts during deferred model discovery", async () => { + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + const mockCreate = vitest.fn() + stubCompletionsCreate(handler, mockCreate) + + // Discovery is deferred and notifies when it starts, so the abort lands while the lookup is + // pending: that is the window completePrompt's rejectOnAbort(fetchModel()) race covers. + let resolveModelLookup!: (models: ModelRecord) => void + const deferredModelLookup = new Promise((resolve) => { + resolveModelLookup = resolve + }) + let notifyLookupStarted!: () => void + const lookupStarted = new Promise((resolve) => { + notifyLookupStarted = resolve + }) + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockImplementationOnce(() => { + notifyLookupStarted() + return deferredModelLookup + }) + + const completion = handler.completePrompt("test prompt", { abortSignal: controller.signal }) + await settlesWithin(lookupStarted, ABORT_SETTLE_MS) + controller.abort() + + await expect(settlesWithin(completion, ABORT_SETTLE_MS)).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + expect(mockCreate).not.toHaveBeenCalled() + + // Settle the abandoned lookup so it cannot outlive the test. + resolveModelLookup({}) + }) + + it("rejects with AbortError when a timeout elapses during deferred model discovery", async () => { + // The configured timeoutMs covers model lookup too, so a lookup that outlives it must end + // the call with the same canonical AbortError and must not reach the SDK. + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn() + stubCompletionsCreate(handler, mockCreate) + + let resolveModelLookup!: (models: ModelRecord) => void + const deferredModelLookup = new Promise((resolve) => { + resolveModelLookup = resolve + }) + let notifyLookupStarted!: () => void + const lookupStarted = new Promise((resolve) => { + notifyLookupStarted = resolve + }) + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockImplementationOnce(() => { + notifyLookupStarted() + return deferredModelLookup + }) + + const completion = handler.completePrompt("test prompt", { timeoutMs: 50 }) + await settlesWithin(lookupStarted, ABORT_SETTLE_MS) + + await expect(settlesWithin(completion, ABORT_SETTLE_MS)).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + expect(mockCreate).not.toHaveBeenCalled() + + // Settle the abandoned lookup so it cannot outlive the test. + resolveModelLookup({}) + }) + + it("rejects with AbortError when the request aborts before a late result lands", async () => { + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + + // Deterministic synchronization: the mock notifies the test when create() has been + // called, so the abort lands after model discovery instead of racing it. + let notifyCreateStarted!: () => void + const createStarted = new Promise((resolve) => { + notifyCreateStarted = resolve + }) + let resolveCreate!: (value: unknown) => void + const mockCreate = vitest.fn().mockImplementation(() => { + notifyCreateStarted() + return new Promise((resolve) => { + resolveCreate = resolve + }) + }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const promise = handler.completePrompt("test prompt", { abortSignal: controller.signal }) + await createStarted + controller.abort() + resolveCreate({ choices: [{ message: { content: "late result" } }] }) + + await expect(promise).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + }) + + it("settles the completePrompt await as AbortError while the SDK is parked in a retry backoff", async () => { + // The same backoff window as createMessage: a create() asleep in its retry sleep never + // settles, so the race in settleOnAbort is what ends the wait on a caller cancellation. + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + let createStarted: () => void + const createAwaited = new Promise((resolve) => { + createStarted = resolve + }) + stubCompletionsCreate( + handler, + vitest.fn().mockImplementation(() => { + createStarted() + return new Promise(() => {}) + }), + ) + + const completion = handler.completePrompt("test prompt", { abortSignal: controller.signal }) + // Abort only after the create call is in flight so this pins the create await, not model discovery. + await createAwaited + controller.abort() + + await expect(settlesWithin(completion, ABORT_SETTLE_MS)).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + }) + + it("normalizes the SDK's own abort error in completePrompt instead of reporting a completion error", async () => { + const handler = new OpenRouterHandler(mockOptions) + const sdkAbort = Object.assign(new Error("Request was aborted."), { + name: "APIUserAbortError", + status: 499, + }) + stubCompletionsCreate(handler, vitest.fn().mockRejectedValue(sdkAbort)) + + await expect(handler.completePrompt("test prompt")).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + expect(mockCaptureException).not.toHaveBeenCalled() + }) + + it("rejects with AbortError when the signal is pre-aborted", async () => { + const handler = new OpenRouterHandler(mockOptions) + const mockCreate = vitest.fn().mockResolvedValue({ choices: [{ message: { content: "response" } }] }) + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + // The catalog loads lazily via ensureModelFetched(), so a fresh handler has not + // started a discovery lookup; the pre-abort guard must reject without starting one. + const { getModels } = await import("../fetchers/modelCache") + const lookupsBefore = vitest.mocked(getModels).mock.calls.length + + const controller = new AbortController() + controller.abort() + + await expect( + handler.completePrompt("test prompt", { abortSignal: controller.signal }), + ).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + expect(vitest.mocked(getModels).mock.calls.length).toBe(lookupsBefore) + }) + + it("rejects with AbortError when aborted mid-flight", async () => { + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + + // Wait for create() to actually start before aborting: aborting during + // model lookup would exercise the discovery race instead of the + // in-flight SDK catch path this test names. + let notifyCreateStarted!: () => void + const createStarted = new Promise((resolve) => { + notifyCreateStarted = resolve + }) + const mockCreate = vitest + .fn() + .mockImplementation(async (_params: unknown, options?: { signal?: AbortSignal }) => { + notifyCreateStarted() + // Emulate the OpenAI SDK: the in-flight request rejects when the signal aborts. + await new Promise((resolve) => { + if (options?.signal?.aborted) { + resolve() + } else { + options?.signal?.addEventListener("abort", () => resolve(), { once: true }) + } + }) + const abortError = new Error("The user aborted a request") + abortError.name = "AbortError" + throw abortError + }) + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const promise = handler.completePrompt("test prompt", { abortSignal: controller.signal }) + await createStarted + controller.abort() + + await expect(promise).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + // The in-flight catch path must have run: create() was entered exactly once. + expect(mockCreate).toHaveBeenCalledTimes(1) + }) + it("rejects with AbortError when only a timeout is provided and it elapses", async () => { + // Non-Anthropic model: also exercises the no-beta-header branch of requestOptions. + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + ...mockOptions, + openRouterModelId: "openai/gpt-4o", + }), + ) + const mockCreate = vitest + .fn() + .mockImplementation(async (_params: unknown, options?: { signal?: AbortSignal }) => { + // Emulate the OpenAI SDK: the in-flight request rejects when the signal times out. + await new Promise((resolve) => { + if (options?.signal?.aborted) { + resolve() + } else { + options?.signal?.addEventListener("abort", () => resolve(), { once: true }) + } + }) + const timeoutError = new Error("TimeoutError: Request timed out.") + timeoutError.name = "TimeoutError" + throw timeoutError + }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + await expect(handler.completePrompt("test prompt", { timeoutMs: 50 })).rejects.toMatchObject({ + name: "AbortError", + }) + }) + + it("rejects with AbortError when both an abort signal and a timeout are provided", async () => { + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + + // Deterministic synchronization: the mock notifies the test when the request + // actually starts, so the abort lands mid-flight (after model lookup) instead of + // winning the race at model discovery on a slow runner. + let notifyCreateStarted!: () => void + const createStarted = new Promise((resolve) => { + notifyCreateStarted = resolve + }) + let requestSignal: AbortSignal | undefined + const mockCreate = vitest + .fn() + .mockImplementation(async (_params: unknown, options?: { signal?: AbortSignal }) => { + notifyCreateStarted() + requestSignal = options?.signal + await new Promise((resolve) => { + if (options?.signal?.aborted) { + resolve() + } else { + options?.signal?.addEventListener("abort", () => resolve(), { once: true }) + } + }) + const abortError = new Error("The user aborted a request") + abortError.name = "AbortError" + throw abortError + }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const promise = handler.completePrompt("test prompt", { + abortSignal: controller.signal, + timeoutMs: 100_000, + }) + // Abort only once create() has actually started (after model lookup). + await createStarted + controller.abort() + + await expect(promise).rejects.toMatchObject({ + name: "AbortError", + message: "The OpenRouter request was aborted", + }) + // The SDK received a merged signal (not the caller's signal) plus the timeout. + expect(requestSignal).toBeDefined() + expect(requestSignal).not.toBe(controller.signal) + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ model: expect.any(String) }), + expect.objectContaining({ timeout: 100_000 }), + ) + }) }) describe("model catalog", () => { @@ -1229,6 +4382,46 @@ describe("OpenRouterHandler", () => { expect(mockGetModelEndpoints).toHaveBeenCalledTimes(3) }) + it("keeps retrying when a lookup returns no models after an earlier partial load", async () => { + const { getModels } = await import("../fetchers/modelCache") + const mockedGetModels = vitest.mocked(getModels) + + mockGetModelEndpoints + // Lookup 1: the model catalog arrives, the endpoint request produced nothing. + .mockResolvedValueOnce({}) + // Lookup 2: the endpoints arrive, the model request answers an empty map. + .mockResolvedValueOnce({ "provider-x": { ...openRouterDefaultModelInfo, maxTokens: 1000 } }) + // Lookup 3: both answers present, so the load is complete from here on. + .mockResolvedValueOnce({ "provider-x": { ...openRouterDefaultModelInfo, maxTokens: 1000 } }) + + const handler = new OpenRouterHandler( + makeApiHandlerOptions({ + openRouterApiKey: "test-key", + openRouterModelId: "anthropic/claude-sonnet-4", + openRouterSpecificProvider: "provider-x", + }), + ) + + await handler.ensureModelFetched() + expect(mockGetModelEndpoints).toHaveBeenCalledTimes(1) + + // The endpoint catalog fills in, but this lookup saw no models. The records from lookup 1 + // are still published, so judging by the published maps would call this load complete; + // the load must stay retryable because the answer it got was empty. + mockedGetModels.mockResolvedValueOnce({}) + await handler.ensureModelFetched() + expect(mockGetModelEndpoints).toHaveBeenCalledTimes(2) + + await handler.ensureModelFetched() + expect(mockGetModelEndpoints).toHaveBeenCalledTimes(3) + expect(handler.getModel().info.maxTokens).toBe(1000) + + // A load that answered both catalogs is memoized. + await handler.ensureModelFetched() + expect(mockGetModelEndpoints).toHaveBeenCalledTimes(3) + expect(mockedGetModels).toHaveBeenCalledTimes(3) + }) + it("memoizes the default-provider load even when the endpoint catalog is empty", async () => { const handler = new OpenRouterHandler(mockOptions) diff --git a/src/api/providers/openrouter.ts b/src/api/providers/openrouter.ts index 6d2057a7a3d..d28ce2ee825 100644 --- a/src/api/providers/openrouter.ts +++ b/src/api/providers/openrouter.ts @@ -43,6 +43,16 @@ import type { SingleCompletionHandler, } from "../index" import { handleOpenAIError } from "./utils/error-handler" +import { + bridgeExternalAbortSignal, + createAbortError, + isRequestAborted, + mergeAbortSignalAndTimeout, + mergeAbortSignals, + rejectOnAbort, + settleOnAbort, + toSdkTimeoutMs, +} from "./utils/abort-signal" import { generateImageWithProvider, ImageGenerationResult } from "./utils/image-generation" import { applyRouterToolPreferences } from "./utils/router-tool-preferences" @@ -141,6 +151,19 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH protected endpoints: ModelRecord = {} private readonly providerName = "OpenRouter" private currentReasoningDetails: any[] = [] + // Monotonic id for model lookups. Overlapping lookups (two requests, or an + // abandoned lookup whose request was cancelled) can settle in any order, and + // only the newest one may publish the records it fetched. + private fetchModelSequence = 0 + // Sequence of the newest lookup whose result this handler actually published. A lookup only + // publishes when it is newer than what was published, so an older lookup that settles first is + // not dropped just because a newer one has started. + private publishedModelSequence = 0 + // Sequence of the newest lookup whose endpoint records were published. Endpoints keep their own + // ordering because they publish even when the model catalog came back empty; sharing the model + // sequence would let an older lookup overwrite newer endpoint records whenever both lookups + // returned an empty catalog, since the model sequence only advances when models publish. + private publishedEndpointSequence = 0 constructor(options: ApiHandlerOptions) { super() @@ -175,14 +198,13 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH async ensureModelFetched(signal?: AbortSignal): Promise { await this.ensureCatalogLoaded(async () => { - await this.fetchModel() - // fetchModel() returns model info, not the catalog, so report emptiness - // from the catalogs it assigned: an empty response must stay retryable. - // With a specific provider selected, the endpoint catalog is what - // getModel() uses, so an empty endpoint response is a failed load even - // when the model-level catalog arrived. - const hasModels = Object.keys(this.models).length > 0 - const hasEndpoints = Object.keys(this.endpoints).length > 0 + const { hasModels, hasEndpoints } = await this.runModelLookup() + // Judge the load by what this lookup returned, not by the records that happen to be + // published: a lookup that came back empty keeps the previous records, so reading + // this.models / this.endpoints here would memoize a failed lookup as complete and never + // retry it. With a specific provider selected, the endpoint catalog is what getModel() + // uses, so an empty endpoint response is a failed load even when the model-level catalog + // arrived. return hasModels && (!this.hasSpecificProvider || hasEndpoints) }, signal) } @@ -216,341 +238,457 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH messages: Anthropic.Messages.MessageParam[], metadata?: ApiHandlerCreateMessageMetadata, ): AsyncGenerator { - const model = await this.fetchModel() - - let { id: modelId, maxTokens, temperature, topP, reasoning } = model - - // Reset reasoning_details accumulator for this request - this.currentReasoningDetails = [] - - // OpenRouter sends reasoning tokens by default for Gemini 2.5 Pro models - // even if you don't request them. This is not the default for - // other providers (including Gemini), so we need to explicitly disable - // them unless the user has explicitly configured reasoning. - // Note: Gemini 3 models use reasoning_details format with thought signatures, - // but we handle this via skip_thought_signature_validator injection below. - if ( - (modelId === "google/gemini-2.5-pro-preview" || modelId === "google/gemini-2.5-pro") && - typeof reasoning === "undefined" - ) { - reasoning = { exclude: true } - } + // Bridge the external abort signal with the shared helper: the helper owns the + // pre-abort guard, the once-only listener, and the handle that the finally block + // below uses to detach it. The helper does not expose its controller, so this + // generator keeps one of its own for the teardown abort - abandoning the generator + // (early break or a downstream error) must still cancel the in-flight request. The + // two signals are merged so either cancellation reaches the SDK; with no external + // signal the request still gets a signal, as before, and the client-level timeout + // remains the default safety net. + const { signal: bridgedSignal, dispose: removeExternalAbortListener } = bridgeExternalAbortSignal( + metadata?.abortSignal, + ) + const teardownController = new AbortController() + const requestSignal = bridgedSignal + ? mergeAbortSignals(bridgedSignal, teardownController.signal) + : teardownController.signal - // Convert Anthropic messages to OpenAI format. - // Pass normalization function for Mistral compatibility (requires 9-char alphanumeric IDs) - const isMistral = modelId.toLowerCase().includes("mistral") - let openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ - { role: "system", content: systemPrompt }, - ...convertToOpenAiMessages( - messages, - isMistral ? { normalizeToolCallId: normalizeMistralToolCallId } : undefined, - ), - ] + try { + // The request was already aborted before we started: fail fast without calling the API. + if (requestSignal.aborted) { + throw createAbortError("OpenRouter") + } - // DeepSeek highly recommends using user instead of system role. - if (modelId.startsWith("deepseek/deepseek-r1") || modelId === "perplexity/sonar-reasoning") { - openAiMessages = convertToR1Format([{ role: "user", content: systemPrompt }, ...messages]) - } + // Model discovery is not signal-aware: race it against the per-request signal so an + // abort during the lookup rejects with AbortError instead of calling the API with an + // already-aborted signal. + const model = await rejectOnAbort(this.fetchModel(), requestSignal, this.providerName) + + let { id: modelId, maxTokens, temperature, topP, reasoning } = model + + // Reset reasoning_details accumulator for this request + this.currentReasoningDetails = [] + + // OpenRouter sends reasoning tokens by default for Gemini 2.5 Pro models + // even if you don't request them. This is not the default for + // other providers (including Gemini), so we need to explicitly disable + // them unless the user has explicitly configured reasoning. + // Note: Gemini 3 models use reasoning_details format with thought signatures, + // but we handle this via skip_thought_signature_validator injection below. + if ( + (modelId === "google/gemini-2.5-pro-preview" || modelId === "google/gemini-2.5-pro") && + typeof reasoning === "undefined" + ) { + reasoning = { exclude: true } + } - // Process reasoning_details when switching models to Gemini. - const isGemini = modelId.startsWith("google/gemini") - - // For Gemini models with native protocol: - // 1. Sanitize messages to handle thought signature validation issues. - // This must happen BEFORE fake encrypted block injection to avoid injecting for - // tool calls that will be dropped due to missing/mismatched reasoning_details. - // 2. Inject fake reasoning.encrypted block for tool calls without existing encrypted reasoning. - // This is required when switching from other models to Gemini to satisfy API validation. - // Per OpenRouter documentation (conversation with Toven, Nov 2025): - // - Create ONE reasoning_details entry per assistant message with tool calls - // - Set `id` to the FIRST tool call's ID from the tool_calls array - // - Set `data` to "skip_thought_signature_validator" to bypass signature validation - // - Set `index` to 0 - // See: https://github.com/cline/cline/issues/8214 - if (isGemini) { - // Step 1: Sanitize messages - filter out tool calls with missing/mismatched reasoning_details - openAiMessages = sanitizeGeminiMessages(openAiMessages, modelId) - - // Step 2: Inject fake reasoning.encrypted block for tool calls that survived sanitization - openAiMessages = openAiMessages.map((msg) => { - if (msg.role === "assistant") { - const toolCalls = (msg as any).tool_calls as any[] | undefined - const existingDetails = (msg as any).reasoning_details as any[] | undefined - - // Only inject if there are tool calls and no existing encrypted reasoning - if (toolCalls && toolCalls.length > 0) { - const hasEncrypted = existingDetails?.some((d) => d.type === "reasoning.encrypted") ?? false - - if (!hasEncrypted) { - // Create ONE fake encrypted block with the FIRST tool call's ID - // This is the documented format from OpenRouter for skipping thought signature validation - const fakeEncrypted = { - type: "reasoning.encrypted", - data: "skip_thought_signature_validator", - id: toolCalls[0].id, - format: "google-gemini-v1", - index: 0, - } + // Convert Anthropic messages to OpenAI format. + // Pass normalization function for Mistral compatibility (requires 9-char alphanumeric IDs) + const isMistral = modelId.toLowerCase().includes("mistral") + let openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ + { role: "system", content: systemPrompt }, + ...convertToOpenAiMessages( + messages, + isMistral ? { normalizeToolCallId: normalizeMistralToolCallId } : undefined, + ), + ] + + // DeepSeek highly recommends using user instead of system role. + if (modelId.startsWith("deepseek/deepseek-r1") || modelId === "perplexity/sonar-reasoning") { + openAiMessages = convertToR1Format([{ role: "user", content: systemPrompt }, ...messages]) + } - return { - ...msg, - reasoning_details: [...(existingDetails ?? []), fakeEncrypted], + // Process reasoning_details when switching models to Gemini. + const isGemini = modelId.startsWith("google/gemini") + + // For Gemini models with native protocol: + // 1. Sanitize messages to handle thought signature validation issues. + // This must happen BEFORE fake encrypted block injection to avoid injecting for + // tool calls that will be dropped due to missing/mismatched reasoning_details. + // 2. Inject fake reasoning.encrypted block for tool calls without existing encrypted reasoning. + // This is required when switching from other models to Gemini to satisfy API validation. + // Per OpenRouter documentation (conversation with Toven, Nov 2025): + // - Create ONE reasoning_details entry per assistant message with tool calls + // - Set `id` to the FIRST tool call's ID from the tool_calls array + // - Set `data` to "skip_thought_signature_validator" to bypass signature validation + // - Set `index` to 0 + // See: https://github.com/cline/cline/issues/8214 + if (isGemini) { + // Step 1: Sanitize messages - filter out tool calls with missing/mismatched reasoning_details + openAiMessages = sanitizeGeminiMessages(openAiMessages, modelId) + + // Step 2: Inject fake reasoning.encrypted block for tool calls that survived sanitization + openAiMessages = openAiMessages.map((msg) => { + if (msg.role === "assistant") { + const toolCalls = (msg as any).tool_calls as any[] | undefined + const existingDetails = (msg as any).reasoning_details as any[] | undefined + + // Only inject if there are tool calls and no existing encrypted reasoning + if (toolCalls && toolCalls.length > 0) { + const hasEncrypted = existingDetails?.some((d) => d.type === "reasoning.encrypted") ?? false + + if (!hasEncrypted) { + // Create ONE fake encrypted block with the FIRST tool call's ID + // This is the documented format from OpenRouter for skipping thought signature validation + const fakeEncrypted = { + type: "reasoning.encrypted", + data: "skip_thought_signature_validator", + id: toolCalls[0].id, + format: "google-gemini-v1", + index: 0, + } + + return { + ...msg, + reasoning_details: [...(existingDetails ?? []), fakeEncrypted], + } } } } - } - return msg - }) - } - - // https://openrouter.ai/docs/features/prompt-caching - // TODO: Add a `promptCacheStratey` field to `ModelInfo`. - if (OPEN_ROUTER_PROMPT_CACHING_MODELS.has(modelId)) { - if (modelId.startsWith("google")) { - // Gemini markers keep their default TTL: ADR-0006 scopes the knob to - // Anthropic-shaped markers only, and Gemini has no 1h class. - addGeminiCacheBreakpoints(systemPrompt, openAiMessages) - } else { - // Capability comes from the resolved ModelInfo, never from the id. - addAnthropicCacheBreakpoints(systemPrompt, openAiMessages, resolveCacheTtl(this.options, model.info)) + return msg + }) } - } - - // https://openrouter.ai/docs/transforms - const completionParams: OpenRouterChatCompletionParams = { - model: modelId, - ...(maxTokens && maxTokens > 0 && { max_tokens: maxTokens }), - temperature, - top_p: topP, - messages: openAiMessages, - stream: true, - stream_options: { include_usage: true }, - // Only include provider if openRouterSpecificProvider is not "[default]". - ...(this.options.openRouterSpecificProvider && - this.options.openRouterSpecificProvider !== OPENROUTER_DEFAULT_PROVIDER_NAME && { - provider: { - order: [this.options.openRouterSpecificProvider], - only: [this.options.openRouterSpecificProvider], - allow_fallbacks: false, - }, - }), - ...(reasoning && { reasoning }), - tools: this.convertToolsForOpenAI(metadata?.tools), - tool_choice: metadata?.tool_choice, - } - // Add Anthropic beta header for fine-grained tool streaming when using Anthropic models - const requestOptions = modelId.startsWith("anthropic/") - ? { headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } } - : undefined + // https://openrouter.ai/docs/features/prompt-caching + // TODO: Add a `promptCacheStratey` field to `ModelInfo`. + if (OPEN_ROUTER_PROMPT_CACHING_MODELS.has(modelId)) { + if (modelId.startsWith("google")) { + // Gemini markers keep their default TTL: ADR-0006 scopes the knob to + // Anthropic-shaped markers only, and Gemini has no 1h class. + addGeminiCacheBreakpoints(systemPrompt, openAiMessages) + } else { + // Capability comes from the resolved ModelInfo, never from the id. + addAnthropicCacheBreakpoints( + systemPrompt, + openAiMessages, + resolveCacheTtl(this.options, model.info), + ) + } + } - let stream - try { - stream = await this.client.chat.completions.create(completionParams, requestOptions) - } catch (error) { - // Try to parse as OpenRouter error structure using Zod - const parseResult = OpenRouterErrorResponseSchema.safeParse(error) + // https://openrouter.ai/docs/transforms + const completionParams: OpenRouterChatCompletionParams = { + model: modelId, + ...(maxTokens && maxTokens > 0 && { max_tokens: maxTokens }), + temperature, + top_p: topP, + messages: openAiMessages, + stream: true, + stream_options: { include_usage: true }, + // Only include provider if openRouterSpecificProvider is not "[default]". + ...(this.options.openRouterSpecificProvider && + this.options.openRouterSpecificProvider !== OPENROUTER_DEFAULT_PROVIDER_NAME && { + provider: { + order: [this.options.openRouterSpecificProvider], + only: [this.options.openRouterSpecificProvider], + allow_fallbacks: false, + }, + }), + ...(reasoning && { reasoning }), + tools: this.convertToolsForOpenAI(metadata?.tools), + tool_choice: metadata?.tool_choice, + } - if (parseResult.success && parseResult.data.error) { - const openRouterError = parseResult.data - const rawString = openRouterError.error?.metadata?.raw - const parsedError = extractErrorFromMetadataRaw(rawString) - const rawErrorMessage = parsedError || openRouterError.error?.message || "Unknown error" + // Add Anthropic beta header for fine-grained tool streaming when using Anthropic models + // and pass the per-request signal so external aborts cancel the in-flight stream. + const requestOptions: OpenAI.RequestOptions = { + ...(modelId.startsWith("anthropic/") + ? { headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } } + : undefined), + signal: requestSignal, + } - const apiError = Object.assign( - new ApiProviderError( - rawErrorMessage, + let stream + try { + // openai@5.23.2 inspects the request signal before the first attempt and after a retry + // sleep has already ended, so a cancellation that lands during the backoff would leave this + // await pending until the sleep finishes. settleOnAbort cuts the wait and rejects with the + // contract AbortError; the SDK promise keeps running on, exactly as before. + stream = await settleOnAbort( + this.client.chat.completions.create(completionParams, requestOptions), + requestSignal, + this.providerName, + ) + } catch (error) { + // Aborted requests are user-initiated: surface them as AbortError instead of + // a completion error (and keep them out of exception telemetry). The SDK reports its own + // cancellations (APIUserAbortError / "Request was aborted.") that the merged signal never + // saw, so recognize those too (precedent: requesty.ts, zoo-gateway.ts). + if (isRequestAborted(error, requestSignal)) { + throw createAbortError("OpenRouter") + } + // Try to parse as OpenRouter error structure using Zod + const parseResult = OpenRouterErrorResponseSchema.safeParse(error) + + if (parseResult.success && parseResult.data.error) { + const openRouterError = parseResult.data + const rawString = openRouterError.error?.metadata?.raw + const parsedError = extractErrorFromMetadataRaw(rawString) + const rawErrorMessage = parsedError || openRouterError.error?.message || "Unknown error" + + const apiError = Object.assign( + new ApiProviderError( + rawErrorMessage, + providerIdentifiers.openrouter, + modelId, + "createMessage", + openRouterError.error?.code, + ), + { + status: openRouterError.error?.code, + error: openRouterError.error, + }, + ) + + TelemetryService.instance.captureException(apiError) + throw handleOpenAIError(error, this.providerName) + } else { + // Fallback for non-OpenRouter errors + const errorMessage = error instanceof Error ? error.message : String(error) + const apiError = new ApiProviderError( + errorMessage, providerIdentifiers.openrouter, modelId, "createMessage", - openRouterError.error?.code, - ), - { - status: openRouterError.error?.code, - error: openRouterError.error, - }, - ) - - TelemetryService.instance.captureException(apiError) - throw handleOpenAIError(error, this.providerName) - } else { - // Fallback for non-OpenRouter errors - const errorMessage = error instanceof Error ? error.message : String(error) - const apiError = new ApiProviderError( - errorMessage, - providerIdentifiers.openrouter, - modelId, - "createMessage", - ) - TelemetryService.instance.captureException(apiError) - throw handleOpenAIError(error, this.providerName) - } - } - - let lastUsage: CompletionUsage | undefined = undefined - // Accumulator for reasoning_details FROM the API. - // We preserve the original shape of reasoning_details to prevent malformed responses. - const reasoningDetailsAccumulator = new Map< - string, - { - type: string - text?: string - summary?: string - data?: string - id?: string | null - format?: string - signature?: string - index: number - } - >() - - // Track whether we've yielded displayable text from reasoning_details. - // When reasoning_details has displayable content (reasoning.text or reasoning.summary), - // we skip yielding the top-level reasoning field to avoid duplicate display. - let hasYieldedReasoningFromDetails = false - const activeToolCallIds = new Set() - - for await (const chunk of stream) { - // OpenRouter returns an error object instead of the OpenAI SDK throwing an error. - if ("error" in chunk) { - this.handleStreamingError(chunk.error as OpenRouterError, modelId, "createMessage") + ) + TelemetryService.instance.captureException(apiError) + throw handleOpenAIError(error, this.providerName) + } } - const delta = chunk.choices[0]?.delta - const finishReason = chunk.choices[0]?.finish_reason - - if (delta) { - // Handle reasoning_details array format (used by Gemini 3, Claude, OpenAI o-series, etc.) - // See: https://openrouter.ai/docs/use-cases/reasoning-tokens#preserving-reasoning-blocks - // Priority: Check for reasoning_details first, as it's the newer format - const deltaWithReasoning = delta as typeof delta & { - reasoning_details?: Array<{ - type: string - text?: string - summary?: string - data?: string - id?: string | null - format?: string - signature?: string - index?: number - }> + let lastUsage: CompletionUsage | undefined = undefined + // Accumulator for reasoning_details FROM the API. + // We preserve the original shape of reasoning_details to prevent malformed responses. + const reasoningDetailsAccumulator = new Map< + string, + { + type: string + text?: string + summary?: string + data?: string + id?: string | null + format?: string + signature?: string + index: number } + >() + + // Track whether we've yielded displayable text from reasoning_details. + // When reasoning_details has displayable content (reasoning.text or reasoning.summary), + // we skip yielding the top-level reasoning field to avoid duplicate display. + let hasYieldedReasoningFromDetails = false + const activeToolCallIds = new Set() + + try { + for await (const chunk of stream) { + // The iterator can keep delivering buffered chunks after the abort has already + // fired (openai@5.23.2 swallows the mid-stream AbortError), so re-check the + // signal before processing each chunk. The yields below are synchronous (there + // is no await between this check and them), so nothing is emitted once the + // signal aborts. + // A yield suspends this generator: the consumer can abort while it is paused + // and we would resume without re-checking. One SDK chunk can yield several + // events, so every yield below is guarded, not just the loop entry. + const abortIfSignalled = () => { + if (requestSignal.aborted) { + throw createAbortError("OpenRouter") + } + } + abortIfSignalled() + + // OpenRouter returns an error object instead of the OpenAI SDK throwing an error. + if ("error" in chunk) { + this.handleStreamingError(chunk.error as OpenRouterError, modelId, "createMessage") + } - if (deltaWithReasoning.reasoning_details && Array.isArray(deltaWithReasoning.reasoning_details)) { - for (const detail of deltaWithReasoning.reasoning_details) { - const index = detail.index ?? 0 - const key = `${detail.type}-${index}` - const existing = reasoningDetailsAccumulator.get(key) + const delta = chunk.choices[0]?.delta + const finishReason = chunk.choices[0]?.finish_reason + + if (delta) { + // Handle reasoning_details array format (used by Gemini 3, Claude, OpenAI o-series, etc.) + // See: https://openrouter.ai/docs/use-cases/reasoning-tokens#preserving-reasoning-blocks + // Priority: Check for reasoning_details first, as it's the newer format + const deltaWithReasoning = delta as typeof delta & { + reasoning_details?: Array<{ + type: string + text?: string + summary?: string + data?: string + id?: string | null + format?: string + signature?: string + index?: number + }> + } - if (existing) { - // Accumulate text/summary/data for existing reasoning detail - if (detail.text !== undefined) { - existing.text = (existing.text || "") + detail.text + if ( + deltaWithReasoning.reasoning_details && + Array.isArray(deltaWithReasoning.reasoning_details) + ) { + for (const detail of deltaWithReasoning.reasoning_details) { + const index = detail.index ?? 0 + const key = `${detail.type}-${index}` + const existing = reasoningDetailsAccumulator.get(key) + + if (existing) { + // Accumulate text/summary/data for existing reasoning detail. Only + // strings accumulate: a non-string fragment would coerce the stored + // value (e.g. "x[object Object]") and be replayed in later requests. + if (typeof detail.text === "string") { + existing.text = (existing.text || "") + detail.text + } + if (typeof detail.summary === "string") { + existing.summary = (existing.summary || "") + detail.summary + } + if (typeof detail.data === "string") { + existing.data = (existing.data || "") + detail.data + } + // Update other fields if provided + if (detail.id !== undefined) existing.id = detail.id + if (detail.format !== undefined) existing.format = detail.format + if (detail.signature !== undefined) existing.signature = detail.signature + } else { + // Start new reasoning detail accumulation + reasoningDetailsAccumulator.set(key, { + type: detail.type, + text: typeof detail.text === "string" ? detail.text : undefined, + summary: typeof detail.summary === "string" ? detail.summary : undefined, + data: typeof detail.data === "string" ? detail.data : undefined, + id: detail.id, + format: detail.format, + signature: detail.signature, + index, + }) + } + + // Yield text for display (still fragmented for live streaming) + // Only reasoning.text and reasoning.summary have displayable content + // reasoning.encrypted is intentionally skipped as it contains redacted content + let reasoningText: string | undefined + if (detail.type === "reasoning.text" && typeof detail.text === "string") { + reasoningText = detail.text + } else if (detail.type === "reasoning.summary" && typeof detail.summary === "string") { + reasoningText = detail.summary + } + + if (reasoningText) { + hasYieldedReasoningFromDetails = true + abortIfSignalled() + yield { type: "reasoning", text: reasoningText } + } } - if (detail.summary !== undefined) { - existing.summary = (existing.summary || "") + detail.summary - } - if (detail.data !== undefined) { - existing.data = (existing.data || "") + detail.data + } + + // Handle top-level reasoning field for UI display. + // Skip if we've already yielded from reasoning_details to avoid duplicate display. + if ("reasoning" in delta && delta.reasoning && typeof delta.reasoning === "string") { + if (!hasYieldedReasoningFromDetails) { + abortIfSignalled() + yield { type: "reasoning", text: delta.reasoning } } - // Update other fields if provided - if (detail.id !== undefined) existing.id = detail.id - if (detail.format !== undefined) existing.format = detail.format - if (detail.signature !== undefined) existing.signature = detail.signature - } else { - // Start new reasoning detail accumulation - reasoningDetailsAccumulator.set(key, { - type: detail.type, - text: detail.text, - summary: detail.summary, - data: detail.data, - id: detail.id, - format: detail.format, - signature: detail.signature, - index, - }) } - // Yield text for display (still fragmented for live streaming) - // Only reasoning.text and reasoning.summary have displayable content - // reasoning.encrypted is intentionally skipped as it contains redacted content - let reasoningText: string | undefined - if (detail.type === "reasoning.text" && typeof detail.text === "string") { - reasoningText = detail.text - } else if (detail.type === "reasoning.summary" && typeof detail.summary === "string") { - reasoningText = detail.summary + // Emit raw tool call chunks - NativeToolCallParser handles state management + if ("tool_calls" in delta && Array.isArray(delta.tool_calls)) { + for (const toolCall of delta.tool_calls) { + if (toolCall.id) { + activeToolCallIds.add(toolCall.id) + } + abortIfSignalled() + yield { + type: "tool_call_partial", + index: toolCall.index, + id: toolCall.id, + name: toolCall.function?.name, + arguments: toolCall.function?.arguments, + } + } } - if (reasoningText) { - hasYieldedReasoningFromDetails = true - yield { type: "reasoning", text: reasoningText } + if (delta.content) { + abortIfSignalled() + yield { type: "text", text: delta.content } } } - } - // Handle top-level reasoning field for UI display. - // Skip if we've already yielded from reasoning_details to avoid duplicate display. - if ("reasoning" in delta && delta.reasoning && typeof delta.reasoning === "string") { - if (!hasYieldedReasoningFromDetails) { - yield { type: "reasoning", text: delta.reasoning } + // Process finish_reason to emit tool_call_end events + // This ensures tool calls are finalized even if the stream doesn't properly close + // Process finish_reason to emit tool_call_end events + // This ensures tool calls are finalized even if the stream doesn't properly close + if (finishReason === "tool_calls") { + for (const id of activeToolCallIds) { + abortIfSignalled() + yield { type: "tool_call_end", id } + } + activeToolCallIds.clear() } - } - // Emit raw tool call chunks - NativeToolCallParser handles state management - if ("tool_calls" in delta && Array.isArray(delta.tool_calls)) { - for (const toolCall of delta.tool_calls) { - if (toolCall.id) { - activeToolCallIds.add(toolCall.id) - } - yield { - type: "tool_call_partial", - index: toolCall.index, - id: toolCall.id, - name: toolCall.function?.name, - arguments: toolCall.function?.arguments, - } + if (chunk.usage) { + lastUsage = chunk.usage } } - if (delta.content) { - yield { type: "text", text: delta.content } + // openai@5.23.2's stream iterator swallows a mid-stream AbortError and returns + // normally instead of throwing, so the catch below would never run: without this + // check, createMessage completes silently after yielding partial output. + if (requestSignal.aborted) { + throw createAbortError("OpenRouter") } - } - // Process finish_reason to emit tool_call_end events - // This ensures tool calls are finalized even if the stream doesn't properly close - if (finishReason === "tool_calls") { - for (const id of activeToolCallIds) { - yield { type: "tool_call_end", id } + // After streaming completes, consolidate and store reasoning_details from the API. + // This filters out corrupted encrypted blocks (missing `data`) and consolidates by index. + if (reasoningDetailsAccumulator.size > 0) { + const rawDetails = Array.from(reasoningDetailsAccumulator.values()) + this.currentReasoningDetails = consolidateReasoningDetails(rawDetails) } - activeToolCallIds.clear() - } - - if (chunk.usage) { - lastUsage = chunk.usage - } - } - - // After streaming completes, consolidate and store reasoning_details from the API. - // This filters out corrupted encrypted blocks (missing `data`) and consolidates by index. - if (reasoningDetailsAccumulator.size > 0) { - const rawDetails = Array.from(reasoningDetailsAccumulator.values()) - this.currentReasoningDetails = consolidateReasoningDetails(rawDetails) - } - if (lastUsage) { - yield { - type: "usage", - inputTokens: lastUsage.prompt_tokens || 0, - outputTokens: lastUsage.completion_tokens || 0, - cacheReadTokens: lastUsage.prompt_tokens_details?.cached_tokens, - reasoningTokens: lastUsage.completion_tokens_details?.reasoning_tokens, - totalCost: (lastUsage.cost_details?.upstream_inference_cost || 0) + (lastUsage.cost || 0), + if (lastUsage) { + yield { + type: "usage", + inputTokens: lastUsage.prompt_tokens || 0, + outputTokens: lastUsage.completion_tokens || 0, + cacheReadTokens: lastUsage.prompt_tokens_details?.cached_tokens, + reasoningTokens: lastUsage.completion_tokens_details?.reasoning_tokens, + totalCost: (lastUsage.cost_details?.upstream_inference_cost || 0) + (lastUsage.cost || 0), + } + } + } catch (error) { + // Normalize abort-driven stream failures (SDK abort or timeout errors) to a + // DOM-standard AbortError so callers can detect the aborted request, including the abort + // shapes the SDK reports on its own without our signal firing. + if (isRequestAborted(error, requestSignal)) { + throw createAbortError("OpenRouter") + } + throw error } + } finally { + // Detach on every exit path - completion, failure, abort, or the consumer + // closing the generator - so the listener never outlives this request. + removeExternalAbortListener() + // Cancel the in-flight request when the consumer abandons the generator + // (early break / downstream error). No-op once the stream has completed + // or the signal is already aborted. + teardownController.abort() } } public async fetchModel() { + const { info } = await this.runModelLookup() + return info + } + + /** + * Run one catalog lookup and publish whichever records it returned, under the per-catalog + * sequences above. It also reports what this lookup itself saw: a lookup that came back empty + * leaves the previous records published (both retention rules are deliberate), so the published + * maps cannot tell a fresh catalog from a failed fetch, and a loader that reads them would memoize + * the failure as a complete load. + */ + private async runModelLookup() { + // Claim the lookup id before the first await so a lookup that starts later + // always outranks an earlier one when both are in flight. + const sequence = ++this.fetchModelSequence const [models, endpoints] = await Promise.all([ getModels({ provider: providerIdentifiers.openrouter }, this), getModelEndpoints( @@ -563,19 +701,57 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH ), ]) - this.models = models - this.endpoints = endpoints + // A slower earlier lookup must not overwrite records a newer lookup already + // published: writing them would silently roll model metadata (pricing, + // limits, endpoint records) back to an older fetch. The stale lookup still + // returns the handler's current model, which is what its caller observes. + if (sequence > this.publishedModelSequence && Object.keys(models).length > 0) { + this.publishedModelSequence = sequence + this.models = models + } - return this.getModel() + // Endpoints are ordered by their own sequence for the same reason, and they publish even when + // the model catalog came back empty: a catalog miss must not discard a successful endpoint + // answer, whose records carry the pricing and limits for the selected provider. The endpoint + // lookup answers an empty map when its request failed, and publishing that would drop the + // records a previous successful lookup installed, so only a non-empty answer replaces them. + if (sequence > this.publishedEndpointSequence && Object.keys(endpoints).length > 0) { + this.publishedEndpointSequence = sequence + this.endpoints = endpoints + } + + return { + info: this.getModel(), + // What this lookup itself saw, reported alongside the model so a loader can judge its own + // attempt instead of the records that happen to be published. + hasModels: Object.keys(models).length > 0, + hasEndpoints: Object.keys(endpoints).length > 0, + } } override getModel() { const id = this.options.openRouterModelId ?? openRouterDefaultModelId let info = this.models[id] ?? openRouterDefaultModelInfo - // If a specific provider is requested, use the endpoint for that provider. - if (this.options.openRouterSpecificProvider && this.endpoints[this.options.openRouterSpecificProvider]) { - info = this.endpoints[this.options.openRouterSpecificProvider] + const specificProvider = this.options.openRouterSpecificProvider + const endpoint = specificProvider ? this.endpoints[specificProvider] : undefined + + // The endpoint record is what OpenRouter reported for the selected provider - its limits, + // pricing and description - so those keep coming from it. Its two capability fields are a copy + // taken off whichever catalog was current when the endpoints were fetched + // (src/api/providers/fetchers/modelEndpointCache.ts:74-79), and an endpoint record is only ever + // replaced by a lookup that actually returned records, so a retained one can predate several + // catalog publications. Model-level capabilities therefore follow the newest catalog entry when + // the model has one, and fall back to the endpoint's copy on a catalog miss. + if (endpoint) { + const model = this.models[id] + info = model + ? { + ...endpoint, + supportsReasoningEffort: model.supportsReasoningEffort, + supportedParameters: model.supportedParameters ? [...model.supportedParameters] : undefined, + } + : endpoint } // Re-apply the Moonshot K3 profile at consumption time: model records are @@ -600,8 +776,32 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH return { id, info, topP: isDeepSeekR1 ? 0.95 : undefined, ...params } } - async completePrompt(prompt: string, _options?: CompletePromptOptions) { - const { id: modelId, maxTokens, temperature, reasoning } = await this.fetchModel() + async completePrompt(prompt: string, options?: CompletePromptOptions) { + // Establish the cancellation scope before model lookup: a pre-aborted call, or + // one aborted while model metadata is loading, must reject promptly instead of + // waiting for the lookup to settle. The configured timeoutMs covers the lookup + // as well. + const requestAbortSignal = mergeAbortSignalAndTimeout(options?.abortSignal, options?.timeoutMs) + if (requestAbortSignal?.aborted) { + throw createAbortError(this.providerName) + } + + let model: Awaited> + try { + model = requestAbortSignal + ? await rejectOnAbort(this.fetchModel(), requestAbortSignal, this.providerName) + : await this.fetchModel() + } catch (error) { + // Only our own merged signal marks this as a cancellation. An + // abort-shaped failure from the model cache's internal timeout is a + // discovery failure, not a caller cancellation, so it propagates with + // its original identity. + if (requestAbortSignal?.aborted) { + throw createAbortError(this.providerName) + } + throw error + } + const { id: modelId, maxTokens, temperature, reasoning } = model const completionParams: OpenRouterChatCompletionParams = { model: modelId, @@ -622,15 +822,46 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH } // Add Anthropic beta header for fine-grained tool streaming when using Anthropic models - const requestOptions = modelId.startsWith("anthropic/") - ? { headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } } - : undefined + // and forward the caller's abort signal / per-request timeout to the SDK. The merged + // signal (established before model lookup, above) aborts when either the caller's signal + // or the timeout fires, so timeouts are normalized to AbortError in the catch below. + // The client-level timeout remains the default safety net; timeoutMs <= 0 disables the + // per-request timeout, and 0 is never passed to the SDK. + // The SDK validates a per-request timeout as a uint32: non-finite values + // must be dropped, fractional delays rounded up (they fail its integer + // validation) and delays above 2^31-1 ms capped (Node clamps them to 1 ms + // otherwise). Same policy as mergeAbortSignalAndTimeout, which owns the + // signal side of the same option. + const perRequestTimeoutMs = + typeof options?.timeoutMs === "number" && Number.isFinite(options.timeoutMs) && options.timeoutMs > 0 + ? toSdkTimeoutMs(options.timeoutMs) + : undefined + + const requestOptions: OpenAI.RequestOptions = { + ...(modelId.startsWith("anthropic/") + ? { headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } } + : undefined), + ...(requestAbortSignal && { signal: requestAbortSignal }), + ...(perRequestTimeoutMs !== undefined && { timeout: perRequestTimeoutMs }), + } let response try { - response = await this.client.chat.completions.create(completionParams, requestOptions) + // The same retry-backoff window as createMessage: cut the wait so a cancellation that lands + // during the SDK's sleep settles as AbortError at once instead of after the backoff ends. + response = await settleOnAbort( + this.client.chat.completions.create(completionParams, requestOptions), + requestAbortSignal, + this.providerName, + ) } catch (error) { + // Aborted requests are user-initiated: surface them as AbortError (this also covers + // timeouts, which abort the same signal) instead of a completion error. SDK-reported + // abort shapes are normalized as well (see the createMessage catch). + if (isRequestAborted(error, requestAbortSignal)) { + throw createAbortError("OpenRouter") + } // Try to parse as OpenRouter error structure using Zod const parseResult = OpenRouterErrorResponseSchema.safeParse(error) @@ -670,6 +901,11 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH } } + if (requestAbortSignal?.aborted) { + // The response resolved after the request was aborted: do not return the late result. + throw createAbortError("OpenRouter") + } + if ("error" in response) { this.handleStreamingError(response.error as OpenRouterError, modelId, "completePrompt") } diff --git a/src/api/transform/__tests__/openai-format.spec.ts b/src/api/transform/__tests__/openai-format.spec.ts index 8a473e6cbc6..24bcdeb78f6 100644 --- a/src/api/transform/__tests__/openai-format.spec.ts +++ b/src/api/transform/__tests__/openai-format.spec.ts @@ -1394,6 +1394,77 @@ describe("consolidateReasoningDetails", () => { expect(result).toHaveLength(1) expect(result[0].summary).toBe("Summary part 1Summary part 2") }) + + it("should consolidate interleaved item types and assert the full output array", () => { + const details: ReasoningDetail[] = [ + { type: "reasoning.summary", summary: "s1", format: "google-gemini-v1", index: 0 }, + { + type: "reasoning.text", + text: "t1", + signature: "sig_1", + id: "rs_t1", + format: "google-gemini-v1", + index: 0, + }, + { type: "reasoning.encrypted", data: "e1", id: "rs_e1", format: "google-gemini-v1", index: 0 }, + { + type: "reasoning.text", + text: "t2", + signature: "sig_2", + id: "rs_t2", + format: "google-gemini-v1", + index: 0, + }, + { type: "reasoning.encrypted", data: "e2", id: "rs_e2", format: "google-gemini-v1", index: 0 }, + { type: "reasoning.encrypted", data: "e3", id: "rs_e3", format: "google-gemini-v1", index: 1 }, + { type: "reasoning.summary", summary: "only summary", format: "google-gemini-v1", index: 1 }, + ] + + const result = consolidateReasoningDetails(details) + + // Full output array, not a subset: consolidated text per index, then the summary-only entry + // per index, then the last encrypted block per index. The summary interleaved at index 0 is + // dropped because text exists at that index, while the index-1 summary survives because no + // text arrived there; a single-type test cannot fail when that predicate narrows. The text + // entry takes id, format and type from the last item in the group that carries them: the + // encrypted fragment arrives last at index 0, so index 0 reports rs_e2 and the consolidated + // text block is labelled reasoning.encrypted (openai-format.ts:93-99, 105). Asserted as + // measured at this revision, because the full array is what catches that labelling. + expect(result).toEqual([ + { + type: "reasoning.encrypted", + text: "t1t2", + signature: "sig_2", + id: "rs_e2", + format: "google-gemini-v1", + index: 0, + }, + { + type: "reasoning.encrypted", + data: "e2", + signature: undefined, + id: "rs_e2", + format: "google-gemini-v1", + index: 0, + }, + { + type: "reasoning.summary", + summary: "only summary", + signature: undefined, + id: "rs_e3", + format: "google-gemini-v1", + index: 1, + }, + { + type: "reasoning.encrypted", + data: "e3", + signature: undefined, + id: "rs_e3", + format: "google-gemini-v1", + index: 1, + }, + ]) + }) }) describe("sanitizeGeminiMessages", () => { diff --git a/src/eslint-suppressions.json b/src/eslint-suppressions.json index 4e010d814f3..ce1137b2c87 100644 --- a/src/eslint-suppressions.json +++ b/src/eslint-suppressions.json @@ -144,11 +144,6 @@ "count": 3 } }, - "api/providers/__tests__/openrouter.spec.ts": { - "@typescript-eslint/no-explicit-any": { - "count": 34 - } - }, "api/providers/__tests__/poe.spec.ts": { "@typescript-eslint/no-explicit-any": { "count": 1 diff --git a/src/test-utils/promise.ts b/src/test-utils/promise.ts new file mode 100644 index 00000000000..6d14892e0e7 --- /dev/null +++ b/src/test-utils/promise.ts @@ -0,0 +1,20 @@ +/** + * Fail fast when a promise never settles. Mutations that break settle or + * reject wiring would otherwise hang the test until Stryker's per-mutant + * timeout, marking the mutant "Timeout" instead of "Killed". + */ +export function settlesWithin(promise: Promise, ms: number): Promise { + return new Promise((resolve, reject) => { + const timer = setTimeout(() => reject(new Error(`operation did not settle within ${ms}ms`)), ms) + void promise.then( + (value) => { + clearTimeout(timer) + resolve(value) + }, + (error) => { + clearTimeout(timer) + reject(error) + }, + ) + }) +} From e89756a882545599b5ce4ef458bae9db6c0158dd Mon Sep 17 00:00:00 2001 From: "user.email" <136036952+easonLiangWorldedtech@users.noreply.github.com> Date: Tue, 24 Nov 2026 01:04:31 +0800 Subject: [PATCH 02/11] test(api): pin the post-settlement abort check and bound the readiness waits CodeRabbit's re-review of ebac6d958daf raised three inline findings on this pull; two are addressed here and one is answered with measurement rather than a change. - openrouter.spec.ts:1897 (4668581516): the readiness wait on `createAwaited` was the one unbounded await in the retry-backoff test, so a discovery that rejected before the create call left the test waiting until the runner timed out. Both twins of that test now bound the wait with settlesWithin like the assertion next to them. Control: replacing the bounded wait with a never-settling promise reproduces the reported failure exactly - 'Test timed out in 20000ms' for that test, and vitest additionally reports an unhandled rejection from the generator step. - openrouter.ts:751 (4668581543): not changed. The endpoint record's capability fields are a copy of whichever catalog was current when the endpoints were fetched (modelEndpointCache.ts:74-79), an endpoint record carries no publication stamp - the only freshness signal in that cache is the NodeCache TTL at modelEndpointCache.ts:19 - and models and endpoints are fetched together under one sequence in runModelLookup (openrouter.ts:687-720), so which side is newer is not decidable at the merge point. The precedence that is there is deliberate and pinned by two tests in this pull, 'takes model capabilities from the newest catalog and endpoint limits from the endpoint' and 'keeps the endpoint's capability copy when the catalog has no entry for the model'. Choosing the other precedence, or stamping generations on endpoint records, is a design change in a file this pull already bills 322 lines for, so it is left to the maintainer rather than folded into a fix. - openrouter.spec.ts:4241 (4668581531): agreed and fixed. No test could see the signal check after the create await: forcing its condition false left all 139 tests passing. The new test reaches it by queueing the cancellation after the SDK promise is fulfilled, which is after settleOnAbort has decided the race and detached its listener but before the provider resumes; the same control now fails exactly that test (1 failed, 139 passed). Two skips, both environmental and both named: local Stryker is unreachable in these worktrees (node_modules/.bin/stryker ENOENT), so no mutant counts are claimed here; and the src typecheck exits 2 on the pre-existing TS2420 in extension/api.ts, which is byte-identical at the pull head, so it is reported rather than fixed in this lane. vitest on this file: 140 passed (140), exit 0. prettier --check on this file and openrouter.ts: exit 0. --- .../providers/__tests__/openrouter.spec.ts | 54 +++++++++++++++++-- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/src/api/providers/__tests__/openrouter.spec.ts b/src/api/providers/__tests__/openrouter.spec.ts index dc29bd310b6..d528de78b06 100644 --- a/src/api/providers/__tests__/openrouter.spec.ts +++ b/src/api/providers/__tests__/openrouter.spec.ts @@ -1893,8 +1893,10 @@ describe("OpenRouterHandler", () => { ) const next = settlesWithin(generator.next(), ABORT_SETTLE_MS) // Only abort once the create call is in flight: model discovery has its own abort race, and - // aborting earlier would settle the generator there instead of at the create await. - await createAwaited + // aborting earlier would settle the generator there instead of at the create await. The readiness + // wait is bounded by the same helper that bounds the generator step, so a discovery that + // rejects fails this line instead of hanging it until the runner times out. + await settlesWithin(createAwaited, ABORT_SETTLE_MS) controller.abort() await expect(next).rejects.toMatchObject({ @@ -4246,6 +4248,48 @@ describe("OpenRouterHandler", () => { }) }) + it("rejects with AbortError when the SDK fulfils first and the abort is queued before the provider resumes", async () => { + // The abort-first case above cannot see the post-settlement signal check under the create + // await: settleOnAbort rejects there first, so the check survives its removal. Aborting + // synchronously after the fulfilment is no better - that is still inside settleOnAbort, + // whose own listener rejects and hides the check; both were measured. Queueing the + // cancellation is the ordering that reaches it: by the time this microtask runs the race + // is decided and the listener is detached, so the abort cannot reject the wait and can + // only flip the signal the check reads on the way back from the fulfilment. + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + let notifyCreateStarted!: () => void + const createStarted = new Promise((resolve) => { + notifyCreateStarted = resolve + }) + let resolveCreate!: (value: unknown) => void + const mockCreate = vitest.fn().mockImplementation(() => { + notifyCreateStarted() + return new Promise((resolve) => { + resolveCreate = resolve + }) + }) + // The auto-mocked OpenAI client is injected via a structural type to avoid `any` casts. + const client = handler["client"] as unknown as { chat: { completions: { create: typeof mockCreate } } } + Object.defineProperty(client, "chat", { + value: { completions: { create: mockCreate } }, + configurable: true, + writable: true, + }) + + const promise = handler.completePrompt("test prompt", { abortSignal: controller.signal }) + await createStarted + resolveCreate({ choices: [{ message: { content: "late result" } }] }) + queueMicrotask(() => { + controller.abort() + }) + + await expect(promise).rejects.toMatchObject({ + name: "AbortError", + message: "The openrouter request was aborted", + }) + }) + it("settles the completePrompt await as AbortError while the SDK is parked in a retry backoff", async () => { // The same backoff window as createMessage: a create() asleep in its retry sleep never // settles, so the race in settleOnAbort is what ends the wait on a caller cancellation. @@ -4264,8 +4308,10 @@ describe("OpenRouterHandler", () => { ) const completion = handler.completePrompt("test prompt", { abortSignal: controller.signal }) - // Abort only after the create call is in flight so this pins the create await, not model discovery. - await createAwaited + // Abort only after the create call is in flight so this pins the create await, not model + // discovery. The readiness wait is bounded by the same helper the completion assertion + // uses, so a discovery that rejects fails here instead of hanging until the runner times out. + await settlesWithin(createAwaited, ABORT_SETTLE_MS) controller.abort() await expect(settlesWithin(completion, ABORT_SETTLE_MS)).rejects.toMatchObject({ From 6f007ca6bd34d659c77470672371def4339dea80 Mon Sep 17 00:00:00 2001 From: fixer Date: Tue, 24 Nov 2026 01:50:47 +0800 Subject: [PATCH 03/11] test(openrouter): bound create-readiness waits so a discovery failure reports itself The review thread on the queued-abort test says the new test repeats the readiness-wait concern raised earlier on this pull: the test awaits create() readiness with no bound, so a model discovery that rejects leaves the readiness promise unsettled and the test waits until the runner times out, while the operation has already rejected with no handler on it. That claim reproduces, and it is not specific to the new test: the same unbounded `await createStarted` appears at five sites in this file (lines 3164, 4241, 4281, 4400 and 4487 of the parent commit). All five are fixed the same way: - the readiness wait uses `settlesWithin(createStarted, ABORT_SETTLE_MS)`, the helper this file already imports and uses for the same purpose at its deferred-discovery tests; - the operation promise gets an observation handler at creation (`promise.catch(() => {})`, the disposition AGENTS.md lists for a floating promise) so a discovery rejection cannot surface as an unhandled rejection while the test waits; the existing `await expect(...).rejects` assertions stay where they are and still report the outcome. Measured, same file, same test, same worktree (wf-1545-r3), vitest.api.config.ts: - intact, whole file: `Test Files 1 passed (1)` / `Tests 140 passed (140)`, exit 0. - control (inject `getModels` rejecting once so create() never starts): parent commit e89756a882: `Tests 1 failed | 139 skipped (140)`, `Error: Test timed out in 20000ms.`, `Error: control: discovery failed`, `Vitest caught 1 unhandled error during the test run.` - 21.75 s wall. this commit: same scenario at all five sites -> `Error: operation did not settle within 150ms`, no unhandled-error line - 1.87-1.91 s wall each. Only a spec changed: blob 30e9fb8863535ee02dacb1278ed7aa3c9e1b3397 replaces dc29bd310b628c95f0a6fa20ded4f849887c03b4's successor d528de78b0628c95f0a6fa20ded4f849887c03b4 in src/api/providers/__tests__/openrouter.spec.ts (4654 -> 4674 lines, trailing LF kept, no CRLF introduced). No billed path is touched, so the mutation gate's changed-line bill is unchanged at extension (322 lines); no Stryker directive was added or removed. `prettier --check` exits 0. `eslint --prune-suppressions --max-warnings=0` exits 0; the touched file carries no suppression entry either way, and the suppressions file was left at HEAD because --prune-suppressions rewrites it whole-file (30422 -> 32770 bytes) with 261 entries and 2813 total suppressions identical on both sides and zero differing keys. `tsc --noEmit -p tsconfig.json` still reports only the environmental `extension/api.ts(45,14): error TS2420`. --- .../providers/__tests__/openrouter.spec.ts | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/api/providers/__tests__/openrouter.spec.ts b/src/api/providers/__tests__/openrouter.spec.ts index d528de78b06..30e9fb88635 100644 --- a/src/api/providers/__tests__/openrouter.spec.ts +++ b/src/api/providers/__tests__/openrouter.spec.ts @@ -3160,8 +3160,12 @@ describe("OpenRouterHandler", () => { const generator = handler.createMessage("system", [{ role: "user" as const, content: "hi" }], metadata) const nextPromise = generator.next() - // Abort only once create() has actually started. - await createStarted + // Observe from creation so an early model-discovery rejection cannot surface as an + // unhandled rejection while this test waits for readiness; the assertion below reports it. + nextPromise.catch(() => {}) + // Abort only once create() has actually started, bounded so that a create() which + // never starts fails here instead of hanging until the runner times out. + await settlesWithin(createStarted, ABORT_SETTLE_MS) controller.abort() await expect(nextPromise).rejects.toMatchObject({ @@ -4238,7 +4242,11 @@ describe("OpenRouterHandler", () => { }) const promise = handler.completePrompt("test prompt", { abortSignal: controller.signal }) - await createStarted + // Observe from creation so an early model-discovery rejection cannot surface as an + // unhandled rejection while this test waits for readiness; the assertion below reports it. + promise.catch(() => {}) + // Bounded readiness: a create() that never starts fails here instead of hanging. + await settlesWithin(createStarted, ABORT_SETTLE_MS) controller.abort() resolveCreate({ choices: [{ message: { content: "late result" } }] }) @@ -4278,7 +4286,12 @@ describe("OpenRouterHandler", () => { }) const promise = handler.completePrompt("test prompt", { abortSignal: controller.signal }) - await createStarted + // Observe from creation so an early model-discovery rejection cannot surface as an + // unhandled rejection while this test waits for readiness; the assertion below reports it. + promise.catch(() => {}) + // Bounded readiness, the same helper the neighbouring tests use: a create() that never + // starts fails here instead of hanging until the runner times out. + await settlesWithin(createStarted, ABORT_SETTLE_MS) resolveCreate({ choices: [{ message: { content: "late result" } }] }) queueMicrotask(() => { controller.abort() @@ -4397,7 +4410,10 @@ describe("OpenRouterHandler", () => { }) const promise = handler.completePrompt("test prompt", { abortSignal: controller.signal }) - await createStarted + // Observe from creation so an early model-discovery rejection cannot surface as an + // unhandled rejection while this test waits for readiness; the assertion below reports it. + promise.catch(() => {}) + await settlesWithin(createStarted, ABORT_SETTLE_MS) controller.abort() await expect(promise).rejects.toMatchObject({ @@ -4483,8 +4499,12 @@ describe("OpenRouterHandler", () => { abortSignal: controller.signal, timeoutMs: 100_000, }) - // Abort only once create() has actually started (after model lookup). - await createStarted + // Observe from creation so an early model-discovery rejection cannot surface as an + // unhandled rejection while this test waits for readiness; the assertion below reports it. + promise.catch(() => {}) + // Abort only once create() has actually started (after model lookup), bounded so a + // create() that never starts fails here instead of hanging until the runner times out. + await settlesWithin(createStarted, ABORT_SETTLE_MS) controller.abort() await expect(promise).rejects.toMatchObject({ From adf6e9a77d237f39812a1b11d7632803a969816e Mon Sep 17 00:00:00 2001 From: "user.email" <136036952+easonLiangWorldedtech@users.noreply.github.com> Date: Tue, 24 Nov 2026 03:04:35 +0800 Subject: [PATCH 04/11] fix(openrouter): bridge the caller signal in completePrompt and detach it on every exit path The review asks for lifecycle cleanup on the completePrompt abort path. The premise holds against the installed SDK: openai@5.23.2 client.mjs:350-351 does const { signal, method, ...options } = init || {}; if (signal) signal.addEventListener('abort', () => controller.abort()); and the package carries one addEventListener in client.js/client.mjs and no removeEventListener (counted across all 216 js/mjs files: client.js add 1 remove 0, client.mjs add 1 remove 0). So the SDK keeps a listener - and the per-request controller behind it - on whichever signal it is handed. completePrompt handed it the caller's own signal: mergeAbortSignalAndTimeout returns the external signal unchanged when there is no usable timeout (utils/abort-signal.ts:42-44), so every completed request without timeoutMs retained one listener on a signal the caller keeps alive. The fix mirrors what this file already does in createMessage (lines 248 and 664-667) and what requesty.ts does at 277-280 and 342-346: bridge first, merge the bridged signal, and run the returned dispose in a finally so completion, failure, abort and a late result all detach it. const { signal: bridgedCallerSignal, dispose: removeExternalAbortListener } = bridgeExternalAbortSignal(options?.abortSignal) const requestAbortSignal = mergeAbortSignalAndTimeout(bridgedCallerSignal, options?.timeoutMs) try { ... } finally { removeExternalAbortListener() } Tests (api layer, vitest.api.config.ts, this worktree's repaired-hybrid node_modules: src/node_modules junctioned and the @roo-code scope repointed at this worktree's packages, so every count below is labelled repaired-hybrid, not a clean install): - intact, this file: Test Files 1 passed (1) / Tests 142 passed (142), exit 0, no unhandled-error line. - intact, whole api project: Test Files 121 passed (121) / Tests 5824 passed | 2 skipped (5826), exit 0. - new test 'detaches its abort listener from the caller signal when the request finishes without a timeout': two completePrompt calls with no timeoutMs on one caller signal, counting its addEventListener/removeEventListener pair and the listeners left attached. The double mirrors the SDK behaviour above (it attaches to the signal it receives and never detaches), otherwise the mocked client would hide the very retention the change is about. - control A (delete the finally-path dispose): Tests 1 failed | 141 passed (142), exit 1, that test only, AssertionError: expected +0 to be 2 - removed 0 against added 2. - control B (hand the caller signal straight to the merge, i.e. no bridge): Tests 2 failed | 140 passed (142), exit 1: the listener test (expected 6 to be 8 - two listeners survive on the caller signal, exactly the SDK's) and 'should pass abort signal through to client' (expected AbortSignal { aborted: false } not to be AbortSignal { aborted: false }). - control C (keep the bridge but drop timeoutMs from the merge): Tests 2 failed | 140 passed (142), exit 1 - 'rejects with AbortError when a timeout elapses during deferred model discovery' and 'rejects with AbortError when only a timeout is provided and it elapses'. The timeout path is therefore still pinned after the reordering. - control D (the opposite order: merge first, then bridge the merged signal) stayed Tests 142 passed (142), exit 0: these tests pin which object the SDK sees, that nothing stays attached to the caller signal, and that the timeout still reaches - they do not pin the order itself. A probe with node:events getEventListeners showed AbortSignal.any adds no visible listener on its sources (spy count 0, visible listeners 0, 0 while a composite was held), so the composite is not the leak and the order is a style choice here, taken from the existing callers. - one existing assertion had to move: 'should pass abort signal through to client' asserted expect.objectContaining({ signal: controller.signal }), which pinned the leak's symptom (the SDK being handed the caller's own object). It now asserts a signal is passed and that it is not the caller's object; propagation is asserted by its own new test, not inherited from identity. - local Stryker stays unavailable (.bin/stryker ENOENT): no mutant count measured locally. Tools: prettier --check exits 0 on both files. eslint --prune-suppressions --max-warnings=0 exits 0 on both files and eslint --max-warnings=0 exits 0 on api/providers; api/providers/openrouter.ts keeps its 6 @typescript-eslint/no-explicit-any suppressions and the spec has no entry, so no count rose. The suppressions file was reverted to HEAD because --prune-suppressions rewrites it whole-file (30422 -> 32770 bytes) with 261 entries and 2813 total suppressions identical and zero differing keys. tsc --noEmit -p tsconfig.json reports only the environmental extension/api.ts(45,14): error TS2420. No Stryker disable directive was added or removed. Bill (scripts/stryker-diff.mjs counting - hunk + side line numbers intersected with executable AST nodes - run against this tree c605d48669c011ad4632741f0e7bdd2b971bcfb6 with base 050c49ef8269: extension 378 lines, overCeiling [], gate max 500, per-file line src/api/providers/openrouter.ts 378; the spec, src/api/transform/__tests__/openai-format.spec.ts and src/test-utils/promise.ts land in test-or-unattributed). Up from the 322 that CI printed twice for head 6f007ca6bd: the bridge, merge, try and dispose lines are only 6 new executable lines; the other ~50 come from re-indenting the 130-line body, which turns previously-unchanged lines into changed ones. --- .../providers/__tests__/openrouter.spec.ts | 76 +++++- src/api/providers/openrouter.ts | 253 ++++++++++-------- 2 files changed, 209 insertions(+), 120 deletions(-) diff --git a/src/api/providers/__tests__/openrouter.spec.ts b/src/api/providers/__tests__/openrouter.spec.ts index 30e9fb88635..b12eec84da4 100644 --- a/src/api/providers/__tests__/openrouter.spec.ts +++ b/src/api/providers/__tests__/openrouter.spec.ts @@ -14,6 +14,8 @@ vitest.mock("../utils/timeout-config", () => ({ const MOCK_TIMEOUT_MS = 300_000 +import { getEventListeners } from "node:events" + import { Anthropic } from "@anthropic-ai/sdk" import OpenAI from "openai" @@ -4002,8 +4004,80 @@ describe("OpenRouterHandler", () => { await handler.completePrompt("test prompt", { abortSignal: controller.signal }) expect(mockCreate).toHaveBeenCalledWith( expect.objectContaining({ model: expect.any(String) }), - expect.objectContaining({ signal: controller.signal }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), ) + // What the SDK is handed is the per-request bridge output, not the caller's own signal: + // the SDK attaches its abort listener to whatever signal it receives and leaves it attached + // once the request ends, so forwarding the caller's long-lived signal would retain one + // listener on it for every completed request. That the caller's cancellation still reaches + // the SDK through the bridge is a separate claim, pinned by its own test below. + const createOptions = mockCreate.mock.calls[0]?.[1] + expect(createOptions?.signal).toBeDefined() + expect(createOptions?.signal).not.toBe(controller.signal) + }) + + it("detaches its abort listener from the caller signal when the request finishes without a timeout", async () => { + // The leak: openai@5.23.2 client.mjs:350-351 attaches an abort listener to whichever signal + // it is handed and never detaches it (client.js carries one addEventListener and no + // removeEventListener), so handing the SDK the caller's own long-lived signal retains one + // listener - and the SDK controller behind it - per completed request. The double below + // mirrors that behaviour so the pair, not just the bridge, is what the test measures. + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + const create = vitest.fn().mockImplementation((_params: unknown, options: { signal?: AbortSignal }) => { + options?.signal?.addEventListener("abort", () => {}) + return Promise.resolve({ choices: [{ message: { content: "response" } }] }) + }) + stubCompletionsCreate(handler, create) + + const added = vitest.spyOn(controller.signal, "addEventListener") + const removed = vitest.spyOn(controller.signal, "removeEventListener") + let addedCount = 0 + let removedCount = 0 + let listenersLeftOnCallerSignal = -1 + try { + // Two calls with no timeoutMs: the branch where the merge used to return the caller's + // signal unchanged, so each completed request left its listener behind. + await handler.completePrompt("test prompt", { abortSignal: controller.signal }) + await handler.completePrompt("test prompt", { abortSignal: controller.signal }) + // Take the evidence before restoring: `mockRestore` clears the call log the two calls + // just filled, so asserting on the spies after the finally asserts on an emptied record. + addedCount = added.mock.calls.length + removedCount = removed.mock.calls.length + listenersLeftOnCallerSignal = getEventListeners(controller.signal, "abort").length + } finally { + added.mockRestore() + removed.mockRestore() + } + + // The floor keeps the pair equality from being satisfied by an empty record: a double that + // never touched the signal at all would leave both counts at 0 and pass 0 === 0. + expect(addedCount, "expected the caller signal to be observed at least once").toBeGreaterThanOrEqual(1) + expect(removedCount).toBe(addedCount) + // The leak itself: nothing may stay attached to the caller's signal once the request ended. + expect(listenersLeftOnCallerSignal, "caller signal kept a listener after the request").toBe(0) + }) + + it("propagates a caller abort to the signal the SDK receives", async () => { + // The ordering half of the bridge: the caller's cancellation must still reach the SDK + // through the per-request signal, and it must do so while the request is in flight. + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + let abortedDuringRequest: boolean | undefined + const create = vitest.fn().mockImplementation((_params: unknown, options: { signal?: AbortSignal }) => { + controller.abort() + abortedDuringRequest = options?.signal?.aborted + return Promise.resolve({ choices: [{ message: { content: "late result" } }] }) + }) + stubCompletionsCreate(handler, create) + + await expect( + handler.completePrompt("test prompt", { abortSignal: controller.signal }), + ).rejects.toMatchObject({ + name: "AbortError", + message: "The openrouter request was aborted", + }) + expect(abortedDuringRequest, "the signal handed to the SDK must abort with the caller's").toBe(true) }) it("should pass timeout through to client", async () => { diff --git a/src/api/providers/openrouter.ts b/src/api/providers/openrouter.ts index dea46d26a96..7b6ae4f7bd0 100644 --- a/src/api/providers/openrouter.ts +++ b/src/api/providers/openrouter.ts @@ -776,141 +776,156 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH } async completePrompt(prompt: string, options?: CompletePromptOptions) { - // Establish the cancellation scope before model lookup: a pre-aborted call, or - // one aborted while model metadata is loading, must reject promptly instead of - // waiting for the lookup to settle. The configured timeoutMs covers the lookup - // as well. - const requestAbortSignal = mergeAbortSignalAndTimeout(options?.abortSignal, options?.timeoutMs) - if (requestAbortSignal?.aborted) { - throw createAbortError(providerIdentifiers.openrouter) - } - - let model: Awaited> + // Bridge the caller's signal before anything else: the OpenAI SDK attaches its abort + // listener to whichever signal it receives and leaves it attached once the request + // completes, and with no usable timeout `mergeAbortSignalAndTimeout` would hand it the + // caller's own long-lived signal - one retained listener and controller per completed + // request. The bridge gives the SDK a per-request signal and returns the handle that + // detaches it, which the `finally` below must run on every exit path. + // + // Establishing the cancellation scope here also covers model lookup: a pre-aborted call, + // or one aborted while model metadata is loading, must reject promptly instead of waiting + // for the lookup to settle. The configured timeoutMs covers the lookup as well. + const { signal: bridgedCallerSignal, dispose: removeExternalAbortListener } = bridgeExternalAbortSignal( + options?.abortSignal, + ) + const requestAbortSignal = mergeAbortSignalAndTimeout(bridgedCallerSignal, options?.timeoutMs) try { - model = requestAbortSignal - ? await rejectOnAbort(this.fetchModel(), requestAbortSignal, providerIdentifiers.openrouter) - : await this.fetchModel() - } catch (error) { - // Only our own merged signal marks this as a cancellation. An - // abort-shaped failure from the model cache's internal timeout is a - // discovery failure, not a caller cancellation, so it propagates with - // its original identity. if (requestAbortSignal?.aborted) { throw createAbortError(providerIdentifiers.openrouter) } - throw error - } - const { id: modelId, maxTokens, temperature, reasoning } = model - - const completionParams: OpenRouterChatCompletionParams = { - model: modelId, - max_tokens: maxTokens, - temperature, - messages: [{ role: "user", content: prompt }], - stream: false, - // Only include provider if openRouterSpecificProvider is not "[default]". - ...(this.options.openRouterSpecificProvider && - this.options.openRouterSpecificProvider !== OPENROUTER_DEFAULT_PROVIDER_NAME && { - provider: { - order: [this.options.openRouterSpecificProvider], - only: [this.options.openRouterSpecificProvider], - allow_fallbacks: false, - }, - }), - ...(reasoning && { reasoning }), - } - // Add Anthropic beta header for fine-grained tool streaming when using Anthropic models - // and forward the caller's abort signal / per-request timeout to the SDK. The merged - // signal (established before model lookup, above) aborts when either the caller's signal - // or the timeout fires, so timeouts are normalized to AbortError in the catch below. - // The client-level timeout remains the default safety net; timeoutMs <= 0 disables the - // per-request timeout, and 0 is never passed to the SDK. - // The SDK validates a per-request timeout as a uint32: non-finite values - // must be dropped, fractional delays rounded up (they fail its integer - // validation) and delays above 2^31-1 ms capped (Node clamps them to 1 ms - // otherwise). Same policy as mergeAbortSignalAndTimeout, which owns the - // signal side of the same option. - const perRequestTimeoutMs = - typeof options?.timeoutMs === "number" && Number.isFinite(options.timeoutMs) && options.timeoutMs > 0 - ? toSdkTimeoutMs(options.timeoutMs) - : undefined - - const requestOptions: OpenAI.RequestOptions = { - ...(modelId.startsWith("anthropic/") - ? { headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } } - : undefined), - ...(requestAbortSignal && { signal: requestAbortSignal }), - ...(perRequestTimeoutMs !== undefined && { timeout: perRequestTimeoutMs }), - } + let model: Awaited> + try { + model = requestAbortSignal + ? await rejectOnAbort(this.fetchModel(), requestAbortSignal, providerIdentifiers.openrouter) + : await this.fetchModel() + } catch (error) { + // Only our own merged signal marks this as a cancellation. An + // abort-shaped failure from the model cache's internal timeout is a + // discovery failure, not a caller cancellation, so it propagates with + // its original identity. + if (requestAbortSignal?.aborted) { + throw createAbortError(providerIdentifiers.openrouter) + } + throw error + } + const { id: modelId, maxTokens, temperature, reasoning } = model + + const completionParams: OpenRouterChatCompletionParams = { + model: modelId, + max_tokens: maxTokens, + temperature, + messages: [{ role: "user", content: prompt }], + stream: false, + // Only include provider if openRouterSpecificProvider is not "[default]". + ...(this.options.openRouterSpecificProvider && + this.options.openRouterSpecificProvider !== OPENROUTER_DEFAULT_PROVIDER_NAME && { + provider: { + order: [this.options.openRouterSpecificProvider], + only: [this.options.openRouterSpecificProvider], + allow_fallbacks: false, + }, + }), + ...(reasoning && { reasoning }), + } - let response + // Add Anthropic beta header for fine-grained tool streaming when using Anthropic models + // and forward the caller's abort signal / per-request timeout to the SDK. The merged + // signal (established before model lookup, above) aborts when either the caller's signal + // or the timeout fires, so timeouts are normalized to AbortError in the catch below. + // The client-level timeout remains the default safety net; timeoutMs <= 0 disables the + // per-request timeout, and 0 is never passed to the SDK. + // The SDK validates a per-request timeout as a uint32: non-finite values + // must be dropped, fractional delays rounded up (they fail its integer + // validation) and delays above 2^31-1 ms capped (Node clamps them to 1 ms + // otherwise). Same policy as mergeAbortSignalAndTimeout, which owns the + // signal side of the same option. + const perRequestTimeoutMs = + typeof options?.timeoutMs === "number" && Number.isFinite(options.timeoutMs) && options.timeoutMs > 0 + ? toSdkTimeoutMs(options.timeoutMs) + : undefined - try { - // The same retry-backoff window as createMessage: cut the wait so a cancellation that lands - // during the SDK's sleep settles as AbortError at once instead of after the backoff ends. - response = await settleOnAbort( - this.client.chat.completions.create(completionParams, requestOptions), - requestAbortSignal, - providerIdentifiers.openrouter, - ) - } catch (error) { - // Aborted requests are user-initiated: surface them as AbortError (this also covers - // timeouts, which abort the same signal) instead of a completion error. SDK-reported - // abort shapes are normalized as well (see the createMessage catch). - if (isRequestAborted(error, requestAbortSignal)) { - throw createAbortError(providerIdentifiers.openrouter) + const requestOptions: OpenAI.RequestOptions = { + ...(modelId.startsWith("anthropic/") + ? { headers: { "x-anthropic-beta": "fine-grained-tool-streaming-2025-05-14" } } + : undefined), + ...(requestAbortSignal && { signal: requestAbortSignal }), + ...(perRequestTimeoutMs !== undefined && { timeout: perRequestTimeoutMs }), } - // Try to parse as OpenRouter error structure using Zod - const parseResult = OpenRouterErrorResponseSchema.safeParse(error) - - if (parseResult.success && parseResult.data.error) { - const openRouterError = parseResult.data - const rawString = openRouterError.error?.metadata?.raw - const parsedError = extractErrorFromMetadataRaw(rawString) - const rawErrorMessage = parsedError || openRouterError.error?.message || "Unknown error" - - const apiError = Object.assign( - new ApiProviderError( - rawErrorMessage, + + let response + + try { + // The same retry-backoff window as createMessage: cut the wait so a cancellation that lands + // during the SDK's sleep settles as AbortError at once instead of after the backoff ends. + response = await settleOnAbort( + this.client.chat.completions.create(completionParams, requestOptions), + requestAbortSignal, + providerIdentifiers.openrouter, + ) + } catch (error) { + // Aborted requests are user-initiated: surface them as AbortError (this also covers + // timeouts, which abort the same signal) instead of a completion error. SDK-reported + // abort shapes are normalized as well (see the createMessage catch). + if (isRequestAborted(error, requestAbortSignal)) { + throw createAbortError(providerIdentifiers.openrouter) + } + // Try to parse as OpenRouter error structure using Zod + const parseResult = OpenRouterErrorResponseSchema.safeParse(error) + + if (parseResult.success && parseResult.data.error) { + const openRouterError = parseResult.data + const rawString = openRouterError.error?.metadata?.raw + const parsedError = extractErrorFromMetadataRaw(rawString) + const rawErrorMessage = parsedError || openRouterError.error?.message || "Unknown error" + + const apiError = Object.assign( + new ApiProviderError( + rawErrorMessage, + providerIdentifiers.openrouter, + modelId, + "completePrompt", + openRouterError.error?.code, + ), + { + status: openRouterError.error?.code, + error: openRouterError.error, + }, + ) + + TelemetryService.instance.captureException(apiError) + throw handleOpenAIError(error, providerIdentifiers.openrouter) + } else { + // Fallback for non-OpenRouter errors + const errorMessage = error instanceof Error ? error.message : String(error) + const apiError = new ApiProviderError( + errorMessage, providerIdentifiers.openrouter, modelId, "completePrompt", - openRouterError.error?.code, - ), - { - status: openRouterError.error?.code, - error: openRouterError.error, - }, - ) + ) + TelemetryService.instance.captureException(apiError) + throw handleOpenAIError(error, providerIdentifiers.openrouter) + } + } - TelemetryService.instance.captureException(apiError) - throw handleOpenAIError(error, providerIdentifiers.openrouter) - } else { - // Fallback for non-OpenRouter errors - const errorMessage = error instanceof Error ? error.message : String(error) - const apiError = new ApiProviderError( - errorMessage, - providerIdentifiers.openrouter, - modelId, - "completePrompt", - ) - TelemetryService.instance.captureException(apiError) - throw handleOpenAIError(error, providerIdentifiers.openrouter) + if (requestAbortSignal?.aborted) { + // The response resolved after the request was aborted: do not return the late result. + throw createAbortError(providerIdentifiers.openrouter) } - } - if (requestAbortSignal?.aborted) { - // The response resolved after the request was aborted: do not return the late result. - throw createAbortError(providerIdentifiers.openrouter) - } + if ("error" in response) { + this.handleStreamingError(response.error as OpenRouterError, modelId, "completePrompt") + } - if ("error" in response) { - this.handleStreamingError(response.error as OpenRouterError, modelId, "completePrompt") + const completion = response as OpenAI.Chat.ChatCompletion + return completion.choices[0]?.message?.content || "" + } finally { + // Detach on every exit path - completion, failure, abort, or a late result. The listener + // sits on the caller's long-lived signal and would outlive this request otherwise. + removeExternalAbortListener() } - - const completion = response as OpenAI.Chat.ChatCompletion - return completion.choices[0]?.message?.content || "" } /** From 9f2848177bfb24f190d887f1c7100bd08b361694 Mon Sep 17 00:00:00 2001 From: "user.email" <136036952+easonLiangWorldedtech@users.noreply.github.com> Date: Tue, 24 Nov 2026 04:16:30 +0800 Subject: [PATCH 05/11] test(api): pin the caller-signal detach on the non-abort failure exits The existing detach tests only take the success path, so the `finally` that the bridge exists for was untested on the exits that need it. Three tests are added: createMessage failing at request creation, the SDK stream throwing mid-iteration, and completePrompt failing with a non-abort error. Each asserts that the listener removed from the caller's signal is the exact reference the bridge attached, under the `abort` type, and that no abort listener is left on the caller's signal. No behaviour change: the provider source and the abort-signal helper are untouched. --- .../providers/__tests__/openrouter.spec.ts | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/src/api/providers/__tests__/openrouter.spec.ts b/src/api/providers/__tests__/openrouter.spec.ts index b12eec84da4..9ba1cec3d2c 100644 --- a/src/api/providers/__tests__/openrouter.spec.ts +++ b/src/api/providers/__tests__/openrouter.spec.ts @@ -4058,6 +4058,136 @@ describe("OpenRouterHandler", () => { expect(listenersLeftOnCallerSignal, "caller signal kept a listener after the request").toBe(0) }) + it("detaches the caller-signal listener when completePrompt fails with a non-abort error", async () => { + // The half of the ask with teeth: a `finally` is justified by the error exits, so the detach is + // proven on a failing request as well as on the successful pair the test above measures. + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + const failure = new Error("completion failed without an abort") + const create = vitest.fn().mockImplementation((_params: unknown, options: { signal?: AbortSignal }) => { + options?.signal?.addEventListener("abort", () => {}) + return Promise.reject(failure) + }) + stubCompletionsCreate(handler, create) + + const addedListeners = vitest.spyOn(controller.signal, "addEventListener") + const removedListeners = vitest.spyOn(controller.signal, "removeEventListener") + let attachedTypes: string[] = [] + let attachedRefs: unknown[] = [] + let detachCalls: { type: string; listener: unknown }[] = [] + let listenersLeft = -1 + try { + // `completePrompt` wraps a non-abort completion failure but keeps the cause text, so + // the assertion matches the message the way the other failure tests in this file do. + await expect(handler.completePrompt("test prompt", { abortSignal: controller.signal })).rejects.toThrow( + "completion failed without an abort", + ) + // Read the record before restoring: `mockRestore` clears the calls these two lines depend on. + attachedTypes = addedListeners.mock.calls.map((call) => String(call[0])) + attachedRefs = addedListeners.mock.calls.map((call) => call[1]) + detachCalls = removedListeners.mock.calls.map((call) => ({ type: String(call[0]), listener: call[1] })) + listenersLeft = getEventListeners(controller.signal, "abort").length + } finally { + addedListeners.mockRestore() + removedListeners.mockRestore() + } + + expect(attachedTypes, "expected the bridge to attach to the caller signal once").toEqual(["abort"]) + // Identity, not merely one removal: the detached listener must be the exact reference the bridge + // added, under the same event type. Removing a different listener would leave this one attached. + expect(detachCalls.length, "expected exactly one detach call on the caller signal").toBe(1) + expect(detachCalls[0]?.type).toBe("abort") + expect(detachCalls[0]?.listener, "the removed listener must be the one the bridge added").toBe( + attachedRefs[0], + ) + expect(listenersLeft, "caller signal kept a listener after a failed request").toBe(0) + }) + + it("detaches the caller-signal listener when createMessage fails at request creation", async () => { + // Same claim on the streaming entry point, failing before any chunk is produced. + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + const failure = new Error("create failed without an abort") + const create = vitest.fn().mockImplementation((_params: unknown, options: { signal?: AbortSignal }) => { + options?.signal?.addEventListener("abort", () => {}) + return Promise.reject(failure) + }) + stubCompletionsCreate(handler, create) + const metadata = makeCreateMessageMetadata({ abortSignal: controller.signal }) + + const addedListeners = vitest.spyOn(controller.signal, "addEventListener") + const removedListeners = vitest.spyOn(controller.signal, "removeEventListener") + let attachedTypes: string[] = [] + let attachedRefs: unknown[] = [] + let detachCalls: { type: string; listener: unknown }[] = [] + let listenersLeft = -1 + try { + await expect( + collectStream(handler.createMessage("test", [{ role: "user" as const, content: "hi" }], metadata)), + ).rejects.toThrow("create failed without an abort") + attachedTypes = addedListeners.mock.calls.map((call) => String(call[0])) + attachedRefs = addedListeners.mock.calls.map((call) => call[1]) + detachCalls = removedListeners.mock.calls.map((call) => ({ type: String(call[0]), listener: call[1] })) + listenersLeft = getEventListeners(controller.signal, "abort").length + } finally { + addedListeners.mockRestore() + removedListeners.mockRestore() + } + + expect(attachedTypes, "expected the bridge to attach to the caller signal once").toEqual(["abort"]) + expect(detachCalls.length, "expected exactly one detach call on the caller signal").toBe(1) + expect(detachCalls[0]?.type).toBe("abort") + expect(detachCalls[0]?.listener, "the removed listener must be the one the bridge added").toBe( + attachedRefs[0], + ) + expect(listenersLeft, "caller signal kept a listener after a failed request").toBe(0) + }) + + it("detaches the caller-signal listener when the SDK stream throws mid-iteration", async () => { + // The exit the success-path tests cannot reach: the request resolves, a chunk is delivered, and + // the iterator then throws, so only the `finally` can detach the listener. + const handler = new OpenRouterHandler(mockOptions) + const controller = new AbortController() + const iteratorError = new Error("stream exploded mid-iteration") + const partialThenThrow = async function* () { + yield { id: "1", choices: [{ delta: { content: "partial" } }] } + throw iteratorError + } + const create = vitest.fn().mockImplementation((_params: unknown, options: { signal?: AbortSignal }) => { + options?.signal?.addEventListener("abort", () => {}) + return Promise.resolve(partialThenThrow()) + }) + stubCompletionsCreate(handler, create) + const metadata = makeCreateMessageMetadata({ abortSignal: controller.signal }) + + const addedListeners = vitest.spyOn(controller.signal, "addEventListener") + const removedListeners = vitest.spyOn(controller.signal, "removeEventListener") + let attachedTypes: string[] = [] + let attachedRefs: unknown[] = [] + let detachCalls: { type: string; listener: unknown }[] = [] + let listenersLeft = -1 + try { + await expect( + collectStream(handler.createMessage("test", [{ role: "user" as const, content: "hi" }], metadata)), + ).rejects.toBe(iteratorError) + attachedTypes = addedListeners.mock.calls.map((call) => String(call[0])) + attachedRefs = addedListeners.mock.calls.map((call) => call[1]) + detachCalls = removedListeners.mock.calls.map((call) => ({ type: String(call[0]), listener: call[1] })) + listenersLeft = getEventListeners(controller.signal, "abort").length + } finally { + addedListeners.mockRestore() + removedListeners.mockRestore() + } + + expect(attachedTypes, "expected the bridge to attach to the caller signal once").toEqual(["abort"]) + expect(detachCalls.length, "expected exactly one detach call on the caller signal").toBe(1) + expect(detachCalls[0]?.type).toBe("abort") + expect(detachCalls[0]?.listener, "the removed listener must be the one the bridge added").toBe( + attachedRefs[0], + ) + expect(listenersLeft, "caller signal kept a listener after a failed stream").toBe(0) + }) + it("propagates a caller abort to the signal the SDK receives", async () => { // The ordering half of the bridge: the caller's cancellation must still reach the SDK // through the per-request signal, and it must do so while the request is in flight. From 6ca3c9eac242e617e3cc47f6c58d7340ce0ecaa9 Mon Sep 17 00:00:00 2001 From: Eason Liang <136036952+easonLiangWorldedtech@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:38:32 +0800 Subject: [PATCH 06/11] test(openrouter): state BYOK attribution in the usage-retention fixture The merge brought upstream's billing rule for OpenRouter usage: the per-request inference cost joins the reported total only when the usage reports is_byok. This lane's usage-retention test carried a fixture from before that rule (cost 2 plus upstream_inference_cost 3, expected 5) and no is_byok field, so after the merge it observed 2 and failed while still proving the thing it was written to prove - that a later chunk without usage does not clobber the last usage chunk. The rule is upstream's deliberate change (it ships with its own usage spec whose fixtures all name is_byok), so the test is what updates, not the code: the fixture now sets is_byok: true and keeps the total at 5. Run in a worktree with a real pnpm install (wt-real-1, tree checked out from this lane's merge commit): api/providers/__tests__/openrouter.spec.ts. --- src/api/providers/__tests__/openrouter.spec.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/api/providers/__tests__/openrouter.spec.ts b/src/api/providers/__tests__/openrouter.spec.ts index 255a3e411f9..1568a30e085 100644 --- a/src/api/providers/__tests__/openrouter.spec.ts +++ b/src/api/providers/__tests__/openrouter.spec.ts @@ -3388,6 +3388,9 @@ describe("OpenRouterHandler", () => { usage: { prompt_tokens: 10, completion_tokens: 5, + // BYOK attribution is what makes the upstream inference cost join the total; + // without it the rule adds nothing, so the fixture has to state it. + is_byok: true, cost: 2, cost_details: { upstream_inference_cost: 3 }, }, From 2a402d4bbf86814afecd2b6c991405bf7143f3da Mon Sep 17 00:00:00 2001 From: Eason Liang <136036952+easonLiangWorldedtech@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:45:24 +0800 Subject: [PATCH 07/11] fix(openrouter): stop sending stream_options now that upstream pins its absence The merge kept this side's request-parameter block, which adds stream_options: { include_usage: true }. Upstream's new usage spec asserts the opposite for this provider: the request body must not carry stream_options (api/providers/__tests__/openrouter-usage.spec.ts:62), and upstream's own code does not send it. That is a behavioural conflict git resolved textually without noticing. Measurement before choosing: the field appears exactly once in openrouter.ts (this side's line), and across both openrouter specs the only mention is upstream's assertion of absence - no test of this lane asserts its presence. Dropping it costs this side nothing, and usage still arrives: upstream's spec exercises usage through the real SDK without the flag. So this side yields to the stated contract. Verified in a worktree with a real pnpm install (wt-real-1, tree checked out from this lane): openrouter.spec.ts 145 passed (145), openrouter-usage.spec.ts 10 passed (10). --- src/api/providers/openrouter.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/api/providers/openrouter.ts b/src/api/providers/openrouter.ts index 315c7cd7056..3dbc23d4d5a 100644 --- a/src/api/providers/openrouter.ts +++ b/src/api/providers/openrouter.ts @@ -375,7 +375,6 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH top_p: topP, messages: openAiMessages, stream: true, - stream_options: { include_usage: true }, // Only include provider if openRouterSpecificProvider is not "[default]". ...(this.options.openRouterSpecificProvider && this.options.openRouterSpecificProvider !== OPENROUTER_DEFAULT_PROVIDER_NAME && { From 115b23f0cba1d271e902d1231267890454a7e23b Mon Sep 17 00:00:00 2001 From: Eason Liang <136036952+easonLiangWorldedtech@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:54:32 +0800 Subject: [PATCH 08/11] docs(tla): model the stream_options contract that 2a402d4b rests on OpenRouterStreamOptions.tla accompanies the line deletion in 2a402d4bbf: the body field set is the only thing the deletion changes, upstream pins the field's absence for this provider, and the claim that usage still arrives without it is carried by a declared CONSTANT assumption rather than derived, so the UsageNeedsFlag variant exists to show what would fail if that assumption were wrong. Gate result (WSL openjdk 21.0.13.1, tlc2.TLC from /mnt/c/tools/tla2tools.jar): Spec.cfg passed with 3 distinct states; SendsStreamOptions failed with exactly NoStreamOptionsInBody (2 states); UsageNeedsFlag failed with exactly UsageReportedAtEnd (3 states); BodyComplete failed in no configuration. Wall clock 3.051340789 s over 3 java invocations. check.sh is absent at this base, so the runner used the same java line and the same two decision strings and pinned the expected invariant per variant. Recorded outcome: null with cause. The only failures reachable from the model are the injected sabotage and the one carried by the declared assumption, so no new invariant is warranted; the two invariants restate assertions that already exist executably (openrouter-usage.spec.ts:62 and the same spec's usage cases). --- ...RouterStreamOptions.SendsStreamOptions.cfg | 13 ++ ...OpenRouterStreamOptions.UsageNeedsFlag.cfg | 13 ++ .../tla/OpenRouterStreamOptions.cfg | 14 +++ .../tla/OpenRouterStreamOptions.tla | 118 ++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 docs/architecture/tla/OpenRouterStreamOptions.SendsStreamOptions.cfg create mode 100644 docs/architecture/tla/OpenRouterStreamOptions.UsageNeedsFlag.cfg create mode 100644 docs/architecture/tla/OpenRouterStreamOptions.cfg create mode 100644 docs/architecture/tla/OpenRouterStreamOptions.tla diff --git a/docs/architecture/tla/OpenRouterStreamOptions.SendsStreamOptions.cfg b/docs/architecture/tla/OpenRouterStreamOptions.SendsStreamOptions.cfg new file mode 100644 index 00000000000..a950396d5ae --- /dev/null +++ b/docs/architecture/tla/OpenRouterStreamOptions.SendsStreamOptions.cfg @@ -0,0 +1,13 @@ +\* OpenRouterStreamOptions.SendsStreamOptions.cfg +CONSTANT Variant = "SendsStreamOptions" +CONSTANT UsageArrivesWithoutFlag = TRUE + +SPECIFICATION Spec + +INVARIANTS BodyComplete +INVARIANTS NoStreamOptionsInBody + + + + +CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpenRouterStreamOptions.UsageNeedsFlag.cfg b/docs/architecture/tla/OpenRouterStreamOptions.UsageNeedsFlag.cfg new file mode 100644 index 00000000000..6e8dc3dd6b1 --- /dev/null +++ b/docs/architecture/tla/OpenRouterStreamOptions.UsageNeedsFlag.cfg @@ -0,0 +1,13 @@ +\* OpenRouterStreamOptions.UsageNeedsFlag.cfg +CONSTANT Variant = "Fixed" +CONSTANT UsageArrivesWithoutFlag = FALSE + +SPECIFICATION Spec + +INVARIANTS BodyComplete + +INVARIANTS UsageReportedAtEnd + + + +CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpenRouterStreamOptions.cfg b/docs/architecture/tla/OpenRouterStreamOptions.cfg new file mode 100644 index 00000000000..07fa386770d --- /dev/null +++ b/docs/architecture/tla/OpenRouterStreamOptions.cfg @@ -0,0 +1,14 @@ +\* OpenRouterStreamOptions.cfg +CONSTANT Variant = "Fixed" +CONSTANT UsageArrivesWithoutFlag = TRUE + +SPECIFICATION Spec + +INVARIANTS BodyComplete + + +INVARIANTS NoStreamOptionsInBody +INVARIANTS UsageReportedAtEnd + + +CHECK_DEADLOCK FALSE diff --git a/docs/architecture/tla/OpenRouterStreamOptions.tla b/docs/architecture/tla/OpenRouterStreamOptions.tla new file mode 100644 index 00000000000..244f413d07a --- /dev/null +++ b/docs/architecture/tla/OpenRouterStreamOptions.tla @@ -0,0 +1,118 @@ +--- +Subsystem: OpenRouter provider request construction (src/api/providers/openrouter.ts). +Source: the chat-completion call path that builds completionParams and posts it. +Sinks: the outbound request body, and the usage chunk the stream reports back. + +Values modelled: the body as the SET OF FIELD NAMES it carries ("model", "stream", +"stream_options", "provider"); the stage of the call (Building, Sending, Done); +whether a usage chunk was reported. + +Failure this models (lane commit 2a402d4bbf86814afecd2b6c991405bf7143f3da): this side's +request-parameter block carried stream_options: { include_usage: true }. Upstream pins +the opposite for this provider - api/providers/__tests__/openrouter-usage.spec.ts:62 +asserts the request body has no stream_options property - and its own code does not +send the field. The merge kept this side's block, so the disagreement was textual-only +and survived into the merged tree. + +Invariant BodyComplete is the opposite of that failure: the deletion must not take a +required field with it, so every sent body still carries model and stream. +Invariant NoStreamOptionsInBody is the contract upstream's spec asserts. +Invariant UsageReportedAtEnd is the claim the deletion depends on - that usage still +arrives when the flag is absent. + +Provenance declaration: UsageArrivesWithoutFlag is a CONSTANT assumption, not a derived +fact. Its evidence is executable and outside this model (upstream's usage spec exercises +usage through the real SDK without the flag); the variant UsageNeedsFlag shows what +would break if that assumption were false, so the assumption is visible rather than +buried in the invariant. + +Variants: "Fixed" = the field is not added. "SendsStreamOptions" = sabotage, restores +this side's pre-merge behaviour. Assumption variant UsageNeedsFlag sets the constant to +FALSE with the field still absent. + +Result: see the FOUND block below, written after the run. + +Review: accompanies the hand-authored line deletion in 2a402d4bbf; the two fields the +lane added on the cost side are modelled by arithmetic attribution that upstream tests +directly, and the merge's ledger resolution is a count fact, not a behaviour. + +Cost (caliber declared before the run, read off the same run): wall clock seconds across +java invocations, one per config; agent-side writing time is excluded. + +Gate: Spec.cfg must pass and every ..cfg must fail (governance copy of +docs/architecture/tla/AGENTS.md, blob 783837dba555c74441b1fe91e4766cd69ed5914a, line 56). +check.sh is absent at this base, so the runner reproduces the same java line and the same +two decision strings, and additionally pins which invariant each variant must violate. + +EXPECTED, written here before TLC ran: "SendsStreamOptions" violates NoStreamOptionsInBody +and nothing else; "UsageNeedsFlag" (assumption set to FALSE, field still absent) violates +UsageReportedAtEnd; "Fixed" violates nothing; BodyComplete fails in no configuration, +because that is the check that the deletion took only the one field. + +FOUND, written after the run: appended at the foot of this file by the run that produced +the archived output, not from memory. +--- + +---- MODULE OpenRouterStreamOptions ---- +EXTENDS Naturals, TLC + +CONSTANT Variant +CONSTANT UsageArrivesWithoutFlag + +VARIABLES stage, body, usageReported + +vars == <> + +RequiredFields == {"model", "stream"} + +Init == + /\ stage = "Building" + /\ body = {} + /\ usageReported = FALSE + +BuildRequest == + /\ stage = "Building" + /\ stage' = "Sending" + /\ body' = IF (Variant = "SendsStreamOptions") + THEN (RequiredFields \cup {"stream_options"}) + ELSE RequiredFields + /\ usageReported' = usageReported + +Send == + /\ stage = "Sending" + /\ stage' = "Done" + /\ body' = body + /\ usageReported' = IF ("stream_options" \in body) + THEN TRUE + ELSE UsageArrivesWithoutFlag + +Nothing == UNCHANGED(vars) + +Next == BuildRequest \/ Send \/ Nothing + +Spec == Init /\ [][Next]_vars + +(* The deletion removed one field; it must not have removed a required one. *) +BodyComplete == stage = "Sending" => (RequiredFields \subseteq body) + +(* The contract upstream's spec pins for this provider. *) +NoStreamOptionsInBody == "stream_options" \notin body + +(* The claim the deletion rests on. *) +UsageReportedAtEnd == stage = "Done" => usageReported + +THEOREM Spec +===== + +(* FOUND, from the run that produced the archived output (not from memory). + Runner: WSL openjdk 21.0.13.1, tlc2.TLC from /mnt/c/tools/tla2tools.jar 2274532 B. + ok OpenRouterStreamOptions.cfg (pass, 3 distinct states, nothing violated) + ok OpenRouterStreamOptions.SendsStreamOptions.cfg (fail, 2 distinct states, + violated exactly NoStreamOptionsInBody) + ok OpenRouterStreamOptions.UsageNeedsFlag.cfg (fail, 3 distinct states, + violated exactly UsageReportedAtEnd) + BodyComplete failed in no configuration: the deletion removed one field, no required + one. Cost: wall clock 3.051340789 s over 3 java invocations, one per config. + Every EXPECTED item landed and nothing else did, so no behaviour exists in the guarded + model that the executable assertions do not already pin; the only reachable failures + are one injected sabotage and one carried by the declared CONSTANT assumption. *) From c0773f673d58fe8e13e5d253ce17d6606179e0c6 Mon Sep 17 00:00:00 2001 From: "user.email" <136036952+easonLiangWorldedtech@users.noreply.github.com> Date: Wed, 25 Nov 2026 22:21:41 +0800 Subject: [PATCH 09/11] chore(eslint): drop the poe.spec.ts suppression row the merge left stale The refresh merge took upstream's api/providers/__tests__/poe.spec.ts (46,229 B, byte identical to ca43e69f70) but kept my branch's ledger row for that file (@typescript-eslint/no-explicit-any, count 1). Upstream's ledger has no such row, so the merge invented a suppression that nothing in the merged tree uses - eslint 9 treats a leftover suppression as a failure, which is what the compile check reported: zoo-code:lint: There are suppressions left that do not occur anymore. Measured here, not inferred: plain 'eslint . --ext=ts --max-warnings=0' on the merged tree exits 2 with exactly that message; with only this entry removed it exits 0. Running the same command with --prune-suppressions removes exactly this one row and nothing else (261 files -> 260), which is the maintenance path AGENTS.md prescribes. Written by hand instead of letting eslint rewrite the file so the diff stays five lines instead of re-indenting all 261 entries: tabs preserved, no CR introduced, byte delta -106. --- src/eslint-suppressions.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/eslint-suppressions.json b/src/eslint-suppressions.json index 47e418d4348..6be65ef2d76 100644 --- a/src/eslint-suppressions.json +++ b/src/eslint-suppressions.json @@ -144,11 +144,6 @@ "count": 3 } }, - "api/providers/__tests__/poe.spec.ts": { - "@typescript-eslint/no-explicit-any": { - "count": 1 - } - }, "api/providers/__tests__/qwen-code-native-tools.spec.ts": { "@typescript-eslint/no-explicit-any": { "count": 2 From cf6e5c34271d55434c289d0f9559706f0d13b760 Mon Sep 17 00:00:00 2001 From: "user.email" <136036952+easonLiangWorldedtech@users.noreply.github.com> Date: Thu, 26 Nov 2026 00:05:35 +0800 Subject: [PATCH 10/11] test(openrouter): assert the timeout aborts the signal handed to the SDK The timeout-only test only asserted the rejection shape (name: "AbortError"). Its stub never captured options.signal, so if completePrompt() stopped passing a signal neither branch of the stub could resolve, the stub promise would simply never settle, and the local timeout race would still reject with AbortError - the test passed either way. Capture the signal the SDK receives and assert it is aborted after the rejection, so the test fails when the timeout no longer reaches the request. Addresses review thread PRRT_kwDOSK-bwM679NIe (src/api/providers/__tests__/openrouter.spec.ts:4665). No production-code change and no suppression-count change in this commit. --- src/api/providers/__tests__/openrouter.spec.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/api/providers/__tests__/openrouter.spec.ts b/src/api/providers/__tests__/openrouter.spec.ts index 1568a30e085..47f1e1a9d31 100644 --- a/src/api/providers/__tests__/openrouter.spec.ts +++ b/src/api/providers/__tests__/openrouter.spec.ts @@ -4637,9 +4637,11 @@ describe("OpenRouterHandler", () => { openRouterModelId: "openai/gpt-4o", }), ) + let requestSignal: AbortSignal | undefined const mockCreate = vitest .fn() .mockImplementation(async (_params: unknown, options?: { signal?: AbortSignal }) => { + requestSignal = options?.signal // Emulate the OpenAI SDK: the in-flight request rejects when the signal times out. await new Promise((resolve) => { if (options?.signal?.aborted) { @@ -4663,6 +4665,8 @@ describe("OpenRouterHandler", () => { await expect(handler.completePrompt("test prompt", { timeoutMs: 50 })).rejects.toMatchObject({ name: "AbortError", }) + + expect(requestSignal?.aborted).toBe(true) }) it("rejects with AbortError when both an abort signal and a timeout are provided", async () => { From 98a912b576540ad36c09e5564437c4259543c226 Mon Sep 17 00:00:00 2001 From: "user.email" <136036952+easonLiangWorldedtech@users.noreply.github.com> Date: Thu, 26 Nov 2026 02:38:35 +0800 Subject: [PATCH 11/11] style(openrouter): indent the captured-signal assignment inside the mock body GitHub CI runs the compile job (which includes prettier --check .) on the auto merge-preview frame pull/1545/merge; reproducing that frame locally (main tip ca43e69f70 merged with cf6e5c3427, --no-ff --no-commit) reported this file only, and prettier 3.9.10 (the version CI installs, per the log install lines) asked for exactly one line: the assignment opened the mock body at four tabs instead of five. No behaviour change: indentation only, one line, inside lines this unit added. --- src/api/providers/__tests__/openrouter.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/providers/__tests__/openrouter.spec.ts b/src/api/providers/__tests__/openrouter.spec.ts index 47f1e1a9d31..4443a032da1 100644 --- a/src/api/providers/__tests__/openrouter.spec.ts +++ b/src/api/providers/__tests__/openrouter.spec.ts @@ -4641,7 +4641,7 @@ describe("OpenRouterHandler", () => { const mockCreate = vitest .fn() .mockImplementation(async (_params: unknown, options?: { signal?: AbortSignal }) => { - requestSignal = options?.signal + requestSignal = options?.signal // Emulate the OpenAI SDK: the in-flight request rejects when the signal times out. await new Promise((resolve) => { if (options?.signal?.aborted) {