CryptoSarom on X (2116260605053321489): "magpie 似乎没法支持 cursor 的赠金读取,
以及赠金使用限制". The card read only the included pools and on-demand.
What Cursor exposes, as Cursor 3.x's IDE (workbench.desktop.main.js) and
cursor-agent 2026.10.01's schemas have it:
- GetCurrentPeriodUsage planUsage.bonusSpend (cents), remainingBonus (bool,
no amount) and bonusTooltip: the IDE's "+$X free usage on us" line.
- DashboardService/GetUsageLimitStatusAndActiveGrants activeGrants
(aiserver.v1.ActiveCreditGrant): totalCents, remainingCents, expiresAtMs,
allowedModelIds / allowedModelTags / allowedModelSelections (models it
may be spent on), allowedClientTypes ("glass" without "ide" is the IDE's
Cloud Agent credits), campaignName. The IDE's "Use your $X free credits"
banner reads it; it matches tags lower case with "-" as ".".
The card now shows Free usage (bonus spent, "more left" when
remainingBonus) and a window per grant (spent / total USD, runsOutAt and
runsOut at its expiry, models when limited, "(Cloud Agents only)" when
cursor-agent can't spend it). All are aside. Routing: a bonus with some
left, or a grant magpie can spend on every model, sets the spent pools
aside as on-demand does; a model-limited grant leaves its models out of
a spent pool (Cursor Models keeps a non-empty list, or goes aside). With
Included usage only on, none of this keeps an account served. A grants
read that fails leaves the card as before.
Run on mote at this tree: HOME=$(mktemp -d) bun test: 3960 pass, 9 skip,
1 fail (a test of my own, since fixed); then credit/usage/included-only
79 pass 0 fail; bun scripts/check.mjs cursor ok. Broken by hand: the new
credit.test.mjs against the old index.mjs, 9 of 9 red.
Real account (Premium, merged weekly, cursor-agent's sign-in, read only):
GetCurrentPeriodUsage, GetUsageLimitStatusAndActiveGrants,
GetCreditGrantsBalance, GetClientVisibleCreditGrants read; it has no bonus
and no grant, and the new plugin's card for it is unchanged (Weekly, Grok
Bot, On-demand). Those replies are credit.test.mjs's REAL_ fixtures; the
#1868 team account's real bonusSpend 14151 now shows "+$141.51 on us".
Not tried: a real account with a grant or remainingBonus true (none at
hand), so the grant fixtures follow the schema, and whether Cursor
really serves a spent pool on bonus is Cursor's word ("free usage beyond
what you've purchased"), not a request made past a spent pool.
161 lines
10 KiB
JavaScript
161 lines
10 KiB
JavaScript
// Cursor's free credit (CryptoSarom on X: "magpie 似乎没法支持 cursor 的赠金
|
|
// 读取, 以及赠金使用限制"), as Cursor 3.x's IDE reads it:
|
|
// GetCurrentPeriodUsage's planUsage.bonusSpend / remainingBonus /
|
|
// bonusTooltip (its "+$X free usage on us" line) and
|
|
// DashboardService/GetUsageLimitStatusAndActiveGrants' activeGrants (its
|
|
// "Use your $X free … credits" banner), whose allowedModelIds,
|
|
// allowedModelTags and allowedClientTypes say what a grant may be spent on.
|
|
//
|
|
// The plan replies are real bytes: REAL_PERIOD and REAL_GRANTS are a
|
|
// Premium (merged, weekly) account's GetCurrentPeriodUsage and
|
|
// GetUsageLimitStatusAndActiveGrants, read 2026-10-31 (autoBucketModels
|
|
// shortened), and the #1868 team account's planUsage has a bonusSpend.
|
|
// No account at hand had a grant, so the grant replies are Connect JSON of
|
|
// aiserver.v1.ActiveCreditGrant as the IDE's and cursor-agent 2026.10.01's
|
|
// schemas declare it (int64 fields as strings, as REAL_GRANTS' cents are).
|
|
import "./nonet.mjs" // first: no request leaves this machine
|
|
import { afterEach, expect, test } from "bun:test"
|
|
import { CursorAuthPlugin } from "./index.mjs"
|
|
|
|
const real = globalThis.fetch
|
|
afterEach(() => (globalThis.fetch = real))
|
|
|
|
let n = 0
|
|
const fresh = () => ({
|
|
type: "oauth",
|
|
access: ["e30", Buffer.from(JSON.stringify({ sub: "auth0|user_credit" + ++n, exp: Math.floor(Date.now() / 1000) + 7200 + n })).toString("base64url"), "sig"].join("."),
|
|
refresh: "",
|
|
expires: 0,
|
|
accountId: "a@b.c",
|
|
})
|
|
|
|
const DASH = "https://api2.cursor.sh/aiserver.v1.DashboardService/"
|
|
const PERIOD = DASH + "GetCurrentPeriodUsage"
|
|
const PLAN = DASH + "GetPlanInfo"
|
|
const HARD = DASH + "GetHardLimit"
|
|
const GRANTS = DASH + "GetUsageLimitStatusAndActiveGrants"
|
|
|
|
const REAL_PERIOD = {"billingCycleStart":"1793078412396","billingCycleEnd":"1793683212396","planUsage":{"remainingBonus":false,"bonusTooltip":"We work with model providers to give you free usage beyond what you've purchased. Amounts may vary.","autoPercentUsed":7.042253521126761,"apiPercentUsed":0,"totalPercentUsed":7.042253521126761},"spendLimitUsage":{"limitType":"user"},"displayThreshold":200,"enabled":true,"displayMessage":"You've used 7% of your included usage","autoModelSelectedDisplayMessage":"You've used 7% of your included total usage","namedModelSelectedDisplayMessage":"You've used 0% of your included API usage","autoBucketModels":["default","composer-2","composer-2.5","grok-4.7"]}
|
|
const REAL_PLAN = {"planInfo":{"planName":"Premium","price":"","billingCycleEnd":"1794547141340","includedUsagePeriod":"INCLUDED_USAGE_PERIOD_WEEKLY","includedUsageResetsAt":"1793683212396"},"isMerged":true}
|
|
const REAL_GRANTS = {"usageLimitPolicyStatus":{"canConfigureSpendLimit":true,"canAdjustOnDemand":true,"recommendedOnDemandLimitCents":"10000","onDemandMinCents":"5000","onDemandMaxCents":"20000","creditRequestButtonHidden":false}}
|
|
|
|
const provider = { models: Object.fromEntries(["auto", "composer-2.5", "claude-opus-5-5@300k", "claude-opus-5-5@1m", "gpt-5.6-sol"].map((id) => [id, { id }])) }
|
|
const at = new Date(1793683212396).toISOString()
|
|
const later = Date.now() + 5 * 86400_000
|
|
|
|
async function read(r, on = false) {
|
|
const seen = []
|
|
globalThis.fetch = async (url, init) => {
|
|
url = String(url)
|
|
seen.push({ url, headers: init.headers, body: init.body })
|
|
if (url === PERIOD) return Response.json(r.period)
|
|
if (url === PLAN) return Response.json(r.plan ?? {})
|
|
if (url === HARD) return Response.json(r.hard ?? { hardLimit: 0 })
|
|
if (url === GRANTS) return r.grants instanceof Response ? r.grants : Response.json(r.grants ?? REAL_GRANTS)
|
|
return new Response("no", { status: 404 })
|
|
}
|
|
const hooks = await CursorAuthPlugin({ magpie: { option: (p, k) => (p === "cursor" && k === "included_only" ? on : undefined) } })
|
|
const a = fresh()
|
|
return { ...(await hooks.auth.usage(async () => a, provider)), seen }
|
|
}
|
|
|
|
// how magpie reads the windows (internal/provider/plugin_usage.go,
|
|
// routing.go allowanceOf): a window not aside and used up stops each model
|
|
// it counts
|
|
function counts(w, model) {
|
|
if (w.models?.length) return w.models.includes(model)
|
|
if (w.notModels?.length) return !w.notModels.includes(model)
|
|
return true
|
|
}
|
|
const stopped = (windows, model) => windows.some((w) => !w.aside && w.used >= 100 && counts(w, model))
|
|
|
|
const spentPools = { billingCycleEnd: "1793683212396", planUsage: { autoPercentUsed: 100, apiPercentUsed: 100, totalPercentUsed: 100 }, autoBucketModels: ["default", "composer-2.5"] }
|
|
const grant = (g) => ({ activeGrants: [{ grantId: "1001", totalCents: "2000", remainingCents: "1500", expiresAtMs: String(later), grantType: "promo", showInClient: true, ...g }] })
|
|
|
|
test("the real account with no bonus and no grant shows no free credit, and asks for grants as the IDE does", async () => {
|
|
const { windows, seen } = await read({ period: REAL_PERIOD, plan: REAL_PLAN })
|
|
expect(windows.map((w) => w.name)).toEqual(["Weekly"])
|
|
const g = seen.find((r) => r.url === GRANTS)
|
|
expect(g.body).toBe("{}")
|
|
expect(g.headers["Connect-Protocol-Version"]).toBe("1")
|
|
})
|
|
|
|
test("the plan's bonus: its spend is shown, and with some left the spent pools don't stop the account", async () => {
|
|
const { windows } = await read({ period: { ...spentPools, planUsage: { ...spentPools.planUsage, bonusSpend: 1234, remainingBonus: true } } })
|
|
expect(windows.find((w) => w.name === "Free usage")).toEqual({ name: "Free usage", used: 0, display: "+$12.34 on us, more left", resetsAt: at, aside: true })
|
|
for (const m of ["composer-2.5", "gpt-5.6-sol"]) expect(stopped(windows, m)).toBe(false)
|
|
// none left: the spent pools stop it again
|
|
const out = (await read({ period: { ...spentPools, planUsage: { ...spentPools.planUsage, bonusSpend: 1234, remainingBonus: false } } })).windows
|
|
expect(out.find((w) => w.name === "Free usage").display).toBe("+$12.34 on us")
|
|
for (const m of ["composer-2.5", "gpt-5.6-sol"]) expect(stopped(out, m)).toBe(true)
|
|
})
|
|
|
|
test("a grant for every model: remaining of total, its expiry, and the spent pools stay aside while it lasts", async () => {
|
|
const { windows } = await read({ period: spentPools, grants: grant({ campaignName: "Launch week" }) })
|
|
expect(windows.find((w) => w.name === "Launch week")).toEqual({
|
|
name: "Launch week", used: 25, display: "$5.00 / $20.00", amount: 5, limit: 20, unit: "USD",
|
|
runsOutAt: new Date(later).toISOString(), runsOut: 15, aside: true,
|
|
})
|
|
for (const m of ["composer-2.5", "gpt-5.6-sol"]) expect(stopped(windows, m)).toBe(false)
|
|
// used up, or expired: the pools are the allowance again
|
|
for (const g of [{ remainingCents: "0" }, { expiresAtMs: String(Date.now() - 1000) }]) {
|
|
const out = (await read({ period: spentPools, grants: grant(g) })).windows
|
|
for (const m of ["composer-2.5", "gpt-5.6-sol"]) expect(stopped(out, m)).toBe(true)
|
|
}
|
|
})
|
|
|
|
test("a grant limited to some models names them, and keeps only those served past a spent pool", async () => {
|
|
for (const g of [{ allowedModelIds: ["claude-opus-5-5"] }, { allowedModelTags: ["opus-5"] }, { allowedModelSelections: [{ modelId: "claude-opus-5-5", maxMode: false }] }]) {
|
|
const { windows } = await read({ period: spentPools, grants: grant(g) })
|
|
const w = windows.find((w) => w.name === "Free credit")
|
|
// a sized id is asked for by its bare name too
|
|
expect(w.models).toEqual(["claude-opus-5-5@300k", "claude-opus-5-5", "claude-opus-5-5@1m"])
|
|
expect(w.aside).toBe(true)
|
|
expect(stopped(windows, "claude-opus-5-5@1m")).toBe(false)
|
|
expect(stopped(windows, "gpt-5.6-sol")).toBe(true)
|
|
expect(stopped(windows, "composer-2.5")).toBe(true)
|
|
}
|
|
// with room in the pools nothing changes
|
|
const room = (await read({ period: { ...spentPools, planUsage: { autoPercentUsed: 10, apiPercentUsed: 10, totalPercentUsed: 10 } }, grants: grant({ allowedModelIds: ["claude-opus-5-5"] }) })).windows
|
|
expect(room.find((w) => w.name === "Other Models").notModels).toEqual(["auto", "composer-2.5", "default"])
|
|
})
|
|
|
|
test("a grant on Cursor Models alone keeps a spent Cursor Models pool from stopping them, never counting every model", async () => {
|
|
const { windows } = await read({ period: spentPools, grants: grant({ allowedModelIds: ["default", "composer-2.5"] }) })
|
|
const cm = windows.find((w) => w.name === "Cursor Models")
|
|
expect(cm.models.length).toBeGreaterThan(0)
|
|
expect(stopped(windows, "composer-2.5")).toBe(false)
|
|
expect(stopped(windows, "auto")).toBe(false)
|
|
expect(stopped(windows, "gpt-5.6-sol")).toBe(true)
|
|
})
|
|
|
|
test("a merged plan's spent Weekly leaves out the models a grant pays for", async () => {
|
|
const { windows } = await read({ period: spentPools, plan: REAL_PLAN, grants: grant({ allowedModelIds: ["gpt-5.6-sol"] }) })
|
|
expect(stopped(windows, "gpt-5.6-sol")).toBe(false)
|
|
expect(stopped(windows, "composer-2.5")).toBe(true)
|
|
})
|
|
|
|
test("a grant magpie can't spend (Cloud Agents only, or another client) is shown with its limit and keeps nothing served", async () => {
|
|
for (const [clients, name] of [[["glass"], "Free credit (Cloud Agents only)"], [["ide"], "Free credit (ide only)"]]) {
|
|
const { windows } = await read({ period: spentPools, grants: grant({ allowedClientTypes: clients }) })
|
|
expect(windows.find((w) => w.name === name)).toBeDefined()
|
|
expect(stopped(windows, "composer-2.5")).toBe(true)
|
|
}
|
|
const cli = (await read({ period: spentPools, grants: grant({ allowedClientTypes: ["ide", "cli"] }) })).windows
|
|
expect(stopped(cli, "composer-2.5")).toBe(false)
|
|
})
|
|
|
|
test("with Included usage only on, free credit is shown and keeps nothing served", async () => {
|
|
const { windows } = await read({ period: { ...spentPools, planUsage: { ...spentPools.planUsage, bonusSpend: 100, remainingBonus: true } }, grants: grant({}) }, true)
|
|
expect(windows.some((w) => w.name === "Free credit")).toBe(true)
|
|
expect(stopped(windows, "composer-2.5")).toBe(true)
|
|
})
|
|
|
|
test("free credit goes after Bot and before Total; a grants read that fails leaves the card as before", async () => {
|
|
const { windows } = await read({ period: spentPools, grants: grant({}) })
|
|
expect(windows.map((w) => w.name)).toEqual(["Cursor Models", "Other Models", "Free credit", "Total"])
|
|
const failed = (await read({ period: spentPools, grants: new Response("no", { status: 500 }) })).windows
|
|
expect(failed.map((w) => w.name)).toEqual(["Cursor Models", "Other Models", "Total"])
|
|
for (const m of ["composer-2.5", "gpt-5.6-sol"]) expect(stopped(failed, m)).toBe(true)
|
|
})
|