cursor: a multi-step turn's last step reports its own share of usage, and a step's calls get batchWait from the latest one (#156 erzay, #155 midmirror)

#156: a native Run of N tool steps ended with TurnEndedUpdate's totals as
the final step's prompt_tokens (1,105,934 on erzay's ~21-step turn, where
the real context was ~61K), and Codex compacted at once. The final step
now keeps its last model call's share, max(that call's chars/4 prompt
estimate, turn total / model calls), split proportionally across input,
cache read and cache write; the rest goes into usage.magpie_deferred
(merged with any settled stateless debt), so magpie still bills the whole
turn. On erzay's numbers that is ~52.7K reported, ~1.05M deferred.

#155: waitForBatch armed once, at a step's first call, so a step whose
calls arrive one by one as the model writes them (13 calls, 9 long-arg)
failed 502 "did not complete its declared native tool batch" ~30s after
the first call even though it never paused that long. The deadline now
restarts on every call, answered call, count and text/reasoning frame.

Run at this commit: packages/cursor `bun test` (isolated HOME) 266 pass,
0 fail, three times; `bun scripts/check.mjs` ok. The three new tests fail
on 4e64f26's source (prompt 1105934; sealed step failed at 300ms; stall
failed 303ms after the first call instead of after the latest).
This commit is contained in:
yetone
2026-10-16 15:09:25 +08:00
parent 423e78e49c
commit c7c6498d85
5 changed files with 119 additions and 5 deletions
+27 -1
View File
@@ -148,7 +148,7 @@ async function agent(options = {}) {
if (options.stallFinal) return
send(update(4, text(1, "native final thinking")))
send(update(1, text(1, marker)))
const usage = 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))
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
if (!options.noEnd) delay(() => stream.end(end()), 55)
@@ -276,6 +276,32 @@ test("native retention: healthy two external roundtrips use one Run", async () =
expect(a.errors).toEqual([])
})
// #156 (erzay, Codex on cursor/claude-opus-5-5@1m): a turn of about 20 tool
// steps ended with TurnEndedUpdate's totals, 1,105,934 prompt (1,035,734 read
// from the cache, 70,156 written) as the final step's prompt_tokens; Codex
// read that as its context and compacted a 61K one. The last call keeps its
// own share; the rest is still billed, as magpie_deferred.
test("native accounting: a multi-step turn's final step reports its own share, the rest deferred (#156)", async () => {
const turnUsage = message(uint(1, 1105934), uint(2, 2100), uint(3, 1035734), uint(4, 70156), uint(5, 900))
const a = await agent({ 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)
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)
expect(u.prompt_tokens).toBeGreaterThan(0)
expect(u.total_tokens).toBe(u.prompt_tokens + u.completion_tokens)
expect(u.prompt_tokens + d.prompt_tokens).toBe(1105934)
expect(u.prompt_tokens_details.cached_tokens + d.cached_tokens).toBe(1035734)
expect(u.prompt_tokens_details.cache_write_tokens + d.cache_write_tokens).toBe(70156)
expect(u.completion_tokens + d.completion_tokens).toBe(2100)
expect(u.completion_tokens_details.reasoning_tokens).toBeLessThanOrEqual(u.completion_tokens)
})
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)