fix(core): strip thoughts from scrubbed history turns and resolve thought leakage#27971
fix(core): strip thoughts from scrubbed history turns and resolve thought leakage#27971amelidev wants to merge 6 commits into
Conversation
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 the Gemini model's internal reasoning thoughts were leaking into the plain-text conversation history. By introducing a preprocessing step to strip these thoughts and subsequently coalescing the remaining turns, the changes ensure that the model history remains clean and compliant with API requirements, preventing the model from confusing its own scratchpad with user-facing dialogue. 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
|
|
📊 PR Size: size/M
|
There was a problem hiding this comment.
Code Review
This pull request introduces changes to filter out internal model thoughts from the chat history during history hardening and scrubbing, preventing thought leakage. It adds a stripThoughts helper function in historyHardening.ts and updates scrubContents to exclude thought parts, accompanied by comprehensive unit tests. The reviewer noted an inconsistency in how thoughts are detected between stripThoughts (strict boolean check) and scrubContents (truthy check), suggesting a unified truthy check to ensure robust thought detection.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request removes peer dependency flags from package-lock.json and implements logic to strip internal model thoughts from the conversation history in packages/core. This includes adding a stripThoughts step in hardenHistory, filtering out thought parts in scrubContents, and adding corresponding unit tests. The review feedback suggests adding defensive checks to ensure that parts are valid objects before using the 'in' operator to prevent runtime TypeErrors, and using the nullish coalescing operator (??) instead of logical OR (||) when providing default values for optional properties.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request removes "peer": true from several dependencies in package-lock.json and implements a mechanism to strip internal model thoughts (where part.thought === true) from conversational history in packages/core. This includes adding a stripThoughts pass in hardenHistory and filtering out thought parts in scrubContents, along with corresponding unit tests. The feedback suggests extracting the inline thought check into a reusable type guard isThoughtPart with a local interface and a detailed comment, in accordance with repository guidelines for using undocumented SDK properties, and using this helper consistently across the codebase.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request removes peer dependency flags from several packages in package-lock.json and implements a mechanism to strip internal model thoughts (where part.thought === true) from chat history in packages/core. Specifically, a new stripThoughts helper has been introduced in historyHardening.ts to filter out thought parts and discard any resulting empty turns during history hardening and scrubbing. Corresponding unit tests have been added to verify this behavior and prevent thought leakage. There are no review comments to address, and I have no additional feedback to provide.
Summary
This PR resolves a Thought Leakage issue where the Gemini model's internal monologues/reasoning thoughts leak into plain-text history turns, confusing the model in subsequent turns and causing it to emulate scratchpad thoughts or enter infinite loop monologues.
Details
scrubContentsinsidepackages/core/src/utils/historyHardening.tsto actively filter out any parts wherepart.thoughtis truthy, before map-scrubbing the remaining fields. This ensures nested executions (e.g. subagents) do not leak raw thoughts as plain conversational text.scrubHistoryto runcoalesceon the scrubbed turns. This safely discards any turns made empty by thought stripping while maintaining the Gemini API's strict role-alternation invariant.stripThoughtsto check for truthyp.thoughtproperties rather than strictlyp.thought === true, matching how thoughts are represented.thought leakage in getHistoryTurns) inpackages/core/src/core/geminiChat.test.tsto reproduce and confirm the fix.Related Issues
Resolves the issue documented in
issue.md.How to Validate
Run the targeted and whole history hardening test suites using Vitest:
npx vitest run packages/core/src/core/geminiChat.test.ts -t "thought leakage in getHistoryTurns" npx vitest run packages/core/src/utils/historyHardening.test.tsAll tests pass perfectly in under 15 seconds.
Pre-Merge Checklist