Commit Graph
4 Commits
Author SHA1 Message Date
nicegui 1eb9473f5b 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).
2026-11-06 20:54:23 +08:00
nicegui 7c49260ac1 cursor: isolate dashboard usage accounting by account (#131)
Cursor's dashboard usage accounting is kept per account, not per conversation.

claimed, owed and flights were keyed by conversation_id alone. When one
conversation's steps ran on two accounts (magpie moving a session between
Cursor accounts), account B's Run collected A's debt from B's own dashboard:
B's own event was counted as A's earlier Run's usage (magpie_deferred) and
B's turn-ending step polled for a debt its dashboard can't have. The keys
are now sha256(token) + conversation_id (usageKey), used by collect, owe,
ledger and the flights cleanup alike. A token change starts a new identity,
as misses/unreadable already were keyed.

Reviewed at 19bbdf8d7162735d7ddafd015d8dfadbb233c3bd, then merged main
(ca6415f, #130) into the branch at 74578e51e4214b75056746aa81ecc8fcba104ed2;
the only conflict was the _internal export line (both openH2 and usageKey
kept). Diff against main unchanged (168+/34-).
- full `bun test` (isolated HOME/TMPDIR): at 19bbdf8 1267 pass/0 fail; at
  74578e5 (with #130) 1292 pass/0 fail; `bun scripts/check.mjs` ok; PR CI
  green at 74578e5.
- own test: A owes one Run on conv, the next step runs on B whose dashboard
  shows B's own event. On main B's owed(true) returns B's event as A's debt
  and polls B's dashboard; here it returns null with no request, and A later
  collects its own event.
- broke by hand: usageKey back to conversation alone fails the three new
  stepusage tests.
Not tried: real Cursor accounts. A conversation moving accounts leaves the
first account's collected-but-unreported carry under that account (expires
after owedFor) instead of reporting it on the other account's row.
2026-10-15 04:15:03 +08:00
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
yetone 10bf7c4364 cursor 0.1.9: a step that calls tools reports what it used, the cache included (yetone/magpie#676) — Cursor tells a turn's usage (TurnEndedUpdate) only once the client has run the tools and sent their results in the same Run, and the caller runs them after the Run is closed, so a tool step had none: its input was estimated with no cache (2395 / 0 where Cursor counted 4265, 4224 of them cached). After the calls it sends only the checkpoint (no token details) and heartbeats, and a cancelled Run ends with an error and no usage. Every Run is counted in the dashboard's usage events (DashboardService/GetFilteredUsageEvents) by its conversation_id, input apart from the cache read and written, as TurnEndedUpdate does; one shows about 2.5 s after the Run closes. So a tool step with no TurnEnded closes the Run, then looks for its event (after 1.5 s, every 0.5 s, up to 6 s): its conversation's, no older than the Run, not already counted; else the estimate as before. stepusage.test.mjs fails on the old code and passes. Tried with the real Cursor account through a sandbox magpie's plugin host: grok-4.7-fast tool calls report 6186 in, 6144 cached, 121 out (0.1.8: 5057 estimated, 0 cached); streamed too. 2026-10-03 21:10:20 +08:00