fix(agent): prevent session context poisoning on interrupted turns - #29265
fix(agent): prevent session context poisoning on interrupted turns#29265rahuldevlab wants to merge 2 commits into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
📊 PR Size: size/M
|
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 interrupting an agentic stream caused the model to receive an interruption warning in its context window, leading to 'context poisoning' where the model would repeat the warning in subsequent turns. By intercepting these placeholders during history compilation and mapping them to a neutral transition phrase, the agent can now recover gracefully from interruptions without disrupting the chat loop. 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 interruption handling in getHistoryTurns within packages/core/src/core/geminiChat.ts by replacing INTERRUPTED_RESPONSE_PLACEHOLDER with a benign 'Continuing.' message in the curated history, preventing turn fusion and poisoning. A corresponding unit test has been added to verify this behavior. The review feedback highlights a potential runtime TypeError where part could be undefined when accessing part.text, and suggests using optional chaining (part?.text) to safely handle these cases.
Summary
This PR resolves a critical issue where interrupting an agentic stream (via SIGINT, timeout, or aborted tool executions) poisons the active chat session history and breaks subsequent prompt execution.
Details
closeUnansweredToolResponseTurn()appends[The previous response was interrupted before it completed.](defined asINTERRUPTED_RESPONSE_PLACEHOLDER) to the history to close the dangling turn and prevent adjacent user turns from coalescing (which would otherwise fuse user chat input into previous tool responses, violating API turn invariants). However, sending this synthetic placeholder to the model in subsequent prompts causes severe context poisoning—the model treats it as a few-shot completion pattern and parrots it back, breaking the agentic loop.packages/core/src/core/geminiChat.ts'sextractCuratedHistory(which compiles the history payload sent to the Gemini API), any model turns containingINTERRUPTED_RESPONSE_PLACEHOLDERare intercepted and mapped to a benign model turn"Continuing.".✦ [The previous response was interrupted before it completed.]."Continuing."transition. If it mimics this phrase in its next response, it is perfectly harmless and does not disrupt the loop.Related Issues
Closes #29264
How to Validate
search the codebase for all API handlers).list files). The agent now executes and completes normally instead of outputting✦ [The previous response was interrupted before it completed.].npm test -w @google/gemini-cli-core -- src/core/geminiChat.test.ts. All tests (including the new suite covering mapped interruption placeholders) pass with 100% success.Pre-Merge Checklist