From 3c36651552ea57f1719610da7f0bb787caf223f4 Mon Sep 17 00:00:00 2001 From: chelsealong Date: Wed, 23 Sep 2026 08:28:52 +0000 Subject: [PATCH 1/2] fix(tools): surface HITL pauses in AgentTool instead of returning "" AgentTool.run_async ran the wrapped agent in a throw-away InMemorySessionService and reduced the nested run to the last event with content, never checking long_running_tool_ids. When the wrapped agent (or a RemoteA2aAgent reaching input_required) paused for adk_request_confirmation or adk_request_input, the last event held only a function-call part, so the caller received {"result": ""} and its model treated the unperformed action as done -- the pending request was silently lost along with the nested session. Track long_running_tool_ids across the nested run and, when the run ends still paused, return an explicit error instead of "", so the caller's model does not mistake a pending confirmation for a completed one. Fixes #7245 --- src/google/adk/tools/agent_tool.py | 17 ++++++++++++++ tests/unittests/tools/test_agent_tool.py | 30 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/google/adk/tools/agent_tool.py b/src/google/adk/tools/agent_tool.py index cb7ae50760c..094cc726ecf 100644 --- a/src/google/adk/tools/agent_tool.py +++ b/src/google/adk/tools/agent_tool.py @@ -317,6 +317,7 @@ async def run_async( last_content = None last_error_message = None last_grounding_metadata = None + paused_tool_ids = None async with Aclosing( runner.run_async( user_id=session.user_id, @@ -334,11 +335,27 @@ async def run_async( if event.content: last_content = event.content last_grounding_metadata = event.grounding_metadata + # The nested run's own session is discarded once this call returns, so + # a pending confirmation/input request (adk_request_confirmation, + # adk_request_input, or a remote agent surfacing input_required) 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. + paused_tool_ids = event.long_running_tool_ids or paused_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 paused_tool_ids: + return ( + f'Error: agent {self.agent.name!r} paused for human input or' + ' confirmation' + f' (pending call ids: {sorted(paused_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..8194777402a 100644 --- a/tests/unittests/tools/test_agent_tool.py +++ b/tests/unittests/tools/test_agent_tool.py @@ -1338,6 +1338,36 @@ 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 + + class TestAgentToolWithCompositeAgents: """Tests for AgentTool wrapping composite agents (SequentialAgent, etc.).""" From b7034e5eb0399cf9a491632bd7b433041f0b1357 Mon Sep 17 00:00:00 2001 From: chelsealong Date: Wed, 23 Sep 2026 08:37:47 +0000 Subject: [PATCH 2/2] fix(tools): don't flag a long-running call resolved in-turn as paused The reviewer for #7245 reproduced a false positive: an ordinary LongRunningFunctionTool that returns a truthy progress value (e.g. {'status': 'pending'}) gets an auto-built function response in the same turn (_caller.py's _execute_single_prepared_call), and the agent can go on to a genuine final answer afterward. The previous fix tracked "was long_running_tool_ids ever set on any event," which stays true even after that call is answered, so AgentTool discarded the real final answer and returned the "paused" error instead. Track only call ids that are still unanswered when the run ends: remove an id from the pending set as soon as a later event's function response matches it, and only report the pause error if ids remain pending. Genuinely unresolved pauses (adk_request_confirmation, adk_request_input, input_required) never get an auto function response, so they still report correctly. Added test_run_async_returns_final_answer_after_long_running_tool_resolves, which fails on the previous version of agent_tool.py (confirmed via git stash) and passes with this fix. --- src/google/adk/tools/agent_tool.py | 30 +++++++++----- tests/unittests/tools/test_agent_tool.py | 53 ++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 10 deletions(-) diff --git a/src/google/adk/tools/agent_tool.py b/src/google/adk/tools/agent_tool.py index 094cc726ecf..2de6a9f14b6 100644 --- a/src/google/adk/tools/agent_tool.py +++ b/src/google/adk/tools/agent_tool.py @@ -317,7 +317,14 @@ async def run_async( last_content = None last_error_message = None last_grounding_metadata = None - paused_tool_ids = 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, @@ -335,23 +342,26 @@ async def run_async( if event.content: last_content = event.content last_grounding_metadata = event.grounding_metadata - # The nested run's own session is discarded once this call returns, so - # a pending confirmation/input request (adk_request_confirmation, - # adk_request_input, or a remote agent surfacing input_required) 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. - paused_tool_ids = event.long_running_tool_ids or paused_tool_ids + 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 paused_tool_ids: + 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(paused_tool_ids)}), which AgentTool' + 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.' ) diff --git a/tests/unittests/tools/test_agent_tool.py b/tests/unittests/tools/test_agent_tool.py index 8194777402a..35e07749651 100644 --- a/tests/unittests/tools/test_agent_tool.py +++ b/tests/unittests/tools/test_agent_tool.py @@ -1368,6 +1368,59 @@ async def test_run_async_reports_error_when_wrapped_agent_pauses(): 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.)."""