Skip to content
Open
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
39 changes: 29 additions & 10 deletions src/google/adk/runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,37 @@ def _can_transfer_between_agents(root: Any) -> bool:


def _stamp_event_branch_context(ic: InvocationContext, event: Event) -> None:
"""Stamps the event with the branch and isolation scope of its matching function call."""
"""Stamps the event with the branch and isolation scope of its matching function calls."""
from .flows.llm_flows.tools._functions import find_event_by_function_call_id
from .flows.llm_flows.tools._functions import find_matching_function_call

if function_call := find_matching_function_call(
ic._get_events(current_invocation=True), event
):
event.branch = function_call.branch
if (
event.isolation_scope is None
and function_call.isolation_scope is not None
):
event.isolation_scope = function_call.isolation_scope
responses = event.get_function_responses()
if not responses:
return

events = ic._get_events(current_invocation=True)
search_space = events[:-1] if events and events[-1].id == event.id else events
matched_calls = [
fc_event
for resp in responses
if resp.id
and (fc_event := find_event_by_function_call_id(search_space, resp.id))
is not None
]
if not matched_calls:
fallback = find_matching_function_call(events, event)
if fallback is None:
return
matched_calls = [fallback]

branches = {fc.branch for fc in matched_calls}
if len(branches) == 1:
event.branch = next(iter(branches))
scopes = {fc.isolation_scope for fc in matched_calls}
if event.isolation_scope is None and len(scopes) == 1:
only_scope = next(iter(scopes))
if only_scope is not None:
event.isolation_scope = only_scope


class Runner:
Expand Down
75 changes: 75 additions & 0 deletions tests/unittests/runners/test_resume_invocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,78 @@ async def _run_async_impl(
assert not await runner.run_async(
invocation_id=invocation_events[0].invocation_id
)


@pytest.mark.asyncio
async def test_resumable_parallel_agent_single_message_resumes_all_branches():
"""One user message answering calls in multiple branches resumes all of them."""
from google.adk.models.base_llm import BaseLlm
from google.adk.models.llm_response import LlmResponse
from google.adk.tools import LongRunningFunctionTool

def approve():
return None

class _FakeBranchLlm(BaseLlm):
async def generate_content_async(self, llm_request, stream=False):
answered = any(
p.function_response
for c in llm_request.contents
for p in c.parts or []
)
part = (
types.Part(text=f"{self.model} done")
if answered
else types.Part.from_function_call(name="approve", args={})
)
yield LlmResponse(content=types.Content(role="model", parts=[part]))

def _branch(name: str) -> LlmAgent:
return LlmAgent(
name=name,
model=_FakeBranchLlm(model=name),
tools=[LongRunningFunctionTool(func=approve)],
)

runner = testing_utils.InMemoryRunner(
app=App(
name="test_app",
root_agent=ParallelAgent(
name="root_agent",
sub_agents=[_branch("branch_a"), _branch("branch_b")],
),
resumability_config=ResumabilityConfig(is_resumable=True),
)
)

initial_events = await runner.run_async("start")
invocation_id = initial_events[0].invocation_id
calls = [c for e in initial_events for c in e.get_function_calls()]
assert len(calls) == 2

answer = types.Content(
role="user",
parts=[
types.Part(
function_response=types.FunctionResponse(
id=c.id, name="approve", response={"result": "ok"}
)
)
for c in calls
],
)
resumed_events = await runner.run_async(
answer, invocation_id=invocation_id
)
texts = sorted(
f"{e.author}: {p.text}"
for e in resumed_events
if e.content
for p in e.content.parts or []
if p.text
)
assert texts == ["branch_a: branch_a done", "branch_b: branch_b done"]
assert any(
e.author == "root_agent" and e.actions.end_of_agent
for e in resumed_events
)