From d792e7ce4d2192d67c9a58031bffb112506dd367 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 25 Oct 2026 00:39:25 +0800 Subject: [PATCH] gateway: a Codex request's client_metadata is read without decoding the whole body (#3394) requestSessionMetadata is asked four or five times for every Codex request (the title, description and prediction checks, the subagent pick, serve). Each time it ran json.Unmarshal over the whole body to read one top-level member, so a long conversation was walked again and again. It now takes client_metadata with topMember, which checks and splits a long body once, and decodes only that member. What a body gives is unchanged: a body that isn't an object, or whose client_metadata isn't an object or null, still falls back to the x-codex-turn-metadata header. One difference: Go's struct decoding matched the name without regard to case, topMember matches it exactly; Codex sends it lower case. Run: - BenchmarkRequestSessionMetadata (5.8MB body): 12.7ms/op before, 0.50ms/op after (-count 3 each). - Real DeepSeek account in a sandbox HOME, magpie serve on 3482, 12 streamed /v1/responses requests of a synthetic 1.5MB Codex body with client_metadata (codexload.py, 2 at a time), magpie's CPU time read with ps, two rounds each: a84d2ec80 1.01s and 0.95s, this 0.68s and 0.67s, about 84ms -> 56ms a request. All 48 answered 200. - New TestSessionMetadataOddBodies (string, null, cut-short, array and nested client_metadata, a duplicate member, long bodies): passes on the old code and the new, so it holds the behaviour, not the speed. - go test ./internal/gateway -run 'Session|Title|Parent|Prediction| Description|Subagent|CallKind|Fork|Metadata': ok. The whole gateway package was not run before the push. - gofmt -l clean; GOOS=linux and windows go vet -tags nogui ./internal/gateway ok. --- internal/gateway/session_parent.go | 36 ++++++++++++++++--------- internal/gateway/session_parent_test.go | 23 ++++++++++++++++ 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/internal/gateway/session_parent.go b/internal/gateway/session_parent.go index 3b58ca275..470da1b87 100644 --- a/internal/gateway/session_parent.go +++ b/internal/gateway/session_parent.go @@ -33,12 +33,13 @@ type sessionMetadata struct { } func requestSessionMetadata(h http.Header, body []byte) sessionMetadata { - var envelope struct { - Metadata map[string]json.RawMessage `json:"client_metadata"` - } + var metadata map[string]json.RawMessage var m sessionMetadata - if bytes.Contains(body, []byte("client_metadata")) && json.Unmarshal(body, &envelope) == nil { - if raw := envelope.Metadata["x-codex-turn-metadata"]; len(raw) > 0 { + // Every Codex request asks this four or five times, so only the member + // is decoded: decoding the whole body walked the conversation each time + // (#3394). A long body is checked and split once (topMember). + if bytes.Contains(body, []byte("client_metadata")) && metadataOf(body, &metadata) { + if raw := metadata["x-codex-turn-metadata"]; len(raw) > 0 { var text string if json.Unmarshal(raw, &text) == nil { raw = []byte(text) @@ -46,16 +47,16 @@ func requestSessionMetadata(h http.Header, body []byte) sessionMetadata { if json.Unmarshal(raw, &m) == nil { return m } - } else if len(envelope.Metadata) > 0 { - _ = json.Unmarshal(envelope.Metadata["turn_id"], &m.Turn) + } else if len(metadata) > 0 { + _ = json.Unmarshal(metadata["turn_id"], &m.Turn) // Compatibility projections used by clients without the full blob. - _ = json.Unmarshal(envelope.Metadata["thread_source"], &m.Source) - _ = json.Unmarshal(envelope.Metadata["parent_thread_id"], &m.Parent) + _ = json.Unmarshal(metadata["thread_source"], &m.Source) + _ = json.Unmarshal(metadata["parent_thread_id"], &m.Parent) if m.Parent == "" { - _ = json.Unmarshal(envelope.Metadata["x-codex-parent-thread-id"], &m.Parent) + _ = json.Unmarshal(metadata["x-codex-parent-thread-id"], &m.Parent) } - _ = json.Unmarshal(envelope.Metadata["forked_from_thread_id"], &m.Forked) - _ = json.Unmarshal(envelope.Metadata["installation_id"], &m.Installation) + _ = json.Unmarshal(metadata["forked_from_thread_id"], &m.Forked) + _ = json.Unmarshal(metadata["installation_id"], &m.Installation) if m.Source != "" || m.Parent != "" || m.Forked != "" { return m } @@ -72,6 +73,17 @@ func requestSessionMetadata(h http.Header, body []byte) sessionMetadata { return m } +// metadataOf decodes body's client_metadata into md, as decoding body into +// a struct with that one field does: false when body isn't a JSON object or +// the member isn't an object or null. +func metadataOf(body []byte, md *map[string]json.RawMessage) bool { + raw, found, ok := topMember(body, "client_metadata") + if !ok { + return false + } + return !found || json.Unmarshal(raw, md) == nil +} + func requestCallKind(h http.Header, m sessionMetadata) string { if h.Get("x-openai-subagent") != "" || h.Get("x-openai-memgen-request") != "" { return callKind(h) diff --git a/internal/gateway/session_parent_test.go b/internal/gateway/session_parent_test.go index e7920e901..d6adec870 100644 --- a/internal/gateway/session_parent_test.go +++ b/internal/gateway/session_parent_test.go @@ -108,3 +108,26 @@ func BenchmarkRequestSessionMetadata(b *testing.B) { } } } + +// Only client_metadata is decoded now (#3394); what a body that isn't the +// usual shape gives stays what decoding the whole body gave. +func TestSessionMetadataOddBodies(t *testing.T) { + h := http.Header{} + h.Set("x-codex-turn-metadata", `{"turn_id":"from-header","parent_thread_id":"head"}`) + long := `"input":"` + strings.Repeat("x", 300000) + `",` + for _, tc := range []struct{ name, body, turn, parent string }{ + {"metadata a string", `{"client_metadata":"x"}`, "from-header", "head"}, + {"metadata null", `{"client_metadata":null}`, "from-header", "head"}, + {"body cut short", `{"client_metadata":{"parent_thread_id":"body"}`, "from-header", "head"}, + {"body an array", `[{"client_metadata":{"parent_thread_id":"body"}}]`, "from-header", "head"}, + {"nested name only", `{"input":[{"client_metadata":{"parent_thread_id":"body"}}]}`, "from-header", "head"}, + {"last of two", `{"client_metadata":{"parent_thread_id":"a"},"client_metadata":{"parent_thread_id":"b"}}`, "", "b"}, + {"long body", `{` + long + `"client_metadata":{"turn_id":"t","parent_thread_id":"body"}}`, "t", "body"}, + {"long body cut short", `{` + long + `"client_metadata":{"turn_id":"t","parent_thread_id":"body"}`, "from-header", "head"}, + } { + m := requestSessionMetadata(h, []byte(tc.body)) + if m.Turn != tc.turn || m.Parent != tc.parent { + t.Errorf("%s: turn %q parent %q, want %q %q", tc.name, m.Turn, m.Parent, tc.turn, tc.parent) + } + } +}