@@ -355,6 +355,40 @@ describe("VsCodeLmHandler", () => {
355355 return drain ( )
356356 }
357357
358+ it . each ( [ { tools : undefined } , { tools : [ ] } ] ) (
359+ "streams marker text immediately when tools are $tools" ,
360+ async ( { tools } ) => {
361+ const parts = [
362+ '<function_calls><invoke name="calculator">' ,
363+ '<parameter name="operation">add</parameter></invoke></function_calls>' ,
364+ ]
365+ let partsProduced = 0
366+ mockLanguageModelChat . sendRequest . mockResolvedValueOnce ( {
367+ stream : ( async function * ( ) {
368+ for ( const part of parts ) {
369+ partsProduced ++
370+ yield new vscode . LanguageModelTextPart ( part )
371+ }
372+ } ) ( ) ,
373+ } )
374+ const stream = handler . createMessage ( "system" , [ { role : "user" , content : "hi" } ] , {
375+ taskId : "test-task" ,
376+ tools,
377+ } )
378+
379+ await expect ( stream . next ( ) ) . resolves . toEqual ( {
380+ done : false ,
381+ value : { type : "text" , text : parts [ 0 ] } ,
382+ } )
383+ expect ( partsProduced ) . toBe ( 1 )
384+ await expect ( stream . next ( ) ) . resolves . toEqual ( {
385+ done : false ,
386+ value : { type : "text" , text : parts [ 1 ] } ,
387+ } )
388+ expect ( ( await collectStream ( stream ) ) . map ( ( chunk ) => chunk . type ) ) . toEqual ( [ "usage" ] )
389+ } ,
390+ )
391+
358392 it ( "recovers a tool call the model streamed as raw invoke XML" , async ( ) => {
359393 const chunks = await collect ( [
360394 "Thinking. " ,
@@ -384,6 +418,73 @@ describe("VsCodeLmHandler", () => {
384418 ] )
385419 } )
386420
421+ it ( "scans unfinished invoke whitespace with linear work across small chunks" , async ( ) => {
422+ const measure = async ( partCount : number ) => {
423+ const originalExec = RegExp . prototype . exec
424+ let scannedCharacters = 0
425+ const execSpy = vi . spyOn ( RegExp . prototype , "exec" ) . mockImplementation ( function (
426+ this : RegExp ,
427+ text : string ,
428+ ) {
429+ if ( this . source . includes ( "invoke" ) || this . source . includes ( "function_calls" ) ) {
430+ scannedCharacters += text . length
431+ }
432+ return originalExec . call ( this , text )
433+ } )
434+ try {
435+ const parts = [ "<invoke" , ...Array < string > ( partCount ) . fill ( " \t\n\r " ) ]
436+ const chunks = await collect ( parts )
437+ expect ( chunks . filter ( ( chunk ) => chunk . type !== "usage" ) ) . toEqual ( [
438+ { type : "text" , text : parts . join ( "" ) } ,
439+ ] )
440+ } finally {
441+ execSpy . mockRestore ( )
442+ }
443+ return scannedCharacters
444+ }
445+
446+ // Count regex input rather than wall time so shared runners do not make this flaky.
447+ const small = await measure ( 200 )
448+ const large = await measure ( 800 )
449+ expect ( large / small ) . toBeGreaterThan ( 3 )
450+ expect ( large / small ) . toBeLessThan ( 6 )
451+ } )
452+
453+ it . each ( [
454+ [ 'name="calculator"></invoke>' ] ,
455+ [ "n" , "a" , "m" , "e" , "=" , '"calculator"></invoke>' ] ,
456+ [ "ordinary text" ] ,
457+ [ '<function_calls><invoke name="calculator"></invoke></function_calls>' ] ,
458+ ] ) ( "preserves the whole parse after carried whitespace followed by %j" , async ( ...ending ) => {
459+ const parts = [ "<ANTML:INVOKE" , " " . repeat ( 80 ) , "\t\n" , ...ending ]
460+ const text = parts . join ( "" )
461+ const expected = extractLeakedToolCalls ( text , new Set ( [ "calculator" ] ) )
462+ const chunks = await collect ( parts )
463+
464+ expect (
465+ chunks
466+ . filter ( ( chunk ) => chunk . type === "text" )
467+ . map ( ( chunk ) => chunk . text )
468+ . join ( "" ) ,
469+ ) . toBe ( expected . leftoverText )
470+ expect ( chunks . filter ( ( chunk ) => chunk . type === "tool_call" ) . map ( ( chunk ) => chunk . name ) ) . toEqual (
471+ expected . calls . map ( ( call ) => call . name ) ,
472+ )
473+ } )
474+
475+ it ( "resumes immediate whitespace delivery after a native call flushes an unfinished invoke" , async ( ) => {
476+ const carried = "<invoke" + " " . repeat ( 80 )
477+ streamMixedParts ( [ "<invoke" , " " . repeat ( 80 ) , { name : "calculator" , input : { } } , " \t" , "tail" ] )
478+ const chunks = await drain ( )
479+
480+ expect ( chunks . filter ( ( chunk ) => chunk . type !== "usage" ) ) . toEqual ( [
481+ { type : "text" , text : carried } ,
482+ { type : "tool_call" , id : "native-1" , name : "calculator" , arguments : "{}" } ,
483+ { type : "text" , text : " \t" } ,
484+ { type : "text" , text : "tail" } ,
485+ ] )
486+ } )
487+
387488 it ( "emits a carried tail as plain text when it never becomes a marker" , async ( ) => {
388489 const chunks = await collect ( [ "hello <par" ] )
389490
@@ -588,6 +689,20 @@ describe("VsCodeLmHandler", () => {
588689 expect ( chunks . some ( ( chunk ) => chunk . type === "tool_call" ) ) . toBe ( false )
589690 } )
590691
692+ it ( "preserves a fenced wrapper when its fence precedes the marker in the same chunk" , async ( ) => {
693+ const text =
694+ '```xml\n<function_calls><invoke name="calculator"><parameter name="operation">add</parameter></invoke></function_calls>\n```'
695+ const chunks = await collect ( [ text ] )
696+
697+ expect ( chunks . some ( ( chunk ) => chunk . type === "tool_call" ) ) . toBe ( false )
698+ expect (
699+ chunks
700+ . filter ( ( chunk ) => chunk . type === "text" )
701+ . map ( ( chunk ) => chunk . text )
702+ . join ( "" ) ,
703+ ) . toBe ( text )
704+ } )
705+
591706 it . each ( [
592707 { name : "write_to_file" , parameter : "content" } ,
593708 { name : "update_todo_list" , parameter : "todos" } ,
0 commit comments