@@ -676,6 +676,77 @@ describe("OpenCodeAdapterV2", () => {
676676 } ) . pipe ( Effect . provide ( IdAllocator . layer ) , Effect . scoped ) ,
677677 ) ;
678678
679+ it . effect (
680+ "disconnects again once a shared MCP server add lands after the background wait gave up" ,
681+ ( ) =>
682+ Effect . gen ( function * ( ) {
683+ const scope = yield * Scope . make ( ) ;
684+ const threadId = ThreadId . make ( "thread-opencode-latest-add" ) ;
685+ McpProviderSession . setMcpProviderSession ( {
686+ environmentId : EnvironmentId . make ( "environment-opencode-latest-add" ) ,
687+ threadId,
688+ providerSessionId : "mcp-session-opencode-latest-add" ,
689+ providerInstanceId : ProviderInstanceId . make ( "opencode-latest-add" ) ,
690+ endpoint : "http://127.0.0.1:43123/mcp" ,
691+ authorizationHeader : "Bearer thread-credential" ,
692+ browserToolsAvailable : false ,
693+ sharedServers : [
694+ { name : "gateway" , url : "http://127.0.0.1:3050/mcp" , enabled : true , headers : { } } ,
695+ ] ,
696+ } ) ;
697+ yield * Effect . addFinalizer ( ( ) =>
698+ Effect . sync ( ( ) => McpProviderSession . clearMcpProviderSession ( threadId ) ) ,
699+ ) ;
700+ const calls : string [ ] = [ ] ;
701+ const addAnswered = promiseGate < void > ( ) ;
702+ const firstDisconnect = promiseGate < void > ( ) ;
703+ const secondDisconnect = promiseGate < void > ( ) ;
704+ const opened = yield * makeOpenCodeRuntimeHarness ( "latest-add" , "root" , {
705+ event : { subscribe : async ( ) => ( { stream : asyncEventStream ( ) . stream } ) } ,
706+ session : {
707+ create : async ( ) => ( { data : { id : "root" , time : { created : 1 , updated : 1 } } } ) ,
708+ abort : async ( ) => ( { data : true } ) ,
709+ children : async ( ) => ( { data : [ ] } ) ,
710+ } ,
711+ mcp : {
712+ add : async ( { name } : { name : string } ) => {
713+ calls . push ( `add:${ name } ` ) ;
714+ await addAnswered . promise ;
715+ calls . push ( `added:${ name } ` ) ;
716+ return { data : { } } ;
717+ } ,
718+ disconnect : async ( { name } : { name : string } ) => {
719+ calls . push ( `disconnect:${ name } ` ) ;
720+ ( calls . filter ( ( call ) => call . startsWith ( "disconnect:" ) ) . length === 1
721+ ? firstDisconnect
722+ : secondDisconnect
723+ ) . resolve ( ) ;
724+ return { data : true } ;
725+ } ,
726+ } ,
727+ } ) . pipe ( Effect . provideService ( Scope . Scope , scope ) , Effect . forkChild ) ;
728+ yield * TestClock . adjust ( "5 seconds" ) ;
729+ yield * Fiber . join ( opened ) ;
730+ const closed = yield * Scope . close ( scope , Exit . void ) . pipe ( Effect . forkChild ) ;
731+ yield * TestClock . adjust ( "5 seconds" ) ;
732+ yield * Fiber . join ( closed ) ;
733+ // The background wait gives up and disconnects best-effort.
734+ yield * TestClock . adjust ( "2 minutes" ) ;
735+ yield * Effect . promise ( ( ) => firstDisconnect . promise ) ;
736+ // OpenCode lands the add later still; it is disconnected again.
737+ addAnswered . resolve ( ) ;
738+ yield * Effect . promise ( ( ) => secondDisconnect . promise ) ;
739+
740+ const gateway = sharedMcpServerName ( threadId , "gateway" ) ;
741+ assert . deepEqual ( calls , [
742+ `add:${ gateway } ` ,
743+ `disconnect:${ gateway } ` ,
744+ `added:${ gateway } ` ,
745+ `disconnect:${ gateway } ` ,
746+ ] ) ;
747+ } ) . pipe ( Effect . provide ( IdAllocator . layer ) , Effect . scoped ) ,
748+ ) ;
749+
679750 it . effect . each ( [ "enumeration" , "abort" , "not-found" , "timeout" ] as const ) (
680751 "reports descendant cleanup %s" ,
681752 ( failure ) =>
0 commit comments