Skip to content

feat(translate): normalize tool order and escape cooling providers - #591

Closed
guyu233 wants to merge 2 commits into
caigee-cmd:mainfrom
guyu233:feat/prompt-cache-tools
Closed

guyu233 wants to merge 2 commits into
caigee-cmd:mainfrom
guyu233:feat/prompt-cache-tools

Conversation

@guyu233

@guyu233 guyu233 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Two remaining cache-hit leakers, stacked on #590.

  • 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, returns the input byte-identical when it is already sorted, and leaves empty / invalid / 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 2 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 the sticky-escape marker so a successful turn does not steal the anchor while the bound account is merely cooling. Short cooldowns still return a Retry-After so the chat keeps its warm cache.

Tests

  • go build ./...
  • go test ./...
  • go test -race ./internal/executor ./internal/translate
  • go vet ./...
  • git diff --check
  • python scripts/release-notes.py validate

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

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.
@guyu233 guyu233 closed this by deleting the head repository Nov 24, 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