gateway: a full window of some models only rests that model (plugins#817)
A subscription account that fails with a window full rests until that
window renews (restAfter's "window" branch). The rest was always on the
account (restKey), whatever the window counted. Now, when every full
window that counts the model counts some models only (Allowance.Pooled,
the same reading a quota refusal already narrows by), the model rests
(restID), and its siblings in their own windows still answer. A full
window of the whole account rests the account as before.
zane668's ZCode Start Plan gift account: GLM-5.3's daily bucket was
spent, and the plugin passed its 200 "exceed quota limit" on as it came.
The gateway read that as a 502 that did not stream (failOther), the
window branch took GLM-5.3's full bucket, and the account rested ~22h.
GLM-5.3-Flash, with its own bucket 71% left and a weekend bucket unused,
was skipped by every group. zcode 0.1.53 now says that answer as a 429
(failQuota, which pooled already narrowed). This change is for any
plugin or built-in whose failure isn't worded as quota while a
per-model window is full.
Checked the reporter's reading before changing anything: spentUp runs
only on the forcedGift and freeGift paths, and the plain path returns
done(kept(res)). failure(502, "... did not stream: exceed quota limit")
is failOther. pooled is computed for failQuota only, and the window
branch set modelBackoff false and kept restKey.
TestScopedWindowFullRestsTheModelOnly builds the allowance through
PluginAllowanceForTest from the plugin's startUsage windows as the
report read them (GLM-5.3 100%, Flash 29%, the weekend bucket 0%):
- the 502 that did not stream rests GLM-5.3 alone till its reset, and
Flash not;
- the plugin's new 429 1005 does too;
- with a whole-account plan window full as well, or alone, the account
rests and Flash with it.
Not changed: the Codex out-of-credit branch's window. Its credits are
the account's.
Run at 93c258ef9 on mote: go vet ./internal/gateway ok; go test -run
'Rest|Window|Pool|Quota|Spent|Narrow|Renew|ZCode|Zcode|Backoff|Fail'
./internal/gateway ok (70.8s). Broken by hand: windowModel left out of
the restID condition, and the 502 subtest fails ("rests
zcode@me@zcode (model ""), want the model alone"). The 429 subtest
passes either way (pooled already covered it), as a guard.
Not tried: a live ZCode account with a spent bucket. The full suite
runs after the push.
Reported-by: zane668
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1457,6 +1457,9 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
|
||||
guessed := false
|
||||
// a failure counted past the account's, for its model alone (below)
|
||||
modelBackoff := false
|
||||
// a failure rested by a full window that counts some models only
|
||||
// (below): that model is out, not the account
|
||||
windowModel := false
|
||||
if why == failProxy {
|
||||
// the account is as good as it was; the proxy is the user's to start
|
||||
return r
|
||||
@@ -1665,6 +1668,15 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
|
||||
// until its window reset (plugins#243, zygs1083-dotcom)
|
||||
if t := c.full(now); !t.IsZero() && c.p.Account != nil && !unserved && status != http.StatusUnauthorized && status != http.StatusForbidden {
|
||||
d, r.By, modelBackoff = min(t.Sub(now), longestQuota), "window", false
|
||||
// and when every full window that counts the model counts
|
||||
// some models only (Pooled), the model rests, not the
|
||||
// account: its other models are still in theirs
|
||||
// (plugins#817, zane668: ZCode's GLM-5.3 bucket spent, its
|
||||
// "exceed quota limit" read as a reply that did not stream,
|
||||
// rested the account a day, GLM-5.3-Flash's own bucket with
|
||||
// 71% left with it). A window of the whole account full
|
||||
// rests the account, as before
|
||||
windowModel = c.pooled(now)
|
||||
}
|
||||
}
|
||||
// an account is kept by the agent its usage is asked of: a plugin's
|
||||
@@ -1697,7 +1709,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 || modelBackoff || overloaded || why == failQuota && resetsWhen.Match(wordsOf(body)) && otherModels.Match(wordsOf(body)) {
|
||||
if pooled || unserved || modelBackoff || windowModel || overloaded || why == failQuota && resetsWhen.Match(wordsOf(body)) && otherModels.Match(wordsOf(body)) {
|
||||
id = c.restID()
|
||||
}
|
||||
// Cline's daily limit of one free model is that model's: its others,
|
||||
|
||||
@@ -2,6 +2,8 @@ package gateway
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -40,3 +42,82 @@ func TestRoutingHoldsAWindowRestToLongestQuota(t *testing.T) {
|
||||
t.Fatalf("window renewing in 3h: rests %v by %s (%s)", time.Until(r.Until), r.By, r.Why)
|
||||
}
|
||||
}
|
||||
|
||||
// A failure rested by a full window rests the model when every full
|
||||
// window that counts it counts some models only: the account's other
|
||||
// models are still in theirs (plugins#817, zane668). ZCode's Start Plan
|
||||
// gift gives GLM-5.3 and GLM-5.3-Flash a bucket each; GLM-5.3's spent,
|
||||
// its 200 "exceed quota limit" reached magpie as a 502 that did not
|
||||
// stream (failOther) and rested the whole account until the bucket
|
||||
// renewed, Flash's own bucket with 71% left with it. A window of the
|
||||
// whole account full still rests the account.
|
||||
func TestScopedWindowFullRestsTheModelOnly(t *testing.T) {
|
||||
now := time.Now()
|
||||
reset := now.Add(22 * time.Hour).UTC().Format(time.RFC3339)
|
||||
weekend := now.Add(3 * 24 * time.Hour).UTC().Format(time.RFC3339)
|
||||
// the zcode plugin's startUsage as the report's `magpie quota zcode`
|
||||
// read it: each bucket lists its model and the model's -Trial entry
|
||||
gift := `{"name":"GLM-5.3","used":100,"amount":3000000,"limit":3000000,"span":86400,"resetsAt":"` + reset + `","models":["glm-5.3","GLM-5.3-Trial"]},` +
|
||||
`{"name":"GLM-5.3-Flash","used":29,"amount":1459132,"limit":5000000,"span":86400,"resetsAt":"` + reset + `","models":["glm-5.3-flash","GLM-5.3-Flash-Trial"]},` +
|
||||
`{"name":"GLM-5.3-Flash · ZCODE WEEKEND BUILD","used":0,"span":259200,"resetsAt":"` + weekend + `","models":["glm-5.3-flash","GLM-5.3-Flash-Trial"]}`
|
||||
// a coding plan's own window counts every model
|
||||
plan := func(used int) string {
|
||||
return `{"name":"5 hours","used":` + strconv.Itoa(used) + `,"span":18000,"resetsAt":"` + reset + `"}`
|
||||
}
|
||||
usage := func(ws ...string) provider.Allowance {
|
||||
return provider.PluginAllowanceForTest([]byte(`{"windows":[`+strings.Join(ws, ",")+`]}`), now)
|
||||
}
|
||||
stalled := []byte(`{"type":"error","error":{"type":"api_error","message":"ZCode did not stream: exceed quota limit"}}`)
|
||||
spent := []byte(`{"code":1005,"msg":"exceed quota limit"}`)
|
||||
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
al provider.Allowance
|
||||
status int
|
||||
body []byte
|
||||
why string
|
||||
model bool // the model rests, not the account
|
||||
}{
|
||||
{"gift bucket full, did not stream", usage(gift), 502, stalled, failOther, true},
|
||||
{"gift bucket full, said 429", usage(gift), 429, spent, failQuota, true},
|
||||
{"gift bucket and the plan full", usage(plan(100), gift), 502, stalled, failOther, false},
|
||||
{"the plan full alone", usage(plan(100)), 502, stalled, failOther, false},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
old := allowances
|
||||
defer func() { allowances = old }()
|
||||
fresh(t)
|
||||
allowances = func(string) map[string]provider.Allowance {
|
||||
return map[string]provider.Allowance{"me@zcode": c.al}
|
||||
}
|
||||
acct := func(model string) candidate {
|
||||
return candidate{p: provider.Provider{ID: "zcode", Account: &provider.Account{Agent: "plugin:zcode", User: "me@zcode"}}, model: model, rest: "zcode"}
|
||||
}
|
||||
glm, flash := acct("GLM-5.3"), acct("GLM-5.3-Flash")
|
||||
s := &Server{}
|
||||
r := s.restAfter(glm, c.status, http.Header{}, c.body)
|
||||
if r.Why != c.why {
|
||||
t.Fatalf("GLM-5.3 failed %s, want %s", r.Why, c.why)
|
||||
}
|
||||
if d := time.Until(r.Until); d < 21*time.Hour || d > 22*time.Hour {
|
||||
t.Fatalf("GLM-5.3 rests %v by %s, want till its window renews", d, r.By)
|
||||
}
|
||||
_, flashRests := restingOf(flash)
|
||||
if c.model {
|
||||
if r.Key != glm.restID() || r.Model != glm.model {
|
||||
t.Fatalf("GLM-5.3's own bucket full rests %s (model %q), want the model alone", r.Key, r.Model)
|
||||
}
|
||||
if flashRests {
|
||||
t.Fatal("GLM-5.3's bucket full rests GLM-5.3-Flash, which has its own")
|
||||
}
|
||||
return
|
||||
}
|
||||
if r.Key != glm.restKey() || r.Model != "" {
|
||||
t.Fatalf("the account's window full rests %s (model %q), want the account", r.Key, r.Model)
|
||||
}
|
||||
if !flashRests {
|
||||
t.Fatal("the account's window full leaves GLM-5.3-Flash in")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user