@@ -218,6 +218,86 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
218218 expect ( records . get ( "parent-1" ) ) . toMatchObject ( { status : "active" , awaitingChildId : undefined } )
219219 } )
220220
221+ it . each ( [
222+ {
223+ name : "the child is deleted between the existence check and the update" ,
224+ deleteBeforeUpdate : true ,
225+ expected : { status : "active" , awaitingChildId : undefined } ,
226+ } ,
227+ {
228+ name : "the child update fails for a reason other than deletion" ,
229+ deleteBeforeUpdate : false ,
230+ expected : { status : "delegated" , awaitingChildId : "child-1" } ,
231+ } ,
232+ ] ) ( "when $name, still rethrows the cancellation" , async ( { deleteBeforeUpdate, expected } ) => {
233+ const parent = makeParentTask ( )
234+ const child = {
235+ taskId : "child-1" ,
236+ abort : false ,
237+ abandoned : false ,
238+ run : vi . fn ( ) ,
239+ dispose : vi . fn ( ) . mockResolvedValue ( undefined ) ,
240+ }
241+ let current : { taskId : string } | undefined = parent
242+ const records = new Map < string , HistoryItem > ( [ [ "parent-1" , { ...parentHistoryItem , status : "interrupted" } ] ] )
243+ const store = {
244+ invalidate : vi . fn ( ) . mockResolvedValue ( undefined ) ,
245+ get : ( id : string ) => records . get ( id ) ,
246+ atomicReadAndUpdate : vi . fn ( async ( id : string , update : ( item : HistoryItem ) => HistoryItem ) => {
247+ if ( id === "child-1" ) {
248+ // The child is still stored when checked, then fails to update.
249+ if ( deleteBeforeUpdate ) records . delete ( id )
250+ throw new Error ( `cannot update ${ id } ` )
251+ }
252+ const item = records . get ( id )
253+ if ( ! item ) throw new Error ( `missing ${ id } ` )
254+ records . set ( id , update ( item ) )
255+ if ( id === "parent-1" ) child . abort = true
256+ return [ ...records . values ( ) ]
257+ } ) ,
258+ }
259+ const log = vi . fn ( )
260+ const provider = makeProviderStub ( {
261+ taskHistoryStore : store ,
262+ taskScheduler : { schedule : vi . fn ( ) } ,
263+ createTask : vi . fn ( async ( ) => {
264+ current = child
265+ records . set ( child . taskId , {
266+ ...parentHistoryItem ,
267+ id : child . taskId ,
268+ status : "active" ,
269+ parentTaskId : "parent-1" ,
270+ } )
271+ return child
272+ } ) ,
273+ createTaskWithHistoryItem : vi . fn ( ) ,
274+ getCurrentTask : ( ) => current ,
275+ isViewLaunched : true ,
276+ emit : vi . fn ( ) ,
277+ postMessageToWebview : vi . fn ( ) ,
278+ deleteTaskWithId : vi . fn ( ) ,
279+ removeClineFromStack : vi . fn ( async ( ) => {
280+ current = undefined
281+ } ) ,
282+ log,
283+ } )
284+
285+ await expect (
286+ ClineProvider . prototype . delegateParentAndOpenChild . call ( provider , {
287+ parentTaskId : "parent-1" ,
288+ message : "Child" ,
289+ initialTodos : [ ] ,
290+ mode : "code" ,
291+ } ) ,
292+ ) . rejects . toThrow ( / c a n c e l l e d / )
293+
294+ expect ( records . get ( "parent-1" ) ) . toMatchObject ( expected )
295+ // A cleanup failure that is not a missing child is logged, never allowed to replace the error.
296+ if ( ! deleteBeforeUpdate ) {
297+ expect ( log ) . toHaveBeenCalledWith ( expect . stringContaining ( "Failed to repair cancelled handoff" ) )
298+ }
299+ } )
300+
221301 it ( "stops before delegation when refreshing the interrupted task's owner fails" , async ( ) => {
222302 const parentTask = makeParentTask ( )
223303 const historyItem : HistoryItem = {
0 commit comments