Files
plugins/packages/cursor/stepusage.test.mjs
T
yetone 288c6bc9c9 cursor 0.1.10: far from Cursor a conversation's tool steps add up to what Cursor counted, cache included (yetone/magpie#1053, #33). The tool-step usage-event wait of 0.1.9 (first ask at 1.5 s, every 0.5 s, up to 6 s) never met an event on the reporter's network (Windows, mainland China: a GetFilteredUsageEvents round trip of ~4.2 s, the event 4–5 s after the Run closes), so tool steps were counted as their whole prompt estimated with no cache (fable 13/13 steps, 0% cached; opus 27%) where Cursor counted 95–99%, and every tool step waited the full 6 s. A tool step's Run is now owed by its conversation (only when a conversation_id names the session): the step counts every event of the conversation's owed Runs that shows within the same wait, its own or earlier ones, and says 0 used when none has; the conversation's next Runs collect the rest in the background from 5 s after the owed Run closed, and a step that ends the turn waits for whatever is still owed (in-flight collections, then a poll for the rest). Events are claimed once, by count, so the conversation's prompt, cache read, cache write and output add up to Cursor's dashboard, some of it a step late. After 3 steps in a row left something owed an account's tool steps stop waiting (asked again every 10th). A Run that fails after collecting gives it back; an account that can't read its events (4xx) and a Run with no session id are estimated as before. latency.test.mjs runs whole conversations through the plugin against a fake Cursor with the reporter's latency scaled 1/20 and the reporter's literal events (in 2 / cacheRead 63457 / cacheWrite 3081, in 2 / 66538 / 14756, in 4 / 67872 / 2392): on 0.1.9 the 3-step conversation sums to 137936 prompt / 131329 cached instead of 218104 / 197867, and skipped steps still wait the full window (292 ms > 150); both pass now, 20 of 20 runs, and stepusage + latency pass with --rerun-each 20. HOME=$(mktemp -d) bun test: 682 pass, 0 fail; check.mjs cursor passes. Not tried with a real Cursor account on a high-latency network.
2026-10-07 03:17:08 +08:00

138 lines
6.4 KiB
JavaScript

