Skip to content

Commit ca3db83

Browse files
committed
fix(api): lint request timeouts and spec loops, fix existing violations
1 parent 7043629 commit ca3db83

17 files changed

Lines changed: 417 additions & 181 deletions

‎packages/config-eslint/api-provider-hygiene.js‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
// Paths are relative to the `src` package, where this config is loaded.
44

55
const sharedAbortHelpers =
6-
"mergeAbortSignalAndTimeout|mergeAbortSignals|throwIfAborted|isRequestAborted|createAbortError|rejectOnAbort|resolveModelWithAbort"
6+
"mergeAbortSignalAndTimeout|mergeAbortSignals|throwIfAborted|isRequestAborted|createAbortError|rejectOnAbort|resolveModelWithAbort|bridgeExternalAbortSignal|hasUsableTimeout|toSdkTimeoutMs"
77

88
// Names that usually hold a secret or an endpoint that can embed credentials.
99
// `cacheKey` and `contextTokens` must not match.
1010
const sensitiveName = "^(url|apiKey|secret|password)$|(Url|URL|ApiKey|Secret|Token)$"
1111

12+
// A per-request `timeoutMs` from CompletePromptOptions. `this.timeoutMs` is the client-level
13+
// timeout from getApiRequestTimeout, which is already valid, so it must not match.
14+
const rawRequestTimeout =
15+
":matches(Identifier[name='timeoutMs'], MemberExpression[property.name='timeoutMs']:not([object.type='ThisExpression']), ChainExpression[expression.property.name='timeoutMs'])"
16+
1217
export const apiProviderHygieneSelectors = [
1318
{
1419
selector: `:matches(FunctionDeclaration, VariableDeclarator)[id.name=/^(${sharedAbortHelpers})$/]`,
@@ -20,6 +25,16 @@ export const apiProviderHygieneSelectors = [
2025
message:
2126
"Use mergeAbortSignalAndTimeout for a variable delay. AbortSignal.timeout throws on Infinity and fractions, and clamps large values to 1 ms.",
2227
},
28+
{
29+
selector: `:matches(Property[key.name='timeout'], AssignmentExpression[left.property.name='timeout']) > ${rawRequestTimeout}`,
30+
message:
31+
"Do not forward a request timeoutMs as is. A SDK treats 0 as an immediate abort and throws on negative, non-finite or fractional values. Use hasUsableTimeout and toSdkTimeoutMs.",
32+
},
33+
{
34+
selector: "Literal[value=2147483647]",
35+
message:
36+
"Do not write the maximum timer delay by hand. Use MAX_TIMEOUT_MS or toSdkTimeoutMs from providers/utils/abort-signal.",
37+
},
2338
{
2439
selector: `CallExpression[callee.object.name='console'] TemplateLiteral > :matches(Identifier[name=/${sensitiveName}/], MemberExpression[property.name=/${sensitiveName}/])`,
2540
message:
@@ -35,3 +50,12 @@ export const apiProviderHygieneConfig = {
3550
"no-restricted-syntax": ["error", ...apiProviderHygieneSelectors],
3651
},
3752
}
53+
54+
// Spec files under src/api. A loop of expects in one test stops at the first failing case and hides the rest.
55+
export const apiProviderTestSelectors = [
56+
{
57+
selector: "ForOfStatement[right.type='ArrayExpression']:has(CallExpression[callee.name='expect'])",
58+
message:
59+
"Use it.each for repeated cases. A loop of expects in one test stops at the first failing case and does not name it.",
60+
},
61+
]

‎packages/config-eslint/api-provider-hygiene.test.js‎

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { describe, it } from "node:test"
44
import { Linter } from "eslint"
55
import typescriptEslint from "typescript-eslint"
66

7-
import { apiProviderHygieneConfig } from "./api-provider-hygiene.js"
7+
import { apiProviderHygieneConfig, apiProviderTestSelectors } from "./api-provider-hygiene.js"
88

99
const linter = new Linter({ configType: "flat" })
1010
const config = [{ files: ["**/*.ts"], languageOptions: { parser: typescriptEslint.parser } }, apiProviderHygieneConfig]
@@ -88,3 +88,89 @@ describe("api provider hygiene: sensitive values in logs", () => {
8888
})
8989
}
9090
})
91+
92+
describe("api provider hygiene: request timeout forwarding", () => {
93+
for (const code of [
94+
"create(body, { timeout: options.timeoutMs })",
95+
"create(body, { timeout: options?.timeoutMs })",
96+
"create(body, { signal, timeout: timeoutMs })",
97+
"requestOptions.timeout = options.timeoutMs",
98+
"requestOptions.timeout = timeoutMs",
99+
]) {
100+
it(`flags: ${code}`, () => {
101+
assert.equal(lint(code).length, 1)
102+
})
103+
}
104+
105+
for (const code of [
106+
"new OpenAI({ timeout: this.timeoutMs })",
107+
"requestOptions.timeout = toSdkTimeoutMs(timeoutMs)",
108+
"create(body, { timeout: toSdkTimeoutMs(options.timeoutMs) })",
109+
"if (hasUsableTimeout(timeoutMs)) requestOptions.timeout = toSdkTimeoutMs(timeoutMs)",
110+
"const timeoutMs = options?.timeoutMs",
111+
"mergeAbortSignalAndTimeout(signal, options?.timeoutMs)",
112+
]) {
113+
it(`allows: ${code}`, () => {
114+
assert.deepEqual(lint(code), [])
115+
})
116+
}
117+
})
118+
119+
describe("api provider hygiene: timeout helpers", () => {
120+
for (const code of [
121+
"const MAX = 2147483647",
122+
"Math.min(Math.ceil(timeoutMs), 2_147_483_647)",
123+
"function hasUsableTimeout(t?: number) { return true }",
124+
"const toSdkTimeoutMs = (t: number) => t",
125+
"function bridgeExternalAbortSignal() {}",
126+
]) {
127+
it(`flags: ${code}`, () => {
128+
assert.equal(lint(code).length, 1)
129+
})
130+
}
131+
132+
it("allows importing the timeout helpers", () => {
133+
assert.deepEqual(
134+
lint('import { hasUsableTimeout, toSdkTimeoutMs } from "./utils/abort-signal"\ntoSdkTimeoutMs(1)'),
135+
[],
136+
)
137+
})
138+
139+
it("allows the literal in the helper module", () => {
140+
assert.deepEqual(lint("export const MAX_TIMEOUT_MS = 2_147_483_647", "api/providers/utils/abort-signal.ts"), [])
141+
})
142+
})
143+
144+
describe("api provider hygiene: spec loops", () => {
145+
const testLinter = new Linter({ configType: "flat" })
146+
const testConfig = [
147+
{ files: ["**/*.ts"], languageOptions: { parser: typescriptEslint.parser } },
148+
{
149+
files: ["api/**/__tests__/**/*.spec.ts"],
150+
rules: { "no-restricted-syntax": ["error", ...apiProviderTestSelectors] },
151+
},
152+
]
153+
const lintSpec = (code) =>
154+
testLinter
155+
.verify(code, testConfig, { filename: "api/providers/__tests__/example.spec.ts" })
156+
.map((m) => m.message)
157+
158+
for (const code of [
159+
"it('x', () => { for (const v of [0, -5]) { expect(f(v)).toBe(1) } })",
160+
"it('x', async () => { for (const v of [0, -5]) { await run(v); expect(f(v)).toBe(1) } })",
161+
]) {
162+
it(`flags a loop of expects over a literal array: ${code}`, () => {
163+
assert.equal(lintSpec(code).length, 1)
164+
})
165+
}
166+
167+
for (const code of [
168+
"it.each([0, -5])('x %s', (v) => { expect(f(v)).toBe(1) })",
169+
"it('x', () => { for (const v of [0, -5]) { run(v) } })",
170+
"it('x', () => { for (const v of cases) { expect(f(v)).toBe(1) } })",
171+
]) {
172+
it(`allows: ${code}`, () => {
173+
assert.deepEqual(lintSpec(code), [])
174+
})
175+
}
176+
})

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,25 +1096,25 @@ describe("VertexHandler", () => {
10961096
)
10971097
})
10981098

