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>
79 lines
2.4 KiB
Go
79 lines
2.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// retryCapped bounds the Retry-After an agent is told, when Settings'
|
|
// RetryAfterCap is set (#5371, tlerbao): a plan's quota running out rests
|
|
// its account inside magpie until the plan's reset, hours away, and that
|
|
// horizon went to the agent as its wait. An agent with its own fallback
|
|
// chain (OpenCode clamps one wait at 900s and asks the same model three
|
|
// times) then sat about 45 minutes before it moved on.
|
|
//
|
|
// It is the one place every answer leaves the gateway: magpie's own
|
|
// turn-aways, a vendor's Retry-After passed on (keepRetry) and the
|
|
// limits' 429s alike. What magpie reads to rest an account (the held
|
|
// try's header, restAfter) is read before here, so the account rests
|
|
// exactly as long either way. Unset, nothing changes.
|
|
func retryCapped(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
next.ServeHTTP(&retryCapWriter{ResponseWriter: w}, r)
|
|
})
|
|
}
|
|
|
|
type retryCapWriter struct {
|
|
http.ResponseWriter
|
|
wrote bool
|
|
}
|
|
|
|
func (c *retryCapWriter) WriteHeader(code int) {
|
|
if !c.wrote {
|
|
c.wrote = true
|
|
if code >= 400 {
|
|
capRetryAfter(c.ResponseWriter.Header(), time.Now())
|
|
}
|
|
}
|
|
c.ResponseWriter.WriteHeader(code)
|
|
}
|
|
|
|
func (c *retryCapWriter) Write(b []byte) (int, error) {
|
|
c.wrote = true // an implicit 200
|
|
return c.ResponseWriter.Write(b)
|
|
}
|
|
|
|
func (c *retryCapWriter) Flush() { _ = http.NewResponseController(c.ResponseWriter).Flush() }
|
|
|
|
// Unwrap lets http.ResponseController and the websocket's Accept reach
|
|
// the connection's own writer (deadlines, Hijack).
|
|
func (c *retryCapWriter) Unwrap() http.ResponseWriter { return c.ResponseWriter }
|
|
|
|
// capRetryAfter writes h's Retry-After, in seconds or as a date, down to
|
|
// RetryAfterCap when it says to wait longer. A shorter one, or none, is
|
|
// left as it is: the cap is never a floor.
|
|
func capRetryAfter(h http.Header, now time.Time) {
|
|
v := h.Get("Retry-After")
|
|
if v == "" {
|
|
return
|
|
}
|
|
most := settings.Shared().RetryAfterCap
|
|
if most == nil || *most <= 0 {
|
|
return
|
|
}
|
|
var wait time.Duration
|
|
if n, err := strconv.ParseFloat(v, 64); err == nil {
|
|
wait = time.Duration(n * float64(time.Second))
|
|
} else if t, err := http.ParseTime(v); err == nil {
|
|
wait = t.Sub(now)
|
|
} else {
|
|
return
|
|
}
|
|
if wait > time.Duration(*most)*time.Second {
|
|
h.Set("Retry-After", strconv.Itoa(*most))
|
|
}
|
|
}
|