Skip to content

Commit e0628a0

Browse files
committed
fix(cursor): wake a pending retry on stop and guard it against steers
Stopping a session left a sendTurn waiting out its transport-failure backoff and then returning the stale failure; it now settles as cancelled at once, like a stop during the prompt itself. A steer that finished during the backoff could also restore the in-flight count to one before the retry re-checked it and so replay the original prompt; sendTurn now bumps a persistent per-session prompt sequence and the retry only proceeds while no later prompt has arrived. Tests synchronize on the warning the adapter logs right before it waits, via a test-scoped logger, instead of racing the first streamed delta.
1 parent f30109b commit e0628a0

2 files changed

Lines changed: 107 additions & 25 deletions

File tree

‎apps/server/src/provider/Layers/CursorAdapter.test.ts‎

Lines changed: 91 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as Deferred from "effect/Deferred";
1111
import * as Effect from "effect/Effect";
1212
import * as Fiber from "effect/Fiber";
1313
import * as Layer from "effect/Layer";
14+
import * as Logger from "effect/Logger";
1415
import * as Schema from "effect/Schema";
1516
import * as Stream from "effect/Stream";
1617
import * as TestClock from "effect/testing/TestClock";
@@ -145,6 +146,19 @@ async function countPromptRequests(requestLogPath: string) {
145146
return requests.filter((entry) => entry.method === "session/prompt").length;
146147
}
147148

