Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/google/adk/workflow/_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,11 +819,14 @@ async def _handle_completion(
return

# A genuine execution records a checkpoint on completion. A replayed
# fast-forward instead re-surfaces its recovered output so a resumable
# stream stays complete, without a redundant checkpoint.
# fast-forward instead re-surfaces its output so a resumable stream stays
# complete, without a redundant checkpoint. An output recovered from the
# session is already persisted there: emitting it again would store it
# once more on every later resume. Only an output this resume derived from
# resolved interrupts still needs to be persisted.
if not replayed:
await self._emit_node_checkpoint(loop_state, ctx)
else:
elif run.recovered_state is None or run.recovered_state.output is None:
await self._maybe_reemit_replayed_output(child_ctx, ctx)

# Buffer downstream triggers.
Expand Down
109 changes: 109 additions & 0 deletions tests/unittests/workflow/test_workflow_hitl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3081,3 +3081,112 @@ def collect(node_input: dict[str, Any]) -> str:
assert b_runs == 1
outputs3 = [e.output for e in events3 if e.output is not None]
assert outputs3[-1] == 'approved:True|from_b'


@pytest.mark.asyncio
async def test_resume_does_not_re_persist_fast_forwarded_outputs(
request: pytest.FixtureRequest,
):
"""Each answered RequestInput output is stored once across resumes.

A loop parks on RequestInput every round and is resumed in the same
invocation. Every resume fast-forwards the runs that finished in earlier
turns. Their outputs are already in the session, so storing them again
would make the session grow quadratically with the number of resumes.
"""
rounds = 4

class _CountNode(BaseNode):

@override
async def _run_impl(
self, *, ctx: Context, node_input: Any
) -> AsyncGenerator[Any, None]:
yield Event(state={'round': ctx.state.get('round', 0) + 1})

class _AskNode(BaseNode):

@override
async def _run_impl(
self, *, ctx: Context, node_input: Any
) -> AsyncGenerator[Any, None]:
yield RequestInput(message='Continue?')

class _GateNode(BaseNode):

@override
async def _run_impl(
self, *, ctx: Context, node_input: Any
) -> AsyncGenerator[Any, None]:
yield Event(route='again' if ctx.state['round'] < rounds else 'done')

count = _CountNode(name='count')
gate = _GateNode(name='gate')
workflow = Workflow(
name='loop',
edges=[
('START', count, _AskNode(name='ask'), gate),
(gate, {'again': count, 'done': _CountNode(name='done')}),
],
)
session_service = InMemorySessionService()
runner = Runner(
app=App(
name=request.function.__name__,
root_agent=workflow,
resumability_config=ResumabilityConfig(is_resumable=True),
),
session_service=session_service,
)
session = await session_service.create_session(
app_name=request.function.__name__, user_id='u'
)

async def run_turn(
message: types.Content, invocation_id: str | None = None
) -> tuple[str | None, list[str]]:
interrupt_ids = []
async for event in runner.run_async(
user_id='u',
session_id=session.id,
invocation_id=invocation_id,
new_message=message,
):
invocation_id = event.invocation_id
interrupt_ids += extract_event_interrupt_ids(event)
return invocation_id, interrupt_ids

async def session_events() -> list[Event]:
stored = await session_service.get_session(
app_name=request.function.__name__, user_id='u', session_id=session.id
)
return stored.events

invocation_id, interrupt_ids = await run_turn(
types.Content(role='user', parts=[types.Part(text='start')])
)
answered = set()
events_added_per_resume = []
while pending := [i for i in interrupt_ids if i not in answered]:
answered.add(pending[-1])
before = len(await session_events())
invocation_id, interrupt_ids = await run_turn(
types.Content(
role='user',
parts=[create_request_input_response(pending[-1], {'ok': True})],
),
invocation_id,
)
events_added_per_resume.append(len(await session_events()) - before)

assert len(answered) == rounds
stored_ask_outputs = [
e.node_info.path
for e in await session_events()
if e.output is not None and e.node_info and '/ask@' in e.node_info.path
]
assert len(stored_ask_outputs) == rounds
assert len(set(stored_ask_outputs)) == rounds
# Every resume but the last one runs a full round, so each adds the same
# number of events instead of one more than the resume before it.
assert len(set(events_added_per_resume[:-1])) == 1
24 changes: 3 additions & 21 deletions tests/unittests/workflow/test_workflow_parallel_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,16 +773,8 @@ async def test_parallel_worker_pauses_for_human_input(
# Then item2 should complete and the full list should be returned
simplified_events2 = simplify_events_with_node(events2)

# NodeA's output was persisted in run 1, so the resume does not emit it again.
assert simplified_events2 == [
(
'parallel_worker_hitl_agent@1/NodeA@1',
{
'output': [
{'val': 'item1', 'ask': False},
{'val': 'item2', 'ask': True},
],
},
),
(
'parallel_worker_hitl_agent@1/Worker@1/Worker@2',
{'output': 'item2_resumed'},
Expand Down Expand Up @@ -1228,13 +1220,8 @@ async def hitl_concurrency_worker(
invocation_id_2 = events2[0].invocation_id

simplified_events2 = simplify_events_with_node(events2)
# NodeA's output was persisted in run 1, so this resume does not emit it again.
assert simplified_events2 == [
(
'max_parallel_workers_hitl_agent@1/NodeA@1',
{
'output': items,
},
),
(
'max_parallel_workers_hitl_agent@1/Worker@1/Worker@2',
{'output': 'item2_resumed'},
Expand Down Expand Up @@ -1267,13 +1254,8 @@ async def hitl_concurrency_worker(
# Then all should complete
simplified_events3 = simplify_events_with_node(events3)

# NodeA's output was persisted in run 1, so this resume does not emit it again.
assert simplified_events3 == [
(
'max_parallel_workers_hitl_agent@1/NodeA@1',
{
'output': items,
},
),
(
'max_parallel_workers_hitl_agent@1/Worker@1/Worker@3',
{'output': 'item3_resumed'},
Expand Down
Loading