Skip to content

Commit bfc8553

Browse files
perf(agents): prepare sibling receiver context once per request
Parse the receiver branch and check its tool ancestry outside the event loop when sibling sharing is enabled. Reuse that path for projecting final answers without adding branch preparation to the default path. Cover ordinary run IDs, receivers inside tool ancestors, and receivers run as tools. Group the sibling-answer cases with the contents test module prefix. Refs google#6496
1 parent e2b8353 commit bfc8553

2 files changed

Lines changed: 83 additions & 13 deletions

File tree

‎src/google/adk/flows/llm_flows/context/_contents.py‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def _copy_content_for_request(
446446

447447

448448
def _get_sibling_final_response(
449-
current_branch: str | None,
449+
current_path: _BranchPath,
450450
event: Event,
451451
agent_name: str,
452452
tool_call_ids: set[str],
@@ -458,8 +458,7 @@ def _get_sibling_final_response(
458458
through skip_summarization, so finality alone is insufficient.
459459
"""
460460
if (
461-
not current_branch
462-
or not event.branch
461+
not event.branch
463462
or event.author in ('user', agent_name)
464463
or event.isolation_scope is not None
465464
or event.partial
@@ -472,13 +471,11 @@ def _get_sibling_final_response(
472471
):
473472
return None
474473

475-
current_path = _BranchPath.from_string(current_branch)
476474
event_path = _BranchPath.from_string(event.branch)
477475
if (
478476
current_path.parent is None
479477
or current_path.parent != event_path.parent
480478
or current_path == event_path
481-
or current_path.run_ids.intersection(tool_call_ids)
482479
or event_path.run_ids.intersection(tool_call_ids)
483480
):
484481
return None
@@ -580,6 +577,22 @@ def _get_contents(
580577
)
581578
tool_call_ids = _collect_function_call_ids(rewind_filtered_events)
582579

580+
# The receiver's branch and tool ancestry are constant across the history.
581+
# Leave the default path alone when sibling sharing is disabled.
582+
sibling_context_path: _BranchPath | None = None
583+
if (
584+
include_sibling_final_responses
585+
and isolation_scope is None
586+
and not is_single_turn
587+
and current_branch
588+
):
589+
current_path = _BranchPath.from_string(current_branch)
590+
if (
591+
current_path.parent is not None
592+
and not current_path.run_ids.intersection(tool_call_ids)
593+
):
594+
sibling_context_path = current_path
595+
583596
# Parse the events, leaving the contents and the function calls and
584597
# responses from the current agent.
585598
raw_filtered_events = []
@@ -597,13 +610,9 @@ def _get_contents(
597610
current_branch, event, agent_name, tool_call_ids
598611
):
599612
raw_filtered_events.append(event)
600-
elif (
601-
include_sibling_final_responses
602-
and isolation_scope is None
603-
and not is_single_turn
604-
):
613+
elif sibling_context_path is not None:
605614
sibling_answer = _get_sibling_final_response(
606-
current_branch, event, agent_name, tool_call_ids
615+
sibling_context_path, event, agent_name, tool_call_ids
607616
)
608617
if sibling_answer is not None:
609618
raw_filtered_events.append(sibling_answer)

tests/unittests/flows/llm_flows/context/test_sibling_final_responses.py renamed to tests/unittests/flows/llm_flows/context/test_contents_sibling_final_responses.py

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ def _answer(**overrides):
3838
return Event(**values)
3939

4040

41-
async def _request(events, *, enabled=True, scope=None, mode='chat', **options):
41+
async def _request(
42+
events,
43+
*,
44+
enabled=True,
45+
scope=None,
46+
mode='chat',
47+
branch='dispatcher.guide',
48+
**options,
49+
):
4250
agent = Agent(
4351
name='guide',
4452
model='gemini-2.5-flash',
@@ -47,7 +55,7 @@ async def _request(events, *, enabled=True, scope=None, mode='chat', **options):
4755
**options,
4856
)
4957
ctx = await testing_utils.create_invocation_context(agent=agent)
50-
ctx.branch = 'dispatcher.guide'
58+
ctx.branch = branch
5159
ctx.isolation_scope = scope
5260
ctx.user_content = None
5361
ctx.session.events = events
@@ -326,6 +334,59 @@ async def test_tool_sub_branch_text_is_not_a_sibling_answer(author):
326334
assert request.contents == []
327335

328336

337+
@pytest.mark.parametrize(
338+
'receiver_branch,sibling_branch,tool_call_id,can_share',
339+
[
340+
(
341+
'dispatcher.guide@guide-run',
342+
'dispatcher.bookings@bookings-run',
343+
None,
344+
True,
345+
),
346+
(
347+
348+
349+
'adk-call',
350+
False,
351+
),
352+
(
353+
'dispatcher.guide@adk-call',
354+
'dispatcher.bookings',
355+
'adk-call',
356+
False,
357+
),
358+
],
359+
ids=['ordinary-run-ids', 'tool-ancestor', 'receiver-as-tool'],
360+
)
361+
async def test_only_non_tool_receivers_can_import_sibling_answers(
362+
receiver_branch, sibling_branch, tool_call_id, can_share
363+
):
364+
"""Ordinary run IDs allow sharing; a receiver's tool ancestry forbids it."""
365+
events = []
366+
if tool_call_id:
367+
events.append(
368+
_answer(
369+
author='dispatcher',
370+
branch=None,
371+
content=types.Content(
372+
role='model',
373+
parts=[
374+
types.Part(
375+
function_call=types.FunctionCall(
376+
id=tool_call_id, name='lookup', args={}
377+
)
378+
)
379+
],
380+
),
381+
)
382+
)
383+
events.append(_answer(branch=sibling_branch))
384+
385+
request = await _request(events, branch=receiver_branch)
386+
387+
assert ('You land on Tuesday at 14:05.' in str(request.contents)) == can_share
388+
389+
329390
@pytest.mark.parametrize('enabled', [False, True])
330391
async def test_runner_shares_only_the_bookings_answer_with_the_guide(enabled):
331392
"""Two real Runner turns preserve public continuity and private tool data.

0 commit comments

Comments
 (0)