Skip to content

Commit dd158bb

Browse files
committed
fix: lock task history invalidation
1 parent 367013f commit dd158bb

2 files changed

Lines changed: 63 additions & 10 deletions

File tree

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -430,23 +430,27 @@ export class TaskHistoryStore {
430430
* Invalidate a single task's cache entry (re-read from disk on next access).
431431
*/
432432
async invalidate(taskId: string): Promise<void> {
433-
try {
434-
const item = await this.readTaskFile(taskId)
435-
if (item) {
436-
this.cache.set(taskId, item)
437-
} else {
433+
return this.withLock(async () => {
434+
try {
435+
const item = await this.readTaskFile(taskId)
436+
if (item) {
437+
this.cache.set(taskId, item)
438+
} else {
439+
this.cache.delete(taskId)
440+
}
441+
} catch {
438442
this.cache.delete(taskId)
439443
}
440-
} catch {
441-
this.cache.delete(taskId)
442-
}
444+
})
443445
}
444446

445447
/**
446448
* Clear all in-memory cache and reload from index.
447449
*/
448-
invalidateAll(): void {
449-
this.cache.clear()
450+
async invalidateAll(): Promise<void> {
451+
return this.withLock(async () => {
452+
this.cache.clear()
453+
})
450454
}
451455

452456
// ────────────────────────────── Migration ──────────────────────────────

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,55 @@ describe("TaskHistoryStore", () => {
442442
})
443443
})
444444

445+
describe("invalidateAll()", () => {
446+
it("waits for an in-flight write before clearing the cache", async () => {
447+
const onWrite = vi.fn().mockResolvedValue(undefined)
448+
store = new TaskHistoryStore(tmpDir, { onWrite })
449+
await store.initialize()
450+
451+
const first = makeHistoryItem({ id: "invalidate-all-first", ts: 1000, tokensIn: 100 })
452+
const second = makeHistoryItem({ id: "invalidate-all-second", ts: 2000 })
453+
await store.upsert(first)
454+
await store.upsert(second)
455+
onWrite.mockClear()
456+
457+
let signalWriteStarted!: () => void
458+
const writeStarted = new Promise<void>((resolve) => {
459+
signalWriteStarted = resolve
460+
})
461+
let releaseWrite!: () => void
462+
const writeCanFinish = new Promise<void>((resolve) => {
463+
releaseWrite = resolve
464+
})
465+
466+
const storeAny = store as any
467+
const originalWriteTaskFile = storeAny.writeTaskFile.bind(store)
468+
vi.spyOn(storeAny, "writeTaskFile").mockImplementation(async (...args: unknown[]) => {
469+
const item = args[0] as HistoryItem
470+
if (item.id === first.id && item.tokensIn === 999) {
471+
signalWriteStarted()
472+
await writeCanFinish
473+
}
474+
return originalWriteTaskFile(...args)
475+
})
476+
477+
const write = store.upsert({ ...first, tokensIn: 999 })
478+
await writeStarted
479+
const invalidation = store.invalidateAll()
480+
481+
releaseWrite()
482+
await write
483+
await invalidation
484+
485+
expect(onWrite).toHaveBeenCalledTimes(1)
486+
expect(onWrite.mock.calls[0][0].map((item: HistoryItem) => item.id).sort()).toEqual([
487+
"invalidate-all-first",
488+
"invalidate-all-second",
489+
])
490+
expect(store.getAll()).toEqual([])
491+
})
492+
})
493+
445494
describe("atomicUpdatePair()", () => {
446495
it("updates both records and both files are written before lock releases", async () => {
447496
await store.initialize()

0 commit comments

Comments
 (0)