Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions packages/provider-cursor/src/server/CursorAgentSdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as NodeServices from "@effect/platform-node/NodeServices";
import type { ListRunsOptions } from "@cursor/sdk";
import type { AgentOptions, ListRunsOptions } from "@cursor/sdk";
import { assert, describe, it } from "@effect/vitest";
import { ProviderSessionId, ThreadId } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
Expand All @@ -25,6 +25,12 @@ const cursorSdkMock = vi.hoisted(() => {
durationMs: 1,
}));
const runCancel = vi.fn(async () => {});
const store = {
dispose: vi.fn(async () => {
closeExecutionOrder.push("store.dispose");
}),
} as never;
const storeOpen = vi.fn(async () => store);
const agentClose = vi.fn(() => {
closeExecutionOrder.push("sdk:agent.close");
if (agentCloseFailure !== undefined) {
Expand Down Expand Up @@ -90,6 +96,8 @@ const cursorSdkMock = vi.hoisted(() => {
cancelRun,
closeExecutionOrder,
create,
store,
storeOpen,
listRuns,
resume,
runOperations,
Expand Down Expand Up @@ -120,7 +128,7 @@ vi.mock("./sdk.ts", () => ({
},
},
createAgentPlatform: vi.fn(),
SqliteLocalAgentStore: { open: vi.fn() },
SqliteLocalAgentStore: { open: cursorSdkMock.storeOpen },
}));

const layerTest = CursorAgentSdk.layer.pipe(
Expand Down Expand Up @@ -202,13 +210,14 @@ describe("CursorAgentSdkRunner", () => {
]);
assert.deepStrictEqual(cursorSdkMock.listRuns.mock.calls[0], [
"agent-cursor-agent-sdk-test",
{ runtime: "local", cwd: process.cwd(), limit: 100 },
{ runtime: "local", cwd: process.cwd(), store: cursorSdkMock.store, limit: 100 },
]);
assert.deepStrictEqual(cursorSdkMock.cancelRun.mock.calls[0], [
"run-stale",
{
runtime: "local",
cwd: process.cwd(),
store: cursorSdkMock.store,
},
]);
}).pipe(Effect.provide(layerTest)),
Expand Down Expand Up @@ -236,12 +245,17 @@ describe("CursorAgentSdkRunner", () => {
),
});

assert.strictEqual(
(cursorSdkMock.create.mock.lastCall as unknown as [AgentOptions])[0].local?.store,
cursorSdkMock.store,
);
cursorSdkMock.closeExecutionOrder.length = 0;
yield* session.close;

assert.deepStrictEqual(cursorSdkMock.closeExecutionOrder, [
"protocol:agent.close",
"sdk:agent.close",
"store.dispose",
]);
assert.equal(cursorSdkMock.agentClose.mock.calls.length, 1);
}).pipe(Effect.provide(layerTest)),
Expand Down Expand Up @@ -279,6 +293,7 @@ describe("CursorAgentSdkRunner", () => {
assert.deepStrictEqual(cursorSdkMock.closeExecutionOrder, [
"protocol:agent.close",
"sdk:agent.close",
"store.dispose",
]);
assert.equal(cursorSdkMock.agentClose.mock.calls.length, 1);
cursorSdkMock.setAgentCloseFailure(undefined);
Expand Down
37 changes: 28 additions & 9 deletions packages/provider-cursor/src/server/CursorAgentSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ function makeCursorAgentSdkProtocolLogger(input: {
export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(function* (
protocolLoggerFor: (input: CursorAgentSdkOpenInput) => CursorAgentSdkProtocolLogger | undefined,
) {
const { Agent, createAgentPlatform } = yield* CursorSdk.CursorSdk;
const { Agent, createAgentPlatform, SqliteLocalAgentStore } = yield* CursorSdk.CursorSdk;

/**
* The Cursor SDK decides once per process whether local sandboxing works, and
Expand Down Expand Up @@ -380,13 +380,35 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
},
});

const cwd =
typeof input.options.local?.cwd === "string"
? input.options.local.cwd
: input.options.local?.cwd?.[0];
// Without a store the SDK opens one per workspace and keeps it for the life of the
// process, with every agent's checkpoint blobs in memory. Ours is released on close.
const ownedStore =
input.options.local?.store === undefined
? yield* Effect.tryPromise({
try: () => SqliteLocalAgentStore.open({ workspaceRef: cwd ?? process.cwd() }),
catch: (cause) => runnerError(cause, "store.open"),
})
: undefined;
const disposeOwnedStore =
ownedStore === undefined
? Effect.void
: Effect.tryPromise(() => ownedStore.dispose()).pipe(Effect.ignore({ log: true }));
const options: AgentOptions =
ownedStore === undefined
? input.options
: { ...input.options, local: { ...input.options.local, store: ownedStore } };

const agent = yield* Effect.tryPromise({
try: () =>
input.operation === "create"
? Agent.create(input.options)
: Agent.resume(input.agentId!, input.options),
? Agent.create(options)
: Agent.resume(input.agentId!, options),
catch: (cause) => runnerError(cause, `agent.${input.operation}`),
});
}).pipe(Effect.onError(() => disposeOwnedStore));

yield* log({
direction: "incoming",
Expand All @@ -397,14 +419,10 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
},
});

const cwd =
typeof input.options.local?.cwd === "string"
? input.options.local.cwd
: input.options.local?.cwd?.[0];
const runOptions = {
runtime: "local" as const,
...(cwd === undefined ? {} : { cwd }),
...(input.options.local?.store === undefined ? {} : { store: input.options.local.store }),
...(options.local?.store === undefined ? {} : { store: options.local.store }),
};
let abandonedRunRecoveryAvailable = input.operation === "resume";

Expand Down Expand Up @@ -619,6 +637,7 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
catch: (cause) => runnerError(cause, "agent.close"),
}),
),
Effect.ensuring(disposeOwnedStore),
),
} satisfies CursorAgentSdkSession;
}),
Expand Down
Loading