fix(server): harden Cursor adapter turn lifecycle #9051
ClosedNeon-Wang(GitHub profile, opens in a new tab)wants to merge 4 commits into
main fromrefs/pull/9051/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/provider/Layers/CursorAdapter.ts line 143Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -130,3 +131,8 @@130131 notificationFiber: Fiber.Fiber<void, never> | undefined;131132 readonly pendingApprovals: Map<ApprovalRequestId, PendingApproval>;132133 readonly pendingUserInputs: Map<ApprovalRequestId, PendingUserInput>;134+ /** Turns interrupted while sendTurn was still preparing (before the prompt135+ * reached the wire). acp.cancel is a no-op at that point, so sendTurn136+ * checks this set at its prompt checkpoints instead. Entries are removed137+ * when the turn settles. */138+ readonly cancelledTurnIds: Set<TurnId>;apps/server/src/provider/Layers/CursorAdapter.ts line 489OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -467,3 +485,5 @@467485 }468486 yield* Effect.ignore(Scope.close(ctx.scope, Exit.void));469487 sessions.delete(ctx.threadId);488+ if (removeLock) {489+ yield* removeThreadLock(ctx.threadId);apps/server/src/provider/Layers/CursorAdapter.ts line 143Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -130,3 +131,8 @@130131 notificationFiber: Fiber.Fiber<void, never> | undefined;131132 readonly pendingApprovals: Map<ApprovalRequestId, PendingApproval>;132133 readonly pendingUserInputs: Map<ApprovalRequestId, PendingUserInput>;134+ /** Turns interrupted while sendTurn was still preparing (before the prompt135+ * reached the wire). acp.cancel is a no-op at that point, so sendTurn136+ * checks this set at its prompt checkpoints instead. Entries are removed137+ * when the turn settles. */138+ readonly cancelledTurnIds: Set<TurnId>;
- apps/server/src/provider/Layers/CursorAdapter.ts line 999Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -927,0 +960,21 @@960+ // interruptTurn cannot reach a turn whose prompt has not been sent961+ // yet (acp.cancel is a no-op pre-prompt), so cancelled turn ids are962+ // checked at both checkpoints instead.963+ const settleIfCancelled = () =>964+ Effect.gen(function* () {965+ if (!ctx.cancelledTurnIds.has(turnId)) {966+ return false;967+ }968+ ctx.cancelledTurnIds.delete(turnId);969+ if (ctx.promptsInFlight === 1) {970+ yield* offerRuntimeEvent({971+ type: "turn.completed",972+ ...(yield* makeEventStamp()),973+ provider: PROVIDER,974+ threadId: input.threadId,975+ turnId,976+ payload: { state: "cancelled", stopReason: "cancelled" },977+ });978+ }979+ return true;980+ });apps/server/src/provider/Layers/CursorAdapter.ts line 938OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -910,4 +932,7 @@910932 });911933912934 return session;913- }).pipe(Effect.scoped),935+ }).pipe(936+ // A failed start leaves no session behind — drop the thread lock so937+ // the lock map does not grow one entry per failed attempt.938+ Effect.tapError(() => removeThreadLock(input.threadId)),
- apps/server/src/provider/Layers/CursorAdapter.ts line 1237Resolvedby Neon-Wang(GitHub profile, opens in a new tab)@@ -1065,3 +1159,18 @@1065- Effect.sync(() => {1066- ctx.promptsInFlight = Math.max(0, ctx.promptsInFlight - 1);1067- }),1159+ // Finalizers must be infallible; the decrement is a leading sync1160+ // step, so ignoring a failed settle publish cannot skip it.1161+ Effect.ignore(1162+ Effect.gen(function* () {1163+ ctx.promptsInFlight = Math.max(0, ctx.promptsInFlight - 1);1164+ if (1165+ ctx.promptsInFlight === 0 &&1166+ !ctx.stopped &&1167+ ctx.cancelledTurnIds.delete(turnId)1168+ ) {1169+ yield* offerRuntimeEvent({1170+ type: "turn.completed",1171+ ...(yield* makeEventStamp()),1172+ provider: PROVIDER,1173+ threadId: input.threadId,1174+ turnId,1175+ payload: { state: "cancelled", stopReason: "cancelled" },1176+ });
- @@ -935,0 +979,21 @@979+ return false;980+ }981+ if (ctx.promptsInFlight !== 1) {982+ // A steered sibling prompt for this same turn is still in983+ // flight or preparing: skip the wire but keep the marker — the984+ // last draining prompt settles the turn (here once985+ // promptsInFlight === 1, or in the ensuring once it hits 0).986+ return true;987+ }988+ ctx.cancelledTurnIds.delete(turnId);989+ settled = true;990+ yield* offerRuntimeEvent({991+ type: "turn.completed",992+ ...(yield* makeEventStamp()),993+ provider: PROVIDER,994+ threadId: input.threadId,995+ turnId,996+ payload: { state: "cancelled", stopReason: "cancelled" },997+ });998+ return true;999+ });@@ -1091,0 +1193,21 @@1193+ ? error.message1194+ : undefined;1195+ const collapsed = (typeof rawMessage === "string" ? rawMessage : String(error))1196+ .replace(/\s+/g, " ")1197+ .trim();1198+ yield* offerRuntimeEvent({1199+ type: "turn.completed",1200+ ...(yield* makeEventStamp()),1201+ provider: PROVIDER,1202+ threadId: input.threadId,1203+ turnId,1204+ payload: {1205+ state: "failed",1206+ stopReason: null,1207+ errorMessage:1208+ collapsed.length > CURSOR_TURN_ERROR_MAX_CHARS1209+ ? `${collapsed.slice(0, CURSOR_TURN_ERROR_MAX_CHARS - 1)}…`1210+ : collapsed,1211+ },1212+ });1213+ }),
juliusmarminge closed this
Sign in to join this conversation.


macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed