@@ -11,6 +11,7 @@ import * as Deferred from "effect/Deferred";
1111import * as Effect from "effect/Effect" ;
1212import * as Fiber from "effect/Fiber" ;
1313import * as Layer from "effect/Layer" ;
14+ import * as Logger from "effect/Logger" ;
1415import * as Schema from "effect/Schema" ;
1516import * as Stream from "effect/Stream" ;
1617import * 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 ) ;
0 commit comments