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 @@
1package executor
2
3import (
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
17const codexAuthAffinityMetadataKey = "auth_affinity_key"
18
19type codexContinuity struct {
20 Key string
21 Source string
22}
23
24func 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
88func 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
96func applyCodexContinuityHeaders(headers http.Header, continuity codexContinuity) {
97 if headers == nil || continuity.Key == "" {
98 return
99 }
100 headers.Set("session_id", continuity.Key)
101}
102
103func 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
111 body, _ = sjson.SetBytes(body, "model", baseModel)111 body, _ = sjson.SetBytes(body, "model", baseModel)
112 body, _ = sjson.SetBytes(body, "stream", true)112 body, _ = sjson.SetBytes(body, "stream", true)
113 body, _ = sjson.DeleteBytes(body, "previous_response_id")113 body, _ = sjson.DeleteBytes(body, "previous_response_id")
114 body, _ = sjson.DeleteBytes(body, "prompt_cache_retention")
115 body, _ = sjson.DeleteBytes(body, "safety_identifier")114 body, _ = sjson.DeleteBytes(body, "safety_identifier")
116 if !gjson.GetBytes(body, "instructions").Exists() {115 if !gjson.GetBytes(body, "instructions").Exists() {
117 body, _ = sjson.SetBytes(body, "instructions", "")116 body, _ = sjson.SetBytes(body, "instructions", "")
118 }117 }
119 118
120 url := strings.TrimSuffix(baseURL, "/") + "/responses"119 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)
122 if err != nil {121 if err != nil {
123 return resp, err122 return resp, err
124 }123 }
125 applyCodexHeaders(httpReq, auth, apiKey, true, e.cfg)124 applyCodexHeaders(httpReq, auth, apiKey, true, e.cfg)
125 logCodexRequestDiagnostics(ctx, auth, req, opts, httpReq.Header, body, continuity)
126 var authID, authLabel, authType, authValue string126 var authID, authLabel, authType, authValue string
127 if auth != nil {127 if auth != nil {
128 authID = auth.ID128 authID = auth.ID
@@ -222,11 +222,12 @@ func (e *CodexExecutor) executeCompact(ctx context.Context, auth *cliproxyauth.A · 93 unmodified lines
222 body, _ = sjson.DeleteBytes(body, "stream")222 body, _ = sjson.DeleteBytes(body, "stream")
223 223
224 url := strings.TrimSuffix(baseURL, "/") + "/responses/compact"224 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)
226 if err != nil {226 if err != nil {
227 return resp, err227 return resp, err
228 }228 }
229 applyCodexHeaders(httpReq, auth, apiKey, false, e.cfg)229 applyCodexHeaders(httpReq, auth, apiKey, false, e.cfg)
230 logCodexRequestDiagnostics(ctx, auth, req, opts, httpReq.Header, body, continuity)
230 var authID, authLabel, authType, authValue string231 var authID, authLabel, authType, authValue string
231 if auth != nil {232 if auth != nil {
232 authID = auth.ID233 authID = auth.ID
@@ -309,7 +310,6 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au · 76 unmodified lines
309 requestedModel := payloadRequestedModel(opts, req.Model)310 requestedModel := payloadRequestedModel(opts, req.Model)
310 body = applyPayloadConfigWithRoot(e.cfg, baseModel, to.String(), "", body, originalTranslated, requestedModel)311 body = applyPayloadConfigWithRoot(e.cfg, baseModel, to.String(), "", body, originalTranslated, requestedModel)
311 body, _ = sjson.DeleteBytes(body, "previous_response_id")312 body, _ = sjson.DeleteBytes(body, "previous_response_id")
312 body, _ = sjson.DeleteBytes(body, "prompt_cache_retention")
313 body, _ = sjson.DeleteBytes(body, "safety_identifier")313 body, _ = sjson.DeleteBytes(body, "safety_identifier")
314 body, _ = sjson.SetBytes(body, "model", baseModel)314 body, _ = sjson.SetBytes(body, "model", baseModel)
315 if !gjson.GetBytes(body, "instructions").Exists() {315 if !gjson.GetBytes(body, "instructions").Exists() {
@@ -317,11 +317,12 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au · 1 unmodified line
317 }317 }
318 318
319 url := strings.TrimSuffix(baseURL, "/") + "/responses"319 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)
321 if err != nil {321 if err != nil {
322 return nil, err322 return nil, err
323 }323 }
324 applyCodexHeaders(httpReq, auth, apiKey, true, e.cfg)324 applyCodexHeaders(httpReq, auth, apiKey, true, e.cfg)
325 logCodexRequestDiagnostics(ctx, auth, req, opts, httpReq.Header, body, continuity)
325 var authID, authLabel, authType, authValue string326 var authID, authLabel, authType, authValue string
326 if auth != nil {327 if auth != nil {
327 authID = auth.ID328 authID = auth.ID
@@ -596,8 +597,9 @@ func (e *CodexExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (* · 268 unmodified lines
596 return auth, nil597 return auth, nil
597}598}
598 599
599func (e *CodexExecutor) cacheHelper(ctx context.Context, from sdktranslator.Format, url string, req cliproxyexecutor.Request, rawJSON []byte) (*http.Request, error) {600func (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) {
600 var cache codexCache601 var cache codexCache
602 continuity := codexContinuity{}
601 if from == "claude" {603 if from == "claude" {
602 userIDResult := gjson.GetBytes(req.Payload, "metadata.user_id")604 userIDResult := gjson.GetBytes(req.Payload, "metadata.user_id")
603 if userIDResult.Exists() {605 if userIDResult.Exists() {
@@ -615,25 +617,20 @@ func (e *CodexExecutor) cacheHelper(ctx context.Context, from sdktranslator.Form · 11 unmodified lines
615 promptCacheKey := gjson.GetBytes(req.Payload, "prompt_cache_key")617 promptCacheKey := gjson.GetBytes(req.Payload, "prompt_cache_key")
616 if promptCacheKey.Exists() {618 if promptCacheKey.Exists() {
617 cache.ID = promptCacheKey.String()619 cache.ID = promptCacheKey.String()
620 continuity = codexContinuity{Key: cache.ID, Source: "prompt_cache_key"}
618 }621 }
619 } else if from == "openai" {622 } else if from == "openai" {
620 if apiKey := strings.TrimSpace(apiKeyFromContext(ctx)); apiKey != "" {623 continuity = resolveCodexContinuity(ctx, auth, req, opts)
621 cache.ID = uuid.NewSHA1(uuid.NameSpaceOID, []byte("cli-proxy-api:codex:prompt-cache:"+apiKey)).String()624 cache.ID = continuity.Key
622 }
623 }625 }
624 626
625 if cache.ID != "" {627 rawJSON = applyCodexContinuityBody(rawJSON, continuity)
626 rawJSON, _ = sjson.SetBytes(rawJSON, "prompt_cache_key", cache.ID)
627 }
628 httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(rawJSON))628 httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(rawJSON))
629 if err != nil {629 if err != nil {
630 return nil, err630 return nil, continuity, err
631 }
632 if cache.ID != "" {
633 httpReq.Header.Set("Conversation_id", cache.ID)
634 httpReq.Header.Set("Session_id", cache.ID)
635 }631 }
636 return httpReq, nil632 applyCodexContinuityHeaders(httpReq.Header, continuity)
633 return httpReq, continuity, nil
637}634}
638 635
639func applyCodexHeaders(r *http.Request, auth *cliproxyauth.Auth, token string, stream bool, cfg *config.Config) {636func 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
646 }643 }
647 644
648 misc.EnsureHeader(r.Header, ginHeaders, "Version", "")645 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())
650 misc.EnsureHeader(r.Header, ginHeaders, "X-Codex-Turn-Metadata", "")647 misc.EnsureHeader(r.Header, ginHeaders, "X-Codex-Turn-Metadata", "")
651 misc.EnsureHeader(r.Header, ginHeaders, "X-Client-Request-Id", "")648 misc.EnsureHeader(r.Header, ginHeaders, "X-Client-Request-Id", "")
652 cfgUserAgent, _ := codexHeaderDefaults(cfg, auth)649 cfgUserAgent, _ := codexHeaderDefaults(cfg, auth)
internal/runtime/executor/codex_executor_cache_test.go+95−6
@@ -8,6 +8,7 @@ import ( · 7 unmodified lines
8 8
9 "github.com/gin-gonic/gin"9 "github.com/gin-gonic/gin"
10 "github.com/google/uuid"10 "github.com/google/uuid"
11 cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
11 cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"12 cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
12 sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"13 sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
13 "github.com/tidwall/gjson"14 "github.com/tidwall/gjson"
@@ -27,7 +28,7 @@ func TestCodexExecutorCacheHelper_OpenAIChatCompletions_StablePromptCacheKeyFrom · 13 unmodified lines
27 }28 }
28 url := "https://example.com/responses"29 url := "https://example.com/responses"
29 30
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)
31 if err != nil {32 if err != nil {
32 t.Fatalf("cacheHelper error: %v", err)33 t.Fatalf("cacheHelper error: %v", err)
33 }34 }
@@ -42,14 +43,14 @@ func TestCodexExecutorCacheHelper_OpenAIChatCompletions_StablePromptCacheKeyFrom · 8 unmodified lines
42 if gotKey != expectedKey {43 if gotKey != expectedKey {
43 t.Fatalf("prompt_cache_key = %q, want %q", gotKey, expectedKey)44 t.Fatalf("prompt_cache_key = %q, want %q", gotKey, expectedKey)
44 }45 }
45 if gotConversation := httpReq.Header.Get("Conversation_id"); gotConversation != expectedKey {46 if gotSession := httpReq.Header.Get("session_id"); gotSession != expectedKey {
46 t.Fatalf("Conversation_id = %q, want %q", gotConversation, expectedKey)47 t.Fatalf("session_id = %q, want %q", gotSession, expectedKey)
47 }48 }
48 if gotSession := httpReq.Header.Get("Session_id"); gotSession != expectedKey {49 if got := httpReq.Header.Get("Conversation_id"); got != "" {
49 t.Fatalf("Session_id = %q, want %q", gotSession, expectedKey)50 t.Fatalf("Conversation_id = %q, want empty", got)
50 }51 }
51 52
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)
53 if err != nil {54 if err != nil {
54 t.Fatalf("cacheHelper error (second call): %v", err)55 t.Fatalf("cacheHelper error (second call): %v", err)
55 }56 }
@@ -62,3 +63,91 @@ func TestCodexExecutorCacheHelper_OpenAIChatCompletions_StablePromptCacheKeyFrom · 6 unmodified lines
62 t.Fatalf("prompt_cache_key (second call) = %q, want %q", gotKey2, expectedKey)63 t.Fatalf("prompt_cache_key (second call) = %q, want %q", gotKey2, expectedKey)
63 }64 }
64}65}
66
67func 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
100func 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
127func 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
178 body, _ = sjson.SetBytes(body, "model", baseModel)178 body, _ = sjson.SetBytes(body, "model", baseModel)
179 body, _ = sjson.SetBytes(body, "stream", true)179 body, _ = sjson.SetBytes(body, "stream", true)
180 body, _ = sjson.DeleteBytes(body, "previous_response_id")180 body, _ = sjson.DeleteBytes(body, "previous_response_id")
181 body, _ = sjson.DeleteBytes(body, "prompt_cache_retention")
182 body, _ = sjson.DeleteBytes(body, "safety_identifier")181 body, _ = sjson.DeleteBytes(body, "safety_identifier")
183 if !gjson.GetBytes(body, "instructions").Exists() {182 if !gjson.GetBytes(body, "instructions").Exists() {
184 body, _ = sjson.SetBytes(body, "instructions", "")183 body, _ = sjson.SetBytes(body, "instructions", "")
@@ -191,6 +190,7 @@ func (e *CodexWebsocketsExecutor) Execute(ctx context.Context, auth *cliproxyaut · 6 unmodified lines
191 }190 }
192 191
193 body, wsHeaders := applyCodexPromptCacheHeaders(from, req, body)192 body, wsHeaders := applyCodexPromptCacheHeaders(from, req, body)
193 continuity := codexContinuity{Key: strings.TrimSpace(wsHeaders.Get("session_id"))}
194 wsHeaders = applyCodexWebsocketHeaders(ctx, wsHeaders, auth, apiKey, e.cfg)194 wsHeaders = applyCodexWebsocketHeaders(ctx, wsHeaders, auth, apiKey, e.cfg)
195 195
196 var authID, authLabel, authType, authValue string196 var authID, authLabel, authType, authValue string
@@ -209,6 +209,7 @@ func (e *CodexWebsocketsExecutor) Execute(ctx context.Context, auth *cliproxyaut · 12 unmodified lines
209 }209 }
210 210
211 wsReqBody := buildCodexWebsocketRequestBody(body)211 wsReqBody := buildCodexWebsocketRequestBody(body)
212 logCodexRequestDiagnostics(ctx, auth, req, opts, wsHeaders, body, continuity)
212 recordAPIRequest(ctx, e.cfg, upstreamRequestLog{213 recordAPIRequest(ctx, e.cfg, upstreamRequestLog{
213 URL: wsURL,214 URL: wsURL,
214 Method: "WEBSOCKET",215 Method: "WEBSOCKET",
@@ -386,6 +387,7 @@ func (e *CodexWebsocketsExecutor) ExecuteStream(ctx context.Context, auth *clipr · 171 unmodified lines
386 }387 }
387 388
388 body, wsHeaders := applyCodexPromptCacheHeaders(from, req, body)389 body, wsHeaders := applyCodexPromptCacheHeaders(from, req, body)
390 continuity := codexContinuity{Key: strings.TrimSpace(wsHeaders.Get("session_id"))}
389 wsHeaders = applyCodexWebsocketHeaders(ctx, wsHeaders, auth, apiKey, e.cfg)391 wsHeaders = applyCodexWebsocketHeaders(ctx, wsHeaders, auth, apiKey, e.cfg)
390 392
391 var authID, authLabel, authType, authValue string393 var authID, authLabel, authType, authValue string
@@ -403,6 +405,7 @@ func (e *CodexWebsocketsExecutor) ExecuteStream(ctx context.Context, auth *clipr · 11 unmodified lines
403 }405 }
404 406
405 wsReqBody := buildCodexWebsocketRequestBody(body)407 wsReqBody := buildCodexWebsocketRequestBody(body)
408 logCodexRequestDiagnostics(ctx, auth, req, opts, wsHeaders, body, continuity)
406 recordAPIRequest(ctx, e.cfg, upstreamRequestLog{409 recordAPIRequest(ctx, e.cfg, upstreamRequestLog{
407 URL: wsURL,410 URL: wsURL,
408 Method: "WEBSOCKET",411 Method: "WEBSOCKET",
@@ -790,8 +793,7 @@ func applyCodexPromptCacheHeaders(from sdktranslator.Format, req cliproxyexecuto · 381 unmodified lines
790 793
791 if cache.ID != "" {794 if cache.ID != "" {
792 rawJSON, _ = sjson.SetBytes(rawJSON, "prompt_cache_key", cache.ID)795 rawJSON, _ = sjson.SetBytes(rawJSON, "prompt_cache_key", cache.ID)
793 headers.Set("Conversation_id", cache.ID)796 headers.Set("session_id", cache.ID)
794 headers.Set("Session_id", cache.ID)
795 }797 }
796 798
797 return rawJSON, headers799 return rawJSON, headers
@@ -826,7 +828,7 @@ func applyCodexWebsocketHeaders(ctx context.Context, headers http.Header, auth * · 28 unmodified lines
826 betaHeader = codexResponsesWebsocketBetaHeaderValue828 betaHeader = codexResponsesWebsocketBetaHeaderValue
827 }829 }
828 headers.Set("OpenAI-Beta", betaHeader)830 headers.Set("OpenAI-Beta", betaHeader)
829 misc.EnsureHeader(headers, ginHeaders, "Session_id", uuid.NewString())831 misc.EnsureHeader(headers, ginHeaders, "session_id", uuid.NewString())
830 ensureHeaderWithConfigPrecedence(headers, ginHeaders, "User-Agent", cfgUserAgent, codexUserAgent)832 ensureHeaderWithConfigPrecedence(headers, ginHeaders, "User-Agent", cfgUserAgent, codexUserAgent)
831 833
832 isAPIKey := false834 isAPIKey := false
internal/runtime/executor/codex_websockets_executor_test.go+25−0
@@ -9,7 +9,9 @@ import ( · 8 unmodified lines
9 "github.com/gin-gonic/gin"9 "github.com/gin-gonic/gin"
10 "github.com/router-for-me/CLIProxyAPI/v6/internal/config"10 "github.com/router-for-me/CLIProxyAPI/v6/internal/config"
11 cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"11 cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
12 cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
12 sdkconfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config"13 sdkconfig "github.com/router-for-me/CLIProxyAPI/v6/sdk/config"
14 sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator"
13 "github.com/tidwall/gjson"15 "github.com/tidwall/gjson"
14)16)
15 17
@@ -32,6 +34,29 @@ func TestBuildCodexWebsocketRequestBodyPreservesPreviousResponseID(t *testing.T) · 16 unmodified lines
32 }34 }
33}35}
34 36
37func 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
35func TestApplyCodexWebsocketHeadersDefaultsToCurrentResponsesBeta(t *testing.T) {60func TestApplyCodexWebsocketHeadersDefaultsToCurrentResponsesBeta(t *testing.T) {
36 headers := applyCodexWebsocketHeaders(context.Background(), http.Header{}, nil, "", nil)61 headers := applyCodexWebsocketHeaders(context.Background(), http.Header{}, nil, "", nil)
37 62
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
65 65
66 // Model66 // Model
67 out, _ = sjson.SetBytes(out, "model", modelName)67 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 }
68 71
69 // Build tool name shortening map from original tools (if any)72 // Build tool name shortening map from original tools (if any)
70 originalToolNameMap := map[string]string{}73 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
633 t.Errorf("tool 'search' not found in output tools: %s", gjson.Get(result, "tools").Raw)633 t.Errorf("tool 'search' not found in output tools: %s", gjson.Get(result, "tools").Raw)
634 }634 }
635}635}
636
637func 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
46}46}
47 47
48const idempotencyKeyMetadataKey = "idempotency_key"48const idempotencyKeyMetadataKey = "idempotency_key"
49const authAffinityMetadataKey = "auth_affinity_key"
49 50
50const (51const (
51 defaultStreamingKeepAliveSeconds = 052 defaultStreamingKeepAliveSeconds = 0
@@ -189,9 +190,11 @@ func requestExecutionMetadata(ctx context.Context) map[string]any { · 137 unmodified lines
189 // Idempotency-Key is an optional client-supplied header used to correlate retries.190 // Idempotency-Key is an optional client-supplied header used to correlate retries.
190 // It is forwarded as execution metadata; when absent we generate a UUID.191 // It is forwarded as execution metadata; when absent we generate a UUID.
191 key := ""192 key := ""
193 explicitIdempotencyKey := ""
192 if ctx != nil {194 if ctx != nil {
193 if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil && ginCtx.Request != nil {195 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
195 }198 }
196 }199 }
197 if key == "" {200 if key == "" {
@@ -207,6 +210,15 @@ func requestExecutionMetadata(ctx context.Context) map[string]any { · 9 unmodified lines
207 }210 }
208 if executionSessionID := executionSessionIDFromContext(ctx); executionSessionID != "" {211 if executionSessionID := executionSessionIDFromContext(ctx); executionSessionID != "" {
209 meta[coreexecutor.ExecutionSessionMetadataKey] = executionSessionID212 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 }
210 }222 }
211 return meta223 return meta
212}224}
sdk/cliproxy/auth/conductor.go+104−7
@@ -128,13 +128,15 @@ func (NoopHook) OnResult(context.Context, Result) {} · 127 unmodified lines
128 128
129// Manager orchestrates auth lifecycle, selection, execution, and persistence.129// Manager orchestrates auth lifecycle, selection, execution, and persistence.
130type Manager struct {130type Manager struct {
131 store Store131 store Store
132 executors map[string]ProviderExecutor132 executors map[string]ProviderExecutor
133 selector Selector133 selector Selector
134 hook Hook134 hook Hook
135 mu sync.RWMutex135 mu sync.RWMutex
136 auths map[string]*Auth136 auths map[string]*Auth
137 scheduler *authScheduler137 scheduler *authScheduler
138 affinityMu sync.RWMutex
139 affinity map[string]string
138 // providerOffsets tracks per-model provider rotation state for multi-provider routing.140 // providerOffsets tracks per-model provider rotation state for multi-provider routing.
139 providerOffsets map[string]int141 providerOffsets map[string]int
140 142
@@ -179,6 +181,7 @@ func NewManager(store Store, selector Selector, hook Hook) *Manager { · 38 unmodified lines
179 selector: selector,181 selector: selector,
180 hook: hook,182 hook: hook,
181 auths: make(map[string]*Auth),183 auths: make(map[string]*Auth),
184 affinity: make(map[string]string),
182 providerOffsets: make(map[string]int),185 providerOffsets: make(map[string]int),
183 modelPoolOffsets: make(map[string]int),186 modelPoolOffsets: make(map[string]int),
184 refreshSemaphore: make(chan struct{}, refreshMaxConcurrency),187 refreshSemaphore: make(chan struct{}, refreshMaxConcurrency),
@@ -1090,6 +1093,12 @@ func (m *Manager) executeMixedOnce(ctx context.Context, providers []string, req · 905 unmodified lines
1090 entry := logEntryWithRequestID(ctx)1093 entry := logEntryWithRequestID(ctx)
1091 debugLogAuthSelection(entry, auth, provider, req.Model)1094 debugLogAuthSelection(entry, auth, provider, req.Model)
1092 publishSelectedAuthMetadata(opts.Metadata, auth.ID)1095 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 }
1093 1102
1094 tried[auth.ID] = struct{}{}1103 tried[auth.ID] = struct{}{}
1095 execCtx := ctx1104 execCtx := ctx
@@ -1168,6 +1177,12 @@ func (m *Manager) executeCountMixedOnce(ctx context.Context, providers []string, · 72 unmodified lines
1168 entry := logEntryWithRequestID(ctx)1177 entry := logEntryWithRequestID(ctx)
1169 debugLogAuthSelection(entry, auth, provider, req.Model)1178 debugLogAuthSelection(entry, auth, provider, req.Model)
1170 publishSelectedAuthMetadata(opts.Metadata, auth.ID)1179 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 }
1171 1186
1172 tried[auth.ID] = struct{}{}1187 tried[auth.ID] = struct{}{}
1173 execCtx := ctx1188 execCtx := ctx
@@ -1254,6 +1269,12 @@ func (m *Manager) executeStreamMixedOnce(ctx context.Context, providers []string · 80 unmodified lines
1254 entry := logEntryWithRequestID(ctx)1269 entry := logEntryWithRequestID(ctx)
1255 debugLogAuthSelection(entry, auth, provider, req.Model)1270 debugLogAuthSelection(entry, auth, provider, req.Model)
1256 publishSelectedAuthMetadata(opts.Metadata, auth.ID)1271 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 }
1257 1278
1258 tried[auth.ID] = struct{}{}1279 tried[auth.ID] = struct{}{}
1259 execCtx := ctx1280 execCtx := ctx
@@ -2222,6 +2243,58 @@ func (m *Manager) CloseExecutionSession(sessionID string) { · 962 unmodified lines
2222 }2243 }
2223}2244}
2224 2245
2246func 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
2264func (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
2274func (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
2288func (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
2225func (m *Manager) useSchedulerFastPath() bool {2298func (m *Manager) useSchedulerFastPath() bool {
2226 if m == nil || m.scheduler == nil {2299 if m == nil || m.scheduler == nil {
2227 return false2300 return false
@@ -2305,6 +2378,18 @@ func (m *Manager) pickNextLegacy(ctx context.Context, provider, model string, op · 77 unmodified lines
2305}2378}
2306 2379
2307func (m *Manager) pickNext(ctx context.Context, provider, model string, opts cliproxyexecutor.Options, tried map[string]struct{}) (*Auth, ProviderExecutor, error) {2380func (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 }
2308 if !m.useSchedulerFastPath() {2393 if !m.useSchedulerFastPath() {
2309 return m.pickNextLegacy(ctx, provider, model, opts, tried)2394 return m.pickNextLegacy(ctx, provider, model, opts, tried)
2310 }2395 }
@@ -2419,6 +2504,18 @@ func (m *Manager) pickNextMixedLegacy(ctx context.Context, providers []string, m · 108 unmodified lines
2419}2504}
2420 2505
2421func (m *Manager) pickNextMixed(ctx context.Context, providers []string, model string, opts cliproxyexecutor.Options, tried map[string]struct{}) (*Auth, ProviderExecutor, string, error) {2506func (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 }
2422 if !m.useSchedulerFastPath() {2519 if !m.useSchedulerFastPath() {
2423 return m.pickNextMixedLegacy(ctx, providers, model, opts, tried)2520 return m.pickNextMixedLegacy(ctx, providers, model, opts, tried)
2424 }2521 }
sdk/cliproxy/auth/conductor_affinity_test.go+85−0
@@ -0,0 +1,85 @@
1package auth
2
3import (
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
12type affinityTestExecutor struct{ id string }
13
14func (e affinityTestExecutor) Identifier() string { return e.id }
15
16func (e affinityTestExecutor) Execute(context.Context, *Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
17 return cliproxyexecutor.Response{}, nil
18}
19
20func (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
26func (e affinityTestExecutor) Refresh(_ context.Context, auth *Auth) (*Auth, error) { return auth, nil }
27
28func (e affinityTestExecutor) CountTokens(context.Context, *Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
29 return cliproxyexecutor.Response{}, nil
30}
31
32func (e affinityTestExecutor) HttpRequest(context.Context, *Auth, *http.Request) (*http.Response, error) {
33 return nil, nil
34}
35
36func 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
73func 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