Skip to content

Commit b8a2b6c

Browse files
kevinkassimocopybara-github
authored andcommitted
fix: include delimiter when matching events from parent nodes in content processor
Previously we only do a simple prefix string matching, thus `agent_00` will match with `agent_0` With this new change, we either check directly equality, or must expect seeing `agent_0.`. See added test for branches we now match / skip. TBF `.` is also not a perfect delimiter (I would imagine users might put dot in agent names). We might consider a follow up that bans such agent names. Tested with script in the linked issue (I updated prompt so we see which agent they see from): Before: ``` [agent_8]: 73 [agent_0]: 97 [agent_1]: 73 [agent_5]: 97 [agent_4]: 73 [agent_2]: 73 [agent_3]: 73 [agent_9]: 93 [agent_6]: 73 [agent_7]: 1 [agent_70]: 1 (agent_7) [agent_20]: 73 (agent_2) [agent_30]: 73 (agent_3) [agent_00]: 97 (agent_0) [agent_40]: 73 (agent_4) [agent_80]: 73 (agent_8) [agent_50]: 97 (agent_5) [agent_90]: 93 (agent_9) [agent_10]: 73 (agent_1) [agent_60]: 73 (agent_6) ``` After: ``` [agent_9]: 73 [agent_6]: 73 [agent_2]: 73 [agent_7]: 93 [agent_4]: 73 [agent_1]: 73 [agent_3]: 73 [agent_5]: 97 [agent_0]: 73 [agent_8]: 87 [agent_50]: 0 [agent_80]: 0 [agent_10]: 0 [agent_90]: 0 [agent_30]: 0 [agent_20]: 0 [agent_60]: 0 [agent_00]: 0 [agent_40]: 0 [agent_70]: 0 ``` Closes #2948 Co-authored-by: Kevin Qian <kqian@google.com> PiperOrigin-RevId: 826187198
1 parent 2274c4f commit b8a2b6c

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/google/adk/flows/llm_flows/contents.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,12 @@ def _is_event_belongs_to_branch(
594594
"""
595595
if not invocation_branch or not event.branch:
596596
return True
597-
return invocation_branch.startswith(event.branch)
597+
# We use dot to delimit branch nodes. To avoid simple prefix match
598+
# (e.g. agent_0 unexpectedly matching agent_00), require either perfect branch
599+
# match, or match prefix with an additional explicit '.'
600+
return invocation_branch == event.branch or invocation_branch.startswith(
601+
f'{event.branch}.'
602+
)
598603

599604

600605
def _is_function_call_event(event: Event, function_name: str) -> bool:

tests/unittests/flows/llm_flows/test_contents_branch.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ async def test_branch_filtering_child_sees_parent():
5858
content=types.ModelContent("Child agent response"),
5959
branch="parent_agent.child_agent", # Current branch - should be included
6060
),
61+
Event(
62+
invocation_id="inv4",
63+
author="child_agent",
64+
content=types.ModelContent("Excluded response 1"),
65+
branch="parent_agent.child_agent000", # Prefix match BUT not itself/ancestor - should be excluded
66+
),
67+
Event(
68+
invocation_id="inv5",
69+
author="child_agent",
70+
content=types.ModelContent("Excluded response 2"),
71+
branch="parent_agent.child", # Prefix match BUT not itself/ancestor - should be excluded
72+
),
6173
]
6274
invocation_context.session.events = events
6375

0 commit comments

Comments
 (0)