Skip to content

Commit a3c4e6b

Browse files
committed
fix(api): preserve abort identity in gateway-b completion error handling
1 parent 88a8446 commit a3c4e6b

8 files changed

Lines changed: 423 additions & 21 deletions

File tree

‎src/api/providers/__tests__/opencode-go.spec.ts‎

Lines changed: 106 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ vitest.mock("vscode", () => ({
99
},
1010
}))
1111

12-
import { Anthropic } from "@anthropic-ai/sdk"
13-
import OpenAI from "openai"
12+
import {
13+
Anthropic,
14+
APIConnectionTimeoutError as AnthropicTimeoutError,
15+
APIUserAbortError as AnthropicAbortError,
16+
} from "@anthropic-ai/sdk"
17+
import OpenAI, { APIConnectionTimeoutError, APIUserAbortError } from "openai"
1418

1519
import { opencodeGoDefaultModelId, opencodeGoModels, isOpencodeGoAnthropicFormatModel } from "@roo-code/types"
1620

@@ -49,15 +53,22 @@ const mockAnthropicCreate = vitest.fn()
4953
}
5054
})
5155

52-
vitest.mock("@anthropic-ai/sdk", () => ({
53-
Anthropic: vitest.fn(function () {
54-
return {
55-
messages: {
56-
create: mockAnthropicCreate,
57-
},
58-
}
59-
}),
60-
}))
56+
// The real SDK error classes are re-exported alongside the mocked client so
57+
// tests can emulate the SDK's abort/timeout rejections and the provider's
58+
// instanceof checks resolve against the same class identity.
59+
vitest.mock("@anthropic-ai/sdk", async () => {
60+
const actual = await vi.importActual<typeof import("@anthropic-ai/sdk")>("@anthropic-ai/sdk")
61+
return {
62+
...actual,
63+
Anthropic: vitest.fn(function () {
64+
return {
65+
messages: {
66+
create: mockAnthropicCreate,
67+
},
68+
}
69+
}),
70+
}
71+
})
6172

