@@ -4140,3 +4140,92 @@ async def handler(payload):
41404140 task .cancel ()
41414141 with contextlib .suppress (asyncio .CancelledError , RuntimeError ):
41424142 await task
4143+
4144+
4145+ @pytest .mark .parametrize ("streamed" , [False , True ], ids = ["non_streamed" , "streamed" ])
4146+ @pytest .mark .asyncio
4147+ async def test_fresh_runs_isolate_nested_agent_tool_approvals (streamed : bool ) -> None :
4148+ """Matching provider calls in fresh runs must not reuse another run's nested pause."""
4149+ executed : list [tuple [dict [str , str ], str , Usage ]] = []
4150+
4151+ @function_tool (needs_approval = True , failure_error_function = None )
4152+ async def confirm (ctx : RunContextWrapper [dict [str , str ]], value : str ) -> str :
4153+ executed .append ((ctx .context , value , ctx .usage ))
4154+ return value
4155+
4156+ inner_model = ScriptedModel (
4157+ steps = [
4158+ [get_function_tool_call ("confirm" , '{"value":"first"}' , call_id = "inner" )],
4159+ [get_function_tool_call ("confirm" , '{"value":"second"}' , call_id = "inner" )],
4160+ [get_text_message ("second confirmed" )],
4161+ [get_text_message ("first confirmed" )],
4162+ ]
4163+ )
4164+ inner = Agent (name = "inner" , model = inner_model , tools = [confirm ])
4165+
4166+ async def on_stream (_event : AgentToolStreamEvent ) -> None :
4167+ pass
4168+
4169+ delegate = inner .as_tool (
4170+ tool_name = "delegate" ,
4171+ tool_description = "Delegate" ,
4172+ on_stream = on_stream if streamed else None ,
4173+ )
4174+ outer_model = ScriptedModel (
4175+ steps = [
4176+ [get_function_tool_call ("delegate" , '{"input":"go"}' , call_id = "outer" )],
4177+ [get_function_tool_call ("delegate" , '{"input":"go"}' , call_id = "outer" )],
4178+ [get_text_message ("second done" )],
4179+ [get_text_message ("first done" )],
4180+ ]
4181+ )
4182+ outer = Agent (name = "outer" , model = outer_model , tools = [delegate ])
4183+
4184+ async def run (input_value : Any , context : dict [str , str ]) -> RunResult | RunResultStreaming :
4185+ if not streamed :
4186+ return await Runner .run (outer , input_value , context = context )
4187+ result = Runner .run_streamed (outer , input_value , context = context )
4188+ async for _event in result .stream_events ():
4189+ pass
4190+ return result
4191+
4192+ first_context = {"user" : "first" }
4193+ second_context = {"user" : "second" }
4194+ first = await run ("go" , first_context )
4195+ # Keep the first result alive and paused while a second fresh run uses the same call fields.
4196+ second = await run ("go" , second_context )
4197+ assert len (first .interruptions ) == len (second .interruptions ) == 1
4198+ first_call = first .interruptions [0 ].raw_item
4199+ second_call = second .interruptions [0 ].raw_item
4200+ assert isinstance (first_call , ResponseFunctionToolCall )
4201+ assert isinstance (second_call , ResponseFunctionToolCall )
4202+ assert json .loads (first_call .arguments ) == {"value" : "first" }
4203+ assert json .loads (second_call .arguments ) == {"value" : "second" }
4204+ assert executed == []
4205+
4206+ second_state = second .to_state ()
4207+ second_state .approve (second .interruptions [0 ])
4208+ second_done = await run (second_state , second_context )
4209+ assert second_done .final_output == "second done"
4210+ assert second_done .interruptions == []
4211+ assert [(context , value ) for context , value , _usage in executed ] == [(second_context , "second" )]
4212+ assert executed [0 ][0 ] is second_context
4213+ assert executed [0 ][2 ] is second_done .context_wrapper .usage
4214+
4215+ # Completing the newer run must not consume the older run's pending approval or arguments.
4216+ still_pending = await run (first .to_state (), first_context )
4217+ assert len (still_pending .interruptions ) == 1
4218+ assert len (executed ) == 1
4219+ first_state = still_pending .to_state ()
4220+ first_state .approve (still_pending .interruptions [0 ])
4221+ first_done = await run (first_state , first_context )
4222+ assert first_done .final_output == "first done"
4223+ assert first_done .interruptions == []
4224+ assert [(context , value ) for context , value , _usage in executed ] == [
4225+ (second_context , "second" ),
4226+ (first_context , "first" ),
4227+ ]
4228+ assert executed [1 ][0 ] is first_context
4229+ assert executed [1 ][2 ] is first_done .context_wrapper .usage
4230+ inner_model .assert_complete ()
4231+ outer_model .assert_complete ()
0 commit comments