Skip to content

fix(executor): retry transient failures in place and keep cache anchors - #590

Closed
guyu233 wants to merge 1 commit into
caigee-cmd:mainfrom
guyu233:fix/cache-affinity-resilience
Closed

guyu233 wants to merge 1 commit into
caigee-cmd:mainfrom
guyu233:fix/cache-affinity-resilience

Conversation

@guyu233

@guyu233 guyu233 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Routing stability for prompt-cache affinity: stop throwing away the warm account on failures that say nothing about the account, and stop inflating cooldowns the upstream already declared.

  • Retry a transient upstream hiccup once on the same account before spending a failover hop (empty stream, first-byte timeout, a stream that dies before the first event). The retry has its own per-request budget, keeps the in-flight slot, and never fires for 429 / quota / auth / invalid_request / client cancel.
  • Stop the exponential ladder from multiplying an upstream-declared cooldown. 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 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 while 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 instead of walking the pool.
  • Raise the default session-affinity TTL from 1h to 4h and expose CLI2API_AFFINITY_TTL / CLI2API_AFFINITY_CAPACITY (hard caps 48h and 1M bindings).
  • Classify exhausted routing as 503 no_accounts and an undecodable upstream body as 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 (last-value fields kept for older console builds), RequestUsageTotals gains cache_hit_rate (nil when no cache accounting was reported), and SeedComponents plus one extra affinity debug log line report per-component seed hashes, so a chat that re-anchors every turn can be traced to a volatile system prefix or a changed first message.

No schema change: all state is in-memory plus existing request-log aggregates, so there is no migration.

Tests

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

Merge order

Independent of other PRs in this series. The follow-up feat/prompt-cache-tools is stacked on this commit; rebase it after this lands.

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