Skip to content

fix(core): prevent infinite loops with hard cap on consecutive same-name tool calls#25764

Open
noxymon wants to merge 7 commits intogoogle-gemini:mainfrom
noxymon:fix/issue-25671
Open

fix(core): prevent infinite loops with hard cap on consecutive same-name tool calls#25764
noxymon wants to merge 7 commits intogoogle-gemini:mainfrom
noxymon:fix/issue-25671

Conversation

@noxymon
Copy link
Copy Markdown

@noxymon noxymon commented Apr 21, 2026

Summary

This PR implements a hard cap for consecutive calls to the same tool name to prevent infinite loops, especially in "thinking" tools where arguments (like thought numbers) change each time. It also enhances the loop detection event to carry more details to the UI.

Details

  • Added CONSECUTIVE_SAME_TOOL_NAME_THRESHOLD (50) and THINKING_TOOL_LOOP_THRESHOLD (15) to LoopDetectionService.
  • Enhanced loop detection to track consecutive calls to the same tool name even if arguments differ.
  • Updated LoopDetected event to carry specific loop details ( ype, count, detail).
  • Improved UI alerting in useGeminiStream to display detailed information about the detected loop.
  • Added comprehensive unit tests for the new loop detection logic.

Related Issues

Fixes #25671

How to Validate

  1. Run
    pm test -w @google/gemini-cli-core -- loopDetectionService.test.ts to verify the logic.
  2. Trigger a loop in the CLI with a tool that changes arguments (e.g. sequentialthinking) and verify it stops after 15 calls with a detailed message.

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

noxymon added 4 commits April 22, 2026 03:07
…oops (google-gemini#25671)

- Added CONSECUTIVE_SAME_TOOL_NAME_THRESHOLD (50) and THINKING_TOOL_LOOP_THRESHOLD (15) to LoopDetectionService.
- Enhanced loop detection to track consecutive calls to the same tool name even if arguments differ.
- Updated LoopDetected event to carry specific loop details.
- Improved UI alerting to display detailed information about the detected loop.
@noxymon noxymon requested a review from a team as a code owner April 21, 2026 21:08
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 21, 2026

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.

@gemini-code-assist
Copy link
Copy Markdown
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 system's ability to detect and prevent infinite loops during tool calls by introducing new thresholds and tracking mechanisms. It specifically addresses cases where tool arguments might change, which previously bypassed simpler loop detection. The changes also improve the user experience by providing more detailed information when a loop is identified, allowing for better debugging and understanding of model behavior.

Highlights

  • Enhanced Loop Detection: Implemented a hard cap for consecutive calls to the same tool name, even if arguments differ, to prevent infinite loops, particularly for 'thinking' tools.
  • Configurable Thresholds: Introduced CONSECUTIVE_SAME_TOOL_NAME_THRESHOLD (50) and THINKING_TOOL_LOOP_THRESHOLD (15) to LoopDetectionService for more granular control over loop detection.
  • Detailed Loop Information: The LoopDetected event now carries specific details about the detected loop (type, count, detail, confirmedByModel) to provide richer context.
  • Improved UI Feedback: The UI alerting in useGeminiStream was updated to display the detailed information from the LoopDetected event, enhancing user understanding.
  • Comprehensive Unit Tests: Added extensive unit tests to cover the new loop detection logic, including scenarios with varying arguments for consecutive tool calls.
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.

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
Copy Markdown
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 enhances loop detection by tracking consecutive calls to the same tool name even when arguments differ, which is particularly useful for identifying loops in 'thinking' tools. It updates the UI to display specific loop details and adds corresponding unit tests. Feedback suggests introducing a distinct telemetry event type for same-name tool calls to distinguish them from truly identical repetitions, ensuring more accurate data analysis.

Comment thread packages/core/src/telemetry/types.ts
Comment thread packages/core/src/services/loopDetectionService.ts Outdated
noxymon and others added 2 commits April 22, 2026 04:16
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Infinite Loop in sequentialthinking Tool leading to Quota Exhaustion

1 participant