Skip to content

Commit 6c1d77f

Browse files
committed
fix(server): failed Cursor turns show Cursor's reason
Every failed Cursor turn showed "Provider turn failed.", and a delegate_task parent got the same text. The SDK reports the reason, for example "Model Blocked Please ask your admin to enable access to Claude Fable 5." or "AI Model Not Found Invalid parameters for registry model", but makeProviderFailure doesn't read a cause's text, and CursorAgentSdkRunnerError only named the SDK method. A run that ends with an error now fails the turn with the run's own error message. CursorAgentSdkRunnerError's message now adds the SDK error's message, code and HTTP status ("Cursor Agent SDK agent.create failed: Invalid User API Key (HTTP 401)"), and a run that throws fails the turn with that message. Model: Claude Opus 5.5 (1M). Harness: Claude Code in T3 Code.
1 parent 3e6b450 commit 6c1d77f

4 files changed

Lines changed: 212 additions & 12 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.test.ts‎

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ import {
3434
makeCursorAdapterV2,
3535
nestedToolCallFromEnvelope,
3636
} from "./CursorAdapterV2.ts";
37-
import { isCursorCancellationError, loggedCursorAgentOptions } from "./CursorAgentSdk.ts";
37+
import {
38+
CursorAgentSdkRunnerError,
39+
isCursorCancellationError,
40+
loggedCursorAgentOptions,
41+
} from "./CursorAgentSdk.ts";
3842

3943
const decodeCursorSettings = Schema.decodeEffect(CursorSettings);
4044

@@ -188,6 +192,149 @@ describe("CursorAdapterV2", () => {
188192
}).pipe(Effect.scoped, Effect.provide(Layer.merge(NodeServices.layer, IdAllocator.layer))),
189193
);
190194

