Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: a Codex request's client_metadata is read without decoding the whole body (#3394)

yetoned792e7ce4d21
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)
+ }
+ }
+}