@@ -673,6 +673,74 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
673673 expect ( provider . createTask ) . not . toHaveBeenCalled ( )
674674 } )
675675
676+ it ( "detects a live delegation owner in another provider instance" , ( ) => {
677+ const provider = {
678+ taskRegistry : { hasRunning : vi . fn ( ) . mockReturnValue ( false ) } ,
679+ } as unknown as ClineProvider
680+ const otherProvider = {
681+ taskRegistry : { hasRunning : vi . fn ( ( taskId : string ) => taskId === "live-child" ) } ,
682+ } as unknown as ClineProvider
683+ const activeInstances = Reflect . get ( ClineProvider , "activeInstances" )
684+ if ( ! ( activeInstances instanceof Set ) ) throw new Error ( "ClineProvider active instance registry unavailable" )
685+ activeInstances . add ( otherProvider )
686+
687+ try {
688+ expect ( ClineProvider . prototype [ "isTaskRunningInAnyProvider" ] . call ( provider , "live-child" ) ) . toBe ( true )
689+ } finally {
690+ activeInstances . delete ( otherProvider )
691+ }
692+ } )
693+
694+ it ( "rejects when a dead delegation chain becomes live during its atomic recovery" , async ( ) => {
695+ const oldChildId = "old-child"
696+ const grandchildId = "grandchild"
697+ const parent = {
698+ ...parentHistoryItem ,
699+ status : "delegated" as const ,
700+ awaitingChildId : oldChildId ,
701+ delegatedToId : oldChildId ,
702+ }
703+ const child = {
704+ ...parentHistoryItem ,
705+ id : oldChildId ,
706+ status : "delegated" as const ,
707+ awaitingChildId : grandchildId ,
708+ delegatedToId : grandchildId ,
709+ }
710+ const grandchild = { ...parentHistoryItem , id : grandchildId , status : "interrupted" as const }
711+ let childIsLive = false
712+ const taskHistoryStore = makeStoreStub ( {
713+ get : vi . fn ( ( id : string ) => ( id === "parent-1" ? parent : id === oldChildId ? child : grandchild ) ) ,
714+ atomicReadAndUpdate : vi . fn ( async ( _taskId : string , updater : ( item : HistoryItem ) => HistoryItem ) => {
715+ childIsLive = true
716+ updater ( child )
717+ return [ ]
718+ } ) ,
719+ } )
720+ const provider = {
721+ taskRegistry : { hasRunning : vi . fn ( ( id : string ) => id === oldChildId && childIsLive ) } ,
722+ getCurrentTask : vi . fn ( ( ) => makeParentTask ( ) ) ,
723+ removeClineFromStack : vi . fn ( ) ,
724+ createTask : vi . fn ( ) ,
725+ taskHistoryStore,
726+ } as unknown as ClineProvider
727+ Object . assign ( provider , {
728+ isTaskRunningInAnyProvider : ClineProvider . prototype [ "isTaskRunningInAnyProvider" ] ,
729+ refreshDelegationChain : ClineProvider . prototype [ "refreshDelegationChain" ] ,
730+ recoverDeadAwaitedChild : ClineProvider . prototype [ "recoverDeadAwaitedChild" ] ,
731+ } )
732+
733+ await expect (
734+ ClineProvider . prototype . delegateParentAndOpenChild . call ( provider , {
735+ parentTaskId : "parent-1" ,
736+ message : "Continue" ,
737+ initialTodos : [ ] ,
738+ mode : "code" ,
739+ } ) ,
740+ ) . rejects . toThrow ( `Delegation chain for child ${ oldChildId } became live during recovery` )
741+ expect ( provider . createTask ) . not . toHaveBeenCalled ( )
742+ } )
743+
676744 it ( "rejects with 'Cannot re-delegate' when the existing awaited child is still active" , async ( ) => {
677745 const oldChildId = "old-child"
678746 const activeChild = { id : oldChildId , status : "active" } as unknown as HistoryItem
0 commit comments