Skip to content

Commit f30109b

Browse files
committed
fix(cursor): retry prompts that only returned a transport failure
Cursor's ACP emits transient transport failures as assistant text and then reports a successful turn. Since #10337 the adapter fails such turns, but every blip still costs the user a manual resend. When the failed attempt produced nothing but the diagnostic (no tool calls, no requests to the user, no other assistant text) a replay cannot duplicate work, so retry the prompt with a bounded backoff before failing the turn. Interrupting the turn during the backoff settles it as cancelled, a steer takes the turn over, and any observed work keeps the existing fail-fast behavior. The mock agent gains T3_ACP_PROMPT_RESPONSE_TEXT_PROMPT_LIMIT so only the first N prompts answer with the scripted text.
1 parent 211618f commit f30109b

3 files changed

Lines changed: 277 additions & 13 deletions

File tree

‎apps/server/scripts/acp-mock-agent.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ const failPrompt = process.env.T3_ACP_FAIL_PROMPT === "1";
5252
const failSetConfigOption = process.env.T3_ACP_FAIL_SET_CONFIG_OPTION === "1";
5353
const exitOnSetConfigOption = process.env.T3_ACP_EXIT_ON_SET_CONFIG_OPTION === "1";
5454
const promptResponseText = process.env.T3_ACP_PROMPT_RESPONSE_TEXT;
55+
// Caps how many leading prompts answer with T3_ACP_PROMPT_RESPONSE_TEXT; later
56+
// prompts fall back to the default text so a retry can be told apart.
57+
const promptResponseTextPromptLimit =
58+
process.env.T3_ACP_PROMPT_RESPONSE_TEXT_PROMPT_LIMIT === undefined
59+
? undefined
60+
: Number(process.env.T3_ACP_PROMPT_RESPONSE_TEXT_PROMPT_LIMIT);
5561
const initialGrokReasoningEffort =
5662
process.env.T3_ACP_INITIAL_GROK_REASONING_EFFORT?.trim() || undefined;
5763
const promptDelayMs = Number(process.env.T3_ACP_PROMPT_DELAY_MS ?? "0");
@@ -77,6 +83,13 @@ let promptCount = 0;
7783
let overlappingFirstPromptId: string | undefined;
7884
const cancelledSessions = new Set<string>();
7985

86+
function promptResponseTextFor(prompt: number): string | undefined {
87+
if (promptResponseTextPromptLimit !== undefined && prompt > promptResponseTextPromptLimit) {
88+
return undefined;
89+
}
90+
return promptResponseText;
91+
}
92+
8093
function promptIdFromRequestMeta(
8194
request: Pick<AcpSchema.PromptRequest, "_meta">,
8295
): string | undefined {
@@ -878,7 +891,7 @@ const program = Effect.gen(function* () {
878891
sessionId: requestedSessionId,
879892
update: {
880893
sessionUpdate: "agent_message_chunk",
881-
content: { type: "text", text: "after tool" },
894+
content: { type: "text", text: promptResponseTextFor(promptCount) ?? "after tool" },
882895
},
883896
});
884897

@@ -1223,7 +1236,7 @@ const program = Effect.gen(function* () {
12231236
sessionId: requestedSessionId,
12241237
update: {
12251238
sessionUpdate: "agent_message_chunk",
1226-
content: { type: "text", text: promptResponseText ?? "hello from mock" },
1239+
content: { type: "text", text: promptResponseTextFor(promptCount) ?? "hello from mock" },
12271240
},
12281241
});
12291242

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

Lines changed: 174 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,44 @@ function waitForJsonLogMatch(
124124
});
125125
}
126126

