From cf2862be51626d7f3f0e5d619934772b28bd1173 Mon Sep 17 00:00:00 2001 From: Minjie Shi Date: Mon, 23 Nov 2026 04:46:24 +0800 Subject: [PATCH] perf(openai): read Codex metadata without copying request history --- .../service/openai_codex_account_identity.go | 2 +- .../openai_codex_account_identity_test.go | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/backend/internal/service/openai_codex_account_identity.go b/backend/internal/service/openai_codex_account_identity.go index c1c790a1b831..a1f267d8f715 100644 --- a/backend/internal/service/openai_codex_account_identity.go +++ b/backend/internal/service/openai_codex_account_identity.go @@ -235,7 +235,7 @@ func applyCodexAccountIdentityClientMetadataRaw(body []byte, account *Account, a if len(body) == 0 || codexAccountIdentityNamespace(account) == "" { return body, false, nil } - root := gjson.ParseBytes(body) + root := parseRawJSONView(body) if !root.IsObject() { return body, false, nil } diff --git a/backend/internal/service/openai_codex_account_identity_test.go b/backend/internal/service/openai_codex_account_identity_test.go index 358f58c21fcf..7a96c6ec5f6d 100644 --- a/backend/internal/service/openai_codex_account_identity_test.go +++ b/backend/internal/service/openai_codex_account_identity_test.go @@ -31,6 +31,7 @@ func TestCodexRequestBodyIdentityNamespaceIsStablePerOAuthAccount(t *testing.T) account11 := &Account{ID: 11, Platform: PlatformOpenAI, Type: AccountTypeOAuth, Credentials: map[string]any{"chatgpt_account_id": "chatgpt-account-11"}} account19 := &Account{ID: 19, Platform: PlatformOpenAI, Type: AccountTypeOAuth, Credentials: map[string]any{"chatgpt_account_id": "chatgpt-account-19"}} + original := bytes.Clone(body) first, changed, err := applyCodexAccountIdentityClientMetadataRaw(body, account11, 77, "") require.NoError(t, err) require.True(t, changed) @@ -62,6 +63,35 @@ func TestCodexRequestBodyIdentityNamespaceIsStablePerOAuthAccount(t *testing.T) for _, field := range []string{"installation_id", "session_id", "thread_id", "turn_id", "window_id"} { require.NotEqual(t, embeddedFirst[field], embeddedSecond[field], field) } + require.Equal(t, original, body) + saved := bytes.Clone(first) + clear(body) + require.Equal(t, saved, first, "rewritten body must survive reuse of the input") +} + +func BenchmarkCodexAccountIdentityMetadataHistory(b *testing.B) { + account := &Account{Platform: PlatformOpenAI, Type: AccountTypeOAuth, Credentials: map[string]any{"chatgpt_account_id": "fixture-account"}} + for _, tc := range []struct { + name string + size int + fields string + }{ + {"4KiB", 4 << 10, ""}, + {"1MiB", 1 << 20, ""}, + {"1MiBPromptCacheKey", 1 << 20, `,"prompt_cache_key":"fixture-session"`}, + } { + body := []byte(`{"input":[{"role":"user","content":"` + strings.Repeat("x", tc.size) + `"}]` + tc.fields + `}`) + b.Run(tc.name, func(b *testing.B) { + b.ReportAllocs() + b.SetBytes(int64(len(body))) + for b.Loop() { + _, changed, err := applyCodexAccountIdentityClientMetadataRaw(body, account, 77) + if err != nil || changed != (tc.fields != "") { + b.Fatalf("changed=%t, err=%v", changed, err) + } + } + }) + } } func TestCodexAccountIdentityNamespaceUsesStableCredentialSource(t *testing.T) {