// A step that calls the caller's tools has no TurnEndedUpdate before the
// Run is closed, so its usage is the dashboard's usage event of the Run's
// conversation (yetone/magpie#676): read, not guessed with no cache.
import "./nonet.mjs" // first: no request leaves this machine
import { afterEach, expect, test } from "bun:test"
import { _internal } from "./index.mjs"
const { pb, decode, stepUsage, STEP_WAIT } = _internal
const real = globalThis.fetch
const wait = { ...STEP_WAIT }
afterEach(() => {
globalThis.fetch = real
Object.assign(STEP_WAIT, wait)
})
const update = (num, body) => ({ data: pb().bytes(1, pb().bytes(num, body)).done() })
// the server's call of the caller's tool cache_probe, as Cursor sends it
const call = {
data: pb()
.bytes(2, pb().varint(1, 7).str(15, "x1").bytes(11, pb().str(1, "magpie-cache_probe").str(3, "call_1").str(5, "cache_probe").done()).done())
.done(),
}
const listed = update(27, pb().varint(1, 1).done())
async function run(frames, opts) {
const out = []
for await (const p of decode(frames, { send() {}, blobs: new Map(), tools: [], estimate: 2395, ...opts })) out.push(p)
return out
}
test("a step that calls tools takes the dashboard's count of its Run", async () => {
const seen = []
const out = await run([update(1, pb().str(1, "probing").done()), listed, call], {
close: () => seen.push("close"),
stepUsage: async () => (seen.push("usage"), { input: 41, output: 133, cacheRead: 4224, cacheWrite: 0, reasoning: 0 }),
})
// the Run closed before the count is asked for
expect(seen).toEqual(["close", "usage"])
expect(out.at(-1)).toEqual({
stop: "tool_calls",
usage: {
prompt_tokens: 4265,
completion_tokens: 133,
total_tokens: 4398,
prompt_tokens_details: { cached_tokens: 4224, cache_write_tokens: 0 },
},
})
})
test("a count Cursor gave, or none to be had, is as before", async () => {
let asked = 0
const stepUsage = async () => (asked++, null)
// the turn ended with fewer calls than listed: its own count
const two = update(27, pb().varint(1, 2).done())
const end = { end: true, data: Buffer.from("{}") }
let out = await run([two, call, update(14, pb().varint(1, 900).varint(2, 3).varint(3, 800).done()), end], { stepUsage })
expect(asked).toBe(0)
expect(out.at(-1).usage.prompt_tokens_details.cached_tokens).toBe(800)
// no event in time: the guess
out = await run([listed, call], { stepUsage })
expect(asked).toBe(1)
expect(out.at(-1).usage.prompt_tokens).toBe(2395)
// a text turn never asks
out = await run([update(1, pb().str(1, "READY").done()), update(14, pb().varint(1, 10).varint(2, 1).done())], { stepUsage })
expect(asked).toBe(1)
expect(out.at(-1).stop).toBe("stop")
})
test("stepUsage finds the Run's event by its conversation and time, once", async () => {
Object.assign(STEP_WAIT, { first: 0, every: 10, until: 200 })
const since = Date.now()
const conv = "3ca0f6c1-b4a9-4991-9ae4-349bb3951caa"
const ev = (dt, c, t) => ({ timestamp: String(since + dt), model: "grok-4.7-medium-fast", conversationId: c, tokenUsage: t })
let shows = 2 // the event shows on the third ask
const asks = []
globalThis.fetch = async (url, init) => {
asks.push({ url: String(url), body: JSON.parse(init.body), auth: init.headers.Authorization })
return Response.json({
usageEventsDisplay: [
ev(2500, "another-conversation", { inputTokens: 9, outputTokens: 9 }),
ev(-30_000, conv, { inputTokens: 8, outputTokens: 8 }), // an earlier Run of it
...(shows-- > 0 ? [] : [ev(2600, conv, { inputTokens: 41, outputTokens: 261, cacheReadTokens: 4224 })]),
],
})
}
expect(await stepUsage("tok", conv, since)).toEqual({ input: 41, output: 261, cacheRead: 4224, cacheWrite: 0, reasoning: 0 })
expect(asks.length).toBe(3)
expect(asks[0].url).toBe("https://api2.cursor.sh/aiserver.v1.DashboardService/GetFilteredUsageEvents")
expect(asks[0].auth).toBe("Bearer tok")
expect(Number(asks[0].body.startDate)).toBeLessThan(since)
// counted once: another step of the conversation doesn't take it again
expect(await stepUsage("tok", conv, since)).toBe(null)
// an account that can't read the events isn't asked again
asks.length = 0
globalThis.fetch = async () => (asks.push(1), new Response("{}", { status: 403 }))
expect(await stepUsage("tok", conv, since)).toBe(null)
expect(asks.length).toBe(1)
})
// yetone/magpie#1053: a step's count can be owed to a later step of its
// conversation (ledger); latency.test.mjs runs whole conversations
test("an account that can't read its events is guessed as before, and owes nothing", async () => {
const { ledger, owed, unreadable, misses } = _internal
Object.assign(STEP_WAIT, { first: 0, every: 10, until: 60 })
const conv = "6b5568d9-0000-4000-8000-000000000003"
let asks = 0
globalThis.fetch = async () => (asks++, new Response("{}", { status: 403 }))
const out = await run([listed, call], { stepUsage: ledger("tok-403", conv, Date.now(), undefined, true).own })
expect(out.at(-1).usage.prompt_tokens).toBe(2395)
expect(owed.has(conv)).toBe(false)
// nor asked again for a while
expect(await ledger("tok-403", conv, Date.now(), undefined, true).own()).toBe(null)
expect(asks).toBe(1)
unreadable.clear()
misses.clear()
})
test("what a Run collected and never gave out goes to the conversation's next step", async () => {
const { ledger, owed } = _internal
Object.assign(STEP_WAIT, { first: 0, every: 10, until: 60 })
const conv = "6b5568d9-0000-4000-8000-000000000004"
const since = Date.now()
globalThis.fetch = async () =>
Response.json({ usageEventsDisplay: [{ timestamp: String(since + 4000), conversationId: conv, tokenUsage: { inputTokens: 2, outputTokens: 621, cacheReadTokens: 63457, cacheWriteTokens: 3081 } }] })
owed.set(conv, { since, n: 1, ready: since, carry: null })
// the Run collects the debt, then fails before its step is counted
const failed = ledger("tok", conv, Date.now(), undefined, true)
await new Promise((r) => setTimeout(r, 30))
failed.done()
expect(owed.get(conv).carry).toEqual({ input: 2, output: 621, cacheRead: 63457, cacheWrite: 3081, reasoning: 0 })
// the next step that ends the turn has it, once
const next = ledger("tok", conv, Date.now(), undefined, true)
expect(await next.owed(true)).toEqual({ input: 2, output: 621, cacheRead: 63457, cacheWrite: 3081, reasoning: 0 })
expect(owed.has(conv)).toBe(false)
expect(await ledger("tok", conv, Date.now(), undefined, true).owed(true)).toBe(null)
})