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
+10
View File
@@ -58,6 +58,16 @@ test("a count Cursor gave, or none to be had, is as before", async () => {
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)
// ModelCallCompletedUpdate (31) before the step seals gives the step its count without polling the dashboard
out = await run([update(31, pb().str(1, "mc-1").varint(2, 950).varint(3, 12).varint(4, 900).varint(5, 40).varint(6, 5).done()), listed, call], { stepUsage })
expect(asked).toBe(0)
expect(out.at(-1).usage).toEqual({
prompt_tokens: 950,
completion_tokens: 12,
total_tokens: 962,
prompt_tokens_details: { cached_tokens: 900, cache_write_tokens: 40 },
completion_tokens_details: { reasoning_tokens: 5 },
})
// no event in time: the guess
out = await run([listed, call], { stepUsage })
expect(asked).toBe(1)