diff --git a/packages/cursor/README.md b/packages/cursor/README.md index 4b8b070..73f3373 100644 --- a/packages/cursor/README.md +++ b/packages/cursor/README.md @@ -153,8 +153,11 @@ Cursor completes by itself, which it writes and completes in the step but never starts on the client or sends to it; those count toward it too. When the count comes after the step's calls, a checkpoint whose state waits on exactly the calls the caller was given also ends the step (#550); a checkpoint between -calls after a count sent ahead of them does not seal the batch early. The -step's calls are the rest. +calls after a count sent ahead of them does not seal the batch early. A +partial or started MCP call can also precede the count while its exec follows +the checkpoint. Until that call arrives as an exec or is completed by Cursor, +the checkpoint cannot end the step merely because it lists only calls already +sent. The step's calls are the rest. These are retention limits. A missing, expired, restarted, busy, incompatible, or full owner uses the original history reconstruction and stateless decoder diff --git a/packages/cursor/index.mjs b/packages/cursor/index.mjs index 56b1734..3f43f58 100644 --- a/packages/cursor/index.mjs +++ b/packages/cursor/index.mjs @@ -3374,8 +3374,10 @@ async function* decode(stream, { send, blobs, tools, estimate, close, stepUsage, // conversation_checkpoint_update: Cursor's state. Once the step // has its count after the calls, a state waiting on just the // calls handed to the caller is a step Cursor has finished: what - // more the count says will not come (plugins#550) - if (listed && step.listedAfter && calls > 0 && pendingCalls(m.data, fields) === calls) { + // more the count says will not come (plugins#550). An announced + // MCP call without an exec or completion must still be awaited, + // even if this checkpoint omits it. + if (listed && step.listedAfter && !step.pendingMcp && calls > 0 && pendingCalls(m.data, fields) === calls) { note?.("waited on") yield await settled() return diff --git a/packages/cursor/native-session.mjs b/packages/cursor/native-session.mjs index 5cfdfe6..97b2b3f 100644 --- a/packages/cursor/native-session.mjs +++ b/packages/cursor/native-session.mjs @@ -566,6 +566,7 @@ export class StepCalls { this.started = new Set() this.execs = new Set() // the short strings of each exec's args: its tool_call_id among them this.partial = new Set() // calls the model is writing in this step + this.mcp = new Set() // MCP calls announced by a partial or started update this.byCursor = new Set() this.listedAfter = false // whether tool_requests_listed came after the latest call/update } @@ -578,6 +579,8 @@ export class StepCalls { this.listedAfter = false const id = str(uf, 1) if (!id) return false + const call = field(uf, 2) + if (n !== 3 && call && this.parse(call.data).some((f) => f.num === 15)) this.mcp.add(id) if (n === 2) this.started.add(id) if (n === 7) this.partial.add(id) if (n !== 3) return false @@ -607,6 +610,13 @@ export class StepCalls { get completed() { return this.byCursor.size } + // Announced MCP calls still awaiting an exec or a Cursor-side completion. + // A checkpoint can omit them while their execs are still on the way. + get pendingMcp() { + let n = 0 + for (const id of this.mcp) if (!this.execs.has(id) && !this.byCursor.has(id)) n++ + return n + } // 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 @@ -618,6 +628,7 @@ export class StepCalls { // nextStep starts the next step's count nextStep() { this.partial.clear() + this.mcp.clear() this.byCursor.clear() this.listedAfter = false } @@ -1233,10 +1244,11 @@ class NativeSession { // When the count was sent before or between the step's calls, a mid-batch // checkpoint after an early call has only the calls sent so far in // pending_tool_calls and must not seal the batch early before the - // remaining calls arrive. + // remaining calls arrive. Even a count after a partial/started MCP update + // can precede its exec: a checkpoint omitting that call must wait for it. waitedOn(data) { const s = this.segment - if (this.terminal || this.state === "awaiting-results" || s.closed || s.expected == null || !s.calls.length || !this.stepCalls.listedAfter) return + if (this.terminal || this.state === "awaiting-results" || s.closed || s.expected == null || !s.calls.length || !this.stepCalls.listedAfter || this.stepCalls.pendingMcp) return if (pendingCalls(data) !== s.calls.length) return s.waitedOn = true this.sealBatch() diff --git a/packages/cursor/package.json b/packages/cursor/package.json index 13597a4..ee5206a 100644 --- a/packages/cursor/package.json +++ b/packages/cursor/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-cursor-auth", - "version": "0.2.46", + "version": "0.2.47", "description": "Cursor subscriptions (Pro, Pro+, Ultra, Teams) in OpenCode and magpie, through the API cursor-agent talks to", "type": "module", "main": "./opencode.mjs", diff --git a/packages/cursor/stall-550.test.mjs b/packages/cursor/stall-550.test.mjs index 0c401f6..38c0709 100644 --- a/packages/cursor/stall-550.test.mjs +++ b/packages/cursor/stall-550.test.mjs @@ -193,6 +193,36 @@ const until = async (predicate) => { } const nativeFrames = (opts) => wire(opts).map((b) => ({ data: envelope(b).subarray(5) })) +// Two real Hydrogen failures: the count follows a partial second call, +// but its exec arrives after a checkpoint waiting on only the first. +const partialBatch = (started) => [ + callUpdate(7, "c3", MCP), + callUpdate(2, "c3", MCP), + bin(2, execMessage("c3")), + callUpdate(7, "c4", MCP), + ...(started ? [callUpdate(2, "c4", MCP)] : []), + bin(1, bin(27, uint(1, 2))), + bin(3, state(['{"call":"c3"}'])), + ...(!started ? [callUpdate(2, "c4", MCP)] : []), + bin(2, message(uint(1, 8), text(15, "exec-c4"), bin(11, mcpArgs("c4")))), +] + +for (const started of [false, true]) test(`native: a checkpoint after the count waits for a ${started ? "started" : "partial"} call's exec`, async () => { + const run = owner() + run.pump(open(partialBatch(started).map((data) => ({ data })))) + await until(() => run.error || run.state === "awaiting-results") + expect(run.error).toBeUndefined() + expect(run.segment.events.filter((e) => e.tool).map((e) => e.tool.name)).toEqual(["read_file", "read_file"]) + expect(run.segment.events.at(-1).stop).toBe("tool_calls") +}) + +for (const started of [false, true]) test(`stateless: a checkpoint after the count waits for a ${started ? "started" : "partial"} call's exec`, async () => { + const { how, events } = await stateless(partialBatch(started)) + 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") +}) + // pending: a checkpoint that waits on more than came, so only the calls // Cursor completed can seal it for (const [name, opts] of [["call updates", { pending: ["c3", "c9"] }], ["the checkpoint alone", { updates: false }]]) test(`native: the reporter's step seals on its one call, by ${name} (#550)`, async () => {