195+
it.effect.each([
196+
{
197+
reason: "the error Cursor's run ended with",
198+
wait: Effect.succeed({
199+
id: "native-cursor-run",
200+
requestId: "native-request",
201+
status: "error" as const,
202+
error: {
203+
message: "Model Blocked Please ask your admin to enable access to Claude Fable 5.",
204+
},
205+
model: { id: "claude-fable-5" },
206+
durationMs: 1,
207+
}),
208+
failure: {
209+
class: "provider_error",
210+
message: "Model Blocked Please ask your admin to enable access to Claude Fable 5.",
211+
},
212+
},
213+
{
214+
reason: "the SDK error that broke the run",
215+
wait: Effect.fail(
216+
new CursorAgentSdkRunnerError({
217+
method: "run.wait",
218+
cause: Object.assign(new Error("Too many requests"), {
219+
code: "resource_exhausted",
220+
status: 429,
221+
}),
222+
}),
223+
),
224+
failure: {
225+
class: "transport_error",
226+
message:
227+
"Cursor Agent SDK run.wait failed: Too many requests (resource_exhausted, HTTP 429)",
228+
},
229+
},
230+
])("fails the turn with $reason", ({ wait, failure }) =>
231+
Effect.gen(function* () {
232+
const fileSystem = yield* FileSystem.FileSystem;
233+
const path = yield* Path.Path;
234+
const workspace = yield* fileSystem.makeTempDirectoryScoped({ prefix: "cursor-v2-failure-" });
235+
const instanceId = ProviderInstanceId.make("cursor");
236+
const threadId = ThreadId.make("cursor-failure-thread");
237+
const modelSelection = { instanceId, model: "claude-fable-5" };
238+
const runtimePolicy = ProviderAdapterV2RuntimePolicy.make({
239+
runtimeMode: "full-access",
240+
interactionMode: "default",
241+
cwd: workspace,
242+
});
243+
const adapter = makeCursorAdapterV2({
244+
instanceId,
245+
settings: yield* decodeCursorSettings({}),
246+
environment: { HOME: workspace },
247+
fileSystem,
248+
path,
249+
idAllocator: yield* IdAllocator.IdAllocatorV2,
250+
serverConfig: yield* ServerConfig.ServerConfig.pipe(
251+
Effect.provide(
252+
ServerConfig.layerTest(workspace, { prefix: "cursor-v2-failure-config-" }),
253+
),
254+
),
255+
runner: {
256+
assertComplete: Effect.void,
257+
open: () =>
258+
Effect.succeed({
259+
agentId: "native-cursor-failure",
260+
listMessages: Effect.succeed([]),
261+
close: Effect.void,
262+
send: () =>
263+
Effect.succeed({
264+
agentId: "native-cursor-failure",
265+
runId: "native-cursor-run",
266+
wait,
267+
cancel: Effect.void,
268+
}),
269+
}),
270+
},
271+
});
272+
const runtime = yield* adapter.openSession({
273+
threadId,
274+
providerSessionId: ProviderSessionId.make("cursor-failure-session"),
275+
modelSelection,
276+
runtimePolicy,
277+
});
278+
const providerThread = yield* runtime.ensureThread({
279+
threadId,
280+
modelSelection,
281+
runtimePolicy,
282+
});
283+
const now = yield* DateTime.now;
284+
yield* runtime.startTurn({
285+
threadId,
286+
providerThread,
287+
modelSelection,
288+
runtimePolicy,
289+
runId: RunId.make("cursor-failure-run"),
290+
runOrdinal: 1,
291+
providerTurnOrdinal: 1,
292+
attemptId: RunAttemptId.make("cursor-failure-attempt"),
293+
rootNodeId: NodeId.make("cursor-failure-root"),
294+
appThread: {
295+
id: threadId,
296+
projectId: ProjectId.make("cursor-failure-project"),
297+
createdBy: "user",
298+
creationSource: "web",
299+
title: "Cursor failure",
300+
providerInstanceId: instanceId,
301+
modelSelection,
302+
runtimeMode: "full-access",
303+
interactionMode: "default",
304+
branch: null,
305+
worktreePath: null,
306+
activeProviderThreadId: providerThread.id,
307+
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
308+
forkedFrom: null,
309+
createdAt: now,
310+
updatedAt: now,
311+
archivedAt: null,
312+
settledOverride: null,
313+
settledAt: null,
314+
lastVisitedAt: null,
315+
deletedAt: null,
316+
},
317+
message: {
318+
messageId: MessageId.make("cursor-failure-message"),
319+
createdBy: "user",
320+
creationSource: "web",
321+
text: "hi",
322+
attachments: [],
323+
},
324+
});
325+
const terminal = yield* runtime.events.pipe(
326+
Stream.filter((event) => event.type === "turn.terminal"),
327+
Stream.runHead,
328+
);
329+
assert.isTrue(Option.isSome(terminal));
330+
if (Option.isSome(terminal)) {
331+
assert.equal(terminal.value.status, "failed");
332+
assert.equal(terminal.value.failure?.class, failure.class);
333+
assert.equal(terminal.value.failure?.message, failure.message);
334+
}
335+
}).pipe(Effect.scoped, Effect.provide(Layer.merge(NodeServices.layer, IdAllocator.layer))),
336+
);
337+
191338
it.effect("fails standalone SDK transport diagnostics and sends compaction as /compress", () =>
192339
Effect.gen(function* () {
193340
const fileSystem = yield* FileSystem.FileSystem;

‎apps/server/src/orchestration-v2/Adapters/CursorAdapterV2.ts‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,14 +2298,18 @@ export function makeCursorAdapterV2(
22982298
status,
22992299
...(status === "failed"
23002300
? {
2301-
failure: makeProviderFailure({
2302-
cause:
2303-
transportFailure ?? (result as { readonly error?: unknown }).error,
2304-
class:
2305-
transportFailure === undefined
2306-
? "provider_error"
2307-
: "transport_error",
2308-
}),
2301+
failure:
2302+
transportFailure === undefined
2303+
? makeProviderFailure({
2304+
cause: result.error,
2305+
// makeProviderFailure never reads a cause's own text.
2306+
message: result.error?.message,
2307+
class: "provider_error",
2308+
})
2309+
: makeProviderFailure({
2310+
cause: transportFailure,
2311+
class: "transport_error",
2312+
}),
23092313
}
23102314
: {}),
23112315
});
@@ -2321,7 +2325,13 @@ export function makeCursorAdapterV2(
23212325
...(context.interrupted
23222326
? {}
23232327
: {
2324-
failure: makeProviderFailure({ cause, class: "transport_error" }),
2328+
failure: makeProviderFailure({
2329+
cause,
2330+
message: CursorAgentSdk.isCursorAgentSdkRunnerError(cause)
2331+
? cause.message
2332+
: undefined,
2333+
class: "transport_error",
2334+
}),
23252335
}),
23262336
});
23272337
}

