Skip to content

fix(auth): prevent stale runtime state inheritance from disabled auth entries#2133

Merged
luispater merged 2 commits intorouter-for-me:devfrom
DragonFSKY:fix/2061-stale-modelstates
Mar 28, 2026
Merged

fix(auth): prevent stale runtime state inheritance from disabled auth entries#2133
luispater merged 2 commits intorouter-for-me:devfrom
DragonFSKY:fix/2061-stale-modelstates

Conversation

@DragonFSKY
Copy link
Copy Markdown
Contributor

Summary

  • Gate runtime state inheritance (ModelStates, LastRefreshedAt, NextRefreshAfter) in both Manager.Update and Service.applyCoreAuthAddOrUpdate so that state is only carried over when both the existing and incoming auth are non-disabled
  • Prevents re-created auth files from inheriting stale cooldown/backoff state from previously disabled entries

Root Cause

When an auth file is deleted, applyCoreAuthRemoval marks the runtime entry as Disabled=true / Status=StatusDisabled but does not remove it from coreManager. When the same file path is re-added, the update branch unconditionally inherited ModelStates from the old disabled entry, causing the new auth to carry over stale cooldown/backoff state and become unroutable.

Changes

  • sdk/cliproxy/auth/conductor.go — Add disabled guard to ModelStates inheritance in Manager.Update
  • sdk/cliproxy/service.go — Add disabled guard to LastRefreshedAt, NextRefreshAfter, and ModelStates inheritance in Service.applyCoreAuthAddOrUpdate
  • sdk/cliproxy/auth/conductor_update_test.go — Add 4 test cases covering all disabled/active transition combinations

Test plan

  • Disabled existing + Disabled incoming → no state inheritance
  • Active existing + Disabled incoming → no state inheritance
  • Disabled existing + Active incoming → no state inheritance
  • Active existing + Active incoming → normal inheritance preserved
  • Full go test ./sdk/cliproxy/auth/ and go test ./sdk/cliproxy/ pass with no regressions

Closes #2061

…ntries

When an auth file is deleted and re-created with the same path/ID, the
new auth could inherit stale ModelStates (cooldown/backoff) from the
previously disabled entry, preventing it from being routed.

Gate runtime state inheritance (ModelStates, LastRefreshedAt,
NextRefreshAfter) on both existing and incoming auth being non-disabled
in Manager.Update and Service.applyCoreAuthAddOrUpdate.

Closes router-for-me#2061
@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 resolves an issue where re-adding an authentication configuration that was previously disabled could lead to it inheriting stale runtime state, such as cooldown or backoff information. The core problem was that disabled authentication entries were retained in the system, and their state was unconditionally inherited upon re-creation. The implemented solution introduces checks to ensure that runtime state inheritance (including ModelStates, LastRefreshedAt, and NextRefreshAfter) only proceeds if both the existing and the incoming authentication entries are active. This prevents new or re-enabled authentication configurations from becoming unroutable due to outdated, disabled state.

Highlights

  • State Inheritance Logic: Runtime state inheritance for ModelStates, LastRefreshedAt, and NextRefreshAfter is now gated to occur only when both the existing and incoming authentication entries are non-disabled.
  • Stale State Prevention: This change prevents re-created authentication files from inheriting stale cooldown or backoff state from previously disabled entries, ensuring new configurations start clean.
  • Test Coverage: Four new test cases were added to cover all combinations of disabled/active transitions for authentication entries, verifying the new inheritance logic.

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

Changelog
  • sdk/cliproxy/auth/conductor.go
    • Added a condition to the Manager.Update method to prevent ModelStates inheritance if either the existing or incoming authentication entry is disabled.
  • sdk/cliproxy/auth/conductor_update_test.go
    • Added four new test cases to validate ModelStates inheritance behavior across various disabled and active authentication state transitions.
  • sdk/cliproxy/service.go
    • Added a condition to the Service.applyCoreAuthAddOrUpdate method to prevent LastRefreshedAt, NextRefreshAfter, and ModelStates inheritance if either the existing or incoming authentication entry is disabled.
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
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 correctly prevents stale runtime state from being inherited by disabled auth entries. The logic is sound and the added tests cover all the required scenarios. I have a couple of suggestions to improve the readability and maintainability of the new conditional logic, which is currently duplicated in two files.

