Skip to content

Commit 7964070

Browse files
PR Batch Testerclaude
andcommitted
fix(provider-cursor): only the SDK's no-progress resume error completes an ended turn
Cursor runs can continue after turn-ended (stop-hook follow-ups, background subagents), so keep every update and leave other errors after turn-ended as failures. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a040b20 commit 7964070

2 files changed

Lines changed: 54 additions & 156 deletions

File tree

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

Lines changed: 40 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,43 @@ describe("CursorAdapterV2", () => {
272272
}),
273273
failure: { class: "transport_error", message: "Provider turn failed." },
274274
},
275-
])("fails the turn with $reason", ({ updates = [], wait, failure }) =>
275+
{
276+
reason: "an error Cursor's run ended with after the turn ended",
277+
updates: [
278+
{ type: "text-delta", text: "The answer." },
279+
{ type: "turn-ended" },
280+
] satisfies ReadonlyArray<InteractionUpdate>,
281+
wait: Effect.succeed({
282+
id: "native-cursor-run",
283+
requestId: "native-request",
284+
status: "error" as const,
285+
error: { message: "Checkpoint persistence failed" },
286+
model: { id: "claude-fable-5" },
287+
durationMs: 1,
288+
}),
289+
failure: { class: "provider_error", message: "Checkpoint persistence failed" },
290+
},
291+
{
292+
reason: "no failure when Cursor's resume after the turn ended made no progress",
293+
// Cursor's SDK resumed from an older checkpoint after the answer was
294+
// delivered, regenerated the closing, then gave up on the run.
295+
updates: [
296+
{ type: "text-delta", text: "The answer." },
297+
{ type: "turn-ended" },
298+
{ type: "text-delta", text: "A regenerated closing." },
299+
{ type: "turn-ended" },
300+
] satisfies ReadonlyArray<InteractionUpdate>,
301+
wait: Effect.succeed({
302+
id: "native-cursor-run",
303+
requestId: "native-request",
304+
status: "error" as const,
305+
error: { message: "Agent turn stopped after repeated resume attempts made no progress" },
306+
model: { id: "claude-fable-5" },
307+
durationMs: 1,
308+
}),
309+
failure: null,
310+
},
311+
])("ends the turn with $reason", ({ updates = [], wait, failure }) =>
276312
Effect.gen(function* () {
277313
const fileSystem = yield* FileSystem.FileSystem;
278314
const workspace = yield* fileSystem.makeTempDirectoryScoped({ prefix: "cursor-v2-failure-" });
@@ -371,9 +407,9 @@ describe("CursorAdapterV2", () => {
371407
);
372408
assert.isTrue(Option.isSome(terminal));
373409
if (Option.isSome(terminal)) {
374-
assert.equal(terminal.value.status, "failed");
375-
assert.equal(terminal.value.failure?.class, failure.class);
376-
assert.equal(terminal.value.failure?.message, failure.message);
410+
assert.equal(terminal.value.status, failure === null ? "completed" : "failed");
411+
assert.equal(terminal.value.failure?.class, failure?.class);
412+
assert.equal(terminal.value.failure?.message, failure?.message);
377413
}
378414
}).pipe(
379415
Effect.scoped,
@@ -383,149 +419,6 @@ describe("CursorAdapterV2", () => {
383419
),
384420
);
385421

386-
it.effect("keeps a delivered answer when the SDK's resume after turn-ended fails", () =>
387-
Effect.gen(function* () {
388-
const fileSystem = yield* FileSystem.FileSystem;
389-
const workspace = yield* fileSystem.makeTempDirectoryScoped({ prefix: "cursor-v2-resume-" });
390-
const instanceId = ProviderInstanceId.make("cursor");
391-
const threadId = ThreadId.make("cursor-resume-thread");
392-
const modelSelection = { instanceId, model: "claude-opus-5-5" };
393-
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
394-
runtimeMode: "full-access",
395-
interactionMode: "default",
396-
cwd: workspace,
397-
});
398-
const usage = { inputTokens: 1, outputTokens: 1, cacheReadTokens: 0, cacheWriteTokens: 0 };
399-
// Cursor's SDK resumes from an older checkpoint after the turn ended,
400-
// regenerates the closing twice, then gives up on the run.
401-
const updates: ReadonlyArray<InteractionUpdate> = [
402-
{ type: "text-delta", text: "The answer." },
403-
{ type: "turn-ended", usage },
404-
{ type: "thinking-delta", text: "Resuming." },
405-
{ type: "text-delta", text: "A second closing." },
406-
{ type: "turn-ended", usage },
407-
{ type: "text-delta", text: "A third closing." },
408-
{ type: "turn-ended", usage },
409-
];
410-
const adapter = yield* makeCursorAdapterV2({
411-
instanceId,
412-
settings: yield* decodeCursorSettings({}),
413-
environment: { HOME: workspace },
414-
}).pipe(
415-
Effect.provideService(CursorAgentSdk.CursorAgentSdkRunner, {
416-
assertComplete: Effect.void,
417-
open: () =>
418-
Effect.succeed({
419-
agentId: "native-cursor-resume",
420-
listMessages: Effect.succeed([]),
421-
close: Effect.void,
422-
send: (input) =>
423-
Effect.gen(function* () {
424-
for (const update of updates) {
425-
yield* input.onDelta!(update).pipe(Effect.orDie);
426-
}
427-
return {
428-
agentId: "native-cursor-resume",
429-
runId: "native-cursor-run",
430-
wait: Effect.succeed({
431-
id: "native-cursor-run",
432-
requestId: "native-request",
433-
status: "error" as const,
434-
error: {
435-
message:
436-
"Agent turn stopped after repeated resume attempts made no progress",
437-
},
438-
model: { id: "claude-opus-5-5" },
439-
durationMs: 1,
440-
}),
441-
cancel: Effect.void,
442-
};
443-
}),
444-
}),
445-
}),
446-
Effect.provide(TestProviderHost.layer({ cwd: workspace })),
447-
);
448-
const runtime = yield* adapter.openSession({
449-
threadId,
450-
providerSessionId: ProviderSessionId.make("cursor-resume-session"),
451-
modelSelection,
452-
runtimePolicy,
453-
});
454-
const providerThread = yield* runtime.ensureThread({
455-
threadId,
456-
modelSelection,
457-
runtimePolicy,
458-
});
459-
const now = yield* DateTime.now;
460-
yield* runtime.startTurn({
461-
threadId,
462-
providerThread,
463-
modelSelection,
464-
runtimePolicy,
465-
runId: RunId.make("cursor-resume-run"),
466-
runOrdinal: 1,
467-
providerTurnOrdinal: 1,
468-
attemptId: RunAttemptId.make("cursor-resume-attempt"),
469-
rootNodeId: NodeId.make("cursor-resume-root"),
470-
appThread: {
471-
id: threadId,
472-
projectId: ProjectId.make("cursor-resume-project"),
473-
createdBy: "user",
474-
creationSource: "web",
475-
title: "Cursor resume",
476-
providerInstanceId: instanceId,
477-
modelSelection,
478-
runtimeMode: "full-access",
479-
interactionMode: "default",
480-
branch: null,
481-
worktreePath: null,
482-
activeProviderThreadId: providerThread.id,
483-
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
484-
forkedFrom: null,
485-
createdAt: now,
486-
updatedAt: now,
487-
archivedAt: null,
488-
settledOverride: null,
489-
settledAt: null,
490-
lastVisitedAt: null,
491-
deletedAt: null,
492-
},
493-
message: {
494-
messageId: MessageId.make("cursor-resume-message"),
495-
createdBy: "user",
496-
creationSource: "web",
497-
text: "hi",
498-
attachments: [],
499-
},
500-
});
501-
const events = yield* runtime.events.pipe(
502-
Stream.takeUntil((event) => event.type === "turn.terminal"),
503-
Stream.runCollect,
504-
);
505-
const terminal = events.find((event) => event.type === "turn.terminal");
506-
assert.equal(terminal?.status, "completed");
507-
assert.isNull(terminal?.failure);
508-
const items = events.flatMap((event) =>
509-
event.type === "turn_item.updated" ? [event.turnItem] : [],
510-
);
511-
assert.deepEqual(
512-
[
513-
...new Set(
514-
items.flatMap((item) =>
515-
item.type === "assistant_message" || item.type === "reasoning" ? [item.text] : [],
516-
),
517-
),
518-
],
519-
["The answer."],
520-
);
521-
}).pipe(
522-
Effect.scoped,
523-
Effect.provide(
524-
Layer.mergeAll(NodeServices.layer, IdAllocator.layer, McpProviderSessions.layer),
525-
),
526-
),
527-
);
528-
529422
it.effect("fails standalone SDK transport diagnostics and sends compaction as /compress", () =>
530423
Effect.gen(function* () {
531424
const fileSystem = yield* FileSystem.FileSystem;

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,11 @@ interface CursorProjectionTarget {
824824
// Fallback final output when Cursor's result carries none. The persisted row only
825825
// ever shows a bounded tail, so keep a little more than that.
826826
const MAX_CURSOR_STREAMED_OUTPUT_CHARS = 256 * 1024;
827+
// After a turn ends, a retryable stream error can make Cursor's SDK resume from
828+
// an older checkpoint inside the same run. When those resumes save nothing, it
829+
// ends the run with this error even though the answer was already delivered.
830+
const CURSOR_RESUME_NO_PROGRESS_MESSAGE =
831+
"Agent turn stopped after repeated resume attempts made no progress";
827832

828833
interface ActiveCursorToolCall {
829834
readonly callId: string;
@@ -1912,10 +1917,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
19121917
context: ActiveCursorTurn,
19131918
update: InteractionUpdate,
19141919
) {
1915-
// After turn-ended the answer is complete. A retryable stream error
1916-
// can still make Cursor's SDK resume from an older checkpoint inside
1917-
// the same run and regenerate the closing; that is not new work.
1918-
if (context.finalized || context.turnEnded) {
1920+
if (context.finalized) {
19191921
return;
19201922
}
19211923
switch (update.type) {
@@ -2104,6 +2106,11 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
21042106
yield* Deferred.succeed(input.context.completed, undefined);
21052107
});
21062108

2109+
const isResumeFailureAfterTurnEnded = (context: ActiveCursorTurn, result: RunResult) =>
2110+
context.turnEnded &&
2111+
result.status === "error" &&
2112+
result.error?.message === CURSOR_RESUME_NO_PROGRESS_MESSAGE;
2113+
21072114
const terminalStatus = (
21082115
context: ActiveCursorTurn,
21092116
result: RunResult,
@@ -2120,8 +2127,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
21202127
case "cancelled":
21212128
return "cancelled";
21222129
case "error":
2123-
// The SDK's failed resume after turn-ended does not undo the answer.
2124-
return context.turnEnded ? "completed" : "failed";
2130+
return isResumeFailureAfterTurnEnded(context, result) ? "completed" : "failed";
21252131
}
21262132
};
21272133

@@ -2368,14 +2374,13 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (
23682374
text: result.result,
23692375
});
23702376
}
2371-
if (context !== null && context.turnEnded && result.status === "error") {
2377+
if (context !== null && isResumeFailureAfterTurnEnded(context, result)) {
23722378
yield* Effect.logWarning(
2373-
"orchestration-v2.cursor-run-failed-after-turn-ended",
2379+
"orchestration-v2.cursor-resume-failed-after-turn-ended",
23742380
{
23752381
providerSessionId: input.providerSessionId,
23762382
providerThreadId: turnInput.providerThread.id,
23772383
providerTurnId,
2378-
error: result.error?.message,
23792384
},
23802385
);
23812386
}

0 commit comments

Comments
 (0)