@@ -2896,6 +2896,131 @@ describe("orchestration v2 provider switching", () => {
28962896 ) ,
28972897 ) ;
28982898
2899+ // regression: #16000 thread setting changes must not end a Claude session's background work
2900+ it . live . each ( [
2901+ [ "runtime mode" , "runtime-mode" ] ,
2902+ [ "worktree" , "worktree" ] ,
2903+ [ "provider instance" , "instance" ] ,
2904+ ] as const ) ( "keeps background work alive when the %s changes on an idle thread" , ( [ , change ] ) =>
2905+ Effect . scoped (
2906+ Effect . gen ( function * ( ) {
2907+ const cwd = yield * checkpointWorkspace ( `settings-background-${ change } ` ) ;
2908+ const nextCwd = yield * checkpointWorkspace ( `settings-background-${ change } -next` ) ;
2909+ const capturedTurns = yield * Ref . make < ReadonlyArray < CapturedTurn > > ( [ ] ) ;
2910+ const backgroundWork = yield * Ref . make ( false ) ;
2911+ const closedSessions = yield * Ref . make < ReadonlyArray < string > > ( [ ] ) ;
2912+ const secondSelection : ModelSelection = {
2913+ ...CLAUDE_MODEL_SELECTION ,
2914+ instanceId : ProviderInstanceId . make ( "claude-second-account" ) ,
2915+ } ;
2916+ const threadId = ThreadId . make ( `thread:settings-background-${ change } ` ) ;
2917+ yield * Effect . gen ( function * ( ) {
2918+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
2919+ const worker = yield * EffectWorker . OrchestrationEffectWorkerV2 ;
2920+ yield * orchestrator . dispatch ( {
2921+ type : "thread.create" ,
2922+ createdBy : "user" ,
2923+ creationSource : "web" ,
2924+ commandId : CommandId . make ( `command:settings-background-${ change } :create` ) ,
2925+ threadId,
2926+ projectId : ProjectId . make ( `project:settings-background-${ change } ` ) ,
2927+ title : "Settings change with background work" ,
2928+ modelSelection : CLAUDE_MODEL_SELECTION ,
2929+ runtimeMode : "full-access" ,
2930+ interactionMode : "default" ,
2931+ branch : null ,
2932+ worktreePath : cwd ,
2933+ } ) ;
2934+ yield * orchestrator . dispatch ( {
2935+ type : "message.dispatch" ,
2936+ createdBy : "user" ,
2937+ creationSource : "web" ,
2938+ commandId : CommandId . make ( `command:settings-background-${ change } :send` ) ,
2939+ threadId,
2940+ messageId : MessageId . make ( `message:settings-background-${ change } :send` ) ,
2941+ text : "Start background work" ,
2942+ attachments : [ ] ,
2943+ modelSelection : CLAUDE_MODEL_SELECTION ,
2944+ dispatchMode : { type : "start_immediately" } ,
2945+ } ) ;
2946+ yield * worker . drain ( ) ;
2947+ const afterFirst = yield * waitForIdle ( threadId ) ;
2948+ const session = afterFirst . providerSessions [ 0 ] ;
2949+ assert . equal ( session ?. status , "ready" ) ;
2950+ // The turn ended but a background subagent keeps running in the CLI process.
2951+ yield * Ref . set ( backgroundWork , true ) ;
2952+
2953+ const commandId = CommandId . make ( `command:settings-background-${ change } :change` ) ;
2954+ yield * orchestrator
2955+ . dispatch (
2956+ change === "runtime-mode"
2957+ ? {
2958+ type : "thread.runtime-mode.set" ,
2959+ commandId,
2960+ threadId,
2961+ runtimeMode : "auto-accept-edits" ,
2962+ }
2963+ : change === "worktree"
2964+ ? { type : "thread.metadata.update" , commandId, threadId, worktreePath : nextCwd }
2965+ : {
2966+ type : "thread.model-selection.set" ,
2967+ commandId,
2968+ threadId,
2969+ modelSelection : secondSelection ,
2970+ } ,
2971+ )
2972+ . pipe ( Effect . ignore ) ;
2973+ yield * worker . drain ( ) ;
2974+
2975+ const projection = yield * orchestrator . getThreadProjection ( threadId ) ;
2976+ // Refusing or deferring are both fine; closing the busy process is not.
2977+ assert . deepEqual ( yield * Ref . get ( closedSessions ) , [ ] ) ;
2978+ assert . deepEqual (
2979+ projection . providerSessions . map ( ( candidate ) => [ candidate . id , candidate . status ] ) ,
2980+ [ [ session ! . id , "ready" ] ] ,
2981+ ) ;
2982+ } ) . pipe (
2983+ Effect . provide (
2984+ ProviderReplayHarness . layerWithRegistry (
2985+ {
2986+ name : `settings-background-${ change } ` ,
2987+ runtimePolicyOverride : {
2988+ cwd,
2989+ approvalPolicy : "never" ,
2990+ sandboxPolicy : {
2991+ type : "readOnly" ,
2992+ access : { type : "fullAccess" } ,
2993+ networkAccess : false ,
2994+ } ,
2995+ } ,
2996+ } ,
2997+ ProviderAdapterRegistry . layerFromAdapters ( [
2998+ makeTestAdapter ( {
2999+ instanceId : CLAUDE_MODEL_SELECTION . instanceId ,
3000+ driver : CLAUDE_DRIVER ,
3001+ capabilities : ClaudeProviderCapabilitiesV2 ,
3002+ modelSelection : CLAUDE_MODEL_SELECTION ,
3003+ responseByRunOrdinal : { 1 : "Background work started" } ,
3004+ capturedTurns,
3005+ backgroundWork,
3006+ closedSessions,
3007+ } ) ,
3008+ makeTestAdapter ( {
3009+ instanceId : secondSelection . instanceId ,
3010+ driver : CLAUDE_DRIVER ,
3011+ capabilities : ClaudeProviderCapabilitiesV2 ,
3012+ modelSelection : secondSelection ,
3013+ responseByRunOrdinal : { } ,
3014+ capturedTurns,
3015+ } ) ,
3016+ ] ) ,
3017+ ) ,
3018+ ) ,
3019+ ) ;
3020+ } ) ,
3021+ ) ,
3022+ ) ;
3023+
28993024 it . live . each ( [ "immediately" , "behind a running turn" ] as const ) (
29003025 "starts a fresh provider session %s and never hands earlier runs to a later provider" ,
29013026 ( variant ) =>
0 commit comments