Skip to content

Conversation

@ShashankSaga
Copy link
Contributor

Summary

This PR introduces lightweight provider health tracking and safe fallback routing inside AIProviderRegistry.

The goal is to improve reliability when an AI provider becomes temporarily unavailable (timeouts, crashes, API errors), without changing any existing behavior or API contracts.

What’s Included

  • Internal health metadata per provider:
    • last failure timestamp
    • consecutive failure count
    • health status (healthy / degraded)
  • Automatic degradation of providers after repeated failures
  • Safe provider selection that:
    • prefers healthy providers
    • skips degraded providers when possible
    • falls back gracefully if all providers are degraded

Design Notes

  • No changes to existing providers or consumers
  • No new dependencies added
  • No frontend or API behavior changes
  • Logic is fully contained inside AIProviderRegistry
  • Designed to be extensible for future retries, cooldowns, or metrics

Motivation

Currently all providers are treated as equally healthy. This PR adds resilience and prepares the system for real-world provider instability while keeping the implementation minimal and non-breaking.


Closes #105

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ShashankSaga, 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 robustness of the AI provider system by integrating a lightweight health tracking and dynamic routing mechanism into the AIProviderRegistry. The changes enable the system to automatically detect and adapt to temporary provider unavailabilities, ensuring continuous operation by prioritizing healthy providers and implementing a graceful fallback strategy. This improvement is designed to be non-breaking, without altering existing API contracts or introducing new dependencies, while laying the groundwork for future resilience features.

Highlights

  • Provider Health Tracking: The AIProviderRegistry now includes internal health metadata for each AI provider, tracking "last failure timestamp" and "consecutive failure count" to determine a "healthy" or "degraded" status.
  • Automatic Degradation: Providers are automatically marked as "degraded" after reaching a configurable threshold of consecutive failures, improving system resilience.
  • Safe Provider Selection & Fallback: The system now intelligently selects providers, preferring "healthy" ones, skipping "degraded" ones, and gracefully falling back to alternative providers if all primary options are unavailable.
  • New GeminiProvider: A new GeminiProvider has been introduced, initially with a mock response, and is registered within the AIProviderRegistry.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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.

@ShashankSaga
Copy link
Contributor Author

Hi @abhishek-nexgen-dev 👋

As discussed, I’ve implemented a minimal, non-breaking provider health tracking and safe fallback mechanism inside AIProviderRegistry.

The changes are fully internal, don’t affect existing flows, and are designed to be extensible for future improvements (cooldowns, retries, metrics).

Please let me know if you’d like any adjustments or additional safeguards added. Happy to iterate further. Thanks!

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 feature for provider health tracking and fallback routing within the AIProviderRegistry. The overall approach is well-contained and the implementation of health status tracking (markSuccess, markFailure) is clear. However, I've identified a significant issue in the generateTextWithFallback method where the logic doesn't fully align with the stated goals in the PR description, and it loses critical error information. My review includes a suggested refactoring of this method to improve its robustness and align it with the intended behavior.

Comment on lines 95 to 114
async generateTextWithFallback(
capabilities: AICapability[],
input: { prompt: string; context?: string }
): Promise<string> {
const candidates = this.findByCapabilities(capabilities)

for (const provider of candidates) {
if (!this.isHealthy(provider.name)) continue

try {
const result = await provider.generateText(input)
this.markSuccess(provider.name)
return result
} catch {
this.markFailure(provider.name)
}
}

throw new Error('No healthy AI provider available')
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The current implementation of generateTextWithFallback has a couple of issues that deviate from the goals outlined in the PR description:

  1. Incorrect Fallback Logic: The PR description mentions that the system should "fall back gracefully if all providers are degraded." However, the code only attempts to use providers that are currently healthy (if (!this.isHealthy(provider.name)) continue) and never tries degraded ones, even if they are the only option.
  2. Error Swallowing: The catch block is empty, which means the actual error from the provider is lost. This makes debugging failures nearly impossible, as the caller only receives a generic "No healthy AI provider available" message without any context on why the provider failed (e.g., API error, timeout, etc.).

I suggest a refactoring to correctly implement the fallback logic and preserve error information for better diagnostics.

  async generateTextWithFallback(
    capabilities: AICapability[],
    input: { prompt: string; context?: string }
  ): Promise<string> {
    const candidates = this.findByCapabilities(capabilities);
    const healthyProviders = candidates.filter(p => this.isHealthy(p.name));
    const degradedProviders = candidates.filter(p => !this.isHealthy(p.name));
    let lastError: unknown;

    // First, try all healthy providers
    for (const provider of healthyProviders) {
      try {
        const result = await provider.generateText(input);
        this.markSuccess(provider.name);
        return result;
      } catch (err) {
        lastError = err;
        this.markFailure(provider.name);
      }
    }

    // If all healthy providers failed, fall back to degraded ones
    for (const provider of degradedProviders) {
      try {
        const result = await provider.generateText(input);
        this.markSuccess(provider.name);
        return result;
      } catch (err) {
        lastError = err;
        this.markFailure(provider.name);
      }
    }

    if (lastError) {
      throw lastError;
    }

    throw new Error('No AI provider available for the specified capabilities.');
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the detailed review 👍
Good catch — I’ve refactored generateTextWithFallback to:

  • Attempt all healthy providers first
  • Gracefully fall back to degraded providers if needed
  • Preserve and rethrow the last provider error for better diagnostics

This aligns the implementation with the PR description while keeping the behavior non-breaking. Please let me know if you’d like further adjustments.

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.

Add Provider Health Tracking and Safe Fallback Routing to AIProviderRegistry

1 participant