Skip to content

Commit 61ddd98

Browse files
committed
fix(provider-cursor): Cursor text generation no longer leaks SQLite file handles on every call
1 parent 8d199b4 commit 61ddd98

6 files changed

Lines changed: 57 additions & 4 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ vi.mock("./sdk.ts", () => ({
104104
},
105105
},
106106
createAgentPlatform: vi.fn(),
107+
SqliteLocalAgentStore: { open: vi.fn() },
107108
}));
108109

109110
const layerTest = CursorAgentSdk.layer.pipe(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ writeFileSync(
2525
);
2626
registerHooks({
2727
resolve(specifier, context, next) {
28-
if (specifier === "@cursor/sdk") {
28+
if (specifier === "@cursor/sdk" || specifier === "@cursor/sdk/sqlite") {
2929
return { url: pathToFileURL(stub).href, shortCircuit: true };
3030
}
3131
return next(specifier, context);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as Context from "effect/Context";
33
import * as Effect from "effect/Effect";
44
import * as Layer from "effect/Layer";
55

6-
import { Agent, createAgentPlatform } from "./sdk.ts";
6+
import { Agent, createAgentPlatform, SqliteLocalAgentStore } from "./sdk.ts";
77

88
/**
99
* The Cursor Agent SDK runs in this process and spawns a shell for tool
@@ -65,6 +65,7 @@ export class CursorSdk extends Context.Service<
6565
{
6666
readonly Agent: typeof Agent;
6767
readonly createAgentPlatform: typeof createAgentPlatform;
68+
readonly SqliteLocalAgentStore: typeof SqliteLocalAgentStore;
6869
}
6970
>()("@t3tools/provider-cursor/server/CursorSdk") {}
7071

@@ -79,5 +80,5 @@ export const layer = Layer.effect(
7980
Effect.sync(() => {
8081
process.off("unhandledRejection", onUnhandledRejection);
8182
}),
82-
).pipe(Effect.as(CursorSdk.of({ Agent, createAgentPlatform }))),
83+
).pipe(Effect.as(CursorSdk.of({ Agent, createAgentPlatform, SqliteLocalAgentStore }))),
8384
);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ export const {
1212
CursorSdkError,
1313
InMemoryCredentialStore,
1414
} = requireCursorSdk("@cursor/sdk") as typeof import("@cursor/sdk");
15+
export const { SqliteLocalAgentStore } = requireCursorSdk(
16+
"@cursor/sdk/sqlite",
17+
) as typeof import("@cursor/sdk/sqlite");

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const cursorSdkMock = vi.hoisted(() => ({
1919
send: vi.fn<(prompt: string) => Promise<unknown>>(),
2020
close: vi.fn(),
2121
cancel: vi.fn(async () => {}),
22+
storeOpen: vi.fn<(options: unknown) => Promise<unknown>>(),
23+
storeDispose: vi.fn(async () => {}),
2224
prompt: vi.fn<(prompt: string, options: unknown) => Promise<RunResult>>(async () => ({
2325
id: "run-cursor-text-generation-test",
2426
status: "finished",
@@ -30,6 +32,7 @@ const cursorSdkMock = vi.hoisted(() => ({
3032
vi.mock("./sdk.ts", () => ({
3133
Agent: { create: cursorSdkMock.create },
3234
createAgentPlatform: vi.fn(),
35+
SqliteLocalAgentStore: { open: cursorSdkMock.storeOpen },
3336
}));
3437

3538
let hasCustomPolicy = false;
@@ -62,6 +65,9 @@ beforeEach(() => {
6265
});
6366
cursorSdkMock.close.mockClear();
6467
cursorSdkMock.cancel.mockClear();
68+
cursorSdkMock.storeDispose.mockClear();
69+
cursorSdkMock.storeOpen.mockReset();
70+
cursorSdkMock.storeOpen.mockResolvedValue({ dispose: cursorSdkMock.storeDispose });
6571
cursorSdkMock.prompt.mockReset();
6672
cursorSdkMock.prompt.mockResolvedValue({
6773
id: "run-cursor-text-generation-test",
@@ -139,6 +145,7 @@ describe("CursorTextGeneration", () => {
139145
},
140146
local: {
141147
cwd: "/isolated-text-generation",
148+
store: { dispose: cursorSdkMock.storeDispose },
142149
autoReview: false,
143150
sandboxOptions: { enabled: true },
144151
settingSources: [],
@@ -148,6 +155,37 @@ describe("CursorTextGeneration", () => {
148155
}).pipe(Effect.provide(layerTest)),
149156
);
150157

158+
it.effect("disposes the agent store after the agent, whether the run succeeds or fails", () =>
159+
Effect.gen(function* () {
160+
const textGeneration = yield* makeCursorTextGeneration(cursorSettings, {
161+
CURSOR_API_KEY: "test-cursor-key",
162+
});
163+
const input = {
164+
cwd: process.cwd(),
165+
branch: "feature/cursor-store",
166+
stagedSummary: "M README.md",
167+
stagedPatch: "diff --git a/README.md b/README.md",
168+
modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "composer-2"),
169+
};
170+
171+
yield* textGeneration.generateCommitMessage(input);
172+
expect(cursorSdkMock.storeOpen).toHaveBeenCalledWith(
173+
expect.objectContaining({ workspaceRef: "/isolated-text-generation" }),
174+
);
175+
expect(cursorSdkMock.storeDispose).toHaveBeenCalledTimes(1);
176+
expect(cursorSdkMock.close.mock.invocationCallOrder[0]).toBeLessThan(
177+
cursorSdkMock.storeDispose.mock.invocationCallOrder[0]!,
178+
);
179+
180+
cursorSdkMock.prompt.mockResolvedValueOnce({
181+
id: "run-cursor-text-generation-test",
182+
status: "error",
183+
});
184+
yield* Effect.flip(textGeneration.generateCommitMessage(input));
185+
expect(cursorSdkMock.storeDispose).toHaveBeenCalledTimes(2);
186+
}).pipe(Effect.provide(layerTest)),
187+
);
188+
151189
it.effect("continues in the temp directory when the SDK cannot sandbox", () =>
152190
Effect.gen(function* () {
153191
cursorSdkMock.create.mockImplementationOnce(async () => {
@@ -175,6 +213,7 @@ describe("CursorTextGeneration", () => {
175213
expect(cursorSdkMock.create.mock.calls[1]?.[0]).toMatchObject({
176214
local: {
177215
cwd: "/isolated-text-generation",
216+
store: { dispose: cursorSdkMock.storeDispose },
178217
autoReview: false,
179218
sandboxOptions: { enabled: false },
180219
settingSources: [],

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const makeCursorTextGeneration = Effect.fn("makeCursorTextGeneration")(fu
5858
withAccess?: CursorAuth["withAccess"],
5959
) {
6060
const fs = yield* FileSystem.FileSystem;
61-
const { Agent } = yield* CursorSdk.CursorSdk;
61+
const { Agent, SqliteLocalAgentStore } = yield* CursorSdk.CursorSdk;
6262
const resolvedEnvironment = environment ?? process.env;
6363

6464
const resolveCursorApiKey = (operation: TextGenerationOperations.Operation) =>
@@ -98,12 +98,21 @@ export const makeCursorTextGeneration = Effect.fn("makeCursorTextGeneration")(fu
9898
});
9999
}
100100
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cursor-text-" });
101+
// The SDK keeps SQLite handles open after `agent.close()` and only releases
102+
// them from a store the caller disposes. Its state lives outside `cwd` so the
103+
// agent's workspace stays empty.
104+
const stateRoot = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cursor-text-state-" });
105+
const store = yield* Effect.acquireRelease(
106+
Effect.tryPromise(() => SqliteLocalAgentStore.open({ workspaceRef: cwd, stateRoot })),
107+
(store) => Effect.tryPromise(() => store.dispose()).pipe(Effect.ignore({ log: true })),
108+
);
101109
const agentOptions = {
102110
apiKey,
103111
mode: "plan",
104112
model: cursorSdkModelSelection(modelSelection),
105113
local: {
106114
cwd,
115+
store,
107116
autoReview: false,
108117
sandboxOptions: { enabled: true },
109118
settingSources: [],

0 commit comments

Comments
 (0)