feat(loop-reduction): implement iterative loop detection and model feedback#20763
feat(loop-reduction): implement iterative loop detection and model feedback#20763aishaneeshah merged 12 commits intomainfrom
Conversation
|
Hi there! Thank you for your contribution to Gemini CLI. To improve our contribution process and better track changes, we now require all pull requests to be associated with an existing issue, as announced in our recent discussion and as detailed in our CONTRIBUTING.md. This pull request is being closed because it is not currently linked to an issue. Once you have updated the description of this PR to link an issue (e.g., by adding How to link an issue: Thank you for your understanding and for being a part of our community! |
|
Hi @aishaneeshah, thank you so much for your contribution to Gemini CLI! We really appreciate the time and effort you've put into this. We're making some updates to our contribution process to improve how we track and review changes. Please take a moment to review our recent discussion post: Improving Our Contribution Process & Introducing New Guidelines. Key Update: Starting January 26, 2026, the Gemini CLI project will require all pull requests to be associated with an existing issue. Any pull requests not linked to an issue by that date will be automatically closed. Thank you for your understanding and for being a part of our community! |
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 significantly enhances the loop detection and recovery system within the Gemini CLI. Instead of abruptly ending a session when a potential loop is identified, the system now intelligently intervenes by providing the model with specific feedback and an opportunity to self-correct. This iterative approach aims to improve the model's ability to navigate complex tasks and reduce premature terminations, leading to a more robust and user-friendly experience. Highlights
Changelog
Activity
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 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 counter productive. 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. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces an iterative loop detection and recovery mechanism, which is a great improvement for handling repetitive model behavior. Instead of immediately terminating, the system now gives the model a chance to recover by providing feedback. The changes to LoopDetectionService to return a structured result and the enhanced telemetry are well-implemented. My main feedback is on a significant code duplication in GeminiClient where the loop recovery logic is handled. Refactoring this into a helper method would improve maintainability.
|
Thank you for linking an issue! This pull request has been automatically reopened. |
|
Size Change: +4.82 kB (+0.02%) Total Size: 26 MB
ℹ️ View Unchanged
|
0d08877 to
dd9ccc9
Compare
ffd4050 to
38ff91e
Compare
163ee4d to
e7cd169
Compare
|
/gemini review |
87341ff to
c37518d
Compare
There was a problem hiding this comment.
Code Review
This pull request introduces a significant improvement to the loop detection mechanism by implementing an iterative recovery system. Instead of terminating immediately, the model is now given a chance to self-correct after the first detected loop, with termination only occurring on a second, persistent loop.
The implementation is well-structured, with the core logic cleanly separated into GeminiClient for the recovery flow and LoopDetectionService for the stateful detection. The changes to the telemetry events in types.ts are also well-aligned, providing more granular data on loop detection events.
However, I've identified a critical issue with the test suite where several important tests for the content loop detection heuristics have been removed or made incomplete during refactoring. This creates a gap in test coverage for logic that prevents false positives on valid markdown structures.
Note: Security Review did not run due to the size of the PR.
1c16169 to
fff9dd2
Compare
fff9dd2 to
7932b84
Compare
fc3f650 to
6ed005c
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a 'Two-Strike' loop recovery mechanism to the Gemini client. Previously, a detected loop would immediately terminate the session. Now, upon the first loop detection (Strike 1), the system attempts recovery by injecting a system message with feedback into the model's context and initiating a new turn. If a second loop is detected (Strike 2) during this recovery or any subsequent turn, the session is terminated. The LoopDetectionService was updated to return a LoopDetectionResult object containing a strike count and details, replacing the previous boolean return. The LLM-based loop detection mechanism's initial check turn was reduced from 40 to 30, and the minimum check interval from 7 to 5 turns. Review comments highlighted a potential second-order prompt injection vulnerability in the _recoverFromLoop method, where loopResult.detail (derived from model output) was directly interpolated into system feedback. A suggestion was made to sanitize this detail by replacing newlines. Additionally, it was recommended to default flashAnalysis to undefined instead of an empty string to ensure a more informative fallback message in the recovery prompt.
| // Clear the detection flag so the recursive turn can proceed, but the count remains 1. | ||
| this.loopDetector.clearDetection(); | ||
|
|
||
| const feedbackText = `System: Potential loop detected. Details: ${loopResult.detail || 'Repetitive patterns identified'}. Please take a step back and confirm you're making forward progress. If not, take a step back, analyze your previous actions and rethink how you're approaching the problem. Avoid repeating the same tool calls or responses without new results.`; |
There was a problem hiding this comment.
The feedbackText constructed for loop recovery interpolates loopResult.detail directly into a system-level instruction. Since loopResult.detail can contain unvalidated content from the model's own previous output or tool call arguments (which may be influenced by user input), this creates a risk of second-order prompt injection. An attacker could craft a payload that, when detected as part of a loop, injects instructions that the model executes during the recovery turn.
Recommendation: Sanitize loopResult.detail before interpolating it into the system feedback message. Ensure that any content derived from the conversation history is properly escaped or wrapped to prevent it from being interpreted as instructions by the model.
| const feedbackText = `System: Potential loop detected. Details: ${loopResult.detail || 'Repetitive patterns identified'}. Please take a step back and confirm you're making forward progress. If not, take a step back, analyze your previous actions and rethink how you're approaching the problem. Avoid repeating the same tool calls or responses without new results.`; | |
| const feedbackText = `System: Potential loop detected. Details: ${(loopResult.detail || 'Repetitive patterns identified').replace(/\n/g, ' ')}. Please take a step back and confirm you're making forward progress. If not, take a step back, analyze your previous actions and rethink how you're approaching the problem. Avoid repeating the same tool calls or responses without new results.`; |
References
- Avoid including user-provided input in content passed to the LLM (
llmContent) to prevent prompt injection. If the input is for display, usereturnDisplay.
| const flashAnalysis = | ||
| typeof flashResult['unproductive_state_analysis'] === 'string' | ||
| ? flashResult['unproductive_state_analysis'] | ||
| : ''; |
There was a problem hiding this comment.
flashAnalysis defaults to an empty string if unproductive_state_analysis is not a string. This can lead to a confusing recovery message for the model, such as "Details: . Please take a step back...". It would be better to default to undefined here, consistent with how mainModelAnalysis is handled. This will allow the fallback message "Repetitive patterns identified" to be used in _recoverFromLoop when the analysis is missing.
| const flashAnalysis = | |
| typeof flashResult['unproductive_state_analysis'] === 'string' | |
| ? flashResult['unproductive_state_analysis'] | |
| : ''; | |
| const flashAnalysis = | |
| typeof flashResult['unproductive_state_analysis'] === 'string' | |
| ? flashResult['unproductive_state_analysis'] | |
| : undefined; |
References
- When using an optional string with a fallback value, ensure the fallback is used for empty or uninformative strings to provide clear messages.
Summary
This PR implements an iterative loop detection and recovery system for the Gemini CLI. Instead of terminating immediately upon detecting a potential loop, the system now provides an automated recovery turn with detailed feedback to the model, allowing it to rethink its approach before final termination.
Details
Key changes include:
• Iterative Feedback Mechanism: Updated
GeminiClientto handle the first loop detection as a feedback turn. The model is warned via a hidden system message (preserving the original user's display context) that includes specific loop details (repeating content snippets or repeated tool calls) and is instructed to rethink the problem.• Structured Loop Detection Results: Refactored
LoopDetectionServiceto return aLoopDetectionResultobject containing detection counts and specific reasoning/context.• Proactive Thresholds: Lowered
LLM_CHECK_AFTER_TURNSfrom 40 to 30 to identify cognitive loops earlier in long-running tasks.• Enhanced Telemetry: Updated
LoopDetectedEventwithcount,analysis, andconfidencefields and introduced a newCONTENT_CHANTING_LOOPtype for more granular tracking.• Comprehensive Test Suite: Added exhaustive unit and integration tests to verify the two-strike logic.
Related Issues
Related to #18551
How to Validate
Build the project:
npm install
npm run build -w @google/gemini-cli-core
Run Loop Detection Tests:
npx vitest run packages/core/src/services/loopDetectionService.test.ts
Run Core Client Tests:
npx vitest run packages/core/src/core/client.test.ts
Comprehensive Test Suite:
npm run test -w @google/gemini-cli-core
Pre-Merge Checklist