@@ -152,14 +152,14 @@ def event_to_pause(self, long_running_function_call) -> Event:
152152 )
153153
154154 def _create_test_invocation_context (
155- self , resumability_config
155+ self , resumability_config : ResumabilityConfig | None = None
156156 ) -> InvocationContext :
157157 """Create a mock invocation context for testing."""
158158 ctx = InvocationContext (
159159 session_service = Mock (spec = BaseSessionService ),
160160 agent = Mock (spec = BaseAgent ),
161161 invocation_id = 'inv_1' ,
162- session = Mock (spec = Session ),
162+ session = Mock (spec = Session , events = [] ),
163163 resumability_config = resumability_config ,
164164 )
165165 return ctx
@@ -208,6 +208,69 @@ def test_should_not_pause_invocation_with_no_function_calls(
208208 nonpausable_event
209209 )
210210
211+ def test_should_not_pause_when_user_resumes_in_sub_branch (
212+ self , event_to_pause , long_running_function_call
213+ ):
214+ """We do not pause the invocation if a subsequent user event belongs to a sub-branch."""
215+ # Arrange
216+ mock_invocation_context = self ._create_test_invocation_context ()
217+ user_event = Event (
218+ invocation_id = 'inv_1' ,
219+ author = 'user' ,
220+ branch = f'agent@{ long_running_function_call .id } .child' ,
221+ )
222+ mock_invocation_context .session .events = [event_to_pause , user_event ]
223+
224+ # Act
225+ should_pause = mock_invocation_context .should_pause_invocation (
226+ event_to_pause
227+ )
228+
229+ # Assert
230+ assert not should_pause
231+
232+ def test_should_not_pause_when_user_resumes_in_deeply_nested_sub_branch (
233+ self , event_to_pause , long_running_function_call
234+ ):
235+ """We do not pause if the user resumes in a deeply nested sub-branch containing the tool call."""
236+ # Arrange
237+ mock_invocation_context = self ._create_test_invocation_context ()
238+ user_event = Event (
239+ invocation_id = 'inv_1' ,
240+ author = 'user' ,
241+ branch = f'parent@other.child@{ long_running_function_call .id } .grandchild' ,
242+ )
243+ mock_invocation_context .session .events = [event_to_pause , user_event ]
244+
245+ # Act
246+ should_pause = mock_invocation_context .should_pause_invocation (
247+ event_to_pause
248+ )
249+
250+ # Assert
251+ assert not should_pause
252+
253+ def test_should_pause_when_user_resumes_in_different_branch (
254+ self , event_to_pause
255+ ):
256+ """We still pause the invocation if the subsequent user event belongs to a different branch."""
257+ # Arrange
258+ mock_invocation_context = self ._create_test_invocation_context ()
259+ user_event = Event (
260+ invocation_id = 'inv_1' ,
261+ author = 'user' ,
262+ branch = 'parent@different_id.child' ,
263+ )
264+ mock_invocation_context .session .events = [event_to_pause , user_event ]
265+
266+ # Act
267+ should_pause = mock_invocation_context .should_pause_invocation (
268+ event_to_pause
269+ )
270+
271+ # Assert
272+ assert should_pause
273+
211274 def test_is_resumable_true (self ):
212275 """Tests that is_resumable is True when resumability is enabled."""
213276 invocation_context = self ._create_test_invocation_context (
@@ -534,3 +597,32 @@ def test_find_matching_function_call_no_response_in_event(
534597 invocation_context = test_invocation_context ([fc_event , fr_event ])
535598 match = invocation_context ._find_matching_function_call (fr_event_no_fr )
536599 assert match is None
600+
601+ def test_stamp_event_branch_context_preserves_isolation_scope (
602+ self , test_invocation_context
603+ ):
604+ """Tests stamp_event_branch_context does not overwrite existing isolation_scope with None."""
605+ fc = Part .from_function_call (name = 'some_tool' , args = {})
606+ fc .function_call .id = 'test_function_call_id'
607+ fc_event = Event (
608+ invocation_id = 'inv_1' ,
609+ author = 'agent' ,
610+ branch = 'root@1' ,
611+ isolation_scope = None , # Coordinator FC has None scope
612+ content = testing_utils .ModelContent ([fc ]),
613+ )
614+ fr = Part .from_function_response (
615+ name = 'some_tool' , response = {'result' : 'ok' }
616+ )
617+ fr .function_response .id = 'test_function_call_id'
618+ fr_event = Event (
619+ invocation_id = 'inv_1' ,
620+ author = 'agent' ,
621+ isolation_scope = 'task_123' , # Pre-populated active task scope
622+ content = Content (role = 'user' , parts = [fr ]),
623+ )
624+ invocation_context = test_invocation_context ([fc_event , fr_event ])
625+
626+ invocation_context .stamp_event_branch_context (fr_event )
627+ assert fr_event .branch == 'root@1'
628+ assert fr_event .isolation_scope == 'task_123'
0 commit comments