gateway: a model failing between its siblings' answers climbs its own rest ladder, not a minute forever (#2241, weizsw)
The backoff count was kept per account (restKey), and any model's answer
cleared it. On an account serving several models, a model that kept
failing while its siblings answered was counted 1 every time and rested
one minute, forever. weizsw's Antigravity seat: 41 of 43 rests at
failures: 1.
restAfterMarked now also counts each model's own failures (restID), and
only that model's answer clears that count (servedCandidate). When a
model's own count is past the account's, a sibling answered since the
model last did, so the failure belongs to the model. It climbs its own
ladder (1, 2, 4, 8, 10 minutes) and rests alone, keyed by restID, while
its siblings stay served. If every model fails with no answer between
them, the account still climbs and rests whole, as before. A window
rest wins as before. A candidate naming no model keeps the account's
count alone.
What was run:
- TestModelLadderClimbsPastSiblingAnswers (new, model_ladder_test.go).
Old code: FAIL ("failure #2 after a sibling's answer: 1 failures,
rests 59.99s; want 2m0s"). With the restID choice taken out by hand:
FAIL ("rests antigravity@seat@example.com, want the model alone").
With the fix: pass.
- The first full run caught TestSmartRouting. Its modelless candidate
("t#fail") was given a "t#fail/" count that its served() call never
cleared. Fixed by counting per model only when the candidate names
one. TestSmartRouting and the rest/ladder tests pass.
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui and GOOS=windows
clean.
- HOME=<scratch> go test -tags nogui -timeout 40m ./...: every package ok
except gateway's TestSmartRouting (above). After the fix, gateway was
rerun whole: ok (468s).
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,90 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// #2241 (weizsw): on an account serving several models, a sibling's answer
|
||||
// cleared the account's failure count, so a model that kept failing
|
||||
// between them rested a minute every time, never longer. Its own count
|
||||
// climbs now, cleared only by its own answer, and it rests alone while
|
||||
// the account's other models go on answering; an account whose models
|
||||
// all fail, with no answer between, still rests whole, as before.
|
||||
func TestModelLadderClimbsPastSiblingAnswers(t *testing.T) {
|
||||
old := allowances
|
||||
defer func() { allowances = old }()
|
||||
allowances = func(string) map[string]provider.Allowance { return nil }
|
||||
s := &Server{}
|
||||
p := provider.Provider{ID: "antigravity", Account: &provider.Account{Agent: "antigravity", User: "seat@example.com"}}
|
||||
sick := candidate{p: p, model: "gemini-3.5-flash-lite", rest: "antigravity#seat"}
|
||||
well := candidate{p: p, model: "gemini-3.8-flash", rest: "antigravity#seat"}
|
||||
t.Cleanup(func() {
|
||||
for _, id := range []string{sick.restKey(), sick.restID(), well.restID()} {
|
||||
clearRest(id)
|
||||
routed.Lock()
|
||||
delete(routed.failures, id)
|
||||
routed.Unlock()
|
||||
}
|
||||
})
|
||||
body := []byte(`{"error":{"message":"the reply ended before it was complete","type":"api_error"}}`)
|
||||
near := func(d, want time.Duration) bool { return d > want-5*time.Second && d <= want }
|
||||
expire := func() {
|
||||
restingUntil.Lock()
|
||||
for _, id := range []string{sick.restKey(), sick.restID()} {
|
||||
if _, ok := restingUntil.m[id]; ok {
|
||||
restingUntil.m[id] = time.Now().Add(-time.Second)
|
||||
}
|
||||
}
|
||||
restingUntil.Unlock()
|
||||
}
|
||||
|
||||
// the first failure says nothing of whose it is: the account rests
|
||||
r := s.restAfter(sick, http.StatusBadGateway, http.Header{}, body)
|
||||
if r.By != "backoff" || r.Failures != 1 || r.Key != sick.restKey() || !near(time.Until(r.Until), time.Minute) {
|
||||
t.Fatalf("first failure: %+v, %v", r, time.Until(r.Until))
|
||||
}
|
||||
// then the sibling answers, and the sick model fails again each time
|
||||
for i, want := range []time.Duration{2 * time.Minute, 4 * time.Minute, 8 * time.Minute, 10 * time.Minute, 10 * time.Minute} {
|
||||
servedCandidate(well, 1)
|
||||
expire()
|
||||
r := s.restAfter(sick, http.StatusBadGateway, http.Header{}, body)
|
||||
if r.Failures != i+2 || !near(time.Until(r.Until), want) {
|
||||
t.Fatalf("failure #%d after a sibling's answer: %d failures, rests %v; want %v", i+2, r.Failures, time.Until(r.Until), want)
|
||||
}
|
||||
if r.Key != sick.restID() || r.Model != sick.model {
|
||||
t.Fatalf("failure #%d rests %q, want the model alone (%q)", i+2, r.Key, sick.restID())
|
||||
}
|
||||
// the account's other models are not set aside by it
|
||||
if resting(well) {
|
||||
t.Fatalf("failure #%d rested the sibling", i+2)
|
||||
}
|
||||
if !resting(sick) {
|
||||
t.Fatalf("failure #%d: the sick model isn't resting", i+2)
|
||||
}
|
||||
// and a sibling's answer doesn't end the model's own rest
|
||||
servedCandidate(well, 1)
|
||||
if !resting(sick) {
|
||||
t.Fatalf("failure #%d: a sibling's answer ended the model's rest", i+2)
|
||||
}
|
||||
}
|
||||
|
||||
// its own answer starts it over
|
||||
servedCandidate(sick, 1)
|
||||
if resting(sick) {
|
||||
t.Fatal("still resting after its own answer")
|
||||
}
|
||||
if r := s.restAfter(sick, http.StatusBadGateway, http.Header{}, body); r.Failures != 1 || r.Key != sick.restKey() || !near(time.Until(r.Until), time.Minute) {
|
||||
t.Fatalf("after its own answer: %+v, %v", r, time.Until(r.Until))
|
||||
}
|
||||
|
||||
// every model failing with no answer between: the account's ladder,
|
||||
// the account resting, as before
|
||||
expire()
|
||||
if r := s.restAfter(well, http.StatusBadGateway, http.Header{}, body); r.Failures != 2 || r.Key != sick.restKey() || !near(time.Until(r.Until), 2*time.Minute) {
|
||||
t.Fatalf("whole account failing: %+v, %v", r, time.Until(r.Until))
|
||||
}
|
||||
}
|
||||
@@ -644,6 +644,8 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
|
||||
// (#1235): the key's other models are still served
|
||||
unserved := why == failOther && (modelRefused(status, body) || codeAssistModelMissing(c.p, status, body))
|
||||
r := Rest{Why: why, Status: status, By: "cooldown"}
|
||||
// a failure counted past the account's, for its model alone (below)
|
||||
modelBackoff := false
|
||||
if why == failProxy {
|
||||
// the account is as good as it was; the proxy is the user's to start
|
||||
return r
|
||||
@@ -723,6 +725,19 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
|
||||
routed.Lock()
|
||||
routed.failures[counted]++
|
||||
n := routed.failures[counted]
|
||||
// the model's own count too, which only its own answer clears: on
|
||||
// an account serving several models, a sibling's answer clears
|
||||
// the account's count, and a model that keeps failing between
|
||||
// them rested a minute forever (#2241, weizsw). Counted past the
|
||||
// account's, a sibling answered since it last did, so the failure
|
||||
// is the model's: it climbs its own ladder and rests alone. A
|
||||
// candidate naming no model has no sibling to tell apart
|
||||
if !unserved && c.model != "" {
|
||||
routed.failures[c.restID()]++
|
||||
if own := routed.failures[c.restID()]; own > n {
|
||||
n, modelBackoff = own, true
|
||||
}
|
||||
}
|
||||
routed.Unlock()
|
||||
d, r.By, r.Failures = min(fallbackCooldown<<min(n-1, 10), c.p.RestCapOr(longestRetry)), "backoff", n
|
||||
// a subscription that failed with a window full is out of it,
|
||||
@@ -732,7 +747,7 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
|
||||
// is the vendor's reading, and one read a year off (a /usage date
|
||||
// with no year) would bench the account until someone noticed
|
||||
if t := c.full(now); !t.IsZero() && c.p.Account != nil && !unserved {
|
||||
d, r.By = min(t.Sub(now), longestQuota), "window"
|
||||
d, r.By, modelBackoff = min(t.Sub(now), longestQuota), "window", false
|
||||
}
|
||||
}
|
||||
// an account is kept by the agent its usage is asked of: a plugin's
|
||||
@@ -760,7 +775,7 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
|
||||
// a refusal that names its reset and points to the account's other
|
||||
// models is the model's limit: the others still answer (WorkBuddy's
|
||||
// "您也可以切换其他模型继续使用", #1669)
|
||||
if pooled || unserved || why == failQuota && resetsWhen.Match(wordsOf(body)) && otherModels.Match(wordsOf(body)) {
|
||||
if pooled || unserved || modelBackoff || why == failQuota && resetsWhen.Match(wordsOf(body)) && otherModels.Match(wordsOf(body)) {
|
||||
id = c.restID()
|
||||
}
|
||||
// out of credit, or of a quota, on a model that costs credits where
|
||||
|
||||
Reference in New Issue
Block a user