zoecats on Discord: a request to one provider (not a group) got a local
relay's short 429 straight back in Claude Code once the few same-one
retries were spent. A group would have held the agent and asked again.
A plain provider now takes the group's replan path (patience.go: replan,
pauseAlive, the held writer) once passing's same-one retries are spent:
its accounts take turns as members do, each pause never shorter than the
Retry-After, and a streaming agent gets keepalives past keepHeldAfter.
- Zero-config: unset, it waits singlePatience, PatienceDefault (60s),
counted from when the agent would have had the error.
- The user's value wins: Provider.Patience, the same values as a group's
Patience, set with `magpie provider set <id> patience=2m|off|default`
(at most 4m). The provider editor has no row for it (no new UI), and its
save keeps it. A group the provider is in keeps the group's own.
- Not retried: a Retry-After past the budget, quota/credit/plan limits,
401/403, refusals, response protection, didNotStream, and an agent
that has gone (499, try marked Gone).
- The try is traced with Again alone, so the Routing page tells it as
"tried again in …".
- codexReset (ChatGPT's 503 for a large request) is left out of the
plain-provider replan, so Codex still gets its 503 with advice.
- Codex's own one-account relay (codexUpstreamOn) does not join. It is
relayed outside the candidate loop, and its backend 429 is always the
plan's usage limit (codexBusy never retries one), so the short rate
limit this waits out doesn't arise there. Its overloads keep
lastRetries and then fallbacks.
longestPause became a var so tests can scale it. Tests updated for the
new count (1 + retries + replanRounds when it never clears):
- TestLastOneLeftIsTriedAgain
- TestRateLimitIsWaitedOut
- TestQueuedStreamFailureIsTheStreamsError
- TestOverflowTheVendorDidntSay
New tests, through the server with a fake upstream and injected
durations, waiting on trace state, not a duration:
- TestSingleProviderWaitsOutAShortRateLimit (stream and whole: 200,
keepalive before the answer, asked twice)
- TestSingleProviderPassesALongLimitThrough (an hour, quota, 401)
- TestSingleProviderAgentGoneInTheWait
- TestSingleProviderOwnPatienceWins (off, shorter and longer than the
default)
- TestPatiencePair (cli)
- TestProviderSaveKeepsPatience (gui)
Broken by hand, each red, then restored:
- singlePatience fallback 0 for a plain provider: WaitsOut stream and
whole got 429, and AgentGone never paused.
- replan's budget check off: PassesALongLimitThrough/an_hour, "magpie
waits to ask again".
- pauseAlive's ctx.Done case removed: AgentGone, "route done false
status 0".
- The gateway ignoring Provider.Patience: OwnPatienceWins off, shorter
and longer all red.
- The GUI save not keeping Patience: TestProviderSaveKeepsPatience,
"patience 0 … want -1 kept".
Run in this tree, before the rebase:
- go test ./internal/gateway ./internal/cli ./internal/gui: ok.
- ./internal/provider: ok with -skip TestUnreadLogins. That test crashes
with "close of synctest channel from outside bubble", the known #3632,
in filememo and not touched here.
- gofmt -l: clean.
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go
vet ./...: all 0.
- go build for darwin, linux (nogui) and windows: ok.
Run again after rebasing on origin/main 39526c069 (17 commits, 22 files
in gateway/provider/cli): gofmt, go build, go vet on the four
packages, the replan, retry, overflow and Codex-reset tests, and every
new test: all ok. The whole gateway package was not run again there.
Not run:
- The full ./... suite: machine load ~150, other sessions' suites
running.
- A real vendor's 429.
- The GUI suite: no GUI file changed.
72 lines
3.6 KiB
Go
72 lines
3.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Some vendors don't say a conversation is too long for the model: Devin
|
|
// answered one of about 225k tokens for swe-1.7-lightning (a 202,752
|
|
// window) with a 502 "capacity issues", WorkBuddy one for glm-5.1 with a
|
|
// 400 "Invalid request parameters", Qoder one of 2M for qmodel with a 400
|
|
// "Error in upstream response" — each answered at a size the model holds.
|
|
// Asked again in place three times, the agent was then handed a 502 and
|
|
// never compacted. A request well past the model's window that fails so
|
|
// is told as one too long, at once.
|
|
func TestOverflowTheVendorDidntSay(t *testing.T) {
|
|
big := strings.Repeat("word ", 2400) // about 3000 tokens as estimated
|
|
small := strings.Repeat("word ", 100) // about 125
|
|
near := strings.Repeat("word ", 1760) // about 2200, which may yet fit
|
|
for _, tc := range []struct {
|
|
name, path, body string
|
|
code int
|
|
reply string
|
|
wantCode, calls int
|
|
want string
|
|
}{
|
|
{"502 anthropic", "/v1/messages", big, 502, `{"error":{"message":"We are experiencing capacity issues"}}`, 400, 1, "prompt is too long"},
|
|
{"400 chat", "/v1/chat/completions", big, 400, `{"error":{"message":"Invalid request parameters"}}`, 400, 1, "context_length_exceeded"},
|
|
// Text alone well past the window is enough to ask for compaction.
|
|
{"413 bytes", "/v1/messages", big, 413, `{"error":{"message":"Request Entity Too Large"}}`, 400, 1, "prompt is too long"},
|
|
{"413 request_too_large", "/v1/messages", big, 413, `{"error":{"type":"request_too_large","message":"Request exceeds the maximum size"}}`, 400, 1, "prompt is too long"},
|
|
// not the conversation's length: a plain provider's busy reply is
|
|
// asked again past the same-one retries, within its patience,
|
|
// until replan's rounds run out
|
|
{"small 502", "/v1/messages", small, 502, `{"error":{"message":"We are experiencing capacity issues"}}`, 502, 1 + lastRetries + replanRounds, "capacity issues"},
|
|
{"near the window", "/v1/messages", near, 502, `{"error":{"message":"We are experiencing capacity issues"}}`, 502, 1 + lastRetries + replanRounds, "capacity issues"},
|
|
{"429", "/v1/messages", big, 429, `{"error":{"message":"Rate limit reached"}}`, 429, 1 + rateRetries + replanRounds, "Rate limit"},
|
|
{"401", "/v1/messages", big, 401, `{"error":{"message":"invalid token"}}`, 401, 1, "invalid token"},
|
|
{"quota", "/v1/messages", big, 400, `{"error":{"message":"insufficient balance"}}`, 400, 1, "insufficient balance"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
proto := provider.Anthropic
|
|
if strings.Contains(tc.path, "chat") {
|
|
proto = provider.Chat
|
|
}
|
|
f := &fake{code: tc.code, ctype: "application/json", reply: tc.reply}
|
|
up := setup(t, proto, f)
|
|
p := provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Contexts: map[string]int{"m1": 2000}}
|
|
if proto == provider.Chat {
|
|
p.Chat = up.URL + "/v1"
|
|
} else {
|
|
p.Anthropic = up.URL
|
|
}
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req := `{"model":"fake/m1","max_tokens":10,"messages":[{"role":"user","content":"` + tc.body + `"}]}`
|
|
code, body := post(t, tc.path, req)
|
|
if code != tc.wantCode || !strings.Contains(body, tc.want) || f.calls != tc.calls {
|
|
t.Fatalf("status %d after %d calls (want %d after %d): %s", code, f.calls, tc.wantCode, tc.calls, body)
|
|
}
|
|
if tc.wantCode != 400 || tc.name == "quota" {
|
|
if strings.Contains(body, "prompt is too long") || strings.Contains(body, "context_length_exceeded") {
|
|
t.Fatalf("told as an overflow: %s", body)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|