@@ -818,3 +818,114 @@ it.effect("keeps a held commentary draft before the steer when turn end flushes
818818 } ) ,
819819 ) ,
820820) ;
821+
822+ it . effect ( "keeps a queued message queued when promoted into a run that Stop reached" , ( ) =>
823+ Effect . scoped (
824+ Effect . gen ( function * ( ) {
825+ const { started, steered, layer, startFirstTurn } =
826+ yield * nextTurnSelectionHarness ( "steering-after-stop" ) ;
827+ yield * Effect . gen ( function * ( ) {
828+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
829+ const worker = yield * EffectWorker . OrchestrationEffectWorkerV2 ;
830+ const threadId = yield * startFirstTurn ;
831+ const first = started [ 0 ] ! ;
832+ yield * orchestrator . dispatch ( {
833+ type : "message.dispatch" ,
834+ commandId : CommandId . make ( "queued" ) ,
835+ threadId,
836+ messageId : MessageId . make ( "message:queued" ) ,
837+ text : "queued" ,
838+ attachments : [ ] ,
839+ modelSelection : runSelection ,
840+ dispatchMode : { type : "queue_after_active" } ,
841+ createdBy : "user" ,
842+ creationSource : "web" ,
843+ } ) ;
844+ const queuedRun = ( yield * orchestrator . getThreadProjection ( threadId ) ) . runs . find (
845+ ( run ) => run . status === "queued" ,
846+ ) ! ;
847+ // The worker has not delivered the interrupt yet, so the run still reads as running.
848+ yield * orchestrator . dispatch ( {
849+ type : "run.interrupt" ,
850+ commandId : CommandId . make ( "stop" ) ,
851+ threadId,
852+ runId : first . runId ,
853+ holdQueue : true ,
854+ } ) ;
855+
856+ const promoteError = yield * orchestrator
857+ . dispatch ( {
858+ type : "queued-message.promote-to-steer" ,
859+ commandId : CommandId . make ( "promote" ) ,
860+ threadId,
861+ queuedRunId : queuedRun . id ,
862+ targetRunId : first . runId ,
863+ } )
864+ . pipe ( Effect . flip ) ;
865+ yield * worker . drain ( ) ;
866+
867+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
868+ assert . equal ( promoteError . _tag , "OrchestratorDispatchError" ) ;
869+ assert . deepEqual ( steered , [ ] ) ;
870+ const queued = projection . runs . find ( ( run ) => run . id === queuedRun . id ) ;
871+ assert . equal ( queued ?. status , "queued" ) ;
872+ assert . isTrue ( queued ?. queueHeld ) ;
873+ } ) . pipe ( Effect . provide ( layer ) ) ;
874+ } ) ,
875+ ) ,
876+ ) ;
877+
878+ it . effect ( "queues a plain send behind the stopped queue and releases its hold" , ( ) =>
879+ Effect . scoped (
880+ Effect . gen ( function * ( ) {
881+ const { started, steered, layer, startFirstTurn } =
882+ yield * nextTurnSelectionHarness ( "plain-send-after-stop" ) ;
883+ yield * Effect . gen ( function * ( ) {
884+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
885+ const worker = yield * EffectWorker . OrchestrationEffectWorkerV2 ;
886+ const threadId = yield * startFirstTurn ;
887+ const send = ( id : string , delivery : "queue" | "auto" ) =>
888+ orchestrator . dispatch ( {
889+ type : "message.dispatch" ,
890+ commandId : CommandId . make ( id ) ,
891+ threadId,
892+ messageId : MessageId . make ( `message:${ id } ` ) ,
893+ text : id ,
894+ attachments : [ ] ,
895+ modelSelection : runSelection ,
896+ ...( delivery === "queue"
897+ ? { dispatchMode : { type : "queue_after_active" } }
898+ : { dispatchMode : { type : "start_immediately" } , deliveryIntent : "auto" } ) ,
899+ createdBy : "user" ,
900+ creationSource : "web" ,
901+ } ) ;
902+ yield * send ( "queued" , "queue" ) ;
903+ yield * orchestrator . dispatch ( {
904+ type : "run.interrupt" ,
905+ commandId : CommandId . make ( "stop" ) ,
906+ threadId,
907+ runId : started [ 0 ] ! . runId ,
908+ holdQueue : true ,
909+ } ) ;
910+ // The provider has not stopped yet, so a plain send still resolves to a steer.
911+ yield * send ( "sent" , "auto" ) ;
912+ yield * worker . drain ( ) ;
913+
914+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
915+ assert . deepEqual ( steered , [ ] ) ;
916+ assert . equal ( started . length , 1 ) ;
917+ const queued = projection . runs
918+ . filter ( ( run ) => run . status === "queued" )
919+ . sort ( ( left , right ) => ( left . queuePosition ?? 0 ) - ( right . queuePosition ?? 0 ) ) ;
920+ assert . deepEqual (
921+ queued . map (
922+ ( run ) => projection . messages . find ( ( message ) => message . id === run . userMessageId ) ?. text ,
923+ ) ,
924+ [ "queued" , "sent" ] ,
925+ ) ;
926+ // The user sent it, so the queue they stopped runs again once the provider stops.
927+ assert . isFalse ( queued . some ( ( run ) => run . queueHeld === true ) ) ;
928+ } ) . pipe ( Effect . provide ( layer ) ) ;
929+ } ) ,
930+ ) ,
931+ ) ;
0 commit comments