cursor 0.2.36: a stateless step whose calls came hands them over when no count follows (#550)

Windsland52's fifth wire trace (2026-10-31 19:44, cursor-grok-4.6-xhigh,
stateless, why "historical native calls"): thinking, text, then two MCP
calls 43 ms apart (partial, started, exec each), no tool count (update
27), no checkpoint, and 24 heartbeats until quiet ended the Run at 240 s.
The stateless decode only ends a step on the count, a checkpoint or EOF,
so it waited out quiet; the retry of the same turn stays stateless, so a
native stall cost two 4-minute waits. Cursor's own client never waits on
toolRequestsListed (b70725c).

Now decode reads with a deadline while the step has calls, no count and
no call still being written (a #7 neither started, exec'd nor completed:
Cursor's own call still running). Once countWait (60 s, NATIVE_LIMITS)
passes since Cursor's last frame that isn't a heartbeat, the calls that
came go to the caller as tool_calls, noted "no count" in the trace.
Heartbeats never move the deadline. The native path is unchanged: there
the step can't end without the count, and its batchWait stays as it was.
60 s and not batchWait's 30 s: a long call is written with heartbeats
only (#377), and a cut there costs a re-written call, not a failed turn.

Run (mote, bun test in packages/cursor at this head): 380 pass, 0 fail.
New tests in stall-550.test.mjs build the fifth trace's frame kinds in
order (bytes made up where the trace has sizes only): the two calls go to
the caller; heartbeats every 20 ms don't move a 300 ms deadline; a call
still being written holds the step; a count inside the wait ends on the
count as before.
Broken by hand: with the deadline never armed, the first two go red
(open after 2 s); without the still-writing guard, the third goes red.
Not tried: a real Cursor account, or a step whose count comes later than
60 s after its last call (none in the reporter's traces).
This commit is contained in:
yetone
2026-11-02 05:06:49 +08:00
parent 04f3608d49
commit 24cd0b9123
4 changed files with 133 additions and 3 deletions
+45 -2
View File
@@ -2853,7 +2853,7 @@ async function runOnce({ tok, base, id, tools, conv, convID, signal, maxMode, pa
// decode follows the server's messages into the answer's pieces until the // decode follows the server's messages into the answer's pieces until the
// turn ends, or the model has made its tool calls. // turn ends, or the model has made its tool calls.
async function* decode(stream, { send, blobs, tools, estimate, close, stepUsage, owedUsage, note }) { async function* decode(stream, { send, blobs, tools, estimate, close, stepUsage, owedUsage, note, countWait = NATIVE_LIMITS.countWait }) {
let calls = 0 let calls = 0
// answered: the step's calls answered here (a refusal, the MCP tool // answered: the step's calls answered here (a refusal, the MCP tool
// list), which Cursor's count of the step's calls takes in too (#142) // list), which Cursor's count of the step's calls takes in too (#142)
@@ -2937,8 +2937,48 @@ async function* decode(stream, { send, blobs, tools, estimate, close, stepUsage,
return finish(owedUsage ? await owedUsage(calls === 0).catch(() => null) : null) return finish(owedUsage ? await owedUsage(calls === 0).catch(() => null) : null)
} }
const closeExec = (id) => send(pb().bytes(5, pb().bytes(1, pb().varint(1, id))).done()) const closeExec = (id) => send(pb().bytes(5, pb().bytes(1, pb().varint(1, id))).done())
// A step whose calls came and whose count never does: Cursor sent the
// calls and then only heartbeats (plugins#550, the fifth trace: two
// calls, no count, 240 s of heartbeats until quiet). Once countWait has
// passed since Cursor's last frame that wasn't a heartbeat, with no call
// still being written, the calls that came are the step's. Heartbeats
// never move the deadline: they are all such a Run sends.
const it = (stream[Symbol.asyncIterator] ?? stream[Symbol.iterator]).call(stream)
let heardAt = Date.now()
const read = async () => {
const next = Promise.resolve(it.next())
if (!calls || listed || step.writing) return next
next.catch(() => {}) // a read left behind by the cut fails unheard
let timer
const cut = new Promise((r) => {
timer = setTimeout(() => r({ cut: true }), Math.max(0, heardAt + countWait - Date.now()))
timer.unref?.()
})
try {
return await Promise.race([next, cut])
} finally {
clearTimeout(timer)
}
}
const beat = (data) => {
try {
const ms = fields(data)
return ms.length > 0 && ms.every((m) => m.num === 1 && fields(m.data).every((u) => u.num === 13))
} catch {
return false
}
}
try { try {
for await (const f of stream) { for (;;) {
const r = await read()
if (r.cut) {
note?.("no count")
yield await settled()
return
}
if (r.done) break
const f = r.value
if (f.end || !beat(f.data)) heardAt = Date.now()
if (f.end) { if (f.end) {
const e = failure(200, f.data) const e = failure(200, f.data)
if (e.status < 200 || e.status > 299) yield { error: e } if (e.status < 200 || e.status > 299) yield { error: e }
@@ -3072,6 +3112,9 @@ async function* decode(stream, { send, blobs, tools, estimate, close, stepUsage,
return return
} }
yield { error: { status: 502, message: "the reply broke off: " + (e?.message ?? e) } } yield { error: { status: 502, message: "the reply broke off: " + (e?.message ?? e) } }
} finally {
// not waited for: after a cut, the read left behind ends with the Run
Promise.resolve(it.return?.()).catch(() => {})
} }
} }
+14
View File
@@ -9,6 +9,12 @@ export const NATIVE_LIMITS = {
resultWait: 120_000, resultWait: 120_000,
lifetime: 15 * 60_000, lifetime: 15 * 60_000,
batchWait: 30_000, batchWait: 30_000,
// How long a stateless step whose calls came waits for Cursor's count of
// them (update 27) with nothing but heartbeats before it hands the calls
// that came to the caller (plugins#550). A count comes within ms of the
// step's last call; a Run that sent calls and then only heartbeats has
// stopped, and the caller's next request goes on from the calls' results.
countWait: 60_000,
drain: 6000, drain: 6000,
replayWait: 120_000, replayWait: 120_000,
blobBytes: 64 << 20, blobBytes: 64 << 20,
@@ -511,6 +517,14 @@ export class StepCalls {
get completed() { get completed() {
return this.byCursor.size return this.byCursor.size
} }
// writing is how many calls the step wrote (7) that are neither started
// on the client, sent as an exec, nor completed by Cursor: a call of
// Cursor's own still running, or one still to be sent
get writing() {
let n = 0
for (const id of this.partial) if (!this.started.has(id) && !this.execs.has(id) && !this.byCursor.has(id)) n++
return n
}
// nextStep starts the next step's count // nextStep starts the next step's count
nextStep() { nextStep() {
this.partial.clear() this.partial.clear()
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@magpie-community/opencode-cursor-auth", "name": "@magpie-community/opencode-cursor-auth",
"version": "0.2.35", "version": "0.2.36",
"description": "Cursor subscriptions (Pro, Pro+, Ultra, Teams) in OpenCode and magpie, through the API cursor-agent talks to", "description": "Cursor subscriptions (Pro, Pro+, Ultra, Teams) in OpenCode and magpie, through the API cursor-agent talks to",
"type": "module", "type": "module",
"main": "./index.mjs", "main": "./index.mjs",
+73
View File
@@ -210,3 +210,76 @@ test("native: a step still short of its count fails batchWait, naming the calls
await until(() => run.error) await until(() => run.error)
expect(run.error.message).toMatch(/^Cursor did not complete its declared native tool batch: 1 of 4 calls came, 0 answered here, 2 completed by Cursor/) expect(run.error.message).toMatch(/^Cursor did not complete its declared native tool batch: 1 of 4 calls came, 0 answered here, 2 completed by Cursor/)
}) })
// plugins#550's fifth trace (Windsland52, 2026-10-31 19:44, stateless,
// cursor-grok-4.6-xhigh): 19 blob requests, thinking and token counts, the
// thinking's end, text, then two MCP calls (partial, started, exec each)
// 43 ms apart, no tool count, and only heartbeats until quiet ended it
// after 240 s. The kinds and order are the trace's; the bytes are made up
// where it has only sizes.
const TRACE5 = [
"H", ...Array(19).fill("get"),
...Array(40).fill(0).flatMap((_, i) => (i % 3 === 0 ? ["T"] : ["T", "8"])), "D",
...Array(26).fill(0).flatMap((_, i) => (i % 3 === 0 ? ["x", "8"] : ["x"])),
"8", "partial:c1", "started:c1", "exec:c1", "8", "partial:c2", "started:c2", "exec:c2",
...Array(24).fill("H"),
]
const wire5 = (extra = []) => [...TRACE5, ...extra].map((t, i) => {
const [what, id] = t.split(":")
if (what === "partial") return callUpdate(7, id, id === "c9" ? SEARCH : MCP)
if (what === "started") return callUpdate(2, id, MCP)
return body(t, i, {})
})
// a stream that goes on with a heartbeat every few ms after its frames, as
// Cursor's did every 10 s, until it is closed
function beating(list, every = 10) {
let i = 0, closed = false
return {
[Symbol.asyncIterator]() { return this },
next: () => closed ? Promise.resolve({ done: true }) : i < list.length
? Promise.resolve({ value: { data: list[i++] }, done: false })
: new Promise((r) => setTimeout(() => r(closed ? { done: true } : { value: { data: bin(1, bin(13, Buffer.alloc(0))) }, done: false }), every)),
return: () => ((closed = true), Promise.resolve({ done: true })),
}
}
async function run5(stream, countWait, ms = 2000) {
const events = [], notes = []
const done = (async () => {
for await (const e of decode(stream, { send() {}, blobs: new Map(), tools: readFile, estimate: 1, note: (n) => notes.push(n), countWait })) {
events.push(e)
if (e.stop || e.error) return
}
})()
const timer = new Promise((r) => setTimeout(() => r("open"), ms))
return { how: await Promise.race([done.then(() => "ended"), timer]), events, notes }
}
test("stateless: the fifth trace's two calls with no count go to the caller once only heartbeats follow (#550)", async () => {
const { how, events, notes } = await run5(beating(wire5()), 200)
expect(how).toBe("ended")
expect(events.filter((e) => e.tool).map((e) => e.tool.name)).toEqual(["read_file", "read_file"])
expect(events.at(-1).stop).toBe("tool_calls")
expect(notes).toEqual(["no count"])
})
test("stateless: the count wait is from Cursor's last frame that isn't a heartbeat (#550)", async () => {
const started = Date.now()
const { how } = await run5(beating(wire5(), 20), 300)
expect(how).toBe("ended")
// heartbeats every 20 ms never moved the 300 ms deadline
expect(Date.now() - started).toBeLessThan(1500)
})
test("stateless: a call Cursor is still running holds the step past the count wait (#550)", async () => {
// c9 written and neither started nor completed: Cursor's own, running
const { how } = await run5(beating(wire5(["partial:c9"])), 100, 800)
expect(how).toBe("open")
})
test("stateless: a count that comes inside the wait ends the step on the count, as before (#550)", async () => {
const list = wire5().slice(0, -24)
const { how, events, notes } = await run5(open([...list, bin(1, bin(27, uint(1, 2)))].map((data) => ({ data }))), 60_000)
expect(how).toBe("ended")
expect(events.filter((e) => e.tool)).toHaveLength(2)
expect(notes).toEqual([])
})