gateway: a translated body lets its built conversation go as it fills (#4004)

marshalBody wrote a translated request's list (Chat's and Anthropic's
messages, Responses' input) an item at a time, but the list itself, the
conversation the builder made, strings joined from the request's parts
among it, stayed held in the body's map until the whole body was
written: at the body's peak it sat beside the written items and the
body. The builder names that list as spent, and each item is set to its
zero value once written. Every caller's map is its own and dropped after
the call (buildChat, buildAnthropic, buildResponses); buildGemini spends
nothing. Keys are checked before anything is spent, so the json.Marshal
fallback for an odd key still sees the whole map.

Measured on mote, TestZZPeak4004-style scratch harness (not committed):
a 45 MiB Codex conversation sent Responses -> Chat through the gateway,
GOGC=10, HeapInuse sampled every 2 ms, 3 runs each:
  base 425a61322: peak 302, 301, 301 MiB (base heap 54 MiB)
  this change:    peak 278, 271, 273 MiB
Live at send is the same, 149 MiB above base (the request, its parse,
the body), as the conversation was garbage by then either way.

Tests: TestMarshalBodyLetsSpentGo (new) writes a list of probes, each
looking whether the item before it was let go, and that a list not
named spent is unchanged. Broken by hand (spend never zeroing): red,
"items [0 1 2 3] were still held when the next was written".
TestMarshalBodyIsJSONMarshal passes with the messages spent.

Run on mote at 425a61322 + this change: GOOS=linux/windows/darwin go
vet -tags nogui ./internal/gateway/: ok; go test -count=1 -timeout 45m
./internal/gateway/: ok (537.8s). Rebased onto f6e7214af, whose only
gateway change is a host in buildChat's inlineThink; at the pushed head:
the three vets again ok, and go test -run 'TestMarshalBody|MiniMax|
Minimax|TestChat|TestResponses|TestAnthropic|Translat' ./internal/gateway/:
ok (31.8s).

Not tried: a real upstream; the bytes sent are the same, which
TestMarshalBodyIsJSONMarshal holds to json.Marshal's.
This commit is contained in:
yetone
2026-10-29 02:17:47 +08:00
parent f6e7214af7
commit 8eb7f5565b
6 changed files with 64 additions and 11 deletions
+1 -1
View File
@@ -1099,7 +1099,7 @@ func anthropicBody(r *Request, model, host string, replay bool) []byte {
out["tool_choice"] = tc
}
}
b, _ := marshalBody(out)
b, _ := marshalBody(out, "messages")
return b
}
+1 -1
View File
@@ -585,7 +585,7 @@ func buildChat(r *Request, model, host string, rejectTemp bool) []byte {
// OpenRouter's own search, for any of its models
out["plugins"] = []map[string]any{{"id": "web"}}
}
b, _ := marshalBody(out)
b, _ := marshalBody(out, "messages")
return b
}
+18 -4
View File
@@ -1447,16 +1447,27 @@ func getIn(body []byte, path string) gjson.Result {
// 316 MiB, the buffers it outgrew being garbage until the next GC (#4004,
// jihuayu's profile on a 2 GiB server). Here no buffer outgrows the
// largest item. A value json.Marshal refuses gets json.Marshal's answer.
func marshalBody(out map[string]any) ([]byte, error) {
//
// spent names the list the builder made for this body alone (its
// messages, its input items): each of its items is let go once written,
// so the built conversation, strings joined from the request's parts
// among it, is garbage while the body fills instead of held beside it to
// the end (#4004). A value json.Marshal refuses is refused still, and the
// list is then left part spent.
func marshalBody(out map[string]any, spent string) ([]byte, error) {
keys := make([]string, 0, len(out))
for k := range out {
// a key json.Marshal writes another way, before anything is spent
if _, ok := appendJSONKey(nil, k, true); !ok {
return json.Marshal(out)
}
keys = append(keys, k)
}
slices.Sort(keys)
vals := make([][]json.RawMessage, len(keys))
n := 2
for i, k := range keys {
items, ok := marshalItems(out[k])
items, ok := marshalItems(out[k], k == spent)
if !ok {
return json.Marshal(out)
}
@@ -1507,8 +1518,8 @@ func isSplitList(v any) bool {
// marshalItems is v as json.Marshal writes it: its items each on their
// own for a list isSplitList takes, or one piece; false when json.Marshal
// refuses any of it.
func marshalItems(v any) ([]json.RawMessage, bool) {
// refuses any of it. spend sets each item written to its zero value.
func marshalItems(v any, spend bool) ([]json.RawMessage, bool) {
if !isSplitList(v) {
b, err := json.Marshal(v)
return []json.RawMessage{b}, err == nil
@@ -1523,6 +1534,9 @@ func marshalItems(v any) ([]json.RawMessage, bool) {
return nil, false
}
items[i] = b
if spend {
rv.Index(i).SetZero()
}
}
return items, true
}
+1 -1
View File
@@ -116,7 +116,7 @@ func buildGemini(r *Request, model string) ([]byte, error) {
return nil, err
}
req["model"] = model
return marshalBody(req)
return marshalBody(req, "")
}
// geminiRequest is the request inside the Code Assist envelope that
+42 -3
View File
@@ -33,7 +33,7 @@ func TestMarshalBodyIsJSONMarshal(t *testing.T) {
}
for i, b := range bodies {
want, werr := json.Marshal(b)
got, gerr := marshalBody(b)
got, gerr := marshalBody(b, "messages")
if (werr == nil) != (gerr == nil) || !bytes.Equal(got, want) {
t.Errorf("body %d:\n got %s (%v)\nwant %s (%v)", i, got, gerr, want, werr)
}
@@ -43,7 +43,7 @@ func TestMarshalBodyIsJSONMarshal(t *testing.T) {
{"x": math.Inf(1)},
{"ch": []any{"a", make(chan int)}},
} {
if _, err := marshalBody(bad); err == nil {
if _, err := marshalBody(bad, ""); err == nil {
t.Errorf("marshalBody(%v) took what json.Marshal refuses", bad)
}
}
@@ -72,9 +72,48 @@ func TestMarshalBodyAllocatesTwoBodies(t *testing.T) {
return m.TotalAlloc - before, n
}
stdAlloc, n := peak(func() []byte { b, _ := json.Marshal(body); return b })
ownAlloc, _ := peak(func() []byte { b, _ := marshalBody(body); return b })
ownAlloc, _ := peak(func() []byte { b, _ := marshalBody(body, ""); return b })
t.Logf("%d MiB body: json.Marshal allocated %d MiB, marshalBody %d MiB", n>>20, stdAlloc>>20, ownAlloc>>20)
if ownAlloc > uint64(n)*5/2 {
t.Errorf("marshalBody allocated %d MiB for a %d MiB body, want at most 2.5x", ownAlloc>>20, n>>20)
}
}
// spentProbe is an item of a list that, as it is written, looks whether
// the item before it was let go.
type spentProbe struct {
list *[]*spentProbe
i int
held *[]int
}
func (p *spentProbe) MarshalJSON() ([]byte, error) {
if p.i > 0 && (*p.list)[p.i-1] != nil {
*p.held = append(*p.held, p.i-1)
}
return []byte(`1`), nil
}
// The conversation a builder made for the body is let go an item at a
// time as the body fills, not held beside it to the end: built for Chat,
// a 45 MiB Codex conversation's messages, strings joined from its parts
// among them, were a copy more at the body's peak (#4004). A list not
// named spent is left as it was.
func TestMarshalBodyLetsSpentGo(t *testing.T) {
var held []int
list := make([]*spentProbe, 5)
for i := range list {
list[i] = &spentProbe{list: &list, i: i, held: &held}
}
other := []string{"a", "b"}
got, err := marshalBody(map[string]any{"messages": list, "other": other}, "messages")
if err != nil || string(got) != `{"messages":[1,1,1,1,1],"other":["a","b"]}` {
t.Fatalf("wrote %s (%v)", got, err)
}
if len(held) > 0 {
t.Errorf("items %v were still held when the next was written", held)
}
if other[0] != "a" || other[1] != "b" {
t.Errorf("a list not spent was changed: %q", other)
}
}
+1 -1
View File
@@ -1357,7 +1357,7 @@ func buildResponses(r *Request, model, host string, rejectTemp bool) []byte {
if len(include) > 0 {
out["include"] = include
}
b, _ := marshalBody(out)
b, _ := marshalBody(out, "input")
// a call the client left unanswered (a turn it interrupted) gets its
// synthetic result, as buildChat's pairToolMessages and
// buildAnthropic's pairToolBlocks give one: the Responses API refuses