diff --git a/src/google/adk/flows/llm_flows/contents.py b/src/google/adk/flows/llm_flows/contents.py index 899f819a..da4cee94 100644 --- a/src/google/adk/flows/llm_flows/contents.py +++ b/src/google/adk/flows/llm_flows/contents.py @@ -594,7 +594,12 @@ def _is_event_belongs_to_branch( """ if not invocation_branch or not event.branch: return True - return invocation_branch.startswith(event.branch) + # We use dot to delimit branch nodes. To avoid simple prefix match + # (e.g. agent_0 unexpectedly matching agent_00), require either perfect branch + # match, or match prefix with an additional explicit '.' + return invocation_branch == event.branch or invocation_branch.startswith( + f'{event.branch}.' + ) def _is_function_call_event(event: Event, function_name: str) -> bool: diff --git a/tests/unittests/flows/llm_flows/test_contents_branch.py b/tests/unittests/flows/llm_flows/test_contents_branch.py index 736909fb..23473541 100644 --- a/tests/unittests/flows/llm_flows/test_contents_branch.py +++ b/tests/unittests/flows/llm_flows/test_contents_branch.py @@ -58,6 +58,18 @@ async def test_branch_filtering_child_sees_parent(): content=types.ModelContent("Child agent response"), branch="parent_agent.child_agent", # Current branch - should be included ), + Event( + invocation_id="inv4", + author="child_agent", + content=types.ModelContent("Excluded response 1"), + branch="parent_agent.child_agent000", # Prefix match BUT not itself/ancestor - should be excluded + ), + Event( + invocation_id="inv5", + author="child_agent", + content=types.ModelContent("Excluded response 2"), + branch="parent_agent.child", # Prefix match BUT not itself/ancestor - should be excluded + ), ] invocation_context.session.events = events