-
Notifications
You must be signed in to change notification settings - Fork 3k
flows: persist stop-streaming cancellation state atomically #4589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import asyncio | ||
| from types import SimpleNamespace | ||
|
|
||
| from google.adk.agents.active_streaming_tool import ActiveStreamingTool | ||
| from google.adk.flows.llm_flows import functions | ||
| from google.genai import types | ||
| import pytest | ||
|
|
||
|
|
||
| async def _infinite_stream() -> None: | ||
| while True: | ||
| await asyncio.sleep(0.1) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_stop_streaming_persists_cancelled_state_atomically(): | ||
| task = asyncio.create_task(_infinite_stream()) | ||
| invocation_context = SimpleNamespace( | ||
| active_streaming_tools={ | ||
| 'monitor_stock_price': ActiveStreamingTool(task=task) | ||
| } | ||
| ) | ||
| tool_context = SimpleNamespace(state={}) | ||
| streaming_lock = asyncio.Lock() | ||
|
|
||
| function_response = await functions._process_function_live_helper( | ||
| tool=SimpleNamespace(name='stop_streaming'), | ||
| tool_context=tool_context, | ||
| function_call=types.FunctionCall( | ||
| name='stop_streaming', | ||
| args={'function_name': 'monitor_stock_price'}, | ||
| ), | ||
| function_args={'function_name': 'monitor_stock_price'}, | ||
| invocation_context=invocation_context, | ||
| streaming_lock=streaming_lock, | ||
| ) | ||
|
|
||
| assert function_response == { | ||
| 'status': 'Successfully stopped streaming function monitor_stock_price' | ||
| } | ||
| assert ( | ||
| tool_context.state[functions.LONG_RUNNING_CANCELLATION_STATE_KEY][ | ||
| 'monitor_stock_price' | ||
| ] | ||
| == 'cancelled' | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_stop_streaming_persists_not_found_state(): | ||
| invocation_context = SimpleNamespace(active_streaming_tools={}) | ||
| tool_context = SimpleNamespace(state={}) | ||
| streaming_lock = asyncio.Lock() | ||
|
|
||
| function_response = await functions._process_function_live_helper( | ||
| tool=SimpleNamespace(name='stop_streaming'), | ||
| tool_context=tool_context, | ||
| function_call=types.FunctionCall( | ||
| name='stop_streaming', | ||
| args={'function_name': 'missing_stream'}, | ||
| ), | ||
| function_args={'function_name': 'missing_stream'}, | ||
| invocation_context=invocation_context, | ||
| streaming_lock=streaming_lock, | ||
| ) | ||
|
|
||
| assert function_response == { | ||
| 'status': 'No active streaming function named missing_stream found' | ||
| } | ||
| assert ( | ||
| tool_context.state[functions.LONG_RUNNING_CANCELLATION_STATE_KEY][ | ||
| 'missing_stream' | ||
| ] | ||
| == 'not_found' | ||
| ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new tests cover the 'cancelled' and 'not_found' states, which is great. However, there's a third state, 'pending', that is recorded when a task cancellation times out. It would be beneficial to add a test case for this scenario to ensure complete coverage of the new state persistence logic. Here is a suggested test case: @pytest.mark.asyncio
async def test_stop_streaming_persists_pending_state_on_timeout():
async def slow_cancel_task():
try:
while True:
await asyncio.sleep(0.1)
except asyncio.CancelledError:
await asyncio.sleep(2) # Simulate slow cleanup
raise
task = asyncio.create_task(slow_cancel_task())
await asyncio.sleep(0.01) # Give the task a moment to start
invocation_context = SimpleNamespace(
active_streaming_tools={
'slow_tool': ActiveStreamingTool(task=task)
}
)
tool_context = SimpleNamespace(state={})
streaming_lock = asyncio.Lock()
function_response = await functions._process_function_live_helper(
tool=SimpleNamespace(name='stop_streaming'),
tool_context=tool_context,
function_call=types.FunctionCall(
name='stop_streaming',
args={'function_name': 'slow_tool'},
),
function_args={'function_name': 'slow_tool'},
invocation_context=invocation_context,
streaming_lock=streaming_lock,
)
assert function_response == {
'status': 'The task is not cancelled yet for slow_tool.'
}
assert (
tool_context.state[functions.LONG_RUNNING_CANCELLATION_STATE_KEY][
'slow_tool'
]
== 'pending'
)
# Clean up the task to avoid it running forever
with pytest.raises(asyncio.CancelledError):
await task |
||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_stop_streaming_persists_pending_state_on_timeout(monkeypatch): | ||
| async def _slow_cancel() -> None: | ||
| try: | ||
| while True: | ||
| await asyncio.sleep(0.1) | ||
| except asyncio.CancelledError: | ||
| await asyncio.sleep(2.0) | ||
| raise | ||
|
|
||
| task = asyncio.create_task(_slow_cancel()) | ||
| invocation_context = SimpleNamespace( | ||
| active_streaming_tools={'slow_stream': ActiveStreamingTool(task=task)} | ||
| ) | ||
| tool_context = SimpleNamespace(state={}) | ||
| streaming_lock = asyncio.Lock() | ||
|
|
||
| async def _fake_wait_for(awaitable, timeout): | ||
| del awaitable, timeout | ||
| raise asyncio.TimeoutError | ||
|
|
||
| monkeypatch.setattr(asyncio, 'wait_for', _fake_wait_for) | ||
|
|
||
| function_response = await functions._process_function_live_helper( | ||
| tool=SimpleNamespace(name='stop_streaming'), | ||
| tool_context=tool_context, | ||
| function_call=types.FunctionCall( | ||
| name='stop_streaming', | ||
| args={'function_name': 'slow_stream'}, | ||
| ), | ||
| function_args={'function_name': 'slow_stream'}, | ||
| invocation_context=invocation_context, | ||
| streaming_lock=streaming_lock, | ||
| ) | ||
|
|
||
| assert function_response == { | ||
| 'status': 'The task is not cancelled yet for slow_stream.' | ||
| } | ||
| assert ( | ||
| tool_context.state[functions.LONG_RUNNING_CANCELLATION_STATE_KEY][ | ||
| 'slow_stream' | ||
| ] | ||
| == 'pending' | ||
| ) | ||
|
|
||
| task.cancel() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This helper function can be refactored for better clarity. Using
.copy()is more idiomatic for creating a shallow copy of a dictionary than using thedict()constructor. I've also renamedprevioustocancellationsto make the variable's purpose clearer.