History

fix(codex): restore prompt cache continuity for Codex requests

Prompt caching on Codex was not reliably reusable through the proxy because repeated chat-completions requests could reach the upstream without the same continuity envelope. In practice this showed up most clearly with OpenCode, where cache reads worked in the reference client but not through CLIProxyAPI, although the root cause is broader than OpenCode itself. The proxy was breaking continuity in several ways: executor-layer Codex request preparation stripped prompt_cache_retention, chat-completions translation did not preserve that field, continuity headers used a different shape than the working client behavior, and OpenAI-style Codex requests could be sent without a stable prompt_cache_key. When that happened, session_id fell back to a fresh random value per request, so upstream Codex treated repeated requests as unrelated turns instead of as part of the same cacheable context. This change fixes that by preserving caller-provided prompt_cache_retention on Codex execution paths, preserving prompt_cache_retention when translating OpenAI chat-completions requests to Codex, aligning Codex continuity headers to session_id, and introducing an explicit Codex continuity policy that derives a stable continuity key from the best available signal. The resolution order prefers an explicit prompt_cache_key, then execution session metadata, then an explicit idempotency key, then stable request-affinity metadata, then a stable client-principal hash, and finally a stable auth-ID hash when no better continuity signal exists. The same continuity key is applied to both prompt_cache_key in the request body and session_id in the request headers so repeated requests reuse the same upstream cache/session identity. The auth manager also keeps auth selection sticky for repeated request sequences, preventing otherwise-equivalent Codex requests from drifting across different upstream auth contexts and accidentally breaking cache reuse. To keep the implementation maintainable, the continuity resolution and diagnostics are centralized in a dedicated Codex continuity helper instead of being scattered across executor flow code. Regression coverage now verifies retention preservation, continuity-key precedence, stable auth-ID fallback, websocket parity, translator preservation, and auth-affinity behavior. Manual validation confirmed prompt cache reads now occur through CLIProxyAPI when using Codex via OpenCode, and the fix should also benefit other clients that rely on stable repeated Codex request continuity.

VooDisss committed 7 months ago

Commit 511b8a91 parent d42b5d410 files · +516 −37Browse files

Unified · Split

10 files
  1. internal/runtime/executor/codex_continuity.go +153 −0
  2. internal/runtime/executor/codex_executor.go +16 −19
  3. internal/runtime/executor/codex_executor_cache_test.go +95 −6
  4. internal/runtime/executor/codex_websockets_executor.go +6 −4
  5. internal/runtime/executor/codex_websockets_executor_test.go +25 −0
  6. internal/translator/codex/openai/chat-completions/codex_openai_request.go +3 −0
  7. internal/translator/codex/openai/chat-completions/codex_openai_request_test.go +16 −0
  8. sdk/api/handlers/handlers.go +13 −1
  9. sdk/cliproxy/auth/conductor.go +104 −7
  10. sdk/cliproxy/auth/conductor_affinity_test.go +85 −0
