@@ -214,6 +214,68 @@ describe("routed Responses custom-tool compatibility", () => {
214214 rewrite . dispose ?.( ) ;
215215 } ) ;
216216
217+ test ( "does not match a known pending item id by output index alone" , ( ) => {
218+ const budget = createTestTranslatorBudget ( ) ;
219+ const chargeRetained = budget . chargeRetained . bind ( budget ) ;
220+ const releaseRetained = budget . releaseRetained . bind ( budget ) ;
221+ let charges = 0 ;
222+ let releases = 0 ;
223+ budget . chargeRetained = ( ...args ) => {
224+ charges += 1 ;
225+ chargeRetained ( ...args ) ;
226+ } ;
227+ budget . releaseRetained = ( ...args ) => {
228+ releases += 1 ;
229+ releaseRetained ( ...args ) ;
230+ } ;
231+ const rewrite = createRoutedCustomToolRestoreBlockRewrite ( new Set ( [ "exec" ] ) , budget ) ;
232+
233+ expect ( rewrite ( frame ( "response.function_call_arguments.delta" , {
234+ output_index : 0 ,
235+ item_id : "fc_a" ,
236+ delta : "{\"input\":\"a" ,
237+ } ) ) ) . toEqual ( [ ] ) ;
238+ expect ( charges ) . toBe ( 1 ) ;
239+
240+ const added = rewrite ( frame ( "response.output_item.added" , {
241+ output_index : 0 ,
242+ item : {
243+ type : "function_call" ,
244+ id : "fc_b" ,
245+ call_id : "call_b" ,
246+ name : "exec" ,
247+ arguments : "" ,
248+ status : "in_progress" ,
249+ } ,
250+ } ) ) ;
251+ expect ( added . map ( block => dataPayload ( block ) . type ) ) . toEqual ( [ "response.output_item.added" ] ) ;
252+ expect ( JSON . stringify ( added ) ) . not . toContain ( '"item_id":"ctc_b"' ) ;
253+ expect ( charges ) . toBe ( 1 ) ;
254+ expect ( releases ) . toBe ( 0 ) ;
255+
256+ rewrite . dispose ?.( ) ;
257+ expect ( budget . snapshot ( ) . currentBytes ) . toBe ( 0 ) ;
258+ } ) ;
259+
260+ test ( "does not retain argument events that arrive after a terminal event" , ( ) => {
261+ const budget = createTestTranslatorBudget ( ) ;
262+ const rewrite = createRoutedCustomToolRestoreBlockRewrite ( new Set ( [ "exec" ] ) , budget ) ;
263+
264+ rewrite ( frame ( "response.completed" , {
265+ response : { id : "resp_1" , status : "completed" , output : [ ] } ,
266+ } ) ) ;
267+ expect ( budget . snapshot ( ) . currentBytes ) . toBe ( 0 ) ;
268+
269+ rewrite ( frame ( "response.function_call_arguments.delta" , {
270+ output_index : 0 ,
271+ item_id : "fc_late" ,
272+ delta : "{\"input\":\"late" ,
273+ } ) ) ;
274+ rewrite . dispose ?.( ) ;
275+
276+ expect ( budget . snapshot ( ) . currentBytes ) . toBe ( 0 ) ;
277+ } ) ;
278+
217279 test ( "replays buffered events unchanged when item done identifies an ordinary function" , ( ) => {
218280 const budget = createTestTranslatorBudget ( ) ;
219281 const rewrite = createRoutedCustomToolRestoreBlockRewrite ( new Set ( [ "exec" ] ) , budget ) ;
@@ -398,6 +460,7 @@ describe("routed Responses custom-tool compatibility", () => {
398460 test ( "handleResponses leaves custom tools native for forward-auth passthrough" , async ( ) => {
399461 const savedFetch = globalThis . fetch ;
400462 let outboundBody : Record < string , unknown > | undefined ;
463+ let outboundAuthorization : string | null = null ;
401464 const upstreamItem = {
402465 type : "function_call" ,
403466 id : "fc_exec" ,
@@ -408,6 +471,7 @@ describe("routed Responses custom-tool compatibility", () => {
408471 } ;
409472 globalThis . fetch = ( async ( _input , init ) => {
410473 outboundBody = JSON . parse ( String ( init ?. body ) ) as Record < string , unknown > ;
474+ outboundAuthorization = new Headers ( init ?. headers ) . get ( "authorization" ) ;
411475 return new Response ( JSON . stringify ( { id : "resp_forward" , status : "completed" , output : [ upstreamItem ] } ) , {
412476 headers : { "content-type" : "application/json" } ,
413477 } ) ;
@@ -438,6 +502,7 @@ describe("routed Responses custom-tool compatibility", () => {
438502 const clientBody = await response . json ( ) as { output : Array < Record < string , unknown > > } ;
439503 const outboundTools = outboundBody ?. tools as Array < Record < string , unknown > > | undefined ;
440504
505+ expect ( outboundAuthorization ) . toBe ( "Bearer caller-token" ) ;
441506 expect ( outboundTools ?. [ 0 ] ) . toMatchObject ( { type : "custom" , name : "exec" } ) ;
442507 expect ( clientBody . output [ 0 ] ) . toMatchObject ( { type : "function_call" , name : "exec" } ) ;
443508 expect ( clientBody . output [ 0 ] ) . not . toHaveProperty ( "input" ) ;
0 commit comments