Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
e61feb1
feat(api): add throwIfAborted helper and completePrompt options regre…
easonliang28 Aug 19, 2026
e65cc08
feat(api): abort signal support for openai, openai-compatible base, z…
easonLiangWorldedtech Aug 20, 2026
4a31692
test(providers): export APIUserAbortError from openai mock in firewor…
easonLiangWorldedtech Aug 20, 2026
35c95ea
fix(api): normalize abort errors raised during stream iteration
easonLiangWorldedtech Aug 20, 2026
217f120
test(providers): cover the base_resp stream error path through the it…
easonLiangWorldedtech Aug 20, 2026
af320da
test(providers): cover remaining branch partials in base provider and…
easonLiangWorldedtech Aug 21, 2026
a0117fb
feat(api): add shared isRequestAborted and createAbortError helpers t…
easonLiangWorldedtech Aug 21, 2026
36f0b42
Merge branch 'feat/abort-r1-foundation' into feat/abort-r2-openai-family
easonLiangWorldedtech Aug 21, 2026
8632025
Merge remote-tracking branch 'upstream/main' into feat/abort-r2-opena…
easonliang28 Sep 2, 2026
20b1228
test(zai): align GLM-5.3-Flash expectations with abort-signal request…
easonliang28 Sep 2, 2026
8ce2489
refactor(api): dedupe handleOpenAIRequestError and strengthen abort-s…
easonliang28 Sep 2, 2026
cb77e0c
fix(api): reject pre-aborted Kimi requests and forward per-request ti…
easonliang28 Sep 2, 2026
c9311dc
fix(api): forward per-request timeoutMs and harden abort specs
easonliang28 Sep 2, 2026
707ddf7
Merge upstream/main (79cd12f2c) into feat/abort-r2-openai-family
easonliang28 Sep 3, 2026
0703c58
fix(ci): declare vitest as a root devDependency so the mutation gate …
easonliang28 Sep 3, 2026
09a0c80
test(api): strengthen focused tests for the changed-code mutation gate
easonliang28 Sep 3, 2026
805f297
test(api): exclude proven-equivalent Stryker mutants from the changed…
easonliang28 Sep 3, 2026
1e0b137
Merge branch 'main' into feat/abort-r2-openai-family
easonLiangWorldedtech Sep 3, 2026
7b31a5b
Merge upstream/main (0d937c050) into feat/abort-r2-openai-family
easonliang28 Sep 4, 2026
42bcdab
Merge remote-tracking branch 'upstream/main' into feat/abort-r2-opena…
easonliang28 Sep 5, 2026
35b1d10
Merge branch main into feat/abort-r2-openai-family
easonliang28 Sep 6, 2026
bccf293
test(api): cover captureError non-Error re-wrap and resolving-contrac…
easonliang28 Sep 7, 2026
74e406e
test(api): add abort-aware error-handler tests and assert Kimi retry …
easonliang28 Sep 7, 2026
a3be5ec
Merge branch 'main' into feat/abort-r2-openai-family
easonLiangWorldedtech Sep 7, 2026
25b6446
Merge remote-tracking branch 'upstream/main' into feat/abort-r2-opena…
easonliang28 Sep 13, 2026
393b516
fix(api): enforce abort-signal loop defense in openai-family streamin…
easonliang28 Sep 13, 2026
7dc331f
Merge branch 'main' into feat/abort-r2-openai-family
easonLiangWorldedtech Sep 29, 2026
4db5623
chore(deps): regenerate pnpm-lock.yaml after main merge
Sep 29, 2026
4238bd1
fix(deps): bump vitest 4.1.9 -> 4.1.11 (dependency-review advisory)
Sep 30, 2026
34a87d5
test(visual): regenerate electron chat-dark sidebar reference for #1311
Sep 30, 2026
9ece5d9
test(visual): restore original chat-dark sidebar reference
Sep 30, 2026
ac0c847
test(openai): make the timeout-only signal test observe the abort
Sep 30, 2026
c44241b
test(kimi-code): keep the SDK create signature in the spy helper
Sep 30, 2026
c2ad99c
Merge remote-tracking branch 'upstream/main' into feat/abort-r2-opena…
Oct 2, 2026
833f5eb
chore: trigger label reconcile (CI green + CR approved, cron stalled)
Oct 2, 2026
74f5113
Merge commit 'refs/shapes/upstream-main' into feat/abort-r2-openai-fa…
Oct 2, 2026
cbcc610
Merge upstream main (3859e5dd8) into feat/abort-r2-openai-family
Oct 4, 2026
fe2cca9
Re-trigger review at the current head
Oct 4, 2026
dbe734f
Distinguish a request timeout from a user cancellation
Oct 4, 2026
c81a73f
Re-trigger review at head: all required checks green and every review…
Oct 4, 2026
ebf71a1
Cover timeout classification through the provider call path
Oct 4, 2026
49467c8
Keep boolean base_resp status codes reported as errors
Oct 4, 2026
7a2d9da
Match the status_msg fallback between the two paths
Oct 4, 2026
1d087ac
Merge branch 'main' into feat/abort-r2-openai-family
easonLiangWorldedtech Oct 5, 2026
cd52589
test(api): pin the two-argument create() call in the completePrompt a…
Oct 5, 2026
a18aee8
Merge upstream/main into feat/abort-r2-openai-family
Oct 7, 2026
833268c
fix(api): cancel Kimi model discovery with the caller, and pin the fl…
Oct 8, 2026
17ab14d
test(api): pin Kimi cancellation that lands during model preparation
Oct 8, 2026
1b248f0
test(api): pin the Kimi signal fallback in the model cache
Oct 8, 2026
6e46bc7
Merge upstream/main 995167eed into feat/abort-r2-openai-family
easonliang28 Oct 12, 2026
9798cf1
style: satisfy the compile job format:check gate on this PR's files
easonliang28 Oct 12, 2026
e12539a
Merge upstream main (285f6687a) into feat/abort-r2-openai-family
easonliang28 Oct 13, 2026
7f085ab
Address review: keep the discovery attempt open across cancellation
easonliang28 Oct 13, 2026
5525ff0
Merge upstream/main (58341d882) into feat/abort-r2-openai-family
easonliang28 Oct 14, 2026
0dc3ca5
chore(api): remove Stryker suppression comments from touched files
easonliang28 Oct 14, 2026
9a9f1da
fix(api): reject non-finite and cap overflowing mergeAbortSignalAndTi…
easonliang28 Oct 14, 2026
f257a54
fix(api): round fractional timeouts before AbortSignal.timeout
easonliang28 Oct 14, 2026
e3360c1
test(api): assert the rounded delay reaching AbortSignal.timeout
easonliang28 Oct 14, 2026
6088f40
fix(api): forward discovery cancellation into the Kimi catalog fetch
easonliang28 Oct 14, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// npx vitest run api/providers/__tests__/base-openai-compatible-provider.spec.ts

