@@ -3580,6 +3580,113 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
35803580 } ) ,
35813581 ) ;
35823582
3583+ it . effect ( "cancels preparing work on archive and keeps it cancelled through replay" , ( ) =>
3584+ Effect . gen ( function * ( ) {
3585+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
3586+ const maintenance = yield * ProjectionMaintenance . ProjectionMaintenanceV2 ;
3587+ const threadId = ThreadId . make ( "runtime-layer-archive-preparing-thread" ) ;
3588+ yield * orchestrator . dispatch ( {
3589+ type : "thread.create" ,
3590+ createdBy : "user" ,
3591+ creationSource : "web" ,
3592+ commandId : CommandId . make ( "archive-preparing-create" ) ,
3593+ threadId,
3594+ projectId : ProjectId . make ( "archive-preparing-project" ) ,
3595+ title : "Archive preparing work" ,
3596+ modelSelection,
3597+ runtimeMode : "full-access" ,
3598+ interactionMode : "default" ,
3599+ branch : null ,
3600+ worktreePath : null ,
3601+ } ) ;
3602+ yield * orchestrator . dispatch ( {
3603+ type : "message.dispatch" ,
3604+ createdBy : "user" ,
3605+ creationSource : "web" ,
3606+ commandId : CommandId . make ( "archive-preparing-message" ) ,
3607+ threadId,
3608+ messageId : MessageId . make ( "archive-preparing-message" ) ,
3609+ text : "Prepare my workspace." ,
3610+ attachments : [ ] ,
3611+ modelSelection,
3612+ dispatchMode : { type : "defer_start" } ,
3613+ } ) ;
3614+ const before = yield * orchestrator . getThreadProjection ( threadId ) ;
3615+ assert . lengthOf ( before . runs , 1 ) ;
3616+ assert . isEmpty ( before . subagents ) ;
3617+ const run = before . runs [ 0 ] ! ;
3618+ assert . equal ( run . status , "preparing" ) ;
3619+ const preparation = before . turnItems . find ( ( item ) => item . type === "command_execution" ) ! ;
3620+ assert . equal ( preparation . status , "running" ) ;
3621+ assert . isEmpty ( ( yield * orchestrator . getThreadShell ( threadId ) ) ! . pendingBackgroundTasks ) ;
3622+
3623+ const archive = {
3624+ type : "thread.archive" as const ,
3625+ commandId : CommandId . make ( "archive-preparing-archive" ) ,
3626+ threadId,
3627+ } ;
3628+ const archivedResult = yield * orchestrator . dispatch ( archive ) ;
3629+ assert . equal ( ( yield * orchestrator . dispatch ( archive ) ) . sequence , archivedResult . sequence ) ;
3630+
3631+ const assertCancelled = Effect . gen ( function * ( ) {
3632+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
3633+ const cancelledRun = projection . runs . find ( ( candidate ) => candidate . id === run . id ) ! ;
3634+ assert . equal ( cancelledRun . status , "cancelled" ) ;
3635+ assert . isNull ( cancelledRun . queuePosition ) ;
3636+ assert . isNotNull ( cancelledRun . completedAt ) ;
3637+ assert . equal ( projection . attempts [ 0 ] ?. status , "cancelled" ) ;
3638+ assert . isNotNull ( projection . attempts [ 0 ] ?. completedAt ) ;
3639+ assert . equal ( projection . nodes [ 0 ] ?. status , "cancelled" ) ;
3640+ assert . isNotNull ( projection . nodes [ 0 ] ?. completedAt ) ;
3641+ const item = projection . turnItems . find ( ( candidate ) => candidate . id === preparation . id ) ! ;
3642+ assert . equal ( item . status , "cancelled" ) ;
3643+ assert . equal ( item . title , "Workspace preparation cancelled" ) ;
3644+ assert . deepEqual ( item . completedAt , cancelledRun . completedAt ) ;
3645+ assert . deepEqual ( item . updatedAt , cancelledRun . completedAt ) ;
3646+ assert . isEmpty ( ( yield * orchestrator . getThreadShell ( threadId ) ) ! . pendingBackgroundTasks ) ;
3647+ assert . equal ( yield * orchestrator . resumeQueuedRuns , 0 ) ;
3648+ } ) ;
3649+ assert . isNotNull ( ( yield * orchestrator . getThreadProjection ( threadId ) ) . thread . archivedAt ) ;
3650+ yield * assertCancelled ;
3651+ assert . isTrue ( ( yield * maintenance . rebuild ) . valid ) ;
3652+ yield * assertCancelled ;
3653+
3654+ // Preparation may finish after archive. Even reopening must not let its
3655+ // late callbacks revive the cancelled run or its background activity.
3656+ yield * orchestrator . dispatch ( {
3657+ type : "thread.unarchive" ,
3658+ commandId : CommandId . make ( "archive-preparing-unarchive" ) ,
3659+ threadId,
3660+ } ) ;
3661+ for ( const callback of [
3662+ { type : "prepared-run.progress" , phase : "setup" } ,
3663+ { type : "prepared-run.release" } ,
3664+ {
3665+ type : "prepared-run.fail" ,
3666+ failure : {
3667+ class : "validation_error" ,
3668+ message : "Late preparation failure" ,
3669+ code : null ,
3670+ retryable : false ,
3671+ } ,
3672+ } ,
3673+ ] as const ) {
3674+ const error = yield * orchestrator
3675+ . dispatch ( {
3676+ ...callback ,
3677+ commandId : CommandId . make ( `archive-preparing-late:${ callback . type } ` ) ,
3678+ threadId,
3679+ runId : run . id ,
3680+ } )
3681+ . pipe ( Effect . flip ) ;
3682+ assert . equal ( error . _tag , "OrchestratorDispatchError" ) ;
3683+ }
3684+ yield * assertCancelled ;
3685+ assert . isTrue ( ( yield * maintenance . rebuild ) . valid ) ;
3686+ yield * assertCancelled ;
3687+ } ) ,
3688+ ) ;
3689+
35833690 it . effect ( "cancels queued work when a thread is archived" , ( ) =>
35843691 Effect . gen ( function * ( ) {
35853692 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
0 commit comments