Skip to content

Commit 798207a

Browse files
google-genai-botcopybara-github
authored andcommitted
fix: Avoid overwriting existing isolation scope in stamp_event_branch_context
Modify stamp_event_branch_context to only apply the isolation scope of the matching function call if the event does not already possess an active scope. This prevents destroying active state context during multi-agent handoffs. PiperOrigin-RevId: 939981267
1 parent 37ca6fb commit 798207a

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/google/adk/agents/invocation_context.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,10 @@ def stamp_event_branch_context(self, event: Event) -> None:
517517
"""Stamps the event with the branch and isolation scope of its matching function call."""
518518
if function_call := self._find_matching_function_call(event):
519519
event.branch = function_call.branch
520-
if function_call.isolation_scope is not None:
520+
if (
521+
event.isolation_scope is None
522+
and function_call.isolation_scope is not None
523+
):
521524
event.isolation_scope = function_call.isolation_scope
522525

523526

tests/unittests/agents/test_invocation_context.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,3 +626,32 @@ def test_stamp_event_branch_context_preserves_isolation_scope(
626626
invocation_context.stamp_event_branch_context(fr_event)
627627
assert fr_event.branch == 'root@1'
628628
assert fr_event.isolation_scope == 'task_123'
629+
630+
def test_stamp_event_branch_context_does_not_overwrite_existing_scope(
631+
self, test_invocation_context
632+
):
633+
"""Tests stamp_event_branch_context does not overwrite existing isolation_scope if set."""
634+
fc = Part.from_function_call(name='some_tool', args={})
635+
fc.function_call.id = 'test_function_call_id'
636+
fc_event = Event(
637+
invocation_id='inv_1',
638+
author='agent',
639+
branch='root@1',
640+
isolation_scope='task_456', # Function call has isolation scope
641+
content=testing_utils.ModelContent([fc]),
642+
)
643+
fr = Part.from_function_response(
644+
name='some_tool', response={'result': 'ok'}
645+
)
646+
fr.function_response.id = 'test_function_call_id'
647+
fr_event = Event(
648+
invocation_id='inv_1',
649+
author='agent',
650+
isolation_scope='task_123', # Pre-populated active task scope
651+
content=Content(role='user', parts=[fr]),
652+
)
653+
invocation_context = test_invocation_context([fc_event, fr_event])
654+
655+
invocation_context.stamp_event_branch_context(fr_event)
656+
assert fr_event.branch == 'root@1'
657+
assert fr_event.isolation_scope == 'task_123'

0 commit comments

Comments
 (0)