Skip to content

fix(gateway): cache affinity and Anthropic message rectification - #510

Open
guyu233 wants to merge 2 commits into
caigee-cmd:mainfrom
guyu233:fix/cache-affinity-message-rectifier
Open

guyu233 wants to merge 2 commits into
caigee-cmd:mainfrom
guyu233:fix/cache-affinity-message-rectifier

Conversation

@guyu233

@guyu233 guyu233 commented Nov 13, 2026

Copy link
Copy Markdown

Summary

Port cache-stability and message-format fixes from the reference 2api gateways and cc-switch.

  • Conversation id aliases: recognise X-Conversation-Id, X-Session-Id, X-Thread-Id, X-CLI2API-Session and body conversation_id / conversationId / session_id / metadata.* aliases (Sliverkiss/workbuddy2api feat: add named client API keys with provider allowlists #35, qoder2api-hub session affinity). Turns stay on one account instead of rotating and losing the upstream prompt cache.

  • CLI2API_MSGSTAT_SHARDS (opt-in, default 0): maps one (API key, model, system prefix, tools) tuple to a small stable account set, mirroring workbuddy2api-hub WB_MSGSTAT_SHARDS. Avoids scattering one-shot /v1/messages calls across the whole pool.

  • CLI2API_AFFINITY_DEBUG (opt-in): structured logs separating route-level session affinity (header / conversation / shard / content-seed) from real upstream cache-token hits, with prompt/cache tokens and hit rate. No SQL migration changes.

  • Thinking rectifier (cc-switch-style, on by default): /v1/messages inbound cleanup of historical thinking / redacted_thinking blocks and stale signature fields. OpenAI-compatible upstreams do not validate Anthropic signatures; replaying them causes 400s and <thinking> scaffolding. Disable with CLI2API_THINKING_RECTIFIER=off.

  • WAF prefilter: skip fingerprint regexes when the request body contains no known trigger. Reduces per-request CPU on large histories and avoids rewriting cached prefixes.

Validation

  • go build ./... and go vet on touched packages: pass

  • go test -count=1 for gateway / translate / workbuddy / console / control / codex / command: pass

  • Executor session/shard/prepare targeted tests: pass (two unrelated pre-existing time-precision failures in pool_test.go and Windows file-lock failures in internal/store reproduce on clean origin/main)

  • Frontend npx tsc -b --force (TS 6.0.3), global tsc -p tsconfig.app.json --noEmit (TS 5.9.3), npx oxlint: pass

  • End-to-end against a freshly built binary: /v1/messages with a stale signed thinking block returns X-Cli2api-Rectified: thinking=1; a body conversationId produces session affinity ... source="conversation" and cache diagnostics under CLI2API_AFFINITY_DEBUG=1.

Switches are documented in deploy/README.md; bilingual changelog fragment included.

Port the cache-stability and message-format fixes from the reference 2api
gateways and cc-switch:

- Recognise conversation ids from headers and body metadata aliases
  (Sliverkiss/workbuddy2api caigee-cmd#35, qoder2api-hub session affinity), so turns
  stay on one account instead of rotating and losing the upstream prompt cache.
- Add opt-in CLI2API_MSGSTAT_SHARDS (workbuddy2api-hub WB_MSGSTAT_SHARDS):
  one (key, model, system prefix, tools) tuple maps to a small stable account
  set instead of being scattered by the first user message.
- Add opt-in CLI2API_AFFINITY_DEBUG diagnostics separating route-level
  session affinity from real upstream cache-token hits.
- Rectify historical Anthropic thinking / redacted_thinking blocks and stale
  signatures on /v1/messages (cc-switch Rectifier). Disable with
  CLI2API_THINKING_RECTIFIER=off.
- Skip WAF fingerprint regexes for bodies with no known trigger, avoiding
  needless CPU work on large histories and keeping cached prefixes stable.

No SQL migration changes; diagnostics are structured logs. New env switches
are documented in deploy/README.md. Add a bilingual changelog fragment.
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.

2 participants