Skip to content

Commit 7f085ab

Browse files
committed
Address review: keep the discovery attempt open across cancellation
prepareRequest set modelDiscoveryAttempted before awaiting getModels, so a caller cancellation that interrupted discovery left the flag set and every later request on the same handler skipped discovery and kept the fallback model metadata. The catch now resets the flag only when the failure is a cancellation (isRequestAborted against the caller's signal); ordinary discovery failures keep the one-attempt behavior. New test aborts during getModels, then makes a second request on the same handler and asserts discovery runs again (negative control verified: removing the reset fails the test; the test is order- independent - the fetch stub is installed before handler construction because the SDK binds globalThis.fetch at construction). Verification at this head: 25/25 green in kimi-code.spec.ts (isolated run of the new test also green); tsc --noEmit 0 errors; eslint --max-warnings=0 clean on touched files; prettier clean.
1 parent e12539a commit 7f085ab

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎src/api/providers/__tests__/kimi-code.spec.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,38 @@ describe("KimiCodeHandler", () => {
352352
expect(createCompletion).not.toHaveBeenCalled()
353353
})
354354

355+
it("retries model discovery on a later request when the caller aborts during discovery", async () => {
356+
// The SDK client binds globalThis.fetch at construction, so the completion
357+
// stub must be installed before the handler is created.
358+
vi.spyOn(globalThis, "fetch").mockImplementation(
359+
async () => new Response(JSON.stringify({ choices: [{ message: { content: "ok" } }] }), { status: 200 }),
360+
)
361+
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "api-key", kimiCodeApiKey: "key" })
362+
const controller = new AbortController()
363+
// The caller cancels while discovery is in flight. The swallowed
364+
// cancellation must not consume the single discovery attempt.
365+
mockGetModels.mockImplementationOnce(async () => {
366+
controller.abort()
367+
throw new DOMException("This operation was aborted", "AbortError")
368+
})
369+
const gen = handler.createMessage("system", [{ role: "user", content: "test" }], {
370+
taskId: "test-task",
371+
abortSignal: controller.signal,
372+
})
373+
await expect(async () => {
374+
for await (const _ of gen) {
375+
// consume
376+
}
377+
}).rejects.toMatchObject({ name: "AbortError" })
378+
expect(mockGetModels).toHaveBeenCalledTimes(1)
379+
380+
// A later request on the same handler must retry discovery rather than
381+
// keep the fallback metadata.
382+
mockGetModels.mockImplementationOnce(async () => ({ "test-model": { maxTokens: 1000 } }))
383+
await handler.completePrompt("second")
384+
expect(mockGetModels).toHaveBeenCalledTimes(2)
385+
})
386+
355387
it("forwards completePrompt abort options and timeoutMs through the override on both 401 retry attempts", async () => {
356388
const handler = new KimiCodeHandler({ kimiCodeAuthMethod: "oauth" })
357389
const unauthorized = Object.assign(new Error("Unauthorized"), { status: 401 })

‎src/api/providers/kimi-code.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { getModelParams } from "../transform/model-params"
2020
import { OpenAiHandler } from "./openai"
2121
import { NOT_PROVIDED } from "./constants"
2222
import { getModels } from "./fetchers/modelCache"
23-
import { throwIfAborted } from "./utils/abort-signal"
23+
import { isRequestAborted, throwIfAborted } from "./utils/abort-signal"
2424
import { getKimiCodeModels } from "./fetchers/kimi-code"
2525

2626
const OAUTH_AUTH_METHOD: KimiCodeAuthMethod = "oauth"
@@ -84,6 +84,13 @@ export class KimiCodeHandler extends OpenAiHandler {
8484
this,
8585
)
8686
} catch (error) {
87+
// A caller cancellation must not consume the single discovery attempt:
88+
// reset the flag so a later request on this handler retries discovery
89+
// instead of keeping fallback metadata. Ordinary discovery failures
90+
// keep the one-attempt behavior.
91+
if (isRequestAborted(error, abortSignal)) {
92+
this.modelDiscoveryAttempted = false
93+
}
8794
// Model discovery is best-effort; preserve the configured ID and fallback metadata.
8895
console.debug("[KimiCode] Model discovery failed; using fallback model metadata", {
8996
message: error instanceof Error ? error.message : String(error),

0 commit comments

Comments
 (0)