Skip to content

Conversation

@SandyTao520
Copy link
Collaborator

@SandyTao520 SandyTao520 commented Nov 11, 2025

TLDR

This PR enhances the LoopDetectionService by implementing a two-stage check mechanism. It first uses the gemini-2.5-flash model for a fast and cost-effective initial assessment. If a potential loop is detected with high confidence (>= 0.9), it performs a secondary confirmation check using gemini-2.5-pro (unless the CLI is in fallback mode). It also adds comprehensive telemetry for these checks.

Dive Deeper

  • 2-Stage Detection:
    • Stage 1: Queries gemini-2.5-flash.
    • Stage 2: If Flash confidence >= 0.9, queries DEFAULT_GEMINI_MODEL (gemini-2.5-pro).
    • Optimization: If this.config.isInFallbackMode() is true, the second check is skipped, and the first result is used. This prevents using the more expensive model when we are already in a fallback state.
  • Telemetry:
    • Added LlmLoopCheckEvent to track:
      • flash_confidence: Confidence score from the Flash model.
      • main_model: The name of the main model used for the second check.
      • main_model_confidence: Confidence score from the main model (or -1 if skipped, or 0 if the second check wasn't triggered).
    • Added corresponding EventMetadataKeys.
  • Tests: Added unit tests in loopDetectionService.test.ts to cover the new logic, including boundary conditions and model disagreement scenarios.

Reviewer Test Plan

  • Run npm run test:ci to verify the new unit tests in packages/core/src/services/loopDetectionService.test.ts.
  • Verify that the tests cover:
    • Triggering the check after LLM_CHECK_AFTER_TURNS.
    • The two-stage logic (Flash -> Pro).
    • The optimization when isInFallbackMode() is true.
    • Confidence threshold handling (>= 0.9).

Testing Matrix

🍏 🪟 🐧
npm run
npx
Docker
Podman - -
Seatbelt - -

Linked issues / bugs

@SandyTao520 SandyTao520 requested a review from a team as a code owner November 11, 2025 21:31
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @SandyTao520, 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 upgrades the LLM-based loop detection mechanism by introducing a more efficient and reliable two-stage verification process. It prioritizes a fast initial check with a lightweight model and escalates to a more robust model for high-confidence detections, ensuring accuracy while optimizing resource usage. Additionally, it integrates detailed telemetry to provide better insights into the loop detection process.

Highlights

  • Two-Stage Loop Detection: The LoopDetectionService now employs a two-stage check. It first uses the gemini-2.5-flash model for a quick assessment. If this model detects a potential loop with high confidence (>= 0.9), a secondary confirmation check is performed using the main configured model, unless the main model is already Flash.
  • Telemetry Integration: Comprehensive telemetry has been added through a new LlmLoopCheckEvent. This event tracks key metrics such as the confidence score from the Flash model, the name of the main model used for the second check, and its corresponding confidence score (or -1 if skipped, 0 if not triggered).
  • Testing Enhancements: New unit tests have been introduced in loopDetectionService.test.ts to thoroughly cover the new two-stage logic. These tests include scenarios for boundary conditions, such as confidence thresholds, and cases where the Flash and main models might disagree on loop detection.
  • Code Refactoring: The checkLlmForLoop method has been refactored to incorporate the new two-stage logic, and new private helper methods like queryLoopDetectionModel and handleConfirmedLoop have been introduced for better code organization and readability.
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.

@SandyTao520 SandyTao520 changed the title feat(core): enhance loop detection with 2-stage check and telemetry feat(core): enhance loop detection with 2-stage check Nov 11, 2025
@github-actions
Copy link

github-actions bot commented Nov 11, 2025

Size Change: +5.37 kB (+0.03%)

Total Size: 20.5 MB

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

compressed-size-action

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 valuable two-stage loop detection mechanism, leveraging a faster model for an initial check before escalating to the main model. This is a smart approach to optimize for cost and latency. The associated telemetry and unit tests are comprehensive and well-implemented. However, I've identified a high-severity issue where the optimization to skip the second check can be defeated when model routing is active (e.g., when using an 'auto' model alias), potentially leading to redundant API calls.

@SandyTao520 SandyTao520 disabled auto-merge November 12, 2025 00:14
@SandyTao520 SandyTao520 added this pull request to the merge queue Nov 12, 2025
Merged via the queue into main with commit 408b885 Nov 12, 2025
22 checks passed
@SandyTao520 SandyTao520 deleted the st/feat/enhanced-loop-detection branch November 12, 2025 04:59
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.

3 participants