@@ -311,6 +311,52 @@ describe("AcpMcpOverAcpBridge", () => {
311311 } ) . pipe ( Effect . scoped ) ,
312312 ) ;
313313
314+ it . effect ( "keeps each initialize on its own session while the replaced one is closing" , ( ) =>
315+ Effect . gen ( function * ( ) {
316+ const deleteStarted = Promise . withResolvers < void > ( ) ;
317+ const deleteResponse = Promise . withResolvers < void > ( ) ;
318+ let sessions = 0 ;
319+ const bridge = yield * makeAcpMcpOverAcpBridge ( {
320+ endpoint : "http://127.0.0.1:1/mcp" ,
321+ authorization : "Bearer bridge-test" ,
322+ allocateConnectionId : Effect . succeed ( "connection-1" ) ,
323+ fetchImplementation : async ( _url , init ) => {
324+ const sessionId = new Headers ( init ?. headers ) . get ( "mcp-session-id" ) ;
325+ if ( init ?. method === "DELETE" ) {
326+ if ( sessionId === "session-1" ) {
327+ deleteStarted . resolve ( ) ;
328+ await deleteResponse . promise ;
329+ }
330+ return new Response ( null , { status : 204 } ) ;
331+ }
332+ const request = JSON . parse ( String ( init ?. body ) ) as { readonly id ?: unknown } ;
333+ // The MCP endpoint refuses an initialize that carries an existing session.
334+ const outcome =
335+ sessionId === null
336+ ? { result : { protocolVersion : "2025-06-18" } }
337+ : { error : { code : - 32600 , message : "initialize must not include a session" } } ;
338+ return new Response ( JSON . stringify ( { jsonrpc : "2.0" , id : request . id , ...outcome } ) , {
339+ status : sessionId === null ? 200 : 400 ,
340+ headers : {
341+ "content-type" : "application/json" ,
342+ ...( sessionId === null ? { "mcp-session-id" : `session-${ ++ sessions } ` } : { } ) ,
343+ } ,
344+ } ) ;
345+ } ,
346+ } ) ;
347+ const initialize = ( requestId : string ) =>
348+ bridge . message ( { serverId : "t3-code" , requestId, method : "initialize" } ) ;
349+ const initialized = { result : { protocolVersion : "2025-06-18" } } ;
350+
351+ yield * initialize ( "mcp-1" ) ;
352+ const waiting = yield * initialize ( "mcp-2" ) . pipe ( Effect . forkChild ) ;
353+ yield * Effect . promise ( ( ) => deleteStarted . promise ) ;
354+ expect ( yield * initialize ( "mcp-3" ) ) . toEqual ( initialized ) ;
355+ deleteResponse . resolve ( ) ;
356+ expect ( yield * Fiber . join ( waiting ) ) . toEqual ( initialized ) ;
357+ } ) . pipe ( Effect . scoped ) ,
358+ ) ;
359+
314360 it . effect ( "closes the session of an initialize that a newer initialize replaced in flight" , ( ) =>
315361 Effect . gen ( function * ( ) {
316362 const firstStarted = Promise . withResolvers < void > ( ) ;
0 commit comments