Skip to content

Comments

fix: skip rewound invocations when resuming agent in _find_agent_to_run#4602

Open
rahulmansharamani14 wants to merge 4 commits intogoogle:mainfrom
rahulmansharamani14:fix/find-agent-to-run-rewind-filtering
Open

fix: skip rewound invocations when resuming agent in _find_agent_to_run#4602
rahulmansharamani14 wants to merge 4 commits intogoogle:mainfrom
rahulmansharamani14:fix/find-agent-to-run-rewind-filtering

Conversation

@rahulmansharamani14
Copy link

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

New tests in tests/unittests/test_runners.py:

  • test_find_agent_to_run_ignores_rewound_sub_agent_event - verifies that a sub-agent event from a rewound invocation is excluded and root_agent is returned.
  • test_find_agent_to_run_ignores_rewound_function_call - verifies that a rewound function-call/response pair is excluded and does not influence agent selection.

Please include a summary of passed pytest results.

python -m pytest tests/unittests/test_runners.py -v
49 passed in 2.03s

Manual End-to-End (E2E) Tests:

The fix is fully exercised by unit tests. No additional manual E2E setup is required for this targeted bug fix.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

Additional unit tests for _filter_rewound_events and further _find_agent_to_run rewind scenarios are the next planned steps.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @rahulmansharamani14, 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 a bug where the agent selection logic in _find_agent_to_run did not correctly account for rewound invocations, potentially leading to an incorrect agent being resumed. By introducing a dedicated event filtering mechanism, the system now accurately processes session history, ensuring that only valid, non-rewound events inform the decision of which agent should run next. This improves the robustness and correctness of agent state management, especially in scenarios involving session rewinds.

Highlights

  • New Event Filtering Function: Introduced a new utility function, _filter_rewound_events, to accurately remove events that have been annulled by a rewind action from a list of session events.
  • Refactored Event Processing: Refactored _get_contents to utilize the new _filter_rewound_events function, centralizing the logic for handling rewound events.
  • Improved Agent Selection Logic: Modified _find_agent_to_run to apply the _filter_rewound_events function to session events before determining the next agent, ensuring that rewound invocations do not incorrectly influence agent selection.
  • New Unit Tests: Added two new unit tests to test_runners.py to specifically validate that _find_agent_to_run correctly ignores rewound sub-agent events and function calls.

🧠 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
  • src/google/adk/flows/llm_flows/contents.py
    • Extracted the logic for filtering rewound events into a new private helper function _filter_rewound_events.
    • Updated _get_contents to call the new _filter_rewound_events function instead of duplicating the filtering logic.
  • src/google/adk/runners.py
    • Modified _find_agent_to_run to filter session events using contents._filter_rewound_events before calling find_matching_function_call.
    • Updated the iteration over session events in _find_agent_to_run to use the filtered_events list.
  • tests/unittests/test_runners.py
    • Imported EventActions and _filter_rewound_events.
    • Added test_find_agent_to_run_ignores_rewound_sub_agent_event to verify that rewound sub-agent events are ignored.
    • Added test_find_agent_to_run_ignores_rewound_function_call to verify that rewound function calls are ignored.
Activity
  • Linked to issue wrong agent to start a new invocation when last invocation was rewound #4169.
  • Added or updated unit tests, which passed locally.
  • Performed a self-review of the code.
  • Commented code in hard-to-understand areas.
  • Confirmed new and existing unit tests pass locally.
  • Confirmed any dependent changes have been merged and published.
  • Planned additional unit tests for _filter_rewound_events and further _find_agent_to_run rewind scenarios.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@adk-bot adk-bot added the core [Component] This issue is related to the core interface and implementation label Feb 24, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request correctly addresses the issue where rewound invocations were not being skipped during agent selection in _find_agent_to_run. The extraction of the filtering logic into a separate function is a good improvement. I have suggested a few refinements regarding the visibility and performance of the new filtering function to ensure it follows best practices and remains efficient as the conversation history grows.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a fix to correctly handle rewound invocations when determining the next agent to run. The core change involves filtering out events that have been annulled by a rewind operation before the agent selection logic is executed. This is achieved by introducing a new helper function, filter_rewound_events, which is then used in _find_agent_to_run. The implementation includes refactoring existing rewind logic into this new function and adding comprehensive unit tests to validate the fix. The changes appear correct and well-tested, effectively addressing the described issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core [Component] This issue is related to the core interface and implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wrong agent to start a new invocation when last invocation was rewound

2 participants