@@ -54,6 +54,41 @@ const makeParentTask = () =>
5454 } ) as any
5555
5656describe ( "ClineProvider.delegateParentAndOpenChild()" , ( ) => {
57+ it ( "stops before delegation when refreshing the interrupted task's owner fails" , async ( ) => {
58+ const parentTask = makeParentTask ( )
59+ const historyItem : HistoryItem = {
60+ ...parentHistoryItem ,
61+ status : "interrupted" ,
62+ parentTaskId : "root" ,
63+ rootTaskId : "root" ,
64+ }
65+ const taskHistoryStore = makeStoreStub ( { get : vi . fn ( ) . mockReturnValue ( historyItem ) } )
66+ taskHistoryStore . invalidate . mockImplementation ( async ( id : string ) => {
67+ if ( id === "root" ) throw new Error ( "owner read failed" )
68+ } )
69+ const provider = {
70+ getCurrentTask : vi . fn ( ( ) => parentTask ) ,
71+ createTask : vi . fn ( ) ,
72+ removeClineFromStack : vi . fn ( ) ,
73+ taskHistoryStore,
74+ // Only the boundaries reached before delegation are needed for this failure.
75+ } as unknown as ClineProvider
76+
77+ await expect (
78+ ClineProvider . prototype . delegateParentAndOpenChild . call ( provider , {
79+ parentTaskId : "parent-1" ,
80+ message : "Do something" ,
81+ initialTodos : [ ] ,
82+ mode : "code" ,
83+ } ) ,
84+ ) . rejects . toThrow ( "owner read failed" )
85+ expect ( taskHistoryStore . invalidate ) . toHaveBeenNthCalledWith ( 2 , "root" )
86+ expect ( taskHistoryStore . atomicReadAndUpdate ) . not . toHaveBeenCalled ( )
87+ expect ( provider . createTask ) . not . toHaveBeenCalled ( )
88+ expect ( provider . removeClineFromStack ) . not . toHaveBeenCalled ( )
89+ expect ( historyItem ) . toMatchObject ( { status : "interrupted" , parentTaskId : "root" , rootTaskId : "root" } )
90+ } )
91+
5792 it ( "rejects a stale restored action before delegation side effects" , async ( ) => {
5893 const parentTask = makeParentTask ( )
5994 const removeClineFromStack = vi . fn ( )
@@ -727,7 +762,7 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
727762 expect ( durableParent . awaitingChildId ) . toBe ( "child-1" )
728763 } )
729764
730- it . each ( [ "legacy" , "pending" , "resolved" , "read-failure" , "write-failure" ] as const ) (
765+ it . each ( [ "legacy" , "pending" , "flushed" , " resolved", "read-failure" , "write-failure" ] as const ) (
731766 "rolls back without automatic action replay after a metadata failure (%s)" ,
732767 async ( scenario ) => {
733768 const persistError = new Error ( "parent metadata persist failed" )
@@ -753,6 +788,21 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
753788 content : [ { type : "tool_use" , id : "failed-action" , name : "new_task" , input : { } } ] ,
754789 } ,
755790 ]
791+ if ( scenario === "flushed" ) {
792+ durableMessages [ 0 ] . content = [
793+ { type : "tool_use" , id : "earlier-action" , name : "read_file" , input : { } } ,
794+ { type : "tool_use" , id : "failed-action" , name : "new_task" , input : { } } ,
795+ ]
796+ durableMessages . push ( {
797+ role : "user" ,
798+ ts : 123 ,
799+ messageId : "flushed-turn" ,
800+ content : [
801+ { type : "tool_result" , tool_use_id : "earlier-action" , content : "File contents" } ,
802+ { type : "text" , text : "Continue with the subtask" } ,
803+ ] ,
804+ } )
805+ }
756806 if ( scenario === "resolved" ) {
757807 durableMessages . push ( {
758808 role : "user" ,
@@ -784,7 +834,14 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
784834 // A rehydrated Task must find a durable result before it can replay the action.
785835 expect ( durableMessages . at ( - 1 ) ) . toMatchObject ( {
786836 role : "user" ,
787- content : [ { type : "tool_result" , tool_use_id : pendingAction . actionId , is_error : true } ] ,
837+ content : expect . arrayContaining ( [
838+ {
839+ type : "tool_result" ,
840+ tool_use_id : pendingAction . actionId ,
841+ is_error : true ,
842+ content : expect . any ( String ) ,
843+ } ,
844+ ] ) ,
788845 } )
789846 }
790847 } )
@@ -842,8 +899,26 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
842899 expect ( createTaskWithHistoryItem ) . toHaveBeenCalledExactlyOnceWith ( historyItem )
843900 }
844901 expect ( saveApiMessages ) . toHaveBeenCalledTimes (
845- scenario === "pending" || scenario === "write-failure" ? 1 : 0 ,
902+ scenario === "pending" || scenario === "flushed" || scenario === " write-failure" ? 1 : 0 ,
846903 )
904+ if ( scenario === "flushed" ) {
905+ expect ( durableMessages ) . toHaveLength ( 2 )
906+ expect ( durableMessages [ 1 ] ) . toEqual ( {
907+ role : "user" ,
908+ ts : 123 ,
909+ messageId : "flushed-turn" ,
910+ content : [
911+ { type : "tool_result" , tool_use_id : "earlier-action" , content : "File contents" } ,
912+ {
913+ type : "tool_result" ,
914+ tool_use_id : "failed-action" ,
915+ content : "Subtask creation failed: parent metadata persist failed" ,
916+ is_error : true ,
917+ } ,
918+ { type : "text" , text : "Continue with the subtask" } ,
919+ ] ,
920+ } )
921+ }
847922 if ( scenario === "pending" ) {
848923 expect ( durableMessages ) . toHaveLength ( 2 )
849924 expect ( durableMessages [ 1 ] ) . toMatchObject ( {
0 commit comments