gateway: input scanners read a request's items in place, not copied (#3383)
Seven readers of a Responses request's input (the seal stripper and withoutKinds in affinity, hasSealedAgentMessage, joinToolRuns, orphanedToolOutputs, pairToolItems, withReasoningText) and the prompt builder decoded `input` into a []json.RawMessage. fillJSONIn clones each RawMessage under a sixteenth of the held body, so every item of a long Codex history was copied once per reader, per request. None of them keeps an item past its return (they marshal new output or assign into the slice), so inputList hands them elementsOf's capped subslices of the raw input instead. hasSealedAgentMessage reads an item's content the same way. Run at83234fe92+ this change: - BenchmarkZZCodexStream (a 680 KB Codex request through the gateway, a probe not committed), -benchtime 30x -count 3: before 20,077,746 B/op 76,687 allocs/op; after 16,417,942 / 16,430,955 / 16,469,180 B/op, ~72,780 allocs/op (-18% bytes). - Real account, sandbox HOME with only the DeepSeek provider, through a recording proxy to api.deepseek.com: 5 Codex-UA /v1/responses requests each with a user message, reasoning, function_call, function_call_output and assistant message in the history, on the old and new binary. All 10 answered "ok"; the 5 bodies sent upstream are byte-identical between old and new. - gofmt -l clean; GOOS=linux and windows go vet -tags nogui ./internal/gateway ok. - go test -timeout 45m ./internal/gateway/: ok (667s). Rebased onto1537817e0: build, both vets and go test -run 'Responses|Affin|Seal|Prompt|Tool|Codex|Reasoning|Orphan|DecodeJSON' ./internal/gateway/ ok (64s). Not tried: the full ./... suite at this head (it runs after the push).
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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"`
|
||||
|
||||
@@ -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++
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user