1099-
it("completePrompt should not forward an unusable timeoutMs", async () => {
1100-
handler = new AnthropicVertexHandler({
1101-
apiModelId: "claude-3-5-sonnet-v2@20241022",
1102-
vertexProjectId: "test-project",
1103-
vertexRegion: "us-central1",
1104-
})
1099+
// The SDK turns timeout: 0 into an immediate abort and rejects
1100+
// negative/non-finite delays, so "no explicit timeout" means no option.
1101+
it.each([0, -5, Number.NaN, Number.POSITIVE_INFINITY])(
1102+
"completePrompt should not forward an unusable timeoutMs (%s)",
1103+
async (timeoutMs) => {
1104+
handler = new AnthropicVertexHandler({
1105+
apiModelId: "claude-3-5-sonnet-v2@20241022",
1106+
vertexProjectId: "test-project",
1107+
vertexRegion: "us-central1",
1108+
})
11051109

1106-
const mockCreate = vitest
1107-
.spyOn(handler["client"].messages, "create")
1108-
.mockResolvedValue({ content: [{ type: "text", text: "response" }] } as never)
1110+
const mockCreate = vitest
1111+
.spyOn(handler["client"].messages, "create")
1112+
.mockResolvedValue({ content: [{ type: "text", text: "response" }] } as never)
11091113

1110-
// The SDK turns timeout: 0 into an immediate abort and rejects
1111-
// negative/non-finite delays, so "no explicit timeout" means no option.
1112-
for (const timeoutMs of [0, -5, Number.NaN, Number.POSITIVE_INFINITY]) {
1113-
mockCreate.mockClear()
11141114
await handler.completePrompt("test prompt", { timeoutMs })
11151115
expect(mockCreate).toHaveBeenCalledWith(expect.any(Object), undefined)
1116-
}
1117-
})
1116+
},
1117+
)
11181118

