GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(cursor): text generation recovers from bad JSON and missing sandbox · pingdotgg/t3code@8fc4b0e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/provider-cursor/src/server
    • textGeneration.test.ts
    • textGeneration.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: packages/provider-cursor/src/server/textGeneration.test.ts
+163-13Lines changed: 163 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { ProviderInstanceId, TextGenerationError } from "@t3tools/contracts";
33
import { CursorSettings } from "../settings.ts";
44
import { describe, expect, it } from "@effect/vitest";
55
import * as Effect from "effect/Effect";
6+
import * as Fiber from "effect/Fiber";
67
import * as FileSystem from "effect/FileSystem";
78
import * as Layer from "effect/Layer";
89
import * as Schema from "effect/Schema";
10+
import * as TestClock from "effect/testing/TestClock";
911
import { createModelSelection } from "@t3tools/shared/model";
1012
import { beforeEach, vi } from "vite-plus/test";
1113

@@ -50,11 +52,18 @@ beforeEach(() => {
5052
cursorSdkMock.create.mockReset();
5153
cursorSdkMock.send.mockReset();
5254
cursorSdkMock.create.mockImplementation(async (options) => {
53-
cursorSdkMock.send.mockImplementation(async (prompt) => ({
54-
status: "running",
55-
cancel: cursorSdkMock.cancel,
56-
wait: () => cursorSdkMock.prompt(prompt, options),
57-
}));
55+
cursorSdkMock.send.mockImplementation(async (prompt) => {
56+
const run = {
57+
status: "running",
58+
cancel: cursorSdkMock.cancel,
59+
wait: async () => {
60+
const result = await cursorSdkMock.prompt(prompt, options);
61+
run.status = result.status;
62+
return result;
63+
},
64+
};
65+
return run;
66+
});
5867
return {
5968
close: cursorSdkMock.close,
6069
[Symbol.asyncDispose]: async () => {
@@ -95,23 +104,30 @@ describe("CursorTextGeneration", () => {
95104
};
96105
yield* generation.generateCommitMessage(input);
97106
expect(cursorSdkMock.create).toHaveBeenLastCalledWith(
98-
expect.objectContaining({ apiKey: "first-browser-key" }),
107+
expect.objectContaining({ apiKey: "first-browser-key", tools: [] }),
99108
);
100109
apiKey = "second-browser-key";
101110
yield* generation.generateCommitMessage(input);
102111
expect(cursorSdkMock.create).toHaveBeenLastCalledWith(
103-
expect.objectContaining({ apiKey: "second-browser-key" }),
112+
expect.objectContaining({ apiKey: "second-browser-key", tools: [] }),
104113
);
105114
}).pipe(Effect.provide(layerTest)),
106115
);
107116

108117
it.effect("continues in the temp directory when the SDK cannot sandbox", () =>
109118
Effect.gen(function* () {
110-
cursorSdkMock.create.mockImplementationOnce(async () => {
111-
throw new Error(
112-
"Local SDK sandboxing was requested, but sandboxing is not supported in this environment. Disable local.sandboxOptions.enabled or remove ~/.cursor/sandbox.json to run without sandboxing.",
113-
);
114-
});
119+
// The SDK accepts sandboxOptions at create and rejects them when send builds the executor.
120+
cursorSdkMock.create.mockImplementationOnce(async () => ({
121+
close: cursorSdkMock.close,
122+
[Symbol.asyncDispose]: async () => {
123+
cursorSdkMock.close();
124+
},
125+
send: async () => {
126+
throw new Error(
127+
"Local SDK sandboxing was requested, but sandboxing is not supported in this environment. Disable local.sandboxOptions.enabled or remove ~/.cursor/sandbox.json to run without sandboxing.",
128+
);
129+
},
130+
}));
115131
const textGeneration = yield* makeCursorTextGeneration(cursorSettings, {
116132
CURSOR_API_KEY: "test-cursor-key",
117133
});
@@ -126,10 +142,13 @@ describe("CursorTextGeneration", () => {
126142

127143
expect(generated.subject).toBe("Add generated commit message");
128144
expect(cursorSdkMock.create).toHaveBeenCalledTimes(2);
145+
expect(cursorSdkMock.close).toHaveBeenCalledTimes(2);
129146
expect(cursorSdkMock.create.mock.calls[0]?.[0]).toMatchObject({
147+
tools: [],
130148
local: { sandboxOptions: { enabled: true } },
131149
});
132150
expect(cursorSdkMock.create.mock.calls[1]?.[0]).toMatchObject({
151+
tools: [],
133152
local: {
134153
cwd: "/isolated-text-generation",
135154
store: { dispose: cursorSdkMock.storeDispose },
@@ -167,9 +186,117 @@ describe("CursorTextGeneration", () => {
167186

168187
expect(generated.subject).toBe("Update README dummy comment with attribution and date");
169188
expect(generated.body).toBe("");
189+
expect(cursorSdkMock.prompt).toHaveBeenCalledOnce();
190+
}).pipe(Effect.provide(layerTest)),
191+
);
192+
193+
it.effect.each([
194+
{ name: "invalid JSON", result: "Add a generated commit message" },
195+
{
196+
name: "a body with the wrong type",
197+
result: '{"subject":"Add generated commit message","body":[]}',
198+
},
199+
{ name: "empty output", result: " " },
200+
])("retries $name with a fresh agent before returning a commit message", ({ result }) =>
201+
Effect.gen(function* () {
202+
cursorSdkMock.prompt
203+
.mockResolvedValueOnce({ id: "invalid-output", status: "finished", result })
204+
.mockImplementationOnce(async (prompt) => {
205+
expect(cursorSdkMock.close).toHaveBeenCalledOnce();
206+
expect(prompt).toContain("Return ONLY the JSON object");
207+
return {
208+
id: "valid-output",
209+
status: "finished",
210+
result: '{"subject":"Add generated commit message","body":""}',
211+
};
212+
});
213+
const generation = yield* makeCursorTextGeneration(cursorSettings, {
214+
CURSOR_API_KEY: "test-cursor-key",
215+
});
216+
const generated = yield* generation.generateCommitMessage({
217+
cwd: process.cwd(),
218+
branch: "feature/cursor",
219+
stagedSummary: "M file.ts",
220+
stagedPatch: "diff",
221+
modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "auto"),
222+
});
223+
224+
expect(generated).toEqual({
225+
subject: "Add generated commit message",
226+
body: "",
227+
});
228+
expect(cursorSdkMock.create).toHaveBeenCalledTimes(2);
229+
expect(cursorSdkMock.close).toHaveBeenCalledTimes(2);
230+
expect(cursorSdkMock.storeDispose).toHaveBeenCalledOnce();
231+
}).pipe(Effect.provide(layerTest)),
232+
);
233+
234+
it.effect("stops after a second invalid reply and preserves the validation error", () =>
235+
Effect.gen(function* () {
236+
cursorSdkMock.prompt.mockResolvedValue({
237+
id: "invalid-output",
238+
status: "finished",
239+
result: '{"subject":"Add generated commit message","body":null}',
240+
});
241+
const generation = yield* makeCursorTextGeneration(cursorSettings, {
242+
CURSOR_API_KEY: "test-cursor-key",
243+
});
244+
const failure = yield* Effect.flip(
245+
generation.generateCommitMessage({
246+
cwd: process.cwd(),
247+
branch: null,
248+
stagedSummary: "M file.ts",
249+
stagedPatch: "diff",
250+
modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "auto"),
251+
}),
252+
);
253+
254+
expect(failure.operation).toBe("generateCommitMessage");
255+
expect(failure.detail).toBe("Cursor SDK returned invalid structured output.");
256+
expect(Schema.isSchemaError(failure.cause)).toBe(true);
257+
expect(cursorSdkMock.prompt).toHaveBeenCalledTimes(2);
170258
}).pipe(Effect.provide(layerTest)),
171259
);
172260

261+
it.effect("keeps both attempts inside the original deadline and cancels the pending retry", () =>
262+
Effect.gen(function* () {
263+
const firstStarted = Promise.withResolvers<void>();
264+
const retryStarted = Promise.withResolvers<void>();
265+
const firstResult = Promise.withResolvers<RunResult>();
266+
cursorSdkMock.prompt
267+
.mockImplementationOnce(() => {
268+
firstStarted.resolve();
269+
return firstResult.promise;
270+
})
271+
.mockImplementationOnce(() => {
272+
retryStarted.resolve();
273+
return new Promise(() => {});
274+
});
275+
const generation = yield* makeCursorTextGeneration(cursorSettings, {
276+
CURSOR_API_KEY: "test-cursor-key",
277+
});
278+
const request = yield* generation
279+
.generateThreadTitle({
280+
cwd: process.cwd(),
281+
message: "Fix reconnect",
282+
modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "auto"),
283+
})
284+
.pipe(Effect.flip, Effect.forkScoped);
285+
286+
yield* Effect.promise(() => firstStarted.promise);
287+
yield* TestClock.adjust("2 minutes");
288+
firstResult.resolve({ id: "invalid-output", status: "finished", result: "Not JSON" });
289+
yield* Effect.promise(() => retryStarted.promise);
290+
yield* TestClock.adjust("1 minute");
291+
292+
expect(cursorSdkMock.prompt).toHaveBeenCalledTimes(2);
293+
expect(cursorSdkMock.cancel).toHaveBeenCalledOnce();
294+
expect((yield* Fiber.join(request)).detail).toBe("Cursor SDK request timed out.");
295+
expect(cursorSdkMock.close).toHaveBeenCalledTimes(2);
296+
expect(cursorSdkMock.storeDispose).toHaveBeenCalledOnce();
297+
}).pipe(Effect.scoped, Effect.provide(layerTest)),
298+
);
299+
173300
it.effect("generates thread titles through Cursor SDK text generation", () =>
174301
Effect.gen(function* () {
175302
cursorSdkMock.prompt.mockResolvedValueOnce({
@@ -208,7 +335,7 @@ describe("CursorTextGeneration", () => {
208335
},
209336
{ status: "cancelled", error: undefined, detail: "Cursor SDK request was cancelled." },
210337
] as const)(
211-
"rejects a $status Cursor SDK run ($error.code) that includes valid title JSON",
338+
"does not retry an unsuccessful $status Cursor SDK run ($error.code)",
212339
({ status, error, detail }) =>
213340
Effect.gen(function* () {
214341
const promptResult = {
@@ -237,9 +364,32 @@ describe("CursorTextGeneration", () => {
237364
expect(failure.message).not.toContain("Raw Cursor");
238365
expect(failure.cause).toBe(error);
239366
expect(cursorSdkMock.close).toHaveBeenCalledOnce();
367+
expect(cursorSdkMock.prompt).toHaveBeenCalledOnce();
240368
}).pipe(Effect.provide(layerTest)),
241369
);
242370

371+
it.effect("stops after a second empty reply", () =>
372+
Effect.gen(function* () {
373+
cursorSdkMock.prompt.mockResolvedValue({
374+
id: "empty-output",
375+
status: "finished",
376+
result: " ",
377+
});
378+
const generation = yield* makeCursorTextGeneration(cursorSettings, {
379+
CURSOR_API_KEY: "test-cursor-key",
380+
});
381+
const failure = yield* Effect.flip(
382+
generation.generateThreadTitle({
383+
cwd: process.cwd(),
384+
message: "Fix reconnect",
385+
modelSelection: createModelSelection(ProviderInstanceId.make("cursor"), "composer-2"),
386+
}),
387+
);
388+
expect(failure.detail).toBe("Cursor SDK returned empty output.");
389+
expect(cursorSdkMock.prompt).toHaveBeenCalledTimes(2);
390+
}).pipe(Effect.provide(layerTest)),
391+
);
392+
243393
it.effect("fails closed when ambient sandbox policy can expand write access", () =>
244394
Effect.gen(function* () {
245395
hasCustomPolicy = true;

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.