Skip to content

Commit 7043629

Browse files
test(providers): pin timeout rounding and the timer cap across the anthropic family
Review follow-ups: move the mergeAbortSignalAndTimeout JSDoc back above its function (MAX_TIMEOUT_MS and toSdkTimeoutMs now sit before it); the completePrompt timeout tests in the anthropic, anthropic-vertex, minimax and xai specs are now it.each cases covering a whole-number, a fractional (12.5 -> 13) and an over-cap (MAX+1 -> MAX) value, so forwarding the raw timeoutMs instead of calling toSdkTimeoutMs fails in every provider. Affected suites 364 passed.
1 parent d1c4636 commit 7043629

5 files changed

Lines changed: 54 additions & 24 deletions

File tree

‎src/api/providers/__tests__/anthropic-vertex.spec.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { GoogleAuth } from "google-auth-library"
77
import { VERTEX_1M_CONTEXT_MODEL_IDS } from "@roo-code/types"
88

99
import { AnthropicVertexHandler } from "../anthropic-vertex"
10+
import { MAX_TIMEOUT_MS } from "../utils/abort-signal"
1011
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
1112
import { makeCreateMessageMetadata } from "../../../test-utils/api"
1213

@@ -1077,15 +1078,21 @@ describe("VertexHandler", () => {
10771078
expect(requestOptions?.signal).toBe(controller.signal)
10781079
})
10791080

1080-
it("completePrompt should pass timeoutMs when provided", async () => {
1081+
it.each([
1082+
{ label: "a whole-number", input: 3000, forwarded: 3000 },
1083+
// Only a call through toSdkTimeoutMs rounds this up.
1084+
{ label: "a fractional", input: 12.5, forwarded: 13 },
1085+
// Only a call through toSdkTimeoutMs caps at the Node timer maximum.
1086+
{ label: "an over-cap", input: MAX_TIMEOUT_MS + 1, forwarded: MAX_TIMEOUT_MS },
1087+
])("completePrompt should forward $label timeoutMs as $forwarded", async ({ input, forwarded }) => {
10811088
const mockCreate = vitest
10821089
.spyOn(handler["client"].messages, "create")
10831090
.mockResolvedValue({ content: [{ type: "text", text: "response" }] } as never)
10841091

1085-
await handler.completePrompt("test prompt", { timeoutMs: 3000 })
1092+
await handler.completePrompt("test prompt", { timeoutMs: input })
10861093
expect(mockCreate).toHaveBeenCalledWith(
10871094
expect.objectContaining({ model: expect.any(String) }),
1088-
expect.objectContaining({ timeout: 3000 }),
1095+
expect.objectContaining({ timeout: forwarded }),
10891096
)
10901097
})
10911098

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// npx vitest run src/api/providers/__tests__/anthropic.spec.ts
22

33
import { AnthropicHandler } from "../anthropic"
4+
import { MAX_TIMEOUT_MS } from "../utils/abort-signal"
45
import { ApiHandlerOptions } from "../../../shared/api"
56
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
67
import { makeCreateMessageMetadata } from "../../../test-utils/api"
@@ -1153,11 +1154,16 @@ describe("AnthropicHandler", () => {
11531154
}
11541155
})
11551156

1156-
it("should round a fractional timeoutMs up before forwarding it", async () => {
1157+
it.each([
1158+
// Only a call through toSdkTimeoutMs rounds this up.
1159+
{ label: "a fractional", input: 12.5, forwarded: 13 },
1160+
// Only a call through toSdkTimeoutMs caps at the Node timer maximum.
1161+
{ label: "an over-cap", input: MAX_TIMEOUT_MS + 1, forwarded: MAX_TIMEOUT_MS },
1162+
])("should forward $label timeoutMs as $forwarded", async ({ input, forwarded }) => {
11571163
mockCreate.mockResolvedValueOnce({ content: [{ type: "text", text: "response" }] })
1158-
await handler.completePrompt("test prompt", { timeoutMs: 12.5 })
1164+
await handler.completePrompt("test prompt", { timeoutMs: input })
11591165
expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ model: mockOptions.apiModelId }), {
1160-
timeout: 13,
1166+
timeout: forwarded,
11611167
})
11621168
})
11631169
})

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { Anthropic } from "@anthropic-ai/sdk"
1313
import { type MinimaxModelId, minimaxDefaultModelId, minimaxModels } from "@roo-code/types"
1414

1515
import { MiniMaxHandler } from "../minimax"
16+
import { MAX_TIMEOUT_MS } from "../utils/abort-signal"
1617
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
1718
import { makeCreateMessageMetadata } from "../../../test-utils/api"
1819
import { clearAllMocks } from "../../../test-utils/reset"
@@ -276,13 +277,19 @@ describe("MiniMaxHandler", () => {
276277
})
277278
})
278279

