Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/core/task-persistence/TaskHistoryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,23 +430,27 @@ export class TaskHistoryStore {
* Invalidate a single task's cache entry (re-read from disk on next access).
*/
async invalidate(taskId: string): Promise<void> {
try {
const item = await this.readTaskFile(taskId)
if (item) {
this.cache.set(taskId, item)
} else {
return this.withLock(async () => {
try {
const item = await this.readTaskFile(taskId)
if (item) {
this.cache.set(taskId, item)
} else {
this.cache.delete(taskId)
}
} catch {
this.cache.delete(taskId)
}
} catch {
this.cache.delete(taskId)
}
})
}

/**
* Clear all in-memory cache and reload from index.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSDoc still says "reload from index" but the body only calls cache.clear() -- loadIndex() is never called. Worth updating to reflect the actual behavior?

*/
invalidateAll(): void {
this.cache.clear()
async invalidateAll(): Promise<void> {
return this.withLock(async () => {
this.cache.clear()
})
}

// ────────────────────────────── Migration ──────────────────────────────
Expand Down
49 changes: 49 additions & 0 deletions src/core/task-persistence/__tests__/TaskHistoryStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,55 @@ describe("TaskHistoryStore", () => {
})
})

describe("invalidateAll()", () => {
it("waits for an in-flight write before clearing the cache", async () => {
const onWrite = vi.fn().mockResolvedValue(undefined)
store = new TaskHistoryStore(tmpDir, { onWrite })
await store.initialize()

const first = makeHistoryItem({ id: "invalidate-all-first", ts: 1000, tokensIn: 100 })
const second = makeHistoryItem({ id: "invalidate-all-second", ts: 2000 })
await store.upsert(first)
await store.upsert(second)
onWrite.mockClear()

let signalWriteStarted!: () => void
const writeStarted = new Promise<void>((resolve) => {
signalWriteStarted = resolve
})
let releaseWrite!: () => void
const writeCanFinish = new Promise<void>((resolve) => {
releaseWrite = resolve
})

const storeAny = store as any
const originalWriteTaskFile = storeAny.writeTaskFile.bind(store)
vi.spyOn(storeAny, "writeTaskFile").mockImplementation(async (...args: unknown[]) => {
const item = args[0] as HistoryItem
if (item.id === first.id && item.tokensIn === 999) {
signalWriteStarted()
await writeCanFinish
}
return originalWriteTaskFile(...args)
})

const write = store.upsert({ ...first, tokensIn: 999 })
await writeStarted
const invalidation = store.invalidateAll()

releaseWrite()
await write
await invalidation

expect(onWrite).toHaveBeenCalledTimes(1)
expect(onWrite.mock.calls[0][0].map((item: HistoryItem) => item.id).sort()).toEqual([
"invalidate-all-first",
"invalidate-all-second",
])
expect(store.getAll()).toEqual([])
})
})

describe("atomicUpdatePair()", () => {
it("updates both records and both files are written before lock releases", async () => {
await store.initialize()
Expand Down
Loading