Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: a Codex turn's rewrites look at its items in place and decode it only when one has something to change (#1532, #2354)

yetone58cfca2f7700
diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.go
index 07e4e654e..44ac174ac 100644
--- a/internal/gateway/codex_backend.go
+++ b/internal/gateway/codex_backend.go
@@ -804,20 +804,30 @@ func sealedCompact(r *http.Request, model string, body []byte) (writer *provider
return nil, "", false
}
-// withModel is a request body asking for another model.
+// withModel is a request body asking for another model; a body of null
+// goes as it came (it was a panic, writing into no map).
func withModel(body []byte, model string) []byte {
- var q map[string]json.RawMessage
- if json.Unmarshal(body, &q) != nil {
+ q, err := rawObject(body)
+ if err != nil || q == nil {
return body
}
q["model"], _ = json.Marshal(model)
- b, err := json.Marshal(q)
+ b, err := marshalJSON(q)
if err != nil {
return body
}
return b
}
+// marshalJSON is json.Marshal, a request body split at its members or
+// items written back without encoding/json's scanner (marshalRaw).
+func marshalJSON(v any) ([]byte, error) {
+ if b, ok := marshalRaw(v, true); ok {
+ return b, nil
+ }
+ return json.Marshal(v)
+}
+
// codexUpstream relays a request as it came, the sign-in included, to where
// Codex would have sent it.
func (s *Server) codexUpstream(w http.ResponseWriter, r *http.Request, rest string, body []byte) {
@@ -1796,6 +1806,9 @@ func codexInput(body []byte, magpieModel bool) (_ []byte, compact bool) {
// when native says its provider compacts natively (NativeCompaction,
// #1099): the summaries magpie made before are still restored.
func codexInputAs(body []byte, magpieModel, native bool) (_ []byte, compact bool) {
+ if !anyInputItem(body, func(it []byte) bool { return codexInputItemChanges(it, magpieModel, native) }) {
+ return body, false
+ }
var q map[string]json.RawMessage
if json.Unmarshal(body, &q) != nil {
return body, false
@@ -1869,13 +1882,44 @@ func codexInputAs(body []byte, magpieModel, native bool) (_ []byte, compact bool
q["client_metadata"] = cm
}
}
- nb, err := json.Marshal(q)
+ nb, err := marshalJSON(q)
if err != nil {
return body, false
}
return nb, compact
}
+// codexInputItemChanges reports whether codexInputAs would put another in
+// the place of it, an input item, or leave it out: read as the rewrite
+// reads it (a map[string]any), without decoding the conversation.
+func codexInputItemChanges(it []byte, magpieModel, native bool) bool {
+ typ, _ := stringBytes(memberOrNil(it, "type"))
+ switch {
+ case string(typ) == "compaction" || string(typ) == "compaction_summary":
+ enc, _ := stringBytes(memberOrNil(it, "encrypted_content"))
+ return hasPrefix(enc, magpieCompaction)
+ case string(typ) == "compaction_trigger":
+ return magpieModel && !native
+ case string(typ) == "reasoning":
+ if magpieModel {
+ return false
+ }
+ if c := memberOrNil(it, "content"); c != nil && string(c) != "null" {
+ if c[0] != '[' {
+ return true
+ }
+ empty := true
+ eachElement(c, func([]byte) bool { empty = false; return false })
+ if !empty {
+ return true
+ }
+ }
+ enc, _ := stringBytes(memberOrNil(it, "encrypted_content"))
+ return len(enc) == 0
+ }
+ return false
+}
+
// turnMetadataHeader is where Codex says what a request is for, as it does
// in client_metadata's x-codex-turn-metadata too.
const turnMetadataHeader = "X-Codex-Turn-Metadata"
@@ -1967,7 +2011,10 @@ func refusesInput(status int, b []byte) bool {
// request goes back 400 "Item with id 'rs_…' not found" (#1008); its
// summary was the model's notes to itself, which no model reads back.
func withoutBareReasoning(body []byte) []byte {
- if !bytes.Contains(body, []byte(`"reasoning"`)) {
+ if !bytes.Contains(body, []byte(`"reasoning"`)) || !anyInputItem(body, func(it []byte) bool {
+ r := reasoningOf(it)
+ return r.typeOK && r.encOK && r.reasoning && !r.sealed
+ }) {
return body
}
var q map[string]json.RawMessage
@@ -1996,14 +2043,94 @@ func withoutBareReasoning(body []byte) []byte {
if len(kept) == len(items) {
return body
}
- q["input"], _ = json.Marshal(kept)
- b, err := json.Marshal(q)
+ q["input"], _ = marshalJSON(kept)
+ b, err := marshalJSON(q)
if err != nil {
return body
}
return b
}
+// reasoningLook is what decoding an input item into the structs the
+// reasoning rewrites read it by gives: its type is reasoning, something
+// is sealed in it, it has its text; and whether a decode of its type
+// beside each other field succeeds.
+type reasoningLook struct {
+ reasoning, sealed, text bool
+ typeOK, encOK, contentOK bool
+}
+
+// reasoningOf is reasoningLook of it, an input item. Its keys are matched
+// as encoding/json matches a field's name (bytes.EqualFold), the last one
+// standing. A plain item is read where it lies, nothing decoded; one with
+// a key escaped or past ASCII, a type that is, or a null where a field
+// is, is decoded.
+func reasoningOf(it []byte) reasoningLook {
+ if len(it) == 0 {
+ return reasoningLook{}
+ }
+ switch it[0] {
+ case 'n':
+ return reasoningLook{typeOK: true, encOK: true, contentOK: true}
+ case '{':
+ default:
+ return reasoningLook{}
+ }
+ r := reasoningLook{typeOK: true, encOK: true, contentOK: true}
+ slow := false
+ eachMember(it, func(k, v []byte) bool {
+ name := k[1 : len(k)-1]
+ if !plainASCII(name) {
+ slow = true
+ return false
+ }
+ field := bytes.EqualFold(name, []byte("type")) || bytes.EqualFold(name, []byte("encrypted_content")) || bytes.EqualFold(name, []byte("content"))
+ if field && v[0] == 'n' {
+ slow = true
+ return false
+ }
+ switch {
+ case bytes.EqualFold(name, []byte("type")):
+ if v[0] != '"' {
+ r.typeOK = false
+ return true
+ }
+ typ := v[1 : len(v)-1]
+ if !plainASCII(typ) {
+ slow = true
+ return false
+ }
+ r.reasoning = string(typ) == "reasoning"
+ case bytes.EqualFold(name, []byte("encrypted_content")):
+ r.encOK = r.encOK && v[0] == '"'
+ r.sealed = len(v) > 2
+ case bytes.EqualFold(name, []byte("content")):
+ r.contentOK = r.contentOK && v[0] == '['
+ r.text = false
+ eachElement(v, func([]byte) bool { r.text = true; return false })
+ }
+ return true
+ })
+ if !slow {
+ return r
+ }
+ var te struct {
+ Type string `json:"type"`
+ Enc string `json:"encrypted_content"`
+ }
+ var tc struct {
+ Type string `json:"type"`
+ Content []json.RawMessage `json:"content"`
+ }
+ errE, errC := decodeJSON(it, &te), decodeJSON(it, &tc)
+ var tt struct {
+ Type string `json:"type"`
+ }
+ errT := decodeJSON(it, &tt)
+ return reasoningLook{reasoning: tt.Type == "reasoning", sealed: te.Enc != "", text: len(tc.Content) > 0,
+ typeOK: errT == nil, encOK: errE == nil, contentOK: errC == nil}
+}
+
// reasoningTextRefused is how unfit remembers a provider turning away, for
// model, reasoning items with their text in them (withoutReasoningText).
func reasoningTextRefused(model string) string { return "reasoning text\x00" + model }
@@ -2038,7 +2165,10 @@ func refusesReasoningText(status int, b, body []byte) bool {
// than its text, so the item goes, as codexInput leaves it out for one of
// Codex's own models.
func withoutReasoningText(body []byte) []byte {
- if !bytes.Contains(body, []byte(`"reasoning"`)) {
+ if !bytes.Contains(body, []byte(`"reasoning"`)) || !anyInputItem(body, func(it []byte) bool {
+ r := reasoningOf(it)
+ return r.typeOK && r.contentOK && r.reasoning && r.text
+ }) {
return body
}
var q map[string]json.RawMessage
@@ -2063,8 +2193,8 @@ func withoutReasoningText(body []byte) []byte {
if len(kept) == len(items) {
return body
}
- q["input"], _ = json.Marshal(kept)
- b, err := json.Marshal(q)
+ q["input"], _ = marshalJSON(kept)
+ b, err := marshalJSON(q)
if err != nil {
return body
}
@@ -2091,6 +2221,14 @@ func callItemIDs(body []byte) []byte {
if !bytes.Contains(body, []byte(`"tool_search_call"`)) && !bytes.Contains(body, []byte(`"custom_tool_call"`)) {
return body
}
+ if !anyInputItem(body, func(it []byte) bool {
+ typ, _ := stringBytes(memberOrNil(it, "type"))
+ prefix := openaiItemPrefix[string(typ)]
+ id, _ := stringBytes(memberOrNil(it, "id"))
+ return prefix != "" && len(id) > 0 && !hasPrefix(id, prefix)
+ }) {
+ return body
+ }
var q map[string]json.RawMessage
if json.Unmarshal(body, &q) != nil {
return body
@@ -2167,6 +2305,12 @@ func plainItemIDs(body []byte) []byte {
if !bytes.Contains(body, []byte(`"id"`)) {
return body
}
+ if !anyInputItem(body, func(it []byte) bool {
+ id, _ := stringBytes(memberOrNil(it, "id"))
+ return len(id) > 0 && !safeID(id)
+ }) {
+ return body
+ }
var q map[string]json.RawMessage
if json.Unmarshal(body, &q) != nil {
return body
@@ -2273,18 +2417,17 @@ func forVendor(p provider.Provider, body []byte) []byte {
if p.Account != nil && p.Account.Agent == "codex" || strings.HasSuffix(p.Host(), "openai.com") {
return body
}
- if !slices.ContainsFunc(openaiOnly, func(k []byte) bool { return bytes.Contains(body, k) }) {
+ if !slices.ContainsFunc(openaiOnly, func(k []byte) bool { return bytes.Contains(body, k) }) || !forVendorChanges(body) {
return body
}
- var q map[string]json.RawMessage
- if json.Unmarshal(body, &q) != nil {
+ q, err := rawObject(body)
+ if err != nil {
return body
}
changed := false
// Codex's stream_options holds reasoning_summary_delivery alone; another
// client's other options stay
- var so map[string]json.RawMessage
- if json.Unmarshal(q["stream_options"], &so) == nil {
+ if so, err := rawObject(q["stream_options"]); err == nil {
if _, ok := so["reasoning_summary_delivery"]; ok {
changed = true
delete(so, "reasoning_summary_delivery")
@@ -2293,12 +2436,11 @@ func forVendor(p provider.Provider, body []byte) []byte {
}
}
}
- var items []json.RawMessage
- if json.Unmarshal(q["input"], &items) == nil {
+ if items, err := rawArray(q["input"]); err == nil {
out := items[:0]
for _, raw := range items {
- var it map[string]json.RawMessage
- if json.Unmarshal(raw, &it) != nil {
+ it, err := rawObject(raw)
+ if err != nil {
out = append(out, raw)
continue
}
@@ -2330,6 +2472,33 @@ func forVendor(p provider.Provider, body []byte) []byte {
return nb
}
+// forVendorChanges reports whether forVendor has anything to leave out of
+// body, read as it reads it, without decoding the conversation: Codex
+// sends what the rewrite looks for by name in every request, and its
+// metadata on every message, so a word in the body said little.
+func forVendorChanges(body []byte) bool {
+ so, found, ok := topMember(body, "stream_options")
+ if !ok {
+ return false
+ }
+ if found && len(so) > 0 && so[0] == '{' {
+ if _, has := memberOf(so, "reasoning_summary_delivery"); has {
+ return true
+ }
+ }
+ return anyInputItem(body, func(it []byte) bool {
+ if len(it) == 0 || it[0] != '{' {
+ return false
+ }
+ if string(memberOrNil(it, "type")) == `"configuration_update"` {
+ return true
+ }
+ _, meta := memberOf(it, "internal_chat_message_metadata_passthrough")
+ _, sealed := memberOf(it, "encrypted_function_args")
+ return meta || sealed
+ })
+}
+
func userMessage(text string) map[string]any {
return map[string]any{"type": "message", "role": "user",
"content": []map[string]any{{"type": "input_text", "text": text}}}
diff --git a/internal/gateway/codex_rewrite_scan_test.go b/internal/gateway/codex_rewrite_scan_test.go
new file mode 100644
index 000000000..51ef28f31
--- /dev/null
+++ b/internal/gateway/codex_rewrite_scan_test.go
@@ -0,0 +1,577 @@
+package gateway
+
+import (
+ "bytes"
+ "encoding/base64"
+ "encoding/json"
+ "fmt"
+ "math/rand/v2"
+ "slices"
+ "strings"
+ "testing"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// The rewrites a Codex request goes through on its way (#1532, #2354):
+// a turn with nothing for one to do isn't decoded by it, and what each
+// writes is what it wrote when it decoded every turn.
+
+// codexSession is a Codex conversation of n exchanges, as Codex sends one to
+// a provider named "OpenAI": its messages' metadata, sealed reasoning,
+// calls of each kind with OpenAI's ids, and items marked by f.
+func codexSession(n int, f func(i int, it map[string]any)) []byte {
+ para := strings.Repeat("reads the file <a> & splits it \"reasoning\" on lines é 漢字\n", 8)
+ meta := map[string]any{"turn_id": "t1"}
+ var items []any
+ add := func(it map[string]any) {
+ f(len(items), it)
+ items = append(items, it)
+ }
+ for i := range n {
+ add(map[string]any{"type": "message", "role": "user", "id": fmt.Sprintf("msg_u%d", i), "content": []any{map[string]any{"type": "input_text", "text": para}}, "internal_chat_message_metadata_passthrough": meta})
+ add(map[string]any{"type": "reasoning", "id": fmt.Sprintf("rs_%d", i), "summary": []any{map[string]any{"type": "summary_text", "text": para}}, "content": []any{}, "encrypted_content": "gAAAA" + strings.Repeat("x", 200)})
+ add(map[string]any{"type": "function_call", "id": fmt.Sprintf("fc_%d", i), "call_id": fmt.Sprintf("call_%d", i), "name": "shell", "arguments": `{"cmd":"ls"}`})
+ add(map[string]any{"type": "function_call_output", "call_id": fmt.Sprintf("call_%d", i), "output": para})
+ add(map[string]any{"type": "custom_tool_call", "id": fmt.Sprintf("ctc_%d", i), "call_id": fmt.Sprintf("cc_%d", i), "name": "apply_patch", "input": para})
+ add(map[string]any{"type": "tool_search_call", "id": fmt.Sprintf("tsc_%d", i), "call_id": fmt.Sprintf("ts_%d", i), "execution": "client", "arguments": map[string]any{"q": "x"}})
+ add(map[string]any{"type": "message", "role": "assistant", "id": fmt.Sprintf("msg_a%d", i), "content": []any{map[string]any{"type": "output_text", "text": para}}})
+ }
+ b, _ := json.Marshal(map[string]any{"model": "gpt-5.5", "instructions": para, "input": items, "store": false, "stream": true,
+ "include": []string{"reasoning.encrypted_content"}, "tools": []any{map[string]any{"type": "function", "name": "shell"}}})
+ return b
+}
+
+type codexRewrite struct {
+ name string
+ now, was func([]byte) []byte
+}
+
+// codexRewrites are the rewrites, each beside what it was before.
+var codexRewrites = func() []codexRewrite {
+ relay := provider.Provider{Responses: "https://relay.example/v1"}
+ rs := []codexRewrite{
+ {"callItemIDs", callItemIDs, wasCallItemIDs},
+ {"plainItemIDs", plainItemIDs, wasPlainItemIDs},
+ {"withoutBareReasoning", withoutBareReasoning, wasWithoutBareReasoning},
+ {"withoutReasoningText", withoutReasoningText, wasWithoutReasoningText},
+ {"withModel", func(b []byte) []byte { return withModel(b, "gpt-5.5<&>") }, func(b []byte) []byte {
+ if string(trimJSON(b)) == "null" {
+ return b // was a panic
+ }
+ return wasWithModel(b, "gpt-5.5<&>")
+ }},
+ {"forVendor", func(b []byte) []byte { return forVendor(relay, b) }, func(b []byte) []byte { return wasForVendor(relay, b) }},
+ }
+ for _, magpie := range []bool{false, true} {
+ for _, native := range []bool{false, true} {
+ rs = append(rs, codexRewrite{fmt.Sprintf("codexInputAs(%v,%v)", magpie, native),
+ func(b []byte) []byte { b, c := codexInputAs(b, magpie, native); return append(b, fmt.Sprint(c)...) },
+ func(b []byte) []byte { b, c := wasCodexInputAs(b, magpie, native); return append(b, fmt.Sprint(c)...) }})
+ }
+ }
+ return rs
+}()
+
+// A turn none of the rewrites has anything to do in goes through each
+// without its conversation decoded: they decoded it whole, every one,
+// every turn, and that was most of a long session's CPU (#1532).
+func TestCodexRewritesLeaveTurnUndecoded(t *testing.T) {
+ body := codexSession(60, func(int, map[string]any) {})
+ for _, r := range codexRewrites {
+ if r.name == "withModel" || r.name == "forVendor" {
+ continue // these have something to do in this turn
+ }
+ in := slices.Clone(body)
+ if got := r.now(in); !bytes.Equal(bytes.TrimSuffix(got, []byte("false")), body) {
+ t.Fatalf("%s changed a turn with nothing to change", r.name)
+ }
+ in = slices.Clone(body)
+ allocs := testing.AllocsPerRun(5, func() { r.now(in) })
+ if allocs > 10 {
+ t.Errorf("%s: %v allocations for a turn it leaves as it came (%d KB), want the conversation not decoded", r.name, allocs, len(body)>>10)
+ }
+ }
+ // a body that only names Codex's metadata goes past forVendor
+ // undecoded too
+ plain := codexSession(60, func(_ int, it map[string]any) { delete(it, "internal_chat_message_metadata_passthrough") })
+ plain = append(plain[:len(plain)-1], []byte(`,"note":"internal_chat_message_metadata_passthrough"}`)...)
+ relay := provider.Provider{Responses: "https://relay.example/v1"}
+ if got := forVendor(relay, plain); !bytes.Equal(got, plain) {
+ t.Fatal("forVendor changed a body with nothing to leave out")
+ }
+ if a := testing.AllocsPerRun(5, func() { forVendor(relay, plain) }); a > 10 {
+ t.Errorf("forVendor: %v allocations for a body it leaves as it came", a)
+ }
+}
+
+// Writing a long body back, split at its items, takes an allocation or
+// two: encoding/json grew its buffer a step at a time, after scanning
+// each value a byte a call (#1532).
+func TestMarshalRawAllocatesOnce(t *testing.T) {
+ q, err := rawObject(codexSession(60, func(int, map[string]any) {}))
+ if err != nil {
+ t.Fatal(err)
+ }
+ items, _ := rawArray(q["input"])
+ if a := testing.AllocsPerRun(5, func() { marshalPlain(items) }); a > 2 {
+ t.Errorf("marshalPlain of %d items: %v allocations", len(items), a)
+ }
+ if a := testing.AllocsPerRun(5, func() { marshalJSON(q) }); a > 3 {
+ t.Errorf("marshalJSON of a body's members: %v allocations", a)
+ }
+}
+
+// codexMarks are the items a rewrite has something to do with, and the
+// shapes it has to read as encoding/json read them.
+var codexMarks = []func(it map[string]any){
+ func(it map[string]any) { it["id"] = "fc_x" },
+ func(it map[string]any) { it["id"] = "rs_a:rs_b" },
+ func(it map[string]any) { it["id"] = "" },
+ func(it map[string]any) { it["id"] = 7 },
+ func(it map[string]any) { delete(it, "id") },
+ func(it map[string]any) { it["type"] = "reasoning" },
+ func(it map[string]any) { it["type"] = "reasoning"; it["encrypted_content"] = "" },
+ func(it map[string]any) { it["type"] = "reasoning"; it["encrypted_content"] = nil },
+ func(it map[string]any) {
+ it["type"] = "reasoning"
+ it["content"] = []any{map[string]any{"type": "reasoning_text", "text": "t"}}
+ },
+ func(it map[string]any) { it["type"] = "reasoning"; it["content"] = nil },
+ func(it map[string]any) { it["type"] = "reasoning"; it["content"] = "text" },
+ func(it map[string]any) { it["type"] = "reasoning"; it["content"] = map[string]any{} },
+ func(it map[string]any) {
+ it["type"] = "compaction"
+ it["encrypted_content"] = magpieCompaction + base64.StdEncoding.EncodeToString([]byte("sum"))
+ },
+ func(it map[string]any) {
+ it["type"] = "compaction_summary"
+ it["encrypted_content"] = magpieCompaction + "!!"
+ },
+ func(it map[string]any) { it["type"] = "compaction"; it["encrypted_content"] = "gAAAA" },
+ func(it map[string]any) { it["type"] = "compaction_trigger" },
+ func(it map[string]any) { it["type"] = "configuration_update" },
+ func(it map[string]any) { it["type"] = 3 },
+ func(it map[string]any) { it["type"] = "tool_search_call"; it["id"] = "fc_9" },
+ func(it map[string]any) { it["type"] = "custom_tool_call"; it["id"] = "fc_9" },
+ func(it map[string]any) { it["type"] = "custom_tool_call"; it["id"] = "ctc" },
+ func(it map[string]any) { it["encrypted_function_args"] = "sealed" },
+ func(it map[string]any) { it["internal_chat_message_metadata_passthrough"] = nil },
+}
+
+// codexRawMarks are what a map can't say: duplicate and escaped keys,
+// items that aren't objects, an input that isn't a list.
+var codexRawMarks = [][2]string{
+ {`{"content"`, `{"type":"reasoning","content"`},
+ {`{"content"`, `{"type":"reasoning","encrypted_content":"","content"`},
+ {`"id":"msg_u1"`, `"id":"msg_u1","id":"a:b"`},
+ {`"id":"msg_u1"`, `"id":"a:b"`},
+ {`"type":"reasoning"`, `"type":"reasoning"`},
+ {`"type":"reasoning"`, `"Type":"reasoning"`},
+ {`"type":"function_call_output"`, `"type":"configuration_update"`},
+ {`"internal_chat_message_metadata_passthrough"`, `"internal_chat_message_metadata_passthrough"`},
+ {`"input":[`, `"input":[null,`},
+ {`"input":[`, `"input":[1,`},
+ {`"input":[`, `"input":["x",[],`},
+ {`"input":[`, `"input":null,"x":[`},
+ {`"input":[`, `"input":{"a":1},"x":[`},
+ {`"input":[`, `"input" : [ `},
+ {`"store":false`, `"store":true`},
+ {`"store":false`, `"store":false,"stream_options":{"reasoning_summary_delivery":"x"}`},
+ {`"store":false`, `"store":false,"stream_options":{"reasoning_summary_delivery":"x","other":1}`},
+ {`"store":false`, `"store":false,"stream_options":null`},
+ {`"store":false`, `"store":false,"stream_options":7`},
+ {`"store":false`, `"store":false,"stream_options":{"other":1}`},
+ {`"stream":true`, `"stream":true,"model":"<b>"`},
+}
+
+func checkCodexRewrites(t *testing.T, body []byte) {
+ t.Helper()
+ for _, r := range codexRewrites {
+ got, want := r.now(slices.Clone(body)), r.was(slices.Clone(body))
+ if !bytes.Equal(got, want) {
+ t.Fatalf("%s of %.300q\n now %.300q\n was %.300q", r.name, body, got, want)
+ }
+ }
+}
+
+func TestCodexRewritesAsBefore(t *testing.T) {
+ checkCodexRewrites(t, codexSession(3, func(int, map[string]any) {}))
+ for m := range codexMarks {
+ for _, at := range []int{0, 1, 5, 20} {
+ checkCodexRewrites(t, codexSession(3, func(i int, it map[string]any) {
+ if i == at {
+ codexMarks[m](it)
+ }
+ }))
+ }
+ }
+ for _, rm := range codexRawMarks {
+ checkCodexRewrites(t, bytes.Replace(codexSession(2, func(int, map[string]any) {}), []byte(rm[0]), []byte(rm[1]), 1))
+ }
+ // marks together, as a session gathers them
+ r := rand.New(rand.NewPCG(5, 6))
+ for range 300 {
+ body := codexSession(2, func(i int, it map[string]any) {
+ if r.IntN(4) == 0 {
+ codexMarks[r.IntN(len(codexMarks))](it)
+ }
+ })
+ for range r.IntN(3) {
+ rm := codexRawMarks[r.IntN(len(codexRawMarks))]
+ body = bytes.Replace(body, []byte(rm[0]), []byte(rm[1]), 1)
+ }
+ checkCodexRewrites(t, body)
+ }
+ // a body long enough to be remembered (longBody), with each mark
+ for m := range codexMarks {
+ checkCodexRewrites(t, codexSession(20, func(i int, it map[string]any) {
+ if i == 77 {
+ codexMarks[m](it)
+ }
+ }))
+ }
+ for _, s := range []string{``, `null`, `[]`, `{}`, `{"input":[]}`, `{"input":null}`, `{`, `{"input":[{"type":"reasoning"}]} x`, `"reasoning"`} {
+ checkCodexRewrites(t, []byte(s))
+ }
+}
+
+func FuzzCodexRewritesAsBefore(f *testing.F) {
+ f.Add(codexSession(1, func(i int, it map[string]any) {
+ if i == 1 {
+ it["content"] = []any{"t"}
+ }
+ }))
+ for _, rm := range codexRawMarks {
+ f.Add(bytes.Replace(codexSession(1, func(int, map[string]any) {}), []byte(rm[0]), []byte(rm[1]), 1))
+ }
+ f.Fuzz(func(t *testing.T, body []byte) { checkCodexRewrites(t, body) })
+}
+
+// What the rewrites were before they looked before decoding (#1532),
+// kept to hold them to it.
+
+// wasMarshalPlain is marshalPlain as encoding/json writes it.
+func wasMarshalPlain(v any) ([]byte, error) {
+ var b bytes.Buffer
+ enc := json.NewEncoder(&b)
+ enc.SetEscapeHTML(false)
+ if err := enc.Encode(v); err != nil {
+ return nil, err
+ }
+ return bytes.TrimRight(b.Bytes(), "\n"), nil
+}
+
+func wasWithModel(body []byte, model string) []byte {
+ var q map[string]json.RawMessage
+ if json.Unmarshal(body, &q) != nil {
+ return body
+ }
+ q["model"], _ = json.Marshal(model)
+ b, err := json.Marshal(q)
+ if err != nil {
+ return body
+ }
+ return b
+}
+
+func wasCodexInputAs(body []byte, magpieModel, native bool) (_ []byte, compact bool) {
+ var q map[string]json.RawMessage
+ if json.Unmarshal(body, &q) != nil {
+ return body, false
+ }
+ var items []map[string]any
+ if json.Unmarshal(q["input"], &items) != nil {
+ return body, false
+ }
+ changed := false
+ out := make([]map[string]any, 0, len(items))
+ for _, it := range items {
+ switch it["type"] {
+ case "compaction", "compaction_summary":
+ enc, _ := it["encrypted_content"].(string)
+ sum, ok := strings.CutPrefix(enc, magpieCompaction)
+ if !ok {
+ out = append(out, it) // OpenAI's own, for OpenAI
+ continue
+ }
+ changed = true
+ if b, err := base64.StdEncoding.DecodeString(sum); err == nil {
+ out = append(out, userMessage(codexSummaryPrefix+"\n"+string(b)))
+ }
+ case "compaction_trigger":
+ if !magpieModel || native {
+ out = append(out, it)
+ continue
+ }
+ changed, compact = true, true
+ out = append(out, userMessage(codexCompactPrompt))
+ case "reasoning":
+ // OpenAI accepts no reasoning content parts in replayed input.
+ // Drop the item so its encrypted_content cannot fail there too.
+ if !magpieModel {
+ if content, present := it["content"]; present && content != nil {
+ parts, ok := content.([]any)
+ if !ok || len(parts) > 0 {
+ changed = true
+ continue
+ }
+ }
+ // Nor reasoning with nothing sealed in it — another vendor's,
+ // only an id (rs_…): OpenAI, which keeps nothing (store is
+ // false), looks the id up and answers 404 "Item … not found".
+ if enc, _ := it["encrypted_content"].(string); enc == "" {
+ changed = true
+ continue
+ }
+ }
+ out = append(out, it)
+ default:
+ out = append(out, it)
+ }
+ }
+ if !changed {
+ return body, false
+ }
+ b, _ := json.Marshal(out)
+ q["input"] = b
+ if compact {
+ // the summary is text; a tool call would be no summary, so it
+ // offers none. Otherwise it goes as Codex asks for its own summary
+ // from a provider it reaches itself: "tools": [] (not left out,
+ // #1511), streamed with its tool_choice — a relay in front of the
+ // ChatGPT backend turns away one that isn't ("invalid codex
+ // request", #292)
+ q["tools"] = json.RawMessage("[]")
+ q["tool_choice"] = json.RawMessage(`"auto"`)
+ q["parallel_tool_calls"] = json.RawMessage("false")
+ if cm, ok := localCompactionMeta(q["client_metadata"]); ok {
+ q["client_metadata"] = cm
+ }
+ }
+ nb, err := json.Marshal(q)
+ if err != nil {
+ return body, false
+ }
+ return nb, compact
+}
+
+func wasWithoutBareReasoning(body []byte) []byte {
+ if !bytes.Contains(body, []byte(`"reasoning"`)) {
+ return body
+ }
+ var q map[string]json.RawMessage
+ if json.Unmarshal(body, &q) != nil {
+ return body
+ }
+ var store bool
+ if json.Unmarshal(q["store"], &store) == nil && store {
+ return body
+ }
+ var items []json.RawMessage
+ if json.Unmarshal(q["input"], &items) != nil {
+ return body
+ }
+ kept := items[:0:0]
+ for _, it := range items {
+ var t struct {
+ Type string `json:"type"`
+ Enc string `json:"encrypted_content"`
+ }
+ if json.Unmarshal(it, &t) == nil && t.Type == "reasoning" && t.Enc == "" {
+ continue
+ }
+ kept = append(kept, it)
+ }
+ if len(kept) == len(items) {
+ return body
+ }
+ q["input"], _ = json.Marshal(kept)
+ b, err := json.Marshal(q)
+ if err != nil {
+ return body
+ }
+ return b
+}
+
+func wasWithoutReasoningText(body []byte) []byte {
+ if !bytes.Contains(body, []byte(`"reasoning"`)) {
+ return body
+ }
+ var q map[string]json.RawMessage
+ if json.Unmarshal(body, &q) != nil {
+ return body
+ }
+ var items []json.RawMessage
+ if json.Unmarshal(q["input"], &items) != nil {
+ return body
+ }
+ kept := items[:0:0]
+ for _, it := range items {
+ var t struct {
+ Type string `json:"type"`
+ Content []json.RawMessage `json:"content"`
+ }
+ if json.Unmarshal(it, &t) == nil && t.Type == "reasoning" && len(t.Content) > 0 {
+ continue
+ }
+ kept = append(kept, it)
+ }
+ if len(kept) == len(items) {
+ return body
+ }
+ q["input"], _ = json.Marshal(kept)
+ b, err := json.Marshal(q)
+ if err != nil {
+ return body
+ }
+ return b
+}
+
+func wasCallItemIDs(body []byte) []byte {
+ if !bytes.Contains(body, []byte(`"tool_search_call"`)) && !bytes.Contains(body, []byte(`"custom_tool_call"`)) {
+ return body
+ }
+ var q map[string]json.RawMessage
+ if json.Unmarshal(body, &q) != nil {
+ return body
+ }
+ var items []json.RawMessage
+ if json.Unmarshal(q["input"], &items) != nil {
+ return body
+ }
+ changed := false
+ for i, raw := range items {
+ var it map[string]json.RawMessage
+ var typ, id string
+ if json.Unmarshal(raw, &it) != nil || json.Unmarshal(it["type"], &typ) != nil {
+ continue
+ }
+ prefix := openaiItemPrefix[typ]
+ if prefix == "" || json.Unmarshal(it["id"], &id) != nil || id == "" || strings.HasPrefix(id, prefix) {
+ continue
+ }
+ if _, rest, ok := strings.Cut(id, "_"); ok && rest != "" {
+ id = prefix + rest
+ } else {
+ id = prefix + id
+ }
+ it["id"], _ = json.Marshal(id)
+ if b, err := wasMarshalPlain(it); err == nil {
+ items[i], changed = b, true
+ }
+ }
+ if !changed {
+ return body
+ }
+ q["input"], _ = wasMarshalPlain(items)
+ nb, err := wasMarshalPlain(q)
+ if err != nil {
+ return body
+ }
+ return nb
+}
+
+func wasPlainItemIDs(body []byte) []byte {
+ if !bytes.Contains(body, []byte(`"id"`)) {
+ return body
+ }
+ var q map[string]json.RawMessage
+ if json.Unmarshal(body, &q) != nil {
+ return body
+ }
+ var items []json.RawMessage
+ if json.Unmarshal(q["input"], &items) != nil {
+ return body
+ }
+ changed := false
+ kept := items[:0:0]
+ for _, raw := range items {
+ var it map[string]json.RawMessage
+ var typ, id string
+ if json.Unmarshal(raw, &it) != nil || json.Unmarshal(it["id"], &id) != nil || id == "" || safeIDChars.MatchString(id) {
+ kept = append(kept, raw)
+ continue
+ }
+ changed = true
+ if json.Unmarshal(it["type"], &typ) == nil && typ == "reasoning" {
+ continue
+ }
+ it["id"], _ = json.Marshal(unsafeToolID.ReplaceAllString(id, "_"))
+ b, err := wasMarshalPlain(it)
+ if err != nil {
+ return body
+ }
+ kept = append(kept, b)
+ }
+ if !changed {
+ return body
+ }
+ q["input"], _ = wasMarshalPlain(kept)
+ nb, err := wasMarshalPlain(q)
+ if err != nil {
+ return body
+ }
+ return nb
+}
+
+func wasForVendor(p provider.Provider, body []byte) []byte {
+ if p.Account != nil && p.Account.Agent == "codex" || strings.HasSuffix(p.Host(), "openai.com") {
+ return body
+ }
+ if !slices.ContainsFunc(openaiOnly, func(k []byte) bool { return bytes.Contains(body, k) }) {
+ return body
+ }
+ var q map[string]json.RawMessage
+ if json.Unmarshal(body, &q) != nil {
+ return body
+ }
+ changed := false
+ // Codex's stream_options holds reasoning_summary_delivery alone; another
+ // client's other options stay
+ var so map[string]json.RawMessage
+ if json.Unmarshal(q["stream_options"], &so) == nil {
+ if _, ok := so["reasoning_summary_delivery"]; ok {
+ changed = true
+ delete(so, "reasoning_summary_delivery")
+ if q["stream_options"], _ = wasMarshalPlain(so); len(so) == 0 {
+ delete(q, "stream_options")
+ }
+ }
+ }
+ var items []json.RawMessage
+ if json.Unmarshal(q["input"], &items) == nil {
+ out := items[:0]
+ for _, raw := range items {
+ var it map[string]json.RawMessage
+ if json.Unmarshal(raw, &it) != nil {
+ out = append(out, raw)
+ continue
+ }
+ if string(it["type"]) == `"configuration_update"` {
+ changed = true
+ continue
+ }
+ _, meta := it["internal_chat_message_metadata_passthrough"]
+ _, sealed := it["encrypted_function_args"]
+ if meta || sealed {
+ changed = true
+ delete(it, "internal_chat_message_metadata_passthrough")
+ delete(it, "encrypted_function_args")
+ raw, _ = wasMarshalPlain(it)
+ }
+ out = append(out, raw)
+ }
+ if changed {
+ q["input"], _ = wasMarshalPlain(out)
+ }
+ }
+ if !changed {
+ return body
+ }
+ nb, err := wasMarshalPlain(q)
+ if err != nil {
+ return body
+ }
+ return nb
+}
diff --git a/internal/gateway/fastjson.go b/internal/gateway/fastjson.go
index 0bc78ac97..092fdfcb3 100644
--- a/internal/gateway/fastjson.go
+++ b/internal/gateway/fastjson.go
@@ -1011,3 +1011,280 @@ func validJSONNumber(data []byte, i int) (int, bool) {
}
return i, true
}
+
+// topMember is the value of the member named key of body's top-level
+// object, as decoding body into a map[string]json.RawMessage has it: the
+// last of that name, the name read with its escapes. ok is false when
+// body isn't a valid JSON object, found false when it has no such member.
+// A long body is checked and split once (longBody), so a rewrite asking
+// whether it has anything to do costs no walk over the conversation.
+func topMember(body []byte, key string) (val []byte, found, ok bool) {
+ var members []memberSpan
+ if len(body) >= validMemoMin {
+ var valid bool
+ if valid, members = longBody(body); !valid {
+ return nil, false, false
+ }
+ } else if !jsonCheck(body) {
+ return nil, false, false
+ }
+ raw := trimJSON(body)
+ if raw[0] != '{' {
+ return nil, false, false
+ }
+ eachMemberIn(raw, members, func(k, v []byte) bool {
+ if keyIs(k, key) {
+ val, found = v, true
+ }
+ return true
+ })
+ return val, found, true
+}
+
+// memberOf is topMember of obj, a valid JSON object.
+func memberOf(obj []byte, key string) (val []byte, found bool) {
+ eachMember(obj, func(k, v []byte) bool {
+ if keyIs(k, key) {
+ val, found = v, true
+ }
+ return true
+ })
+ return val, found
+}
+
+// keyIs reports whether quoted, a valid JSON string, is key.
+func keyIs(quoted []byte, key string) bool {
+ if len(quoted) == len(key)+2 && string(quoted[1:len(quoted)-1]) == key {
+ return true
+ }
+ if bytes.IndexByte(quoted, '\\') < 0 {
+ return false
+ }
+ s, _ := unquoteJSON(quoted)
+ return s == key
+}
+
+// stringBytes is the string raw, a valid JSON value, holds, as encoding/json
+// decodes it; false for any other kind of value. A plain ASCII string is
+// raw's own bytes, nothing copied.
+func stringBytes(raw []byte) ([]byte, bool) {
+ if len(raw) == 0 || raw[0] != '"' {
+ return nil, false
+ }
+ if s := raw[1 : len(raw)-1]; plainASCII(s) {
+ return s, true
+ }
+ s, ok := unquoteJSON(raw)
+ return []byte(s), ok
+}
+
+// plainASCII reports whether s, a JSON string's insides, is printable ASCII
+// with no escape: what it decodes to is s itself.
+func plainASCII(s []byte) bool {
+ for _, c := range s {
+ if c == '\\' || c >= 0x80 {
+ return false
+ }
+ }
+ return true
+}
+
+// hasPrefix is strings.HasPrefix of b, nothing copied.
+func hasPrefix(b []byte, prefix string) bool {
+ return len(b) >= len(prefix) && string(b[:len(prefix)]) == prefix
+}
+
+// safeID is safeIDChars.Match of id, nothing allocated.
+func safeID(id []byte) bool {
+ for _, c := range id {
+ if !('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' || c == '_' || c == '-') {
+ return false
+ }
+ }
+ return len(id) > 0
+}
+
+// inputItems calls f with each item of a Responses body's input, as
+// decoding the body into a map[string]json.RawMessage and its input into
+// a []json.RawMessage has them, until f returns false. ok is false when
+// the body isn't a valid object or its input isn't an array (nor null,
+// which has no items).
+func inputItems(body []byte, f func(item []byte) bool) (ok bool) {
+ in, found, ok := topMember(body, "input")
+ if !ok {
+ return false
+ }
+ if !found || string(in) == "null" {
+ return true
+ }
+ if in[0] != '[' {
+ return false
+ }
+ eachElement(in, f)
+ return true
+}
+
+// anyInputItem reports whether any of a Responses body's input items
+// (inputItems) is one f is looking for: whether a rewrite of them has
+// anything to do, told without decoding the conversation. Most turns it
+// hasn't, and each rewrite decoded all of it and the decoding was most
+// of the CPU of a long Codex session's turn (#1532, #2354).
+func anyInputItem(body []byte, f func(item []byte) bool) (found bool) {
+ inputItems(body, func(it []byte) bool {
+ found = f(it)
+ return !found
+ })
+ return found
+}
+
+// memberOrNil is memberOf of it when it is an object, as decoding it into
+// a map[string]json.RawMessage reads it, and nil for any other value.
+func memberOrNil(it []byte, key string) []byte {
+ if len(it) == 0 || it[0] != '{' {
+ return nil
+ }
+ v, _ := memberOf(it, key)
+ return v
+}
+
+// marshalRaw is json.Marshal of v, or marshalPlain's encoding with
+// escapeHTML false, for a map[string]json.RawMessage or a
+// []json.RawMessage: a request body split at its members or its items
+// and put back together. encoding/json writes each raw value through its
+// scanner, a call a byte, so a Codex conversation of 5 MB with one item
+// changed cost tens of milliseconds to write back, on every turn and by
+// each rewrite (#1532). Here each value is checked by jsonCheck and
+// copied, its strings whole; the bytes are encoding/json's. ok is false
+// for any other type, and for a value encoding/json would refuse, which
+// the caller then gives encoding/json to say so.
+func marshalRaw(v any, escapeHTML bool) (_ []byte, ok bool) {
+ switch v := v.(type) {
+ case map[string]json.RawMessage:
+ if v == nil {
+ return []byte("null"), true
+ }
+ n := 2
+ keys := make([]string, 0, len(v))
+ for k, raw := range v {
+ n += len(k) + len(raw) + 4
+ keys = append(keys, k)
+ }
+ slices.Sort(keys)
+ dst := make([]byte, 0, n)
+ dst = append(dst, '{')
+ for i, k := range keys {
+ if i > 0 {
+ dst = append(dst, ',')
+ }
+ if dst, ok = appendJSONKey(dst, k, escapeHTML); !ok {
+ return nil, false
+ }
+ dst = append(dst, ':')
+ if dst, ok = appendRawJSON(dst, v[k], escapeHTML); !ok {
+ return nil, false
+ }
+ }
+ return append(dst, '}'), true
+ case []json.RawMessage:
+ if v == nil {
+ return []byte("null"), true
+ }
+ n := 2
+ for _, raw := range v {
+ n += len(raw) + 1
+ }
+ dst := make([]byte, 0, n)
+ dst = append(dst, '[')
+ for i, raw := range v {
+ if i > 0 {
+ dst = append(dst, ',')
+ }
+ if dst, ok = appendRawJSON(dst, raw, escapeHTML); !ok {
+ return nil, false
+ }
+ }
+ return append(dst, ']'), true
+ }
+ return nil, false
+}
+
+// appendJSONKey is key quoted as encoding/json quotes a string: as it is
+// when it is printable ASCII with nothing in it to escape, and otherwise
+// by encoding/json itself.
+func appendJSONKey(dst []byte, key string, escapeHTML bool) ([]byte, bool) {
+ for i := 0; i < len(key); i++ {
+ if c := key[i]; c < 0x20 || c >= 0x7f || c == '"' || c == '\\' || escapeHTML && (c == '<' || c == '>' || c == '&') {
+ var b bytes.Buffer
+ enc := json.NewEncoder(&b)
+ enc.SetEscapeHTML(escapeHTML)
+ if enc.Encode(key) != nil {
+ return dst, false
+ }
+ return append(dst, bytes.TrimRight(b.Bytes(), "\n")...), true
+ }
+ }
+ dst = append(dst, '"')
+ dst = append(dst, key...)
+ return append(dst, '"'), true
+}
+
+// appendRawJSON is what encoding/json writes of raw as a json.RawMessage:
+// null for nil, and otherwise raw compacted (appendCompactJSON); false
+// when raw isn't valid JSON.
+func appendRawJSON(dst []byte, raw json.RawMessage, escapeHTML bool) ([]byte, bool) {
+ if raw == nil {
+ return append(dst, "null"...), true
+ }
+ if !jsonCheck(raw) {
+ return dst, false
+ }
+ return appendCompactJSON(dst, raw, escapeHTML), true
+}
+
+// appendCompactJSON is encoding/json's appendCompact of src, a valid JSON
+// value: without the space between its tokens, and with <, >, & and
+// U+2028, U+2029 escaped when escapeHTML, as json.Marshal writes them.
+// Those are only ever in a string, where a valid value's space is too, so
+// strings are copied whole, or looked through for those alone.
+func appendCompactJSON(dst, src []byte, escapeHTML bool) []byte {
+ start := 0
+ for i := 0; i < len(src); {
+ switch c := src[i]; {
+ case c == '"':
+ j := skipJSONString(src, i)
+ if escapeHTML {
+ dst = append(dst, src[start:i]...)
+ dst = appendHTMLEscaped(dst, src[i:j])
+ start = j
+ }
+ i = j
+ case isJSONSpace(c):
+ dst = append(dst, src[start:i]...)
+ i++
+ start = i
+ default:
+ i++
+ }
+ }
+ return append(dst, src[start:]...)
+}
+
+// appendHTMLEscaped is s with <, >, & and U+2028, U+2029 as \u escapes.
+func appendHTMLEscaped(dst, s []byte) []byte {
+ const hex = "0123456789abcdef"
+ start := 0
+ for i := 0; i < len(s); i++ {
+ switch c := s[i]; {
+ case c == '<' || c == '>' || c == '&':
+ dst = append(dst, s[start:i]...)
+ dst = append(dst, '\\', 'u', '0', '0', hex[c>>4], hex[c&0xF])
+ start = i + 1
+ case c == 0xE2 && i+2 < len(s) && s[i+1] == 0x80 && s[i+2]&^1 == 0xA8:
+ dst = append(dst, s[start:i]...)
+ dst = append(dst, '\\', 'u', '2', '0', '2', hex[s[i+2]&0xF])
+ start = i + 3
+ i += 2
+ }
+ }
+ return append(dst, s[start:]...)
+}
diff --git a/internal/gateway/fastjson_marshal_test.go b/internal/gateway/fastjson_marshal_test.go
new file mode 100644
index 000000000..aa2edbeb2
--- /dev/null
+++ b/internal/gateway/fastjson_marshal_test.go
@@ -0,0 +1,128 @@
+package gateway
+
+import (
+ "bytes"
+ "encoding/json"
+ "math/rand/v2"
+ "strings"
+ "testing"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// mrCompare checks marshalRaw against encoding/json for v, with HTML
+// escaped (json.Marshal) and without (an Encoder told not to): the same
+// bytes, or encoding/json's error where marshalRaw declined.
+func mrCompare(t *testing.T, v any) {
+ t.Helper()
+ for _, escape := range []bool{true, false} {
+ var b bytes.Buffer
+ enc := json.NewEncoder(&b)
+ enc.SetEscapeHTML(escape)
+ werr := enc.Encode(v)
+ want := bytes.TrimRight(b.Bytes(), "\n")
+ got, ok := marshalRaw(v, escape)
+ if !ok {
+ if werr == nil {
+ t.Fatalf("escape %v: marshalRaw declined %#v, encoding/json wrote %q", escape, v, want)
+ }
+ continue
+ }
+ if werr != nil {
+ t.Fatalf("escape %v: marshalRaw wrote %q, encoding/json refused: %v", escape, got, werr)
+ }
+ if !bytes.Equal(got, want) {
+ t.Fatalf("escape %v:\n marshalRaw %q\n encoding/json %q", escape, got, want)
+ }
+ }
+}
+
+// mrValues are raw values as a request's members and items hold them:
+// spaced, escaped, HTML and the line separators in strings, nothing, and
+// what isn't JSON at all.
+var mrValues = []string{
+ `1`, `-0.5e+3`, `true`, `null`, `""`, `"plain"`, `"a<b>&c"`, "\"line\u2028sep\u2029\"",
+ "\"\xe2\x80\"", "\"\xff\xfe bad utf8\"", `"esc \" \\ \/ \u003c \n"`,
+ ` { "a" : [ 1 , 2 , { "b" : "x y" } ] , "c" : null } `, "\t[\n\"x\"\r\n]\n",
+ `{"html":"<script>&amp;</script>","n":[1,2.5,-3e10]}`, `[]`, `{}`, `[ ]`, `{ }`,
+ ``, ` `, `{`, `[1,]`, `"open`, `tru`, `01`, `{"a":1}{"b":2}`, `nul`,
+ strings.Repeat("[", 10001) + strings.Repeat("]", 10001),
+}
+
+// The keys a body's members have: plain, needing an escape, and those
+// encoding/json escapes only with HTML.
+var mrKeys = []string{"model", "input", "", "a b", "<tag>", "a&b", "quo\"te", "back\\slash", "tab\t", "é", "\u2028", "\xff", "\x7f", "z", "A"}
+
+func TestMarshalRawIsEncodingJSON(t *testing.T) {
+ mrCompare(t, map[string]json.RawMessage(nil))
+ mrCompare(t, []json.RawMessage(nil))
+ mrCompare(t, map[string]json.RawMessage{})
+ mrCompare(t, []json.RawMessage{})
+ mrCompare(t, map[string]json.RawMessage{"a": nil})
+ mrCompare(t, []json.RawMessage{nil, json.RawMessage("1")})
+ for _, v := range mrValues {
+ mrCompare(t, []json.RawMessage{json.RawMessage(v)})
+ for _, k := range mrKeys {
+ mrCompare(t, map[string]json.RawMessage{k: json.RawMessage(v), "input": json.RawMessage(`[1]`)})
+ }
+ }
+ all := map[string]json.RawMessage{}
+ for i, k := range mrKeys {
+ all[k] = json.RawMessage(mrValues[i%14])
+ }
+ mrCompare(t, all)
+ // whole bodies split at their members and items, as the rewrites do
+ for _, proto := range []provider.Protocol{provider.Anthropic, provider.Chat, provider.Responses} {
+ body := largeConversation(proto, "m", 300)
+ var q map[string]json.RawMessage
+ if err := json.Unmarshal(body, &q); err != nil {
+ t.Fatal(err)
+ }
+ mrCompare(t, q)
+ for _, v := range q {
+ var items []json.RawMessage
+ if json.Unmarshal(v, &items) == nil {
+ mrCompare(t, items)
+ }
+ }
+ }
+ for _, s := range fjSeeds {
+ var q map[string]json.RawMessage
+ if json.Unmarshal([]byte(s), &q) == nil {
+ mrCompare(t, q)
+ }
+ }
+ // every one-byte change of a spaced value
+ r := rand.New(rand.NewPCG(3, 4))
+ for _, s := range mrValues[11:14] {
+ for i := range len(s) {
+ b := []byte(s)
+ b[i] = " \t\n{}[]\",:\\<>&nul0e.-x\xe2\xff"[r.IntN(24)]
+ mrCompare(t, []json.RawMessage{b})
+ mrCompare(t, map[string]json.RawMessage{"k": b})
+ }
+ }
+}
+
+// marshalPlain writes what it wrote through encoding/json, for these
+// types as for any other.
+func TestMarshalPlainRawTypes(t *testing.T) {
+ q := map[string]json.RawMessage{"b": json.RawMessage(` "<x>" `), "a": json.RawMessage(`[ 1 ]`)}
+ got, err := marshalPlain(q)
+ if err != nil || string(got) != `{"a":[1],"b":"<x>"}` {
+ t.Fatalf("marshalPlain = %q, %v", got, err)
+ }
+ if _, err := marshalPlain(map[string]json.RawMessage{"a": json.RawMessage(`{`)}); err == nil {
+ t.Fatal("marshalPlain took a value that isn't JSON")
+ }
+}
+
+func FuzzMarshalRaw(f *testing.F) {
+ for i, v := range mrValues[:len(mrValues)-1] {
+ f.Add([]byte(v), mrKeys[i%len(mrKeys)])
+ }
+ f.Fuzz(func(t *testing.T, data []byte, key string) {
+ mrCompare(t, []json.RawMessage{data, nil})
+ mrCompare(t, map[string]json.RawMessage{key: data, "model": json.RawMessage(`"m"`)})
+ })
+}
diff --git a/internal/gateway/toolsearch.go b/internal/gateway/toolsearch.go
index 68983b07d..09d1913a6 100644
--- a/internal/gateway/toolsearch.go
+++ b/internal/gateway/toolsearch.go
@@ -106,6 +106,9 @@ func undefer(tm map[string]any) {
// marshalPlain is json.Marshal leaving <, > and & as they are.
func marshalPlain(v any) ([]byte, error) {
+ if b, ok := marshalRaw(v, false); ok {
+ return b, nil
+ }
var b bytes.Buffer
enc := json.NewEncoder(&b)
enc.SetEscapeHTML(false)