cursor 0.2.40: a new user turn whose old native calls are all answered gets a retained Run (#672)

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.
This commit is contained in:
Gavin Wang
2026-11-03 19:45:28 +08:00
committed by GitHub
parent 1d2edbbf2e
commit 4f3eddc256
5 changed files with 34 additions and 4 deletions
+4 -1
View File
@@ -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
+5 -1
View File
@@ -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)
})
+9 -1
View File
@@ -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 }
}
+15
View File
@@ -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))))
+1 -1
View File
@@ -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",