import { Anthropic } from "@anthropic-ai/sdk"
import OpenAI from "openai"
import OpenAI, { APIUserAbortError } from "openai"

import type { ModelInfo } from "@roo-code/types"

Expand All @@ -14,6 +14,8 @@ const mockCreate = vi.fn()

// Mock OpenAI module
vi.mock("openai", () => ({
// Named export consumed by the provider for abort-error normalization
APIUserAbortError: class extends Error {},
default: vi.fn(function () {
return {
chat: {
Expand Down Expand Up @@ -49,6 +51,20 @@ class TestOpenAiCompatibleProvider extends BaseOpenAiCompatibleProvider<"test-mo
}
}

/**
* Captures the rejection of an operation as an Error. The abort contract always
* throws an Error; the guard keeps strict typing without a cast. Fails the test
* if the operation resolves.
*/
async function captureError(operation: Promise<unknown>): Promise<Error> {
try {
await operation
} catch (error) {
return error instanceof Error ? error : new Error(String(error))
}
throw new Error("Expected the operation to reject")
}

describe("BaseOpenAiCompatibleProvider", () => {
let handler: TestOpenAiCompatibleProvider

Expand Down Expand Up @@ -278,6 +294,171 @@ describe("BaseOpenAiCompatibleProvider", () => {
})
})

describe("abort signal wiring", () => {
it("should pass the metadata abort signal to the client request", async () => {
const controller = new AbortController()
mockCreate.mockImplementationOnce(() => asyncStreamFrom([]))

const stream = handler.createMessage("system prompt", [], {
taskId: "test-task",
abortSignal: controller.signal,
})
await stream.next()

expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ model: "test-model" }), {
signal: controller.signal,
})
})

it("should reject before issuing any request when the abort signal is already aborted", async () => {
const controller = new AbortController()
controller.abort()

await expect(async () => {
for await (const _ of handler.createMessage("system prompt", [], {
taskId: "test-task",
abortSignal: controller.signal,
})) {
// consume
}
}).rejects.toMatchObject({ name: "AbortError", message: "This operation was aborted" })
expect(mockCreate).not.toHaveBeenCalled()
})

it("should normalize the SDK APIUserAbortError from the stream path into the abort contract", async () => {
mockCreate.mockImplementationOnce(() => {
throw new APIUserAbortError()
})

const result = await captureError(
(async () => {
for await (const _ of handler.createMessage("system prompt", [])) {
// consume
}
})(),
)

// The SDK error has name "Error" and a message ending in a period; the
// provider must rethrow the Task.ts contract shape instead.
expect(result.name).toBe("AbortError")
expect(result.message).toBe("TestProvider request aborted")
expect(result.message.endsWith("aborted")).toBe(true)
})

it("should normalize an abort error raised during stream iteration into the abort contract", async () => {
mockCreate.mockImplementationOnce(() =>
(async function* () {
yield { choices: [{ delta: { content: "partial" } }] }
throw new APIUserAbortError()
})(),
)

const result = await captureError(
(async () => {
for await (const _ of handler.createMessage("system prompt", [])) {
// consume
}
})(),
)

// The iterator rejects after the first chunk; the provider must normalize
// it to the Task.ts contract shape (name + message ending in "aborted").
expect(result.name).toBe("AbortError")
expect(result.message).toBe("TestProvider request aborted")
expect(result.message.endsWith("aborted")).toBe(true)
})

it("should wrap a non-abort base_resp stream error with the provider prefix through the iteration wrapper", async () => {
mockCreate.mockImplementationOnce(() =>
asyncStreamFrom([
{
choices: [{ delta: { content: "partial" } }],
base_resp: { status_code: 1041, status_msg: "Invalid token" },
},
]),
)

const result = await captureError(
(async () => {
for await (const _ of handler.createMessage("system prompt", [])) {
// consume
}
})(),
)

expect(result.message).toBe("TestProvider completion error: TestProvider API Error (1041): Invalid token")
})
it("should still wrap non-abort request errors with the provider prefix", async () => {
mockCreate.mockImplementationOnce(() => {
throw new Error("boom")
})

const result = await captureError(
(async () => {
for await (const _ of handler.createMessage("system prompt", [])) {
// consume
}
})(),
)

expect(result.message).toBe("TestProvider completion error: boom")
})

it("should pass the completePrompt abort signal to the client request", async () => {
const controller = new AbortController()
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "response" } }] })

const result = await handler.completePrompt("test prompt", { abortSignal: controller.signal })

expect(result).toBe("response")
expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ model: "test-model" }), {
signal: controller.signal,
})
})

