@@ -4932,6 +4932,97 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
49324932 } ) ,
49334933 ) ;
49344934
4935+ it . effect ( "queues a message sent with Queue while a usage limit blocks the thread" , ( ) =>
4936+ Effect . gen ( function * ( ) {
4937+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
4938+ const eventSink = yield * EventSink . EventSinkV2 ;
4939+ const threadId = ThreadId . make ( "runtime-layer-queue-after-limit" ) ;
4940+ yield * orchestrator . dispatch ( {
4941+ type : "thread.create" ,
4942+ commandId : CommandId . make ( `${ threadId } :create` ) ,
4943+ threadId,
4944+ createdBy : "user" ,
4945+ creationSource : "web" ,
4946+ projectId : ProjectId . make ( `${ threadId } :project` ) ,
4947+ title : "Queue after limit" ,
4948+ modelSelection,
4949+ runtimeMode : "full-access" ,
4950+ interactionMode : "default" ,
4951+ branch : null ,
4952+ worktreePath : process . cwd ( ) ,
4953+ } ) ;
4954+ const send = ( index : number , type : "start_immediately" | "queue_after_active" ) =>
4955+ orchestrator . dispatch ( {
4956+ type : "message.dispatch" ,
4957+ commandId : CommandId . make ( `${ threadId } :${ index } ` ) ,
4958+ threadId,
4959+ messageId : MessageId . make ( `${ threadId } :${ index } ` ) ,
4960+ text : `Prompt ${ index } ` ,
4961+ attachments : [ ] ,
4962+ createdBy : "user" ,
4963+ creationSource : "web" ,
4964+ dispatchMode : { type } ,
4965+ } ) ;
4966+ yield * send ( 0 , "start_immediately" ) ;
4967+ const [ limited ] = ( yield * orchestrator . getThreadProjection ( threadId ) ) . runs ;
4968+ assert . isDefined ( limited ) ;
4969+ const now = yield * DateTime . now ;
4970+ yield * eventSink . write ( {
4971+ events : [
4972+ {
4973+ id : EventId . make ( `${ threadId } :error` ) ,
4974+ type : "turn-item.updated" ,
4975+ threadId,
4976+ runId : limited . id ,
4977+ nodeId : limited . rootNodeId ! ,
4978+ providerInstanceId : limited . providerInstanceId ,
4979+ occurredAt : now ,
4980+ payload : {
4981+ id : TurnItemId . make ( `${ threadId } :error` ) ,
4982+ type : "error" ,
4983+ threadId,
4984+ runId : limited . id ,
4985+ nodeId : limited . rootNodeId ! ,
4986+ providerThreadId : limited . providerThreadId ,
4987+ providerTurnId : null ,
4988+ nativeItemRef : null ,
4989+ parentItemId : null ,
4990+ ordinal : 2 ,
4991+ status : "failed" ,
4992+ title : "Usage limit" ,
4993+ startedAt : now ,
4994+ completedAt : now ,
4995+ updatedAt : now ,
4996+ failure : {
4997+ class : "usage_limit" ,
4998+ message : "Usage limit reached" ,
4999+ code : "usage_limit" ,
5000+ retryable : null ,
5001+ } ,
5002+ } ,
5003+ } ,
5004+ {
5005+ id : EventId . make ( `${ threadId } :failed` ) ,
5006+ type : "run.updated" ,
5007+ threadId,
5008+ runId : limited . id ,
5009+ providerInstanceId : limited . providerInstanceId ,
5010+ occurredAt : now ,
5011+ payload : { ...limited , status : "failed" , completedAt : now } ,
5012+ } ,
5013+ ] ,
5014+ } ) ;
5015+
5016+ yield * send ( 1 , "queue_after_active" ) ;
5017+ yield * orchestrator . resumeQueuedRuns ;
5018+ const queued = ( yield * orchestrator . getThreadProjection ( threadId ) ) . runs . find (
5019+ ( run ) => run . userMessageId === MessageId . make ( `${ threadId } :1` ) ,
5020+ ) ;
5021+ assert . equal ( queued ?. status , "queued" ) ;
5022+ assert . notEqual ( queued ?. queueHeld , true ) ;
5023+ } ) ,
5024+ ) ;
5025+
49355026 it . effect . each ( [
49365027 [ "server" , modelSelection ] ,
49375028 [ "provider" , modelSelection ] ,
0 commit comments