Skip to content

Commit d7d8157

Browse files
committed
fix: enforce tool approval regardless of call order in mixed batches
Scan the whole batch before deciding, applying priority approval > declaration-only > unknown-call termination, so an always_require tool is no longer bypassed when a declaration-only or unknown call precedes it. Fixes #8079
1 parent 81ee134 commit d7d8157

2 files changed

Lines changed: 100 additions & 5 deletions

File tree

‎python/packages/core/agent_framework/_tools.py‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,9 +2246,12 @@ async def _try_execute_function_call_groups(
22462246
]
22472247

22482248
# Classify the entire batch first: any required user interaction pauses the batch before execution.
2249+
# Scan every call before deciding so classification travels with each call, not with its position in
2250+
# the batch. Priority (highest first): approval pause > declaration-only user-input > unknown-call
2251+
# termination. A user-input pause therefore takes precedence over unknown-call termination in mixed batches.
22492252
requires_approval = False
22502253
has_declaration_only_call = False
2251-
# A user-input pause takes precedence over unknown-call termination in mixed batches.
2254+
unknown_call_name: str | None = None
22522255
for function_call in actionable_calls:
22532256
function_name = function_call.name
22542257
logger.debug(
@@ -2260,12 +2263,20 @@ async def _try_execute_function_call_groups(
22602263
if function_name in approval_tool_names:
22612264
logger.debug("Approval needed for function: %s", function_name)
22622265
requires_approval = True
2263-
break
2266+
continue
22642267
if function_name in declaration_only_tool_names or function_name in additional_tool_names:
22652268
has_declaration_only_call = True
2266-
break
2267-
if config.get("terminate_on_unknown_calls", False) and function_name not in tool_map:
2268-
raise KeyError(f'Error: Requested function "{function_name}" not found.')
2269+
continue
2270+
if (
2271+
unknown_call_name is None
2272+
and config.get("terminate_on_unknown_calls", False)
2273+
and function_name not in tool_map
2274+
):
2275+
unknown_call_name = function_name
2276+
# Defer unknown-call termination until the whole batch is classified so a higher-priority approval or
2277+
# declaration-only pause anywhere in the batch is not skipped by an earlier unknown call.
2278+
if not requires_approval and not has_declaration_only_call and unknown_call_name is not None:
2279+
raise KeyError(f'Error: Requested function "{unknown_call_name}" not found.')
22692280
if requires_approval:
22702281
# Surface only the approvals the host must decide; session-backed safe siblings wait for that resume.
22712282
# approval can only be needed for Function Call Content, not Approval Responses.

‎python/packages/core/tests/core/test_function_invocation_logic.py‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
28772961
async 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

Comments
 (0)