@@ -327,6 +327,141 @@ it("routes only enrolled workflow members after the root turn ends", () => {
327327 ) ;
328328} ) ;
329329
330+ it ( "moves a child thread to the run that shows its subagent now" , ( ) => {
331+ const threadId = ThreadId . make ( "thread:handoff" ) ;
332+ const rootProviderTurnId = ProviderTurnId . make ( "provider-turn:handoff" ) ;
333+ const identity : RunExecutionService . ProviderEventRouteIdentity = {
334+ threadId,
335+ runId : RunId . make ( "run:handoff:1" ) ,
336+ attemptId : RunAttemptId . make ( "attempt:handoff:1" ) ,
337+ providerThreadId : ProviderThreadId . make ( "provider-thread:handoff" ) ,
338+ } ;
339+ const resumedChild = ThreadId . make ( "thread:handoff:resumed" ) ;
340+ const siblingChild = ThreadId . make ( "thread:handoff:sibling" ) ;
341+ const providerThreadOf = ( childThreadId : ThreadId ) =>
342+ ProviderThreadId . make ( `provider-thread:${ childThreadId } ` ) ;
343+ const subagent = ( childThreadId : ThreadId , runId : RunId ) : OrchestrationV2Subagent => ( {
344+ id : NodeId . make ( `node:${ childThreadId } ` ) ,
345+ threadId,
346+ runId,
347+ parentNodeId : NodeId . make ( "node:handoff" ) ,
348+ origin : "provider_native" ,
349+ createdBy : "agent" ,
350+ driver,
351+ providerInstanceId : ProviderInstanceId . make ( "codex" ) ,
352+ providerThreadId : providerThreadOf ( childThreadId ) ,
353+ childThreadId,
354+ nativeTaskRef : null ,
355+ prompt : "Work" ,
356+ title : "Work" ,
357+ model : null ,
358+ status : "running" ,
359+ result : null ,
360+ startedAt : null ,
361+ completedAt : null ,
362+ updatedAt : DateTime . makeUnsafe ( 0 ) ,
363+ } ) ;
364+ const childMessage = ( childThreadId : ThreadId ) =>
365+ ( {
366+ type : "message.updated" ,
367+ driver,
368+ message : {
369+ id : MessageId . make ( `message:${ childThreadId } ` ) ,
370+ threadId : childThreadId ,
371+ runId : null ,
372+ } ,
373+ } ) as ProviderAdapter . ProviderAdapterV2Event ;
374+ const childTurn = ( childThreadId : ThreadId , ordinal : number ) =>
375+ ( {
376+ type : "provider_turn.updated" ,
377+ driver,
378+ providerTurn : {
379+ id : ProviderTurnId . make ( `provider-turn:${ childThreadId } :${ ordinal } ` ) ,
380+ providerThreadId : providerThreadOf ( childThreadId ) ,
381+ runAttemptId : null ,
382+ status : "running" ,
383+ } ,
384+ } ) as ProviderAdapter . ProviderAdapterV2Event ;
385+ const ended = RunExecutionService . routeProviderEvent (
386+ {
387+ type : "turn.terminal" ,
388+ driver,
389+ providerThreadId : identity . providerThreadId ,
390+ providerTurnId : rootProviderTurnId ,
391+ runOrdinal : 1 ,
392+ status : "completed" ,
393+ failure : null ,
394+ threadDisposition : "reusable" ,
395+ } ,
396+ identity ,
397+ RunExecutionService . makeProviderEventRoutingState ( {
398+ identity,
399+ providerTurnId : rootProviderTurnId ,
400+ relatedThreadIds : [ resumedChild , siblingChild ] ,
401+ relatedProviderThreadIds : [ providerThreadOf ( resumedChild ) , providerThreadOf ( siblingChild ) ] ,
402+ } ) ,
403+ ) [ 1 ] ;
404+ const [ firstTurnAccepted , withChildTurn ] = RunExecutionService . routeProviderEvent (
405+ childTurn ( resumedChild , 1 ) ,
406+ identity ,
407+ ended ,
408+ ) ;
409+ assert . isTrue ( firstTurnAccepted ) ;
410+ const [ movedAccepted , afterMove ] = RunExecutionService . routeProviderEvent (
411+ {
412+ type : "subagent.updated" ,
413+ driver,
414+ subagent : subagent ( resumedChild , RunId . make ( "run:handoff:2" ) ) ,
415+ } ,
416+ identity ,
417+ withChildTurn ,
418+ ) ;
419+ assert . isFalse ( movedAccepted ) ;
420+ assert . isFalse (
421+ RunExecutionService . routeProviderEvent ( childMessage ( resumedChild ) , identity , afterMove ) [ 0 ] ,
422+ ) ;
423+ assert . isTrue (
424+ RunExecutionService . routeProviderEvent ( childMessage ( siblingChild ) , identity , afterMove ) [ 0 ] ,
425+ ) ;
426+ assert . isFalse (
427+ RunExecutionService . routeProviderEvent ( childTurn ( resumedChild , 1 ) , identity , afterMove ) [ 0 ] ,
428+ ) ;
429+ assert . isFalse (
430+ RunExecutionService . routeProviderEvent ( childTurn ( resumedChild , 2 ) , identity , afterMove ) [ 0 ] ,
431+ ) ;
432+ assert . isTrue (
433+ RunExecutionService . routeProviderEvent ( childTurn ( siblingChild , 1 ) , identity , afterMove ) [ 0 ] ,
434+ ) ;
435+ assert . isTrue (
436+ RunExecutionService . routeProviderEvent (
437+ { type : "subagent.updated" , driver, subagent : subagent ( siblingChild , identity . runId ) } ,
438+ identity ,
439+ afterMove ,
440+ ) [ 0 ] ,
441+ ) ;
442+ const resuming : RunExecutionService . ProviderEventRouteIdentity = {
443+ ...identity ,
444+ runId : RunId . make ( "run:handoff:2" ) ,
445+ attemptId : RunAttemptId . make ( "attempt:handoff:2" ) ,
446+ } ;
447+ const resumingState = RunExecutionService . makeProviderEventRoutingState ( {
448+ identity : resuming ,
449+ providerTurnId : ProviderTurnId . make ( "provider-turn:handoff:2" ) ,
450+ } ) ;
451+ assert . isFalse (
452+ RunExecutionService . routeProviderEvent ( childMessage ( resumedChild ) , resuming , resumingState ) [ 0 ] ,
453+ ) ;
454+ const [ moveAccepted , afterTake ] = RunExecutionService . routeProviderEvent (
455+ { type : "subagent.updated" , driver, subagent : subagent ( resumedChild , resuming . runId ) } ,
456+ resuming ,
457+ resumingState ,
458+ ) ;
459+ assert . isTrue ( moveAccepted ) ;
460+ assert . isTrue (
461+ RunExecutionService . routeProviderEvent ( childMessage ( resumedChild ) , resuming , afterTake ) [ 0 ] ,
462+ ) ;
463+ } ) ;
464+
330465it ( "does not route a superseded attempt through a reused provider thread" , ( ) => {
331466 const threadId = ThreadId . make ( "thread:shared-runtime:restart" ) ;
332467 const providerThreadId = ProviderThreadId . make ( "provider-thread:shared-runtime:restart" ) ;
@@ -1740,6 +1875,65 @@ it.effect("keeps ingesting a child thread's late background item completion", ()
17401875 } ) ,
17411876) ;
17421877
1878+ it . effect . each ( [ null , "run:nested-sdk" ] as const ) (
1879+ "releases a settled run when its live child transfers to another run (%s)" ,
1880+ ( nestedRunId ) =>
1881+ Effect . gen ( function * ( ) {
1882+ const consumed = yield * Deferred . make < void > ( ) ;
1883+ const result = yield * Effect . raceFirst (
1884+ runBackgroundItemScenario (
1885+ "child-transfer-release" ,
1886+ ( ids ) => {
1887+ const running = subagentEvent ( ids , "running" ) ;
1888+ if ( running . type !== "subagent.updated" ) throw new Error ( "Missing subagent" ) ;
1889+ const linked = {
1890+ ...running ,
1891+ subagent : {
1892+ ...running . subagent ,
1893+ origin : "provider_native" as const ,
1894+ childThreadId : ids . childThreadId ,
1895+ providerThreadId : null ,
1896+ } ,
1897+ } ;
1898+ return [
1899+ childThreadCreatedEvent ( ids ) ,
1900+ linked ,
1901+ {
1902+ ...linked ,
1903+ subagent : {
1904+ ...linked . subagent ,
1905+ id : NodeId . make ( "node:child-transfer-release:nested" ) ,
1906+ threadId : ids . childThreadId ,
1907+ runId : nestedRunId === null ? null : RunId . make ( nestedRunId ) ,
1908+ childThreadId : null ,
1909+ } ,
1910+ } ,
1911+ childBackgroundTurnItemEvent ( ids , "running" , 1 ) ,
1912+ rootTerminalEvent ( ids , "completed" ) ,
1913+ { ...linked , subagent : { ...linked . subagent , runId : RunId . make ( "run:replacement" ) } } ,
1914+ ] ;
1915+ } ,
1916+ {
1917+ keepEventStreamOpen : true ,
1918+ onEventsConsumed : Deferred . succeed ( consumed , undefined ) . pipe ( Effect . asVoid ) ,
1919+ } ,
1920+ ) . pipe ( Effect . map ( Option . some ) ) ,
1921+ Deferred . await ( consumed ) . pipe ( Effect . as ( Option . none ( ) ) ) ,
1922+ ) ;
1923+ assert . isTrue (
1924+ Option . isSome ( result ) ,
1925+ "the transferred child must release its prior subscription" ,
1926+ ) ;
1927+ if ( Option . isSome ( result ) )
1928+ assert . deepEqual ( result . value , [
1929+ "subagent:running" ,
1930+ "subagent:running" ,
1931+ "turn_item:running" ,
1932+ "root-finalized" ,
1933+ ] ) ;
1934+ } ) ,
1935+ ) ;
1936+
17431937it . effect ( "keeps ingesting until the last of several background items terminalizes" , ( ) =>
17441938 Effect . gen ( function * ( ) {
17451939 const secondItemId = TurnItemId . make ( "turn-item:bg-multi:second" ) ;
@@ -4377,6 +4571,7 @@ function runBackgroundItemScenario(
43774571 ReadonlyArray < { readonly id : TurnItemId ; readonly runId : RunId } >
43784572 > ;
43794573 readonly onSubscribe ?: Effect . Effect < void > ;
4574+ readonly onEventsConsumed ?: Effect . Effect < void > ;
43804575 readonly onFinalized ?: (
43814576 events : ReadonlyArray < OrchestrationV2DomainEvent > ,
43824577 ) => Effect . Effect < void > ;
@@ -4444,7 +4639,11 @@ function runBackgroundItemScenario(
44444639 events : Stream . empty ,
44454640 subscribeEvents : Effect . gen ( function * ( ) {
44464641 yield * options ?. onSubscribe ?? Effect . void ;
4447- const events = Stream . fromIterable ( makeEvents ( ids ) ) ;
4642+ const events = Stream . fromIterable ( makeEvents ( ids ) ) . pipe (
4643+ Stream . concat (
4644+ Stream . fromEffect ( options ?. onEventsConsumed ?? Effect . void ) . pipe ( Stream . drain ) ,
4645+ ) ,
4646+ ) ;
44484647 return {
44494648 events :
44504649 options ?. keepEventStreamOpen === true
0 commit comments