fix(core): rollback entire multi-turn request on cancellation or abort - #28801
Conversation
|
📊 PR Size: size/M
|
|
/gemini review |
Summary of ChangesHello, 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 critical issue where cancelling a multi-turn prompt left the chat session in a corrupted state, causing subsequent user requests to be incorrectly interpreted as continuations of the aborted task. By introducing a tracking mechanism for prompt-specific history and token baselines, the system can now reliably perform a full rollback of the session state upon cancellation, ensuring that the agent starts from a clean slate for new interactions. Highlights
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 the 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 counterproductive. 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. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to roll back the entire multi-turn request history and token counts when a continuation stream is aborted or cancelled, along with a corresponding test case. The review feedback suggests optimizing this implementation by replacing the Map tracking structures with simple optional instance variables, as execution is sequential. Additionally, it recommends making the error message checks for 'abort' and 'cancel' case-insensitive to ensure robust error handling.
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to roll back the entire multi-turn chat history (including function responses) when a continuation stream is aborted or cancelled. It tracks the original history length and token count per prompt ID and performs a rollback if an abort/cancellation is detected. A test case has been added to verify this behavior. The feedback suggests avoiding loose substring matching on error.message (such as checking for 'abort' or 'cancel') to prevent false positives from network or socket errors, recommending instead to check for explicit error names like CanceledError and FatalCancellationError.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces state tracking to roll back the entire multi-turn request history (including function responses) in GeminiChat when a continuation stream is aborted or cancelled, and adds a corresponding unit test. The reviewer recommends resetting the tracking properties (promptOriginalHistoryLength, promptOriginalTokenCount, and lastPromptId) to undefined after a successful rollback to prevent stale state from persisting.
Summary
This PR resolves a recurring issue where aborting/cancelling a multi-turn prompt containing tool calls left the session's chat history in an incomplete, un-responded state (ending with pending tool response turns). Consequently, when the user sent a new unrelated request (such as
Hello), the model would continue the unfinished task from the previous cancelled prompt instead of answering the new request.The previous fix in PR #28316 was limited strictly to
packages/a2a-server(the experimental Agent-to-Agent server project), where it ensured that the background execution loop in the a2a executor terminated upon task cancellation. However, it did not address or modify the coreGeminiChatsession history or token tracking layer (packages/core/src/core/geminiChat.ts) which manages history and session turns for the main Gemini CLI Agent itself. As a result, the history corruption and subsequent task resumption bug persisted in the standard CLI agent.Details
promptOriginalHistoryLengthsandpromptOriginalTokenCountsinGeminiChatto record the conversation history length and baseline token counts whenever a new prompt starts.GeminiChat.sendMessageStream. If the prompt execution is aborted or cancelled at any point (even deep during subsequent tool continuation streams under the sameprompt_id), the conversational history and token baseline are fully rolled back to the pristine state before that prompt started.packages/core/src/core/geminiChat.test.ts(should roll back the entire multi-turn request including function responses when a continuation stream is aborted/cancelled) to ensure correct multi-turn cancellation and rollback behavior.Related Issues
Resolves the issue described in
issue.md.How to Validate
Automated Verification
You can run the new unit test programmatically:
npm test -w @google/gemini-cli-core -- src/core/geminiChat.test.tsManual Verification
npm run build && npm run startCtrl + Cwhile it is executing/streaming.Hellogreeting.Pre-Merge Checklist