Merge pull request #8464 from kiuber/fix/ws-http-bridge-codex-fingerprint

fix(openai): apply Codex fingerprint convergence in the WebSocket HTTP bridge so multi-turn requests hit the prompt cache
This commit is contained in:
Wesley Liddick authored and GitHub committed 2026-11-07 10:58:12 +08:00
commit a2f7c12cdd
2 files changed
+60

No files matched your search

@@ -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)
@@ -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")
}