@@ -2,6 +2,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
22import { assert , describe , it } from "@effect/vitest" ;
33import { ProviderSessionId , ThreadId } from "@t3tools/contracts" ;
44import * as Effect from "effect/Effect" ;
5+ import * as Fiber from "effect/Fiber" ;
56import * as Layer from "effect/Layer" ;
67import { vi } from "vite-plus/test" ;
78
@@ -22,7 +23,9 @@ const cursorSdkMock = vi.hoisted(() => {
2223 model : { id : "default" } ,
2324 durationMs : 1 ,
2425 } ) ) ;
26+ let rejectRunWait : ( ( cause : unknown ) => void ) | undefined ;
2527 const runCancel = vi . fn ( async ( ) => { } ) ;
28+ let onDelta : ( ( input : { readonly update : unknown } ) => Promise < void > ) | undefined ;
2629 const agentClose = vi . fn ( ( ) => {
2730 closeExecutionOrder . push ( "sdk:agent.close" ) ;
2831 if ( agentCloseFailure !== undefined ) {
@@ -35,6 +38,7 @@ const cursorSdkMock = vi.hoisted(() => {
3538 options : { readonly onDelta ?: ( input : { readonly update : unknown } ) => Promise < void > } ,
3639 ) => {
3740 runOperations . push ( "run.start" ) ;
41+ onDelta = options . onDelta ;
3842 if ( staleRunActive ) {
3943 throw new Error ( "Agent agent-cursor-agent-sdk-test already has active run" ) ;
4044 }
@@ -84,6 +88,16 @@ const cursorSdkMock = vi.hoisted(() => {
8488 runCancel,
8589 runWait,
8690 send,
91+ deliverDelta : async ( update : unknown ) => onDelta ?.( { update } ) ,
92+ setRunWaitPending : ( ) => {
93+ runWait . mockImplementationOnce (
94+ ( ) =>
95+ new Promise ( ( _resolve , reject ) => {
96+ rejectRunWait = reject ;
97+ } ) ,
98+ ) ;
99+ } ,
100+ rejectRunWait : ( cause : unknown ) => rejectRunWait ?.( cause ) ,
87101 setAgentCloseFailure : ( failure : unknown ) => {
88102 agentCloseFailure = failure ;
89103 } ,
@@ -132,6 +146,85 @@ const layerTest = CursorAgentSdk.layer.pipe(
132146) ;
133147
134148describe ( "CursorAgentSdkRunner" , ( ) => {
149+ it . effect ( "keeps dispatching after a callback failure and reports its method" , ( ) =>
150+ Effect . gen ( function * ( ) {
151+ const runner = yield * CursorAgentSdk . CursorAgentSdkRunner ;
152+ const session = yield * runner . open ( {
153+ operation : "create" ,
154+ options : { model : { id : "default" } , mode : "agent" , local : { cwd : process . cwd ( ) } } ,
155+ threadId : ThreadId . make ( "thread-cursor-agent-sdk-callback-failure-test" ) ,
156+ providerSessionId : ProviderSessionId . make (
157+ "provider-session-cursor-agent-sdk-callback-failure-test" ,
158+ ) ,
159+ } ) ;
160+ const received : Array < string > = [ ] ;
161+ const run = yield * session . send ( {
162+ message : "continue" ,
163+ onDelta : ( update ) => {
164+ if ( update . type === "text-delta" && update . text === "bad" ) {
165+ return Effect . die ( new Error ( "local callback failed" ) ) ;
166+ }
167+ if ( update . type === "text-delta" ) received . push ( update . text ) ;
168+ return Effect . void ;
169+ } ,
170+ } ) ;
171+
172+ yield * Effect . promise ( ( ) => cursorSdkMock . deliverDelta ( { type : "text-delta" , text : "bad" } ) ) ;
173+ yield * Effect . promise ( ( ) =>
174+ cursorSdkMock . deliverDelta ( { type : "text-delta" , text : "after failure" } ) ,
175+ ) ;
176+ const error = yield * run . wait . pipe ( Effect . flip ) ;
177+
178+ assert . deepEqual ( received , [ "after failure" ] ) ;
179+ assert . equal ( error . method , "run.onDelta" ) ;
180+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
181+ ) ;
182+
183+ it . effect ( "waits for an in-flight callback before classifying a run.wait failure" , ( ) =>
184+ Effect . gen ( function * ( ) {
185+ cursorSdkMock . setRunWaitPending ( ) ;
186+ cursorSdkMock . runCancel . mockClear ( ) ;
187+ let startCallback ! : ( ) => void ;
188+ const callbackStarted = new Promise < void > ( ( resolve ) => {
189+ startCallback = resolve ;
190+ } ) ;
191+ let failCallback ! : ( ) => void ;
192+ const runner = yield * CursorAgentSdk . CursorAgentSdkRunner ;
193+ const session = yield * runner . open ( {
194+ operation : "create" ,
195+ options : { model : { id : "default" } , mode : "agent" , local : { cwd : process . cwd ( ) } } ,
196+ threadId : ThreadId . make ( "thread-cursor-agent-sdk-wait-race-test" ) ,
197+ providerSessionId : ProviderSessionId . make (
198+ "provider-session-cursor-agent-sdk-wait-race-test" ,
199+ ) ,
200+ } ) ;
201+ const run = yield * session . send ( {
202+ message : "continue" ,
203+ onDelta : ( update ) => {
204+ if ( update . type !== "text-delta" || update . text !== "pending" ) return Effect . void ;
205+ return Effect . promise (
206+ ( ) =>
207+ new Promise < void > ( ( _resolve , reject ) => {
208+ failCallback = ( ) => reject ( new Error ( "local callback failed" ) ) ;
209+ startCallback ( ) ;
210+ } ) ,
211+ ) ;
212+ } ,
213+ } ) ;
214+
215+ const wait = yield * run . wait . pipe ( Effect . flip , Effect . forkChild ( { startImmediately : true } ) ) ;
216+ const delivery = cursorSdkMock . deliverDelta ( { type : "text-delta" , text : "pending" } ) ;
217+ yield * Effect . promise ( ( ) => callbackStarted ) ;
218+ yield * Effect . sync ( ( ) => cursorSdkMock . rejectRunWait ( new Error ( "SDK wait failed" ) ) ) ;
219+ yield * Effect . sync ( ( ) => failCallback ( ) ) ;
220+ yield * Effect . promise ( ( ) => delivery ) ;
221+
222+ const error = yield * Fiber . join ( wait ) ;
223+ assert . equal ( error . method , "run.onDelta" ) ;
224+ assert . equal ( cursorSdkMock . runCancel . mock . calls . length , 0 ) ;
225+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
226+ ) ;
227+
135228 it . effect ( "retries a resumed send after cancelling its abandoned local run" , ( ) =>
136229 Effect . gen ( function * ( ) {
137230 cursorSdkMock . runOperations . length = 0 ;
0 commit comments