From e700db55e80f7b549da820732ee0f10956c93972 Mon Sep 17 00:00:00 2001 From: Chaitanya Deshpande Date: Wed, 2 Dec 2026 22:08:56 +0530 Subject: [PATCH] fix(agents): keep a resumed SequentialAgent or LoopAgent paused while a long-running call is unanswered In a resumable app, a sub-agent that resumes but still waits on a long-running call ends its run without yielding the call again (the call was already yielded and persisted). SequentialAgent and LoopAgent only detect a pause from the events their sub-agent yields, so they treated the waiting sub-agent as finished and ran the next one. Answering one of two long-running calls, or one branch of a ParallelAgent, therefore ran the rest of the workflow early, ended the invocation, and the later answer was dropped. Both agents now also check the current invocation for interrupts that are still unanswered (index_open_interrupts) before moving to the next sub-agent. --- src/google/adk/agents/invocation_context.py | 13 ++ src/google/adk/agents/loop_agent.py | 5 + src/google/adk/agents/sequential_agent.py | 5 + .../runners/test_resume_invocation.py | 138 ++++++++++++++++++ 4 files changed, 161 insertions(+) diff --git a/src/google/adk/agents/invocation_context.py b/src/google/adk/agents/invocation_context.py index 4674444b98a..b0af8a6759e 100644 --- a/src/google/adk/agents/invocation_context.py +++ b/src/google/adk/agents/invocation_context.py @@ -37,6 +37,7 @@ from ..events._branch_path import _BranchPath from ..events._internal_metadata import without_internal_metadata from ..events._interrupts import extract_event_interrupt_ids +from ..events._interrupts import index_open_interrupts from ..events._node_path_builder import NodePathBuilder from ..events.event import Event from ..live import _active_background_tool @@ -829,6 +830,18 @@ def should_pause_invocation(self, event: Event) -> bool: return False + def _has_open_interrupts(self) -> bool: + """Returns whether a call of this invocation still awaits its answer. + + A resumed agent whose long-running call is still unanswered ends its run + without yielding the call again, so its parent sees no pausing event. The + parent checks this before moving on, so that answering one of several + long-running calls does not end the invocation with the rest unanswered. + """ + return bool( + index_open_interrupts(self._get_events(current_invocation=True)) + ) + def new_invocation_context_id() -> str: return "e-" + platform_uuid.new_uuid() diff --git a/src/google/adk/agents/loop_agent.py b/src/google/adk/agents/loop_agent.py index 9f3bf8857a6..a134078056d 100644 --- a/src/google/adk/agents/loop_agent.py +++ b/src/google/adk/agents/loop_agent.py @@ -123,6 +123,11 @@ async def _run_async_impl( if ctx.should_pause_invocation(event): pause_invocation = True + # A resumed sub-agent still waiting on a long-running call yields no + # pausing event, so check for unanswered calls before moving on. + if ctx.is_resumable and ctx._has_open_interrupts(): + pause_invocation = True + if should_exit or pause_invocation: break # break inner for loop diff --git a/src/google/adk/agents/sequential_agent.py b/src/google/adk/agents/sequential_agent.py index 2ec8cf51e6c..d2fd2014ec0 100644 --- a/src/google/adk/agents/sequential_agent.py +++ b/src/google/adk/agents/sequential_agent.py @@ -132,6 +132,11 @@ async def _run_async_impl( if ctx.should_pause_invocation(event): pause_invocation = True + # A resumed sub-agent still waiting on a long-running call yields no + # pausing event, so check for unanswered calls before moving on. + if ctx.is_resumable and ctx._has_open_interrupts(): + pause_invocation = True + # Skip the rest of the sub-agents if the invocation is paused. if pause_invocation: return diff --git a/tests/unittests/runners/test_resume_invocation.py b/tests/unittests/runners/test_resume_invocation.py index 38d3eefdcab..b4a72e91eb7 100644 --- a/tests/unittests/runners/test_resume_invocation.py +++ b/tests/unittests/runners/test_resume_invocation.py @@ -20,7 +20,9 @@ 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.loop_agent import LoopAgent from google.adk.agents.parallel_agent import ParallelAgent +from google.adk.agents.sequential_agent import SequentialAgent from google.adk.apps.app import App from google.adk.apps.app import ResumabilityConfig from google.adk.events.event import Event @@ -494,3 +496,139 @@ async def _run_async_impl( assert not await runner.run_async( invocation_id=invocation_events[0].invocation_id ) + + +def _long_running_calls(events: list[Event]) -> list[types.FunctionCall]: + return [ + fc + for event in events + for fc in event.get_function_calls() + if fc.name == "test_tool" + ] + + +def _answer(function_call: types.FunctionCall) -> types.Content: + return testing_utils.UserContent( + Part( + function_response=FunctionResponse( + id=function_call.id, + name=function_call.name, + response={"result": "approved"}, + ) + ) + ) + + +def _texts(events: list[Event]) -> list[tuple[str, str]]: + return [ + (event.author, part.text) + for event in events + if event.content + for part in event.content.parts or [] + if part.text + ] + + +@pytest.mark.parametrize("workflow_agent_cls", [SequentialAgent, LoopAgent]) +@pytest.mark.asyncio +async def test_answering_one_of_two_long_running_calls_keeps_workflow_paused( + workflow_agent_cls, +): + """A sub-agent still waiting on a call keeps its workflow paused.""" + first_agent = LlmAgent( + name="first_agent", + model=testing_utils.MockModel.create( + responses=[ + [ + Part.from_function_call(name="test_tool", args={}), + Part.from_function_call(name="test_tool", args={}), + ], + "first agent done", + ] + ), + tools=[LongRunningFunctionTool(func=test_tool)], + ) + next_agent = LlmAgent( + name="next_agent", + model=testing_utils.MockModel.create(responses=["next agent done"]), + ) + kwargs = {"max_iterations": 1} if workflow_agent_cls is LoopAgent else {} + runner = testing_utils.InMemoryRunner( + app=App( + name="test_app", + root_agent=workflow_agent_cls( + name="root_agent", + sub_agents=[first_agent, next_agent], + **kwargs, + ), + resumability_config=ResumabilityConfig(is_resumable=True), + ) + ) + first_call, second_call = _long_running_calls( + await runner.run_async("test user query") + ) + + # The second call is still unanswered, so nothing may run yet. + assert not await runner.run_async(new_message=_answer(first_call)) + + events = await runner.run_async(new_message=_answer(second_call)) + assert _texts(events) == [ + ("first_agent", "first agent done"), + ("next_agent", "next agent done"), + ] + assert events[-1].author == "root_agent" + assert events[-1].actions.end_of_agent + + +@pytest.mark.asyncio +async def test_answering_one_parallel_branch_keeps_sequential_agent_paused(): + """The step after a ParallelAgent waits for every branch's answer.""" + + def branch(name: str) -> LlmAgent: + return LlmAgent( + name=name, + model=testing_utils.MockModel.create( + responses=[ + Part.from_function_call(name="test_tool", args={}), + f"{name} done", + ] + ), + tools=[LongRunningFunctionTool(func=test_tool)], + ) + + next_agent = LlmAgent( + name="next_agent", + model=testing_utils.MockModel.create(responses=["next agent done"]), + ) + runner = testing_utils.InMemoryRunner( + app=App( + name="test_app", + root_agent=SequentialAgent( + name="root_agent", + sub_agents=[ + ParallelAgent( + name="parallel_agent", + sub_agents=[branch("branch_a"), branch("branch_b")], + ), + next_agent, + ], + ), + resumability_config=ResumabilityConfig(is_resumable=True), + ) + ) + calls = { + event.author: fc + for event in await runner.run_async("test user query") + for fc in event.get_function_calls() + } + + events = await runner.run_async(new_message=_answer(calls["branch_a"])) + assert _texts(events) == [("branch_a", "branch_a done")] + + events = await runner.run_async(new_message=_answer(calls["branch_b"])) + assert _texts(events) == [ + ("branch_b", "branch_b done"), + ("next_agent", "next agent done"), + ] + assert events[-1].author == "root_agent" + assert events[-1].actions.end_of_agent