diff --git a/packages/cursor/index.mjs b/packages/cursor/index.mjs index 50e6e14..7e3dc92 100644 --- a/packages/cursor/index.mjs +++ b/packages/cursor/index.mjs @@ -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 // 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 // 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) @@ -2937,8 +2937,48 @@ async function* decode(stream, { send, blobs, tools, estimate, close, stepUsage, 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()) + // 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 { - 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) { const e = failure(200, f.data) 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 } 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(() => {}) } } diff --git a/packages/cursor/native-session.mjs b/packages/cursor/native-session.mjs index eb6b86a..af6e46f 100644 --- a/packages/cursor/native-session.mjs +++ b/packages/cursor/native-session.mjs @@ -9,6 +9,12 @@ export const NATIVE_LIMITS = { resultWait: 120_000, lifetime: 15 * 60_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, replayWait: 120_000, blobBytes: 64 << 20, @@ -511,6 +517,14 @@ export class StepCalls { get completed() { 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() { this.partial.clear() diff --git a/packages/cursor/package.json b/packages/cursor/package.json index 132dd2a..be05939 100644 --- a/packages/cursor/package.json +++ b/packages/cursor/package.json @@ -1,6 +1,6 @@ { "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", "type": "module", "main": "./index.mjs", diff --git a/packages/cursor/stall-550.test.mjs b/packages/cursor/stall-550.test.mjs index 057c01e..2a77c0b 100644 --- a/packages/cursor/stall-550.test.mjs +++ b/packages/cursor/stall-550.test.mjs @@ -210,3 +210,76 @@ test("native: a step still short of its count fails batchWait, naming the calls 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/) }) + +// 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([]) +})