Skip to content

feat(loop-reduction): implement iterative loop detection and model feedback#20763

Merged
aishaneeshah merged 12 commits intomainfrom
loop-reduction
Mar 4, 2026
Merged

feat(loop-reduction): implement iterative loop detection and model feedback#20763
aishaneeshah merged 12 commits intomainfrom
loop-reduction

Conversation

@aishaneeshah
Copy link
Contributor

@aishaneeshah aishaneeshah commented Mar 1, 2026

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 GeminiClient to 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 LoopDetectionService to return a LoopDetectionResult object containing detection counts and specific reasoning/context.
• Proactive Thresholds: Lowered LLM_CHECK_AFTER_TURNS from 40 to 30 to identify cognitive loops earlier in long-running tasks.
• Enhanced Telemetry: Updated LoopDetectedEvent with count, analysis, and confidence fields and introduced a new CONTENT_CHANTING_LOOP type 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

  1. Build the project:
    npm install
    npm run build -w @google/gemini-cli-core

  2. Run Loop Detection Tests:
    npx vitest run packages/core/src/services/loopDetectionService.test.ts

  3. Run Core Client Tests:
    npx vitest run packages/core/src/core/client.test.ts

  4. Comprehensive Test Suite:
    npm run test -w @google/gemini-cli-core

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

@aishaneeshah aishaneeshah requested a review from a team as a code owner March 1, 2026 15:32
@gemini-cli
Copy link
Contributor

gemini-cli bot commented Mar 1, 2026

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 Fixes #123 or Related to #123), it will be automatically reopened.

