From 40d9c0964b05f192f3b31ab155aa037eebf42593 Mon Sep 17 00:00:00 2001 From: kiuber Date: Sat, 31 Oct 2026 06:38:40 -0700 Subject: [PATCH] fix(openai): apply Codex fingerprint convergence in the WebSocket HTTP bridge proxyOpenAIWSHTTPBridgeTurn built its upstream request without staging the converged Codex fingerprint IDs, unlike Forward and forwardOpenAIPassthrough. The bridge therefore sent different device-fingerprint headers than the HTTP paths for the same account, and when the client sent no prompt_cache_key the request carried no session_id at all, so upstream prompt-cache routing was effectively random and multi-turn requests rarely hit the cache. Stage the IDs (and rewrite client_metadata) the same way forwardOpenAIPassthrough does, clearing any stale IDs first. Co-Authored-By: Claude Opus 5.5 Co-authored-by: multica-agent --- .../internal/service/openai_ws_http_bridge.go | 21 ++++++++++ .../service/openai_ws_http_bridge_test.go | 39 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/backend/internal/service/openai_ws_http_bridge.go b/backend/internal/service/openai_ws_http_bridge.go index 066c769c20..63891b8ee8 100644 --- a/backend/internal/service/openai_ws_http_bridge.go +++ b/backend/internal/service/openai_ws_http_bridge.go @@ -449,6 +449,27 @@ func (s *OpenAIGatewayService) proxyOpenAIWSHTTPBridgeTurn( if err != nil { return nil, fmt.Errorf("prepare http bridge body: %w", err) } + // Codex fingerprint convergence, same as Forward / forwardOpenAIPassthrough. Builders only read + // staged IDs; without staging, the bridge sends no account-level session_id, so clients without + // prompt_cache_key get random upstream prompt-cache routing and rarely hit the cache. + stageCodexFingerprintIDs(c, nil) + if account.UsesOpenAICodexProtocol() { + var clientHeaders http.Header + if c != nil && c.Request != nil { + clientHeaders = c.Request.Header + } + fpIDs := resolveCodexFingerprintIDsFromRequest(account, clientHeaders) + if fpIDs != nil { + fpBody, fpChanged, fpErr := applyCodexFingerprintClientMetadataRaw(body, fpIDs) + if fpErr != nil { + return nil, fmt.Errorf("apply http bridge codex fingerprint: %w", fpErr) + } + if fpChanged { + body = fpBody + } + } + stageCodexFingerprintIDs(c, fpIDs) + } grokIntentSourceBody := append([]byte(nil), body...) _, grokExplicitToolsField := openAIWSHTTPBridgeRawField(grokIntentSourceBody, "tools") grokExplicitToolIntent := account.Platform == PlatformGrok && hasGrokResponsesToolIntent(grokIntentSourceBody) diff --git a/backend/internal/service/openai_ws_http_bridge_test.go b/backend/internal/service/openai_ws_http_bridge_test.go index c8c654e6c6..b65c1ac68d 100644 --- a/backend/internal/service/openai_ws_http_bridge_test.go +++ b/backend/internal/service/openai_ws_http_bridge_test.go @@ -2352,3 +2352,42 @@ func TestOpenAIWSHTTPBridge_IdleTimeoutClosesClientSession(t *testing.T) { } require.Len(t, upstream.bodies, 1, "an idle client must not leave a continuation request running") } + +func TestProxyOpenAIWSHTTPBridgeTurn_StagesCodexFingerprint(t *testing.T) { + gin.SetMode(gin.TestMode) + run := func(account *Account) *http.Request { + sse := `data: {"type":"response.completed","response":{"id":"resp_fp","model":"gpt-5.5","status":"completed","usage":{"input_tokens":1,"output_tokens":1}}}` + "\n\n" + upstream := &httpUpstreamRecorder{resp: &http.Response{ + StatusCode: http.StatusOK, + Header: http.Header{"Content-Type": []string{"text/event-stream"}}, + Body: io.NopCloser(strings.NewReader(sse)), + }} + svc := &OpenAIGatewayService{ + cfg: &config.Config{Gateway: config.GatewayConfig{MaxLineSize: defaultMaxLineSize}}, + httpUpstream: upstream, + } + c, _ := gin.CreateTestContext(httptest.NewRecorder()) + c.Request = httptest.NewRequest(http.MethodGet, "/v1/responses", nil) + payload := []byte(`{"type":"response.create","model":"gpt-5.5","store":false,"input":"hi"}`) + _, err := svc.proxyOpenAIWSHTTPBridgeTurn( + context.Background(), c, account, "test-token", payload, len(payload), + "gpt-5.5", "", "", "", "", 1, + func([]byte) error { return nil }, + ) + require.NoError(t, err) + require.NotNil(t, upstream.lastReq) + return upstream.lastReq + } + + full := newTestOAuthAccount(7101, map[string]any{"codex_fingerprint_mode": "full"}) + ids := resolveCodexFingerprintIDsFromRequest(full, nil) + require.NotNil(t, ids) + first := run(full) + require.Equal(t, ids.sessionID, first.Header.Get("session_id")) + require.Equal(t, ids.installationID, first.Header.Get("x-codex-installation-id")) + // Every bridge request carries the same account-level session_id, keeping upstream cache routing stable + require.Equal(t, first.Header.Get("session_id"), run(full).Header.Get("session_id")) + + off := newTestOAuthAccount(7102, map[string]any{"codex_fingerprint_mode": "off"}) + require.Empty(t, run(off).Header.Get("session_id"), "accounts without convergence get no session_id") +}