Repository navigation
fix(flows): keep rewound turns out of Interactions API chaining - #7821
Open
adityasingh2400 wants to merge 1 commit into
Open
adityasingh2400 wants to merge 1 commit into
adityasingh2400 wants to merge 1 commit into
Conversation
_find_previous_interaction_state scanned raw session events, so after Runner.rewind_async the next request still set previous_interaction_id to a rewound turn's interaction. On the Interactions API that id is the model's context, so the rewound turn came back into it, against the rewind guarantee. Scan the events that survive _apply_rewinds, as prompt building and compaction already do. The managed agent uses the same lookup and gets the fix too. Fixes google#7820
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):
Problem:
On the Interactions API, the model's context is the server-side history behind
previous_interaction_id._find_previous_interaction_statechose that id by scanning rawsession.events. AfterRunner.rewind_async, the next request therefore continued from a rewound turn's interaction, and the turn came back into the model's context.rewind.mdpromises that ADK "leaves the rewound turns out of every prompt that it builds afterwards".Solution:
Scan
_apply_rewinds(events)instead ofevents._rewind_eventsdescribes_apply_rewindsas the single source of truth for which events are live, and prompt contents, compaction, memory ingestion and the agent router already use it. After a rewind, the request now continues from the last live interaction, or sends noprevious_interaction_idwhen every interaction was rewound._managed_agent.pyuses the same helper, so it gets the fix as well. The branch filtering and the returned(interaction_id, environment_id)shape are unchanged.Testing Plan
Unit Tests:
Added to
tests/unittests/flows/llm_flows/context/test_interactions.py:test_find_previous_interaction_state_skips_rewound_turns: rewinding turn 2 makes the lookup return turn 1's(interaction_id, environment_id).test_find_previous_interaction_state_none_when_every_turn_rewound: returns(None, None).test_find_previous_interaction_state_chains_turns_after_a_rewind: a turn taken after the rewind is still found, as a guard.pre-commit runpasses on the changed files, andmypyreports no issues in_interactions.py.Manual End-to-End (E2E) Tests:
I ran a real
RunnerwithLlmAgent(model=Gemini("gemini-2.5-flash", use_interactions_api=True))and anInMemorySessionService. Onlyinteractions_utils.generate_content_via_interactionswas stubbed: it recordsllm_request.previous_interaction_idand returnsinteraction_id=f"int_{n}". I ran turn 1 ("My name is Ada."), turn 2 ("Actually, call me Bob."), thenrewind_async(rewind_before_invocation_id=<turn 2>), then turn 3 ("What is my name?"). The script is in #7820.Checklist
Additional context
None.