149+
// Captures the warning the adapter logs right before it starts waiting to
150+
// retry, so a test can act while that backoff is pending. Provide `layer` to
151+
// the sendTurn under test.
152+
function watchForRetryWait() {
153+
const waiting = Deferred.makeUnsafe<void>();
154+
const logger = Logger.make(({ message }) => {
155+
if (String(message).includes("retrying the prompt")) {
156+
Deferred.doneUnsafe(waiting, Effect.void);
157+
}
158+
});
159+
return { waiting, layer: Logger.layer([logger]) };
160+
}
161+
148162
// Transport-failure retries back off on the clock. Pump it in small hops until
149163
// the effect settles; the mock agent runs on the real clock, so each hop also
150164
// gives its stdio replies a scheduler turn to land.
@@ -320,13 +334,49 @@ cursorAdapterTestLayer("CursorAdapterLive", (it) => {
320334
Stream.runCollect,
321335
Effect.forkChild,
322336
);
323-
const sawDiagnostic = yield* Deferred.make<void>();
324-
yield* adapter.streamEvents.pipe(
325-
Stream.filter((event) => event.threadId === threadId && event.type === "content.delta"),
326-
Stream.take(1),
327-
Stream.runForEach(() => Deferred.succeed(sawDiagnostic, undefined).pipe(Effect.asVoid)),
337+
const retryWait = watchForRetryWait();
338+
yield* adapter.startSession({
339+
threadId,
340+
provider: ProviderDriverKind.make("cursor"),
341+
cwd: process.cwd(),
342+
runtimeMode: "full-access",
343+
});
344+
const sendTurnFiber = yield* adapter
345+
.sendTurn({ threadId, input: "continue", attachments: [] })
346+
.pipe(Effect.provide(retryWait.layer), Effect.forkChild);
347+
yield* Deferred.await(retryWait.waiting);
348+
yield* adapter.interruptTurn(threadId);
349+
// No clock pumping: the interrupt itself has to wake the waiting retry.
350+
const turn = yield* Fiber.join(sendTurnFiber);
351+
const runtimeEvents = Array.from(yield* Fiber.join(runtimeEventsFiber));
352+
const completed = runtimeEvents.filter((event) => event.type === "turn.completed");
353+
assert.equal(completed.length, 1);
354+
assert.equal(String(completed[0]?.turnId), String(turn.turnId));
355+
assert.deepStrictEqual(completed[0]?.payload, {
356+
state: "cancelled",
357+
stopReason: "cancelled",
358+
});
359+
assert.equal(yield* Effect.promise(() => countPromptRequests(requestLogPath)), 1);
360+
yield* adapter.stopSession(threadId);
361+
}),
362+
);
363+
364+
it.effect("settles as cancelled when the session stops while waiting to retry", () =>
365+
Effect.gen(function* () {
366+
const adapter = yield* CursorAdapter;
367+
const settings = yield* ServerSettingsService;
368+
const threadId = ThreadId.make("cursor-transport-error-stop");
369+
const { wrapperPath, requestLogPath } = yield* Effect.promise(() =>
370+
makeTransportFailureWrapper(),
371+
);
372+
yield* settings.updateSettings({ providers: { cursor: { binaryPath: wrapperPath } } });
373+
const runtimeEventsFiber = yield* adapter.streamEvents.pipe(
374+
Stream.filter((event) => event.threadId === threadId),
375+
Stream.takeUntil((event) => event.type === "turn.completed"),
376+
Stream.runCollect,
328377
Effect.forkChild,
329378
);
379+
const retryWait = watchForRetryWait();
330380
yield* adapter.startSession({
331381
threadId,
332382
provider: ProviderDriverKind.make("cursor"),
@@ -335,21 +385,11 @@ cursorAdapterTestLayer("CursorAdapterLive", (it) => {
335385
});
336386
const sendTurnFiber = yield* adapter
337387
.sendTurn({ threadId, input: "continue", attachments: [] })
338-
.pipe(Effect.forkChild);
339-
// The diagnostic is projected right before the adapter starts its backoff;
340-
// 10ms hops stay well short of the first delay, so the interrupt below
341-
// lands while the adapter is waiting to retry.
342-
yield* Effect.gen(function* () {
343-
for (let attempt = 0; attempt < 200; attempt += 1) {
344-
if (yield* Deferred.isDone(sawDiagnostic)) {
345-
return;
346-
}
347-
yield* TestClock.adjust("10 millis");
348-
}
349-
throw new Error("Timed out waiting for the mock agent's transport diagnostic.");
350-
});
351-
yield* adapter.interruptTurn(threadId);
352-
const turn = yield* pumpRetryClock(Fiber.join(sendTurnFiber));
388+
.pipe(Effect.provide(retryWait.layer), Effect.forkChild);
389+
yield* Deferred.await(retryWait.waiting);
390+
yield* adapter.stopSession(threadId);
391+
// No clock pumping: stopping the session has to wake the waiting retry.
392+
const turn = yield* Fiber.join(sendTurnFiber);
353393
const runtimeEvents = Array.from(yield* Fiber.join(runtimeEventsFiber));
354394
const completed = runtimeEvents.filter((event) => event.type === "turn.completed");
355395
assert.equal(completed.length, 1);
@@ -359,6 +399,37 @@ cursorAdapterTestLayer("CursorAdapterLive", (it) => {
359399
stopReason: "cancelled",
360400
});
361401
assert.equal(yield* Effect.promise(() => countPromptRequests(requestLogPath)), 1);
402+
}),
403+
);
404+
405+
it.effect("lets a steer during the backoff take the turn instead of replaying the prompt", () =>
406+
Effect.gen(function* () {
407+
const adapter = yield* CursorAdapter;
408+
const settings = yield* ServerSettingsService;
409+
const threadId = ThreadId.make("cursor-transport-error-steer");
410+
// Only the first prompt fails, so the steer's own prompt completes at once.
411+
const { wrapperPath, requestLogPath } = yield* Effect.promise(() =>
412+
makeTransportFailureWrapper({ T3_ACP_PROMPT_RESPONSE_TEXT_PROMPT_LIMIT: "1" }),
413+
);
414+
yield* settings.updateSettings({ providers: { cursor: { binaryPath: wrapperPath } } });
415+
const retryWait = watchForRetryWait();
416+
yield* adapter.startSession({
417+
threadId,
418+
provider: ProviderDriverKind.make("cursor"),
419+
cwd: process.cwd(),
420+
runtimeMode: "full-access",
421+
});
422+
const originalFiber = yield* adapter
423+
.sendTurn({ threadId, input: "continue", attachments: [] })
424+
.pipe(Effect.provide(retryWait.layer), Effect.forkChild);
425+
yield* Deferred.await(retryWait.waiting);
426+
// The steer finishes well inside the backoff, so the in-flight count is
427+
// back at 1 by the time the original attempt would retry.
428+
yield* adapter.sendTurn({ threadId, input: "actually do this", attachments: [] });
429+
const error = yield* pumpRetryClock(Fiber.join(originalFiber).pipe(Effect.flip));
430+
assert.equal(error._tag, "ProviderAdapterRequestError");
431+
// The original attempt and the steer, with no replay of the original prompt.
432+
assert.equal(yield* Effect.promise(() => countPromptRequests(requestLogPath)), 2);
362433
yield* adapter.stopSession(threadId);
363434
}),
364435
);

‎apps/server/src/provider/Layers/CursorAdapter.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ interface CursorSessionContext {
182182
readonly promptActivity: PromptActivity;
183183
/** Settled by `interruptTurn` so a pending transport-failure retry settles as cancelled. */
184184
turnInterrupted: Deferred.Deferred<void>;
185+
/**
186+
* Bumped by every sendTurn. A transport-failure retry replays its prompt
187+
* only while no later prompt (a steer) has arrived; `promptsInFlight` is
188+
* transient and can be back at 1 by then.
189+
*/
190+
promptSequence: number;
185191
stopped: boolean;
186192
}
187193

@@ -505,6 +511,7 @@ export function makeCursorAdapter(
505511
Effect.gen(function* () {
506512
if (ctx.stopped) return;
507513
ctx.stopped = true;
514+
yield* Deferred.succeed(ctx.turnInterrupted, undefined);
508515
yield* settlePendingApprovalsAsCancelled(ctx.pendingApprovals);
509516
yield* settlePendingUserInputsAsEmptyAnswers(ctx.pendingUserInputs);
510517
if (ctx.notificationFiber) {
@@ -837,6 +844,7 @@ export function makeCursorAdapter(
837844
activeTurnId: undefined,
838845
cursorSkillNames: undefined,
839846
promptsInFlight: 0,
847+
promptSequence: 0,
840848
assistantReply: new CursorTransportFailure(),
841849
promptActivity,
842850
turnInterrupted: yield* Deferred.make<void>(),
@@ -990,6 +998,8 @@ export function makeCursorAdapter(
990998
// resolving from here on does not settle the turn; the matching
991999
// decrement is the `ensuring` below.
9921000
ctx.promptsInFlight += 1;
1001+
ctx.promptSequence += 1;
1002+
const promptSequence = ctx.promptSequence;
9931003

9941004
return yield* Effect.gen(function* () {
9951005
const turnModelSelection =
@@ -1127,7 +1137,7 @@ export function makeCursorAdapter(
11271137
// a steer, a cancel, or any work leaves the failure to the check below.
11281138
for (const delay of TRANSPORT_FAILURE_RETRY_DELAYS) {
11291139
if (
1130-
ctx.promptsInFlight !== 1 ||
1140+
ctx.promptSequence !== promptSequence ||
11311141
result.stopReason === "cancelled" ||
11321142
failure === undefined ||
11331143
!promptDidNoWork(ctx.promptActivity)
@@ -1147,14 +1157,15 @@ export function makeCursorAdapter(
11471157
Effect.as(true),
11481158
Effect.timeoutOrElse({ duration: delay, orElse: () => Effect.succeed(false) }),
11491159
);
1150-
// A stop or a steer that landed during the backoff owns the turn now.
1151-
if (ctx.stopped || ctx.promptsInFlight !== 1) {
1152-
break;
1153-
}
1160+
// interruptTurn and stopSession both settle the wait as cancelled.
11541161
if (interrupted) {
11551162
result = { stopReason: "cancelled" };
11561163
break;
11571164
}
1165+
// A steer that landed during the backoff owns the turn now.
1166+
if (ctx.promptSequence !== promptSequence) {
1167+
break;
1168+
}
11581169
resetPromptActivity(ctx.promptActivity);
11591170
ctx.assistantReply = new CursorTransportFailure();
11601171
result = yield* prompt;

0 commit comments

Comments
 (0)