Skip to content

Commit 19346d2

Browse files
committed
fix(delegation): broadcast atomic handoff history updates
1 parent 135011d commit 19346d2

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

‎src/__tests__/history-resume-delegation.spec.ts‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,132 @@ describe("History resume delegation - parent metadata transitions", () => {
941941
expect(calls).toEqual(["first", "second"])
942942
})
943943

944+
it("reopenParentFromDelegation posts taskHistoryItemUpdated for both records when view is launched", async () => {
945+
const childItem = { id: "c-webview", status: "active" }
946+
const parentItem = {
947+
id: "p-webview",
948+
status: "delegated",
949+
awaitingChildId: "c-webview",
950+
childIds: [],
951+
ts: 100,
952+
task: "Parent webview",
953+
tokensIn: 0,
954+
tokensOut: 0,
955+
totalCost: 0,
956+
}
957+
958+
// After atomicUpdatePair resolves, get() returns the merged committed items.
959+
const updatedChild = { ...childItem, status: "completed" }
960+
const updatedParent = {
961+
...parentItem,
962+
status: "active",
963+
awaitingChildId: undefined,
964+
completedByChildId: "c-webview",
965+
}
966+
const itemMap = new Map<string, any>([
967+
["c-webview", updatedChild],
968+
["p-webview", updatedParent],
969+
])
970+
const taskHistoryStore = {
971+
atomicUpdatePair: vi.fn(async (_fId: string, _sId: string, fU: (h: any) => any, sU: (h: any) => any) => {
972+
fU(childItem)
973+
sU(parentItem)
974+
return []
975+
}),
976+
get: vi.fn((id: string) => itemMap.get(id)),
977+
}
978+
979+
const postMessageToWebview = vi.fn().mockResolvedValue(undefined)
980+
981+
const provider = makeProviderStub({
982+
contextProxy: { globalStorageUri: { fsPath: "/tmp" } },
983+
getTaskWithId: vi.fn().mockResolvedValue({ historyItem: parentItem }),
984+
emit: vi.fn(),
985+
isViewLaunched: true,
986+
getCurrentTask: vi.fn(() => ({ taskId: "c-webview" })),
987+
removeClineFromStack: vi.fn().mockResolvedValue(undefined),
988+
createTaskWithHistoryItem: vi.fn().mockResolvedValue({
989+
resumeAfterDelegation: vi.fn().mockResolvedValue(undefined),
990+
overwriteClineMessages: vi.fn().mockResolvedValue(undefined),
991+
overwriteApiConversationHistory: vi.fn().mockResolvedValue(undefined),
992+
}),
993+
taskHistoryStore,
994+
postMessageToWebview,
995+
} as any)
996+
997+
vi.mocked(readTaskMessages).mockResolvedValue([])
998+
vi.mocked(readApiMessages).mockResolvedValue([])
999+
1000+
await (ClineProvider.prototype as any).reopenParentFromDelegation.call(provider, {
1001+
parentTaskId: "p-webview",
1002+
childTaskId: "c-webview",
1003+
completionResultSummary: "Done",
1004+
})
1005+
1006+
// Must post taskHistoryItemUpdated for both the child (completed) and parent (active)
1007+
const webviewCalls = postMessageToWebview.mock.calls.filter((c) => c[0]?.type === "taskHistoryItemUpdated")
1008+
expect(webviewCalls).toHaveLength(2)
1009+
1010+
const childMsg = webviewCalls.find((c) => c[0].taskHistoryItem.id === "c-webview")
1011+
expect(childMsg?.[0].taskHistoryItem).toMatchObject({ id: "c-webview", status: "completed" })
1012+
1013+
const parentMsg = webviewCalls.find((c) => c[0].taskHistoryItem.id === "p-webview")
1014+
expect(parentMsg?.[0].taskHistoryItem).toMatchObject({ id: "p-webview", status: "active" })
1015+
1016+
// Both must be sent after atomicUpdatePair (verified by call order)
1017+
const atomicOrder = taskHistoryStore.atomicUpdatePair.mock.invocationCallOrder[0]
1018+
for (const call of webviewCalls) {
1019+
const msgOrder =
1020+
postMessageToWebview.mock.invocationCallOrder[postMessageToWebview.mock.calls.indexOf(call)]
1021+
expect(atomicOrder).toBeLessThan(msgOrder)
1022+
}
1023+
})
1024+
1025+
it("reopenParentFromDelegation does NOT post taskHistoryItemUpdated when view is not launched", async () => {
1026+
const childItem = { id: "c-noview", status: "active" }
1027+
const parentItem = {
1028+
id: "p-noview",
1029+
status: "delegated",
1030+
awaitingChildId: "c-noview",
1031+
childIds: [],
1032+
ts: 100,
1033+
task: "Parent no-view",
1034+
tokensIn: 0,
1035+
tokensOut: 0,
1036+
totalCost: 0,
1037+
}
1038+
const taskHistoryStore = makeTaskHistoryStoreStub(childItem, parentItem)
1039+
const postMessageToWebview = vi.fn().mockResolvedValue(undefined)
1040+
1041+
const provider = makeProviderStub({
1042+
contextProxy: { globalStorageUri: { fsPath: "/tmp" } },
1043+
getTaskWithId: vi.fn().mockResolvedValue({ historyItem: parentItem }),
1044+
emit: vi.fn(),
1045+
isViewLaunched: false,
1046+
getCurrentTask: vi.fn(() => ({ taskId: "c-noview" })),
1047+
removeClineFromStack: vi.fn().mockResolvedValue(undefined),
1048+
createTaskWithHistoryItem: vi.fn().mockResolvedValue({
1049+
resumeAfterDelegation: vi.fn().mockResolvedValue(undefined),
1050+
overwriteClineMessages: vi.fn().mockResolvedValue(undefined),
1051+
overwriteApiConversationHistory: vi.fn().mockResolvedValue(undefined),
1052+
}),
1053+
taskHistoryStore,
1054+
postMessageToWebview,
1055+
} as any)
1056+
1057+
vi.mocked(readTaskMessages).mockResolvedValue([])
1058+
vi.mocked(readApiMessages).mockResolvedValue([])
1059+
1060+
await (ClineProvider.prototype as any).reopenParentFromDelegation.call(provider, {
1061+
parentTaskId: "p-noview",
1062+
childTaskId: "c-noview",
1063+
completionResultSummary: "Done",
1064+
})
1065+
1066+
const webviewCalls = postMessageToWebview.mock.calls.filter((c) => c[0]?.type === "taskHistoryItemUpdated")
1067+
expect(webviewCalls).toHaveLength(0)
1068+
})
1069+
9441070
describe("atomicUpdatePair handoff correctness", () => {
9451071
it("after reopenParentFromDelegation, child is completed and parent is active atomically", async () => {
9461072
const parentItem = {

‎src/core/webview/ClineProvider.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3781,6 +3781,18 @@ export class ClineProvider
37813781
)
37823782
this.recentTasksCache = undefined
37833783

3784+
// Notify the webview of both updated items so its in-memory history stays current.
3785+
if (this.isViewLaunched) {
3786+
const updatedChild = this.taskHistoryStore.get(childTaskId)
3787+
const updatedParent = this.taskHistoryStore.get(parentTaskId)
3788+
if (updatedChild) {
3789+
await this.postMessageToWebview({ type: "taskHistoryItemUpdated", taskHistoryItem: updatedChild })
3790+
}
3791+
if (updatedParent) {
3792+
await this.postMessageToWebview({ type: "taskHistoryItemUpdated", taskHistoryItem: updatedParent })
3793+
}
3794+
}
3795+
37843796
// 6) Emit TaskDelegationCompleted (provider-level)
37853797
try {
37863798
this.emit(RooCodeEventName.TaskDelegationCompleted, parentTaskId, childTaskId, completionResultSummary)

0 commit comments

Comments
 (0)