3d63fc9cb made an in-stream 408/429/5xx say "(<status>: you can retry
your request)", so Pi, which decides from the text alone, tries again.
zminds' other case wasn't covered: a relay answering 400
{"message":"Concurrency limit exceeded for account, please retry
later","type":"upstream_error"}. magpie's group already fails over on
it (quotaWords has "exceeded"), but when every member says it, the text
Pi got was the relay's own, which matches none of Pi's retry words, so
the run ended after one try.
What changed: saysBusy adds " (busy: you can retry your request)" to a
400, 409 or 422 whose text says the upstream is busy (concurrency limit,
too many pending/concurrent/in-flight requests, temporarily
unavailable, retry/try again later, 并发, 稍后重试), unless it also
names spent money or quota (quota, insufficient, balance, credit,
billing, budget, usage limit, 余额, 额度, 欠费, 套餐, 用量), which a retry
doesn't fix, or already says it passes, or is too long to be a short
reason. It runs on both the whole reply (writeErrorSaid) and the
stream's error event (streamError). The status sent is unchanged.
Run at the pushed head, on mote:
- TestBusyRelaySaysItPasses (new; the words, and what keeps a text as
it is: spent quota, other statuses, already said) and
TestGroupBusyRelaySaysItPasses (new; a two-member group, both
answering zminds' literal 400 body, whole and stream, and the text
the agent gets passes Pi's own retry and deny regexes): ok, beside
TestStreamErrorSaysItPasses and TestGroupLastFailureInStreamSaysItPasses.
- Broken by hand: saysBusy a no-op, both new tests red.
- go test -tags nogui -timeout 45m ./internal/gateway/: every test ok except
TestCodexPoolSaysTheClientsVersion, red the same in a full run at a
clean d39358f1b (origin/main) copy and green alone there; it is
#4807 (another test's Codex identity reaches it).
- GOOS=linux and GOOS=windows go vet -tags nogui ./internal/gateway/:
ok; gofmt -l clean.
Not tried: a real Pi run against a busy relay. ump45nose's Cursor batch
502 is not changed here; a 5xx already carries its status (and in a
stream, 3d63fc9cb's wording), but that wasn't watched in a real run.
A Codex user on a DeepSeek model kept getting "stream disconnected
before completion: 1017dp: 413 Request Entity Too Large: a web page,
not an API reply ... (request_too_large)" with "reconnecting 5/5".
The big body took the vendor past keepHeldAfter, so Codex's stream
had begun, and streamError sent response.failed with code
server_error. Codex (codex-api sse/responses_error.rs) reads any code
it doesn't know as Retryable, so it sent the same body five more
times. Each was refused the same way, and nothing compacted.
context_length_exceeded is the one code Codex stops on: it marks the
window full and compacts, and core compact.rs trims the oldest items
for as long as the compaction is refused too.
- streamError: a 413 on the Responses shape is code
context_length_exceeded, for every Responses client. The text still
says request_too_large for pi.
- failTo: Codex reads a 413 told by HTTP status as UnexpectedStatus,
and a 400 context_length_exceeded as InvalidRequest
(api_bridge.rs). Neither compacts. So when Codex (fromCodex, or
Claude Code's Codex plugin) asked for a stream, a byte cap or an
overflow goes to it in a stream even before one began
(keptAlive.compactsInStream). A vendor's own overflow, released
until now, goes this way too. Other clients keep the status they got
before.
- Nothing changes for resting: a 413 still rests nobody, and a group
still tries the next member first.
Tests: codex_byte_cap_test.go. It replays the reporter's nginx page
mid-stream and before the stream, plus a vendor overflow 400, and
checks a non-Codex client keeps its 413. Red on the old code:
TestCodexByteCapInStreamCompacts got "server_error" (the reporter's
exact text), and both TestCodexByteCapBeforeStreamCompacts cases got
a 413 or 400 JSON. TestStreamErrorByteCapInEveryShape now expects
context_length_exceeded for Responses.
Run on mote (mote-run, this tree): go test ./internal/gateway on the
byte-cap, stream-error, overflow, compact and Codex tests: ok. GOOS=linux
and GOOS=windows go vet ./internal/gateway: ok. gofmt -l: clean.
Full suite: mote-run go test -timeout 45m ./... at fb29cd30f plus this
change (before the gateway-routing.md paragraph, docs only): 82 packages
ok; internal/gui red in two tests this change doesn't reach, both filed:
TestSettingsSaveKeepsWhatItDoesNotSend (SearchAll not kept, #4514) and
TestMainWindowCorners/macOS_26_SDK (macOS 27 toolbar, #3949).
Not tried: a real Codex against a real 413 vendor. Codex's handling
was read from openai/codex main, not run. DeepSeek documents no
request-body byte limit, and whether 1017dp is DeepSeek's own API or
a relay in front of it is unknown.
jilen (#3728): fx asked magpie for codex/gpt-6.1-sol on
/v1/chat/completions and showed "system: InvalidChunk: Invalid Chat
Completions response at model (event 1).", while the usage log had the
vendor's 502, an EOF from chatgpt.com. No stream was saved, but fx's
InvalidChunk at event 1 says one had begun: magpie had kept fx alive
with the stream's headers and comments, so the failure went as an
event of that stream (streamError). The encoder streamError made, from
an empty Request, began the message before the error with a Chat chunk
whose "model" was "". fx takes a chunk with an empty model for a broken
one and stops there, so it never read the error after it. Anthropic's
message_start and Responses' response.created, response.in_progress
and response.failed went out with an empty model the same way.
keptAlive now keeps the model the request names when serve takes it,
as magpie spells it there: a flat id (a~m) is its catalog id, a dashed
Claude id its dotted one, and Codex's own model through magpie's
accounts codex/<model>. An effort the agent put on the model is kept on
it (a/m:high), the name it asked by. serve has it from the body it
already read for askedAt; on Codex's own sign-in (codexUpstreamOn) it
is the model Codex wrote, which its turn already read. Neither reads
the body again. streamError begins with that model, from failTo,
holdWriter.release and Codex's failWith. Gemini's error names no model,
as before. docs/subsystems/gateway-routing.md says so.
fx still shows no reason. Its Chat reader ends the stream with
ProviderError at an event with an "error" key and keeps nothing of it
(src/gateway/chat_completions_protocol.zig:723 in fx 0.0.14, :710 on
fx's main e71a663b); only an error status's body reaches its message.
So fx says ProviderError where it said InvalidChunk, and the vendor's
EOF is in the stream and magpie's usage log, not in fx.
Not changed: an error after a reply that already went, a loop cut's
(cutLoop), starts a message of its own after the vendor's, under a new
id. A model in it wouldn't help, fx stops at the new id: #3792. Nor the
error's shape: the encoders still begin a message ahead of it. fx looks
for an event's "error" before its id and model, so an error alone would
get past it too, but no other agent was tried on one; that is #3792's
question as well.
What was run (tree 908b9857, on origin/main 2beed3b0, go1.26.8,
linux/amd64):
- TestStreamErrorNamesTheModel, new: streamError in each protocol with
the report's model and message. Chat's, Anthropic's and Responses'
events name that model and no other, Gemini's none, and each has the
message.
- TestKeptAliveChatErrorNamesTheModel, new: a Chat stream through serve,
kept alive while the vendor says nothing, then the vendor gone with no
reply (the EOF), or answering 502, 413, a 200 that isn't a stream, or
a JSON 200 that is no reply. In each, the stream's first chunk names
a/m and the error follows; the 413's says request_too_large. Asked
for a/m:high, the vendor gone, the first chunk names a/m:high.
- TestKeptAliveChatOnCodexErrorNamesTheModel, new: the report's route, a
Chat stream on magpie's codex provider (Codex's sign-in), the ChatGPT
backend a local server that says nothing until the stream was kept
alive and then hangs up, each time it is asked. The first chunk names
codex/gpt-5.5, and the error after it has the backend's EOF.
- TestKeptAliveCodexErrorNamesTheModel, new: Codex on its own sign-in,
the backend overloaded, Codex kept alive while the turn waits to be
asked again, then the backend gone. Every model its events name is
gpt-5.5, the model Codex asked for, and they name one.
- The four, -count=50: ok.
- -race -count=20 -cpu 1,2 over them and the stream tests beside them
(TestStreamError*, TestGroupLastFailureInStreamSaysItPasses,
TestStreamedByteCap, TestLoopEnds, TestLoopCutMidLine,
TestCodexOneAccountOverload, the Quiet ones): ok, no race.
- Broken by hand, each red:
- streamError's encoder made from an empty Request again:
TestStreamErrorNamesTheModel, every case of
TestKeptAliveChatErrorNamesTheModel,
TestKeptAliveChatOnCodexErrorNamesTheModel and
TestKeptAliveCodexErrorNamesTheModel.
- serve's keptAlive with no model: every case of
TestKeptAliveChatErrorNamesTheModel and
TestKeptAliveChatOnCodexErrorNamesTheModel.
- keptAlive given the model without its effort: the a/m:high case
alone.
- failTo naming none: the 413 case.
- holdWriter.release naming none for a reply that is no reply: the
JSON 200's case; for an error status: the vendor gone, a/m:high,
the 502, the 200 that isn't a stream, and
TestKeptAliveChatOnCodexErrorNamesTheModel.
- Codex's keptAlive with no model, or its failWith naming none:
TestKeptAliveCodexErrorNamesTheModel.
- A real run: magpie built at 2beed3b0 and at this tree (go build
-tags nogui -buildvcs=false), magpie serve in a sandbox HOME, a
provider (a local server) that holds its first answer 20s and then
hangs up, or answers 502, and fx 0.0.14 asking it through
/v1/chat/completions (fx ask, its settings' provider
openai-chat-completions at the gateway), with --json and without.
fx exits 1 after about 53s each time: InvalidChunk at 2beed3b0, all
four times (hang-up and 502, --json and not), ProviderError at this
tree, all four. curl asking the same: ": keepalive" lines, then a
first chunk with "model":"" (2beed3b0) or "model":"up/m" (this
tree), then the error with the vendor's EOF or 502.
- gofmt -l; go vet -tags nogui and go build -tags nogui for linux,
windows and darwin, and go vet and go build for windows without the
tag: exit 0 each. go vet and go build without the tag on linux: exit
1 (this box has no GTK or WebKitGTK dev packages).
- go test -tags nogui -count=1 -timeout 45m ./..., Bun 1.4.2 on PATH:
67 ok, and 7 red, none in what this changes: agent
TestCodexDriftReached (#3398); TestInstalledVersion,
TestHowInstalled, TestCursorFolderFromTheEditorIsNotTheCLI,
TestCopilotFolderFromAnIDEPluginIsNotTheCLI,
TestQoderSettingsFromAnIDEPluginIsNotTheCLI and
TestOmpDetectedWithPiDir (#3141);
TestReasonixCLIRequiresSupportedNativeVersion and
TestReasonixCLIRecoversAfterProbeTimeout (#3641); davsync
TestSyncTeraCloudStillSeesOtherWrites (#3420); edit
TestWriteAtomicReadOnlyHardLink and testenv TestProgram (#3640);
gateway TestPluginToldWhereTheAgentWorks (#3675); gui
TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie
(#3649); provider's "close of synctest channel from outside bubble"
from TestUnreadLoginsWriteErrorsKeepCredentials (#3632).
Not tried: the real chatgpt.com (it needs a ChatGPT account); the
ChatGPT backend was a local server that hangs up. fx's interactive
view, where the report's line came from.
Review (yetone/magpie maintainer agent), for jilen's #3728:
- Read every line. The only encoder built with an empty Request is
streamError's; its five callers (failTo, holdWriter.release's two
cases, cutLoop, Codex's failWith) are each given a model, cutLoop ""
on purpose (#3792).
- Red by hand at c711c3391 on 1cbdda25b, macOS, go1.27: streamError
back to &Request{}: all four new tests red; keptAlive built without
model: the two Chat kept-alive tests red; Codex's kept.model unset:
TestKeptAliveCodexErrorNamesTheModel red. Green restored.
- go test -tags nogui -race ./internal/gateway -run
'NamesTheModel|TestStreamError': ok. Full go test -tags nogui
-timeout 45m ./internal/gateway: ok (573s).
- gofmt -l clean; GOOS=linux go vet -tags nogui and GOOS=windows go vet
./internal/gateway: ok.
- Real run: magpie serve (base and patched builds) in a sandbox HOME, a
fake Chat vendor that holds 20s then answers 502, a streamed
/v1/chat/completions for fakev/fake-model. Base: the kept-alive first
chunk says "model":"", then the error. Patched: "model":
"fakev/fake-model", then the same error.
- Rebased onto ab546a4ee (gateway changed since): gofmt, vet linux and
windows, and go test ./internal/gateway -run
'NamesTheModel|StreamError|KeptAlive|KeepAlive|Alive|Codex' again
there: ok. The full package ran at the earlier base only.
- Not tried: fx itself, the real ChatGPT backend.
A vendor's 413 that came after the stream's headers had gone out (a
keepalive sent while the vendor was quiet) reached Pi as a stream error
of type api_error saying "413 Request Entity Too Large". Pi compacts only
when the error's text says request_too_large (overflow.ts), so the turn
stopped. A 413 before the headers was already typed request_too_large
(writeErrorSaid, 97f264c7).
streamError, which every failure in a stream already begun goes through
(failTo and holdWriter's), appends "(request_too_large)" to the text of a
413 that isn't a context overflow and doesn't say it already; the Chat
and Anthropic encoders type that event request_too_large. Gemini keeps
its own status shape; an overflow stays context_length_exceeded.
Review change (on top of ggbdpq's 8a278645): the test matched
request_too_large anywhere in the body, so Chat's new type alone passed
it: with the text append taken out by hand it stayed green. It now reads
the error event's message (what Pi reads) and type. The new
TestStreamErrorByteCapInEveryShape runs streamError for Chat, Anthropic,
Responses and Gemini, and for a 413 that says the prompt is too long.
Run:
- With gateway.go, chat.go and anthropic.go put back: both tests FAIL.
With only the text append taken out: both FAIL. With only Anthropic's
type taken out: TestStreamErrorByteCapInEveryShape FAILs ("anthropic …
want "type":"request_too_large""). Restored: ok.
- TestBodyRejectionErrorShapes, TestStreamErrorSaysItPasses,
TestGroupLastFailureInStreamSaysItPasses: ok.
- Full suite, go test -tags nogui -timeout 45m ./... (set -o pipefail,
sandbox HOME, load ~350) at 0224e826f (these four commits on
ebf3f48f7) plus #2545's patch: every package ok except internal/agent,
whose one red (TestRekeyGivesWSLAgentTheSharingKey) is #2545's, which
was dropped (problems posted on #2545). internal/agent alone at
0224e826f: ok. Rebased on dccfefdf7 (#2635, internal/library only):
build, vet, internal/library ok. gofmt -l clean; go vet darwin (gui),
GOOS=linux and GOOS=windows -tags nogui: clean. GUI (i18n-keys,
gui-de/ja/zh-TW, import-*, key-import, login-import): all pass except
login-import's Chromium case, which fails the same alone on origin/main
ebf3f48f7 (#1307, output posted there).
Not tried: a real nginx 413 or a real Pi compacting after it.
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
Once a group has sent the agent the stream's headers (the keepalive
while a member is slow), a later failure reaches the agent as an error
event in a 200 stream, with the member's words only: "no first token in
30s", "Upstream service temporarily unavailable", "521 status code (no
body)". Pi decides whether to retry from that text alone, and none of
these match its list, so the turn ended after one try where Claude Code
and Codex retried.
streamError, the one place every in-stream error passes, now runs the
text through saysPassing: for 408, 429 or 5xx whose text names none of
the words such agents retry on, it appends "(<status>: you can retry
your request)". A text that already says so, and a request's own fault
(400, 401, 404 ...), are left as they were. A response not yet begun
already carries its real status; unchanged.
Not done: an X-Magpie-Retryable header (can't be sent once the stream
has begun), and widening retryable()/passing() for relay wordings and
521-527 (overlaps the #2313 work on relay 400s).
Tests (internal/gateway/stream_error_passing_test.go, with Pi 1.2.0's
own allow and deny regexes as the reporter quoted them):
- TestStreamErrorSaysItPasses: the reporter's literal texts.
- TestGroupLastFailureInStreamSaysItPasses: a slow first member, a
keepalive, then a last member's relay 503; the stream's error is one
Pi retries. Broken by hand (Text: msg back): FAIL.
Run at origin/main 9d97fd073 + this change and the #2373/#2372/#2374 commits beside it (one tree), -tags nogui -count=1
-timeout 45m: internal/gateway, plugin, provider, agent,
catalog, gui ok. gofmt -l clean; go vet -tags nogui ./... for GOOS
darwin, linux, windows clean.
Full suite: SUITE_LINE