Repository navigation
fix(tools): surface HITL pauses in AgentTool instead of returning "" - #7250
Closed
chelsealong wants to merge 2 commits into
Closed
chelsealong wants to merge 2 commits into
chelsealong wants to merge 2 commits into
Conversation
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.
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #7245.
The bug
AgentTool.run_asyncruns the wrapped agent in a throw-awayInMemorySessionServiceand reduces the nested run to the last event thathas content (
src/google/adk/tools/agent_tool.py). It readsstate_delta,error_message,contentandgrounding_metadataoff nested events, butnever
long_running_tool_ids.When the wrapped agent pauses for human input — a
FunctionTool(..., require_confirmation=True)emittingadk_request_confirmation, arequest_inputcall emittingadk_request_input, or aRemoteA2aAgentwhose remote task reaches
TASK_STATE_INPUT_REQUIRED— the nested run endson an event whose content is only a function-call part.
AgentToolextractsno text from that, so it returns
"". The caller's model gets{"result": ""}, treats the unperformed action as done, and the pendingconfirmation/input is lost for good, since the nested session that held it
is discarded when
run_asyncreturns.The fix
Track the set of
long_running_tool_idsthat are still unanswered when thenested run ends, instead of just "was
long_running_tool_idsever set onany event." An ordinary
LongRunningFunctionToolthat returns a truthyprogress value (e.g.
{'status': 'pending'}) gets an auto-built functionresponse in the very same turn (see
_caller.py's_execute_single_prepared_call), and the agent can go on to produce a realfinal answer afterward —
long_running_tool_idsis still set on that call'sevent, 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 thepause 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, sothey 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_SingleTurnAgentToolalready does, so thepause 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
AgentToolwith a static inner agent whose only event is anadk_request_confirmationfunction call withlong_running_tool_ids={'adk-123'}, and asserts the explicit pause erroris returned instead of
"".test_run_async_returns_final_answer_after_long_running_tool_resolves(new): drives
AgentToolthrough call → matching function-response →genuine final-text event (the same interleaving
test_functions_long_running.py::test_async_functionexercises for anordinary
LongRunningFunctionTool), and asserts the real final answer isreturned rather than a fabricated pause error.
Confirmed the new test fails on the previous version of
agent_tool.py(isolated viagit stash push -- src/google/adk/tools/agent_tool.py,rerun,
git stash popto restore):With the fix:
Also ran
pyink --checkandisort --checkon both changed files; noformatting changes needed.
AI assistance disclosure
This change was written with Claude Code (Anthropic).
🤖 Generated with Claude Code