Skip to content

Commit 21d35c4

Browse files
fix(task-history): atomic per-task merge and drop shared index file (#1231) (#1261)
* fix(task-history): prevent concurrent index clobbering * test(task-history): add regression coverage for self-healing index recovery * fix(task-history): atomic read-modify-write prevents cross-process lost updates * fix(task-history): atomic read-modify-write prevents cross-process lost updates * fix(task-history): drop _index.json, scan task dirs on read * refactor(task-history): extract computeDelta and mergeWithDisk helpers * fix(safeWriteJson): propagate non-ENOENT read errors during merge * fix(task-history): harden cross-process merge and reconciliation * fix(task-history): throw DeltaRejectedError on invalid merge and harden edge cases --------- Co-authored-by: Naved Merchant <[email protected]>
1 parent 63aca68 commit 21d35c4

10 files changed

Lines changed: 418 additions & 257 deletions

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

Lines changed: 145 additions & 166 deletions
Large diffs are not rendered by default.

‎src/core/task-persistence/__tests__/TaskHistoryStore.crossInstance.spec.ts‎

Lines changed: 148 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as os from "os"
66

77
import type { HistoryItem } from "@roo-code/types"
88

9-
import { TaskHistoryStore } from "../TaskHistoryStore"
9+
import { TaskHistoryStore, DeltaRejectedError } from "../TaskHistoryStore"
1010
import { GlobalFileNames } from "../../../shared/globalFileNames"
1111

1212
vi.mock("../../../utils/storage", () => ({
@@ -15,12 +15,30 @@ vi.mock("../../../utils/storage", () => ({
1515
}),
1616
}))
1717

18-
// Mock safeWriteJson to use plain fs writes in tests (avoids proper-lockfile issues)
18+
// Mock safeWriteJson to use plain fs writes but honor the merge callback.
1919
vi.mock("../../../utils/safeWriteJson", () => ({
20-
safeWriteJson: vi.fn().mockImplementation(async (filePath: string, data: any) => {
21-
await fs.mkdir(path.dirname(filePath), { recursive: true })
22-
await fs.writeFile(filePath, JSON.stringify(data, null, "\t"), "utf8")
23-
}),
20+
safeWriteJson: vi
21+
.fn()
22+
.mockImplementation(
23+
async (
24+
filePath: string,
25+
data: unknown,
26+
options?: { merge?: (existing: unknown, incoming: unknown) => unknown },
27+
) => {
28+
await fs.mkdir(path.dirname(filePath), { recursive: true })
29+
if (options?.merge) {
30+
let existing: unknown = null
31+
try {
32+
const raw = await fs.readFile(filePath, "utf8")
33+
existing = JSON.parse(raw)
34+
} catch {
35+
// File does not exist or is corrupt
36+
}
37+
data = options.merge(existing, data)
38+
}
39+
await fs.writeFile(filePath, JSON.stringify(data, null, "\t"), "utf8")
40+
},
41+
),
2442
}))
2543

2644
function makeHistoryItem(overrides: Partial<HistoryItem> = {}): HistoryItem {
@@ -124,6 +142,27 @@ describe("TaskHistoryStore cross-instance safety", () => {
124142
expect(storeB.get("shared-task")).toBeUndefined()
125143
})
126144

145+
it("delete by instance A is detected even when the task directory remains", async () => {
146+
await storeA.initialize()
147+
await storeB.initialize()
148+
149+
const item = makeHistoryItem({ id: "file-only-delete" })
150+
await storeA.upsert(item)
151+
await storeB.reconcile()
152+
153+
expect(storeB.get("file-only-delete")).toBeDefined()
154+
155+
// delete() unlinks history_item.json but leaves the task directory.
156+
await storeA.delete("file-only-delete")
157+
158+
// Directory still exists (other files like ui_messages.json may remain).
159+
const taskDir = path.join(tmpDir, "tasks", "file-only-delete")
160+
await expect(fs.access(taskDir)).resolves.toBeUndefined()
161+
162+
await storeB.reconcile()
163+
expect(storeB.get("file-only-delete")).toBeUndefined()
164+
})
165+
127166
it("per-task file updates by one instance are visible to another after invalidation", async () => {
128167
await storeA.initialize()
129168
await storeB.initialize()
@@ -164,4 +203,107 @@ describe("TaskHistoryStore cross-instance safety", () => {
164203
expect(storeA.getAll().length).toBe(10)
165204
expect(storeB.getAll().length).toBe(10)
166205
})
206+
207+
/**
208+
* Host B completes a task on disk while host A's cache still has it
209+
* active. Host A's next save updates only totalCost (a full-object
210+
* upsert — the realistic production shape). The diff-delta merge
211+
* preserves B's status because status did not change in A's cache.
212+
*/
213+
it("per-task diff-delta preserves a peer's status change on full-object upsert", async () => {
214+
await storeA.initialize()
215+
216+
// Base item with an explicit status — mirrors real production items.
217+
const base = makeHistoryItem({ id: "shared-task", status: "active", totalCost: 0.01, ts: 1000 })
218+
await storeA.upsert(base)
219+
220+
// Host B completes the task on disk; A's cache still has "active".
221+
const filePath = path.join(tmpDir, "tasks", "shared-task", GlobalFileNames.historyItem)
222+
const onDisk = JSON.parse(await fs.readFile(filePath, "utf8"))
223+
onDisk.status = "completed"
224+
onDisk.completionResultSummary = "done by host B"
225+
await fs.writeFile(filePath, JSON.stringify(onDisk), "utf8")
226+
227+
// Host A does a full-object upsert (the realistic path — spread the
228+
// cached item and change one field). The cached item has status: "active".
229+
await storeA.upsert({ ...storeA.get("shared-task")!, totalCost: 9.99 })
230+
231+
const final = JSON.parse(await fs.readFile(filePath, "utf8")) as HistoryItem
232+
expect(final.totalCost).toBe(9.99)
233+
// Status is preserved from disk because A's delta does not include
234+
// status — it was unchanged relative to A's cache.
235+
expect(final.status).toBe("completed")
236+
expect(final.completionResultSummary).toBe("done by host B")
237+
238+
// Cache reflects the caller's totalCost change and the peer's status.
239+
expect(storeA.get("shared-task")!.totalCost).toBe(9.99)
240+
expect(storeA.get("shared-task")!.status).toBe("completed")
241+
expect(storeA.get("shared-task")!.completionResultSummary).toBe("done by host B")
242+
})
243+
244+
/**
245+
* Regression: a stale host whose cache says "active" tries to write
246+
* status: "delegated" after a peer already wrote "completed" to disk.
247+
* The merge must reject the entire delta (including companion fields)
248+
* to prevent an internally-inconsistent record.
249+
*/
250+
it("merge rejects an invalid status transition against disk and throws DeltaRejectedError", async () => {
251+
await storeA.initialize()
252+
253+
const base = makeHistoryItem({ id: "guarded-task", status: "active", totalCost: 0.01, ts: 1000 })
254+
await storeA.upsert(base)
255+
256+
// Peer writes terminal "completed" directly to disk.
257+
const filePath = path.join(tmpDir, "tasks", "guarded-task", GlobalFileNames.historyItem)
258+
const onDisk = JSON.parse(await fs.readFile(filePath, "utf8"))
259+
onDisk.status = "completed"
260+
onDisk.completionResultSummary = "done by peer"
261+
await fs.writeFile(filePath, JSON.stringify(onDisk), "utf8")
262+
263+
// Host A's cache still has "active". It tries to delegate (active → delegated
264+
// passes the cache check, but completed → delegated is invalid on disk).
265+
const staleItem = storeA.get("guarded-task")!
266+
await expect(
267+
storeA.upsert({
268+
...staleItem,
269+
status: "delegated",
270+
awaitingChildId: "child-99",
271+
delegatedToId: "child-99",
272+
}),
273+
).rejects.toThrow(DeltaRejectedError)
274+
275+
const final = JSON.parse(await fs.readFile(filePath, "utf8")) as HistoryItem
276+
// Terminal status must survive — disk is untouched.
277+
expect(final.status).toBe("completed")
278+
expect(final.completionResultSummary).toBe("done by peer")
279+
// Companion fields from the rejected delta must NOT be applied.
280+
expect(final.awaitingChildId).toBeUndefined()
281+
expect(final.delegatedToId).toBeUndefined()
282+
283+
// Cache must reflect the disk state, not the stale delta.
284+
expect(storeA.get("guarded-task")!.status).toBe("completed")
285+
})
286+
287+
/**
288+
* When both hosts change the same field, the last writer wins.
289+
* This is expected — true conflict resolution requires application
290+
* semantics that a generic merge cannot provide.
291+
*/
292+
it("same-field changes from both hosts are last-writer-wins", async () => {
293+
await storeA.initialize()
294+
await storeB.initialize()
295+
296+
const base = makeHistoryItem({ id: "shared-task", status: "active", totalCost: 0.01, ts: 1000 })
297+
await storeA.upsert(base)
298+
await storeB.reconcile()
299+
300+
// Both hosts change totalCost.
301+
await storeA.upsert({ ...storeA.get("shared-task")!, totalCost: 1.0 })
302+
await storeB.upsert({ ...storeB.get("shared-task")!, totalCost: 2.0 })
303+
304+
const filePath = path.join(tmpDir, "tasks", "shared-task", GlobalFileNames.historyItem)
305+
const final = JSON.parse(await fs.readFile(filePath, "utf8")) as HistoryItem
306+
// B wrote last, so B's value wins.
307+
expect(final.totalCost).toBe(2.0)
308+
})
167309
})

‎src/core/task-persistence/__tests__/TaskHistoryStore.reconciliation.spec.ts‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ describe("TaskHistoryStore reconcileDelegationState", () => {
465465
await expect(fs.access(intentPath)).rejects.toThrow()
466466
})
467467

468-
it("does not schedule the derived index before repair-intent cleanup succeeds", async () => {
468+
it("removes the repair-intent file after successful replay", async () => {
469469
const child = makeItem({ id: "child-deferred-index", status: "active", parentTaskId: "parent-deferred-index" })
470470
const parent = makeItem({
471471
id: "parent-deferred-index",
@@ -478,23 +478,12 @@ describe("TaskHistoryStore reconcileDelegationState", () => {
478478
await fs.writeFile(intentPath, JSON.stringify(makeRepairIntent(parent, child)))
479479
await store.reconcile({ forceRefresh: true })
480480

481-
const events: string[] = []
482481
const storeInternals = store as unknown as {
483-
scheduleIndexWrite: () => void
484-
removeDelegationRepairIntent: () => Promise<void>
485482
replayDelegationRepairIntent: () => Promise<void>
486483
}
487-
vi.spyOn(storeInternals, "removeDelegationRepairIntent").mockImplementation(async () => {
488-
events.push("cleanup")
489-
await fs.unlink(intentPath)
490-
})
491-
vi.spyOn(storeInternals, "scheduleIndexWrite").mockImplementation(() => {
492-
events.push("schedule")
493-
})
494484

495485
await storeInternals.replayDelegationRepairIntent()
496486

497-
expect(events).toEqual(["cleanup", "schedule"])
498487
await expect(fs.access(intentPath)).rejects.toThrow()
499488
})
500489

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

Lines changed: 16 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,19 @@ describe("TaskHistoryStore", () => {
5757
expect(store.getAll()).toEqual([])
5858
})
5959

60-
it("initializes from existing index file", async () => {
60+
it("initializes from existing per-task files", async () => {
6161
const tasksDir = path.join(tmpDir, "tasks")
6262
await fs.mkdir(tasksDir, { recursive: true })
6363

6464
const item1 = makeHistoryItem({ id: "task-1", ts: 1000 })
6565
const item2 = makeHistoryItem({ id: "task-2", ts: 2000 })
6666

67-
// Create task directories so reconciliation doesn't remove them
6867
await fs.mkdir(path.join(tasksDir, "task-1"), { recursive: true })
6968
await fs.mkdir(path.join(tasksDir, "task-2"), { recursive: true })
7069

71-
// Write per-task files
7270
await fs.writeFile(path.join(tasksDir, "task-1", GlobalFileNames.historyItem), JSON.stringify(item1))
7371
await fs.writeFile(path.join(tasksDir, "task-2", GlobalFileNames.historyItem), JSON.stringify(item2))
7472

75-
// Write index
76-
const index = {
77-
version: 1,
78-
updatedAt: Date.now(),
79-
entries: [item1, item2],
80-
}
81-
await fs.writeFile(path.join(tasksDir, GlobalFileNames.historyIndex), JSON.stringify(index))
82-
8373
await store.initialize()
8474

8575
expect(store.getAll()).toHaveLength(2)
@@ -374,7 +364,7 @@ describe("TaskHistoryStore", () => {
374364
expect(store.get("idem-task")).toBeDefined()
375365
})
376366

377-
it("serializes migration cache and index updates behind the store lock", async () => {
367+
it("serializes migration cache updates behind the store lock", async () => {
378368
const tasksDir = path.join(tmpDir, "tasks")
379369
const migrated = makeHistoryItem({ id: "migration-locked" })
380370
const concurrent = makeHistoryItem({ id: "migration-concurrent" })
@@ -389,12 +379,17 @@ describe("TaskHistoryStore", () => {
389379
const migrationWriteStarted = new Promise<void>((resolve) => {
390380
signalMigrationWriteStarted = resolve
391381
})
392-
const storeInternals = store as unknown as { writeIndex: () => Promise<void> }
393-
const originalWriteIndex = storeInternals.writeIndex.bind(store)
394-
vi.spyOn(storeInternals, "writeIndex").mockImplementation(async () => {
395-
signalMigrationWriteStarted()
396-
await migrationWriteCanFinish
397-
return originalWriteIndex()
382+
383+
const { safeWriteJson: mockSafeWriteJson } = await import("../../../utils/safeWriteJson")
384+
const originalImpl = vi.mocked(mockSafeWriteJson).getMockImplementation()!
385+
let firstCall = true
386+
vi.mocked(mockSafeWriteJson).mockImplementation(async (...args) => {
387+
if (firstCall) {
388+
firstCall = false
389+
signalMigrationWriteStarted()
390+
await migrationWriteCanFinish
391+
}
392+
return originalImpl(...args)
398393
})
399394

400395
const migration = store.migrateFromGlobalState([migrated])
@@ -407,45 +402,6 @@ describe("TaskHistoryStore", () => {
407402

408403
expect(store.get(migrated.id)).toEqual(migrated)
409404
expect(store.get(concurrent.id)).toEqual(concurrent)
410-
await store.flushIndex()
411-
const index = JSON.parse(await fs.readFile(path.join(tasksDir, GlobalFileNames.historyIndex), "utf8")) as {
412-
entries: HistoryItem[]
413-
}
414-
expect(index.entries.map((entry) => entry.id)).toEqual(expect.arrayContaining([migrated.id, concurrent.id]))
415-
})
416-
})
417-
418-
describe("flushIndex()", () => {
419-
it("writes index to disk on flush", async () => {
420-
await store.initialize()
421-
422-
await store.upsert(makeHistoryItem({ id: "flush-task" }))
423-
await store.flushIndex()
424-
425-
const indexPath = path.join(tmpDir, "tasks", GlobalFileNames.historyIndex)
426-
const raw = await fs.readFile(indexPath, "utf8")
427-
const index = JSON.parse(raw)
428-
429-
expect(index.version).toBe(1)
430-
expect(index.entries).toHaveLength(1)
431-
expect(index.entries[0].id).toBe("flush-task")
432-
})
433-
})
434-
435-
describe("dispose()", () => {
436-
it("flushes index on dispose", async () => {
437-
await store.initialize()
438-
439-
await store.upsert(makeHistoryItem({ id: "dispose-task" }))
440-
store.dispose()
441-
442-
// Give the flush a moment to complete
443-
await new Promise((resolve) => setTimeout(resolve, 100))
444-
445-
const indexPath = path.join(tmpDir, "tasks", GlobalFileNames.historyIndex)
446-
const raw = await fs.readFile(indexPath, "utf8")
447-
const index = JSON.parse(raw)
448-
expect(index.entries).toHaveLength(1)
449405
})
450406
})
451407

@@ -748,8 +704,9 @@ describe("TaskHistoryStore", () => {
748704
const parentDisk = JSON.parse(await fs.readFile(parentFile, "utf8"))
749705
expect(parentDisk.status).toBe("delegated")
750706

751-
// Cache was NOT updated (cache set is deferred until after both writes succeed)
752-
expect(store.get("child-partial")?.status).toBe("active")
707+
// First record's cache IS updated (it was committed to disk).
708+
// Second record's cache is unchanged (write never completed).
709+
expect(store.get("child-partial")?.status).toBe("completed")
753710
expect(store.get("parent-partial")?.status).toBe("delegated")
754711
})
755712

‎src/core/webview/__tests__/webviewMessageHandler.importRooHistory.spec.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ describe("webviewMessageHandler - importRooHistory", () => {
5252
taskHistoryStore: {
5353
invalidateAll: ReturnType<typeof vi.fn>
5454
reconcile: ReturnType<typeof vi.fn>
55-
flushIndex: ReturnType<typeof vi.fn>
5655
}
5756
postMessageToWebview: ReturnType<typeof vi.fn>
5857
postStateToWebview: ReturnType<typeof vi.fn>
@@ -71,7 +70,6 @@ describe("webviewMessageHandler - importRooHistory", () => {
7170
taskHistoryStore: {
7271
invalidateAll: vi.fn(),
7372
reconcile: vi.fn().mockResolvedValue(undefined),
74-
flushIndex: vi.fn().mockResolvedValue(undefined),
7573
},
7674
postMessageToWebview: vi.fn().mockResolvedValue(undefined),
7775
postStateToWebview: vi.fn().mockResolvedValue(undefined),
@@ -106,7 +104,7 @@ describe("webviewMessageHandler - importRooHistory", () => {
106104
expect(importRooTaskHistoryMock).toHaveBeenCalledWith("/mock/storage", expect.any(Function))
107105
expect(mockProvider.taskHistoryStore.invalidateAll).toHaveBeenCalledTimes(1)
108106
expect(mockProvider.taskHistoryStore.reconcile).toHaveBeenCalledTimes(1)
109-
expect(mockProvider.taskHistoryStore.flushIndex).toHaveBeenCalledTimes(1)
107+
110108
expect(mockProvider.postStateToWebview).toHaveBeenCalledTimes(1)
111109
expect(mockProvider.postMessageToWebview).toHaveBeenNthCalledWith(1, {
112110
type: "rooHistoryImportProgress",
@@ -189,7 +187,7 @@ describe("webviewMessageHandler - importRooHistory", () => {
189187
expect(importRooTaskHistoryMock).toHaveBeenCalledWith("/mock/storage", expect.any(Function))
190188
expect(mockProvider.taskHistoryStore.invalidateAll).not.toHaveBeenCalled()
191189
expect(mockProvider.taskHistoryStore.reconcile).not.toHaveBeenCalled()
192-
expect(mockProvider.taskHistoryStore.flushIndex).not.toHaveBeenCalled()
190+
193191
expect(mockProvider.postStateToWebview).not.toHaveBeenCalled()
194192
expect(mockProvider.postMessageToWebview).toHaveBeenNthCalledWith(2, {
195193
type: "rooHistoryImportProgress",
@@ -222,7 +220,7 @@ describe("webviewMessageHandler - importRooHistory", () => {
222220
// after a partial-copy failure still reconciles the store.
223221
expect(mockProvider.taskHistoryStore.invalidateAll).toHaveBeenCalledTimes(1)
224222
expect(mockProvider.taskHistoryStore.reconcile).toHaveBeenCalledTimes(1)
225-
expect(mockProvider.taskHistoryStore.flushIndex).toHaveBeenCalledTimes(1)
223+
226224
expect(mockProvider.postStateToWebview).toHaveBeenCalledTimes(1)
227225
expect(vscode.window.showWarningMessage).toHaveBeenCalledWith(
228226
"common:warnings.rooHistoryImport.alreadyImported",
@@ -237,7 +235,7 @@ describe("webviewMessageHandler - importRooHistory", () => {
237235

238236
expect(mockProvider.taskHistoryStore.invalidateAll).not.toHaveBeenCalled()
239237
expect(mockProvider.taskHistoryStore.reconcile).not.toHaveBeenCalled()
240-
expect(mockProvider.taskHistoryStore.flushIndex).not.toHaveBeenCalled()
238+
241239
expect(mockProvider.postStateToWebview).not.toHaveBeenCalled()
242240
expect(mockProvider.log).toHaveBeenCalledWith("[importRooHistory] failed: permission denied")
243241
expect(mockProvider.postMessageToWebview).toHaveBeenNthCalledWith(2, {

‎src/core/webview/webviewMessageHandler.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,6 @@ export const webviewMessageHandler = async (
10031003
// so a retry after a partial-copy failure still reconciles the store.
10041004
await provider.taskHistoryStore.invalidateAll()
10051005
await provider.taskHistoryStore.reconcile()
1006-
await provider.taskHistoryStore.flushIndex()
10071006
await provider.postStateToWebview()
10081007
await provider.postMessageToWebview({
10091008
type: "rooHistoryImportProgress",

0 commit comments

Comments
 (0)