Skip to content

Commit 2fe1189

Browse files
committed
test(task-history): add regression coverage for self-healing index recovery
1 parent aee7d64 commit 2fe1189

11 files changed

Lines changed: 31 additions & 943 deletions

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

Lines changed: 0 additions & 104 deletions
This file was deleted.

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

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type { HistoryItem } from "@roo-code/types"
99
import { GlobalFileNames } from "../../shared/globalFileNames"
1010
import { safeWriteJson } from "../../utils/safeWriteJson"
1111
import { getStorageBasePath } from "../../utils/storage"
12-
import { taskHistoryLock } from "./TaskHistoryLock"
1312

1413
/** Valid status values for a task's HistoryItem. */
1514
export type HistoryItemStatus = NonNullable<HistoryItem["status"]>
@@ -80,11 +79,12 @@ interface DelegationRepairIntent {
8079
* as a cache for fast list reads at startup.
8180
*
8281
* Cross-process safety for per-task files comes from `safeWriteJson`'s
83-
* `proper-lockfile`. The shared `_index.json` is treated as a rebuildable
84-
* cache: writers rebuild it from on-disk `history_item.json` files under a
85-
* cross-process `tasks/_history.lock`, so a stale in-memory snapshot cannot
86-
* clobber entries published by another extension host. Within a single
87-
* process, an in-process write lock serializes mutations.
82+
* `proper-lockfile`. The shared `_index.json` is a best-effort startup
83+
* cache: it may lag or miss entries from other hosts, but
84+
* `reconcile()` rebuilds from authoritative per-task files — at startup
85+
* (`forceRefresh: true`), via `fs.watch`, and on a 5-minute periodic
86+
* fallback. Within a single extension host process,
87+
* an in-process write lock serializes mutations.
8888
*/
8989
/**
9090
* Options for TaskHistoryStore constructor.
@@ -884,57 +884,17 @@ export class TaskHistoryStore {
884884
}
885885

886886
/**
887-
* Rebuild `_index.json` from authoritative per-task files under a
888-
* cross-process lock.
889-
*
890-
* Must not dump `this.cache` directly: each extension host only has a
891-
* partial view, and a full-cache snapshot write is a lost-update hazard
892-
* when two hosts flush inside the watcher debounce window (see #1231).
893-
*
894-
* Safe to call while the in-process `withLock` is already held (e.g.
895-
* migration) — only the shared file lock is acquired here.
887+
* Write the full index to disk.
896888
*/
897889
private async writeIndex(): Promise<void> {
898-
await taskHistoryLock.withLock(this.globalStoragePath, async () => {
899-
const indexPath = await this.getIndexPath()
900-
const entries = await this.collectIndexEntriesFromDisk()
901-
const index: HistoryIndex = {
902-
version: 1,
903-
updatedAt: Date.now(),
904-
entries,
905-
}
906-
907-
await safeWriteJson(indexPath, index)
908-
})
909-
}
910-
911-
/**
912-
* Scan task directories and load each `history_item.json` for the index.
913-
* Per-task files are the source of truth; missing/corrupt files are skipped.
914-
*/
915-
private async collectIndexEntriesFromDisk(): Promise<HistoryItem[]> {
916-
const tasksDir = await this.getTasksDir()
917-
918-
let dirEntries: string[]
919-
try {
920-
dirEntries = await fs.readdir(tasksDir)
921-
} catch {
922-
return []
923-
}
924-
925-
const entries: HistoryItem[] = []
926-
for (const name of dirEntries) {
927-
if (name.startsWith("_") || name.startsWith(".")) {
928-
continue
929-
}
930-
931-
const item = await this.readTaskFile(name)
932-
if (item) {
933-
entries.push(item)
934-
}
890+
const indexPath = await this.getIndexPath()
891+
const index: HistoryIndex = {
892+
version: 1,
893+
updatedAt: Date.now(),
894+
entries: this.getAll(),
935895
}
936896

937-
return entries.sort((a, b) => b.ts - a.ts)
897+
await safeWriteJson(indexPath, index)
938898
}
939899

940900
/**

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

Lines changed: 0 additions & 107 deletions
This file was deleted.

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,22 +167,20 @@ describe("TaskHistoryStore cross-instance safety", () => {
167167

168168
/**
169169
* Regression for #1231: two hosts each hold only their own task in cache and
170-
* flush `_index.json` without watcher reconciliation. A cache-snapshot writer
171-
* would drop the other host's entry; disk-authoritative rebuild must keep both.
170+
* flush `_index.json` without watcher reconciliation. The last writer's
171+
* index reflects only its own cache (LWW). Per-task files remain intact,
172+
* and reconcile recovers the full set.
172173
*/
173-
it("stale in-memory snapshots cannot drop peer entries from _index.json on flush", async () => {
174+
it("reconcile recovers peer entries after a last-writer-wins index flush", async () => {
174175
await storeA.initialize()
175176
await storeB.initialize()
176177

177-
// Model both processes flushing inside the watcher debounce window:
178-
// no reconcile between upserts and index flushes.
179178
disableBackgroundReconciliation(storeA)
180179
disableBackgroundReconciliation(storeB)
181180

182181
await storeA.upsert(makeHistoryItem({ id: "task-a", task: "from A", ts: 1000 }))
183182
await storeB.upsert(makeHistoryItem({ id: "task-b", task: "from B", ts: 2000 }))
184183

185-
// Each cache is intentionally partial — the pre-fix failure mode.
186184
expect(storeA.get("task-a")).toBeDefined()
187185
expect(storeA.get("task-b")).toBeUndefined()
188186
expect(storeB.get("task-b")).toBeDefined()
@@ -191,10 +189,24 @@ describe("TaskHistoryStore cross-instance safety", () => {
191189
await storeA.flushIndex()
192190
await storeB.flushIndex()
193191

192+
// Per-task files survive regardless of which host flushed last.
194193
const tasksDir = path.join(tmpDir, "tasks")
194+
195+
// The index reflects the last writer's partial cache (LWW clobber).
196+
const indexRawBefore = await fs.readFile(path.join(tasksDir, GlobalFileNames.historyIndex), "utf8")
197+
const indexBefore = JSON.parse(indexRawBefore) as { entries: HistoryItem[] }
198+
expect(indexBefore.entries.map((e) => e.id)).toEqual(["task-b"])
195199
const taskDirs = (await fs.readdir(tasksDir)).filter((name) => !name.startsWith("_") && !name.startsWith("."))
196200
expect(taskDirs.sort()).toEqual(["task-a", "task-b"])
197201

202+
// Reconcile rebuilds the full picture from authoritative per-task files.
203+
await storeA.reconcile({ forceRefresh: true })
204+
expect(storeA.get("task-a")).toBeDefined()
205+
expect(storeA.get("task-b")).toBeDefined()
206+
expect(storeA.getAll()).toHaveLength(2)
207+
208+
// A post-reconcile flush writes the complete set.
209+
await storeA.flushIndex()
198210
const indexRaw = await fs.readFile(path.join(tasksDir, GlobalFileNames.historyIndex), "utf8")
199211
const index = JSON.parse(indexRaw) as { entries: HistoryItem[] }
200212
const indexIds = index.entries.map((entry) => entry.id).sort()

0 commit comments

Comments
 (0)