History

fix(auth): preserve session affinity across priorities

Keep a healthy cached session binding even when a higher-priority credential recovers. Continue applying credential priority to cold selection, plugin scheduling, and true failover. Resolve the across-priority candidate list in a single availability pass and narrow the highest-priority tier from the same cloned auths, so the selection path does not scan or deep-clone candidates twice. Document that an established binding outranks credential priority. Fixes #4808

sususu committed 3 months ago

Commit 65fc5361 parent 2e91e994 files · +308 −36Browse files

Unified · Split

4 files
  1. config.example.yaml +3 −0
  2. sdk/cliproxy/auth/conductor_selection.go +37 −19
  3. sdk/cliproxy/auth/selector.go +90 −17
  4. sdk/cliproxy/auth/session_affinity_priority_test.go +178 −0
config.example.yaml+3−0
@@ -209,6 +209,9 @@ routing: · 208 unmodified lines
209209 # followed by prompt_cache_key, Responses conversation IDs, legacy body IDs,
210210 # execution or derived session identity, and the existing first-message hash fallback.
211211 # Automatic failover is always enabled when bound auth becomes unavailable.
212+ # An established binding outranks credential priority: once a session is bound, that
213+ # credential is kept even if a higher-priority credential recovers. Credential priority
214+ # still decides cold bindings, requests without a session, and post-failover rebinding.
212215 session-affinity: false # default: false
213216 # How long session-to-auth bindings are retained. Default: 1h
214217 session-affinity-ttl: "1h"
sdk/cliproxy/auth/conductor_selection.go+37−19
@@ -287,6 +287,14 @@ func (m *Manager) SetRoundTripperProvider(p RoundTripperProvider) { · 286 unmodified lines
287287 }
288288
289289 func (m *Manager) availableAuthsForRouteModel(auths []*Auth, provider, routeModel string, now time.Time) ([]*Auth, error) {
290+ return m.availableAuthsForRouteModelWithPriorityMode(auths, provider, routeModel, now, false)
291+}
292+
293+func (m *Manager) availableAuthsForRouteModelAcrossPriorities(auths []*Auth, provider, routeModel string, now time.Time) ([]*Auth, error) {
294+ return m.availableAuthsForRouteModelWithPriorityMode(auths, provider, routeModel, now, true)
295+}
296+
297+func (m *Manager) availableAuthsForRouteModelWithPriorityMode(auths []*Auth, provider, routeModel string, now time.Time, allPriorities bool) ([]*Auth, error) {
290298 if len(auths) == 0 {
291299 return nil, &Error{Code: "auth_not_found", Message: "no auth candidates"}
292300 }
@@ -325,20 +333,32 @@ func (m *Manager) availableAuthsForRouteModel(auths []*Auth, provider, routeMode · 32 unmodified lines
325333 return nil, &Error{Code: "auth_unavailable", Message: "no auth available"}
326334 }
327335
328− bestPriority := 0
329− found := false
330− for priority := range availableByPriority {
331− if !found || priority > bestPriority {
332− bestPriority = priority
333− found = true
334− }
335− }
336+ return availableAuthsFromPriorityBuckets(availableByPriority, allPriorities), nil
337+}
336338
337− available := availableByPriority[bestPriority]
338− if len(available) > 1 {
339− sort.Slice(available, func(i, j int) bool { return available[i].ID < available[j].ID })
340− }
341− return available, nil
339+// availableAuthsForSelector reports the candidates handed to priority-scoped consumers such as
340+// the plugin scheduler, plus the candidates handed to the configured selector. Both are equal
341+// unless session affinity is active, in which case the selector additionally receives lower
342+// priority tiers so an established binding can be validated instead of being preempted by a
343+// recovered higher-priority credential.
344+func (m *Manager) availableAuthsForSelector(selector Selector, auths []*Auth, provider, routeModel string, now time.Time) (priorityAuths, selectorAuths []*Auth, err error) {
345+ if _, sessionAffinity := selector.(*SessionAffinitySelector); !sessionAffinity {
346+ priorityAuths, err = m.availableAuthsForRouteModel(auths, provider, routeModel, now)
347+ if err != nil {
348+ return nil, nil, err
349+ }
350+ priorityAuths = cloneAuthSlice(priorityAuths)
351+ return priorityAuths, priorityAuths, nil
352+ }
353+
354+ // One availability pass and one clone pass serve both lists: the highest priority tier is a
355+ // subset of the across-priority candidates, so it is narrowed from the same cloned auths.
356+ selectorAuths, err = m.availableAuthsForRouteModelAcrossPriorities(auths, provider, routeModel, now)
357+ if err != nil {
358+ return nil, nil, err
359+ }
360+ selectorAuths = cloneAuthSlice(selectorAuths)
361+ return highestPriorityAuths(selectorAuths), selectorAuths, nil
342362 }
343363
344364 func selectionArgForSelector(selector Selector, routeModel string) string {
@@ -1011,12 +1031,11 @@ func (m *Manager) pickNextLegacy(ctx context.Context, provider, model string, op · 666 unmodified lines
10111031 m.mu.RUnlock()
10121032 return nil, nil, &Error{Code: "auth_not_found", Message: "no auth available"}
10131033 }
1014− available, errAvailable := m.availableAuthsForRouteModel(candidates, provider, model, time.Now())
1034+ available, selectorAuths, errAvailable := m.availableAuthsForSelector(selector, candidates, provider, model, time.Now())
10151035 if errAvailable != nil {
10161036 m.mu.RUnlock()
10171037 return nil, nil, errAvailable
10181038 }
1019− available = cloneAuthSlice(available)
10201039 m.mu.RUnlock()
10211040
10221041 selected, handled, errPick := m.pickViaPluginScheduler(ctx, pluginScheduler, provider, []string{provider}, model, opts, tried, available)
@@ -1025,7 +1044,7 @@ func (m *Manager) pickNextLegacy(ctx context.Context, provider, model string, op · 2 unmodified lines
10251044 }
10261045 if !handled {
10271046 selectorCtx := withWeightedSelectorStateModel(ctx, selector, model)
1028− selected, errPick = selector.Pick(selectorCtx, provider, selectionArgForSelector(selector, model), opts, available)
1047+ selected, errPick = selector.Pick(selectorCtx, provider, selectionArgForSelector(selector, model), opts, selectorAuths)
10291048 if errPick != nil {
10301049 return nil, nil, errPick
10311050 }
@@ -1329,12 +1348,11 @@ func (m *Manager) pickNextMixedLegacy(ctx context.Context, providers []string, m · 297 unmodified lines
13291348 m.mu.RUnlock()
13301349 return nil, nil, "", &Error{Code: "auth_not_found", Message: "no auth available"}
13311350 }
1332− available, errAvailable := m.availableAuthsForRouteModel(candidates, "mixed", model, time.Now())
1351+ available, selectorAuths, errAvailable := m.availableAuthsForSelector(selector, candidates, "mixed", model, time.Now())
13331352 if errAvailable != nil {
13341353 m.mu.RUnlock()
13351354 return nil, nil, "", errAvailable
13361355 }
1337− available = cloneAuthSlice(available)
13381356 m.mu.RUnlock()
13391357
13401358 selected, handled, errPick := m.pickViaPluginScheduler(ctx, pluginScheduler, "mixed", providers, model, opts, tried, available)
@@ -1343,7 +1361,7 @@ func (m *Manager) pickNextMixedLegacy(ctx context.Context, providers []string, m · 2 unmodified lines
13431361 }
13441362 if !handled {
13451363 selectorCtx := withWeightedSelectorStateModel(ctx, selector, model)
1346− selected, errPick = selector.Pick(selectorCtx, "mixed", selectionArgForSelector(selector, model), opts, available)
1364+ selected, errPick = selector.Pick(selectorCtx, "mixed", selectionArgForSelector(selector, model), opts, selectorAuths)
13471365 if errPick != nil {
13481366 return nil, nil, "", errPick
13491367 }
sdk/cliproxy/auth/selector.go+90−17
@@ -269,6 +269,14 @@ func collectAvailableByPriority(auths []*Auth, model string, now time.Time) (ava · 268 unmodified lines
269269 }
270270
271271 func getAvailableAuths(auths []*Auth, provider, model string, now time.Time) ([]*Auth, error) {
272+ return getAvailableAuthsWithPriorityMode(auths, provider, model, now, false)
273+}
274+
275+func getAvailableAuthsAcrossPriorities(auths []*Auth, provider, model string, now time.Time) ([]*Auth, error) {
276+ return getAvailableAuthsWithPriorityMode(auths, provider, model, now, true)
277+}
278+
279+func getAvailableAuthsWithPriorityMode(auths []*Auth, provider, model string, now time.Time, allPriorities bool) ([]*Auth, error) {
272280 if len(auths) == 0 {
273281 return nil, &Error{Code: "auth_not_found", Message: "no auth candidates"}
274282 }
@@ -289,20 +297,73 @@ func getAvailableAuths(auths []*Auth, provider, model string, now time.Time) ([] · 14 unmodified lines
289297 return nil, &Error{Code: "auth_unavailable", Message: "no auth available"}
290298 }
291299
300+ return availableAuthsFromPriorityBuckets(availableByPriority, allPriorities), nil
301+}
302+
303+// availableAuthsFromPriorityBuckets flattens availability buckets into a stable, ID-sorted slice.
304+// When allPriorities is false only the highest available priority tier is returned.
305+// When allPriorities is true every tier is merged, so the result carries no priority ordering:
306+// use it for membership checks or feed it to highestPriorityAuths, never as a priority-ordered
307+// selection order.
308+func availableAuthsFromPriorityBuckets(availableByPriority map[int][]*Auth, allPriorities bool) []*Auth {
309+ var candidates []*Auth
310+ if allPriorities {
311+ total := 0
312+ for _, bucket := range availableByPriority {
313+ total += len(bucket)
314+ }
315+ candidates = make([]*Auth, 0, total)
316+ for _, bucket := range availableByPriority {
317+ candidates = append(candidates, bucket...)
318+ }
319+ } else {
320+ bestPriority := 0
321+ found := false
322+ for priority := range availableByPriority {
323+ if !found || priority > bestPriority {
324+ bestPriority = priority
325+ found = true
326+ }
327+ }
328+ bucket := availableByPriority[bestPriority]
329+ candidates = make([]*Auth, 0, len(bucket))
330+ candidates = append(candidates, bucket...)
331+ }
332+ if len(candidates) > 1 {
333+ sort.Slice(candidates, func(i, j int) bool { return candidates[i].ID < candidates[j].ID })
334+ }
335+ return candidates
336+}
337+
338+// highestPriorityAuths narrows an availability slice to its highest priority tier while
339+// preserving the input order. The input slice is returned unchanged when every candidate
340+// already shares the highest priority, so the common single-tier case allocates nothing.
341+func highestPriorityAuths(auths []*Auth) []*Auth {
342+ if len(auths) <= 1 {
343+ return auths
344+ }
292345 bestPriority := 0
293− found := false
294− for priority := range availableByPriority {
295− if !found || priority > bestPriority {
346+ bestCount := 0
347+ for _, auth := range auths {
348+ priority := authPriority(auth)
349+ switch {
350+ case bestCount == 0 || priority > bestPriority:
296351 bestPriority = priority
297− found = true
352+ bestCount = 1
353+ case priority == bestPriority:
354+ bestCount++
298355 }
299356 }
300−
301− available := availableByPriority[bestPriority]
302− if len(available) > 1 {
303− sort.Slice(available, func(i, j int) bool { return available[i].ID < available[j].ID })
357+ if bestCount == len(auths) {
358+ return auths
359+ }
360+ highest := make([]*Auth, 0, bestCount)
361+ for _, auth := range auths {
362+ if authPriority(auth) == bestPriority {
363+ highest = append(highest, auth)
364+ }
304365 }
305− return available, nil
366+ return highest
306367 }
307368
308369 // Pick selects the next available auth for the provider in a round-robin manner.
@@ -567,26 +628,38 @@ func NewSessionAffinitySelectorWithConfig(cfg SessionAffinityConfig) *SessionAff · 258 unmodified lines
567628 // Explicit Claude Code, Codex, OpenCode, pi, and request-body session signals
568629 // precede execution metadata, stable derived identity, and the legacy hash fallback.
569630 //
631+// An established binding outranks credential priority: a bound credential that is still
632+// available is reused even when a higher-priority credential recovers. Credential priority
633+// applies to cold bindings, requests without a session, and genuine bound-credential
634+// failover, so the fallback selector only ever receives the highest available priority tier.
635+//
570636 // Note: The cache key includes provider, session ID, and model to handle cases where
571637 // a session uses multiple models (e.g., gemini-2.5-pro and gemini-3-flash-preview)
572638 // that may be supported by different auth credentials, and to avoid cross-provider conflicts.
573639 func (s *SessionAffinitySelector) Pick(ctx context.Context, provider, model string, opts cliproxyexecutor.Options, auths []*Auth) (*Auth, error) {
574640 entry := selectorLogEntry(ctx)
575641 primaryID, fallbackID := extractSessionIDs(opts.Headers, opts.OriginalRequest, opts.Metadata)
576− if primaryID == "" {
577− entry.Debugf("session-affinity: no session ID extracted, falling back to default selector | provider=%s model=%s", provider, model)
578− return s.fallback.Pick(ctx, provider, model, opts, auths)
579− }
580−
581642 now := time.Now()
582643 availabilityCandidates := auths
583644 if _, weighted := s.fallback.(*WeightedRoundRobinSelector); weighted {
584645 availabilityCandidates = positiveWeightAuths(auths)
585646 }
586− available, err := getAvailableAuths(availabilityCandidates, provider, model, now)
647+ if primaryID == "" {
648+ fallbackAuths, errAvailable := getAvailableAuths(availabilityCandidates, provider, model, now)
649+ if errAvailable != nil {
650+ return nil, errAvailable
651+ }
652+ entry.Debugf("session-affinity: no session ID extracted, falling back to default selector | provider=%s model=%s", provider, model)
653+ return s.fallback.Pick(ctx, provider, model, opts, fallbackAuths)
654+ }
655+
656+ // A single availability pass serves both lookups: the bound credential is validated against
657+ // every priority tier, while the fallback selector keeps seeing only the highest tier.
658+ available, err := getAvailableAuthsAcrossPriorities(availabilityCandidates, provider, model, now)
587659 if err != nil {
588660 return nil, err
589661 }
662+ fallbackAuths := highestPriorityAuths(available)
590663
591664 cacheKey := provider + "::" + primaryID + "::" + model
592665 fallbackKey := ""
@@ -610,7 +683,7 @@ func (s *SessionAffinitySelector) Pick(ctx context.Context, provider, model stri · 17 unmodified lines
610683 }
611684 }
612685 // Cached auth not available, reselect via fallback selector for even distribution
613− auth, err := s.fallback.Pick(ctx, provider, model, opts, auths)
686+ auth, err := s.fallback.Pick(ctx, provider, model, opts, fallbackAuths)
614687 if err != nil {
615688 return nil, err
616689 }
@@ -631,7 +704,7 @@ func (s *SessionAffinitySelector) Pick(ctx context.Context, provider, model stri · 14 unmodified lines
631704 }
632705 }
633706
634− auth, err := s.fallback.Pick(ctx, provider, model, opts, auths)
707+ auth, err := s.fallback.Pick(ctx, provider, model, opts, fallbackAuths)
635708 if err != nil {
636709 return nil, err
637710 }
sdk/cliproxy/auth/session_affinity_priority_test.go+178−0
@@ -0,0 +1,178 @@
1+package auth
2+
3+import (
4+ "context"
5+ "net/http"
6+ "testing"
7+ "time"
8+
9+ "github.com/router-for-me/CLIProxyAPI/v7/internal/registry"
10+ cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor"
11+)
12+
13+func TestManagerSessionAffinityPreservesBindingAcrossHigherPriorityRecovery(t *testing.T) {
14+ for _, testCase := range []struct {
15+ name string
16+ providerSuffix string
17+ pick func(*Manager, context.Context, string, string, cliproxyexecutor.Options) (*Auth, error)
18+ }{
19+ {
20+ name: "single provider",
21+ providerSuffix: "single",
22+ pick: func(manager *Manager, ctx context.Context, provider, model string, opts cliproxyexecutor.Options) (*Auth, error) {
23+ auth, _, errPick := manager.pickNext(ctx, provider, model, opts, nil)
24+ return auth, errPick
25+ },
26+ },
27+ {
28+ name: "mixed provider",
29+ providerSuffix: "mixed",
30+ pick: func(manager *Manager, ctx context.Context, provider, model string, opts cliproxyexecutor.Options) (*Auth, error) {
31+ auth, _, _, errPick := manager.pickNextMixed(ctx, []string{provider}, model, opts, nil)
32+ return auth, errPick
33+ },
34+ },
35+ } {
36+ t.Run(testCase.name, func(t *testing.T) {
37+ ctx := context.Background()
38+ provider := "affinity-priority-" + testCase.providerSuffix
39+ model := "affinity-priority-model"
40+ highID := provider + "-high"
41+ lowID := provider + "-low"
42+
43+ manager := NewManager(nil, nil, nil)
44+ affinity := NewSessionAffinitySelectorWithConfig(SessionAffinityConfig{
45+ Fallback: &RoundRobinSelector{},
46+ TTL: time.Hour,
47+ })
48+ defer affinity.Stop()
49+ manager.SetSelector(affinity)
50+ manager.RegisterExecutor(schedulerTestExecutor{provider: provider})
51+
52+ for _, auth := range []*Auth{
53+ {ID: highID, Provider: provider, Status: StatusActive, Attributes: map[string]string{"priority": "1"}},
54+ {ID: lowID, Provider: provider, Status: StatusActive, Attributes: map[string]string{"priority": "0"}},
55+ } {
56+ if _, errRegister := manager.Register(WithSkipPersist(ctx), auth); errRegister != nil {
57+ t.Fatalf("Register(%s): %v", auth.ID, errRegister)
58+ }
59+ registry.GetGlobalRegistry().RegisterClient(auth.ID, provider, []*registry.ModelInfo{{ID: model}})
60+ t.Cleanup(func() { registry.GetGlobalRegistry().UnregisterClient(auth.ID) })
61+ }
62+
63+ opts := cliproxyexecutor.Options{Metadata: map[string]any{
64+ cliproxyexecutor.DerivedSessionIDMetadataKey: "stable-session",
65+ }}
66+ pick := func(pickOpts cliproxyexecutor.Options) *Auth {
67+ t.Helper()
68+ auth, errPick := testCase.pick(manager, ctx, provider, model, pickOpts)
69+ if errPick != nil {
70+ t.Fatalf("pick: %v", errPick)
71+ }
72+ if auth == nil {
73+ t.Fatal("pick returned nil auth")
74+ }
75+ return auth
76+ }
77+
78+ if got := pick(opts); got.ID != highID {
79+ t.Fatalf("cold binding = %q, want high priority %q", got.ID, highID)
80+ }
81+
82+ manager.MarkResult(ctx, Result{
83+ AuthID: highID,
84+ Provider: provider,
85+ Model: model,
86+ Success: false,
87+ Error: &Error{HTTPStatus: http.StatusTooManyRequests, Message: "quota"},
88+ })
89+ if got := pick(opts); got.ID != lowID {
90+ t.Fatalf("failover binding = %q, want %q", got.ID, lowID)
91+ }
92+
93+ expireSessionAffinityPriorityModelCooldown(t, manager, highID, model)
94+ if got := pick(opts); got.ID != lowID {
95+ t.Fatalf("binding after higher-priority recovery = %q, want sticky %q", got.ID, lowID)
96+ }
97+
98+ newSessionOpts := cliproxyexecutor.Options{Metadata: map[string]any{
99+ cliproxyexecutor.DerivedSessionIDMetadataKey: "new-session",
100+ }}
101+ if got := pick(newSessionOpts); got.ID != highID {
102+ t.Fatalf("cold binding for new session = %q, want high priority %q", got.ID, highID)
103+ }
104+
105+ manager.MarkResult(ctx, Result{
106+ AuthID: lowID,
107+ Provider: provider,
108+ Model: model,
109+ Success: false,
110+ Error: &Error{HTTPStatus: http.StatusTooManyRequests, Message: "quota"},
111+ })
112+ if got := pick(opts); got.ID != highID {
113+ t.Fatalf("binding after bound auth became unavailable = %q, want %q", got.ID, highID)
114+ }
115+ })
116+ }
117+}
118+
119+func TestSessionAffinityFallbackOnlyReceivesHighestAvailablePriority(t *testing.T) {
120+ selector := NewSessionAffinitySelectorWithConfig(SessionAffinityConfig{
121+ Fallback: lastAuthSelector{},
122+ TTL: time.Hour,
123+ })
124+ defer selector.Stop()
125+
126+ high := &Auth{ID: "a-high", Provider: "test", Status: StatusActive, Attributes: map[string]string{"priority": "1"}}
127+ low := &Auth{ID: "z-low", Provider: "test", Status: StatusActive, Attributes: map[string]string{"priority": "0"}}
128+ auths := []*Auth{high, low}
129+ opts := cliproxyexecutor.Options{Metadata: map[string]any{
130+ cliproxyexecutor.DerivedSessionIDMetadataKey: "stable-session",
131+ }}
132+
133+ assertPick := func(label string, pickOpts cliproxyexecutor.Options, wantID string) {
134+ t.Helper()
135+ got, errPick := selector.Pick(context.Background(), "test", "model", pickOpts, auths)
136+ if errPick != nil {
137+ t.Fatalf("%s: %v", label, errPick)
138+ }
139+ if got == nil {
140+ t.Fatalf("%s = nil, want %q", label, wantID)
141+ }
142+ if got.ID != wantID {
143+ t.Fatalf("%s = %q, want %q", label, got.ID, wantID)
144+ }
145+ }
146+
147+ assertPick("cold binding", opts, high.ID)
148+ assertPick("no-session fallback", cliproxyexecutor.Options{}, high.ID)
149+
150+ high.Unavailable = true
151+ assertPick("fallback after bound auth became unavailable", opts, low.ID)
152+}
153+
154+type lastAuthSelector struct{}
155+
156+func (lastAuthSelector) Pick(_ context.Context, _, _ string, _ cliproxyexecutor.Options, auths []*Auth) (*Auth, error) {
157+ if len(auths) == 0 {
158+ return nil, &Error{Code: "auth_not_found", Message: "no auth candidates"}
159+ }
160+ return auths[len(auths)-1], nil
161+}
162+
163+func expireSessionAffinityPriorityModelCooldown(t *testing.T, manager *Manager, authID, model string) {
164+ t.Helper()
165+ manager.mu.Lock()
166+ defer manager.mu.Unlock()
167+ auth := manager.auths[authID]
168+ if auth == nil {
169+ t.Fatalf("auth %q not found", authID)
170+ }
171+ state := auth.ModelStates[model]
172+ if state == nil {
173+ t.Fatalf("model state %q not found for auth %q", model, authID)
174+ }
175+ expired := time.Now().Add(-time.Second)
176+ state.NextRetryAfter = expired
177+ state.Quota.NextRecoverAt = expired
178+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t