The tool execution pipeline in tool_execution.py manages concurrent function-tool invocations with failure arbitration, cancellation propagation, and phase-aware task state tracking across batches of tool calls. Task failures are ranked by exception severity and submission order, enabling the pipeline to surface the most significant early failure when multiple tools fail concurrently.
tool_execution.py is the execution-time module for the run pipeline, hosting tool execution helpers, approval plumbing, and payload coercion; action classes are defined separately in tool_actions.py.[1] Exported functions from tool_execution.py include execute_function_tool_calls, execute_custom_tool_calls, execute_local_shell_calls, execute_shell_calls, execute_apply_patch_calls, execute_computer_actions, execute_approved_tools, and approval/resolution helpers.[1]
_FunctionToolFailure is a frozen dataclass that pairs a BaseException with an order integer and a source literal ("direct", "cancelled_teardown", or "post_invoke") for arbitrating which failure wins when multiple function-tool tasks fail concurrently.[1] _get_function_tool_failure_priority assigns priority 0 to asyncio.CancelledError, 1 to Exception, and 2 to any other BaseException (e.g., SystemExit), so fatal exceptions always win arbitration.[1] _select_function_tool_failure arbitrates concurrent function-tool failures by priority first, then by the tool call's order (lower order wins ties), ensuring the most significant earlier-submitted failure surfaces.[1]
_FunctionToolTaskState tracks per-task mutable execution state including the ToolRunFunction, an order index, the live asyncio.Task, and a boolean in_post_invoke_phase flag used to distinguish execution phases during failure arbitration.[1] When a failure is detected, tool_execution.py calls _cancel_function_tool_tasks to cancel all sibling tasks in a batch, enabling structured cooperative cancellation across concurrently executing function tools.[1] The constant _FUNCTION_TOOL_CANCELLED_DRAIN_SECONDS = 0.25 defines the time budget given to sibling tasks to drain after cancellation propagation.[1] The constant _FUNCTION_TOOL_POST_INVOKE_WAIT_SECONDS = 0.1 defines the wait window given after a function tool's primary invocation completes before proceeding to the next pipeline phase.[1]
_consume_function_tool_task_result reports background task exceptions to the event loop's exception handler via call_exception_handler; cancelled tasks and tasks whose message_for_exception returns None are silently dropped.[1] Background cleanup tasks that raise asyncio.CancelledError are silently ignored; those raising an Exception emit a warning message; those raising a fatal BaseException emit a fatal-level message — controlled by _background_cleanup_task_exception_message.[1] Detached parent-cancelled tasks that raise an Exception are silently dropped (return None); those raising a fatal BaseException emit a fatal-level message — controlled by _parent_cancelled_task_exception_message.[1]
_ToolOutputGuardrailExecutionResult wraps a tool output together with an is_rejection flag, distinguishing a genuine tool result from output synthesized by a rejecting guardrail.[1]
Sources