dallascao on Discord: generating images in Codex and revising them again
and again, magpie answered 413 "request body is over magpie's gateway
limit of 512 MB (decoded); the provider was not asked" on
127.0.0.1:3425/backend-api/codex/responses, while Codex signed in to
OpenAI directly went on. The count was right: Codex keeps each image it
generated in the conversation (a function_call_output input_image, a
~3 MB PNG as ~4 MB base64; codex-rs ext/image-generation to_response_item,
and history keeps image_generation_call results), and over HTTP sends the
conversation whole every turn, so ~130 images is 512 MB decoded. To
OpenAI directly Codex uses its WebSocket and sends a delta.
- Codex's own path (codexBackend, the Codex relay, the Responses
WebSocket's read limit, keyLimited on CodexPath) reads to
codexBodyLimit: a sixteenth of the machine's memory (memLimit, else
proc.MemTotal), 512 MB at least, 2 GB at most. 16 GB: 1024 MB,
24 GB: 1536 MB, 32 GB+: 2048 MB, 8 GB or unknown: 512 MB as before.
It is still the bomb guard: nothing decodes past it, and the zstd
decoder keeps its window bound.
- A turn for a magpie model ("provider/model") on that path is held to
the 512 MB of every other route once its model is read: it is
translated, at several times its size. Its 413 says one of Codex's own
models takes up to codexBodyLimit; writeError's /compact hint stays.
- provider.codexNamesBad read tools/input/tool_choice with
GetManyBytes, which copied the whole input (the body's size: 300 MB of
a 300 MB turn in the alloc profile). It reads in place now.
Cost at the largest input (mote, a relayed gpt-5.5 turn of random
~3 MB images, heap objects sampled every 5ms, load ~350): 300 MB took
4.7s, 600 MB 7.7s, 1000 MB 11.9s, peak heap 1.5 to 2.1 GB past the
body's own bytes. The rest of the cost is io.ReadAll growing the decoded
body and fillJSONIn/exchangeOf; bodyRoom still runs a body bigger than
its room alone.
Run at the pushed head (mote): gofmt -l clean; GOOS=linux and
GOOS=windows go vet ./internal/gateway ./internal/provider ok;
go test -timeout 45m ./internal/provider ok, ./internal/gateway ok but
TestCompactionOverByteCapEveryTime, red in the full package run only
(#4862, filed before this; passes alone 3 of 3 here).
TestRequestBodyTooLarge/codex now sends codexBodyLimit+1.
- TestCodexGeneratedImagesPastOldLimit (16 GB machine fixture, a 516 MB
turn of generated images in Codex's shape): reaches the fake ChatGPT
backend whole in ~6s; the same turn for x/gpt-5 is 413 naming
"one of Codex's own models takes up to 1024 MB".
- TestCodexBodyLimitFromMemory: 2/8/16/24/64 GiB and unknown memory.
Broken by hand: codexBackend reading with limit 0 (the old code) makes
TestCodexGeneratedImagesPastOldLimit red with the reporter's 413; the
magpie-model check turned off makes it red (404 from routing, not 413).
Not tried: the reporter's own session or a real Codex image-generation
run through magpie; a turn past 1 GB on a 16 GB machine; the WebSocket's
raised read limit has no test of its own.
189 lines
5.4 KiB
Go
189 lines
5.4 KiB
Go
package provider
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"regexp"
|
|
"unsafe"
|
|
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
// The ChatGPT backend checks every tool name a request carries, a call's
|
|
// in the input as much as a tool's in tools, against ^[a-zA-Z0-9_.-]+\z,
|
|
// and turns the whole request away when one fails: 400 "Invalid
|
|
// 'input[83].name': string does not match pattern" (wing3556 on Discord,
|
|
// on a compaction). A conversation can hold a call by a name no tool has:
|
|
// a model another provider served wrote one with a space, a colon or a
|
|
// word of its own script in it, and the agent kept the call in its
|
|
// history beside the "no such tool" it answered. Each request that
|
|
// replays it, a compaction's above all, failed on Codex every time. Such a
|
|
// name goes as CodexToolName makes it; a valid one goes as it is, so a
|
|
// conversation that worked is sent byte for byte as before.
|
|
|
|
// codexNameOK is a name the ChatGPT backend takes.
|
|
var codexNameOK = regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`)
|
|
|
|
// CodexToolName is name as the ChatGPT backend takes it: as it is when
|
|
// it is valid, else each character the backend refuses as "_", cut to
|
|
// 55, and a hash of the whole after it (as FlatName cuts a long name), so
|
|
// a name is made the same on every request and two names that differ
|
|
// never come out alike. An agent's declared tool so renamed is called
|
|
// back by its own name (CodexToolNamesBack).
|
|
func CodexToolName(name string) string {
|
|
if codexNameOK.MatchString(name) {
|
|
return name
|
|
}
|
|
b := []byte(name)
|
|
out := make([]byte, 0, len(b)+9)
|
|
for _, c := range b {
|
|
if c == '_' || c == '.' || c == '-' || c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' {
|
|
out = append(out, c)
|
|
} else {
|
|
out = append(out, '_')
|
|
}
|
|
}
|
|
if len(out) > 55 {
|
|
out = out[:55]
|
|
}
|
|
sum := sha256.Sum256(b)
|
|
return string(out) + "_" + hex.EncodeToString(sum[:4])
|
|
}
|
|
|
|
// CodexToolNamesBack maps each name of declared that CodexToolName
|
|
// changes back to the name the agent declared; nil when none changes.
|
|
func CodexToolNamesBack(declared []string) map[string]string {
|
|
var back map[string]string
|
|
for _, n := range declared {
|
|
if c := CodexToolName(n); c != n {
|
|
if back == nil {
|
|
back = map[string]string{}
|
|
}
|
|
back[c] = n
|
|
}
|
|
}
|
|
return back
|
|
}
|
|
|
|
// CodexToolNames is body, a Responses request for the ChatGPT backend,
|
|
// with every tool name in it as CodexToolName makes it, or body byte for
|
|
// byte when each one is valid already.
|
|
func CodexToolNames(body []byte) []byte {
|
|
if !codexNamesBad(body) {
|
|
return body
|
|
}
|
|
dec := json.NewDecoder(bytes.NewReader(body))
|
|
dec.UseNumber()
|
|
var m map[string]any
|
|
if dec.Decode(&m) != nil || m == nil || !codexToolNames(m) {
|
|
return body
|
|
}
|
|
var buf bytes.Buffer
|
|
enc := json.NewEncoder(&buf)
|
|
enc.SetEscapeHTML(false)
|
|
if enc.Encode(m) != nil {
|
|
return body
|
|
}
|
|
return bytes.TrimSuffix(buf.Bytes(), []byte("\n"))
|
|
}
|
|
|
|
// codexNamesBad reports whether body names a tool the backend refuses,
|
|
// read where codexToolNames reads it, without decoding the conversation.
|
|
func codexNamesBad(body []byte) bool {
|
|
bad := false
|
|
name := func(v gjson.Result) {
|
|
if n := v.Get("name"); n.Type == gjson.String && !codexNameOK.MatchString(n.String()) {
|
|
bad = true
|
|
}
|
|
}
|
|
var tools func(ts gjson.Result)
|
|
tools = func(ts gjson.Result) {
|
|
ts.ForEach(func(_, t gjson.Result) bool {
|
|
switch t.Get("type").String() {
|
|
case "function", "custom":
|
|
name(t)
|
|
case "namespace":
|
|
tools(t.Get("tools"))
|
|
}
|
|
return !bad
|
|
})
|
|
}
|
|
// read in place: GetManyBytes copies each value it finds, the input
|
|
// whole, which is the body's size (hundreds of MB with Codex's images)
|
|
top := gjson.GetMany(unsafe.String(unsafe.SliceData(body), len(body)), "tools", "input", "tool_choice")
|
|
tools(top[0])
|
|
top[1].ForEach(func(_, it gjson.Result) bool {
|
|
switch it.Get("type").String() {
|
|
case "function_call", "custom_tool_call":
|
|
name(it)
|
|
case "additional_tools", "tool_search_output":
|
|
tools(it.Get("tools"))
|
|
}
|
|
return !bad
|
|
})
|
|
if tc := top[2]; tc.IsObject() {
|
|
switch tc.Get("type").String() {
|
|
case "function", "custom":
|
|
name(tc)
|
|
case "allowed_tools":
|
|
tools(tc.Get("tools"))
|
|
}
|
|
}
|
|
return bad
|
|
}
|
|
|
|
// codexToolNames makes every tool name in m, a Responses request, one the
|
|
// backend takes: a function's or a custom tool's in tools (a namespace's
|
|
// own among them), in an additional_tools item or a tool search's output,
|
|
// a call's in the input, and the one tool_choice names. It says whether
|
|
// it changed one.
|
|
func codexToolNames(m map[string]any) bool {
|
|
changed := false
|
|
fix := func(o map[string]any) {
|
|
if n, ok := o["name"].(string); ok {
|
|
if c := CodexToolName(n); c != n {
|
|
o["name"] = c
|
|
changed = true
|
|
}
|
|
}
|
|
}
|
|
var tools func(ts []any)
|
|
tools = func(ts []any) {
|
|
for _, t := range ts {
|
|
tm, _ := t.(map[string]any)
|
|
switch tm["type"] {
|
|
case "function", "custom":
|
|
fix(tm)
|
|
case "namespace":
|
|
nested, _ := tm["tools"].([]any)
|
|
tools(nested)
|
|
}
|
|
}
|
|
}
|
|
ts, _ := m["tools"].([]any)
|
|
tools(ts)
|
|
input, _ := m["input"].([]any)
|
|
for _, raw := range input {
|
|
it, _ := raw.(map[string]any)
|
|
switch it["type"] {
|
|
case "function_call", "custom_tool_call":
|
|
fix(it)
|
|
case "additional_tools", "tool_search_output":
|
|
nested, _ := it["tools"].([]any)
|
|
tools(nested)
|
|
}
|
|
}
|
|
if tc, _ := m["tool_choice"].(map[string]any); tc != nil {
|
|
switch tc["type"] {
|
|
case "function", "custom":
|
|
fix(tc)
|
|
case "allowed_tools":
|
|
nested, _ := tc["tools"].([]any)
|
|
tools(nested)
|
|
}
|
|
}
|
|
return changed
|
|
}
|