127+
const transportFailureText = "Error: RetriableError: [unavailable] Error";
128+
129+
// A mock agent that answers prompts with a leaked Cursor transport diagnostic
130+
// and logs every request so tests can count prompt attempts.
131+
async function makeTransportFailureWrapper(extraEnv?: Record<string, string>) {
132+
const dir = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "cursor-transport-failure-"));
133+
const requestLogPath = NodePath.join(dir, "requests.ndjson");
134+
await NodeFSP.writeFile(requestLogPath, "", "utf8");
135+
const wrapperPath = await makeMockAgentWrapper({
136+
T3_ACP_REQUEST_LOG_PATH: requestLogPath,
137+
T3_ACP_PROMPT_RESPONSE_TEXT: transportFailureText,
138+
...extraEnv,
139+
});
140+
return { wrapperPath, requestLogPath };
141+
}
142+
143+
async function countPromptRequests(requestLogPath: string) {
144+
const requests = await readJsonLines(requestLogPath);
145+
return requests.filter((entry) => entry.method === "session/prompt").length;
146+
}
147+
148+
// Transport-failure retries back off on the clock. Pump it in small hops until
149+
// the effect settles; the mock agent runs on the real clock, so each hop also
150+
// gives its stdio replies a scheduler turn to land.
151+
function pumpRetryClock<A, E, R>(effect: Effect.Effect<A, E, R>) {
152+
return Effect.raceFirst(
153+
effect,
154+
Effect.gen(function* () {
155+
for (let attempt = 0; attempt < 400; attempt += 1) {
156+
yield* TestClock.adjust("50 millis");
157+
}
158+
return yield* Effect.die(
159+
new Error("Timed out pumping the test clock through transport-failure retries."),
160+
);
161+
}),
162+
);
163+
}
164+
127165
// Tests mutate `ServerSettingsService` mid-flight (e.g. setting
128166
// `providers.cursor.binaryPath` to a mock ACP wrapper). The adapter
129167
// captures `cursorSettings` once at construction, so without a resolver
@@ -162,15 +200,54 @@ const cursorAdapterTestLayer = it.layer(
162200
);
163201

