@@ -663,6 +663,78 @@ describe("ClineProvider flicker-free cancel", () => {
663663 )
664664 } )
665665
666+ it ( "preserves delegated lineage when cancelling an already-interrupted child" , async ( ) => {
667+ const childHistory : HistoryItem = {
668+ id : "child-1" ,
669+ number : 2 ,
670+ task : "child task" ,
671+ ts : Date . now ( ) ,
672+ tokensIn : 10 ,
673+ tokensOut : 20 ,
674+ totalCost : 0.001 ,
675+ workspace : "/test/workspace" ,
676+ parentTaskId : "parent-1" ,
677+ rootTaskId : "root-1" ,
678+ status : "interrupted" ,
679+ }
680+ const parentHistory : HistoryItem = {
681+ id : "parent-1" ,
682+ number : 1 ,
683+ task : "parent task" ,
684+ ts : Date . now ( ) ,
685+ tokensIn : 10 ,
686+ tokensOut : 20 ,
687+ totalCost : 0.001 ,
688+ workspace : "/test/workspace" ,
689+ status : "delegated" ,
690+ awaitingChildId : "child-1" ,
691+ delegatedToId : "child-1" ,
692+ }
693+
694+ Object . assign ( mockTask1 , {
695+ taskId : "child-1" ,
696+ instanceId : "instance-child" ,
697+ rootTask : { taskId : "root-1" } ,
698+ parentTask : { taskId : "parent-1" } ,
699+ parentTaskId : "parent-1" ,
700+ cancelCurrentRequest : vi . fn ( ) ,
701+ abortTask : vi . fn ( ) . mockResolvedValue ( undefined ) ,
702+ abandoned : false ,
703+ isStreaming : false ,
704+ didFinishAbortingStream : true ,
705+ isWaitingForFirstChunk : false ,
706+ } )
707+ seedRegistry ( provider , mockTask1 )
708+ provider . getTaskWithId = vi . fn ( ) . mockImplementation ( ( id ) => {
709+ if ( id === "child-1" ) return Promise . resolve ( { historyItem : childHistory } )
710+ if ( id === "parent-1" ) return Promise . resolve ( { historyItem : parentHistory } )
711+ throw new Error ( `unexpected task lookup: ${ id } ` )
712+ } ) as unknown as ClineProvider [ "getTaskWithId" ]
713+
714+ const updateTaskHistorySpy = vi . spyOn ( provider , "updateTaskHistory" ) . mockResolvedValue ( [ ] )
715+ const createTaskWithHistoryItemSpy = vi
716+ . spyOn ( provider , "createTaskWithHistoryItem" )
717+ . mockResolvedValue ( undefined as unknown as CreatedHistoryTask )
718+
719+ await provider . cancelTask ( )
720+
721+ expect ( updateTaskHistorySpy ) . not . toHaveBeenCalled ( )
722+ expect ( createTaskWithHistoryItemSpy ) . toHaveBeenCalledWith (
723+ expect . objectContaining ( {
724+ id : "child-1" ,
725+ status : "interrupted" ,
726+ parentTaskId : "parent-1" ,
727+ rootTaskId : "root-1" ,
728+ parentTask : expect . objectContaining ( { taskId : "parent-1" } ) ,
729+ rootTask : expect . objectContaining ( { taskId : "root-1" } ) ,
730+ } ) ,
731+ )
732+ expect ( provider [ "cancelledDelegationChildIds" ] . has ( "child-1" ) ) . toBe ( false )
733+ expect ( mockOutputChannel . appendLine ) . not . toHaveBeenCalledWith (
734+ expect . stringContaining ( "Invalid task status transition: interrupted → interrupted" ) ,
735+ )
736+ } )
737+
666738 it ( "detaches runtime parent links when delegated parent detach fails" , async ( ) => {
667739 const mockRootTask = { taskId : "root-1" }
668740 const mockParentTask = { taskId : "parent-1" }
0 commit comments