zola_xynb on X asked for a cap per model or per provider under one gateway key. A key limit can now name models (access.Limit.Models, "<provider>/<model>" or "<provider>/*"), so one key can have, for example, a month of $20 on openai/* next to a month for the whole key. Limits without models still cover the whole key, as before. - budget: sums are kept per window and scope, and count a ledger row only when its provider/model matches (any of the model's ids). reserve holds a request to a scoped limit only when the model it names is served there. windowsOf keeps every scope's sums, so the ledger is not read again on each request. - gateway: keyLimited puts the key's spent scopes on the caller (withSpent), count_tokens included. A direct request for a spent model gets a 429 with Retry-After and no X-Magpie-Limit-Reset, so a remote magpie rests only that model. A routing group, a provider's fallback and embedding/image/video/decision groups skip spent members (groupKeeps, allowedCandidates) and get a 429 only when none is left. System One, count_tokens and magpie's own calls for the key are held too. Model lists are unchanged. A refusal turned away before routing now records its ErrType in the ledger. - CLI: magpie gateway-key limit <id> add month --cost 20 --model openai/* (--model repeatable, --models a,b). - GUI: the key limit editor has a Models field that opens the app menu (multi-pick: All models, every model of a provider, each model). It has no <select> and no new screen. The badge, line and tabs name the models. Strings are in every language. - docs: reference.md and subsystems/gateway-routing.md. Run at d52725d32 (this change on4ef47b3f4), on mote: - go test ./... -timeout 45m, under the shared lock, with the log kept whole: every package ok except three reds outside this change. None of the three touches what changed: - TestClosedBeforeSentEveryTimeSaysWhy (#5031, passes alone 3/3) - TestMainWindowCorners/macOS_26_SDK (#3949, red alone on macOS 27) - TestMoveToPlugin TempDir cleanup (filed #5524, passes alone 2/2) - go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go build ./...: ok. gofmt -l: nothing. - GUI, chromium + webkit: gateway-key-*.test.cjs, gateway-keys-*, i18n-keys, i18n-en-no-cjk and usage-remote-key-limits passed 244/246. The new gateway-key-model-limit runs in every language at 1100 and 420px. The two reds also fail on origin/main3cbb7e042: - gateway-key-models en at 560px (#4081, #4616, #5233) - i18n-keys "a plugin's words are its own" (#5327, #5332, #5426, #5482) Broken by hand; each one turned the named tests red: - groupKeeps' spent skip removed: ItsModelsAlone and MediaGroups. - modelAllowed's spent check removed: ItsModelsAlone and AFallback. - withSpent on count_tokens removed: ItsModelsAlone. - the scoped refusal sending X-Magpie-Limit-Reset: ItsModelsAlone and SystemOne. - sums counting every model: ItsModelsAlone, TestScopedLimitCountsItsModels and TestScopedBesideTheWholeKey. - embeddings' keyGroupRefusal back to keyModelRefusal: MediaGroups. - the turned-away row's ErrType dropped: ItsModelsAlone. - keyHolds not holding for Spent: ItsModelsAlone, AFallback and MediaGroups. - decide.go's 429 back to a 403 stayed green. keyLimited's reserve refuses a direct decider first, so that line is a second guard. Not tried: - a real vendor account or a real remote magpie. - the Codex backend path and a video group: they go through the same keyLimited/groupKeeps code, but no test of their own. - the whole GUI suite: only the files above. - Windows and Linux test runs: vet only.
63 lines
2.3 KiB
Go
63 lines
2.3 KiB
Go
package access
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A limit of some models (zola_xynb on X) keeps its models cleaned, never
|
|
// a routing group, and sits beside a limit of the whole key in the same
|
|
// window: two limits in one window are the same only for the same models.
|
|
func TestScopedLimitValidAndBeside(t *testing.T) {
|
|
l, err := (&Limit{Period: "month", Cost: 20, Models: []string{" openai/* ", "OpenAI/*", "anthropic/claude-opus"}}).Valid()
|
|
if err != nil || len(l.Models) != 2 || l.Models[0] != "openai/*" {
|
|
t.Fatalf("cleaned as %+v %v", l, err)
|
|
}
|
|
for _, bad := range []string{"group/coding", "Auto/x", "gpt-5"} {
|
|
if _, err := (&Limit{Period: "day", Tokens: 5, Models: []string{bad}}).Valid(); err == nil {
|
|
t.Errorf("%s was taken", bad)
|
|
}
|
|
}
|
|
many := make([]string, MaxLimitModels+1)
|
|
for i := range many {
|
|
many[i] = "p/m" + strings.Repeat("x", i)
|
|
}
|
|
if _, err := (&Limit{Period: "day", Tokens: 5, Models: many}).Valid(); err == nil {
|
|
t.Error("more models than a limit counts were taken")
|
|
}
|
|
if (&Limit{Models: []string{"b/*", "A/x"}}).Scope() != (&Limit{Models: []string{"a/X", "b/*"}}).Scope() {
|
|
t.Error("the same models in another order or case are another scope")
|
|
}
|
|
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
Now = func() time.Time { return time.Date(2026, 11, 6, 8, 0, 0, 0, time.UTC) }
|
|
t.Cleanup(func() { Now = time.Now })
|
|
if _, err := Update("add-key", Change{Name: "Shared"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keys, _ := List()
|
|
id := keys[0].ID
|
|
at := func(n int, l *Limit) error {
|
|
_, err := Update("limit-key", Change{Key: id, Limit: l, Index: &n})
|
|
return err
|
|
}
|
|
if err := at(0, &Limit{Period: "month", Cost: 100}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := at(1, &Limit{Period: "month", Cost: 20, Models: []string{"openai/*"}}); err != nil {
|
|
t.Fatal("a month of OpenAI beside a month of the key:", err)
|
|
}
|
|
if err := at(2, &Limit{Period: "month", Cost: 5, Models: []string{"OpenAI/*"}}); err == nil || !strings.Contains(err.Error(), "openai/*") {
|
|
t.Fatalf("a second month of OpenAI: %v", err)
|
|
}
|
|
if err := at(2, &Limit{Period: "month", Cost: 5}); err == nil {
|
|
t.Fatal("a second month of the whole key was taken")
|
|
}
|
|
keys, _ = List()
|
|
ls := keys[0].Limits()
|
|
if len(ls) != 2 || ls[0].Scoped() || !ls[1].Scoped() || ls[1].Models[0] != "openai/*" {
|
|
t.Fatalf("kept as %+v", ls)
|
|
}
|
|
}
|