Files
magpie/internal/provider/allowance_pooled_test.go
T
yetone 99009b9333 provider: a plugin's model pool counts a bare id with its @sizes, so a routing group's cursor/grok-4.7 is weighed on Cursor Models, not a used-up Other Models (Will on Discord)
Will's routing group keeps cursor/grok-4.7. Cursor's plugin (0.2.2/0.2.3)
lists Grok 4.7 only at its context sizes, grok-4.7@256k and
grok-4.7@500k, and its usage() builds the Cursor Models window's models
and Other Models' notModels from those ids. quotaOfPlugin matched a model
against those lists by the whole string, so bare grok-4.7 (which the
plugin still runs, at its default size) fell into Other Models: used up,
so the Routing page showed it 0% left and weighRouted ranked it last,
while composer-2.5 showed ~84%.

The window's lists are now an idSet: a bare id is in it when one of its
id@size is, and an id@size when its bare id is. Only the trailing @size
is taken off, so distinct models stay apart: another @size of a model,
another family member (grok-4), and other suffixes (ZCode's -Trial) still
match by the whole id. Every route that asks a plugin window whether it
counts a model goes through w.matches -> Limit.applies (routing's For,
Pace, Count, Renewal, fallback's Pooled, CapHeld, QuotaWindow.Counts for
the usage page's holds), so the one change reaches them all. host.js's
contract comment says so for plugin authors.

The plugin's own lists are also fixed in magpie-community/plugins (cursor
0.2.4 adds each listed id's bare name to its pool), for magpie versions
before this one.

Verified:
- TestPluginPoolCountsBareIDOfSizes (the reporter's literal ids) fails on
  the old code: "grok-4.7: used 100, want 16", "the usage page's holds
  count grok-4.7 in Other Models", "grok-4.8@1m: used 100, want 16";
  passes with the change.
- gofmt -l clean; go vet, GOOS=linux and GOOS=windows go vet -tags nogui
  on internal/provider and internal/plugin; go build for darwin, linux,
  windows.
- go test -tags nogui ./... green but TestNoCommandBypassesProc, red on
  main since 2da8fbe4 and fixed by b934d64a, which this is rebased on.
Not verified: against Will's real Cursor account.
2026-10-10 10:54:22 +08:00

100 lines
3.8 KiB
Go

package provider
import (
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
)
// A refusal for a model counted by a pool of some models only is that
// model's, not the account's: Cursor's Other Models used up leaves Auto in
// Cursor Models (Xiaopodev on X), Opus's own week leaves Sonnet; one of
// the whole account's windows used up is the account's.
func TestAllowancePooled(t *testing.T) {
now := time.Now()
month := now.Add(20 * 24 * time.Hour)
pool := map[string]bool{"auto": true, "composer-2": true}
q := quotaOfPlugin(SubscriptionQuota{}, plugin.Usage{Windows: []plugin.UsageWindow{
{Name: "Cursor Models", Used: 12, Models: []string{"auto", "composer-2"}},
{Name: "Other Models", Used: 100, NotModels: []string{"auto", "composer-2"}},
{Name: "Total", Used: 60, Aside: true},
}})
cursor := allowanceOf(q.Windows, now)
for _, m := range []string{"auto", "composer-2", "claude-4.6-sonnet", "gpt-5.5"} {
if !cursor.Pooled(m, 100, now) {
t.Errorf("Cursor: %s refused isn't the model's alone", m)
}
if used, _ := cursor.For(m, now); (used == 100) == pool[m] {
t.Errorf("Cursor: %s used %v", m, used)
}
}
claude := Allowance{
{Used: 100, Resets: month, Span: 7 * 24 * time.Hour, Model: "opus"},
{Used: 40, Resets: month, Span: 7 * 24 * time.Hour},
}
if !claude.Pooled("claude-opus-4-7", 100, now) {
t.Error("Opus's own week used up rests the account")
}
if claude.Pooled("claude-sonnet-4-6", 100, now) {
t.Error("Sonnet, refused with no pool of its full, counts as pooled")
}
claude[1].Used = 100
if claude.Pooled("claude-opus-4-7", 100, now) {
t.Error("the account's week used up rests only Opus")
}
if (Allowance{}).Pooled("auto", 100, now) {
t.Error("an allowance not known counts as pooled")
}
}
// Will on Discord: Cursor's plugin lists Grok 4.7 at its context sizes
// alone (grok-4.7@256k, grok-4.7@500k), and a routing group still asks for
// bare cursor/grok-4.7, which the plugin runs at its default size. That is
// the Cursor Models pool's (84% left), not Other Models' (used up); the
// Routing page showed it 0% left and routing ranked it last. The lists are
// the ones cursor 0.2.3's usage() returns for such an account.
func TestPluginPoolCountsBareIDOfSizes(t *testing.T) {
now := time.Now()
pool := []string{"auto", "grok-4.7@256k", "grok-4.7@500k", "composer-2.5", "default"}
q := quotaOfPlugin(SubscriptionQuota{}, plugin.Usage{Windows: []plugin.UsageWindow{
{Name: "Cursor Models", Used: 16, Models: pool},
{Name: "Other Models", Used: 100, NotModels: pool},
{Name: "Total", Used: 60, Aside: true},
}})
a := allowanceOf(q.Windows, now)
for m, want := range map[string]float64{
"grok-4.7": 16, // the reporter's: bare, its sizes listed
"Grok-4.7": 16,
"grok-4.7@256k": 16,
"composer-2.5": 16,
// distinct models stay in Other Models: another family member, a
// suffix that isn't a size, a size of a model the pool doesn't list
"grok-4": 100,
"grok-4.7-trial": 100,
"claude-opus-5-5": 100,
"claude-opus-5-5@300k": 100,
"composer-2.5-fast@1m": 100,
"@256k": 100,
} {
if used, _ := a.For(m, now); used != want {
t.Errorf("%s: used %v, want %v", m, used, want)
}
}
if !q.Windows[0].Counts("grok-4.7") || q.Windows[1].Counts("grok-4.7") {
t.Error("the usage page's holds count grok-4.7 in Other Models")
}
// the other way: the pool lists a bare id (Cursor's autoBucketModels),
// and the model is asked for at a size
q = quotaOfPlugin(SubscriptionQuota{}, plugin.Usage{Windows: []plugin.UsageWindow{
{Name: "Cursor Models", Used: 16, Models: []string{"auto", "grok-4.8"}},
{Name: "Other Models", Used: 100, NotModels: []string{"auto", "grok-4.8"}},
}})
a = allowanceOf(q.Windows, now)
if used, _ := a.For("grok-4.8@1m", now); used != 16 {
t.Errorf("grok-4.8@1m: used %v, want 16 (Cursor Models)", used)
}
}