The best way to control Claude Code, Codex and more from any computer or phone
fix(cursor): resume moved threads from their original session store (#34794)
Yash Singh committed
Nov 12, 2026 at 00:49 UTC
a18a458f2552ac5cd9dd561a834049b98b202ae9
5 files changed
+108
-12
packages/contracts/src/orchestrationV2.ts
+1
@@ -1061,6 +1061,7 @@ export const OrchestrationV2PendingBackgroundTask = kindUnionWithFallback(
1061
export type OrchestrationV2PendingBackgroundTask = typeof OrchestrationV2PendingBackgroundTask.Type;
1062
1063
export const OrchestrationV2ProviderThreadNativeMetadata = Schema.Struct({
1064
+ cursorStoreCwd: Schema.optional(TrimmedNonEmptyString),
1065
modelSelection: Schema.optional(ModelSelection),
1066
title: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
1067
updatedAt: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
packages/provider-cursor/src/server/CursorAgentSdk.test.ts
+38
@@ -3,6 +3,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
3
import { assert, describe, it } from "@effect/vitest";
4
import { ProviderSessionId, ThreadId } from "@t3tools/contracts";
5
import * as Effect from "effect/Effect";
6
+import * as FileSystem from "effect/FileSystem";
7
import * as Layer from "effect/Layer";
8
import * as Exit from "effect/Exit";
9
import * as Cause from "effect/Cause";
@@ -147,6 +148,43 @@ const layerTest = CursorAgentSdk.layer.pipe(
148
);
149
150
describe("CursorAgentSdkRunner", () => {
151
+ it.effect("resumes from the original store after the working directory changes", () =>
152
+ Effect.gen(function* () {
153
+ const fileSystem = yield* FileSystem.FileSystem;
154
+ const workspace = yield* fileSystem.makeTempDirectoryScoped({ prefix: "cursor-resume-" });
155
+ const loaders = yield* Effect.promise(() =>
156
+ vi.importActual<typeof import("./sdk.ts")>("./sdk.ts"),
157
+ );
158
+ const sdk = loaders.loadCursorSdk();
159
+ const store = new sdk.JsonlLocalAgentStore(workspace);
160
+ const storeCwd = `${workspace}/checkout`;
161
+ const cwd = `${workspace}/worktree`;
162
+ const platform = yield* Effect.promise(() =>
163
+ sdk.createAgentPlatform({ localStore: store, workspaceRef: storeCwd }),
164
+ );
165
+ const { agent } = yield* Effect.promise(() =>
166
+ platform.store.createAgent({ agentId: "agent-moved", workspaceRef: storeCwd }),
167
+ );
168
+ const runner = yield* CursorAgentSdk.makeCursorAgentSdkRunner(() => undefined).pipe(
169
+ Effect.provideService(CursorSdk.CursorSdk, {
170
+ load: loaders.loadCursorSdk,
171
+ loadSqlite: loaders.loadCursorSqlite,
172
+ }),
173
+ );
174
+ const session = yield* runner.open({
175
+ operation: "resume",
176
+ agentId: agent.agentId,
177
+ storeCwd,
178
+ options: { apiKey: "synthetic-cursor-key", local: { cwd, store } },
179
+ threadId: ThreadId.make("moved-workspace"),
180
+ providerSessionId: ProviderSessionId.make("moved-workspace"),
181
+ });
182
+ assert.equal(session.agentId, agent.agentId);
183
+ assert.deepStrictEqual(yield* session.listMessages, []);
184
+ yield* session.close;
185
+ }).pipe(Effect.provide(NodeServices.layer)),
186
+ );
187
+
188
it.effect("constructs without loading and reports first-use load failures as runner errors", () =>
189
Effect.gen(function* () {
190
const failure = new Error("missing SDK chunk");
packages/provider-cursor/src/server/CursorAgentSdk.ts
+44
-11
@@ -82,6 +82,7 @@ export const isCursorAgentSdkRunnerError = Schema.is(CursorAgentSdkRunnerError);
82
export interface CursorAgentSdkOpenInput {
83
readonly operation: "create" | "resume";
84
readonly agentId?: string;
85
+ readonly storeCwd?: string;
86
readonly options: AgentOptions;
87
readonly threadId: ThreadId;
88
readonly providerSessionId: OrchestrationV2ProviderSession["id"];
@@ -402,7 +403,7 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
403
404
return CursorAgentSdkRunner.of({
405
open: Effect.fn("CursorAgentSdkRunner.open")(function* (input) {
405
- const { Agent } = yield* Effect.try({
406
+ const { Agent, createAgentPlatform } = yield* Effect.try({
407
try: sdk.load,
408
catch: (cause) => runnerError(cause, `agent.${input.operation}`),
409
});
@@ -424,11 +425,30 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
425
},
426
});
427
428
+ // Cursor uses cwd for both execution and store lookup; a moved thread keeps its original store.
429
+ const storeCwd = input.storeCwd;
430
+ const storePlatform =
431
+ input.operation === "resume" &&
432
+ storeCwd !== undefined &&
433
+ storeCwd !== input.options.local?.cwd
434
+ ? yield* Effect.tryPromise({
435
+ try: () =>
436
+ createAgentPlatform({
437
+ workspaceRef: storeCwd,
438
+ ...(input.options.local?.store === undefined
439
+ ? {}
440
+ : { localStore: input.options.local.store }),
441
+ }),
442
+ catch: (cause) => runnerError(cause, "agent.resume"),
443
+ })
444
+ : undefined;
445
const agent = yield* Effect.tryPromise({
446
try: () =>
447
input.operation === "create"
448
? Agent.create(input.options)
431
- : Agent.resume(input.agentId!, input.options),
449
+ : storePlatform === undefined
450
+ ? Agent.resume(input.agentId!, input.options)
451
+ : storePlatform.resumeAgent(input.agentId!, input.options),
452
catch: (cause) => runnerError(cause, `agent.${input.operation}`),
453
});
454
@@ -525,10 +545,15 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
545
do {
546
const page = yield* Effect.tryPromise({
547
try: () =>
528
- Agent.listRuns(agent.agentId, {
529
- ...runOptions,
530
- ...(cursor === undefined ? {} : { cursor }),
531
- }),
548
+ storePlatform === undefined
549
+ ? Agent.listRuns(agent.agentId, {
550
+ ...runOptions,
551
+ ...(cursor === undefined ? {} : { cursor }),
552
+ })
553
+ : storePlatform.listRuns(
554
+ agent.agentId,
555
+ cursor === undefined ? {} : { cursor },
556
+ ),
557
catch: (cause) => runnerError(cause, "agent.listRuns"),
558
});
559
activeRun =
@@ -545,7 +570,10 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
570
payload: { type: "run.cancel", runId: activeRun.id },
571
});
572
yield* Effect.tryPromise({
548
- try: () => Agent.cancelRun(activeRun.id, runOptions),
573
+ try: () =>
574
+ storePlatform === undefined
575
+ ? Agent.cancelRun(activeRun.id, runOptions)
576
+ : storePlatform.cancelRun(activeRun.id),
577
catch: (cause) => runnerError(cause, "agent.cancelRun"),
578
});
579
return yield* startRun();
@@ -661,10 +689,15 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
689
Effect.andThen(
690
Effect.tryPromise({
691
try: () =>
664
- Agent.messages.list(agent.agentId, {
665
- runtime: "local",
666
- ...(cwd === undefined ? {} : { cwd }),
667
- }),
692
+ storePlatform === undefined
693
+ ? Agent.messages.list(agent.agentId, {
694
+ runtime: "local",
695
+ ...(cwd === undefined ? {} : { cwd }),
696
+ ...(input.options.local?.store === undefined
697
+ ? {}
698
+ : { store: input.options.local.store }),
699
+ })
700
+ : storePlatform.getAgentMessages(agent.agentId),
701
catch: (cause) => runnerError(cause, "agent.messages.list"),
702
}),
703
),
packages/provider-cursor/src/server/adapter.test.ts
+1
@@ -193,6 +193,7 @@ describe("CursorAdapterV2", () => {
193
modelSelection,
194
runtimePolicy,
195
});
196
+ assert.equal(providerThread.nativeMetadata?.cursorStoreCwd, workspace);
197
const now = yield* DateTime.now;
198
yield* runtime.startTurn({
199
threadId,
packages/provider-cursor/src/server/adapter.ts
+24
-1
@@ -247,6 +247,7 @@ function makeProviderThread(input: {
247
readonly appThreadId: OrchestrationV2ProviderThread["appThreadId"];
248
readonly providerSessionId: OrchestrationV2ProviderThread["providerSessionId"];
249
readonly nativeThreadId: string;
250
+ readonly storeCwd: string;
251
readonly ownerNodeId?: OrchestrationV2ProviderThread["ownerNodeId"];
252
readonly forkedFrom?: OrchestrationV2ProviderThread["forkedFrom"];
253
readonly now: DateTime.Utc;
@@ -267,6 +268,7 @@ function makeProviderThread(input: {
268
strength: "strong",
269
},
270
nativeConversationHeadRef: null,
271
+ nativeMetadata: { cursorStoreCwd: input.storeCwd },
272
status: "idle",
273
firstRunOrdinal: null,
274
lastRunOrdinal: null,
@@ -960,6 +962,7 @@ interface ActiveCursorTurn {
962
963
interface CursorLiveAgent {
964
readonly nativeThreadId: string;
965
+ readonly storeCwd: string;
966
readonly session: CursorAgentSdk.CursorAgentSdkSession;
967
}
968
@@ -2246,8 +2249,18 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
2249
readonly modelSelection: ModelSelection;
2250
readonly runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy;
2251
readonly agentId?: string;
2252
+ readonly storeCwd?: string | undefined;
2253
}) {
2254
const existing = yield* Ref.get(liveAgent);
2255
+ const cwd = openInput.runtimePolicy.cwd ?? process.cwd();
2256
+ const storeCwd =
2257
+ openInput.storeCwd ??
2258
+ (openInput.operation === "resume"
2259
+ ? existing !== null && existing.nativeThreadId === openInput.agentId
2260
+ ? existing.storeCwd
2261
+ : input.resumeFromSession?.cwd
2262
+ : undefined) ??
2263
+ cwd;
2264
if (
2265
existing !== null &&
2266
openInput.operation === "resume" &&
@@ -2282,6 +2295,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
2295
.open({
2296
operation: openInput.operation,
2297
...(openInput.agentId === undefined ? {} : { agentId: openInput.agentId }),
2298
+ storeCwd,
2299
options,
2300
threadId: openInput.threadId,
2301
providerSessionId: input.providerSessionId,
@@ -2289,6 +2303,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
2303
.pipe(Effect.mapError(classifyCursorModelRejection(openInput.modelSelection)));
2304
const next = {
2305
nativeThreadId: sdkSession.agentId,
2306
+ storeCwd,
2307
session: sdkSession,
2308
} satisfies CursorLiveAgent;
2309
yield* Ref.set(liveAgent, next);
@@ -2400,6 +2415,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
2415
const agent = yield* openAgent({
2416
operation: "resume",
2417
agentId,
2418
+ storeCwd: turnInput.providerThread.nativeMetadata?.cursorStoreCwd,
2419
threadId: turnInput.threadId,
2420
modelSelection: turnInput.modelSelection,
2421
runtimePolicy: turnInput.runtimePolicy,
@@ -2655,6 +2671,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
2671
appThreadId: threadInput.threadId,
2672
providerSessionId: input.providerSessionId,
2673
nativeThreadId: opened.nativeThreadId,
2674
+ storeCwd: opened.storeCwd,
2675
now,
2676
});
2677
},
@@ -2673,9 +2690,10 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
2690
resumeThread: Effect.fn("CursorAdapterV2.resumeThread")(
2691
function* (threadInput: { readonly providerThread: OrchestrationV2ProviderThread }) {
2692
const agentId = nativeThreadId(threadInput.providerThread);
2676
- yield* openAgent({
2693
+ const opened = yield* openAgent({
2694
operation: "resume",
2695
agentId,
2696
+ storeCwd: threadInput.providerThread.nativeMetadata?.cursorStoreCwd,
2697
threadId: threadInput.providerThread.appThreadId ?? input.threadId,
2698
modelSelection: input.modelSelection,
2699
runtimePolicy: input.runtimePolicy,
@@ -2683,6 +2701,10 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
2701
const now = yield* DateTime.now;
2702
return {
2703
...threadInput.providerThread,
2704
+ nativeMetadata: {
2705
+ ...threadInput.providerThread.nativeMetadata,
2706
+ cursorStoreCwd: opened.storeCwd,
2707
+ },
2708
providerSessionId: input.providerSessionId,
2709
status: "idle" as const,
2710
updatedAt: now,
@@ -2824,6 +2846,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
2846
const agent = yield* openAgent({
2847
operation: "resume",
2848
agentId: requestedAgentId,
2849
+ storeCwd: snapshotInput.providerThread.nativeMetadata?.cursorStoreCwd,
2850
threadId: snapshotInput.providerThread.appThreadId ?? input.threadId,
2851
modelSelection: input.modelSelection,
2852
runtimePolicy: input.runtimePolicy,