provider: a Cursor Team member with no limit of their own spends the team's on-demand pool, shown and counted as the limit, not as unlimited (#1868)
PaSSw0rds' Team account (#1476) has on-demand turned on for the whole organization: spendLimitUsage has no individualLimit, pooledLimit "45120000" and pooledRemaining "38785730" are int64 strings, pooledUsed 6334270 a number, and GetHardLimit answers {"hardLimit": 451200} with or without a team id. cursorOnDemandOf read that as unlimited, as cursor-agent's own usage view does, so On-demand showed only the member's $198.55 at 0%, aside, and a spent team pool never counted the account used up; magpie would keep routing to it until Cursor refused. Now a team member with no individualLimit and a pooledLimit > 0 gets a fixed On-demand window on the pool: used = pooledUsed / pooledLimit, display "$63,342.70 / $451,200.00 (you: $198.55)", not aside. A hard limit that forbids on-demand still reads as disabled, and a team with no pool and a hard limit still as unlimited. Dollar figures group thousands (cursorDollars), as plugin 0.2.10 does. This is the built-in, for users who haven't moved to the plugin; moved users get the same from @magpie-community/opencode-cursor-auth 0.2.10 (next commit raises the mover's min). Run, under a sandbox HOME with pinned caches: go test -tags nogui ./internal/provider/ (ok); gofmt -l; go vet ./... on darwin, GOOS=linux -tags nogui and GOOS=windows (clean). Broken by hand: with the old cursor_usage.go, TestCursorOnDemandKeepsTheAccount/a_team's_org-wide_on-demand_pool_is_the_allowance fails (On-demand "$198.55", used 0, aside; the spent pool "used up false"); with a fix that puts the member's spend against the pool, it fails too. Built from the payloads quoted in #1476. Not tried against a real Team account.
This commit is contained in:
@@ -17,8 +17,8 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -116,6 +116,7 @@ type cursorSpend struct {
|
||||
IndividualLimit *json.Number `json:"individualLimit"`
|
||||
LimitType string `json:"limitType"`
|
||||
PooledLimit json.Number `json:"pooledLimit"` // an int64 string
|
||||
PooledUsed json.Number `json:"pooledUsed"`
|
||||
}
|
||||
|
||||
// cursorHardLimit is GetHardLimit's reply: the on-demand limit, in dollars.
|
||||
@@ -126,16 +127,21 @@ type cursorHardLimit struct {
|
||||
|
||||
// cursorOnDemand is what Cursor still serves once the included usage is
|
||||
// spent: kind is "fixed" (up to limit dollars), "unlimited", "disabled" or
|
||||
// "unavailable" (not known).
|
||||
// "unavailable" (not known). mine is the member's own spend when used is
|
||||
// their team's pool's (pool is set).
|
||||
type cursorOnDemand struct {
|
||||
kind string
|
||||
used, limit float64 // dollars
|
||||
kind string
|
||||
used, limit, mine float64 // dollars
|
||||
pool bool
|
||||
}
|
||||
|
||||
// cursorOnDemandOf derives the On-demand state as cursor-agent 2026.10.01's
|
||||
// usage view does (usage-data.ts), and as the plugin's onDemandOf: from the
|
||||
// period's spendLimitUsage and, where that sets no limit of the user's,
|
||||
// GetHardLimit's reply (nil when it couldn't be read).
|
||||
// GetHardLimit's reply (nil when it couldn't be read). A team member with
|
||||
// no limit of their own spends the team's pool, which cursor-agent's view
|
||||
// calls unlimited but which runs out at pooledLimit for the whole team
|
||||
// (#1868): that is a fixed limit on the pool's spend.
|
||||
func cursorOnDemandOf(p cursorPeriod, hard *cursorHardLimit) cursorOnDemand {
|
||||
num := func(n json.Number) float64 { f, _ := n.Float64(); return f }
|
||||
s := p.SpendLimitUsage
|
||||
@@ -146,9 +152,9 @@ func cursorOnDemandOf(p cursorPeriod, hard *cursorHardLimit) cursorOnDemand {
|
||||
var own *cursorOnDemand
|
||||
if s.IndividualLimit != nil {
|
||||
if l := num(*s.IndividualLimit); l > 0 {
|
||||
own = &cursorOnDemand{"fixed", used, l / 100}
|
||||
own = &cursorOnDemand{kind: "fixed", used: used, limit: l / 100}
|
||||
} else {
|
||||
own = &cursorOnDemand{"disabled", used, 0}
|
||||
own = &cursorOnDemand{kind: "disabled", used: used}
|
||||
}
|
||||
}
|
||||
if s.LimitType == "team" {
|
||||
@@ -156,25 +162,27 @@ func cursorOnDemandOf(p cursorPeriod, hard *cursorHardLimit) cursorOnDemand {
|
||||
case own != nil:
|
||||
return *own
|
||||
case hard != nil && (hard.NoUsageBasedAllowed || hard.HardLimit <= 0):
|
||||
return cursorOnDemand{"disabled", used, 0}
|
||||
case hard != nil, num(s.PooledLimit) > 0:
|
||||
return cursorOnDemand{"unlimited", used, 0}
|
||||
return cursorOnDemand{kind: "disabled", used: used}
|
||||
case num(s.PooledLimit) > 0:
|
||||
return cursorOnDemand{kind: "fixed", used: num(s.PooledUsed) / 100, limit: num(s.PooledLimit) / 100, mine: used, pool: true}
|
||||
case hard != nil:
|
||||
return cursorOnDemand{kind: "unlimited", used: used}
|
||||
}
|
||||
return cursorOnDemand{"unavailable", used, 0}
|
||||
return cursorOnDemand{kind: "unavailable", used: used}
|
||||
}
|
||||
switch {
|
||||
case hard == nil && own != nil:
|
||||
return *own
|
||||
case hard == nil:
|
||||
return cursorOnDemand{"unavailable", used, 0}
|
||||
return cursorOnDemand{kind: "unavailable", used: used}
|
||||
case hard.NoUsageBasedAllowed:
|
||||
return cursorOnDemand{"disabled", used, 0}
|
||||
return cursorOnDemand{kind: "disabled", used: used}
|
||||
case hard.HardLimit >= 2147483647:
|
||||
return cursorOnDemand{"unlimited", used, 0}
|
||||
return cursorOnDemand{kind: "unlimited", used: used}
|
||||
case hard.HardLimit > 0:
|
||||
return cursorOnDemand{"fixed", used, hard.HardLimit}
|
||||
return cursorOnDemand{kind: "fixed", used: used, limit: hard.HardLimit}
|
||||
}
|
||||
return cursorOnDemand{"disabled", used, 0}
|
||||
return cursorOnDemand{kind: "disabled", used: used}
|
||||
}
|
||||
|
||||
// cursorDashboard posts {} to one of Cursor's DashboardService methods and
|
||||
@@ -248,13 +256,31 @@ func cursorWindows(ctx context.Context, token string) ([]QuotaWindow, error) {
|
||||
}
|
||||
switch od.kind {
|
||||
case "fixed":
|
||||
ws = append(ws, QuotaWindow{Name: "On-demand", Used: max(0, min(100, 100*od.used/od.limit)), Display: fmt.Sprintf("$%.2f / $%.2f", od.used, od.limit), ResetsAt: resets})
|
||||
display := cursorDollars(od.used) + " / " + cursorDollars(od.limit)
|
||||
if od.pool {
|
||||
display += " (you: " + cursorDollars(od.mine) + ")"
|
||||
}
|
||||
ws = append(ws, QuotaWindow{Name: "On-demand", Used: max(0, min(100, 100*od.used/od.limit)), Display: display, ResetsAt: resets})
|
||||
case "unlimited":
|
||||
ws = append(ws, QuotaWindow{Name: "On-demand", Display: fmt.Sprintf("$%.2f", od.used), ResetsAt: resets, Aside: true})
|
||||
ws = append(ws, QuotaWindow{Name: "On-demand", Display: cursorDollars(od.used), ResetsAt: resets, Aside: true})
|
||||
}
|
||||
return append(ws, QuotaWindow{Name: "Total", Used: u.Total, ResetsAt: resets, Aside: true}), nil
|
||||
}
|
||||
|
||||
// cursorDollars says a dollar amount to the cent, thousands grouped, as the
|
||||
// plugin does: a team's pool runs to six figures ($451,200.00).
|
||||
func cursorDollars(d float64) string {
|
||||
s := strconv.FormatFloat(math.Abs(d), 'f', 2, 64)
|
||||
whole, cents, _ := strings.Cut(s, ".")
|
||||
for i := len(whole) - 3; i > 0; i -= 3 {
|
||||
whole = whole[:i] + "," + whole[i:]
|
||||
}
|
||||
if d < 0 && s != "0.00" {
|
||||
return "-$" + whole + "." + cents
|
||||
}
|
||||
return "$" + whole + "." + cents
|
||||
}
|
||||
|
||||
// cursorPoolBase names a model's family: lower case, without cursor- and
|
||||
// the effort and speed the CLI adds to it.
|
||||
func cursorPoolBase(model string) string {
|
||||
|
||||
@@ -178,26 +178,44 @@ func TestCursorOnDemandKeepsTheAccount(t *testing.T) {
|
||||
t.Fatalf("on-demand resets %v", ws[2].ResetsAt)
|
||||
}
|
||||
})
|
||||
t.Run("a team with a hard limit and none of the member's never runs out", func(t *testing.T) {
|
||||
for i, c := range []struct {
|
||||
spend map[string]any
|
||||
hard any
|
||||
}{
|
||||
{map[string]any{"individualUsed": 900, "limitType": "team"}, map[string]any{"hardLimit": 2000, "perUserMonthlyLimitDollars": 0}},
|
||||
{map[string]any{"individualUsed": 900, "limitType": "team", "pooledLimit": "100000"}, map[string]any{"hardLimit": 2000}},
|
||||
// GetHardLimit unread: the team's pooled limit says it is allowed
|
||||
{map[string]any{"individualUsed": 900, "limitType": "team", "pooledLimit": "100000"}, 500},
|
||||
} {
|
||||
ws := run(t, c.spend, c.hard)
|
||||
for _, w := range ws {
|
||||
if !w.Aside {
|
||||
t.Errorf("%d: %s not aside: %+v", i, w.Name, shape(ws))
|
||||
}
|
||||
t.Run("a team with a hard limit, no pool and none of the member's never runs out", func(t *testing.T) {
|
||||
ws := run(t, map[string]any{"individualUsed": 900, "limitType": "team"}, map[string]any{"hardLimit": 2000, "perUserMonthlyLimitDollars": 0})
|
||||
for _, w := range ws {
|
||||
if !w.Aside {
|
||||
t.Errorf("%s not aside: %+v", w.Name, shape(ws))
|
||||
}
|
||||
if len(ws) != 4 || ws[2].Name != "On-demand" || ws[2].Display != "$9.00" || usedUp(ws) {
|
||||
t.Errorf("%d: windows = %+v", i, shape(ws))
|
||||
}
|
||||
if len(ws) != 4 || ws[2].Name != "On-demand" || ws[2].Display != "$9.00" || usedUp(ws) {
|
||||
t.Errorf("windows = %+v", shape(ws))
|
||||
}
|
||||
})
|
||||
// #1868 (PaSSw0rds, a real Team account whose on-demand is turned on for
|
||||
// the whole organization, in #1476): spendLimitUsage has no
|
||||
// individualLimit at all, the pool's limit and remainder are int64
|
||||
// strings and its spend a number, and GetHardLimit answers the pool's
|
||||
// limit in dollars with or without a team id. The pool is what runs
|
||||
// out, for the whole team.
|
||||
t.Run("a team's org-wide on-demand pool is the allowance", func(t *testing.T) {
|
||||
pool := func(used int, remaining string) map[string]any {
|
||||
return map[string]any{"totalSpend": 6334270, "pooledLimit": "45120000", "pooledUsed": used, "pooledRemaining": remaining, "individualUsed": 19855, "limitType": "team"}
|
||||
}
|
||||
for i, hard := range []any{map[string]any{"hardLimit": 451200}, map[string]any{"hardLimit": 451200, "isDynamicTeamLimit": true}, 500} {
|
||||
ws := run(t, pool(6334270, "38785730"), hard)
|
||||
want := []win{{"Cursor Models", 100, "", true}, {"Other Models", 100, "", true}, {"On-demand", 100 * 6334270.0 / 45120000, "$63,342.70 / $451,200.00 (you: $198.55)", false}, {"Total", 100, "", true}}
|
||||
if got := shape(ws); fmt.Sprint(got) != fmt.Sprint(want) || usedUp(ws) {
|
||||
t.Errorf("%d: windows = %+v, used up %v\nwant %+v", i, got, usedUp(ws), want)
|
||||
}
|
||||
}
|
||||
// the pool spent: the account is used up, though the member spent little of it
|
||||
ws := run(t, pool(45120000, "0"), map[string]any{"hardLimit": 451200})
|
||||
if got := shape(ws)[2]; got != (win{"On-demand", 100, "$451,200.00 / $451,200.00 (you: $198.55)", false}) || !usedUp(ws) {
|
||||
t.Errorf("spent: %+v, used up %v", shape(ws), usedUp(ws))
|
||||
}
|
||||
// nothing spent from the pool yet: Connect JSON leaves pooledUsed out
|
||||
ws = run(t, map[string]any{"individualUsed": 900, "limitType": "team", "pooledLimit": "100000"}, 500)
|
||||
if got := shape(ws)[2]; got != (win{"On-demand", 0, "$0.00 / $1,000.00 (you: $9.00)", false}) || usedUp(ws) {
|
||||
t.Errorf("none spent: %+v, used up %v", shape(ws), usedUp(ws))
|
||||
}
|
||||
})
|
||||
t.Run("a personal hard limit is the on-demand limit; the top int32 is none", func(t *testing.T) {
|
||||
ws := run(t, map[string]any{"individualUsed": 500, "limitType": "user"}, map[string]any{"hardLimit": 20})
|
||||
|
||||
Reference in New Issue
Block a user