Skip to content

Commit f46cef2

Browse files
committed
fix(persistence): isolate history mocks and log deletion failures
1 parent ba03708 commit f46cef2

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,9 @@ export class TaskHistoryStore {
339339
await removeJsonBackups(absoluteFilePath)
340340
await fs.unlink(absoluteFilePath)
341341
})
342-
} catch {
343-
// File may already be deleted
342+
} catch (error) {
343+
if (getErrorCode(error) !== "ENOENT")
344+
console.warn(`[TaskHistoryStore] Failed to delete task ${taskId}:`, error)
344345
}
345346

346347
// Call onWrite callback inside the lock for serialized write-through
@@ -370,8 +371,9 @@ export class TaskHistoryStore {
370371
await removeJsonBackups(absoluteFilePath)
371372
await fs.unlink(absoluteFilePath)
372373
})
373-
} catch {
374-
// File may already be deleted
374+
} catch (error) {
375+
if (getErrorCode(error) !== "ENOENT")
376+
console.warn(`[TaskHistoryStore] Failed to delete task ${taskId}:`, error)
375377
}
376378
}
377379

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5808,6 +5808,18 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
58085808
})
58095809

58105810
describe("getTaskWithId", () => {
5811+
const apiHistoryPath = path.join("/test/task/path", "api_conversation_history.json")
5812+
5813+
beforeEach(async () => {
5814+
const fsp = await import("fs/promises")
5815+
vi.mocked(fsp.readFile).mockReset().mockResolvedValue("")
5816+
})
5817+
5818+
afterEach(async () => {
5819+
const fsp = await import("fs/promises")
5820+
vi.mocked(fsp.readFile).mockReset().mockResolvedValue("")
5821+
})
5822+
58115823
it("does not restore a deleted file-backed task from legacy history", async () => {
58125824
const historyItem = {
58135825
id: "deleted-task",
@@ -5864,12 +5876,15 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
58645876
const fsp = await import("fs/promises")
58655877
const missing = Object.assign(new Error("missing"), { code: "ENOENT" })
58665878
const messages = [{ role: "user", content: "committed" }]
5867-
vi.mocked(fsp.readFile)
5868-
.mockRejectedValueOnce(missing)
5869-
.mockRejectedValueOnce(missing)
5870-
.mockResolvedValueOnce(JSON.stringify(messages))
5879+
let historyReads = 0
5880+
vi.mocked(fsp.readFile).mockImplementation(async (filePath) => {
5881+
if (filePath !== apiHistoryPath) return ""
5882+
if (++historyReads <= 2) throw missing
5883+
return JSON.stringify(messages)
5884+
})
58715885
const result = await provider.getTaskWithId(historyItem.id)
58725886
expect(result.apiConversationHistory).toEqual(messages)
5887+
expect(historyReads).toBe(3)
58735888
expect(recoverJsonFile).toHaveBeenCalledWith(result.apiConversationHistoryFilePath)
58745889
})
58755890

@@ -5879,7 +5894,10 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
58795894
key === "taskHistory" ? [historyItem] : undefined,
58805895
)
58815896
const fsp = await import("fs/promises")
5882-
vi.mocked(fsp.readFile).mockRejectedValueOnce(Object.assign(new Error(code), { code }))
5897+
vi.mocked(fsp.readFile).mockImplementation(async (filePath) => {
5898+
if (filePath === apiHistoryPath) throw Object.assign(new Error(code), { code })
5899+
return ""
5900+
})
58835901
await expect(provider.getTaskWithId(historyItem.id)).rejects.toMatchObject({ code })
58845902
})
58855903

@@ -5894,7 +5912,9 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
58945912

58955913
// Make readFile return corrupted JSON
58965914
const fsp = await import("fs/promises")
5897-
vi.mocked(fsp.readFile).mockResolvedValueOnce("{not valid json!!!" as never)
5915+
vi.mocked(fsp.readFile).mockImplementation(async (filePath) =>
5916+
filePath === apiHistoryPath ? "{not valid json!!!" : "",
5917+
)
58985918

58995919
const deleteTaskSpy = vi.spyOn(provider, "deleteTaskFromState")
59005920

0 commit comments

Comments
 (0)