164202
cursorAdapterTestLayer("CursorAdapterLive", (it) => {
165-
it.effect("rejects a Cursor transport error returned as a successful assistant answer", () =>
203+
it.effect("retries a prompt whose only output was a Cursor transport error", () =>
204+
Effect.gen(function* () {
205+
const adapter = yield* CursorAdapter;
206+
const settings = yield* ServerSettingsService;
207+
const threadId = ThreadId.make("cursor-transport-error-retry");
208+
// Only the first prompt fails; the retry gets the default answer.
209+
const { wrapperPath, requestLogPath } = yield* Effect.promise(() =>
210+
makeTransportFailureWrapper({ T3_ACP_PROMPT_RESPONSE_TEXT_PROMPT_LIMIT: "1" }),
211+
);
212+
yield* settings.updateSettings({ providers: { cursor: { binaryPath: wrapperPath } } });
213+
const runtimeEventsFiber = yield* adapter.streamEvents.pipe(
214+
Stream.filter((event) => event.threadId === threadId),
215+
Stream.takeUntil((event) => event.type === "turn.completed"),
216+
Stream.runCollect,
217+
Effect.forkChild,
218+
);
219+
yield* adapter.startSession({
220+
threadId,
221+
provider: ProviderDriverKind.make("cursor"),
222+
cwd: process.cwd(),
223+
runtimeMode: "full-access",
224+
});
225+
const turn = yield* pumpRetryClock(
226+
adapter.sendTurn({ threadId, input: "continue", attachments: [] }),
227+
);
228+
const runtimeEvents = Array.from(yield* Fiber.join(runtimeEventsFiber));
229+
const completed = runtimeEvents.filter((event) => event.type === "turn.completed");
230+
assert.equal(completed.length, 1);
231+
assert.equal(String(completed[0]?.turnId), String(turn.turnId));
232+
assert.deepStrictEqual(completed[0]?.payload, { state: "completed", stopReason: "end_turn" });
233+
// The retry continues the turn instead of opening a new one.
234+
assert.equal(runtimeEvents.filter((event) => event.type === "turn.started").length, 1);
235+
const deltas = runtimeEvents.flatMap((event) =>
236+
event.type === "content.delta" ? [event.payload.delta] : [],
237+
);
238+
assert.deepStrictEqual(deltas, [transportFailureText, "hello from mock"]);
239+
assert.equal(yield* Effect.promise(() => countPromptRequests(requestLogPath)), 2);
240+
yield* adapter.stopSession(threadId);
241+
}),
242+
);
243+
244+
it.effect("fails the turn once Cursor transport error retries are exhausted", () =>
166245
Effect.gen(function* () {
167246
const adapter = yield* CursorAdapter;
168247
const settings = yield* ServerSettingsService;
169248
const threadId = ThreadId.make("cursor-transport-error-answer");
170-
const wrapperPath = yield* Effect.promise(() =>
171-
makeMockAgentWrapper({
172-
T3_ACP_PROMPT_RESPONSE_TEXT: "Error: RetriableError: WritableIterable is closed",
173-
}),
249+
const { wrapperPath, requestLogPath } = yield* Effect.promise(() =>
250+
makeTransportFailureWrapper(),
174251
);
175252
yield* settings.updateSettings({ providers: { cursor: { binaryPath: wrapperPath } } });
176253
const runtimeEventsFiber = yield* adapter.streamEvents.pipe(
@@ -184,17 +261,105 @@ cursorAdapterTestLayer("CursorAdapterLive", (it) => {
184261
cwd: process.cwd(),
185262
runtimeMode: "full-access",
186263
});
187-
const error = yield* adapter
188-
.sendTurn({ threadId, input: "continue", attachments: [] })
189-
.pipe(Effect.flip);
264+
const error = yield* pumpRetryClock(
265+
adapter.sendTurn({ threadId, input: "continue", attachments: [] }).pipe(Effect.flip),
266+
);
190267
assert.equal(error._tag, "ProviderAdapterRequestError");
191268
if (error._tag === "ProviderAdapterRequestError") {
192269
assert.equal(error.detail, "Cursor reported a transport failure.");
193-
assert.equal(error.cause, "Error: RetriableError: WritableIterable is closed");
270+
assert.equal(error.cause, transportFailureText);
194271
}
195272
yield* adapter.stopSession(threadId);
196273
const runtimeEvents = yield* Fiber.join(runtimeEventsFiber);
197274
assert.isFalse(runtimeEvents.some((event) => event.type === "turn.completed"));
275+
// The initial attempt plus one retry per configured backoff.
276+
assert.equal(yield* Effect.promise(() => countPromptRequests(requestLogPath)), 3);
277+
}),
278+
);
279+
280+
it.effect("does not retry a Cursor transport error once the prompt did work", () =>
281+
Effect.gen(function* () {
282+
const adapter = yield* CursorAdapter;
283+
const settings = yield* ServerSettingsService;
284+
const threadId = ThreadId.make("cursor-transport-error-after-work");
285+
// Assistant text, a tool call, then the diagnostic as its own segment.
286+
const { wrapperPath, requestLogPath } = yield* Effect.promise(() =>
287+
makeTransportFailureWrapper({ T3_ACP_EMIT_INTERLEAVED_ASSISTANT_TOOL_CALLS: "1" }),
288+
);
289+
yield* settings.updateSettings({ providers: { cursor: { binaryPath: wrapperPath } } });
290+
yield* adapter.startSession({
291+
threadId,
292+
provider: ProviderDriverKind.make("cursor"),
293+
cwd: process.cwd(),
294+
runtimeMode: "full-access",
295+
});
296+
const error = yield* pumpRetryClock(
297+
adapter.sendTurn({ threadId, input: "continue", attachments: [] }).pipe(Effect.flip),
298+
);
299+
assert.equal(error._tag, "ProviderAdapterRequestError");
300+
if (error._tag === "ProviderAdapterRequestError") {
301+
assert.equal(error.detail, "Cursor reported a transport failure.");
302+
}
303+
assert.equal(yield* Effect.promise(() => countPromptRequests(requestLogPath)), 1);
304+
yield* adapter.stopSession(threadId);
305+
}),
306+
);
307+
308+
it.effect("settles as cancelled when interrupted while waiting to retry a transport error", () =>
309+
Effect.gen(function* () {
310+
const adapter = yield* CursorAdapter;
311+
const settings = yield* ServerSettingsService;
312+
const threadId = ThreadId.make("cursor-transport-error-interrupt");
313+
const { wrapperPath, requestLogPath } = yield* Effect.promise(() =>
314+
makeTransportFailureWrapper(),
315+
);
316+
yield* settings.updateSettings({ providers: { cursor: { binaryPath: wrapperPath } } });
317+
const runtimeEventsFiber = yield* adapter.streamEvents.pipe(
318+
Stream.filter((event) => event.threadId === threadId),
319+
Stream.takeUntil((event) => event.type === "turn.completed"),
320+
Stream.runCollect,
321+
Effect.forkChild,
322+
);
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)),
328+
Effect.forkChild,
329+
);
330+
yield* adapter.startSession({
331+
threadId,
332+
provider: ProviderDriverKind.make("cursor"),
333+
cwd: process.cwd(),
334+
runtimeMode: "full-access",
335+
});
336+
const sendTurnFiber = yield* adapter
337+
.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));
353+
const runtimeEvents = Array.from(yield* Fiber.join(runtimeEventsFiber));
354+
const completed = runtimeEvents.filter((event) => event.type === "turn.completed");
355+
assert.equal(completed.length, 1);
356+
assert.equal(String(completed[0]?.turnId), String(turn.turnId));
357+
assert.deepStrictEqual(completed[0]?.payload, {
358+
state: "cancelled",
359+
stopReason: "cancelled",
360+
});
361+
assert.equal(yield* Effect.promise(() => countPromptRequests(requestLogPath)), 1);
362+
yield* adapter.stopSession(threadId);
198363
}),
199364
);
200365

0 commit comments

Comments
 (0)