@@ -3873,6 +3873,113 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
38733873 } ) ,
38743874 ) ;
38753875
3876+ it . effect ( "cancels preparing work on archive and keeps it cancelled through replay" , ( ) =>
3877+ Effect . gen ( function * ( ) {
3878+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
3879+ const maintenance = yield * ProjectionMaintenance . ProjectionMaintenanceV2 ;
3880+ const threadId = ThreadId . make ( "runtime-layer-archive-preparing-thread" ) ;
3881+ yield * orchestrator . dispatch ( {
3882+ type : "thread.create" ,
3883+ createdBy : "user" ,
3884+ creationSource : "web" ,
3885+ commandId : CommandId . make ( "archive-preparing-create" ) ,
3886+ threadId,
3887+ projectId : ProjectId . make ( "archive-preparing-project" ) ,
3888+ title : "Archive preparing work" ,
3889+ modelSelection,
3890+ runtimeMode : "full-access" ,
3891+ interactionMode : "default" ,
3892+ branch : null ,
3893+ worktreePath : null ,
3894+ } ) ;
3895+ yield * orchestrator . dispatch ( {
3896+ type : "message.dispatch" ,
3897+ createdBy : "user" ,
3898+ creationSource : "web" ,
3899+ commandId : CommandId . make ( "archive-preparing-message" ) ,
3900+ threadId,
3901+ messageId : MessageId . make ( "archive-preparing-message" ) ,
3902+ text : "Prepare my workspace." ,
3903+ attachments : [ ] ,
3904+ modelSelection,
3905+ dispatchMode : { type : "defer_start" } ,
3906+ } ) ;
3907+ const before = yield * orchestrator . getThreadProjection ( threadId ) ;
3908+ assert . lengthOf ( before . runs , 1 ) ;
3909+ assert . isEmpty ( before . subagents ) ;
3910+ const run = before . runs [ 0 ] ! ;
3911+ assert . equal ( run . status , "preparing" ) ;
3912+ const preparation = before . turnItems . find ( ( item ) => item . type === "command_execution" ) ! ;
3913+ assert . equal ( preparation . status , "running" ) ;
3914+ assert . isEmpty ( ( yield * orchestrator . getThreadShell ( threadId ) ) ! . pendingBackgroundTasks ) ;
3915+
3916+ const archive = {
3917+ type : "thread.archive" as const ,
3918+ commandId : CommandId . make ( "archive-preparing-archive" ) ,
3919+ threadId,
3920+ } ;
3921+ const archivedResult = yield * orchestrator . dispatch ( archive ) ;
3922+ assert . equal ( ( yield * orchestrator . dispatch ( archive ) ) . sequence , archivedResult . sequence ) ;
3923+
3924+ const assertCancelled = Effect . gen ( function * ( ) {
3925+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
3926+ const cancelledRun = projection . runs . find ( ( candidate ) => candidate . id === run . id ) ! ;
3927+ assert . equal ( cancelledRun . status , "cancelled" ) ;
3928+ assert . isNull ( cancelledRun . queuePosition ) ;
3929+ assert . isNotNull ( cancelledRun . completedAt ) ;
3930+ assert . equal ( projection . attempts [ 0 ] ?. status , "cancelled" ) ;
3931+ assert . isNotNull ( projection . attempts [ 0 ] ?. completedAt ) ;
3932+ assert . equal ( projection . nodes [ 0 ] ?. status , "cancelled" ) ;
3933+ assert . isNotNull ( projection . nodes [ 0 ] ?. completedAt ) ;
3934+ const item = projection . turnItems . find ( ( candidate ) => candidate . id === preparation . id ) ! ;
3935+ assert . equal ( item . status , "cancelled" ) ;
3936+ assert . equal ( item . title , "Workspace preparation cancelled" ) ;
3937+ assert . deepEqual ( item . completedAt , cancelledRun . completedAt ) ;
3938+ assert . deepEqual ( item . updatedAt , cancelledRun . completedAt ) ;
3939+ assert . isEmpty ( ( yield * orchestrator . getThreadShell ( threadId ) ) ! . pendingBackgroundTasks ) ;
3940+ assert . equal ( yield * orchestrator . resumeQueuedRuns , 0 ) ;
3941+ } ) ;
3942+ assert . isNotNull ( ( yield * orchestrator . getThreadProjection ( threadId ) ) . thread . archivedAt ) ;
3943+ yield * assertCancelled ;
3944+ assert . isTrue ( ( yield * maintenance . rebuild ) . valid ) ;
3945+ yield * assertCancelled ;
3946+
3947+ // Preparation may finish after archive. Even reopening must not let its
3948+ // late callbacks revive the cancelled run or its background activity.
3949+ yield * orchestrator . dispatch ( {
3950+ type : "thread.unarchive" ,
3951+ commandId : CommandId . make ( "archive-preparing-unarchive" ) ,
3952+ threadId,
3953+ } ) ;
3954+ for ( const callback of [
3955+ { type : "prepared-run.progress" , phase : "setup" } ,
3956+ { type : "prepared-run.release" } ,
3957+ {
3958+ type : "prepared-run.fail" ,
3959+ failure : {
3960+ class : "validation_error" ,
3961+ message : "Late preparation failure" ,
3962+ code : null ,
3963+ retryable : false ,
3964+ } ,
3965+ } ,
3966+ ] as const ) {
3967+ const error = yield * orchestrator
3968+ . dispatch ( {
3969+ ...callback ,
3970+ commandId : CommandId . make ( `archive-preparing-late:${ callback . type } ` ) ,
3971+ threadId,
3972+ runId : run . id ,
3973+ } )
3974+ . pipe ( Effect . flip ) ;
3975+ assert . equal ( error . _tag , "OrchestratorDispatchError" ) ;
3976+ }
3977+ yield * assertCancelled ;
3978+ assert . isTrue ( ( yield * maintenance . rebuild ) . valid ) ;
3979+ yield * assertCancelled ;
3980+ } ) ,
3981+ ) ;
3982+
38763983 it . effect ( "cancels queued work when a thread is archived" , ( ) =>
38773984 Effect . gen ( function * ( ) {
38783985 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
0 commit comments