@@ -17,6 +17,7 @@ import {
1717 ProjectId ,
1818 ProviderDriverKind ,
1919 ProviderInstanceId ,
20+ ProviderSessionId ,
2021 ProviderThreadId ,
2122 RunId ,
2223 RuntimeRequestId ,
@@ -3706,96 +3707,139 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
37063707) ;
37073708
37083709it . layer ( layerTest ) ( "delegated tasks across a server restart" , ( it ) => {
3709- it . effect ( "queues a child result without creating a provider history handoff" , ( ) =>
3710- Effect . gen ( function * ( ) {
3711- const orchestrator = yield * Orchestrator . OrchestratorV2 ;
3712- const eventSink = yield * EventSink . EventSinkV2 ;
3713- const now = yield * DateTime . now ;
3714- const threadId = ThreadId . make ( "thread:result-message-parent" ) ;
3715- const projectId = ProjectId . make ( "project:result-message" ) ;
3716- const runId = RunId . make ( "run:result-message-parent" ) ;
3717- const rootNodeId = NodeId . make ( "node:result-message-parent" ) ;
3718- yield * seedParentWithTerminalTask ( {
3719- threadId,
3720- projectId,
3721- runId,
3722- rootNodeId,
3723- taskId : NodeId . make ( "node:result-message-seed" ) ,
3724- deliveryState : "acknowledged" ,
3725- now,
3726- } ) ;
3727- const child = yield * seedRestartCancelledChild ( {
3728- parentThreadId : threadId ,
3729- projectId,
3730- parentRunId : runId ,
3731- rootNodeId,
3732- name : "result-message-child" ,
3733- completionWake : "always" ,
3734- continuationPending : false ,
3735- runStatus : "completed" ,
3736- now,
3737- } ) ;
3738- const parent = yield * orchestrator . getThreadProjection ( threadId ) ;
3739- const childProjection = yield * orchestrator . getThreadProjection ( child . childThreadId ) ;
3740- const providerThreadId = ProviderThreadId . make ( "provider-thread:result-message-child" ) ;
3741- yield * eventSink . write ( {
3742- commandId : CommandId . make ( "command:result-message-child-provider" ) ,
3743- events : [
3744- {
3745- id : EventId . make ( "event:result-message-child-provider" ) ,
3746- type : "provider-thread.updated" ,
3747- threadId : child . childThreadId ,
3748- occurredAt : now ,
3749- payload : {
3750- ...parent . providerThreads [ 0 ] ! ,
3751- id : providerThreadId ,
3752- appThreadId : child . childThreadId ,
3710+ it . effect . each ( [
3711+ { name : "releases" , key : "" , pendingBackgroundTasks : [ ] , released : true } ,
3712+ {
3713+ name : "keeps while an unawaited background command runs" ,
3714+ key : "-background" ,
3715+ pendingBackgroundTasks : [ { taskId : "dev-server" , kind : "command" as const } ] ,
3716+ released : false ,
3717+ } ,
3718+ ] ) (
3719+ "queues a child result without creating a provider history handoff and $name its process" ,
3720+ ( { key, pendingBackgroundTasks, released } ) =>
3721+ Effect . gen ( function * ( ) {
3722+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
3723+ const eventSink = yield * EventSink . EventSinkV2 ;
3724+ const now = yield * DateTime . now ;
3725+ const threadId = ThreadId . make ( `thread:result-message${ key } -parent` ) ;
3726+ const projectId = ProjectId . make ( `project:result-message${ key } ` ) ;
3727+ const runId = RunId . make ( `run:result-message${ key } -parent` ) ;
3728+ const rootNodeId = NodeId . make ( `node:result-message${ key } -parent` ) ;
3729+ yield * seedParentWithTerminalTask ( {
3730+ threadId,
3731+ projectId,
3732+ runId,
3733+ rootNodeId,
3734+ taskId : NodeId . make ( `node:result-message${ key } -seed` ) ,
3735+ deliveryState : "acknowledged" ,
3736+ now,
3737+ } ) ;
3738+ const child = yield * seedRestartCancelledChild ( {
3739+ parentThreadId : threadId ,
3740+ projectId,
3741+ parentRunId : runId ,
3742+ rootNodeId,
3743+ name : `result-message${ key } -child` ,
3744+ completionWake : "always" ,
3745+ continuationPending : false ,
3746+ runStatus : "completed" ,
3747+ now,
3748+ } ) ;
3749+ const parent = yield * orchestrator . getThreadProjection ( threadId ) ;
3750+ const childProjection = yield * orchestrator . getThreadProjection ( child . childThreadId ) ;
3751+ const providerThreadId = ProviderThreadId . make (
3752+ `provider-thread:result-message${ key } -child` ,
3753+ ) ;
3754+ yield * eventSink . write ( {
3755+ commandId : CommandId . make ( `command:result-message${ key } -child-provider` ) ,
3756+ events : [
3757+ {
3758+ id : EventId . make ( `event:result-message${ key } -child-session` ) ,
3759+ type : "provider-session.attached" ,
3760+ threadId : child . childThreadId ,
3761+ occurredAt : now ,
3762+ payload : {
3763+ id : ProviderSessionId . make ( `provider-session:result-message${ key } -child` ) ,
3764+ driver,
3765+ providerInstanceId : modelSelection . instanceId ,
3766+ status : "ready" ,
3767+ cwd : "/repo" ,
3768+ model : modelSelection . model ,
3769+ capabilities : {
3770+ ...CodexProviderCapabilitiesV2 ,
3771+ sessions : {
3772+ ...CodexProviderCapabilitiesV2 . sessions ,
3773+ supportsMultipleProviderThreadsPerSession : false ,
3774+ } ,
3775+ } ,
3776+ createdAt : now ,
3777+ updatedAt : now ,
3778+ lastError : null ,
3779+ } ,
37533780 } ,
3754- } ,
3755- {
3756- id : EventId . make ( "event:result-message-child-run" ) ,
3757- type : "run.updated" ,
3758- threadId : child . childThreadId ,
3759- runId : child . childRunId ,
3760- occurredAt : now ,
3761- payload : { ...childProjection . runs [ 0 ] ! , providerThreadId } ,
3762- } ,
3763- ] ,
3764- } ) ;
3781+ {
3782+ id : EventId . make ( `event:result-message${ key } -child-provider` ) ,
3783+ type : "provider-thread.updated" ,
3784+ threadId : child . childThreadId ,
3785+ occurredAt : now ,
3786+ payload : {
3787+ ...parent . providerThreads [ 0 ] ! ,
3788+ id : providerThreadId ,
3789+ appThreadId : child . childThreadId ,
3790+ pendingBackgroundTasks,
3791+ } ,
3792+ } ,
3793+ {
3794+ id : EventId . make ( `event:result-message${ key } -child-run` ) ,
3795+ type : "run.updated" ,
3796+ threadId : child . childThreadId ,
3797+ runId : child . childRunId ,
3798+ occurredAt : now ,
3799+ payload : { ...childProjection . runs [ 0 ] ! , providerThreadId } ,
3800+ } ,
3801+ ] ,
3802+ } ) ;
37653803
3766- yield * orchestrator . recoverDelegatedTasks ;
3767- const recovered = yield * orchestrator . getThreadProjection ( threadId ) ;
3768- const task = recovered . subagents . find ( ( candidate ) => candidate . id === child . taskId ) ! ;
3769- assert . equal ( task . status , "completed" ) ;
3770- assert . isNotNull ( task . result ) ;
3771- assert . isEmpty ( recovered . contextHandoffs ) ;
3772- assert . equal ( recovered . contextTransfers . length , 1 ) ;
3804+ yield * orchestrator . recoverDelegatedTasks ;
3805+ const recovered = yield * orchestrator . getThreadProjection ( threadId ) ;
3806+ const task = recovered . subagents . find ( ( candidate ) => candidate . id === child . taskId ) ! ;
3807+ assert . equal ( task . status , "completed" ) ;
3808+ assert . isNotNull ( task . result ) ;
3809+ assert . isEmpty ( recovered . contextHandoffs ) ;
3810+ assert . equal ( recovered . contextTransfers . length , 1 ) ;
3811+ // A one-process-per-thread child releases its process once its result is recorded,
3812+ // unless background work it did not await would die with it.
3813+ assert . equal (
3814+ ( yield * orchestrator . getThreadProjection ( child . childThreadId ) ) . providerSessions . length ,
3815+ released ? 0 : 1 ,
3816+ ) ;
37733817
3774- const delivery = recovered . runs . find ( ( candidate ) => candidate . id === runId ) !
3775- . delegatedCompletion ! . delivery ! ;
3776- const messageId = delivery . messageId ;
3777- yield * orchestrator . dispatch ( {
3778- type : "message.dispatch" ,
3779- commandId : CommandId . make ( " command:result-message-delivery" ) ,
3780- threadId,
3781- messageId,
3782- text : "Delegated task finished." ,
3783- attachments : [ ] ,
3784- dispatchMode : { type : "queue_after_active" } ,
3785- createdBy : "agent" ,
3786- creationSource : "server" ,
3787- delegatedCompletion : {
3788- parentRunId : runId ,
3789- generation : delivery . generation ,
3790- taskIds : [ child . taskId ] ,
3791- } ,
3792- } ) ;
3793- const queued = yield * orchestrator . getThreadProjection ( threadId ) ;
3794- assert . include (
3795- queued . messages . find ( ( message ) => message . id === messageId ) ?. text ?? "" ,
3796- task . result ! ,
3797- ) ;
3798- } ) ,
3818+ const delivery = recovered . runs . find ( ( candidate ) => candidate . id === runId ) !
3819+ . delegatedCompletion ! . delivery ! ;
3820+ const messageId = delivery . messageId ;
3821+ yield * orchestrator . dispatch ( {
3822+ type : "message.dispatch" ,
3823+ commandId : CommandId . make ( ` command:result-message${ key } -delivery` ) ,
3824+ threadId,
3825+ messageId,
3826+ text : "Delegated task finished." ,
3827+ attachments : [ ] ,
3828+ dispatchMode : { type : "queue_after_active" } ,
3829+ createdBy : "agent" ,
3830+ creationSource : "server" ,
3831+ delegatedCompletion : {
3832+ parentRunId : runId ,
3833+ generation : delivery . generation ,
3834+ taskIds : [ child . taskId ] ,
3835+ } ,
3836+ } ) ;
3837+ const queued = yield * orchestrator . getThreadProjection ( threadId ) ;
3838+ assert . include (
3839+ queued . messages . find ( ( message ) => message . id === messageId ) ?. text ?? "" ,
3840+ task . result ! ,
3841+ ) ;
3842+ } ) ,
37993843 ) ;
38003844
38013845 it . effect ( "holds a restart-cancelled child for its continuation's result" , ( ) =>
0 commit comments