@@ -2914,6 +2914,48 @@ describe("VsCodeLmHandler", () => {
29142914 expect ( removeEventListenerSpy ) . toHaveBeenCalledWith ( "abort" , addEventListenerSpy . mock . calls [ 0 ] [ 1 ] )
29152915 } )
29162916
2917+ it ( "should reject with the canonical AbortError when only the external signal aborts" , async ( ) => {
2918+ // Parity with the completePrompt signal-only case: neutralise the bridged token
2919+ // cancel so only externalAbortSignal.aborted is true, then end the stream quietly.
2920+ let releaseStream : ( ) => void = ( ) => { }
2921+ const streamGate = new Promise < void > ( ( resolve ) => {
2922+ releaseStream = resolve
2923+ } )
2924+ mockLanguageModelChat . sendRequest . mockResolvedValueOnce ( {
2925+ stream : ( async function * ( ) {
2926+ await streamGate
2927+ return
2928+ } ) ( ) ,
2929+ text : ( async function * ( ) {
2930+ await streamGate
2931+ return
2932+ } ) ( ) ,
2933+ } )
2934+
2935+ const controller = new AbortController ( )
2936+ const stream = handler . createMessage (
2937+ "system" ,
2938+ [ { role : "user" , content : "hi" } ] ,
2939+ makeCreateMessageMetadata ( { abortSignal : controller . signal } ) ,
2940+ )
2941+ const firstChunk = stream . next ( )
2942+ await vi . waitFor ( ( ) => expect ( mockLanguageModelChat . sendRequest ) . toHaveBeenCalledTimes ( 1 ) )
2943+
2944+ // Only the external signal aborts: the bridge's token cancel is a no-op.
2945+ ; ( tokenSourceInstance ( ) . cancel as Mock ) . mockImplementation ( ( ) => { } )
2946+ controller . abort ( )
2947+ expect ( tokenSourceInstance ( ) . token . isCancellationRequested ) . toBe ( false )
2948+
2949+ releaseStream ( )
2950+
2951+ await expect ( firstChunk ) . rejects . toSatisfy (
2952+ ( error ) =>
2953+ error instanceof Error &&
2954+ error . name === "AbortError" &&
2955+ error . message === "The VS Code LM request was aborted" ,
2956+ )
2957+ } )
2958+
29172959 it ( "should cancel the request token when the consumer stops consuming early" , async ( ) => {
29182960 const systemPrompt = "You are a helpful assistant"
29192961 const messages : Anthropic . Messages . MessageParam [ ] = [ { role : "user" as const , content : "Hello" } ]
@@ -4269,6 +4311,92 @@ describe("VsCodeLmHandler", () => {
42694311 expect ( tokenSource . dispose ) . toHaveBeenCalled ( )
42704312 } )
42714313
4314+ it ( "dispose cancels and disposes a streaming completion" , async ( ) => {
4315+ let releaseStream : ( ) => void = ( ) => { }
4316+ const streamGate = new Promise < void > ( ( resolve ) => {
4317+ releaseStream = resolve
4318+ } )
4319+ mockLanguageModelChat . sendRequest . mockResolvedValueOnce ( {
4320+ stream : ( async function * ( ) {
4321+ await streamGate
4322+ yield new vscode . LanguageModelTextPart ( "partial" )
4323+ } ) ( ) ,
4324+ text : ( async function * ( ) {
4325+ await streamGate
4326+ yield "partial"
4327+ } ) ( ) ,
4328+ } )
4329+ handler [ "client" ] = mockLanguageModelChat
4330+
4331+ const completion = handler . completePrompt ( "Test prompt" ) . catch ( ( error : unknown ) => error )
4332+ await vi . waitFor ( ( ) => expect ( mockLanguageModelChat . sendRequest ) . toHaveBeenCalledTimes ( 1 ) )
4333+ const tokenSource = tokenSourceInstance ( )
4334+
4335+ handler . dispose ( )
4336+ expect ( tokenSource . cancel ) . toHaveBeenCalled ( )
4337+ expect ( tokenSource . dispose ) . toHaveBeenCalled ( )
4338+
4339+ releaseStream ( )
4340+ const error = await completion
4341+ expect ( ( error as Error ) . name ) . toBe ( "AbortError" )
4342+ } )
4343+
4344+ it ( "dispose cancels every concurrent completion" , async ( ) => {
4345+ let releaseStream : ( ) => void = ( ) => { }
4346+ const streamGate = new Promise < void > ( ( resolve ) => {
4347+ releaseStream = resolve
4348+ } )
4349+ const gatedResponse = ( ) => ( {
4350+ stream : ( async function * ( ) {
4351+ await streamGate
4352+ yield new vscode . LanguageModelTextPart ( "partial" )
4353+ } ) ( ) ,
4354+ text : ( async function * ( ) {
4355+ await streamGate
4356+ yield "partial"
4357+ } ) ( ) ,
4358+ } )
4359+ mockLanguageModelChat . sendRequest
4360+ . mockResolvedValueOnce ( gatedResponse ( ) )
4361+ . mockResolvedValueOnce ( gatedResponse ( ) )
4362+ handler [ "client" ] = mockLanguageModelChat
4363+
4364+ const first = handler . completePrompt ( "First" ) . catch ( ( error : unknown ) => error )
4365+ const second = handler . completePrompt ( "Second" ) . catch ( ( error : unknown ) => error )
4366+ await vi . waitFor ( ( ) => expect ( mockLanguageModelChat . sendRequest ) . toHaveBeenCalledTimes ( 2 ) )
4367+ const firstSource = tokenSourceInstance ( 0 )
4368+ const secondSource = tokenSourceInstance ( 1 )
4369+
4370+ handler . dispose ( )
4371+ expect ( firstSource . cancel ) . toHaveBeenCalled ( )
4372+ expect ( firstSource . dispose ) . toHaveBeenCalled ( )
4373+ expect ( secondSource . cancel ) . toHaveBeenCalled ( )
4374+ expect ( secondSource . dispose ) . toHaveBeenCalled ( )
4375+
4376+ releaseStream ( )
4377+ await Promise . all ( [ first , second ] )
4378+ } )
4379+
4380+ it ( "dispose after a finished completion does not cancel it again" , async ( ) => {
4381+ mockLanguageModelChat . sendRequest . mockResolvedValueOnce ( {
4382+ stream : ( async function * ( ) {
4383+ yield new vscode . LanguageModelTextPart ( "done" )
4384+ } ) ( ) ,
4385+ text : ( async function * ( ) {
4386+ yield "done"
4387+ } ) ( ) ,
4388+ } )
4389+ handler [ "client" ] = mockLanguageModelChat
4390+
4391+ await expect ( handler . completePrompt ( "Test prompt" ) ) . resolves . toBe ( "done" )
4392+ const tokenSource = tokenSourceInstance ( )
4393+ // The finished completion removed itself from the active set in its finally block.
4394+ expect ( tokenSource . dispose ) . toHaveBeenCalled ( )
4395+
4396+ handler . dispose ( )
4397+ expect ( tokenSource . cancel ) . not . toHaveBeenCalled ( )
4398+ } )
4399+
42724400 it ( "should attach and detach the abort listener around the completion" , async ( ) => {
42734401 const mockModel = { ...mockLanguageModelChat }
42744402 ; ( vscode . lm . selectChatModels as Mock ) . mockResolvedValueOnce ( [ mockModel ] )
0 commit comments