fix(provider-cursor): Cursor text generation no longer leaks SQLite file handles on every call (#19310)

Takeover of #18154. Per-call SqliteLocalAgentStore so Cursor text generation closes its SQLite handles, and the agent now closes before its store on sign-out. Fixes #18151.

Co-authored-by: Aaron Queen <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-10-14 01:14:50 -07:00
1 parent 4cd0076ec6
commit acb90279e2
6 files changed
+119 -5

No files matched your search

@@ -104,6 +104,7 @@ vi.mock("./sdk.ts", () => ({
},
},
createAgentPlatform: vi.fn(),
SqliteLocalAgentStore: { open: vi.fn() },
}));
const layerTest = CursorAgentSdk.layer.pipe(
@@ -25,7 +25,7 @@ writeFileSync(
);
registerHooks({
resolve(specifier, context, next) {
if (specifier === "@cursor/sdk") {
if (specifier === "@cursor/sdk" || specifier === "@cursor/sdk/sqlite") {
return { url: pathToFileURL(stub).href, shortCircuit: true };
}
return next(specifier, context);
@@ -3,7 +3,7 @@ import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { Agent, createAgentPlatform } from "./sdk.ts";
import { Agent, createAgentPlatform, SqliteLocalAgentStore } from "./sdk.ts";
/**
* The Cursor Agent SDK runs in this process and spawns a shell for tool
@@ -65,6 +65,7 @@ export class CursorSdk extends Context.Service<
{
readonly Agent: typeof Agent;
readonly createAgentPlatform: typeof createAgentPlatform;
readonly SqliteLocalAgentStore: typeof SqliteLocalAgentStore;
}
>()("@t3tools/provider-cursor/server/CursorSdk") {}
@@ -79,5 +80,5 @@ export const layer = Layer.effect(
Effect.sync(() => {
process.off("unhandledRejection", onUnhandledRejection);
}),
).pipe(Effect.as(CursorSdk.of({ Agent, createAgentPlatform }))),
).pipe(Effect.as(CursorSdk.of({ Agent, createAgentPlatform, SqliteLocalAgentStore }))),
);
@@ -12,3 +12,6 @@ export const {
CursorSdkError,
InMemoryCredentialStore,
} = requireCursorSdk("@cursor/sdk") as typeof import("@cursor/sdk");
export const { SqliteLocalAgentStore } = requireCursorSdk(
"@cursor/sdk/sqlite",
) as typeof import("@cursor/sdk/sqlite");
@@ -1,8 +1,10 @@
import type { RunResult } from "@cursor/sdk";
import type { RunResult, SdkCredentialStore, StoredSdkCredentials } from "@cursor/sdk";
import { ProviderInstanceId, TextGenerationError } from "@t3tools/contracts";
import { CursorSettings } from "../settings.ts";
import { describe, expect, it } from "@effect/vitest";
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as FileSystem from "effect/FileSystem";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
@@ -11,6 +13,7 @@ import * as Schema from "effect/Schema";
import { createModelSelection } from "@t3tools/shared/model";
import { beforeEach, vi } from "vite-plus/test";
import { makeCursorAuth } from "./auth.ts";
import { makeCursorTextGeneration } from "./textGeneration.ts";
import * as CursorSdk from "./CursorSdk.ts";
@@ -19,6 +22,8 @@ const cursorSdkMock = vi.hoisted(() => ({
send: vi.fn<(prompt: string) => Promise<unknown>>(),
close: vi.fn(),
cancel: vi.fn(async () => {}),
storeOpen: vi.fn<(options: unknown) => Promise<unknown>>(),
storeDispose: vi.fn(async () => {}),
prompt: vi.fn<(prompt: string, options: unknown) => Promise<RunResult>>(async () => ({
id: "run-cursor-text-generation-test",
status: "finished",
@@ -30,6 +35,7 @@ const cursorSdkMock = vi.hoisted(() => ({
vi.mock("./sdk.ts", () => ({
Agent: { create: cursorSdkMock.create },
createAgentPlatform: vi.fn(),
SqliteLocalAgentStore: { open: cursorSdkMock.storeOpen },
}));
let hasCustomPolicy = false;
@@ -62,6 +68,9 @@ beforeEach(() => {
});
cursorSdkMock.close.mockClear();
cursorSdkMock.cancel.mockClear();
cursorSdkMock.storeDispose.mockClear();
cursorSdkMock.storeOpen.mockReset();
cursorSdkMock.storeOpen.mockResolvedValue({ dispose: cursorSdkMock.storeDispose });
cursorSdkMock.prompt.mockReset();
cursorSdkMock.prompt.mockResolvedValue({
id: "run-cursor-text-generation-test",
@@ -139,6 +148,7 @@ describe("CursorTextGeneration", () => {
},
local: {
cwd: "/isolated-text-generation",
store: { dispose: cursorSdkMock.storeDispose },
autoReview: false,
sandboxOptions: { enabled: true },
settingSources: [],
@@ -148,6 +158,93 @@ describe("CursorTextGeneration", () => {
}).pipe(Effect.provide(layerTest)),
);
it.effect("disposes the agent store after the agent, whether the run succeeds or fails", () =>
Effect.gen(function* () {
const textGeneration = yield* makeCursorTextGeneration(cursorSettings, {
CURSOR_API_KEY: "test-cursor-key",
});
const input = {
cwd: process.cwd(),
branch: "feature/cursor-store",
stagedSummary: "M README.md",
stagedPatch: "diff --git a/README.md b/README.md",
modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "composer-2"),
};
yield* textGeneration.generateCommitMessage(input);
expect(cursorSdkMock.storeOpen).toHaveBeenCalledWith(
expect.objectContaining({ workspaceRef: "/isolated-text-generation" }),
);
expect(cursorSdkMock.storeDispose).toHaveBeenCalledTimes(1);
expect(cursorSdkMock.close.mock.invocationCallOrder[0]).toBeLessThan(
cursorSdkMock.storeDispose.mock.invocationCallOrder[0]!,
);
cursorSdkMock.prompt.mockResolvedValueOnce({
id: "run-cursor-text-generation-test",
status: "error",
});
yield* Effect.flip(textGeneration.generateCommitMessage(input));
expect(cursorSdkMock.storeDispose).toHaveBeenCalledTimes(2);
}).pipe(Effect.provide(layerTest)),
);
it.effect("closes the agent before its store when sign-out interrupts a run", () =>
Effect.gen(function* () {
let credentials: StoredSdkCredentials | undefined = {
version: 1,
backendUrl: "https://api2.cursor.sh",
apiKey: "browser-key",
createdAtMs: 0,
};
const store: SdkCredentialStore = {
load: async () => credentials,
save: async (next) => {
credentials = next;
},
clear: async () => {
credentials = undefined;
},
};
const auth = yield* makeCursorAuth({
instanceId: ProviderInstanceId.make("cursor"),
displayName: "Cursor",
enabled: true,
store,
onChanged: () => Effect.void,
});
const started = Promise.withResolvers<void>();
cursorSdkMock.prompt.mockImplementationOnce(() => {
started.resolve();
return new Promise(() => {});
});
const generation = yield* makeCursorTextGeneration(
cursorSettings,
{},
auth.requireApiKey,
auth.withAccess,
);
const request = yield* generation
.generateCommitMessage({
cwd: process.cwd(),
branch: "feature/cursor-sign-out",
stagedSummary: "M README.md",
stagedPatch: "diff --git a/README.md b/README.md",
modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "composer-2"),
})
.pipe(Effect.forkScoped);
yield* Effect.promise(() => started.promise);
yield* auth.controller.logout(Effect.void);
expect(Exit.isFailure(yield* Fiber.await(request))).toBe(true);
expect(cursorSdkMock.storeDispose).toHaveBeenCalledTimes(1);
expect(cursorSdkMock.close.mock.invocationCallOrder[0]).toBeLessThan(
cursorSdkMock.storeDispose.mock.invocationCallOrder[0]!,
);
}).pipe(Effect.scoped, Effect.provide(Layer.merge(layerTest, NodeCrypto.layer))),
);
it.effect("continues in the temp directory when the SDK cannot sandbox", () =>
Effect.gen(function* () {
cursorSdkMock.create.mockImplementationOnce(async () => {
@@ -175,6 +272,7 @@ describe("CursorTextGeneration", () => {
expect(cursorSdkMock.create.mock.calls[1]?.[0]).toMatchObject({
local: {
cwd: "/isolated-text-generation",
store: { dispose: cursorSdkMock.storeDispose },
autoReview: false,
sandboxOptions: { enabled: false },
settingSources: [],
@@ -58,7 +58,7 @@ export const makeCursorTextGeneration = Effect.fn("makeCursorTextGeneration")(fu
withAccess?: CursorAuth["withAccess"],
) {
const fs = yield* FileSystem.FileSystem;
const { Agent } = yield* CursorSdk.CursorSdk;
const { Agent, SqliteLocalAgentStore } = yield* CursorSdk.CursorSdk;
const resolvedEnvironment = environment ?? process.env;
const resolveCursorApiKey = (operation: TextGenerationOperations.Operation) =>
@@ -98,12 +98,21 @@ export const makeCursorTextGeneration = Effect.fn("makeCursorTextGeneration")(fu
});
}
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cursor-text-" });
// The SDK keeps SQLite handles open after `agent.close()` and only releases
// them from a store the caller disposes. Its state lives outside `cwd` so the
// agent's workspace stays empty.
const stateRoot = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cursor-text-state-" });
const store = yield* Effect.acquireRelease(
Effect.tryPromise(() => SqliteLocalAgentStore.open({ workspaceRef: cwd, stateRoot })),
(store) => Effect.tryPromise(() => store.dispose()).pipe(Effect.ignore({ log: true })),
);
const agentOptions = {
apiKey,
mode: "plan",
model: cursorSdkModelSelection(modelSelection),
local: {
cwd,
store,
autoReview: false,
sandboxOptions: { enabled: true },
settingSources: [],
@@ -184,6 +193,8 @@ export const makeCursorTextGeneration = Effect.fn("makeCursorTextGeneration")(fu
return yield* TextGenerationOperations.decodeJsonReply(input, "Cursor SDK", rawResult);
}).pipe(
// Scope this call inside `withAccess`, so sign-out closes the agent before its store.
Effect.scoped,
(effect) => (withAccess ? withAccess(effect) : effect),
Effect.scoped,
Effect.mapError((cause) =>