fix(live): stop background tools before the transfer delay - #7011
Open
ehtesham-zahoor wants to merge 1 commit into
Open
fix(live): stop background tools before the transfer delay#7011ehtesham-zahoor wants to merge 1 commit into
ehtesham-zahoor wants to merge 1 commit into
Conversation
The handoff waits DEFAULT_TRANSFER_AGENT_DELAY before it cancels the send task and closes the connection. The connection is open for the whole of that wait and the send task is still draining the live request queue, so a tool of the handing-off agent that completed inside the window had its function response forwarded to a model that never called it -- the outcome _stop_background_tool_tasks exists to prevent. Stop the tools before the delay rather than after it. The delay, the send task cancellation and the connection close are unchanged, so the transfer's own function response still reaches the model before the connection closes.
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.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
Testing Plan
Unit Tests:
Added to
tests/unittests/streaming/test_live_tool_shutdown.py:test_handoff_stops_tools_before_the_transfer_delay, parametrized overtransfer delays and tool completion times.
test_handoff_stops_streaming_tools_before_the_transfer_delay, covering theother kind of background tool that
_stop_background_tool_tasksstops.Both assert that no function response from the handing-off agent's tools
reaches the model after the transfer event is yielded. A streaming tool
legitimately streams to its own agent before the handoff, so only what follows
the handoff is asserted on. Each case also asserts the tool actually started,
so none can pass vacuously.
Timings covered, and which ones discriminate (result on unmodified
main):mainWithout the fix (source reverted, tests unchanged):
With the fix:
pyink==25.12andisort==8.0.1both report clean on the two changed files.Multi-version run with
tox(py310–py314):py313 and py314 each report the same two failures, and they are not from this
change:
Both fail identically with this branch's two files reverted to
main, so theyreproduce on unmodified upstream. The cause is local: the assertion trips on
sitecustomize, which ships with Homebrew's Python(
/opt/homebrew/Cellar/python@3.13/.../lib/python3.13/sitecustomize.py) and isnot present in the interpreters
uvdownloads — which is why only the twoHomebrew versions here are affected. The tests added by this PR pass on both:
py313: 12 passed,py314: 12 passed.The existing
test_streaming_tool_stops_when_its_agent_hands_offdoes notcover this: it observes
tasks[0].done()and the tick count from inside thesub agent, which only runs once the handoff has already completed, so it passes
either way.
Manual End-to-End (E2E) Tests:
adk webis not involved — this is a timing window insiderun_live, so theE2E evidence is a standalone script that drives the real flow, live request
queue, send task and background tool task against a recording connection. The
full script is in #7006 and runs against released
google-adk==2.8.0withpip install google-adk==2.8.0 && python repro.py.Before (a tool completing 0.25s into the 1.0s delay):
After:
Control — the same script with the tool completing at 2.0s, outside the window,
on unmodified
main. Nothing is delivered, confirming the harness distinguishesthe two cases rather than always reporting a hit:
Note the close still happens at 1.01s after the change: the delay, the send
task cancellation and the connection close are untouched, so the transfer's own
function response — queued just above the branch with
live_request_queue.send_content(event.content)— still reaches the modelbefore the connection goes away. Only the parent's tools are fenced earlier.
Checklist
Additional context
The guard and the delay both entered in
0088abbe, with the guard alreadypositioned after the delay;
65b382d5later moved the block into_live_llm_flow.pyunchanged. This keeps the boundary set in #6541, where theconnection-close and delay logic was explicitly left alone — that logic is not
touched here, only the position of
_stop_background_tool_tasks.Disclosure: I used an AI assistant for repository navigation, reproduction
support, and drafting. I reviewed the change, ran the tests and the E2E script,
and verified the failing-without-the-fix result myself.