Skip to content

fix(core): rollback entire multi-turn request on cancellation or abort - #28801

Merged
DavidAPierce merged 2 commits into
google-gemini:mainfrom
amelidev:b_522406228
Aug 13, 2026
Merged

fix(core): rollback entire multi-turn request on cancellation or abort#28801
DavidAPierce merged 2 commits into
google-gemini:mainfrom
amelidev:b_522406228

Conversation

@amelidev

Copy link
Copy Markdown
Contributor

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.

⚠️ Note on why PR #28316 didn't cover this issue

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 core GeminiChat session 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

  • Initial State Tracking: Added mapping fields promptOriginalHistoryLengths and promptOriginalTokenCounts in GeminiChat to record the conversation history length and baseline token counts whenever a new prompt starts.
  • Rollback on Abort/Cancellation: Refactored the catch block of GeminiChat.sendMessageStream. If the prompt execution is aborted or cancelled at any point (even deep during subsequent tool continuation streams under the same prompt_id), the conversational history and token baseline are fully rolled back to the pristine state before that prompt started.
  • Added Tests: Added a new unit test in 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.ts

Manual Verification

  1. Build and start the CLI in development:
    npm run build && npm run start
  2. Run a query that uses tools, e.g.:
    Add a description at the top of @package.json
    
  3. Press Ctrl + C while it is executing/streaming.
  4. Send a simple Hello greeting.
  5. Expected Behavior: The model greets you nicely rather than attempting to continue the cancelled package.json edit.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run

@amelidev
amelidev requested a review from a team as a code owner August 13, 2026 18:15
@github-actions github-actions Bot added the size/m A medium sized PR label Aug 13, 2026
@github-actions

github-actions Bot commented Aug 13, 2026

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 119
  • Additions: +118
  • Deletions: -1
  • Files changed: 2

@amelidev

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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

  • Session State Integrity: Implemented state tracking for multi-turn prompts using new mapping fields to record history length and token counts at the start of each prompt.
  • Robust Rollback Mechanism: Enhanced the error handling in GeminiChat.sendMessageStream to fully revert conversational history and token counts to their pre-prompt state upon detection of an abort or cancellation signal.
  • Regression Testing: Added a comprehensive unit test to verify that multi-turn requests, including those involving function responses, are correctly rolled back when interrupted.
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 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 .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 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

  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.

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

Copy link
Copy Markdown
Contributor

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 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.

Comment thread packages/core/src/core/geminiChat.ts Outdated
Comment thread packages/core/src/core/geminiChat.ts
Comment thread packages/core/src/core/geminiChat.ts Outdated
Comment thread packages/core/src/core/geminiChat.ts Outdated

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

Copy link
Copy Markdown
Contributor

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 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.

Comment thread packages/core/src/core/geminiChat.ts Outdated
@amelidev

Copy link
Copy Markdown
Contributor Author

/gemini review

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

Copy link
Copy Markdown
Contributor

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 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.

Comment thread packages/core/src/core/geminiChat.ts
@DavidAPierce
DavidAPierce added this pull request to the merge queue Aug 13, 2026
Merged via the queue into google-gemini:main with commit 783f6cb Aug 13, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/m A medium sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants