@@ -380,6 +380,56 @@ suite('ByokLmProxyService', () => {
380380 } ) ;
381381 } ) ;
382382
383+ test ( 'bounds abandoned tool continuations while preserving recent state' , async ( ) => {
384+ const captured : IByokLmChatRequest [ ] = [ ] ;
385+ const maximumPendingContinuations = 256 ;
386+
387+ await withProxy (
388+ async request => {
389+ const index = captured . length ;
390+ captured . push ( request ) ;
391+ if ( index <= maximumPendingContinuations + 1 ) {
392+ return {
393+ responseId : `resp_${ index } ` ,
394+ output : [ { type : 'function_call' , callId : `call_${ index } ` , name : 'tool' , argumentsJson : '{}' } ] ,
395+ } ;
396+ }
397+ return { output : [ { type : 'message' , content : [ { type : 'text' , text : 'done' } ] } ] } ;
398+ } ,
399+ async handle => {
400+ const post = async ( index : number , input : unknown ) => {
401+ const response = await fetch ( responsesUrl ( handle , 'acme' ) , {
402+ method : 'POST' ,
403+ headers : authHeaders ( handle , `sess-${ index } ` ) ,
404+ body : JSON . stringify ( { model : 'm' , input } ) ,
405+ } ) ;
406+ assert . strictEqual ( response . status , 200 ) ;
407+ await response . text ( ) ;
408+ } ;
409+
410+ // Sessions can disappear after receiving a tool call. Fill the proxy,
411+ // refresh its oldest entry, then overflow it with one abandoned session.
412+ for ( let index = 0 ; index < maximumPendingContinuations ; index ++ ) {
413+ await post ( index , [ ] ) ;
414+ }
415+ await post ( 0 , [ ] ) ;
416+ await post ( maximumPendingContinuations , [ ] ) ;
417+
418+ for ( const [ session , call ] of [ [ 1 , 1 ] , [ 0 , maximumPendingContinuations ] ] ) {
419+ await post ( session , [
420+ { type : 'function_call' , call_id : `call_${ call } ` , name : 'tool' , arguments : '{}' } ,
421+ { type : 'function_call_output' , call_id : `call_${ call } ` , output : 'done' } ,
422+ ] ) ;
423+ }
424+ } ,
425+ ) ;
426+
427+ assert . deepStrictEqual ( captured . slice ( - 2 ) . map ( request => request . previousResponseId ) , [
428+ undefined ,
429+ `resp_${ maximumPendingContinuations } ` ,
430+ ] ) ;
431+ } ) ;
432+
383433 test ( 'decodes a url-encoded vendor path segment' , async ( ) => {
384434 let captured : IByokLmChatRequest | undefined ;
385435 await withProxy (
0 commit comments