Skip to content

Commit 77a7302

Browse files
committed
test(task): cover delegation recovery branches
1 parent a9e75ce commit 77a7302

3 files changed

Lines changed: 83 additions & 4 deletions

File tree

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,74 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
673673
expect(provider.createTask).not.toHaveBeenCalled()
674674
})
675675

676+
it("detects a live delegation owner in another provider instance", () => {
677+
const provider = {
678+
taskRegistry: { hasRunning: vi.fn().mockReturnValue(false) },
679+
} as unknown as ClineProvider
680+
const otherProvider = {
681+
taskRegistry: { hasRunning: vi.fn((taskId: string) => taskId === "live-child") },
682+
} as unknown as ClineProvider
683+
const activeInstances = Reflect.get(ClineProvider, "activeInstances")
684+
if (!(activeInstances instanceof Set)) throw new Error("ClineProvider active instance registry unavailable")
685+
activeInstances.add(otherProvider)
686+
687+
try {
688+
expect(ClineProvider.prototype["isTaskRunningInAnyProvider"].call(provider, "live-child")).toBe(true)
689+
} finally {
690+
activeInstances.delete(otherProvider)
691+
}
692+
})
693+
694+
it("rejects when a dead delegation chain becomes live during its atomic recovery", async () => {
695+
const oldChildId = "old-child"
696+
const grandchildId = "grandchild"
697+
const parent = {
698+
...parentHistoryItem,
699+
status: "delegated" as const,
700+
awaitingChildId: oldChildId,
701+
delegatedToId: oldChildId,
702+
}
703+
const child = {
704+
...parentHistoryItem,
705+
id: oldChildId,
706+
status: "delegated" as const,
707+
awaitingChildId: grandchildId,
708+
delegatedToId: grandchildId,
709+
}
710+
const grandchild = { ...parentHistoryItem, id: grandchildId, status: "interrupted" as const }
711+
let childIsLive = false
712+
const taskHistoryStore = makeStoreStub({
713+
get: vi.fn((id: string) => (id === "parent-1" ? parent : id === oldChildId ? child : grandchild)),
714+
atomicReadAndUpdate: vi.fn(async (_taskId: string, updater: (item: HistoryItem) => HistoryItem) => {
715+
childIsLive = true
716+
updater(child)
717+
return []
718+
}),
719+
})
720+
const provider = {
721+
taskRegistry: { hasRunning: vi.fn((id: string) => id === oldChildId && childIsLive) },
722+
getCurrentTask: vi.fn(() => makeParentTask()),
723+
removeClineFromStack: vi.fn(),
724+
createTask: vi.fn(),
725+
taskHistoryStore,
726+
} as unknown as ClineProvider
727+
Object.assign(provider, {
728+
isTaskRunningInAnyProvider: ClineProvider.prototype["isTaskRunningInAnyProvider"],
729+
refreshDelegationChain: ClineProvider.prototype["refreshDelegationChain"],
730+
recoverDeadAwaitedChild: ClineProvider.prototype["recoverDeadAwaitedChild"],
731+
})
732+
733+
await expect(
734+
ClineProvider.prototype.delegateParentAndOpenChild.call(provider, {
735+
parentTaskId: "parent-1",
736+
message: "Continue",
737+
initialTodos: [],
738+
mode: "code",
739+
}),
740+
).rejects.toThrow(`Delegation chain for child ${oldChildId} became live during recovery`)
741+
expect(provider.createTask).not.toHaveBeenCalled()
742+
})
743+
676744
it("rejects with 'Cannot re-delegate' when the existing awaited child is still active", async () => {
677745
const oldChildId = "old-child"
678746
const activeChild = { id: oldChildId, status: "active" } as unknown as HistoryItem

‎src/core/task-persistence/__tests__/taskLifecycle.spec.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ describe("task lifecycle transitions", () => {
6868

6969
it("recognizes only delegated chains that terminate without a live owner", () => {
7070
const interrupted = item("grandchild", { status: "interrupted" })
71+
const completed = item("completed-grandchild", { status: "completed" })
72+
const active = item("active-grandchild")
7173
const child = item("child", { status: "delegated", awaitingChildId: interrupted.id })
72-
const tasks = new Map([interrupted].map((task) => [task.id, task]))
74+
const tasks = new Map([interrupted, completed, active].map((task) => [task.id, task]))
7375

7476
expect(isDeadDelegationChain(child, (id) => tasks.get(id))).toBe(true)
77+
expect(isDeadDelegationChain({ ...child, awaitingChildId: completed.id }, (id) => tasks.get(id))).toBe(true)
78+
expect(isDeadDelegationChain({ ...child, awaitingChildId: active.id }, (id) => tasks.get(id))).toBe(false)
79+
expect(isDeadDelegationChain({ ...child, awaitingChildId: undefined }, (id) => tasks.get(id))).toBe(true)
7580
expect(
7681
isDeadDelegationChain(
7782
child,
@@ -81,6 +86,11 @@ describe("task lifecycle transitions", () => {
8186
).toBe(false)
8287
expect(isDeadDelegationChain({ ...child, status: "active" }, (id) => tasks.get(id))).toBe(false)
8388
expect(isDeadDelegationChain({ ...child, awaitingChildId: "missing" }, (id) => tasks.get(id))).toBe(true)
89+
expect(
90+
isDeadDelegationChain({ ...child, awaitingChildId: child.id }, (id) =>
91+
id === child.id ? child : undefined,
92+
),
93+
).toBe(false)
8494
})
8595

8696
it("recovers a dead delegated child without releasing its parent's ownership", () => {
@@ -101,6 +111,9 @@ describe("task lifecycle transitions", () => {
101111
})
102112
expect(parent).toMatchObject({ status: "delegated", awaitingChildId: "child" })
103113
expect(() => recoverDeadDelegatedChild(parent, { ...child, status: "active" })).toThrow(/status active/)
114+
expect(() => recoverDeadDelegatedChild({ ...parent, awaitingChildId: "other-child" }, child)).toThrow(
115+
/not delegated to child/,
116+
)
104117
})
105118

106119
it("completes only the child the parent still awaits", () => {

‎src/core/task-persistence/taskLifecycle.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function isDeadDelegationChain(
7272

7373
const visited = new Set<string>()
7474
let current: HistoryItem | undefined = child
75-
while (current) {
75+
while (true) {
7676
if (visited.has(current.id) || isTaskLive(current.id)) return false
7777
visited.add(current.id)
7878
if (current.status === "interrupted" || current.status === "completed") return true
@@ -81,8 +81,6 @@ export function isDeadDelegationChain(
8181
current = getTask(current.awaitingChildId)
8282
if (!current) return true
8383
}
84-
85-
return true
8684
}
8785

8886
/**

0 commit comments

Comments
 (0)