@@ -41,6 +41,7 @@ import * as DateTime from "effect/DateTime";
4141import * as Deferred from "effect/Deferred" ;
4242import * as Effect from "effect/Effect" ;
4343import * as FileSystem from "effect/FileSystem" ;
44+ import * as Fiber from "effect/Fiber" ;
4445import * as Layer from "effect/Layer" ;
4546import * as Option from "effect/Option" ;
4647import * as PubSub from "effect/PubSub" ;
@@ -253,6 +254,7 @@ function makeDeterministicAdapter(input: {
253254 } ,
254255 ] ) ;
255256 const eventTime = yield * DateTime . now ;
257+ // This fixture assigns one native turn per app run.
256258 const providerTurnId = ProviderTurnId . make (
257259 `provider-turn:${ input . instanceId } :${ turnInput . threadId } :${ turnInput . runOrdinal } ` ,
258260 ) ;
@@ -272,7 +274,7 @@ function makeDeterministicAdapter(input: {
272274 nativeId : `native-turn:${ turnInput . threadId } :${ turnInput . runOrdinal } ` ,
273275 strength : "strong" ,
274276 } ,
275- ordinal : turnInput . providerTurnOrdinal ,
277+ ordinal : turnInput . runOrdinal ,
276278 status : "running" ,
277279 startedAt : eventTime ,
278280 completedAt : null ,
@@ -300,7 +302,7 @@ function makeDeterministicAdapter(input: {
300302 nativeId : `native-turn:${ turnInput . threadId } :${ turnInput . runOrdinal } ` ,
301303 strength : "strong" ,
302304 } ,
303- ordinal : turnInput . providerTurnOrdinal ,
305+ ordinal : turnInput . runOrdinal ,
304306 status : "completed" ,
305307 startedAt : eventTime ,
306308 completedAt : eventTime ,
@@ -366,7 +368,7 @@ function makeDeterministicAdapter(input: {
366368 nativeId : `native-turn:${ turnInput . threadId } :${ turnInput . runOrdinal } ` ,
367369 strength : "strong" ,
368370 } ,
369- ordinal : turnInput . providerTurnOrdinal ,
371+ ordinal : turnInput . runOrdinal ,
370372 status : "interrupted" ,
371373 startedAt : completedAt ,
372374 completedAt,
@@ -3898,9 +3900,13 @@ describe("orchestrator MCP toolkit", () => {
38983900 } ) ,
38993901 ) ,
39003902 ) ;
3901- it . live . each ( [ "always" , "settled_only" ] as const ) (
3902- "wakes the parent for completed child follow-ups after an original %s delegation" ,
3903- ( completionWake ) =>
3903+ it . live . each ( [
3904+ { completionWake : "always" , delayedDelivery : false } ,
3905+ { completionWake : "settled_only" , delayedDelivery : false } ,
3906+ { completionWake : "always" , delayedDelivery : true } ,
3907+ ] as const ) (
3908+ "wakes the parent after an original $completionWake delegation with delayed delivery $delayedDelivery" ,
3909+ ( { completionWake, delayedDelivery } ) =>
39043910 Effect . scoped (
39053911 Effect . gen ( function * ( ) {
39063912 const cwd = yield * checkpointWorkspace ( "parent-wake-followup-comparison" ) ;
@@ -3909,35 +3915,53 @@ describe("orchestrator MCP toolkit", () => {
39093915 const initialGate = yield * Deferred . make < void > ( ) ;
39103916 const controlGate = yield * Deferred . make < void > ( ) ;
39113917 const foregroundGate = yield * Deferred . make < void > ( ) ;
3918+ const selectionEntered = yield * Deferred . make < void > ( ) ;
3919+ const selectionGate = yield * Deferred . make < void > ( ) ;
39123920 const foregroundPrompt = "Hold the parent while its child follow-up completes." ;
39133921 const initialPrompt = "Original delegated task for follow-up comparison." ;
39143922 const controlPrompt = "Control delegated task without follow-ups." ;
39153923 const followupPrompt = "Follow-up after the original delegated task completed." ;
39163924 const registryLayer = ProviderAdapterRegistry . makeLayer ( [
3917- makeDeterministicAdapter ( {
3918- instanceId : codexInstanceId ,
3919- driver : ProviderDriverKind . make ( "codex" ) ,
3920- capabilities : {
3921- ...CodexProviderCapabilitiesV2 ,
3922- turns : { ...CodexProviderCapabilitiesV2 . turns , supportsActiveSteering : false } ,
3923- } ,
3924- capturedTurns,
3925- shouldComplete : ( ) => true ,
3926- terminalGate : ( turn ) =>
3927- turn . message . text === parentPrompt
3928- ? parentGate
3929- : turn . message . text === initialPrompt
3930- ? initialGate
3931- : turn . message . text === controlPrompt
3932- ? controlGate
3933- : turn . message . text === foregroundPrompt
3934- ? foregroundGate
3935- : undefined ,
3936- response : ( turn ) => `Result: ${ turn . message . text } ` ,
3937- } ) ,
3925+ {
3926+ ...makeDeterministicAdapter ( {
3927+ instanceId : codexInstanceId ,
3928+ driver : ProviderDriverKind . make ( "codex" ) ,
3929+ capabilities : {
3930+ ...CodexProviderCapabilitiesV2 ,
3931+ turns : {
3932+ ...CodexProviderCapabilitiesV2 . turns ,
3933+ supportsActiveSteering : false ,
3934+ } ,
3935+ } ,
3936+ capturedTurns,
3937+ shouldComplete : ( ) => true ,
3938+ terminalGate : ( turn ) =>
3939+ turn . message . text === parentPrompt
3940+ ? parentGate
3941+ : turn . message . text === initialPrompt
3942+ ? initialGate
3943+ : turn . message . text === controlPrompt
3944+ ? controlGate
3945+ : turn . message . text === foregroundPrompt
3946+ ? foregroundGate
3947+ : undefined ,
3948+ response : ( turn ) => `Result: ${ turn . message . text } ` ,
3949+ } ) ,
3950+ planSelectionTransition : ( { target } ) =>
3951+ Effect . gen ( function * ( ) {
3952+ if ( target . model === "gpt-5.4-next" ) {
3953+ yield * Deferred . succeed ( selectionEntered , undefined ) ;
3954+ yield * Deferred . await ( selectionGate ) ;
3955+ }
3956+ return { type : "apply_on_next_turn" as const } ;
3957+ } ) ,
3958+ } ,
39383959 ] ) ;
39393960 const layer = makeOrchestratorV2ReplayLayerWithRegistry (
3940- { name : "parent-wake-followup-comparison" , runtimePolicyOverride : { cwd } } ,
3961+ {
3962+ name : "parent-wake-followup-comparison" ,
3963+ runtimePolicyOverride : { cwd } ,
3964+ } ,
39413965 registryLayer ,
39423966 { runContinuationWorker : true } ,
39433967 ) ;
@@ -4069,27 +4093,52 @@ describe("orchestrator MCP toolkit", () => {
40694093 ( event ) =>
40704094 event . type === "provider-turn.updated" && event . payload . status === "running" ,
40714095 ) ;
4072- const childSequence = yield * orchestrator . getThreadEventSequence ( worker . childThreadId ! ) ;
4073- yield * orchestrator . dispatch ( {
4074- type : "message.dispatch" ,
4075- createdBy : "agent" ,
4076- creationSource : "mcp" ,
4077- commandId : CommandId . make ( "wake-comparison:followup" ) ,
4078- threadId : worker . childThreadId ! ,
4079- messageId : MessageId . make ( "wake-comparison:followup" ) ,
4080- text : followupPrompt ,
4081- attachments : [ ] ,
4082- modelSelection : codexSelection ,
4083- dispatchMode : { type : "start_immediately" } ,
4084- } ) ;
4085- const followupTerminal = yield * awaitEvent (
4086- worker . childThreadId ! ,
4087- childSequence ,
4088- ( event ) => event . type === "run.updated" && event . payload . status === "completed" ,
4089- ) ;
4090- if ( followupTerminal . event . type !== "run.updated" )
4091- throw new Error ( "Missing follow-up run." ) ;
4092- const followupRunId = followupTerminal . event . payload . id ;
4096+ // A slow provider selection holds the actual parent dispatch lock.
4097+ // Both child runs finish before their result handlers acquire it.
4098+ const selectionFiber = delayedDelivery
4099+ ? yield * orchestrator
4100+ . dispatch ( {
4101+ type : "thread.model-selection.set" ,
4102+ commandId : CommandId . make ( "wake-comparison:slow-selection" ) ,
4103+ threadId : parentThreadId ,
4104+ modelSelection : {
4105+ ...codexSelection ,
4106+ model : "gpt-5.4-next" ,
4107+ } ,
4108+ } )
4109+ . pipe ( Effect . forkChild )
4110+ : undefined ;
4111+ if ( delayedDelivery ) yield * Deferred . await ( selectionEntered ) ;
4112+ const followupRunIds : RunId [ ] = [ ] ;
4113+ for ( let index = 0 ; index < ( delayedDelivery ? 2 : 1 ) ; index ++ ) {
4114+ const childSequence = yield * orchestrator . getThreadEventSequence (
4115+ worker . childThreadId ! ,
4116+ ) ;
4117+ yield * orchestrator . dispatch ( {
4118+ type : "message.dispatch" ,
4119+ createdBy : "agent" ,
4120+ creationSource : "mcp" ,
4121+ commandId : CommandId . make ( `wake-comparison:followup:${ index } ` ) ,
4122+ threadId : worker . childThreadId ! ,
4123+ messageId : MessageId . make ( `wake-comparison:followup:${ index } ` ) ,
4124+ text : `${ followupPrompt } ${ index } ` ,
4125+ attachments : [ ] ,
4126+ modelSelection : codexSelection ,
4127+ dispatchMode : { type : "start_immediately" } ,
4128+ } ) ;
4129+ const followupTerminal = yield * awaitEvent (
4130+ worker . childThreadId ! ,
4131+ childSequence ,
4132+ ( event ) => event . type === "run.updated" && event . payload . status === "completed" ,
4133+ ) ;
4134+ if ( followupTerminal . event . type !== "run.updated" )
4135+ throw new Error ( "Missing follow-up run." ) ;
4136+ followupRunIds . push ( followupTerminal . event . payload . id ) ;
4137+ }
4138+ if ( selectionFiber !== undefined ) {
4139+ yield * Deferred . succeed ( selectionGate , undefined ) ;
4140+ yield * Fiber . join ( selectionFiber ) ;
4141+ }
40934142 // This later independent completion is a barrier on the sequential
40944143 // terminal reactor and continuation worker, so absence needs no sleep.
40954144 const controlSequence = yield * orchestrator . getThreadEventSequence ( parentThreadId ) ;
@@ -4141,7 +4190,7 @@ describe("orchestrator MCP toolkit", () => {
41414190 ( transfer ) =>
41424191 transfer . type === "subagent_result" &&
41434192 transfer . sourceThreadId === worker . childThreadId &&
4144- transfer . sourcePoint . runId === followupRunId ,
4193+ followupRunIds . includes ( transfer . sourcePoint . runId ! ) ,
41454194 ) ;
41464195 const controlTransfers = finalParent . contextTransfers . filter (
41474196 ( transfer ) =>
@@ -4154,7 +4203,10 @@ describe("orchestrator MCP toolkit", () => {
41544203 expect (
41554204 followupTransfers ,
41564205 "A completed follow-up must deliver its result to the parent" ,
4157- ) . toHaveLength ( 1 ) ;
4206+ ) . toHaveLength ( followupRunIds . length ) ;
4207+ expect (
4208+ new Set ( followupTransfers . map ( ( transfer ) => transfer . sourcePoint . runId ) ) . size ,
4209+ ) . toBe ( followupRunIds . length ) ;
41584210 expect ( workerWakes ) . toHaveLength ( 2 ) ;
41594211 const originalTask = initialParent . subagents . find ( ( task ) => task . id === worker . id ) ! ;
41604212 const finalTask = finalParent . subagents . find ( ( task ) => task . id === worker . id ) ! ;
0 commit comments