cursor 0.2.30: an Enterprise account's overall allowance from usage-summary (#391)
Fixes yetone/magpie#3288. An Enterprise account's GetCurrentPeriodUsage has no planUsage, so its card was empty. usage() now reads the website's /api/usage-summary with the same dashboard session the Grok Bot read builds, and shows an Enterprise window (amount/limit in USD, reset at the cycle's end) when the summary is Enterprise with overall enabled and used/limit real numbers. It is aside while team on-demand is unlimited or a measured budget has room, and a measured on-demand budget gets its own window. Anything else leaves the card empty, as before. Version 0.2.30. The reviewer pushed 9435f59 on top of e2f03a0: main merged in (the only conflict was the version line; main took 0.2.27 to 0.2.29 meanwhile), version 0.2.30. Run at 9435f59f0f0e4902e37f8634eebe8e41127cc9b1, on mote (mote-run, HOME=$(mktemp -d)): - bun test packages/cursor: 328 pass, 0 fail (19 files). bun scripts/check.mjs cursor: ok. - Full bun test: 3063 pass, 8 skip, 0 fail (257 files). - Broken by hand on usage.test.mjs, each red then restored: the old Number() reads of overall.used/limit (the can't-be-read test; null/""/false/[] read as 0 again), no room check on the on-demand budget (the 50000 and 60000 of 50000 cases), the summary read off (10 tests). - npm pack 0.2.30 and imported the packed index.mjs (CursorAuthPlugin, _internal). npm has 0.2.29. Not tried: a real Enterprise account (none here); the summary's shape rests on the author's account. Still unanswered from the review: whether an Enterprise seat's summary carries a personal on-demand cap (individualUsage.onDemand); a ms-string billingCycleEnd gives no resetsAt. #372 touches the same `if (!u)` branch and now has to rebase onto this.
This commit is contained in:
@@ -254,6 +254,17 @@ own spends the team's pool: On-demand shows the pool's spend and limit,
|
||||
with the member's own spend beside it, and the account is used up when
|
||||
the pool is.
|
||||
|
||||
An Enterprise account's period has no Cursor Models, Other Models or Total
|
||||
in `GetCurrentPeriodUsage`: Cursor reports its overall allowance through
|
||||
the website's `usage-summary` instead. Magpie shows that as Enterprise,
|
||||
for example `49.45 / 800 USD · 6.2% used`, or
|
||||
`750.55 / 800 USD · 93.8% left` with Magpie's Used / Left choice, reset at
|
||||
the billing cycle's end. When the summary says team on-demand is enabled
|
||||
and unlimited or still has room, the Enterprise window is informational:
|
||||
reaching its included allowance doesn't count the account as used up.
|
||||
On-demand also shows the team's spend when known, with its limit when it
|
||||
has one. A reached on-demand limit counts the account as used up.
|
||||
|
||||
Sign in to the Cursor account linked in Grok Bot. A linked SuperGrok
|
||||
subscription grants Bot access without a paid Cursor plan; see
|
||||
[Grok Bot plans and billing](https://cursor.com/help/grok-bot/plans).
|
||||
|
||||
@@ -532,14 +532,79 @@ async function hardLimit(tok) {
|
||||
// dollars, thousands grouped: a team's pool runs to six figures ($451,200.00)
|
||||
const dollars = (n) => "$" + n.toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 })
|
||||
|
||||
// usage is the account's windows this billing period; an enterprise plan
|
||||
// reports spend instead, and gets none. ids are the models it can be
|
||||
// asked for, each counted by the pool Cursor bills it to. An account that
|
||||
// may spend on-demand once its included usage is gone (fottencity on
|
||||
// enterpriseUsage is the allowance GetCurrentPeriodUsage leaves out on an
|
||||
// Enterprise account. That call answers with only the billing cycle there;
|
||||
// the website's usage-summary has individualUsage.overall, in cents, and
|
||||
// the account's membership. The CLI JWT is also the website's dashboard
|
||||
// session, as botUsage builds it. undefined when the summary says nothing
|
||||
// this can show, so a failed read stays an empty card, as before.
|
||||
async function enterpriseUsage(tok) {
|
||||
try {
|
||||
const subject = JSON.parse(Buffer.from(tok.split(".")[1], "base64url").toString()).sub
|
||||
if (typeof subject !== "string") return undefined
|
||||
const user = subject.split("|").filter(Boolean).at(-1)
|
||||
if (!user || !/^[a-zA-Z0-9._-]+$/.test(user)) return undefined
|
||||
const res = await fetch(WEBSITE + "/api/usage-summary", {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Origin: WEBSITE,
|
||||
Cookie: "WorkosCursorSessionToken=" + encodeURIComponent(user + "::" + tok),
|
||||
},
|
||||
redirect: "error",
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
})
|
||||
if (!res.ok) return undefined
|
||||
const data = await res.json()
|
||||
if (String(data?.membershipType).toLowerCase() !== "enterprise") return undefined
|
||||
const overall = data?.individualUsage?.overall
|
||||
if (overall?.enabled !== true) return undefined
|
||||
const { used, limit } = overall
|
||||
if (typeof used !== "number" || !Number.isFinite(used) || used < 0) return undefined
|
||||
if (typeof limit !== "number" || !Number.isFinite(limit) || limit <= 0) return undefined
|
||||
const end = Date.parse(data.billingCycleEnd ?? "")
|
||||
const resets = Number.isFinite(end) ? { resetsAt: new Date(end).toISOString() } : {}
|
||||
const od = data.teamUsage?.onDemand
|
||||
const spent = typeof od?.used === "number" && Number.isFinite(od.used) && od.used >= 0
|
||||
const unlimited = od?.enabled === true && od.limit === null
|
||||
const fixed = od?.enabled === true && typeof od.limit === "number" && Number.isFinite(od.limit) && od.limit > 0 && spent
|
||||
// Only an explicitly unlimited budget or a measured budget with room
|
||||
// lets a spent included allowance stay available to magpie's routing.
|
||||
const aside = unlimited || (fixed && od.used < od.limit)
|
||||
const windows = [{
|
||||
name: "Enterprise",
|
||||
used: Math.max(0, Math.min(100, (100 * used) / limit)),
|
||||
display: `${dollars(used / 100)} / ${dollars(limit / 100)}`,
|
||||
amount: used / 100,
|
||||
limit: limit / 100,
|
||||
unit: "USD",
|
||||
...resets,
|
||||
...(aside ? { aside: true } : {}),
|
||||
}]
|
||||
if (fixed) windows.push({
|
||||
name: "On-demand",
|
||||
used: Math.min(100, (100 * od.used) / od.limit),
|
||||
display: `${dollars(od.used / 100)} / ${dollars(od.limit / 100)}`,
|
||||
amount: od.used / 100,
|
||||
limit: od.limit / 100,
|
||||
unit: "USD",
|
||||
...resets,
|
||||
})
|
||||
else if (unlimited && spent) windows.push({ name: "On-demand", used: 0, display: dollars(od.used / 100), ...resets, aside: true })
|
||||
return windows
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
// usage is the account's windows this billing period. ids are the models it
|
||||
// can be asked for, each counted by the pool Cursor bills it to. An account
|
||||
// that may spend on-demand once its included usage is gone (fottencity on
|
||||
// Discord: a team that pays for more) is still served when the pools are
|
||||
// spent, so they are shown aside, and its On-demand spend is what can run
|
||||
// out: at its limit (a team member with none of their own: the team's
|
||||
// pool), or never when it has none.
|
||||
// pool), or never when it has none. An Enterprise account's period has no
|
||||
// planUsage; its allowance comes from usage-summary instead.
|
||||
async function usage(tok, ids) {
|
||||
const [res, hard] = await Promise.all([
|
||||
fetch(API + "/aiserver.v1.DashboardService/GetCurrentPeriodUsage", {
|
||||
@@ -553,7 +618,11 @@ async function usage(tok, ids) {
|
||||
if (!res.ok) return { error: statusText(res.status), windows: [] }
|
||||
const data = await res.json()
|
||||
const u = data?.planUsage
|
||||
if (!u) return { windows: [] }
|
||||
if (!u) {
|
||||
// Prefer measured Enterprise usage before any reset-date-only fallback.
|
||||
const windows = await enterpriseUsage(tok)
|
||||
return windows ? { windows } : { windows: [] }
|
||||
}
|
||||
const ms = /^[+-]?\d+$/.test(data.billingCycleEnd ?? "") ? Number(data.billingCycleEnd) : 0
|
||||
const resets = ms > 0 ? { resetsAt: new Date(ms).toISOString() } : {}
|
||||
const bucket = new Set((data.autoBucketModels ?? []).map(poolBase))
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-cursor-auth",
|
||||
"version": "0.2.29",
|
||||
"version": "0.2.30",
|
||||
"description": "Cursor subscriptions (Pro, Pro+, Ultra, Teams) in OpenCode and magpie, through the API cursor-agent talks to",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
@@ -119,10 +119,154 @@ test("a model asked for by its bare name counts in the pool its sized ids are in
|
||||
expect(["claude-opus-5-5", counts(cursor, "claude-opus-5-5"), counts(other, "claude-opus-5-5")]).toEqual(["claude-opus-5-5", false, true])
|
||||
})
|
||||
|
||||
test("an enterprise plan's spend is no window", async () => {
|
||||
test("an enterprise plan's spend is no planUsage window", async () => {
|
||||
expect((await run(fresh(), () => Response.json({ spendLimitUsage: {} }))).u).toEqual({ windows: [], signIn: "kept" })
|
||||
})
|
||||
|
||||
const SUMMARY = "https://cursor.com/api/usage-summary"
|
||||
const enterprise = (overall = { enabled: true, used: 4945, limit: 80000, remaining: 75055 }) => ({
|
||||
billingCycleEnd: "2026-11-01T00:00:00.000Z",
|
||||
membershipType: "enterprise",
|
||||
limitType: "team",
|
||||
individualUsage: { overall },
|
||||
teamUsage: { onDemand: { enabled: true, used: 0, limit: null, remaining: null } },
|
||||
})
|
||||
|
||||
test("an enterprise account's overall allowance comes from usage-summary, with native USD amounts", async () => {
|
||||
const a = botAuth()
|
||||
const { u, seen } = await run(a, (url) => {
|
||||
if (url === PLAN) return Response.json({})
|
||||
if (url === SUMMARY) return Response.json(enterprise())
|
||||
return Response.json({ billingCycleEnd: "1792925602350" })
|
||||
})
|
||||
expect(u.windows).toEqual([{
|
||||
name: "Enterprise",
|
||||
used: (100 * 4945) / 80000,
|
||||
display: "$49.45 / $800.00",
|
||||
amount: 49.45,
|
||||
limit: 800,
|
||||
unit: "USD",
|
||||
resetsAt: "2026-11-01T00:00:00.000Z",
|
||||
aside: true,
|
||||
}, {
|
||||
name: "On-demand", used: 0, display: "$0.00", resetsAt: "2026-11-01T00:00:00.000Z", aside: true,
|
||||
}])
|
||||
expect(seen.find((r) => r.url === SUMMARY)).toEqual({
|
||||
url: SUMMARY,
|
||||
method: "GET",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
Origin: "https://cursor.com",
|
||||
Cookie: "WorkosCursorSessionToken=" + encodeURIComponent("user_test::" + a.access),
|
||||
},
|
||||
redirect: "error",
|
||||
})
|
||||
})
|
||||
|
||||
// These are dashboard fixtures, not a real account driven past its limit.
|
||||
const enterpriseOnDemand = (onDemand, used = 80000) => {
|
||||
const data = enterprise({ enabled: true, used, limit: 80000 })
|
||||
data.teamUsage = { onDemand }
|
||||
return run(botAuth(), (url) => Response.json(url === SUMMARY ? data : {}))
|
||||
}
|
||||
const holdsAccount = (windows) => windows.some((w) => !w.aside && w.used >= 100)
|
||||
|
||||
test("an enterprise allowance at or past its limit stays aside with unlimited on-demand on", async () => {
|
||||
for (const used of [80000, 90000]) {
|
||||
const { u } = await enterpriseOnDemand({ enabled: true, used: 12345, limit: null }, used)
|
||||
expect(u.windows[0]).toMatchObject({ name: "Enterprise", used: 100, amount: used / 100, aside: true })
|
||||
expect(u.windows[1]).toEqual({ name: "On-demand", used: 0, display: "$123.45", resetsAt: "2026-11-01T00:00:00.000Z", aside: true })
|
||||
expect(holdsAccount(u.windows)).toBe(false)
|
||||
}
|
||||
})
|
||||
|
||||
test("an enterprise allowance at its limit is not aside with on-demand off", async () => {
|
||||
for (const limit of [null, 50000]) {
|
||||
const { u } = await enterpriseOnDemand({ enabled: false, used: 12345, limit })
|
||||
expect(u.windows).toHaveLength(1)
|
||||
expect(u.windows[0].aside).toBeUndefined()
|
||||
expect(holdsAccount(u.windows)).toBe(true)
|
||||
}
|
||||
})
|
||||
|
||||
for (const [used, aside, percent] of [[0, true, 0], [49999, true, 99.998], [50000, false, 100], [60000, false, 100]]) {
|
||||
test(`enterprise on-demand at ${used} of 50000 cents: only a budget with room keeps the allowance aside`, async () => {
|
||||
const { u } = await enterpriseOnDemand({ enabled: true, used, limit: 50000 })
|
||||
expect(u.windows).toHaveLength(2)
|
||||
expect(u.windows[0].aside).toBe(aside ? true : undefined)
|
||||
expect(u.windows[1]).toEqual({
|
||||
name: "On-demand", used: percent, display: `$${(used / 100).toFixed(2)} / $500.00`,
|
||||
amount: used / 100, limit: 500, unit: "USD", resetsAt: "2026-11-01T00:00:00.000Z",
|
||||
})
|
||||
expect(holdsAccount(u.windows)).toBe(!aside)
|
||||
})
|
||||
}
|
||||
|
||||
test("an enterprise account's unknown or invalid on-demand budget doesn't bypass its allowance", async () => {
|
||||
for (const onDemand of [
|
||||
undefined, null, {}, { enabled: "true", used: 0, limit: null },
|
||||
{ enabled: true, used: 0 }, { enabled: true, used: 0, limit: 0 },
|
||||
{ enabled: true, used: 0, limit: -1 }, { enabled: true, used: 0, limit: "unknown" },
|
||||
{ enabled: true, limit: 50000 }, { enabled: true, used: null, limit: 50000 },
|
||||
{ enabled: true, used: -1, limit: 50000 }, { enabled: true, used: "unknown", limit: 50000 },
|
||||
]) {
|
||||
const { u } = await enterpriseOnDemand(onDemand)
|
||||
expect(u.windows).toHaveLength(1)
|
||||
expect(u.windows[0].aside).toBeUndefined()
|
||||
expect(holdsAccount(u.windows)).toBe(true)
|
||||
}
|
||||
})
|
||||
|
||||
test("explicitly unlimited enterprise on-demand stays aside even when its spend is unknown", async () => {
|
||||
const { u } = await enterpriseOnDemand({ enabled: true, limit: null })
|
||||
expect(u.windows).toHaveLength(1)
|
||||
expect(u.windows[0].aside).toBe(true)
|
||||
expect(holdsAccount(u.windows)).toBe(false)
|
||||
})
|
||||
|
||||
test("usage-summary is only asked when planUsage is missing", async () => {
|
||||
const { seen } = await run(auth, (url) =>
|
||||
url === PLAN ? Response.json({}) : Response.json({ planUsage: { autoPercentUsed: 1 } }),
|
||||
)
|
||||
expect(seen.map((r) => r.url)).not.toContain(SUMMARY)
|
||||
})
|
||||
|
||||
test("an enterprise summary that can't be read leaves the card empty, as before", async () => {
|
||||
for (const reply of [
|
||||
() => new Response("no", { status: 401 }),
|
||||
() => Response.json({ membershipType: "pro", individualUsage: { overall: { enabled: true, used: 1, limit: 2 } } }),
|
||||
() => Response.json(enterprise({ enabled: false, used: 1, limit: 2 })),
|
||||
() => Response.json(enterprise({ enabled: true, used: 1, limit: 0 })),
|
||||
...["used", "limit"].flatMap((field) =>
|
||||
[undefined, null, "", false, true, [], [1], {}, "0", "80000", -1].map((value) =>
|
||||
() => Response.json(enterprise({ enabled: true, used: 4945, limit: 80000, [field]: value })),
|
||||
),
|
||||
),
|
||||
// JSON permits exponents that overflow JavaScript's finite numbers.
|
||||
...["used", "limit"].flatMap((field) => ["1e400", "-1e400"].map((value) =>
|
||||
() => new Response(`{"membershipType":"enterprise","individualUsage":{"overall":{"enabled":true,"used":${field === "used" ? value : 4945},"limit":${field === "limit" ? value : 80000}}}}`),
|
||||
)),
|
||||
() => new Response("not JSON"),
|
||||
]) {
|
||||
const { u } = await run(botAuth(), (url) => {
|
||||
if (url === PLAN) return Response.json({})
|
||||
if (url === SUMMARY) return reply()
|
||||
return Response.json({ billingCycleEnd: "1792925602350" })
|
||||
})
|
||||
expect(u).toEqual({ windows: [], signIn: "kept" })
|
||||
}
|
||||
})
|
||||
|
||||
test("an enterprise summary with numeric zero used still shows the full allowance", async () => {
|
||||
const { u } = await run(botAuth(), (url) => Response.json(
|
||||
url === SUMMARY ? enterprise({ enabled: true, used: 0, limit: 80000 }) : {},
|
||||
))
|
||||
expect(u.windows[0]).toEqual({
|
||||
name: "Enterprise", used: 0, display: "$0.00 / $800.00",
|
||||
amount: 0, limit: 800, unit: "USD", resetsAt: "2026-11-01T00:00:00.000Z", aside: true,
|
||||
})
|
||||
})
|
||||
|
||||
test("a refused token is the status magpie says", async () => {
|
||||
expect((await run(fresh(), () => new Response("no", { status: 401 }))).u).toEqual({ error: "Unauthorized", windows: [], signIn: "kept" })
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user