Skip to content

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

Closed

Description

@chelxxx

** Please make sure you read the contribution guide and file the issues in the right place. **
Contribution guide.

Describe the bug
When we use rewind_async to cancel an invocation, roll back the state changes, and start a new invocation, rewound events are filtered out for LLM calls, but not when determining which agent to hand off to first.

To Reproduce
Please share a minimal code and data to reproduce your problem.
Steps to reproduce the behavior:

  1. Install '...'
  2. Run '....'
  3. Open '....'
  4. Provide error or stacktrace

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. macOS, Linux, Windows]
  • Python version(python -V):
  • ADK version(pip show google-adk):

Model Information:

  • Are you using LiteLLM: Yes/No
  • Which model is being used(e.g. gemini-2.5-pro)

Additional context
Add any other context about the problem here.

Activity

  1. added
    core[Component] This issue is related to the core interface and implementation
    on Jan 15, 2026
  2. added theissue type on Jan 15, 2026
  3. wuliang229 commented on Jan 15, 2026

    @wuliang229
    Collaborator

    @chelxxx could you be more specific about "not when determining which agent to hand off to first"? Maybe provide an example sequence of events or code snippet to help reproduce.

  4. added
    request clarification[Status] The maintainer need clarification or more information from the author
    on Jan 15, 2026
  5. adk-bot commented on Jan 23, 2026

    @adk-bot
    Collaborator

    This issue has been automatically marked as stale because it has not had recent activity for 7 days after a maintainer requested clarification. It will be closed if no further activity occurs within 7 days.

  6. added
    stale[Status] Issues which have been marked inactive since there is no user response
    on Jan 23, 2026
  7. chelxxx commented on Jan 23, 2026

    @chelxxx
    Author

    @chelxxx could you be more specific about "not when determining which agent to hand off to first"? Maybe provide an example sequence of events or code snippet to help reproduce.

    I am too lazy to open a PR (mostly don't want to write tests), but this is the fix and tested with my data (which unfortunately I can't share). Hope this help you understand it better. So add this to

    def _find_agent_to_run(

    Image

  8. removed
    stale[Status] Issues which have been marked inactive since there is no user response
    on Jan 24, 2026
  9. surajksharma07 commented on Jan 29, 2026

    @surajksharma07
    Collaborator

    @chelxxx the issue is that _find_agent_to_run in runners.py:1078 iterates through all session.events without filtering out rewound events, unlike _get_contents in contents.py:380-397 which correctly skips them.

    The fix is to apply the same rewind filtering logic (iterate backward, skip events between the rewind marker and rewind_before_invocation_id) to the event list at the start of _find_agent_to_run before both the find_matching_function_call call and the agent selection loop.

    This is still present in v1.23.0 — would you be open to submitting your fix as a PR?

    It looks correct based on our testing.

  10. chelxxx commented on Jan 29, 2026

    @chelxxx
    Author

    @chelxxx the issue is that _find_agent_to_run in runners.py:1078 iterates through all session.events without filtering out rewound events, unlike _get_contents in contents.py:380-397 which correctly skips them.

    The fix is to apply the same rewind filtering logic (iterate backward, skip events between the rewind marker and rewind_before_invocation_id) to the event list at the start of _find_agent_to_run before both the find_matching_function_call call and the agent selection loop.

    This is still present in v1.23.0 — would you be open to submitting your fix as a PR?

    It looks correct based on our testing.

    yeah, I will open a PR later.

  11. surajksharma07 commented on Feb 22, 2026

    @surajksharma07
    Collaborator

    This is still present in v1.25.1 (latest).

  12. added
    needs review[Status] The PR/issue is awaiting review from the maintainer
    and removed
    request clarification[Status] The maintainer need clarification or more information from the author
    on Feb 22, 2026
  13. rahulmansharamani14 commented on Feb 22, 2026

    @rahulmansharamani14
    Contributor

    Hi @surajksharma07 @chelxxx, the rewind filtering logic makes sense! If no one is working on this, I'd be happy to submit the fix and write associated tests. Let me know!

  14. surajksharma07 commented on Feb 22, 2026

    @surajksharma07
    Collaborator

    @rahulmansharamani14 please go ahead — no one else is working on this.

    The two spots to patch in _find_agent_to_run() (runners.py:1058 and :1070) both need the same backward-iteration filtering already in contents.py:409-449; extract that into a small helper and pass the filtered list to find_matching_function_call and the event loop.

    For tests, a scenario that calls rewind_async() on a multi-agent session and then verifies _find_agent_to_run returns the root agent (not the sub-agent) would cover the regression cleanly.

  15. surajksharma07 commented on Feb 22, 2026

    @surajksharma07
    Collaborator

    @seanzhou1023 Please have a look into it once.

  16. rahulmansharamani14 commented on Feb 24, 2026

    @rahulmansharamani14
    Contributor

    @surajksharma07 @seanzhou1023
    Hey, just wanted to let you know that I've opened PR #4602 for this issue. It extracts the existing rewind-filtering logic into a shared filter_rewound_events helper and applies it in find_agent_to_run so rewound invocations are correctly ignored during agent resume. Includes unit tests covering the fix.

    Would love your feedback. Thanks!

  17. surajksharma07 commented on Feb 24, 2026

    @surajksharma07
    Collaborator

    @rahulmansharamani14 Thanks for getting this done so quickly — the filter_rewound_events helper approach is exactly right.

    we will get a review queued on #4602; @seanzhou1023 worth taking a look too when you get a chance.

  18. rahulmansharamani14 commented on Feb 24, 2026

    @rahulmansharamani14
    Contributor

    Thanks @surajksharma07 for the quick feedback, and @chelxxx for the initial groundwork on this - really helped shape the approach!

  19. added a commit that references this issue on Aug 31, 2026
    ed1306f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

core[Component] This issue is related to the core interface and implementationneeds review[Status] The PR/issue is awaiting review from the maintainer

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions