Repository navigation
Review only: HOUSE-79 upstream v8.0.44, part 1 of 13: auth routing, affinity, quota, cooldown #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package auth | ||
|
|
||
| // WatchAuthChanges observes structural changes to one runtime credential, | ||
| // including token preparation/refresh, replacement, removal and store reload. | ||
| // Notifications are coalesced: consumers must inspect the latest manager state. | ||
| // The caller must unsubscribe when its work ends. Request result accounting does | ||
| // not notify watchers, so ordinary Generation increments do not invalidate work. | ||
| func (m *Manager) WatchAuthChanges(authID string) (<-chan struct{}, func()) { | ||
| changes := make(chan struct{}, 1) | ||
| if m == nil || authID == "" { | ||
| close(changes) | ||
| return changes, func() {} | ||
| } | ||
| m.mu.Lock() | ||
| if m.authChangeWatchers == nil { | ||
| m.authChangeWatchers = make(map[string]map[chan struct{}]struct{}) | ||
| } | ||
| if m.authChangeWatchers[authID] == nil { | ||
| m.authChangeWatchers[authID] = make(map[chan struct{}]struct{}) | ||
| } | ||
| m.authChangeWatchers[authID][changes] = struct{}{} | ||
| m.mu.Unlock() | ||
| return changes, func() { | ||
| m.mu.Lock() | ||
| defer m.mu.Unlock() | ||
| watchers := m.authChangeWatchers[authID] | ||
| if _, exists := watchers[changes]; !exists { | ||
| return | ||
| } | ||
| delete(watchers, changes) | ||
| if len(watchers) == 0 { | ||
| delete(m.authChangeWatchers, authID) | ||
| } | ||
| close(changes) | ||
| } | ||
| } | ||
|
|
||
| // Caller holds m.mu. No user callback or blocking work runs under the manager lock. | ||
| func (m *Manager) notifyAuthChangeLocked(authID string) { | ||
| for changes := range m.authChangeWatchers[authID] { | ||
| select { | ||
| case changes <- struct{}{}: | ||
| default: | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/router-for-me/CLIProxyAPI/v8/internal/auth/codex" | ||
| ) | ||
|
|
||
| // IsCodexAuthRecord reports whether auth represents a Codex credential record. | ||
| func IsCodexAuthRecord(auth *Auth) bool { | ||
| if auth == nil { | ||
| return false | ||
| } | ||
| if strings.EqualFold(strings.TrimSpace(auth.Provider), "codex") { | ||
| return true | ||
| } | ||
| if auth.Metadata != nil { | ||
| if t, ok := auth.Metadata["type"].(string); ok && strings.EqualFold(strings.TrimSpace(t), "codex") { | ||
| return true | ||
| } | ||
| } | ||
| if _, ok := auth.Storage.(*codex.CodexTokenStorage); ok { | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // PersistCodexAuthFile persists a Codex auth record to path using inter-process file locking, | ||
| // read-before-write merging, and scoped refresh patches when present. | ||
| // It returns handled=false when auth is not a Codex credential record. | ||
| func PersistCodexAuthFile(ctx context.Context, path string, auth *Auth) (bool, error) { | ||
| if !IsCodexAuthRecord(auth) { | ||
| return false, nil | ||
| } | ||
|
|
||
| type metadataSetter interface { | ||
| SetMetadata(map[string]any) | ||
| } | ||
|
|
||
| switch { | ||
| case auth.CodexRefreshPatch != nil: | ||
| errRefresh := codex.WithFileLock(path, func() error { | ||
| existing, errRead := os.ReadFile(path) | ||
| if errRead != nil { | ||
| if os.IsNotExist(errRead) { | ||
| if auth.Metadata == nil { | ||
| auth.Metadata = make(map[string]any) | ||
| } | ||
| auth.Metadata["disabled"] = auth.Disabled | ||
| NormalizeCredentialMetadata(auth.Metadata) | ||
| return codex.WriteJSONFileAtomic(path, auth.Metadata) | ||
| } | ||
| return fmt.Errorf("auth filestore: read refresh source failed: %w", errRead) | ||
| } | ||
| var diskMap map[string]any | ||
| if errUnmarshal := json.Unmarshal(existing, &diskMap); errUnmarshal != nil || diskMap == nil { | ||
| return fmt.Errorf("auth filestore: invalid refresh source JSON") | ||
| } | ||
| merged := auth.CodexRefreshPatch.Apply(diskMap) | ||
| NormalizeCredentialMetadata(merged) | ||
| if errWrite := codex.WriteJSONFileAtomic(path, merged); errWrite != nil { | ||
| return errWrite | ||
| } | ||
| auth.Metadata = merged | ||
| return nil | ||
| }) | ||
| if errRefresh != nil { | ||
| return true, errRefresh | ||
| } | ||
| auth.CodexRefreshPatch = nil | ||
| auth.Storage = nil | ||
| if disabled, ok := auth.Metadata["disabled"].(bool); ok { | ||
| auth.Disabled = disabled | ||
| } | ||
| return true, nil | ||
|
|
||
| case auth.Storage != nil: | ||
| if auth.Metadata == nil { | ||
| auth.Metadata = make(map[string]any) | ||
| } | ||
| auth.Metadata["disabled"] = auth.Disabled | ||
| if storage, ok := auth.Storage.(*codex.CodexTokenStorage); ok && storage != nil { | ||
| clonedStorage := *storage | ||
| clonedStorage.ReplaceCredentials = HasAuthCreationIntent(ctx) | ||
| auth.Storage = &clonedStorage | ||
| } | ||
| if setter, ok := auth.Storage.(metadataSetter); ok { | ||
| setter.SetMetadata(auth.Metadata) | ||
| } | ||
| if errSave := auth.Storage.SaveTokenToFile(path); errSave != nil { | ||
| return true, errSave | ||
| } | ||
| if persistedBytes, errReadPersisted := os.ReadFile(path); errReadPersisted == nil && len(persistedBytes) > 0 { | ||
| var diskMap map[string]any | ||
| if errUnmarshal := json.Unmarshal(persistedBytes, &diskMap); errUnmarshal == nil && len(diskMap) > 0 { | ||
| auth.Metadata = codex.MergeCodexCredentialsFromDisk(diskMap, auth.Metadata) | ||
| NormalizeCredentialMetadata(auth.Metadata) | ||
| } | ||
| } | ||
| return true, nil | ||
|
|
||
| case auth.Metadata != nil: | ||
| auth.Metadata["disabled"] = auth.Disabled | ||
| errLock := codex.WithFileLock(path, func() error { | ||
| existing, errRead := os.ReadFile(path) | ||
| if errRead == nil && len(existing) > 0 { | ||
| var diskMap map[string]any | ||
| if errUnmarshal := json.Unmarshal(existing, &diskMap); errUnmarshal == nil && len(diskMap) > 0 { | ||
| if !HasAuthCreationIntent(ctx) { | ||
| auth.Metadata = codex.MergeCodexCredentialsFromDisk(diskMap, auth.Metadata) | ||
| } | ||
| auth.Metadata["disabled"] = auth.Disabled | ||
| NormalizeCredentialMetadata(auth.Metadata) | ||
| if raw, errMarshal := json.Marshal(auth.Metadata); errMarshal == nil && string(existing) == string(raw) { | ||
| return nil | ||
| } | ||
| } | ||
| } else if errRead != nil && !os.IsNotExist(errRead) { | ||
| return fmt.Errorf("auth filestore: read existing failed: %w", errRead) | ||
| } | ||
| return codex.WriteJSONFileAtomic(path, auth.Metadata) | ||
| }) | ||
| if errLock != nil { | ||
| return true, errLock | ||
| } | ||
| return true, nil | ||
|
|
||
| default: | ||
| return false, nil | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Medium · Treat future credit-hint timestamps as stale
If a hint is written by a node whose clock is ahead,
now.Sub(hint.UpdatedAt)is negative and therefore always below the refresh interval. A known-unavailable hint can consequently exclude an Antigravity credential without revalidation until the local clock passes the future timestamp plus ten minutes, yielding a false no-fallback result when it is the only candidate.