diff --git a/src/google/adk/tools/agent_tool.py b/src/google/adk/tools/agent_tool.py index cb7ae50760c..2de6a9f14b6 100644 --- a/src/google/adk/tools/agent_tool.py +++ b/src/google/adk/tools/agent_tool.py @@ -317,6 +317,14 @@ async def run_async( last_content = None last_error_message = None last_grounding_metadata = None + # Long-running tool calls (adk_request_confirmation, adk_request_input, a + # remote agent's input_required, or an ordinary LongRunningFunctionTool) + # all set long_running_tool_ids on the event carrying the call. Most of + # them resolve later in the very same run via an auto-built function + # response (e.g. a progress-reporting tool that returns a truthy value), + # so only a call id that is *never* answered by a following function + # response before the run ends is actually still pending. + pending_tool_ids: set[str] = set() async with Aclosing( runner.run_async( user_id=session.user_id, @@ -334,11 +342,30 @@ async def run_async( if event.content: last_content = event.content last_grounding_metadata = event.grounding_metadata + pending_tool_ids -= { + fr.id for fr in event.get_function_responses() if fr.id + } + if event.long_running_tool_ids: + pending_tool_ids |= event.long_running_tool_ids # Clean up runner resources (especially MCP sessions) # to avoid "Attempted to exit cancel scope in a different task" errors await runner.close() + if pending_tool_ids: + # The nested run's own session is discarded once this call returns, so + # a call id still pending here (e.g. adk_request_confirmation or + # adk_request_input) has nowhere left to be resumed. Surface that as an + # explicit error instead of silently returning "" and letting the + # caller's model believe the wrapped agent finished. + return ( + f'Error: agent {self.agent.name!r} paused for human input or' + ' confirmation' + f' (pending call ids: {sorted(pending_tool_ids)}), which AgentTool' + ' cannot resume. Wrap it as a single_turn sub-agent instead if it' + ' needs to pause mid-call.' + ) + if last_content is None or last_content.parts is None: return last_error_message or '' parts_text = (_part_to_text(p) for p in last_content.parts if not p.thought) diff --git a/tests/unittests/tools/test_agent_tool.py b/tests/unittests/tools/test_agent_tool.py index ab34045da08..35e07749651 100644 --- a/tests/unittests/tools/test_agent_tool.py +++ b/tests/unittests/tools/test_agent_tool.py @@ -1338,6 +1338,89 @@ async def test_run_async_preserves_error_when_only_thought_parts(): assert result == 'A2A request failed: 503' +@mark.asyncio +async def test_run_async_reports_error_when_wrapped_agent_pauses(): + """A HITL pause (adk_request_confirmation) must not look like a success. + + The nested run's session is thrown away once AgentTool.run_async returns, + so a pending long_running_tool_ids call can never be resumed. The caller + must see an explicit error instead of {"result": ""}, which would make its + model believe the pending action already completed. + """ + result = await _run_agent_tool_with_events([ + Event( + author='inner_agent', + content=types.Content( + role='model', + parts=[ + types.Part( + function_call=types.FunctionCall( + name='adk_request_confirmation', args={} + ) + ) + ], + ), + long_running_tool_ids={'adk-123'}, + ), + ]) + assert result != '' + assert 'adk-123' in result + assert 'inner_agent' in result + + +@mark.asyncio +async def test_run_async_returns_final_answer_after_long_running_tool_resolves(): + """A long-running call answered later in the same run is not a pause. + + An ordinary LongRunningFunctionTool that returns a truthy progress value + (e.g. {'status': 'pending'}) gets an auto-built function response in the + very same turn (see _caller.py), and the agent can go on to produce a real + final answer after that. long_running_tool_ids is still set on the call + event in this case, but the call is not actually left pending when the run + ends, so AgentTool must return the genuine final answer instead of the + "paused" error. + """ + result = await _run_agent_tool_with_events([ + Event( + author='inner_agent', + content=types.Content( + role='model', + parts=[ + types.Part( + function_call=types.FunctionCall( + name='slow_tool', args={}, id='adk-123' + ) + ) + ], + ), + long_running_tool_ids={'adk-123'}, + ), + Event( + author='inner_agent', + content=types.Content( + role='user', + parts=[ + types.Part( + function_response=types.FunctionResponse( + name='slow_tool', + response={'status': 'pending'}, + id='adk-123', + ) + ) + ], + ), + ), + Event( + author='inner_agent', + content=types.Content( + role='model', + parts=[types.Part(text='final answer after pending tool')], + ), + ), + ]) + assert result == 'final answer after pending tool' + + class TestAgentToolWithCompositeAgents: """Tests for AgentTool wrapping composite agents (SequentialAgent, etc.)."""