@@ -18,6 +18,7 @@ import {
1818 ProviderSessionId ,
1919 ProviderThreadId ,
2020 ProviderTurnId ,
21+ ScheduledTaskId ,
2122 ThreadId ,
2223 TurnItemId ,
2324 ProviderDriverKind ,
@@ -2973,6 +2974,100 @@ describe("orchestration v2 provider switching", () => {
29732974 ) ,
29742975 ) ;
29752976
2977+ it . live (
2978+ "starts a scheduled fresh run while the previous run's session still has background work" ,
2979+ ( ) =>
2980+ Effect . scoped (
2981+ Effect . gen ( function * ( ) {
2982+ const cwd = yield * checkpointWorkspace ( "scheduled-fresh-background" ) ;
2983+ const capturedTurns = yield * Ref . make < ReadonlyArray < CapturedTurn > > ( [ ] ) ;
2984+ const closedSessions = yield * Ref . make < ReadonlyArray < string > > ( [ ] ) ;
2985+ const backgroundWork = yield * Ref . make ( false ) ;
2986+ const layerRegistry = ProviderAdapterRegistry . layerFromAdapters ( [
2987+ makeTestAdapter ( {
2988+ instanceId : CLAUDE_MODEL_SELECTION . instanceId ,
2989+ driver : CLAUDE_DRIVER ,
2990+ capabilities : ClaudeProviderCapabilitiesV2 ,
2991+ modelSelection : CLAUDE_MODEL_SELECTION ,
2992+ responseByRunOrdinal : { } ,
2993+ capturedTurns,
2994+ closedSessions,
2995+ backgroundWork,
2996+ nativeThreadGeneration : yield * Ref . make ( 0 ) ,
2997+ } ) ,
2998+ ] ) ;
2999+ const threadId = ThreadId . make ( "thread:scheduled-fresh-background" ) ;
3000+ yield * Effect . gen ( function * ( ) {
3001+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
3002+ const worker = yield * EffectWorker . OrchestrationEffectWorkerV2 ;
3003+ // The command ScheduledTaskService sends for a fresh-session schedule.
3004+ const fire = ( name : string ) =>
3005+ orchestrator . dispatch ( {
3006+ type : "message.dispatch" ,
3007+ createdBy : "user" ,
3008+ creationSource : "web" ,
3009+ commandId : CommandId . make ( `scheduled-task:scheduled-fresh-background:${ name } ` ) ,
3010+ threadId,
3011+ messageId : MessageId . make ( `message:scheduled-fresh-background:${ name } ` ) ,
3012+ scheduledTaskId : ScheduledTaskId . make ( "scheduled-fresh-background" ) ,
3013+ text : `Scheduled ${ name } ` ,
3014+ attachments : [ ] ,
3015+ modelSelection : CLAUDE_MODEL_SELECTION ,
3016+ freshContext : true ,
3017+ dispatchMode : { type : "queue_after_active" } ,
3018+ } ) ;
3019+ yield * orchestrator . dispatch ( {
3020+ type : "thread.create" ,
3021+ createdBy : "user" ,
3022+ creationSource : "web" ,
3023+ commandId : CommandId . make ( "command:scheduled-fresh-background:create" ) ,
3024+ threadId,
3025+ projectId : ProjectId . make ( "project:scheduled-fresh-background" ) ,
3026+ title : "Scheduled fresh session" ,
3027+ modelSelection : CLAUDE_MODEL_SELECTION ,
3028+ runtimeMode : "full-access" ,
3029+ interactionMode : "default" ,
3030+ branch : null ,
3031+ worktreePath : cwd ,
3032+ } ) ;
3033+ yield * fire ( "first" ) ;
3034+ yield * worker . drain ( ) ;
3035+ yield * waitForIdle ( threadId ) ;
3036+ // The first run left a background command running after its turn ended.
3037+ yield * Ref . set ( backgroundWork , true ) ;
3038+ yield * fire ( "second" ) ;
3039+ yield * worker . drain ( ) ;
3040+ yield * waitForIdle ( threadId ) ;
3041+ const turns = yield * Ref . get ( capturedTurns ) ;
3042+ const turnFor = ( name : string ) =>
3043+ turns . find ( ( turn ) => turn . text . endsWith ( `Scheduled ${ name } ` ) ) ;
3044+ assert . isDefined ( turnFor ( "second" ) ) ;
3045+ assert . notEqual ( turnFor ( "second" ) ! . nativeThreadId , turnFor ( "first" ) ! . nativeThreadId ) ;
3046+ // Closing the busy session would end its background work.
3047+ assert . deepEqual ( yield * Ref . get ( closedSessions ) , [ ] ) ;
3048+ } ) . pipe (
3049+ Effect . provide (
3050+ ProviderReplayHarness . layerWithRegistry (
3051+ {
3052+ name : "scheduled-fresh-background" ,
3053+ runtimePolicyOverride : {
3054+ cwd,
3055+ approvalPolicy : "never" ,
3056+ sandboxPolicy : {
3057+ type : "readOnly" ,
3058+ access : { type : "fullAccess" } ,
3059+ networkAccess : false ,
3060+ } ,
3061+ } ,
3062+ } ,
3063+ layerRegistry ,
3064+ ) ,
3065+ ) ,
3066+ ) ;
3067+ } ) ,
3068+ ) ,
3069+ ) ;
3070+
29763071 it . live ( "keeps the thread a queued fresh run reserved for that run" , ( ) =>
29773072 Effect . scoped (
29783073 Effect . gen ( function * ( ) {
0 commit comments