The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 21:00 UTC
fix(cursor): recover restarted threads with older runs (#24540)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Yash Singh <saiansh2525@gmail.com>
Julius Marminge committed
Nov 11, 2026 at 20:27 UTC
8c15be97ddb594183d8972db73e4c643a1eaf9e0
2 files changed
+49
-17
packages/provider-cursor/src/server/CursorAgentSdk.test.ts
+30
-10
@@ -1,3 +1,4 @@
1
+import type { ListRunsOptions } from "@cursor/sdk";
2
import * as NodeServices from "@effect/platform-node/NodeServices";
3
import { assert, describe, it } from "@effect/vitest";
4
import { ProviderSessionId, ThreadId } from "@t3tools/contracts";
@@ -15,7 +16,8 @@ const cursorSdkMock = vi.hoisted(() => {
16
const closeExecutionOrder: Array<string> = [];
17
const runOperations: Array<string> = [];
18
let agentCloseFailure: unknown;
18
- let staleRunActive = false;
19
+ // The local store lists runs oldest first, like the real SDK.
20
+ let storedRuns: Array<{ readonly id: string; readonly status: string }> = [];
21
const runWait = vi.fn(async () => ({
22
id: "run-cursor-agent-sdk-test",
23
requestId: "request-cursor-agent-sdk-test",
@@ -38,7 +40,7 @@ const cursorSdkMock = vi.hoisted(() => {
40
options: { readonly onDelta?: (input: { readonly update: unknown }) => Promise<void> },
41
) => {
42
runOperations.push("run.start");
41
- if (staleRunActive) {
43
+ if (storedRuns.some((run) => run.status === "running")) {
44
throw new Error("Agent agent-cursor-agent-sdk-test already has active run");
45
}
46
await options.onDelta?.({
@@ -66,15 +68,21 @@ const cursorSdkMock = vi.hoisted(() => {
68
send,
69
close: agentClose,
70
}));
69
- const listRuns = vi.fn(async () => {
71
+ const listRuns = vi.fn(async (_agentId: string, options?: ListRunsOptions) => {
72
runOperations.push("agent.listRuns");
73
+ // Mirrors the SDK's opaque cursor pagination with its default page size of 50.
74
+ const offset = options?.cursor === undefined ? 0 : Number(options.cursor);
75
+ const end = offset + (options?.limit ?? 50);
76
return {
72
- items: staleRunActive ? [{ id: "run-stale", status: "running" }] : [],
77
+ items: storedRuns.slice(offset, end),
78
+ ...(end < storedRuns.length ? { nextCursor: String(end) } : {}),
79
};
80
});
75
- const cancelRun = vi.fn(async () => {
81
+ const cancelRun = vi.fn(async (runId: string) => {
82
runOperations.push("agent.cancelRun");
77
- staleRunActive = false;
83
+ storedRuns = storedRuns.map((run) =>
84
+ run.id === runId ? { ...run, status: "cancelled" } : run,
85
+ );
86
});
87
88
return {
@@ -92,8 +100,8 @@ const cursorSdkMock = vi.hoisted(() => {
100
setAgentCloseFailure: (failure: unknown) => {
101
agentCloseFailure = failure;
102
},
95
- setStaleRunActive: (active: boolean) => {
96
- staleRunActive = active;
103
+ setStoredRuns: (runs: Array<{ readonly id: string; readonly status: string }>) => {
104
+ storedRuns = runs;
105
},
106
};
107
});
@@ -179,7 +187,13 @@ describe("CursorAgentSdkRunner", () => {
187
cursorSdkMock.runOperations.length = 0;
188
cursorSdkMock.listRuns.mockClear();
189
cursorSdkMock.cancelRun.mockClear();
182
- cursorSdkMock.setStaleRunActive(true);
190
+ cursorSdkMock.setStoredRuns([
191
+ ...Array.from({ length: 50 }, (_, index) => ({
192
+ id: `run-terminal-${index}`,
193
+ status: index === 0 ? "cancelled" : "finished",
194
+ })),
195
+ { id: "run-stale", status: "running" },
196
+ ]);
197
198
const runner = yield* CursorAgentSdk.CursorAgentSdkRunner;
199
const session = yield* runner.open({
@@ -198,12 +212,18 @@ describe("CursorAgentSdkRunner", () => {
212
assert.deepStrictEqual(cursorSdkMock.runOperations, [
213
"run.start",
214
"agent.listRuns",
215
+ "agent.listRuns",
216
"agent.cancelRun",
217
"run.start",
218
]);
219
assert.deepStrictEqual(cursorSdkMock.listRuns.mock.calls[0], [
220
"agent-cursor-agent-sdk-test",
206
- { runtime: "local", cwd: process.cwd(), limit: 1 },
221
+ { runtime: "local", cwd: process.cwd() },
222
+ ]);
223
+ assert.strictEqual(cursorSdkMock.listRuns.mock.calls.length, 2);
224
+ assert.deepStrictEqual(cursorSdkMock.listRuns.mock.calls[1], [
225
+ "agent-cursor-agent-sdk-test",
226
+ { runtime: "local", cwd: process.cwd(), cursor: "50" },
227
]);
228
assert.deepStrictEqual(cursorSdkMock.cancelRun.mock.calls[0], [
229
"run-stale",
packages/provider-cursor/src/server/CursorAgentSdk.ts
+19
-7
@@ -2,6 +2,7 @@ import type {
2
AgentMessage,
3
AgentOptions,
4
InteractionUpdate,
5
+ Run,
6
RunResult,
7
SDKUserMessage,
8
SendOptions,
@@ -517,13 +518,24 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
518
(error) =>
519
Effect.gen(function* () {
520
abandonedRunRecoveryAvailable = false;
520
- const latestRun = yield* Effect.tryPromise({
521
- try: () => Agent.listRuns(agent.agentId, { ...runOptions, limit: 1 }),
522
- catch: (cause) => runnerError(cause, "agent.listRuns"),
523
- });
524
- const activeRun = latestRun.items.find(
525
- (candidate) => candidate.status === "running",
526
- );
521
+ // Local stores list runs oldest first, so the active run is on the
522
+ // last page and every page has to be read to reach it.
523
+ let activeRun: Run | undefined;
524
+ let cursor: string | undefined;
525
+ do {
526
+ const page = yield* Effect.tryPromise({
527
+ try: () =>
528
+ Agent.listRuns(agent.agentId, {
529
+ ...runOptions,
530
+ ...(cursor === undefined ? {} : { cursor }),
531
+ }),
532
+ catch: (cause) => runnerError(cause, "agent.listRuns"),
533
+ });
534
+ activeRun =
535
+ page.items.findLast((candidate) => candidate.status === "running") ??
536
+ activeRun;
537
+ cursor = page.nextCursor;
538
+ } while (cursor !== undefined);
539
if (activeRun === undefined) {
540
return yield* error;
541
}