@@ -2874,6 +2874,90 @@ def known_func(arg1: str) -> str:
28742874 assert exec_counter == 0
28752875
28762876
2877+ @pytest .mark .parametrize ("approval_first" , [True , False ], ids = ["approval-first" , "declaration-only-first" ])
2878+ async def test_mixed_batch_approval_enforced_regardless_of_call_order (
2879+ chat_client_base : SupportsChatGetResponse , approval_first : bool
2880+ ):
2881+ """A tool with approval_mode='always_require' must pause for approval even when a declaration-only
2882+ call precedes it in the same batch.
2883+
2884+ Regression: batch classification used to stop at the first matching call, so a declaration-only call
2885+ appearing before an approval-required call caused the whole batch to be returned as user input and the
2886+ approval gate to be silently bypassed. Classification must travel with each call, not with its position.
2887+ """
2888+ from agent_framework import FunctionTool
2889+
2890+ @tool (name = "approval_func" , approval_mode = "always_require" )
2891+ def approval_func (arg1 : str ) -> str :
2892+ return f"Approved { arg1 } "
2893+
2894+ declaration_func = FunctionTool (
2895+ name = "declaration_func" ,
2896+ func = None ,
2897+ description = "A declaration-only function for testing" ,
2898+ input_model = {"type" : "object" , "properties" : {"arg1" : {"type" : "string" }}, "required" : ["arg1" ]},
2899+ )
2900+
2901+ approval_call = Content .from_function_call (call_id = "a1" , name = "approval_func" , arguments = '{"arg1": "x"}' )
2902+ declaration_call = Content .from_function_call (call_id = "d1" , name = "declaration_func" , arguments = '{"arg1": "y"}' )
2903+ contents = [approval_call , declaration_call ] if approval_first else [declaration_call , approval_call ]
2904+
2905+ chat_client_base .run_responses = [ # type: ignore[attr-defined] # ty: ignore[unresolved-attribute]
2906+ ChatResponse (messages = Message (role = "assistant" , contents = contents )),
2907+ ChatResponse (messages = Message (role = "assistant" , contents = ["done" ])),
2908+ ]
2909+
2910+ response = await chat_client_base .get_response (
2911+ [Message (role = "user" , contents = ["hello" ])],
2912+ options = {"tool_choice" : "auto" , "tools" : [approval_func , declaration_func ]},
2913+ )
2914+
2915+ approval_requests = [
2916+ content
2917+ for msg in response .messages
2918+ for content in msg .contents
2919+ if content .type == "function_approval_request" and content .function_call .name == "approval_func"
2920+ ]
2921+ assert len (approval_requests ) == 1 , "approval gate must be surfaced regardless of call order"
2922+
2923+
2924+ async def test_mixed_batch_approval_takes_precedence_over_unknown_call_termination (
2925+ chat_client_base : SupportsChatGetResponse ,
2926+ ):
2927+ """An approval-required call anywhere in the batch must pause before an unknown call terminates it.
2928+
2929+ Regression: with terminate_on_unknown_calls=True, an unknown call appearing before an approval-required
2930+ call raised KeyError first, so the approval pause (higher priority) was never reached.
2931+ """
2932+
2933+ @tool (name = "approval_func" , approval_mode = "always_require" )
2934+ def approval_func (arg1 : str ) -> str :
2935+ return f"Approved { arg1 } "
2936+
2937+ chat_client_base .function_invocation_configuration ["terminate_on_unknown_calls" ] = True # type: ignore[attr-defined] # ty: ignore[unresolved-attribute]
2938+ chat_client_base .run_responses = [ # type: ignore[attr-defined] # ty: ignore[unresolved-attribute]
2939+ ChatResponse (
2940+ messages = Message (
2941+ role = "assistant" ,
2942+ contents = [
2943+ Content .from_function_call (call_id = "u1" , name = "unknown_function" , arguments = '{"arg1": "x"}' ),
2944+ Content .from_function_call (call_id = "a1" , name = "approval_func" , arguments = '{"arg1": "y"}' ),
2945+ ],
2946+ )
2947+ ),
2948+ ChatResponse (messages = Message (role = "assistant" , contents = ["done" ])),
2949+ ]
2950+
2951+ response = await chat_client_base .get_response (
2952+ [Message (role = "user" , contents = ["hello" ])], options = {"tool_choice" : "auto" , "tools" : [approval_func ]}
2953+ )
2954+
2955+ approval_requests = [
2956+ content for msg in response .messages for content in msg .contents if content .type == "function_approval_request"
2957+ ]
2958+ assert len (approval_requests ) >= 1 , "approval pause must take precedence over unknown-call termination"
2959+
2960+
28772961async def test_function_invocation_config_additional_tools (chat_client_base : SupportsChatGetResponse ):
28782962 """Test that additional_tools are available but treated as declaration_only."""
28792963 exec_counter_visible = 0
0 commit comments