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:
@@ -73,7 +73,7 @@ const UPDATES = {
|
||||
1: "text", 2: "tool call started", 3: "tool call completed", 4: "thinking", 5: "thinking done",
|
||||
6: "user message", 7: "partial tool call", 8: "token count", 9: "summary", 10: "summary started",
|
||||
11: "summary done", 12: "shell output", 14: "turn end", 15: "tool call delta", 16: "step started",
|
||||
17: "step done", 27: "tool count",
|
||||
17: "step done", 27: "tool count", 31: "model call completed",
|
||||
}
|
||||
const MESSAGES = { 2: "tool call", 3: "checkpoint", 4: "blob request" }
|
||||
function kindOf(f) {
|
||||
@@ -872,6 +872,8 @@ export class NativeSessionPool {
|
||||
owner.checkpoint = null
|
||||
owner.execs.clear()
|
||||
owner.nativeCalls.clear()
|
||||
owner.stepUsages.clear()
|
||||
owner.lastStepUsage = null
|
||||
}
|
||||
async clear() {
|
||||
await Promise.all([...this.entries].map((e) => e.cancel("native owner removed")))
|
||||
@@ -906,6 +908,8 @@ class NativeSession {
|
||||
// latest one began: the last step's own share of the turn (#156)
|
||||
this.steps = 1
|
||||
this.stepSaid = 0
|
||||
this.stepUsages = new Map()
|
||||
this.lastStepUsage = null
|
||||
this.said = 0
|
||||
this.wrote = 0
|
||||
this.terminal = false
|
||||
@@ -1099,8 +1103,20 @@ class NativeSession {
|
||||
this.stepCalls.nextStep()
|
||||
this.quiet.nextStep(this.steps)
|
||||
this.stepSaid = this.said
|
||||
this.lastStepUsage = null
|
||||
if (estimate != null) this.stepEstimate = estimate
|
||||
}
|
||||
modelCallCompleted(fields) {
|
||||
const id = str(fields, 1)
|
||||
const entry = {
|
||||
usage: this.wire.usageOf(fields, 1),
|
||||
measuredInput: fields.some((f) => [2, 4, 5].includes(f.num)),
|
||||
measuredOutput: fields.some((f) => f.num === 3),
|
||||
hasReasoning: fields.some((f) => f.num === 6),
|
||||
}
|
||||
this.stepUsages.set(id || `step:${this.steps}:${this.stepUsages.size}`, entry)
|
||||
this.lastStepUsage = entry
|
||||
}
|
||||
// waitForBatch gives the step batchWait from its latest call, answered
|
||||
// call, count, text or heartbeat, not from its first. Cursor sends each
|
||||
// call once the model has written its arguments, so a step of many long
|
||||
@@ -1281,28 +1297,57 @@ class NativeSession {
|
||||
if (this.error || this.state === "complete") return
|
||||
if (!this.terminal) invalid("native Run ended before TurnEnded or a complete final answer", 502)
|
||||
const { usageOf } = this.wire
|
||||
const measuredInput = this.usageFields?.some((f) => [1, 3, 4].includes(f.num))
|
||||
const measuredOutput = this.usageFields?.some((f) => f.num === 2)
|
||||
const turnInput = this.usageFields?.some((f) => [1, 3, 4].includes(f.num))
|
||||
const turnOutput = this.usageFields?.some((f) => f.num === 2)
|
||||
const last = this.lastStepUsage
|
||||
const measuredInput = turnInput || !!last?.measuredInput
|
||||
const measuredOutput = turnOutput || !!last?.measuredOutput
|
||||
const u = usageOf(this.usageFields ?? [])
|
||||
if (!turnInput && last?.measuredInput) Object.assign(u, { input: last.usage.input, cacheRead: last.usage.cacheRead, cacheWrite: last.usage.cacheWrite })
|
||||
if (!turnOutput && last?.measuredOutput) Object.assign(u, { output: last.usage.output, reasoning: last.usage.reasoning })
|
||||
if (!measuredInput) u.input = this.estimate
|
||||
if (!measuredOutput) u.output = Math.floor((this.said + 3) / 4)
|
||||
// A turn of several model calls: TurnEndedUpdate counts them all, but a
|
||||
// client reads the last response's prompt as how full its context is
|
||||
// (Codex compacted a 61K context at 1.1M, #156). The last call keeps its
|
||||
// own share, the larger of its prompt estimate and the turn's mean per
|
||||
// call; the rest is billed as magpie_deferred.
|
||||
// (Codex compacted a 61K context at 1.1M, #156). When Cursor emitted
|
||||
// ModelCallCompletedUpdate (31) for the last step, it keeps its exact
|
||||
// count; otherwise it keeps the larger of its prompt estimate and the
|
||||
// turn's mean per call. The rest is billed as magpie_deferred.
|
||||
let earlier = null
|
||||
if (this.steps > 1) {
|
||||
const whole = u.input + u.cacheRead + u.cacheWrite
|
||||
const share = (total, own) => total > 0 ? Math.min(1, Math.max(own, total / this.steps) / total) : 0
|
||||
const fi = share(whole, measuredInput ? this.stepEstimate ?? 0 : whole)
|
||||
const fo = share(u.output, Math.floor((this.said - this.stepSaid + 3) / 4))
|
||||
const own = {
|
||||
input: Math.round(u.input * fi), cacheRead: Math.round(u.cacheRead * fi), cacheWrite: Math.round(u.cacheWrite * fi),
|
||||
output: Math.round(u.output * fo), reasoning: Math.round(u.reasoning * fo),
|
||||
if (last && (last.measuredInput || last.measuredOutput)) {
|
||||
const prev = [...this.stepUsages.values()].filter((e) => e !== last)
|
||||
const sum = (k) => prev.reduce((s, e) => s + e.usage[k], 0)
|
||||
const whole = u.input + u.cacheRead + u.cacheWrite
|
||||
const share = (total, own) => total > 0 ? Math.min(1, Math.max(own, total / this.steps) / total) : 0
|
||||
const fi = share(whole, turnInput ? this.stepEstimate ?? 0 : whole)
|
||||
const fo = share(u.output, Math.floor((this.said - this.stepSaid + 3) / 4))
|
||||
const own = {
|
||||
input: last.measuredInput ? last.usage.input : Math.round(u.input * fi),
|
||||
cacheRead: last.measuredInput ? last.usage.cacheRead : Math.round(u.cacheRead * fi),
|
||||
cacheWrite: last.measuredInput ? last.usage.cacheWrite : Math.round(u.cacheWrite * fi),
|
||||
output: last.measuredOutput ? last.usage.output : Math.round(u.output * fo),
|
||||
reasoning: last.hasReasoning ? last.usage.reasoning : last.measuredOutput ? Math.min(u.reasoning, last.usage.output) : Math.round(u.reasoning * fo),
|
||||
}
|
||||
earlier = {
|
||||
input: turnInput ? Math.max(0, u.input - own.input) : sum("input"),
|
||||
cacheRead: turnInput ? Math.max(0, u.cacheRead - own.cacheRead) : sum("cacheRead"),
|
||||
cacheWrite: turnInput ? Math.max(0, u.cacheWrite - own.cacheWrite) : sum("cacheWrite"),
|
||||
output: turnOutput ? Math.max(0, u.output - own.output) : sum("output"),
|
||||
}
|
||||
Object.assign(u, own)
|
||||
} else {
|
||||
const whole = u.input + u.cacheRead + u.cacheWrite
|
||||
const share = (total, own) => total > 0 ? Math.min(1, Math.max(own, total / this.steps) / total) : 0
|
||||
const fi = share(whole, measuredInput ? this.stepEstimate ?? 0 : whole)
|
||||
const fo = share(u.output, Math.floor((this.said - this.stepSaid + 3) / 4))
|
||||
const own = {
|
||||
input: Math.round(u.input * fi), cacheRead: Math.round(u.cacheRead * fi), cacheWrite: Math.round(u.cacheWrite * fi),
|
||||
output: Math.round(u.output * fo), reasoning: Math.round(u.reasoning * fo),
|
||||
}
|
||||
earlier = { input: u.input - own.input, cacheRead: u.cacheRead - own.cacheRead, cacheWrite: u.cacheWrite - own.cacheWrite, output: u.output - own.output }
|
||||
Object.assign(u, own)
|
||||
}
|
||||
earlier = { input: u.input - own.input, cacheRead: u.cacheRead - own.cacheRead, cacheWrite: u.cacheWrite - own.cacheWrite, output: u.output - own.output }
|
||||
Object.assign(u, own)
|
||||
}
|
||||
// What a later Run collected of this Run's event while this one waited on
|
||||
// results (handover) is in the turn's totals too: it comes off them, from
|
||||
@@ -1317,7 +1362,7 @@ class NativeSession {
|
||||
this.finalUsage = {
|
||||
prompt_tokens: prompt, completion_tokens: u.output, total_tokens: prompt + u.output,
|
||||
prompt_tokens_details: { cached_tokens: u.cacheRead, cache_write_tokens: u.cacheWrite },
|
||||
...(this.usageFields?.some((f) => f.num === 5) ? { completion_tokens_details: { reasoning_tokens: u.reasoning } } : {}),
|
||||
...(this.usageFields?.some((f) => f.num === 5) || last?.hasReasoning ? { completion_tokens_details: { reasoning_tokens: u.reasoning } } : {}),
|
||||
...(!measuredInput || !measuredOutput ? { magpie_native: { accounting: "estimated" } } : {}),
|
||||
}
|
||||
this.state = "finalizing"
|
||||
@@ -1373,6 +1418,8 @@ class NativeSession {
|
||||
} else if (u.num === 14) {
|
||||
if (!this.segment.content && !this.segment.calls.length) invalid("an empty reply (Cursor ended the turn without text or a tool call)", 502)
|
||||
this.terminalSeen(uf)
|
||||
} else if (u.num === 31) {
|
||||
this.modelCallCompleted(uf)
|
||||
} else if (u.num === 27) {
|
||||
this.listed(num(uf, 1))
|
||||
if (this.error) return
|
||||
|
||||
Reference in New Issue
Block a user