fix(cursor): retire interrupted turn generations

This commit is contained in:
t3-code[bot] committed 2026-08-06 22:52:48 +00:00
1 parent ac2cd317ce
commit 03044135db
2 files changed
+117 -4

No files matched your search

@@ -11,6 +11,7 @@ import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Ref from "effect/Ref";
import * as Schema from "effect/Schema";
import * as Stream from "effect/Stream";
import * as TestClock from "effect/testing/TestClock";
@@ -1130,6 +1131,98 @@ cursorAdapterTestLayer("CursorAdapterLive", (it) => {
yield* adapter.stopSession(threadId);
}),
);
it.effect("starts a fresh turn immediately after interrupting a hanging prompt", () =>
Effect.gen(function* () {
const adapter = yield* CursorAdapter;
const serverSettings = yield* ServerSettingsService;
const threadId = ThreadId.make("cursor-interrupt-follow-up-thread");
const tempDir = yield* Effect.promise(() =>
NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "cursor-acp-")),
);
const requestLogPath = NodePath.join(tempDir, "requests.ndjson");
const argvLogPath = NodePath.join(tempDir, "argv.txt");
yield* Effect.promise(() => NodeFSP.writeFile(requestLogPath, "", "utf8"));
const wrapperPath = yield* Effect.promise(() =>
makeProbeWrapper(requestLogPath, argvLogPath, {
T3_ACP_HANG_FIRST_PROMPT_FOREVER: "1",
}),
);
yield* serverSettings.updateSettings({
providers: { cursor: { binaryPath: wrapperPath } },
});
const events: Array<ProviderRuntimeEvent> = [];
const firstTurnStarted = yield* Deferred.make<string>();
const twoTurnsCompleted = yield* Deferred.make<void>();
const completedCountRef = yield* Ref.make(0);
const eventFiber = yield* Stream.runForEach(adapter.streamEvents, (event) =>
Effect.gen(function* () {
if (String(event.threadId) !== String(threadId)) return;
events.push(event);
if (event.type === "turn.started") {
yield* Deferred.succeed(firstTurnStarted, String(event.turnId)).pipe(Effect.ignore);
}
if (event.type === "turn.completed") {
const completedCount = yield* Ref.updateAndGet(completedCountRef, (count) => count + 1);
if (completedCount === 2) {
yield* Deferred.succeed(twoTurnsCompleted, undefined).pipe(Effect.ignore);
}
}
}),
).pipe(Effect.forkChild);
yield* adapter.startSession({
threadId,
provider: ProviderDriverKind.make("cursor"),
cwd: process.cwd(),
runtimeMode: "approval-required",
modelSelection: {
instanceId: ProviderInstanceId.make("cursor"),
model: "default",
},
});
const firstSendFiber = yield* adapter
.sendTurn({ threadId, input: "hang", attachments: [] })
.pipe(Effect.forkChild);
const firstTurnId = yield* Deferred.await(firstTurnStarted).pipe(Effect.timeout("2 seconds"));
yield* waitForJsonLogMatch(
requestLogPath,
(request) => request.method === "session/prompt",
80,
).pipe(Effect.timeout("2 seconds"));
yield* adapter.interruptTurn(threadId).pipe(Effect.timeout("2 seconds"));
const followUp = yield* adapter
.sendTurn({ threadId, input: "follow up", attachments: [] })
.pipe(Effect.timeout("2 seconds"));
yield* Fiber.join(firstSendFiber).pipe(Effect.timeout("2 seconds"));
yield* Deferred.await(twoTurnsCompleted).pipe(Effect.timeout("2 seconds"));
yield* Fiber.interrupt(eventFiber);
assert.notEqual(String(followUp.turnId), firstTurnId);
const completed = events.filter(
(event) => String(event.threadId) === String(threadId) && event.type === "turn.completed",
);
assert.lengthOf(completed, 2);
assert.equal(String(completed[0]?.turnId), firstTurnId);
if (completed[0]?.type === "turn.completed") {
assert.equal(completed[0].payload.state, "cancelled");
assert.equal(completed[0].payload.stopReason, "cancelled");
}
assert.equal(String(completed[1]?.turnId), String(followUp.turnId));
if (completed[1]?.type === "turn.completed") {
assert.equal(completed[1].payload.state, "completed");
}
const requests = yield* Effect.promise(() => readJsonLines(requestLogPath));
assert.lengthOf(
requests.filter((request) => request.method === "session/prompt"),
2,
);
yield* adapter.stopSession(threadId);
}).pipe(TestClock.withLive),
);
it.effect("resolves a pending permission before completing a normally returned turn", () =>
Effect.gen(function* () {
const adapter = yield* CursorAdapter;
@@ -1039,11 +1039,10 @@ export function makeCursorAdapter(
Effect.gen(function* () {
if (promptFinalized) return false;
promptFinalized = true;
if (ctx.callbackLifecycle.generation !== generation) return false;
ctx.promptsInFlight = Math.max(0, ctx.promptsInFlight - 1);
// Only the final prompt for this generation may settle and complete the turn.
if (ctx.promptsInFlight !== 0 || ctx.callbackLifecycle.generation !== generation) {
return false;
}
if (ctx.promptsInFlight !== 0) return false;
ctx.callbackLifecycle.admissionOpen = false;
yield* settlePendingCallbacks(
@@ -1190,14 +1189,21 @@ export function makeCursorAdapter(
const interruptTurn: CursorAdapterShape["interruptTurn"] = (threadId) =>
Effect.gen(function* () {
const ctx = yield* requireSession(threadId);
yield* Effect.uninterruptible(
const interruptedTurnId = yield* Effect.uninterruptible(
ctx.callbackLifecycle.semaphore.withPermit(
Effect.gen(function* () {
const turnId = ctx.promptsInFlight > 0 ? ctx.activeTurnId : undefined;
ctx.callbackLifecycle.admissionOpen = false;
yield* settlePendingCallbacks(
Array.from(ctx.pendingApprovals.values()),
Array.from(ctx.pendingUserInputs.values()),
);
if (turnId) {
ctx.promptsInFlight = 0;
ctx.callbackLifecycle.generation += 1;
ctx.lastPlanFingerprint = undefined;
}
return turnId;
}),
),
);
@@ -1208,6 +1214,20 @@ export function makeCursorAdapter(
),
),
);
if (interruptedTurnId && !ctx.stopped) {
yield* makeEventStamp().pipe(
Effect.flatMap((stamp) =>
offerRuntimeEvent({
type: "turn.completed",
...stamp,
provider: PROVIDER,
threadId,
turnId: interruptedTurnId,
payload: { state: "cancelled", stopReason: "cancelled" },
}),
),
);
}
});
const respondToRequest: CursorAdapterShape["respondToRequest"] = (