Skip to content

fix(tools): surface HITL pauses in AgentTool instead of returning "" - #7250

Closed
chelsealong wants to merge 2 commits into
google:mainfrom
chelsealong:fix-7245-agent-tool-drops-hitl-pause
Closed

chelsealong wants to merge 2 commits into
google:mainfrom
chelsealong:fix-7245-agent-tool-drops-hitl-pause

Conversation

@chelsealong

Copy link
Copy Markdown
Contributor

Fixes #7245.

The bug

AgentTool.run_async runs the wrapped agent in a throw-away
InMemorySessionService and reduces the nested run to the last event that
has content (src/google/adk/tools/agent_tool.py). It reads state_delta,
error_message, content and grounding_metadata off nested events, but
never long_running_tool_ids.

When the wrapped agent pauses for human input — a FunctionTool(..., require_confirmation=True) emitting adk_request_confirmation, a
request_input call emitting adk_request_input, or a RemoteA2aAgent
whose remote task reaches TASK_STATE_INPUT_REQUIRED — the nested run ends
on an event whose content is only a function-call part. AgentTool extracts
no text from that, so it returns "". The caller's model gets
{"result": ""}, treats the unperformed action as done, and the pending
confirmation/input is lost for good, since the nested session that held it
is discarded when run_async returns.

The fix

Track the set of long_running_tool_ids that are still unanswered when the
nested run ends, instead of just "was long_running_tool_ids ever set on
any event." 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's
_execute_single_prepared_call), and the agent can go on to produce a real
final answer afterward — long_running_tool_ids is still set on that call's
event, but the call is not actually left pending once a later event answers
it. The first version of this fix treated "seen anywhere in the run" as
"still paused," so it discarded that genuine final answer and returned the
misleading "paused" error instead — a regression an independent review of
this PR caught and reproduced.

Now a call id is removed from the pending set as soon as a later event's
function response matches it (event.get_function_responses()), and the
pause error is only returned if ids remain pending when the run ends.
Genuinely unresolved pauses (adk_request_confirmation, adk_request_input,
input_required) never get an auto function response in the same turn, so
they are unaffected and still report the explicit error instead of "".

The larger option in the issue (routing the wrapped agent through
tool_context.run_node, as _SingleTurnAgentTool already does, so the
pause and its resumption actually propagate to the caller) changes the
tool's execution model and needs a maintainer decision on approach; this PR
only stops the misleading empty/incorrect result.

Testing plan

  • test_run_async_reports_error_when_wrapped_agent_pauses (existing):
    drives AgentTool with a static inner agent whose only event is an
    adk_request_confirmation function call with
    long_running_tool_ids={'adk-123'}, and asserts the explicit pause error
    is returned instead of "".
  • test_run_async_returns_final_answer_after_long_running_tool_resolves
    (new): drives AgentTool through call → matching function-response →
    genuine final-text event (the same interleaving
    test_functions_long_running.py::test_async_function exercises for an
    ordinary LongRunningFunctionTool), and asserts the real final answer is
    returned rather than a fabricated pause error.

Confirmed the new test fails on the previous version of
agent_tool.py (isolated via git stash push -- src/google/adk/tools/agent_tool.py,
rerun, git stash pop to restore):

>     assert result == 'final answer after pending tool'
E     AssertionError: assert 'Error: agent...use mid-call.' == 'final answer... pending tool'
E       - final answer after pending tool
E       + Error: agent 'inner_agent' paused for human input or confirmation (pending call ids: ['adk-123']), which AgentTool cannot resume. Wrap it as a single_turn sub-agent instead if it needs to pause mid-call.

With the fix:

$ pytest tests/unittests/tools/test_agent_tool.py tests/unittests/flows/llm_flows/tools/test_functions_long_running.py -q
56 passed, 52 warnings in 4.38s

$ pytest tests/unittests/tools tests/unittests/flows/llm_flows -q
3341 passed, 1 skipped, 893 warnings in 65.35s

Also ran pyink --check and isort --check on both changed files; no
formatting changes needed.

AI assistance disclosure

This change was written with Claude Code (Anthropic).

🤖 Generated with Claude Code

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 google#7245
The reviewer for google#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.
@adk-bot

adk-bot commented Oct 15, 2026

Copy link
Copy Markdown
Collaborator

Thank you @chelsealong for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit 9a5510e.

Closing this PR as the changes are now in the main branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AgentTool drops a HITL pause from the wrapped agent (incl. RemoteA2aAgent input_required): caller gets {"result": ""} and carries on

3 participants