Skip to content

Commit 981b668

Browse files
committed
fix: flush write-through on partial deleteMany and reject on lock compromise
- deleteMany now awaits the write-through callback with the current cache when a later deletion fails after an earlier success, so persisted globalState.taskHistory does not keep already-deleted tasks. The original deletion error stays the rejection, and a failed flush is logged without masking it. - acquireFileLock records a lock compromise instead of throwing from the proper-lockfile renewal timer. The release rejects with the recorded compromise error, and withFileLock and safeWriteJson reject a successful operation whose lock was compromised. Operation errors keep precedence. - Add regression tests for both behaviors.
1 parent 8797436 commit 981b668

6 files changed

Lines changed: 327 additions & 14 deletions

File tree

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,30 @@ export class TaskHistoryStore {
281281
*/
282282
async deleteMany(taskIds: string[]): Promise<void> {
283283
return this.withLock(async () => {
284-
for (const taskId of taskIds) {
285-
await this.deleteTaskFile(taskId)
284+
let deletedCount = 0
285+
try {
286+
for (const taskId of taskIds) {
287+
await this.deleteTaskFile(taskId)
288+
deletedCount++
289+
}
290+
} catch (error) {
291+
// Earlier deletions already removed their files and cache entries.
292+
// Await the write-through with the current cache before the
293+
// original deletion error rejects the call, so persisted
294+
// globalState does not keep already-deleted tasks.
295+
if (deletedCount > 0 && this.onWrite) {
296+
try {
297+
await this.onWrite(this.getAll())
298+
} catch (writeError) {
299+
// The deletion failure is the primary error. Report the
300+
// write-through failure without masking it.
301+
console.error(
302+
"[TaskHistoryStore] deleteMany write-through after partial deletion failed:",
303+
writeError,
304+
)
305+
}
306+
}
307+
throw error
286308
}
287309

288310
// Call onWrite callback inside the lock for serialized write-through

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,80 @@ describe("TaskHistoryStore", () => {
323323
expect(store.get(failed.id)).toEqual(failed)
324324
expect(store.get(later.id)).toEqual(later)
325325
})
326+
327+
it("flushes the write-through with the current cache when a later deletion fails", async () => {
328+
store.dispose()
329+
const globalState: HistoryItem[] = []
330+
const onWrite = vi.fn(async (items: HistoryItem[]) => {
331+
globalState.splice(0, globalState.length, ...items)
332+
})
333+
store = new TaskHistoryStore(tmpDir, { onWrite })
334+
await store.initialize()
335+
336+
const first = makeHistoryItem({ id: "batch-wt-success" })
337+
const failed = makeHistoryItem({ id: "batch-wt-failure" })
338+
const later = makeHistoryItem({ id: "batch-wt-later" })
339+
await store.upsert(first)
340+
await store.upsert(failed)
341+
await store.upsert(later)
342+
343+
const actualFs = await vi.importActual<typeof import("fs/promises")>("fs/promises")
344+
const unlinkError = Object.assign(new Error("unlink failed"), { code: "EACCES" })
345+
vi.mocked(fs.unlink).mockImplementation(async (filePath) => {
346+
if (filePath.toString().includes(failed.id)) {
347+
throw unlinkError
348+
}
349+
return actualFs.unlink(filePath)
350+
})
351+
352+
await expect(store.deleteMany([first.id, failed.id, later.id])).rejects.toBe(unlinkError)
353+
354+
// The earlier deleted task is evicted from the store cache.
355+
expect(store.get(first.id)).toBeUndefined()
356+
// The awaited write-through persisted that state before the original
357+
// deletion error rejected the call, so stale globalState cannot
358+
// return the deleted task.
359+
expect(globalState.find((item) => item.id === first.id)).toBeUndefined()
360+
expect(globalState.find((item) => item.id === failed.id)).toEqual(failed)
361+
expect(globalState.find((item) => item.id === later.id)).toEqual(later)
362+
363+
vi.mocked(fs.unlink).mockImplementation(async (filePath) => actualFs.unlink(filePath))
364+
})
365+
366+
it("keeps the original deletion error when the partial write-through also fails", async () => {
367+
store.dispose()
368+
const onWrite = vi.fn()
369+
// Three upserts succeed, then the partial-failure flush rejects.
370+
onWrite
371+
.mockResolvedValueOnce(undefined)
372+
.mockResolvedValueOnce(undefined)
373+
.mockResolvedValueOnce(undefined)
374+
.mockRejectedValueOnce(new Error("write-through failed"))
375+
store = new TaskHistoryStore(tmpDir, { onWrite })
376+
await store.initialize()
377+
378+
const first = makeHistoryItem({ id: "batch-wt2-success" })
379+
const failed = makeHistoryItem({ id: "batch-wt2-failure" })
380+
const later = makeHistoryItem({ id: "batch-wt2-later" })
381+
await store.upsert(first)
382+
await store.upsert(failed)
383+
await store.upsert(later)
384+
385+
const actualFs = await vi.importActual<typeof import("fs/promises")>("fs/promises")
386+
const unlinkError = Object.assign(new Error("unlink failed"), { code: "EACCES" })
387+
vi.mocked(fs.unlink).mockImplementation(async (filePath) => {
388+
if (filePath.toString().includes(failed.id)) {
389+
throw unlinkError
390+
}
391+
return actualFs.unlink(filePath)
392+
})
393+
394+
// The deletion failure is the primary error and must not be masked
395+
// by the failed write-through.
396+
await expect(store.deleteMany([first.id, failed.id, later.id])).rejects.toBe(unlinkError)
397+
398+
vi.mocked(fs.unlink).mockImplementation(async (filePath) => actualFs.unlink(filePath))
399+
})
326400
})
327401

328402
describe("reconcile()", () => {
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// pnpm --filter roo-cline test utils/__tests__/fileLock.spec.ts
2+
3+
import * as path from "path"
4+
5+
import { acquireFileLock, withFileLock } from "../fileLock"
6+
7+
const { lockMock } = vi.hoisted(() => ({ lockMock: vi.fn() }))
8+
9+
vi.mock("proper-lockfile", () => ({ lock: lockMock, default: { lock: lockMock } }))
10+
11+
interface CapturedLockOptions {
12+
onCompromised: (error: Error) => void
13+
}
14+
15+
function requireCapturedLockOptions(): CapturedLockOptions {
16+
if (!capturedOptions) {
17+
throw new Error("proper-lockfile options were not captured")
18+
}
19+
return capturedOptions
20+
}
21+
22+
let capturedOptions: CapturedLockOptions | undefined
23+
24+
describe("fileLock", () => {
25+
const absoluteFilePath = path.resolve("/virtual/dir/target.json")
26+
let underlyingRelease: ReturnType<typeof vi.fn>
27+
let consoleError: ReturnType<typeof vi.spyOn>
28+
29+
beforeEach(() => {
30+
consoleError = vi.spyOn(console, "error").mockImplementation(() => {})
31+
underlyingRelease = vi.fn().mockResolvedValue(undefined)
32+
capturedOptions = undefined
33+
lockMock.mockImplementation(async (_filePath: string, options: CapturedLockOptions) => {
34+
capturedOptions = options
35+
return underlyingRelease
36+
})
37+
})
38+
39+
afterEach(() => {
40+
consoleError.mockRestore()
41+
vi.clearAllMocks()
42+
})
43+
44+
it("records the compromise without throwing from the renewal callback", () => {
45+
const compromiseError = Object.assign(new Error("lock renewal failed"), { code: "ECOMPROMISED" })
46+
47+
return acquireFileLock(absoluteFilePath).then((release) => {
48+
expect(() => requireCapturedLockOptions().onCompromised(compromiseError)).not.toThrow()
49+
return release().catch(() => {})
50+
})
51+
})
52+
53+
it("rejects the owning operation with the recorded compromise error", async () => {
54+
const compromiseError = Object.assign(new Error("lock renewal failed"), { code: "ECOMPROMISED" })
55+
const operation = vi.fn(async () => {
56+
// Simulate the proper-lockfile renewal timer reporting a
57+
// compromise while the operation still holds the lock.
58+
requireCapturedLockOptions().onCompromised(compromiseError)
59+
return "done"
60+
})
61+
62+
await expect(withFileLock(absoluteFilePath, operation)).rejects.toBe(compromiseError)
63+
expect(operation).toHaveBeenCalledTimes(1)
64+
// proper-lockfile already marked the lock released, so the wrapper
65+
// must not call the underlying release after a compromise.
66+
expect(underlyingRelease).not.toHaveBeenCalled()
67+
})
68+
69+
it("keeps the operation error when the operation fails after a compromise", async () => {
70+
const compromiseError = Object.assign(new Error("lock renewal failed"), { code: "ECOMPROMISED" })
71+
const operationError = new Error("operation failed")
72+
const operation = vi.fn(async () => {
73+
requireCapturedLockOptions().onCompromised(compromiseError)
74+
throw operationError
75+
})
76+
77+
await expect(withFileLock(absoluteFilePath, operation)).rejects.toBe(operationError)
78+
})
79+
80+
it("rejects a direct release with the recorded compromise error", async () => {
81+
const compromiseError = Object.assign(new Error("lock renewal failed"), { code: "ECOMPROMISED" })
82+
const release = await acquireFileLock(absoluteFilePath)
83+
84+
requireCapturedLockOptions().onCompromised(compromiseError)
85+
86+
await expect(release()).rejects.toBe(compromiseError)
87+
expect(underlyingRelease).not.toHaveBeenCalled()
88+
})
89+
90+
it("releases normally without a compromise", async () => {
91+
const operation = vi.fn(async () => "done")
92+
93+
await expect(withFileLock(absoluteFilePath, operation)).resolves.toBe("done")
94+
expect(underlyingRelease).toHaveBeenCalledTimes(1)
95+
})
96+
97+
it("keeps reporting success when an unrelated release error occurs", async () => {
98+
underlyingRelease.mockRejectedValue(new Error("unlock failed"))
99+
100+
await expect(
101+
withFileLock(
102+
absoluteFilePath,
103+
vi.fn(async () => "done"),
104+
),
105+
).resolves.toBe("done")
106+
expect(consoleError).toHaveBeenCalled()
107+
})
108+
})

‎src/utils/__tests__/safeWriteJson.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,67 @@ describe("safeWriteJson", () => {
477477
await fs.unlink(lockTestFilePath).catch(() => {}) // Ignore errors if file doesn't exist
478478
vi.unmock("proper-lockfile") // Ensure the mock is removed after this test
479479
})
480+
481+
test("rejects with a lock compromise error after a successful write", async () => {
482+
vi.resetModules()
483+
484+
const data = { message: "compromise after success" }
485+
const compromiseTestFilePath = path.join(tempDir, "compromise-test-file.json")
486+
await fs.writeFile(compromiseTestFilePath, JSON.stringify({ initial: "content" }))
487+
488+
const compromiseError = Object.assign(new Error("lock was compromised"), { code: "ECOMPROMISED" })
489+
vi.doMock("proper-lockfile", () => ({
490+
...vi.importActual("proper-lockfile"),
491+
lock: vi.fn().mockResolvedValue(vi.fn().mockRejectedValue(compromiseError)),
492+
}))
493+
494+
const { safeWriteJson: mockedSafeWriteJson } = await import("../safeWriteJson")
495+
496+
// The write itself succeeds, but the release reports a compromised
497+
// lock, so the operation must reject instead of reporting success.
498+
await expect(mockedSafeWriteJson(compromiseTestFilePath, data)).rejects.toBe(compromiseError)
499+
500+
await fs.unlink(compromiseTestFilePath).catch(() => {})
501+
vi.doUnmock("proper-lockfile")
502+
})
503+
504+
test("keeps the original write error when the write fails and the lock was compromised", async () => {
505+
vi.resetModules()
506+
507+
const data = { message: "compromise after failure" }
508+
const compromiseTestFilePath = path.join(tempDir, "compromise-failure-test-file.json")
509+
await fs.writeFile(compromiseTestFilePath, JSON.stringify({ initial: "content" }))
510+
511+
const compromiseError = Object.assign(new Error("lock was compromised"), { code: "ECOMPROMISED" })
512+
vi.doMock("proper-lockfile", () => ({
513+
...vi.importActual("proper-lockfile"),
514+
lock: vi.fn().mockResolvedValue(vi.fn().mockRejectedValue(compromiseError)),
515+
}))
516+
517+
const createWriteStreamSpy = vi.spyOn(fsSyncActual, "createWriteStream")
518+
createWriteStreamSpy.mockImplementationOnce(() => {
519+
// A plain Writable provides the pipe and error surface that
520+
// `_streamDataToFile` uses, but not the full WriteStream
521+
// interface, so this cast is deliberate.
522+
const errorStream = new Writable({
523+
write: (_chunk, _encoding, callback) => {
524+
callback(new Error("Stream write error"))
525+
},
526+
}) as unknown as fsSyncActual.WriteStream
527+
errorStream.close = vi.fn()
528+
return errorStream
529+
})
530+
531+
const { safeWriteJson: mockedSafeWriteJson } = await import("../safeWriteJson")
532+
533+
// The write failure is the primary error and must not be masked by
534+
// the compromise reported at release time.
535+
await expect(mockedSafeWriteJson(compromiseTestFilePath, data)).rejects.toThrow("Stream write error")
536+
537+
createWriteStreamSpy.mockRestore()
538+
await fs.unlink(compromiseTestFilePath).catch(() => {})
539+
vi.doUnmock("proper-lockfile")
540+
})
480541
test("should release lock even if an error occurs mid-operation", async () => {
481542
const data = { message: "test lock release on error" }
482543

‎src/utils/fileLock.ts‎

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ export const LOCK_STALE_MS = 31_000
55

66
export async function acquireFileLock(filePath: string): Promise<() => Promise<void>> {
77
const absoluteFilePath = path.resolve(filePath)
8+
// proper-lockfile calls `onCompromised` from its renewal timer after the
9+
// lock promise already resolved. A throw here becomes an uncaught
10+
// exception in the host instead of a rejection, so record the error and
11+
// fail the owning operation through its release call instead.
12+
let compromisedError: Error | null = null
813
try {
9-
return await lockfile.lock(absoluteFilePath, {
14+
const release = await lockfile.lock(absoluteFilePath, {
1015
stale: LOCK_STALE_MS,
1116
update: 10_000,
1217
realpath: false,
@@ -18,9 +23,18 @@ export async function acquireFileLock(filePath: string): Promise<() => Promise<v
1823
},
1924
onCompromised: (error) => {
2025
console.error(`Lock at ${absoluteFilePath} was compromised:`, error)
21-
throw error
26+
compromisedError = error
2227
},
2328
})
29+
return async () => {
30+
if (compromisedError) {
31+
// proper-lockfile marks the lock released when it reports a
32+
// compromise and its own release resolves silently, so reject
33+
// with the recorded compromise error here.
34+
throw compromisedError
35+
}
36+
await release()
37+
}
2438
} catch (error) {
2539
console.error(`Failed to acquire lock for ${absoluteFilePath}:`, error)
2640
throw error
@@ -38,13 +52,34 @@ export async function withFileLock<T>(
3852
const absoluteFilePath = path.resolve(filePath)
3953
const releaseLock = await acquireFileLock(absoluteFilePath)
4054

55+
let result: T
4156
try {
42-
return await operation(absoluteFilePath)
43-
} finally {
57+
result = await operation(absoluteFilePath)
58+
} catch (operationError) {
59+
// The operation error is the primary failure. Release without
60+
// reporting a secondary release error over it.
4461
try {
4562
await releaseLock()
46-
} catch (error) {
47-
console.error(`Failed to release lock for ${absoluteFilePath}:`, error)
63+
} catch (releaseError) {
64+
console.error(`Failed to release lock for ${absoluteFilePath}:`, releaseError)
65+
}
66+
throw operationError
67+
}
68+
69+
try {
70+
await releaseLock()
71+
} catch (releaseError) {
72+
const code =
73+
releaseError && typeof releaseError === "object" && "code" in releaseError
74+
? (releaseError as { code: unknown }).code
75+
: undefined
76+
if (code === "ECOMPROMISED") {
77+
// The lock was compromised while this operation held it, so the
78+
// operation ran without mutual exclusion. Reject the operation
79+
// that owns the lock instead of reporting success.
80+
throw releaseError
4881
}
82+
console.error(`Failed to release lock for ${absoluteFilePath}:`, releaseError)
4983
}
84+
return result
5085
}

0 commit comments

Comments
 (0)