@@ -1002,3 +1002,140 @@ it.live.each(
10021002 } ) ,
10031003 ) ,
10041004) ;
1005+
1006+ it . live ( "a refused queued send fails alone, and the queue behind it keeps running" , ( ) =>
1007+ Effect . scoped (
1008+ Effect . gen ( function * ( ) {
1009+ const name = "background-work-queue" ;
1010+ const cwd = yield * checkpointWorkspace ( name ) ;
1011+ const threadId = ThreadId . make ( `thread:${ name } ` ) ;
1012+ const targetId = ProviderInstanceId . make ( "codex-background-target" ) ;
1013+ const state = yield * Ref . make < RestartAdapterState > ( {
1014+ activeTurn : null ,
1015+ opened : [ ] ,
1016+ started : [ ] ,
1017+ closedSessionCount : 0 ,
1018+ failedReplacementOpen : true ,
1019+ } ) ;
1020+ const sessions : Array < ProviderAdapter . ProviderAdapterV2SessionRuntime > = [ ] ;
1021+ const adapters = [ providerInstanceId , targetId ] . map ( ( instanceId ) => {
1022+ const base = makeRestartAdapter ( state , exclusiveCapabilities , instanceId ) ;
1023+ return {
1024+ ...base ,
1025+ openSession : ( input ) =>
1026+ base . openSession ( input ) . pipe (
1027+ Effect . map ( ( session ) => {
1028+ const runtime : ProviderAdapter . ProviderAdapterV2SessionRuntime = {
1029+ ...session ,
1030+ backgroundTasksEndWithProcess : true ,
1031+ hasPendingBackgroundWork : Effect . succeed ( true ) ,
1032+ hasPendingBackgroundWorkForThread : ( ) => Effect . succeed ( false ) ,
1033+ } ;
1034+ sessions . push ( runtime ) ;
1035+ return runtime ;
1036+ } ) ,
1037+ ) ,
1038+ } satisfies ProviderAdapter . ProviderAdapterV2 [ "Service" ] ;
1039+ } ) ;
1040+ const layerRegistry = Layer . succeed ( ProviderAdapterRegistry . ProviderAdapterRegistryV2 , {
1041+ get : ( instanceId ) =>
1042+ Effect . succeed ( adapters . find ( ( adapter ) => adapter . instanceId === instanceId ) ! ) ,
1043+ list : ( ) => Effect . succeed ( [ providerInstanceId , targetId ] ) ,
1044+ getMetadata : ( ) =>
1045+ Effect . succeed ( {
1046+ driver,
1047+ continuationKey : "codex:home:/shared" ,
1048+ enabled : true ,
1049+ capabilities : exclusiveCapabilities ,
1050+ } ) ,
1051+ } ) ;
1052+ yield * Effect . gen ( function * ( ) {
1053+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
1054+ const worker = yield * EffectWorker . OrchestrationEffectWorkerV2 ;
1055+ yield * orchestrator . dispatch ( {
1056+ type : "thread.create" ,
1057+ commandId : CommandId . make ( `${ name } :create` ) ,
1058+ threadId,
1059+ projectId : ProjectId . make ( `project:${ name } ` ) ,
1060+ title : name ,
1061+ modelSelection : initialSelection ,
1062+ runtimeMode : "full-access" ,
1063+ interactionMode : "default" ,
1064+ branch : null ,
1065+ worktreePath : cwd ,
1066+ createdBy : "user" ,
1067+ creationSource : "web" ,
1068+ } ) ;
1069+ const send = (
1070+ id : string ,
1071+ dispatchMode : { readonly type : "start_immediately" | "queue_after_active" } ,
1072+ modelSelection ?: ModelSelection ,
1073+ ) =>
1074+ orchestrator . dispatch ( {
1075+ type : "message.dispatch" ,
1076+ commandId : CommandId . make ( `${ name } :${ id } ` ) ,
1077+ threadId,
1078+ messageId : MessageId . make ( `${ name } :${ id } ` ) ,
1079+ text : id ,
1080+ attachments : [ ] ,
1081+ ...( modelSelection === undefined ? { } : { modelSelection } ) ,
1082+ dispatchMode,
1083+ createdBy : "user" ,
1084+ creationSource : "web" ,
1085+ } ) ;
1086+ // A held queue never starts another turn, so holding ends the wait too.
1087+ const runningTurnOrHeldQueue = ( ) =>
1088+ orchestrator . streamDomainEvents . pipe (
1089+ Stream . filter (
1090+ ( event ) =>
1091+ ( event . type === "provider-turn.updated" && event . payload . status === "running" ) ||
1092+ ( event . type === "run.updated" && event . payload . queueHeld === true ) ,
1093+ ) ,
1094+ Stream . take ( 1 ) ,
1095+ Stream . runDrain ,
1096+ Effect . forkScoped ,
1097+ ) ;
1098+ const firstRunning = yield * runningTurnOrHeldQueue ( ) ;
1099+ yield * send ( "first" , { type : "start_immediately" } ) ;
1100+ yield * worker . drain ( ) ;
1101+ yield * Fiber . join ( firstRunning ) ;
1102+ yield * worker . drain ( ) ;
1103+ const before = yield * orchestrator . getThreadProjection ( threadId ) ;
1104+ const providerThread = before . providerThreads . find (
1105+ ( thread ) => thread . id === before . thread . activeProviderThreadId ,
1106+ ) ! ;
1107+
1108+ const targetSelection = { instanceId : targetId , model : initialSelection . model } ;
1109+ yield * send ( "other-1" , { type : "queue_after_active" } , targetSelection ) ;
1110+ yield * send ( "other-2" , { type : "queue_after_active" } , targetSelection ) ;
1111+ yield * send ( "same" , { type : "queue_after_active" } ) ;
1112+ yield * worker . drain ( ) ;
1113+
1114+ // Both sends to the other account are refused; the send that keeps
1115+ // the session starts on it.
1116+ const sameRunning = yield * runningTurnOrHeldQueue ( ) ;
1117+ yield * sessions [ 0 ] ! . interruptTurn ( { providerThread } as never ) ;
1118+ yield * Fiber . join ( sameRunning ) ;
1119+ yield * worker . drain ( ) ;
1120+
1121+ const after = yield * orchestrator . getThreadProjection ( threadId ) ;
1122+ const status = ( id : string ) =>
1123+ after . runs . find ( ( run ) => run . userMessageId === `${ name } :${ id } ` ) ?. status ;
1124+ assert . deepEqual ( [ "other-1" , "other-2" , "same" ] . map ( status ) , [
1125+ "failed" ,
1126+ "failed" ,
1127+ "running" ,
1128+ ] ) ;
1129+ assert . isFalse ( after . runs . some ( ( run ) => run . queueHeld === true ) ) ;
1130+ assert . deepEqual (
1131+ after . providerSessions
1132+ . filter ( ( session ) => session . id === providerThread . providerSessionId )
1133+ . map ( ( session ) => session . status ) ,
1134+ [ "ready" ] ,
1135+ ) ;
1136+ assert . equal ( sessions . length , 1 ) ;
1137+ assert . equal ( ( yield * Ref . get ( state ) ) . closedSessionCount , 0 ) ;
1138+ } ) . pipe ( Effect . provide ( ProviderReplayHarness . layerWithRegistry ( { name } , layerRegistry ) ) ) ;
1139+ } ) ,
1140+ ) ,
1141+ ) ;
0 commit comments