Skip to content

Commit a18a458

Browse files
authored
fix(cursor): resume moved threads from their original session store (#34794)
1 parent 62dc589 commit a18a458

5 files changed

Lines changed: 108 additions & 12 deletions

File tree

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,7 @@ export const OrchestrationV2PendingBackgroundTask = kindUnionWithFallback(
10611061
export type OrchestrationV2PendingBackgroundTask = typeof OrchestrationV2PendingBackgroundTask.Type;
10621062

10631063
export const OrchestrationV2ProviderThreadNativeMetadata = Schema.Struct({
1064+
cursorStoreCwd: Schema.optional(TrimmedNonEmptyString),
10641065
modelSelection: Schema.optional(ModelSelection),
10651066
title: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),
10661067
updatedAt: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)),

‎packages/provider-cursor/src/server/CursorAgentSdk.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
33
import { assert, describe, it } from "@effect/vitest";
44
import { ProviderSessionId, ThreadId } from "@t3tools/contracts";
55
import * as Effect from "effect/Effect";
6+
import * as FileSystem from "effect/FileSystem";
67
import * as Layer from "effect/Layer";
78
import * as Exit from "effect/Exit";
89
import * as Cause from "effect/Cause";
@@ -147,6 +148,43 @@ const layerTest = CursorAgentSdk.layer.pipe(
147148
);
148149

149150
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+
150188
it.effect("constructs without loading and reports first-use load failures as runner errors", () =>
151189
Effect.gen(function* () {
152190
const failure = new Error("missing SDK chunk");

‎packages/provider-cursor/src/server/CursorAgentSdk.ts‎

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export const isCursorAgentSdkRunnerError = Schema.is(CursorAgentSdkRunnerError);
8282
export interface CursorAgentSdkOpenInput {
8383
readonly operation: "create" | "resume";
8484
readonly agentId?: string;
85+
readonly storeCwd?: string;
8586
readonly options: AgentOptions;
8687
readonly threadId: ThreadId;
8788
readonly providerSessionId: OrchestrationV2ProviderSession["id"];
@@ -402,7 +403,7 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
402403

403404
return CursorAgentSdkRunner.of({
404405
open: Effect.fn("CursorAgentSdkRunner.open")(function* (input) {
405-
const { Agent } = yield* Effect.try({
406+
const { Agent, createAgentPlatform } = yield* Effect.try({
406407
try: sdk.load,
407408
catch: (cause) => runnerError(cause, `agent.${input.operation}`),
408409
});
@@ -424,11 +425,30 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
424425
},
425426
});
426427

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;
427445
const agent = yield* Effect.tryPromise({
428446
try: () =>
429447
input.operation === "create"
430448
? 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),
432452
catch: (cause) => runnerError(cause, `agent.${input.operation}`),
433453
});
434454

@@ -525,10 +545,15 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
525545
do {
526546
const page = yield* Effect.tryPromise({
527547
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+
),
532557
catch: (cause) => runnerError(cause, "agent.listRuns"),
533558
});
534559
activeRun =
@@ -545,7 +570,10 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
545570
payload: { type: "run.cancel", runId: activeRun.id },
546571
});
547572
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),
549577
catch: (cause) => runnerError(cause, "agent.cancelRun"),
550578
});
551579
return yield* startRun();
@@ -661,10 +689,15 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
661689
Effect.andThen(
662690
Effect.tryPromise({
663691
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),
668701
catch: (cause) => runnerError(cause, "agent.messages.list"),
669702
}),
670703
),

‎packages/provider-cursor/src/server/adapter.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ describe("CursorAdapterV2", () => {
193193
modelSelection,
194194
runtimePolicy,
195195
});
196+
assert.equal(providerThread.nativeMetadata?.cursorStoreCwd, workspace);
196197
const now = yield* DateTime.now;
197198
yield* runtime.startTurn({
198199
threadId,

‎packages/provider-cursor/src/server/adapter.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ function makeProviderThread(input: {
247247
readonly appThreadId: OrchestrationV2ProviderThread["appThreadId"];
248248
readonly providerSessionId: OrchestrationV2ProviderThread["providerSessionId"];
249249
readonly nativeThreadId: string;
250+
readonly storeCwd: string;
250251
readonly ownerNodeId?: OrchestrationV2ProviderThread["ownerNodeId"];
251252
readonly forkedFrom?: OrchestrationV2ProviderThread["forkedFrom"];
252253
readonly now: DateTime.Utc;
@@ -267,6 +268,7 @@ function makeProviderThread(input: {
267268
strength: "strong",
268269
},
269270
nativeConversationHeadRef: null,
271+
nativeMetadata: { cursorStoreCwd: input.storeCwd },
270272
status: "idle",
271273
firstRunOrdinal: null,
272274
lastRunOrdinal: null,
@@ -960,6 +962,7 @@ interface ActiveCursorTurn {
960962

961963
interface CursorLiveAgent {
962964
readonly nativeThreadId: string;
965+
readonly storeCwd: string;
963966
readonly session: CursorAgentSdk.CursorAgentSdkSession;
964967
}
965968

@@ -2246,8 +2249,18 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
22462249
readonly modelSelection: ModelSelection;
22472250
readonly runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy;
22482251
readonly agentId?: string;
2252+
readonly storeCwd?: string | undefined;
22492253
}) {
22502254
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;
22512264
if (
22522265
existing !== null &&
22532266
openInput.operation === "resume" &&
@@ -2282,13 +2295,15 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
22822295
.open({
22832296
operation: openInput.operation,
22842297
...(openInput.agentId === undefined ? {} : { agentId: openInput.agentId }),
2298+
storeCwd,
22852299
options,
22862300
threadId: openInput.threadId,
22872301
providerSessionId: input.providerSessionId,
22882302
})
22892303
.pipe(Effect.mapError(classifyCursorModelRejection(openInput.modelSelection)));
22902304
const next = {
22912305
nativeThreadId: sdkSession.agentId,
2306+
storeCwd,
22922307
session: sdkSession,
22932308
} satisfies CursorLiveAgent;
22942309
yield* Ref.set(liveAgent, next);
@@ -2400,6 +2415,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
24002415
const agent = yield* openAgent({
24012416
operation: "resume",
24022417
agentId,
2418+
storeCwd: turnInput.providerThread.nativeMetadata?.cursorStoreCwd,
24032419
threadId: turnInput.threadId,
24042420
modelSelection: turnInput.modelSelection,
24052421
runtimePolicy: turnInput.runtimePolicy,
@@ -2655,6 +2671,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
26552671
appThreadId: threadInput.threadId,
26562672
providerSessionId: input.providerSessionId,
26572673
nativeThreadId: opened.nativeThreadId,
2674+
storeCwd: opened.storeCwd,
26582675
now,
26592676
});
26602677
},
@@ -2673,16 +2690,21 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
26732690
resumeThread: Effect.fn("CursorAdapterV2.resumeThread")(
26742691
function* (threadInput: { readonly providerThread: OrchestrationV2ProviderThread }) {
26752692
const agentId = nativeThreadId(threadInput.providerThread);
2676-
yield* openAgent({
2693+
const opened = yield* openAgent({
26772694
operation: "resume",
26782695
agentId,
2696+
storeCwd: threadInput.providerThread.nativeMetadata?.cursorStoreCwd,
26792697
threadId: threadInput.providerThread.appThreadId ?? input.threadId,
26802698
modelSelection: input.modelSelection,
26812699
runtimePolicy: input.runtimePolicy,
26822700
});
26832701
const now = yield* DateTime.now;
26842702
return {
26852703
...threadInput.providerThread,
2704+
nativeMetadata: {
2705+
...threadInput.providerThread.nativeMetadata,
2706+
cursorStoreCwd: opened.storeCwd,
2707+
},
26862708
providerSessionId: input.providerSessionId,
26872709
status: "idle" as const,
26882710
updatedAt: now,
@@ -2824,6 +2846,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
28242846
const agent = yield* openAgent({
28252847
operation: "resume",
28262848
agentId: requestedAgentId,
2849+
storeCwd: snapshotInput.providerThread.nativeMetadata?.cursorStoreCwd,
28272850
threadId: snapshotInput.providerThread.appThreadId ?? input.threadId,
28282851
modelSelection: input.modelSelection,
28292852
runtimePolicy: input.runtimePolicy,

0 commit comments

Comments
 (0)