Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/google/adk/flows/llm_flows/tools/_agent_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from ....tools.base_toolset import BaseToolset
from ....tools.tool_context import ToolContext
from .._base_llm_processor import BaseLlmRequestProcessor
from ._batch_executor import _gather_or_cancel
from ._live_caller import _is_streaming_tool

if TYPE_CHECKING:
Expand Down Expand Up @@ -82,21 +83,21 @@ async def process_agent_tools(

from ....agents.llm_agent import _convert_tool_union_to_tools

# Resolve tool_unions in parallel. ``asyncio.gather`` preserves
# input order in the returned list, so the serial commit phase below
# still observes ``agent.tools`` order. If any resolution raises,
# gather cancels the siblings and propagates -- same observable
# behavior as the previous serial loop, which would propagate the
# first exception and abandon the rest.
resolved_tools_per_union = await asyncio.gather(*(
_convert_tool_union_to_tools(
tool_union,
ReadonlyContext(invocation_context),
model,
multiple_tools,
# Resolve tool_unions in parallel. ``_gather_or_cancel`` preserves
# input order in the returned list and cancels/awaits sibling tasks if any
# listing fails or raises ``asyncio.CancelledError``.
tasks = [
asyncio.create_task(
_convert_tool_union_to_tools(
tool_union,
ReadonlyContext(invocation_context),
model,
multiple_tools,
)
)
for tool_union in agent.tools
))
]
resolved_tools_per_union = await _gather_or_cancel(tasks)

# Serial commit phase, in original ``agent.tools`` order. Mutations
# to ``llm_request`` and reads of its state (model, config.tools,
Expand Down
48 changes: 48 additions & 0 deletions tests/unittests/flows/llm_flows/test_tool_request_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,51 @@ async def test_processor_appended_to_request_processors_sees_no_tools_yet():
await _run_preprocess(flow, Agent(name='test_agent', tools=[_a_tool]))

assert probe.seen_tool_names == []


@pytest.mark.asyncio
async def test_process_agent_tools_cancels_siblings_when_listing_cancels():
"""Child-originated CancelledError settles and cancels sibling toolset listings."""
import asyncio
from google.adk.tools.base_toolset import BaseToolset

started = asyncio.Event()
release = asyncio.Event()
cleaned = asyncio.Event()
completed = asyncio.Event()

class _WaitingToolset(BaseToolset):

async def get_tools(self, readonly_context=None):
del readonly_context
started.set()
try:
await release.wait()
completed.set()
return []
finally:
cleaned.set()

async def close(self):
pass

class _CancellingToolset(BaseToolset):

async def get_tools(self, readonly_context=None):
del readonly_context
await started.wait()
raise asyncio.CancelledError('listing cancelled')

async def close(self):
pass

agent = Agent(name='test_agent', tools=[_WaitingToolset(), _CancellingToolset()])
ctx = await testing_utils.create_invocation_context(
agent=agent, user_content='test message'
)

with pytest.raises(asyncio.CancelledError):
await _agent_tools.process_agent_tools(ctx, LlmRequest())

assert cleaned.is_set()
assert not completed.is_set()