Repository navigation
Conversation
Routing stability for cache affinity: - Retry a transient upstream hiccup once on the same account before spending a failover hop. An empty stream, a first-byte timeout, or a stream that dies before the first event usually recovers on the same worker, and switching accounts throws away the prompt cache. The retry has its own per-request budget, keeps the in-flight slot, and skips any non-transient failure. - Stop the exponential ladder from multiplying a cooldown the upstream declared itself. Transient failures are capped at 60s; a quota reset reported in the response body is used verbatim (>=1m, <=24h) instead of being clamped to the 10-minute rate-limit window, which would have retried a dead account every ten minutes. Durable failures (auth, unhinted rate limits) still escalate. - Keep a session binding through a short cooldown: failover no longer clears the anchor when the account is only cooling, and bindSession/CommitSession leave the anchor alone while its remaining cooldown is 5 minutes or less, so the next turn returns to the cache-warm account. - Default session-affinity TTL 1h -> 4h, with CLI2API_AFFINITY_TTL and CLI2API_AFFINITY_CAPACITY overrides (48h / 1M bindings hard caps). - Classify exhausted routing (503 no_accounts) and undecodable upstream bodies (502 upstream_decode_error) instead of leaking a bare 502 upstream_error. Observability, so the change can be measured: - SessionAffinityStats gains miss_reasons / escape_reasons distributions alongside the existing last-value fields. - RequestUsageTotals gains cache_hit_rate (hit / (hit + miss)), nil when no cache accounting was reported. - SeedComponents reports per-component session-seed hashes; Prepare logs them under CLI2API_AFFINITY_DEBUG so a chat that re-anchors every turn can be traced to a volatile system prefix or a changed first message.
Two remaining cache-hit leakers: - Normalize the tools array before hashing the session seed. Tool definitions are a set, but clients such as Claude Code and Cline rebuild the array per turn; an order change used to move the seed, scatter the chat across accounts and invalidate the upstream cached prefix. NormalizeToolsOrder sorts by tool name plus the compact body so identical sets hash identically, returns input byte-identical when already sorted, and leaves invalid/empty/unnamed input untouched. Payload reordering is opt-in via CLI2API_TOOLS_NORMALIZE because some models notice the array order; the fingerprint side is always on. - Escape a sticky provider binding when the whole provider is cooling for a real reason (quota, or a retry hint of two minutes or more) and a different provider family can serve the model. Previously only concurrency saturation could escape, so a quota-exhausted provider answered 429 while a healthy supplier sat idle. capacityOutsideProvider deliberately excludes same-family region hops (Qoder global/CN are isolated upstreams), and the escape keeps stickyEscape so a successful turn does not steal the anchor while the bound account is merely cooling; short cooldowns still return a Retry-After.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Two remaining cache-hit leakers, stacked on #590.
NormalizeToolsOrdersorts by tool name plus the compact body, returns the input byte-identical when it is already sorted, and leaves empty / invalid / unnamed input untouched. Payload reordering is opt-in viaCLI2API_TOOLS_NORMALIZEbecause some models notice the array order; the fingerprint side is always on.429while a healthy supplier sat idle.capacityOutsideProviderdeliberately excludes same-family region hops (Qoder global/CN are isolated upstreams), and the escape keeps the sticky-escape marker so a successful turn does not steal the anchor while the bound account is merely cooling. Short cooldowns still return aRetry-Afterso the chat keeps its warm cache.Tests
go build ./...go test ./...go test -race ./internal/executor ./internal/translatego vet ./...git diff --checkpython scripts/release-notes.py validateMerge order
Stacked on #590 — the first commit in this PR is #590's commit. Merge #590 first, then rebase this branch onto
main; the diff collapses to the 5 files that belong here.