-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathtest_resume_invocation.py
More file actions
364 lines (329 loc) · 10.9 KB
/
test_resume_invocation.py
File metadata and controls
364 lines (329 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# 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.
"""Tests for edge cases of resuming invocations."""
import asyncio
import copy
from typing import AsyncGenerator
from google.adk.agents.base_agent import BaseAgent
from google.adk.agents.invocation_context import InvocationContext
from google.adk.agents.llm_agent import LlmAgent
from google.adk.agents.parallel_agent import ParallelAgent
from google.adk.apps.app import App
from google.adk.apps.app import ResumabilityConfig
from google.adk.events.event import Event
from google.adk.events.event_actions import EventActions
from google.adk.tools.long_running_tool import LongRunningFunctionTool
from google.genai import types
from google.genai.types import FunctionResponse
from google.genai.types import Part
import pytest
from .. import testing_utils
def transfer_call_part(agent_name: str) -> Part:
return Part.from_function_call(
name="transfer_to_agent", args={"agent_name": agent_name}
)
TRANSFER_RESPONSE_PART = Part.from_function_response(
name="transfer_to_agent", response={"result": None}
)
def test_tool() -> dict[str, str]:
return {"result": "test tool result"}
test_tool.__test__ = False
class _ParallelEscalationTestingAgent(BaseAgent):
"""A testing agent that emits a single event after a delay."""
delay: float = 0
response_text: str = ""
escalate: bool = False
emit_follow_up_after_first_event: bool = False
def _create_event(
self,
ctx: InvocationContext,
text: str,
*,
escalate: bool = False,
) -> Event:
return Event(
author=self.name,
branch=ctx.branch,
invocation_id=ctx.invocation_id,
content=types.Content(role="model", parts=[types.Part(text=text)]),
actions=EventActions(escalate=True) if escalate else EventActions(),
)
async def _run_async_impl(
self, ctx: InvocationContext
) -> AsyncGenerator[Event, None]:
await asyncio.sleep(self.delay)
yield self._create_event(
ctx, self.response_text, escalate=self.escalate
)
if self.emit_follow_up_after_first_event:
yield self._create_event(ctx, "This event should not be emitted.")
@pytest.mark.asyncio
async def test_resume_invocation_from_sub_agent():
"""A test case for an edge case, where an invocation-to-resume starts from a sub-agent.
For example:
invocation1: root_agent -> sub_agent
invocation2: sub_agent [paused][resume]
"""
# Step 1: Setup
# root_agent -> sub_agent
sub_agent = LlmAgent(
name="sub_agent",
model=testing_utils.MockModel.create(
responses=[
"first response from sub_agent",
"second response from sub_agent",
"third response from sub_agent",
]
),
)
root_agent = LlmAgent(
name="root_agent",
model=testing_utils.MockModel.create(
responses=[transfer_call_part(sub_agent.name)]
),
sub_agents=[sub_agent],
)
runner = testing_utils.InMemoryRunner(
app=App(
name="test_app",
root_agent=root_agent,
resumability_config=ResumabilityConfig(is_resumable=True),
)
)
# Step 2: Run the first invocation
# Expect the invocation to start from root_agent and transferred to sub_agent.
invocation_1_events = await runner.run_async("test user query")
assert testing_utils.simplify_resumable_app_events(
copy.deepcopy(invocation_1_events)
) == [
(
root_agent.name,
transfer_call_part(sub_agent.name),
),
(
root_agent.name,
TRANSFER_RESPONSE_PART,
),
(
sub_agent.name,
"first response from sub_agent",
),
(
sub_agent.name,
testing_utils.END_OF_AGENT,
),
(
root_agent.name,
testing_utils.END_OF_AGENT,
),
]
# Step 3: Run the second invocation
# Expect the invocation to directly start from sub_agent.
invocation_2_events = await runner.run_async(
"test user query 2",
)
assert testing_utils.simplify_resumable_app_events(
copy.deepcopy(invocation_2_events)
) == [
(
sub_agent.name,
"second response from sub_agent",
),
(sub_agent.name, testing_utils.END_OF_AGENT),
]
# Asserts the invocation will be a no-op if the current agent in context is
# already final.
assert not await runner.run_async(
invocation_id=invocation_2_events[0].invocation_id
)
# Step 4: Copy all session.events[:-1] to a new session
# This is to simulate the case where we pause on the second invocation.
session_id = runner.session_id
session = await runner.runner.session_service.get_session(
app_name="test_app", user_id="test_user", session_id=session_id
)
new_session = await runner.runner.session_service.create_session(
app_name=session.app_name, user_id=session.user_id
)
for event in session.events[:-1]:
await runner.runner.session_service.append_event(new_session, event)
runner.session_id = new_session.id
# Step 5: Resume the second invocation
resumed_invocation_2_events = await runner.run_async(
invocation_id=invocation_2_events[0].invocation_id
)
assert testing_utils.simplify_resumable_app_events(
copy.deepcopy(resumed_invocation_2_events)
) == [
(
sub_agent.name,
"third response from sub_agent",
),
(sub_agent.name, testing_utils.END_OF_AGENT),
]
@pytest.mark.asyncio
async def test_resume_any_invocation():
"""A test case for resuming a previous invocation instead of the last one."""
# Step 1: Setup
long_running_test_tool = LongRunningFunctionTool(
func=test_tool,
)
root_agent = LlmAgent(
name="root_agent",
model=testing_utils.MockModel.create(
responses=[
Part.from_function_call(name="test_tool", args={}),
"llm response in invocation 2",
Part.from_function_call(name="test_tool", args={}),
"llm response after resuming invocation 1",
]
),
tools=[long_running_test_tool],
)
runner = testing_utils.InMemoryRunner(
app=App(
name="test_app",
root_agent=root_agent,
resumability_config=ResumabilityConfig(is_resumable=True),
)
)
# Step 2: Run the first invocation, which pauses on the long running function.
invocation_1_events = await runner.run_async("test user query")
assert testing_utils.simplify_resumable_app_events(
copy.deepcopy(invocation_1_events)
) == [
(
root_agent.name,
Part.from_function_call(name="test_tool", args={}),
),
(
root_agent.name,
Part.from_function_response(
name="test_tool", response={"result": "test tool result"}
),
),
]
# Step 3: Run the second invocation, expect it to finish normally.
invocation_2_events = await runner.run_async(
"test user query 2",
)
assert testing_utils.simplify_resumable_app_events(
copy.deepcopy(invocation_2_events)
) == [
(
root_agent.name,
"llm response in invocation 2",
),
(root_agent.name, testing_utils.END_OF_AGENT),
]
# Step 4: Run the third invocation, which also pauses on the long running
# function.
invocation_3_events = await runner.run_async(
"test user query 3",
)
assert testing_utils.simplify_resumable_app_events(
copy.deepcopy(invocation_3_events)
) == [
(
root_agent.name,
Part.from_function_call(name="test_tool", args={}),
),
(
root_agent.name,
Part.from_function_response(
name="test_tool", response={"result": "test tool result"}
),
),
]
# Step 5: Resume the first invocation with long running function response.
resumed_invocation_1_events = await runner.run_async(
invocation_id=invocation_1_events[0].invocation_id,
new_message=testing_utils.UserContent(
Part(
function_response=FunctionResponse(
id=invocation_1_events[0].content.parts[0].function_call.id,
name="test_tool",
response={"result": "test tool update"},
)
),
),
)
assert testing_utils.simplify_resumable_app_events(
copy.deepcopy(resumed_invocation_1_events)
) == [
(
root_agent.name,
"llm response after resuming invocation 1",
),
(root_agent.name, testing_utils.END_OF_AGENT),
]
@pytest.mark.asyncio
async def test_resumable_parallel_agent_escalation_short_circuits_persisted_run():
"""Runner persists fast+escalating events and marks the parent run complete."""
fast_agent = _ParallelEscalationTestingAgent(
name="fast_agent",
delay=0.05,
response_text="fast response",
)
escalating_agent = _ParallelEscalationTestingAgent(
name="escalating_agent",
delay=0.1,
response_text="escalating response",
escalate=True,
emit_follow_up_after_first_event=True,
)
slow_agent = _ParallelEscalationTestingAgent(
name="slow_agent",
delay=0.5,
response_text="slow response",
)
runner = testing_utils.InMemoryRunner(
app=App(
name="test_app",
root_agent=ParallelAgent(
name="root_agent",
sub_agents=[fast_agent, escalating_agent, slow_agent],
),
resumability_config=ResumabilityConfig(is_resumable=True),
)
)
invocation_events = await runner.run_async("test user query")
simplified_events = testing_utils.simplify_resumable_app_events(
copy.deepcopy(invocation_events)
)
assert simplified_events == [
("root_agent", {}),
("fast_agent", "fast response"),
("escalating_agent", "escalating response"),
("root_agent", testing_utils.END_OF_AGENT),
]
session = await runner.runner.session_service.get_session(
app_name=runner.app_name,
user_id="test_user",
session_id=runner.session_id,
)
persisted_events = [
event
for event in session.events
if event.invocation_id == invocation_events[0].invocation_id
and event.author != "user"
]
assert testing_utils.simplify_resumable_app_events(
copy.deepcopy(persisted_events)
) == simplified_events
assert all(event.author != "slow_agent" for event in persisted_events)
# A completed resumable invocation should not restart cancelled siblings.
assert not await runner.run_async(
invocation_id=invocation_events[0].invocation_id
)