From 4f3eddc25641f303fb1e881168993cb5700ac8df Mon Sep 17 00:00:00 2001 From: Gavin Wang Date: Tue, 3 Nov 2026 19:45:28 +0800 Subject: [PATCH] cursor 0.2.40: a new user turn whose old native calls are all answered gets a retained Run (#672) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A new user turn whose earlier native calls (call_cursor_…) all have tool results now gets a retained Run of its own, with those calls imported as history the way the stateless path imports them. Any call still pending, a request ending in tool results with no live owner, or a tool attachment last still falls back as before; the owner-busy check is unchanged. Nothing is submitted to an old exec: a user message last has no trailing handles. By ooboqoo (#672). Run at the reviewed head 5b4dcf1 on mote: - packages/cursor `bun test`: 394 pass, 0 fail - `bun scripts/check.mjs`: ok Broken by hand: with origin/main's native-session.mjs and the PR's tests, "terminal old handles do not wedge a new user turn" and the new admission test both fail (2 fail, 168 pass in those two files). Not tried: a real Cursor account (the author reports ~270 requests over a day, all 200 or client-cancelled 499); Cursor dashboard usage or caching. --- packages/cursor/README.md | 5 ++++- packages/cursor/native-e2e.test.mjs | 6 +++++- packages/cursor/native-session.mjs | 10 +++++++++- packages/cursor/native-session.test.mjs | 15 +++++++++++++++ packages/cursor/package.json | 2 +- 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/packages/cursor/README.md b/packages/cursor/README.md index c305961..9e63831 100644 --- a/packages/cursor/README.md +++ b/packages/cursor/README.md @@ -99,7 +99,10 @@ plugin's `fetch` answers them on Cursor's agent API: Tool results can continue a retained Run in the same plugin process. The owner uses the same session/cache key and first user message as `conversation_id`, so -Task subagents sharing their parent's session can run independently. Retention +Task subagents sharing their parent's session can run independently. A new +user turn whose earlier native calls all have their results gets a retained Run +of its own, with those calls as imported history; while any of them is still +pending, the turn goes stateless. Retention waits at most 120 seconds for a result batch and 15 minutes for a Run's total lifetime. A continuation arriving with less than 10 minutes of that lifetime left rebuilds from supplied history on a fresh Run before submitting results diff --git a/packages/cursor/native-e2e.test.mjs b/packages/cursor/native-e2e.test.mjs index e05d4ee..301d18b 100644 --- a/packages/cursor/native-e2e.test.mjs +++ b/packages/cursor/native-e2e.test.mjs @@ -536,7 +536,11 @@ test("native retention: terminal old handles do not wedge a new user turn", asyn const final = await a.ask(account, next) expect(final.status).toBe(200) next.messages.push(choice(final).message, { role: "user", content: "new turn after terminal owner" }) - rebuilt(await a.ask(account, next), "finished-tool") + // every old call has its result, so the new turn gets a native owner of its own + const fresh = await a.ask(account, next) + expect(fresh.status).toBe(200) + expect(choice(fresh).message.content).toContain("rebuilt:finished-tool") + expect(fresh.diagnostic).toMatch(/^same-run;/) expect(a.runs).toHaveLength(2) expect(a.results).toHaveLength(1) }) diff --git a/packages/cursor/native-session.mjs b/packages/cursor/native-session.mjs index 00681d2..e920bee 100644 --- a/packages/cursor/native-session.mjs +++ b/packages/cursor/native-session.mjs @@ -796,7 +796,15 @@ export class NativeSessionPool { } // Pending ancestors, new input, and terminal old handles are ordinary // imported history, even while a parent Run is still awaiting its results. - if (chat.messages.some((m) => m?.tool_calls?.some((c) => HANDLE.test(c?.id ?? "")))) return fallback("historical native calls") + // A fresh user turn whose old native calls all have results imports them + // into a NEW owner; those results are never submitted to their old exec. + const historical = chat.messages.flatMap((m) => (m?.tool_calls ?? []).filter((c) => HANDLE.test(c?.id ?? ""))) + if (historical.length) { + const answered = new Set(chat.messages.filter((m) => m?.role === "tool").map((m) => m.tool_call_id)) + const last = chat.messages.at(-1) + const freshTurn = last?.role === "user" && !toolAttachment(last) + if (!freshTurn || historical.some((c) => !answered.has(c.id))) return fallback("historical native calls") + } if (selector && this.selectors.has(account + ":" + selector)) return fallback("owner busy") return { account, selector, controls, selected: !!selector, chat } } diff --git a/packages/cursor/native-session.test.mjs b/packages/cursor/native-session.test.mjs index 6e34e8e..451394a 100644 --- a/packages/cursor/native-session.test.mjs +++ b/packages/cursor/native-session.test.mjs @@ -141,6 +141,21 @@ test("native admission: competing initial requests cannot overwrite a session re expect(pool.selectors.get(run.account + ":" + run.selector)).toBe(run) }) +test("native admission: a fresh user turn whose old native calls all have results gets an owner; any pending call falls back", () => { + const pool = new NativeSessionPool() + pools.push(pool) + const id = "call_cursor_abcdef_0_0" + const call = { role: "assistant", content: "", tool_calls: [{ id, type: "function", function: { name: "audit_echo", arguments: "{}" } }] } + const result = { role: "tool", tool_call_id: id, content: "done" } + const answered = [{ role: "user", content: "first" }, call, result, { role: "assistant", content: "finished" }, { role: "user", content: "next" }] + expect(pool.prepare("synthetic-token", { messages: answered }, "synthetic-session", registered).fallback).toBeUndefined() + // the old call has no result: it is pending, so the request is imported history + const pending = answered.filter((m) => m !== result) + expect(pool.prepare("synthetic-token", { messages: pending }, "synthetic-session", registered).fallback).toBe("historical native calls") + // a tool result with no owner here is not a fresh user turn + expect(pool.prepare("synthetic-token", { messages: answered.slice(0, 3) }, "synthetic-session", registered).fallback).toBe("owner unavailable") +}) + test("native admission: completed replay entries are bounded; active entries are preserved", () => { const { pool, run } = owner({}, { perAccount: 2, perProcess: 2 }) run.terminalSeen(nativeFields(message(uint(1, 0), uint(2, 0)))) diff --git a/packages/cursor/package.json b/packages/cursor/package.json index d9a8fef..0bacb47 100644 --- a/packages/cursor/package.json +++ b/packages/cursor/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-cursor-auth", - "version": "0.2.39", + "version": "0.2.40", "description": "Cursor subscriptions (Pro, Pro+, Ultra, Teams) in OpenCode and magpie, through the API cursor-agent talks to", "type": "module", "main": "./opencode.mjs",