@@ -663,22 +663,100 @@ 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" ,
666+ it . each ( [
667+ [ "a stale cancellation guard" , true ] ,
668+ [ "an empty cancellation guard" , false ] ,
669+ ] as const ) (
670+ "preserves delegated lineage when cancelling an already-interrupted child with %s" ,
671+ async ( _case , seedGuard ) => {
672+ const childHistory : HistoryItem = {
673+ id : "child-1" ,
674+ number : 2 ,
675+ task : "child task" ,
676+ ts : Date . now ( ) ,
677+ tokensIn : 10 ,
678+ tokensOut : 20 ,
679+ totalCost : 0.001 ,
680+ workspace : "/test/workspace" ,
681+ parentTaskId : "parent-1" ,
682+ rootTaskId : "root-1" ,
683+ status : "interrupted" ,
684+ }
685+ const parentHistory : HistoryItem = {
686+ id : "parent-1" ,
687+ number : 1 ,
688+ task : "parent task" ,
689+ ts : Date . now ( ) ,
690+ tokensIn : 10 ,
691+ tokensOut : 20 ,
692+ totalCost : 0.001 ,
693+ workspace : "/test/workspace" ,
694+ status : "delegated" ,
695+ awaitingChildId : "child-1" ,
696+ delegatedToId : "child-1" ,
697+ }
698+
699+ Object . assign ( mockTask1 , {
700+ taskId : "child-1" ,
701+ instanceId : "instance-child" ,
702+ rootTask : { taskId : "root-1" } ,
703+ parentTask : { taskId : "parent-1" } ,
704+ parentTaskId : "parent-1" ,
705+ cancelCurrentRequest : vi . fn ( ) ,
706+ abortTask : vi . fn ( ) . mockResolvedValue ( undefined ) ,
707+ abandoned : false ,
708+ isStreaming : false ,
709+ didFinishAbortingStream : true ,
710+ isWaitingForFirstChunk : false ,
711+ } )
712+ seedRegistry ( provider , mockTask1 )
713+ provider . getTaskWithId = vi . fn ( ) . mockImplementation ( ( id ) => {
714+ if ( id === "child-1" ) return Promise . resolve ( { historyItem : childHistory } )
715+ if ( id === "parent-1" ) return Promise . resolve ( { historyItem : parentHistory } )
716+ throw new Error ( `unexpected task lookup: ${ id } ` )
717+ } ) as unknown as ClineProvider [ "getTaskWithId" ]
718+
719+ const updateTaskHistorySpy = vi . spyOn ( provider , "updateTaskHistory" ) . mockResolvedValue ( [ ] )
720+ const createTaskWithHistoryItemSpy = vi
721+ . spyOn ( provider , "createTaskWithHistoryItem" )
722+ . mockResolvedValue ( undefined as unknown as CreatedHistoryTask )
723+ if ( seedGuard ) provider [ "cancelledDelegationChildIds" ] . add ( "child-1" )
724+ expect ( provider [ "cancelledDelegationChildIds" ] . has ( "child-1" ) ) . toBe ( seedGuard )
725+
726+ await provider . cancelTask ( )
727+
728+ expect ( updateTaskHistorySpy ) . not . toHaveBeenCalled ( )
729+ expect ( createTaskWithHistoryItemSpy ) . toHaveBeenCalledWith (
730+ expect . objectContaining ( {
731+ id : "child-1" ,
732+ status : "interrupted" ,
733+ parentTaskId : "parent-1" ,
734+ rootTaskId : "root-1" ,
735+ parentTask : expect . objectContaining ( { taskId : "parent-1" } ) ,
736+ rootTask : expect . objectContaining ( { taskId : "root-1" } ) ,
737+ } ) ,
738+ )
739+ expect ( provider [ "cancelledDelegationChildIds" ] . has ( "child-1" ) ) . toBe ( false )
740+ } ,
741+ )
742+
743+ it ( "uses the in-lock child status when another transition interrupted it" , async ( ) => {
744+ const activeChild : HistoryItem = {
745+ id : "child-race" ,
669746 number : 2 ,
670747 task : "child task" ,
671748 ts : Date . now ( ) ,
672749 tokensIn : 10 ,
673750 tokensOut : 20 ,
674751 totalCost : 0.001 ,
675752 workspace : "/test/workspace" ,
676- parentTaskId : "parent-1 " ,
677- rootTaskId : "root-1 " ,
678- status : "interrupted " ,
753+ parentTaskId : "parent-race " ,
754+ rootTaskId : "root-race " ,
755+ status : "active " ,
679756 }
757+ const interruptedChild : HistoryItem = { ...activeChild , status : "interrupted" }
680758 const parentHistory : HistoryItem = {
681- id : "parent-1 " ,
759+ id : "parent-race " ,
682760 number : 1 ,
683761 task : "parent task" ,
684762 ts : Date . now ( ) ,
@@ -687,16 +765,16 @@ describe("ClineProvider flicker-free cancel", () => {
687765 totalCost : 0.001 ,
688766 workspace : "/test/workspace" ,
689767 status : "delegated" ,
690- awaitingChildId : "child-1 " ,
691- delegatedToId : "child-1 " ,
768+ awaitingChildId : "child-race " ,
769+ delegatedToId : "child-race " ,
692770 }
693771
694772 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 " ,
773+ taskId : "child-race " ,
774+ instanceId : "instance-child-race " ,
775+ rootTask : { taskId : "root-race " } ,
776+ parentTask : { taskId : "parent-race " } ,
777+ parentTaskId : "parent-race " ,
700778 cancelCurrentRequest : vi . fn ( ) ,
701779 abortTask : vi . fn ( ) . mockResolvedValue ( undefined ) ,
702780 abandoned : false ,
@@ -705,36 +783,23 @@ describe("ClineProvider flicker-free cancel", () => {
705783 isWaitingForFirstChunk : false ,
706784 } )
707785 seedRegistry ( provider , mockTask1 )
786+ let childReads = 0
708787 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 } )
788+ if ( id === "child-race" ) {
789+ childReads += 1
790+ return Promise . resolve ( { historyItem : childReads === 1 ? activeChild : interruptedChild } )
791+ }
792+ if ( id === "parent-race" ) return Promise . resolve ( { historyItem : parentHistory } )
711793 throw new Error ( `unexpected task lookup: ${ id } ` )
712794 } ) as unknown as ClineProvider [ "getTaskWithId" ]
713795
714796 const updateTaskHistorySpy = vi . spyOn ( provider , "updateTaskHistory" ) . mockResolvedValue ( [ ] )
715- const createTaskWithHistoryItemSpy = vi
716- . spyOn ( provider , "createTaskWithHistoryItem" )
717- . mockResolvedValue ( undefined as unknown as CreatedHistoryTask )
718- provider [ "cancelledDelegationChildIds" ] . add ( "child-1" )
719- expect ( provider [ "cancelledDelegationChildIds" ] . has ( "child-1" ) ) . toBe ( true )
797+ vi . spyOn ( provider , "createTaskWithHistoryItem" ) . mockResolvedValue ( undefined as unknown as CreatedHistoryTask )
720798
721799 await provider . cancelTask ( )
722800
801+ expect ( childReads ) . toBe ( 2 )
723802 expect ( updateTaskHistorySpy ) . not . toHaveBeenCalled ( )
724- expect ( createTaskWithHistoryItemSpy ) . toHaveBeenCalledWith (
725- expect . objectContaining ( {
726- id : "child-1" ,
727- status : "interrupted" ,
728- parentTaskId : "parent-1" ,
729- rootTaskId : "root-1" ,
730- parentTask : expect . objectContaining ( { taskId : "parent-1" } ) ,
731- rootTask : expect . objectContaining ( { taskId : "root-1" } ) ,
732- } ) ,
733- )
734- expect ( provider [ "cancelledDelegationChildIds" ] . has ( "child-1" ) ) . toBe ( false )
735- expect ( mockOutputChannel . appendLine ) . not . toHaveBeenCalledWith (
736- expect . stringContaining ( "Invalid task status transition: interrupted → interrupted" ) ,
737- )
738803 } )
739804
740805 it ( "detaches runtime parent links when delegated parent detach fails" , async ( ) => {
0 commit comments