@@ -3511,6 +3511,201 @@ describe("ProviderCommandReactor", () => {
35113511 expect ( recoveredThread ?. session ?. runtimeMode ) . toBe ( "approval-required" ) ;
35123512 } ) ;
35133513
3514+ it ( "does not send a turn when a blocked runtime-mode replacement leaves the live session in place" , async ( ) => {
3515+ let blockRuntimeModeReplacement = false ;
3516+ const harness = await createHarness ( {
3517+ startSessionEffect : ( session ) => {
3518+ if ( blockRuntimeModeReplacement && session . runtimeMode === "approval-required" ) {
3519+ return Effect . fail (
3520+ new ProviderAdapterRequestError ( {
3521+ provider : session . provider ,
3522+ method : "startSession" ,
3523+ detail :
3524+ "Claude session replacement is blocked: 1 live task(s) are still running. Retry after background work finishes." ,
3525+ } ) ,
3526+ ) ;
3527+ }
3528+ return Effect . succeed ( session ) ;
3529+ } ,
3530+ } ) ;
3531+ const now = "2026-01-01T00:00:00.000Z" ;
3532+ const threadId = ThreadId . make ( "thread-1" ) ;
3533+
3534+ await harness . runEffect (
3535+ harness . engine . dispatch ( {
3536+ type : "thread.runtime-mode.set" ,
3537+ commandId : CommandId . make ( "cmd-runtime-mode-set-blocked-full-access" ) ,
3538+ threadId,
3539+ runtimeMode : "full-access" ,
3540+ createdAt : now ,
3541+ } ) ,
3542+ ) ;
3543+
3544+ await harness . runEffect (
3545+ harness . engine . dispatch ( {
3546+ type : "thread.turn.start" ,
3547+ commandId : CommandId . make ( "cmd-turn-start-runtime-mode-blocked-1" ) ,
3548+ threadId,
3549+ message : {
3550+ messageId : asMessageId ( "user-message-runtime-mode-blocked-1" ) ,
3551+ role : "user" ,
3552+ text : "first" ,
3553+ attachments : [ ] ,
3554+ } ,
3555+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
3556+ runtimeMode : "full-access" ,
3557+ createdAt : now ,
3558+ } ) ,
3559+ ) ;
3560+
3561+ await waitFor ( ( ) => harness . startSession . mock . calls . length === 1 ) ;
3562+ await waitFor ( ( ) => harness . sendTurn . mock . calls . length === 1 ) ;
3563+ expect ( harness . runtimeSessions [ 0 ] ?. runtimeMode ) . toBe ( "full-access" ) ;
3564+
3565+ blockRuntimeModeReplacement = true ;
3566+
3567+ await harness . runEffect (
3568+ harness . engine . dispatch ( {
3569+ type : "thread.runtime-mode.set" ,
3570+ commandId : CommandId . make ( "cmd-runtime-mode-set-blocked-approval-required" ) ,
3571+ threadId,
3572+ runtimeMode : "approval-required" ,
3573+ createdAt : now ,
3574+ } ) ,
3575+ ) ;
3576+ await waitFor ( ( ) => harness . startSession . mock . calls . length === 2 ) ;
3577+ await harness . drain ( ) ;
3578+
3579+ expect ( harness . sendTurn . mock . calls . length ) . toBe ( 1 ) ;
3580+ expect ( harness . runtimeSessions ) . toHaveLength ( 1 ) ;
3581+ expect ( harness . runtimeSessions [ 0 ] ?. runtimeMode ) . toBe ( "full-access" ) ;
3582+
3583+ await harness . runEffect (
3584+ harness . engine . dispatch ( {
3585+ type : "thread.turn.start" ,
3586+ commandId : CommandId . make ( "cmd-turn-start-runtime-mode-blocked-2" ) ,
3587+ threadId,
3588+ message : {
3589+ messageId : asMessageId ( "user-message-runtime-mode-blocked-2" ) ,
3590+ role : "user" ,
3591+ text : "second" ,
3592+ attachments : [ ] ,
3593+ } ,
3594+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
3595+ runtimeMode : "approval-required" ,
3596+ createdAt : now ,
3597+ } ) ,
3598+ ) ;
3599+
3600+ await waitFor ( async ( ) => {
3601+ const readModel = await harness . readModel ( ) ;
3602+ const thread = readModel . threads . find ( ( entry ) => entry . id === threadId ) ;
3603+ return (
3604+ thread ?. activities . some ( ( activity ) => activity . kind === "provider.turn.start.failed" ) ??
3605+ false
3606+ ) ;
3607+ } ) ;
3608+
3609+ expect ( harness . startSession . mock . calls . length ) . toBe ( 3 ) ;
3610+ expect ( harness . sendTurn . mock . calls . length ) . toBe ( 1 ) ;
3611+ expect ( harness . runtimeSessions [ 0 ] ?. runtimeMode ) . toBe ( "full-access" ) ;
3612+
3613+ const poisoned = await harness . readModel ( ) ;
3614+ const poisonedThread = poisoned . threads . find ( ( entry ) => entry . id === threadId ) ;
3615+ expect ( poisonedThread ?. runtimeMode ) . toBe ( "approval-required" ) ;
3616+ expect ( poisonedThread ?. session ?. runtimeMode ) . toBe ( "full-access" ) ;
3617+
3618+ harness . threadBackgroundLiveness . recordTaskLiveness ( {
3619+ threadId,
3620+ taskId : "task-live" ,
3621+ taskType : "local_agent" ,
3622+ status : "in_progress" ,
3623+ kind : "started" ,
3624+ } ) ;
3625+
3626+ await harness . runEffect (
3627+ harness . engine . dispatch ( {
3628+ type : "thread.turn.start" ,
3629+ commandId : CommandId . make ( "cmd-turn-start-runtime-mode-blocked-3" ) ,
3630+ threadId,
3631+ message : {
3632+ messageId : asMessageId ( "user-message-runtime-mode-blocked-3" ) ,
3633+ role : "user" ,
3634+ text : "third" ,
3635+ attachments : [ ] ,
3636+ } ,
3637+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
3638+ runtimeMode : "approval-required" ,
3639+ createdAt : now ,
3640+ } ) ,
3641+ ) ;
3642+
3643+ await waitFor ( async ( ) => {
3644+ const readModel = await harness . readModel ( ) ;
3645+ const thread = readModel . threads . find ( ( entry ) => entry . id === threadId ) ;
3646+ return (
3647+ thread ?. activities . filter ( ( activity ) => activity . kind === "provider.turn.start.failed" )
3648+ . length === 2
3649+ ) ;
3650+ } ) ;
3651+
3652+ expect ( harness . startSession . mock . calls . length ) . toBe ( 3 ) ;
3653+ expect ( harness . sendTurn . mock . calls . length ) . toBe ( 1 ) ;
3654+ expect ( harness . stopSession . mock . calls . length ) . toBe ( 0 ) ;
3655+
3656+ const blocked = await harness . readModel ( ) ;
3657+ const blockedThread = blocked . threads . find ( ( entry ) => entry . id === threadId ) ;
3658+ expect ( blockedThread ?. activities ) . toEqual (
3659+ expect . arrayContaining ( [
3660+ expect . objectContaining ( {
3661+ kind : "provider.turn.start.failed" ,
3662+ payload : expect . objectContaining ( {
3663+ detail : expect . stringContaining ( "cannot apply runtime mode 'approval-required'" ) ,
3664+ } ) ,
3665+ } ) ,
3666+ ] ) ,
3667+ ) ;
3668+ expect ( blockedThread ?. session ?. runtimeMode ) . toBe ( "full-access" ) ;
3669+ expect ( harness . runtimeSessions [ 0 ] ?. runtimeMode ) . toBe ( "full-access" ) ;
3670+
3671+ blockRuntimeModeReplacement = false ;
3672+ harness . threadBackgroundLiveness . recordTaskLiveness ( {
3673+ threadId,
3674+ taskId : "task-live" ,
3675+ taskType : "local_agent" ,
3676+ status : "completed" ,
3677+ kind : "completed" ,
3678+ } ) ;
3679+
3680+ await harness . runEffect (
3681+ harness . engine . dispatch ( {
3682+ type : "thread.turn.start" ,
3683+ commandId : CommandId . make ( "cmd-turn-start-runtime-mode-blocked-4" ) ,
3684+ threadId,
3685+ message : {
3686+ messageId : asMessageId ( "user-message-runtime-mode-blocked-4" ) ,
3687+ role : "user" ,
3688+ text : "fourth" ,
3689+ attachments : [ ] ,
3690+ } ,
3691+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
3692+ runtimeMode : "approval-required" ,
3693+ createdAt : now ,
3694+ } ) ,
3695+ ) ;
3696+
3697+ await waitFor ( ( ) => harness . startSession . mock . calls . length === 4 ) ;
3698+ await waitFor ( ( ) => harness . sendTurn . mock . calls . length === 2 ) ;
3699+ expect ( harness . startSession . mock . calls [ 3 ] ?. [ 1 ] ) . toMatchObject ( {
3700+ threadId,
3701+ runtimeMode : "approval-required" ,
3702+ } ) ;
3703+
3704+ const recovered = await harness . readModel ( ) ;
3705+ const recoveredThread = recovered . threads . find ( ( entry ) => entry . id === threadId ) ;
3706+ expect ( recoveredThread ?. session ?. runtimeMode ) . toBe ( "approval-required" ) ;
3707+ } ) ;
3708+
35143709 it ( "reuses a live claude session when provider options change during background work" , async ( ) => {
35153710 const harness = await createHarness ( {
35163711 threadModelSelection : {
0 commit comments