6273
describe("OpencodeGoHandler", () => {
6374
const mockOptions: ApiHandlerOptions = {
@@ -709,6 +720,48 @@ describe("OpencodeGoHandler", () => {
709720
expect(Object.keys(requestOptions ?? {})).not.toContain("timeout")
710721
})
711722

723+
it("completePrompt preserves abort identity when the caller aborts (Anthropic path)", async () => {
724+
// Emulate the Anthropic SDK: an aborted request signal rejects with
725+
// APIUserAbortError ("Request was aborted." — the trailing period would
726+
// fail task-level abort detection, so the provider must normalize it).
727+
mockAnthropicCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => {
728+
if (options?.signal?.aborted) {
729+
throw new AnthropicAbortError()
730+
}
731+
throw new Error("boom")
732+
})
733+
const handler = new OpencodeGoHandler(anthropicOptions)
734+
const controller = new AbortController()
735+
controller.abort()
736+
737+
const error = await handler.completePrompt("ping", { abortSignal: controller.signal }).then(
738+
() => undefined,
739+
(e: unknown) => e,
740+
)
741+
expect(error).toMatchObject({ name: "AbortError" })
742+
expect((error as Error).message.endsWith("aborted")).toBe(true)
743+
expect((error as Error).message).not.toContain("completion error")
744+
})
745+
746+
it("completePrompt surfaces request timeouts as an AbortError (Anthropic path)", async () => {
747+
// Emulate the Anthropic SDK: when the request timeout fires, the SDK
748+
// surfaces APIConnectionTimeoutError ("Request timed out.") once retries
749+
// are exhausted — verified against @anthropic-ai/sdk against a hung server.
750+
mockAnthropicCreate.mockImplementation(async (_params: unknown, options: { timeout?: number }) => {
751+
await new Promise((resolve) => setTimeout(resolve, options?.timeout ?? 50))
752+
throw new AnthropicTimeoutError()
753+
})
754+
const handler = new OpencodeGoHandler(anthropicOptions)
755+
756+
const error = await handler.completePrompt("ping", { timeoutMs: 50 }).then(
757+
() => undefined,
758+
(e: unknown) => e,
759+
)
760+
expect(error).toMatchObject({ name: "AbortError" })
761+
expect((error as Error).message.endsWith("aborted")).toBe(true)
762+
expect((error as Error).message).not.toContain("completion error")
763+
})
764+
712765
it("completePrompt works without options (backward compatible, Anthropic path)", async () => {
713766
mockAnthropicCreate.mockResolvedValue({ content: [{ type: "text", text: "response" }] })
714767
const handler = new OpencodeGoHandler(anthropicOptions)
@@ -798,6 +851,48 @@ describe("OpencodeGoHandler", () => {
798851
expect(requestOptions).not.toHaveProperty("timeout")
799852
})
800853

854+
it("completePrompt preserves abort identity when the caller aborts (OpenAI path)", async () => {
855+
// Emulate the OpenAI SDK: an aborted request signal rejects with
856+
// APIUserAbortError ("Request was aborted." — the trailing period would
857+
// fail task-level abort detection, so the provider must normalize it).
858+
mockCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => {
859+
if (options?.signal?.aborted) {
860+
throw new APIUserAbortError()
861+
}
862+
throw new Error("boom")
863+
})
864+
const handler = new OpencodeGoHandler(openaiOptions)
865+
const controller = new AbortController()
866+
controller.abort()
867+
868+
const error = await handler.completePrompt("ping", { abortSignal: controller.signal }).then(
869+
() => undefined,
870+
(e: unknown) => e,
871+
)
872+
expect(error).toMatchObject({ name: "AbortError" })
873+
expect((error as Error).message.endsWith("aborted")).toBe(true)
874+
expect((error as Error).message).not.toContain("completion error")
875+
})
876+
877+
it("completePrompt surfaces request timeouts as an AbortError (OpenAI path)", async () => {
878+
// Emulate the OpenAI SDK: when the request timeout fires, the SDK
879+
// surfaces APIConnectionTimeoutError ("Request timed out.") once retries
880+
// are exhausted — verified against openai v5.23.2 against a hung server.
881+
mockCreate.mockImplementation(async (_params: unknown, options: { timeout?: number }) => {
882+
await new Promise((resolve) => setTimeout(resolve, options?.timeout ?? 50))
883+
throw new APIConnectionTimeoutError()
884+
})
885+
const handler = new OpencodeGoHandler(openaiOptions)
886+
887+
const error = await handler.completePrompt("ping", { timeoutMs: 50 }).then(
888+
() => undefined,
889+
(e: unknown) => e,
890+
)
891+
expect(error).toMatchObject({ name: "AbortError" })
892+
expect((error as Error).message.endsWith("aborted")).toBe(true)
893+
expect((error as Error).message).not.toContain("completion error")
894+
})
895+
801896
it("completePrompt works without options (backward compatible, OpenAI path)", async () => {
802897
mockCreate.mockResolvedValueOnce({
803898
choices: [{ message: { content: "response" } }],

‎src/api/providers/__tests__/unbound.spec.ts‎

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Anthropic } from "@anthropic-ai/sdk"
2-
import OpenAI from "openai"
2+
import OpenAI, { APIConnectionTimeoutError, APIUserAbortError } from "openai"
33

44
import { UnboundHandler } from "../unbound"
55
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
@@ -10,8 +10,13 @@ import { makeCreateMessageMetadata } from "../../../test-utils/api"
1010
// can configure the SDK `create` call without untyped access casts.
1111
const sharedMockCreate = vi.hoisted(() => vi.fn())
1212

13-
vi.mock("openai", () => {
13+
// The real SDK error classes are re-exported alongside the mocked client so
14+
// tests can emulate the SDK's abort/timeout rejections (APIUserAbortError,
15+
// APIConnectionTimeoutError) and the provider's instanceof checks resolve.
16+
vi.mock("openai", async () => {
17+
const actual = await vi.importActual<typeof import("openai")>("openai")
1418
return {
19+
...actual,
1520
default: vi.fn(function () {
1621
return {
1722
chat: {
@@ -257,6 +262,56 @@ describe("UnboundHandler", () => {
257262
const requestOptions = call[1] as { timeout?: number } | undefined
258263
expect(requestOptions).not.toHaveProperty("timeout")
259264
})
265+
266+
it("completePrompt should preserve abort identity when the caller aborts", async () => {
267+
// Emulate the OpenAI SDK: an aborted request signal rejects with
268+
// APIUserAbortError ("Request was aborted." — the trailing period would
269+
// fail task-level abort detection, so the provider must normalize it).
270+
sharedMockCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => {
271+
if (options?.signal?.aborted) {
272+
throw new APIUserAbortError()
273+
}
274+
throw new Error("boom")
275+
})
276+
277+
const handler = new UnboundHandler({
278+
unboundApiKey: "test-key",
279+
unboundModelId: "openai/gpt-4o",
280+
})
281+
const controller = new AbortController()
282+
controller.abort()
283+
284+
const error = await handler.completePrompt("Write a haiku", { abortSignal: controller.signal }).then(
285+
() => undefined,
286+
(e: unknown) => e,
287+
)
288+
expect(error).toMatchObject({ name: "AbortError" })
289+
expect((error as Error).message.endsWith("aborted")).toBe(true)
290+
expect((error as Error).message).not.toContain("completion error")
291+
})
292+
293+
it("completePrompt should surface request timeouts as an AbortError", async () => {
294+
// Emulate the OpenAI SDK: when the request timeout fires, the SDK
295+
// surfaces APIConnectionTimeoutError ("Request timed out.") once retries
296+
// are exhausted — verified against openai v5.23.2 against a hung server.
297+
sharedMockCreate.mockImplementation(async (_params: unknown, options: { timeout?: number }) => {
298+
await new Promise((resolve) => setTimeout(resolve, options?.timeout ?? 50))
299+
throw new APIConnectionTimeoutError()
300+
})
301+
302+
const handler = new UnboundHandler({
303+
unboundApiKey: "test-key",
304+
unboundModelId: "openai/gpt-4o",
305+
})
306+
307+
const error = await handler.completePrompt("Write a haiku", { timeoutMs: 50 }).then(
308+
() => undefined,
309+
(e: unknown) => e,
310+
)
311+
expect(error).toMatchObject({ name: "AbortError" })
312+
expect((error as Error).message.endsWith("aborted")).toBe(true)
313+
expect((error as Error).message).not.toContain("completion error")
314+
})
260315
it("completePrompt should work without options (backward compatible)", async () => {
261316
sharedMockCreate.mockResolvedValue({
262317
choices: [{ message: { content: "completed text" } }],
@@ -294,7 +349,12 @@ describe("UnboundHandler", () => {
294349
makeCreateMessageMetadata({ abortSignal: controller.signal }),
295350
)
296351

297-
await expect(collectStream(stream)).rejects.toThrow("Unbound completion error: The operation was aborted.")
352+
// The bridge surfaces a DOM-standard AbortError (series standard)
353+
// instead of the wrapped completion error.
354+
await expect(collectStream(stream)).rejects.toMatchObject({
355+
name: "AbortError",
356+
message: "Unbound request aborted",
357+
})
298358
expect(requestError).toMatchObject({ name: "AbortError" })
299359
})
300360

‎src/api/providers/__tests__/vercel-ai-gateway.spec.ts‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ vitest.mock("vscode", () => ({
1010
}))
1111

1212
import { Anthropic } from "@anthropic-ai/sdk"
13-
import OpenAI from "openai"
13+
import OpenAI, { APIConnectionTimeoutError, APIUserAbortError } from "openai"
1414

1515
import { VercelAiGatewayHandler } from "../vercel-ai-gateway"
1616
import { makeApiHandlerOptions, makeCreateMessageMetadata } from "../../../test-utils/api"
@@ -777,6 +777,50 @@ describe("VercelAiGatewayHandler", () => {
777777
// without a timeout key.
778778
expect(Object.keys(requestOptions ?? {})).not.toContain("timeout")
779779
})
780+
781+
it("should preserve abort identity when the caller aborts", async () => {
782+
// Emulate the OpenAI SDK: an aborted request signal rejects with
783+
// APIUserAbortError ("Request was aborted." — the trailing period would
784+
// fail task-level abort detection, so the provider must normalize it).
785+
mockCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => {
786+
if (options?.signal?.aborted) {
787+
throw new APIUserAbortError()
788+
}
789+
throw new Error("boom")
790+
})
791+
792+
const handler = new VercelAiGatewayHandler(mockOptions)
793+
const controller = new AbortController()
794+
controller.abort()
795+
796+
const error = await handler.completePrompt("test prompt", { abortSignal: controller.signal }).then(
797+
() => undefined,
798+
(e: unknown) => e,
799+
)
800+
expect(error).toMatchObject({ name: "AbortError" })
801+
expect((error as Error).message.endsWith("aborted")).toBe(true)
802+
expect((error as Error).message).not.toContain("completion error")
803+
})
804+
805+
it("should surface request timeouts as an AbortError", async () => {
806+
// Emulate the OpenAI SDK: when the request timeout fires, the SDK
807+
// surfaces APIConnectionTimeoutError ("Request timed out.") once retries
808+
// are exhausted — verified against openai v5.23.2 against a hung server.
809+
mockCreate.mockImplementation(async (_params: unknown, options: { timeout?: number }) => {
810+
await new Promise((resolve) => setTimeout(resolve, options?.timeout ?? 50))
811+
throw new APIConnectionTimeoutError()
812+
})
813+
814+
const handler = new VercelAiGatewayHandler(mockOptions)
815+
816+
const error = await handler.completePrompt("test prompt", { timeoutMs: 50 }).then(
817+
() => undefined,
818+
(e: unknown) => e,
819+
)
820+
expect(error).toMatchObject({ name: "AbortError" })
821+
expect((error as Error).message.endsWith("aborted")).toBe(true)
822+
expect((error as Error).message).not.toContain("completion error")
823+
})
780824
it("should work without options (backward compatible)", async () => {
781825
const handler = new VercelAiGatewayHandler(mockOptions)
782826
mockCreate.mockImplementation(async () => ({

‎src/api/providers/__tests__/zoo-gateway.spec.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ vitest.mock("../../../i18n", () => ({
2626
t: (key: string) => key,
2727
}))
2828

29-
import OpenAI from "openai"
29+
import OpenAI, { APIConnectionTimeoutError, APIUserAbortError } from "openai"
3030

3131
import { zooGatewayDefaultModelId, ZOO_GATEWAY_DEFAULT_TEMPERATURE } from "@roo-code/types"
3232

@@ -502,6 +502,49 @@ describe("ZooGatewayHandler", () => {
502502
expect(requestOptions).not.toHaveProperty("timeout")
503503
})
504504

505+
it("should preserve abort identity when the caller aborts", async () => {
506+
// Emulate the OpenAI SDK: an aborted request signal rejects with
507+
// APIUserAbortError ("Request was aborted." — the trailing period would
508+
// fail task-level abort detection, so the provider must normalize it).
509+
mockCreate.mockImplementation(async (_params: unknown, options: { signal?: AbortSignal }) => {
510+
if (options?.signal?.aborted) {
511+
throw new APIUserAbortError()
512+
}
513+
throw new Error("boom")
514+
})
515+
516+
const handler = new ZooGatewayHandler(mockOptions)
517+
const controller = new AbortController()
518+
controller.abort()
519+
520+
const error = await handler.completePrompt("test prompt", { abortSignal: controller.signal }).then(
521+
() => undefined,
522+
(e: unknown) => e,
523+
)
524+
expect(error).toMatchObject({ name: "AbortError" })
525+
expect((error as Error).message.endsWith("aborted")).toBe(true)
526+
expect((error as Error).message).not.toContain("completion error")
527+
})
528+
529+
it("should surface request timeouts as an AbortError", async () => {
530+
// Emulate the OpenAI SDK: when the request timeout fires, the SDK
531+
// surfaces APIConnectionTimeoutError ("Request timed out.") once retries
532+
// are exhausted — verified against openai v5.23.2 against a hung server.
533+
mockCreate.mockImplementation(async (_params: unknown, options: { timeout?: number }) => {
534+
await new Promise((resolve) => setTimeout(resolve, options?.timeout ?? 50))
535+
throw new APIConnectionTimeoutError()
536+
})
537+
538+
const handler = new ZooGatewayHandler(mockOptions)
539+
540+
const error = await handler.completePrompt("test prompt", { timeoutMs: 50 }).then(
541+
() => undefined,
542+
(e: unknown) => e,
543+
)
544+
expect(error).toMatchObject({ name: "AbortError" })
545+
expect((error as Error).message.endsWith("aborted")).toBe(true)
546+
expect((error as Error).message).not.toContain("completion error")
547+
})
505548
it("should work without options (backward compatible)", async () => {
506549
const handler = new ZooGatewayHandler(mockOptions)
507550
mockCreate.mockImplementation(async () => ({

0 commit comments

Comments
 (0)