Skip to content

Commit 3c51b63

Browse files
committed
fix(api): handle vscode-lm createMessage aborts during client init and streaming
- createMessage re-checks the external abort signal after client initialization (and before sendRequest), cancelling the local token source and throwing an AbortError when the signal aborted while getClient() was pending - createMessage re-checks the external abort signal at the top of the stream consumption loop so a late abort stops the stream instead of yielding stale chunks (the bridged listener still covers the normal mid-flight case) - spec: the mid-flight abort test now expects the stream to stop with an AbortError; added a regression test where client initialization is gated on a release promise and the signal aborts in that window - the generator rejects with AbortError and sendRequest is never called
1 parent 00249a6 commit 3c51b63

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

‎src/api/providers/__tests__/vscode-lm.spec.ts‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,43 @@ describe("VsCodeLmHandler", () => {
508508
expect(mockLanguageModelChat.sendRequest).not.toHaveBeenCalled()
509509
})
510510

511+
it("should reject with an AbortError when the external signal aborts during client initialization", async () => {
512+
const systemPrompt = "You are a helpful assistant"
513+
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user" as const, content: "Hello" }]
514+
515+
// Gate model selection so the generator waits inside getClient(): the
516+
// pre-abort check and listener attachment happened before this window.
517+
let releaseClient: () => void = () => {}
518+
const clientGate = new Promise<void>((resolve) => {
519+
releaseClient = resolve
520+
})
521+
;(vscode.lm.selectChatModels as Mock).mockImplementation(() =>
522+
clientGate.then(() => [{ ...mockLanguageModelChat }]),
523+
)
524+
handler["client"] = null
525+
526+
const controller = new AbortController()
527+
const stream = handler.createMessage(
528+
systemPrompt,
529+
messages,
530+
makeCreateMessageMetadata({ abortSignal: controller.signal }),
531+
)
532+
const firstChunk = stream.next()
533+
534+
// Release the client, then abort before the generator can reach sendRequest.
535+
releaseClient()
536+
controller.abort()
537+
538+
await expect(firstChunk).rejects.toSatisfy((error) => error instanceof Error && error.name === "AbortError")
539+
540+
// The abort landed after client initialization, so no host request started.
541+
expect(handler["client"]).not.toBeNull()
542+
expect(mockLanguageModelChat.sendRequest).toHaveBeenCalledTimes(0)
543+
544+
// The local token source was cancelled for the request that never started.
545+
expect(tokenSourceInstance().cancel).toHaveBeenCalled()
546+
})
547+
511548
it("should bridge a mid-flight external abort to the request cancellation token", async () => {
512549
const systemPrompt = "You are a helpful assistant"
513550
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user" as const, content: "Hello" }]
@@ -537,10 +574,11 @@ describe("VsCodeLmHandler", () => {
537574
await vi.waitFor(() => expect(mockLanguageModelChat.sendRequest).toHaveBeenCalledTimes(1))
538575

539576
// Abort the external signal while the request is in flight, then let the
540-
// (token-agnostic) mock stream finish.
577+
// (token-agnostic) mock stream finish. The late abort must stop the stream
578+
// instead of yielding stale chunks.
541579
controller.abort()
542580
releaseStream()
543-
await expect(firstChunk).resolves.toEqual({ done: false, value: { type: "text", text: "Hello" } })
581+
await expect(firstChunk).rejects.toSatisfy((error) => error instanceof Error && error.name === "AbortError")
544582

545583
// The bridge relayed the abort to the request cancellation token.
546584
const tokenSource = tokenSourceInstance()

‎src/api/providers/vscode-lm.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,16 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
422422
let accumulatedText: string = ""
423423

424424
try {
425+
// Re-check after client initialization: the external signal may have
426+
// aborted while getClient() (or the token calculation) was pending. Bail
427+
// before invoking the host so no request starts for a cancelled request.
428+
if (externalAbortSignal?.aborted) {
429+
cancellationTokenSource.cancel()
430+
const abortError = new Error("Zoo Code <Language Model API>: Request aborted")
431+
abortError.name = "AbortError"
432+
throw abortError
433+
}
434+
425435
// Create the response stream with required options
426436
const requestOptions: vscode.LanguageModelChatRequestOptions = {
427437
justification: `Zoo Code would like to use '${client.name}' from '${client.vendor}', Click 'Allow' to proceed.`,
@@ -436,6 +446,13 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
436446

437447
// Consume the stream and handle both text and tool call chunks
438448
for await (const chunk of response.stream) {
449+
// A late abort while consuming must stop the stream instead of
450+
// yielding stale chunks.
451+
if (externalAbortSignal?.aborted) {
452+
const abortError = new Error("Zoo Code <Language Model API>: Request aborted")
453+
abortError.name = "AbortError"
454+
throw abortError
455+
}
439456
if (chunk instanceof vscode.LanguageModelTextPart) {
440457
// Validate text part value
441458
if (typeof chunk.value !== "string") {

0 commit comments

Comments
 (0)