279-
it("should pass only timeoutMs when no signal provided", async () => {
280+
it.each([
281+
{ label: "a whole-number", input: 3000, forwarded: 3000 },
282+
// Only a call through toSdkTimeoutMs rounds this up.
283+
{ label: "a fractional", input: 12.5, forwarded: 13 },
284+
// Only a call through toSdkTimeoutMs caps at the Node timer maximum.
285+
{ label: "an over-cap", input: MAX_TIMEOUT_MS + 1, forwarded: MAX_TIMEOUT_MS },
286+
])("should forward $label timeoutMs as $forwarded when no signal provided", async ({ input, forwarded }) => {
280287
mockCreate.mockResolvedValueOnce({
281288
content: [{ type: "text", text: "response" }],
282289
})
283-
await handler.completePrompt("test prompt", { timeoutMs: 3000 })
290+
await handler.completePrompt("test prompt", { timeoutMs: input })
284291
expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ model: expect.any(String) }), {
285-
timeout: 3000,
292+
timeout: forwarded,
286293
})
287294
})
288295

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import OpenAI, { APIUserAbortError } from "openai"
2828
import { xaiDefaultModelId, xaiModels } from "@roo-code/types"
2929

3030
import { XAIHandler } from "../xai"
31+
import { MAX_TIMEOUT_MS } from "../utils/abort-signal"
3132
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
3233
import { makeCreateMessageMetadata } from "../../../test-utils/api"
3334
import { clearAllMocks } from "../../../test-utils/reset"
@@ -387,14 +388,23 @@ describe("XAIHandler", () => {
387388
})
388389
})
389390

390-
it("completePrompt should pass only timeoutMs when no signal provided", async () => {
391-
mockResponsesCreate.mockResolvedValueOnce({ output_text: "response" })
391+
it.each([
392+
{ label: "a whole-number", input: 3000, forwarded: 3000 },
393+
// Only a call through toSdkTimeoutMs rounds this up.
394+
{ label: "a fractional", input: 12.5, forwarded: 13 },
395+
// Only a call through toSdkTimeoutMs caps at the Node timer maximum.
396+
{ label: "an over-cap", input: MAX_TIMEOUT_MS + 1, forwarded: MAX_TIMEOUT_MS },
397+
])(
398+
"completePrompt should forward $label timeoutMs as $forwarded when no signal provided",
399+
async ({ input, forwarded }) => {
400+
mockResponsesCreate.mockResolvedValueOnce({ output_text: "response" })
392401

393-
await handler.completePrompt("test prompt", { timeoutMs: 3000 })
394-
expect(mockResponsesCreate).toHaveBeenCalledWith(expect.objectContaining({ model: expect.any(String) }), {
395-
timeout: 3000,
396-
})
397-
})
402+
await handler.completePrompt("test prompt", { timeoutMs: input })
403+
expect(mockResponsesCreate).toHaveBeenCalledWith(expect.objectContaining({ model: expect.any(String) }), {
404+
timeout: forwarded,
405+
})
406+
},
407+
)
398408

399409
it("completePrompt should not forward an unusable timeoutMs", async () => {
400410
// "No explicit timeout" must leave the option absent: timeout: 0 aborts

‎src/api/providers/utils/abort-signal.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ export function hasUsableTimeout(timeoutMs?: number): timeoutMs is number {
1111
return typeof timeoutMs === "number" && Number.isFinite(timeoutMs) && timeoutMs > 0
1212
}
1313

14-
/**
15-
* Merge an optional external abort signal with an optional timeout.
16-
*
17-
* Timeout values <= 0 and non-finite values are treated as disabled; finite
18-
* delays above 2^31-1 ms are capped (Node clamps them to 1 ms otherwise). The timeout is created via the
19-
* native AbortSignal.timeout() API, which self-manages its timer — callers do
20-
* not need to (and cannot) clear it manually.
21-
*/
2214
/** Maximum delay a Node timer (and therefore an SDK timeout option) accepts. */
2315
export const MAX_TIMEOUT_MS = 2_147_483_647
2416

@@ -31,6 +23,14 @@ export function toSdkTimeoutMs(timeoutMs: number): number {
3123
return Math.min(Math.max(1, Math.ceil(timeoutMs)), MAX_TIMEOUT_MS)
3224
}
3325

26+
/**
27+
* Merge an optional external abort signal with an optional timeout.
28+
*
29+
* Timeout values <= 0 and non-finite values are treated as disabled; finite
30+
* delays above 2^31-1 ms are capped (Node clamps them to 1 ms otherwise). The timeout is created via the
31+
* native AbortSignal.timeout() API, which self-manages its timer — callers do
32+
* not need to (and cannot) clear it manually.
33+
*/
3434
export function mergeAbortSignalAndTimeout(externalSignal?: AbortSignal, timeoutMs?: number): AbortSignal | undefined {
3535
// Node's AbortSignal.timeout() throws ERR_OUT_OF_RANGE for non-integer
3636
// delays (Infinity) and clamps delays above 2^31-1 ms to 1 ms, which would

0 commit comments

Comments
 (0)