it("should merge completePrompt timeoutMs into the request signal", async () => {
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "response" } }] })

await handler.completePrompt("test prompt", { timeoutMs: 5000 })

const requestOptions = mockCreate.mock.calls.at(-1)?.[1]
expect(requestOptions?.signal).toBeInstanceOf(AbortSignal)
expect(requestOptions?.signal.aborted).toBe(false)
})

it("should not set a request signal for zero completePrompt timeoutMs", async () => {
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "response" } }] })

await handler.completePrompt("test prompt", { timeoutMs: 0 })

expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ model: "test-model" }), undefined)
})

it("should reject before any request when the completePrompt signal is already aborted", async () => {
const controller = new AbortController()
controller.abort()

await expect(
handler.completePrompt("test prompt", { abortSignal: controller.signal }),
).rejects.toMatchObject({
name: "AbortError",
message: "This operation was aborted",
})
expect(mockCreate).not.toHaveBeenCalled()
})

it("should normalize the SDK APIUserAbortError from completePrompt", async () => {
mockCreate.mockImplementationOnce(() => {
throw new APIUserAbortError()
})

const result = await captureError(handler.completePrompt("test prompt"))

expect(result.name).toBe("AbortError")
expect(result.message).toBe("TestProvider request aborted")
})
})

describe("Tool call handling", () => {
it("should yield tool_call_end events when finish_reason is tool_calls", async () => {
mockCreate.mockImplementationOnce(() =>
Expand Down
29 changes: 29 additions & 0 deletions src/api/providers/__tests__/complete-prompt-options.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, it, expect } from "vitest"

import type { CompletePromptOptions } from "../../index"

describe("CompletePromptOptions", () => {
it("should allow abortSignal property", () => {
const controller = new AbortController()
const options: CompletePromptOptions = { abortSignal: controller.signal }
expect(options.abortSignal).toBe(controller.signal)
})

it("should allow timeoutMs property", () => {
const options: CompletePromptOptions = { timeoutMs: 5000 }
expect(options.timeoutMs).toBe(5000)
})

it("should allow both abortSignal and timeoutMs together", () => {
const controller = new AbortController()
const options: CompletePromptOptions = { abortSignal: controller.signal, timeoutMs: 10000 }
expect(options.abortSignal).toBe(controller.signal)
expect(options.timeoutMs).toBe(10000)
})

it("should allow empty options object", () => {
const options: CompletePromptOptions = {}
expect(options.abortSignal).toBeUndefined()
expect(options.timeoutMs).toBeUndefined()
})
})
2 changes: 2 additions & 0 deletions src/api/providers/__tests__/fireworks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ vi.mock("openai", () => ({
},
}
}),
// Named export consumed by the provider for abort-error normalization
APIUserAbortError: class extends Error {},
}))

