Skip to content

fix(auth): mirror session affinity retention and sticky fallback - #197

Closed
warelik wants to merge 1 commit into
kaitranntt:mainfrom
warelik:fix/session-affinity-sticky-fallback
Closed

fix(auth): mirror session affinity retention and sticky fallback#197
warelik wants to merge 1 commit into
kaitranntt:mainfrom
warelik:fix/session-affinity-sticky-fallback

Conversation

@warelik

@warelik warelik commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Mirror upstream CLIProxyAPI PR #5109 (session affinity retention across transient errors and sticky fallback during cooldown).

Problem

Previously, transient errors (HTTP 5xx, 429 rate limits, request timeouts, and Cloudflare challenge responses) purged session-affinity bindings from cache or applied a blanket 5-second session quarantine. This evicted warm prompt caches prematurely and caused unnecessary credential rotations across requests. Furthermore, during credential cooldown, fallback selection did not maintain stickiness across requests sharing session aliases.

Fix

  • Retain affinity on transient errors: Only terminal errors (401, 402, 403, 404, invalid_grant, model_not_supported) unbind sessions from cache. Transient errors (5xx, 429, 408, Cloudflare challenges) retain affinity so requests return to warm prompt caches once cooldown or rate limits clear.
  • Sticky temporary fallback: Introduce fallbackCache in SessionAffinitySelector to provide sticky fallback credentials across session aliases during primary cooldown.
  • Automatic recovery: When the primary credential recovers from cooldown, temporary fallback bindings are invalidated and traffic seamlessly returns to the primary credential.
  • Model namespace isolation: Prevent model fallback suffix collisions in temporary fallback cache.

Testing

  • Ported 9 regression tests in sdk/cliproxy/auth/session_affinity_retention_test.go:
    • TestSessionAffinity_Transient503RetainsBindingAcrossRecovery
    • TestSessionAffinity_Transient429RetryAfterRetainsBindingAcrossRecovery
    • TestSessionAffinity_Terminal401InvalidAPIKeyUnbindsSession
    • TestSessionAffinitySelector_OnResult_TransientVsTerminalClassification
    • TestSessionAffinity_StickyTemporaryFallbackDuringPrimaryCooldown
    • TestSessionAffinity_StickyTemporaryFallbackTTLExpiry
    • TestSessionAffinity_StickyTemporaryFallbackSharedAcrossAliases
    • TestSessionAffinity_StickyTemporaryFallbackSecondaryBranch
    • TestSessionAffinity_ModelFallbackSuffixDoesNotCollideWithTemporaryFallback
  • Full test suite passed: go test -count=1 ./...
  • Lint/vet clean: go vet ./sdk/cliproxy/auth/...

Mirror

Retain session affinity across transient errors (5xx, 429, timeouts,
Cloudflare challenges) so requests return to warm prompt caches upon
recovery. Maintain sticky fallback during primary cooldown and clear
temporary fallback bindings when the primary credential recovers.

Mirror upstream CLIProxyAPI PR #5109 (commits 5a4f2086, 31f1ff8c,
15b22675, f174bf68).
@warelik

warelik commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

Closing this PR — the branch is the wrong shape for this repository and should not be merged as-is.

What this branch actually does against main:

149	201	sdk/cliproxy/auth/selector.go
1	303	sdk/cliproxy/auth/selector_review_p2_test.go
38	707	sdk/cliproxy/auth/selector_test.go
0	938	sdk/cliproxy/auth/session_affinity_fix_test.go
3	3	sdk/cliproxy/auth/session_affinity_metadata_test.go
12	34	sdk/cliproxy/auth/session_affinity_priority_test.go
805	0	sdk/cliproxy/auth/session_affinity_retention_test.go
37	208	sdk/cliproxy/auth/session_cache.go
8 files changed, 1045 insertions(+), 2394 deletions(-)

It deletes session_affinity_fix_test.go in full (938 lines) and 707 lines of selector_test.go, i.e. it removes this repository's own session-affinity coverage and substitutes a differently-shaped implementation. That is a revert of existing work dressed up as a fix, not a targeted improvement, and no reviewer should be asked to accept it.

Context on how this happened: the change was derived from the equivalent work in the upstream repository (router-for-me/CLIProxyAPI#5109). This repository's sdk/cliproxy/auth/selector.go and session_cache.go have diverged substantially from upstream (339 and 248 differing lines at the merge base respectively), and this repository already carries its own session-affinity fix and its own test suite for it, which upstream does not have. Porting the upstream patch on top of that divergence produces exactly the destructive diff above.

Next step on our side: verify against this repository's current main whether a genuine session-affinity retention defect exists here at all — specifically whether a session binding survives a transient 429/503 and is dropped on a terminal 401/invalid-key. If a real defect is demonstrated, we will open a new, narrowly-scoped PR that adds coverage rather than removing it, and that does not touch the existing session_affinity_fix_test.go expectations without a written justification. If no defect is demonstrated, nothing further is needed here.

Apologies for the noise.

@warelik warelik closed this Aug 20, 2026
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.

1 participant