Comment on lines +835 to 839
if !existing.Disabled && existing.Status != StatusDisabled && !auth.Disabled && auth.Status != StatusDisabled {
if len(auth.ModelStates) == 0 && len(existing.ModelStates) > 0 {
auth.ModelStates = existing.ModelStates
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This condition is a bit long and is also duplicated in sdk/cliproxy/service.go. To improve readability and maintainability, I suggest introducing local variables with an explanatory comment. This makes the logic clearer and easier to manage. I've added a similar suggestion for the other file where this logic is duplicated.

                // An auth entry is considered enabled if it is not explicitly disabled and its status is not StatusDisabled.
                // We check both fields for robustness as they might be updated from different sources.
                isExistingEnabled := !existing.Disabled && existing.Status != StatusDisabled
                isIncomingEnabled := !auth.Disabled && auth.Status != StatusDisabled
                if isExistingEnabled && isIncomingEnabled {
                        if len(auth.ModelStates) == 0 && len(existing.ModelStates) > 0 {
                                auth.ModelStates = existing.ModelStates
                        }
                }

Comment thread sdk/cliproxy/service.go
Comment on lines +289 to 295
if !existing.Disabled && existing.Status != coreauth.StatusDisabled && !auth.Disabled && auth.Status != coreauth.StatusDisabled {
auth.LastRefreshedAt = existing.LastRefreshedAt
auth.NextRefreshAfter = existing.NextRefreshAfter
if len(auth.ModelStates) == 0 && len(existing.ModelStates) > 0 {
auth.ModelStates = existing.ModelStates
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

As mentioned in the comment for sdk/cliproxy/auth/conductor.go, this condition is duplicated. Extracting the logic into local variables improves readability and maintainability by making the code self-documenting and reducing redundancy.

Suggested change
if !existing.Disabled && existing.Status != coreauth.StatusDisabled && !auth.Disabled && auth.Status != coreauth.StatusDisabled {
auth.LastRefreshedAt = existing.LastRefreshedAt
auth.NextRefreshAfter = existing.NextRefreshAfter
if len(auth.ModelStates) == 0 && len(existing.ModelStates) > 0 {
auth.ModelStates = existing.ModelStates
}
}
// An auth entry is considered enabled if it is not explicitly disabled and its status is not StatusDisabled.
// We check both fields for robustness as they might be updated from different sources.
isExistingEnabled := !existing.Disabled && existing.Status != coreauth.StatusDisabled
isIncomingEnabled := !auth.Disabled && auth.Status != coreauth.StatusDisabled
if isExistingEnabled && isIncomingEnabled {
auth.LastRefreshedAt = existing.LastRefreshedAt
auth.NextRefreshAfter = existing.NextRefreshAfter
if len(auth.ModelStates) == 0 && len(existing.ModelStates) > 0 {
auth.ModelStates = existing.ModelStates
}
}

Copy link
Copy Markdown
Collaborator

@luispater luispater left a comment

Choose a reason for hiding this comment

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

Summary:
This change matches the reported root cause. The new guards in both Manager.Update and Service.applyCoreAuthAddOrUpdate prevent stale runtime state from being inherited when either the existing auth or the incoming auth is disabled.

I did not find any blocking correctness issues.

Non-blocking:

  • Consider adding a service-level regression test for the watcher delete -> re-add flow so LastRefreshedAt, NextRefreshAfter, and the service-side inheritance path are covered directly.

Test plan:

  • Verified go test ./sdk/cliproxy/auth
  • Verified go test ./sdk/cliproxy
  • Verified go build -o test-output ./cmd/server

This is an automated Codex review result and still requires manual verification by a human reviewer.

@DragonFSKY
Copy link
Copy Markdown
Contributor Author

Thanks for the review.

I added the suggested service-level regression test for the watcher delete -> re-add path, covering LastRefreshedAt, NextRefreshAfter, and stale ModelStates inheritance on re-add.

@luispater luispater changed the base branch from main to dev March 28, 2026 12:50
@luispater luispater merged commit 73c8317 into router-for-me:dev Mar 28, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Re-created auth file inherits stale runtime state when re-added with the same path

2 participants