Skip to content

Commit 76586ed

Browse files
fix(mistral): classify the first abort cause and type the timeout mock
Review follow-ups: use the merged signal reason so a timeout-first abort stays TimeoutError; add ordering tests; drop the timeout mock cast. mistral.spec 50 passed.
1 parent 0f02404 commit 76586ed

2 files changed

Lines changed: 78 additions & 5 deletions

File tree

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

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,11 +871,11 @@ describe("MistralHandler", () => {
871871
vi.useFakeTimers()
872872
// The merged deadline uses native AbortSignal.timeout; drive it with fake
873873
// timers so the test is deterministic and does not wait in real time.
874-
const timeoutSpy = vi.spyOn(AbortSignal, "timeout").mockImplementation(((ms: number) => {
874+
const timeoutSpy = vi.spyOn(AbortSignal, "timeout").mockImplementation((ms = 0) => {
875875
const controller = new AbortController()
876876
setTimeout(() => controller.abort(new DOMException("timed out", "TimeoutError")), ms)
877877
return controller.signal
878-
}) as unknown as typeof AbortSignal.timeout)
878+
})
879879
try {
880880
mockComplete.mockImplementation(
881881
(_options: unknown, requestOptions?: { fetchOptions?: { signal?: AbortSignal } }) =>
@@ -908,6 +908,74 @@ describe("MistralHandler", () => {
908908
}
909909
})
910910

911+
it("keeps TimeoutError when the timeout fires before a later caller abort", async () => {
912+
vi.useFakeTimers()
913+
const timeoutSpy = vi.spyOn(AbortSignal, "timeout").mockImplementation((ms = 0) => {
914+
const controller = new AbortController()
915+
setTimeout(() => controller.abort(new DOMException("timed out", "TimeoutError")), ms)
916+
return controller.signal
917+
})
918+
const caller = new AbortController()
919+
try {
920+
mockComplete.mockImplementation(
921+
(_options: unknown, requestOptions?: { fetchOptions?: { signal?: AbortSignal } }) =>
922+
new Promise<never>((_resolve, reject) => {
923+
const signal = requestOptions?.fetchOptions?.signal
924+
const fail = () =>
925+
reject(Object.assign(new Error("Request timed out"), { name: "RequestTimeoutError" }))
926+
signal?.addEventListener("abort", fail, { once: true })
927+
}),
928+
)
929+
930+
const promise = handler.completePrompt("Test prompt", { abortSignal: caller.signal, timeoutMs: 100 })
931+
const assertion = expect(promise).rejects.toMatchObject({ name: "TimeoutError" })
932+
// Timeout fires first, then the caller also aborts before the rejection settles.
933+
await vi.advanceTimersByTimeAsync(120)
934+
caller.abort()
935+
await assertion
936+
} finally {
937+
timeoutSpy.mockRestore()
938+
vi.useRealTimers()
939+
}
940+
})
941+
942+
it("keeps AbortError when the caller aborts before the timeout fires", async () => {
943+
vi.useFakeTimers()
944+
const timeoutSpy = vi.spyOn(AbortSignal, "timeout").mockImplementation((ms = 0) => {
945+
const controller = new AbortController()
946+
setTimeout(() => controller.abort(new DOMException("timed out", "TimeoutError")), ms)
947+
return controller.signal
948+
})
949+
const caller = new AbortController()
950+
try {
951+
mockComplete.mockImplementation(
952+
(_options: unknown, requestOptions?: { fetchOptions?: { signal?: AbortSignal } }) =>
953+
new Promise<never>((_resolve, reject) => {
954+
const signal = requestOptions?.fetchOptions?.signal
955+
const fail = () => reject(new Error("socket closed"))
956+
caller.signal.addEventListener(
957+
"abort",
958+
() => {
959+
// Caller aborts first; the mock then rejects a plain error.
960+
fail()
961+
},
962+
{ once: true },
963+
)
964+
if (signal?.aborted) fail()
965+
}),
966+
)
967+
968+
const promise = handler.completePrompt("Test prompt", { abortSignal: caller.signal, timeoutMs: 100 })
969+
const assertion = expect(promise).rejects.toMatchObject({ name: "AbortError" })
970+
caller.abort()
971+
await assertion
972+
expect(caller.signal.aborted).toBe(true)
973+
} finally {
974+
timeoutSpy.mockRestore()
975+
vi.useRealTimers()
976+
}
977+
})
978+
911979
it("should surface a standard AbortError when the SDK throws an abort error before the signal flag propagates", async () => {
912980
// The signal flag has not propagated yet, but the SDK rejects with its
913981
// own abort error: isRequestAborted must catch the error-name branch.

‎src/api/providers/mistral.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,14 @@ export class MistralHandler extends BaseProvider implements SingleCompletionHand
278278
// which does not match the abort-name checks on its own).
279279
if (isRequestAborted(error, requestAbortSignal)) {
280280
if (requestAbortSignal?.aborted) {
281-
throw callerAbortSignal?.aborted
282-
? createAbortError("Mistral")
283-
: createTimeoutError("Mistral", "completion request")
281+
// The merged signal keeps the FIRST abort reason: a timeout that fired before
282+
// the caller aborted must stay a TimeoutError even if the caller signal is
283+
// aborted by the time the catch runs.
284+
const abortedByTimeout =
285+
(requestAbortSignal.reason as { name?: string } | undefined)?.name === "TimeoutError"
286+
throw abortedByTimeout
287+
? createTimeoutError("Mistral", "completion request")
288+
: createAbortError("Mistral")
284289
}
285290
throw createAbortError("Mistral")
286291
}

0 commit comments

Comments
 (0)