How to link an issue:
Add a keyword followed by the issue number (e.g., Fixes #123) in the description of your pull request. For more details on supported keywords and how linking works, please refer to the GitHub Documentation on linking pull requests to issues.

Thank you for your understanding and for being a part of our community!

@gemini-cli gemini-cli bot closed this Mar 1, 2026
@gemini-cli
Copy link
Contributor

gemini-cli bot commented Mar 1, 2026

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!

@gemini-code-assist
Copy link
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 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

  • Iterative Loop Feedback: The Gemini CLI now provides automated recovery turns with detailed feedback to the model upon initial loop detection, rather than immediate termination. This allows the model to rethink its approach before final termination.
  • Structured Loop Detection Results: The LoopDetectionService has been refactored to return a LoopDetectionResult object, providing more granular information about detected loops, including count and specific details.
  • Proactive LLM Checks: The threshold for triggering LLM-based loop checks (LLM_CHECK_AFTER_TURNS) has been reduced from 30 to 20 turns to identify cognitive loops earlier in long-running tasks.
  • Enhanced Telemetry: The LoopDetectedEvent has been updated to include analysis and confidence fields, and a new CONTENT_CHANTING_LOOP type has been introduced for more granular tracking.
  • History Truncation for LLM: The history sent to the LLM for loop detection is now truncated to prevent excessively long prompts, improving efficiency and relevance of loop analysis.
Changelog
  • packages/core/src/core/client.test.ts
    • Updated loopDetector mocks to return LoopDetectionResult objects instead of booleans.
    • Adjusted test assertions to expect LoopDetectionResult.count instead of boolean values.
  • packages/core/src/core/client.ts
    • Modified sendMessageStream to process LoopDetectionResult from loopDetector.turnStarted and loopDetector.addAndCheck.
    • Implemented a recursive call to sendMessageStream with system feedback when a loop is detected for the first time, allowing the model to rethink.
  • packages/core/src/services/loopDetectionService.test.ts
    • Adjusted test assertions to expect LoopDetectionResult.count instead of boolean values from addAndCheck and turnStarted.
    • Updated advanceTurns calls to reflect the new LLM_CHECK_AFTER_TURNS value (from 30 to 20).
    • Increased fillerContent length in a test to 2000 from 500.
  • packages/core/src/services/loopDetectionService.ts
    • Reduced LLM_CHECK_AFTER_TURNS constant from 30 to 20.
    • Defined LoopDetectionResult interface to include count and optional detail.
    • Added detectedCount and lastLoopDetail properties to track loop state.
    • Refactored addAndCheck and turnStarted methods to return LoopDetectionResult objects.
    • Enhanced addAndCheck to provide specific detail for tool call and content loops.
    • Updated checkForLoopWithLLM to return an object { isLoop: boolean; analysis?: string } and to truncate history content sent to the LLM.
    • Modified handleConfirmedLoop to pass analysis and confidence to LoopDetectedEvent.
    • Reset detectedCount and lastLoopDetail in the reset method.
    • Improved error logging for queryLoopDetectionModel.
  • packages/core/src/telemetry/types.ts
    • Added CONTENT_CHANTING_LOOP to the LoopType enum.
    • Extended LoopDetectedEvent constructor and properties to include analysis and confidence.
Activity
  • The pull request introduces an iterative loop detection and recovery system.
  • The GeminiClient was updated to provide automated feedback to the model upon initial loop detection.
  • The LoopDetectionService was refactored to return structured LoopDetectionResult objects.
  • The LLM_CHECK_AFTER_TURNS threshold was lowered for earlier loop identification.
  • Telemetry events for loop detection were enhanced with additional details.
  • Tests were updated to reflect the new LoopDetectionResult structure and adjusted thresholds.
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 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 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

  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.

Copy link
Contributor

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

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

@gemini-cli gemini-cli bot reopened this Mar 2, 2026
@gemini-cli
Copy link
Contributor

gemini-cli bot commented Mar 2, 2026

Thank you for linking an issue! This pull request has been automatically reopened.

@github-actions
Copy link

github-actions bot commented Mar 2, 2026

Size Change: +4.82 kB (+0.02%)

Total Size: 26 MB

Filename Size Change
./bundle/gemini.js 25.5 MB +4.82 kB (+0.02%)
ℹ️ View Unchanged
Filename Size
./bundle/node_modules/@google/gemini-cli-devtools/dist/client/main.js 221 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/_client-assets.js 227 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/index.js 11.5 kB
./bundle/node_modules/@google/gemini-cli-devtools/dist/src/types.js 132 B
./bundle/sandbox-macos-permissive-open.sb 890 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB
./bundle/sandbox-macos-strict-open.sb 4.82 kB
./bundle/sandbox-macos-strict-proxied.sb 5.02 kB

compressed-size-action

@gemini-cli gemini-cli bot added area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item. labels Mar 2, 2026
@aishaneeshah aishaneeshah reopened this Mar 3, 2026
@aishaneeshah aishaneeshah force-pushed the loop-reduction branch 2 times, most recently from ffd4050 to 38ff91e Compare March 3, 2026 20:02
@aishaneeshah
Copy link
Contributor Author

/gemini review

Copy link
Contributor

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

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

@aishaneeshah aishaneeshah force-pushed the loop-reduction branch 2 times, most recently from 1c16169 to fff9dd2 Compare March 4, 2026 15:53
@aishaneeshah
Copy link
Contributor Author

/gemini review

Copy link
Contributor

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

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 '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.`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

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.

Suggested change
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
  1. Avoid including user-provided input in content passed to the LLM (llmContent) to prevent prompt injection. If the input is for display, use returnDisplay.

Comment on lines +585 to +588
const flashAnalysis =
typeof flashResult['unproductive_state_analysis'] === 'string'
? flashResult['unproductive_state_analysis']
: '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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.

Suggested change
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
  1. When using an optional string with a fallback value, ensure the fallback is used for empty or uninformative strings to provide clear messages.

@aishaneeshah aishaneeshah added this pull request to the merge queue Mar 4, 2026
Merged via the queue into main with commit e200b40 Mar 4, 2026
28 checks passed
@aishaneeshah aishaneeshah deleted the loop-reduction branch March 4, 2026 19:51
BryanBradfo pushed a commit to BryanBradfo/gemini-cli that referenced this pull request Mar 5, 2026
struckoff pushed a commit to struckoff/gemini-cli that referenced this pull request Mar 6, 2026
kunal-10-cloud pushed a commit to kunal-10-cloud/gemini-cli that referenced this pull request Mar 12, 2026
liamhelmer pushed a commit to badal-io/gemini-cli that referenced this pull request Mar 12, 2026
yashodipmore pushed a commit to yashodipmore/geemi-cli that referenced this pull request Mar 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality 🔒 maintainer only ⛔ Do not contribute. Internal roadmap item.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants