Skip to content

Commit 527097c

Browse files
PierrunoYTclaude
andcommitted
fix(delegation): repair the parent when the child is deleted during the cancelled-handoff update
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 487b3a0 commit 527097c

2 files changed

Lines changed: 105 additions & 16 deletions

File tree

‎src/__tests__/ClineProvider.delegation.spec.ts‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/cancelled/)
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 = {

‎src/core/webview/ClineProvider.ts‎

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4172,25 +4172,34 @@ export class ClineProvider
41724172
await ClineProvider.prototype.drainTaskDisposal.call(this, child)
41734173
const parentHistory = this.taskHistoryStore.get(parentTaskId)
41744174
if (parentHistory?.awaitingChildId === child.taskId) {
4175+
// The child was deleted concurrently; release the parent through the abandon
4176+
// transition. Only id and status of the stand-in child are read.
4177+
const releaseParentFromMissingChild = () =>
4178+
this.taskHistoryStore.atomicReadAndUpdate(parentTaskId, (current) =>
4179+
current.status === "delegated" && current.awaitingChildId === child.taskId
4180+
? abandonDelegatedChild(current, {
4181+
...current,
4182+
id: child.taskId,
4183+
status: "interrupted",
4184+
}).parent
4185+
: current,
4186+
)
41754187
try {
41764188
if (this.taskHistoryStore.get(child.taskId)) {
4177-
await this.taskHistoryStore.atomicReadAndUpdate(child.taskId, (historyItem) =>
4178-
historyItem.status === "active"
4179-
? interruptDelegatedChild(parentHistory, historyItem)
4180-
: historyItem,
4181-
)
4189+
try {
4190+
await this.taskHistoryStore.atomicReadAndUpdate(child.taskId, (historyItem) =>
4191+
historyItem.status === "active"
4192+
? interruptDelegatedChild(parentHistory, historyItem)
4193+
: historyItem,
4194+
)
4195+
} catch (updateError) {
4196+
// Deleted between the check above and the update: repair the parent instead.
4197+
// Any other failure is unrelated to a missing child and is reported below.
4198+
if (this.taskHistoryStore.get(child.taskId)) throw updateError
4199+
await releaseParentFromMissingChild()
4200+
}
41824201
} else {
4183-
// The child was deleted concurrently; release the parent through the abandon
4184-
// transition. Only id and status of the stand-in child are read.
4185-
await this.taskHistoryStore.atomicReadAndUpdate(parentTaskId, (current) =>
4186-
current.status === "delegated" && current.awaitingChildId === child.taskId
4187-
? abandonDelegatedChild(current, {
4188-
...current,
4189-
id: child.taskId,
4190-
status: "interrupted",
4191-
}).parent
4192-
: current,
4193-
)
4202+
await releaseParentFromMissingChild()
41944203
}
41954204
} catch (cleanupError) {
41964205
// Never let a cleanup failure replace the cancellation error.

0 commit comments

Comments
 (0)