internal/runtime/executor/codex_continuity.go+153−0
@@ -0,0 +1,153 @@
1+package executor
2+
3+import (
4+ "context"
5+ "fmt"
6+ "net/http"
7+ "strings"
8+
9+ "github.com/google/uuid"
10+ cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
11+ cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
12+ log "github.com/sirupsen/logrus"
13+ "github.com/tidwall/gjson"
14+ "github.com/tidwall/sjson"
15+)
16+
17+const codexAuthAffinityMetadataKey = "auth_affinity_key"
18+
19+type codexContinuity struct {
20+ Key string
21+ Source string
22+}
23+
24+func resolveCodexContinuity(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) codexContinuity {
25+ if promptCacheKey := strings.TrimSpace(gjson.GetBytes(req.Payload, "prompt_cache_key").String()); promptCacheKey != "" {
26+ return codexContinuity{Key: promptCacheKey, Source: "prompt_cache_key"}
27+ }
28+ if opts.Metadata != nil {
29+ if raw, ok := opts.Metadata[cliproxyexecutor.ExecutionSessionMetadataKey]; ok && raw != nil {
30+ switch v := raw.(type) {
31+ case string:
32+ if trimmed := strings.TrimSpace(v); trimmed != "" {
33+ return codexContinuity{Key: trimmed, Source: "execution_session"}
34+ }
35+ case []byte:
36+ if trimmed := strings.TrimSpace(string(v)); trimmed != "" {
37+ return codexContinuity{Key: trimmed, Source: "execution_session"}
38+ }
39+ }
40+ }
41+ }
42+ if ginCtx := ginContextFrom(ctx); ginCtx != nil {
43+ if ginCtx.Request != nil {
44+ if v := strings.TrimSpace(ginCtx.GetHeader("Idempotency-Key")); v != "" {
45+ return codexContinuity{Key: v, Source: "idempotency_key"}
46+ }
47+ }
48+ if v, exists := ginCtx.Get("apiKey"); exists && v != nil {
49+ switch value := v.(type) {
50+ case string:
51+ if trimmed := strings.TrimSpace(value); trimmed != "" {
52+ return codexContinuity{Key: uuid.NewSHA1(uuid.NameSpaceOID, []byte("cli-proxy-api:codex:prompt-cache:"+trimmed)).String(), Source: "client_principal"}
53+ }
54+ case fmt.Stringer:
55+ if trimmed := strings.TrimSpace(value.String()); trimmed != "" {
56+ return codexContinuity{Key: uuid.NewSHA1(uuid.NameSpaceOID, []byte("cli-proxy-api:codex:prompt-cache:"+trimmed)).String(), Source: "client_principal"}
57+ }
58+ default:
59+ trimmed := strings.TrimSpace(fmt.Sprintf("%v", value))
60+ if trimmed != "" {
61+ return codexContinuity{Key: uuid.NewSHA1(uuid.NameSpaceOID, []byte("cli-proxy-api:codex:prompt-cache:"+trimmed)).String(), Source: "client_principal"}
62+ }
63+ }
64+ }
65+ }
66+ if opts.Metadata != nil {
67+ if raw, ok := opts.Metadata[codexAuthAffinityMetadataKey]; ok && raw != nil {
68+ switch v := raw.(type) {
69+ case string:
70+ if trimmed := strings.TrimSpace(v); trimmed != "" {
71+ return codexContinuity{Key: trimmed, Source: "auth_affinity"}
72+ }
73+ case []byte:
74+ if trimmed := strings.TrimSpace(string(v)); trimmed != "" {
75+ return codexContinuity{Key: trimmed, Source: "auth_affinity"}
76+ }
77+ }
78+ }
79+ }
80+ if auth != nil {
81+ if authID := strings.TrimSpace(auth.ID); authID != "" {
82+ return codexContinuity{Key: uuid.NewSHA1(uuid.NameSpaceOID, []byte("cli-proxy-api:codex:prompt-cache:auth:"+authID)).String(), Source: "auth_id"}
83+ }
84+ }
85+ return codexContinuity{}
86+}
87+
88+func applyCodexContinuityBody(rawJSON []byte, continuity codexContinuity) []byte {
89+ if continuity.Key == "" {
90+ return rawJSON
91+ }
92+ rawJSON, _ = sjson.SetBytes(rawJSON, "prompt_cache_key", continuity.Key)
93+ return rawJSON
94+}
95+
96+func applyCodexContinuityHeaders(headers http.Header, continuity codexContinuity) {
97+ if headers == nil || continuity.Key == "" {
98+ return
99+ }
100+ headers.Set("session_id", continuity.Key)
101+}
102+
103+func logCodexRequestDiagnostics(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, headers http.Header, body []byte, continuity codexContinuity) {
104+ if !log.IsLevelEnabled(log.DebugLevel) {
105+ return
106+ }
107+ entry := logWithRequestID(ctx)
108+ authID := ""
109+ authFile := ""
110+ if auth != nil {
111+ authID = strings.TrimSpace(auth.ID)
112+ authFile = strings.TrimSpace(auth.FileName)
113+ }
114+ selectedAuthID := ""
115+ executionSessionID := ""
116+ if opts.Metadata != nil {
117+ if raw, ok := opts.Metadata[cliproxyexecutor.SelectedAuthMetadataKey]; ok && raw != nil {
118+ switch v := raw.(type) {
119+ case string:
120+ selectedAuthID = strings.TrimSpace(v)
121+ case []byte:
122+ selectedAuthID = strings.TrimSpace(string(v))
123+ }
124+ }
125+ if raw, ok := opts.Metadata[cliproxyexecutor.ExecutionSessionMetadataKey]; ok && raw != nil {
126+ switch v := raw.(type) {
127+ case string:
128+ executionSessionID = strings.TrimSpace(v)
129+ case []byte:
130+ executionSessionID = strings.TrimSpace(string(v))
131+ }
132+ }
133+ }
134+ entry.Debugf(
135+ "codex request diagnostics auth_id=%s selected_auth_id=%s auth_file=%s exec_session=%s continuity_source=%s session_id=%s prompt_cache_key=%s prompt_cache_retention=%s store=%t has_instructions=%t reasoning_effort=%s reasoning_summary=%s chatgpt_account_id=%t originator=%s model=%s source_format=%s",
136+ authID,
137+ selectedAuthID,
138+ authFile,
139+ executionSessionID,
140+ continuity.Source,
141+ strings.TrimSpace(headers.Get("session_id")),
142+ gjson.GetBytes(body, "prompt_cache_key").String(),
143+ gjson.GetBytes(body, "prompt_cache_retention").String(),
144+ gjson.GetBytes(body, "store").Bool(),
145+ gjson.GetBytes(body, "instructions").Exists(),
146+ gjson.GetBytes(body, "reasoning.effort").String(),
147+ gjson.GetBytes(body, "reasoning.summary").String(),
148+ strings.TrimSpace(headers.Get("Chatgpt-Account-Id")) != "",
149+ strings.TrimSpace(headers.Get("Originator")),
150+ req.Model,
151+ opts.SourceFormat.String(),
152+ )
153+}
internal/runtime/executor/codex_executor.go+16−19
@@ -111,18 +111,18 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re · 110 unmodified lines
111111 body, _ = sjson.SetBytes(body, "model", baseModel)
112112 body, _ = sjson.SetBytes(body, "stream", true)
113113 body, _ = sjson.DeleteBytes(body, "previous_response_id")
114− body, _ = sjson.DeleteBytes(body, "prompt_cache_retention")
115114 body, _ = sjson.DeleteBytes(body, "safety_identifier")
116115 if !gjson.GetBytes(body, "instructions").Exists() {
117116 body, _ = sjson.SetBytes(body, "instructions", "")
118117 }
119118
120119 url := strings.TrimSuffix(baseURL, "/") + "/responses"
121− httpReq, err := e.cacheHelper(ctx, from, url, req, body)
120+ httpReq, continuity, err := e.cacheHelper(ctx, auth, from, url, req, opts, body)
122121 if err != nil {
123122 return resp, err
124123 }
125124 applyCodexHeaders(httpReq, auth, apiKey, true, e.cfg)
125+ logCodexRequestDiagnostics(ctx, auth, req, opts, httpReq.Header, body, continuity)
126126 var authID, authLabel, authType, authValue string
127127 if auth != nil {
128128 authID = auth.ID
@@ -222,11 +222,12 @@ func (e *CodexExecutor) executeCompact(ctx context.Context, auth *cliproxyauth.A · 93 unmodified lines
222222 body, _ = sjson.DeleteBytes(body, "stream")
223223
224224 url := strings.TrimSuffix(baseURL, "/") + "/responses/compact"
225− httpReq, err := e.cacheHelper(ctx, from, url, req, body)
225+ httpReq, continuity, err := e.cacheHelper(ctx, auth, from, url, req, opts, body)
226226 if err != nil {
227227 return resp, err
228228 }
229229 applyCodexHeaders(httpReq, auth, apiKey, false, e.cfg)
230+ logCodexRequestDiagnostics(ctx, auth, req, opts, httpReq.Header, body, continuity)
230231 var authID, authLabel, authType, authValue string
231232 if auth != nil {
232233 authID = auth.ID
@@ -309,7 +310,6 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au · 76 unmodified lines
309310 requestedModel := payloadRequestedModel(opts, req.Model)
310311 body = applyPayloadConfigWithRoot(e.cfg, baseModel, to.String(), "", body, originalTranslated, requestedModel)
311312 body, _ = sjson.DeleteBytes(body, "previous_response_id")
312− body, _ = sjson.DeleteBytes(body, "prompt_cache_retention")
313313 body, _ = sjson.DeleteBytes(body, "safety_identifier")
314314 body, _ = sjson.SetBytes(body, "model", baseModel)
315315 if !gjson.GetBytes(body, "instructions").Exists() {
@@ -317,11 +317,12 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au · 1 unmodified line
317317 }
318318
319319 url := strings.TrimSuffix(baseURL, "/") + "/responses"
320− httpReq, err := e.cacheHelper(ctx, from, url, req, body)
320+ httpReq, continuity, err := e.cacheHelper(ctx, auth, from, url, req, opts, body)
321321 if err != nil {
322322 return nil, err
323323 }
324324 applyCodexHeaders(httpReq, auth, apiKey, true, e.cfg)
325+ logCodexRequestDiagnostics(ctx, auth, req, opts, httpReq.Header, body, continuity)
325326 var authID, authLabel, authType, authValue string
326327 if auth != nil {
327328 authID = auth.ID
@@ -596,8 +597,9 @@ func (e *CodexExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (* · 268 unmodified lines
596597 return auth, nil
597598 }
598599
599−func (e *CodexExecutor) cacheHelper(ctx context.Context, from sdktranslator.Format, url string, req cliproxyexecutor.Request, rawJSON []byte) (*http.Request, error) {
600+func (e *CodexExecutor) cacheHelper(ctx context.Context, auth *cliproxyauth.Auth, from sdktranslator.Format, url string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, rawJSON []byte) (*http.Request, codexContinuity, error) {
600601 var cache codexCache
602+ continuity := codexContinuity{}
601603 if from == "claude" {
602604 userIDResult := gjson.GetBytes(req.Payload, "metadata.user_id")
603605 if userIDResult.Exists() {
@@ -615,25 +617,20 @@ func (e *CodexExecutor) cacheHelper(ctx context.Context, from sdktranslator.Form · 11 unmodified lines
615617 promptCacheKey := gjson.GetBytes(req.Payload, "prompt_cache_key")
616618 if promptCacheKey.Exists() {
617619 cache.ID = promptCacheKey.String()
620+ continuity = codexContinuity{Key: cache.ID, Source: "prompt_cache_key"}
618621 }
619622 } else if from == "openai" {
620− if apiKey := strings.TrimSpace(apiKeyFromContext(ctx)); apiKey != "" {
621− cache.ID = uuid.NewSHA1(uuid.NameSpaceOID, []byte("cli-proxy-api:codex:prompt-cache:"+apiKey)).String()
622− }
623+ continuity = resolveCodexContinuity(ctx, auth, req, opts)
624+ cache.ID = continuity.Key
623625 }
624626
625− if cache.ID != "" {
626− rawJSON, _ = sjson.SetBytes(rawJSON, "prompt_cache_key", cache.ID)
627− }
627+ rawJSON = applyCodexContinuityBody(rawJSON, continuity)
628628 httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(rawJSON))
629629 if err != nil {
630− return nil, err
631− }
632− if cache.ID != "" {
633− httpReq.Header.Set("Conversation_id", cache.ID)
634− httpReq.Header.Set("Session_id", cache.ID)
630+ return nil, continuity, err
635631 }
636− return httpReq, nil
632+ applyCodexContinuityHeaders(httpReq.Header, continuity)
633+ return httpReq, continuity, nil
637634 }
638635
639636 func applyCodexHeaders(r *http.Request, auth *cliproxyauth.Auth, token string, stream bool, cfg *config.Config) {
@@ -646,7 +643,7 @@ func applyCodexHeaders(r *http.Request, auth *cliproxyauth.Auth, token string, s · 6 unmodified lines
646643 }
647644
648645 misc.EnsureHeader(r.Header, ginHeaders, "Version", "")
649− misc.EnsureHeader(r.Header, ginHeaders, "Session_id", uuid.NewString())
646+ misc.EnsureHeader(r.Header, ginHeaders, "session_id", uuid.NewString())
650647 misc.EnsureHeader(r.Header, ginHeaders, "X-Codex-Turn-Metadata", "")
651648 misc.EnsureHeader(r.Header, ginHeaders, "X-Client-Request-Id", "")
652649 cfgUserAgent, _ := codexHeaderDefaults(cfg, auth)
internal/runtime/executor/codex_executor_cache_test.go+95−6
@@ -8,6 +8,7 @@ import ( · 7 unmodified lines
88
99 "github.com/gin-gonic/gin"
1010 "github.com/google/uuid"
11+ cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
1112 cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
1213 sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
1314 "github.com/tidwall/gjson"
@@ -27,7 +28,7 @@ func TestCodexExecutorCacheHelper_OpenAIChatCompletions_StablePromptCacheKeyFrom · 13 unmodified lines
2728 }
2829 url := "https://example.com/responses"
2930
30− httpReq, err := executor.cacheHelper(ctx, sdktranslator.FromString("openai"), url, req, rawJSON)
31+ httpReq, _, err := executor.cacheHelper(ctx, nil, sdktranslator.FromString("openai"), url, req, cliproxyexecutor.Options{}, rawJSON)
3132 if err != nil {
3233 t.Fatalf("cacheHelper error: %v", err)
3334 }
@@ -42,14 +43,14 @@ func TestCodexExecutorCacheHelper_OpenAIChatCompletions_StablePromptCacheKeyFrom · 8 unmodified lines
4243 if gotKey != expectedKey {
4344 t.Fatalf("prompt_cache_key = %q, want %q", gotKey, expectedKey)
4445 }
45− if gotConversation := httpReq.Header.Get("Conversation_id"); gotConversation != expectedKey {
46− t.Fatalf("Conversation_id = %q, want %q", gotConversation, expectedKey)
46+ if gotSession := httpReq.Header.Get("session_id"); gotSession != expectedKey {
47+ t.Fatalf("session_id = %q, want %q", gotSession, expectedKey)
4748 }
48− if gotSession := httpReq.Header.Get("Session_id"); gotSession != expectedKey {
49− t.Fatalf("Session_id = %q, want %q", gotSession, expectedKey)
49+ if got := httpReq.Header.Get("Conversation_id"); got != "" {
50+ t.Fatalf("Conversation_id = %q, want empty", got)
5051 }
5152
52− httpReq2, err := executor.cacheHelper(ctx, sdktranslator.FromString("openai"), url, req, rawJSON)
53+ httpReq2, _, err := executor.cacheHelper(ctx, nil, sdktranslator.FromString("openai"), url, req, cliproxyexecutor.Options{}, rawJSON)
5354 if err != nil {
5455 t.Fatalf("cacheHelper error (second call): %v", err)
5556 }
@@ -62,3 +63,91 @@ func TestCodexExecutorCacheHelper_OpenAIChatCompletions_StablePromptCacheKeyFrom · 6 unmodified lines
6263 t.Fatalf("prompt_cache_key (second call) = %q, want %q", gotKey2, expectedKey)
6364 }
6465 }
66+
67+func TestCodexExecutorCacheHelper_OpenAIResponses_PreservesPromptCacheRetention(t *testing.T) {
68+ executor := &CodexExecutor{}
69+ url := "https://example.com/responses"
70+ req := cliproxyexecutor.Request{
71+ Model: "gpt-5.3-codex",
72+ Payload: []byte(`{"model":"gpt-5.3-codex","prompt_cache_key":"cache-key-1","prompt_cache_retention":"persistent"}`),
73+ }
74+ rawJSON := []byte(`{"model":"gpt-5.3-codex","stream":true,"prompt_cache_retention":"persistent"}`)
75+
76+ httpReq, _, err := executor.cacheHelper(context.Background(), nil, sdktranslator.FromString("openai-response"), url, req, cliproxyexecutor.Options{}, rawJSON)
77+ if err != nil {
78+ t.Fatalf("cacheHelper error: %v", err)
79+ }
80+
81+ body, err := io.ReadAll(httpReq.Body)
82+ if err != nil {
83+ t.Fatalf("read request body: %v", err)
84+ }
85+
86+ if got := gjson.GetBytes(body, "prompt_cache_key").String(); got != "cache-key-1" {
87+ t.Fatalf("prompt_cache_key = %q, want %q", got, "cache-key-1")
88+ }
89+ if got := gjson.GetBytes(body, "prompt_cache_retention").String(); got != "persistent" {
90+ t.Fatalf("prompt_cache_retention = %q, want %q", got, "persistent")
91+ }
92+ if got := httpReq.Header.Get("session_id"); got != "cache-key-1" {
93+ t.Fatalf("session_id = %q, want %q", got, "cache-key-1")
94+ }
95+ if got := httpReq.Header.Get("Conversation_id"); got != "" {
96+ t.Fatalf("Conversation_id = %q, want empty", got)
97+ }
98+}
99+
100+func TestCodexExecutorCacheHelper_OpenAIChatCompletions_UsesExecutionSessionForContinuity(t *testing.T) {
101+ executor := &CodexExecutor{}
102+ rawJSON := []byte(`{"model":"gpt-5.4","stream":true}`)
103+ req := cliproxyexecutor.Request{
104+ Model: "gpt-5.4",
105+ Payload: []byte(`{"model":"gpt-5.4"}`),
106+ }
107+ opts := cliproxyexecutor.Options{Metadata: map[string]any{cliproxyexecutor.ExecutionSessionMetadataKey: "exec-session-1"}}
108+
109+ httpReq, _, err := executor.cacheHelper(context.Background(), nil, sdktranslator.FromString("openai"), "https://example.com/responses", req, opts, rawJSON)
110+ if err != nil {
111+ t.Fatalf("cacheHelper error: %v", err)
112+ }
113+
114+ body, err := io.ReadAll(httpReq.Body)
115+ if err != nil {
116+ t.Fatalf("read request body: %v", err)
117+ }
118+
119+ if got := gjson.GetBytes(body, "prompt_cache_key").String(); got != "exec-session-1" {
120+ t.Fatalf("prompt_cache_key = %q, want %q", got, "exec-session-1")
121+ }
122+ if got := httpReq.Header.Get("session_id"); got != "exec-session-1" {
123+ t.Fatalf("session_id = %q, want %q", got, "exec-session-1")
124+ }
125+}
126+
127+func TestCodexExecutorCacheHelper_OpenAIChatCompletions_FallsBackToStableAuthID(t *testing.T) {
128+ executor := &CodexExecutor{}
129+ rawJSON := []byte(`{"model":"gpt-5.4","stream":true}`)
130+ req := cliproxyexecutor.Request{
131+ Model: "gpt-5.4",
132+ Payload: []byte(`{"model":"gpt-5.4"}`),
133+ }
134+ auth := &cliproxyauth.Auth{ID: "codex-auth-1", Provider: "codex"}
135+
136+ httpReq, _, err := executor.cacheHelper(context.Background(), auth, sdktranslator.FromString("openai"), "https://example.com/responses", req, cliproxyexecutor.Options{}, rawJSON)
137+ if err != nil {
138+ t.Fatalf("cacheHelper error: %v", err)
139+ }
140+
141+ body, err := io.ReadAll(httpReq.Body)
142+ if err != nil {
143+ t.Fatalf("read request body: %v", err)
144+ }
145+
146+ expected := uuid.NewSHA1(uuid.NameSpaceOID, []byte("cli-proxy-api:codex:prompt-cache:auth:codex-auth-1")).String()
147+ if got := gjson.GetBytes(body, "prompt_cache_key").String(); got != expected {
148+ t.Fatalf("prompt_cache_key = %q, want %q", got, expected)
149+ }
150+ if got := httpReq.Header.Get("session_id"); got != expected {
151+ t.Fatalf("session_id = %q, want %q", got, expected)
152+ }
153+}
internal/runtime/executor/codex_websockets_executor.go+6−4
@@ -178,7 +178,6 @@ func (e *CodexWebsocketsExecutor) Execute(ctx context.Context, auth *cliproxyaut · 177 unmodified lines
178178 body, _ = sjson.SetBytes(body, "model", baseModel)
179179 body, _ = sjson.SetBytes(body, "stream", true)
180180 body, _ = sjson.DeleteBytes(body, "previous_response_id")
181− body, _ = sjson.DeleteBytes(body, "prompt_cache_retention")
182181 body, _ = sjson.DeleteBytes(body, "safety_identifier")
183182 if !gjson.GetBytes(body, "instructions").Exists() {
184183 body, _ = sjson.SetBytes(body, "instructions", "")
@@ -191,6 +190,7 @@ func (e *CodexWebsocketsExecutor) Execute(ctx context.Context, auth *cliproxyaut · 6 unmodified lines
191190 }
192191
193192 body, wsHeaders := applyCodexPromptCacheHeaders(from, req, body)
193+ continuity := codexContinuity{Key: strings.TrimSpace(wsHeaders.Get("session_id"))}
194194 wsHeaders = applyCodexWebsocketHeaders(ctx, wsHeaders, auth, apiKey, e.cfg)
195195
196196 var authID, authLabel, authType, authValue string
@@ -209,6 +209,7 @@ func (e *CodexWebsocketsExecutor) Execute(ctx context.Context, auth *cliproxyaut · 12 unmodified lines
209209 }
210210
211211 wsReqBody := buildCodexWebsocketRequestBody(body)
212+ logCodexRequestDiagnostics(ctx, auth, req, opts, wsHeaders, body, continuity)
212213 recordAPIRequest(ctx, e.cfg, upstreamRequestLog{
213214 URL: wsURL,
214215 Method: "WEBSOCKET",
@@ -386,6 +387,7 @@ func (e *CodexWebsocketsExecutor) ExecuteStream(ctx context.Context, auth *clipr · 171 unmodified lines
386387 }
387388
388389 body, wsHeaders := applyCodexPromptCacheHeaders(from, req, body)
390+ continuity := codexContinuity{Key: strings.TrimSpace(wsHeaders.Get("session_id"))}
389391 wsHeaders = applyCodexWebsocketHeaders(ctx, wsHeaders, auth, apiKey, e.cfg)
390392
391393 var authID, authLabel, authType, authValue string
@@ -403,6 +405,7 @@ func (e *CodexWebsocketsExecutor) ExecuteStream(ctx context.Context, auth *clipr · 11 unmodified lines
403405 }
404406
405407 wsReqBody := buildCodexWebsocketRequestBody(body)
408+ logCodexRequestDiagnostics(ctx, auth, req, opts, wsHeaders, body, continuity)
406409 recordAPIRequest(ctx, e.cfg, upstreamRequestLog{
407410 URL: wsURL,
408411 Method: "WEBSOCKET",
@@ -790,8 +793,7 @@ func applyCodexPromptCacheHeaders(from sdktranslator.Format, req cliproxyexecuto · 381 unmodified lines
790793
791794 if cache.ID != "" {
792795 rawJSON, _ = sjson.SetBytes(rawJSON, "prompt_cache_key", cache.ID)
793− headers.Set("Conversation_id", cache.ID)
794− headers.Set("Session_id", cache.ID)
796+ headers.Set("session_id", cache.ID)
795797 }
796798
797799 return rawJSON, headers
@@ -826,7 +828,7 @@ func applyCodexWebsocketHeaders(ctx context.Context, headers http.Header, auth * · 28 unmodified lines
826828 betaHeader = codexResponsesWebsocketBetaHeaderValue
827829 }
828830 headers.Set("OpenAI-Beta", betaHeader)
829− misc.EnsureHeader(headers, ginHeaders, "Session_id", uuid.NewString())
831+ misc.EnsureHeader(headers, ginHeaders, "session_id", uuid.NewString())
830832 ensureHeaderWithConfigPrecedence(headers, ginHeaders, "User-Agent", cfgUserAgent, codexUserAgent)
831833
832834 isAPIKey := false
internal/runtime/executor/codex_websockets_executor_test.go+25−0
@@ -9,7 +9,9 @@ import ( · 8 unmodified lines
99 "github.com/gin-gonic/gin"
1010 "github.com/router-for-me/CLIProxyAPI/v6/internal/config"
1111 cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
12+ cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
1213 sdkconfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config"
14+ sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
1315 "github.com/tidwall/gjson"
1416 )
1517
@@ -32,6 +34,29 @@ func TestBuildCodexWebsocketRequestBodyPreservesPreviousResponseID(t *testing.T) · 16 unmodified lines
3234 }
3335 }
3436
37+func TestApplyCodexPromptCacheHeaders_PreservesPromptCacheRetention(t *testing.T) {
38+ req := cliproxyexecutor.Request{
39+ Model: "gpt-5-codex",
40+ Payload: []byte(`{"prompt_cache_key":"cache-key-1","prompt_cache_retention":"persistent"}`),
41+ }
42+ body := []byte(`{"model":"gpt-5-codex","stream":true,"prompt_cache_retention":"persistent"}`)
43+
44+ updatedBody, headers := applyCodexPromptCacheHeaders(sdktranslator.FromString("openai-response"), req, body)
45+
46+ if got := gjson.GetBytes(updatedBody, "prompt_cache_key").String(); got != "cache-key-1" {
47+ t.Fatalf("prompt_cache_key = %q, want %q", got, "cache-key-1")
48+ }
49+ if got := gjson.GetBytes(updatedBody, "prompt_cache_retention").String(); got != "persistent" {
50+ t.Fatalf("prompt_cache_retention = %q, want %q", got, "persistent")
51+ }
52+ if got := headers.Get("session_id"); got != "cache-key-1" {
53+ t.Fatalf("session_id = %q, want %q", got, "cache-key-1")
54+ }
55+ if got := headers.Get("Conversation_id"); got != "" {
56+ t.Fatalf("Conversation_id = %q, want empty", got)
57+ }
58+}
59+
3560 func TestApplyCodexWebsocketHeadersDefaultsToCurrentResponsesBeta(t *testing.T) {
3661 headers := applyCodexWebsocketHeaders(context.Background(), http.Header{}, nil, "", nil)
3762
internal/translator/codex/openai/chat-completions/codex_openai_request.go+3−0
@@ -65,6 +65,9 @@ func ConvertOpenAIRequestToCodex(modelName string, inputRawJSON []byte, stream b · 64 unmodified lines
6565
6666 // Model
6767 out, _ = sjson.SetBytes(out, "model", modelName)
68+ if v := gjson.GetBytes(rawJSON, "prompt_cache_retention"); v.Exists() {
69+ out, _ = sjson.SetBytes(out, "prompt_cache_retention", v.Value())
70+ }
6871
6972 // Build tool name shortening map from original tools (if any)
7073 originalToolNameMap := map[string]string{}
internal/translator/codex/openai/chat-completions/codex_openai_request_test.go+16−0
@@ -633,3 +633,19 @@ func TestToolsDefinitionTranslated(t *testing.T) { · 632 unmodified lines
633633 t.Errorf("tool 'search' not found in output tools: %s", gjson.Get(result, "tools").Raw)
634634 }
635635 }
636+
637+func TestPromptCacheRetentionPreserved(t *testing.T) {
638+ input := []byte(`{
639+ "model": "gpt-4o",
640+ "prompt_cache_retention": "persistent",
641+ "messages": [
642+ {"role": "user", "content": "Hello"}
643+ ]
644+ }`)
645+
646+ out := ConvertOpenAIRequestToCodex("gpt-4o", input, true)
647+
648+ if got := gjson.GetBytes(out, "prompt_cache_retention").String(); got != "persistent" {
649+ t.Fatalf("prompt_cache_retention = %q, want %q", got, "persistent")
650+ }
651+}
sdk/api/handlers/handlers.go+13−1
@@ -46,6 +46,7 @@ type ErrorDetail struct { · 45 unmodified lines
4646 }
4747
4848 const idempotencyKeyMetadataKey = "idempotency_key"
49+const authAffinityMetadataKey = "auth_affinity_key"
4950
5051 const (
5152 defaultStreamingKeepAliveSeconds = 0
@@ -189,9 +190,11 @@ func requestExecutionMetadata(ctx context.Context) map[string]any { · 137 unmodified lines
189190 // Idempotency-Key is an optional client-supplied header used to correlate retries.
190191 // It is forwarded as execution metadata; when absent we generate a UUID.
191192 key := ""
193+ explicitIdempotencyKey := ""
192194 if ctx != nil {
193195 if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil && ginCtx.Request != nil {
194− key = strings.TrimSpace(ginCtx.GetHeader("Idempotency-Key"))
196+ explicitIdempotencyKey = strings.TrimSpace(ginCtx.GetHeader("Idempotency-Key"))
197+ key = explicitIdempotencyKey
195198 }
196199 }
197200 if key == "" {
@@ -207,6 +210,15 @@ func requestExecutionMetadata(ctx context.Context) map[string]any { · 9 unmodified lines
207210 }
208211 if executionSessionID := executionSessionIDFromContext(ctx); executionSessionID != "" {
209212 meta[coreexecutor.ExecutionSessionMetadataKey] = executionSessionID
213+ meta[authAffinityMetadataKey] = executionSessionID
214+ } else if explicitIdempotencyKey != "" {
215+ meta[authAffinityMetadataKey] = explicitIdempotencyKey
216+ } else if ctx != nil {
217+ if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil {
218+ if apiKey, exists := ginCtx.Get("apiKey"); exists && apiKey != nil {
219+ meta[authAffinityMetadataKey] = fmt.Sprintf("principal:%v", apiKey)
220+ }
221+ }
210222 }
211223 return meta
212224 }
sdk/cliproxy/auth/conductor.go+104−7
@@ -128,13 +128,15 @@ func (NoopHook) OnResult(context.Context, Result) {} · 127 unmodified lines
128128
129129 // Manager orchestrates auth lifecycle, selection, execution, and persistence.
130130 type Manager struct {
131− store Store
132− executors map[string]ProviderExecutor
133− selector Selector
134− hook Hook
135− mu sync.RWMutex
136− auths map[string]*Auth
137− scheduler *authScheduler
131+ store Store
132+ executors map[string]ProviderExecutor
133+ selector Selector
134+ hook Hook
135+ mu sync.RWMutex
136+ auths map[string]*Auth
137+ scheduler *authScheduler
138+ affinityMu sync.RWMutex
139+ affinity map[string]string
138140 // providerOffsets tracks per-model provider rotation state for multi-provider routing.
139141 providerOffsets map[string]int
140142
@@ -179,6 +181,7 @@ func NewManager(store Store, selector Selector, hook Hook) *Manager { · 38 unmodified lines
179181 selector: selector,
180182 hook: hook,
181183 auths: make(map[string]*Auth),
184+ affinity: make(map[string]string),
182185 providerOffsets: make(map[string]int),
183186 modelPoolOffsets: make(map[string]int),
184187 refreshSemaphore: make(chan struct{}, refreshMaxConcurrency),
@@ -1090,6 +1093,12 @@ func (m *Manager) executeMixedOnce(ctx context.Context, providers []string, req · 905 unmodified lines
10901093 entry := logEntryWithRequestID(ctx)
10911094 debugLogAuthSelection(entry, auth, provider, req.Model)
10921095 publishSelectedAuthMetadata(opts.Metadata, auth.ID)
1096+ if affinityKey := authAffinityKeyFromMetadata(opts.Metadata); affinityKey != "" {
1097+ m.SetAuthAffinity(affinityKey, auth.ID)
1098+ if log.IsLevelEnabled(log.DebugLevel) {
1099+ entry.Debugf("auth affinity pinned key=%s auth_id=%s provider=%s model=%s", affinityKey, auth.ID, provider, req.Model)
1100+ }
1101+ }
10931102
10941103 tried[auth.ID] = struct{}{}
10951104 execCtx := ctx
@@ -1168,6 +1177,12 @@ func (m *Manager) executeCountMixedOnce(ctx context.Context, providers []string, · 72 unmodified lines
11681177 entry := logEntryWithRequestID(ctx)
11691178 debugLogAuthSelection(entry, auth, provider, req.Model)
11701179 publishSelectedAuthMetadata(opts.Metadata, auth.ID)
1180+ if affinityKey := authAffinityKeyFromMetadata(opts.Metadata); affinityKey != "" {
1181+ m.SetAuthAffinity(affinityKey, auth.ID)
1182+ if log.IsLevelEnabled(log.DebugLevel) {
1183+ entry.Debugf("auth affinity pinned key=%s auth_id=%s provider=%s model=%s", affinityKey, auth.ID, provider, req.Model)
1184+ }
1185+ }
11711186
11721187 tried[auth.ID] = struct{}{}
11731188 execCtx := ctx
@@ -1254,6 +1269,12 @@ func (m *Manager) executeStreamMixedOnce(ctx context.Context, providers []string · 80 unmodified lines
12541269 entry := logEntryWithRequestID(ctx)
12551270 debugLogAuthSelection(entry, auth, provider, req.Model)
12561271 publishSelectedAuthMetadata(opts.Metadata, auth.ID)
1272+ if affinityKey := authAffinityKeyFromMetadata(opts.Metadata); affinityKey != "" {
1273+ m.SetAuthAffinity(affinityKey, auth.ID)
1274+ if log.IsLevelEnabled(log.DebugLevel) {
1275+ entry.Debugf("auth affinity pinned key=%s auth_id=%s provider=%s model=%s", affinityKey, auth.ID, provider, req.Model)
1276+ }
1277+ }
12571278
12581279 tried[auth.ID] = struct{}{}
12591280 execCtx := ctx
@@ -2222,6 +2243,58 @@ func (m *Manager) CloseExecutionSession(sessionID string) { · 962 unmodified lines
22222243 }
22232244 }
22242245
2246+func authAffinityKeyFromMetadata(meta map[string]any) string {
2247+ if len(meta) == 0 {
2248+ return ""
2249+ }
2250+ raw, ok := meta["auth_affinity_key"]
2251+ if !ok || raw == nil {
2252+ return ""
2253+ }
2254+ switch val := raw.(type) {
2255+ case string:
2256+ return strings.TrimSpace(val)
2257+ case []byte:
2258+ return strings.TrimSpace(string(val))
2259+ default:
2260+ return ""
2261+ }
2262+}
2263+
2264+func (m *Manager) AuthAffinity(key string) string {
2265+ key = strings.TrimSpace(key)
2266+ if m == nil || key == "" {
2267+ return ""
2268+ }
2269+ m.affinityMu.RLock()
2270+ defer m.affinityMu.RUnlock()
2271+ return strings.TrimSpace(m.affinity[key])
2272+}
2273+
2274+func (m *Manager) SetAuthAffinity(key, authID string) {
2275+ key = strings.TrimSpace(key)
2276+ authID = strings.TrimSpace(authID)
2277+ if m == nil || key == "" || authID == "" {
2278+ return
2279+ }
2280+ m.affinityMu.Lock()
2281+ if m.affinity == nil {
2282+ m.affinity = make(map[string]string)
2283+ }
2284+ m.affinity[key] = authID
2285+ m.affinityMu.Unlock()
2286+}
2287+
2288+func (m *Manager) ClearAuthAffinity(key string) {
2289+ key = strings.TrimSpace(key)
2290+ if m == nil || key == "" {
2291+ return
2292+ }
2293+ m.affinityMu.Lock()
2294+ delete(m.affinity, key)
2295+ m.affinityMu.Unlock()
2296+}
2297+
22252298 func (m *Manager) useSchedulerFastPath() bool {
22262299 if m == nil || m.scheduler == nil {
22272300 return false
@@ -2305,6 +2378,18 @@ func (m *Manager) pickNextLegacy(ctx context.Context, provider, model string, op · 77 unmodified lines
23052378 }
23062379
23072380 func (m *Manager) pickNext(ctx context.Context, provider, model string, opts cliproxyexecutor.Options, tried map[string]struct{}) (*Auth, ProviderExecutor, error) {
2381+ if pinnedAuthID := pinnedAuthIDFromMetadata(opts.Metadata); pinnedAuthID == "" {
2382+ if affinityKey := authAffinityKeyFromMetadata(opts.Metadata); affinityKey != "" {
2383+ if affinityAuthID := m.AuthAffinity(affinityKey); affinityAuthID != "" {
2384+ meta := opts.Metadata
2385+ if meta == nil {
2386+ meta = make(map[string]any)
2387+ opts.Metadata = meta
2388+ }
2389+ meta[cliproxyexecutor.PinnedAuthMetadataKey] = affinityAuthID
2390+ }
2391+ }
2392+ }
23082393 if !m.useSchedulerFastPath() {
23092394 return m.pickNextLegacy(ctx, provider, model, opts, tried)
23102395 }
@@ -2419,6 +2504,18 @@ func (m *Manager) pickNextMixedLegacy(ctx context.Context, providers []string, m · 108 unmodified lines
24192504 }
24202505
24212506 func (m *Manager) pickNextMixed(ctx context.Context, providers []string, model string, opts cliproxyexecutor.Options, tried map[string]struct{}) (*Auth, ProviderExecutor, string, error) {
2507+ if pinnedAuthID := pinnedAuthIDFromMetadata(opts.Metadata); pinnedAuthID == "" {
2508+ if affinityKey := authAffinityKeyFromMetadata(opts.Metadata); affinityKey != "" {
2509+ if affinityAuthID := m.AuthAffinity(affinityKey); affinityAuthID != "" {
2510+ meta := opts.Metadata
2511+ if meta == nil {
2512+ meta = make(map[string]any)
2513+ opts.Metadata = meta
2514+ }
2515+ meta[cliproxyexecutor.PinnedAuthMetadataKey] = affinityAuthID
2516+ }
2517+ }
2518+ }
24222519 if !m.useSchedulerFastPath() {
24232520 return m.pickNextMixedLegacy(ctx, providers, model, opts, tried)
24242521 }
sdk/cliproxy/auth/conductor_affinity_test.go+85−0
@@ -0,0 +1,85 @@
1+package auth
2+
3+import (
4+ "context"
5+ "net/http"
6+ "testing"
7+
8+ "github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
9+ cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
10+)
11+
12+type affinityTestExecutor struct{ id string }
13+
14+func (e affinityTestExecutor) Identifier() string { return e.id }
15+
16+func (e affinityTestExecutor) Execute(context.Context, *Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
17+ return cliproxyexecutor.Response{}, nil
18+}
19+
20+func (e affinityTestExecutor) ExecuteStream(context.Context, *Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, error) {
21+ ch := make(chan cliproxyexecutor.StreamChunk)
22+ close(ch)
23+ return &cliproxyexecutor.StreamResult{Chunks: ch}, nil
24+}
25+
26+func (e affinityTestExecutor) Refresh(_ context.Context, auth *Auth) (*Auth, error) { return auth, nil }
27+
28+func (e affinityTestExecutor) CountTokens(context.Context, *Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
29+ return cliproxyexecutor.Response{}, nil
30+}
31+
32+func (e affinityTestExecutor) HttpRequest(context.Context, *Auth, *http.Request) (*http.Response, error) {
33+ return nil, nil
34+}
35+
36+func TestManagerPickNextMixedUsesAuthAffinity(t *testing.T) {
37+ t.Parallel()
38+
39+ manager := NewManager(nil, &RoundRobinSelector{}, nil)
40+ manager.executors["codex"] = affinityTestExecutor{id: "codex"}
41+ reg := registry.GetGlobalRegistry()
42+ reg.RegisterClient("codex-a", "codex", []*registry.ModelInfo{{ID: "gpt-5.4"}})
43+ reg.RegisterClient("codex-b", "codex", []*registry.ModelInfo{{ID: "gpt-5.4"}})
44+ t.Cleanup(func() {
45+ reg.UnregisterClient("codex-a")
46+ reg.UnregisterClient("codex-b")
47+ })
48+ if _, errRegister := manager.Register(context.Background(), &Auth{ID: "codex-a", Provider: "codex"}); errRegister != nil {
49+ t.Fatalf("Register(codex-a) error = %v", errRegister)
50+ }
51+ if _, errRegister := manager.Register(context.Background(), &Auth{ID: "codex-b", Provider: "codex"}); errRegister != nil {
52+ t.Fatalf("Register(codex-b) error = %v", errRegister)
53+ }
54+
55+ manager.SetAuthAffinity("idem-1", "codex-b")
56+ opts := cliproxyexecutor.Options{Metadata: map[string]any{"auth_affinity_key": "idem-1"}}
57+
58+ got, _, provider, errPick := manager.pickNextMixed(context.Background(), []string{"codex"}, "gpt-5.4", opts, map[string]struct{}{})
59+ if errPick != nil {
60+ t.Fatalf("pickNextMixed() error = %v", errPick)
61+ }
62+ if provider != "codex" {
63+ t.Fatalf("provider = %q, want %q", provider, "codex")
64+ }
65+ if got == nil || got.ID != "codex-b" {
66+ t.Fatalf("auth.ID = %v, want codex-b", got)
67+ }
68+ if pinned := pinnedAuthIDFromMetadata(opts.Metadata); pinned != "codex-b" {
69+ t.Fatalf("pinned auth metadata = %q, want %q", pinned, "codex-b")
70+ }
71+}
72+
73+func TestManagerAuthAffinityRoundTrip(t *testing.T) {
74+ t.Parallel()
75+
76+ manager := NewManager(nil, nil, nil)
77+ manager.SetAuthAffinity("idem-2", "auth-1")
78+ if got := manager.AuthAffinity("idem-2"); got != "auth-1" {
79+ t.Fatalf("AuthAffinity = %q, want %q", got, "auth-1")
80+ }
81+ manager.ClearAuthAffinity("idem-2")
82+ if got := manager.AuthAffinity("idem-2"); got != "" {
83+ t.Fatalf("AuthAffinity after clear = %q, want empty", got)
84+ }
85+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t