@@ -41,7 +41,10 @@ const modelSelection = { instanceId, model: "test-model" };
4141// Codex turns leave commands running, then the thread moves to another
4242// provider thread (a provider switch). Stop on the newer, settled run must
4343// reach both provider threads and end all of the Codex work.
44- const stopEarlierBackgroundWork = ( failedStart : boolean ) =>
44+ const stopEarlierBackgroundWork = (
45+ failedStart : boolean ,
46+ stopWithQueue ?: "thread.stop" | "run.interrupt" ,
47+ ) =>
4548 Effect . scoped (
4649 Effect . gen ( function * ( ) {
4750 const cwd = yield * checkpointWorkspace ( "background-work-stop" ) ;
@@ -440,12 +443,73 @@ const stopEarlierBackgroundWork = (failedStart: boolean) =>
440443 } ) ;
441444 }
442445
443- yield * orchestrator . dispatch ( {
444- type : "run.interrupt" ,
445- commandId : CommandId . make ( "stop-background-work" ) ,
446- threadId,
447- runId : failedStart ? failedRun . runId : latestRun . runId ,
448- } ) ;
446+ if ( stopWithQueue !== undefined ) {
447+ const owner = ( yield * orchestrator . getThreadProjection ( threadId ) ) . runs . find (
448+ ( run ) => run . id === latestRun . runId ,
449+ ) ! ;
450+ // A message queues while checkpointing finishes, then the user holds the queue.
451+ yield * sink . write ( {
452+ events : [
453+ {
454+ id : EventId . make ( "latest-run-waiting" ) ,
455+ type : "run.updated" ,
456+ threadId,
457+ runId : owner . id ,
458+ occurredAt : now ,
459+ payload : { ...owner , status : "waiting" , completedAt : null } ,
460+ } ,
461+ ] ,
462+ } ) ;
463+ yield * orchestrator . dispatch ( {
464+ type : "message.dispatch" ,
465+ commandId : CommandId . make ( "queue-follow-up" ) ,
466+ threadId,
467+ messageId : MessageId . make ( "message:queue-follow-up" ) ,
468+ text : "Follow up after the background work" ,
469+ attachments : [ ] ,
470+ dispatchMode : { type : "queue_after_active" } ,
471+ createdBy : "user" ,
472+ creationSource : "web" ,
473+ } ) ;
474+ const queued = ( yield * orchestrator . getThreadProjection ( threadId ) ) . runs . at ( - 1 ) ! ;
475+ assert . equal ( queued . status , "queued" ) ;
476+ yield * sink . write ( {
477+ events : [
478+ {
479+ id : EventId . make ( "queue-held" ) ,
480+ type : "run.updated" ,
481+ threadId,
482+ runId : queued . id ,
483+ occurredAt : now ,
484+ payload : { ...queued , queueHeld : true } ,
485+ } ,
486+ {
487+ id : EventId . make ( "latest-run-settled" ) ,
488+ type : "run.updated" ,
489+ threadId,
490+ runId : owner . id ,
491+ occurredAt : now ,
492+ payload : owner ,
493+ } ,
494+ ] ,
495+ } ) ;
496+ }
497+
498+ yield * orchestrator . dispatch (
499+ stopWithQueue === "thread.stop"
500+ ? {
501+ type : "thread.stop" ,
502+ commandId : CommandId . make ( "stop-background-work" ) ,
503+ threadId,
504+ }
505+ : {
506+ type : "run.interrupt" ,
507+ commandId : CommandId . make ( "stop-background-work" ) ,
508+ threadId,
509+ runId : failedStart ? failedRun . runId : latestRun . runId ,
510+ ...( stopWithQueue === undefined ? { } : { holdQueue : true } ) ,
511+ } ,
512+ ) ;
449513 yield * worker . drain ( ) ;
450514
451515 // Stop reaches both provider threads. The Codex one is interrupted at
@@ -459,6 +523,11 @@ const stopEarlierBackgroundWork = (failedStart: boolean) =>
459523 ] ,
460524 ) ;
461525 const after = yield * orchestrator . getThreadProjection ( threadId ) ;
526+ if ( stopWithQueue !== undefined ) {
527+ assert . equal ( after . runs . at ( - 1 ) ?. status , "queued" ) ;
528+ assert . equal ( after . runs . at ( - 1 ) ?. queueHeld , true ) ;
529+ assert . lengthOf ( started , 1 ) ;
530+ }
462531 assert . deepEqual (
463532 [ devServerId , watcherId , reviewerId ] . map (
464533 ( id ) => after . turnItems . find ( ( item ) => item . id === id ) ?. status ,
@@ -485,3 +554,8 @@ it.effect(
485554 "Stop reaches earlier background work after the newest run fails before provider start" ,
486555 ( ) => stopEarlierBackgroundWork ( true ) ,
487556) ;
557+
558+ it . effect . each ( [ "thread.stop" , "run.interrupt" ] as const ) (
559+ "%s stops background work when a later message is queued" ,
560+ ( stopType ) => stopEarlierBackgroundWork ( false , stopType ) ,
561+ ) ;
0 commit comments