diff --git a/src/api/providers/__tests__/opencode-go.spec.ts b/src/api/providers/__tests__/opencode-go.spec.ts index 6ee2df0f87..9931ea5ea8 100644 --- a/src/api/providers/__tests__/opencode-go.spec.ts +++ b/src/api/providers/__tests__/opencode-go.spec.ts @@ -9,8 +9,12 @@ vitest.mock("vscode", () => ({ }, })) -import { Anthropic } from "@anthropic-ai/sdk" -import OpenAI from "openai" +import { + Anthropic, + APIConnectionTimeoutError as AnthropicTimeoutError, + APIUserAbortError as AnthropicAbortError, +} from "@anthropic-ai/sdk" +import OpenAI, { APIConnectionTimeoutError, APIUserAbortError } from "openai" import { opencodeGoDefaultModelId, @@ -21,10 +25,20 @@ import { import { OpencodeGoHandler } from "../opencode-go" import { getModels } from "../fetchers/modelCache" +import { MAX_TIMEOUT_MS } from "../utils/abort-signal" import { ApiHandlerOptions } from "../../../shared/api" import { Package } from "../../../shared/package" import { asyncStreamFrom, collectStream } from "../../../test-utils/stream" import { clearAllMocks } from "../../../test-utils/reset" +import { makeCreateMessageMetadata } from "../../../test-utils/api" + +// The automocked openai module swaps the SDK's error classes for mock constructors, which are not +// Error instances, so only the actual module's class can exercise the name/message arms of the +// provider's abort classification. +async function sdkAbortError(): Promise { + const { APIUserAbortError } = await vi.importActual("openai") + return new APIUserAbortError() +} vitest.mock("openai") vitest.mock("delay", () => ({ @@ -73,15 +87,22 @@ const mockResponsesCreate = vitest.fn() } }) -vitest.mock("@anthropic-ai/sdk", () => ({ - Anthropic: vitest.fn(function () { - return { - messages: { - create: mockAnthropicCreate, - }, - } - }), -})) +// The real SDK error classes are re-exported alongside the mocked client so +// tests can emulate the SDK's abort/timeout rejections and the provider's +// instanceof checks resolve against the same class identity. +vitest.mock("@anthropic-ai/sdk", async () => { + const actual = await vi.importActual("@anthropic-ai/sdk") + return { + ...actual, + Anthropic: vitest.fn(function () { + return { + messages: { + create: mockAnthropicCreate, + }, + } + }), + } +}) describe("OpencodeGoHandler", () => { const mockOptions: ApiHandlerOptions = { @@ -206,16 +227,18 @@ describe("OpencodeGoHandler", () => { const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] await collectStream(handler.createMessage("sys", messages)) - const callArgs = mockCreate.mock.calls[0][0] as Record - expect(callArgs).toMatchObject({ - model: "glm-5.1", - stream: true, - stream_options: { include_usage: true }, - // glm-5.1 maxTokens (131_072) is clamped to 20% of its 204_800 - // context window => 40_960. - max_completion_tokens: 40_960, - temperature: expect.any(Number), - }) + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ + model: "glm-5.1", + stream: true, + stream_options: { include_usage: true }, + // glm-5.1 maxTokens (131_072) is clamped to 20% of its 204_800 + // context window => 40_960. + max_completion_tokens: 40_960, + temperature: expect.any(Number), + }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), + ) }) it("sends the stable conversation ID to chat completions", async () => { @@ -249,11 +272,13 @@ describe("OpencodeGoHandler", () => { await collectStream(handler.createMessage("sys", messages)) // glm-5.1 advertises supportsReasoningEffort with a default of "medium". - const callArgs = mockCreate.mock.calls[0][0] as Record - expect(callArgs).toMatchObject({ - model: "glm-5.1", - reasoning_effort: "medium", - }) + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ + model: "glm-5.1", + reasoning_effort: "medium", + }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), + ) }) it("omits reasoning_effort when the user disables reasoning", async () => { @@ -282,6 +307,87 @@ describe("OpencodeGoHandler", () => { expect(callArgs.messages.filter((m) => m.role === "user")).toHaveLength(1) }) + it("merges post-tool-result text into the tool message for preserveReasoning models", async () => { + // convertToR1Format with mergeToolResultText folds text that follows + // tool_results into the last tool message (interleaved-thinking + // support) instead of opening a new user message; assert the merged + // wire shape so the preserveReasoning conversion branch and its + // mergeToolResultText flag stay kill-tested. + const handler = new OpencodeGoHandler(mockOptions) + const messages: Anthropic.Messages.MessageParam[] = [ + { role: "user", content: "Hi" }, + { + role: "assistant", + content: [ + { type: "text", text: "let me check" }, + { type: "tool_use", id: "tu_1", name: "read_file", input: { path: "/tmp/x" } }, + ], + }, + { + role: "user", + content: [ + { type: "tool_result", tool_use_id: "tu_1", content: "file-contents" }, + { type: "text", text: "and here is more context" }, + ], + }, + ] + + await collectStream(handler.createMessage("sys", messages)) + + const callArgs = mockCreate.mock.calls[0][0] as { + messages: Array<{ role: string; content?: unknown }> + } + const bodyMessages = callArgs.messages + const toolIndex = bodyMessages.findIndex((m) => m.role === "tool") + expect(bodyMessages[toolIndex]?.content).toBe("file-contents\n\nand here is more context") + // No user message may follow the tool message: the trailing text + // merged into the tool message instead of opening a new user turn. + expect(bodyMessages.slice(toolIndex + 1).some((m) => m.role === "user")).toBe(false) + }) + + it("keeps post-tool-result text unmerged for non-preserveReasoning models", async () => { + // The plain OpenAI converter (non-preserveReasoning models) does not + // merge post-tool-result text into the tool message: the text stays + // a separate user message. Pin the plain shape so the always-R1 + // direction of the conversion branch stays kill-tested. + vitest.mocked(getModels).mockImplementationOnce(async () => ({ + "kimi-k2.6": { ...opencodeGoModels["kimi-k2.6"] }, + })) + + const handler = new OpencodeGoHandler({ ...mockOptions, opencodeGoModelId: "kimi-k2.6" }) + const messages: Anthropic.Messages.MessageParam[] = [ + { role: "user", content: "Hi" }, + { + role: "assistant", + content: [ + { type: "text", text: "let me check" }, + { type: "tool_use", id: "tu_1", name: "read_file", input: { path: "/tmp/x" } }, + ], + }, + { + role: "user", + content: [ + { type: "tool_result", tool_use_id: "tu_1", content: "file-contents" }, + { type: "text", text: "and here is more context" }, + ], + }, + ] + + await collectStream(handler.createMessage("sys", messages)) + + const callArgs = mockCreate.mock.calls[0][0] as { + messages: Array<{ role: string; content?: unknown }> + } + const bodyMessages = callArgs.messages + const toolIndex = bodyMessages.findIndex((m) => m.role === "tool") + // Unmerged: the tool message keeps only the tool result content. + expect(bodyMessages[toolIndex]?.content).toBe("file-contents") + // ...and the trailing text stays its own user message (array + // content, the plain converter's shape). + const trailingUser = bodyMessages.slice(toolIndex + 1).find((m) => m.role === "user") + expect(trailingUser?.content).toEqual([{ type: "text", text: "and here is more context" }]) + }) + it("streams reasoning chunks from delta.reasoning_content", async () => { mockCreate.mockImplementationOnce(async () => asyncStreamFrom([ @@ -475,170 +581,1014 @@ describe("OpencodeGoHandler", () => { await collectStream(handler.createMessage("sys", messages)) - const callArgs = mockCreate.mock.calls[0][0] as Record - expect(callArgs.max_completion_tokens).toBe(999) + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ max_completion_tokens: 999 }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), + ) }) - }) - describe("completePrompt", () => { - it("returns the message content for a non-streaming completion", async () => { - mockCreate.mockResolvedValue({ choices: [{ message: { content: "the answer" } }] }) - const handler = new OpencodeGoHandler(mockOptions) - expect(await handler.completePrompt("ping")).toBe("the answer") - const callArgs = mockCreate.mock.calls[0][0] as Record - expect(callArgs).toMatchObject({ - model: "glm-5.1", - stream: false, - // glm-5.1 maxTokens (131_072) clamped to 20% of 204_800 => 40_960. - max_completion_tokens: 40_960, - reasoning_effort: "medium", - }) - }) + it("sends the unclamped modelMaxTokens override when it exceeds the model ceiling", async () => { + // getModelMaxOutputTokens clamps the slider override to the model + // ceiling for supportsMaxTokens models (glm-5.1 reports + // supportsMaxTokens), but the OpenAI branch sends the raw override + // (modelMaxTokens || maxTokens): with a ceiling-bounded override the + // wire value is the unclamped override. Pin that so the + // includeMaxTokens branch stays kill-tested. + vitest.mocked(getModels).mockImplementationOnce(async () => ({ + "glm-5.1": { ...opencodeGoModels["glm-5.1"], maxTokens: 500 }, + })) - it("wraps errors with an Opencode Go-specific message", async () => { - mockCreate.mockRejectedValue(new Error("boom")) - const handler = new OpencodeGoHandler(mockOptions) - await expect(handler.completePrompt("ping")).rejects.toThrow("Opencode Go completion error: boom") + const handler = new OpencodeGoHandler({ ...mockOptions, includeMaxTokens: true, modelMaxTokens: 999 }) + await collectStream(handler.createMessage("sys", [{ role: "user", content: "Hi" }])) + + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ max_completion_tokens: 999 }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), + ) }) - it("rethrows non-Error values unchanged", async () => { - mockCreate.mockRejectedValue("not an error") - const handler = new OpencodeGoHandler(mockOptions) - await expect(handler.completePrompt("ping")).rejects.toBe("not an error") + it("sends the clamped maxTokens when includeMaxTokens is off", async () => { + // With the flag off the body uses the resolved maxTokens, which the + // shared getModelMaxOutputTokens already clamps to the model ceiling + // for supportsMaxTokens models. + vitest.mocked(getModels).mockImplementationOnce(async () => ({ + "glm-5.1": { ...opencodeGoModels["glm-5.1"], maxTokens: 500 }, + })) + + const handler = new OpencodeGoHandler({ ...mockOptions, modelMaxTokens: 999 }) + await collectStream(handler.createMessage("sys", [{ role: "user", content: "Hi" }])) + + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ max_completion_tokens: 500 }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), + ) }) - it("returns an empty string when no content is returned", async () => { - mockCreate.mockResolvedValue({ choices: [] }) + it("defaults parallel_tool_calls to true and honors an explicit false", async () => { + // The OpenAI branch defaults parallel_tool_calls to true when the + // metadata omits it and forwards an explicit false unchanged; pin + // both so the nullish default stays kill-tested (the Responses + // branch has its own assertions). const handler = new OpencodeGoHandler(mockOptions) - expect(await handler.completePrompt("ping")).toBe("") - }) - it("honors includeMaxTokens/modelMaxTokens override for max_completion_tokens", async () => { - mockCreate.mockResolvedValue({ choices: [{ message: { content: "ok" } }] }) - const handler = new OpencodeGoHandler({ ...mockOptions, includeMaxTokens: true, modelMaxTokens: 4321 }) - await handler.completePrompt("ping") - const callArgs = mockCreate.mock.calls[0][0] as Record - expect(callArgs.max_completion_tokens).toBe(4321) + await collectStream(handler.createMessage("sys", [{ role: "user", content: "Hi" }])) + expect(mockCreate.mock.calls[0][0]).toEqual(expect.objectContaining({ parallel_tool_calls: true })) + + await collectStream( + handler.createMessage( + "sys", + [{ role: "user", content: "Hi" }], + makeCreateMessageMetadata({ parallelToolCalls: false }), + ), + ) + expect(mockCreate.mock.calls[1][0]).toEqual(expect.objectContaining({ parallel_tool_calls: false })) }) - it("sends the per-instance session ID on non-streaming completions", async () => { - mockCreate.mockResolvedValue({ choices: [{ message: { content: "ok" } }] }) + it("rethrows non-abort errors from the OpenAI stream unchanged", async () => { + // A mid-stream failure that is not an abort (e.g. a connection + // reset) must propagate unchanged — the catch only normalizes + // aborts to a DOM-standard AbortError. + const streamError = new Error("connection reset") + mockCreate.mockImplementation(async () => + (async function* () { + yield { choices: [{ delta: { content: "partial" }, index: 0 }], index: 0 } + throw streamError + })(), + ) + const handler = new OpencodeGoHandler(mockOptions) - await handler.completePrompt("ping") - const sessionId = mockCreate.mock.calls[0][1]?.headers["x-opencode-session"] - expect(sessionId).toEqual(expect.any(String)) + const error = await collectStream(handler.createMessage("sys", [{ role: "user", content: "hi" }])).then( + () => undefined, + (e: unknown) => e, + ) - // completePrompt has no taskId metadata, so the per-instance ID is - // reused across calls on the same handler. - await handler.completePrompt("ping") - expect(mockCreate.mock.calls[1][1]?.headers["x-opencode-session"]).toBe(sessionId) + expect(error).toBe(streamError) }) - }) - - describe("Anthropic-format models (qwen3.7-max)", () => { - // qwen3.7-max is only reachable via the Anthropic Messages endpoint - // (/v1/messages); sending it to /v1/chat/completions is what produces - // "401 Model qwen3.7-max is not supported for format oa-compat". - const anthropicOptions: ApiHandlerOptions = { - opencodeGoApiKey: "test-key", - opencodeGoModelId: "qwen3.7-max", - } - beforeEach(() => { - mockAnthropicCreate.mockImplementation(async () => + it("skips empty choices, empty deltas and tool calls without a function field", async () => { + // Full-list assertions: a frame without choices[0], a delta without + // content/tool_calls, and a tool call missing `function` must each + // contribute nothing except the partial tool call with undefined + // name/arguments (toEqual ignores undefined-valued keys). + mockCreate.mockImplementationOnce(async () => asyncStreamFrom([ + { choices: [], index: 0 }, + { choices: [{ delta: {}, index: 0 }], index: 0 }, { - type: "message_start", - message: { - usage: { - input_tokens: 10, - output_tokens: 0, - cache_creation_input_tokens: 2, - cache_read_input_tokens: 3, - }, - }, - }, - { - type: "content_block_start", + choices: [{ delta: { tool_calls: [{ index: 0, id: "call_1" }] }, index: 0 }], index: 0, - content_block: { type: "text", text: "" }, }, - { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "Hello" } }, { - type: "content_block_start", - index: 1, - content_block: { type: "tool_use", id: "toolu_1", name: "read_file", input: {} }, + choices: [{ delta: {}, index: 0 }], + index: 0, + usage: { prompt_tokens: 1, completion_tokens: 2 }, }, + ]), + ) + + const handler = new OpencodeGoHandler(mockOptions) + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "hi" }] + + const chunks = await collectStream(handler.createMessage("sys", messages)) + + expect(chunks).toEqual([ + { type: "tool_call_partial", index: 0, id: "call_1" }, + { type: "usage", inputTokens: 1, outputTokens: 2 }, + ]) + }) + + it("emits the text delta and the usage chunk with cached tokens from the final frame", async () => { + mockCreate.mockImplementationOnce(async () => + asyncStreamFrom([ + { choices: [{ delta: { content: "hi" }, index: 0 }], index: 0 }, { - type: "content_block_delta", - index: 1, - delta: { type: "input_json_delta", partial_json: '{"path":' }, + choices: [{ delta: {}, index: 0 }], + index: 0, + usage: { + prompt_tokens: 4, + completion_tokens: 9, + prompt_tokens_details: { cached_tokens: 6 }, + }, }, - { type: "content_block_stop", index: 1 }, - { type: "message_delta", usage: { output_tokens: 5 } }, - { type: "message_stop" }, ]), ) - }) - it("routes the request through the Anthropic /v1/messages client, not chat completions", async () => { - const handler = new OpencodeGoHandler(anthropicOptions) - const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + const handler = new OpencodeGoHandler(mockOptions) + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "hi" }] - await collectStream(handler.createMessage("sys", messages)) + const chunks = await collectStream(handler.createMessage("sys", messages)) - const callArgs = mockAnthropicCreate.mock.calls[0][0] as Record - expect(callArgs).toMatchObject({ - model: "qwen3.7-max", - stream: true, - system: expect.arrayContaining([expect.objectContaining({ type: "text", text: "sys" })]), + expect(chunks).toEqual([ + { type: "text", text: "hi" }, + { type: "usage", inputTokens: 4, outputTokens: 9, cacheReadTokens: 6 }, + ]) + }) + }) + + describe("createMessage abort signal bridging", () => { + it("rejects with an AbortError when the external signal is already aborted", async () => { + // A pre-aborted request must fail fast before any model-catalog or + // SDK work starts: the standardized AbortError wins over any + // resolution failure, and the request itself never begins. + let capturedSignal: AbortSignal | undefined + mockCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => { + capturedSignal = options?.signal + throw new DOMException("The operation was aborted.", "AbortError") }) - // The OpenAI chat completions endpoint must NOT be used for this model. + + const handler = new OpencodeGoHandler(mockOptions) + const controller = new AbortController() + controller.abort() + + const stream = handler.createMessage( + "sys", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + + const error = await collectStream(stream).then( + () => undefined, + (e: unknown) => e, + ) + // The fast-fail guard throws before the request starts. expect(mockCreate).not.toHaveBeenCalled() + // Pre-flight cancellation must skip the model catalog entirely: + // the getModels mock must remain uncalled, not just the SDK create. + expect(vitest.mocked(getModels)).not.toHaveBeenCalled() + expect(capturedSignal).toBeUndefined() + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") }) - it("sends the stable conversation ID to the Anthropic messages endpoint", async () => { - const handler = new OpencodeGoHandler(anthropicOptions) - const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + it("rejects with the standardized AbortError when the external signal aborts while model resolution is pending", async () => { + // The model catalog stays pending while the external signal aborts: + // the resolution race must settle with the standardized AbortError + // before the lookup is released, and the request itself must never + // start. A bridge-only fix would let the lookup finish and abort the + // internal controller after the fact; this gate proves the prompt + // settles on the abort itself. + let releaseResolution!: () => void + const resolutionGate = new Promise((resolve) => { + releaseResolution = resolve + }) + // The exact promise the provider awaits, so the test can await the lookup's own + // settlement instead of guessing how long a parked lookup takes. + let lookupPromise: Promise> | undefined + vitest.mocked(getModels).mockImplementationOnce(() => { + const parked = (async () => { + await resolutionGate + return { "glm-5.1": { ...opencodeGoModels["glm-5.1"] } } + })() + lookupPromise = parked + return parked + }) - await collectStream(handler.createMessage("sys", messages, { taskId: "conversation-123" })) + const handler = new OpencodeGoHandler(mockOptions) + const controller = new AbortController() - expect(mockAnthropicCreate.mock.calls[0][1]?.headers).toMatchObject({ - "x-opencode-session": "conversation-123", + const consumed = collectStream( + handler.createMessage( + "sys", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ) + + // Observable condition: the catalog lookup has been invoked and is parked on the + // gate, which is what the 10 ms sleep was standing in for. + await vitest.waitFor(() => expect(vitest.mocked(getModels)).toHaveBeenCalledTimes(1), { + timeout: 1000, + interval: 5, }) + controller.abort() + + const error = await consumed.then( + () => undefined, + (e: unknown) => e, + ) + try { + // The catalog lookup was attempted but the prompt must have settled + // on the abort itself, before the lookup was released: no SDK call. + expect(vitest.mocked(getModels)).toHaveBeenCalledTimes(1) + // Deliberately not asserted here: until the finally below has awaited the provider's own + // lookup promise, a request that starts late from a drained continuation could still be + // missed. The claim is made once, after that drain. + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + } finally { + // Release the parked lookup even when an assertion above threw: leaving the gate shut would + // strand the very promise the provider awaited, which is the resource this test owns. + releaseResolution() + // Observable condition: the parked lookup has actually settled. Awaiting the same + // promise the provider awaits drains the provider's continuation first, because the + // provider subscribed to it before this test did; only then is "no late request" a + // statement about drained state rather than about a 10 ms window. + await lookupPromise + } + expect(mockCreate).not.toHaveBeenCalled() }) - it("falls back to a stable per-instance session ID when no taskId is provided", async () => { - const handler = new OpencodeGoHandler(anthropicOptions) - const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + it("aborts the request when the caller's signal fires after model resolution but before the bridge", async () => { + // resolveModelWithAbort removes its listener when the lookup settles. An abort + // landing in the window before createMessage installs its bridge is caught by + // the already-aborted guard; without that guard the listener would be attached + // after the event, the internal controller would stay live, and the request + // could start without cancellation. + const controller = new AbortController() + const handler = new OpencodeGoHandler(mockOptions) + // The resolution value's id getter fires the abort while createMessage reads + // the resolved fields - exactly the post-resolution window. + const resolved = { + get id() { + controller.abort() + return "glm-5.1" + }, + info: { ...opencodeGoModels["glm-5.1"] }, + format: "openai" as const, + temperature: undefined, + reasoningEffort: undefined, + maxTokens: undefined, + } + vi.spyOn(handler as unknown as { resolveModel: () => Promise }, "resolveModel").mockResolvedValue( + resolved, + ) + mockCreate.mockImplementation(async (_params: unknown, options?: { signal?: AbortSignal }) => { + // The guard must have aborted the internal controller before the request. + expect(options?.signal?.aborted).toBe(true) + throw new APIUserAbortError() + }) - await collectStream(handler.createMessage("sys", messages)) + const error = await collectStream( + handler.createMessage( + "sys", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ).then( + () => undefined, + (e: unknown) => e, + ) - const sessionId = mockAnthropicCreate.mock.calls[0][1]?.headers["x-opencode-session"] - expect(sessionId).toEqual(expect.any(String)) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }) - // The same handler instance reuses its session ID across requests. - await collectStream(handler.createMessage("sys", messages)) - expect(mockAnthropicCreate.mock.calls[1][1]?.headers["x-opencode-session"]).toBe(sessionId) + it("propagates a raw model-lookup failure unchanged when no abort signal is present", async () => { + // Without an abort signal the resolution must not be wrapped or + // normalized: a catalog failure keeps its own identity so callers + // can distinguish a lookup failure from a cancellation. + const lookupError = new Error("catalog offline") + vitest.mocked(getModels).mockRejectedValueOnce(lookupError) + + const handler = new OpencodeGoHandler(mockOptions) + + const error = await collectStream(handler.createMessage("sys", [{ role: "user", content: "hi" }])).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toBe(lookupError) + expect(mockCreate).not.toHaveBeenCalled() }) - it("streams text, tool-call, usage and cost chunks from the Anthropic stream", async () => { - const handler = new OpencodeGoHandler(anthropicOptions) - const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + it("normalizes an abort-flavored model-lookup failure to the provider AbortError", async () => { + // A lookup that fails with an AbortError-shaped error (e.g. the + // catalog fetch itself was aborted) must settle with the provider's + // standardized AbortError, not the raw error shape. + const abortFlavored = new Error("The operation was aborted.") + abortFlavored.name = "AbortError" + vitest.mocked(getModels).mockRejectedValueOnce(abortFlavored) - const chunks = await collectStream(handler.createMessage("sys", messages)) + const handler = new OpencodeGoHandler(mockOptions) - expect(chunks).toContainEqual({ type: "text", text: "Hello" }) - expect(chunks).toContainEqual({ - type: "tool_call_partial", - index: 1, - id: "toolu_1", - name: "read_file", - arguments: undefined, + const error = await collectStream(handler.createMessage("sys", [{ role: "user", content: "hi" }])).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).not.toBe(abortFlavored) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("aborts the in-flight request when the external signal fires mid-stream", async () => { + // The mock polls the INTERNAL controller signal instead of waiting + // for an "abort" event: bounded polling means the test can never + // hang if the bridge stops forwarding aborts, and it rejects as + // soon as the bridge aborts the controller. + let capturedSignal: AbortSignal | undefined + let firstChunkDelivered = false + mockCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => { + capturedSignal = options?.signal + return (async function* () { + yield { choices: [{ delta: { content: "partial" }, index: 0 }], index: 0 } + // Runs only once the consumer has taken the first chunk and asked for the next + // one, which is the state the 25 ms sleep was approximating. + firstChunkDelivered = true + // Observable condition: the bridge aborted the internal controller. Polling it + // means a bridge that stops forwarding aborts fails on a printed condition + // instead of quietly running the stream to its end. + await vitest.waitFor(() => expect(capturedSignal?.aborted).toBe(true), { + timeout: 1000, + interval: 5, + }) + throw new DOMException("The operation was aborted.", "AbortError") + })() }) - expect(chunks).toContainEqual({ - type: "tool_call_partial", + + const handler = new OpencodeGoHandler(mockOptions) + const controller = new AbortController() + + const consumed = collectStream( + handler.createMessage( + "sys", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ) + + // Observable condition: the first chunk has been consumed and the stream is asking for + // the next one; abort mid-stream from there rather than after a fixed 25 ms. + await vitest.waitFor(() => expect(firstChunkDelivered).toBe(true), { timeout: 1000, interval: 5 }) + controller.abort() + + const error = await consumed.then( + () => undefined, + (e: unknown) => e, + ) + expect(capturedSignal?.aborted).toBe(true) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }) + + it("surfaces a cancel when the SDK closes the chat stream without throwing", async () => { + // The OpenAI SDK swallows the body-read abort and can close the iterator normally instead + // of throwing: the text already yielded must not be reported as a completed turn, and the + // loop must not read the chunk it is handed after the cancel. + let lateChunkReads = 0 + const abortedReader = { + get choices() { + lateChunkReads += 1 + return [{ delta: { content: "late" } }] + }, + index: 0, + } + let requestedNextChunk = false + const controller = new AbortController() + mockCreate.mockImplementationOnce(async (_params: unknown, options: { signal?: AbortSignal }) => { + const requestSignal = options?.signal + return (async function* () { + yield { choices: [{ delta: { content: "partial" } }], index: 0 } + // Runs only once the provider has consumed the first chunk and asked for the next one. + requestedNextChunk = true + // Observable condition: the bridge aborted the request signal. + await vitest.waitFor(() => expect(requestSignal?.aborted).toBe(true), { + timeout: 1000, + interval: 5, + }) + // What the SDK hands back after swallowing the abort: a final chunk, no throw. + yield abortedReader + })() + }) + const handler = new OpencodeGoHandler(mockOptions) + const consumed = collectStream( + handler.createMessage( + "sys", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ) + + // Observable condition: the provider is parked on the next chunk; cancel from there. + await vitest.waitFor(() => expect(requestedNextChunk).toBe(true), { timeout: 1000, interval: 5 }) + controller.abort() + + const error = await consumed.then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + expect(lateChunkReads).toBe(0) // the loop left before reading the post-cancel chunk + }) + + it.each([ + [ + "Anthropic", + () => new OpencodeGoHandler({ ...mockOptions, opencodeGoModelId: "qwen3.7-max" }), + mockAnthropicCreate, + ], + [ + "Responses", + () => new OpencodeGoHandler({ ...mockOptions, opencodeGoModelId: "gpt-5.6-luna" }), + mockResponsesCreate, + ], + ["Chat Completions", () => new OpencodeGoHandler(mockOptions), mockCreate], + ])( + "settles the %s stream-open wait when the caller aborts during an SDK retry backoff", + async (_label, makeHandler, createMock) => { + // The pinned SDKs sleep out a Retry-After backoff inside the create call before they look at + // the signal, so awaiting that promise alone leaves both the pending first next() and the + // bridge cleanup waiting for the sleep to end; racing the open settles on the cancel instead. + let requestSignal: AbortSignal | undefined + createMock.mockImplementationOnce( + (_params: unknown, options?: { signal?: AbortSignal }) => + new Promise(() => { + requestSignal = options?.signal + }), + ) + const handler = makeHandler() as OpencodeGoHandler + const controller = new AbortController() + + const settled = collectStream( + handler.createMessage( + "sys", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ).then( + () => undefined, + (e: unknown) => e, + ) + + // Observable condition: the request really started, so the provider is parked on the open. The + // rejection handler is attached before the poll, so a rejection during readiness is observed. + await vitest.waitFor(() => expect(requestSignal).toBeInstanceOf(AbortSignal), { + timeout: 1000, + interval: 5, + }) + controller.abort() + + const error = await settled + expect(requestSignal?.aborted).toBe(true) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }, + ) + + it("re-checks the signal before the buffered text item of a reasoning chunk", async () => { + // One frame carries reasoning plus text. The consumer takes the reasoning item, cancels and + // asks for the next one, which resumes this generator inside the same iteration: only a guard + // in front of the buffered item can keep a cancelled turn's text from arriving, because the + // top-of-loop guard already ran for this chunk. + mockCreate.mockImplementationOnce(async () => + asyncStreamFrom([ + { + choices: [{ delta: { reasoning_content: "think", content: "answer" }, index: 0 }], + index: 0, + }, + ]), + ) + const handler = new OpencodeGoHandler(mockOptions) + const controller = new AbortController() + const addEventListenerSpy = vi.spyOn(controller.signal, "addEventListener") + const removeListenerSpy = vi.spyOn(controller.signal, "removeEventListener") + + const iterator = handler + .createMessage( + "sys", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "reasoning", text: "think" }) + // Observable condition: the consumer holds the reasoning item and cancels before pulling the + // next one. + controller.abort() + + const settled = await iterator.next().then( + (result) => result.value, + (error: unknown) => error, + ) + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Opencode Go request was aborted") + // The bridge is detached by reference on this path too: a cancelled turn must not leave a + // listener behind on a task-scoped signal. + const abortAdds = addEventListenerSpy.mock.calls.filter(([event]) => event === "abort") + const bridgeListener = abortAdds[1]?.[1] + expect(typeof bridgeListener).toBe("function") + const removed = removeListenerSpy.mock.calls + .filter(([event]) => event === "abort") + .map(([, listener]) => listener) + expect(removed).toContain(bridgeListener) + }) + + it("re-checks the signal between two tool-call items of one chunk", async () => { + // Two tool calls arrive in a single delta. A consumer that takes the first partial and cancels + // must not be handed the second one, so the guard belongs inside the per-item loop. + mockCreate.mockImplementationOnce(async () => + asyncStreamFrom([ + { + choices: [ + { + delta: { + tool_calls: [ + { index: 0, id: "call-1", function: { name: "read_file", arguments: "{}" } }, + { index: 1, id: "call-2", function: { name: "list_files", arguments: "{}" } }, + ], + }, + index: 0, + }, + ], + index: 0, + }, + ]), + ) + const handler = new OpencodeGoHandler(mockOptions) + const controller = new AbortController() + const iterator = handler + .createMessage( + "sys", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "tool_call_partial", id: "call-1" }) + controller.abort() + + const settled = await iterator.next().then( + (result) => result.value, + (error: unknown) => error, + ) + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Opencode Go request was aborted") + }) + + it("re-checks the signal before the usage item of a content and usage chunk", async () => { + // A cancel must not report usage for the turn it stopped. The usage item follows the text item + // of the same frame, so only the guard in front of it keeps a cancelled turn's usage out of + // the stream; without it the consumer is handed usage and then the abort. + mockCreate.mockImplementationOnce(async () => + asyncStreamFrom([ + { + choices: [{ delta: { content: "answer" }, index: 0 }], + index: 0, + usage: { prompt_tokens: 2, completion_tokens: 3 }, + }, + ]), + ) + const handler = new OpencodeGoHandler(mockOptions) + const controller = new AbortController() + const iterator = handler + .createMessage( + "sys", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "text", text: "answer" }) + controller.abort() + + const settled = await iterator.next().then( + (result) => result.value, + (error: unknown) => error, + ) + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Opencode Go request was aborted") + }) + + it("detaches the bridged abort listener when the request completes normally", async () => { + // The listener is added with { once: true }, so it only detaches on + // abort. A task-scoped signal spanning many requests must not + // accumulate a listener per request: assert explicit removal after a + // normal (non-aborted) completion. + mockCreate.mockImplementation(async () => + asyncStreamFrom([ + { + choices: [{ delta: { content: "ok" }, index: 0 }], + index: 0, + }, + { + choices: [{ delta: {}, index: 0 }], + index: 0, + usage: { prompt_tokens: 2, completion_tokens: 3 }, + }, + ]), + ) + + const handler = new OpencodeGoHandler(mockOptions) + const controller = new AbortController() + const removeListenerSpy = vi.spyOn(controller.signal, "removeEventListener") + const addEventListenerSpy = vi.spyOn(controller.signal, "addEventListener") + + const stream = handler.createMessage( + "sys", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + + const chunks = await collectStream(stream) + expect(chunks).toContainEqual({ type: "text", text: "ok" }) + // Assert the exact listener reference so a bridge that removes a + // different callback than the one it registered cannot pass. + // The model-resolution race registers its own "abort" listener on the same signal and detaches + // it before the request starts, so there are exactly two registrations here and the bridge's is + // the second one. Naming it by position — and requiring it to differ from the race's callback — + // means a bridge that stopped registering its own listener cannot satisfy the assertions below + // by falling back on the race's. + const abortAddCalls = addEventListenerSpy.mock.calls.filter(([event]) => event === "abort") + const abortRemoveCalls = removeListenerSpy.mock.calls + .filter(([event]) => event === "abort") + .map(([, listener]) => listener) + expect(abortAddCalls.length).toBe(2) + const [raceCall, bridgeCall] = abortAddCalls + const addedListener = bridgeCall?.[1] + expect(typeof addedListener).toBe("function") + expect(addedListener).not.toBe(raceCall?.[1]) + // The listener is registered with { once: true } — assert the exact + // options so a bridge that drops them (and relies on the finally + // block alone for single-shot semantics) is caught. + expect(addEventListenerSpy).toHaveBeenCalledWith("abort", addedListener, { once: true }) + // Both listeners are gone by reference: the lookup race's first (its cleanup ran before the + // request), the bridge's last. + expect(abortRemoveCalls).toEqual([raceCall?.[1], addedListener]) + expect(controller.signal.aborted).toBe(false) + }) + + it("detaches the bridged abort listener from the Anthropic-format path", async () => { + // The Anthropic branch (streamAnthropicMessage) has its own finally + // block that removes the bridged listener; assert explicit removal + // after a normal (non-aborted) completion on that path too. + mockAnthropicCreate.mockImplementationOnce(async () => + asyncStreamFrom([ + { type: "message_start", message: { usage: { input_tokens: 1, output_tokens: 0 } } }, + { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }, + { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } }, + { type: "message_delta", usage: { output_tokens: 1 } }, + { type: "message_stop" }, + ]), + ) + + const handler = new OpencodeGoHandler({ + opencodeGoApiKey: "test-key", + opencodeGoModelId: "qwen3.7-max", + }) + const controller = new AbortController() + const removeListenerSpy = vi.spyOn(controller.signal, "removeEventListener") + const addEventListenerSpy = vi.spyOn(controller.signal, "addEventListener") + + const stream = handler.createMessage( + "sys", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + + const chunks = await collectStream(stream) + expect(chunks).toContainEqual({ type: "text", text: "ok" }) + // The model-resolution race registers its own "abort" listener on the same signal and detaches + // it before the request starts, so there are exactly two registrations here and the bridge's is + // the second one. Naming it by position — and requiring it to differ from the race's callback — + // means a bridge that stopped registering its own listener cannot satisfy the assertions below + // by falling back on the race's. + const abortAddCalls = addEventListenerSpy.mock.calls.filter(([event]) => event === "abort") + const abortRemoveCalls = removeListenerSpy.mock.calls + .filter(([event]) => event === "abort") + .map(([, listener]) => listener) + expect(abortAddCalls.length).toBe(2) + const [raceCall, bridgeCall] = abortAddCalls + const addedListener = bridgeCall?.[1] + expect(typeof addedListener).toBe("function") + expect(addedListener).not.toBe(raceCall?.[1]) + expect(addEventListenerSpy).toHaveBeenCalledWith("abort", addedListener, { once: true }) + // Both listeners are gone by reference: the lookup race's first (its cleanup ran before the + // request), the bridge's last. + expect(abortRemoveCalls).toEqual([raceCall?.[1], addedListener]) + expect(controller.signal.aborted).toBe(false) + }) + + it("detaches the bridged abort listener when the OpenAI message conversion fails", async () => { + // The OpenAI branch converts messages and tools before the + // request; the conversion must run inside the same cleanup scope + // as the Anthropic and Responses branches so an unstringifiable + // tool input does not leave the bridged listener attached. + // A throwing value getter fails the conversion deterministically + // without depending on cycle detection in sanitizeSurrogatesDeep. + const failingInput: Record = { + get value(): never { + throw new Error("unstringifiable tool input") + }, + } + + const handler = new OpencodeGoHandler(mockOptions) + const controller = new AbortController() + const removeListenerSpy = vi.spyOn(controller.signal, "removeEventListener") + const addEventListenerSpy = vi.spyOn(controller.signal, "addEventListener") + + const stream = handler.createMessage( + "sys", + [ + { role: "user", content: "hi" }, + { + role: "assistant", + content: [{ type: "tool_use", id: "tu_1", name: "calc", input: failingInput }], + }, + ], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + + const error = await collectStream(stream).then( + () => undefined, + (e: unknown) => e, + ) + // The conversion failure propagates unchanged (the signal never + // aborted) and the bridge listener is detached by reference. + expect(error).toBeInstanceOf(Error) + expect((error as Error).message).toMatch(/unstringifiable tool input/) + // The same distinction as the other detach tests: two registrations on the caller's signal, the + // second one the bridge's, and both detached by reference in that order. + const abortAddCalls = addEventListenerSpy.mock.calls.filter(([event]) => event === "abort") + const abortRemoveCalls = removeListenerSpy.mock.calls + .filter(([event]) => event === "abort") + .map(([, listener]) => listener) + expect(abortAddCalls.length).toBe(2) + const [raceCall, bridgeCall] = abortAddCalls + const addedListener = bridgeCall?.[1] + expect(typeof addedListener).toBe("function") + expect(addedListener).not.toBe(raceCall?.[1]) + expect(abortRemoveCalls).toEqual([raceCall?.[1], addedListener]) + expect(controller.signal.aborted).toBe(false) + }) + }) + describe("completePrompt", () => { + it("returns the message content for a non-streaming completion", async () => { + mockCreate.mockResolvedValue({ choices: [{ message: { content: "the answer" } }] }) + const handler = new OpencodeGoHandler(mockOptions) + expect(await handler.completePrompt("ping")).toBe("the answer") + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ + model: "glm-5.1", + stream: false, + // glm-5.1 maxTokens (131_072) clamped to 20% of 204_800 => 40_960. + max_completion_tokens: 40_960, + reasoning_effort: "medium", + }), + expect.objectContaining({ + headers: expect.objectContaining({ "x-opencode-session": expect.any(String) }), + }), + ) + }) + + it("wraps errors with an Opencode Go-specific message", async () => { + mockCreate.mockRejectedValue(new Error("boom")) + const handler = new OpencodeGoHandler(mockOptions) + await expect(handler.completePrompt("ping")).rejects.toThrow("Opencode Go completion error: boom") + }) + + it("rethrows non-Error values unchanged", async () => { + mockCreate.mockRejectedValue("not an error") + const handler = new OpencodeGoHandler(mockOptions) + await expect(handler.completePrompt("ping")).rejects.toBe("not an error") + }) + + it("returns an empty string when no content is returned", async () => { + mockCreate.mockResolvedValue({ choices: [] }) + const handler = new OpencodeGoHandler(mockOptions) + expect(await handler.completePrompt("ping")).toBe("") + }) + + it("honors includeMaxTokens/modelMaxTokens override for max_completion_tokens", async () => { + mockCreate.mockResolvedValue({ choices: [{ message: { content: "ok" } }] }) + const handler = new OpencodeGoHandler({ ...mockOptions, includeMaxTokens: true, modelMaxTokens: 4321 }) + await handler.completePrompt("ping") + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ max_completion_tokens: 4321 }), + expect.objectContaining({ + headers: expect.objectContaining({ "x-opencode-session": expect.any(String) }), + }), + ) + }) + + it("rejects with the standardized AbortError when the caller aborts while completePrompt is resolving the model", async () => { + // completePrompt must scope model resolution like createMessage does: + // a catalog lookup parked on a gate has to settle on the standardized + // AbortError as soon as the caller's signal fires, without waiting for + // the lookup to finish and without starting the completion request. + let releaseResolution!: () => void + const resolutionGate = new Promise((resolve) => { + releaseResolution = resolve + }) + // The exact promise the provider awaits, so the test can await the lookup's own + // settlement instead of guessing how long a parked lookup takes. + let lookupPromise: Promise> | undefined + vitest.mocked(getModels).mockImplementationOnce(() => { + const parked = (async () => { + await resolutionGate + return { "glm-5.1": { ...opencodeGoModels["glm-5.1"] } } + })() + lookupPromise = parked + return parked + }) + + const handler = new OpencodeGoHandler(mockOptions) + const controller = new AbortController() + + const completion = handler.completePrompt("ping", { abortSignal: controller.signal }) + + // Observable condition: the catalog lookup has been invoked and is parked on the + // gate, which is what the 10 ms sleep was standing in for. + await vitest.waitFor(() => expect(vitest.mocked(getModels)).toHaveBeenCalledTimes(1), { + timeout: 1000, + interval: 5, + }) + controller.abort() + + await expect(completion).rejects.toMatchObject({ + name: "AbortError", + message: "The Opencode Go request was aborted", + }) + // Deliberately not asserted here: only after awaiting the provider's own lookup promise + // below is 'no request started' a statement about drained state rather than about timing. + expect(mockCreate).not.toHaveBeenCalled() + + // Observable condition: the parked lookup has actually settled. Awaiting the same + // promise the provider awaits drains the provider's continuation first, because the + // provider subscribed to it before this test did; only then is "no late request" a + // statement about drained state rather than about a 10 ms window. + releaseResolution() + await lookupPromise + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("rejects with the AbortError before the model lookup when completePrompt gets a pre-aborted signal", async () => { + const handler = new OpencodeGoHandler(mockOptions) + const controller = new AbortController() + controller.abort() + + await expect(handler.completePrompt("ping", { abortSignal: controller.signal })).rejects.toMatchObject({ + name: "AbortError", + message: "The Opencode Go request was aborted", + }) + // The catalog must be skipped entirely, not merely raced. + expect(vitest.mocked(getModels)).not.toHaveBeenCalled() + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("sends the per-instance session ID on non-streaming completions", async () => { + mockCreate.mockResolvedValue({ choices: [{ message: { content: "ok" } }] }) + const handler = new OpencodeGoHandler(mockOptions) + + await handler.completePrompt("ping") + const sessionId = mockCreate.mock.calls[0][1]?.headers["x-opencode-session"] + expect(sessionId).toEqual(expect.any(String)) + + // completePrompt has no taskId metadata, so the per-instance ID is + // reused across calls on the same handler. + await handler.completePrompt("ping") + expect(mockCreate.mock.calls[1][1]?.headers["x-opencode-session"]).toBe(sessionId) + }) + }) + + describe("Anthropic-format models (qwen3.7-max)", () => { + // qwen3.7-max is only reachable via the Anthropic Messages endpoint + // (/v1/messages); sending it to /v1/chat/completions is what produces + // "401 Model qwen3.7-max is not supported for format oa-compat". + const anthropicOptions: ApiHandlerOptions = { + opencodeGoApiKey: "test-key", + opencodeGoModelId: "qwen3.7-max", + } + + beforeEach(() => { + mockAnthropicCreate.mockImplementation(async () => + asyncStreamFrom([ + { + type: "message_start", + message: { + usage: { + input_tokens: 10, + output_tokens: 0, + cache_creation_input_tokens: 2, + cache_read_input_tokens: 3, + }, + }, + }, + { + type: "content_block_start", + index: 0, + content_block: { type: "text", text: "" }, + }, + { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "Hello" } }, + { + type: "content_block_start", + index: 1, + content_block: { type: "tool_use", id: "toolu_1", name: "read_file", input: {} }, + }, + { + type: "content_block_delta", + index: 1, + delta: { type: "input_json_delta", partial_json: '{"path":' }, + }, + { type: "content_block_stop", index: 1 }, + { type: "message_delta", usage: { output_tokens: 5 } }, + { type: "message_stop" }, + ]), + ) + }) + + it("routes the request through the Anthropic /v1/messages client, not chat completions", async () => { + const handler = new OpencodeGoHandler(anthropicOptions) + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + + await collectStream(handler.createMessage("sys", messages)) + + expect(mockAnthropicCreate).toHaveBeenCalledWith( + expect.objectContaining({ + model: "qwen3.7-max", + stream: true, + system: expect.arrayContaining([expect.objectContaining({ type: "text", text: "sys" })]), + }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), + ) + // The OpenAI chat completions endpoint must NOT be used for this model. + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("sends the stable conversation ID to the Anthropic messages endpoint", async () => { + const handler = new OpencodeGoHandler(anthropicOptions) + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + + await collectStream(handler.createMessage("sys", messages, { taskId: "conversation-123" })) + + expect(mockAnthropicCreate.mock.calls[0][1]?.headers).toMatchObject({ + "x-opencode-session": "conversation-123", + }) + }) + + it("falls back to a stable per-instance session ID when no taskId is provided", async () => { + const handler = new OpencodeGoHandler(anthropicOptions) + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + + await collectStream(handler.createMessage("sys", messages)) + + const sessionId = mockAnthropicCreate.mock.calls[0][1]?.headers["x-opencode-session"] + expect(sessionId).toEqual(expect.any(String)) + + // The same handler instance reuses its session ID across requests. + await collectStream(handler.createMessage("sys", messages)) + expect(mockAnthropicCreate.mock.calls[1][1]?.headers["x-opencode-session"]).toBe(sessionId) + }) + + it("streams text, tool-call, usage and cost chunks from the Anthropic stream", async () => { + const handler = new OpencodeGoHandler(anthropicOptions) + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + + const chunks = await collectStream(handler.createMessage("sys", messages)) + + expect(chunks).toContainEqual({ type: "text", text: "Hello" }) + expect(chunks).toContainEqual({ + type: "tool_call_partial", + index: 1, + id: "toolu_1", + name: "read_file", + arguments: undefined, + }) + expect(chunks).toContainEqual({ + type: "tool_call_partial", index: 1, id: undefined, name: undefined, @@ -663,6 +1613,23 @@ describe("OpencodeGoHandler", () => { ) }) + it("preserves abort identity when the Anthropic request rejects with a name-based AbortError", async () => { + // No SDK abort class and no aborted signal: only the DOM-standard + // name === "AbortError" check marks a cancelled pre-stream request. + const rawAbort = Object.assign(new Error("raw"), { name: "AbortError" }) + mockAnthropicCreate.mockRejectedValueOnce(rawAbort) + + const handler = new OpencodeGoHandler(anthropicOptions) + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "hi" }] + + const error = await collectStream(handler.createMessage("sys", messages)).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }) + it("applies cache-control breakpoints when the model supports prompt caching", async () => { const handler = new OpencodeGoHandler(anthropicOptions) const messages: Anthropic.Messages.MessageParam[] = [ @@ -689,16 +1656,18 @@ describe("OpencodeGoHandler", () => { const handler = new OpencodeGoHandler(anthropicOptions) expect(await handler.completePrompt("ping")).toBe("the answer") - const callArgs = mockAnthropicCreate.mock.calls[0][0] as Record - expect(callArgs).toMatchObject({ - model: "qwen3.7-max", - stream: false, - messages: [{ role: "user", content: "ping" }], - // qwen3.7-max maxTokens (65_536) clamped to 20% of its 1M - // context window (200_000) => 65_536. includeMaxTokens is off, - // so the model default is used. - max_tokens: 65_536, - }) + expect(mockAnthropicCreate).toHaveBeenCalledWith( + expect.objectContaining({ + model: "qwen3.7-max", + stream: false, + messages: [{ role: "user", content: "ping" }], + // qwen3.7-max maxTokens (65_536) clamped to 20% of its 1M + // context window (200_000) => 65_536. includeMaxTokens is off, + // so the model default is used. + max_tokens: 65_536, + }), + undefined, + ) expect(mockCreate).not.toHaveBeenCalled() }) @@ -713,8 +1682,7 @@ describe("OpencodeGoHandler", () => { modelMaxTokens: 2048, }) await handler.completePrompt("ping") - const callArgs = mockAnthropicCreate.mock.calls[0][0] as Record - expect(callArgs.max_tokens).toBe(2048) + expect(mockAnthropicCreate).toHaveBeenCalledWith(expect.objectContaining({ max_tokens: 2048 }), undefined) }) it("completePrompt falls back to the model max_tokens when includeMaxTokens is on but modelMaxTokens is unset", async () => { @@ -760,6 +1728,375 @@ describe("OpencodeGoHandler", () => { expect(await handler.completePrompt("ping")).toBe("") }) + it("completePrompt passes abort signal through to Anthropic client", async () => { + mockAnthropicCreate.mockResolvedValue({ content: [{ type: "text", text: "response" }] }) + const controller = new AbortController() + const handler = new OpencodeGoHandler(anthropicOptions) + await handler.completePrompt("ping", { abortSignal: controller.signal }) + // With no timeoutMs, mergeAbortSignalAndTimeout returns the caller's signal unchanged: + // pin the identity so forwarding a freshly-wrapped signal (or dropping the caller's) + // cannot pass. + expect(mockAnthropicCreate).toHaveBeenCalledWith(expect.objectContaining({ model: expect.any(String) }), { + signal: controller.signal, + }) + }) + + it("completePrompt passes both signal and timeoutMs through to Anthropic client", async () => { + mockAnthropicCreate.mockResolvedValue({ content: [{ type: "text", text: "response" }] }) + const controller = new AbortController() + const handler = new OpencodeGoHandler(anthropicOptions) + await handler.completePrompt("ping", { abortSignal: controller.signal, timeoutMs: 10000 }) + expect(mockAnthropicCreate).toHaveBeenCalledWith(expect.objectContaining({ model: expect.any(String) }), { + signal: expect.any(AbortSignal), + timeout: 10000, + }) + }) + + it("completePrompt passes the timeout deadline signal and timeoutMs when no caller signal is provided", async () => { + mockAnthropicCreate.mockResolvedValue({ content: [{ type: "text", text: "response" }] }) + const handler = new OpencodeGoHandler(anthropicOptions) + await handler.completePrompt("ping", { timeoutMs: 5000 }) + // The per-request timeout now also bounds model resolution, so the merged deadline + // signal is forwarded alongside the SDK timeout option. + expect(mockAnthropicCreate).toHaveBeenCalledWith(expect.objectContaining({ model: expect.any(String) }), { + signal: expect.any(AbortSignal), + timeout: 5000, + }) + }) + + it("completePrompt omits the timeout option when timeoutMs is 0 (Anthropic path)", async () => { + // The SDK treats timeout: 0 as an immediate abort, so the "disabled" + // value must never be forwarded — assert the absence of the option. + mockAnthropicCreate.mockResolvedValue({ content: [{ type: "text", text: "response" }] }) + const handler = new OpencodeGoHandler(anthropicOptions) + await handler.completePrompt("ping", { timeoutMs: 0 }) + const call = mockAnthropicCreate.mock.calls[mockAnthropicCreate.mock.calls.length - 1] + const requestOptions = call[1] as { timeout?: number } | undefined + // When no option is forwarded the provider omits the SDK options + // argument entirely, so absence means: undefined arg OR an arg + // without a timeout key. + expect(Object.keys(requestOptions ?? {})).not.toContain("timeout") + }) + it("completePrompt omits the timeout option when timeoutMs is negative (Anthropic path)", async () => { + // The disabled-timeout contract covers every non-positive value, not just 0. + mockAnthropicCreate.mockResolvedValue({ content: [{ type: "text", text: "response" }] }) + const handler = new OpencodeGoHandler(anthropicOptions) + await handler.completePrompt("ping", { timeoutMs: -1 }) + const call = mockAnthropicCreate.mock.calls[mockAnthropicCreate.mock.calls.length - 1] + const requestOptions = call[1] as { timeout?: number } | undefined + expect(Object.keys(requestOptions ?? {})).not.toContain("timeout") + }) + + it("completePrompt preserves abort identity when the caller aborts mid-request (Anthropic path)", async () => { + // The abort has to land after the request starts. A pre-aborted signal is + // caught by resolveModelWithAbort during model resolution, so the completion + // catch in opencode-go.ts (the isRequestAborted branch) never runs and the + // test would pass without exercising it. The mock rejects with a generic + // Error rather than an abort-shaped one, exactly the shape the sibling + // Responses test uses, so normalizing it proves the catch consults the + // caller signal argument and not just the error name or message. + mockAnthropicCreate.mockImplementation( + (_params: unknown, options: { signal?: AbortSignal }) => + new Promise((_resolve, reject) => { + const signal = options?.signal + if (!signal) { + reject(new Error("the provider forwarded no request signal")) + return + } + signal.addEventListener("abort", () => reject(new Error("boom")), { once: true }) + }), + ) + const handler = new OpencodeGoHandler(anthropicOptions) + const controller = new AbortController() + const completion = handler.completePrompt("ping", { abortSignal: controller.signal }) + // Attach the rejection handler before polling: if the lookup or the SDK call rejects while this + // test is still waiting for readiness, the rejection is already observed. + const settled = completion.then( + () => undefined, + (e: unknown) => e, + ) + + await vitest.waitFor(() => expect(mockAnthropicCreate).toHaveBeenCalled()) + controller.abort() + + const error = await settled + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }) + + it("completePrompt surfaces request timeouts as an AbortError (Anthropic path)", async () => { + // Emulate the Anthropic SDK: when the request timeout fires, the SDK + // surfaces APIConnectionTimeoutError ("Request timed out.") once retries + // are exhausted — verified against @anthropic-ai/sdk against a hung server. + mockAnthropicCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => { + const requestSignal = options?.signal + if (!requestSignal) { + throw new Error("the provider forwarded no request signal") + } + // Observable condition: the deadline signal the provider shares with this request has + // fired. The 50 ms sleep stood in for that same instant — the SDK's own timeout + // budget and this deadline are the same number by design, so the signal is the only + // witness that either leg of the race actually ran. + await vitest.waitFor(() => expect(requestSignal.aborted).toBe(true), { timeout: 1000, interval: 5 }) + throw new AnthropicTimeoutError() + }) + const handler = new OpencodeGoHandler(anthropicOptions) + + const error = await handler.completePrompt("ping", { timeoutMs: 50 }).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }) + + it("completePrompt works without options (backward compatible, Anthropic path)", async () => { + mockAnthropicCreate.mockResolvedValue({ content: [{ type: "text", text: "response" }] }) + const handler = new OpencodeGoHandler(anthropicOptions) + const result = await handler.completePrompt("ping") + expect(result).toBe("response") + expect(mockAnthropicCreate).toHaveBeenCalledWith( + expect.objectContaining({ model: expect.any(String) }), + undefined, + ) + }) + + it("completePrompt keeps the model max_tokens when includeMaxTokens is off (Anthropic path)", async () => { + // includeMaxTokens unset: modelMaxTokens must NOT replace the model + // default — only the explicit includeMaxTokens flag opts into the + // user override. + mockAnthropicCreate.mockResolvedValueOnce({ content: [{ type: "text", text: "ok" }] }) + const handler = new OpencodeGoHandler({ ...anthropicOptions, modelMaxTokens: 2048 }) + await handler.completePrompt("ping") + expect(mockAnthropicCreate).toHaveBeenCalledWith( + expect.objectContaining({ model: "qwen3.7-max", max_tokens: 65_536 }), + undefined, + ) + }) + + it("completePrompt forwards an explicit model temperature (Anthropic path)", async () => { + mockAnthropicCreate.mockResolvedValueOnce({ content: [{ type: "text", text: "ok" }] }) + const handler = new OpencodeGoHandler({ ...anthropicOptions, modelTemperature: 0.7 }) + await handler.completePrompt("ping") + expect(mockAnthropicCreate).toHaveBeenCalledWith( + expect.objectContaining({ model: "qwen3.7-max", temperature: 0.7 }), + undefined, + ) + }) + + it("completePrompt settles a pre-aborted signal before the Anthropic request starts", async () => { + // Model resolution runs inside the cancellation scope, so a signal + // that is already aborted rejects before the Anthropic request is + // built: the SDK must never be called. + const controller = new AbortController() + controller.abort() + const handler = new OpencodeGoHandler(anthropicOptions) + + const error = await handler.completePrompt("ping", { abortSignal: controller.signal }).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + expect(mockAnthropicCreate).not.toHaveBeenCalled() + }) + + it("completePrompt preserves abort identity for a name-based AbortError rejection (Anthropic path)", async () => { + // No aborted signal and no SDK abort class: only the DOM-standard + // name === "AbortError" check marks a cancelled request. + const rawAbort = Object.assign(new Error("raw"), { name: "AbortError" }) + mockAnthropicCreate.mockRejectedValueOnce(rawAbort) + const handler = new OpencodeGoHandler(anthropicOptions) + + const error = await handler.completePrompt("ping").then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }) + + describe("completePrompt (OpenAI path)", () => { + const openaiOptions: ApiHandlerOptions = { + opencodeGoApiKey: "test-key", + // The handler reads opencodeGoModelId; apiModelId would be ignored silently and the whole + // describe would run the glm-5.2 default while claiming glm-5.1. + opencodeGoModelId: "glm-5.1", // OpenAI-format model + } + + beforeEach(() => { + clearAllMocks() + }) + + it("completePrompt returns text for OpenAI path", async () => { + mockCreate.mockResolvedValueOnce({ + choices: [{ message: { content: "response" } }], + }) + + const handler = new OpencodeGoHandler(openaiOptions) + expect(await handler.completePrompt("ping")).toBe("response") + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ model: "glm-5.1", stream: false }), + { headers: { "x-opencode-session": expect.any(String) } }, // session header only: no signal/timeout + ) + }) + + it("completePrompt passes abort signal through to OpenAI client", async () => { + mockCreate.mockResolvedValueOnce({ + choices: [{ message: { content: "response" } }], + }) + const controller = new AbortController() + const handler = new OpencodeGoHandler(openaiOptions) + + await handler.completePrompt("ping", { abortSignal: controller.signal }) + expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ model: "glm-5.1", stream: false }), { + signal: expect.any(AbortSignal), + headers: { "x-opencode-session": expect.any(String) }, + }) + }) + + it("completePrompt passes both signal and timeoutMs through to OpenAI client", async () => { + mockCreate.mockResolvedValueOnce({ + choices: [{ message: { content: "response" } }], + }) + const controller = new AbortController() + const handler = new OpencodeGoHandler(openaiOptions) + + await handler.completePrompt("ping", { abortSignal: controller.signal, timeoutMs: 10000 }) + expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ model: "glm-5.1", stream: false }), { + signal: expect.any(AbortSignal), + timeout: 10000, + headers: { "x-opencode-session": expect.any(String) }, + }) + }) + + it("completePrompt passes only timeoutMs when no signal is provided", async () => { + mockCreate.mockResolvedValueOnce({ + choices: [{ message: { content: "response" } }], + }) + const handler = new OpencodeGoHandler(openaiOptions) + + await handler.completePrompt("ping", { timeoutMs: 5000 }) + expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ model: "glm-5.1", stream: false }), { + signal: expect.any(AbortSignal), + timeout: 5000, + headers: { "x-opencode-session": expect.any(String) }, + }) + }) + + it("completePrompt omits the timeout option when timeoutMs is 0 (OpenAI path)", async () => { + // The OpenAI SDK treats timeout: 0 as an immediate abort, so the + // "disabled" value must never be forwarded — assert the absence of + // the option. + mockCreate.mockResolvedValueOnce({ + choices: [{ message: { content: "response" } }], + }) + const handler = new OpencodeGoHandler(openaiOptions) + await handler.completePrompt("ping", { timeoutMs: 0 }) + const call = mockCreate.mock.calls[mockCreate.mock.calls.length - 1] + const requestOptions = call[1] as { timeout?: number } | undefined + expect(requestOptions).not.toHaveProperty("timeout") + }) + it("completePrompt omits the timeout option when timeoutMs is negative (OpenAI path)", async () => { + // The disabled-timeout contract covers every non-positive value, not just 0. + mockCreate.mockResolvedValueOnce({ + choices: [{ message: { content: "response" } }], + }) + const handler = new OpencodeGoHandler(openaiOptions) + await handler.completePrompt("ping", { timeoutMs: -1 }) + const call = mockCreate.mock.calls[mockCreate.mock.calls.length - 1] + const requestOptions = call[1] as { timeout?: number } | undefined + expect(requestOptions).not.toHaveProperty("timeout") + }) + + it("completePrompt normalizes the OpenAI SDK's own abort rejection", async () => { + // Delivered from the SDK mock with no caller signal: the pre-flight guard cannot fire, + // so the mock really runs and its APIUserAbortError ("Request was aborted." — the + // trailing period would fail task-level abort detection) is what the catch must normalize. + mockCreate.mockImplementationOnce(async () => { + throw await sdkAbortError() + }) + const handler = new OpencodeGoHandler(openaiOptions) + + const error = await handler.completePrompt("ping").then( + () => undefined, + (e: unknown) => e, + ) + expect(mockCreate).toHaveBeenCalledTimes(1) // the SDK call ran, not the fast-fail guard + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }) + + it("completePrompt surfaces request timeouts as an AbortError (OpenAI path)", async () => { + // Emulate the OpenAI SDK: when the request timeout fires, the SDK + // surfaces APIConnectionTimeoutError ("Request timed out.") once retries + // are exhausted — verified against openai v5.23.2 against a hung server. + mockCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => { + const requestSignal = options?.signal + if (!requestSignal) { + throw new Error("the provider forwarded no request signal") + } + // Observable condition: the deadline signal the provider shares with this request + // has fired, the same instant the 50 ms sleep was standing in for. + await vitest.waitFor(() => expect(requestSignal.aborted).toBe(true), { timeout: 1000, interval: 5 }) + throw new APIConnectionTimeoutError() + }) + const handler = new OpencodeGoHandler(openaiOptions) + + const error = await handler.completePrompt("ping", { timeoutMs: 50 }).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }) + + it("completePrompt works without options (backward compatible, OpenAI path)", async () => { + mockCreate.mockResolvedValueOnce({ + choices: [{ message: { content: "response" } }], + }) + const handler = new OpencodeGoHandler(openaiOptions) + + const result = await handler.completePrompt("ping") + expect(result).toBe("response") + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ model: "glm-5.1", stream: false }), + { headers: { "x-opencode-session": expect.any(String) } }, // session header only: no signal/timeout + ) + }) + + it("completePrompt settles a pre-aborted signal before the OpenAI request starts", async () => { + // Model resolution runs inside the cancellation scope, so a signal + // that is already aborted rejects before the chat completion is + // issued: the SDK must never be called. + const controller = new AbortController() + controller.abort() + const handler = new OpencodeGoHandler(openaiOptions) + + const error = await handler.completePrompt("ping", { abortSignal: controller.signal }).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("completePrompt preserves abort identity for a name-based AbortError rejection (OpenAI path)", async () => { + // No aborted signal and no SDK abort class: only the DOM-standard + // name === "AbortError" check marks a cancelled request. + const rawAbort = Object.assign(new Error("raw"), { name: "AbortError" }) + mockCreate.mockRejectedValueOnce(rawAbort) + const handler = new OpencodeGoHandler(openaiOptions) + + const error = await handler.completePrompt("ping").then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }) + }) it("omits tools and tool_choice from the Anthropic request when no tools are provided", async () => { const handler = new OpencodeGoHandler(anthropicOptions) const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] @@ -905,8 +2242,10 @@ describe("OpencodeGoHandler", () => { await collectStream(handler.createMessage("sys", messages)) - const callArgs = mockAnthropicCreate.mock.calls[0][0] as Record - expect(callArgs.max_tokens).toBe(8192) + expect(mockAnthropicCreate).toHaveBeenCalledWith( + expect.objectContaining({ max_tokens: 8192 }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), + ) }) it("falls back to the model max_tokens when includeMaxTokens is on but modelMaxTokens is unset", async () => { @@ -916,11 +2255,13 @@ describe("OpencodeGoHandler", () => { await collectStream(handler.createMessage("sys", messages)) // qwen3.7-max maxTokens (65_536) clamped to 20% of 1M context => 65_536. - const callArgs = mockAnthropicCreate.mock.calls[0][0] as Record - expect(callArgs.max_tokens).toBe(65_536) + expect(mockAnthropicCreate).toHaveBeenCalledWith( + expect.objectContaining({ max_tokens: 65_536 }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), + ) }) - it("accumulates output tokens across message_delta events into the final cost", async () => { + it("adopts the cumulative output token count in the final Anthropic cost", async () => { mockAnthropicCreate.mockImplementationOnce(async () => asyncStreamFrom([ { type: "message_start", message: { usage: { input_tokens: 10, output_tokens: 0 } } }, @@ -941,30 +2282,214 @@ describe("OpencodeGoHandler", () => { if (!costChunk || costChunk.type !== "usage") { throw new Error("Expected usage chunk with cost") } - // qwen3.7-max: input $2.5/M, output $7.5/M. Accumulated output - // tokens (4 + 6 = 10) must feed the cost calc — without the - // accumulation fix this would only reflect the 10 input tokens - // (0.000025) instead of input + output (0.0001). - expect(costChunk.totalCost).toBeCloseTo((10 * 2.5 + 10 * 7.5) / 1_000_000, 10) + // qwen3.7-max: input $2.5/M, output $7.5/M. Each message_delta reports the running total, + // so the final cost reflects the reported 6 output tokens rather than the sum of the + // two deltas (4 + 6 = 10) - adopting the reported total is what anthropic.ts and + // minimax.ts do. The per-event usage chunks above still carry each delta's own value. + expect(costChunk.totalCost).toBeCloseTo((10 * 2.5 + 6 * 7.5) / 1_000_000, 10) + }) + + it("does not yield a cost chunk when the stream reports no token usage", async () => { + mockAnthropicCreate.mockImplementationOnce(async () => + asyncStreamFrom([ + { type: "message_start", message: { usage: { input_tokens: 0, output_tokens: 0 } } }, + { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }, + { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "hi" } }, + { type: "message_delta", usage: { output_tokens: 0 } }, + { type: "message_stop" }, + ]), + ) + + const handler = new OpencodeGoHandler(anthropicOptions) + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + + const chunks = await collectStream(handler.createMessage("sys", messages)) + + expect(chunks.some((c) => c.type === "usage" && c.totalCost !== undefined)).toBe(false) + }) + + it("re-checks the signal between the separator and the body of an indexed thinking block", async () => { + // The thinking branch carries its own separator guard: an indexed thinking block yields the newline + // and then the block body, and only the check in between can stop the body after a cancel. + mockAnthropicCreate.mockImplementationOnce(async () => + asyncStreamFrom([ + { + type: "content_block_start", + index: 1, + content_block: { type: "thinking", thinking: "pondering" }, + }, + ]), + ) + const controller = new AbortController() + const handler = new OpencodeGoHandler(anthropicOptions) + const iterator = handler + .createMessage( + "sys", + [{ role: "user", content: "Hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "reasoning", text: "\n" }) + // Observable condition: the separator reached the consumer, so the cancel lands before the body. + controller.abort() + + const settled = await iterator.next().then( + (result) => result.value, + (error: unknown) => error, + ) + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Opencode Go request was aborted") + }) + + it("completes an Anthropic stream when the caller passes no abort signal at all", async () => { + // Every guard in this helper is written as abortSignal?.aborted. With no caller signal there is + // nothing to consult, so they must fall through: dropping the optional chain would turn each guard + // into a property access on undefined and fail a stream that nobody cancelled. + const events = [ + { type: "content_block_start", index: 0, content_block: { type: "text", text: "one" } }, + { type: "content_block_start", index: 1, content_block: { type: "text", text: "two" } }, + { type: "content_block_start", index: 2, content_block: { type: "thinking", thinking: "pondering" } }, + { type: "message_start", message: { usage: { input_tokens: 3, output_tokens: 4 } } }, + ] + mockAnthropicCreate.mockImplementationOnce(async () => asyncStreamFrom(events)) + const handler = new OpencodeGoHandler(anthropicOptions) + const chunks = await collectStream( + handler.createMessage( + "sys", + [{ role: "user", content: "Hi" }], + // No caller signal at all: the uncancelled default path through every guard. + makeCreateMessageMetadata({ abortSignal: undefined }), + ), + ) + expect(chunks.filter((chunk) => chunk.type === "text").map((chunk) => chunk.text)).toEqual([ + "one", + "\n", + "two", + ]) + expect(chunks.filter((chunk) => chunk.type === "reasoning").map((chunk) => chunk.text)).toEqual([ + "\n", + "pondering", + ]) + expect(chunks.some((chunk) => chunk.type === "usage" && chunk.totalCost !== undefined)).toBe(true) + }) + + it("rejects an Anthropic stream that closes quietly after a cancel and still succeeds on a live signal", async () => { + // The Anthropic SDK swallows the body-read AbortError and returns from the iterator, so the + // helper can reach its terminal cost yield on a cancelled request. A cancelled turn must not + // report terminal usage as if it finished, and a live signal must still complete normally. + const events = [ + { type: "content_block_start", index: 0, content_block: { type: "text", text: "answer" } }, + { type: "message_start", message: { usage: { input_tokens: 3, output_tokens: 4 } } }, + ] + const handler = new OpencodeGoHandler(anthropicOptions) + const cancelled = new AbortController() + mockAnthropicCreate.mockImplementationOnce(async () => asyncStreamFrom(events)) + const iterator = handler + .createMessage( + "sys", + [{ role: "user", content: "Hi" }], + makeCreateMessageMetadata({ abortSignal: cancelled.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "text", text: "answer" }) + // Observable condition: the consumer holds the partial text and cancels; the mock iterator then + // reports done instead of throwing, which is what the pinned SDK does. + cancelled.abort() + + const settled = await iterator.next().then( + (result) => result.value, + (error: unknown) => error, + ) + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Opencode Go request was aborted") + + // The same fixture with the caller signal live is an ordinary completion, terminal cost included. + mockAnthropicCreate.mockImplementationOnce(async () => asyncStreamFrom(events)) + const live = new AbortController() + const chunks = await collectStream( + handler.createMessage( + "sys", + [{ role: "user", content: "Hi" }], + makeCreateMessageMetadata({ abortSignal: live.signal }), + ), + ) + expect(chunks.some((chunk) => chunk.type === "usage" && chunk.totalCost !== undefined)).toBe(true) + expect(live.signal.aborted).toBe(false) + }) + + it("rejects instead of reporting terminal cost when the cancel lands after the last Anthropic event", async () => { + // Every event was dispatched and delivered, so neither the per-event check nor the separator check + // can run again; only a check after the loop can stop the helper from yielding its terminal cost + // chunk for a turn the caller already cancelled. + mockAnthropicCreate.mockImplementationOnce(async () => + asyncStreamFrom([ + { type: "message_start", message: { usage: { input_tokens: 3, output_tokens: 4 } } }, + { type: "content_block_start", index: 0, content_block: { type: "text", text: "answer" } }, + { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: " more" } }, + ]), + ) + const controller = new AbortController() + const handler = new OpencodeGoHandler(anthropicOptions) + const iterator = handler + .createMessage( + "sys", + [{ role: "user", content: "Hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + [Symbol.asyncIterator]() + + const delivered = [] + for (let i = 0; i < 3; i++) { + const result = await iterator.next() + if (result.done) throw new Error("expected three delivered items") + delivered.push(result.value) + } + expect(delivered.map((item) => item.type)).toEqual(["usage", "text", "text"]) + // Observable condition: the caller cancels after the last event was handed over, and the mock + // iterator then reports done the way the pinned SDK does after swallowing the abort. + controller.abort() + + const settled = await iterator.next().then( + (result) => result.value, + (error: unknown) => error, + ) + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Opencode Go request was aborted") }) - it("does not yield a cost chunk when the stream reports no token usage", async () => { + it("re-checks the signal between the two yields of one Anthropic event", async () => { + // An indexed content_block_start emits a separator item and then the block text. A consumer that + // takes the separator and cancels must not be handed the text: only a guard inside the event + // dispatch can stop it, because the post-stream guard runs after both yields. mockAnthropicCreate.mockImplementationOnce(async () => asyncStreamFrom([ - { type: "message_start", message: { usage: { input_tokens: 0, output_tokens: 0 } } }, - { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }, - { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "hi" } }, - { type: "message_delta", usage: { output_tokens: 0 } }, - { type: "message_stop" }, + { type: "content_block_start", index: 1, content_block: { type: "text", text: "answer" } }, ]), ) - + const controller = new AbortController() const handler = new OpencodeGoHandler(anthropicOptions) - const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + const iterator = handler + .createMessage( + "sys", + [{ role: "user", content: "Hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + [Symbol.asyncIterator]() - const chunks = await collectStream(handler.createMessage("sys", messages)) + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "text", text: "\n" }) + controller.abort() - expect(chunks.some((c) => c.type === "usage" && c.totalCost !== undefined)).toBe(false) + const settled = await iterator.next().then( + (result) => result.value, + (error: unknown) => error, + ) + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Opencode Go request was aborted") }) it("completePrompt wraps Anthropic errors with an Opencode Go-specific message", async () => { @@ -984,6 +2509,68 @@ describe("OpencodeGoHandler", () => { await collectStream(handler.createMessage("sys", messages)) }).rejects.toThrow("Opencode Go completion error: rate limited") }) + + it("preserves abort identity for aborted Anthropic requests from createMessage", async () => { + // A cancelled /v1/messages request (the SDK rejects with + // APIUserAbortError) must surface as a DOM-standard AbortError, not + // the wrapped "completion error" reserved for other failures. + mockAnthropicCreate.mockRejectedValue(new AnthropicAbortError()) + const handler = new OpencodeGoHandler(anthropicOptions) + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + await expect(async () => { + await collectStream(handler.createMessage("sys", messages)) + }).rejects.toMatchObject({ + name: "AbortError", + message: "The Opencode Go request was aborted", + }) + }) + + it("normalizes a mid-stream abort on the Anthropic path to the standardized AbortError", async () => { + // A cancellation that surfaces after the /v1/messages stream has + // started must normalize to the standardized AbortError like the + // other wire formats, not leak the raw SDK rejection. + let capturedSignal: AbortSignal | undefined + let firstChunkDelivered = false + mockAnthropicCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => { + capturedSignal = options?.signal + return (async function* () { + yield { + type: "content_block_delta", + index: 0, + delta: { type: "text_delta", text: "partial" }, + } + // Runs only once the consumer has taken the first chunk and asked for the next + // one, which is the state the 25 ms sleep was approximating. + firstChunkDelivered = true + // Observable condition: the bridge aborted the internal controller. + await vitest.waitFor(() => expect(capturedSignal?.aborted).toBe(true), { + timeout: 1000, + interval: 5, + }) + throw new AnthropicAbortError() + })() + }) + const controller = new AbortController() + const handler = new OpencodeGoHandler(anthropicOptions) + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + + const consumed = collectStream( + handler.createMessage("sys", messages, { taskId: "test-task", abortSignal: controller.signal }), + ) + + // Observable condition: the first chunk has been consumed and the stream is asking for + // the next one; abort mid-stream from there rather than after a fixed 25 ms. + await vitest.waitFor(() => expect(firstChunkDelivered).toBe(true), { timeout: 1000, interval: 5 }) + controller.abort() + + const error = await consumed.then( + () => undefined, + (e: unknown) => e, + ) + expect(capturedSignal?.aborted).toBe(true) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }) }) describe("Responses-format models (gpt-5.6-luna, gpt-6-luna)", () => { @@ -1018,7 +2605,11 @@ describe("OpencodeGoHandler", () => { ) }) - it("forwards the abort signal to the streaming Responses request", async () => { + it("forwards the per-request abort signal to the streaming Responses request", async () => { + // The /v1/responses request is wired to the per-request internal + // controller (the bridge target), not the caller's signal: the + // caller's signal is bridged into the internal controller, and the + // SDK watches only the internal one. const handler = new OpencodeGoHandler(lunaOptions) const controller = new AbortController() const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] @@ -1027,10 +2618,413 @@ describe("OpencodeGoHandler", () => { handler.createMessage("sys", messages, { taskId: "test-task", abortSignal: controller.signal }), ) - expect(mockResponsesCreate.mock.calls[0][1]).toEqual({ - signal: controller.signal, - headers: { "x-opencode-session": "test-task" }, + const callOptions = mockResponsesCreate.mock.calls[0][1] as { signal?: AbortSignal; headers?: unknown } + expect(callOptions.headers).toEqual({ "x-opencode-session": "test-task" }) + expect(callOptions.signal).toBeInstanceOf(AbortSignal) + expect(callOptions.signal).not.toBe(controller.signal) + expect(callOptions.signal?.aborted).toBe(false) + }) + + it("normalizes an aborted Responses request to the standardized AbortError", async () => { + // Emulate the OpenAI SDK against the per-request internal signal: + // the caller's signal aborts just as the request starts, the bridge + // flips the internal controller, and the SDK rejects with + // APIUserAbortError. The Responses-path guard must normalize it to + // the DOM-standard AbortError instead of wrapping it. + const controller = new AbortController() + let sdkSignal: AbortSignal | undefined + mockResponsesCreate.mockImplementation(async (_body: unknown, options: { signal?: AbortSignal }) => { + sdkSignal = options?.signal + controller.abort() + // A plain failure rather than an abort-shaped one: the conversion into the contract + // AbortError can then only come from the provider consulting the aborted signal, so the + // assertions below have to be carried by the captured signal, not by that conversion. + throw new Error("gateway rejected the request") + }) + const handler = new OpencodeGoHandler(lunaOptions) + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + + const error = await collectStream( + handler.createMessage("sys", messages, { taskId: "test-task", abortSignal: controller.signal }), + ).then( + () => undefined, + (e: unknown) => e, + ) + // A cancelled /v1/responses request surfaces as a DOM-standard + // AbortError, not the wrapped "completion error" reserved for other + // failures. + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + // Observed outside the provider's conversion: the signal the SDK actually received had already + // flipped, so the bridge - not the catch's signal arm alone - carried the cancel into the request. + expect(sdkSignal).toBeInstanceOf(AbortSignal) + expect(sdkSignal?.aborted).toBe(true) + }) + + it("normalizes an SDK abort rejection on the Responses stream path while the signal is live", async () => { + // No caller signal here, so nothing aborts: the only thing that can normalize this rejection + // is the error-name arm of the pre-stream catch. Without that arm the failure falls through + // as "Opencode Go completion error: Request was aborted.", which the outer catch cannot + // recognize either — its message check is exact and the signal never aborted. + mockResponsesCreate.mockImplementationOnce(async () => { + throw await sdkAbortError() + }) + const handler = new OpencodeGoHandler(lunaOptions) + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + + const error = await collectStream(handler.createMessage("sys", messages)).then( + () => undefined, + (e: unknown) => e, + ) + expect(mockResponsesCreate).toHaveBeenCalledTimes(1) // the SDK call really ran + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }) + it("normalizes a mid-stream abort on the Responses path to the standardized AbortError", async () => { + // A cancellation that surfaces after the /v1/responses stream has + // started must normalize to the standardized AbortError like the + // other wire formats, not leak the raw SDK rejection. + let capturedSignal: AbortSignal | undefined + let firstChunkDelivered = false + mockResponsesCreate.mockImplementation(async (_body: unknown, options: { signal?: AbortSignal }) => { + capturedSignal = options?.signal + return (async function* () { + yield { type: "response.output_text.delta", delta: "partial" } + // Runs only once the consumer has taken the first chunk and asked for the next + // one, which is the state the 25 ms sleep was approximating. + firstChunkDelivered = true + // Observable condition: the bridge aborted the internal controller. + await vitest.waitFor(() => expect(capturedSignal?.aborted).toBe(true), { + timeout: 1000, + interval: 5, + }) + throw new APIUserAbortError() + })() + }) + const controller = new AbortController() + const handler = new OpencodeGoHandler(lunaOptions) + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + + const consumed = collectStream( + handler.createMessage("sys", messages, { taskId: "test-task", abortSignal: controller.signal }), + ) + + // Observable condition: the first chunk has been consumed and the stream is asking for + // the next one; abort mid-stream from there rather than after a fixed 25 ms. + await vitest.waitFor(() => expect(firstChunkDelivered).toBe(true), { timeout: 1000, interval: 5 }) + controller.abort() + + const error = await consumed.then( + () => undefined, + (e: unknown) => e, + ) + expect(capturedSignal?.aborted).toBe(true) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }) + + it("completes a Responses stream when the caller passes no abort signal at all", async () => { + // The after-stream guard runs on every normal end of a Responses stream, including one where the + // caller never supplied a signal, so an absent signal must read as not-cancelled. + mockResponsesCreate.mockImplementationOnce(async () => + asyncStreamFrom([{ type: "response.output_text.delta", delta: "partial" }]), + ) + const handler = new OpencodeGoHandler(lunaOptions) + const chunks = await collectStream( + handler.createMessage( + "sys", + [{ role: "user", content: "Hi" }], + makeCreateMessageMetadata({ abortSignal: undefined }), + ), + ) + expect(chunks.filter((chunk) => chunk.type === "text").map((chunk) => chunk.text)).toEqual(["partial"]) + }) + + it("rejects a Responses stream that closes quietly after a cancel and still succeeds on a live signal", async () => { + // The pinned OpenAI SDK detaches the bridge and returns from SSE iteration when a pending read + // raises AbortError, so the Responses processor sees an ordinary end of stream. A cancelled turn + // must reject with the contract AbortError instead of resolving as a completed turn, while the + // same quiet end with a live caller signal is still a normal completion. + const events = [{ type: "response.output_text.delta", delta: "partial" }] + const handler = new OpencodeGoHandler(lunaOptions) + const cancelled = new AbortController() + mockResponsesCreate.mockImplementationOnce(async () => asyncStreamFrom(events)) + const iterator = handler + .createMessage( + "sys", + [{ role: "user", content: "Hi" }], + makeCreateMessageMetadata({ abortSignal: cancelled.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "text", text: "partial" }) + // Observable condition: the consumer holds the partial text, cancels, and the mock iterator then + // reports done instead of throwing - the quiet close the SDK actually performs. + cancelled.abort() + + const settled = await iterator.next().then( + (result) => result.value, + (error: unknown) => error, + ) + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Opencode Go request was aborted") + + // Same fixture, live signal: the quiet end is an ordinary completion. + mockResponsesCreate.mockImplementationOnce(async () => asyncStreamFrom(events)) + const live = new AbortController() + const chunks = await collectStream( + handler.createMessage( + "sys", + [{ role: "user", content: "Hi" }], + makeCreateMessageMetadata({ abortSignal: live.signal }), + ), + ) + expect(chunks.filter((chunk) => chunk.type === "text").map((chunk) => chunk.text)).toEqual(["partial"]) + expect(live.signal.aborted).toBe(false) + }) + + // One transport read can decode both the text delta and the terminal event. Both cancel + // checks are exercised against this same buffered payload: the pull-time test below cancels + // before the second read is requested, the read-gated test after it releases that read only + // once the cancel has landed, so the pre-pull check has already been evaluated by then. + // The shape a source iterator's close() resolves with, shared by the two cancel-window tests. + type CloseOutcome = Promise<{ done: boolean; value: undefined }> + + const bufferedCompletionEvents = [ + { type: "response.output_text.delta", delta: "partial" }, + { + type: "response.completed", + response: { usage: { input_tokens: 10, output_tokens: 5 } }, + }, + ] + + it("rejects a Responses turn whose buffered completion event arrives after the caller cancels", async () => { + // One transport read can decode both the text delta and the terminal event: the + // consumer takes the delta, cancels, and the processor would otherwise pull the + // already-buffered response.completed and hand over its usage chunk for a turn + // nobody is reading any more. The pull-time check must end the stream there, so the + // cancelled turn rejects with the contract error and reports no terminal cost. + const events = bufferedCompletionEvents + const handler = new OpencodeGoHandler(lunaOptions) + const cancelled = new AbortController() + mockResponsesCreate.mockImplementationOnce(async () => asyncStreamFrom(events)) + const iterator = handler + .createMessage( + "sys", + [{ role: "user", content: "Hi" }], + makeCreateMessageMetadata({ abortSignal: cancelled.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "text", text: "partial" }) + // Observable consequence: the next pull must not deliver the buffered terminal event. + cancelled.abort() + + const next = await iterator.next().then( + (result) => ({ + done: result.done, + value: result.value as unknown, + rejected: undefined as unknown, + }), + (error: unknown) => ({ done: false, value: undefined, rejected: error }), + ) + expect(next.rejected).toMatchObject({ name: "AbortError" }) + expect((next.rejected as Error).message).toBe("The Opencode Go request was aborted") + expect(next.value).toBeUndefined() + + // Same buffered fixture, live caller signal: an ordinary completion, cost included. + mockResponsesCreate.mockImplementationOnce(async () => asyncStreamFrom(events)) + const live = new AbortController() + const chunks = await collectStream( + handler.createMessage( + "sys", + [{ role: "user", content: "Hi" }], + makeCreateMessageMetadata({ abortSignal: live.signal }), + ), + ) + expect(chunks.filter((chunk) => chunk.type === "text").map((chunk) => chunk.text)).toEqual(["partial"]) + expect(chunks.some((chunk) => chunk.type === "usage" && chunk.totalCost !== undefined)).toBe(true) + expect(live.signal.aborted).toBe(false) + }) + + it("rejects a Responses turn whose buffered completion event resolves after the pull already started", async () => { + // Same buffered payload as the test above, with the transport read held open past the cancel: + // for this pull the pre-pull check has already been evaluated, so only a check in front of + // forwarding the processed chunk can keep the terminal usage chunk from a consumer that has + // given up on the turn. + type ReadGatedResponsesIterator = { + next: () => Promise<{ done: boolean; value: unknown }> + return: (value?: unknown) => CloseOutcome + [Symbol.asyncIterator]: () => ReadGatedResponsesIterator + } + const events = bufferedCompletionEvents + let releaseRead!: (event: unknown) => void + const readGate = new Promise((resolve) => { + releaseRead = resolve + }) + let index = 0 + const read = vitest.fn((): Promise<{ done: boolean; value: unknown }> => { + const event = events[index] + if (event === undefined) { + return Promise.resolve({ done: true, value: undefined }) + } + index += 1 + // The terminal event is the read the cancel outlives. + return index === events.length + ? readGate.then((value) => ({ done: false, value })) + : Promise.resolve({ done: false, value: event }) + }) + const closeSpy = vitest.fn((): CloseOutcome => Promise.resolve({ done: true, value: undefined })) + const source: ReadGatedResponsesIterator = { + next: read, + return: closeSpy, + [Symbol.asyncIterator]: () => source, + } + const handler = new OpencodeGoHandler(lunaOptions) + const cancelled = new AbortController() + mockResponsesCreate.mockImplementationOnce(async () => source) + const iterator = handler + .createMessage( + "sys", + [{ role: "user", content: "Hi" }], + makeCreateMessageMetadata({ abortSignal: cancelled.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "text", text: "partial" }) + const pending = iterator.next().then( + (result) => ({ + done: result.done, + value: result.value as unknown, + rejected: undefined as unknown, + }), + (error: unknown) => ({ done: false, value: undefined, rejected: error }), + ) + // Observable condition: the second read has been requested and has not settled, so the + // pre-pull check has already run for it when the cancel lands. + await vitest.waitFor(() => expect(read).toHaveBeenCalledTimes(2), { timeout: 1000, interval: 5 }) + cancelled.abort() + releaseRead(events[events.length - 1]) + const outcome = await pending + expect(outcome.rejected).toMatchObject({ name: "AbortError" }) + expect((outcome.rejected as Error).message).toBe("The Opencode Go request was aborted") + expect(outcome.value).toBeUndefined() // no terminal cost for a turn the caller gave up on + // The SDK iterator is still closed: once by the transform unwinding the delegated loop and + // once by the provider's own cleanup, which is the count the early-exit test pins as well. + expect(closeSpy).toHaveBeenCalledTimes(2) + }) + + it("rejects a cancelled Responses turn when the cancel lands while the iterator is closing", async () => { + // The provider's finally block awaits the SDK iterator's return(), which makes the cleanup a + // suspension point of its own. A cancel that lands there used to settle the waiting pull as an + // ordinary end of stream, because the quiet-close check ran before the cleanup instead of after + // it. The check still runs outside the finally, so a stream error already in flight keeps its + // own failure instead of being replaced by the abort. + type PendingCloseResponsesIterator = { + next: () => Promise<{ done: boolean; value: unknown }> + return: (value?: unknown) => CloseOutcome + [Symbol.asyncIterator]: () => PendingCloseResponsesIterator + } + let releaseCleanup!: () => void + const cleanupGate = new Promise((resolve) => { + releaseCleanup = resolve }) + const events = [{ type: "response.output_text.delta", delta: "partial" }] + let index = 0 + const closeSpy = vitest.fn((): CloseOutcome => cleanupGate.then(() => ({ done: true, value: undefined }))) + const source: PendingCloseResponsesIterator = { + next: () => { + const event = events[index] + if (event === undefined) { + return Promise.resolve({ done: true, value: undefined }) + } + index += 1 + return Promise.resolve({ done: false, value: event }) + }, + return: closeSpy, + [Symbol.asyncIterator]: () => source, + } + const handler = new OpencodeGoHandler(lunaOptions) + const cancelled = new AbortController() + mockResponsesCreate.mockImplementationOnce(async () => source) + const iterator = handler + .createMessage( + "sys", + [{ role: "user", content: "Hi" }], + makeCreateMessageMetadata({ abortSignal: cancelled.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "text", text: "partial" }) + const settled = iterator.next().then( + (result) => ({ + done: result.done, + value: result.value as unknown, + rejected: undefined as unknown, + }), + (error: unknown) => ({ done: false, value: undefined, rejected: error }), + ) + // Observable condition: the provider has reached its cleanup await - the close has been + // requested and the waiting pull has not settled, which is the window this test is about. + await vitest.waitFor(() => expect(closeSpy).toHaveBeenCalledTimes(1), { timeout: 1000, interval: 5 }) + cancelled.abort() + releaseCleanup() + const outcome = await settled + expect(outcome.rejected).toMatchObject({ name: "AbortError" }) + expect((outcome.rejected as Error).message).toBe("The Opencode Go request was aborted") + expect(outcome.done).toBe(false) // a cancel during cleanup is not a normal end of stream + expect(outcome.value).toBeUndefined() + }) + + it("wraps non-abort Responses pre-stream failures with the Opencode Go prefix", async () => { + // A non-abort rejection from responses.create (e.g. an upstream 500) + // must be wrapped like the other wire formats, not normalized. + mockResponsesCreate.mockRejectedValue(new Error("boom")) + const handler = new OpencodeGoHandler(lunaOptions) + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + + await expect(collectStream(handler.createMessage("sys", messages))).rejects.toThrow( + "Opencode Go completion error: boom", + ) + }) + + it("detaches the bridged abort listener from the Responses-format path", async () => { + // The Responses branch must detach the bridged listener on the way + // out like the other formats: a task-scoped signal must not + // accumulate one listener per request. + const handler = new OpencodeGoHandler(lunaOptions) + const controller = new AbortController() + const removeListenerSpy = vi.spyOn(controller.signal, "removeEventListener") + const addEventListenerSpy = vi.spyOn(controller.signal, "addEventListener") + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + + const chunks = await collectStream( + handler.createMessage("sys", messages, { taskId: "test-task", abortSignal: controller.signal }), + ) + expect(chunks).toContainEqual({ type: "text", text: "Hello" }) + // The model-resolution race registers its own "abort" listener on the same signal and detaches + // it before the request starts, so there are exactly two registrations here and the bridge's is + // the second one. Naming it by position — and requiring it to differ from the race's callback — + // means a bridge that stopped registering its own listener cannot satisfy the assertions below + // by falling back on the race's. + const abortAddCalls = addEventListenerSpy.mock.calls.filter(([event]) => event === "abort") + const abortRemoveCalls = removeListenerSpy.mock.calls + .filter(([event]) => event === "abort") + .map(([, listener]) => listener) + expect(abortAddCalls.length).toBe(2) + const [raceCall, bridgeCall] = abortAddCalls + const addedListener = bridgeCall?.[1] + expect(typeof addedListener).toBe("function") + expect(addedListener).not.toBe(raceCall?.[1]) + expect(addEventListenerSpy).toHaveBeenCalledWith("abort", addedListener, { once: true }) + // Both listeners are gone by reference: the lookup race's first (its cleanup ran before the + // request), the bridge's last. + expect(abortRemoveCalls).toEqual([raceCall?.[1], addedListener]) + expect(controller.signal.aborted).toBe(false) }) it("falls back to a stable per-instance session ID when no taskId is provided", async () => { @@ -1074,6 +3068,54 @@ describe("OpencodeGoHandler", () => { expect(iterator.return).toHaveBeenCalledTimes(2) }) + type NextOnlyResponsesIterator = { + next: () => Promise<{ done: boolean; value: unknown }> + [Symbol.asyncIterator]: () => NextOnlyResponsesIterator + } + it("keeps the Responses stream callable when the source iterator has no return method", async () => { + // The shared transform accepts any AsyncIterable, so the provider must not assume + // the captured iterator implements return: a plain next-only iterator has to stream, + // honour a cancel with the contract AbortError, and still clean up without throwing. + const events = [ + { type: "response.output_text.delta", delta: "partial" }, + { + type: "response.completed", + response: { usage: { input_tokens: 10, output_tokens: 5 } }, + }, + ] + let index = 0 + const sourceIterator: NextOnlyResponsesIterator = { + next: () => + index < events.length + ? Promise.resolve({ done: false, value: events[index++] }) + : Promise.resolve({ done: true, value: undefined }), + [Symbol.asyncIterator]() { + return this + }, + } + mockResponsesCreate.mockResolvedValue(sourceIterator) + const handler = new OpencodeGoHandler(lunaOptions) + const cancelled = new AbortController() + const iterator = handler + .createMessage( + "sys", + [{ role: "user", content: "Hi" }], + makeCreateMessageMetadata({ abortSignal: cancelled.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "text", text: "partial" }) + cancelled.abort() + + const settled = await iterator.next().then( + (result) => result.value, + (error: unknown) => error, + ) + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Opencode Go request was aborted") + }) + it("preserves the stream error when iterator cleanup fails", async () => { const circular: { self?: unknown } = {} circular.self = circular @@ -1138,7 +3180,13 @@ describe("OpencodeGoHandler", () => { await vitest.waitFor(() => expect(mockResponsesCreate).toHaveBeenCalled()) controller.abort() - await expect(nextPromise).rejects.toBe(abortDouble) + // The read rejection lands on an aborted request signal, so the + // Responses branch normalizes it to the standardized AbortError + // (series standard) while still closing the in-flight iterator. + await expect(nextPromise).rejects.toMatchObject({ + name: "AbortError", + message: "The Opencode Go request was aborted", + }) expect(iterator.return).toHaveBeenCalled() }) @@ -1477,7 +3525,10 @@ describe("OpencodeGoHandler", () => { await expect(handler.completePrompt("ping")).rejects.toBe("completion failure") }) - it("rejects non-streaming Responses completion when the abort signal fires", async () => { + it("normalizes an aborted non-streaming Responses completion to the standardized AbortError", async () => { + // A caller-initiated cancellation of a /v1/responses completion must + // surface as a DOM-standard AbortError (series standard), not the raw + // rejection or a wrapped completion error. const controller = new AbortController() // One double object here too. The Responses completion path wraps every failure // (`opencode-go.ts:439` throws `Opencode Go completion error: `), so the @@ -1496,11 +3547,45 @@ describe("OpencodeGoHandler", () => { controller.abort() await expect(completion).rejects.toMatchObject({ - name: "Error", - message: `Opencode Go completion error: ${abortDouble.message}`, + name: "AbortError", + message: "The Opencode Go request was aborted", }) }) + it.each([ + ["anthropic", { opencodeGoApiKey: "test-key", opencodeGoModelId: "qwen3.7-max" }, mockAnthropicCreate], + ["responses", { opencodeGoApiKey: "test-key", opencodeGoModelId: "gpt-5.6-luna" }, mockResponsesCreate], + ["openai", { opencodeGoApiKey: "test-key", opencodeGoModelId: "glm-5.1" }, mockCreate], + ] as const)( + "completePrompt pins the caller signal and rejects with AbortError for the %s format", + async (_format, options, requestMock) => { + clearAllMocks() + const controller = new AbortController() + const pending = new Promise((_resolve, reject) => { + controller.signal.addEventListener( + "abort", + () => reject(Object.assign(new Error("aborted"), { name: "AbortError" })), + { once: true }, + ) + }) + requestMock.mockReturnValue(pending) + const handler = new OpencodeGoHandler(options) + const completion = handler.completePrompt("ping", { abortSignal: controller.signal }) + + await vitest.waitFor(() => expect(requestMock).toHaveBeenCalled()) + const requestOptions = requestMock.mock.calls[0][1] as { signal?: AbortSignal } | undefined + // With no timeoutMs the merged signal is the caller signal itself. + expect(requestOptions?.signal).toBe(controller.signal) + + controller.abort() + expect(requestOptions?.signal?.aborted).toBe(true) + await expect(completion).rejects.toMatchObject({ + name: "AbortError", + message: "The Opencode Go request was aborted", + }) + }, + ) + it("completePrompt calls responses.create and returns output_text", async () => { mockResponsesCreate.mockResolvedValue({ output_text: "Hello!" }) const handler = new OpencodeGoHandler(lunaOptions) @@ -1561,12 +3646,87 @@ describe("OpencodeGoHandler", () => { await handler.completePrompt("ping", { abortSignal: controller.signal }) expect(mockResponsesCreate.mock.calls[0][1]).toEqual({ - signal: controller.signal, + signal: expect.any(AbortSignal), + headers: { "x-opencode-session": expect.any(String) }, + }) + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("forwards a positive timeoutMs to the non-streaming Responses request", async () => { + mockResponsesCreate.mockResolvedValue({ output_text: "Hello!" }) + const handler = new OpencodeGoHandler(lunaOptions) + + await handler.completePrompt("ping", { timeoutMs: 5_000 }) + + expect(mockResponsesCreate.mock.calls[0][1]).toEqual({ + signal: expect.any(AbortSignal), + timeout: 5_000, headers: { "x-opencode-session": expect.any(String) }, }) expect(mockCreate).not.toHaveBeenCalled() }) + it("omits the timeout option from the Responses request when timeoutMs is 0", async () => { + // The OpenAI SDK treats timeout: 0 as an immediate abort, so the + // "disabled" value must never be forwarded — assert the absence of + // the option (a forwarded timeout: 0 would fail this assertion). + mockResponsesCreate.mockResolvedValue({ output_text: "Hello!" }) + const handler = new OpencodeGoHandler(lunaOptions) + + await handler.completePrompt("ping", { timeoutMs: 0 }) + + const callOptions = mockResponsesCreate.mock.calls[0][1] as Record + expect(callOptions).not.toHaveProperty("timeout") + expect(callOptions).not.toHaveProperty("signal") + expect(mockCreate).not.toHaveBeenCalled() + }) + it("omits the timeout option from the Responses request when timeoutMs is negative", async () => { + // The disabled-timeout contract covers every non-positive value, not just 0. + mockResponsesCreate.mockResolvedValue({ output_text: "Hello!" }) + const handler = new OpencodeGoHandler(lunaOptions) + + await handler.completePrompt("ping", { timeoutMs: -1 }) + + const callOptions = mockResponsesCreate.mock.calls[0][1] as Record + expect(callOptions).not.toHaveProperty("timeout") + expect(callOptions).not.toHaveProperty("signal") + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("preserves abort identity on the Responses completion path", async () => { + // Delivered from the SDK mock with no caller signal: the pre-flight guard cannot fire, so + // the mock really runs and its APIUserAbortError is what this path must normalize into the + // DOM-standard AbortError, not a wrapped completion error. + mockResponsesCreate.mockImplementationOnce(async () => { + throw await sdkAbortError() + }) + const handler = new OpencodeGoHandler(lunaOptions) + + const error = await handler.completePrompt("ping").then( + () => undefined, + (e: unknown) => e, + ) + expect(mockResponsesCreate).toHaveBeenCalledTimes(1) // the SDK call ran, not the guard + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }) + + it("surfaces Responses request timeouts as an AbortError in completePrompt", async () => { + // Emulate the OpenAI SDK: when the request timeout fires, the SDK + // surfaces APIConnectionTimeoutError ("Request timed out.") — the + // series standard maps it to the same AbortError identity as caller + // cancellations. + mockResponsesCreate.mockRejectedValue(new APIConnectionTimeoutError()) + const handler = new OpencodeGoHandler(lunaOptions) + + const error = await handler.completePrompt("ping").then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }) + it("completePrompt sends the per-instance session ID to the Responses endpoint", async () => { mockResponsesCreate.mockResolvedValue({ output_text: "Hello!" }) const handler = new OpencodeGoHandler(lunaOptions) @@ -1764,4 +3924,392 @@ describe("OpencodeGoHandler", () => { expect(isOpencodeGoAnthropicFormatModel("some-unknown-model")).toBe(false) }) }) + + describe("completePrompt catch classification", () => { + it.each([ + { + label: "Anthropic", + options: { ...mockOptions, opencodeGoModelId: "qwen3.7-max" }, + arm: (controller: AbortController) => + mockAnthropicCreate.mockImplementationOnce(() => { + controller.abort() + throw new Error("boom") + }), + sdkCall: mockAnthropicCreate, + }, + { + label: "Responses", + options: { ...mockOptions, opencodeGoModelId: "gpt-5.6-luna" }, + arm: (controller: AbortController) => + mockResponsesCreate.mockImplementationOnce(() => { + controller.abort() + throw new Error("boom") + }), + sdkCall: mockResponsesCreate, + }, + { + label: "Chat Completions", + options: mockOptions, + arm: (controller: AbortController) => + mockCreate.mockImplementationOnce(() => { + controller.abort() + throw new Error("boom") + }), + sdkCall: mockCreate, + }, + ])( + "classifies a caller cancel that lands inside the $label SDK call as an AbortError", + async ({ options, arm, sdkCall }) => { + // The cancel arrives during the SDK call and the SDK rejects with a generic error that + // carries no abort shape, before any abort race is installed: only the caller signal can + // classify it. The SDK-error tests cannot reach this arm because their mock decides the + // classification instead of the signal. + const controller = new AbortController() + arm(controller) + const handler = new OpencodeGoHandler(options) + + const error = await handler.completePrompt("ping", { abortSignal: controller.signal }).then( + () => undefined, + (e: unknown) => e, + ) + expect(sdkCall).toHaveBeenCalledTimes(1) // the mock ran and threw the generic "boom" + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }, + ) + + it.each([ + { + label: "Anthropic", + options: { ...mockOptions, opencodeGoModelId: "qwen3.7-max" }, + arm: () => + mockAnthropicCreate.mockImplementationOnce(() => { + throw new AnthropicTimeoutError() + }), + sdkCall: mockAnthropicCreate, + }, + { + label: "Responses", + options: { ...mockOptions, opencodeGoModelId: "gpt-5.6-luna" }, + arm: () => + mockResponsesCreate.mockImplementationOnce(() => { + throw new APIConnectionTimeoutError() + }), + sdkCall: mockResponsesCreate, + }, + { + label: "Chat Completions", + options: mockOptions, + arm: () => + mockCreate.mockImplementationOnce(() => { + throw new APIConnectionTimeoutError() + }), + sdkCall: mockCreate, + }, + ])( + "classifies the $label SDK's own timeout error without a provider deadline", + async ({ options, arm, sdkCall }) => { + // No timeoutMs and nothing aborted: the provider deadline is not what classifies this + // failure, the SDK timeout class is. The deadline tests cannot isolate that arm because + // their signal is already aborted by the time the SDK rejects, so the signal arm of the + // condition would answer first. + arm() + const handler = new OpencodeGoHandler(options) + + const error = await handler.completePrompt("ping").then( + () => undefined, + (e: unknown) => e, + ) + expect(sdkCall).toHaveBeenCalledTimes(1) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }, + ) + }) + describe("completePrompt shared timeout deadline", () => { + // Stands in for a slow catalog lookup: the typed fetcher stays pending until the test releases + // it, so "the deadline fired first" is an observable state the test can assert, not a race + // against a fixed delay the test cannot see. The record it eventually resolves with is one + // the provider can actually use, so the lookup could have continued had the deadline lost. + const gateModelLookup = () => { + let release!: () => void + let pending = true + const lookup = new Promise>>((resolve) => { + release = () => { + pending = false + resolve({ + "glm-5.1": { ...opencodeGoModels["glm-5.1"] }, + "qwen3.7-max": { ...opencodeGoModels["qwen3.7-max"] }, + "gpt-5.6-luna": { ...opencodeGoModels["gpt-5.6-luna"] }, + }) + } + }) + vitest.mocked(getModels).mockImplementationOnce(() => lookup) + return { isPending: () => pending, release } + } + + it.each([ + [ + "Anthropic", + () => new OpencodeGoHandler({ ...mockOptions, opencodeGoModelId: "qwen3.7-max" }), + mockAnthropicCreate, + ], + [ + "Responses", + () => new OpencodeGoHandler({ ...mockOptions, opencodeGoModelId: "gpt-5.6-luna" }), + mockResponsesCreate, + ], + ["OpenAI", () => new OpencodeGoHandler(mockOptions), mockCreate], + ])("bounds %s model resolution by the request timeout", async (_label, makeHandler, createMock) => { + const handler = makeHandler() as OpencodeGoHandler + const { isPending, release } = gateModelLookup() + + try { + const error = await handler.completePrompt("ping", { timeoutMs: 20 }).catch((caught: unknown) => caught) + + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + // The gate is the typed catalog fetcher, so this asserts the lookup really started. + expect(vitest.mocked(getModels)).toHaveBeenCalledTimes(1) + // The lookup was still parked when the deadline fired, which is the condition the old + // 80 ms delay was approximating; it must still be pending here, and no completion + // request may have started. + expect(isPending()).toBe(true) + expect(createMock).not.toHaveBeenCalled() + } finally { + // Release the parked lookup so the test leaves no pending promise behind. + release() + } + }) + + it("hands the dispatched request the cancellation scope that bounded model resolution", async () => { + // No timeoutMs: mergeAbortSignalAndTimeout returns the caller's own signal, so the object + // the SDK receives is observable. The catalog fetcher is parked first, so dispatch can only + // happen after the lookup ran under that scope; a provider that built a fresh cancellation + // scope at SDK dispatch would hand over a different object and a later deadline. + const { isPending, release } = gateModelLookup() + let sdkSignal: AbortSignal | undefined + let sdkSettled = false + mockCreate.mockImplementation((_params: unknown, options?: { signal?: AbortSignal }) => { + sdkSignal = options?.signal + const pending = new Promise(() => {}) + void pending.then(() => { + sdkSettled = true + }) + return pending + }) + const handler = new OpencodeGoHandler(mockOptions) as OpencodeGoHandler + const controller = new AbortController() + const settled = handler.completePrompt("ping", { abortSignal: controller.signal }).then( + () => undefined, + (caught: unknown) => caught, + ) + + try { + await vitest.waitFor(() => expect(vitest.mocked(getModels)).toHaveBeenCalledTimes(1), { + timeout: 1000, + interval: 5, + }) + // The lookup is still parked: nothing has been dispatched yet. + expect(isPending()).toBe(true) + release() + await vitest.waitFor(() => expect(sdkSignal).toBeInstanceOf(AbortSignal), { + timeout: 1000, + interval: 5, + }) + } finally { + release() + } + + // Same scope, not a re-armed one: reference identity is the only assertion a fresh + // dispatch-phase controller can fail, and it needs no timing. + expect(sdkSignal).toBe(controller.signal) + expect(sdkSignal?.aborted).toBe(false) + controller.abort() + // No await between these two reads: the cancel has to reach the SDK-facing signal + // when it happens, not by the time the completion settles. + expect(sdkSignal?.aborted).toBe(true) + + const error = await settled + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + // The SDK promise never settled, so this rejection can only come from the provider's + // race against the scope the lookup already used. + expect(sdkSettled).toBe(false) + expect(isPending()).toBe(false) + }) + + it.each([ + [ + "Anthropic", + () => new OpencodeGoHandler({ ...mockOptions, opencodeGoModelId: "qwen3.7-max" }), + mockAnthropicCreate, + ], + ["Chat Completions", () => new OpencodeGoHandler(mockOptions), mockCreate], + [ + "Responses", + () => new OpencodeGoHandler({ ...mockOptions, opencodeGoModelId: "gpt-5.6-luna" }), + mockResponsesCreate, + ], + ])( + "rejects at the deadline even while the SDK retry delay keeps the %s request pending", + async (_label, makeHandler, createMock) => { + // The SDK's internal retry backoff is not covered by the request signal; without the + // rejectOnAbort race this promise would never settle and the test would time out. + let requestSignal: AbortSignal | undefined + let sdkSettled = false + createMock.mockImplementation((_params: unknown, options?: { signal?: AbortSignal }) => { + requestSignal = options?.signal + const pending = new Promise(() => {}) + void pending.then(() => { + sdkSettled = true + }) + return pending + }) + const handler = makeHandler() as OpencodeGoHandler + + const error = await handler.completePrompt("ping", { timeoutMs: 30 }).catch((caught: unknown) => caught) + + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + // The deadline's own effect plus the identity the race has to carry, not elapsed time: neither + // reading changes when a worker stalls, which is what the removed 1 s bound hinged on. + expect(requestSignal).toBeInstanceOf(AbortSignal) + expect(requestSignal?.aborted).toBe(true) + expect(sdkSettled).toBe(false) + }, + ) + }) + + // Regression evidence that every completePrompt wire format actually applies the shared + // hasUsableTimeout/toSdkTimeoutMs contract: a disabled budget (NaN, Infinity, negative) must + // never reach the SDK, and a usable one must arrive rounded up (the SDKs reject fractional + // timeouts) and capped at the largest delay a Node timer accepts. Every row also carries the + // caller's signal, so dropping the budget can never drop the cancellation scope. + describe("completePrompt timeout option per wire format", () => { + const timeoutRows: { label: string; timeoutMs: number; expected: number | undefined }[] = [ + { label: "NaN", timeoutMs: Number.NaN, expected: undefined }, + { label: "Infinity", timeoutMs: Number.POSITIVE_INFINITY, expected: undefined }, + { label: "negative", timeoutMs: -5, expected: undefined }, + { label: "fractional", timeoutMs: 12.5, expected: 13 }, + { label: "above the Node timer cap", timeoutMs: MAX_TIMEOUT_MS + 1, expected: MAX_TIMEOUT_MS }, + ] + + function assertTimeoutOption( + requestOptions: { timeout?: number; signal?: AbortSignal } | undefined, + expected: number | undefined, + callerSignal: AbortSignal, + ): void { + if (expected === undefined) { + expect(Object.keys(requestOptions ?? {})).not.toContain("timeout") + // With no usable timer the merged signal must be the caller's own signal: reference identity is + // the only assertion a deadline-only signal cannot satisfy. + expect(requestOptions?.signal).toBe(callerSignal) + } else { + expect(requestOptions?.timeout).toBe(expected) + // With a usable timer the request runs under a merged deadline rather than the caller's own + // signal, and the caller's cancel still has to reach it (pinned by the cancel test below). + expect(requestOptions?.signal).toBeInstanceOf(AbortSignal) + expect(requestOptions?.signal).not.toBe(callerSignal) + } + } + + it.each(timeoutRows)( + "applies the $label timeoutMs to the Anthropic messages request options", + async ({ timeoutMs, expected }) => { + mockAnthropicCreate.mockResolvedValue({ content: [{ type: "text", text: "response" }] }) + const controller = new AbortController() + const handler = new OpencodeGoHandler({ ...mockOptions, opencodeGoModelId: "qwen3.7-max" }) + await handler.completePrompt("ping", { abortSignal: controller.signal, timeoutMs }) + const call = mockAnthropicCreate.mock.calls[mockAnthropicCreate.mock.calls.length - 1] + assertTimeoutOption( + call[1] as { timeout?: number; signal?: AbortSignal } | undefined, + expected, + controller.signal, + ) + }, + ) + + it.each(timeoutRows)( + "applies the $label timeoutMs to the Responses request options", + async ({ timeoutMs, expected }) => { + mockResponsesCreate.mockResolvedValue({ output_text: "response" }) + const controller = new AbortController() + const handler = new OpencodeGoHandler({ ...mockOptions, opencodeGoModelId: "gpt-5.6-luna" }) + await handler.completePrompt("ping", { abortSignal: controller.signal, timeoutMs }) + const call = mockResponsesCreate.mock.calls[mockResponsesCreate.mock.calls.length - 1] + assertTimeoutOption( + call[1] as { timeout?: number; signal?: AbortSignal } | undefined, + expected, + controller.signal, + ) + }, + ) + + it.each(timeoutRows)( + "applies the $label timeoutMs to the chat completions request options", + async ({ timeoutMs, expected }) => { + mockCreate.mockResolvedValue({ choices: [{ message: { content: "response" } }] }) + const controller = new AbortController() + const handler = new OpencodeGoHandler(mockOptions) + await handler.completePrompt("ping", { abortSignal: controller.signal, timeoutMs }) + const call = mockCreate.mock.calls[mockCreate.mock.calls.length - 1] + assertTimeoutOption( + call[1] as { timeout?: number; signal?: AbortSignal } | undefined, + expected, + controller.signal, + ) + }, + ) + + it.each([ + [ + "Anthropic", + () => new OpencodeGoHandler({ ...mockOptions, opencodeGoModelId: "qwen3.7-max" }), + mockAnthropicCreate, + ], + [ + "Responses", + () => new OpencodeGoHandler({ ...mockOptions, opencodeGoModelId: "gpt-5.6-luna" }), + mockResponsesCreate, + ], + ["Chat Completions", () => new OpencodeGoHandler(mockOptions), mockCreate], + ])( + "settles on a caller cancel while a usable timeout is armed for the %s request", + async (_label, makeHandler, createMock) => { + // A usable budget must not disable caller cancellation and a live-but-unusable signal must not + // stand in for the budget: park the request under the merged deadline signal, cancel the caller + // well before the deadline, and require the cancel to reach that signal. + let sdkSignal: AbortSignal | undefined + createMock.mockImplementationOnce((_params: unknown, options?: { signal?: AbortSignal }) => { + sdkSignal = options?.signal + return new Promise(() => {}) + }) + const handler = makeHandler() as OpencodeGoHandler + const controller = new AbortController() + const settled = handler + .completePrompt("ping", { abortSignal: controller.signal, timeoutMs: 5000 }) + .then( + () => undefined, + (e: unknown) => e, + ) + + await vitest.waitFor(() => expect(sdkSignal).toBeInstanceOf(AbortSignal), { + timeout: 1000, + interval: 5, + }) + // Observable condition: the request runs under a merged deadline signal, not the caller's own. + expect(sdkSignal).not.toBe(controller.signal) + expect(sdkSignal?.aborted).toBe(false) + controller.abort() + // Asserted with no await in between: caller cancellation has to reach the signal the SDK + // received at the moment of the cancel, not by the time the completion settles. + expect(sdkSignal?.aborted).toBe(true) + + const error = await settled + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Opencode Go request was aborted") + }, + ) + }) }) diff --git a/src/api/providers/__tests__/unbound.spec.ts b/src/api/providers/__tests__/unbound.spec.ts index c0f142d8ad..fd4d68488f 100644 --- a/src/api/providers/__tests__/unbound.spec.ts +++ b/src/api/providers/__tests__/unbound.spec.ts @@ -1,20 +1,30 @@ import { Anthropic } from "@anthropic-ai/sdk" -import OpenAI from "openai" +import OpenAI, { APIConnectionTimeoutError, APIUserAbortError } from "openai" import { unboundDefaultModelInfo } from "@roo-code/types" import { UnboundHandler } from "../unbound" +import { getModels } from "../fetchers/modelCache" import { asyncStreamFrom, collectStream } from "../../../test-utils/stream" import { clearAllMocks } from "../../../test-utils/reset" +import { makeCreateMessageMetadata } from "../../../test-utils/api" -vi.mock("openai", () => { - const createMock = vi.fn() +// Single hoisted mock shared by the `openai` factory and every test so tests +// can configure the SDK `create` call without untyped access casts. +const sharedMockCreate = vi.hoisted(() => vi.fn()) + +// The real SDK error classes are re-exported alongside the mocked client so +// tests can emulate the SDK's abort/timeout rejections (APIUserAbortError, +// APIConnectionTimeoutError) and the provider's instanceof checks resolve. +vi.mock("openai", async () => { + const actual = await vi.importActual("openai") return { + ...actual, default: vi.fn(function () { return { chat: { completions: { - create: createMock, + create: sharedMockCreate, }, }, } @@ -40,6 +50,19 @@ vi.mock("../fetchers/modelCache", () => ({ }), })) +// A bounded observation window built on vi.waitFor: it polls while its callback throws, so a +// callback that always throws keeps it polling for the whole window (measured on vitest 4.1.12: +// a callback that only returns a value settles immediately instead of waiting). It also doubles +// as the "still pending" sentinel in the cancel races below, which is what pins the race itself. +async function observeWindow(ms: number): Promise<"pending"> { + return vi + .waitFor(() => expect(false).toBe(true), { timeout: ms, interval: 10 }) + .then( + () => "pending", + () => "pending", + ) +} + describe("UnboundHandler", () => { beforeEach(() => { clearAllMocks() @@ -181,7 +204,178 @@ describe("UnboundHandler", () => { mode: "architect", }, }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), + ) + }) + + it("wraps non-abort pre-stream failures via handleOpenAIError", async () => { + // A non-abort rejection from create() (e.g. an upstream 500) must be + // routed through handleOpenAIError, not the AbortError normalization + // path: assert the wrapped identity and the preserved message. + sharedMockCreate.mockRejectedValue(new Error("upstream 500")) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const stream = handler.createMessage("system", [{ role: "user", content: "hi" }], { + taskId: "t", + tools: [], + }) + + const error = await collectStream(stream).then( + () => undefined, + (e: unknown) => e, ) + expect(error).toBeInstanceOf(Error) + expect((error as Error).message).toBe("Unbound completion error: upstream 500") + expect((error as Error).name).not.toBe("AbortError") + }) + + it("emits tool_call_partial chunks for native tool calls in the stream", async () => { + // Native tool calls arrive on delta.tool_calls and must be re-emitted + // as raw tool_call_partial chunks for NativeToolCallParser to assemble. + sharedMockCreate.mockResolvedValue( + asyncStreamFrom([ + { + choices: [ + { + delta: { + tool_calls: [ + { + index: 0, + id: "call_1", + type: "function", + function: { name: "get_weather", arguments: '{"city": "NYC"}' }, + }, + ], + }, + }, + ], + }, + { choices: [{ delta: { content: "done" } }], usage: { prompt_tokens: 1, completion_tokens: 1 } }, + ]), + ) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user", content: "hi" }], { + taskId: "t", + tools: [], + }), + ) + + // Ordered, not membership: a provider that emitted the tool partial twice, or emitted it after + // the text, must fail here. The fixture interleaves tool, text and usage frames. + expect(chunks).toEqual([ + { type: "tool_call_partial", index: 0, id: "call_1", name: "get_weather", arguments: '{"city": "NYC"}' }, + { type: "text", text: "done" }, + { + type: "usage", + inputTokens: 1, + outputTokens: 1, + cacheWriteTokens: 0, + cacheReadTokens: 0, + totalCost: expect.any(Number), + }, + ]) + }) + + it("skips frames without a first choice", async () => { + sharedMockCreate.mockResolvedValue( + asyncStreamFrom([{ choices: [] }, { choices: [{ delta: { content: "hi" } }] }]), + ) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user", content: "hi" }], { taskId: "t", tools: [] }), + ) + + expect(chunks).toEqual([{ type: "text", text: "hi" }]) + }) + + it("ignores a non-array tool_calls field on the delta", async () => { + sharedMockCreate.mockResolvedValue( + // Truthy and not an array: a bare null would also pass if the production guard dropped its + // Array.isArray check, because "null" in an if is simply falsy. A truthy non-array forces the + // iteration itself to be the thing the guard prevents. + asyncStreamFrom([{ choices: [{ delta: { content: "hi", tool_calls: {} } }] }]), + ) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user", content: "hi" }], { taskId: "t", tools: [] }), + ) + + expect(chunks).toEqual([{ type: "text", text: "hi" }]) + }) + + it("emits a partial tool call with undefined name and arguments when function is absent", async () => { + sharedMockCreate.mockResolvedValue( + asyncStreamFrom([{ choices: [{ delta: { tool_calls: [{ index: 0, id: "call_1" }] } }] }]), + ) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user", content: "hi" }], { taskId: "t", tools: [] }), + ) + + expect(chunks).toEqual([{ type: "tool_call_partial", index: 0, id: "call_1" }]) + }) + + it("keeps the last reported usage when a later frame carries none", async () => { + sharedMockCreate.mockResolvedValue( + asyncStreamFrom([ + { choices: [{ delta: { content: "hi" } }], usage: { prompt_tokens: 1, completion_tokens: 2 } }, + { choices: [{ delta: {} }] }, + ]), + ) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user", content: "hi" }], { taskId: "t", tools: [] }), + ) + + expect(chunks).toEqual([ + { type: "text", text: "hi" }, + expect.objectContaining({ type: "usage", inputTokens: 1, outputTokens: 2 }), + ]) + }) + + it("emits no usage chunk when the stream reports none", async () => { + sharedMockCreate.mockResolvedValue(asyncStreamFrom([{ choices: [{ delta: { content: "hi" } }] }])) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const chunks = await collectStream( + handler.createMessage("system", [{ role: "user", content: "hi" }], { taskId: "t", tools: [] }), + ) + + expect(chunks).toEqual([{ type: "text", text: "hi" }]) }) it("completePrompt returns the response text", async () => { @@ -201,7 +395,947 @@ describe("UnboundHandler", () => { expect.objectContaining({ messages: [{ role: "system", content: "Write a haiku" }], }), + {}, + ) + }) + + it("completePrompt hands the client a per-request bridge that settles on the caller cancel", async () => { + const controller = new AbortController() + let sdkSignal: AbortSignal | undefined + let abortedAtEntry = true + // A create that never settles reproduces the SDK waiting out a retry delay while the caller + // cancels. Observations are recorded here and asserted below: an assertion raised inside the + // mock would be normalized into the very AbortError this test expects. + sharedMockCreate.mockImplementation(async (_params: unknown, options?: { signal?: AbortSignal }) => { + sdkSignal = options?.signal + abortedAtEntry = sdkSignal?.aborted === true + return new Promise(() => {}) + }) + const handler = new UnboundHandler({ unboundApiKey: "test-key", unboundModelId: "openai/gpt-4o" }) + const completing = handler.completePrompt("Write a haiku", { abortSignal: controller.signal }).then( + () => "resolved", + (e: unknown) => e, ) + await vi.waitFor(() => expect(sharedMockCreate).toHaveBeenCalledTimes(1)) + controller.abort() + const error = await Promise.race([completing, observeWindow(200)]) + // The SDK adds an abort listener to whatever signal it is handed and never removes it, so it + // must receive the per-request bridge rather than the caller's long-lived signal. + expect(sdkSignal).toBeInstanceOf(AbortSignal) + expect(sdkSignal).not.toBe(controller.signal) + expect(abortedAtEntry).toBe(false) + expect(sdkSignal?.aborted).toBe(true) // the bridge forwarded the cancel to the SDK signal + expect(error).not.toBe("pending") // an in-flight completion must not outlive the cancel + expect(error).not.toBe("resolved") + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Unbound request was aborted") + }) + + it("completePrompt should pass timeout through to client", async () => { + sharedMockCreate.mockResolvedValue({ + choices: [{ message: { content: "completed text" } }], + }) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + await handler.completePrompt("Write a haiku", { timeoutMs: 5000 }) + // Without a caller signal there is no bridge to hand over, so the option object stays without a + // signal key rather than carrying an undefined one. + expect(Object.keys((sharedMockCreate.mock.calls.at(-1)?.[1] ?? {}) as object)).not.toContain("signal") + expect(sharedMockCreate).toHaveBeenCalledWith( + expect.objectContaining({ model: expect.any(String) }), + expect.objectContaining({ timeout: 5000 }), + ) + }) + + it("completePrompt should omit the timeout option when timeoutMs is 0", async () => { + // The OpenAI SDK treats timeout: 0 as an immediate abort, so the + // "disabled" value must never be forwarded — assert the absence of + // the option (a forwarded timeout: 0 would fail this assertion). + sharedMockCreate.mockResolvedValue({ + choices: [{ message: { content: "completed text" } }], + }) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + await handler.completePrompt("Write a haiku", { timeoutMs: 0 }) + const call = sharedMockCreate.mock.calls[sharedMockCreate.mock.calls.length - 1] + const requestOptions = call[1] as { timeout?: number } | undefined + expect(requestOptions).not.toHaveProperty("timeout") + }) + + it("completePrompt should omit the timeout option for a negative timeoutMs and keep the bridged signal", async () => { + // -5 is a disabled budget, not a five-second one: hasUsableTimeout rejects every non-positive + // value, so nothing may reach the SDK. Dropping the budget must not drop the cancel either — + // the per-request bridged signal still has to reach the client. + sharedMockCreate.mockResolvedValue({ + choices: [{ message: { content: "completed text" } }], + }) + + const controller = new AbortController() + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + await handler.completePrompt("Write a haiku", { abortSignal: controller.signal, timeoutMs: -5 }) + const call = sharedMockCreate.mock.calls[sharedMockCreate.mock.calls.length - 1] + const requestOptions = call[1] as { timeout?: number; signal?: AbortSignal } | undefined + expect(requestOptions).not.toHaveProperty("timeout") + expect(requestOptions?.signal).toBeInstanceOf(AbortSignal) + }) + + it("detaches the bridged abort listener when the completion finishes", async () => { + // The OpenAI SDK keeps an abort listener on whatever signal it is handed and never removes it, so + // the bridge's own listener has to go in the completion's finally: a task-scoped signal spanning + // several completions must not accumulate one listener per request. + sharedMockCreate.mockResolvedValue({ + choices: [{ message: { content: "completed text" } }], + }) + + const controller = new AbortController() + const addEventListenerSpy = vi.spyOn(controller.signal, "addEventListener") + const removeListenerSpy = vi.spyOn(controller.signal, "removeEventListener") + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + await handler.completePrompt("Write a haiku", { abortSignal: controller.signal }) + + try { + const abortAdds = addEventListenerSpy.mock.calls.filter(([event]) => event === "abort") + const bridgeListener = abortAdds.at(-1)?.[1] + expect(typeof bridgeListener).toBe("function") + expect(addEventListenerSpy).toHaveBeenCalledWith("abort", bridgeListener, { once: true }) + const removed = removeListenerSpy.mock.calls + .filter(([event]) => event === "abort") + .map(([, listener]) => listener) + expect(removed).toContain(bridgeListener) + expect(controller.signal.aborted).toBe(false) + } finally { + addEventListenerSpy.mockRestore() + removeListenerSpy.mockRestore() + } + }) + + it("completePrompt should preserve abort identity when the caller aborts during the request", async () => { + // Emulate the OpenAI SDK: an aborted request signal rejects with + // APIUserAbortError ("Request was aborted." — the trailing period would + // fail task-level abort detection, so the provider must normalize it). + // The abort fires inside the mock so the SDK abort branch is actually reached: + // a pre-aborted signal settles in the cancellation scope before the mock + // runs, which would leave this catch untested. + const controller = new AbortController() + sharedMockCreate.mockImplementationOnce(async (_params: unknown, _options: { signal?: AbortSignal }) => { + controller.abort() + throw new APIUserAbortError() + }) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const error = await handler.completePrompt("Write a haiku", { abortSignal: controller.signal }).then( + () => undefined, + (e: unknown) => e, + ) + expect(sharedMockCreate).toHaveBeenCalledTimes(1) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Unbound request was aborted") + }) + + it("completePrompt should surface request timeouts as an AbortError", async () => { + // Emulate the OpenAI SDK: when the request timeout fires, the SDK + // surfaces APIConnectionTimeoutError ("Request timed out.") once retries + // are exhausted — verified against openai v5.23.2 against a hung server. + sharedMockCreate.mockImplementationOnce( + async (_params: unknown, options: { timeout?: number; signal?: AbortSignal }) => { + // The SDK surfaces its timeout error only once the client's patience runs out: wait on + // the per-request signal the handler passed instead of guessing at a duration. + const sdkSignal = options?.signal + if (sdkSignal && !sdkSignal.aborted) { + await new Promise((resolve) => { + sdkSignal.addEventListener("abort", () => resolve(), { once: true }) + }) + } + throw new APIConnectionTimeoutError() + }, + ) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const error = await handler.completePrompt("Write a haiku", { timeoutMs: 50 }).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Unbound request was aborted") + }) + + it("completePrompt should preserve abort identity when the signal aborts during the request with a plain error", async () => { + // The aborted-signal disjunct alone must normalize a plain rejection + // (not just SDK abort classes) to the DOM-standard AbortError. The abort + // fires inside the request so the cancellation scope still lets the model + // lookup finish and the catch observes an aborted signal. + const controller = new AbortController() + sharedMockCreate.mockImplementationOnce(async (_params: unknown, _options: { signal?: AbortSignal }) => { + controller.abort() + throw new Error("boom") + }) + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const error = await handler.completePrompt("Write a haiku", { abortSignal: controller.signal }).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Unbound request was aborted") + }) + + it("completePrompt should preserve abort identity for a name-based AbortError rejection", async () => { + // No aborted signal and no SDK abort class: only the DOM-standard + // name === "AbortError" check marks a cancelled request. + sharedMockCreate.mockRejectedValueOnce(Object.assign(new Error("raw"), { name: "AbortError" })) + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const error = await handler.completePrompt("Write a haiku").then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Unbound request was aborted") + }) + + it("completePrompt should wrap a plain rejection when no options are provided", async () => { + // No options at all: options?.abortSignal must tolerate an undefined + // options argument and the rejection must surface as the wrapped + // completion error. + sharedMockCreate.mockRejectedValueOnce(new Error("boom")) + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const error = await handler.completePrompt("Write a haiku").then( + () => undefined, + (e: unknown) => e, + ) + expect((error as Error).message).toBe("Unbound completion error: boom") + }) + + it("completePrompt should wrap a non-Error rejection with its object string", async () => { + // The 4th disjunct must require an actual Error instance: a plain + // object with name === "AbortError" is not a cancelled request and + // must go through the completion-error wrapping path. + sharedMockCreate.mockRejectedValueOnce({ name: "AbortError" }) + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const error = await handler.completePrompt("Write a haiku").then( + () => undefined, + (e: unknown) => e, + ) + expect((error as Error).message).toBe("Unbound completion error: [object Object]") + }) + it("completePrompt should work without options (backward compatible)", async () => { + sharedMockCreate.mockResolvedValue({ + choices: [{ message: { content: "completed text" } }], + }) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const result = await handler.completePrompt("Write a haiku") + expect(result).toBe("completed text") + }) + + it("completePrompt settles on the abort while model resolution is pending", async () => { + // The catalog lookup parks forever, so the prompt can only settle through + // the cancellation scope around the lookup; without it the handler waits + // for the catalog and then issues the SDK request. + let releaseGate!: () => void + const gate = new Promise((resolve) => { + releaseGate = resolve + }) + let markLookupParked!: () => void + const lookupParked = new Promise((resolve) => { + markLookupParked = resolve + }) + vi.mocked(getModels).mockImplementationOnce(async () => { + markLookupParked() + await gate + return {} + }) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + const controller = new AbortController() + + const completion = handler.completePrompt("Write a haiku", { abortSignal: controller.signal }) + await lookupParked + controller.abort() + + const error = await completion.then( + () => undefined, + (caught: unknown) => caught, + ) + // Release the parked lookup so the disposable mock does not leak into the + // next test in the file. + releaseGate() + expect(sharedMockCreate).not.toHaveBeenCalled() + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Unbound request was aborted") + }) + + describe("createMessage abort signal bridging", () => { + it("rejects with the standardized AbortError before any request work when the external signal is already aborted", async () => { + // A pre-aborted request must fail fast before any model-catalog or + // SDK work starts: the standardized AbortError wins over any + // resolution failure, and the request itself never begins. + let capturedSignal: AbortSignal | undefined + sharedMockCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => { + capturedSignal = options?.signal + throw new DOMException("The operation was aborted.", "AbortError") + }) + + const controller = new AbortController() + controller.abort() + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const stream = handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + + const error = await collectStream(stream).then( + () => undefined, + (e: unknown) => e, + ) + // The fast-fail guard rejects before the request starts. + expect(sharedMockCreate).not.toHaveBeenCalled() + // Pre-flight cancellation must skip the model catalog entirely: + // the getModels mock must remain uncalled, not just the SDK create. + expect(getModels).not.toHaveBeenCalled() + expect(capturedSignal).toBeUndefined() + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Unbound request was aborted") + }) + + it("rejects with the standardized AbortError when the external signal aborts while model resolution is pending", async () => { + // The model catalog stays pending while the external signal aborts: + // the resolution race must settle with the standardized AbortError + // before the lookup is released, and the request itself must never + // start. A bridge-only fix would let the lookup finish and abort the + // internal controller after the fact; this gate proves the prompt + // settles on the abort itself. + let releaseResolution!: () => void + const resolutionGate = new Promise((resolve) => { + releaseResolution = resolve + }) + let markLookupParked!: () => void + const lookupParked = new Promise((resolve) => { + markLookupParked = resolve + }) + vi.mocked(getModels).mockImplementationOnce(async () => { + markLookupParked() + await resolutionGate + return { + "openai/gpt-4o": { + maxTokens: 4096, + contextWindow: 128000, + supportsImages: true, + supportsPromptCache: false, + inputPrice: 2.5, + outputPrice: 10, + description: "GPT-4o", + }, + } + }) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + const controller = new AbortController() + + const consumed = collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ) + + // Let the lookup park on the gate, then abort while it is still pending. + await lookupParked + controller.abort() + + const error = await consumed.then( + () => undefined, + (e: unknown) => e, + ) + // The catalog lookup was attempted but the prompt must have settled + // on the abort itself, before the lookup was released: no SDK call. + expect(getModels).toHaveBeenCalledTimes(1) + expect(sharedMockCreate).not.toHaveBeenCalled() + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Unbound request was aborted") + + // Releasing the lookup afterwards must not start a late request. + releaseResolution() + // A late request is an absence, so give the released continuation a real observation window. + // vi.waitFor resolves as soon as its callback stops throwing, and a callback that only returns a + // falsy value never throws at all (measured on vitest 4.1.12: it settled well inside the timeout), + // so polling the call count here would not wait. The assertion below is what fails when a late + // request appears. + await observeWindow(50) + expect(sharedMockCreate).not.toHaveBeenCalled() + }) + + it("aborts the in-flight request when the external signal fires mid-stream", async () => { + // The mock polls the INTERNAL controller signal (bounded 40x5ms) + // instead of waiting for an "abort" event, so the test can never + // hang if the bridge stops forwarding aborts. + let capturedSignal: AbortSignal | undefined + sharedMockCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => { + capturedSignal = options?.signal + return (async function* () { + yield { choices: [{ delta: { content: "partial" } }] } + // Reached only when the consumer asks for the next chunk, i.e. the first chunk is + // delivered: fire the caller's abort here so the abort is mid-stream by construction. + controller.abort() + if (capturedSignal?.aborted) { + throw new Error("boom") + } + })() + }) + + const controller = new AbortController() + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const consumed = collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ) + + // Let the request start and the first chunk be yielded before aborting. + // The mock calls controller.abort() the moment the first chunk has been consumed, + + // The stream loop normalizes failures that surface after the stream + // has started (series standard): the cancellation must surface as + // the standardized AbortError, not the raw rejection. + const error = await consumed.then( + () => undefined, + (e: unknown) => e, + ) + expect(capturedSignal?.aborted).toBe(true) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Unbound request was aborted") + }) + + it("normalizes a mid-stream APIUserAbortError to the standardized AbortError", async () => { + // The SDK can raise its own user-abort error even when the per-request + // controller was not aborted (e.g. the SDK watched a different signal): + // the instanceof branch of the loop catch must normalize it. + sharedMockCreate.mockImplementation(async () => + (async function* () { + yield { choices: [{ delta: { content: "partial" } }] } + throw new APIUserAbortError() + })(), + ) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + await expect( + collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: new AbortController().signal }), + ), + ), + ).rejects.toMatchObject({ + name: "AbortError", + message: "The Unbound request was aborted", + }) + }) + + it("normalizes a name-based AbortError thrown from the stream to the standardized AbortError", async () => { + // The DOM-style name check must catch errors that are not SDK abort + // classes: a plain Error named AbortError is normalized too. + const abortError = new Error("The operation was aborted.") + abortError.name = "AbortError" + sharedMockCreate.mockImplementation(async () => + (async function* () { + yield { choices: [{ delta: { content: "partial" } }] } + throw abortError + })(), + ) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + await expect( + collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: new AbortController().signal }), + ), + ), + ).rejects.toMatchObject({ + name: "AbortError", + message: "The Unbound request was aborted", + }) + }) + + it("rethrows a non-Error mid-stream failure that merely looks like an abort", async () => { + // The instanceof guard is load-bearing: a plain object carrying an + // AbortError name is not an Error, so it must not be normalized and + // must propagate unchanged. + const fake = { name: "AbortError", message: "suspicious" } + sharedMockCreate.mockImplementation(async () => + (async function* () { + yield { choices: [{ delta: { content: "partial" } }] } + throw fake + })(), + ) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const error = await collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: new AbortController().signal }), + ), + ).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toBe(fake) + }) + + it("rethrows a plain non-abort Error instance from the stream unchanged", async () => { + // The name check must not over-normalize: a genuine Error whose + // name is not "AbortError" must propagate unchanged — only real + // aborts surface as the standardized AbortError. + const boom = new Error("stream exploded") + sharedMockCreate.mockImplementation(async () => + (async function* () { + yield { choices: [{ delta: { content: "partial" } }] } + throw boom + })(), + ) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const error = await collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: new AbortController().signal }), + ), + ).then( + () => undefined, + (e: unknown) => e, + ) + // Identity, not shape: the rethrown error must be the very Error + // instance the stream raised, not a normalized abort error. + expect(error).toBe(boom) + }) + + it("surfaces a cancel when the SDK closes the stream without throwing", async () => { + // The SDK consumes the body-read abort itself and can end the iterator normally, so the + // provider must inspect the caller signal rather than read that quiet end as a completed turn. + const controller = new AbortController() + let lateChunkReads = 0 + sharedMockCreate.mockImplementationOnce(async () => + (async function* () { + yield { choices: [{ delta: { content: "a" } }], index: 0 } + controller.abort() + // A frame still queued behind the cancel: pulling it is fine, reading it is not. + yield { + get choices() { + lateChunkReads += 1 + return [{ delta: { content: "late" }, index: 0 }] + }, + index: 0, + } + })(), + ) + const handler = new UnboundHandler({ unboundApiKey: "test-key", unboundModelId: "openai/gpt-4o" }) + const error = await collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ + abortSignal: controller.signal, + }), + ), + ).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Unbound request was aborted") + expect(lateChunkReads).toBe(0) // the cancelled stream is not read again + }) + + it("does not emit the text item of a frame whose reasoning item was already taken", async () => { + // A consumer that takes the reasoning item, cancels and pulls again resumes this generator inside the + // same iteration, after the top-of-loop check has run: only a check in front of the text item can + // keep a cancelled turn from emitting it. + sharedMockCreate.mockImplementationOnce(async () => + asyncStreamFrom([ + { choices: [{ delta: { reasoning_content: "thinking", content: "answer" } }], index: 0 }, + ]), + ) + const controller = new AbortController() + const handler = new UnboundHandler({ unboundApiKey: "test-key", unboundModelId: "openai/gpt-4o" }) + const iterator = handler + .createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "reasoning", text: "thinking" }) + // Observable condition: this item reached the consumer; the frame still owes its next item. + controller.abort() + + const settled = await iterator.next().then( + (result) => result.value, + (error: unknown) => error, + ) + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Unbound request was aborted") + + // The same frame with a live caller signal still delivers both items. + sharedMockCreate.mockImplementationOnce(async () => + asyncStreamFrom([ + { choices: [{ delta: { reasoning_content: "thinking", content: "answer" } }], index: 0 }, + ]), + ) + const live = new AbortController() + const chunks = await collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: live.signal }), + ), + ) + expect(chunks.map((chunk: { type: string }) => chunk.type)).toEqual(["reasoning", "text"]) + }) + + it("does not emit the rest of a tool-call chunk once the caller cancels", async () => { + // Consumer-driven: take the first partial, cancel, then require the next call to reject without + // building the second one. The getter variant aborted before either partial existed, so it could + // not tell a per-partial check apart from a check in front of the whole frame. + const controller = new AbortController() + let laterToolCallReads = 0 + const toolCalls = [ + { + index: 0, + id: "call_1", + type: "function", + function: { name: "get_weather", arguments: '{"city":"NYC"}' }, + }, + { + get index() { + laterToolCallReads += 1 + return 0 + }, + id: "call_2", + type: "function", + function: { name: "get_time", arguments: '{"tz":"UTC"}' }, + }, + ] + sharedMockCreate.mockImplementationOnce(async () => + asyncStreamFrom([{ choices: [{ delta: { tool_calls: toolCalls } }], index: 0 }]), + ) + const handler = new UnboundHandler({ unboundApiKey: "test-key", unboundModelId: "openai/gpt-4o" }) + const iterator = handler + .createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "tool_call_partial", id: "call_1" }) + // Observable condition: the first partial reached the consumer, so the cancel lands between partials. + controller.abort() + + const settled = await iterator.next().then( + (result) => result.value, + (error: unknown) => error, + ) + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Unbound request was aborted") + expect(laterToolCallReads).toBe(0) // the second partial is never built + }) + + it("settles the stream at once when the caller cancels while create() is pending", async () => { + // The SDK waits out a Retry-After backoff without observing the signal, so an in-flight create + // can outlive the cancel; a never-settling create reproduces that wait. + sharedMockCreate.mockImplementation(() => new Promise(() => {})) + const controller = new AbortController() + const handler = new UnboundHandler({ unboundApiKey: "test-key", unboundModelId: "openai/gpt-4o" }) + const collecting = collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ + abortSignal: controller.signal, + }), + ), + ).then( + () => "resolved", + (e: unknown) => e, + ) + // Cancel only once the lookup has settled and the SDK call is in flight: an earlier abort would + // be answered by the model-lookup race rather than this one. + await vi.waitFor(() => expect(sharedMockCreate).toHaveBeenCalledTimes(1)) + controller.abort() + const settled = await Promise.race([collecting, observeWindow(200)]) + expect(settled).not.toBe("pending") // an in-flight create must not outlive the cancel + expect(settled).not.toBe("resolved") + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Unbound request was aborted") + }) + + it.each(["mid-stream throw", "rejected create()", "early gen.return()"] as const)( + "detaches the bridged abort listener on %s", + async (mode) => { + const controller = new AbortController() + let sdkSignal: AbortSignal | undefined + const addEventListenerSpy = vi.spyOn(controller.signal, "addEventListener") + const removeListenerSpy = vi.spyOn(controller.signal, "removeEventListener") + + // Every mode records the signal the provider handed the SDK. The model-lookup race registers + // its own "abort" listener with { once: true } on the caller's signal, so without this capture + // the assertions below could be satisfied by the race's listener without any request bridge. + if (mode === "rejected create()") { + sharedMockCreate.mockImplementationOnce( + async (_params: unknown, options?: { signal?: AbortSignal }) => { + sdkSignal = options?.signal + throw new Error("boom") + }, + ) + } else if (mode === "mid-stream throw") { + sharedMockCreate.mockImplementationOnce( + async (_params: unknown, options?: { signal?: AbortSignal }) => { + sdkSignal = options?.signal + return (async function* () { + yield { choices: [{ delta: { content: "p" }, index: 0 }], index: 0 } + throw new Error("boom") + })() + }, + ) + } else { + sharedMockCreate.mockImplementationOnce( + async (_params: unknown, options?: { signal?: AbortSignal }) => { + sdkSignal = options?.signal + return (async function* () { + yield { choices: [{ delta: { content: "p" }, index: 0 }], index: 0 } + await new Promise(() => {}) + })() + }, + ) + } + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + const iterator = handler + .createMessage( + "prompt", + [{ role: "user", content: "hello" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + [Symbol.asyncIterator]() + + if (mode === "early gen.return()") { + await iterator.next() + await iterator.return?.(undefined) + } else { + await iterator.next().catch(() => undefined) + await iterator.next().catch(() => undefined) + await iterator.return?.(undefined) + } + + const abortAddCalls = addEventListenerSpy.mock.calls.filter(([event]) => event === "abort") + const abortRemoveCalls = removeListenerSpy.mock.calls.filter(([event]) => event === "abort") + const bridgeListener = abortAddCalls.at(-1)?.[1] + expect(abortAddCalls.length).toBeGreaterThan(0) + expect(abortRemoveCalls.some(([, listener]) => listener === bridgeListener)).toBe(true) + // The SDK must receive the request bridge, not the caller's own signal: without it no bridge + // listener is ever registered, and the removal above would be the lookup race's. + expect(sdkSignal).toBeInstanceOf(AbortSignal) + expect(sdkSignal).not.toBe(controller.signal) + }, + ) + + it("detaches the bridged abort listener when the request completes normally", async () => { + // The listener is added with { once: true }, so it only detaches on + // abort. A task-scoped signal spanning many requests must not + // accumulate a listener per request: assert explicit removal after a + // normal (non-aborted) completion. + let sdkSignal: AbortSignal | undefined + sharedMockCreate.mockImplementation(async (_params: unknown, options?: { signal?: AbortSignal }) => { + sdkSignal = options?.signal + return asyncStreamFrom([ + { choices: [{ delta: { content: "ok" } }] }, + { choices: [{ delta: {} }], usage: { prompt_tokens: 1, completion_tokens: 1 } }, + ]) + }) + + const controller = new AbortController() + const removeListenerSpy = vi.spyOn(controller.signal, "removeEventListener") + const addEventListenerSpy = vi.spyOn(controller.signal, "addEventListener") + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const chunks = await collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ) + + expect(chunks).toContainEqual({ type: "text", text: "ok" }) + // Assert the exact listener reference so a bridge that removes a + // different callback than the one it registered cannot pass. + // The rejectOnAbort race registers its own "abort" listener on the + // same external signal during model resolution, so the first "abort" + // registration is the race's, not the bridge's. Target the last + // registration so the options/removal assertions below cannot be + // satisfied by the race's listener. + const abortAddCalls = addEventListenerSpy.mock.calls.filter(([event]) => event === "abort") + const addedListener = abortAddCalls[abortAddCalls.length - 1]?.[1] + expect(typeof addedListener).toBe("function") + // The listener is registered with { once: true } — assert the exact + // options so a bridge that drops them (and relies on the finally + // block alone for single-shot semantics) is caught. + expect(addEventListenerSpy).toHaveBeenCalledWith("abort", addedListener, { once: true }) + expect(removeListenerSpy).toHaveBeenCalledWith("abort", addedListener) + expect(controller.signal.aborted).toBe(false) + // The SDK received the bridge rather than the caller's signal, so the listener above is the + // request bridge's and not the model-lookup race's. + expect(sdkSignal).toBeInstanceOf(AbortSignal) + expect(sdkSignal).not.toBe(controller.signal) + }) + + it("streams normally when called without metadata", async () => { + // metadata?.abortSignal must tolerate a missing metadata argument. + sharedMockCreate.mockImplementation(async () => + asyncStreamFrom([{ choices: [{ delta: { content: "hi" } }] }]), + ) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const chunks = await collectStream(handler.createMessage("system", [{ role: "user", content: "hi" }])) + + expect(chunks).toEqual([{ type: "text", text: "hi" }]) + }) + + it("preserves abort identity when the SDK rejects with APIUserAbortError and no signal is aborted", async () => { + // No external signal: the aborted-controller disjunct is false, so + // the APIUserAbortError disjunct alone must normalize the rejection + // to the DOM-standard AbortError. + sharedMockCreate.mockRejectedValueOnce(new APIUserAbortError()) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const error = await collectStream( + handler.createMessage("system", [{ role: "user", content: "hi" }], { taskId: "t", tools: [] }), + ).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Unbound request was aborted") + }) + + it("preserves abort identity when the SDK rejects with a name-based AbortError", async () => { + // No SDK abort class and no aborted signal: only the DOM-standard + // name === "AbortError" check marks a cancelled pre-stream request. + sharedMockCreate.mockRejectedValueOnce(Object.assign(new Error("raw"), { name: "AbortError" })) + + const handler = new UnboundHandler({ + unboundApiKey: "test-key", + unboundModelId: "openai/gpt-4o", + }) + + const error = await collectStream( + handler.createMessage("system", [{ role: "user", content: "hi" }], { taskId: "t", tools: [] }), + ).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Unbound request was aborted") + }) }) describe("model catalog", () => { diff --git a/src/api/providers/__tests__/vercel-ai-gateway.spec.ts b/src/api/providers/__tests__/vercel-ai-gateway.spec.ts index 5bafa25c78..ec2a2bfd86 100644 --- a/src/api/providers/__tests__/vercel-ai-gateway.spec.ts +++ b/src/api/providers/__tests__/vercel-ai-gateway.spec.ts @@ -10,10 +10,11 @@ vitest.mock("vscode", () => ({ })) import { Anthropic } from "@anthropic-ai/sdk" -import OpenAI from "openai" +import OpenAI, { APIConnectionTimeoutError, APIUserAbortError } from "openai" import { VercelAiGatewayHandler } from "../vercel-ai-gateway" -import { makeApiHandlerOptions } from "../../../test-utils/api" +import { getModels } from "../fetchers/modelCache" +import { makeApiHandlerOptions, makeCreateMessageMetadata } from "../../../test-utils/api" import { asyncStreamFrom, collectStream } from "../../../test-utils/stream" import { clearAllMocks } from "../../../test-utils/reset" import { vercelAiGatewayDefaultModelId, VERCEL_AI_GATEWAY_DEFAULT_TEMPERATURE } from "@roo-code/types" @@ -168,6 +169,19 @@ const mockConstructor = vitest.fn() }, }) +// A bounded observation window built on vi.waitFor: it polls while its callback throws, so a +// callback that always throws keeps it polling for the whole window (measured on vitest 4.1.12: +// a callback that only returns a value settles immediately instead of waiting). It also doubles +// as the "still pending" sentinel in the cancel races below, which is what pins the race itself. +async function observeWindow(ms: number): Promise<"pending"> { + return vi + .waitFor(() => expect(false).toBe(true), { timeout: ms, interval: 10 }) + .then( + () => "pending", + () => "pending", + ) +} + describe("VercelAiGatewayHandler", () => { const mockOptions = makeApiHandlerOptions({ vercelAiGatewayApiKey: "test-key", @@ -311,6 +325,55 @@ describe("VercelAiGatewayHandler", () => { }).rejects.toThrow("Vercel AI Gateway stream error") }) + it("throws the default message when an in-stream error chunk has an empty message", async () => { + // An empty message must not be forwarded — it would become + // Error("") with no diagnostic at all. + mockCreate.mockImplementation(async () => asyncStreamFrom([{ error: { message: "" } }])) + + const handler = new VercelAiGatewayHandler(mockOptions) + const stream = handler.createMessage("You are a helpful assistant.", [{ role: "user", content: "Hello" }]) + + await expect(async () => { + await collectStream(stream) + }).rejects.toThrow("Vercel AI Gateway stream error") + }) + + it("treats a present-but-undefined error key as no error", async () => { + mockCreate.mockImplementation(async () => + asyncStreamFrom([{ error: undefined, choices: [{ delta: { content: "hi" }, index: 0 }], index: 0 }]), + ) + + const handler = new VercelAiGatewayHandler(mockOptions) + const stream = handler.createMessage("You are a helpful assistant.", [{ role: "user", content: "Hello" }]) + + const chunks = await collectStream(stream) + expect(chunks).toEqual([{ type: "text", text: "hi" }]) + }) + + it("skips frames without a delta and tool calls without a function field", async () => { + // Full-list assertion: a frame without choices[0], a choice without + // a delta, and a tool call missing function must each contribute + // nothing except the partial tool call with undefined name/arguments + // (toEqual ignores undefined-valued keys). + mockCreate.mockImplementation(async () => + asyncStreamFrom([ + { choices: [], index: 0 }, + { choices: [{}], index: 0 }, + { choices: [{ delta: { content: "hi" }, index: 0 }], index: 0 }, + { choices: [{ delta: { tool_calls: [{ index: 0, id: "call_1" }] }, index: 0 }], index: 0 }, + ]), + ) + + const handler = new VercelAiGatewayHandler(mockOptions) + const stream = handler.createMessage("You are a helpful assistant.", [{ role: "user", content: "Hello" }]) + + const chunks = await collectStream(stream) + expect(chunks).toEqual([ + { type: "text", text: "hi" }, + { type: "tool_call_partial", index: 0, id: "call_1" }, + ]) + }) + it("uses correct temperature from options", async () => { const customTemp = 0.5 const handler = new VercelAiGatewayHandler( @@ -329,6 +392,7 @@ describe("VercelAiGatewayHandler", () => { expect.objectContaining({ temperature: customTemp, }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), ) }) @@ -344,6 +408,7 @@ describe("VercelAiGatewayHandler", () => { expect.objectContaining({ temperature: VERCEL_AI_GATEWAY_DEFAULT_TEMPERATURE, }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), ) }) @@ -363,6 +428,7 @@ describe("VercelAiGatewayHandler", () => { temperature: undefined, max_completion_tokens: 128000, }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), ) }) @@ -381,6 +447,7 @@ describe("VercelAiGatewayHandler", () => { model: "anthropic/claude-fable-5.1", temperature: undefined, }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), ) }) @@ -518,6 +585,7 @@ describe("VercelAiGatewayHandler", () => { expect.objectContaining({ max_completion_tokens: 64000, // max tokens for sonnet 4 }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), ) }) @@ -593,6 +661,7 @@ describe("VercelAiGatewayHandler", () => { }), ]), }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), ) }) @@ -610,6 +679,7 @@ describe("VercelAiGatewayHandler", () => { expect.objectContaining({ tool_choice: "auto", }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), ) }) @@ -627,6 +697,7 @@ describe("VercelAiGatewayHandler", () => { expect.objectContaining({ parallel_tool_calls: true, }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), ) }) @@ -644,6 +715,7 @@ describe("VercelAiGatewayHandler", () => { tools: expect.any(Array), parallel_tool_calls: true, }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), ) }) @@ -740,6 +812,7 @@ describe("VercelAiGatewayHandler", () => { expect.objectContaining({ stream_options: { include_usage: true }, }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), ) }) }) @@ -778,6 +851,7 @@ describe("VercelAiGatewayHandler", () => { temperature: VERCEL_AI_GATEWAY_DEFAULT_TEMPERATURE, max_completion_tokens: 64000, }), + undefined, ) }) @@ -796,6 +870,7 @@ describe("VercelAiGatewayHandler", () => { expect.objectContaining({ temperature: customTemp, }), + undefined, ) }) @@ -828,10 +903,703 @@ describe("VercelAiGatewayHandler", () => { const result = await handler.completePrompt("Test") expect(result).toBe("") }) + + it("hands the client a per-request bridge that settles on the caller cancel", async () => { + const controller = new AbortController() + let sdkSignal: AbortSignal | undefined + let abortedAtEntry = true + // A create that never settles reproduces the SDK waiting out a retry delay while the caller + // cancels. Observations are recorded here and asserted below: an assertion raised inside the + // mock would be normalized into the same AbortError this test expects. + mockCreate.mockImplementationOnce(async (_params: unknown, options?: { signal?: AbortSignal }) => { + sdkSignal = options?.signal + abortedAtEntry = sdkSignal?.aborted === true + return new Promise(() => {}) + }) + const handler = new VercelAiGatewayHandler(mockOptions) + const completing = handler.completePrompt("test prompt", { abortSignal: controller.signal }).then( + () => "resolved", + (e: unknown) => e, + ) + await vi.waitFor(() => expect(mockCreate).toHaveBeenCalledTimes(1)) + controller.abort() + const error = await Promise.race([completing, observeWindow(200)]) + // The SDK adds an abort listener to whatever signal it is handed and never removes it, so it + // must receive the per-request bridge rather than the caller's long-lived signal. + expect(sdkSignal).toBeInstanceOf(AbortSignal) + expect(sdkSignal).not.toBe(controller.signal) + expect(abortedAtEntry).toBe(false) + expect(sdkSignal?.aborted).toBe(true) // the bridge forwarded the cancel to the SDK signal + expect(error).not.toBe("pending") // an in-flight completion must not outlive the cancel + expect(error).not.toBe("resolved") + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Vercel AI Gateway request was aborted") + }) + + it("should pass timeout through to client", async () => { + const handler = new VercelAiGatewayHandler(mockOptions) + mockCreate.mockImplementation(async () => ({ + choices: [ + { + message: { role: "assistant", content: "response" }, + finish_reason: "stop", + index: 0, + }, + ], + })) + + await handler.completePrompt("test prompt", { timeoutMs: 5000 }) + // Without a caller signal there is no bridge to hand over, so the option object stays without a + // signal key rather than carrying an undefined one. + expect(Object.keys((mockCreate.mock.calls.at(-1)?.[1] ?? {}) as object)).not.toContain("signal") + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ model: expect.any(String) }), + expect.objectContaining({ timeout: 5000 }), + ) + }) + + it("should omit the timeout option when timeoutMs is 0", async () => { + // The OpenAI SDK treats timeout: 0 as an immediate abort, so the + // "disabled" value must never be forwarded — assert the absence of + // the option (a forwarded timeout: 0 would fail this assertion). + const handler = new VercelAiGatewayHandler(mockOptions) + mockCreate.mockImplementation(async () => ({ + choices: [ + { + message: { role: "assistant", content: "response" }, + finish_reason: "stop", + index: 0, + }, + ], + })) + + await handler.completePrompt("test prompt", { timeoutMs: 0 }) + const call = mockCreate.mock.calls[mockCreate.mock.calls.length - 1] + const requestOptions = call[1] as { timeout?: number } | undefined + // When no option is forwarded the provider omits the SDK options + // argument entirely, so absence means: undefined arg OR an arg + // without a timeout key. + expect(Object.keys(requestOptions ?? {})).not.toContain("timeout") + }) + + it("should omit the timeout option for a negative timeoutMs and keep the bridged signal", async () => { + // -5 is a disabled budget, not a five-second one: hasUsableTimeout rejects every + // non-positive value, so nothing may reach the SDK — and dropping the budget must not + // drop the cancel, so the bridged per-request signal still has to be forwarded. + const handler = new VercelAiGatewayHandler(mockOptions) + mockCreate.mockImplementation(async () => ({ + choices: [ + { + message: { role: "assistant", content: "response" }, + finish_reason: "stop", + index: 0, + }, + ], + })) + const controller = new AbortController() + + await handler.completePrompt("test prompt", { abortSignal: controller.signal, timeoutMs: -5 }) + const call = mockCreate.mock.calls[mockCreate.mock.calls.length - 1] + const requestOptions = call[1] as { timeout?: number; signal?: AbortSignal } | undefined + expect(requestOptions).not.toHaveProperty("timeout") + expect(requestOptions?.signal).toBeInstanceOf(AbortSignal) + }) + + it("detaches the bridged abort listener when the completion finishes", async () => { + // The OpenAI SDK keeps an abort listener on whatever signal it is handed and never removes it, so + // the bridge's own listener has to go in the completion's finally: a task-scoped signal spanning + // several completions must not accumulate one listener per request. + mockCreate.mockImplementation(async () => ({ + choices: [{ message: { role: "assistant", content: "response" } }], + })) + + const controller = new AbortController() + const addEventListenerSpy = vi.spyOn(controller.signal, "addEventListener") + const removeListenerSpy = vi.spyOn(controller.signal, "removeEventListener") + const handler = new VercelAiGatewayHandler(mockOptions) + + await handler.completePrompt("test prompt", { abortSignal: controller.signal }) + + try { + const abortAdds = addEventListenerSpy.mock.calls.filter(([event]) => event === "abort") + const bridgeListener = abortAdds.at(-1)?.[1] + expect(typeof bridgeListener).toBe("function") + expect(addEventListenerSpy).toHaveBeenCalledWith("abort", bridgeListener, { once: true }) + const removed = removeListenerSpy.mock.calls + .filter(([event]) => event === "abort") + .map(([, listener]) => listener) + expect(removed).toContain(bridgeListener) + expect(controller.signal.aborted).toBe(false) + } finally { + addEventListenerSpy.mockRestore() + removeListenerSpy.mockRestore() + } + }) + + it("should preserve abort identity when the caller aborts during the request", async () => { + // Emulate the OpenAI SDK: an aborted request signal rejects with + // APIUserAbortError ("Request was aborted." — the trailing period would + // fail task-level abort detection, so the provider must normalize it). + // The abort fires inside the mock so the SDK abort branch is actually reached: + // a pre-aborted signal settles in the cancellation scope before the mock + // runs, which would leave this catch untested. + const controller = new AbortController() + mockCreate.mockImplementationOnce(async (_params: unknown, _options: { signal?: AbortSignal }) => { + controller.abort() + throw new APIUserAbortError() + }) + + const handler = new VercelAiGatewayHandler(mockOptions) + + const error = await handler.completePrompt("test prompt", { abortSignal: controller.signal }).then( + () => undefined, + (e: unknown) => e, + ) + expect(mockCreate).toHaveBeenCalledTimes(1) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Vercel AI Gateway request was aborted") + }) + + it("should surface request timeouts as an AbortError", async () => { + // Emulate the OpenAI SDK: when the request timeout fires, the SDK + // surfaces APIConnectionTimeoutError ("Request timed out.") once retries + // are exhausted — verified against openai v5.23.2 against a hung server. + mockCreate.mockImplementationOnce( + async (_params: unknown, options: { timeout?: number; signal?: AbortSignal }) => { + // The SDK surfaces its timeout error only once the client's patience runs out: wait on + // the per-request signal the handler passed instead of guessing at a duration. + const sdkSignal = options?.signal + if (sdkSignal && !sdkSignal.aborted) { + await new Promise((resolve) => { + sdkSignal.addEventListener("abort", () => resolve(), { once: true }) + }) + } + throw new APIConnectionTimeoutError() + }, + ) + + const handler = new VercelAiGatewayHandler(mockOptions) + + const error = await handler.completePrompt("test prompt", { timeoutMs: 50 }).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Vercel AI Gateway request was aborted") + }) + + it("should preserve abort identity when the signal aborts during the request with a plain error", async () => { + // The aborted-signal disjunct alone must normalize a plain + // rejection (not just SDK abort classes) to the DOM-standard + // AbortError. The abort fires inside the request so the cancellation + // scope still lets the model lookup finish and the catch observes an + // aborted signal. + const controller = new AbortController() + mockCreate.mockImplementationOnce(async (_params: unknown, _options: { signal?: AbortSignal }) => { + controller.abort() + throw new Error("boom") + }) + const handler = new VercelAiGatewayHandler(mockOptions) + + const error = await handler.completePrompt("test prompt", { abortSignal: controller.signal }).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Vercel AI Gateway request was aborted") + }) + + it("should preserve abort identity for a name-based AbortError rejection", async () => { + // No aborted signal and no SDK abort class: only the DOM-standard + // name === "AbortError" check marks a cancelled request. + mockCreate.mockRejectedValueOnce(Object.assign(new Error("raw"), { name: "AbortError" })) + const handler = new VercelAiGatewayHandler(mockOptions) + + const error = await handler.completePrompt("test prompt").then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Vercel AI Gateway request was aborted") + }) + it("should work without options (backward compatible)", async () => { + const handler = new VercelAiGatewayHandler(mockOptions) + mockCreate.mockImplementation(async () => ({ + choices: [ + { + message: { role: "assistant", content: "response" }, + finish_reason: "stop", + index: 0, + }, + ], + })) + + const result = await handler.completePrompt("test prompt") + expect(result).toBe("response") + }) + }) + + it("completePrompt settles on the abort while model resolution is pending", async () => { + // The catalog lookup parks forever, so the prompt can only settle through + // the cancellation scope around the lookup; without it the handler waits + // for the catalog and then issues the SDK request. + let releaseGate!: () => void + const gate = new Promise((resolve) => { + releaseGate = resolve + }) + let markLookupParked!: () => void + const lookupParked = new Promise((resolve) => { + markLookupParked = resolve + }) + vitest.mocked(getModels).mockImplementationOnce(async () => { + markLookupParked() + await gate + return {} + }) + + const handler = new VercelAiGatewayHandler(mockOptions) + const controller = new AbortController() + + const completion = handler.completePrompt("test prompt", { abortSignal: controller.signal }) + await lookupParked + controller.abort() + + const error = await completion.then( + () => undefined, + (caught: unknown) => caught, + ) + // Release the parked lookup so the disposable mock does not leak into the + // next test in the file. + releaseGate() + expect(mockCreate).not.toHaveBeenCalled() + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Vercel AI Gateway request was aborted") + }) + describe("createMessage abort signal bridging", () => { + it("rejects with the standardized AbortError before any request work when the external signal is already aborted", async () => { + // A pre-aborted request must fail fast before any model-catalog or + // SDK work starts: the standardized AbortError wins over any + // resolution failure, and the request itself never begins. + let capturedSignal: AbortSignal | undefined + mockCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => { + capturedSignal = options?.signal + throw new DOMException("The operation was aborted.", "AbortError") + }) + + const handler = new VercelAiGatewayHandler(mockOptions) + const controller = new AbortController() + controller.abort() + + const stream = handler.createMessage( + "test prompt", + [{ role: "user", content: "hello" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + + const error = await collectStream(stream).then( + () => undefined, + (e: unknown) => e, + ) + // The fast-fail guard rejects before the request starts. + expect(mockCreate).not.toHaveBeenCalled() + // Pre-flight cancellation must skip the model catalog entirely: + // the getModels mock must remain uncalled, not just the SDK create. + expect(getModels).not.toHaveBeenCalled() + expect(capturedSignal).toBeUndefined() + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Vercel AI Gateway request was aborted") + }) + + it("rejects with the standardized AbortError when the external signal aborts while model resolution is pending", async () => { + // The model catalog stays pending while the external signal aborts: + // the resolution race must settle with the standardized AbortError + // before the lookup is released, and the request itself must never + // start. A bridge-only fix would let the lookup finish and abort the + // internal controller after the fact; this gate proves the prompt + // settles on the abort itself. + let releaseResolution!: () => void + const resolutionGate = new Promise((resolve) => { + releaseResolution = resolve + }) + let markLookupParked!: () => void + const lookupParked = new Promise((resolve) => { + markLookupParked = resolve + }) + vitest.mocked(getModels).mockImplementationOnce(async () => { + markLookupParked() + await resolutionGate + return { + "anthropic/claude-sonnet-4": { + maxTokens: 64000, + contextWindow: 200000, + supportsImages: true, + supportsPromptCache: true, + inputPrice: 3, + outputPrice: 15, + description: "Claude Sonnet 4", + }, + } + }) + + const handler = new VercelAiGatewayHandler(mockOptions) + const controller = new AbortController() + + const consumed = collectStream( + handler.createMessage( + "test prompt", + [{ role: "user", content: "hello" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ) + + // Let the lookup park on the gate, then abort while it is still pending. + await lookupParked + controller.abort() + + const error = await consumed.then( + () => undefined, + (e: unknown) => e, + ) + // The catalog lookup was attempted but the prompt must have settled + // on the abort itself, before the lookup was released: no SDK call. + expect(getModels).toHaveBeenCalledTimes(1) + expect(mockCreate).not.toHaveBeenCalled() + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Vercel AI Gateway request was aborted") + + // Releasing the lookup afterwards must not start a late request. + releaseResolution() + // A late request is an absence, so give the released continuation a real observation window. + // vi.waitFor resolves as soon as its callback stops throwing, and a callback that only returns a + // falsy value never throws at all (measured on vitest 4.1.12: it settled well inside the timeout), + // so polling the call count here would not wait. The assertion below is what fails when a late + // request appears. + await observeWindow(50) + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("aborts the in-flight request when the external signal fires mid-stream", async () => { + // The mock polls the INTERNAL controller signal (bounded 40x5ms) + // instead of waiting for an "abort" event, so the test can never + // hang if the bridge stops forwarding aborts. + let capturedSignal: AbortSignal | undefined + mockCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => { + capturedSignal = options?.signal + return (async function* () { + yield { choices: [{ delta: { content: "partial" }, index: 0 }], index: 0 } + // Reached only when the consumer asks for the next chunk, i.e. the first chunk is + // delivered: fire the caller's abort here so the abort is mid-stream by construction. + controller.abort() + if (capturedSignal?.aborted) { + throw new DOMException("The operation was aborted.", "AbortError") + } + })() + }) + + const handler = new VercelAiGatewayHandler(mockOptions) + const controller = new AbortController() + + const consumed = collectStream( + handler.createMessage( + "test prompt", + [{ role: "user", content: "hello" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ) + + // Let the request start and the first chunk be yielded before aborting. + // The mock calls controller.abort() the moment the first chunk has been consumed, + + const error = await consumed.then( + () => undefined, + (e: unknown) => e, + ) + expect(capturedSignal?.aborted).toBe(true) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Vercel AI Gateway request was aborted") + }) + + it("surfaces a cancel when the SDK closes the stream without throwing", async () => { + // The SDK consumes the body-read abort itself and can end the iterator normally, so the + // provider must inspect the caller signal rather than read that quiet end as a completed turn. + const controller = new AbortController() + let lateChunkReads = 0 + mockCreate.mockImplementationOnce(async () => + (async function* () { + yield { choices: [{ delta: { content: "a" } }], index: 0 } + controller.abort() + // A frame still queued behind the cancel: pulling it is fine, reading it is not. + yield { + get choices() { + lateChunkReads += 1 + return [{ delta: { content: "late" }, index: 0 }] + }, + index: 0, + } + })(), + ) + const handler = new VercelAiGatewayHandler(mockOptions) + const error = await collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ + abortSignal: controller.signal, + }), + ), + ).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Vercel AI Gateway request was aborted") + expect(lateChunkReads).toBe(0) // the cancelled stream is not read again + }) + + it("does not emit the usage item of a frame whose text was already taken", async () => { + // The usage item is the last thing this iteration can hand over, so a cancel between the text and + // the usage must reject rather than bill a turn the caller already cancelled. + // Observable condition below: the text reaches the consumer first, then the cancel lands. + const frame = { + choices: [{ delta: { content: "answer" } }], + usage: { prompt_tokens: 1, completion_tokens: 2, cost: 0.01 }, + index: 0, + } + mockCreate.mockImplementationOnce(async () => asyncStreamFrom([frame])) + const controller = new AbortController() + const handler = new VercelAiGatewayHandler(mockOptions) + const iterator = handler + .createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "text", text: "answer" }) + // Observable condition: this item reached the consumer; the frame still owes its next item. + controller.abort() + + const settled = await iterator.next().then( + (result) => result.value, + (error: unknown) => error, + ) + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Vercel AI Gateway request was aborted") + + // The same frame with a live caller signal still delivers both items. + mockCreate.mockImplementationOnce(async () => asyncStreamFrom([frame])) + const live = new AbortController() + const chunks = await collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: live.signal }), + ), + ) + expect(chunks.map((chunk: { type: string }) => chunk.type)).toEqual(["text", "usage"]) + }) + + it("does not emit the rest of a tool-call chunk once the caller cancels", async () => { + // Consumer-driven: take the first partial, cancel, then require the next call to reject without + // building the second one. The getter variant aborted before either partial existed, so it could + // not tell a per-partial check apart from a check in front of the whole frame. + const controller = new AbortController() + let laterToolCallReads = 0 + const toolCalls = [ + { + index: 0, + id: "call_1", + type: "function", + function: { name: "get_weather", arguments: '{"city":"NYC"}' }, + }, + { + get index() { + laterToolCallReads += 1 + return 0 + }, + id: "call_2", + type: "function", + function: { name: "get_time", arguments: '{"tz":"UTC"}' }, + }, + ] + mockCreate.mockImplementationOnce(async () => + asyncStreamFrom([{ choices: [{ delta: { tool_calls: toolCalls } }], index: 0 }]), + ) + const handler = new VercelAiGatewayHandler(mockOptions) + const iterator = handler + .createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "tool_call_partial", id: "call_1" }) + // Observable condition: the first partial reached the consumer, so the cancel lands between partials. + controller.abort() + + const settled = await iterator.next().then( + (result) => result.value, + (error: unknown) => error, + ) + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Vercel AI Gateway request was aborted") + expect(laterToolCallReads).toBe(0) // the second partial is never built + }) + + it("settles the stream at once when the caller cancels while create() is pending", async () => { + // The SDK waits out a Retry-After backoff without observing the signal, so an in-flight create + // can outlive the cancel; a never-settling create reproduces that wait. + mockCreate.mockImplementation(() => new Promise(() => {})) + const controller = new AbortController() + const handler = new VercelAiGatewayHandler(mockOptions) + const collecting = collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ + abortSignal: controller.signal, + }), + ), + ).then( + () => "resolved", + (e: unknown) => e, + ) + // Cancel only once the lookup has settled and the SDK call is in flight: an earlier abort would + // be answered by the model-lookup race rather than this one. + await vi.waitFor(() => expect(mockCreate).toHaveBeenCalledTimes(1)) + controller.abort() + const settled = await Promise.race([collecting, observeWindow(200)]) + expect(settled).not.toBe("pending") // an in-flight create must not outlive the cancel + expect(settled).not.toBe("resolved") + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Vercel AI Gateway request was aborted") + }) + + it.each(["mid-stream throw", "rejected create()", "early gen.return()"] as const)( + "detaches the bridged abort listener on %s", + async (mode) => { + const controller = new AbortController() + const addEventListenerSpy = vi.spyOn(controller.signal, "addEventListener") + const removeListenerSpy = vi.spyOn(controller.signal, "removeEventListener") + + if (mode === "rejected create()") { + mockCreate.mockRejectedValueOnce(new Error("boom")) + } else if (mode === "mid-stream throw") { + mockCreate.mockImplementationOnce(async () => + (async function* () { + yield { choices: [{ delta: { content: "p" }, index: 0 }], index: 0 } + throw new Error("boom") + })(), + ) + } else { + mockCreate.mockImplementationOnce(async () => + (async function* () { + yield { choices: [{ delta: { content: "p" }, index: 0 }], index: 0 } + await new Promise(() => {}) + })(), + ) + } + + const handler = new VercelAiGatewayHandler(mockOptions) + const iterator = handler + .createMessage( + "prompt", + [{ role: "user", content: "hello" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + [Symbol.asyncIterator]() + + if (mode === "early gen.return()") { + await iterator.next() + await iterator.return?.(undefined) + } else { + await iterator.next().catch(() => undefined) + await iterator.next().catch(() => undefined) + await iterator.return?.(undefined) + } + + const abortAddCalls = addEventListenerSpy.mock.calls.filter(([event]) => event === "abort") + const abortRemoveCalls = removeListenerSpy.mock.calls.filter(([event]) => event === "abort") + const bridgeListener = abortAddCalls.at(-1)?.[1] + expect(abortAddCalls.length).toBeGreaterThan(0) + expect(abortRemoveCalls.some(([, listener]) => listener === bridgeListener)).toBe(true) + }, + ) + + it("detaches the bridged abort listener when the request completes normally", async () => { + // The listener is added with { once: true }, so it only detaches on + // abort. A task-scoped signal spanning many requests must not + // accumulate a listener per request: assert explicit removal after a + // normal (non-aborted) completion. + mockCreate.mockImplementation(async () => + asyncStreamFrom([ + { + choices: [{ delta: { content: "ok" }, index: 0 }], + index: 0, + }, + { + choices: [{ delta: {}, index: 0 }], + index: 0, + usage: { prompt_tokens: 2, completion_tokens: 3 }, + }, + ]), + ) + + const handler = new VercelAiGatewayHandler(mockOptions) + const controller = new AbortController() + const removeListenerSpy = vi.spyOn(controller.signal, "removeEventListener") + const addEventListenerSpy = vi.spyOn(controller.signal, "addEventListener") + + const stream = handler.createMessage( + "test prompt", + [{ role: "user", content: "hello" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + + const chunks = await collectStream(stream) + expect(chunks).toContainEqual({ type: "text", text: "ok" }) + // Assert the exact listener reference so a bridge that removes a + // different callback than the one it registered cannot pass. + // The rejectOnAbort race registers its own "abort" listener on the + // same external signal during model resolution, so the first "abort" + // registration is the race's, not the bridge's. Target the last + // registration so the options/removal assertions below cannot be + // satisfied by the race's listener. + const abortAddCalls = addEventListenerSpy.mock.calls.filter(([event]) => event === "abort") + const addedListener = abortAddCalls[abortAddCalls.length - 1]?.[1] + expect(typeof addedListener).toBe("function") + // The listener is registered with { once: true } — assert the exact + // options so a bridge that drops them (and relies on the finally + // block alone for single-shot semantics) is caught. + expect(addEventListenerSpy).toHaveBeenCalledWith("abort", addedListener, { once: true }) + expect(removeListenerSpy).toHaveBeenCalledWith("abort", addedListener) + expect(controller.signal.aborted).toBe(false) + }) }) describe("temperature support", () => { it("applies temperature for supported models", async () => { + // Pin the response: a later describe's mock implementation may have + // left the shared mock in a state this test does not expect. + mockCreate.mockResolvedValueOnce({ + choices: [ + { + message: { role: "assistant", content: "Test completion response" }, + finish_reason: "stop", + index: 0, + }, + ], + usage: { + prompt_tokens: 8, + completion_tokens: 4, + total_tokens: 12, + }, + }) + const handler = new VercelAiGatewayHandler( makeApiHandlerOptions({ ...mockOptions, @@ -846,6 +1614,7 @@ describe("VercelAiGatewayHandler", () => { expect.objectContaining({ temperature: 0.9, }), + undefined, ) }) }) diff --git a/src/api/providers/__tests__/zoo-gateway.spec.ts b/src/api/providers/__tests__/zoo-gateway.spec.ts index a9beaada1c..59ec845240 100644 --- a/src/api/providers/__tests__/zoo-gateway.spec.ts +++ b/src/api/providers/__tests__/zoo-gateway.spec.ts @@ -27,16 +27,18 @@ vitest.mock("../../../i18n", () => ({ })) import type { MockInstance } from "vitest" -import OpenAI from "openai" +import OpenAI, { APIConnectionTimeoutError, APIUserAbortError } from "openai" import { zooGatewayDefaultModelId, ZOO_GATEWAY_DEFAULT_TEMPERATURE } from "@roo-code/types" import { ZooGatewayHandler, classifyGatewayApiError, toGatewayStreamError } from "../zoo-gateway" +import { getModels } from "../fetchers/modelCache" import { ApiHandlerOptions } from "../../../shared/api" import { Package } from "../../../shared/package" import { clearZooCodeToken } from "../../../services/zoo-code-auth" import { asyncStreamFrom, collectStream } from "../../../test-utils/stream" import { clearAllMocks } from "../../../test-utils/reset" +import { makeCreateMessageMetadata } from "../../../test-utils/api" vitest.mock("openai") vitest.mock("delay", () => ({ @@ -119,6 +121,19 @@ function mockOpenAIClient() { mockOpenAIClient() +// A bounded observation window built on vi.waitFor: it polls while its callback throws, so a +// callback that always throws keeps it polling for the whole window (measured on vitest 4.1.12: +// a callback that only returns a value settles immediately instead of waiting). It also doubles +// as the "still pending" sentinel in the cancel races below, which is what pins the race itself. +async function observeWindow(ms: number): Promise<"pending"> { + return vi + .waitFor(() => expect(false).toBe(true), { timeout: ms, interval: 10 }) + .then( + () => "pending", + () => "pending", + ) +} + describe("ZooGatewayHandler", () => { const mockOptions: ApiHandlerOptions = { zooSessionToken: "zoo_ext_test_token", @@ -376,6 +391,7 @@ describe("ZooGatewayHandler", () => { "X-Zoo-Task-ID": "task-123", "X-Zoo-Mode": "code", }, + signal: expect.any(AbortSignal), }), ) }) @@ -824,8 +840,811 @@ describe("ZooGatewayHandler", () => { expect(getModels).not.toHaveBeenCalled() expect(mockCreate).not.toHaveBeenCalled() }) + + it("should pass abort signal through to client", async () => { + const handler = new ZooGatewayHandler(mockOptions) + const controller = new AbortController() + mockCreate.mockImplementation(async () => ({ + choices: [{ message: { role: "assistant", content: "response" } }], + })) + + await handler.completePrompt("test prompt", { abortSignal: controller.signal }) + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ model: expect.any(String) }), + expect.objectContaining({ signal: expect.any(AbortSignal) }), + ) + // The handler hands the SDK a per-request signal bridged from the caller's, so the + // forwarded object is a fresh AbortSignal rather than the caller's own instance. + // The linkage itself is asserted by the abort-during-request test below. + const call = mockCreate.mock.calls[mockCreate.mock.calls.length - 1] + const forwarded = (call[1] as { signal?: AbortSignal } | undefined)?.signal + expect(forwarded).toBeInstanceOf(AbortSignal) + expect(forwarded?.aborted).toBe(false) + }) + + it("should pass timeout through to client", async () => { + const handler = new ZooGatewayHandler(mockOptions) + mockCreate.mockImplementation(async () => ({ + choices: [{ message: { role: "assistant", content: "response" } }], + })) + + await handler.completePrompt("test prompt", { timeoutMs: 5000 }) + expect(mockCreate).toHaveBeenCalledWith( + expect.objectContaining({ model: expect.any(String) }), + expect.objectContaining({ timeout: 5000 }), + ) + }) + + it("should omit the timeout option when timeoutMs is 0", async () => { + // The OpenAI SDK treats timeout: 0 as an immediate abort, so the + // "disabled" value must never be forwarded — assert the absence of + // the option (a forwarded timeout: 0 would fail this assertion). + const handler = new ZooGatewayHandler(mockOptions) + mockCreate.mockImplementation(async () => ({ + choices: [{ message: { role: "assistant", content: "response" } }], + })) + + await handler.completePrompt("test prompt", { timeoutMs: 0 }) + const call = mockCreate.mock.calls[mockCreate.mock.calls.length - 1] + const requestOptions = call[1] as { timeout?: number } | undefined + expect(requestOptions).not.toHaveProperty("timeout") + }) + + it("should omit the timeout option for a negative timeoutMs and keep the bridged signal", async () => { + // -5 is a disabled budget, not a five-second one: hasUsableTimeout rejects every + // non-positive value, so nothing may reach the SDK — and dropping the budget must not + // drop the cancel, so the bridged per-request signal still has to be forwarded. + const handler = new ZooGatewayHandler(mockOptions) + mockCreate.mockImplementation(async () => ({ + choices: [{ message: { role: "assistant", content: "response" } }], + })) + const controller = new AbortController() + + await handler.completePrompt("test prompt", { abortSignal: controller.signal, timeoutMs: -5 }) + const call = mockCreate.mock.calls[mockCreate.mock.calls.length - 1] + const requestOptions = call[1] as { timeout?: number; signal?: AbortSignal } | undefined + expect(requestOptions).not.toHaveProperty("timeout") + expect(requestOptions?.signal).toBeInstanceOf(AbortSignal) + }) + + it("should preserve abort identity when the caller aborts during the request", async () => { + // Emulate the OpenAI SDK: an aborted request signal rejects with + // APIUserAbortError ("Request was aborted." — the trailing period would + // fail task-level abort detection, so the provider must normalize it). + // The abort fires inside the mock so the SDK abort branch is actually reached: + // a pre-aborted signal settles in the cancellation scope before the mock + // runs, which would leave this catch untested. The pre-abort entry guard + // is covered by its own test below. + const controller = new AbortController() + let sdkSignal: AbortSignal | undefined + let abortedAtEntry = true + mockCreate.mockImplementationOnce(async (_params: unknown, options: { signal?: AbortSignal }) => { + // The bridged per-request signal is what the SDK receives; the caller's abort must reach it + // while the request is in flight. The state is recorded here and asserted below: an assertion + // raised inside this mock would itself be normalized into the AbortError this test expects. + sdkSignal = options.signal + abortedAtEntry = sdkSignal?.aborted === true + controller.abort() + throw new APIUserAbortError() + }) + + const handler = new ZooGatewayHandler(mockOptions) + + const error = await handler.completePrompt("test prompt", { abortSignal: controller.signal }).then( + () => undefined, + (e: unknown) => e, + ) + expect(mockCreate).toHaveBeenCalledTimes(1) + expect(sdkSignal).toBeInstanceOf(AbortSignal) + expect(sdkSignal).not.toBe(controller.signal) // the bridge, not the caller's own signal + expect(abortedAtEntry).toBe(false) + expect(sdkSignal?.aborted).toBe(true) // the bridge forwarded the cancel to the SDK signal + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Zoo Gateway request was aborted") + }) + + it("should classify the SDK's own timeout error while the request signal is live", async () => { + // The neighbouring timeout test aborts the signal before the SDK raises its error, so the + // aborted-signal disjunct alone could satisfy it. Reject with APIConnectionTimeoutError while + // nothing has been aborted: only the error-class disjunct can classify that as AbortError. + const controller = new AbortController() + mockCreate.mockImplementationOnce(() => { + throw new APIConnectionTimeoutError() + }) + + const handler = new ZooGatewayHandler(mockOptions) + + const error = await handler.completePrompt("test prompt", { abortSignal: controller.signal }).then( + () => undefined, + (e: unknown) => e, + ) + + expect(controller.signal.aborted).toBe(false) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Zoo Gateway request was aborted") + }) + + it("should surface request timeouts as an AbortError", async () => { + // Emulate the OpenAI SDK: when the request timeout fires, the SDK + // surfaces APIConnectionTimeoutError ("Request timed out.") once retries + // are exhausted — verified against openai v5.23.2 against a hung server. + mockCreate.mockImplementationOnce( + async (_params: unknown, options: { timeout?: number; signal?: AbortSignal }) => { + // The SDK surfaces its timeout error only once the client's patience runs out: wait on + // the per-request signal the handler passed instead of guessing at a duration. + const sdkSignal = options?.signal + if (!sdkSignal) { + // Without a per-request signal there is no deadline to wait for, so this mock would throw + // immediately and the test would pass even if the provider stopped forwarding the budget. + throw new Error("the provider forwarded no deadline signal") + } + if (!sdkSignal.aborted) { + await new Promise((resolve) => { + sdkSignal.addEventListener("abort", () => resolve(), { once: true }) + }) + } + throw new APIConnectionTimeoutError() + }, + ) + + const handler = new ZooGatewayHandler(mockOptions) + + const error = await handler.completePrompt("test prompt", { timeoutMs: 50 }).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Zoo Gateway request was aborted") + }) + + it("should preserve abort identity when the signal aborts during the request with a plain error", async () => { + // The aborted-signal disjunct alone must normalize a plain + // rejection (not just SDK abort classes) to the DOM-standard + // AbortError. The abort fires inside the request so the cancellation + // scope still lets the model lookup finish and the catch observes an + // aborted signal. + const controller = new AbortController() + mockCreate.mockImplementationOnce(async (_params: unknown, _options: { signal?: AbortSignal }) => { + controller.abort() + throw new Error("boom") + }) + const handler = new ZooGatewayHandler(mockOptions) + + const error = await handler.completePrompt("test prompt", { abortSignal: controller.signal }).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Zoo Gateway request was aborted") + }) + + it("should preserve abort identity for a name-based AbortError rejection", async () => { + // No aborted signal and no SDK abort class: only the DOM-standard + // name === "AbortError" check marks a cancelled request. + mockCreate.mockRejectedValueOnce(Object.assign(new Error("raw"), { name: "AbortError" })) + const handler = new ZooGatewayHandler(mockOptions) + + const error = await handler.completePrompt("test prompt").then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Zoo Gateway request was aborted") + }) + it("should work without options (backward compatible)", async () => { + const handler = new ZooGatewayHandler(mockOptions) + mockCreate.mockImplementation(async () => ({ + choices: [{ message: { role: "assistant", content: "response" } }], + })) + + const result = await handler.completePrompt("test prompt") + expect(result).toBe("response") + }) + }) + + it("completePrompt settles on the abort before the auth check when the signal is already aborted", async () => { + // The entry guard must win over the fallible auth step, exactly as in + // createMessage: a cancelled task does not run auth or the catalog lookup. + const handler = new ZooGatewayHandler(mockOptions) + const controller = new AbortController() + controller.abort() + + const error = await handler.completePrompt("test prompt", { abortSignal: controller.signal }).then( + () => undefined, + (caught: unknown) => caught, + ) + + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Zoo Gateway request was aborted") + expect(vitest.mocked(getModels)).not.toHaveBeenCalled() + }) + + it("completePrompt keeps the guard ahead of the auth check when the session is unauthenticated", async () => { + // The guard is not equivalent to the helper's own aborted check: with no + // session token the auth step runs first and would raise its own error, + // so the fast-fail must win before ensureAuthenticated. + const handler = new ZooGatewayHandler({ ...mockOptions, zooSessionToken: undefined }) + const controller = new AbortController() + controller.abort() + + const error = await handler.completePrompt("test prompt", { abortSignal: controller.signal }).then( + () => undefined, + (caught: unknown) => caught, + ) + + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Zoo Gateway request was aborted") + expect(mockCreate).not.toHaveBeenCalled() + expect(vitest.mocked(getModels)).not.toHaveBeenCalled() + }) + + it("completePrompt settles on the abort while model resolution is pending", async () => { + // The catalog lookup parks forever, so the prompt can only settle through + // the cancellation scope around the lookup; without it the handler waits + // for the catalog and then issues the SDK request. + let releaseGate!: () => void + const gate = new Promise((resolve) => { + releaseGate = resolve + }) + let markLookupParked!: () => void + const lookupParked = new Promise((resolve) => { + markLookupParked = resolve + }) + vitest.mocked(getModels).mockImplementationOnce(async () => { + markLookupParked() + await gate + return {} + }) + + const handler = new ZooGatewayHandler(mockOptions) + const controller = new AbortController() + + const completion = handler.completePrompt("test prompt", { abortSignal: controller.signal }) + await lookupParked + controller.abort() + + const error = await completion.then( + () => undefined, + (caught: unknown) => caught, + ) + // Release the parked lookup so the disposable mock does not leak into the + // next test in the file. + releaseGate() + expect(mockCreate).not.toHaveBeenCalled() + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Zoo Gateway request was aborted") }) + describe("createMessage abort signal bridging", () => { + it("rejects with the standardized AbortError before any request work when the external signal is already aborted", async () => { + // A pre-aborted request must fail fast before any model-catalog or + // SDK work starts: the standardized AbortError wins over any + // resolution failure, and the request itself never begins. + let capturedSignal: AbortSignal | undefined + mockCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => { + capturedSignal = options?.signal + throw new Error("boom") + }) + + const handler = new ZooGatewayHandler(mockOptions) + const controller = new AbortController() + controller.abort() + + const stream = handler.createMessage( + "prompt", + [{ role: "user", content: "hello" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + + const error = await collectStream(stream).then( + () => undefined, + (e: unknown) => e, + ) + // The fast-fail guard rejects before the request starts. + expect(mockCreate).not.toHaveBeenCalled() + // Pre-flight cancellation must skip the model catalog entirely: + // the getModels mock must remain uncalled, not just the SDK create. + expect(getModels).not.toHaveBeenCalled() + expect(capturedSignal).toBeUndefined() + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Zoo Gateway request was aborted") + }) + + it("rejects with the standardized AbortError before the auth check when the external signal is already aborted and the session is unauthenticated", async () => { + // The entry fast-fail must win over the auth failure: with no + // session token a pre-aborted request surfaces the standardized + // AbortError, not the auth error the unauthenticated fall-through + // would raise. + let capturedSignal: AbortSignal | undefined + mockCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => { + capturedSignal = options?.signal + throw new Error("boom") + }) + + const handler = new ZooGatewayHandler({ ...mockOptions, zooSessionToken: undefined }) + const controller = new AbortController() + controller.abort() + + const stream = handler.createMessage( + "prompt", + [{ role: "user", content: "hello" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + + const error = await collectStream(stream).then( + () => undefined, + (e: unknown) => e, + ) + // The fast-fail guard rejects before the auth check and the request. + expect(mockCreate).not.toHaveBeenCalled() + expect(getModels).not.toHaveBeenCalled() + expect(capturedSignal).toBeUndefined() + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Zoo Gateway request was aborted") + }) + + it("rejects with the standardized AbortError when the external signal aborts while model resolution is pending", async () => { + // The model catalog stays pending while the external signal aborts: + // the resolution race must settle with the standardized AbortError + // before the lookup is released, and the request itself must never + // start. A bridge-only fix would let the lookup finish and abort the + // internal controller after the fact; this gate proves the prompt + // settles on the abort itself. + let releaseResolution!: () => void + const resolutionGate = new Promise((resolve) => { + releaseResolution = resolve + }) + let markLookupParked!: () => void + const lookupParked = new Promise((resolve) => { + markLookupParked = resolve + }) + vitest.mocked(getModels).mockImplementationOnce(async () => { + markLookupParked() + await resolutionGate + return DEFAULT_MODEL_CATALOG + }) + + const handler = new ZooGatewayHandler(mockOptions) + const controller = new AbortController() + + const consumed = collectStream( + handler.createMessage( + "prompt", + [{ role: "user", content: "hello" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ) + + // Let the lookup park on the gate, then abort while it is still pending. + await lookupParked + controller.abort() + + const error = await consumed.then( + () => undefined, + (e: unknown) => e, + ) + // The catalog lookup was attempted but the prompt must have settled + // on the abort itself, before the lookup was released: no SDK call. + expect(getModels).toHaveBeenCalledTimes(1) + expect(mockCreate).not.toHaveBeenCalled() + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Zoo Gateway request was aborted") + + // Releasing the lookup afterwards must not start a late request. + releaseResolution() + // A late request is an absence, so give the released continuation a real observation window. + // vi.waitFor resolves as soon as its callback stops throwing, and a callback that only returns a + // falsy value never throws at all (measured on vitest 4.1.12: it settled well inside the timeout), + // so polling the call count here would not wait. The assertion below is what fails when a late + // request appears. + await observeWindow(50) + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("aborts the in-flight request when the external signal fires mid-stream", async () => { + // The mock polls the INTERNAL controller signal (bounded 40x5ms) + // instead of waiting for an "abort" event, so the test can never + // hang if the bridge stops forwarding aborts. + let capturedSignal: AbortSignal | undefined + mockCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => { + capturedSignal = options?.signal + return (async function* () { + yield { choices: [{ delta: { content: "partial" }, index: 0 }], index: 0 } + // Reached only when the consumer asks for the next chunk, i.e. the first chunk is + // delivered: fire the caller's abort here so the abort is mid-stream by construction. + controller.abort() + if (capturedSignal?.aborted) { + throw new DOMException("The operation was aborted.", "AbortError") + } + })() + }) + + const handler = new ZooGatewayHandler(mockOptions) + const controller = new AbortController() + + const consumed = collectStream( + handler.createMessage( + "prompt", + [{ role: "user", content: "hello" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ) + + // Let the request start and the first chunk be yielded before aborting. + // The mock calls controller.abort() the moment the first chunk has been consumed, + + const error = await consumed.then( + () => undefined, + (e: unknown) => e, + ) + expect(capturedSignal?.aborted).toBe(true) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Zoo Gateway request was aborted") + }) + + it("surfaces a cancel when the SDK closes the stream without throwing", async () => { + // The SDK consumes the body-read abort itself and can end the iterator normally, so the + // provider must inspect the caller signal rather than read that quiet end as a completed turn. + const controller = new AbortController() + let lateChunkReads = 0 + mockCreate.mockImplementationOnce(async () => + (async function* () { + yield { choices: [{ delta: { content: "a" } }], index: 0 } + controller.abort() + // A frame still queued behind the cancel: pulling it is fine, reading it is not. + yield { + get choices() { + lateChunkReads += 1 + return [{ delta: { content: "late" }, index: 0 }] + }, + index: 0, + } + })(), + ) + const handler = new ZooGatewayHandler(mockOptions) + const error = await collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ + abortSignal: controller.signal, + }), + ), + ).then( + () => undefined, + (e: unknown) => e, + ) + expect(error).toMatchObject({ name: "AbortError" }) + expect((error as Error).message).toBe("The Zoo Gateway request was aborted") + expect(lateChunkReads).toBe(0) // the cancelled stream is not read again + }) + + it("does not emit the usage item of a frame whose text was already taken", async () => { + // The usage item is the last thing this iteration can hand over, so a cancel between the text and + // the usage must reject rather than bill a turn the caller already cancelled. + // Observable condition below: the text reaches the consumer first, then the cancel lands. + const frame = { + choices: [{ delta: { content: "answer" } }], + usage: { prompt_tokens: 1, completion_tokens: 2, cost: 0.01 }, + index: 0, + } + mockCreate.mockImplementationOnce(async () => asyncStreamFrom([frame])) + const controller = new AbortController() + const handler = new ZooGatewayHandler(mockOptions) + const iterator = handler + .createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "text", text: "answer" }) + // Observable condition: this item reached the consumer; the frame still owes its next item. + controller.abort() + + const settled = await iterator.next().then( + (result) => result.value, + (error: unknown) => error, + ) + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Zoo Gateway request was aborted") + + // The same frame with a live caller signal still delivers both items. + mockCreate.mockImplementationOnce(async () => asyncStreamFrom([frame])) + const live = new AbortController() + const chunks = await collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: live.signal }), + ), + ) + expect(chunks.map((chunk: { type: string }) => chunk.type)).toEqual(["text", "usage"]) + }) + + it("does not emit the rest of a tool-call chunk once the caller cancels", async () => { + // Consumer-driven: take the first partial, cancel, then require the next call to reject without + // building the second one. The getter variant aborted before either partial existed, so it could + // not tell a per-partial check apart from a check in front of the whole frame. + const controller = new AbortController() + let laterToolCallReads = 0 + const toolCalls = [ + { + index: 0, + id: "call_1", + type: "function", + function: { name: "get_weather", arguments: '{"city":"NYC"}' }, + }, + { + get index() { + laterToolCallReads += 1 + return 0 + }, + id: "call_2", + type: "function", + function: { name: "get_time", arguments: '{"tz":"UTC"}' }, + }, + ] + mockCreate.mockImplementationOnce(async () => + asyncStreamFrom([{ choices: [{ delta: { tool_calls: toolCalls } }], index: 0 }]), + ) + const handler = new ZooGatewayHandler(mockOptions) + const iterator = handler + .createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + [Symbol.asyncIterator]() + + const first = await iterator.next() + expect(first.value).toMatchObject({ type: "tool_call_partial", id: "call_1" }) + // Observable condition: the first partial reached the consumer, so the cancel lands between partials. + controller.abort() + + const settled = await iterator.next().then( + (result) => result.value, + (error: unknown) => error, + ) + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Zoo Gateway request was aborted") + expect(laterToolCallReads).toBe(0) // the second partial is never built + }) + + it("settles the stream at once when the caller cancels while create() is pending", async () => { + // The SDK waits out a Retry-After backoff without observing the signal, so an in-flight create + // can outlive the cancel; a never-settling create reproduces that wait. + mockCreate.mockImplementation(() => new Promise(() => {})) + const controller = new AbortController() + const handler = new ZooGatewayHandler(mockOptions) + const collecting = collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ + abortSignal: controller.signal, + }), + ), + ).then( + () => "resolved", + (e: unknown) => e, + ) + // Cancel only once the lookup has settled and the SDK call is in flight: an earlier abort would + // be answered by the model-lookup race rather than this one. + await vi.waitFor(() => expect(mockCreate).toHaveBeenCalledTimes(1)) + controller.abort() + const settled = await Promise.race([collecting, observeWindow(200)]) + expect(settled).not.toBe("pending") // an in-flight create must not outlive the cancel + expect(settled).not.toBe("resolved") + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Zoo Gateway request was aborted") + }) + + it("settles at once when the caller cancels while the add-credits prompt is open", async () => { + // The 402 notification waits for the user: unless it is raced like the completion is, the + // iterator stays parked on the original 402 and the cancel never settles the stream. + const controller = new AbortController() + const consoleError = vitest.spyOn(console, "error").mockImplementation(() => {}) + try { + mockCreate.mockImplementationOnce(function () { + throw makeApiError(402) + }) + showErrorMessage.mockImplementationOnce(() => new Promise(() => {})) + const handler = new ZooGatewayHandler(mockOptions) + const draining = collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ + abortSignal: controller.signal, + }), + ), + ).then( + () => "resolved", + (e: unknown) => e, + ) + await vi.waitFor(() => expect(showErrorMessage).toHaveBeenCalledTimes(1)) + controller.abort() + const settled = await Promise.race([draining, observeWindow(200)]) + expect(settled).not.toBe("pending") // the open prompt must not outlive the cancel + expect(settled).not.toBe("resolved") + expect(settled).toMatchObject({ name: "AbortError" }) + expect((settled as Error).message).toBe("The Zoo Gateway request was aborted") + // A cancelled prompt is not a surfacing failure, so it must not log like one (as completePrompt + // already avoids on its own abort path). + expect(consoleError).not.toHaveBeenCalled() + } finally { + consoleError.mockRestore() + } + }) + + it("logs a failed add-credits notification on the streaming path and still rethrows the gateway error", async () => { + // The streaming catch reaches the same surfacing failure completePrompt does. With the caller's + // signal live the log is the only evidence that the notification itself failed: the guard in + // front of the console call must not swallow it, and the original 402 must still reach the + // caller instead of an AbortError. + const controller = new AbortController() + const consoleError = vitest.spyOn(console, "error").mockImplementation(() => {}) + try { + mockCreate.mockImplementationOnce(function () { + throw makeApiError(402, { message: "out of credits" }) + }) + showErrorMessage.mockImplementationOnce(() => Promise.reject(new Error("notification failed"))) + const handler = new ZooGatewayHandler(mockOptions) + + const settled = await collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ).then( + () => "resolved", + (e: unknown) => e, + ) + expect(consoleError).toHaveBeenCalledWith("Failed to surface Zoo Gateway error:", "notification failed") + expect((settled as Error).message).toContain("out of credits") + expect((settled as Error).name).not.toBe("AbortError") + expect(controller.signal.aborted).toBe(false) + } finally { + consoleError.mockRestore() + } + }) + + it("normalizes a name-based AbortError on the streaming path without running the gateway notification", async () => { + // No aborted signal and no SDK abort class: only the DOM-standard name marks a cancelled + // request. Without the classification in front of the notification path this rejection falls + // through into the add-credits surfacing and the raw error reaches the caller instead of the + // contract identity, which is what the two assertions below discriminate. + const controller = new AbortController() + const consoleError = vitest.spyOn(console, "error").mockImplementation(() => {}) + try { + const rawAbort = Object.assign(new Error("raw"), { name: "AbortError" }) + mockCreate.mockImplementationOnce(function () { + throw rawAbort + }) + const handler = new ZooGatewayHandler(mockOptions) + + const settled = await collectStream( + handler.createMessage( + "system", + [{ role: "user", content: "hi" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ), + ).then( + () => "resolved", + (e: unknown) => e, + ) + expect(settled).toMatchObject({ name: "AbortError", message: "The Zoo Gateway request was aborted" }) + expect(showErrorMessage).not.toHaveBeenCalled() + expect(consoleError).not.toHaveBeenCalled() + } finally { + consoleError.mockRestore() + } + }) + + it.each(["mid-stream throw", "rejected create()", "early gen.return()"] as const)( + "detaches the bridged abort listener on %s", + async (mode) => { + const controller = new AbortController() + const addEventListenerSpy = vi.spyOn(controller.signal, "addEventListener") + const removeListenerSpy = vi.spyOn(controller.signal, "removeEventListener") + + if (mode === "rejected create()") { + mockCreate.mockRejectedValueOnce(new Error("boom")) + } else if (mode === "mid-stream throw") { + mockCreate.mockImplementationOnce(async () => + (async function* () { + yield { choices: [{ delta: { content: "p" }, index: 0 }], index: 0 } + throw new Error("boom") + })(), + ) + } else { + mockCreate.mockImplementationOnce(async () => + (async function* () { + yield { choices: [{ delta: { content: "p" }, index: 0 }], index: 0 } + await new Promise(() => {}) + })(), + ) + } + const handler = new ZooGatewayHandler(mockOptions) + const iterator = handler + .createMessage( + "prompt", + [{ role: "user", content: "hello" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + [Symbol.asyncIterator]() + + if (mode === "early gen.return()") { + await iterator.next() + await iterator.return?.(undefined) + } else { + await iterator.next().catch(() => undefined) + await iterator.next().catch(() => undefined) + await iterator.return?.(undefined) + } + + const abortAddCalls = addEventListenerSpy.mock.calls.filter(([event]) => event === "abort") + const abortRemoveCalls = removeListenerSpy.mock.calls.filter(([event]) => event === "abort") + const bridgeListener = abortAddCalls.at(-1)?.[1] + expect(abortAddCalls.length).toBeGreaterThan(0) + expect(abortRemoveCalls.some(([, listener]) => listener === bridgeListener)).toBe(true) + }, + ) + + it("detaches the bridged abort listener when the request completes normally", async () => { + // The listener is added with { once: true }, so it only detaches on + // abort. A task-scoped signal spanning many requests must not + // accumulate a listener per request: assert explicit removal after a + // normal (non-aborted) completion. + mockCreate.mockImplementation(async () => + asyncStreamFrom([ + { + choices: [{ delta: { content: "ok" }, index: 0 }], + index: 0, + }, + { + choices: [{ delta: {}, index: 0 }], + index: 0, + usage: { prompt_tokens: 2, completion_tokens: 3 }, + }, + ]), + ) + + const handler = new ZooGatewayHandler(mockOptions) + const controller = new AbortController() + const removeListenerSpy = vi.spyOn(controller.signal, "removeEventListener") + const addEventListenerSpy = vi.spyOn(controller.signal, "addEventListener") + + const stream = handler.createMessage( + "prompt", + [{ role: "user", content: "hello" }], + makeCreateMessageMetadata({ abortSignal: controller.signal }), + ) + + const chunks = await collectStream(stream) + expect(chunks).toContainEqual({ type: "text", text: "ok" }) + // Assert the exact listener reference so a bridge that removes a + // different callback than the one it registered cannot pass. + // The rejectOnAbort race registers its own "abort" listener on the + // same external signal during model resolution, so the first "abort" + // registration is the race's, not the bridge's. Target the last + // registration so the options/removal assertions below cannot be + // satisfied by the race's listener. + const abortAddCalls = addEventListenerSpy.mock.calls.filter(([event]) => event === "abort") + const addedListener = abortAddCalls[abortAddCalls.length - 1]?.[1] + expect(typeof addedListener).toBe("function") + // The listener is registered with { once: true } — assert the exact + // options so a bridge that drops them (and relies on the finally + // block alone for single-shot semantics) is caught. + expect(addEventListenerSpy).toHaveBeenCalledWith("abort", addedListener, { once: true }) + expect(removeListenerSpy).toHaveBeenCalledWith("abort", addedListener) + expect(controller.signal.aborted).toBe(false) + }) + }) describe("classifyGatewayApiError", () => { it("returns sign_in on 401", () => { expect(classifyGatewayApiError(makeApiError(401))).toEqual({ kind: "sign_in" }) diff --git a/src/api/providers/opencode-go.ts b/src/api/providers/opencode-go.ts index c6b995f161..da0c605549 100644 --- a/src/api/providers/opencode-go.ts +++ b/src/api/providers/opencode-go.ts @@ -1,7 +1,7 @@ import { v7 as uuidv7 } from "uuid" -import { Anthropic } from "@anthropic-ai/sdk" +import { Anthropic, APIConnectionTimeoutError as AnthropicTimeoutError } from "@anthropic-ai/sdk" import { CacheControlEphemeral } from "@anthropic-ai/sdk/resources" -import OpenAI from "openai" +import OpenAI, { APIConnectionTimeoutError } from "openai" import { type ModelInfo, @@ -41,6 +41,17 @@ import { convertOpenAIToolsToAnthropic, convertOpenAIToolChoiceToAnthropic, } from "../../core/prompts/tools/native-tools/converters" +import { + bridgeExternalAbortSignal, + createAbortError, + hasUsableTimeout, + isRequestAborted, + mergeAbortSignalAndTimeout, + rejectOnAbort, + resolveModelWithAbort, + settleOnAbort, + toSdkTimeoutMs, +} from "./utils/abort-signal" /** * The wire formats exposed by the Opencode Go gateway: @@ -229,92 +240,199 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio messages: Anthropic.Messages.MessageParam[], metadata?: ApiHandlerCreateMessageMetadata, ): ApiStream { - const { id: modelId, info, format, temperature, reasoningEffort, maxTokens } = await this.resolveModel() + // Establish the cancellation scope around model resolution: a + // pre-aborted signal rejects before the lookup starts, and a signal + // that fires while model metadata is loading settles on the + // standardized AbortError; any other resolution failure propagates + // unchanged. + const externalAbortSignal = metadata?.abortSignal + const resolved = await resolveModelWithAbort(() => this.resolveModel(), externalAbortSignal, "Opencode Go") + const { id: modelId, info, format, temperature, reasoningEffort, maxTokens } = resolved + + // Per-request signal bridged from the caller's abort signal via the shared helper + // (pre-aborted guard, once-only listener, retained cleanup): the request-local + // signal is always defined for the streaming helpers, and dispose() detaches the + // listener when the request ends instead of leaking one per completed request. + const { signal: bridgedSignal, dispose: detachExternalAbortSignal } = + bridgeExternalAbortSignal(externalAbortSignal) + // The streaming helpers always forward a signal: keep the never-aborting fallback the + // previous inline controller provided when the caller has no external signal. + const requestSignal = bridgedSignal ?? new AbortController().signal if (format === "anthropic") { - yield* this.streamAnthropicMessage(modelId, info, temperature, maxTokens, systemPrompt, messages, metadata) + try { + yield* this.streamAnthropicMessage( + modelId, + info, + temperature, + maxTokens, + systemPrompt, + messages, + requestSignal, + metadata, + ) + } catch (error) { + // Preserve abort identity (series standard): a cancellation that + // surfaces mid-stream must normalize to the provider AbortError, + // matching the OpenAI streaming branch. + if (isRequestAborted(error, requestSignal)) { + throw createAbortError("Opencode Go") + } + throw error + } finally { + detachExternalAbortSignal() + } return } if (format === "responses") { - yield* this.streamResponsesMessage( - modelId, - info, - maxTokens, - reasoningEffort, - systemPrompt, - messages, - metadata, - ) + try { + yield* this.streamResponsesMessage( + modelId, + info, + maxTokens, + reasoningEffort, + systemPrompt, + messages, + requestSignal, + metadata, + ) + } catch (error) { + // Preserve abort identity (series standard): a cancellation that + // surfaces mid-stream must normalize to the provider AbortError, + // matching the OpenAI streaming branch. + if (isRequestAborted(error, requestSignal)) { + throw createAbortError("Opencode Go") + } + throw error + } finally { + detachExternalAbortSignal() + } + return } // preserveReasoning models (GLM/DeepSeek/MiMo/MiniMax/Qwen) require // reasoning_content to be carried across tool-call continuations. - const preserveReasoning = info.preserveReasoning === true - const convertedMessages = preserveReasoning - ? convertToR1Format(messages, { mergeToolResultText: true }) - : convertToOpenAiMessages(messages) - - const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ - { role: "system", content: systemPrompt }, - ...convertedMessages, - ] + // The conversion and request construction run inside the cleanup + // scope, like the Anthropic and Responses branches: a conversion + // failure (e.g. an unstringifiable tool input) still detaches the + // bridged abort listener. + try { + const preserveReasoning = info.preserveReasoning === true + const convertedMessages = preserveReasoning + ? convertToR1Format(messages, { mergeToolResultText: true }) + : convertToOpenAiMessages(messages) - const body: OpenAI.Chat.ChatCompletionCreateParams = { - model: modelId, - messages: openAiMessages, - temperature: this.supportsTemperature(modelId) ? temperature : undefined, - max_completion_tokens: - this.options.includeMaxTokens === true ? this.options.modelMaxTokens || maxTokens : maxTokens, - stream: true, - stream_options: { include_usage: true }, - tools: this.convertToolsForOpenAI(metadata?.tools), - tool_choice: metadata?.tool_choice, - parallel_tool_calls: metadata?.parallelToolCalls ?? true, - ...(reasoningEffort && { - reasoning_effort: reasoningEffort as OpenAI.Chat.ChatCompletionCreateParams["reasoning_effort"], - }), - } + const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ + { role: "system", content: systemPrompt }, + ...convertedMessages, + ] - const completion = await this.client.chat.completions.create(body, { - headers: { "x-opencode-session": metadata?.taskId ?? this.sessionId }, - }) + const body: OpenAI.Chat.ChatCompletionCreateParams = { + model: modelId, + messages: openAiMessages, + temperature: this.supportsTemperature(modelId) ? temperature : undefined, + max_completion_tokens: + this.options.includeMaxTokens === true ? this.options.modelMaxTokens || maxTokens : maxTokens, + stream: true, + stream_options: { include_usage: true }, + tools: this.convertToolsForOpenAI(metadata?.tools), + tool_choice: metadata?.tool_choice, + parallel_tool_calls: metadata?.parallelToolCalls ?? true, + ...(reasoningEffort && { + reasoning_effort: reasoningEffort as OpenAI.Chat.ChatCompletionCreateParams["reasoning_effort"], + }), + } - for await (const chunk of completion) { - const delta = chunk.choices[0]?.delta + // Race the stream open: openai 5.23.2 sleeps out a Retry-After backoff before it looks at + // the signal, so awaiting the create alone leaves both the pending next() and the bridge + // cleanup waiting until the sleep ends. The signal is still handed to the SDK. + const completion = await settleOnAbort( + this.client.chat.completions.create(body, { + signal: requestSignal, + headers: { "x-opencode-session": metadata?.taskId ?? this.sessionId }, + }), + requestSignal, + "Opencode Go", + ) - // Several Go-plan models (GLM, DeepSeek) stream reasoning via this field. - const reasoningText = extractReasoningFromDelta(delta) - if (reasoningText) { - yield { type: "reasoning", text: reasoningText } - } + for await (const chunk of completion) { + // The OpenAI SDK swallows the body-read abort and can end the iterator normally, so + // leave the loop here and let the check below raise the contract AbortError. + if (requestSignal.aborted) { + break + } - if (delta?.content) { - yield { type: "text", text: delta.content } - } + const delta = chunk.choices[0]?.delta - // Emit raw tool call chunks - NativeToolCallParser handles state management. - if (delta?.tool_calls) { - for (const toolCall of delta.tool_calls) { + // Several Go-plan models (GLM, DeepSeek) stream reasoning via this field. + const reasoningText = extractReasoningFromDelta(delta) + if (reasoningText) { + yield { type: "reasoning", text: reasoningText } + } + + // The Kenari buffered-chunk shape: one frame can carry reasoning plus text, or several + // tool-call items, and a consumer that takes the first item, cancels and asks for the next + // one resumes this generator inside the same iteration. Re-check the request signal before + // every buffered item, not only at the top of the loop. + if (requestSignal.aborted) { + throw createAbortError("Opencode Go") // a cancel must not emit the rest of this chunk + } + + if (delta?.content) { + yield { type: "text", text: delta.content } + } + + // Emit raw tool call chunks - NativeToolCallParser handles state management. + if (delta?.tool_calls) { + for (const toolCall of delta.tool_calls) { + if (requestSignal.aborted) { + throw createAbortError("Opencode Go") // a cancel must not emit the next tool call + } + + yield { + type: "tool_call_partial", + index: toolCall.index, + id: toolCall.id, + name: toolCall.function?.name, + arguments: toolCall.function?.arguments, + } + } + } + + // A cancel must not report usage for a turn it stopped; the guard below the loop would + // never run because the consumer's next() is what resumes us here. + if (requestSignal.aborted) { + throw createAbortError("Opencode Go") // no usage chunk for a cancelled turn + } + + if (chunk.usage) { yield { - type: "tool_call_partial", - index: toolCall.index, - id: toolCall.id, - name: toolCall.function?.name, - arguments: toolCall.function?.arguments, + type: "usage", + inputTokens: chunk.usage.prompt_tokens || 0, + outputTokens: chunk.usage.completion_tokens || 0, + cacheReadTokens: chunk.usage.prompt_tokens_details?.cached_tokens || undefined, } } } - if (chunk.usage) { - yield { - type: "usage", - inputTokens: chunk.usage.prompt_tokens || 0, - outputTokens: chunk.usage.completion_tokens || 0, - cacheReadTokens: chunk.usage.prompt_tokens_details?.cached_tokens || undefined, - } + // A cancel the SDK swallowed ends the iterator without a throw: check the request signal + // before the generator returns, so a cancelled turn does not report the partial text as a + // completed turn. The catch below normalizes the same signal, so this keeps one shape. + if (requestSignal.aborted) { + throw createAbortError("Opencode Go") } + } catch (error) { + // Preserve abort identity (series standard): surface a cancelled + // request as a DOM-standard AbortError rather than leaking the + // raw SDK abort error. + if (isRequestAborted(error, requestSignal)) { + throw createAbortError("Opencode Go") + } + throw error + } finally { + detachExternalAbortSignal() } } @@ -364,6 +482,7 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio reasoningEffort: ReasoningEffortExtended | undefined, systemPrompt: string, messages: Anthropic.Messages.MessageParam[], + abortSignal: AbortSignal, metadata?: ApiHandlerCreateMessageMetadata, ): ApiStream { const input = convertToResponsesApiInput(messages) @@ -428,13 +547,32 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio : {}), } + // Wrap pre-stream errors with the same "Opencode Go completion error:" + // prefix used by completePrompt so the Responses-format path surfaces + // failures consistently. Abort identity is preserved first (series + // standard): a cancelled request must surface as a DOM-standard + // AbortError, not a wrapped completion error. Mid-stream errors + // propagate unchanged, matching the other streaming paths. let stream: AsyncIterable try { - stream = await this.client.responses.create(requestBody, { - signal: metadata?.abortSignal, - headers: { "x-opencode-session": metadata?.taskId ?? this.sessionId }, - }) + // Race the stream open: openai sleeps out a Retry-After backoff before it observes the + // signal, so awaiting the create alone leaves both the pending next() and the bridge cleanup + // waiting for the sleep to end. The signal is still handed to the SDK for the body read. + stream = await settleOnAbort( + this.client.responses.create(requestBody, { + signal: abortSignal, + headers: { "x-opencode-session": metadata?.taskId ?? this.sessionId }, + }), + abortSignal, + "Opencode Go", + ) } catch (error) { + // isRequestAborted covers the signal-aborted case plus the OpenAI + // SDK's APIUserAbortError (name "APIUserAbortError" / "Request was + // aborted."). + if (isRequestAborted(error, abortSignal)) { + throw createAbortError("Opencode Go") + } if (error instanceof Error) { throw new Error(`Opencode Go completion error: ${error.message}`) } @@ -480,7 +618,35 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio try { // Pass the captured iterator to ensure early cancellation triggers the finally block. - yield* processResponsesApiStream({ [Symbol.asyncIterator]: () => streamIterator }, normalizeUsage) + // A cancel must not surface an event the pinned decoder already buffered: the + // processor pulls the next event before it yields it, so a pull that lands after + // the caller cancelled would still emit the buffered usage chunk for a turn the + // caller gave up on. Ending the stream here lets the after-loop guard below + // produce the contract AbortError, with no terminal cost for a cancelled turn. + // Forward each processed chunk only after re-checking the signal: a pull that started + // before the cancel can resolve with the event the pinned decoder had already buffered, + // so the pre-pull check alone cannot keep that chunk away from a consumer that gave up + // on the turn. + for await (const chunk of processResponsesApiStream( + { + [Symbol.asyncIterator]: () => ({ + next: () => + abortSignal.aborted + ? Promise.resolve({ done: true, value: undefined }) + : streamIterator.next(), + // Forward the iterator's own cleanup so an early consumer exit still closes + // the SDK iterator the way direct delegation did, and stay callable when the + // source iterator does not implement return at all. + return: streamIterator.return?.bind(streamIterator), + }), + }, + normalizeUsage, + )) { + if (abortSignal.aborted) { + throw createAbortError("Opencode Go") // a buffered chunk must not reach a cancelled consumer + } + yield chunk + } } finally { try { // Explicitly close the iterator to drop the connection and halt work. @@ -489,6 +655,14 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio // Swallow cleanup errors so they do not mask the primary stream or runtime error. } } + // The cleanup above is a suspension point of its own, so a cancel that lands while the + // iterator is closing has to be observed after it, not before. This check runs outside + // the finally on purpose: throwing from a finally would replace a stream error that is + // already in flight, and a cancelled turn must not swallow the failure that actually + // ended the stream. A live caller signal still ends the stream normally. + if (abortSignal.aborted) { + throw createAbortError("Opencode Go") // a quiet SSE close must not read as a completed turn + } } /** @@ -510,6 +684,7 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio maxTokens: number | undefined, systemPrompt: string, messages: Anthropic.Messages.MessageParam[], + abortSignal: AbortSignal | undefined, metadata?: ApiHandlerCreateMessageMetadata, ): ApiStream { const cacheControl: CacheControlEphemeral = { type: "ephemeral" } @@ -560,10 +735,25 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio // errors propagate unchanged, matching the OpenAI streaming path. let stream try { - stream = await this.anthropicClient.messages.create(requestParams, { - headers: { "x-opencode-session": metadata?.taskId ?? this.sessionId }, - }) + // Race the stream open: the SDK sleeps out a Retry-After backoff before it observes the + // signal, so awaiting the create alone leaves the wait hanging until the sleep ends. The signal is + // still handed to the SDK for the body read. + stream = await settleOnAbort( + this.anthropicClient.messages.create(requestParams, { + signal: abortSignal, + headers: { "x-opencode-session": metadata?.taskId ?? this.sessionId }, + }), + abortSignal, + "Opencode Go", + ) } catch (error) { + // Preserve abort identity (series standard): a cancelled request + // must surface as a DOM-standard AbortError, not a wrapped + // completion error. isRequestAborted also covers the Anthropic + // SDK's APIUserAbortError (name "APIUserAbortError"). + if (isRequestAborted(error, abortSignal)) { + throw createAbortError("Opencode Go") + } if (error instanceof Error) { throw new Error(`Opencode Go completion error: ${error.message}`) } @@ -576,6 +766,13 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio let cacheReadTokens = 0 for await (const chunk of stream) { + // A cancel can land between two yields of one event: an indexed content_block_start emits a + // separator and then the block text. Re-check before forwarding output, not only at the loop top + // of the next event. + if (abortSignal?.aborted) { + throw createAbortError("Opencode Go") // a cancel must not emit the rest of this event + } + switch (chunk.type) { case "message_start": { // Tells us cache reads/writes/input/output. @@ -603,12 +800,9 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio } case "message_delta": // Tells us stop_reason, stop_sequence, and output tokens. - // Anthropic streams the cumulative output token count in each - // message_delta (the final event carries the total), so - // accumulate it into the running total used for cost - // calculation — otherwise the final cost only reflects the - // (typically zero) message_start output tokens. - outputTokens += chunk.usage.output_tokens || 0 + // The API reports the cumulative output token count, so adopt + // it rather than adding it to the message_start seed value. + outputTokens = chunk.usage.output_tokens || outputTokens yield { type: "usage", inputTokens: 0, @@ -625,6 +819,10 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio // Yield thinking/reasoning content if (chunk.index > 0) { yield { type: "reasoning", text: "\n" } + + if (abortSignal?.aborted) { + throw createAbortError("Opencode Go") // a cancel must not emit the block body + } } yield { type: "reasoning", text: chunk.content_block.thinking } @@ -633,6 +831,10 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio // We may receive multiple text blocks if (chunk.index > 0) { yield { type: "text", text: "\n" } + + if (abortSignal?.aborted) { + throw createAbortError("Opencode Go") // a cancel must not emit the block text + } } yield { type: "text", text: chunk.content_block.text } @@ -677,6 +879,11 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio break } } + // The Anthropic SDK can end iteration normally after swallowing the body-read abort, so a + // cancelled stream reaches here too. A cancelled turn must not report terminal usage as if it finished. + if (abortSignal?.aborted) { + throw createAbortError("Opencode Go") // no terminal usage for a cancelled turn + } // Calculate and yield final cost if (inputTokens > 0 || outputTokens > 0 || cacheWriteTokens > 0 || cacheReadTokens > 0) { @@ -744,28 +951,73 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio * @throws Error with an Opencode Go-specific prefix if the request fails. */ async completePrompt(prompt: string, options?: CompletePromptOptions): Promise { - const { id: modelId, format, temperature, reasoningEffort, maxTokens } = await this.resolveModel() + // Establish the cancellation scope around model resolution, matching + // createMessage: a pre-aborted signal rejects before the /models lookup + // starts, a signal that fires mid-lookup settles on the standardized + // AbortError instead of the raw lookup failure, and the caller's per-request + // timeout bounds this phase too (merged deadline passed to the resolver). + const resolutionAbortSignal = mergeAbortSignalAndTimeout(options?.abortSignal, options?.timeoutMs) + const { + id: modelId, + format, + temperature, + reasoningEffort, + maxTokens, + } = await resolveModelWithAbort(() => this.resolveModel(), resolutionAbortSignal, "Opencode Go") + const timeoutMs = options?.timeoutMs if (format === "anthropic") { try { - const message = await this.anthropicClient.messages.create({ - model: modelId, - // Honour the same includeMaxTokens/modelMaxTokens override - // logic as the streaming path so non-streaming completions - // respect the user's max-output slider instead of always - // falling back to the model default. - max_tokens: - this.options.includeMaxTokens === true - ? this.options.modelMaxTokens || maxTokens || 16_384 - : (maxTokens ?? 16_384), - temperature: this.supportsTemperature(modelId) ? (temperature ?? 1.0) : undefined, - messages: [{ role: "user", content: prompt }], - stream: false, - }) + // Build request options with abortSignal and/or timeout handling. + // hasUsableTimeout/toSdkTimeoutMs own the non-positive and non-finite + // semantics: the SDKs treat timeout: 0 as an immediate abort and + // throw on negative/non-finite values, so only a usable budget is + // forwarded. + const requestOptions: Anthropic.RequestOptions = { + ...(resolutionAbortSignal && { signal: resolutionAbortSignal }), + } + if (hasUsableTimeout(timeoutMs)) { + requestOptions.timeout = toSdkTimeoutMs(timeoutMs) + } + + const pendingMessage = this.anthropicClient.messages.create( + { + model: modelId, + // Honour the same includeMaxTokens/modelMaxTokens override + // logic as the streaming path so non-streaming completions + // respect the user's max-output slider instead of always + // falling back to the model default. + max_tokens: + this.options.includeMaxTokens === true + ? this.options.modelMaxTokens || maxTokens || 16_384 + : (maxTokens ?? 16_384), + temperature: this.supportsTemperature(modelId) ? (temperature ?? 1.0) : undefined, + messages: [{ role: "user", content: prompt }], + stream: false, + }, + Object.keys(requestOptions).length > 0 ? requestOptions : undefined, + ) + // Race the SDK promise against the merged deadline, like the other two formats: + // a retry backoff longer than timeoutMs must not outlast the caller's budget. + const message = resolutionAbortSignal + ? await rejectOnAbort(pendingMessage, resolutionAbortSignal, "Opencode Go") + : await pendingMessage const content = message.content.find(({ type }) => type === "text") return content?.type === "text" ? content.text : "" } catch (error) { + // Preserve abort identity (series standard): caller-initiated + // cancellations and request timeouts must surface as a + // DOM-standard AbortError, not a wrapped completion error. The + // Anthropic SDK reports both with messages ending in a period + // ("Request was aborted.", "Request timed out."), which would not + // match task-level abort detection (message ending in "aborted"). + // isRequestAborted covers the caller's signal plus the SDK abort + // names/messages (AbortError, APIUserAbortError); the Anthropic + // SDK timeout keeps its series-standard abort identity. + if (isRequestAborted(error, options?.abortSignal) || error instanceof AnthropicTimeoutError) { + throw createAbortError("Opencode Go") + } if (error instanceof Error) { throw new Error(`Opencode Go completion error: ${error.message}`) } @@ -775,7 +1027,18 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio if (format === "responses") { try { - const response = await this.client.responses.create( + // Build request options with abortSignal and/or timeout (series + // standard): hasUsableTimeout/toSdkTimeoutMs own the disabled and + // non-finite semantics, so the SDK option is only set for a usable + // budget. + const createOptions: OpenAI.RequestOptions = { + ...(resolutionAbortSignal && { signal: resolutionAbortSignal }), + } + if (hasUsableTimeout(timeoutMs)) { + createOptions.timeout = toSdkTimeoutMs(timeoutMs) + } + + const pendingResponse = this.client.responses.create( { model: modelId, input: [{ role: "user", content: [{ type: "input_text", text: prompt }] }], @@ -802,10 +1065,26 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio } : {}), }, - { signal: options?.abortSignal, headers: { "x-opencode-session": this.sessionId } }, + { ...createOptions, headers: { "x-opencode-session": this.sessionId } }, ) + // The SDK's own retry delays are not covered by the request signal: race the + // promise against the merged deadline so a retry backoff cannot outlast timeoutMs. + const response = resolutionAbortSignal + ? await rejectOnAbort(pendingResponse, resolutionAbortSignal, "Opencode Go") + : await pendingResponse return response.output_text || "" } catch (error) { + // Preserve abort identity (series standard): caller-initiated + // cancellations and request timeouts must surface as a + // DOM-standard AbortError, not a wrapped completion error. The + // OpenAI SDK reports both with messages ending in a period + // ("Request was aborted.", "Request timed out."), which would not + // match task-level abort detection (message ending in "aborted"). + // SDK request timeouts are not aborts, but the series standard + // maps them to the same AbortError identity as caller cancellations. + if (isRequestAborted(error, options?.abortSignal) || error instanceof APIConnectionTimeoutError) { + throw createAbortError("Opencode Go") + } if (error instanceof Error) { throw new Error(`Opencode Go completion error: ${error.message}`) } @@ -832,11 +1111,39 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio reasoningEffort as OpenAI.Chat.ChatCompletionCreateParams["reasoning_effort"] } - const response = await this.client.chat.completions.create(requestOptions, { + // Build request options with abortSignal and/or timeout for the OpenAI + // path: hasUsableTimeout/toSdkTimeoutMs own the disabled and non-finite + // semantics. + const createOptions: OpenAI.RequestOptions = { + ...(resolutionAbortSignal && { signal: resolutionAbortSignal }), + } + if (hasUsableTimeout(timeoutMs)) { + createOptions.timeout = toSdkTimeoutMs(timeoutMs) + } + + const pendingResponse = this.client.chat.completions.create(requestOptions, { + ...createOptions, headers: { "x-opencode-session": this.sessionId }, }) + // Race the SDK promise against the merged deadline so a retry delay longer than + // timeoutMs cannot keep this completion waiting past the caller's budget. + const response = resolutionAbortSignal + ? await rejectOnAbort(pendingResponse, resolutionAbortSignal, "Opencode Go") + : await pendingResponse return response.choices[0]?.message.content || "" } catch (error) { + // Preserve abort identity (series standard): caller-initiated + // cancellations and request timeouts must surface as a + // DOM-standard AbortError, not a wrapped completion error. The + // OpenAI SDK reports both with messages ending in a period + // ("Request was aborted.", "Request timed out."), which would not + // match task-level abort detection (message ending in "aborted"). + // isRequestAborted covers the caller's signal plus the SDK abort + // names/messages (AbortError, APIUserAbortError); the OpenAI SDK + // timeout keeps its series-standard abort identity. + if (isRequestAborted(error, options?.abortSignal) || error instanceof APIConnectionTimeoutError) { + throw createAbortError("Opencode Go") + } if (error instanceof Error) { throw new Error(`Opencode Go completion error: ${error.message}`) } diff --git a/src/api/providers/unbound.ts b/src/api/providers/unbound.ts index 59c7bb3b66..9fc773bd5b 100644 --- a/src/api/providers/unbound.ts +++ b/src/api/providers/unbound.ts @@ -1,5 +1,5 @@ import { Anthropic } from "@anthropic-ai/sdk" -import OpenAI from "openai" +import OpenAI, { APIConnectionTimeoutError } from "openai" import { type ModelInfo, @@ -29,6 +29,15 @@ import type { } from "../index" import { handleOpenAIError } from "./utils/error-handler" import { applyRouterToolPreferences } from "./utils/router-tool-preferences" +import { + bridgeExternalAbortSignal, + createAbortError, + hasUsableTimeout, + isRequestAborted, + resolveModelWithAbort, + settleOnAbort, + toSdkTimeoutMs, +} from "./utils/abort-signal" import { extractReasoningFromDelta } from "./utils/extract-reasoning" // Unbound usage includes extra fields for Anthropic cache tokens. @@ -152,6 +161,13 @@ export class UnboundHandler extends BaseProvider implements SingleCompletionHand messages: Anthropic.Messages.MessageParam[], metadata?: ApiHandlerCreateMessageMetadata, ): ApiStream { + // Establish the cancellation scope around model resolution: a + // pre-aborted signal rejects before the lookup starts, and a signal + // that fires while model metadata is loading settles on the + // standardized AbortError; any other resolution failure propagates + // unchanged. + const externalAbortSignal = metadata?.abortSignal + const resolved = await resolveModelWithAbort(() => this.fetchModel(), externalAbortSignal, "Unbound") const { id: model, info, @@ -159,7 +175,7 @@ export class UnboundHandler extends BaseProvider implements SingleCompletionHand temperature, reasoningEffort: reasoning_effort, reasoning: thinking, - } = await this.fetchModel() + } = resolved const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ { role: "system", content: systemPrompt }, @@ -185,51 +201,109 @@ export class UnboundHandler extends BaseProvider implements SingleCompletionHand tool_choice: metadata?.tool_choice, } - let stream + // Per-request signal bridged from the caller's abort signal via the shared helper + // (pre-aborted guard, once-only listener, retained cleanup). The never-aborting + // fallback preserves the previous always-signal behaviour when the caller has no + // external signal, and dispose() detaches the listener when the request ends. + const { signal: bridgedSignal, dispose: detachExternalAbortSignal } = + bridgeExternalAbortSignal(externalAbortSignal) + const requestSignal = bridgedSignal ?? new AbortController().signal + try { - stream = await this.client.chat.completions.create(completionParams) - } catch (error) { - throw handleOpenAIError(error, this.providerName) - } - let lastUsage: any = undefined + let stream + try { + const pendingStream = this.client.chat.completions.create(completionParams, { signal: requestSignal }) + // Race the create: the SDK sleeps out a Retry-After backoff without observing the signal, + // so awaiting it alone can outlive the cancel (same shape as zoo-gateway.ts's completion path). + stream = await settleOnAbort(pendingStream, bridgedSignal, "Unbound") + } catch (error) { + // Preserve abort identity (series standard): a cancelled request + // must surface as a DOM-standard AbortError, not a wrapped + // completion error. + if (isRequestAborted(error, externalAbortSignal)) { + throw createAbortError("Unbound") + } + throw handleOpenAIError(error, this.providerName) + } + let lastUsage: OpenAI.Completions.CompletionUsage | undefined + + try { + for await (const chunk of stream) { + // The SDK swallows the body-read abort and can end the iterator normally, so leave the loop + // here and let the check below raise the contract AbortError. + if (externalAbortSignal?.aborted) { + break + } - for await (const chunk of stream) { - const delta = chunk.choices[0]?.delta + const delta = chunk.choices[0]?.delta - const reasoningText = extractReasoningFromDelta(delta) - if (reasoningText) { - yield { type: "reasoning", text: reasoningText } - } + const reasoningText = extractReasoningFromDelta(delta) + if (reasoningText) { + yield { type: "reasoning", text: reasoningText } + } - if (delta?.content) { - yield { type: "text", text: delta.content } - } + if (delta?.content) { + if (externalAbortSignal?.aborted) { + throw createAbortError("Unbound") // a cancel must not emit this frame's text + } + + yield { type: "text", text: delta.content } + } + + // Handle native tool calls + if (delta && "tool_calls" in delta && Array.isArray(delta.tool_calls)) { + for (const toolCall of delta.tool_calls) { + if (externalAbortSignal?.aborted) { + throw createAbortError("Unbound") // a cancel must not emit the rest of this chunk + } + + yield { + type: "tool_call_partial", + index: toolCall.index, + id: toolCall.id, + name: toolCall.function?.name, + arguments: toolCall.function?.arguments, + } + } + } - // Handle native tool calls - if (delta && "tool_calls" in delta && Array.isArray(delta.tool_calls)) { - for (const toolCall of delta.tool_calls) { - 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 (chunk.usage) { - lastUsage = chunk.usage - } - } + // A cancel the SDK swallowed ends the iterator without a throw: check the caller signal + // before emitting the retained usage, so a cancelled turn does not read as completed. + if (externalAbortSignal?.aborted) { + throw createAbortError("Unbound") + } - if (lastUsage) { - yield this.processUsageMetrics(lastUsage, info) + if (lastUsage) { + yield this.processUsageMetrics(lastUsage, info) + } + } catch (error) { + // Preserve abort identity (series standard): a cancellation that + // surfaces after the stream has started must also normalize to + // the standardized AbortError, not the raw SDK rejection. + if (isRequestAborted(error, externalAbortSignal)) { + throw createAbortError("Unbound") + } + throw error + } + } finally { + detachExternalAbortSignal() } } - async completePrompt(prompt: string, _options?: CompletePromptOptions): Promise { - const { id: model, maxTokens: max_tokens, temperature } = await this.fetchModel() + async completePrompt(prompt: string, options?: CompletePromptOptions): Promise { + // Model resolution must honour the caller's cancellation scope the same + // way createMessage does: a signal that fires during the lookup settles on + // the standardized AbortError instead of waiting for the catalog to finish. + const { + id: model, + maxTokens: max_tokens, + temperature, + } = await resolveModelWithAbort(() => this.fetchModel(), options?.abortSignal, "Unbound") const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [{ role: "system", content: prompt }] @@ -239,12 +313,44 @@ export class UnboundHandler extends BaseProvider implements SingleCompletionHand messages: openAiMessages, temperature: temperature, } + // Build request options with abortSignal and/or timeout. + // timeoutMs <= 0 means "no explicit timeout": omit the SDK timeout + // option entirely — the OpenAI SDK treats timeout: 0 as an immediate + // abort, which would cancel the request right away. + const createOptions: OpenAI.RequestOptions = {} + // The SDK adds an abort listener to whatever signal it is handed and never removes it, so the + // caller's signal is bridged to a per-request signal that the finally below disposes. + const { signal: bridgedSignal, dispose: detachExternalAbortSignal } = bridgeExternalAbortSignal( + options?.abortSignal, + ) + if (bridgedSignal) { + createOptions.signal = bridgedSignal + } + const timeoutMs = options?.timeoutMs + if (hasUsableTimeout(timeoutMs)) { + createOptions.timeout = toSdkTimeoutMs(timeoutMs) + } let response: OpenAI.Chat.ChatCompletion try { - response = await this.client.chat.completions.create(completionParams) + const pendingCompletion = this.client.chat.completions.create(completionParams, createOptions) + // Race the completion: the SDK does not observe the signal while it waits out a retry delay. + response = await settleOnAbort(pendingCompletion, bridgedSignal, "Unbound") } catch (error) { + // Preserve abort identity (series standard): caller-initiated + // cancellations and request timeouts must surface as a + // DOM-standard AbortError, not a wrapped completion error. The + // OpenAI SDK reports both with messages ending in a period + // ("Request was aborted.", "Request timed out."), which would not + // match task-level abort detection (message ending in "aborted"). + // SDK request timeouts are not aborts, but the series standard maps + // them to the same AbortError identity as caller cancellations. + if (isRequestAborted(error, options?.abortSignal) || error instanceof APIConnectionTimeoutError) { + throw createAbortError("Unbound") + } throw handleOpenAIError(error, this.providerName) + } finally { + detachExternalAbortSignal() } return response.choices[0]?.message.content || "" } diff --git a/src/api/providers/vercel-ai-gateway.ts b/src/api/providers/vercel-ai-gateway.ts index 135ac8bef6..6ad80e233a 100644 --- a/src/api/providers/vercel-ai-gateway.ts +++ b/src/api/providers/vercel-ai-gateway.ts @@ -1,5 +1,5 @@ import { Anthropic } from "@anthropic-ai/sdk" -import OpenAI from "openai" +import OpenAI, { APIConnectionTimeoutError } from "openai" import { vercelAiGatewayDefaultModelId, @@ -26,6 +26,15 @@ import type { ModelCacheScope, } from "../index" import { RouterProvider } from "./router-provider" +import { + bridgeExternalAbortSignal, + createAbortError, + hasUsableTimeout, + isRequestAborted, + resolveModelWithAbort, + settleOnAbort, + toSdkTimeoutMs, +} from "./utils/abort-signal" import { getVercelAiGatewayModels } from "./fetchers/vercel-ai-gateway" // Extend OpenAI's CompletionUsage to include Vercel AI Gateway specific fields @@ -75,7 +84,14 @@ export class VercelAiGatewayHandler extends RouterProvider implements SingleComp messages: Anthropic.Messages.MessageParam[], metadata?: ApiHandlerCreateMessageMetadata, ): ApiStream { - const { id: modelId, info } = await this.fetchModel() + // Establish the cancellation scope around model resolution: a + // pre-aborted signal rejects before the lookup starts, and a signal + // that fires while model metadata is loading settles on the + // standardized AbortError; any other resolution failure propagates + // unchanged. + const externalAbortSignal = metadata?.abortSignal + const resolved = await resolveModelWithAbort(() => this.fetchModel(), externalAbortSignal, "Vercel AI Gateway") + const { id: modelId, info } = resolved const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ { role: "system", content: systemPrompt }, @@ -106,57 +122,113 @@ export class VercelAiGatewayHandler extends RouterProvider implements SingleComp ;(body as { reasoning_effort?: ReasoningEffortExtended }).reasoning_effort = reasoningEffort } - const completion = await this.client.chat.completions.create(body) - - for await (const chunk of completion) { - // Vercel AI Gateway reports mid-stream failures as an in-band error chunk - // rather than throwing, so surface it instead of returning an empty response. - if ("error" in chunk && chunk.error) { - const raw = chunk.error as { message?: unknown } - const message = - typeof raw.message === "string" && raw.message.length > 0 - ? raw.message - : "Vercel AI Gateway stream error" - throw new Error(message) - } + // Per-request signal bridged from the caller's abort signal via the shared helper + // (pre-aborted guard, once-only listener, retained cleanup). The never-aborting + // fallback preserves the previous always-signal behaviour when the caller has no + // external signal, and dispose() detaches the listener when the request ends. + const { signal: bridgedSignal, dispose: detachExternalAbortSignal } = + bridgeExternalAbortSignal(externalAbortSignal) + const requestSignal = bridgedSignal ?? new AbortController().signal + + try { + const pendingCompletion = this.client.chat.completions.create(body, { signal: requestSignal }) + // Race the create: the SDK sleeps out a Retry-After backoff without observing the signal. + const completion = await settleOnAbort(pendingCompletion, bridgedSignal, "Vercel AI Gateway") - const delta = chunk.choices[0]?.delta - if (delta?.content) { - yield { - type: "text", - text: delta.content, + for await (const chunk of completion) { + // The SDK swallows the body-read abort and can end the iterator normally, so leave the loop + // here and let the check after the try raise the contract AbortError. + if (externalAbortSignal?.aborted) { + break + } + + // Vercel AI Gateway reports mid-stream failures as an in-band error chunk + // rather than throwing, so surface it instead of returning an empty response. + if ("error" in chunk && chunk.error) { + const raw = chunk.error as { message?: unknown } + const message = + typeof raw.message === "string" && raw.message.length > 0 + ? raw.message + : "Vercel AI Gateway stream error" + throw new Error(message) } - } - // Emit raw tool call chunks - NativeToolCallParser handles state management - if (delta?.tool_calls) { - for (const toolCall of delta.tool_calls) { + const delta = chunk.choices[0]?.delta + if (delta?.content) { yield { - type: "tool_call_partial", - index: toolCall.index, - id: toolCall.id, - name: toolCall.function?.name, - arguments: toolCall.function?.arguments, + type: "text", + text: delta.content, + } + } + + // Emit raw tool call chunks - NativeToolCallParser handles state management + if (delta?.tool_calls) { + for (const toolCall of delta.tool_calls) { + if (externalAbortSignal?.aborted) { + throw createAbortError("Vercel AI Gateway") // a cancel must not emit the rest of this chunk + } + + yield { + type: "tool_call_partial", + index: toolCall.index, + id: toolCall.id, + name: toolCall.function?.name, + arguments: toolCall.function?.arguments, + } } } - } - if (chunk.usage) { - const usage = chunk.usage as VercelAiGatewayUsage - yield { - type: "usage", - inputTokens: usage.prompt_tokens || 0, - outputTokens: usage.completion_tokens || 0, - cacheWriteTokens: usage.cache_creation_input_tokens || undefined, - cacheReadTokens: usage.prompt_tokens_details?.cached_tokens || undefined, - totalCost: usage.cost ?? 0, + if (chunk.usage) { + // A cancel that lands after the text of this frame must not be billed: the usage item is the + // last thing the same iteration can still hand over. + if (externalAbortSignal?.aborted) { + throw createAbortError("Vercel AI Gateway") // no usage chunk for a cancelled turn + } + + const usage = chunk.usage as VercelAiGatewayUsage + yield { + type: "usage", + inputTokens: usage.prompt_tokens || 0, + outputTokens: usage.completion_tokens || 0, + cacheWriteTokens: usage.cache_creation_input_tokens || undefined, + cacheReadTokens: usage.prompt_tokens_details?.cached_tokens || undefined, + totalCost: usage.cost ?? 0, + } } } + } catch (error) { + // Preserve abort identity (series standard): a cancelled request + // must surface as a DOM-standard AbortError, not the raw SDK abort + // error or an in-band stream error raised while aborting. + if (isRequestAborted(error, externalAbortSignal)) { + throw createAbortError("Vercel AI Gateway") + } + throw error + } finally { + detachExternalAbortSignal() + } + // The SDK can close the SSE iterator without throwing after a cancel: a cancelled stream must not + // read as a completed turn, so reject with the contract AbortError before the generator returns. + if (externalAbortSignal?.aborted) { + throw createAbortError("Vercel AI Gateway") } } - async completePrompt(prompt: string, _options?: CompletePromptOptions): Promise { - const { id: modelId, info } = await this.fetchModel() + async completePrompt(prompt: string, options?: CompletePromptOptions): Promise { + // Model resolution must honour the caller's cancellation scope the same + // way createMessage does: a signal that fires during the lookup settles on + // the standardized AbortError instead of waiting for the catalog to finish. + const { id: modelId, info } = await resolveModelWithAbort( + () => this.fetchModel(), + options?.abortSignal, + "Vercel AI Gateway", + ) + + // The SDK adds an abort listener to whatever signal it is handed and never removes it, so the + // caller's signal is bridged to a per-request signal that the finally below disposes. + const { signal: bridgedSignal, dispose: detachExternalAbortSignal } = bridgeExternalAbortSignal( + options?.abortSignal, + ) try { const reasoningEffort = getReasoningEffort(this.options, info) @@ -174,14 +246,44 @@ export class VercelAiGatewayHandler extends RouterProvider implements SingleComp } requestOptions.max_completion_tokens = info.maxTokens + // Build request options with abortSignal and/or timeout. + // timeoutMs <= 0 means "no explicit timeout": omit the SDK timeout + // option entirely — the OpenAI SDK treats timeout: 0 as an immediate + // abort, which would cancel the request right away. + const createOptions: OpenAI.RequestOptions = {} + if (bridgedSignal) { + createOptions.signal = bridgedSignal + } + const timeoutMs = options?.timeoutMs + if (hasUsableTimeout(timeoutMs)) { + createOptions.timeout = toSdkTimeoutMs(timeoutMs) + } - const response = await this.client.chat.completions.create(requestOptions) + const pendingCompletion = this.client.chat.completions.create( + requestOptions, + Object.keys(createOptions).length > 0 ? createOptions : undefined, + ) + // Race the completion: the SDK does not observe the signal while it waits out a retry delay. + const response = await settleOnAbort(pendingCompletion, bridgedSignal, "Vercel AI Gateway") return response.choices[0]?.message.content || "" } catch (error) { + // Preserve abort identity (series standard): caller-initiated + // cancellations and request timeouts must surface as a + // DOM-standard AbortError, not a wrapped completion error. The + // OpenAI SDK reports both with messages ending in a period + // ("Request was aborted.", "Request timed out."), which would not + // match task-level abort detection (message ending in "aborted"). + // SDK request timeouts are not aborts, but the series standard maps + // them to the same AbortError identity as caller cancellations. + if (isRequestAborted(error, options?.abortSignal) || error instanceof APIConnectionTimeoutError) { + throw createAbortError("Vercel AI Gateway") + } if (error instanceof Error) { throw new Error(`Vercel AI Gateway completion error: ${error.message}`) } throw error + } finally { + detachExternalAbortSignal() } } } diff --git a/src/api/providers/zoo-gateway.ts b/src/api/providers/zoo-gateway.ts index 2171c49dda..24589b7334 100644 --- a/src/api/providers/zoo-gateway.ts +++ b/src/api/providers/zoo-gateway.ts @@ -1,6 +1,6 @@ import * as vscode from "vscode" import { Anthropic } from "@anthropic-ai/sdk" -import OpenAI from "openai" +import OpenAI, { APIConnectionTimeoutError } from "openai" import { zooGatewayDefaultModelId, @@ -37,7 +37,9 @@ import { isRequestAborted, mergeAbortSignalAndTimeout, rejectOnAbort, + settleOnAbort, throwIfAborted, + resolveModelWithAbort, toSdkTimeoutMs, } from "./utils/abort-signal" @@ -209,9 +211,22 @@ export class ZooGatewayHandler extends RouterProvider implements SingleCompletio messages: Anthropic.Messages.MessageParam[], metadata?: ApiHandlerCreateMessageMetadata, ): ApiStream { + // Fail fast when the task is already cancelled before any model-catalog + // work starts: the standardized AbortError wins over any failure the + // fallible resolution could raise — including the auth check below. + const externalAbortSignal = metadata?.abortSignal + if (externalAbortSignal?.aborted) { + throw createAbortError("Zoo Gateway") + } + this.ensureAuthenticated() - const { id: modelId, info } = await this.fetchModel() + // Establish the cancellation scope around model resolution: a signal + // that fires while model metadata is loading settles on the + // standardized AbortError; any other resolution failure propagates + // unchanged. + const resolved = await resolveModelWithAbort(() => this.fetchModel(), externalAbortSignal, "Zoo Gateway") + const { id: modelId, info } = resolved const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ { role: "system", content: systemPrompt }, @@ -247,12 +262,30 @@ export class ZooGatewayHandler extends RouterProvider implements SingleCompletio parallel_tool_calls: metadata?.parallelToolCalls ?? true, } + // Per-request signal bridged from the caller's abort signal via the shared helper + // (pre-aborted guard, once-only listener, retained cleanup). The never-aborting + // fallback preserves the previous always-signal behaviour when the caller has no + // external signal, and dispose() detaches the listener when the request ends. + const { signal: bridgedSignal, dispose: detachExternalAbortSignal } = + bridgeExternalAbortSignal(externalAbortSignal) + const requestSignal = bridgedSignal ?? new AbortController().signal + try { - const completion = await this.client.chat.completions.create(body, { + const pendingCompletion = this.client.chat.completions.create(body, { headers: requestHeaders, + signal: requestSignal, }) + // Race the create: the SDK sleeps out a Retry-After backoff without observing the signal, so + // awaiting it alone can outlive the cancel (the completion path below already races this way). + const completion = await settleOnAbort(pendingCompletion, bridgedSignal, "Zoo Gateway") for await (const chunk of completion) { + // The SDK swallows the body-read abort and can end the iterator normally, so leave the loop + // here and let the checks below raise the contract AbortError. + if (externalAbortSignal?.aborted) { + break + } + // Once the gateway starts streaming the HTTP status is already 200, so it // reports upstream failures (e.g. provider rate limits) as an in-band error // chunk. Surface it so the user sees the real reason instead of an empty reply. @@ -271,6 +304,10 @@ export class ZooGatewayHandler extends RouterProvider implements SingleCompletio // Emit raw tool call chunks - NativeToolCallParser handles state management if (delta?.tool_calls) { for (const toolCall of delta.tool_calls) { + if (externalAbortSignal?.aborted) { + throw createAbortError("Zoo Gateway") // a cancel must not emit the rest of this chunk + } + yield { type: "tool_call_partial", index: toolCall.index, @@ -282,6 +319,12 @@ export class ZooGatewayHandler extends RouterProvider implements SingleCompletio } if (chunk.usage) { + // A cancel that lands after the text of this frame must not be billed: the usage item is the + // last thing the same iteration can still hand over. + if (externalAbortSignal?.aborted) { + throw createAbortError("Zoo Gateway") // no usage chunk for a cancelled turn + } + const usage = chunk.usage as ZooGatewayUsage yield { type: "usage", @@ -294,19 +337,47 @@ export class ZooGatewayHandler extends RouterProvider implements SingleCompletio } } } catch (error) { + // Preserve abort identity (series standard): a cancelled request + // must surface as a DOM-standard AbortError before the gateway + // error surfacing/telemetry path, not the raw SDK abort error. + if (isRequestAborted(error, externalAbortSignal)) { + throw createAbortError("Zoo Gateway") + } try { - await surfaceGatewayApiError(error) + // The notification can wait for the user: a cancel that arrives while it is open must settle at + // once instead of holding the iterator on the original 402 (same shape as completePrompt). + const surfacing = surfaceGatewayApiError(error) + await settleOnAbort(surfacing, bridgedSignal, "Zoo Gateway") } catch (surfaceError) { - console.error( - "Failed to surface Zoo Gateway error:", - surfaceError instanceof Error ? surfaceError.message : surfaceError, - ) + if (!externalAbortSignal?.aborted) { + console.error( + "Failed to surface Zoo Gateway error:", + surfaceError instanceof Error ? surfaceError.message : surfaceError, + ) + } + } + if (externalAbortSignal?.aborted) { + throw createAbortError("Zoo Gateway") // cancelled while the notification was open } throw error + } finally { + detachExternalAbortSignal() + } + // The SDK can close the SSE iterator without throwing after a cancel: a cancelled stream must not + // read as a completed turn, so reject with the contract AbortError before the generator returns. + if (externalAbortSignal?.aborted) { + throw createAbortError("Zoo Gateway") } } async completePrompt(prompt: string, options?: CompletePromptOptions): Promise { + // Same entry guard as createMessage: a cancelled task must not run the + // fallible auth check, and the standardized AbortError wins over anything + // the auth step or the lookup could raise. + if (options?.abortSignal?.aborted) { + throw createAbortError("Zoo Gateway") + } + // Same abort-signal contract as the other providers (see requesty.ts): the SDK gets a // per-request signal bridged from the caller's signal, so its abort listener does not stay on // the caller's long-lived signal. The `finally` below detaches the bridge on every exit path. @@ -344,6 +415,10 @@ export class ZooGatewayHandler extends RouterProvider implements SingleCompletio } requestOptions.max_completion_tokens = info.maxTokens + // Build request options with abortSignal and/or timeout. + // timeoutMs <= 0 means "no explicit timeout": omit the SDK timeout + // option entirely — the OpenAI SDK treats timeout: 0 as an immediate + // abort, which would cancel the request right away. const createOptions: OpenAI.RequestOptions = { ...(requestAbortSignal && { signal: requestAbortSignal }), @@ -360,7 +435,10 @@ export class ZooGatewayHandler extends RouterProvider implements SingleCompletio : await pendingCompletion } catch (error) { // A cancel or timeout is not a gateway failure: do not show a gateway error for it. - if (requestAbortSignal?.aborted) { + // The series standard also maps a name-based abort rejection and the SDK's own + // timeout class onto the same AbortError identity, in case the SDK rejects a + // moment before the merged request signal flips. + if (isRequestAborted(error, requestAbortSignal) || error instanceof APIConnectionTimeoutError) { throw createAbortError(ZOO_GATEWAY_PROVIDER_NAME) } try { diff --git a/src/eslint-suppressions.json b/src/eslint-suppressions.json index d5b845997a..6b3e69539e 100644 --- a/src/eslint-suppressions.json +++ b/src/eslint-suppressions.json @@ -326,7 +326,7 @@ }, "api/providers/unbound.ts": { "@typescript-eslint/no-explicit-any": { - "count": 3 + "count": 2 } }, "api/providers/utils/__tests__/error-handler.spec.ts": {