@@ -71,6 +71,9 @@ const runtimeMock = {
7171 | null ,
7272 sessionChildrenCalls : [ ] as string [ ] ,
7373 sessionChildrenById : new Map < string , Array < { id : string } > > ( ) ,
74+ sessionChildrenImplementation : null as
75+ | ( ( sessionID : string ) => Promise < Array < { id : string } > > )
76+ | null ,
7477 closeCalls : [ ] as string [ ] ,
7578 revertCalls : [ ] as Array < { sessionID : string ; messageID ?: string } > ,
7679 messageCalls : [ ] as Array < { sessionID : string ; messageID : string } > ,
@@ -120,6 +123,7 @@ const runtimeMock = {
120123 this . state . abortImplementation = null ;
121124 this . state . sessionChildrenCalls . length = 0 ;
122125 this . state . sessionChildrenById . clear ( ) ;
126+ this . state . sessionChildrenImplementation = null ;
123127 this . state . closeCalls . length = 0 ;
124128 this . state . revertCalls . length = 0 ;
125129 this . state . messageCalls . length = 0 ;
@@ -258,7 +262,11 @@ const OpenCodeRuntimeTestDouble: OpenCodeRuntimeShape = {
258262 } ,
259263 children : async ( { sessionID } : { sessionID : string } ) => {
260264 runtimeMock . state . sessionChildrenCalls . push ( sessionID ) ;
261- return { data : runtimeMock . state . sessionChildrenById . get ( sessionID ) ?? [ ] } ;
265+ return {
266+ data : runtimeMock . state . sessionChildrenImplementation
267+ ? await runtimeMock . state . sessionChildrenImplementation ( sessionID )
268+ : ( runtimeMock . state . sessionChildrenById . get ( sessionID ) ?? [ ] ) ,
269+ } ;
262270 } ,
263271 status : async ( ) => {
264272 runtimeMock . state . sessionStatusCalls += 1 ;
@@ -3131,6 +3139,79 @@ it.layer(OpenCodeAdapterTestLayer)("OpenCodeAdapterLive", (it) => {
31313139 } ) ,
31323140 ) ;
31333141
3142+ it . effect ( "limits SDK requests across the full OpenCode child tree" , ( ) =>
3143+ Effect . gen ( function * ( ) {
3144+ const adapter = yield * OpenCodeAdapter ;
3145+ const threadId = asThreadId ( "thread-interrupt-child-request-limit" ) ;
3146+ const rootSessionId = "http://127.0.0.1:9999/session" ;
3147+ const requestRelease = promiseWithResolvers < void > ( ) ;
3148+ const limitReached = promiseWithResolvers < void > ( ) ;
3149+ let inFlight = 0 ;
3150+ let maxInFlight = 0 ;
3151+ const holdRequest = async < T > ( result : T ) : Promise < T > => {
3152+ inFlight += 1 ;
3153+ maxInFlight = Math . max ( maxInFlight , inFlight ) ;
3154+ if ( inFlight === 8 ) {
3155+ limitReached . resolve ( undefined ) ;
3156+ }
3157+ await requestRelease . promise ;
3158+ inFlight -= 1 ;
3159+ return result ;
3160+ } ;
3161+
3162+ const children = Array . from ( { length : 8 } , ( _ , index ) => ( { id : `ses_child_${ index } ` } ) ) ;
3163+ runtimeMock . state . sessionChildrenById . set ( rootSessionId , children ) ;
3164+ for ( const child of children . slice ( 1 ) ) {
3165+ runtimeMock . state . sessionChildrenById . set (
3166+ child . id ,
3167+ Array . from ( { length : 8 } , ( _ , index ) => ( { id : `${ child . id } _nested_${ index } ` } ) ) ,
3168+ ) ;
3169+ }
3170+ runtimeMock . state . abortImplementation = async ( sessionID ) => {
3171+ if ( sessionID . includes ( "_nested_" ) ) {
3172+ await holdRequest ( undefined ) ;
3173+ }
3174+ } ;
3175+ runtimeMock . state . sessionChildrenImplementation = async ( sessionID ) => {
3176+ if ( sessionID === "ses_child_0" ) {
3177+ return await holdRequest ( [ ] ) ;
3178+ }
3179+ return runtimeMock . state . sessionChildrenById . get ( sessionID ) ?? [ ] ;
3180+ } ;
3181+
3182+ yield * adapter . startSession ( {
3183+ provider : ProviderDriverKind . make ( "opencode" ) ,
3184+ threadId,
3185+ runtimeMode : "full-access" ,
3186+ } ) ;
3187+ const turn = yield * adapter . sendTurn ( {
3188+ threadId,
3189+ input : "Run a nested child tree" ,
3190+ modelSelection : createModelSelection (
3191+ ProviderInstanceId . make ( "opencode" ) ,
3192+ "opencode/kimi-k3" ,
3193+ ) ,
3194+ } ) ;
3195+
3196+ const interruptFiber = yield * adapter
3197+ . interruptTurn ( threadId , turn . turnId )
3198+ . pipe ( Effect . forkChild ) ;
3199+ yield * Effect . promise ( ( ) => limitReached . promise ) ;
3200+ yield * Effect . yieldNow ;
3201+
3202+ NodeAssert . equal ( inFlight , 8 ) ;
3203+ NodeAssert . equal ( maxInFlight , 8 ) ;
3204+
3205+ requestRelease . resolve ( undefined ) ;
3206+ yield * Fiber . join ( interruptFiber ) ;
3207+
3208+ runtimeMock . state . abortImplementation = null ;
3209+ runtimeMock . state . sessionChildrenImplementation = null ;
3210+ runtimeMock . state . sessionChildrenById . clear ( ) ;
3211+ yield * adapter . stopSession ( threadId ) ;
3212+ } ) ,
3213+ ) ;
3214+
31343215 it . effect ( "attempts every child abort and fails the interrupt when one child abort fails" , ( ) =>
31353216 Effect . gen ( function * ( ) {
31363217 const adapter = yield * OpenCodeAdapter ;
0 commit comments