@@ -32,6 +32,8 @@ const driver = ProviderDriverKind.make("codex");
3232const providerThreadId = ProviderThreadId . make ( "provider-thread:restart" ) ;
3333const sessionId = ProviderSessionId . make ( "session:restart" ) ;
3434const attemptId = RunAttemptId . make ( "attempt:restart" ) ;
35+ // "No project" threads belong to the environment's Scratch project.
36+ const scratchProjectId = ProjectId . make ( "project:scratch" ) ;
3537
3638function makeProjection ( ) {
3739 return {
@@ -155,76 +157,63 @@ it("recovers an admitted continuation after another crash before provider start"
155157 assert . equal ( restartContinuationRun ( starting ) ?. id , runId ) ;
156158} ) ;
157159
158- it ( "continues a settled root run only when the restart cancelled its background work" , ( ) => {
160+ it ( "does not continue settled root runs with restart- cancelled background work" , ( ) => {
159161 const projection = makeProjection ( ) ;
160162 const settled = {
161163 ...projection ,
162- runs : [ { ...projection . runs [ 0 ] ! , status : "completed" as const } ] ,
164+ runs : [
165+ {
166+ ...projection . runs [ 0 ] ! ,
167+ status : "completed" as const ,
168+ restartCancelledBackgroundWork : [ { kind : "shell" as const , label : "sleep 25" } ] ,
169+ } ,
170+ ] ,
163171 providerThreads : [ { ...projection . providerThreads [ 0 ] ! , status : "idle" as const } ] ,
164172 providerSessions : [ { ...projection . providerSessions [ 0 ] ! , status : "stopped" as const } ] ,
165173 providerTurns : [ ] ,
166174 } ;
167- const lostWork = new Set ( [ providerThreadId ] ) ;
168- assert . isUndefined ( restartContinuationRun ( settled ) ) ;
169- assert . equal ( restartContinuationRun ( settled , lostWork ) ?. id , runId ) ;
170- // Work an older provider thread launched (before a provider switch) is not
171- // this run's: its provider was never told about it and cannot continue it.
172- assert . isUndefined (
173- restartContinuationRun ( settled , new Set ( [ ProviderThreadId . make ( "provider-thread:claude" ) ] ) ) ,
174- ) ;
175- for ( const invalid of [
176- { ...settled , thread : { ...settled . thread , archivedAt : { } } } ,
177- { ...settled , thread : { ...settled . thread , deletedAt : { } } } ,
178- { ...settled , runs : [ { ...settled . runs [ 0 ] ! , status : "failed" as const } ] } ,
179- {
180- ...settled ,
181- providerThreads : [ { ...settled . providerThreads [ 0 ] ! , nativeThreadRef : null } ] ,
182- } ,
183- ] )
184- assert . isUndefined (
185- restartContinuationRun ( invalid as OrchestrationV2ThreadProjection , lostWork ) ,
186- ) ;
175+ for ( const projectId of [ scratchProjectId , projection . thread . projectId ] )
176+ for ( const status of [ "completed" , "waiting" ] as const )
177+ assert . isUndefined (
178+ restartContinuationRun ( {
179+ ...settled ,
180+ thread : { ...settled . thread , projectId } ,
181+ runs : [ { ...settled . runs [ 0 ] ! , status } ] ,
182+ } ) ,
183+ ) ;
187184} ) ;
188185
189- it . effect ( "prompts a settled thread's continuation with the note of its lost work" , ( ) =>
190- Effect . gen ( function * ( ) {
191- const base = makeProjection ( ) ;
192- const work = [ { kind : "shell" as const , label : "sleep 25 && echo DONE" } ] ;
193- const projection = {
194- ...base ,
195- runs : [ { ...base . runs [ 0 ] ! , status : "completed" , restartCancelledBackgroundWork : work } ] ,
196- providerTurns : [ { ...base . providerTurns [ 0 ] ! , status : "completed" } ] ,
197- } as unknown as OrchestrationV2ThreadProjection ;
198- const commands : Parameters <
199- ThreadManagementService . ThreadManagementService [ "Service" ] [ "dispatch" ]
200- > [ 0 ] [ ] = [ ] ;
201- yield * continueRestartedRun ( { threadId, sourceRunId : runId } ) . pipe (
202- Effect . provide (
203- Layer . merge (
204- Layer . mock ( ThreadManagementService . ThreadManagementService ) ( {
205- getThreadRecords : ( ) => Effect . succeed ( projection ) ,
206- recoverDelegatedTask : ( ) => Effect . void ,
207- dispatch : ( command ) => {
208- commands . push ( command ) ;
209- return Effect . succeed ( { } as never ) ;
210- } ,
211- } ) ,
212- ServerSettings . layerTest ( { continueThreadsAfterServerUpdate : true } ) ,
186+ it . effect . each ( [ "completed" , "waiting" , "cancelled" ] as const ) (
187+ "ignores a pending restart continuation for a %s run whose provider turn settled" ,
188+ ( status ) =>
189+ Effect . gen ( function * ( ) {
190+ const base = makeProjection ( ) ;
191+ const work = [ { kind : "shell" as const , label : "sleep 25 && echo DONE" } ] ;
192+ const projection = {
193+ ...base ,
194+ runs : [ { ...base . runs [ 0 ] ! , status, restartCancelledBackgroundWork : work } ] ,
195+ providerTurns : [ { ...base . providerTurns [ 0 ] ! , status : "completed" } ] ,
196+ } as unknown as OrchestrationV2ThreadProjection ;
197+ const commands : Parameters <
198+ ThreadManagementService . ThreadManagementService [ "Service" ] [ "dispatch" ]
199+ > [ 0 ] [ ] = [ ] ;
200+ yield * continueRestartedRun ( { threadId, sourceRunId : runId } ) . pipe (
201+ Effect . provide (
202+ Layer . merge (
203+ Layer . mock ( ThreadManagementService . ThreadManagementService ) ( {
204+ getThreadRecords : ( ) => Effect . succeed ( projection ) ,
205+ recoverDelegatedTask : ( ) => Effect . void ,
206+ dispatch : ( command ) => {
207+ commands . push ( command ) ;
208+ return Effect . succeed ( { } as never ) ;
209+ } ,
210+ } ) ,
211+ ServerSettings . layerTest ( { continueThreadsAfterServerUpdate : true } ) ,
212+ ) ,
213213 ) ,
214- ) ,
215- ) ;
216- assert . lengthOf ( commands , 1 ) ;
217- const command = commands [ 0 ] ! ;
218- assert . equal (
219- command . type === "message.dispatch" ? command . restartContinuationOfRunId : null ,
220- runId ,
221- ) ;
222- assert . include (
223- command . type === "message.dispatch" ? command . text : "" ,
224- "sleep 25 && echo DONE" ,
225- ) ;
226- assert . notInclude ( command . type === "message.dispatch" ? command . text : "" , "Continue where" ) ;
227- } ) ,
214+ ) ;
215+ assert . lengthOf ( commands , 0 ) ;
216+ } ) ,
228217) ;
229218
230219it . effect ( "does not continue a failed run that lost background work" , ( ) =>
@@ -419,7 +408,7 @@ it.effect("prepares no continuation for background work another provider thread
419408 ) ;
420409 yield * recovery . prepareForShutdown ;
421410 assert . lengthOf ( writes , 0 ) ;
422- // The same work on the Codex run's own provider thread is continued .
411+ // Work on the settled run's own provider thread must not wake it either .
423412 const ownWork = {
424413 ...projection ,
425414 turnItems : [ { ...projection . turnItems [ 0 ] ! , providerThreadId } ] ,
@@ -446,13 +435,103 @@ it.effect("prepares no continuation for background work another provider thread
446435 ) ,
447436 ) ;
448437 yield * ownRecovery . prepareForShutdown ;
449- assert . deepEqual (
450- writes . map ( ( write ) => write . effects [ 0 ] ?. request ) ,
451- [ { type : "provider-runtime.continue" , sourceRunId : runId } ] ,
452- ) ;
438+ assert . lengthOf ( writes , 0 ) ;
453439 } ) ,
454440) ;
455441
442+ it . effect . each ( [
443+ [ "completed" , scratchProjectId ] ,
444+ [ "completed" , ProjectId . make ( "restart-project" ) ] ,
445+ [ "waiting" , scratchProjectId ] ,
446+ [ "waiting" , ProjectId . make ( "restart-project" ) ] ,
447+ ] as const ) (
448+ "cleans up background work without waking a %s run in project %s" ,
449+ ( [ status , projectId ] ) =>
450+ Effect . gen ( function * ( ) {
451+ const base = makeProjection ( ) ;
452+ const projection = {
453+ ...base ,
454+ thread : { ...base . thread , projectId } ,
455+ runs : [ { ...base . runs [ 0 ] ! , status } ] ,
456+ providerTurns : [ { ...base . providerTurns [ 0 ] ! , status : "completed" } ] ,
457+ turnItems : [
458+ {
459+ id : "turn-item:background-subagent" ,
460+ runId,
461+ nodeId : null ,
462+ providerThreadId,
463+ providerInstanceId : instanceId ,
464+ type : "subagent" ,
465+ subagentId : "subagent:background" ,
466+ title : "Background reviewer" ,
467+ status : "running" ,
468+ } ,
469+ ] ,
470+ subagents : [
471+ {
472+ id : "subagent:background" ,
473+ runId,
474+ driver,
475+ providerInstanceId : instanceId ,
476+ status : "running" ,
477+ } ,
478+ ] ,
479+ } as unknown as OrchestrationV2ThreadProjection ;
480+ for ( const trigger of [ "startup" , "shutdown" ] as const ) {
481+ const commits : Parameters < EventSink . EventSinkV2 [ "Service" ] [ "commitCommand" ] > [ 0 ] [ ] = [ ] ;
482+ const writes : Parameters < EventSink . EventSinkV2 [ "Service" ] [ "writeWithEffects" ] > [ 0 ] [ ] = [ ] ;
483+ const recovery = yield * ProviderRuntimeRecovery . make . pipe (
484+ Effect . provide (
485+ Layer . mergeAll (
486+ ServerSettings . layerTest ( { continueThreadsAfterServerUpdate : true } ) ,
487+ Layer . mock ( ProjectionStore . ProjectionStoreV2 ) ( {
488+ getRecoveryThreadIds : ( ) => Effect . succeed ( [ threadId ] ) ,
489+ getRuntimeRecoveryProjection : ( ) => Effect . succeed ( projection ) ,
490+ } ) ,
491+ Layer . mock ( EventSink . EventSinkV2 ) ( {
492+ writeWithEffects : ( input ) =>
493+ Effect . sync ( ( ) => {
494+ writes . push ( input ) ;
495+ return [ ] ;
496+ } ) ,
497+ commitCommand : ( input ) =>
498+ Effect . sync ( ( ) => {
499+ commits . push ( input ) ;
500+ return { committed : true , cancelledEffectCount : 0 } as never ;
501+ } ) ,
502+ } ) ,
503+ IdAllocator . layer ,
504+ Layer . mock ( EffectWorker . OrchestrationEffectWorkerV2 ) ( {
505+ runRecoveryOnce : Effect . succeed ( false ) ,
506+ } ) ,
507+ Layer . mock ( EffectOutbox . EffectOutboxV2 ) ( {
508+ listByCommandId : ( ) => Effect . succeed ( [ ] ) ,
509+ reconcileAfterProcessLoss : Effect . succeed ( { requeued : 0 , cancelled : 0 } ) ,
510+ } ) ,
511+ ) ,
512+ ) ,
513+ ) ;
514+ if ( trigger === "shutdown" ) yield * recovery . prepareForShutdown ;
515+ yield * recovery . reconcile ( trigger ) ;
516+ assert . lengthOf ( writes , 0 ) ;
517+ assert . lengthOf ( commits , 1 ) ;
518+ assert . lengthOf ( commits [ 0 ] ! . effects , 0 ) ;
519+ const events = commits [ 0 ] ! . events ;
520+ for ( const type of [ "turn-item.updated" , "subagent.updated" ] as const )
521+ assert . isTrue (
522+ events . some ( ( event ) => event . type === type && event . payload . status === "cancelled" ) ,
523+ ) ;
524+ const note = events . find ( ( event ) => event . type === "run.background-work-cancelled" ) ;
525+ assert . equal ( note ?. runId , runId ) ;
526+ assert . deepEqual ( note ?. payload . restartCancelledBackgroundWork , [
527+ { kind : "subagent" , label : "Background reviewer" , id : "turn-item:background-subagent" } ,
528+ ] ) ;
529+ if ( status === "completed" )
530+ assert . isFalse ( events . some ( ( event ) => event . type === "run.updated" ) ) ;
531+ }
532+ } ) ,
533+ ) ;
534+
456535it . effect ( "does not cancel or resume a run that completes while shutdown intent commits" , ( ) =>
457536 Effect . gen ( function * ( ) {
458537 let projection = makeProjection ( ) ;
0 commit comments