@@ -35,7 +35,11 @@ import {
3535 makeCursorAdapterV2 ,
3636 nestedToolCallFromEnvelope ,
3737} from "./CursorAdapterV2.ts" ;
38- import { isCursorCancellationError , loggedCursorAgentOptions } from "./CursorAgentSdk.ts" ;
38+ import {
39+ CursorAgentSdkRunnerError ,
40+ isCursorCancellationError ,
41+ loggedCursorAgentOptions ,
42+ } from "./CursorAgentSdk.ts" ;
3943
4044const decodeCursorSettings = Schema . decodeEffect ( CursorSettings ) ;
4145
@@ -61,6 +65,8 @@ describe("CursorAdapterV2", () => {
6165 const opened : Array < CursorAgentSdkOpenInput > = [ ] ;
6266 const sent : Array < CursorAgentSdkSendInput < never > [ "options" ] > = [ ] ;
6367 const calls : Array < string > = [ ] ;
68+ let failNextResume = false ;
69+ let failNextClose = false ;
6470 const setCredential = (
6571 authorizationHeader : string ,
6672 endpoint = "http://127.0.0.1:43123/mcp" ,
@@ -87,15 +93,29 @@ describe("CursorAdapterV2", () => {
8793 runner : {
8894 assertComplete : Effect . void ,
8995 open : ( input ) =>
90- Effect . sync ( ( ) => {
96+ Effect . gen ( function * ( ) {
9197 opened . push ( input ) ;
9298 calls . push ( input . operation ) ;
9399 if ( input . operation === "resume" ) assert . equal ( input . agentId , "native-cursor-mcp" ) ;
100+ if ( failNextResume ) {
101+ failNextResume = false ;
102+ return yield * new CursorAgentSdkRunnerError ( {
103+ method : "agent.resume" ,
104+ cause : new Error ( "Synthetic resume failure" ) ,
105+ } ) ;
106+ }
94107 return {
95108 agentId : "native-cursor-mcp" ,
96109 listMessages : Effect . succeed ( [ ] ) ,
97- close : Effect . sync ( ( ) => {
110+ close : Effect . gen ( function * ( ) {
98111 calls . push ( "close" ) ;
112+ if ( failNextClose ) {
113+ failNextClose = false ;
114+ return yield * new CursorAgentSdkRunnerError ( {
115+ method : "agent.close" ,
116+ cause : new Error ( "Synthetic close failure" ) ,
117+ } ) ;
118+ }
99119 } ) ,
100120 send : ( sendInput ) =>
101121 Effect . sync ( ( ) => {
@@ -250,6 +270,31 @@ describe("CursorAdapterV2", () => {
250270 yield * turn ( ) ;
251271 assert . equal ( opened . length , beforeResumedSend ) ;
252272 assert . deepEqual ( calls . slice ( - 2 ) , [ "resume" , "send" ] ) ;
273+
274+ // A failed replacement must not send through, or cache, the old handle.
275+ // Exercise this after both successful and failed close attempts.
276+ setCredential ( "Bearer synthetic-retry" ) ;
277+ failNextResume = true ;
278+ failNextClose = initialMcp ;
279+ const sentBeforeFailure = sent . length ;
280+ const openedBeforeFailure = opened . length ;
281+ const failure = yield * Effect . flip ( turn ( ) ) ;
282+ assert . equal ( failure . _tag , "ProviderAdapterTurnStartError" ) ;
283+ assert . equal ( sent . length , sentBeforeFailure ) ;
284+ assert . equal ( opened . length , openedBeforeFailure + 1 ) ;
285+ assert . deepEqual ( calls . slice ( - 2 ) , [ "close" , "resume" ] ) ;
286+ assertCredential ( "Bearer synthetic-retry" ) ;
287+
288+ yield * turn ( ) ;
289+ assert . equal ( opened . length , openedBeforeFailure + 2 ) ;
290+ assert . equal ( sent . length , sentBeforeFailure + 1 ) ;
291+ assert . deepEqual ( calls . slice ( - 3 ) , [ "resume" , "resume" , "send" ] ) ;
292+ assertCredential ( "Bearer synthetic-retry" ) ;
293+ for ( const attempt of opened ) {
294+ const logged = loggedCursorAgentOptions ( attempt . options ) ;
295+ assert . notProperty ( logged , "apiKey" ) ;
296+ assert . notProperty ( logged , "mcpServers" ) ;
297+ }
253298 } ) . pipe ( Effect . scoped , Effect . provide ( Layer . merge ( NodeServices . layer , IdAllocator . layer ) ) ) ,
254299 ) ;
255300
0 commit comments