Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop): skip a pending chat cleanup another pass already finished

Alfredo Lopez5ab4cffffafd
diff --git a/desktop-app/src/__tests__/chatGetIndex.backgroundCleanup.test.ts b/desktop-app/src/__tests__/chatGetIndex.backgroundCleanup.test.ts
index 556651ba2..d0b6f43bd 100644
--- a/desktop-app/src/__tests__/chatGetIndex.backgroundCleanup.test.ts
+++ b/desktop-app/src/__tests__/chatGetIndex.backgroundCleanup.test.ts
@@ -133,4 +133,63 @@ describe('runChatGetIndex background artifact cleanup', () => {
expect(visible.map(chat => chat.id)).toEqual(['kept-chat'])
})
})
+
+ it('finishes a pending cleanup once when two reads overlap', async () => {
+ await queuePendingCleanup()
+ let release!: () => void
+ const blocked = new Promise<void>(resolve => {
+ release = resolve
+ })
+ const remove = fs.rm.bind(fs)
+ let artifactRemovals = 0
+ vi.spyOn(fs, 'rm').mockImplementation(async (...args) => {
+ if (String(args[0]) === artifactPath) {
+ artifactRemovals += 1
+ await blocked
+ }
+ return remove(...args)
+ })
+ const serializeChat = vi.spyOn(
+ store as unknown as { serializeChat: (...args: unknown[]) => Promise<unknown> },
+ 'serializeChat'
+ )
+ const saveIndex = vi.spyOn(
+ store as unknown as { saveIndex: (...args: unknown[]) => Promise<void> },
+ 'saveIndex'
+ )
+ const retry = vi.spyOn(store, 'retryPendingDeleteCleanups')
+ const warnCleanupFailure = vi.fn()
+ const deps = {
+ store,
+ getChatDeletionFenceAuthority: () => ({ authorityScope }),
+ warnCleanupFailure,
+ }
+
+ const reads = [runChatGetIndex(deps, agentRef), runChatGetIndex(deps, agentRef)]
+ try {
+ // Both passes snapshot the queue with the tombstone in it: the first is
+ // blocked removing the artifact, the second waits behind it for the chat.
+ await vi.waitFor(() => {
+ expect(artifactRemovals).toBe(1)
+ expect(serializeChat).toHaveBeenCalledTimes(2)
+ })
+ for (const index of await Promise.all(reads)) {
+ expect(index.pendingChatCleanup).toEqual([tombstone])
+ }
+ } finally {
+ release()
+ await Promise.allSettled([...reads, ...retry.mock.results.map(result => result.value)])
+ }
+
+ expect(retry).toHaveBeenCalledTimes(2)
+ // Liveness: the cleanup ran to completion and the kept chat is untouched.
+ await expect(fs.access(artifactPath)).rejects.toMatchObject({ code: 'ENOENT' })
+ const finishedIndex = await store.getIndex(agentRef)
+ expect(finishedIndex.pendingChatCleanup).toEqual([])
+ expect(finishedIndex.chats.map(chat => chat.id)).toEqual(['kept-chat'])
+ // The second pass found the tombstone dequeued: no second removal or save.
+ expect(artifactRemovals).toBe(1)
+ expect(saveIndex).toHaveBeenCalledTimes(1)
+ expect(warnCleanupFailure).not.toHaveBeenCalled()
+ })
})
diff --git a/desktop-app/src/__tests__/chatStore.test.ts b/desktop-app/src/__tests__/chatStore.test.ts
index 08742a4be..81027077f 100644
--- a/desktop-app/src/__tests__/chatStore.test.ts
+++ b/desktop-app/src/__tests__/chatStore.test.ts
@@ -1054,16 +1054,17 @@ describe('pending cleanup retry per agent', () => {
const reads = recordIndexReads()
await store.retryPendingDeleteCleanups(TEAM_A_SCOPE, 'agent-1')
- // The sweep reads each agent's index to find its queue, and once more to
+ // The sweep reads each agent's index to find its queue, again to confirm the
+ // entry is still queued before removing its artifacts, and once more to
// drop the finished entry.
- expect(reads).toHaveLength(2 * AGENTS.length)
+ expect(reads).toHaveLength(3 * AGENTS.length)
const afterSweep = reads.length
for (const agentRef of AGENTS.slice(1)) {
await store.retryPendingDeleteCleanups(TEAM_A_SCOPE, agentRef)
}
expect(reads.slice(afterSweep)).toEqual(AGENTS.slice(1).map(indexPathOf))
- expect(reads).toHaveLength(3 * AGENTS.length - 1)
+ expect(reads).toHaveLength(4 * AGENTS.length - 1)
vi.restoreAllMocks()
for (const agentRef of AGENTS) expect(await pendingOf(agentRef)).toEqual([])
})
@@ -1086,8 +1087,13 @@ describe('pending cleanup retry per agent', () => {
vi.restoreAllMocks()
expect(await pendingOf('agent-3')).toEqual([])
await expect(fs.access(join(tempDir, 'agent-3', 'chats', 'late-delete'))).rejects.toThrow()
- // Only agent-3 was touched: read to find its queue, then to drop the entry.
- expect(retryReads).toEqual([indexPathOf('agent-3'), indexPathOf('agent-3')])
+ // Only agent-3 was touched: read to find its queue, to confirm the entry is
+ // still queued, then to drop the entry.
+ expect(retryReads).toEqual([
+ indexPathOf('agent-3'),
+ indexPathOf('agent-3'),
+ indexPathOf('agent-3'),
+ ])
})
it('walks every agent again after the next bind', async () => {
diff --git a/desktop-app/src/chatStore.ts b/desktop-app/src/chatStore.ts
index 08eb4885a..b49eb17a8 100644
--- a/desktop-app/src/chatStore.ts
+++ b/desktop-app/src/chatStore.ts
@@ -1932,6 +1932,16 @@ export class ChatStore {
}
let artifactsRemoved = false
try {
+ // Every catalog read starts a retry pass from its own snapshot of the
+ // queue, so overlapping passes reach the same tombstone. They are
+ // serialized per chat; the later one finds it already dequeued and stops.
+ const index = await this.getIndex(agentRef)
+ const stillPending = (index.pendingChatCleanup ?? []).some(
+ existing =>
+ existing.chatId === tombstone.chatId &&
+ sameChatDeletionIdentity(existing.authorityScope, tombstone.authorityScope)
+ )
+ if (!stillPending) return true
artifactsRemoved = await this.removeChatArtifacts(
agentRef,
tombstone.chatId,