Skip to content

Commit a627716

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 069128d commit a627716

3 files changed

Lines changed: 107 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
@@ -81,7 +81,8 @@ class NodeInfo(BaseModel):
8181
8282
Such a run leaves no other trace in the session, so this event records that
8383
it finished. On resume the run is replayed from history instead of running
84-
again.
84+
again. For a ``wait_for_output`` node this means the run ended ``WAITING``;
85+
agents and ``rerun_on_resume`` nodes, which rerun on resume, get no marker.
8586
"""
8687

8788
@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: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3003,3 +3003,99 @@ async def parent(*, ctx: Context, node_input: Any):
30033003

30043004
assert emails_sent == ['email']
30053005
assert {'ok': True} in [e.output for e in events2 if e.output is not None]
3006+
3007+
3008+
class _QuietWaitingNode(BaseNode):
3009+
"""A wait_for_output node that never emits, so it always ends WAITING."""
3010+
3011+
wait_for_output: bool = True
3012+
calls: list[Any] = Field(default_factory=list)
3013+
3014+
@override
3015+
async def _run_impl(
3016+
self, *, ctx: Context, node_input: Any
3017+
) -> AsyncGenerator[Any, None]:
3018+
self.calls.append(node_input)
3019+
return
3020+
yield # pylint: disable=unreachable
3021+
3022+
3023+
@pytest.mark.parametrize(
3024+
('rerun_on_resume', 'expect_marker'), [(False, True), (True, False)]
3025+
)
3026+
@pytest.mark.asyncio
3027+
async def test_waiting_node_run_is_recorded_unless_it_reruns(
3028+
rerun_on_resume: bool, expect_marker: bool
3029+
):
3030+
"""A waiting run gets the marker, except on a node that reruns on resume."""
3031+
3032+
def ask():
3033+
return RequestInput(message='Approve?')
3034+
3035+
waiting = _QuietWaitingNode(name='waiting', rerun_on_resume=rerun_on_resume)
3036+
wf = Workflow(name='wf', edges=[(START, waiting), (START, ask)])
3037+
runner = testing_utils.InMemoryRunner(app=_app('waiting_marker', wf, False))
3038+
3039+
await runner.run_async('start')
3040+
3041+
markers = [
3042+
e
3043+
for e in runner.session.events
3044+
if e.node_info.name == 'waiting' and e.node_info.completed
3045+
]
3046+
assert bool(markers) == expect_marker
3047+
3048+
3049+
@pytest.mark.parametrize('resumable', [False, True])
3050+
@pytest.mark.asyncio
3051+
async def test_waiting_dynamic_child_keeps_parent_waiting_on_resume(
3052+
resumable: bool,
3053+
):
3054+
"""A ctx.run_node child that ended WAITING is not handed back as a result.
3055+
3056+
With raise_on_wait=True the parent stays WAITING in the first turn, and it
3057+
must still be WAITING after another branch's request is answered, rather
3058+
than continuing as if the child had finished with None.
3059+
"""
3060+
child = _QuietWaitingNode(name='child')
3061+
after_child = []
3062+
3063+
@node(rerun_on_resume=True)
3064+
async def parent(*, ctx: Context, node_input: Any):
3065+
await ctx.run_node(child, raise_on_wait=True)
3066+
after_child.append('ran')
3067+
yield 'parent done'
3068+
3069+
def ask():
3070+
return RequestInput(message='Approve?')
3071+
3072+
wf = Workflow(name='wf', edges=[(START, parent), (START, ask)])
3073+
runner = testing_utils.InMemoryRunner(
3074+
app=_app('waiting_dynamic_child', wf, resumable)
3075+
)
3076+
3077+
events2 = await _run_and_answer_first_request(runner)
3078+
3079+
assert after_child == []
3080+
assert 'parent done' not in [e.output for e in events2]
3081+
# The child's waiting run is replayed, not run again.
3082+
assert child.calls == [None]
3083+
3084+
3085+
@pytest.mark.parametrize('resumable', [False, True])
3086+
@pytest.mark.asyncio
3087+
async def test_quiet_waiting_node_is_not_rerun_on_resume(resumable: bool):
3088+
"""A waiting run that emitted no event is replayed, not run again."""
3089+
waiting = _QuietWaitingNode(name='waiting')
3090+
3091+
def ask():
3092+
return RequestInput(message='Approve?')
3093+
3094+
wf = Workflow(name='wf', edges=[(START, waiting), (START, ask)])
3095+
runner = testing_utils.InMemoryRunner(
3096+
app=_app('quiet_waiting_not_rerun', wf, resumable)
3097+
)
3098+
3099+
await _run_and_answer_first_request(runner)
3100+
3101+
assert len(waiting.calls) == 1

0 commit comments

Comments
 (0)