A JSON body holding bytes that aren't UTF-8 (GBK, a surrogate written as
UTF-8, \xed\xa0\x80) or a lone surrogate's escape ("\ud800") was sent on
with those bytes as they came. DeepSeek and OpenAI answered 400, MiniMax
200 with nothing, so in a routing group one request worked or failed by
which member got it.
readBoundedRequestBody, which every route reading an agent's body goes
through (each API, Gemini's, count_tokens, the Codex backend path and
relay, a Codex socket's turn, served as a POST through the handler), now
makes them U+FFFD (textBody): one per bad byte, as Go's encoding/json
does. A surrogate pair's escapes, an escaped backslash before "ud800",
and multipart, audio, image or octet-stream bodies are left alone.
Why replace, not refuse (the report's option A): a tool result carries a
file's bytes as they are, and an agent resends the whole history every
turn. Anthropic answers a lone surrogate 400 ("no low surrogate in
string"), which fails every later turn of a Claude Code session; a 400
from magpie would do the same. Replacing is what Go's decoder and WHATWG
decoders do, keeps the session going, and changes only bytes that were
not text. Since nothing bad reaches the vendor, its 400 can't rest an
account or count against a key (the test checks nothing rests).
Cost: utf8.Valid plus a scan of backslashes, no copy when valid; 24 MB of
escape-dense JSON in 24.5 ms on mote (~1 GB/s).
Run (mote):
- new tests: TestBadTextReachesVendorsAsReplacement (inbound Chat,
Responses, Messages x upstream Chat, Responses, Anthropic x six bad
texts x stream on/off, in a user message and a tool result),
TestBadTextOnSocketAndGemini, TestGoodTextGoesAsSent, TestTextBody:
ok; also ok on Windows (win-run).
- broken by hand: with the textBody call removed from
readBoundedRequestBody, TestBadTextReachesVendorsAsReplacement,
TestGoodTextGoesAsSent and TestBadTextOnSocketAndGemini go red with the
fake vendor's 400 (the reporter's case).
- the whole internal/gateway package: queued behind other sessions on
mote's suite lock (2h+); the full suite runs after the tag and its
reds are filed. Rerun at the rebased head (cdff4b726 parent e4197144a):
the new tests and the body-reader tests (TestPayloadTooLarge*,
TestOverLimit*) ok.
- gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows: clean.
Not tried: a real DeepSeek/OpenAI/MiniMax request (tests never reach the
network); the strict fake answers DeepSeek's wording. Bytes in an MCP
proxy's body (/mcp/...) are not touched; that isn't a model request.