describe("FireworksHandler", () => {
Expand Down
114 changes: 112 additions & 2 deletions src/api/providers/__tests__/kimi-code.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,32 @@ vi.mock("../../../integrations/kimi-code/oauth", () => ({

vi.mock("../fetchers/modelCache", () => ({ getModels: mockGetModels }))

/**
* Spies on the inherited OpenAI client's chat.completions.create. `client` is
* protected on the OpenAiHandler base (not on the public interface), so it is
* reached through a documented `as unknown as` double assertion (AGENTS.md
* last resort; no `as any`).
*/
function completionsCreate(handler: KimiCodeHandler): ReturnType<typeof vi.fn> {
const client = (
handler as unknown as {
client: { chat: { completions: Record<string, (...args: never[]) => never> } }
}
).client
return vi.spyOn(client.chat.completions, "create") as unknown as ReturnType<typeof vi.fn>
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
}

/** Captures the rejection of an operation; fails the test if it resolves. */
async function captureError(operation: Promise<unknown>): Promise<Error> {
try {
await operation
} catch (error) {
// Abort normalization always throws an Error; the guard keeps strict typing without casts.
return error instanceof Error ? error : new Error(String(error))
}
throw new Error("Expected the operation to reject")
}

describe("KimiCodeHandler", () => {
beforeEach(() => {
clearAllMocks()
Expand Down Expand Up @@ -117,8 +143,7 @@ describe("KimiCodeHandler", () => {
it("force-refreshes and retries exactly once after a non-streaming OAuth 401", async () => {
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "oauth" })
const unauthorized = Object.assign(new Error("Unauthorized"), { status: 401 })
const createCompletion = vi
.spyOn((handler as any).client.chat.completions, "create")
const createCompletion = completionsCreate(handler)
.mockRejectedValueOnce(unauthorized)
.mockResolvedValueOnce({ choices: [{ message: { content: "retried" } }] })

Expand Down Expand Up @@ -235,4 +260,89 @@ describe("KimiCodeHandler", () => {
})
expect(handler.getModel().reasoning).toEqual({ reasoning_effort: "max" })
})

it("forwards the metadata abort signal to the inherited OpenAI SDK request", async () => {
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "api-key", kimiCodeApiKey: "key" })
const controller = new AbortController()
const streamChunks = (async function* () {
yield { choices: [{ delta: { content: "hi" } }] }
})()
const createCompletion = completionsCreate(handler).mockResolvedValueOnce(streamChunks)

const gen = handler.createMessage("system", [{ role: "user", content: "test" }], {
taskId: "test-task",
abortSignal: controller.signal,
})
const first = await gen.next()

expect(first.value).toEqual({ type: "text", text: "hi" })
expect(createCompletion).toHaveBeenCalledWith(expect.anything(), { signal: controller.signal })
})

it("rejects before any request when the createMessage abort signal is already aborted", async () => {
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "api-key", kimiCodeApiKey: "key" })
const controller = new AbortController()
controller.abort()
const createCompletion = completionsCreate(handler)

const gen = handler.createMessage("system", [{ role: "user", content: "test" }], {
taskId: "test-task",
abortSignal: controller.signal,
})

await expect(async () => {
for await (const _ of gen) {
// consume
}
}).rejects.toMatchObject({ name: "AbortError", message: "This operation was aborted" })
expect(createCompletion).not.toHaveBeenCalled()
Comment thread
coderabbitai[bot] marked this conversation as resolved.
})

it("forwards completePrompt abort options through the override on both 401 retry attempts", async () => {
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "oauth" })
const unauthorized = Object.assign(new Error("Unauthorized"), { status: 401 })
const createCompletion = completionsCreate(handler)
.mockRejectedValueOnce(unauthorized)
.mockResolvedValueOnce({ choices: [{ message: { content: "retried" } }] })
const controller = new AbortController()

await expect(handler.completePrompt("test", { abortSignal: controller.signal })).resolves.toBe("retried")
expect(mockForceRefreshAccessToken).toHaveBeenCalledOnce()
expect(createCompletion).toHaveBeenCalledTimes(2)
for (const call of createCompletion.mock.calls) {
expect(call[1]).toEqual({ signal: controller.signal })
}
})

it("rejects before any request when the completePrompt signal is already aborted", async () => {
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "api-key", kimiCodeApiKey: "key" })
const controller = new AbortController()
controller.abort()
const createCompletion = completionsCreate(handler)

await expect(handler.completePrompt("test", { abortSignal: controller.signal })).rejects.toMatchObject({
name: "AbortError",
message: "This operation was aborted",
})
expect(createCompletion).not.toHaveBeenCalled()
})

it("surfaces a normalized AbortError when the SDK aborts a streaming request", async () => {
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "api-key", kimiCodeApiKey: "key" })
// The real SDK class: this spec does not mock the openai module.
const { APIUserAbortError } = await import("openai")
completionsCreate(handler).mockRejectedValueOnce(new APIUserAbortError())

const gen = handler.createMessage("system", [{ role: "user", content: "test" }], { taskId: "test-task" })
const result = await captureError(
(async () => {
for await (const _ of gen) {
// consume
}
})(),
)

expect(result.name).toBe("AbortError")
expect(result.message).toBe("OpenAI request aborted")
})
})
Loading
Loading