TypeScript
fix(provider-cursor): finished Cursor turns no longer end in Provider error after the SDK's late resume (#24231)
shivambe1d0667320d
diff --git a/packages/provider-cursor/src/server/adapter.test.ts b/packages/provider-cursor/src/server/adapter.test.tsindex 0686b0c831d..f2cefd0dbc5 100644--- a/packages/provider-cursor/src/server/adapter.test.ts+++ b/packages/provider-cursor/src/server/adapter.test.ts@@ -272,7 +272,62 @@ describe("CursorAdapterV2", () => {}),failure: { class: "transport_error", message: "Provider turn failed." },},- ])("fails the turn with $reason", ({ updates = [], wait, failure }) =>+ {+ reason: "no failure when Cursor's resume after the turn ended made no progress",+ // Cursor's SDK resumed from an older checkpoint after the answer was+ // delivered, regenerated the closing, then gave up on the run.+ updates: [+ { type: "text-delta", text: "The answer." },+ { type: "turn-ended" },+ { type: "text-delta", text: "A regenerated closing." },+ { type: "turn-ended" },+ ] satisfies ReadonlyArray<InteractionUpdate>,+ wait: Effect.succeed({+ id: "native-cursor-run",+ requestId: "native-request",+ status: "error" as const,+ error: { message: "Agent turn stopped after repeated resume attempts made no progress" },+ model: { id: "claude-fable-5" },+ durationMs: 1,+ }),+ failure: null,+ },+ {+ reason: "the error a continuation after the turn ended stopped with",+ updates: [+ { type: "text-delta", text: "The answer." },+ { type: "turn-ended" },+ { type: "text-delta", text: "Following up on the stop hook." },+ ] satisfies ReadonlyArray<InteractionUpdate>,+ wait: Effect.succeed({+ id: "native-cursor-run",+ requestId: "native-request",+ status: "error" as const,+ error: { message: "Agent turn stopped after repeated resume attempts made no progress" },+ model: { id: "claude-fable-5" },+ durationMs: 1,+ }),+ failure: {+ class: "provider_error",+ message: "Agent turn stopped after repeated resume attempts made no progress",+ },+ },+ {+ reason: "no answer from a resume after the turn ended that made no progress",+ updates: [{ type: "turn-ended" }] satisfies ReadonlyArray<InteractionUpdate>,+ wait: Effect.succeed({+ id: "native-cursor-run",+ requestId: "native-request",+ status: "error" as const,+ error: { message: "Agent turn stopped after repeated resume attempts made no progress" },+ result: "A regenerated closing.",+ model: { id: "claude-fable-5" },+ durationMs: 1,+ }),+ failure: null,+ answers: [],+ },+ ])("ends the turn with $reason", ({ updates = [], wait, failure, answers }) =>Effect.gen(function* () {const fileSystem = yield* FileSystem.FileSystem;const workspace = yield* fileSystem.makeTempDirectoryScoped({ prefix: "cursor-v2-failure-" });@@ -365,15 +420,23 @@ describe("CursorAdapterV2", () => {attachments: [],},});- const terminal = yield* runtime.events.pipe(- Stream.filter((event) => event.type === "turn.terminal"),- Stream.runHead,+ const events = yield* runtime.events.pipe(+ Stream.takeUntil((event) => event.type === "turn.terminal"),+ Stream.runCollect,);- assert.isTrue(Option.isSome(terminal));- if (Option.isSome(terminal)) {- assert.equal(terminal.value.status, "failed");- assert.equal(terminal.value.failure?.class, failure.class);- assert.equal(terminal.value.failure?.message, failure.message);+ const terminal = events.find((event) => event.type === "turn.terminal");+ assert.equal(terminal?.status, failure === null ? "completed" : "failed");+ assert.equal(terminal?.failure?.class, failure?.class);+ assert.equal(terminal?.failure?.message, failure?.message);+ if (answers !== undefined) {+ assert.deepEqual(+ events.flatMap((event) =>+ event.type === "message.updated" && event.message.role === "assistant"+ ? [event.message.text]+ : [],+ ),+ answers,+ );}}).pipe(Effect.scoped,diff --git a/packages/provider-cursor/src/server/adapter.ts b/packages/provider-cursor/src/server/adapter.tsindex 825596c04e6..0349f011166 100644--- a/packages/provider-cursor/src/server/adapter.ts+++ b/packages/provider-cursor/src/server/adapter.ts@@ -824,6 +824,11 @@ interface CursorProjectionTarget {// Fallback final output when Cursor's result carries none. The persisted row only// ever shows a bounded tail, so keep a little more than that.const MAX_CURSOR_STREAMED_OUTPUT_CHARS = 256 * 1024;+// After a turn ends, a retryable stream error can make Cursor's SDK resume from+// an older checkpoint inside the same run. When those resumes save nothing, it+// ends the run with this error even though the answer was already delivered.+const CURSOR_RESUME_NO_PROGRESS_MESSAGE =+ "Agent turn stopped after repeated resume attempts made no progress";interface ActiveCursorToolCall {readonly callId: string;@@ -871,6 +876,8 @@ interface ActiveCursorTurn {readonly reasoning: ActiveCursorTextStream;interrupted: boolean;finalized: boolean;+ /** The agent's latest turn ended and no new work has started since. */+ turnEnded: boolean;}interface CursorLiveAgent {@@ -1914,6 +1921,18 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (if (context.finalized) {return;}+ if (update.type === "turn-ended") {+ context.turnEnded = true;+ } else if (+ update.type === "step-started" ||+ update.type === "user-message-appended" ||+ update.type === "text-delta" ||+ update.type === "thinking-delta" ||+ update.type === "partial-tool-call" ||+ update.type === "tool-call-started"+ ) {+ context.turnEnded = false;+ }switch (update.type) {case "text-delta":// Classify each assistant message on its own, so earlier@@ -2099,6 +2118,11 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (yield* Deferred.succeed(input.context.completed, undefined);});+ const isResumeFailureAfterTurnEnded = (context: ActiveCursorTurn, result: RunResult) =>+ context.turnEnded &&+ result.status === "error" &&+ result.error?.message === CURSOR_RESUME_NO_PROGRESS_MESSAGE;+const terminalStatus = (context: ActiveCursorTurn,result: RunResult,@@ -2115,7 +2139,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (case "cancelled":return "cancelled";case "error":- return "failed";+ return isResumeFailureAfterTurnEnded(context, result) ? "completed" : "failed";}};@@ -2319,6 +2343,7 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (},interrupted: false,finalized: false,+ turnEnded: false,};yield* Ref.set(activeTurn, context);yield* emitProviderEvent({@@ -2350,6 +2375,8 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (if (context !== null &&context.assistant.nextSegment === 0 &&+ // Text from a resume that made no progress is not the turn's answer.+ !isResumeFailureAfterTurnEnded(context, result) &&result.result !== undefined &&result.result.length > 0) {@@ -2361,6 +2388,16 @@ export const makeCursorAdapterV2 = Effect.fn("makeCursorAdapterV2")(function* (text: result.result,});}+ if (context !== null && isResumeFailureAfterTurnEnded(context, result)) {+ yield* Effect.logWarning(+ "orchestration-v2.cursor-resume-failed-after-turn-ended",+ {+ providerSessionId: input.providerSessionId,+ providerThreadId: turnInput.providerThread.id,+ providerTurnId,+ },+ );+ }if (context !== null) {const transportFailure = context.assistantReply.failure;const status =