Files
plugins/packages/cursor/deferred-usage.test.mjs
T
geoqiao 4aae1ce7b9 cursor 0.2.11: a step's usage is its own; what it collected of earlier steps is magpie_deferred (#101)
A tool step whose usage event shows after its wait reports 0, and the
conversation's next step collects it. That step used to add it to its own
prompt_tokens, which clients read as context occupancy: pi saw 366940 tokens
(181270 + 185670) on a 300k window and compacted, though 185670 were in use.

Now a step's standard usage fields are its own Run's only. What it collected
of earlier steps goes in usage.magpie_deferred (prompt_tokens,
completion_tokens, cached_tokens, cache_write_tokens), which magpie bills with
the step (yetone/magpie PR) and clients ignore. ledger() also splits collected
events by timestamp: an event stamped from the Run's start on (less 1 s) is
the Run's own; earlier ones and carried counts are deferred. Before, a late
event of an earlier Run could land in a step's own count through gather().

Verified: bun test on packages/cursor, 88 pass (86 before, plus
deferred-usage.test.mjs); bun scripts/check.mjs. With the old index.mjs the
new test fails with Expected 185670, Received 366940. Against real Cursor
(composer-2.5, STEP_WAIT shortened to force late events, 2 rounds) the
deferred totals matched the dashboard's usage events token for token.
The repo-wide bun test has 2 failures in packages/trae that fail the same on
9074723 without this change.
2026-10-13 14:51:26 +08:00

85 lines
3.9 KiB
JavaScript

// A tool step whose usage event shows only after its wait says 0 used,
// and the conversation's next step collects it. That step's own count is
// its usage; what it collected of the earlier step goes apart, as
// magpie_deferred: a client that reads prompt_tokens as how full the
// context is (pi, OpenCode) once saw an earlier step's prompt added to
// its own, the context twice its size (366940 = 181270 + 185670 on a
// 300k window, where 185670 was used).
import "./nonet.mjs"
import { afterEach, expect, test } from "bun:test"
import { _internal } from "./index.mjs"
const { pb, decode, ledger, owed, misses, STEP_WAIT } = _internal
const real = globalThis.fetch
const wait = { ...STEP_WAIT }
afterEach(() => {
globalThis.fetch = real
Object.assign(STEP_WAIT, wait)
owed.clear()
misses.clear()
})
const update = (num, body) => ({ data: pb().bytes(1, pb().bytes(num, body)).done() })
const listed = update(27, pb().varint(1, 1).done())
const call = {
data: pb()
.bytes(2, pb().varint(1, 7).str(15, "x1").bytes(11, pb().str(1, "magpie-bash").str(3, "call_1").str(5, "bash").done()).done())
.done(),
}
async function step(book) {
const out = []
for await (const p of decode([listed, call], { send() {}, blobs: new Map(), tools: [], estimate: 1, stepUsage: book.own, owedUsage: book.owed })) out.push(p)
book.done()
return out.at(-1).usage
}
// the report's two tool steps of one pi session
const E1 = { inputTokens: 2, outputTokens: 1913, cacheReadTokens: 177533, cacheWriteTokens: 3735 } // 181270
const E2 = { inputTokens: 4, outputTokens: 2240, cacheReadTokens: 181266, cacheWriteTokens: 4400 } // 185670
const prompt = (e) => e.inputTokens + e.cacheReadTokens + e.cacheWriteTokens
test("a step's prompt_tokens is its own Run's; what it collected of an earlier step is magpie_deferred", async () => {
Object.assign(STEP_WAIT, { first: 0, every: 10, until: 40, owedAt: 0 })
const conv = "8a462df8-0000-4000-8000-000000000001"
// step 1's Run began a while ago, as a tool step's did before its tool ran
const since = Date.now() - 30_000
const shown = []
globalThis.fetch = async () => Response.json({ usageEventsDisplay: shown.map((e) => ({ ...e })) })
// step 1: its event shows only after its wait, so it is owed and says 0
const u1 = await step(ledger("tok", conv, since, undefined, true))
expect(u1.prompt_tokens).toBe(0)
expect(u1.magpie_deferred).toBeUndefined()
// step 1's late event, then step 2's own
// Cursor stamps a Run's event as the Run begins, a second or two in
shown.push({ timestamp: String(since + 2000), conversationId: conv, tokenUsage: E1 })
const since2 = Date.now()
shown.push({ timestamp: String(since2 + 2000), conversationId: conv, tokenUsage: E2 })
const u2 = await step(ledger("tok", conv, since2, undefined, true))
// the context as it is: step 2's prompt alone (was 366940)
expect(u2.prompt_tokens).toBe(prompt(E2))
expect(u2.total_tokens).toBe(prompt(E2) + E2.outputTokens)
expect(u2.prompt_tokens_details).toEqual({ cached_tokens: E2.cacheReadTokens, cache_write_tokens: E2.cacheWriteTokens })
// and step 1's, which magpie still bills, apart
expect(u2.magpie_deferred).toEqual({
prompt_tokens: prompt(E1),
completion_tokens: E1.outputTokens,
cached_tokens: E1.cacheReadTokens,
cache_write_tokens: E1.cacheWriteTokens,
})
// together, what Cursor counted
expect(u1.prompt_tokens + u2.prompt_tokens + u2.magpie_deferred.prompt_tokens).toBe(prompt(E1) + prompt(E2))
})
test("a step that owed nothing has no magpie_deferred", async () => {
Object.assign(STEP_WAIT, { first: 0, every: 10, until: 200, owedAt: 0 })
const conv = "8a462df8-0000-4000-8000-000000000002"
const since = Date.now()
globalThis.fetch = async () => Response.json({ usageEventsDisplay: [{ timestamp: String(since + 2000), conversationId: conv, tokenUsage: E1 }] })
const u = await step(ledger("tok", conv, since, undefined, true))
expect(u.prompt_tokens).toBe(prompt(E1))
expect(u.magpie_deferred).toBeUndefined()
})