gateway: a rewritten body is written back by marshalJSON, not json.Marshal (#3394)
withReasoningText runs on every replayed request to DeepSeek and the
like (#1104), and wrote the whole body back with json.Marshal twice:
once for the input list, once for the body around it. encoding/json
writes a json.RawMessage through its scanner, a call a byte, so a long
conversation was compacted twice a request. marshalJSON (marshalRaw,
#1532) writes the same bytes, checked by jsonCheck and copied, and
falls back to json.Marshal for any other value; TestMarshalRawIsEncodingJSON
holds the two equal. The same body rewrites in affinity.go,
anthropic.go, codex_backend.go and limit.go took json.Marshal of a
map[string]json.RawMessage too, and use marshalJSON now. Nothing a
body goes out as changes.
Run:
- Real DeepSeek account in a sandbox HOME, magpie serve on 3482, 12
streamed /v1/responses requests of a synthetic 1.5MB conversation with
Codex 0.162.0's real tools and a codex_cli_rs/0.162.1 User-Agent, CPU
time from ps, two rounds each: b97e0aa2a 0.74s and 0.75s, this 0.67s and
0.66s, about 62ms -> 55ms a request. All 48 answered 200.
- go test ./internal/gateway -count 1 -timeout 45m at this tree: ok
(370s).
- No new test: the change is the encoder, and TestMarshalRawIsEncodingJSON
already fails if marshalJSON's bytes differ from json.Marshal's.
- gofmt -l clean; GOOS=linux and windows go vet -tags nogui ./internal/gateway ok.
This commit is contained in:
@@ -817,7 +817,7 @@ func withoutRefused(key, who string, body []byte) ([]byte, bool) {
|
||||
return nil, false
|
||||
}
|
||||
q["input"], _ = json.Marshal(kept)
|
||||
b, err := json.Marshal(q)
|
||||
b, err := marshalJSON(q)
|
||||
return b, err == nil
|
||||
}
|
||||
|
||||
@@ -873,6 +873,6 @@ func withoutKinds(body []byte, kinds ...string) ([]byte, bool) {
|
||||
return nil, false
|
||||
}
|
||||
q["input"], _ = json.Marshal(kept)
|
||||
b, err := json.Marshal(q)
|
||||
b, err := marshalJSON(q)
|
||||
return b, err == nil
|
||||
}
|
||||
|
||||
@@ -372,7 +372,7 @@ func withThinkingOnToolTurns(body []byte) []byte {
|
||||
return body
|
||||
}
|
||||
q["messages"], _ = json.Marshal(msgs)
|
||||
nb, err := json.Marshal(q)
|
||||
nb, err := marshalJSON(q)
|
||||
if err != nil {
|
||||
return body
|
||||
}
|
||||
@@ -427,7 +427,7 @@ func withoutThinkingOff(body []byte) ([]byte, bool) {
|
||||
return nil, false
|
||||
}
|
||||
delete(q, "thinking")
|
||||
out, err := json.Marshal(q)
|
||||
out, err := marshalJSON(q)
|
||||
return out, err == nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1511,7 +1511,7 @@ func withoutItem(body []byte, id string) ([]byte, bool) {
|
||||
return nil, false
|
||||
}
|
||||
q["input"], _ = json.Marshal(kept)
|
||||
b, err := json.Marshal(q)
|
||||
b, err := marshalJSON(q)
|
||||
return b, err == nil
|
||||
}
|
||||
|
||||
@@ -2822,7 +2822,7 @@ func plainCompact(body []byte) []byte {
|
||||
userMessage(codexCompactPrompt),
|
||||
})
|
||||
delete(q, "previous_response_id")
|
||||
nb, err := json.Marshal(q)
|
||||
nb, err := marshalJSON(q)
|
||||
if err != nil {
|
||||
return body
|
||||
}
|
||||
|
||||
@@ -60,6 +60,6 @@ func withTokenFloor(body []byte, floor int) ([]byte, bool) {
|
||||
if !raised {
|
||||
return nil, false
|
||||
}
|
||||
b, err := json.Marshal(q)
|
||||
b, err := marshalJSON(q)
|
||||
return b, err == nil
|
||||
}
|
||||
|
||||
@@ -950,7 +950,7 @@ func withReasoningText(body []byte) []byte {
|
||||
continue
|
||||
}
|
||||
m["content"], _ = json.Marshal([]map[string]string{{"type": "reasoning_text", "text": b.String()}})
|
||||
if nb, err := json.Marshal(m); err == nil {
|
||||
if nb, err := marshalJSON(m); err == nil {
|
||||
items[i], changed = nb, true
|
||||
}
|
||||
}
|
||||
@@ -961,8 +961,8 @@ func withReasoningText(body []byte) []byte {
|
||||
if !changed {
|
||||
return body
|
||||
}
|
||||
q["input"], _ = json.Marshal(items)
|
||||
nb, err := json.Marshal(q)
|
||||
q["input"], _ = marshalJSON(items)
|
||||
nb, err := marshalJSON(q)
|
||||
if err != nil {
|
||||
return body
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user