diff --git a/internal/gateway/affinity.go b/internal/gateway/affinity.go index a30da86dd..d2ba0a859 100644 --- a/internal/gateway/affinity.go +++ b/internal/gateway/affinity.go @@ -801,8 +801,8 @@ func withoutRefused(key, who string, body []byte) ([]byte, bool) { if decodeJSON(body, &q) != nil { return nil, false } - var items []json.RawMessage - if decodeJSON(q["input"], &items) != nil { + items, isList := inputList(q["input"]) + if !isList { return nil, false } kept := items[:0:0] @@ -855,8 +855,8 @@ func withoutKinds(body []byte, kinds ...string) ([]byte, bool) { if decodeJSON(body, &q) != nil { return nil, false } - var items []json.RawMessage - if decodeJSON(q["input"], &items) != nil { + items, isList := inputList(q["input"]) + if !isList { return nil, false } kept := items[:0:0] diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.go index 50b86aaf9..8cf8be00c 100644 --- a/internal/gateway/codex_backend.go +++ b/internal/gateway/codex_backend.go @@ -504,19 +504,19 @@ func hasSealedAgentMessage(body []byte) bool { if decodeJSON(body, &request) != nil { return false } - var items []json.RawMessage - if decodeJSON(request["input"], &items) != nil { + items, isList := inputList(request["input"]) + if !isList { return false } for _, raw := range items { var item struct { - Type string `json:"type"` - Content []json.RawMessage `json:"content"` + Type string `json:"type"` + Content json.RawMessage `json:"content"` } if decodeJSON(raw, &item) != nil || item.Type != "agent_message" { continue } - for _, content := range item.Content { + for _, content := range elementsOf(trimJSON(item.Content)) { var part struct { Type string `json:"type"` EncryptedContent string `json:"encrypted_content"` diff --git a/internal/gateway/fastjson.go b/internal/gateway/fastjson.go index 8523f1054..1b0c6e5d1 100644 --- a/internal/gateway/fastjson.go +++ b/internal/gateway/fastjson.go @@ -556,6 +556,26 @@ func elementsOf(raw []byte) []json.RawMessage { return out } +// inputList is a request's input list, raw from a decoded body (so valid +// JSON), for a reader that looks at its items and keeps none past its +// return: elementsOf, so no item is copied. Decoding it as a +// []json.RawMessage copied every item, since each is under a sixteenth of +// the body (heldShareMost): 3.7 of the 20 MB a 680 KB Codex request +// allocated went to such copies (#3383). ok is what that decode's error +// was: false when the input is missing or neither a list nor null. +func inputList(raw json.RawMessage) ([]json.RawMessage, bool) { + raw = trimJSON(raw) + switch { + case len(raw) == 0: + return nil, false + case raw[0] == '[': + return elementsOf(raw), true + case string(raw) == "null": + return nil, true + } + return nil, false +} + // skipJSONString is the end of the valid JSON string at i. func skipJSONString(data []byte, i int) int { i++ diff --git a/internal/gateway/prompt.go b/internal/gateway/prompt.go index 5796af3bd..51147e56d 100644 --- a/internal/gateway/prompt.go +++ b/internal/gateway/prompt.go @@ -830,8 +830,7 @@ func (b *promptBuilder) responses(body []byte) bool { b.userText(s) return true } - var items []json.RawMessage - _ = decodeJSON(req.Input, &items) + items, _ := inputList(req.Input) for _, it := range items { b.responsesItem(it) } diff --git a/internal/gateway/responses.go b/internal/gateway/responses.go index 64f586701..94d98d4a9 100644 --- a/internal/gateway/responses.go +++ b/internal/gateway/responses.go @@ -597,8 +597,8 @@ func joinToolRuns(body []byte) []byte { if decodeJSON(body, &q) != nil { return body } - var input []json.RawMessage - if decodeJSON(q["input"], &input) != nil { + input, isList := inputList(q["input"]) + if !isList { return body } type item struct { @@ -699,8 +699,8 @@ func orphanedToolOutputs(body []byte) []byte { if decodeJSON(body, &q) != nil { return body } - var input []json.RawMessage - if decodeJSON(q["input"], &input) != nil { + input, isList := inputList(q["input"]) + if !isList { return body } changed := false @@ -793,8 +793,8 @@ func pairToolItems(body []byte) []byte { if decodeJSON(body, &q) != nil { return body } - var input []json.RawMessage - if decodeJSON(q["input"], &input) != nil { + input, isList := inputList(q["input"]) + if !isList { return body } type item struct { @@ -917,8 +917,8 @@ func withReasoningText(body []byte) []byte { if decodeJSON(body, &q) != nil { return body } - var items []json.RawMessage - if decodeJSON(q["input"], &items) != nil { + items, isList := inputList(q["input"]) + if !isList { return body } changed := false