@@ -910,7 +910,83 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
910910 expect ( taskRegistry . push ) . toHaveBeenCalledOnce ( )
911911 } )
912912
913- it ( "rejects re-delegation when the interrupted awaited child becomes live before the commit" , async ( ) => {
913+ it . each ( [ "this provider" , "another provider" ] as const ) (
914+ "rejects re-delegation when the interrupted awaited child becomes live in %s before the commit" ,
915+ async ( owner ) => {
916+ const oldChildId = "old-child"
917+ const parent : HistoryItem = {
918+ ...parentHistoryItem ,
919+ status : "delegated" ,
920+ awaitingChildId : oldChildId ,
921+ delegatedToId : oldChildId ,
922+ childIds : [ oldChildId ] ,
923+ }
924+ const records = new Map < string , HistoryItem > ( [
925+ [ "parent-1" , parent ] ,
926+ [ oldChildId , { ...parentHistoryItem , id : oldChildId , status : "interrupted" , parentTaskId : "parent-1" } ] ,
927+ ] )
928+ let oldChildIsLive = false
929+ const isLiveIn = ( target : typeof owner ) => ( id : string ) =>
930+ owner === target && id === oldChildId && oldChildIsLive
931+ const taskHistoryStore = makeStoreStub ( {
932+ get : vi . fn ( ( id : string ) => records . get ( id ) ) ,
933+ atomicReadAndUpdate : vi . fn ( async ( taskId : string , updater : ( item : HistoryItem ) => HistoryItem ) => {
934+ records . set ( taskId , updater ( records . get ( taskId ) ! ) )
935+ return [ ]
936+ } ) ,
937+ } )
938+ const parentTask = makeParentTask ( )
939+ const provider = {
940+ _disposed : false ,
941+ taskScheduler : new TaskScheduler ( ) ,
942+ taskRegistry : { hasRunning : vi . fn ( isLiveIn ( "this provider" ) ) } ,
943+ emit : vi . fn ( ) ,
944+ getCurrentTask : vi . fn ( ( ) => parentTask ) ,
945+ removeClineFromStack : vi . fn ( ) . mockResolvedValue ( undefined ) ,
946+ // The old child is resumed after the pre-check but before the parent commit.
947+ createTask : vi . fn ( async ( ) => {
948+ oldChildIsLive = true
949+ return { taskId : "child-2" , run : vi . fn ( ) . mockResolvedValue ( undefined ) }
950+ } ) ,
951+ deleteTaskWithId : vi . fn ( ) . mockResolvedValue ( undefined ) ,
952+ getTaskWithId : vi . fn ( async ( ) => ( { historyItem : records . get ( "parent-1" ) } ) ) ,
953+ createTaskWithHistoryItem : vi . fn ( ) . mockResolvedValue ( undefined ) ,
954+ log : vi . fn ( ) ,
955+ isViewLaunched : false ,
956+ recentTasksCache : undefined ,
957+ taskHistoryStore,
958+ } as unknown as ClineProvider
959+ Object . assign ( provider , {
960+ isTaskRunningInAnyProvider : ClineProvider . prototype [ "isTaskRunningInAnyProvider" ] ,
961+ } )
962+ const otherProvider = {
963+ taskRegistry : { hasRunning : vi . fn ( isLiveIn ( "another provider" ) ) } ,
964+ } as unknown as ClineProvider
965+ const activeInstances = Reflect . get ( ClineProvider , "activeInstances" )
966+ if ( ! ( activeInstances instanceof Set ) ) throw new Error ( "ClineProvider active instance registry unavailable" )
967+ activeInstances . add ( otherProvider )
968+
969+ try {
970+ await expect (
971+ ClineProvider . prototype . delegateParentAndOpenChild . call ( provider , {
972+ parentTaskId : "parent-1" ,
973+ message : "Continue" ,
974+ initialTodos : [ ] ,
975+ mode : "code" ,
976+ } ) ,
977+ ) . rejects . toThrow ( `awaited child ${ oldChildId } became live before delegation commit` )
978+ } finally {
979+ activeInstances . delete ( otherProvider )
980+ }
981+ expect ( records . get ( "parent-1" ) ) . toMatchObject ( {
982+ status : "delegated" ,
983+ awaitingChildId : oldChildId ,
984+ delegatedToId : oldChildId ,
985+ } )
986+ } ,
987+ )
988+
989+ it ( "delays awaited child registration until the re-delegation commit finishes" , async ( ) => {
914990 const oldChildId = "old-child"
915991 const parent : HistoryItem = {
916992 ...parentHistoryItem ,
@@ -923,52 +999,61 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
923999 [ "parent-1" , parent ] ,
9241000 [ oldChildId , { ...parentHistoryItem , id : oldChildId , status : "interrupted" , parentTaskId : "parent-1" } ] ,
9251001 ] )
926- let oldChildIsLive = false
1002+ let releaseCommit ! : ( ) => void
1003+ const commitBlocked = new Promise < void > ( ( resolve ) => {
1004+ releaseCommit = resolve
1005+ } )
9271006 const taskHistoryStore = makeStoreStub ( {
9281007 get : vi . fn ( ( id : string ) => records . get ( id ) ) ,
9291008 atomicReadAndUpdate : vi . fn ( async ( taskId : string , updater : ( item : HistoryItem ) => HistoryItem ) => {
9301009 records . set ( taskId , updater ( records . get ( taskId ) ! ) )
1010+ await commitBlocked
9311011 return [ ]
9321012 } ) ,
9331013 } )
9341014 const parentTask = makeParentTask ( )
1015+ const taskRegistry = { hasRunning : vi . fn ( ) . mockReturnValue ( false ) , push : vi . fn ( ) }
9351016 const provider = {
9361017 _disposed : false ,
9371018 taskScheduler : new TaskScheduler ( ) ,
938- taskRegistry : { hasRunning : vi . fn ( ( id : string ) => id === oldChildId && oldChildIsLive ) } ,
1019+ taskRegistry,
9391020 emit : vi . fn ( ) ,
9401021 getCurrentTask : vi . fn ( ( ) => parentTask ) ,
9411022 removeClineFromStack : vi . fn ( ) . mockResolvedValue ( undefined ) ,
942- // The old child is resumed elsewhere after the pre-check but before the parent commit.
943- createTask : vi . fn ( async ( ) => {
944- oldChildIsLive = true
945- return { taskId : "child-2" , run : vi . fn ( ) . mockResolvedValue ( undefined ) }
946- } ) ,
1023+ createTask : vi . fn ( async ( ) => ( { taskId : "child-2" , run : vi . fn ( ) . mockResolvedValue ( undefined ) } ) ) ,
9471024 deleteTaskWithId : vi . fn ( ) . mockResolvedValue ( undefined ) ,
9481025 getTaskWithId : vi . fn ( async ( ) => ( { historyItem : records . get ( "parent-1" ) } ) ) ,
9491026 createTaskWithHistoryItem : vi . fn ( ) . mockResolvedValue ( undefined ) ,
9501027 log : vi . fn ( ) ,
9511028 isViewLaunched : false ,
9521029 recentTasksCache : undefined ,
9531030 taskHistoryStore,
1031+ throwIfTaskRegistrationCancelled : ClineProvider . prototype [ "throwIfTaskRegistrationCancelled" ] ,
1032+ performPreparationTasks : vi . fn ( ) ,
1033+ getState : vi . fn ( ) . mockResolvedValue ( { mode : "code" } ) ,
9541034 } as unknown as ClineProvider
9551035 Object . assign ( provider , {
9561036 isTaskRunningInAnyProvider : ClineProvider . prototype [ "isTaskRunningInAnyProvider" ] ,
9571037 } )
9581038
959- await expect (
960- ClineProvider . prototype . delegateParentAndOpenChild . call ( provider , {
961- parentTaskId : "parent-1" ,
962- message : "Continue" ,
963- initialTodos : [ ] ,
964- mode : "code" ,
965- } ) ,
966- ) . rejects . toThrow ( `awaited child ${ oldChildId } became live before delegation commit` )
967- expect ( records . get ( "parent-1" ) ) . toMatchObject ( {
968- status : "delegated" ,
969- awaitingChildId : oldChildId ,
970- delegatedToId : oldChildId ,
1039+ const delegation = ClineProvider . prototype . delegateParentAndOpenChild . call ( provider , {
1040+ parentTaskId : "parent-1" ,
1041+ message : "Continue" ,
1042+ initialTodos : [ ] ,
1043+ mode : "code" ,
9711044 } )
1045+ await vi . waitFor ( ( ) => expect ( taskHistoryStore . atomicReadAndUpdate ) . toHaveBeenCalled ( ) )
1046+ const registration = ClineProvider . prototype . addClineToStack . call ( provider , {
1047+ taskId : oldChildId ,
1048+ emit : vi . fn ( ) ,
1049+ } as never )
1050+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) )
1051+ expect ( taskRegistry . push ) . not . toHaveBeenCalled ( )
1052+
1053+ releaseCommit ( )
1054+ await Promise . all ( [ delegation , registration ] )
1055+ expect ( taskRegistry . push ) . toHaveBeenCalledOnce ( )
1056+ expect ( records . get ( "parent-1" ) ) . toMatchObject ( { status : "delegated" , awaitingChildId : "child-2" } )
9721057 } )
9731058
9741059 it ( "does not recover when a descendant is live in another provider" , async ( ) => {
0 commit comments