runner: avoid duplicate user event append on invocation retry#4507
runner: avoid duplicate user event append on invocation retry#4507davidahmann wants to merge 3 commits intogoogle:mainfrom
Conversation
Summary of ChangesHello @davidahmann, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses an issue where retrying or resuming an invocation could lead to duplicate user events being recorded in the session history. By introducing a mechanism to detect and skip these redundant events, the change ensures the integrity of the session history and prevents unnecessary data duplication, particularly in scenarios involving invocation retries. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Implemented an invocation-scoped dedupe guard for user-message append in retry/resume paths. Scope in this PR:
This contribution was informed by patterns from Gait: https://github.com/davidahmann/gait |
There was a problem hiding this comment.
Code Review
This pull request effectively addresses the issue of duplicate user events on invocation retries by introducing a check in _append_new_message_to_session. The new method _has_duplicate_user_event_for_invocation correctly identifies duplicates based on invocation ID, message content, and state delta. The accompanying tests cover the happy paths for skipping duplicates and keeping non-duplicates.
My review includes a suggestion to refactor the duplicate check for better readability and a recommendation to enhance test coverage to include scenarios with state_delta.
| for event in session.events: | ||
| if event.invocation_id != invocation_id or event.author != 'user': | ||
| continue | ||
| if ( | ||
| event.content == new_message | ||
| and event.actions.state_delta == expected_state_delta | ||
| ): | ||
| return True | ||
| return False |
There was a problem hiding this comment.
For better readability and conciseness, you can refactor this loop into a single statement using a generator expression with any(). This is a more Pythonic way to check for the existence of an item in a sequence that matches a condition.
return any(
event.content == new_message
and event.actions.state_delta == expected_state_delta
for event in session.events
if event.author == "user" and event.invocation_id == invocation_id
)| async def test_append_new_message_to_session_skips_duplicate_retry_message(): | ||
| session_service = InMemorySessionService() | ||
| runner = Runner( | ||
| app_name="test_app", | ||
| agent=MockLlmAgent("root_agent"), | ||
| session_service=session_service, | ||
| artifact_service=InMemoryArtifactService(), | ||
| ) | ||
| session = await session_service.create_session( | ||
| app_name="test_app", | ||
| user_id="test_user", | ||
| ) | ||
| user_message = types.Content( | ||
| role="user", | ||
| parts=[types.Part(text="retry message")], | ||
| ) | ||
| invocation_context = runner._new_invocation_context( | ||
| session, | ||
| invocation_id="inv-retry", | ||
| new_message=user_message, | ||
| run_config=RunConfig(), | ||
| ) | ||
|
|
||
| await runner._append_new_message_to_session( | ||
| session=session, | ||
| new_message=user_message, | ||
| invocation_context=invocation_context, | ||
| ) | ||
| await runner._append_new_message_to_session( | ||
| session=session, | ||
| new_message=user_message, | ||
| invocation_context=invocation_context, | ||
| ) | ||
|
|
||
| matched_events = [ | ||
| event | ||
| for event in session.events | ||
| if event.author == "user" | ||
| and event.invocation_id == "inv-retry" | ||
| and event.content == user_message | ||
| ] | ||
| assert len(matched_events) == 1 |
There was a problem hiding this comment.
The new tests cover the duplicate detection logic well for messages. However, the check for duplicates also involves state_delta, which is not covered in the tests.
To improve test coverage, please consider adding a test case that verifies the behavior with different state_delta values. For example:
- Append a message with a specific
state_delta. - Append the same message with the same
state_delta(should be skipped). - Append the same message with a different
state_delta(should be appended). - Append the same message with
state_delta=None(should be appended).
This will ensure the state_delta comparison in _has_duplicate_user_event_for_invocation works as expected.
Problem
Retry/resume calls with the same
invocation_idand same user message could append duplicate user events to session history.What changed
Runner._append_new_message_to_sessionkeyed by invocation id + content + state delta.Validation
uv sync --extra testuvx pyink --config pyproject.toml src/google/adk/runners.py tests/unittests/test_runners.pyuv run pytest tests/unittests/test_runners.pyuv run pytest tests/unittests/runners/test_resume_invocation.pyRefs #4506