Skip to content

Commit d65ab2e

Browse files
committed
fix(workflow): record a waiting wait_for_output run that emitted nothing
The completion marker skipped every wait_for_output node, so a run that ended WAITING without emitting any event left nothing in the session. On resume it was treated as a new run and executed again with the input it already handled. Only agents (task or chat mode) and rerun_on_resume nodes rerun on resume to take new input, so only they skip the marker now. Other wait_for_output nodes record that their run ended. A ctx.run_node child recorded this way still reports no output, so raise_on_wait keeps its parent WAITING. See google#7905.
1 parent d3f2698 commit d65ab2e

3 files changed

Lines changed: 86 additions & 4 deletions

File tree

‎src/google/adk/events/event.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ class NodeInfo(BaseModel):
8080
8181
Such a run leaves no other trace in the session, so this event records that
8282
it finished. On resume the run is replayed from history instead of running
83-
again.
83+
again. For a ``wait_for_output`` node this means the run ended ``WAITING``;
84+
agents and ``rerun_on_resume`` nodes, which rerun on resume, get no marker.
8485
"""
8586

8687
@property

‎src/google/adk/workflow/_node_runner.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ async def _record_completion_without_result(self, ctx: Context) -> None:
560560
nothing in the session that shows it ran, so resume would run it (and its
561561
side effects) again. The marker lets the scan replay it instead.
562562
"""
563+
from ..agents.base_agent import BaseAgent
563564
from ..events.event import Event
564565
from ._workflow import Workflow
565566

@@ -574,9 +575,14 @@ async def _record_completion_without_result(self, ctx: Context) -> None:
574575
or ctx._output_delegated
575576
or ctx.route is not None
576577
or ctx.actions.transfer_to_agent is not None
577-
# Finishing without output means a wait_for_output node is still
578-
# waiting for more input, not that it is done.
579-
or self._node.wait_for_output
578+
# A waiting agent (task or chat mode) or rerun_on_resume node reruns on
579+
# resume to take new input, so its run must not be recorded as ended.
580+
or (
581+
self._node.wait_for_output
582+
and (
583+
self._node.rerun_on_resume or isinstance(self._node, BaseAgent)
584+
)
585+
)
580586
# Tests wrap Context around invocation-context stubs that lack
581587
# is_aborted or return a truthy Mock; neither means aborted.
582588
or getattr(ctx._invocation_context, "is_aborted", False) is True

‎tests/unittests/workflow/test_workflow_hitl.py‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2817,3 +2817,78 @@ async def parent(*, ctx: Context, node_input: Any):
28172817

28182818
assert emails_sent == ['email']
28192819
assert {'ok': True} in [e.output for e in events2 if e.output is not None]
2820+
2821+
2822+
class _QuietWaitingNode(BaseNode):
2823+
"""A wait_for_output node that never emits, so it always ends WAITING."""
2824+
2825+
wait_for_output: bool = True
2826+
calls: list[Any] = Field(default_factory=list)
2827+
2828+
@override
2829+
async def _run_impl(
2830+
self, *, ctx: Context, node_input: Any
2831+
) -> AsyncGenerator[Any, None]:
2832+
self.calls.append(node_input)
2833+
return
2834+
yield # pylint: disable=unreachable
2835+
2836+
2837+
@pytest.mark.parametrize(
2838+
('rerun_on_resume', 'expect_marker'), [(False, True), (True, False)]
2839+
)
2840+
@pytest.mark.asyncio
2841+
async def test_waiting_node_run_is_recorded_unless_it_reruns(
2842+
rerun_on_resume: bool, expect_marker: bool
2843+
):
2844+
"""A waiting run gets the marker, except on a node that reruns on resume."""
2845+
2846+
def ask():
2847+
return RequestInput(message='Approve?')
2848+
2849+
waiting = _QuietWaitingNode(name='waiting', rerun_on_resume=rerun_on_resume)
2850+
wf = Workflow(name='wf', edges=[(START, waiting), (START, ask)])
2851+
runner = testing_utils.InMemoryRunner(app=_app('waiting_marker', wf, False))
2852+
2853+
await runner.run_async('start')
2854+
2855+
markers = [
2856+
e
2857+
for e in runner.session.events
2858+
if e.node_info.name == 'waiting' and e.node_info.completed
2859+
]
2860+
assert bool(markers) == expect_marker
2861+
2862+
2863+
@pytest.mark.parametrize('resumable', [False, True])
2864+
@pytest.mark.asyncio
2865+
async def test_waiting_dynamic_child_keeps_parent_waiting_on_resume(
2866+
resumable: bool,
2867+
):
2868+
"""A ctx.run_node child that ended WAITING is not handed back as a result.
2869+
2870+
With raise_on_wait=True the parent stays WAITING in the first turn, and it
2871+
must still be WAITING after another branch's request is answered, rather
2872+
than continuing as if the child had finished with None.
2873+
"""
2874+
child = _QuietWaitingNode(name='child')
2875+
after_child = []
2876+
2877+
@node(rerun_on_resume=True)
2878+
async def parent(*, ctx: Context, node_input: Any):
2879+
await ctx.run_node(child, raise_on_wait=True)
2880+
after_child.append('ran')
2881+
yield 'parent done'
2882+
2883+
def ask():
2884+
return RequestInput(message='Approve?')
2885+
2886+
wf = Workflow(name='wf', edges=[(START, parent), (START, ask)])
2887+
runner = testing_utils.InMemoryRunner(
2888+
app=_app('waiting_dynamic_child', wf, resumable)
2889+
)
2890+
2891+
events2 = await _run_and_answer_first_request(runner)
2892+
2893+
assert after_child == []
2894+
assert 'parent done' not in [e.output for e in events2]

0 commit comments

Comments
 (0)