When a plugin's model has an API id other than the one asked for, withModel decoded the whole request into a map and marshalled it again, which allocated 469 MiB for a 45 MiB body, about ten times the body. Now a body with one top-level "model" string gets just that string replaced, in one buffer of the body's size. Every other byte goes as it came. A body that isn't JSON, has no string model, or names "model" twice takes the old path. A body of null goes as it came; it was a panic, writing into a nil map. The gateway's own withModel (Codex backend) and rewriteModel are left as they are. TestCodexRewritesAsBefore pins the Codex backend's sorted bytes, and TestUpstreamBodiesUnchanged pins rewriteModel's handling of a duplicate model. Both went red when the splice was tried there. Run on mote at 3aa36bbea (since rebased onto main, which changed nothing in internal/provider or internal/gateway): - TestWithModelAsksForTheModel: a nested "model" is kept, spacing is kept, an escaped model is replaced, and a missing, numeric or duplicate model, a cut-off body and null are covered. - TestWithModelCopiesOnce: a 45 MiB body is copied once (45 MiB allocated). Against the old withModel, the test is red: 469 MiB allocated, and the body's < was escaped. - go vet ./internal/provider for darwin, linux and windows: ok. - go test ./internal/provider ./internal/gateway -timeout 45m: ok (106s, 495s), the plugin parity tests included. Not tried with a real account: none of the plugins signed in here (WorkBuddy, Devin, Cursor and others) has a model whose API id differs, so this path isn't reached by them today.
54 lines
1.8 KiB
Go
54 lines
1.8 KiB
Go
package provider
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// withModel asks for the plugin's model and leaves the rest of the
|
|
// request as it was, a "model" deeper in the body included.
|
|
func TestWithModelAsksForTheModel(t *testing.T) {
|
|
for _, c := range []struct{ in, want string }{
|
|
{`{"model":"a","messages":[{"role":"user","content":"<x>","model":"keep"}]}`, `{"model":"b","messages":[{"role":"user","content":"<x>","model":"keep"}]}`},
|
|
{`{ "stream" : true , "model" : "a" }`, `{ "stream" : true , "model" : "b" }`},
|
|
{`{"model":"a\"q","x":1}`, `{"model":"b","x":1}`},
|
|
{`{"x":1}`, `{"model":"b","x":1}`},
|
|
{`{"model":1,"x":1}`, `{"model":"b","x":1}`},
|
|
{`{"model":"a","model":"c","x":1}`, `{"model":"b","x":1}`},
|
|
{`{"model":"a",`, `{"model":"a",`},
|
|
{`null`, `null`},
|
|
} {
|
|
got := string(withModel([]byte(c.in), "b"))
|
|
if got == c.want {
|
|
continue
|
|
}
|
|
var g, w any
|
|
if json.Unmarshal([]byte(got), &g) != nil || json.Unmarshal([]byte(c.want), &w) != nil || !reflect.DeepEqual(g, w) {
|
|
t.Errorf("withModel(%s) = %s, want %s", c.in, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A 45 MB request is copied once to ask for another model, not decoded
|
|
// and marshalled again (#4004).
|
|
func TestWithModelCopiesOnce(t *testing.T) {
|
|
body := []byte(`{"model":"a","messages":[{"role":"tool","content":"` + strings.Repeat("output <line>\\n", 3<<20) + `"}]}`)
|
|
runtime.GC()
|
|
var m runtime.MemStats
|
|
runtime.ReadMemStats(&m)
|
|
before := m.TotalAlloc
|
|
out := withModel(body, "plugin-model")
|
|
runtime.ReadMemStats(&m)
|
|
n := m.TotalAlloc - before
|
|
t.Logf("%d MiB body: withModel allocated %d MiB", len(body)>>20, n>>20)
|
|
if n > uint64(len(body))*11/10 {
|
|
t.Errorf("withModel allocated %d MiB for a %d MiB body", n>>20, len(body)>>20)
|
|
}
|
|
if !strings.HasPrefix(string(out), `{"model":"plugin-model","messages":`) {
|
|
t.Errorf("withModel wrote %.60s", out)
|
|
}
|
|
}
|