Skip to content

Commit cb3b675

Browse files
committed
fix(task): preserve subtask links after repeated Stop
1 parent a0f2e03 commit cb3b675

2 files changed

Lines changed: 86 additions & 5 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3639,14 +3639,23 @@ export class ClineProvider
36393639
if (parentHistory?.status === "delegated" && parentHistory?.awaitingChildId === task.taskId) {
36403640
// Mark the child interrupted and leave parent delegated with awaitingChildId
36413641
// intact — the user can resume this child later and it will report back.
3642-
historyItem = interruptDelegatedChild(parentHistory, historyItem!)
3643-
await this.updateTaskHistory(historyItem)
3642+
// A previous cancellation may already have persisted the interrupted status
3643+
// before its caller lost the response. Treat that replay as success without
3644+
// weakening the lifecycle state machine's self-loop rejection.
3645+
if (historyItem!.status !== "interrupted") {
3646+
historyItem = interruptDelegatedChild(parentHistory, historyItem!)
3647+
await this.updateTaskHistory(historyItem)
3648+
this.log(
3649+
`[cancelTask] Marked child ${task.taskId} interrupted; parent ${task.parentTaskId} stays delegated`,
3650+
)
3651+
} else {
3652+
this.log(
3653+
`[cancelTask] Child ${task.taskId} is already interrupted; parent ${task.parentTaskId} stays delegated`,
3654+
)
3655+
}
36443656
// Clear any stale fail-closed entry from a prior failed cancel attempt so
36453657
// reopenParentFromDelegation is not incorrectly blocked on resume.
36463658
this.cancelledDelegationChildIds.delete(task.taskId)
3647-
this.log(
3648-
`[cancelTask] Marked child ${task.taskId} interrupted; parent ${task.parentTaskId} stays delegated`,
3649-
)
36503659
}
36513660
})
36523661
} catch (error) {

‎src/core/webview/__tests__/ClineProvider.flicker-free-cancel.spec.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,78 @@ 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",
669+
number: 2,
670+
task: "child task",
671+
ts: Date.now(),
672+
tokensIn: 10,
673+
tokensOut: 20,
674+
totalCost: 0.001,
675+
workspace: "/test/workspace",
676+
parentTaskId: "parent-1",
677+
rootTaskId: "root-1",
678+
status: "interrupted",
679+
}
680+
const parentHistory: HistoryItem = {
681+
id: "parent-1",
682+
number: 1,
683+
task: "parent task",
684+
ts: Date.now(),
685+
tokensIn: 10,
686+
tokensOut: 20,
687+
totalCost: 0.001,
688+
workspace: "/test/workspace",
689+
status: "delegated",
690+
awaitingChildId: "child-1",
691+
delegatedToId: "child-1",
692+
}
693+
694+
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",
700+
cancelCurrentRequest: vi.fn(),
701+
abortTask: vi.fn().mockResolvedValue(undefined),
702+
abandoned: false,
703+
isStreaming: false,
704+
didFinishAbortingStream: true,
705+
isWaitingForFirstChunk: false,
706+
})
707+
seedRegistry(provider, mockTask1)
708+
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 })
711+
throw new Error(`unexpected task lookup: ${id}`)
712+
}) as unknown as ClineProvider["getTaskWithId"]
713+
714+
const updateTaskHistorySpy = vi.spyOn(provider, "updateTaskHistory").mockResolvedValue([])
715+
const createTaskWithHistoryItemSpy = vi
716+
.spyOn(provider, "createTaskWithHistoryItem")
717+
.mockResolvedValue(undefined as unknown as CreatedHistoryTask)
718+
719+
await provider.cancelTask()
720+
721+
expect(updateTaskHistorySpy).not.toHaveBeenCalled()
722+
expect(createTaskWithHistoryItemSpy).toHaveBeenCalledWith(
723+
expect.objectContaining({
724+
id: "child-1",
725+
status: "interrupted",
726+
parentTaskId: "parent-1",
727+
rootTaskId: "root-1",
728+
parentTask: expect.objectContaining({ taskId: "parent-1" }),
729+
rootTask: expect.objectContaining({ taskId: "root-1" }),
730+
}),
731+
)
732+
expect(provider["cancelledDelegationChildIds"].has("child-1")).toBe(false)
733+
expect(mockOutputChannel.appendLine).not.toHaveBeenCalledWith(
734+
expect.stringContaining("Invalid task status transition: interrupted → interrupted"),
735+
)
736+
})
737+
666738
it("detaches runtime parent links when delegated parent detach fails", async () => {
667739
const mockRootTask = { taskId: "root-1" }
668740
const mockParentTask = { taskId: "parent-1" }

0 commit comments

Comments
 (0)