Skip to content

Commit b754937

Browse files
committed
perf(cursor): load the agent SDK only when needed
1 parent 1d626d0 commit b754937

17 files changed

Lines changed: 481 additions & 104 deletions

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

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { assert, describe, it } from "@effect/vitest";
33
import { ProviderSessionId, ThreadId } from "@t3tools/contracts";
44
import * as Effect from "effect/Effect";
55
import * as Layer from "effect/Layer";
6+
import * as Exit from "effect/Exit";
7+
import * as Cause from "effect/Cause";
68
import { vi } from "vite-plus/test";
79

810
import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers";
@@ -94,17 +96,19 @@ const cursorSdkMock = vi.hoisted(() => {
9496
});
9597

9698
vi.mock("./sdk.ts", () => ({
97-
Agent: {
98-
create: cursorSdkMock.create,
99-
resume: cursorSdkMock.resume,
100-
listRuns: cursorSdkMock.listRuns,
101-
cancelRun: cursorSdkMock.cancelRun,
102-
messages: {
103-
list: vi.fn(async () => []),
99+
loadCursorSdk: () => ({
100+
Agent: {
101+
create: cursorSdkMock.create,
102+
resume: cursorSdkMock.resume,
103+
listRuns: cursorSdkMock.listRuns,
104+
cancelRun: cursorSdkMock.cancelRun,
105+
messages: {
106+
list: vi.fn(async () => []),
107+
},
104108
},
105-
},
106-
createAgentPlatform: vi.fn(),
107-
SqliteLocalAgentStore: { open: vi.fn() },
109+
createAgentPlatform: vi.fn(),
110+
}),
111+
loadCursorSqlite: () => ({ SqliteLocalAgentStore: { open: vi.fn() } }),
108112
}));
109113

110114
const layerTest = CursorAgentSdk.layer.pipe(
@@ -132,6 +136,41 @@ const layerTest = CursorAgentSdk.layer.pipe(
132136
);
133137

134138
describe("CursorAgentSdkRunner", () => {
139+
it.effect("constructs without loading and reports first-use load failures as runner errors", () =>
140+
Effect.gen(function* () {
141+
const failure = new Error("missing SDK chunk");
142+
const load = vi.fn(() => {
143+
throw failure;
144+
});
145+
const runner = yield* CursorAgentSdk.makeCursorAgentSdkRunner(() => undefined).pipe(
146+
Effect.provideService(CursorSdk.CursorSdk, {
147+
load,
148+
loadSqlite: () => {
149+
throw new Error("unused SQLite");
150+
},
151+
}),
152+
);
153+
assert.strictEqual(load.mock.calls.length, 0);
154+
const result = yield* runner
155+
.open({
156+
operation: "create",
157+
options: {},
158+
threadId: ThreadId.make("lazy-load"),
159+
providerSessionId: ProviderSessionId.make("lazy-load"),
160+
})
161+
.pipe(Effect.exit);
162+
assert(Exit.isFailure(result));
163+
assert.deepStrictEqual(
164+
Cause.squash(result.cause),
165+
new CursorAgentSdk.CursorAgentSdkRunnerError({
166+
method: "agent.create",
167+
cause: failure,
168+
}),
169+
);
170+
assert.strictEqual(load.mock.calls.length, 1);
171+
}),
172+
);
173+
135174
it.effect("retries a resumed send after cancelling its abandoned local run", () =>
136175
Effect.gen(function* () {
137176
cursorSdkMock.runOperations.length = 0;

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ function makeCursorAgentSdkProtocolLogger(input: {
340340
export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(function* (
341341
protocolLoggerFor: (input: CursorAgentSdkOpenInput) => CursorAgentSdkProtocolLogger | undefined,
342342
) {
343-
const { Agent, createAgentPlatform } = yield* CursorSdk.CursorSdk;
343+
const sdk = yield* CursorSdk.CursorSdk;
344344

345345
// Prime with cursorsandbox before any Full access run; the SDK caches sandbox support per process.
346346
const sandboxSupportPrimeStarted = yield* Ref.make(false);
@@ -352,7 +352,9 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
352352
}
353353
yield* Effect.tryPromise(async () => {
354354
const cwd = typeof options.local?.cwd === "string" ? options.local.cwd : undefined;
355-
const platform = await createAgentPlatform(cwd === undefined ? {} : { workspaceRef: cwd });
355+
const platform = await sdk
356+
.load()
357+
.createAgentPlatform(cwd === undefined ? {} : { workspaceRef: cwd });
356358
const release = await platform.prewarmLocalWorkspace({
357359
...(options.apiKey === undefined ? {} : { apiKey: options.apiKey }),
358360
local: {
@@ -368,6 +370,10 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
368370

369371
return CursorAgentSdkRunner.of({
370372
open: Effect.fn("CursorAgentSdkRunner.open")(function* (input) {
373+
const { Agent } = yield* Effect.try({
374+
try: sdk.load,
375+
catch: (cause) => runnerError(cause, `agent.${input.operation}`),
376+
});
371377
if (input.options.local?.sandboxOptions?.enabled === false) {
372378
yield* primeCursorSandboxSupport(input.options);
373379
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import { vi } from "vite-plus/test";
55

66
import * as CursorSdk from "./CursorSdk.ts";
77

8-
vi.mock("./sdk.ts", () => ({ Agent: {}, createAgentPlatform: {}, SqliteLocalAgentStore: {} }));
9-
108
// Shaped like the SDK's minified class: the constructor is not named after the error.
119
class o extends Error {
1210
override name = "WriteIterableClosedError";

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

Lines changed: 4 additions & 5 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, SqliteLocalAgentStore } from "./sdk.ts";
6+
import { loadCursorSdk, loadCursorSqlite } from "./sdk.ts";
77

88
// The SDK leaves a shell-error promise unhandled, which would exit the server.
99
const CURSOR_SHELL_SPAWN_MARKERS = [
@@ -60,9 +60,8 @@ function onUnhandledRejection(reason: unknown): void {
6060
export class CursorSdk extends Context.Service<
6161
CursorSdk,
6262
{
63-
readonly Agent: typeof Agent;
64-
readonly createAgentPlatform: typeof createAgentPlatform;
65-
readonly SqliteLocalAgentStore: typeof SqliteLocalAgentStore;
63+
readonly load: typeof loadCursorSdk;
64+
readonly loadSqlite: typeof loadCursorSqlite;
6665
}
6766
>()("@t3tools/provider-cursor/server/CursorSdk") {}
6867

@@ -77,5 +76,5 @@ export const layer = Layer.effect(
7776
Effect.sync(() => {
7877
process.off("unhandledRejection", onUnhandledRejection);
7978
}),
80-
).pipe(Effect.as(CursorSdk.of({ Agent, createAgentPlatform, SqliteLocalAgentStore }))),
79+
).pipe(Effect.as(CursorSdk.of({ load: loadCursorSdk, loadSqlite: loadCursorSqlite }))),
8180
);

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

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ import type { SDKModel, SDKUser } from "@cursor/sdk";
22
import { describe, expect, it } from "@effect/vitest";
33
import * as Effect from "effect/Effect";
44
import * as Exit from "effect/Exit";
5+
import * as Cause from "effect/Cause";
6+
import * as Deferred from "effect/Deferred";
7+
import * as Fiber from "effect/Fiber";
8+
import * as Layer from "effect/Layer";
9+
import * as HttpClient from "effect/http/HttpClient";
10+
import * as HttpClientResponse from "effect/http/HttpClientResponse";
11+
import { vi } from "vite-plus/test";
12+
13+
const sdkLoad = vi.hoisted(() =>
14+
vi.fn(() => {
15+
throw new Error("Catalog must not load the SDK");
16+
}),
17+
);
18+
vi.mock("./sdk.ts", () => ({ loadCursorSdk: sdkLoad, isCursorSdkError: () => false }));
519

620
import * as CursorSdkCatalog from "./CursorSdkCatalog.ts";
721

@@ -24,6 +38,127 @@ const catalogError = () =>
2438
});
2539

2640
describe("CursorSdkCatalog", () => {
41+
it.effect.each([
42+
{ route: "me", status: 200, body: user, expected: "ready" },
43+
{
44+
route: "me",
45+
status: 401,
46+
body: { error: { code: "feature_unavailable" } },
47+
expected: "unauthenticated",
48+
},
49+
{
50+
route: "me",
51+
status: 403,
52+
body: { error: { code: "feature_unavailable" } },
53+
expected: "no-user",
54+
},
55+
{ route: "me", status: 403, body: "Forbidden", expected: "no-user" },
56+
{ route: "me", status: 403, body: { code: "plan_required" }, expected: "plan" },
57+
{ route: "models", status: 403, body: { error: { code: "plan_required" } }, expected: "plan" },
58+
{ route: "models", status: 403, body: {}, expected: "blocked" },
59+
{ route: "me", status: 503, body: "Unavailable", expected: "error" },
60+
{ route: "models", status: 200, body: { items: [{ id: "missing-name" }] }, expected: "error" },
61+
{ route: "me", status: 200, body: "invalid JSON", expected: "error" },
62+
])(
63+
"reads $route HTTP $status as $expected without loading the SDK",
64+
({ route, status, body, expected }) =>
65+
Effect.gen(function* () {
66+
sdkLoad.mockClear();
67+
const requests: Array<string> = [];
68+
const models = [
69+
{
70+
...model,
71+
aliases: ["opus"],
72+
parameters: [{ id: "fast", values: [{ value: "true" }] }],
73+
variants: [
74+
{
75+
displayName: "Fast",
76+
params: [{ id: "fast", value: "true" }],
77+
isDefault: true,
78+
isMaxMode: true,
79+
},
80+
],
81+
},
82+
];
83+
const client = HttpClient.make((request) => {
84+
expect(request.headers.authorization).toBe("Bearer test-key");
85+
expect(request.method).toBe("GET");
86+
requests.push(request.url);
87+
const matches = request.url === `https://api.cursor.com/v1/${route}`;
88+
const responseBody = matches
89+
? body
90+
: request.url.endsWith("/me")
91+
? user
92+
: { items: models };
93+
const response =
94+
typeof responseBody === "string"
95+
? new Response(responseBody, { status: matches ? status : 200 })
96+
: Response.json(responseBody, { status: matches ? status : 200 });
97+
return Effect.succeed(HttpClientResponse.fromWeb(request, response));
98+
});
99+
const catalog = yield* CursorSdkCatalog.CursorSdkCatalog.pipe(
100+
Effect.provide(
101+
CursorSdkCatalog.layer.pipe(
102+
Layer.provide(Layer.succeed(HttpClient.HttpClient, client)),
103+
),
104+
),
105+
);
106+
expect(sdkLoad).not.toHaveBeenCalled();
107+
const result = yield* catalog.read("test-key").pipe(Effect.exit);
108+
expect(requests).toContain(`https://api.cursor.com/v1/${route}`);
109+
if (expected === "ready" || expected === "no-user") {
110+
expect(Exit.isSuccess(result)).toBe(true);
111+
if (Exit.isSuccess(result))
112+
expect(result.value).toEqual({
113+
user: expected === "no-user" ? undefined : user,
114+
models,
115+
});
116+
} else {
117+
expect(Exit.isFailure(result)).toBe(true);
118+
if (Exit.isFailure(result))
119+
expect(Cause.squash(result.cause)).toMatchObject({
120+
authenticationFailure: expected === "unauthenticated",
121+
planRequired: expected === "plan",
122+
accountBlocked: expected === "blocked",
123+
});
124+
}
125+
expect(sdkLoad).not.toHaveBeenCalled();
126+
}),
127+
);
128+
129+
it.effect("shares concurrent first model discovery until the response arrives", () =>
130+
Effect.gen(function* () {
131+
const modelsEntered = yield* Deferred.make<void>();
132+
const secondUserEntered = yield* Deferred.make<void>();
133+
const response = yield* Deferred.make<ReadonlyArray<SDKModel>>();
134+
let userCalls = 0;
135+
let modelCalls = 0;
136+
const catalog = yield* CursorSdkCatalog.makeCursorSdkCatalog({
137+
readUser: () =>
138+
Effect.gen(function* () {
139+
userCalls += 1;
140+
if (userCalls === 2) yield* Deferred.succeed(secondUserEntered, undefined);
141+
return user;
142+
}),
143+
readModels: () =>
144+
Effect.gen(function* () {
145+
modelCalls += 1;
146+
yield* Deferred.succeed(modelsEntered, undefined);
147+
return yield* Deferred.await(response);
148+
}),
149+
});
150+
expect(modelCalls).toBe(0);
151+
const first = yield* catalog.read("test-key").pipe(Effect.forkChild);
152+
yield* Deferred.await(modelsEntered);
153+
const second = yield* catalog.read("test-key").pipe(Effect.forkChild);
154+
yield* Deferred.await(secondUserEntered);
155+
expect(modelCalls).toBe(1);
156+
yield* Deferred.succeed(response, [model]);
157+
expect((yield* Fiber.join(first)).models).toEqual([model]);
158+
expect((yield* Fiber.join(second)).models).toEqual([model]);
159+
}),
160+
);
161+
27162
it.effect("does not cache failed or empty model discovery", () =>
28163
Effect.gen(function* () {
29164
let modelCalls = 0;

0 commit comments

Comments
 (0)