11191119
it("should handle empty content array for Claude", async () => {
11201120
handler = new AnthropicVertexHandler({

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,20 +1139,21 @@ describe("AnthropicHandler", () => {
11391139
expect(mockCreate).toHaveBeenCalledWith(expect.any(Object), undefined)
11401140
})
11411141

1142-
it("should not forward an unusable timeoutMs (0, negative, non-finite)", async () => {
1143-
// The Anthropic SDK turns timeout: 0 into an immediate abort and throws
1144-
// on negative/non-finite delays, so "no explicit timeout" must mean the
1145-
// option is absent rather than 0.
1146-
for (const timeoutMs of [0, -5, Number.NaN, Number.POSITIVE_INFINITY]) {
1142+
// The Anthropic SDK turns timeout: 0 into an immediate abort and throws
1143+
// on negative/non-finite delays, so "no explicit timeout" must mean the
1144+
// option is absent rather than 0.
1145+
it.each([0, -5, Number.NaN, Number.POSITIVE_INFINITY])(
1146+
"should not forward an unusable timeoutMs (%s)",
1147+
async (timeoutMs) => {
11471148
mockCreate.mockClear()
11481149
mockCreate.mockResolvedValueOnce({ content: [{ type: "text", text: "response" }] })
11491150
await handler.completePrompt("test prompt", { timeoutMs })
11501151
expect(mockCreate).toHaveBeenCalledWith(
11511152
expect.objectContaining({ model: mockOptions.apiModelId }),
11521153
undefined,
11531154
)
1154-
}
1155-
})
1155+
},
1156+
)
11561157

11571158
it.each([
11581159
// Only a call through toSdkTimeoutMs rounds this up.

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

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -138,37 +138,49 @@ describe("AwsBedrockHandler", () => {
138138
},
139139
)
140140

141-
it.each([
142-
["anthropic.claude-sonnet-4-5-20250929-v1:0", 64_000],
143-
["anthropic.claude-sonnet-4-6", 64_000],
144-
["anthropic.claude-sonnet-5", 128_000],
145-
["anthropic.claude-opus-4-7", 128_000],
146-
["anthropic.claude-opus-4-8", 128_000],
147-
["anthropic.claude-opus-5", 128_000],
148-
["anthropic.claude-opus-5-5", 128_000],
149-
["anthropic.claude-haiku-5-5", 128_000],
150-
] as const)("exposes %s output ceiling without raising request defaults", async (apiModelId, ceiling) => {
151-
for (const enableReasoningEffort of [true, false]) {
152-
for (const modelMaxTokens of [undefined, ceiling]) {
153-
const provider = new AwsBedrockHandler({ apiModelId, enableReasoningEffort, modelMaxTokens })
154-
const modelInfo = provider.getModel().info
155-
expect(modelInfo.maxTokens).toBe(ceiling)
156-
provider["client"].send = vi.fn().mockResolvedValue({ stream: asyncStreamFrom([]) })
157-
await collectStream(provider.createMessage("system", [{ role: "user", content: "hello" }]))
158-
const expected =
159-
modelMaxTokens && (enableReasoningEffort || modelInfo.supportsMaxTokens)
160-
? ceiling
161-
: enableReasoningEffort
162-
? 16_384
163-
: 8192
164-
expect(mockConverseStreamCommand).toHaveBeenLastCalledWith(
165-
expect.objectContaining({
166-
inferenceConfig: expect.objectContaining({ maxTokens: expected }),
167-
}),
168-
)
169-
}
170-
}
171-
})
141+
it.each(
142+
(
143+
[
144+
["anthropic.claude-sonnet-4-5-20250929-v1:0", 64_000],
145+
["anthropic.claude-sonnet-4-6", 64_000],
146+
["anthropic.claude-sonnet-5", 128_000],
147+
["anthropic.claude-opus-4-7", 128_000],
148+
["anthropic.claude-opus-4-8", 128_000],
149+
["anthropic.claude-opus-5", 128_000],
150+
["anthropic.claude-opus-5-5", 128_000],
151+
["anthropic.claude-haiku-5-5", 128_000],
152+
] as const
153+
).flatMap(([apiModelId, ceiling]) =>
154+
[true, false].flatMap((enableReasoningEffort) =>
155+
[undefined, ceiling].map((modelMaxTokens) => ({
156+
apiModelId,
157+
ceiling,
158+
enableReasoningEffort,
159+
modelMaxTokens,
160+
})),
161+
),
162+
),
163+
)(
164+
"exposes $apiModelId output ceiling without raising request defaults (reasoning effort: $enableReasoningEffort, modelMaxTokens: $modelMaxTokens)",
165+
async ({ apiModelId, ceiling, enableReasoningEffort, modelMaxTokens }) => {
166+
const provider = new AwsBedrockHandler({ apiModelId, enableReasoningEffort, modelMaxTokens })
167+
const modelInfo = provider.getModel().info
168+
expect(modelInfo.maxTokens).toBe(ceiling)
169+
provider["client"].send = vi.fn().mockResolvedValue({ stream: asyncStreamFrom([]) })
170+
await collectStream(provider.createMessage("system", [{ role: "user", content: "hello" }]))
171+
const expected =
172+
modelMaxTokens && (enableReasoningEffort || modelInfo.supportsMaxTokens)
173+
? ceiling
174+
: enableReasoningEffort
175+
? 16_384
176+
: 8192
177+
expect(mockConverseStreamCommand).toHaveBeenLastCalledWith(
178+
expect.objectContaining({
179+
inferenceConfig: expect.objectContaining({ maxTokens: expected }),
180+
}),
181+
)
182+
},
183+
)
172184

173185
describe("getModel", () => {
174186
it("should return the correct model info for a standard model", () => {

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,32 @@ describe("FriendliHandler", () => {
181181
expect(result).toBe(expectedResponse)
182182
})
183183

184+
it("completePrompt should forward the abort signal", async () => {
185+
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "ok" } }] })
186+
const { signal } = new AbortController()
187+
await handler.completePrompt("test prompt", { abortSignal: signal })
188+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything(), { signal })
189+
})
190+
191+
it.each([
192+
{ label: "a whole-number", input: 3000, forwarded: 3000 },
193+
{ label: "a fractional", input: 12.5, forwarded: 13 },
194+
{ label: "an over-cap", input: 2_147_483_648, forwarded: 2_147_483_647 },
195+
])("completePrompt should forward $label timeoutMs as $forwarded", async ({ input, forwarded }) => {
196+
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "ok" } }] })
197+
await handler.completePrompt("test prompt", { timeoutMs: input })
198+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything(), { timeout: forwarded })
199+
})
200+
201+
it.each([0, -5, Number.NaN, Number.POSITIVE_INFINITY])(
202+
"completePrompt should not forward an unusable timeoutMs (%s)",
203+
async (timeoutMs) => {
204+
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "ok" } }] })
205+
await handler.completePrompt("test prompt", { timeoutMs })
206+
expect(mockCreate).toHaveBeenLastCalledWith(expect.anything(), undefined)
207+
},
208+
)
209+
184210
it("should handle errors in completePrompt", async () => {
185211
const errorMessage = "Friendli API error"
186212
mockCreate.mockRejectedValueOnce(new Error(errorMessage))

‎src/api/providers/__tests__/io-intelligence.spec.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,27 @@ describe("IOIntelligenceHandler", () => {
364364
expect(mockCreate).toHaveBeenCalledWith(expect.objectContaining({ stream: false }), { signal, timeout: 5_000 })
365365
})
366366

367+
it.each([
368+
{ label: "a whole-number", input: 3000, forwarded: 3000 },
369+
{ label: "a fractional", input: 12.5, forwarded: 13 },
370+
{ label: "an over-cap", input: 2_147_483_648, forwarded: 2_147_483_647 },
371+
])("completePrompt forwards $label timeoutMs as $forwarded", async ({ input, forwarded }) => {
372+
mockCreate.mockResolvedValue({ choices: [{ message: { role: "assistant", content: "ok" } }] })
373+
const handler = new IOIntelligenceHandler({ ioIntelligenceModelId: "meta-llama/Llama-3.3-70B-Instruct" })
374+
await handler.completePrompt("ping", { timeoutMs: input })
375+
expect(mockCreate.mock.calls[0][1]).toEqual({ timeout: forwarded })
376+
})
377+
378+
it.each([0, -5, Number.NaN, Number.POSITIVE_INFINITY])(
379+
"completePrompt does not forward an unusable timeoutMs (%s)",
380+
async (timeoutMs) => {
381+
mockCreate.mockResolvedValue({ choices: [{ message: { role: "assistant", content: "ok" } }] })
382+
const handler = new IOIntelligenceHandler({ ioIntelligenceModelId: "meta-llama/Llama-3.3-70B-Instruct" })
383+
await handler.completePrompt("ping", { timeoutMs })
384+
expect(mockCreate.mock.calls[0][1]).toBeUndefined()
385+
},
386+
)
387+
367388
it("does not send the OpenAI Compatible custom headers to the fixed endpoint", () => {
368389
new IOIntelligenceHandler({
369390
ioIntelligenceApiKey: "test-key",

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,19 +293,20 @@ describe("MiniMaxHandler", () => {
293293
})
294294
})
295295

296-
it("should not forward an unusable timeoutMs", async () => {
297-
// "No explicit timeout" must leave the option absent: timeout: 0 aborts
298-
// immediately and negative/non-finite delays throw inside the SDK.
299-
for (const timeoutMs of [0, -5, Number.NaN, Number.POSITIVE_INFINITY]) {
296+
// "No explicit timeout" must leave the option absent: timeout: 0 aborts
297+
// immediately and negative/non-finite delays throw inside the SDK.
298+
it.each([0, -5, Number.NaN, Number.POSITIVE_INFINITY])(
299+
"should not forward an unusable timeoutMs (%s)",
300+
async (timeoutMs) => {
300301
mockCreate.mockClear()
301302
mockCreate.mockResolvedValueOnce({ content: [{ type: "text", text: "response" }] })
302303
await handler.completePrompt("test prompt", { timeoutMs })
303304
expect(mockCreate).toHaveBeenCalledWith(
304305
expect.objectContaining({ model: expect.any(String) }),
305306
undefined,
306307
)
307-
}
308-
})
308+
},
309+
)
309310

310311
it("completePrompt should send the configured temperature, user message and stream flag", async () => {
311312
const temperatureHandler = new MiniMaxHandler({

0 commit comments

Comments
 (0)