@@ -918,6 +918,149 @@ async def fake_resolve_interrupted_turn(**kwargs: object) -> SingleStepResult:
918918 assert server_managed_values == [True ]
919919
920920
921+ def _sent_tool_outputs (model : ScriptedModel , * , first_call_index : int ) -> list [tuple [str , str ]]:
922+ """Collect the tool outputs the model received, from `first_call_index` onward."""
923+ outputs : list [tuple [str , str ]] = []
924+ for call in model .calls [first_call_index :]:
925+ for item in cast (list [dict [str , Any ]], call .input ):
926+ if item .get ("type" ) == "function_call_output" :
927+ outputs .append ((str (item .get ("call_id" )), str (item .get ("output" ))))
928+ return outputs
929+
930+
931+ async def _run_server_managed (
932+ agent : Agent [Any ],
933+ agent_input : Any ,
934+ * ,
935+ run_config : RunConfig ,
936+ use_conversation_id : bool ,
937+ streaming : bool ,
938+ ) -> Any :
939+ """Run the agent under one of the server-managed continuation modes."""
940+ kwargs : dict [str , Any ] = (
941+ {"conversation_id" : "conv-resume" }
942+ if use_conversation_id
943+ else {"auto_previous_response_id" : True }
944+ )
945+ if streaming :
946+ streamed = Runner .run_streamed (agent , agent_input , run_config = run_config , ** kwargs )
947+ async for _ in streamed .stream_events ():
948+ pass
949+ return streamed
950+ return await Runner .run (agent , agent_input , run_config = run_config , ** kwargs )
951+
952+
953+ @pytest .mark .parametrize ("streaming" , [False , True ], ids = ["non_streamed" , "streamed" ])
954+ @pytest .mark .parametrize ("serialize_state" , [False , True ], ids = ["live_state" , "serialized_state" ])
955+ @pytest .mark .parametrize (
956+ "use_conversation_id" , [False , True ], ids = ["auto_previous_response_id" , "conversation_id" ]
957+ )
958+ @pytest .mark .asyncio
959+ async def test_resumed_server_managed_run_sends_tool_not_found_output (
960+ streaming : bool ,
961+ serialize_state : bool ,
962+ use_conversation_id : bool ,
963+ ) -> None :
964+ """A resumed server-managed run must send the output built for a missing tool.
965+
966+ The interrupted turn answers the unknown tool locally while another call waits for
967+ approval. The server already owns both calls, so resuming has to deliver both outputs.
968+ """
969+
970+ @function_tool (name_override = "needs_ok" , needs_approval = True )
971+ async def needs_ok (text : str ) -> str :
972+ return text
973+
974+ model = ScriptedModel ()
975+ agent = Agent (name = "test" , model = model , tools = [needs_ok ])
976+ model .extend (
977+ [
978+ [
979+ get_function_tool_call (
980+ "needs_ok" , json .dumps ({"text" : "one" }), call_id = "call-approval"
981+ ),
982+ get_function_tool_call ("missing_tool" , json .dumps ({}), call_id = "call-missing" ),
983+ ],
984+ [get_text_message ("done" )],
985+ ]
986+ )
987+ run_config = RunConfig (tool_not_found_behavior = "return_error_to_model" )
988+
989+ async def run_once (agent_input : Any ) -> Any :
990+ return await _run_server_managed (
991+ agent ,
992+ agent_input ,
993+ run_config = run_config ,
994+ use_conversation_id = use_conversation_id ,
995+ streaming = streaming ,
996+ )
997+
998+ first = await run_once ("Use needs_ok and missing_tool" )
999+ state = first .to_state ()
1000+ if serialize_state :
1001+ state = await RunState .from_json (agent , json .loads (json .dumps (state .to_json ())))
1002+ interruptions = state .get_interruptions ()
1003+ assert [item .raw_item .call_id for item in interruptions ] == ["call-approval" ]
1004+ state .approve (interruptions [0 ])
1005+
1006+ resumed = await run_once (state )
1007+
1008+ assert resumed .final_output == "done"
1009+ delivered = _sent_tool_outputs (model , first_call_index = 1 )
1010+ assert sorted (call_id for call_id , _ in delivered ) == ["call-approval" , "call-missing" ]
1011+ assert "missing_tool" in dict (delivered )["call-missing" ]
1012+
1013+
1014+ @pytest .mark .asyncio
1015+ async def test_resumed_server_managed_run_sends_each_tool_output_once () -> None :
1016+ """Staged approvals must deliver every output exactly once to a server-managed conversation.
1017+
1018+ Approving one of two gated calls resumes and interrupts again without a model request, so
1019+ the same model response stays current across both resumes.
1020+ """
1021+
1022+ @function_tool (name_override = "needs_ok" , needs_approval = True )
1023+ async def needs_ok (text : str ) -> str :
1024+ return f"ok:{ text } "
1025+
1026+ model = ScriptedModel ()
1027+ agent = Agent (name = "test" , model = model , tools = [needs_ok ])
1028+ model .extend (
1029+ [
1030+ [
1031+ get_function_tool_call ("needs_ok" , json .dumps ({"text" : "a" }), call_id = "call-a" ),
1032+ get_function_tool_call ("needs_ok" , json .dumps ({"text" : "b" }), call_id = "call-b" ),
1033+ get_function_tool_call ("missing_tool" , json .dumps ({}), call_id = "call-missing" ),
1034+ ],
1035+ [get_text_message ("done" )],
1036+ ]
1037+ )
1038+ run_config = RunConfig (tool_not_found_behavior = "return_error_to_model" )
1039+
1040+ async def run_once (agent_input : Any ) -> Any :
1041+ return await _run_server_managed (
1042+ agent ,
1043+ agent_input ,
1044+ run_config = run_config ,
1045+ use_conversation_id = False ,
1046+ streaming = False ,
1047+ )
1048+
1049+ result = await run_once ("Use needs_ok twice and missing_tool" )
1050+ for expected_model_calls in (1 , 2 ):
1051+ state = await RunState .from_json (agent , json .loads (json .dumps (result .to_state ().to_json ())))
1052+ interruptions = state .get_interruptions ()
1053+ assert interruptions
1054+ state .approve (interruptions [0 ])
1055+ result = await run_once (state )
1056+ # Approving only the first gated call resumes without asking the model again.
1057+ assert len (model .calls ) == expected_model_calls
1058+
1059+ assert result .final_output == "done"
1060+ delivered = _sent_tool_outputs (model , first_call_index = 1 )
1061+ assert sorted (call_id for call_id , _ in delivered ) == ["call-a" , "call-b" , "call-missing" ]
1062+
1063+
9211064@pytest .mark .asyncio
9221065async def test_resumed_approval_does_not_duplicate_session_items () -> None :
9231066 async def test_tool () -> str :
0 commit comments