‎apps/server/src/orchestration-v2/Adapters/CursorAgentSdk.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,25 @@ describe("CursorAgentSdkRunner", () => {
301301
assert.equal(cursorSdkMock.runWait.mock.calls.length, 1);
302302
}).pipe(Effect.provide(testLayer)),
303303
);
304+
305+
it("names the SDK's reason, code and HTTP status in a runner error", () => {
306+
const failed = (cause: unknown) =>
307+
new CursorAgentSdk.CursorAgentSdkRunnerError({ method: "agent.create", cause }).message;
308+
assert.equal(
309+
failed(Object.assign(new Error("Too many requests"), { code: "rate_limited", status: 429 })),
310+
"Cursor Agent SDK agent.create failed: Too many requests (rate_limited, HTTP 429)",
311+
);
312+
// Cursor's messages often start with the code already.
313+
assert.equal(
314+
failed(
315+
Object.assign(new Error("[resource_exhausted] Error"), { code: "resource_exhausted" }),
316+
),
317+
"Cursor Agent SDK agent.create failed: [resource_exhausted] Error",
318+
);
319+
assert.equal(
320+
failed(Object.assign(new Error("Invalid User API Key"), { code: "error", status: 401 })),
321+
"Cursor Agent SDK agent.create failed: Invalid User API Key (HTTP 401)",
322+
);
323+
assert.equal(failed(undefined), "Cursor Agent SDK agent.create failed.");
324+
});
304325
});

‎apps/server/src/orchestration-v2/Adapters/CursorAgentSdk.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,33 @@ export class CursorAgentSdkRunnerError extends Schema.TaggedError<CursorAgentSdk
3131
},
3232
) {
3333
override get message(): string {
34-
return `Cursor Agent SDK ${this.method} failed.`;
34+
const reason = sdkErrorReason(this.cause);
35+
return reason === undefined
36+
? `Cursor Agent SDK ${this.method} failed.`
37+
: `Cursor Agent SDK ${this.method} failed: ${reason}`;
3538
}
3639
}
3740

38-
const isCursorAgentSdkRunnerError = Schema.is(CursorAgentSdkRunnerError);
41+
/** Codes Cursor sends when it has no specific one. */
42+
const GENERIC_SDK_ERROR_CODES = new Set(["", "error", "unknown"]);
43+
44+
/** The SDK's own message, with the code and HTTP status its errors carry. */
45+
function sdkErrorReason(cause: unknown): string | undefined {
46+
if (!(cause instanceof Error)) return undefined;
47+
const message = cause.message.trim();
48+
if (message.length === 0) return undefined;
49+
const code = Reflect.get(cause, "code");
50+
const status = Reflect.get(cause, "status");
51+
const details = [
52+
...(typeof code === "string" && !GENERIC_SDK_ERROR_CODES.has(code) && !message.includes(code)
53+
? [code]
54+
: []),
55+
...(typeof status === "number" ? [`HTTP ${status}`] : []),
56+
];
57+
return details.length === 0 ? message : `${message} (${details.join(", ")})`;
58+
}
59+
60+
export const isCursorAgentSdkRunnerError = Schema.is(CursorAgentSdkRunnerError);
3961

4062
export interface CursorAgentSdkOpenInput {
4163
readonly operation: "create" | "resume";

0 commit comments

Comments
 (0)