gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

fix: prevent infinite loop in prompt completion on error

Open galz10 opened this issue 3 weeks ago • 2 comments

Summary

Fixes a critical bug where the background prompt completion feature would enter an infinite retry loop when encountering an API error (such as "Quota Exceeded").

Details

The issue was caused by the usePromptCompletion hook clearing its internal state (lastRequestedTextRef) whenever an API request failed. This was intended to allow retries, but instead caused the component to treat the existing user input as "new" during the next render cycle, triggering an immediate retry. This cycle would repeat indefinitely as long as the error persisted.

The fix removes the logic that clears lastRequestedTextRef on error. Now, the CLI remembers that it has already attempted completion for the current text, preventing automatic retries until the user actually modifies the input.

Related Issues

Fixes #14477

How to Validate

  1. Ensure prompt completion is enabled in your configuration (enablePromptCompletion: true).
  2. Open the CLI and type a partial command (e.g., Create a func).
  3. Simulate a failure condition (e.g., by temporarily disabling network or if you are hitting a rate limit).
  4. Before Fix: The CLI would endlessly spam the API in the background (visible in logs or via high CPU usage).
  5. After Fix: The CLI should attempt the request once, fail gracefully, and not retry until you type another character.

Pre-Merge Checklist

  • [ ] Updated relevant documentation and README (if needed)
  • [x] Added/updated tests (Verified via reproduction test suite locally)
  • [ ] Noted breaking changes (if any)
  • [x] Validated on required platforms/methods:
    • [x] MacOS
      • [x] npm run
      • [ ] npx
      • [ ] Docker
      • [ ] Podman
      • [ ] Seatbelt
    • [ ] Windows
      • [ ] npm run
      • [ ] npx
      • [ ] Docker
    • [ ] Linux
      • [ ] npm run
      • [ ] npx
      • [ ] Docker

galz10 avatar Dec 05 '25 00:12 galz10

Summary of Changes

Hello @galz10, I'm Gemini Code Assist[^1]! 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 resolves a significant bug in the CLI's background prompt completion mechanism. Previously, API errors would cause the system to enter an endless retry loop, leading to excessive resource consumption and API calls. The implemented solution prevents this by ensuring that the system no longer automatically retries completion requests after an error unless the user explicitly changes their input, thereby improving the stability and efficiency of the CLI.

Highlights

  • Bug Fix: Infinite Retry Loop: Addresses a critical bug where the background prompt completion feature would enter an infinite retry loop when encountering API errors like 'Quota Exceeded'.
  • Root Cause Identified: The issue stemmed from the usePromptCompletion hook clearing its internal state (lastRequestedTextRef) upon API request failure, which inadvertently triggered immediate retries as the existing user input was treated as 'new'.
  • Solution Implemented: The fix involves removing the logic that clears lastRequestedTextRef on error. This ensures the CLI remembers previous completion attempts, preventing automatic retries until the user actively modifies their input.
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 :thumbsup: and :thumbsdown: 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.

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

gemini-code-assist[bot] avatar Dec 05 '25 00:12 gemini-code-assist[bot]

Size Change: -45 B (0%)

Total Size: 22 MB

ℹ️ View Unchanged
Filename Size Change
./bundle/gemini.js 22 MB -45 B (0%)
./bundle/sandbox-macos-permissive-closed.sb 1.03 kB 0 B
./bundle/sandbox-macos-permissive-open.sb 890 B 0 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB 0 B
./bundle/sandbox-macos-restrictive-closed.sb 3.29 kB 0 B
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB 0 B
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB 0 B

compressed-size-action

github-actions[bot] avatar Dec 05 '25 00:12 github-actions[bot]