Skip to content

Commit de25d82

Browse files
authored
fix(core): scope function tool approvals to their owning agent (#5144)
fix: scope function tool approvals to their owning agent
1 parent 0f87da2 commit de25d82

20 files changed

Lines changed: 1472 additions & 88 deletions

‎src/agents/_run_state_agent_identity.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,17 @@ def _agent_identity_sort_key(
433433
)
434434

435435

436+
def _get_ambiguous_agent_ids(initial_agent: Agent[Any]) -> set[int]:
437+
"""Find owners distinguishable only by graph traversal order."""
438+
groups: dict[tuple[str, int, str], list[int]] = {}
439+
for agent in _iter_agent_graph(initial_agent):
440+
priority, signature, _ = _agent_identity_sort_key(
441+
agent, root_agent=initial_agent, original_index=0
442+
)
443+
groups.setdefault((agent.name, priority, signature), []).append(id(agent))
444+
return {agent_id for group in groups.values() if len(group) > 1 for agent_id in group}
445+
446+
436447
def _build_agent_identity_map(initial_agent: Agent[Any]) -> dict[str, Agent[Any]]:
437448
"""Build a stable identity map that preserves duplicate agent names."""
438449
ordered_agents = list(_iter_agent_graph(initial_agent))

‎src/agents/realtime/session.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,10 +878,12 @@ async def _send_tool_rejection(
878878
agent: RealtimeAgent,
879879
) -> None:
880880
"""Send a rejection response back to the model and emit an end event."""
881+
approval_item = self._build_tool_approval_item(tool, event, agent)
881882
rejection_message = await self._resolve_approval_rejection_message(
882883
tool=tool,
883884
call_id=event.call_id,
884-
tool_call=self._build_tool_approval_item(tool, event, agent).raw_item,
885+
tool_call=approval_item.raw_item,
886+
approval_item=approval_item,
885887
)
886888
await self._send_tool_output_completion(
887889
_PendingToolOutput(
@@ -943,12 +945,14 @@ async def _resolve_approval_rejection_message(
943945
tool: FunctionTool,
944946
call_id: str,
945947
tool_call: Any | None = None,
948+
approval_item: ToolApprovalItem | None = None,
946949
) -> str:
947950
"""Resolve model-visible output text for approval rejections."""
948951
explicit_message = self._context_wrapper.get_rejection_message(
949952
tool.name,
950953
call_id,
951954
tool_lookup_key=get_function_tool_lookup_key_for_tool(tool),
955+
existing_pending=approval_item,
952956
)
953957
if explicit_message is not None:
954958
return explicit_message

‎src/agents/run.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ async def _run_impl(
650650
run_state=run_state,
651651
context=context,
652652
)
653+
context_wrapper._resolve_function_approval_owners(starting_agent)
653654
context = context_wrapper.context
654655

655656
reject_unrecoverable_terminal_state(run_state)
@@ -666,6 +667,7 @@ async def _run_impl(
666667
auto_previous_response_id=auto_previous_response_id,
667668
)
668669
context_wrapper = ensure_context_wrapper(context)
670+
context_wrapper._resolve_function_approval_owners(starting_agent)
669671
context = context_wrapper.context
670672
set_agent_tool_state_scope(context_wrapper, uuid4().hex)
671673

@@ -2435,6 +2437,7 @@ def run_streamed(
24352437
run_state=run_state,
24362438
context=context,
24372439
)
2440+
context_wrapper._resolve_function_approval_owners(starting_agent)
24382441
context = context_wrapper.context
24392442

24402443
# Override max_turns with the state's max_turns to preserve it across resumption
@@ -2451,6 +2454,7 @@ def run_streamed(
24512454
auto_previous_response_id=auto_previous_response_id,
24522455
)
24532456
context_wrapper = ensure_context_wrapper(context)
2457+
context_wrapper._resolve_function_approval_owners(starting_agent)
24542458
set_agent_tool_state_scope(context_wrapper, uuid4().hex)
24552459
# input_for_state is the same as input_for_result here
24562460
input_for_state = input_for_result

0 commit comments

Comments
 (0)