cursor 0.2.43: read per-step ModelCallCompletedUpdate usage (#796)

Asks AgentService/Run for emit_model_call_usage (AgentRunRequest field 37) and reads InteractionUpdate 31 (ModelCallCompletedUpdate: model_call_id, then input, output, cache read, cache write and reasoning tokens). These field numbers are checked against cursor-agent 2026.11.05-86047df's bundle. A multi-step native turn's final step reports that call's exact counts. The rest of TurnEnded's totals goes to usage.magpie_deferred, or the earlier calls' #31 sum when TurnEnded has no counts. A stateless step with a #31 before it seals takes that count and doesn't poll the dashboard. Without #31, both paths are as before.

Reviewed at d2c49f3b on mote (mote-run, HOME=$(mktemp -d)):
- bun scripts/check.mjs cursor: ok.
- bun test packages/cursor: 406 pass, 0 fail. native-e2e alone 3 times: 145 pass each.
- Probes: TurnEnded with no counts and a #31 per step, and only the final step's #31. Both give own 61000/700 and deferred 105000/1400, which add up to the turn.
- Broken by hand, red each time: no field 37; native ignoring #31; #31 read unshifted; stateless ignoring #31; stateless still polling after #31.
- Not red: dropping the lastStepUsage reset in nextStep. That test gap is filed as a follow-up.

Not tried: a real Cursor account (whether api2 sends #31, and in what order).
This commit is contained in:
nicegui
2026-11-06 20:54:23 +08:00
committed by GitHub
parent 28108ddeec
commit 1eb9473f5b
6 changed files with 130 additions and 29 deletions
+34
View File
@@ -149,6 +149,7 @@ async function agent(options = {}) {
const advance = () => {
const expected = phase === 0 && options.parallel ? [firstID, 8] : [phase === 0 ? firstID : 8]
if (!expected.every((id) => closed.has(id))) return
if (options.stepUsages?.[phase]) send(update(31, options.stepUsages[phase]))
if (phase === 0 && !options.parallel && !options.finalAfterFirst) {
marker = received.get(firstID).content[0].text
phase = 1
@@ -170,6 +171,7 @@ async function agent(options = {}) {
if (options.halfFinal) { send(update(1, text(1, "half said"))); return }
send(update(4, text(1, "native final thinking")))
send(update(1, text(1, marker)))
if (options.stepUsages?.[2]) send(update(31, options.stepUsages[2]))
const usage = options.turnUsage ?? (options.zeroUsage ? message(uint(1, 0), uint(2, 0), uint(5, 0)) : message(uint(1, 23), uint(2, 7), uint(3, 10), uint(4, 0), uint(5, 4)))
send(update(14, usage))
state(false, true) // late state after TurnEnded must still be retained
@@ -312,6 +314,7 @@ test("native accounting: a multi-step turn's final step reports its own share, t
const final = await a.ask(account, continueWith(next, second.body, ["two"]))
expect(final.status).toBe(200)
expect(a.runs).toHaveLength(1)
expect(number(a.runs[0], 37)).toBe(1) // emit_model_call_usage
const u = final.body.usage, d = u.magpie_deferred
// three model calls: none is more than half the turn
expect(u.prompt_tokens).toBeLessThan(1105934 / 2)
@@ -324,6 +327,37 @@ test("native accounting: a multi-step turn's final step reports its own share, t
expect(u.completion_tokens_details.reasoning_tokens).toBeLessThanOrEqual(u.completion_tokens)
})
test("native accounting: ModelCallCompletedUpdate (31) gives the final step its exact context and defers the rest (#156)", async () => {
// Three model calls: 50K -> 55K -> 61K context; TurnEndedUpdate sums all three (166K).
const stepUsages = [
message(text(1, "mc-1"), uint(2, 50000), uint(3, 600), uint(4, 25000), uint(5, 24990), uint(6, 200)),
message(text(1, "mc-2"), uint(2, 55000), uint(3, 800), uint(4, 50000), uint(5, 4990), uint(6, 300)),
message(text(1, "mc-3"), uint(2, 61000), uint(3, 700), uint(4, 55000), uint(5, 5980), uint(6, 400)),
]
const turnUsage = message(uint(1, 166000), uint(2, 2100), uint(3, 130000), uint(4, 35960), uint(5, 900))
const a = await agent({ stepUsages, turnUsage }), account = auth(), chat = initial()
const first = await a.ask(account, chat)
const next = continueWith(chat, first.body, ["one"])
const second = await a.ask(account, next)
const final = await a.ask(account, continueWith(next, second.body, ["two"]))
expect(final.status).toBe(200)
expect(a.runs).toHaveLength(1)
expect(number(a.runs[0], 37)).toBe(1)
expect(final.body.usage).toEqual({
prompt_tokens: 61000,
completion_tokens: 700,
total_tokens: 61700,
prompt_tokens_details: { cached_tokens: 55000, cache_write_tokens: 5980 },
completion_tokens_details: { reasoning_tokens: 400 },
magpie_deferred: {
prompt_tokens: 105000,
completion_tokens: 1400,
cached_tokens: 75000,
cache_write_tokens: 29980,
},
})
})
test("native retention: Task parent awaiting results permits a child sharing session with different first user", async () => {
const a = await agent({ name: "Task" }), account = auth(), parent = initial({ tools: [{ type: "function", function: { name: "Task", parameters: { type: "object" } } }] })
const first = await a.ask(account, parent)