diff --git a/src/google/adk/flows/llm_flows/core/_resume.py b/src/google/adk/flows/llm_flows/core/_resume.py index 1106808430c..473ef31dd96 100644 --- a/src/google/adk/flows/llm_flows/core/_resume.py +++ b/src/google/adk/flows/llm_flows/core/_resume.py @@ -24,6 +24,7 @@ from __future__ import annotations +from collections import Counter import dataclasses import enum from typing import Any @@ -216,8 +217,9 @@ def _unexecuted_calls_event( ) -> Event | None: """`call_event` cut down to the calls that never ran, or None if all did. - A call ran when a response in `later_events` carries its id, or its name - with no id. None is also returned once the agent has written any event with + A call ran when a response in `later_events` carries its id. Responses with + no id each match one remaining same-name call, in call order. None is also + returned once the agent has written any event with content after `call_event`: tool responses and auth or confirmation requests are only written after the whole batch ran, so a call still missing its response then ran and lost it, or is pending. @@ -229,19 +231,24 @@ def _unexecuted_calls_event( return None responses = [fr for ev in later_events for fr in ev.get_function_responses()] answered_ids = {fr.id for fr in responses if fr.id is not None} - answered_names = {fr.name for fr in responses if fr.id is None} - unexecuted_ids = { - fc.id - for fc in call_event.get_function_calls() - if fc.id not in answered_ids and fc.name not in answered_names - } - if not unexecuted_ids or call_event.content is None: + answered_names = Counter(fr.name for fr in responses if fr.id is None) + if call_event.content is None: + return None + parts = [] + has_unexecuted_call = False + for part in call_event.content.parts or []: + if (call := part.function_call) is None: + parts.append(part) + elif call.id in answered_ids: + # Explicit IDs take precedence without consuming a name-only response. + continue + elif answered_names[call.name]: + answered_names[call.name] -= 1 + else: + parts.append(part) + has_unexecuted_call = True + if not has_unexecuted_call: return None - parts = [ - part - for part in call_event.content.parts or [] - if part.function_call is None or part.function_call.id in unexecuted_ids - ] return call_event.model_copy( update={'content': call_event.content.model_copy(update={'parts': parts})} ) diff --git a/tests/unittests/flows/llm_flows/core/test_resume.py b/tests/unittests/flows/llm_flows/core/test_resume.py index 6ac2c4e8104..55df3bbefc6 100644 --- a/tests/unittests/flows/llm_flows/core/test_resume.py +++ b/tests/unittests/flows/llm_flows/core/test_resume.py @@ -77,7 +77,7 @@ def _response_event( ) -def _parallel_call_event(calls: list[tuple[str, str]]) -> Event: +def _parallel_call_event(calls: list[tuple[str, str | None]]) -> Event: return Event( author='agent', invocation_id='inv-1', @@ -364,6 +364,90 @@ def test_response_without_an_id_answers_its_call_by_name(self): ) assert _replayed_ids(decision) == ['c2'] + @pytest.mark.parametrize( + ('call_ids', 'response_ids', 'replayed_ids'), + [ + (['c1', 'c2'], [None], ['c2']), + (['c1', 'c2'], [None, None], []), + (['c1', 'c2'], [None, None, None], []), + (['c1', 'c2', 'c3'], [None, 'c1'], ['c3']), + (['c1', 'c2', 'c3'], ['c1', None], ['c3']), + (['c1', 'c2'], ['c1', 'c1'], ['c2']), + (['c1', 'c2'], [None, 'other'], ['c2']), + ([None, None], [None], [None]), + ([None, 'c2', None], [None, 'c2'], [None]), + ], + ids=[ + 'one_idless_response', + 'all_idless_responses', + 'extra_idless_response', + 'idless_before_explicit_id', + 'explicit_id_before_idless', + 'duplicate_explicit_id', + 'unmatched_explicit_id', + 'idless_calls', + 'mixed_call_ids', + ], + ) + def test_same_name_responses_answer_only_matching_calls( + self, + call_ids: list[str | None], + response_ids: list[str | None], + replayed_ids: list[str | None], + ) -> None: + """Each ID-less response covers one call after explicit IDs are matched.""" + call = _parallel_call_event([('ask', call_id) for call_id in call_ids]) + # Distinct arguments identify ID-less siblings even when their IDs match. + for index, part in enumerate(call.content.parts): + part.function_call.args = {'index': index} + responses = [_response_event('ask', call_id) for call_id in response_ids] + events = [call, *responses] + original_events = [event.model_copy(deep=True) for event in events] + + decision = decide_resume(self._ctx(), events, {'ask': object()}) + + if replayed_ids: + assert decision.action is ResumeAction.REPLAY_CALLS + assert _replayed_ids(decision) == replayed_ids + assert decision.replay_event().get_function_calls()[-1].args == { + 'index': len(call_ids) - 1 + } + else: + assert decision.action is ResumeAction.CONTINUE + assert events == original_events + + def test_idless_response_replay_preserves_other_content_parts(self) -> None: + """Filtering calls retains non-call parts in their original order.""" + call = _parallel_call_event([('ask', 'c1'), ('ask', 'c2')]) + call.content.parts.insert(0, types.Part(text='Before calls')) + call.content.parts.insert(2, types.Part(text='Between calls')) + call.content.parts.append(types.Part(text='After calls')) + original_call = call.model_copy(deep=True) + + decision = decide_resume( + self._ctx(), [call, _response_event('ask', None)], {'ask': object()} + ) + + assert decision.action is ResumeAction.REPLAY_CALLS + assert decision.replay_event().content.parts == [ + call.content.parts[0], + call.content.parts[2], + call.content.parts[3], + call.content.parts[4], + ] + assert call == original_call + + def test_idless_response_after_agent_batch_completion_does_not_replay( + self, + ) -> None: + """An agent-authored batch result prevents replay of a pending sibling.""" + call = _parallel_call_event([('ask', 'c1'), ('ask', 'c2')]) + events = [call, _response_event('ask', None, author='agent')] + + decision = decide_resume(self._ctx(), events, {'ask': object()}) + + assert decision.action is ResumeAction.CONTINUE + def test_sibling_missing_a_response_after_an_auth_resume_is_not_replayed( self, ): diff --git a/tests/unittests/flows/llm_flows/test_base_llm_flow.py b/tests/unittests/flows/llm_flows/test_base_llm_flow.py index 9f68599f8c8..a2bebc66f62 100644 --- a/tests/unittests/flows/llm_flows/test_base_llm_flow.py +++ b/tests/unittests/flows/llm_flows/test_base_llm_flow.py @@ -53,6 +53,7 @@ from google.adk.tools.base_toolset import BaseToolset from google.adk.tools.enterprise_search_tool import EnterpriseWebSearchTool from google.adk.tools.google_search_tool import GoogleSearchTool +from google.adk.tools.tool_context import ToolContext from google.adk.utils.context_utils import Aclosing from google.adk.utils.variant_utils import GoogleLLMVariant from google.genai import types @@ -2989,6 +2990,81 @@ async def test_resume_short_circuit_skips_partial_function_call(): assert not any(e.actions and e.actions.transfer_to_agent for e in events) +@pytest.mark.parametrize( + ('response_ids', 'expected_executions'), + [([None], [1, 2]), ([None, 'c1'], [2]), (['c1', None], [2])], + ids=['idless', 'idless_before_explicit_id', 'explicit_id_before_idless'], +) +async def test_resume_executes_only_unanswered_same_name_calls( + response_ids: list[str | None], expected_executions: list[int] +) -> None: + """Restored ID-less responses do not hide unexecuted same-name siblings.""" + executions = [] + + def ask(index: int, tool_context: ToolContext) -> dict[str, int]: + executions.append(index) + tool_context.actions.skip_summarization = True + return {'index': index} + + agent = Agent( + name='root_agent', + model=testing_utils.MockModel.create(responses=['No replay occurred']), + tools=[ask], + ) + invocation_context = await testing_utils.create_invocation_context( + agent=agent, user_content='Resume the saved calls' + ) + invocation_context.resumability_config = ResumabilityConfig(is_resumable=True) + call_event = Event( + invocation_id=invocation_context.invocation_id, + author=agent.name, + content=types.Content( + role='model', + parts=[ + types.Part( + function_call=types.FunctionCall( + id=f'c{index + 1}', name='ask', args={'index': index} + ) + ) + for index in range(3) + ], + ), + ) + response_events = [ + Event( + invocation_id=invocation_context.invocation_id, + author='user', + content=types.Content( + role='user', + parts=[ + types.Part( + function_response=types.FunctionResponse( + id=response_id, + name='ask', + response={'result': 'saved'}, + ) + ) + ], + ), + ) + for response_id in response_ids + ] + invocation_context.session.events.extend([call_event, *response_events]) + original_call = call_event.model_copy(deep=True) + + events = [ + event async for event in agent._llm_flow.run_async(invocation_context) + ] + + assert sorted(executions) == expected_executions + assert [ + response.id + for event in events + for response in event.get_function_responses() + ] == [f'c{index + 1}' for index in expected_executions] + assert call_event == original_call + + @pytest.mark.asyncio async def test_preprocess_final_response_skips_llm_call(): """A final response from preprocessing must finish the current step."""