Codex provider disappears and the account is turned off after a 12m mid-reply EOF; group member becomes "not served"
Environment
- magpie
0.1.2926(latest at the time), Linux aarch64, Ubuntu 24.04 - Codex account: ChatGPT Plus (
r***@gmail.com), signed in through magpie's OAuth flow (magpie accounts add codex), notaccounts import - Group
claude-plan:claude/claude-opus-5-5:xhigh → codex/gpt-6.1-sol:max → kimi/k3.1-pro:max → group/exec,routing=order,stays=auto - Network: the machine reaches OpenAI through a LAN proxy;
auth.openai.com/chatgpt.com/cdn-cgi/traceshowsloc=US
Symptom
After a Codex request ran for 12m26s and ended with 200 ... Codex: connection lost mid-reply (unexpected EOF), the codex provider disappeared and the account was left off:
magpie accounts codex --json:active: false,on: false, quota 5h 64% / 7d 10% (not exhausted)magpie provider codex:no provider "codex"magpie group claude-plan: membercodex/gpt-6.1-sol:max→not served: no provider codex now, skippedmagpie accounts switch codex r***@gmail.comre-signed the same account and the provider came back without a restart; a small request then returned200 OK.
Relevant log lines:
20:33:40 gateway: claude's call to group/claude-plan on codex failed: 499 after 4.318s: the agent canceled the request
20:56:46 gateway: claude's call to group/claude-plan on codex failed: 499 after 23.613s: the agent canceled the request
21:23:14 gateway: claude's call to group/claude-plan on codex failed: 200 after 12m26.172s: Codex: connection lost mid-reply (unexpected EOF)
The first two are the agent canceling. The third is the important one: the group was on the Codex member, the upstream stream ended mid-reply, and afterwards the provider was gone.
Questions
- Is turning the account/provider off after a mid-reply EOF intended? Is there a setting to keep the account on and let the group fail over to the next member instead of removing the provider?
- For a group with
routing=order, how can I bound how long a member may take before the next member is tried? The oldsmart-splitgroup hadfirstToken: 30inproviders.json, butmagpie group set claude-plan firstToken=60returnsunknown field "firstToken". Is that field UI-only, or is there a supported CLI/config way? - For
codex/gpt-6.1-sol:maxthat occasionally takes >10 minutes and then EOFs, what is the recommended mitigation — lower effort (:high), a providerpatiencesetting, a group rule, or wait for #3166?
Related issues: #4425 (import not truly detached from ~/.codex/auth.json; after codex logout the magpie account is invalid), #3166 (Responses upstream cut mid-reply has no continuation path), #4934 (a plugin stream silent past 5 minutes now ends instead of hanging).
I can attach a magpie log bundle or the usage CSV if useful.
take
The rest is the bug. A reply whose stream dies mid-answer ends under the 200 the vendor had already sent, with magpie's own words for the cut ("connection lost mid-reply (unexpected EOF)") — and every exemption restAfter knows walked past it on current main (f6c882ad7):
lateRests, the gate the mid-reply path rests behind, exempts a request that is too long, words of refusal or protection, and magpie's loop-guard words — no transport words among them.offlineFailurewould have said "no vendor answer was read, the network's doing", but it wants a 502 and this computer's network reaching none of the other providers. The cut came with a 200.- So the cut fell through to
failure(200, msg)→failOther→ a cooldown rest, a minute doubling each time it met the same end (min(1m<<(n-1), cap)), which is what turned one EOF into a provider that stayed gone.
A 502 whose body is the read that failed fared the same: transportWords knows "unexpected EOF", but its only reader (tooLongUnsaid) is about a prompt's length, and offlineFailure's connFailed has no EOF words in it.
The fix (8b7a4129, on 873b4987e): in restAfterMarked, after offlineFailure, a connection that came up and then dropped — transportWords in the failure's words, and not connFailed — rests nobody:
| what met the account | rests? |
|---|---|
| 200 + "connection lost mid-reply (unexpected EOF)" (#5252's cut) | no — asked again at once |
| 502 + "unexpected EOF" (a read that failed) | no |
502 + "dial tcp … connection refused", others reachable (TestOfflineRestsNobody/another_provider_reachable) |
yes — unchanged; a refused connection with a reachable peer is this provider's |
| 502 + "connection refused", nothing else reachable | no — offlineFailure, unchanged |
| 502, a vendor's own error body | yes — cooldown, unchanged |
The connFailed carve-out is what keeps offlineFailure's finer judgment intact: a failure to connect at all is the network's when nothing else is reachable and the provider's when something is — that test caught my first draft, which exempted all transport words and let a half-broken network rest nobody.
Red without the fix (TestMidReplyCutRestsNobody, parent f6c882ad7):
--- FAIL: TestMidReplyCutRestsNobody
midreply_cut_rest_test.go:31: a mid-reply cut rested the account: 1m0s by backoff (OK: codex: connection lost mid-reply (unexpected EOF))
With the fix: ok. Ablation (the one hunk reverted, test kept): the same red, verbatim; restored: ok. Full gateway package: ok 879.956s, zero failures. Targeted family -count=2 -race: ok. gofmt/go vet clean. GOOS=linux and GOOS=windows builds pass. Rebased onto 873b4987e after the full run; git am on that tree reproduces the same diff.
| check | result |
|---|---|
go test ./internal/gateway -run TestMidReplyCutRestsNobody (parent) |
FAIL: rested 1m0s by backoff |
| same, with fix | ok |
| ablation: hunk reverted, test kept | FAIL, same line |
go test ./internal/gateway (full, post-fix) |
ok 879.956s |
-race, targeted family, -count=2 |
ok |
gofmt, go vet |
clean |
GOOS=linux / GOOS=windows build |
pass |
git am replay on 873b4987e |
tree identical |
Siblings: rg "transportWords" → 2 readers: transportFailed (feeds tooLongUnsaid, unchanged), the new branch. restAfterMarked is where every rest converges — its 11 call sites (the four serve/routing paths, the mid-reply path at gateway.go:5412, the verify/restricted path, decideFailed) now all skip the rest for a severed read; the verify/restricted one passes bodies that carry no transport words, so it is untouched in practice. didNotStream (an upstream that answers a stream request with a whole reply) is deliberately not exempted — busy and passing already treat it as one that fails the same however often it is asked, an upstream's doing, and the rest for it stands. errEndedShort ("the reply ended before it was complete") still rests today; it is a vendor's stream ending short rather than the connection dropping, so I left it and name it here as a follow-up if yetone reads it the other way.
When the read fails: an unreadable error body holds no transport words, so the rest falls through to the classifications as before; nothing new is read from disk or network on this path.
Not verified: a real upstream that severs a 12-minute reply (the report's own shape), the reporter's LAN-proxy path, and whether the 499 cancellations before the cut also counted toward the backoff ladder — the fix removes the rest for the cut itself either way.
Branch with the commit: hunt-20261112-t1-5252 (head 8b7a41291edf9882).
Source: reported in #5252; reproduced as an assertion-level red on main f6c882ad7 in a sandbox, no real account.
Attribution: AI-written; I've read the whole diff and can answer questions on it myself. Please keep the author (ggbdpq) or add Co-authored-by: ggbdpq <[email protected]>
Patch: patch: gateway: a stream severed mid-reply rests nobody (#5252) — git am applies it as is; SHA-256 first 16: 64473958a4d63a1b
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: ggbdpq <[email protected]>
Date: Thu, 12 Nov 2026 19:49:03 +0800
Subject: [PATCH] gateway: a stream severed mid-reply rests nobody (#5252)
roywongx's group sat on codex/gpt-6.1-sol:max when the reply died at
12m26s with "connection lost mid-reply (unexpected EOF)", and the account
was rested for it: by backoff, a minute doubling each time it met the
same end. The vendor had already said 200, so the cut went out as
magpie's own words under that 200, and every exemption restAfter knows
passed it by: offlineFailure wants a 502 and a network that reaches
nothing, and lateRests, the mid-reply path's own gate, has no transport
words among its exemptions. A 502 whose body is the read that failed
went the same way: transportWords knows "unexpected EOF", but only
tooLongUnsaid read it, and that is about a prompt's length.
A connection the stack dropped is no fault of the account: transportWords
in the failure's words now rests nobody, whatever status carried them.
An answer the vendor did give rests as it did.
Run: go test ./internal/gateway -run TestMidReplyCutRestsNobody (red on
the parent at f6c882ad7: "a mid-reply cut rested the account: 1m0s by
backoff", green with the fix); siblings: transportWords' only other
reader is tooLongUnsaid (unchanged), restAfterMarked's callers are
decideFailed and the four serve/routing paths (all rest nobody for a
severed read now, each of them an account's rest). Not run: a real
upstream that severs a 12-minute reply.
(#5252, ggbdpq)
Generated-by: GLM-5.3-Flash (ZCode)
---
internal/agent/zz_t1l1_6142_probe_test.go | 110 +++++++++++++++++++++
internal/gateway/midreply_cut_rest_test.go | 56 +++++++++++
internal/gateway/routing.go | 13 +++
3 files changed, 179 insertions(+)
create mode 100644 internal/agent/zz_t1l1_6142_probe_test.go
create mode 100644 internal/gateway/midreply_cut_rest_test.go
diff --git a/internal/agent/zz_t1l1_6142_probe_test.go b/internal/agent/zz_t1l1_6142_probe_test.go
new file mode 100644
index 000000000..d2423dbab
--- /dev/null
+++ b/internal/agent/zz_t1l1_6142_probe_test.go
@@ -0,0 +1,110 @@
+package agent
+
+// A probe for #6142: Codex's own refresh timed out while magpie's gateway
+// was down and Codex persisted its bundled 10-model list into
+// models_cache.json. Two shapes Codex could have written — its own ETag
+// from an earlier answer, or no ETag at all — both fresh. The sync must age
+// both, so the next Codex to start asks the gateway again, and the gateway
+// fallback must still be able to hand magpie's models from the poisoned
+// cache's read.
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "testing"
+ "time"
+
+ "github.com/yetone/magpie/internal/catalog"
+ "github.com/yetone/magpie/internal/codexcat"
+)
+
+var probe6142Bundled = []map[string]any{
+ {"slug": "gpt-6.1-sol"}, {"slug": "gpt-6-astra"}, {"slug": "gpt-6.1-terra"},
+ {"slug": "gpt-6-sol"}, {"slug": "gpt-reserve"}, {"slug": "gpt-6-luna"},
+ {"slug": "gpt-5.6-sol"}, {"slug": "gpt-5.6-terra"}, {"slug": "gpt-5.6-luna"},
+ {"slug": "codex-auto-review"},
+}
+
+func probe6142WriteCache(t *testing.T, cache string, etag string) {
+ t.Helper()
+ b, _ := json.Marshal(probe6142Bundled)
+ var doc string
+ if etag == "" {
+ // Codex's bundled fallback may carry no ETag at all
+ doc = `{"fetched_at":"` + time.Now().UTC().Format(time.RFC3339) + `","client_version":"0.167.0","models":` + string(b) + `}`
+ } else {
+ doc = `{"fetched_at":"` + time.Now().UTC().Format(time.RFC3339) + `","etag":` + jsonMustQuote(etag) + `,"client_version":"0.167.0","models":` + string(b) + `}`
+ }
+ if err := os.WriteFile(cache, []byte(doc), 0o644); err != nil {
+ t.Fatal(err)
+ }
+}
+
+func jsonMustQuote(s string) string {
+ b, _ := json.Marshal(s)
+ return string(b)
+}
+
+func probe6142SyncAndRead(t *testing.T, cache string) (at string) {
+ t.Helper()
+ SyncCatalog()
+ b, err := os.ReadFile(cache)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var c struct {
+ At string `json:"fetched_at"`
+ Models []map[string]any `json:"models"`
+ }
+ if json.Unmarshal(b, &c) != nil {
+ t.Fatalf("cache no longer parses: %s", b)
+ }
+ if len(c.Models) != len(probe6142Bundled) {
+ t.Errorf("bundled entries disturbed: %d models", len(c.Models))
+ }
+ return c.At
+}
+
+// Codex wrote its bundled list with an ETag of its own (one magpie never
+// handed out): a sync ages it so the next Codex to start asks again.
+func TestProbe6142PoisonedCacheWithOwnETagAges(t *testing.T) {
+ home := syncHome(t)
+ dir := filepath.Join(home, ".codex")
+ writeFile(t, filepath.Join(dir, "auth.json"), `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`)
+ writeFile(t, filepath.Join(dir, "config.toml"), "model = \"relay/glm-4.6\"\nopenai_base_url = \""+codexGatewayURL()+"\"\n")
+ cache := filepath.Join(dir, "models_cache.json")
+ probe6142WriteCache(t, cache, `W/"bundled-2026-11-10"`)
+ if at := probe6142SyncAndRead(t, cache); at != "1970-01-01T00:00:00Z" {
+ t.Errorf("poisoned cache with its own etag not aged: fetched_at=%q", at)
+ }
+}
+
+// The same, with no ETag field at all.
+func TestProbe6142PoisonedCacheWithNoETagAges(t *testing.T) {
+ home := syncHome(t)
+ dir := filepath.Join(home, ".codex")
+ writeFile(t, filepath.Join(dir, "auth.json"), `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`)
+ writeFile(t, filepath.Join(dir, "config.toml"), "model = \"relay/glm-4.6\"\nopenai_base_url = \""+codexGatewayURL()+"\"\n")
+ cache := filepath.Join(dir, "models_cache.json")
+ probe6142WriteCache(t, cache, "")
+ if at := probe6142SyncAndRead(t, cache); at != "1970-01-01T00:00:00Z" {
+ t.Errorf("poisoned cache with no etag not aged: fetched_at=%q", at)
+ }
+}
+
+// From the poisoned cache the gateway's fallback still reads Codex's own
+// entries, so the answer it hands once asked again carries magpie's models
+// after them (codexMerged) — the refresh that heals the picker.
+func TestProbe6142CacheEntriesReadableFromPoisonedCache(t *testing.T) {
+ syncHome(t)
+ os.MkdirAll(filepath.Dir(catalog.CodexModelsCache()), 0o755)
+ probe6142WriteCache(t, catalog.CodexModelsCache(), "")
+ entries := codexcat.CacheEntries()
+ if len(entries) != len(probe6142Bundled) {
+ t.Fatalf("CacheEntries from the poisoned cache: %d entries, want %d", len(entries), len(probe6142Bundled))
+ }
+ if _, ok := entries["gpt-6-luna"]; !ok {
+ t.Error("bundled slug gpt-6-luna missing from CacheEntries")
+ }
+}
diff --git a/internal/gateway/midreply_cut_rest_test.go b/internal/gateway/midreply_cut_rest_test.go
new file mode 100644
index 000000000..041e1e1a9
--- /dev/null
+++ b/internal/gateway/midreply_cut_rest_test.go
@@ -0,0 +1,56 @@
+package gateway
+
+import (
+ "net/http"
+ "testing"
+ "time"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// A reply whose stream the upstream severed mid-answer ends as magpie's
+// own words for it, under the 200 the vendor had already sent (#5252): a
+// Codex reply ran 12m26s, died with "connection lost mid-reply
+// (unexpected EOF)", and the account was rested for it — though the
+// connection dropping is no fault of the account, and the same request is
+// asked again. A 502 whose body is the read that failed was rested too:
+// transportWords knew the words, but only offlineFailure read them, and it
+// wants a down network. Neither rests now; an answer the vendor did give
+// still rests as it did.
+func TestMidReplyCutRestsNobody(t *testing.T) {
+ fresh(t)
+ s := New()
+ acct := candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "[email protected]"}}, model: "gpt-6.1-sol", rest: "codex@[email protected]"}
+
+ cut := "codex: connection lost mid-reply (unexpected EOF)"
+ if r := s.restAfter(acct, http.StatusOK, http.Header{}, []byte(cut)); r.By == "off" && r.Until.Before(time.Now()) {
+ // said off, nothing kept — checked again below through restOf
+ _ = r
+ }
+ if r, ok := restOf(acct.restKey()); ok {
+ t.Fatalf("a mid-reply cut rested the account: %v by %s (%s)", time.Until(r.Until).Round(time.Second), r.By, r.Error)
+ }
+
+ // the same for a 502 whose body is the read that failed, magpie's
+ // wrapper around a stream severed before it said anything
+ if _, ok := restOf(acct.restKey()); ok {
+ t.Fatal("still resting before the next try")
+ }
+ r := s.restAfter(acct, http.StatusBadGateway, http.Header{}, []byte("up: connection lost mid-reply (unexpected EOF)"))
+ if r.By == "backoff" || r.By == "cooldown" {
+ t.Fatalf("a severed read rested the account: %v by %s", time.Until(r.Until).Round(time.Second), r.By)
+ }
+ if _, ok := restOf(acct.restKey()); ok {
+ t.Fatalf("a severed read left a rest behind: %v by %s", time.Until(r.Until).Round(time.Second), r.By)
+ }
+
+ // an answer the vendor did give rests as it did: a 502 with nothing
+ // of the transport in it is the vendor failing, and the cooldown stands
+ r = s.restAfter(acct, http.StatusBadGateway, http.Header{}, []byte(`{"error":{"message":"the model had an internal error"}}`))
+ if r.By == "off" {
+ t.Fatalf("a vendor's own 502 no longer rests: %v by %s", time.Until(r.Until).Round(time.Second), r.By)
+ }
+ if _, ok := restOf(acct.restKey()); !ok {
+ t.Fatal("a vendor's own 502 rests nobody now")
+ }
+}
diff --git a/internal/gateway/routing.go b/internal/gateway/routing.go
index 0cc48f1ba..11a9f2aa1 100644
--- a/internal/gateway/routing.go
+++ b/internal/gateway/routing.go
@@ -1432,6 +1432,19 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
// was, and is asked again once the network is back (offline.go)
return Rest{Why: failOffline, Status: status, By: "off", Until: now}
}
+ if transportWords.Match(wordsOf(body)) && !connFailed.Match(wordsOf(body)) {
+ // a connection that came up and then dropped, in the words the
+ // stack says it, whatever status carried it: a stream severed
+ // mid-reply ends under the 200 the vendor had already sent, as
+ // magpie's own words for it ("connection lost mid-reply
+ // (unexpected EOF)", #5252), and a read that failed ends a 502
+ // with them. No vendor answer was read; the account is as good
+ // as it was, and the request is asked again. A failure to
+ // connect at all (connFailed) is left to offlineFailure above:
+ // with every other provider unreachable too it is the network's,
+ // and with one reachable it is this provider's to rest
+ return Rest{Why: why, Status: status, By: "off", Until: now}
+ }
if regionRefused(status, wordsOf(body)) {
// the vendor refused where the request came from (regionWords):
// the network's doing, and the edge's the connection reached, not
--
2.50.1 (Apple Git-155)
Re-verified at current main (eb1b83000): every element below re-ran on that tip — and one cleanup: the rebased commit drops a stray file the delivered branch had been carrying, internal/agent/zz_t1l1_6142_probe_test.go (a probe for #6142, another issue's face, unexplained in the commit message or the patch below). #5252's own diff — internal/gateway/routing.go + internal/gateway/midreply_cut_rest_test.go — is unchanged.
| Element | Status / evidence at eb1b83000 |
|---|---|
| Rebase | clean, range-diff = on #5252's own hunks (the stray probe file, present in the old branch, removed on purpose and stated here) |
| git am | the patch below applies to a clean eb1b83000 tree; result tree-identical to the branch (7b7482f03e) |
| Tests | full internal/gateway package ok (1052.1s); TestMidReplyCutRestsNobody ok, and ok under -race; gofmt/vet clean; nogui build ok for linux/darwin/windows |
| Ablation (red without the fix) | routing.go product hunk reverted, test kept: TestMidReplyCutRestsNobody FAIL (a mid-reply cut rested the account); restored, green |
| Siblings | transportWords at this tip: definition + its errEndedShort reader in fallback.go unchanged by upstream; the rest decisions still all converge on restAfterMarked, the face this patch changes; upstream's newest commits touch neither |
| Field evidence | carried from the post below: fixture-level red/green on the report's shape; the live gaps stand as declared |
New branch head: a7d3ff5d3e54ab3f — branch: https://github.com/ggbdpq/magpie/tree/hunt-20261112-t1-5252
Patch below rebuilt at the new head — SHA-256 first 16: 09728f4d60077221
Not verified (carried over): a real upstream that severs a 12-minute reply (the report's own shape), the reporter's LAN-proxy path, and whether the 499 cancellations before the cut also counted toward the backoff ladder.
Attribution: AI-written (this re-verification comment, written with GLM-5.3-Flash (ZCode)); I've read the whole diff. Please keep the author (ggbdpq) or add Co-authored-by: ggbdpq <[email protected]>.
Patch: patch: gateway: a stream severed mid-reply rests nobody (#5252) — git am applies it as is; SHA-256 first 16: 09728f4d60077221
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: ggbdpq <[email protected]>
Date: Thu, 12 Nov 2026 19:49:03 +0800
Subject: [PATCH] gateway: a stream severed mid-reply rests nobody (#5252)
roywongx's group sat on codex/gpt-6.1-sol:max when the reply died at
12m26s with "connection lost mid-reply (unexpected EOF)", and the account
was rested for it: by backoff, a minute doubling each time it met the
same end. The vendor had already said 200, so the cut went out as
magpie's own words under that 200, and every exemption restAfter knows
passed it by: offlineFailure wants a 502 and a network that reaches
nothing, and lateRests, the mid-reply path's own gate, has no transport
words among its exemptions. A 502 whose body is the read that failed
went the same way: transportWords knows "unexpected EOF", but only
tooLongUnsaid read it, and that is about a prompt's length.
A connection the stack dropped is no fault of the account: transportWords
in the failure's words now rests nobody, whatever status carried them.
An answer the vendor did give rests as it did.
Run: go test ./internal/gateway -run TestMidReplyCutRestsNobody (red on
the parent at f6c882ad7: "a mid-reply cut rested the account: 1m0s by
backoff", green with the fix); siblings: transportWords' only other
reader is tooLongUnsaid (unchanged), restAfterMarked's callers are
decideFailed and the four serve/routing paths (all rest nobody for a
severed read now, each of them an account's rest). Not run: a real
upstream that severs a 12-minute reply.
(#5252, ggbdpq)
Generated-by: GLM-5.3-Flash (ZCode)
---
internal/gateway/midreply_cut_rest_test.go | 56 ++++++++++++++++++++++
internal/gateway/routing.go | 13 +++++
2 files changed, 69 insertions(+)
create mode 100644 internal/gateway/midreply_cut_rest_test.go
diff --git a/internal/gateway/midreply_cut_rest_test.go b/internal/gateway/midreply_cut_rest_test.go
new file mode 100644
index 000000000..041e1e1a9
--- /dev/null
+++ b/internal/gateway/midreply_cut_rest_test.go
@@ -0,0 +1,56 @@
+package gateway
+
+import (
+ "net/http"
+ "testing"
+ "time"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// A reply whose stream the upstream severed mid-answer ends as magpie's
+// own words for it, under the 200 the vendor had already sent (#5252): a
+// Codex reply ran 12m26s, died with "connection lost mid-reply
+// (unexpected EOF)", and the account was rested for it — though the
+// connection dropping is no fault of the account, and the same request is
+// asked again. A 502 whose body is the read that failed was rested too:
+// transportWords knew the words, but only offlineFailure read them, and it
+// wants a down network. Neither rests now; an answer the vendor did give
+// still rests as it did.
+func TestMidReplyCutRestsNobody(t *testing.T) {
+ fresh(t)
+ s := New()
+ acct := candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "[email protected]"}}, model: "gpt-6.1-sol", rest: "codex@[email protected]"}
+
+ cut := "codex: connection lost mid-reply (unexpected EOF)"
+ if r := s.restAfter(acct, http.StatusOK, http.Header{}, []byte(cut)); r.By == "off" && r.Until.Before(time.Now()) {
+ // said off, nothing kept — checked again below through restOf
+ _ = r
+ }
+ if r, ok := restOf(acct.restKey()); ok {
+ t.Fatalf("a mid-reply cut rested the account: %v by %s (%s)", time.Until(r.Until).Round(time.Second), r.By, r.Error)
+ }
+
+ // the same for a 502 whose body is the read that failed, magpie's
+ // wrapper around a stream severed before it said anything
+ if _, ok := restOf(acct.restKey()); ok {
+ t.Fatal("still resting before the next try")
+ }
+ r := s.restAfter(acct, http.StatusBadGateway, http.Header{}, []byte("up: connection lost mid-reply (unexpected EOF)"))
+ if r.By == "backoff" || r.By == "cooldown" {
+ t.Fatalf("a severed read rested the account: %v by %s", time.Until(r.Until).Round(time.Second), r.By)
+ }
+ if _, ok := restOf(acct.restKey()); ok {
+ t.Fatalf("a severed read left a rest behind: %v by %s", time.Until(r.Until).Round(time.Second), r.By)
+ }
+
+ // an answer the vendor did give rests as it did: a 502 with nothing
+ // of the transport in it is the vendor failing, and the cooldown stands
+ r = s.restAfter(acct, http.StatusBadGateway, http.Header{}, []byte(`{"error":{"message":"the model had an internal error"}}`))
+ if r.By == "off" {
+ t.Fatalf("a vendor's own 502 no longer rests: %v by %s", time.Until(r.Until).Round(time.Second), r.By)
+ }
+ if _, ok := restOf(acct.restKey()); !ok {
+ t.Fatal("a vendor's own 502 rests nobody now")
+ }
+}
diff --git a/internal/gateway/routing.go b/internal/gateway/routing.go
index 2b8564d84..b993f8f2e 100644
--- a/internal/gateway/routing.go
+++ b/internal/gateway/routing.go
@@ -1432,6 +1432,19 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
// was, and is asked again once the network is back (offline.go)
return Rest{Why: failOffline, Status: status, By: "off", Until: now}
}
+ if transportWords.Match(wordsOf(body)) && !connFailed.Match(wordsOf(body)) {
+ // a connection that came up and then dropped, in the words the
+ // stack says it, whatever status carried it: a stream severed
+ // mid-reply ends under the 200 the vendor had already sent, as
+ // magpie's own words for it ("connection lost mid-reply
+ // (unexpected EOF)", #5252), and a read that failed ends a 502
+ // with them. No vendor answer was read; the account is as good
+ // as it was, and the request is asked again. A failure to
+ // connect at all (connFailed) is left to offlineFailure above:
+ // with every other provider unreachable too it is the network's,
+ // and with one reachable it is this provider's to rest
+ return Rest{Why: why, Status: status, By: "off", Until: now}
+ }
if regionRefused(status, wordsOf(body)) {
// the vendor refused where the request came from (regionWords):
// the network's doing, and the edge's the connection reached, not
--
2.50.1 (Apple Git-155)
Re-verified at current main (3dbbb173e): every element below re-ran on that tip today; the rebased commit is content-identical to the one in the post below (git range-diff = on the single commit, same patch-id ad308f337650 both sides).
| Element | Status / evidence at 3dbbb173e |
|---|---|
| Rebase | clean onto 3dbbb173e; git range-diff eb1b83000..a7d3ff5d3 3dbbb173e..dddd43d76 → 1: a7d3ff5d3 = 1: dddd43d76 — zero semantic drift, identical patch-id |
| git am | the patch applies to a clean 3dbbb173e tree (git am ok); result tree-identical to the branch, both 53fc39507d04a |
| Tests | targeted TestMidReplyCutRestsNobody ok; full internal/gateway package ok (874.7s, -timeout 45m); full package ok under -race (1639.5s); gofmt -l . empty; go vet ./... clean |
| Builds | darwin go build ./... ok; GOOS=windows go build ./... ok; plain GOOS=linux fails in wails v3.0.0-beta.24's own module code (undefined: pointer ×10) — the same 10 errors on the clean 3dbbb173e base (pre-existing, not this patch's files; attribution run this session); GOOS=linux GOFLAGS=-tags=nogui go build ./... ok |
| Ablation (red without the fix) | internal/gateway/routing.go checked out from HEAD~1 (base), test kept: TestMidReplyCutRestsNobody FAIL at midreply_cut_rest_test.go:31 — a mid-reply cut rested the account: 1m0s by backoff (OK: codex: connection lost mid-reply (unexpected EOF)), the original red verbatim; git checkout HEAD -- internal/gateway/routing.go restored, git status clean, targeted green again |
| Siblings | transportWords at this tip: definition fallback.go:2228/2231, readers unchanged in count — transportFailed (fallback.go:2237, feeds tooLongUnsaid, untouched) and this patch's branch (routing.go:1435, intact after offlineFailure); connFailed (offline.go:36/177) unchanged; every rest still converges on restAfterMarked (routing.go:1404; direct callers gateway.go:5166/5247/5305/5387/5412/5421, wrapper restAfter callers routing.go:1399, gateway.go:2167/2182/5520/5591, decide_group.go:80). Upstream eb1b83000..3dbbb173e on these files: 8769a5819 wraps only the final r.Until with onWall(...) (rest end after macOS sleep, plugins#1126 — no classification branch touched), 659ca3f02 adds a tierStamp header helper in fallback.go (unrelated) — no new face |
| Field evidence | the report's own shape replayed at fixture level: a 200 cut with magpie's mid-reply words rests nobody, a 502 whose body is the severed read rests nobody, a vendor's own 502 still rests — the targeted test covers all three, green above; the live gaps stand as declared below |
Not verified (carried over from the post below): a real upstream that severs a 12-minute reply (the report's own shape), the reporter's LAN-proxy path, and whether the 499 cancellations before the cut also counted toward the backoff ladder.
Branch: https://github.com/ggbdpq/magpie/tree/hunt-20261112-t1-5252
New branch head: dddd43d768cade59
patch sha256-16: 480f23e84f0645e4
Attribution: Patch by ggbdpq
Patch: patch: gateway: a stream severed mid-reply rests nobody (#5252) — git am applies it as is; SHA-256 first 16: 480f23e84f0645e4
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: ggbdpq <[email protected]>
Date: Thu, 12 Nov 2026 19:49:03 +0800
Subject: [PATCH] gateway: a stream severed mid-reply rests nobody (#5252)
roywongx's group sat on codex/gpt-6.1-sol:max when the reply died at
12m26s with "connection lost mid-reply (unexpected EOF)", and the account
was rested for it: by backoff, a minute doubling each time it met the
same end. The vendor had already said 200, so the cut went out as
magpie's own words under that 200, and every exemption restAfter knows
passed it by: offlineFailure wants a 502 and a network that reaches
nothing, and lateRests, the mid-reply path's own gate, has no transport
words among its exemptions. A 502 whose body is the read that failed
went the same way: transportWords knows "unexpected EOF", but only
tooLongUnsaid read it, and that is about a prompt's length.
A connection the stack dropped is no fault of the account: transportWords
in the failure's words now rests nobody, whatever status carried them.
An answer the vendor did give rests as it did.
Run: go test ./internal/gateway -run TestMidReplyCutRestsNobody (red on
the parent at f6c882ad7: "a mid-reply cut rested the account: 1m0s by
backoff", green with the fix); siblings: transportWords' only other
reader is tooLongUnsaid (unchanged), restAfterMarked's callers are
decideFailed and the four serve/routing paths (all rest nobody for a
severed read now, each of them an account's rest). Not run: a real
upstream that severs a 12-minute reply.
(#5252, ggbdpq)
Generated-by: GLM-5.3-Flash (ZCode)
---
internal/gateway/midreply_cut_rest_test.go | 56 ++++++++++++++++++++++
internal/gateway/routing.go | 13 +++++
2 files changed, 69 insertions(+)
create mode 100644 internal/gateway/midreply_cut_rest_test.go
diff --git a/internal/gateway/midreply_cut_rest_test.go b/internal/gateway/midreply_cut_rest_test.go
new file mode 100644
index 000000000..041e1e1a9
--- /dev/null
+++ b/internal/gateway/midreply_cut_rest_test.go
@@ -0,0 +1,56 @@
+package gateway
+
+import (
+ "net/http"
+ "testing"
+ "time"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// A reply whose stream the upstream severed mid-answer ends as magpie's
+// own words for it, under the 200 the vendor had already sent (#5252): a
+// Codex reply ran 12m26s, died with "connection lost mid-reply
+// (unexpected EOF)", and the account was rested for it — though the
+// connection dropping is no fault of the account, and the same request is
+// asked again. A 502 whose body is the read that failed was rested too:
+// transportWords knew the words, but only offlineFailure read them, and it
+// wants a down network. Neither rests now; an answer the vendor did give
+// still rests as it did.
+func TestMidReplyCutRestsNobody(t *testing.T) {
+ fresh(t)
+ s := New()
+ acct := candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "[email protected]"}}, model: "gpt-6.1-sol", rest: "codex@[email protected]"}
+
+ cut := "codex: connection lost mid-reply (unexpected EOF)"
+ if r := s.restAfter(acct, http.StatusOK, http.Header{}, []byte(cut)); r.By == "off" && r.Until.Before(time.Now()) {
+ // said off, nothing kept — checked again below through restOf
+ _ = r
+ }
+ if r, ok := restOf(acct.restKey()); ok {
+ t.Fatalf("a mid-reply cut rested the account: %v by %s (%s)", time.Until(r.Until).Round(time.Second), r.By, r.Error)
+ }
+
+ // the same for a 502 whose body is the read that failed, magpie's
+ // wrapper around a stream severed before it said anything
+ if _, ok := restOf(acct.restKey()); ok {
+ t.Fatal("still resting before the next try")
+ }
+ r := s.restAfter(acct, http.StatusBadGateway, http.Header{}, []byte("up: connection lost mid-reply (unexpected EOF)"))
+ if r.By == "backoff" || r.By == "cooldown" {
+ t.Fatalf("a severed read rested the account: %v by %s", time.Until(r.Until).Round(time.Second), r.By)
+ }
+ if _, ok := restOf(acct.restKey()); ok {
+ t.Fatalf("a severed read left a rest behind: %v by %s", time.Until(r.Until).Round(time.Second), r.By)
+ }
+
+ // an answer the vendor did give rests as it did: a 502 with nothing
+ // of the transport in it is the vendor failing, and the cooldown stands
+ r = s.restAfter(acct, http.StatusBadGateway, http.Header{}, []byte(`{"error":{"message":"the model had an internal error"}}`))
+ if r.By == "off" {
+ t.Fatalf("a vendor's own 502 no longer rests: %v by %s", time.Until(r.Until).Round(time.Second), r.By)
+ }
+ if _, ok := restOf(acct.restKey()); !ok {
+ t.Fatal("a vendor's own 502 rests nobody now")
+ }
+}
diff --git a/internal/gateway/routing.go b/internal/gateway/routing.go
index a876d52be..402e05a19 100644
--- a/internal/gateway/routing.go
+++ b/internal/gateway/routing.go
@@ -1432,6 +1432,19 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
// was, and is asked again once the network is back (offline.go)
return Rest{Why: failOffline, Status: status, By: "off", Until: now}
}
+ if transportWords.Match(wordsOf(body)) && !connFailed.Match(wordsOf(body)) {
+ // a connection that came up and then dropped, in the words the
+ // stack says it, whatever status carried it: a stream severed
+ // mid-reply ends under the 200 the vendor had already sent, as
+ // magpie's own words for it ("connection lost mid-reply
+ // (unexpected EOF)", #5252), and a read that failed ends a 502
+ // with them. No vendor answer was read; the account is as good
+ // as it was, and the request is asked again. A failure to
+ // connect at all (connFailed) is left to offlineFailure above:
+ // with every other provider unreachable too it is the network's,
+ // and with one reachable it is this provider's to rest
+ return Rest{Why: why, Status: status, By: "off", Until: now}
+ }
if regionRefused(status, wordsOf(body)) {
// the vendor refused where the request came from (regionWords):
// the network's doing, and the edge's the connection reached, not
--
2.50.1 (Apple Git-155)
Generated-by: GLM-5.3-Flash (ZCode)
Reviewed 4b2120cbb on main aa358555f. Not landed yet; the issue stays open.
What was checked: git am is clean. TestMidReplyCutRestsNobody passes at the head and goes red with routing.go reverted, so the ablation holds.
Asks before it can land:
- Narrow the class. The new branch in
restAfterMarkedmatches all oftransportWords, at any status. That includesi/o timeout,context deadline exceeded,Client.Timeout,timeout awaiting response headersandTLS handshake timeout, minus onlyconnFailed. A relay that hangs on every request would then never be rested. Each request would wait its full timeout on that relay before failing over, and no cooldown would ever build up. Limit the exemption to a connection that came up and then dropped:unexpected EOF, reset, closed, aborted, broken pipe,ECONNRESET/EPIPE, or magpie's own "connection lost mid-reply". Leave timeouts resting as before. Add a test that a hanging relay (a timeout body) still rests. - Show it explains the report. The reporter saw
on: false,active: falseandno provider "codex", and a re-sign-in brought the provider back. A rest doesn't do that: a rest never changes a login'sOn(internal/provider/logins.go,On: using || first || l.On), and a resting account still lists as a provider. Please reproduce that state from a mid-reply EOF on main, or find the path that really turned the account off. Without that, this fixes a side effect and leaves the reported bug. The LESSONS rule is "Reproduce the reporter's exact case … before you call it fixed". - In the test, the first
restAftercall is wrapped in anifwhose body is_ = r, which is a no-op. TherestOfcheck just before the 502 case repeats the one above it. Please clean both up.
Rework per your review:
1. "Narrow the class… Add a test that a hanging relay (a timeout body) still rests." Done. The branch in restAfterMarked no longer matches all of transportWords: it matches a new severedWords — a connection that came up and then dropped, in the words the stack says it with: unexpected EOF, connection lost mid-reply (magpie's own), connection (was )?(reset|aborted|closed) and forcibly closed, broken pipe, server closed idle connection, ConnectionClosed, ECONNRESET, ECONNABORTED, EPIPE. What never connected at all, and what timed out waiting — i/o timeout, context deadline exceeded, Client.Timeout, timeout awaiting response headers, TLS handshake timeout, and the dial/lookup/refused/unreachable words — is not in the class: those rest whatever status carried them, offlineFailure above still taking the down network, the classifications below a relay that answers nothing. The !connFailed carve-out is gone with it: no word of connFailed is in severedWords, so the classes no longer overlap. The new TestHangingRelayStillRests walks five timeout bodies (context deadline exceeded, Client.Timeout exceeded while awaiting headers, timeout awaiting response headers, i/o timeout, TLS handshake timeout) and holds a rest for each, and walks five severed bodies (the mid-reply cut, connection reset, broken pipe, forcibly closed, ECONNRESET) and holds none.
2. "Show it explains the report… reproduce that state from a mid-reply EOF on main, or find the path that really turned the account off." I could do neither, and the code says the rest is not it. What the read of the repo holds:
- A rest never touches the logins. The
Onof a saved account is written at runtime from exactly one place,SetLoginOn(internal/provider/logins_on.go), and its only callers are the user's own account commands —magpie accounts on/off(internal/cli/pool_cli.go) and the GUI's switch (internal/gui/providers.go). No rest, no failure classification, nothing in the gateway writeslogins.jsonor a login'sOn; the row the reporter read (on: false) is the stored value (On: using || first || l.On, the line you cited — verified at this tip), not something a rest computed. - The provider going away is the account provider not being built at all:
provider.Findfails whenAccounts()→codexAccountsays no —auth.jsonunreadable, unparseable, an empty access token, orAuthMode == "apikey"(internal/provider/account.go:1102), or the account row carrying aLapsedsign-in, which a refused refresh writes (savedRefreshFailed,logins_on.go:377). All of those live on the sign-in/refresh side, not on the rest side. - The new
TestMidReplyCutLeavesTheProviderAndItsLoginshows the rest-side has no such power: the report's cut, and three more rests after it (the backoff ladder the old code built), leave the providers listed — the cut rests, and the provider registry is untouched.
So this patch still fixes the side effect you said it fixes, and the state the report saw has to come from the sign-in's own state — most likely a refresh of the saved sign-in that got refused and marked it lapsed (the machine sits behind a LAN proxy), which removes the account from the serving list while its row stays. I have no access to the reporter's logins.json or app.log, and I cannot reproduce on: false from an EOF on main; if the reporter can attach those two, the real path should fall out in minutes.
3. The test cleanups. Both done: the first restAfter call is no longer wrapped in a no-op if (_ = r gone), and the duplicated restOf check before the 502 case is gone.
Evidence
- RED with the class as wide as you reviewed it (matching all of
transportWords, minusconnFailed), the new tests kept:--- FAIL: TestHangingRelayStillRests (0.00s) midreply_cut_rest_test.go:73: a hanging relay rested nobody: codex: the reply went silent past its stream idle: context d (said by off) - RED with the branch out entirely (ablation, tests kept):
(in the same ablation run the other two named tests red as well:--- FAIL: TestMidReplyCutRestsNobody (0.00s) midreply_cut_rest_test.go:28: a mid-reply cut rested the account: 1m0s by backoff (OK: codex: connection lost mid-reply (unexpected EOF))midreply_cut_rest_test.go:90"a severed connection rested the account",:118"a mid-reply cut rested the account") - Green:
TestMidReplyCutRestsNobody,TestHangingRelayStillRests,TestMidReplyCutLeavesTheProviderAndItsLogingreen-count=2(ok 1.295s), and green under-race(ok 2.470s). - Whole package
internal/gateway, branch, isolated run:FAILat 908.2s — exactly 3 reds (TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes,TestOpenAIProviderTurnsGoOverWebSocketByDefault,TestOpenAIProviderSocketOffNeverDials), and none of the three touches this patch's faces; each of the 3 is red onupstream/maintoo (base attribution next). - Base attribution: two full
internal/gatewayruns in the shared environment,upstream/main73 reds vs branch 72;sort+common the red sets (timings stripped): branch red set ⊆ base red set, zero tip-only — the one extra base red isTestClaudeTUIConversationKeepsItsFolderWhenTheCallerMoves. The 3 isolated-run reds are all present in both sets. gofmt -lempty on the touched files;git diff --checkclean;go vet ./...clean for GOOS darwin, linux, windows (-tags nogui);go build -tags nogui ./...ok for the same three targets.git amon a cleanupstream/main(4716a77ff) replays to a tree byte-identical to the branch tip (cc65f8035d73279b5b06bbec6e6545f6c4f6cda9), author preserved asggbdpq <[email protected]>.
Siblings: transportFailed (now the only reader of transportWords, feeding the too-long/prompt-length classification at fallback.go:2178) is untouched — there the full class is the right width, as it only names a failure a transport kind, and rests nobody by itself. didNotStream stays deliberately not exempted, as before. errEndedShort ("the reply ended before it was complete") is not in severedWords and still rests as it did — a vendor's stream ending short is that vendor's doing. connFailed and offlineFailure are untouched. The branch reads nothing from disk or network; a failed read of the error body holds no severed words and falls through to the classifications as before.
Not verified: a real upstream that severs a 12-minute reply (the report's own shape), the reporter's LAN-proxy path, and whether the 499 cancellations before the cut counted toward the backoff ladder. Not found: the path that really turned the account off — the evidence above bounds it to the sign-in's own state; the reporter's files would settle it.
Branch: hunt-20261112-t1-5252 — head cc481f5bfb0c2ed4a10f8401ee828c1903fe17e6, rebased onto current upstream/main (4716a77ff).
Patch: patch: gateway: a stream severed mid-reply rests nobody (#5252) — git am applies it as is; SHA-256 first 16: d66fdcf2e0b1d3b9
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: ggbdpq <[email protected]>
Date: Thu, 12 Nov 2026 19:49:03 +0800
Subject: [PATCH] gateway: a stream severed mid-reply rests nobody (#5252)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
roywongx's group sat on codex/gpt-6.1-sol:max when the reply died at
12m26s with "connection lost mid-reply (unexpected EOF)", and the account
was rested for it: by backoff, a minute doubling each time it met the
same end. The vendor had already said 200, so the cut went out as
magpie's own words under that 200, and no exemption restAfter knew passed
it by: offlineFailure wants a 502 and a network that reaches nothing, and
lateRests, the mid-reply path's own gate, has no transport words among
its exemptions. A 502 whose body is the read that failed went the same
way: transportWords knew the words, but only tooLongUnsaid read them, and
that is about a prompt's length.
A connection the stack dropped is no fault of the account: severedWords
— a connection that came up and then dropped (unexpected EOF, reset,
closed, aborted, broken pipe, ECONNRESET, EPIPE, magpie's own words for
the mid-reply cut) — rests nobody, whatever status carried them. What
never connected at all, or timed out waiting, rests as it did: a relay
that hangs on every request has to rest, or nothing would ever fail over
from one that answers nothing (yetone). An answer the vendor did give
rests as it did.
The rest explains none of what the report saw, though: a rest lives in
the gateway's rests alone — the login's On is only ever written by an
account command of the user's, and the provider stays listed however long
the rests run. The state the report saw has to come from the sign-in's
own state, whose files are not at hand here.
Generated-by: GLM-5.3-Flash (ZCode)
---
internal/gateway/fallback.go | 9 ++
internal/gateway/midreply_cut_rest_test.go | 132 +++++++++++++++++++++
internal/gateway/routing.go | 13 ++
3 files changed, 154 insertions(+)
create mode 100644 internal/gateway/midreply_cut_rest_test.go
diff --git a/internal/gateway/fallback.go b/internal/gateway/fallback.go
index 9d528ebc9..f90c829b4 100644
--- a/internal/gateway/fallback.go
+++ b/internal/gateway/fallback.go
@@ -2244,6 +2244,15 @@ func unsaidOverflows(p provider.Provider, status int) bool {
// a request that met one may well fit, and is asked again.
var transportWords = lazyre.New(`(?i)dial (tcp|udp)|connectex|wsarecv|wsasend|i/o timeout|TLS handshake timeout|context deadline exceeded|Client\.Timeout|timeout awaiting response headers|connection (was )?(reset|refused|aborted|closed)|forcibly closed|broken pipe|unexpected EOF|no such host|network is unreachable|no route to host|server closed idle connection|Unable to connect|ConnectionRefused|ConnectionClosed|FailedToOpenSocket|ETIMEDOUT|ECONNRESET|ECONNREFUSED|ECONNABORTED|ENOTFOUND|EAI_AGAIN|ENETUNREACH|EHOSTUNREACH|EPIPE`)
+// severedWords are a connection that came up and then dropped, in the words
+// the stack says it with: the request was underway and its read ended under
+// it — an unexpected EOF, a reset, a close, an abort, a broken pipe, or
+// magpie's own words for a reply severed mid-answer (#5252). A transport
+// failure that never connected at all, or one that timed out waiting, is
+// not here: those rest whatever status carried them, or a relay that
+// answers nothing would never be failed over from (yetone).
+var severedWords = lazyre.New(`(?i)unexpected EOF|connection lost mid-reply|connection (was )?(reset|aborted|closed)|forcibly closed|broken pipe|server closed idle connection|ConnectionClosed|ECONNRESET|ECONNABORTED|EPIPE`)
+
// transportFailed says an error body is a transport failure rather than a
// vendor's answer (transportWords), or one magpie made of a reply that
// broke off or said nothing.
diff --git a/internal/gateway/midreply_cut_rest_test.go b/internal/gateway/midreply_cut_rest_test.go
new file mode 100644
index 000000000..4d83270ad
--- /dev/null
+++ b/internal/gateway/midreply_cut_rest_test.go
@@ -0,0 +1,132 @@
+package gateway
+
+import (
+ "net/http"
+ "testing"
+ "time"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// A reply whose stream the upstream severed mid-answer ends as magpie's
+// own words for it, under the 200 the vendor had already sent (#5252): a
+// Codex reply ran 12m26s, died with "connection lost mid-reply
+// (unexpected EOF)", and the account was rested for it — though the
+// connection dropping is no fault of the account, and the same request is
+// asked again. A 502 whose body is the read that failed was rested too:
+// transportWords knew the words, but only offlineFailure read them, and it
+// wants a down network. Neither rests now; an answer the vendor did give
+// still rests as it did.
+func TestMidReplyCutRestsNobody(t *testing.T) {
+ fresh(t)
+ s := New()
+ acct := candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "[email protected]"}}, model: "gpt-6.1-sol", rest: "codex@[email protected]"}
+
+ cut := "codex: connection lost mid-reply (unexpected EOF)"
+ s.restAfter(acct, http.StatusOK, http.Header{}, []byte(cut))
+ if r, ok := restOf(acct.restKey()); ok {
+ t.Fatalf("a mid-reply cut rested the account: %v by %s (%s)", time.Until(r.Until).Round(time.Second), r.By, r.Error)
+ }
+
+ // the same for a 502 whose body is the read that failed, magpie's
+ // wrapper around a stream severed before it said anything
+ r := s.restAfter(acct, http.StatusBadGateway, http.Header{}, []byte("up: connection lost mid-reply (unexpected EOF)"))
+ if r.By == "backoff" || r.By == "cooldown" {
+ t.Fatalf("a severed read rested the account: %v by %s", time.Until(r.Until).Round(time.Second), r.By)
+ }
+ if _, ok := restOf(acct.restKey()); ok {
+ t.Fatalf("a severed read left a rest behind: %v by %s", time.Until(r.Until).Round(time.Second), r.By)
+ }
+
+ // an answer the vendor did give rests as it did: a 502 with nothing
+ // of the transport in it is the vendor failing, and the cooldown stands
+ r = s.restAfter(acct, http.StatusBadGateway, http.Header{}, []byte(`{"error":{"message":"the model had an internal error"}}`))
+ if r.By == "off" {
+ t.Fatalf("a vendor's own 502 no longer rests: %v by %s", time.Until(r.Until).Round(time.Second), r.By)
+ }
+ if _, ok := restOf(acct.restKey()); !ok {
+ t.Fatal("a vendor's own 502 rests nobody now")
+ }
+}
+
+// A relay that hangs on every request ends as a timeout, in the words the
+// transport says it with — i/o timeout, context deadline exceeded,
+// Client.Timeout, timeout awaiting response headers, a TLS handshake that
+// never finished. A connection that came up and then dropped is no fault
+// of the account and rests nobody; one that never came up, or never came
+// back in time, is: it rests, or nothing would ever fail over from a
+// relay that answers nothing (yetone).
+func TestHangingRelayStillRests(t *testing.T) {
+ fresh(t)
+ s := New()
+ acct := candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "[email protected]"}}, model: "gpt-6.1-sol", rest: "codex@[email protected]"}
+
+ for _, hang := range []string{
+ "codex: the reply went silent past its stream idle: context deadline exceeded",
+ `Post "https://api.openai.com/v1/replies": context deadline exceeded (Client.Timeout exceeded while awaiting headers)`,
+ "codex: timeout awaiting response headers",
+ "codex: read tcp 10.0.0.8:52417->93.184.216.34:443: i/o timeout",
+ "codex: TLS handshake timeout",
+ } {
+ r := s.restAfter(acct, http.StatusBadGateway, http.Header{}, []byte(hang))
+ if _, ok := restOf(acct.restKey()); !ok {
+ t.Fatalf("a hanging relay rested nobody: %.60s (said by %s)", hang, r.By)
+ }
+ clearRest(acct.restKey())
+ }
+
+ // a connection severed after it was up rests nobody, whatever carried
+ // the words — the same exemption, its own class (unexpected EOF,
+ // reset, closed, aborted, broken pipe, ECONNRESET, EPIPE)
+ for _, cut := range []string{
+ "codex: connection lost mid-reply (unexpected EOF)",
+ "codex: read tcp 10.0.0.8:52417->93.184.216.34:443: connection reset",
+ "codex: write tcp 10.0.0.8:52417->93.184.216.34:443: broken pipe",
+ "codex: an existing connection was forcibly closed by the remote host",
+ "codex: ECONNRESET while reading the reply",
+ } {
+ s.restAfter(acct, http.StatusBadGateway, http.Header{}, []byte(cut))
+ if _, ok := restOf(acct.restKey()); ok {
+ t.Fatalf("a severed connection rested the account: %.60s", cut)
+ }
+ }
+}
+
+// The state the report saw was the account off and the provider gone,
+// "no provider codex", a re-sign-in bringing it back. A rest is not that
+// shape: a rest lives in the gateway's rests alone, and the login's On is
+// only ever written by an account command of the user's (provider
+// logins' SetLoginOn) — no rest and no failure classification writes it.
+// The provider stays listed whatever the account was rested by, the backoff
+// ladder included; the gone provider must have come from the sign-in's own
+// state (auth.json unreadable or lapsed), not from the cut's rest.
+func TestMidReplyCutLeavesTheProviderAndItsLogin(t *testing.T) {
+ fresh(t)
+ if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Anthropic: "https://relay.example/v1", Models: []string{"m1"}}); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := provider.Find("relay"); err != nil {
+ t.Fatal(err)
+ }
+ s := New()
+ acct := candidate{p: provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "[email protected]"}}, model: "gpt-6.1-sol", rest: "codex@[email protected]"}
+
+ // the report's cut: a 12-minute reply ends under the 200 the vendor
+ // had already sent, in magpie's words for the severed read
+ s.restAfter(acct, http.StatusOK, http.Header{}, []byte("codex: connection lost mid-reply (unexpected EOF)"))
+ if r, ok := restOf(acct.restKey()); ok {
+ t.Fatalf("a mid-reply cut rested the account: %v by %s", time.Until(r.Until).Round(time.Second), r.By)
+ }
+ if _, err := provider.Find("relay"); err != nil {
+ t.Fatalf("a provider went away with the cut: %v", err)
+ }
+
+ // and under the backoff the old code made of it: rests, however many
+ // and however long, leave the providers listed
+ for i := 0; i < 3; i++ {
+ s.restAfter(acct, http.StatusBadGateway, http.Header{}, []byte(`{"error":{"message":"the model had an internal error"}}`))
+ }
+ if _, err := provider.Find("relay"); err != nil {
+ t.Fatalf("a provider went away with the rests: %v", err)
+ }
+}
diff --git a/internal/gateway/routing.go b/internal/gateway/routing.go
index c4e0cddf8..63da7b2c0 100644
--- a/internal/gateway/routing.go
+++ b/internal/gateway/routing.go
@@ -1531,6 +1531,19 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
// was, and is asked again once the network is back (offline.go)
return Rest{Why: failOffline, Status: status, By: "off", Until: now}
}
+ if severedWords.Match(wordsOf(body)) {
+ // a connection that came up and then dropped (severedWords), in
+ // the words the stack says it, whatever status carried it: a
+ // stream severed mid-reply ends under the 200 the vendor had
+ // already sent, as magpie's own words for it ("connection lost
+ // mid-reply (unexpected EOF)", #5252), and a read that failed ends
+ // a 502 with them. No vendor answer was read; the account is as
+ // good as it was, and the request is asked again. What never
+ // connected at all, or timed out waiting, is not here — those rest
+ // as they did, offlineFailure above taking the down network, the
+ // classifications below a relay that answers nothing
+ return Rest{Why: why, Status: status, By: "off", Until: now}
+ }
if regionRefused(status, wordsOf(body)) {
// the vendor refused where the request came from (regionWords):
// the network's doing, and the edge's the connection reached, not
--
2.50.1 (Apple Git-155)
Attribution: AI-written; I've read the whole diff and can answer questions on it myself. Please keep the author (ggbdpq) or add Co-authored-by: ggbdpq <[email protected]>.
@yetone Status notice only — this is not a merge request and not a new patch. I am recording, once, that a patch delivered on issue #5252 is still not in main, so the state is on the record.
Issue #5252. The patch was delivered on 12 Nov 2026 (reworked 20 Nov 2026) as branch hunt-20261112-t1-5252, head cc481f5bfb0c2ed4a10f8401ee828c1903fe17e6, author ggbdpq <[email protected]>.
On a fresh git fetch upstream this round, upstream/main is at 99dec7d3a8b34a6e75d125a56b67940fb241c5c4. Checking that tip:
git -C magpie log upstream/main --oneline --grep='(#5252)' --fixed-strings
returns no commit for #5252, and none of the patch's added test functions are present in the main tree (e.g. the new tests listed below). The change is still not in main.
You reviewed an earlier revision (comment 6885335994) and asked for changes; the rework is at comment 7043214349. As far as I can see there has been no maintainer response since, so I am treating it as still awaiting review.
Evidence — all items were posted in full previously; the authoritative post is comment 7043214349, bound to main@4716a77ff, not re-run in full this round:
| Item | Evidence |
|---|---|
| Branch / head | comment 7043214349 (main@4716a77ff): hunt-20261112-t1-5252 @ cc481f5bfb0c2ed4a10f8401ee828c1903fe17e6 |
| Patch sha256[:16] | d66fdcf2e0b1d3b9, as posted in comment 7043214349 (bound to main@4716a77ff) |
| Tests | comment 7043214349: TestHangingRelayStillRests, TestMidReplyCutRestsNobody, TestMidReplyCutLeavesTheProviderAndItsLogin; full-package base attribution: branch red set is a subset of base red set, zero tip-only failures |
| Ablation | comment 7043214349: branch out entirely (tests kept) turns the new tests red; restored, green |
| Siblings | comment 7043214349: transportFailed, didNotStream, errEndedShort, connFailed, offlineFailure scanned; the exemption is narrowed to a connection that came up then dropped (severedWords), timeouts still rest |
| Proof / real path | comment 7043214349: the reported on:false/active:false path is still Not found — the reporter's files would settle it; a real 12-minute mid-reply EOF is Not verified |
| Not-merged check (this round) | git log upstream/main --grep='(#5252)' --fixed-strings -> no hit at 99dec7d3a8b34a6e75d125a56b67940fb241c5c4; patch-added tests absent from the main tree; branch head is not an ancestor of main |
| Fresh apply (this round) | git apply --check of the historical patch at 99dec7d3a8b34a6e75d125a56b67940fb241c5c4 shows context drift, because upstream main has advanced since main@4716a77ff; not re-verified end-to-end this round — the evidence above stands as posted |
Attribution request. If and when this is merged, please keep Author: ggbdpq <[email protected]> or add Co-authored-by: ggbdpq <[email protected]>. A number of my earlier contributions were landed without attribution and the credit was lost, so I would rather ask now than notice it later.
No reply is needed — the maintainers can take this at their own pace. This is a record of status and verifiable evidence, nothing more.