Skip to content

Commit f324d7d

Browse files
PierrunoYTclaude
andcommitted
fix(delegation): repair cancelled handoff when the committed child was deleted
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent b1c87fd commit f324d7d

2 files changed

Lines changed: 87 additions & 5 deletions

File tree

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,65 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
159159
} else expect(records.has("child-1")).toBe(false)
160160
})
161161

162+
it("releases the parent and rethrows the cancellation when the committed child was deleted", async () => {
163+
const parent = makeParentTask()
164+
const child = {
165+
taskId: "child-1",
166+
abort: false,
167+
abandoned: false,
168+
run: vi.fn(),
169+
dispose: vi.fn().mockResolvedValue(undefined),
170+
}
171+
let current: { taskId: string } | undefined = parent
172+
const records = new Map<string, HistoryItem>([["parent-1", { ...parentHistoryItem, status: "interrupted" }]])
173+
const store = {
174+
invalidate: vi.fn().mockResolvedValue(undefined),
175+
get: (id: string) => records.get(id),
176+
atomicReadAndUpdate: vi.fn(async (id: string, update: (item: HistoryItem) => HistoryItem) => {
177+
const item = records.get(id)
178+
if (!item) throw new Error(`missing ${id}`)
179+
records.set(id, update(item))
180+
if (id === "parent-1") child.abort = true
181+
return [...records.values()]
182+
}),
183+
}
184+
const provider = makeProviderStub({
185+
taskHistoryStore: store,
186+
taskScheduler: { schedule: vi.fn() },
187+
createTask: vi.fn(async () => {
188+
current = child
189+
records.set(child.taskId, {
190+
...parentHistoryItem,
191+
id: child.taskId,
192+
status: "active",
193+
parentTaskId: "parent-1",
194+
})
195+
return child
196+
}),
197+
createTaskWithHistoryItem: vi.fn(),
198+
getCurrentTask: () => current,
199+
isViewLaunched: true,
200+
emit: vi.fn(),
201+
postMessageToWebview: vi.fn(),
202+
deleteTaskWithId: vi.fn(),
203+
// A concurrent deleteTaskWithId removes the child while the handoff unwinds.
204+
removeClineFromStack: vi.fn(async () => {
205+
if (current === child) records.delete(child.taskId)
206+
current = undefined
207+
}),
208+
})
209+
210+
await expect(
211+
ClineProvider.prototype.delegateParentAndOpenChild.call(provider, {
212+
parentTaskId: "parent-1",
213+
message: "Child",
214+
initialTodos: [],
215+
mode: "code",
216+
}),
217+
).rejects.toThrow(/cancelled/)
218+
expect(records.get("parent-1")).toMatchObject({ status: "active", awaitingChildId: undefined })
219+
})
220+
162221
it("stops before delegation when refreshing the interrupted task's owner fails", async () => {
163222
const parentTask = makeParentTask()
164223
const historyItem: HistoryItem = {

‎src/core/webview/ClineProvider.ts‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4131,11 +4131,34 @@ export class ClineProvider
41314131
await ClineProvider.prototype.drainTaskDisposal.call(this, child)
41324132
const parentHistory = this.taskHistoryStore.get(parentTaskId)
41334133
if (parentHistory?.awaitingChildId === child.taskId) {
4134-
await this.taskHistoryStore.atomicReadAndUpdate(child.taskId, (historyItem) =>
4135-
historyItem.status === "active"
4136-
? interruptDelegatedChild(parentHistory, historyItem)
4137-
: historyItem,
4138-
)
4134+
try {
4135+
if (this.taskHistoryStore.get(child.taskId)) {
4136+
await this.taskHistoryStore.atomicReadAndUpdate(child.taskId, (historyItem) =>
4137+
historyItem.status === "active"
4138+
? interruptDelegatedChild(parentHistory, historyItem)
4139+
: historyItem,
4140+
)
4141+
} else {
4142+
// The child was deleted concurrently; release the parent through the abandon
4143+
// transition. Only id and status of the stand-in child are read.
4144+
await this.taskHistoryStore.atomicReadAndUpdate(parentTaskId, (current) =>
4145+
current.status === "delegated" && current.awaitingChildId === child.taskId
4146+
? abandonDelegatedChild(current, {
4147+
...current,
4148+
id: child.taskId,
4149+
status: "interrupted",
4150+
}).parent
4151+
: current,
4152+
)
4153+
}
4154+
} catch (cleanupError) {
4155+
// Never let a cleanup failure replace the cancellation error.
4156+
this.log(
4157+
`[delegateParentAndOpenChild] Failed to repair cancelled handoff ${parentTaskId} -> ${child.taskId}: ${
4158+
(cleanupError as Error)?.message ?? String(cleanupError)
4159+
}`,
4160+
)
4161+
}
41394162
}
41404163
throw err
41414164
}

0 commit comments

Comments
 (0)