Skip to content

fix(core): avoid duplicate tool response turns when resuming sessions - #29618

Merged
DavidAPierce merged 4 commits into
google-gemini:mainfrom
diegogodinezr:GH-29365
Oct 6, 2026
Merged

DavidAPierce merged 4 commits into
google-gemini:mainfrom
diegogodinezr:GH-29365

Conversation

@diegogodinezr

Copy link
Copy Markdown
Contributor

Summary

Prevent duplicate tool response turn deserialization in convertSessionToClientHistory when resuming recorded sessions.

Details

When reconstructing client history during session resume, convertSessionToClientHistory previously replayed recorded user functionResponse turns while also synthesizing ${msg.id}_response turns from toolCalls[].result on the preceding model message. This resulted in redundant history synthesis and mismatched pairing alignment between functionCall and functionResponse entries.

This change:

  • Checks whether subsequent user messages before the next model turn already supply functionResponse entries for the model turn's toolCalls, skipping redundant synthesis while retaining fallback synthesis from toolCalls[].result for legacy session recordings.
  • Filters out duplicate functionResponse entries with the same id within a single tool exchange round so existing recordings with duplicated entries deserialize cleanly.
  • Adds unit test coverage in packages/core/src/utils/sessionUtils.test.ts for modern session recordings, legacy session recordings, parallel tool calls, and duplicate entry filtering.

Related Issues

Fixes #29365

How to Validate

Run the unit test suite for sessionUtils:

npm test -w @google/gemini-cli-core -- src/utils/sessionUtils.test.ts

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
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

When reconstructing client history during session resume, convertSessionToClientHistory
replays recorded user response turns while also re-synthesizing response turns from
toolCalls[].result on preceding model turns.

This change:
- Checks whether recorded user response turns are already present before synthesizing
  from toolCalls[].result, retaining fallback synthesis for legacy recordings.
- Filters out duplicate functionResponse entries matching the same callId within a single turn.
- Adds test coverage for single tool calls, parallel tool calls, legacy formats, and deduplication.
@diegogodinezr
diegogodinezr requested a review from a team as a code owner October 2, 2026 21:33
@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 an issue where resuming recorded sessions caused duplicate tool response turns. By intelligently checking for existing user-provided responses before synthesizing them from model tool calls and implementing deduplication logic, the history reconstruction process is now more robust and prevents redundant entries.

Highlights

  • Duplicate Tool Response Prevention: Updated convertSessionToClientHistory to detect and skip redundant synthesis of tool responses when a recorded user message already contains the corresponding functionResponse.
  • Response Deduplication: Implemented a seenFunctionResponseIds tracker to filter out duplicate functionResponse entries within a single tool exchange round, ensuring cleaner deserialization.
  • Enhanced Test Coverage: Added comprehensive unit tests in packages/core/src/utils/sessionUtils.test.ts covering modern and legacy session recordings, parallel tool calls, and corrupted history 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 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. ↩

@github-actions github-actions Bot added the size/l A large sized PR label Oct 2, 2026
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

📊 PR Size: size/L

  • Lines changed: 547
  • Additions: +527
  • Deletions: -20
  • Files changed: 2

@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 updates the convertSessionToClientHistory function in sessionUtils.ts to prevent duplicate tool response turns and deduplicate functionResponse parts in the conversation history. It tracks processed function response IDs using a set and looks ahead at subsequent user messages to avoid synthesizing duplicate tool responses. Unit tests have been updated and added to verify these deduplication behaviors. I have no feedback to provide.

@diegogodinezr

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 enhances convertSessionToClientHistory in packages/core/src/utils/sessionUtils.ts to deduplicate function response IDs and avoid synthesizing duplicate tool response turns when they are already recorded in subsequent user messages. It also adds unit tests to verify these deduplication scenarios. The review feedback suggests utilizing a pendingPartsToAppend map to merge synthesized tool responses into the correct subsequent user turns, preventing consecutive user turns and keeping parallel tool responses grouped together.

Comment thread packages/core/src/utils/sessionUtils.ts
Comment thread packages/core/src/utils/sessionUtils.ts
@gemini-cli gemini-cli Bot added priority/p1 Important and should be addressed in the near term. area/core Issues related to User Interface, OS Support, Core Functionality labels Oct 2, 2026
@diegogodinezr

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 refactors convertSessionToClientHistory in sessionUtils.ts to deduplicate tool response turns and prevent duplicate tool responses when a recorded user functionResponse turn follows. It also adds comprehensive test cases to verify these behaviors. However, a potential runtime error was identified where msg.content can be undefined or null, which would cause a crash when processing user messages. A fix is recommended to safely handle optional properties.

Comment thread packages/core/src/utils/sessionUtils.ts Outdated
@diegogodinezr

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 updates the convertSessionToClientHistory function in packages/core/src/utils/sessionUtils.ts to improve the handling of tool responses and prevent duplication. The changes introduce logic to track and deduplicate function responses, ensuring that tool calls are correctly merged into subsequent user turns instead of creating redundant response turns. Additionally, the test suite in packages/core/src/utils/sessionUtils.test.ts has been expanded with several new test cases to verify these deduplication and merging behaviors, as well as to ensure robust handling of user messages with undefined content. I have no feedback to provide as there were no review comments.

@DavidAPierce

Copy link
Copy Markdown
Contributor

Findings

🔴 Critical / High Priority

None identified. The fix directly solves the duplicate response problem, gracefully repairs already-corrupted historical session files, and preserves full backwards compatibility for legacy recordings.


🟡 Improvements

  1. JSDoc Documentation for convertSessionToClientHistory Behavior
    • File: packages/core/src/utils/sessionUtils.ts (lines 107–112)
    • Observation: The behavior of convertSessionToClientHistory now incorporates sophisticated lookahead and merge logic to reconcile modern vs. legacy tool response recordings. Adding a brief JSDoc note explaining the forward-scan and merge strategy will help future contributors understand how tool call results are reconciled with recorded user turns.

🟢 Nitpicks

  1. Map Cleanup Lifecycle
    • File: packages/core/src/utils/sessionUtils.ts (lines 115, 122)
    • Observation: Once extraParts are retrieved from pendingPartsToAppend.get(msg.id), calling pendingPartsToAppend.delete(msg.id) allows the buffered parts to be garbage collected immediately during large session reconstruction.

@DavidAPierce
DavidAPierce enabled auto-merge October 6, 2026 20:19
@DavidAPierce
DavidAPierce added this pull request to the merge queue Oct 6, 2026
Merged via the queue into google-gemini:main with commit 486ae2c Oct 6, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality priority/p1 Important and should be addressed in the near term. size/l A large sized PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resuming a session (-r) sends every tool result twice: toolCalls[].result and the recorded functionResponse turn are both replayed

2 participants