History

feat(executor): refine session and conversation header handling for Codex

- Updated session handling to replace `Session_id` and `Conversation_id` headers with new logic ensuring consistent use of `Cache.ID` and prompt keys. - Restored `Session_id` as a priority extraction source for `ExtractSessionID`. - Added tests to validate case-sensitive and case-insensitive headers, canonical account header usage, and session key preservation. - Removed legacy support for deprecated `Conversation_id` header to clean up API.

Luis Pater committed 5 months agov7.1.37

Commit 05b97241 parent fb4f39d9 files · +129 −61Browse files

Unified · Split

9 files
  1. config.example.yaml +1 −1
  2. internal/config/config.go +1 −1
  3. internal/runtime/executor/codex_executor.go +20 −9
  4. internal/runtime/executor/codex_executor_cache_test.go +18 −4
  5. internal/runtime/executor/codex_websockets_executor.go +15 −14
  6. internal/runtime/executor/codex_websockets_executor_test.go +34 −11
  7. sdk/api/handlers/openai/openai_responses_websocket_toolcall_repair.go +6 −0
  8. sdk/cliproxy/auth/selector.go +27 −15
  9. sdk/cliproxy/auth/selector_test.go +7 −6
config.example.yaml+1−1
@@ -119,7 +119,7 @@ routing: · 118 unmodified lines
119119 strategy: "round-robin" # round-robin (default), fill-first
120120 # Enable universal session-sticky routing for all clients.
121121 # Session IDs are extracted from: metadata.user_id (Claude Code session format),
122− # X-Session-ID, X-Amp-Thread-Id (Amp CLI),
122+ # X-Session-ID, Session_id (Codex), X-Amp-Thread-Id (Amp CLI),
123123 # X-Client-Request-Id (PI), conversation_id, or first few messages hash.
124124 # Automatic failover is always enabled when bound auth becomes unavailable.
125125 session-affinity: false # default: false
internal/config/config.go+1−1
@@ -237,7 +237,7 @@ type RoutingConfig struct { · 236 unmodified lines
237237
238238 // SessionAffinity enables universal session-sticky routing for all clients.
239239 // Session IDs are extracted from multiple sources:
240− // metadata.user_id (Claude Code session format), X-Session-ID,
240+ // metadata.user_id (Claude Code session format), X-Session-ID, Session_id (Codex),
241241 // X-Amp-Thread-Id (Amp CLI thread), X-Client-Request-Id (PI), metadata.user_id,
242242 // conversation_id, or message hash.
243243 // Automatic failover is always enabled when bound auth becomes unavailable.
internal/runtime/executor/codex_executor.go+20−9
@@ -931,6 +931,9 @@ func (e *CodexExecutor) cacheHelper(ctx context.Context, from sdktranslator.Form · 930 unmodified lines
931931 if err != nil {
932932 return nil, nil, codexIdentityConfuseState{}, err
933933 }
934+ if cache.ID != "" {
935+ httpReq.Header.Set("Session_id", cache.ID)
936+ }
934937 return httpReq, rawJSON, identityState, nil
935938 }
936939
@@ -964,7 +967,6 @@ func applyCodexIdentityConfuseHeaders(headers http.Header, state *codexIdentityC · 27 unmodified lines
964967 if headers == nil {
965968 return
966969 }
967− defer deleteDeprecatedCodexConversationHeader(headers)
968970 if state == nil || !state.enabled {
969971 return
970972 }
@@ -977,6 +979,12 @@ func applyCodexIdentityConfuseHeaders(headers http.Header, state *codexIdentityC · 6 unmodified lines
977979 }
978980
979981 setHeaderCasePreserved(headers, "Session-Id", state.promptCacheKey)
982+ if headerValueCaseInsensitive(headers, "session_id") != "" {
983+ setHeaderCasePreserved(headers, "session_id", state.promptCacheKey)
984+ }
985+ if headerValueCaseInsensitive(headers, "Conversation_id") != "" {
986+ setHeaderCasePreserved(headers, "Conversation_id", state.promptCacheKey)
987+ }
980988 headers.Set("X-Client-Request-Id", state.promptCacheKey)
981989 headers.Set("Thread-Id", state.promptCacheKey)
982990 headers.Set("X-Codex-Window-Id", state.promptCacheKey+":0")
@@ -1072,6 +1080,10 @@ func applyCodexHeaders(r *http.Request, auth *cliproxyauth.Auth, token string, s · 89 unmodified lines
10721080 cfgUserAgent, _ := codexHeaderDefaults(cfg, auth)
10731081 ensureHeaderWithConfigPrecedence(r.Header, ginHeaders, "User-Agent", cfgUserAgent, codexUserAgent)
10741082
1083+ if strings.Contains(r.Header.Get("User-Agent"), "Mac OS") {
1084+ misc.EnsureHeader(r.Header, ginHeaders, "Session_id", uuid.NewString())
1085+ }
1086+
10751087 if stream {
10761088 r.Header.Set("Accept", "text/event-stream")
10771089 } else {
@@ -1090,19 +1102,18 @@ func applyCodexHeaders(r *http.Request, auth *cliproxyauth.Auth, token string, s · 12 unmodified lines
10901102 } else if !isAPIKey {
10911103 r.Header.Set("Originator", codexOriginator)
10921104 }
1093− // if !isAPIKey {
1094− // if auth != nil && auth.Metadata != nil {
1095− // if accountID, ok := auth.Metadata["account_id"].(string); ok {
1096− // r.Header.Set("Chatgpt-Account-Id", accountID)
1097− // }
1098− // }
1099− // }
1105+ if !isAPIKey {
1106+ if auth != nil && auth.Metadata != nil {
1107+ if accountID, ok := auth.Metadata["account_id"].(string); ok {
1108+ r.Header.Set("Chatgpt-Account-Id", accountID)
1109+ }
1110+ }
1111+ }
11001112 var attrs map[string]string
11011113 if auth != nil {
11021114 attrs = auth.Attributes
11031115 }
11041116 util.ApplyCustomHeadersFromAttrs(r, attrs)
1105− deleteDeprecatedCodexConversationHeader(r.Header)
11061117 }
11071118
11081119 func newCodexStatusErr(statusCode int, body []byte) statusErr {
internal/runtime/executor/codex_executor_cache_test.go+18−4
@@ -47,8 +47,8 @@ func TestCodexExecutorCacheHelper_OpenAIChatCompletions_StablePromptCacheKeyFrom · 46 unmodified lines
4747 if gotConversation := httpReq.Header.Get("Conversation_id"); gotConversation != "" {
4848 t.Fatalf("Conversation_id = %q, want empty", gotConversation)
4949 }
50− if gotSession := httpReq.Header.Get("Session_id"); gotSession != "" {
51− t.Fatalf("Session_id = %q, want empty", gotSession)
50+ if gotSession := httpReq.Header.Get("Session_id"); gotSession != expectedKey {
51+ t.Fatalf("Session_id = %q, want %q", gotSession, expectedKey)
5252 }
5353
5454 httpReq2, _, _, err := executor.cacheHelper(ctx, sdktranslator.FromString("openai"), url, nil, req, req.Payload, rawJSON)
@@ -119,8 +119,8 @@ func TestCodexExecutorCacheHelper_IdentityConfuseRemapsBodyAndHeaders(t *testing · 64 unmodified lines
119119 t.Fatalf("%s = %q, want %q", headerName, gotHeader, expectedPromptCacheKey)
120120 }
121121 }
122− if gotSession := httpReq.Header.Get("Session_id"); gotSession != "" {
123− t.Fatalf("Session_id = %q, want empty", gotSession)
122+ if gotSession := httpReq.Header.Get("Session_id"); gotSession != expectedPromptCacheKey {
123+ t.Fatalf("Session_id = %q, want %q", gotSession, expectedPromptCacheKey)
124124 }
125125 if gotWindow := httpReq.Header.Get("X-Codex-Window-Id"); gotWindow != expectedPromptCacheKey+":0" {
126126 t.Fatalf("X-Codex-Window-Id = %q, want %q", gotWindow, expectedPromptCacheKey+":0")
@@ -137,6 +137,20 @@ func TestCodexExecutorCacheHelper_IdentityConfuseRemapsBodyAndHeaders(t *testing · 10 unmodified lines
137137 }
138138 }
139139
140+func TestApplyCodexHeadersUsesAccountHeaderForOAuth(t *testing.T) {
141+ httpReq := httptest.NewRequest("POST", "https://example.com/responses", nil)
142+ auth := &cliproxyauth.Auth{
143+ Provider: "codex",
144+ Metadata: map[string]any{"account_id": "acct-1"},
145+ }
146+
147+ applyCodexHeaders(httpReq, auth, "oauth-token", true, nil)
148+
149+ if got := httpReq.Header.Get("Chatgpt-Account-Id"); got != "acct-1" {
150+ t.Fatalf("Chatgpt-Account-Id = %q, want acct-1", got)
151+ }
152+}
153+
140154 func TestCodexIdentityConfuseKeepsClientBodySeparateFromUpstreamBody(t *testing.T) {
141155 cfg := &config.Config{
142156 Routing: config.RoutingConfig{Strategy: "fill-first"},
internal/runtime/executor/codex_websockets_executor.go+15−14
@@ -857,6 +857,8 @@ func applyCodexPromptCacheHeaders(from sdktranslator.Format, req cliproxyexecuto · 856 unmodified lines
857857
858858 if cache.ID != "" {
859859 rawJSON, _ = sjson.SetBytes(rawJSON, "prompt_cache_key", cache.ID)
860+ setHeaderCasePreserved(headers, "session_id", cache.ID)
861+ headers.Set("Conversation_id", cache.ID)
860862 }
861863
862864 return rawJSON, headers
@@ -897,27 +899,30 @@ func applyCodexWebsocketHeaders(ctx context.Context, headers http.Header, auth * · 34 unmodified lines
897899 betaHeader = codexResponsesWebsocketBetaHeaderValue
898900 }
899901 headers.Set("OpenAI-Beta", betaHeader)
902+ if strings.Contains(headers.Get("User-Agent"), "Mac OS") {
903+ ensureHeaderCasePreserved(headers, ginHeaders, "session_id", "", uuid.NewString())
904+ }
905+ ensureHeaderCasePreserved(headers, ginHeaders, "session_id", "", "")
900906 if originator := strings.TrimSpace(ginHeaders.Get("Originator")); originator != "" {
901907 headers.Set("Originator", originator)
902908 } else if !isAPIKey {
903909 headers.Set("Originator", codexOriginator)
904910 }
905− // if !isAPIKey {
906− // if auth != nil && auth.Metadata != nil {
907− // if accountID, ok := auth.Metadata["account_id"].(string); ok {
908− // if trimmed := strings.TrimSpace(accountID); trimmed != "" {
909− // setHeaderCasePreserved(headers, "ChatGPT-Account-ID", trimmed)
910− // }
911− // }
912− // }
913− // }
911+ if !isAPIKey {
912+ if auth != nil && auth.Metadata != nil {
913+ if accountID, ok := auth.Metadata["account_id"].(string); ok {
914+ if trimmed := strings.TrimSpace(accountID); trimmed != "" {
915+ setHeaderCasePreserved(headers, "ChatGPT-Account-ID", trimmed)
916+ }
917+ }
918+ }
919+ }
914920
915921 var attrs map[string]string
916922 if auth != nil {
917923 attrs = auth.Attributes
918924 }
919925 util.ApplyCustomHeadersFromAttrs(&http.Request{Header: headers}, attrs)
920− deleteDeprecatedCodexConversationHeader(headers)
921926
922927 return headers
923928 }
@@ -993,10 +998,6 @@ func deleteHeaderCaseInsensitive(headers http.Header, key string) { · 69 unmodified lines
993998 }
994999 }
9951000
996−func deleteDeprecatedCodexConversationHeader(headers http.Header) {
997− deleteHeaderCaseInsensitive(headers, "Conversation_id")
998−}
999−
10001001 func codexHeaderDefaults(cfg *config.Config, auth *cliproxyauth.Auth) (string, string) {
10011002 if cfg == nil || auth == nil {
10021003 return "", ""
internal/runtime/executor/codex_websockets_executor_test.go+34−11
@@ -217,8 +217,11 @@ func TestApplyCodexWebsocketHeadersPassesThroughClientIdentityHeaders(t *testing · 216 unmodified lines
217217 if got := headers.Get("X-Client-Request-Id"); got != "019d2233-e240-7162-992d-38df0a2a0e0d" {
218218 t.Fatalf("X-Client-Request-Id = %s, want %s", got, "019d2233-e240-7162-992d-38df0a2a0e0d")
219219 }
220− if got := headerValueCaseInsensitive(headers, "session_id"); got != "" {
221− t.Fatalf("session_id = %q, want empty", got)
220+ if got := headerValueCaseInsensitive(headers, "session_id"); got != "legacy-session" {
221+ t.Fatalf("session_id = %s, want legacy-session", got)
222+ }
223+ if _, ok := headers["session_id"]; !ok {
224+ t.Fatalf("expected lowercase session_id header key, got %#v", headers)
222225 }
223226 }
224227
@@ -341,16 +344,36 @@ func TestApplyCodexWebsocketHeadersPreservesExplicitAPIKeyUserAgent(t *testing.T · 116 unmodified lines
341344 }
342345 }
343346
344−func TestApplyCodexPromptCacheHeadersDoesNotSetDeprecatedConversationHeader(t *testing.T) {
347+func TestApplyCodexWebsocketHeadersUsesCanonicalAccountHeader(t *testing.T) {
348+ auth := &cliproxyauth.Auth{Provider: "codex", Metadata: map[string]any{"account_id": "acct-1"}}
349+
350+ headers := applyCodexWebsocketHeaders(context.Background(), http.Header{}, auth, "", nil)
351+
352+ if got := headerValueCaseInsensitive(headers, "ChatGPT-Account-ID"); got != "acct-1" {
353+ t.Fatalf("ChatGPT-Account-ID = %s, want acct-1", got)
354+ }
355+ values, ok := headers["ChatGPT-Account-ID"]
356+ if !ok {
357+ t.Fatalf("expected exact ChatGPT-Account-ID key, got %#v", headers)
358+ }
359+ if len(values) != 1 || values[0] != "acct-1" {
360+ t.Fatalf("ChatGPT-Account-ID values = %#v, want [acct-1]", values)
361+ }
362+}
363+
364+func TestApplyCodexPromptCacheHeadersSetsLowercaseSessionAndLegacyConversation(t *testing.T) {
345365 req := cliproxyexecutor.Request{Model: "gpt-5-codex", Payload: []byte(`{"prompt_cache_key":"cache-1"}`)}
346366
347367 _, headers := applyCodexPromptCacheHeaders("openai-response", req, []byte(`{"model":"gpt-5-codex"}`))
348368
349− if got := headerValueCaseInsensitive(headers, "session_id"); got != "" {
350− t.Fatalf("session_id = %q, want empty", got)
369+ if got := headerValueCaseInsensitive(headers, "session_id"); got != "cache-1" {
370+ t.Fatalf("session_id = %s, want cache-1", got)
371+ }
372+ if _, ok := headers["session_id"]; !ok {
373+ t.Fatalf("expected lowercase session_id key, got %#v", headers)
351374 }
352− if got := headers.Get("Conversation_id"); got != "" {
353− t.Fatalf("Conversation_id = %q, want empty", got)
375+ if got := headers.Get("Conversation_id"); got != "cache-1" {
376+ t.Fatalf("Conversation_id = %s, want cache-1", got)
354377 }
355378 }
356379
@@ -379,8 +402,8 @@ func TestApplyCodexWebsocketHeadersIdentityConfuseRemapsPromptCacheKey(t *testin · 22 unmodified lines
379402 if gotKey := gjson.GetBytes(body, "prompt_cache_key").String(); gotKey != expectedPromptCacheKey {
380403 t.Fatalf("prompt_cache_key = %q, want %q", gotKey, expectedPromptCacheKey)
381404 }
382− if gotSession := headerValueCaseInsensitive(headers, "session_id"); gotSession != "" {
383− t.Fatalf("session_id = %q, want empty", gotSession)
405+ if gotSession := headerValueCaseInsensitive(headers, "session_id"); gotSession != expectedPromptCacheKey {
406+ t.Fatalf("session_id = %q, want %q", gotSession, expectedPromptCacheKey)
384407 }
385408 if gotRequestID := headers.Get("X-Client-Request-Id"); gotRequestID != expectedPromptCacheKey {
386409 t.Fatalf("X-Client-Request-Id = %q, want %q", gotRequestID, expectedPromptCacheKey)
@@ -388,8 +411,8 @@ func TestApplyCodexWebsocketHeadersIdentityConfuseRemapsPromptCacheKey(t *testin · 1 unmodified line
388411 if gotThreadID := headers.Get("Thread-Id"); gotThreadID != expectedPromptCacheKey {
389412 t.Fatalf("Thread-Id = %q, want %q", gotThreadID, expectedPromptCacheKey)
390413 }
391− if gotConversation := headers.Get("Conversation_id"); gotConversation != "" {
392− t.Fatalf("Conversation_id = %q, want empty", gotConversation)
414+ if gotConversation := headers.Get("Conversation_id"); gotConversation != expectedPromptCacheKey {
415+ t.Fatalf("Conversation_id = %q, want %q", gotConversation, expectedPromptCacheKey)
393416 }
394417 if gotWindowID := headers.Get("X-Codex-Window-Id"); gotWindowID != expectedPromptCacheKey+":0" {
395418 t.Fatalf("X-Codex-Window-Id = %q, want %q", gotWindowID, expectedPromptCacheKey+":0")
sdk/api/handlers/openai/openai_responses_websocket_toolcall_repair.go+6−0
@@ -147,6 +147,12 @@ func websocketDownstreamSessionKey(req *http.Request) string { · 146 unmodified lines
147147 return sessionID
148148 }
149149 }
150+ if sessionID := strings.TrimSpace(req.Header.Get("Session-Id")); sessionID != "" {
151+ return sessionID
152+ }
153+ if sessionID := strings.TrimSpace(req.Header.Get("Session_id")); sessionID != "" {
154+ return sessionID
155+ }
150156 return ""
151157 }
152158
sdk/cliproxy/auth/selector.go+27−15
@@ -471,11 +471,12 @@ func NewSessionAffinitySelectorWithConfig(cfg SessionAffinityConfig) *SessionAff · 470 unmodified lines
471471 // Priority for session ID extraction:
472472 // 1. metadata.user_id (Claude Code format with _session_{uuid}) - highest priority
473473 // 2. X-Session-ID header
474−// 3. X-Amp-Thread-Id header (Amp CLI thread ID)
475−// 4. X-Client-Request-Id header (PI)
476−// 5. metadata.user_id (non-Claude Code format)
477−// 6. conversation_id field in request body
478−// 7. Stable hash from first few messages content (fallback)
474+// 3. Session_id header (Codex)
475+// 4. X-Amp-Thread-Id header (Amp CLI thread ID)
476+// 5. X-Client-Request-Id header (PI)
477+// 6. metadata.user_id (non-Claude Code format)
478+// 7. conversation_id field in request body
479+// 8. Stable hash from first few messages content (fallback)
479480 //
480481 // Note: The cache key includes provider, session ID, and model to handle cases where
481482 // a session uses multiple models (e.g., gemini-2.5-pro and gemini-3-flash-preview)
@@ -572,11 +573,12 @@ func (s *SessionAffinitySelector) InvalidateAuth(authID string) { · 90 unmodified lines
572573 // Priority order:
573574 // 1. metadata.user_id (Claude Code format with _session_{uuid}) - highest priority for Claude Code clients
574575 // 2. X-Session-ID header
575−// 3. X-Amp-Thread-Id header (Amp CLI thread ID)
576−// 4. X-Client-Request-Id header (PI)
577−// 5. metadata.user_id (non-Claude Code format)
578−// 6. conversation_id field in request body
579−// 7. Stable hash from first few messages content (fallback)
576+// 3. Session_id header (Codex)
577+// 4. X-Amp-Thread-Id header (Amp CLI thread ID)
578+// 5. X-Client-Request-Id header (PI)
579+// 6. metadata.user_id (non-Claude Code format)
580+// 7. conversation_id field in request body
581+// 8. Stable hash from first few messages content (fallback)
580582 func ExtractSessionID(headers http.Header, payload []byte, metadata map[string]any) string {
581583 primary, _ := extractSessionIDs(headers, payload, metadata)
582584 return primary
@@ -612,14 +614,24 @@ func extractSessionIDs(headers http.Header, payload []byte, metadata map[string] · 29 unmodified lines
612614 }
613615 }
614616
615− // 3. X-Amp-Thread-Id header (Amp CLI thread ID)
617+ // 3. Session_id header (Codex)
618+ if headers != nil {
619+ if sid := headers.Get("Session-Id"); sid != "" {
620+ return "codex:" + sid, ""
621+ }
622+ if sid := headers.Get("Session_id"); sid != "" {
623+ return "codex:" + sid, ""
624+ }
625+ }
626+
627+ // 4. X-Amp-Thread-Id header (Amp CLI thread ID)
616628 if headers != nil {
617629 if tid := headers.Get("X-Amp-Thread-Id"); tid != "" {
618630 return "amp:" + tid, ""
619631 }
620632 }
621633
622− // 4. X-Client-Request-Id header (PI)
634+ // 5. X-Client-Request-Id header (PI)
623635 if headers != nil {
624636 if rid := headers.Get("X-Client-Request-Id"); rid != "" {
625637 return "clientreq:" + rid, ""
@@ -630,18 +642,18 @@ func extractSessionIDs(headers http.Header, payload []byte, metadata map[string] · 4 unmodified lines
630642 return "", ""
631643 }
632644
633− // 5. metadata.user_id (non-Claude Code format)
645+ // 6. metadata.user_id (non-Claude Code format)
634646 userID := gjson.GetBytes(payload, "metadata.user_id").String()
635647 if userID != "" {
636648 return "user:" + userID, ""
637649 }
638650
639− // 6. conversation_id field
651+ // 7. conversation_id field
640652 if convID := gjson.GetBytes(payload, "conversation_id").String(); convID != "" {
641653 return "conv:" + convID, ""
642654 }
643655
644− // 7. Hash-based fallback from message content
656+ // 8. Hash-based fallback from message content
645657 return extractMessageHashIDs(payload)
646658 }
647659
sdk/cliproxy/auth/selector_test.go+7−6
@@ -776,15 +776,16 @@ func TestExtractSessionID_Headers(t *testing.T) { · 775 unmodified lines
776776 }
777777 }
778778
779−func TestExtractSessionID_IgnoresCodexSessionIDHeader(t *testing.T) {
779+func TestExtractSessionID_CodexSessionIDHeader(t *testing.T) {
780780 t.Parallel()
781781
782782 headers := make(http.Header)
783783 headers.Set("Session_id", "codex-session-123")
784784
785785 got := ExtractSessionID(headers, nil, nil)
786− if got != "" {
787− t.Errorf("ExtractSessionID() with deprecated Session_id = %q, want empty", got)
786+ want := "codex:codex-session-123"
787+ if got != want {
788+ t.Errorf("ExtractSessionID() with Session_id = %q, want %q", got, want)
788789 }
789790 }
790791
@@ -801,7 +802,7 @@ func TestExtractSessionID_ClientRequestIDHeader(t *testing.T) { · 10 unmodified lines
801802 }
802803 }
803804
804−func TestExtractSessionID_ClientRequestIDIgnoresDeprecatedCodexSessionID(t *testing.T) {
805+func TestExtractSessionID_CodexSessionIDPriorityOverClientRequestID(t *testing.T) {
805806 t.Parallel()
806807
807808 headers := make(http.Header)
@@ -809,9 +810,9 @@ func TestExtractSessionID_ClientRequestIDIgnoresDeprecatedCodexSessionID(t *test · 1 unmodified line
809810 headers.Set("Session_id", "codex-session-456")
810811
811812 got := ExtractSessionID(headers, nil, nil)
812− want := "clientreq:pi-session-123"
813+ want := "codex:codex-session-456"
813814 if got != want {
814− t.Errorf("ExtractSessionID() = %q, want %q (deprecated Session_id should be ignored)", got, want)
815+ t.Errorf("ExtractSessionID() = %q, want %q (Session_id should take priority over X-Client-Request-Id)", got, want)
815816 }
816817 }
817818

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