From 7cbce9dc4504a329b8cb014e2ce77bca02528d0b Mon Sep 17 00:00:00 2001 From: Eason Liang <136036952+easonLiangWorldedtech@users.noreply.github.com> Date: Fri, 20 Nov 2026 04:45:09 +0800 Subject: [PATCH] feat(api): abort signal support for opencode-go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - createMessage: bridge metadata.abortSignal to a per-request AbortController (Bedrock pattern: pre-aborted guard, once-listener, detached on completion so a task-scoped signal does not accumulate listeners); model resolution runs inside the shared resolveModelWithAbort cancellation scope (pre-aborted fast-fail, mid-resolution race) - aborted/timeout requests normalize to the provider AbortError on all three wire formats (anthropic /v1/messages, responses /v1/responses, openai chat completions), both pre-stream and mid-stream; non-abort failures keep the wrapped "Opencode Go completion error:" identity - the OpenAI branch runs message conversion and request construction inside the same cleanup scope as the Anthropic and Responses branches, so a conversion failure (e.g. an unstringifiable tool input) still detaches the bridged abort listener (CodeRabbit fix on this PR) - completePrompt: forwards abortSignal/timeoutMs to all three SDK paths (timeoutMs <= 0 omits the SDK timeout option, since the SDK treats timeout: 0 as an immediate abort); aborted completions and APIConnectionTimeoutError/APITimeoutError normalize to the provider AbortError (series standard) The inner pre-stream provider-name literal is provably equivalent and documented as such in the PR body; no Stryker disable directive is added anywhere in the tree. createMessage's outer catch applies the identical isRequestAborted check to the same controller signal and re-standardizes the error, so the inner layer's only unique behavior is the non-abort completion-error wrap (stays kill-tested), and the abort-normalization condition beside that literal is killed by a live-signal delivery added in the second review round. The cleanup-scope re-indentation made the gate instrument pre-existing request-construction lines for the first time; their previously unobservable mutants are kill-tested: R1 tool-result merge shape for preserveReasoning models and the plain shape for non-preserveReasoning models, the includeMaxTokens on/off branches with a ceiling-bounded model, and the parallel_tool_calls default/explicit-false. Unit 2/3 of the #1295 split (content source: 62f596c5d); stacks on the shared-util unit. Part of the abort-signal series (round 1). Builds on #674, #901, #1008. Addresses #404. Review round (CodeRabbit threads on 41ef0e0352fc): the Chat Completions stream loop now breaks on the request signal at the top of each iteration and re-checks that signal after the loop, so an SDK that swallows the body-read abort and closes the iterator normally cannot report a cancelled turn as completed. The new test parks the iterator after the first chunk, hands back a getter-counting chunk once the caller cancels, and asserts both the standardized AbortError and zero post-cancel chunk reads; flipping either guard to a false condition fails exactly that test (1 failed | 195 passed). The Responses pre-stream abort arm had no live-signal delivery, so one is added: with no caller signal nothing aborts, only the error-name arm can normalize, and the ConditionalExpression mutant dies with it (same control). The provider-name literals at the two stream catches and at the three completePrompt races were re-measured: each survives the full 196-test suite, which matches the equivalence argument above. The detach tests now require exactly two abort registrations on the caller's signal, name the bridge's by position, require it to differ from the model-resolution race's callback, and assert both removals by reference in that order. The SDK abort and timeout deliveries now reject from inside the SDK mock with no caller signal (the automocked openai module's error classes are not Error instances, so the name/message arm is exercised with the real class taken from the actual module), a caller cancel landing inside the SDK call is classified across all three formats, and the parked-lookup gate is the typed getModels fetcher rather than a spy on the private resolveModel. Review round 2 (CodeRabbit pre-merge checks): completePrompt gained a parameterized timeout-option test per wire format (Anthropic messages, Responses, chat completions) that proves each call site applies the shared hasUsableTimeout/toSdkTimeoutMs contract rather than only testing the helper: NaN, Infinity and -5 leave no timeout key, 12.5 forwards timeout: 13, MAX_TIMEOUT_MS + 1 forwards MAX_TIMEOUT_MS, and every row also asserts the caller's signal reached the SDK options, so dropping the budget can never drop the cancel. Controls: deleting the forward arm fails two rows per format, forcing it to always forward fails the three disabled-budget rows, and replacing toSdkTimeoutMs(timeoutMs) with the raw value fails exactly the two forwarding rows. The five provider-name literals on the abort paths were re-measured at this head: all five survive the full 211-test suite, which is the measured half of the equivalence argument in the PR body. Verification on this tree: src tsc --noEmit exit 0 apart from the two known donor-store integrations/misc/export-markdown.ts rows; vitest 211 passed (211) in declaration order and in shuffled order; eslint . --ext=ts --max-warnings=0 without --prune-suppressions exit 0 with src/eslint-suppressions.json byte-identical (a prune run found zero decreases for this unit); prettier 3.9.10 --check clean on the staged blobs extracted with core.autocrlf=false. Review round 3 (CodeRabbit pre-merge checks, six Minor rows): the Chat Completions streaming loop now re-checks the request signal in front of every buffered item of a frame — the text item after a reasoning item, each tool-call partial inside the per-item loop, and the usage item — because a consumer that takes the first item, cancels and pulls the next one resumes this generator inside the same iteration, where the top-of-loop guard has already run. The three stream-open awaits (Anthropic /v1/messages, Responses /v1/responses and chat completions) now go through settleOnAbort with the signal still handed to the SDK, so a 429 with Retry-After plus a caller cancel no longer holds the pending first next() and the bridge cleanup until the sleep ends. Regression evidence: one parameterized test for the three stream-open races plus three manual-iteration tests that abort between two items of one frame (the buffered-text variant also asserts the bridge listener is detached by reference). Negative controls: removing each open wrapper fails exactly that format's row (1 failed | 216 passed each), disabling each buffered-item guard fails exactly that item's test, and reverting the OpenAI-path fixture key from opencodeGoModelId to the ignored apiModelId fails the five assertions that now pin model "glm-5.1" instead of expect.any(String). The parked-lookup test releases its gate in a finally so an assertion throw cannot strand the promise the provider awaits; measured with a probe: a release placed after the throwing assertion leaves the promise unresolved, the same release in a finally settles it. Verification on this round: vitest 217 passed (217) in declaration order and with --sequence.shuffle; tsc --noEmit -p tsconfig.json from src: zero errors; eslint . --ext=ts --max-warnings=0 without --prune-suppressions: exit 0 with src/eslint-suppressions.json byte-identical (a prune run over the two unit files reported zero count decreases); prettier 3.9.10 --check: clean. Review round 4 (CodeRabbit pre-merge checks, one Major and nine Minor rows, handled in a single pass): the Responses and Anthropic helpers no longer let a normally-ending SDK iterator read as a completed turn. The Responses helper re-checks the bridged signal after processResponsesApiStream returns; the Anthropic helper re-checks it at the top of each event dispatch, between the separator item and the block body of an indexed content_block_start, and after the loop before the terminal cost chunk, so a cancel cannot emit the rest of an event or bill a turn the caller already cancelled. Four tests drive the shape the pinned SDKs actually produce - hand the consumer one item, cancel, then let the mock iterator report done instead of throwing - and each one also runs the identical fixture with a live caller signal, which still delivers its text and its terminal cost. Negative controls, each against the full suite: the Responses post-stream check (1 failed | 223 passed), the Anthropic after-loop check (killed by the test that cancels after the last event was delivered), the event-dispatch check (killed by the quiet-close test) and the separator checks (killed by the two-yields-of-one-event test) each fail their own test and nothing else that matters. The Responses pre-stream abort test no longer certifies itself through the provider's own catch: the mock captures the signal the SDK actually received and then throws a plain failure, so the only evidence left is that the captured signal is an AbortSignal that had already flipped; handing the SDK an unrelated signal instead of the bridge fails seven tests, including that one. The timeout matrix now pins signal identity rather than signal type - with no usable budget the SDK must hold the caller's own signal, with a usable one it must hold the merged deadline - and a new parameterized test parks each format's request under a 5000 ms budget, cancels the caller long before the deadline and requires the merged signal to flip and the completion to reject with the contract AbortError; replacing the merged deadline with the caller's own signal on the chat-completions completion fails 26 tests, including those rows. Three tests that polled for readiness before observing the rejection now attach the handler when the promise is created, so a model lookup or stream open that rejects during the poll cannot go unobserved. Verification on this round: vitest 224 passed (224) in declaration order and with --sequence.shuffle; tsc --noEmit -p tsconfig.json from src: zero errors; eslint . --ext=ts --max-warnings=0 without --prune-suppressions: exit 0 with src/eslint-suppressions.json byte-identical (a prune run over the two unit files found zero decreases, so the rewritten file was reverted); prettier 3.9.10 --check: clean. Review round 5 (mutation sticky at d4ce1ce123f8: blocking 23, advisories 0): this round adds no production lines. Three tests were added, each aimed at a variant the sticky lists: a cancel between the separator and the body of an indexed thinking block (kills the ConditionalExpression -> false variant at opencode-go.ts:796 and puts line 797 under coverage), and one run per streaming format that passes no abort signal at all, so every guard in the helper is exercised with an absent caller signal. Measured against the suite: each of the 17 StringLiteral -> "" rows (357, 380, 391, 407, 424, 567, 574, 626, 717, 725, 743, 797, 809, 858, 976, 1046, 1104) leaves 227/227 green, and each of the 5 OptionalChaining rows (625, 742, 796, 808, 857) leaves 227/227 green. The reason is structural, not a matter of unasserted detail: at the OptionalChaining lines the operand is the request-local signal that bridgeExternalAbortSignal/mergeAbortSignalAndTimeout always hand to the SDK, so it is never nullish there, and an inner createAbortError is replaced by the enclosing catch's own unmutated literal (for example the stream-open catch at 719 re-throwing at 725, and the completion catch at 981 re-throwing at 992), so the tests that do assert the exact contract message still see "The Opencode Go request was aborted". The one variant that is observably different is now killed: ConditionalExpression -> false at 796 fails the new thinking test (1 failed | 226 skipped) where it left the previous suite green. Verification on this round: vitest 227 passed (227) in declaration order and with --sequence.shuffle; tsc --noEmit -p tsconfig.json from src: zero errors; eslint . --ext=ts --max-warnings=0 without --prune-suppressions: exit 0, with eslint-suppressions.json unchanged because --prune-suppressions on the two unit files reported no count change; prettier 3.9.10 --check: clean. Review round 6 (ask: kill the StringLiteral family with test-side message assertions before anything else, no new production code). The spec's two remaining name-only assertions now pin the message too (spec:3824 and spec:3867, in the two "bounds %s model resolution by the request timeout" groups), so every name assertion in the file carries the contract message. That changed no survivor, and the reason is worth recording: with the message pinned everywhere, each of the 17 StringLiteral -> "" rows still leaves 227/227 green, because the message those tests read is produced by an enclosing catch that throws its own unmutated createAbortError("Opencode Go") - the createMessage-level catch at :279 and the Chat Completions stream helper's catch at :431 - and blanking either of those two literals fails 8 and 7 tests respectively, while blanking any guard or race label fails none. The five OptionalChaining rows are equivalent for a separate reason: the streaming helpers are always handed bridgedSignal ?? new AbortController().signal at :260, so their operand is never nullish; with all five optional chains removed at once the suite is still 227/227, including the two runs that pass no caller signal at all. No production line changed in either of the last two rounds; the provider blob is unchanged at 970de27474f6. Verification on this round: vitest 227 passed (227) in declaration order and with --sequence.shuffle; tsc --noEmit -p tsconfig.json from src: zero errors; eslint . --ext=ts --max-warnings=0 without --prune-suppressions: exit 0 with eslint-suppressions.json unchanged; prettier 3.9.10 --check: clean. Review round 7 (authorized: the pre-yield cancel check on the Responses path - one guard, one helper, one push, no gateway propagation). `streamResponsesMessage` now hands the shared transform an iterator whose `next()` reports the stream ended when the bridged signal is already aborted, so a pull that lands after the caller cancelled can no longer deliver the event the pinned decoder had already buffered; the after-loop guard then produces the contract AbortError. The helper's cleanup forward (`return: streamIterator.return?.bind(streamIterator)`) preserves the previous delegation behaviour - an early consumer exit still closes the SDK iterator twice, as the pre-existing cleanup test asserts - and stays callable when the source iterator implements no `return` at all. This helper's signal parameter is now typed `AbortSignal` (`createMessage` supplies `bridgedSignal ?? new AbortController().signal`), which is why the after-loop guard no longer needs its optional chain. Nothing was added to the three gateway helpers, as instructed. Two tests carry it: the buffered-completion fixture (text delta and `response.completed` from one read, take the delta, cancel, require the rejection and no usage item) and a next-only source iterator, which proves the cleanup forward must not assume `return` exists. Controls: against the pre-change provider the buffered fixture fails with 'expected undefined to match object { name: AbortError }' - today the usage chunk is delivered - so this was an uncovered gap, not a restatement of covered behaviour. For the new code: the pull guard's `ConditionalExpression -> false` fails exactly the two new tests and `-> true` fails 15; the `OptionalChaining` on the cleanup forward fails the next-only test; neutering the after-loop guard now fails 3 tests including the buffered fixture, which is the variant that fixture kills. Verification on this round: vitest 229 passed (229) in declaration order and with `--sequence.shuffle`; `tsc --noEmit -p tsconfig.json` from `src`: zero errors;`eslint . --ext=ts --max-warnings=0` without `--prune-suppressions`: exit 0 with `eslint-suppressions.json` unchanged and`--prune-suppressions` reporting no decrease; prettier 3.9.10 `--check`: clean. Against merge-base `75a93a9d9acc`: opencode-go.spec.ts +2543/-200=2743, opencode-go.ts +390/-91=481 = 3224 a+d. Review round 8 (CodeRabbit review 5982073495, line 3872 wall-clock row): the deadline test no longer measures elapsed time. It parks the SDK promise so it never settles, then asserts the observed abort reason (name AbortError plus the exact contract message), that the signal handed to the SDK really is an AbortSignal and had already flipped, and that a settled-flag attached to the parked promise is still false, so the rejection can only come from the provider's race; the 1s bound is gone (it was the assertion a stalled worker could fail) and the duplicated message assertion was deduplicated. The caller-cancel-under- budget test now reads the SDK-facing signal immediately before controller.abort() and immediately after it, with no await between the two reads, and the redundant post-settlement read was removed. Shared-deadline row: a new test parks the catalog fetcher, releases it, waits for the dispatch, and then asserts by reference identity that the dispatched request runs on the caller's own cancellation scope (with no timeoutMs, mergeAbortSignalAndTimeout returns the caller's signal unchanged at utils/abort-signal.ts:42-44), flips it, requires the flip on the SDK-facing signal with no await in between, and requires the completion to reject with the contract AbortError while the parked SDK promise is still unsettled. Controls: replacing signal: resolutionAbortSignal in the chat-completions dispatch with a fresh controller fails exactly that test (AssertionError: expected AbortSignal { aborted: false } to be AbortSignal { aborted: false }, 1 failed | 229 skipped), and forcing the merge's timeout arm off fails all three rows of the deadline test (3 failed). Signal identity for the timeout shapes was already pinned by assertTimeoutOption (spec:4056), which requires the caller's own object with no usable budget and a merged deadline distinct from it with a usable one. Verification on this round: vitest 230 passed (230) in declaration order and with --sequence.shuffle; tsc --noEmit -p tsconfig.json from src: zero errors; eslint . --ext=ts --max-warnings=0 without --prune-suppressions: exit 0 with eslint-suppressions.json unchanged (--prune-suppressions on the spec reported no count decrease, so the rewritten file was reverted); prettier 3.9.10 --check: clean. No production line changed in this round; the provider blob is unchanged at bb1acfe2dcc1. Against merge-base 75a93a9d9acc: opencode-go.spec.ts +2597/-191=2788, opencode-go.ts +390/-91=481 = 3269 a+d. --- .../providers/__tests__/opencode-go.spec.ts | 2918 +++++++++++++++-- src/api/providers/opencode-go.ts | 501 ++- 2 files changed, 3137 insertions(+), 282 deletions(-) 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/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}`) }