Skip to content

Commit b460112

Browse files
committed
fix: harden task history recovery and reconciliation
1 parent 01f7b1c commit b460112

16 files changed

Lines changed: 362 additions & 91 deletions

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

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,9 @@ export class TaskHistoryStore {
283283
try {
284284
const filePath = await this.getTaskFilePath(taskId)
285285
await deleteJsonFile(filePath)
286-
} catch {
286+
} catch (error) {
287+
if (getErrorCode(error) !== "ENOENT")
288+
console.warn("[TaskHistoryStore] Failed to delete task history:", error)
287289
// File may already be deleted
288290
}
289291

@@ -311,7 +313,9 @@ export class TaskHistoryStore {
311313
try {
312314
const filePath = await this.getTaskFilePath(taskId)
313315
await deleteJsonFile(filePath)
314-
} catch {
316+
} catch (error) {
317+
if (getErrorCode(error) !== "ENOENT")
318+
console.warn("[TaskHistoryStore] Failed to delete task history:", error)
315319
// File may already be deleted
316320
}
317321
}
@@ -355,7 +359,7 @@ export class TaskHistoryStore {
355359
const taskFilePath = await this.getTaskFilePath(taskId)
356360
const { mtimeMs } = await fs.stat(taskFilePath).catch(async (error: unknown) => {
357361
if (getErrorCode(error) !== "ENOENT") throw error
358-
await recoverMissingJsonFile(taskFilePath)
362+
await recoverMissingJsonFile(taskFilePath, { waitForStaleLock: false })
359363
return fs.stat(taskFilePath)
360364
})
361365
liveIds.add(taskId)
@@ -375,7 +379,12 @@ export class TaskHistoryStore {
375379
this.cache.set(taskId, item)
376380
}
377381
}
378-
} catch {
382+
} catch (error) {
383+
if (getErrorCode(error) !== "ENOENT") {
384+
liveIds.add(taskId)
385+
console.warn("[TaskHistoryStore] Failed to reconcile task history:", error)
386+
continue
387+
}
379388
// File may be temporarily absent during a peer's atomic
380389
// rename window in safeWriteJson. The advisory lock is
381390
// held for the entire write, so its presence means a
@@ -666,7 +675,7 @@ export class TaskHistoryStore {
666675
const intentPath = await this.getDelegationRepairIntentPath()
667676
let parsed: unknown
668677
try {
669-
parsed = JSON.parse(await fs.readFile(intentPath, "utf8")) as unknown
678+
parsed = JSON.parse(await readFileWithMissingRetry(intentPath)) as unknown
670679
} catch (error) {
671680
if (this.isFileNotFoundError(error)) {
672681
return null
@@ -726,7 +735,7 @@ export class TaskHistoryStore {
726735

727736
private async removeDelegationRepairIntent(): Promise<void> {
728737
try {
729-
await fs.unlink(await this.getDelegationRepairIntentPath())
738+
await deleteJsonFile(await this.getDelegationRepairIntentPath())
730739
} catch (error) {
731740
console.warn("[TaskHistoryStore] Failed to remove completed delegation repair intent:", error)
732741
}
@@ -834,10 +843,21 @@ export class TaskHistoryStore {
834843
try {
835844
await fs.access(filePath)
836845
// File already exists, skip (don't overwrite existing per-task files)
837-
} catch {
838-
// File doesn't exist, write it
839-
await safeWriteJson(filePath, item)
840-
this.cache.set(item.id, item)
846+
} catch (error) {
847+
if (getErrorCode(error) !== "ENOENT") throw error
848+
// Recovery or a peer may supply a committed record after the access check.
849+
let persisted = item
850+
await safeWriteJson(filePath, item, {
851+
merge: (existing) => {
852+
if (existing !== null) {
853+
const recovered = historyItemSchema.parse(existing)
854+
if (recovered.id !== item.id) throw new Error("Migration task ID mismatch")
855+
persisted = recovered
856+
}
857+
return persisted
858+
},
859+
})
860+
this.cache.set(item.id, persisted)
841861
}
842862
}
843863

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ function createAction(actionId: string, message: string) {
9191
}
9292

9393
describe("TaskHistoryStore real cross-host locking", () => {
94+
it("preserves a recovered record instead of overwriting it during legacy migration", async () => {
95+
const storagePath = await fs.mkdtemp(path.join(os.tmpdir(), "task-history-migrate-"))
96+
const store = new TaskHistoryStore(storagePath)
97+
try {
98+
const legacy = item("migrated")
99+
const committed = { ...legacy, task: "committed newer task", totalCost: 7 }
100+
const taskDir = path.join(storagePath, "tasks", legacy.id)
101+
await fs.mkdir(taskDir, { recursive: true })
102+
await fs.writeFile(path.join(taskDir, ".history_item.json.bak_1.tmp"), JSON.stringify(committed))
103+
await store.migrateFromGlobalState([legacy])
104+
expect(store.get(legacy.id)).toEqual(committed)
105+
expect(JSON.parse(await fs.readFile(path.join(taskDir, "history_item.json"), "utf8"))).toEqual(committed)
106+
} finally {
107+
store.dispose()
108+
await fs.rm(storagePath, { recursive: true, force: true })
109+
}
110+
})
111+
94112
it("preserves independent stale-cache deltas through the real per-file lock", async () => {
95113
const storagePath = await fs.mkdtemp(path.join(os.tmpdir(), "task-history-real-lock-"))
96114
const storeA = new TaskHistoryStore(storagePath)

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

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import * as fs from "fs/promises"
44
import * as path from "path"
55
import * as os from "os"
6+
import * as recovery from "../../../utils/jsonFileRecovery"
67

78
import type { HistoryItem } from "@roo-code/types"
89

@@ -167,12 +168,28 @@ describe("TaskHistoryStore reconcileDelegationState", () => {
167168
})
168169

169170
afterEach(async () => {
171+
vi.restoreAllMocks()
170172
safeWriteJsonMock.mockImplementation(writeJson)
171173
for (const disposable of disposables) disposable.dispose()
172174
disposables.clear()
173175
await fs.rm(tmpDir, { recursive: true, force: true }).catch(() => {})
174176
})
175177

178+
it.each(["EACCES", "EIO", "ELOCKED", "missing"])("handles failed startup recovery: %s", async (code) => {
179+
const item = makeItem({ id: "recovery-failure", status: "active" })
180+
await seedItems([item])
181+
await store.initialize()
182+
store.dispose()
183+
const target = path.join(tmpDir, "tasks", item.id, GlobalFileNames.historyItem)
184+
await fs.rename(target, path.join(path.dirname(target), `.${path.basename(target)}.bak_1.tmp`))
185+
const recover = vi.spyOn(recovery, "recoverMissingJsonFile")
186+
if (code === "missing") recover.mockResolvedValue(false)
187+
else recover.mockRejectedValue(Object.assign(new Error("recovery failed"), { code }))
188+
await store.reconcile()
189+
expect(recover).toHaveBeenCalledWith(target, { waitForStaleLock: false })
190+
expect(store.get(item.id)).toEqual(code === "missing" ? undefined : item)
191+
})
192+
176193
it("repairs orphaned delegation: delegated parent whose child does not exist → active", async () => {
177194
const parent = makeItem({ id: "parent-1", status: "delegated", awaitingChildId: "missing-child" })
178195
await seedItems([parent])
@@ -444,26 +461,36 @@ describe("TaskHistoryStore reconcileDelegationState", () => {
444461
await expect(fs.access(intentPath)).rejects.toThrow()
445462
})
446463

447-
it("replays a both-at-target intent without writing task files", async () => {
448-
const child = makeItem({ id: "child-at-target", status: "interrupted", parentTaskId: "parent-at-target" })
449-
const parent = makeItem({ id: "parent-at-target", status: "active" })
450-
await seedItems([parent, child])
451-
const intentPath = path.join(tmpDir, "tasks", GlobalFileNames.delegationRepairIntent)
452-
await fs.writeFile(
453-
intentPath,
454-
JSON.stringify(makeRepairIntent({ ...parent, status: "delegated", awaitingChildId: child.id }, child)),
455-
)
456-
457-
const writeCalls: string[] = []
458-
safeWriteJsonMock.mockImplementation(async (filePath, data) => {
459-
writeCalls.push(filePath)
460-
await writeJson(filePath, data)
461-
})
462-
await store.initialize()
464+
it.each([false, true])(
465+
"replays a both-at-target intent without writing task files (backup: %s)",
466+
async (backup) => {
467+
const child = makeItem({ id: "child-at-target", status: "interrupted", parentTaskId: "parent-at-target" })
468+
const parent = makeItem({ id: "parent-at-target", status: "active" })
469+
await seedItems([parent, child])
470+
const intentPath = path.join(tmpDir, "tasks", GlobalFileNames.delegationRepairIntent)
471+
await fs.writeFile(
472+
intentPath,
473+
JSON.stringify(makeRepairIntent({ ...parent, status: "delegated", awaitingChildId: child.id }, child)),
474+
)
463475

464-
expect(writeCalls.filter((filePath) => filePath.endsWith(GlobalFileNames.historyItem))).toEqual([])
465-
await expect(fs.access(intentPath)).rejects.toThrow()
466-
})
476+
if (backup) {
477+
await fs.rename(
478+
intentPath,
479+
path.join(path.dirname(intentPath), `.${path.basename(intentPath)}.bak_1.tmp`),
480+
)
481+
}
482+
483+
const writeCalls: string[] = []
484+
safeWriteJsonMock.mockImplementation(async (filePath, data) => {
485+
writeCalls.push(filePath)
486+
await writeJson(filePath, data)
487+
})
488+
await store.initialize()
489+
490+
expect(writeCalls.filter((filePath) => filePath.endsWith(GlobalFileNames.historyItem))).toEqual([])
491+
await expect(fs.access(intentPath)).rejects.toThrow()
492+
},
493+
)
467494

468495
it("removes the repair-intent file after successful replay", async () => {
469496
const child = makeItem({ id: "child-deferred-index", status: "active", parentTaskId: "parent-deferred-index" })
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as fs from "fs/promises"
2+
import * as os from "os"
3+
import * as path from "path"
4+
5+
import { readFileWithMissingRetry } from "../readFileWithMissingRetry"
6+
7+
describe("readFileWithMissingRetry", () => {
8+
let dir: string
9+
beforeEach(async () => {
10+
dir = await fs.mkdtemp(path.join(os.tmpdir(), "missing-retry-"))
11+
})
12+
afterEach(async () => {
13+
await fs.rm(dir, { recursive: true, force: true })
14+
})
15+
16+
it("reads an existing file", async () => {
17+
const target = path.join(dir, "data.json")
18+
await fs.writeFile(target, "[]")
19+
await expect(readFileWithMissingRetry(target)).resolves.toBe("[]")
20+
})
21+
22+
it("preserves ENOENT after the second read when nothing can be recovered", async () => {
23+
await expect(readFileWithMissingRetry(path.join(dir, "missing.json"))).rejects.toMatchObject({ code: "ENOENT" })
24+
})
25+
26+
it("propagates non-ENOENT errors", async () => {
27+
await expect(readFileWithMissingRetry(dir)).rejects.toMatchObject({ code: "EISDIR" })
28+
})
29+
})

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { LogFunction } from "../../utils/outputChannelLogger"
2+
13
/**
24
* Structured trace points for task-persistence file writes and shutdown.
35
*
@@ -21,6 +23,8 @@ export type PersistenceTraceStage =
2123
| "ui-messages-write-complete"
2224
| "ui-messages-marker-check"
2325
| "shutdown"
26+
| "json-recovery"
27+
| "json-lock-wait"
2428

2529
export type PersistenceTraceFields = {
2630
taskId?: string
@@ -29,7 +33,7 @@ export type PersistenceTraceFields = {
2933
detail?: string
3034
}
3135

32-
type PersistenceTraceSink = (line: string) => void
36+
type PersistenceTraceSink = LogFunction<[line: string]>
3337

3438
let traceSink: PersistenceTraceSink | undefined
3539

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { getErrorCode } from "../../utils/errorCode"
2+
export { getErrorCode } from "../../utils/errorCode"
3+
14
import * as fs from "fs/promises"
25
import { recoverMissingJsonFile } from "../../utils/jsonFileRecovery"
36

@@ -6,17 +9,11 @@ import { recoverMissingJsonFile } from "../../utils/jsonFileRecovery"
69
// new file to target), because a single rename over an open file fails with
710
// EPERM on Windows. A reader can see ENOENT between those two renames, so
811
// retry once, then recover under the writer's lock if a process crash left
9-
// recoverable backups. This can wait out the 31-second lock staleness window.
12+
// recoverable backups. Recovery may retry lock acquisition for up to 35 seconds.
1013
const READ_RETRY_MIN_MS = 1
1114
const READ_RETRY_MAX_MS = 10
1215
const READ_RETRY_RANGE_MS = READ_RETRY_MAX_MS - READ_RETRY_MIN_MS + 1
1316

14-
export function getErrorCode(error: unknown): string | undefined {
15-
return typeof error === "object" && error !== null && "code" in error && typeof error.code === "string"
16-
? error.code
17-
: undefined
18-
}
19-
2017
/**
2118
* Formats the actionable detail of an underlying error for inclusion in
2219
* wrapping error messages: the filesystem error code when present (e.g.

‎src/core/webview/ClineProvider.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2742,11 +2742,13 @@ export class ClineProvider
27422742
historyItem: HistoryItem
27432743
aggregatedCosts: AggregatedCosts
27442744
}> {
2745-
const { historyItem } = await this.getTaskWithId(taskId)
2745+
const historyItem = this.getTaskHistoryItem(taskId)
2746+
if (!historyItem) throw new Error("Task not found")
27462747

27472748
const aggregatedCosts = await aggregateTaskCostsRecursive(taskId, async (id: string) => {
2748-
const result = await this.getTaskWithId(id)
2749-
return result.historyItem
2749+
const item = this.getTaskHistoryItem(id)
2750+
if (!item) throw new Error("Task not found")
2751+
return item
27502752
})
27512753

27522754
return { historyItem, aggregatedCosts }

‎src/core/webview/__tests__/ClineProvider.spec.ts‎

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7959,15 +7959,69 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
79597959
provider.taskHistoryStore["cache"].set(historyItem.id, historyItem)
79607960
provider["taskHistoryStoreInitialized"] = true
79617961
const fsp = await import("fs/promises")
7962-
vi.mocked(fsp.readFile)
7963-
.mockRejectedValueOnce(Object.assign(new Error("Replacement in progress"), { code: "ENOENT" }))
7964-
.mockResolvedValueOnce(JSON.stringify([{ role: "assistant", content: "committed" }]))
7962+
let reads = 0
7963+
vi.mocked(fsp.readFile).mockImplementation(async (file) => {
7964+
if (!String(file).endsWith("api_conversation_history.json")) return ""
7965+
if (++reads === 1) throw Object.assign(new Error("Replacement in progress"), { code: "ENOENT" })
7966+
return JSON.stringify([{ role: "assistant", content: "committed" }])
7967+
})
79657968

79667969
const result = await provider.getTaskWithId(historyItem.id)
79677970

79687971
expect(result.apiConversationHistory).toEqual([{ role: "assistant", content: "committed" }])
79697972
})
79707973

7974+
it.each(["ENOENT", "EACCES"])("returns empty history after %s without removing the task", async (code) => {
7975+
const item = {
7976+
id: "failed-api-read",
7977+
task: "test",
7978+
ts: 1,
7979+
number: 1,
7980+
tokensIn: 0,
7981+
tokensOut: 0,
7982+
totalCost: 0,
7983+
}
7984+
provider.taskHistoryStore["cache"].set(item.id, item)
7985+
provider["taskHistoryStoreInitialized"] = true
7986+
const fsp = await import("fs/promises")
7987+
const read = vi.mocked(fsp.readFile)
7988+
let reads = 0
7989+
read.mockImplementation(async (file) => {
7990+
if (!String(file).endsWith("api_conversation_history.json")) return ""
7991+
reads++
7992+
throw Object.assign(new Error("read failed"), { code })
7993+
})
7994+
const result = await provider.getTaskWithId(item.id)
7995+
expect(result.apiConversationHistory).toEqual([])
7996+
expect(provider.taskHistoryStore.get(item.id)).toEqual(item)
7997+
expect(reads).toBe(code === "ENOENT" ? 2 : 1)
7998+
})
7999+
8000+
it("aggregates parent and child costs without reading API history", async () => {
8001+
const parent = {
8002+
id: "cost-parent",
8003+
task: "test",
8004+
ts: 1,
8005+
number: 1,
8006+
tokensIn: 0,
8007+
tokensOut: 0,
8008+
totalCost: 2,
8009+
childIds: ["cost-child"],
8010+
}
8011+
provider.taskHistoryStore["cache"].set(parent.id, parent)
8012+
provider.taskHistoryStore["cache"].set("cost-child", {
8013+
...parent,
8014+
id: "cost-child",
8015+
totalCost: 3,
8016+
childIds: [],
8017+
})
8018+
provider["taskHistoryStoreInitialized"] = true
8019+
const read = vi.spyOn(provider, "getTaskWithId")
8020+
const result = await provider.getTaskWithAggregatedCosts(parent.id)
8021+
expect(result.aggregatedCosts.totalCost).toBe(5)
8022+
expect(read).not.toHaveBeenCalled()
8023+
})
8024+
79718025
it("does not restore a deleted file-backed task from legacy history", async () => {
79728026
const historyItem = {
79738027
id: "deleted-task",

0 commit comments

Comments
 (0)