Grok Bot has an allowance of its own, apart from Cursor's monthly usage, and it shows on cursor.com's dashboard only. auth.usage now also reads https://cursor.com/api/dashboard/get-sand-usage-status with the sign-in as the dashboard session (WorkosCursorSessionToken, as Cursor's dashboard and CodexBar's CursorSandUsage.swift send it), and adds a "Grok Bot" window: a paid grant with its weekly reset, a live trial as "Grok Bot (trial)" without one. The window is aside, so Bot running out never stops Cursor model requests, and it comes before Total so it is among the Usage card's three rings. No grant, an expired trial, a non-2xx, a redirect (not followed), a hang past 5 s or a reply that isn't JSON each leave Cursor's own windows as they were. Rebased onto main after cursor 0.2.4 (7516d63); package version is 0.2.5. Optional pins for the 5 s AbortSignal.timeout and the Origin header are not in this commit (Codex CLI unavailable for the edits); the runtime guards stay. Tests: usagePercent 130 is shown as 100 (and -5 as 0), and a 307 to a login page is refused rather than followed; each fails with its guard removed from botUsage. HOME=$(mktemp -d) bun test packages/cursor: 73 pass, 0 fail. HOME=$(mktemp -d) bun test (whole repo, on this commit atop main7516d63): 1033 pass, 0 fail. bun scripts/check.mjs: all 15 ok. Checked by hand once before an earlier squash: a Free Cursor account linked to SuperGrok showed the Bot ring in magpie. Not tried with a paid Cursor grant.
217 lines
12 KiB
JavaScript
217 lines
12 KiB
JavaScript
// auth.usage tells what magpie's built-in Cursor account shows
|
|
// (internal/provider/cursor_usage.go), against Cursor's replies as its tests
|
|
// give them (cursor_usage_test.go).
|
|
import "./nonet.mjs" // first: no request leaves this machine
|
|
import { afterEach, expect, test } from "bun:test"
|
|
import { CursorAuthPlugin, _internal } from "./index.mjs"
|
|
|
|
const real = globalThis.fetch
|
|
afterEach(() => (globalThis.fetch = real))
|
|
|
|
// a token that runs out in an hour
|
|
const jwt = (exp) => ["e30", Buffer.from(JSON.stringify({ exp })).toString("base64url"), "sig"].join(".")
|
|
const tok = jwt(Math.floor(Date.now() / 1000) + 3600)
|
|
const auth = { type: "oauth", access: tok, refresh: "", expires: 0, accountId: "a@b.c" }
|
|
// a sign-in of its own, so no plan asked before is remembered for it
|
|
let n = 0
|
|
const fresh = () => ({ ...auth, access: jwt(Math.floor(Date.now() / 1000) + 7200 + ++n) })
|
|
|
|
const firstParty = ["grok-4.7@256k", "grok-4.7@500k", "grok-4.6", "grok-4.7-xhigh-fast", "cursor-grok-4.7-high-fast", "cursor-grok-4.6-high-fast", "grok-4.5-fast-high", "auto", "default", "composer-2.5", "COMPOSER-2.5-FAST", "composer"]
|
|
const bucketed = ["future-first-party", "grok-4.8-high", "cursor-grok-4.8-xhigh-fast"]
|
|
const others = ["claude-opus-5-5@300k", "claude-opus-5-5@1m", "claude-opus-5-5", "gpt-5.6-sol", "gemini-3.1-pro", "grok-3", "grok-4.70", "grok-4.80-high", "unknown-model"]
|
|
const provider = { models: Object.fromEntries([...firstParty, ...bucketed, ...others].map((id) => [id, { id }])) }
|
|
|
|
async function run(a, reply) {
|
|
const seen = []
|
|
globalThis.fetch = async (url, init) => {
|
|
seen.push({ url: String(url), method: init.method, headers: init.headers, body: init.body, ...(init.redirect ? { redirect: init.redirect } : {}) })
|
|
const res = await reply(String(url))
|
|
if (String(url) === BOT && res.status === 307 && res.headers.has("Location")) {
|
|
if (init.redirect === "error") throw new TypeError("redirect refused")
|
|
return Response.json(paidBot)
|
|
}
|
|
return res
|
|
}
|
|
const hooks = await CursorAuthPlugin()
|
|
return { u: await hooks.auth.usage(async () => a, provider), seen }
|
|
}
|
|
|
|
// counts is whether a window counts model, as magpie reads the lists
|
|
// (internal/provider/plugin_usage.go)
|
|
function counts(w, model) {
|
|
const m = model.toLowerCase()
|
|
if (w.models?.length) return w.models.some((x) => x.toLowerCase() === m)
|
|
if (w.notModels?.length) return !w.notModels.some((x) => x.toLowerCase() === m)
|
|
return true
|
|
}
|
|
|
|
const PERIOD = "https://api2.cursor.sh/aiserver.v1.DashboardService/GetCurrentPeriodUsage"
|
|
const PLAN = "https://api2.cursor.sh/aiserver.v1.DashboardService/GetPlanInfo"
|
|
|
|
test("the period's three windows, reset at the cycle's end", async () => {
|
|
const { u, seen } = await run(auth, (url) =>
|
|
url === PLAN
|
|
? Response.json({ planInfo: { planName: "Pro" } })
|
|
: Response.json({ billingCycleEnd: "1792833042000", planUsage: { autoPercentUsed: 12.5, apiPercentUsed: 40, totalPercentUsed: 20 } }),
|
|
)
|
|
expect(seen.find((r) => r.url === PERIOD)).toEqual({
|
|
url: PERIOD,
|
|
method: "POST",
|
|
headers: { Authorization: `Bearer ${tok}`, "Content-Type": "application/json", "Connect-Protocol-Version": "1" },
|
|
body: "{}",
|
|
})
|
|
const at = new Date(1792833042000).toISOString()
|
|
expect(u.windows.map(({ models, notModels, ...w }) => w)).toEqual([
|
|
{ name: "Cursor Models", used: 12.5, resetsAt: at },
|
|
{ name: "Other Models", used: 40, resetsAt: at },
|
|
{ name: "Total", used: 20, resetsAt: at, aside: true },
|
|
])
|
|
expect(u.error).toBeUndefined()
|
|
})
|
|
|
|
test("the card names the plan as cursor-agent about does (GetPlanInfo's planName), asked once an hour", async () => {
|
|
const a = fresh()
|
|
const reply = (url) =>
|
|
url === PLAN ? Response.json({ planInfo: { planName: "Pro+" } }) : Response.json({ planUsage: { autoPercentUsed: 1, apiPercentUsed: 2, totalPercentUsed: 3 } })
|
|
const first = await run(a, reply)
|
|
expect(first.u.plan).toBe("Pro+")
|
|
const plan = first.seen.find((r) => r.url === PLAN)
|
|
expect(plan.method).toBe("POST")
|
|
expect(plan.headers.Authorization).toBe(`Bearer ${a.access}`)
|
|
const again = await run(a, reply)
|
|
expect(again.u.plan).toBe("Pro+")
|
|
expect(again.seen.map((r) => r.url)).toEqual([PERIOD])
|
|
})
|
|
|
|
test("a plan Cursor doesn't say leaves the card's plan out", async () => {
|
|
const a = fresh()
|
|
const { u } = await run(a, (url) => (url === PLAN ? new Response("no", { status: 500 }) : Response.json({ planUsage: { autoPercentUsed: 1 } })))
|
|
expect(u.plan).toBeUndefined()
|
|
expect(u.windows.length).toBe(3)
|
|
})
|
|
|
|
for (const bucket of [["default", "composer-2.5", "cursor-grok-4.5-high", "future-first-party", "Grok-4.8"], undefined]) {
|
|
test(`each model counts in the pool magpie puts it in (autoBucketModels ${bucket ? "given" : "left out"})`, async () => {
|
|
const { u } = await run(auth, () =>
|
|
Response.json({ billingCycleEnd: "1792833042000", planUsage: { autoPercentUsed: 25, apiPercentUsed: 100, totalPercentUsed: 100 }, autoBucketModels: bucket }),
|
|
)
|
|
const [cursor, other, total] = u.windows
|
|
const pool = bucket ? [...firstParty, ...bucketed] : firstParty
|
|
const rest = bucket ? others : [...others, ...bucketed]
|
|
for (const m of pool) expect([m, counts(cursor, m), counts(other, m)]).toEqual([m, true, false])
|
|
for (const m of rest) expect([m, counts(cursor, m), counts(other, m)]).toEqual([m, false, true])
|
|
expect(total.models).toBeUndefined()
|
|
expect(total.notModels).toBeUndefined()
|
|
})
|
|
}
|
|
|
|
// Will on Discord: the list has grok-4.7 only at its sizes, a routing group
|
|
// still asks for bare grok-4.7, and it was counted in Other Models (used up)
|
|
test("a model asked for by its bare name counts in the pool its sized ids are in", async () => {
|
|
const { u } = await run(auth, () =>
|
|
Response.json({ billingCycleEnd: "1792833042000", planUsage: { autoPercentUsed: 16, apiPercentUsed: 100, totalPercentUsed: 60 } }),
|
|
)
|
|
const [cursor, other] = u.windows
|
|
for (const m of ["grok-4.7", "Grok-4.7"]) expect([m, counts(cursor, m), counts(other, m)]).toEqual([m, true, false])
|
|
// a bare other model stays in Other Models
|
|
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 () => {
|
|
expect((await run(fresh(), () => Response.json({ spendLimitUsage: {} }))).u).toEqual({ windows: [], signIn: "kept" })
|
|
})
|
|
|
|
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" })
|
|
})
|
|
|
|
test("a run-out sign-in says so, and asks nothing", async () => {
|
|
const { u, seen } = await run({ ...auth, access: jwt(1) }, () => Response.json({}))
|
|
expect(u).toEqual({ error: "Cursor's sign-in has run out; sign in to Cursor again", windows: [], signIn: "kept" })
|
|
expect(seen).toEqual([])
|
|
})
|
|
|
|
// the built-in's 401 for any "expired", a token's or a trial's, which marked
|
|
// no account (errorResponse says kept)
|
|
test("anything expired is the built-in's 401", () => {
|
|
const { failure } = _internal
|
|
const says = (msg) => failure(400, JSON.stringify({ code: "failed_precondition", message: msg }))
|
|
for (const m of ["Your access token has expired", "Session expired, please log in again", "Your free trial has expired", "This link has expired"])
|
|
expect(says(m)).toEqual({ status: 401, message: m + " — sign in to Cursor again in magpie" })
|
|
})
|
|
|
|
// The built-in's usage read neither marked nor cleared the account, so each
|
|
// read says kept, a clean one too.
|
|
test("a usage read keeps the account, clean or not", async () => {
|
|
const PERIOD_OK = () => Response.json({ billingCycleEnd: "1792833042000", planUsage: { autoPercentUsed: 1, apiPercentUsed: 2, totalPercentUsed: 3 } })
|
|
expect((await run(fresh(), PERIOD_OK)).u.signIn).toBe("kept")
|
|
expect((await run(fresh(), () => new Response("no", { status: 401 }))).u.signIn).toBe("kept")
|
|
expect((await run({ ...auth, access: jwt(1) }, PERIOD_OK)).u.signIn).toBe("kept")
|
|
})
|
|
|
|
// the gateway names the provider before a plugin's error
|
|
test("an error doesn't name Cursor again", () => {
|
|
const f = _internal.failure(429, JSON.stringify({ code: "resource_exhausted", message: "slow down" }))
|
|
expect(f).toEqual({ status: 429, message: "usage limit reached: slow down" })
|
|
})
|
|
|
|
test("a model list Cursor couldn't give fails, not shrinks to the configured few", async () => {
|
|
globalThis.fetch = async () => new Response("down", { status: 503 })
|
|
const hooks = await CursorAuthPlugin()
|
|
const configured = { models: { auto: { id: "auto" } } }
|
|
await expect(hooks.provider.models(configured, { auth: fresh() })).rejects.toThrow()
|
|
})
|
|
|
|
const BOT = "https://cursor.com/api/dashboard/get-sand-usage-status"
|
|
const botAuth = () => ({ ...fresh(), access: ["e30", Buffer.from(JSON.stringify({ sub: "auth0|user_test", exp: Math.floor(Date.now() / 1000) + 7200 + ++n })).toString("base64url"), "sig"].join(".") })
|
|
const reset = "2030-01-08T00:00:00.000Z"
|
|
const paidBot = { includedLimitZero: false, usagePercent: 42, nextResetTimestampUtc: reset }
|
|
const period = { planUsage: { autoPercentUsed: 12, apiPercentUsed: 34, totalPercentUsed: 20 } }
|
|
const botRun = (data, monthly = period, account = botAuth()) => run(account, (url) => {
|
|
if (url === PLAN) return Response.json({})
|
|
if (url === BOT) {
|
|
if (data instanceof Error) throw data
|
|
return data instanceof Response ? data : Response.json(data)
|
|
}
|
|
return Response.json(monthly)
|
|
})
|
|
|
|
test("Bot uses the dashboard session and appears before Total without gating Cursor models", async () => {
|
|
const a = botAuth()
|
|
const { u, seen } = await botRun(paidBot, period, a)
|
|
expect(u.windows.map((w) => w.name)).toEqual(["Cursor Models", "Other Models", "Grok Bot", "Total"])
|
|
expect(u.windows[2]).toEqual({ name: "Grok Bot", used: 42, aside: true, resetsAt: reset, span: 604800 })
|
|
expect(seen.find((r) => r.url === BOT)).toMatchObject({ method: "POST", body: "{}", headers: { Cookie: "WorkosCursorSessionToken=" + encodeURIComponent("user_test::" + a.access) } })
|
|
})
|
|
|
|
test("Bot grants work without Cursor monthly usage, with current or legacy allowance flags", async () => {
|
|
expect((await botRun({ ...paidBot, hasNonZeroIncludedLimit: false }, {})).u.windows).toEqual([
|
|
{ name: "Grok Bot", used: 42, aside: true, resetsAt: reset, span: 604800 },
|
|
])
|
|
expect((await botRun({ hasNonZeroIncludedLimit: true, usagePercent: 0 }, {})).u.windows).toEqual([{ name: "Grok Bot", used: 0, aside: true }])
|
|
})
|
|
|
|
test("an exhausted trial stays visible, without treating expiry as a reset", async () => {
|
|
const trial = { includedLimitZero: true, sandTrialExpiresAt: "2099-01-01T00:00:00Z", usagePercent: 100, nextResetTimestampUtc: reset }
|
|
expect((await botRun(trial)).u.windows[2]).toEqual({ name: "Grok Bot (trial)", used: 100, aside: true })
|
|
expect((await botRun({ ...trial, sandTrialExpiresAt: "2000-01-01T00:00:00Z" })).u.windows.map((w) => w.name)).toEqual(["Cursor Models", "Other Models", "Total"])
|
|
})
|
|
|
|
test("absent or failed Bot readings preserve Cursor usage and sign-in", async () => {
|
|
const normal = (await run(fresh(), (url) => Response.json(url === PLAN ? {} : period))).u
|
|
for (const data of [null, {}, { ...paidBot, includedLimitZero: true, hasNonZeroIncludedLimit: true }, { ...paidBot, usagePercent: null }, new Response("", { status: 403 }), new Response("not JSON"), new Error("network unavailable")])
|
|
expect((await botRun(data)).u).toEqual(normal)
|
|
})
|
|
|
|
test("Bot usage is clamped", async () => {
|
|
for (const [usagePercent, used] of [[130, 100], [-5, 0]])
|
|
expect((await botRun({ ...paidBot, usagePercent })).u.windows[2]).toEqual({ name: "Grok Bot", used, aside: true, resetsAt: reset, span: 604800 })
|
|
})
|
|
|
|
test("Bot login redirects are refused", async () => {
|
|
const normal = (await run(botAuth(), (url) => Response.json(url === PLAN ? {} : period))).u
|
|
const { u, seen } = await botRun(new Response(null, { status: 307, headers: { Location: "https://cursor.com/login?returnTo=dashboard" } }))
|
|
expect(u).toEqual(normal)
|
|
expect(seen.find((r) => r.url === BOT)).toMatchObject({ redirect: "error" })
|
|
})
|