tlerbao (#5371): an Antigravity quota ran out, magpie rested the account
until the plan's reset hours away, and that horizon went to OpenCode as
Retry-After. OpenCode clamps one wait at 900s and asks the same model a
few times, so it sat ~45 minutes before its own fallback chain moved on.
RetryAfterCap (seconds, unset by default) bounds the Retry-After an
agent is told. Accounts rest exactly as long either way: restAfter and
the held try read the header before it leaves.
Changed from ggbdpq's patch:
- The cap is one wrapper in Server.Handler (retryCapped), on every
answer of 400 and up, not capAfter at four call sites. The patch
missed the vendor's own Retry-After passed on by keepRetry (19821s and
an HTTP date went out uncapped) and any other site that writes one.
The wrapper has Flush and Unwrap, so ResponseController and the
websocket's Hijack still reach the connection.
- settings.Shared() per answer, not settings.Load() from disk.
- The setting is reachable: settingsapi Keys and Merge (kept when not
sent), a Settings › Models › Replies row (1/5/15 min, No limit) with
zh, zh-TW, ja and de text, and prefsKeep. The patch failed
TestKeysAreWhatTheSaveWrites ("only saved: [retryAfterCap]").
- Stays opt-in: a default cap makes a backing-off client ask again
sooner, and each ask can spend a confirm probe on an exhausted account.
- Core script budget not raised; three app.js comments were shortened.
Run on mote at this head (rebased on 1493acb46):
- go test -tags nogui ./internal/gateway -run 'TestRetryAfter|
TestQuotaRestsUntilTheTimeOfDaySet|TestAllRestingAnswersFromRest': ok
(GoesOutWholeByDefault, CapBoundsTheAgentWait, CapLeavesTheRestWhole,
CapBoundsTheVendorsOwn, ALimitIsAsked, Rests).
- settingsapi/cli TestKeysAreWhatTheSaveWrites,
TestSettingsCLISetsWhatTheWindowSaves, TestBodySetsOneKeyOnly: ok.
- internal/gui Budget|I18n: ok.
- GUI, chromium + webkit: retry-after-cap-setting (new, 5 languages,
1100 and 420), refusal-retries-setting, settings-hints, i18n-keys,
i18n-en-no-cjk, reply-model-setting: 91 of 92. The one red is
i18n-keys "a plugin's words are its own", already filed as #5332 and
#5426 (strings outside this change).
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui on
gateway, settings, settingsapi: clean.
Broken by hand: with retryCapped taken out of Handler,
CapBoundsTheAgentWait fails (Retry-After "19252", want at most 60s) and
CapBoundsTheVendorsOwn fails (Retry-After "19821", want "60", and the
HTTP-date case). With the patch's own code, CapBoundsTheVendorsOwn fails
the same way.
Not tried: a real Antigravity account running out with a real OpenCode
fallback chain; the full suite.
Rebased onto 766d2a9f5's descendants: TestCoreScriptBudget was 1 byte
over (1533803 > 1533802) after ce2a59daa's app.js change; the new
row's comment lost a word. At the pushed head on mote:
TestCoreScriptBudget ok, -run RetryAfter ./internal/gateway ok.
Co-authored-by: ggbdpq <ggbdpq@gmail.com>
142 lines
4.8 KiB
Go
142 lines
4.8 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// burnBoth rests both keys the way a plan's day running out does: the
|
|
// quota comes back at, hours from now.
|
|
func burnBoth(t *testing.T, hours int, on ...func()) *httptest.ResponseRecorder {
|
|
d, srv := deadChain(t, "dead-a", "dead-b")
|
|
t.Cleanup(func() { _ = d.took() })
|
|
for _, f := range on {
|
|
f()
|
|
}
|
|
at := time.Now().Add(time.Duration(hours)*time.Hour + 21*time.Minute).Truncate(time.Minute)
|
|
if err := provider.SetQuotaResetAt("mockdead", at.UTC().Format("15:04")+"Z"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := 0; i < 3; i++ {
|
|
postDead(t, srv)
|
|
}
|
|
rec := postDead(t, srv)
|
|
if rec.Code != http.StatusTooManyRequests {
|
|
t.Fatalf("turn-away: %d %s", rec.Code, rec.Body)
|
|
}
|
|
// the confirm asks restLast makes (one resting candidate a request,
|
|
// #1772) are the vendor's business as ever; the cap is about the
|
|
// wait the agent is told, not about burning the accounts
|
|
d.took()
|
|
return rec
|
|
}
|
|
|
|
// #5371 (tlerbao): a quota exhaustion rests the account for hours, and
|
|
// the 429 the agent gets carried the whole horizon as Retry-After — an
|
|
// agent that runs its own fallback chain (OpenCode clamps a single wait
|
|
// at 900s, retries the same model a few times) then stalls ~45 minutes
|
|
// before it ever fails over.
|
|
func TestRetryAfterGoesOutWholeByDefault(t *testing.T) {
|
|
rec := burnBoth(t, 5)
|
|
ra, err := strconv.Atoi(rec.Header().Get("Retry-After"))
|
|
if err != nil || ra < 4*3600 {
|
|
t.Fatalf("default: Retry-After %q, want the rest's whole horizon (~5h), err %v", rec.Header().Get("Retry-After"), err)
|
|
}
|
|
if !strings.Contains(rec.Body.String(), "weekly usage limit") {
|
|
t.Errorf("default: the vendor's reason is gone: %s", rec.Body)
|
|
}
|
|
}
|
|
|
|
func TestRetryAfterCapBoundsTheAgentWait(t *testing.T) {
|
|
rec := burnBoth(t, 5, func() {
|
|
// the cap is on before the plan's day runs out: the agent's wait
|
|
// is bounded, the accounts rest the whole horizon just the same
|
|
capRetryAfterOn(t, 60)
|
|
})
|
|
ra, err := strconv.Atoi(rec.Header().Get("Retry-After"))
|
|
if err != nil || ra > 60 {
|
|
t.Fatalf("capped: Retry-After %q, want at most 60s — the agent fails over instead of sleeping the horizon (err %v)", rec.Header().Get("Retry-After"), err)
|
|
}
|
|
if !strings.Contains(rec.Body.String(), "weekly usage limit") {
|
|
t.Errorf("capped: the vendor's reason is gone: %s", rec.Body)
|
|
}
|
|
}
|
|
|
|
// capRetryAfterOn turns the cap on, as Settings' row saves it.
|
|
func capRetryAfterOn(t *testing.T, secs int) {
|
|
t.Helper()
|
|
st := settings.Load()
|
|
st.RetryAfterCap = &secs
|
|
if err := settings.Save(st); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// The cap bounds only what the agent is told: the accounts rest the
|
|
// plan's whole horizon inside magpie, and are not asked again sooner.
|
|
func TestRetryAfterCapLeavesTheRestWhole(t *testing.T) {
|
|
burnBoth(t, 5, func() { capRetryAfterOn(t, 60) })
|
|
restingUntil.Lock()
|
|
var longest time.Time
|
|
for _, until := range restingUntil.m {
|
|
if until.After(longest) {
|
|
longest = until
|
|
}
|
|
}
|
|
restingUntil.Unlock()
|
|
if time.Until(longest) < 4*time.Hour {
|
|
t.Fatalf("the accounts rest until %v, want the plan's reset ~5h away", longest)
|
|
}
|
|
}
|
|
|
|
// A vendor's own Retry-After, passed on with its 429 (keepRetry), is
|
|
// bounded the same way: Antigravity's quota runs out with a reset hours
|
|
// away, said by the vendor rather than by magpie's rest.
|
|
func TestRetryAfterCapBoundsTheVendorsOwn(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
cap int
|
|
said, want string
|
|
}{
|
|
{"unset", 0, "19821", "19821"},
|
|
{"over", 60, "19821", "60"},
|
|
{"under", 60, "30", "30"},
|
|
{"date", 60, time.Now().Add(5 * time.Hour).UTC().Format(http.TimeFormat), "60"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
restingUntil.Lock()
|
|
restingUntil.m, restingUntil.note = map[string]time.Time{}, map[string]Rest{}
|
|
restingUntil.Unlock()
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Retry-After", tc.said)
|
|
w.WriteHeader(http.StatusTooManyRequests)
|
|
w.Write([]byte(`{"error":{"message":"quota exhausted","type":"rate_limit_error"}}`))
|
|
}))
|
|
t.Cleanup(up.Close)
|
|
if err := provider.Save(provider.Provider{ID: "mockdead", Name: "mockdead", Chat: up.URL + "/v1", Models: []string{"glm-5.3-flash"}, Key: "k"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tc.cap > 0 {
|
|
capRetryAfterOn(t, tc.cap)
|
|
}
|
|
rec := postDead(t, New())
|
|
if rec.Code != http.StatusTooManyRequests {
|
|
t.Fatalf("status %d %s", rec.Code, rec.Body)
|
|
}
|
|
if got := rec.Header().Get("Retry-After"); got != tc.want {
|
|
t.Errorf("Retry-After %q, want %q", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|