Skip to content

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

TypeScript

perf(desktop): return chat:getIndex without waiting for deleted-chat cleanup

Alfredo Lopez426eb72dace4
diff --git a/desktop-app/src/__tests__/chatGetIndex.backgroundCleanup.test.ts b/desktop-app/src/__tests__/chatGetIndex.backgroundCleanup.test.ts
new file mode 100644
index 000000000..556651ba2
--- /dev/null
+++ b/desktop-app/src/__tests__/chatGetIndex.backgroundCleanup.test.ts
@@ -0,0 +1,136 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { promises as fs } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { deletedChatIdsForScope } from '../../ui/src/hooks/domain/useChatListController'
+import { runChatGetIndex } from '../chatGetIndexHandler.js'
+import { ChatStore } from '../chatStore.js'
+import type { ChatIndex, ChatMetadata } from '../types.js'
+
+const authorityScope = { environmentKey: 'env-a', userId: 'user-a', teamId: 'team-a' }
+const agentRef = 'agent-1'
+const deletedChatId = 'deleted-chat'
+const tombstone = { chatId: deletedChatId, authorityScope }
+let tempDir: string
+let artifactPath: string
+let store: ChatStore
+
+beforeEach(async () => {
+ tempDir = await fs.mkdtemp(join(tmpdir(), 'chat-get-index-background-'))
+ artifactPath = join(tempDir, agentRef, 'chats', deletedChatId)
+ store = new ChatStore(tempDir)
+ store.setAuthorityScope(authorityScope)
+})
+
+afterEach(async () => {
+ vi.restoreAllMocks()
+ await fs.rm(tempDir, { recursive: true, force: true })
+})
+
+async function queuePendingCleanup(): Promise<ChatMetadata> {
+ await store.createChat(agentRef, 'kept-chat')
+ await store.createChat(agentRef, deletedChatId)
+ await store.saveMessages(agentRef, deletedChatId, [
+ { id: 'message-1', role: 'user', content: 'Deleted transcript', timestamp: 1 },
+ ])
+ const originalIndex = await store.getIndex(agentRef)
+ const deletedChat = originalIndex.chats.find(chat => chat.id === deletedChatId)!
+ const remove = fs.rm.bind(fs)
+ const denial = Object.assign(new Error('Artifact removal denied'), { code: 'EACCES' })
+ const rm = vi.spyOn(fs, 'rm').mockImplementation(async (...args) => {
+ if (String(args[0]) === artifactPath) throw denial
+ return remove(...args)
+ })
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
+ try {
+ await expect(store.deleteChat(agentRef, deletedChatId, authorityScope)).resolves.toEqual({
+ cleanupPending: true,
+ })
+ expect((await store.getIndex(agentRef)).pendingChatCleanup).toEqual([tombstone])
+ await expect(fs.access(artifactPath)).resolves.toBeUndefined()
+ } finally {
+ rm.mockRestore()
+ warn.mockRestore()
+ }
+ return deletedChat
+}
+
+async function readWhileArtifactRemovalIsBlocked(inspect: (index: ChatIndex) => void) {
+ let release!: () => void
+ let removalStarted = false
+ const blocked = new Promise<void>(resolve => {
+ release = resolve
+ })
+ const remove = fs.rm.bind(fs)
+ vi.spyOn(fs, 'rm').mockImplementation(async (...args) => {
+ if (String(args[0]) === artifactPath) {
+ removalStarted = true
+ await blocked
+ }
+ return remove(...args)
+ })
+ const retry = vi.spyOn(store, 'retryPendingDeleteCleanups')
+ const warnCleanupFailure = vi.fn()
+ let returnedIndex: ChatIndex | undefined
+ const read = runChatGetIndex(
+ {
+ store,
+ getChatDeletionFenceAuthority: () => ({ authorityScope }),
+ warnCleanupFailure,
+ },
+ agentRef
+ ).then(index => {
+ returnedIndex = index
+ return index
+ })
+ try {
+ expect(retry).toHaveBeenCalledTimes(1)
+ expect(retry).toHaveBeenCalledWith(authorityScope, agentRef)
+ await vi.waitFor(() => {
+ expect(removalStarted).toBe(true)
+ expect(returnedIndex).toBeDefined()
+ })
+ await expect(fs.access(artifactPath)).resolves.toBeUndefined()
+ const index = await read
+ expect(index.pendingChatCleanup).toEqual([tombstone])
+ inspect(index)
+ } finally {
+ release()
+ await Promise.allSettled([read, ...retry.mock.results.map(result => result.value)])
+ }
+ await expect(fs.access(artifactPath)).rejects.toMatchObject({ code: 'ENOENT' })
+ const finishedIndex = await store.getIndex(agentRef)
+ expect(finishedIndex.pendingChatCleanup).toEqual([])
+ expect(finishedIndex.deletedChatTombstones).toEqual([tombstone])
+ expect(warnCleanupFailure).not.toHaveBeenCalled()
+}
+
+describe('runChatGetIndex background artifact cleanup', () => {
+ it('returns the real index before artifact removal, then removes the artifact and clears its queue', async () => {
+ await queuePendingCleanup()
+
+ await readWhileArtifactRemovalIsBlocked(index => {
+ expect(index.chats.map(chat => chat.id)).toEqual(['kept-chat'])
+ })
+ })
+
+ it('keeps a pending deletion hidden by the renderer tombstone filter before cleanup finishes', async () => {
+ const staleChat = await queuePendingCleanup()
+ const pendingIndex = await store.getIndex(agentRef)
+ // Include a stale catalog row so visibility depends on the durable tombstone,
+ // rather than passing merely because deleteChat already removed the row.
+ await fs.writeFile(
+ join(tempDir, agentRef, 'index.json'),
+ JSON.stringify({ ...pendingIndex, chats: [...pendingIndex.chats, staleChat] })
+ )
+
+ await readWhileArtifactRemovalIsBlocked(index => {
+ expect(index.chats.map(chat => chat.id)).toEqual(['kept-chat', deletedChatId])
+ expect(index.deletedChatTombstones).toEqual([tombstone])
+ const deleted = new Set(deletedChatIdsForScope(index, authorityScope))
+ expect([...deleted]).toEqual([deletedChatId])
+ const visible = index.chats.filter(chat => !deleted.has(chat.id))
+ expect(visible.map(chat => chat.id)).toEqual(['kept-chat'])
+ })
+ })
+})
diff --git a/desktop-app/src/__tests__/chatGetIndexHandler.test.ts b/desktop-app/src/__tests__/chatGetIndexHandler.test.ts
index 8f1380e60..9cac45e66 100644
--- a/desktop-app/src/__tests__/chatGetIndexHandler.test.ts
+++ b/desktop-app/src/__tests__/chatGetIndexHandler.test.ts
@@ -23,7 +23,7 @@ const index: ChatIndex = {
}
describe('runChatGetIndex', () => {
- it('awaits cleanup before reading the index, preserving the IPC baseline order', async () => {
+ it('returns the index while the cleanup started with its authority and agent is still pending', async () => {
const order: string[] = []
let release!: () => void
const cleanup = new Promise<void>(resolve => {
@@ -48,14 +48,19 @@ describe('runChatGetIndex', () => {
warnCleanupFailure: vi.fn(),
}
const result = runChatGetIndex(deps, 'agent-1')
- expect(order).toEqual(['authority', 'cleanup:start'])
- expect(deps.store.retryPendingDeleteCleanups).toHaveBeenCalledWith(authorityScope, 'agent-1')
- expect(deps.store.getIndex).toHaveBeenCalledTimes(0)
- release()
- expect(await result).toBe(index)
- expect(order).toEqual(['authority', 'cleanup:start', 'cleanup:end', 'read'])
- expect(deps.store.getIndex).toHaveBeenCalledWith('agent-1')
- expect(deps.warnCleanupFailure).toHaveBeenCalledTimes(0)
+ try {
+ expect(order).toEqual(['authority', 'cleanup:start', 'read'])
+ expect(deps.store.retryPendingDeleteCleanups).toHaveBeenCalledWith(authorityScope, 'agent-1')
+ expect(await result).toBe(index)
+ expect(order).toEqual(['authority', 'cleanup:start', 'read'])
+ expect(deps.store.getIndex).toHaveBeenCalledWith('agent-1')
+ expect(deps.warnCleanupFailure).toHaveBeenCalledTimes(0)
+ } finally {
+ release()
+ await deps.store.retryPendingDeleteCleanups.mock.results[0]?.value
+ await result
+ }
+ expect(order).toEqual(['authority', 'cleanup:start', 'read', 'cleanup:end'])
})
it.each(['authority', 'cleanup'])(
@@ -78,28 +83,77 @@ describe('runChatGetIndex', () => {
expect(await runChatGetIndex(deps, 'agent-1')).toBe(index)
expect(deps.getChatDeletionFenceAuthority).toHaveBeenCalledTimes(1)
expect(deps.store.getIndex).toHaveBeenCalledTimes(1)
+ expect(deps.warnCleanupFailure).toHaveBeenCalledTimes(1)
expect(deps.warnCleanupFailure).toHaveBeenCalledWith(failure)
}
)
- it('propagates index read failures after cleanup has completed', async () => {
+ it('propagates index read failures while cleanup is still pending', async () => {
const failure = new Error('Read failed')
- const cleanup = vi.fn(async () => {})
+ let release!: () => void
+ const pending = new Promise<void>(resolve => {
+ release = resolve
+ })
+ const cleanup = vi.fn(() => pending)
const read = vi.fn(async () => {
throw failure
})
- await expect(
- runChatGetIndex(
- {
- store: { retryPendingDeleteCleanups: cleanup, getIndex: read },
- getChatDeletionFenceAuthority: () => ({ authorityScope }),
- warnCleanupFailure: vi.fn(),
- },
- 'agent-1'
- )
- ).rejects.toBe(failure)
- expect(cleanup).toHaveBeenCalledTimes(1)
- expect(read).toHaveBeenCalledTimes(1)
+ const result = runChatGetIndex(
+ {
+ store: { retryPendingDeleteCleanups: cleanup, getIndex: read },
+ getChatDeletionFenceAuthority: () => ({ authorityScope }),
+ warnCleanupFailure: vi.fn(),
+ },
+ 'agent-1'
+ )
+ const assertion = expect(result).rejects.toBe(failure)
+ try {
+ expect(cleanup).toHaveBeenCalledTimes(1)
+ expect(read).toHaveBeenCalledTimes(1)
+ await assertion
+ } finally {
+ release()
+ await assertion
+ }
+ })
+
+ it('reports a later background rejection exactly once without an unhandled rejection', async () => {
+ const failure = new Error('Background cleanup failed')
+ let release!: () => void
+ let reject!: (error: Error) => void
+ const cleanup = new Promise<void>((resolve, fail) => {
+ release = resolve
+ reject = fail
+ })
+ const retry = vi.fn(() => cleanup)
+ const read = vi.fn(async () => index)
+ const warnCleanupFailure = vi.fn()
+ const unhandled: unknown[] = []
+ const onUnhandled = (error: unknown) => unhandled.push(error)
+ process.on('unhandledRejection', onUnhandled)
+ const result = runChatGetIndex(
+ {
+ store: { retryPendingDeleteCleanups: retry, getIndex: read },
+ getChatDeletionFenceAuthority: () => ({ authorityScope }),
+ warnCleanupFailure,
+ },
+ 'agent-1'
+ )
+ try {
+ expect(retry).toHaveBeenCalledWith(authorityScope, 'agent-1')
+ expect(read).toHaveBeenCalledWith('agent-1')
+ expect(await result).toBe(index)
+ reject(failure)
+ // Give Node a turn to emit an unhandled rejection if the catch is missing.
+ await new Promise<void>(resolve => setImmediate(resolve))
+ expect(warnCleanupFailure).toHaveBeenCalledTimes(1)
+ expect(warnCleanupFailure).toHaveBeenCalledWith(failure)
+ expect(unhandled).toEqual([])
+ } finally {
+ release()
+ await result
+ process.removeListener('unhandledRejection', onUnhandled)
+ }
})
it('can be compiled and imported by a plain Node benchmark without Electron', () => {
@@ -116,22 +170,49 @@ describe('runChatGetIndex', () => {
[
'-e',
`
+ const assert = require('node:assert/strict');
const { runChatGetIndex } = require(process.argv[1]);
const order = [];
- runChatGetIndex({
- getChatDeletionFenceAuthority: () => ({ authorityScope: {} }),
- store: {
- retryPendingDeleteCleanups: async () => { order.push('cleanup'); },
- getIndex: async () => { order.push('read'); return { chats: [1] }; }
- },
- warnCleanupFailure: error => { throw error; }
- }, 'agent-1').then(index => process.stdout.write(JSON.stringify({ order, count: index.chats.length })));
+ let release;
+ const pending = new Promise(resolve => { release = resolve; });
+ let cleanup;
+ async function main() {
+ const result = runChatGetIndex({
+ getChatDeletionFenceAuthority: () => ({ authorityScope: {} }),
+ store: {
+ retryPendingDeleteCleanups: () => {
+ order.push('cleanup:start');
+ cleanup = pending.then(() => { order.push('cleanup:end'); });
+ return cleanup;
+ },
+ getIndex: async () => { order.push('read'); return { chats: [1] }; }
+ },
+ warnCleanupFailure: error => { throw error; }
+ }, 'agent-1');
+ let index;
+ let beforeRelease;
+ try {
+ assert.deepEqual(order, ['cleanup:start', 'read']);
+ index = await result;
+ beforeRelease = [...order];
+ } finally {
+ release();
+ await cleanup;
+ await result;
+ }
+ process.stdout.write(JSON.stringify({ beforeRelease, afterRelease: order, count: index.chats.length }));
+ }
+ main().catch(error => { console.error(error); process.exitCode = 1; });
`,
modulePath,
],
{ cwd: dir, encoding: 'utf8' }
)
- expect(JSON.parse(output)).toEqual({ order: ['cleanup', 'read'], count: 1 })
+ expect(JSON.parse(output)).toEqual({
+ beforeRelease: ['cleanup:start', 'read'],
+ afterRelease: ['cleanup:start', 'read', 'cleanup:end'],
+ count: 1,
+ })
expect(compiled).not.toContain('electron')
} finally {
fs.rmSync(dir, { recursive: true, force: true })
diff --git a/desktop-app/src/__tests__/ipc.chatGetIndex.test.ts b/desktop-app/src/__tests__/ipc.chatGetIndex.test.ts
index c72bf81c0..535d26fe4 100644
--- a/desktop-app/src/__tests__/ipc.chatGetIndex.test.ts
+++ b/desktop-app/src/__tests__/ipc.chatGetIndex.test.ts
@@ -1,10 +1,10 @@
/**
* `chat:getIndex` must keep returning the index WITH its tombstones when the
- * deleted-chat cleanup retry that precedes it fails (scope changed mid-flight,
+ * background deleted-chat cleanup retry fails (scope changed mid-flight,
* no session, unreadable chats directory). The renderer filters deleted chats
* with those tombstones; a rejected read leaves them visible.
*/
-import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest'
+import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { promises as fs } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
@@ -66,7 +66,20 @@ beforeAll(async () => {
await call('chat:delete', { version: 3, fence, agentRef: 'agent-1', chatId: 'deleted-chat' })
})
-afterEach(() => {
+beforeEach(async () => {
+ const binding = await import('../chatStoreBinding.js')
+ vi.spyOn(binding.requireChatStore(), 'retryPendingDeleteCleanups')
+ service.getChatDeletionFenceAuthority.mockClear()
+})
+
+async function settleCleanupRetries(): Promise<void> {
+ const binding = await import('../chatStoreBinding.js')
+ const retry = vi.mocked(binding.requireChatStore().retryPendingDeleteCleanups)
+ await Promise.allSettled(retry.mock.results.map(result => result.value))
+}
+
+afterEach(async () => {
+ await settleCleanupRetries()
authority = { authorityScope: TEAM_A, sessionGeneration: 1 }
vi.restoreAllMocks()
})
@@ -83,12 +96,16 @@ const tombstones = (index: unknown) =>
describe('chat:getIndex', () => {
it('returns the tombstones when the cleanup retry runs normally', async () => {
+ const binding = await import('../chatStoreBinding.js')
+ const retry = vi.mocked(binding.requireChatStore().retryPendingDeleteCleanups)
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
const index = await call('chat:getIndex', { agentRef: 'agent-1' })
+ await settleCleanupRetries()
expect(tombstones(index)).toEqual(['deleted-chat'])
expect(service.getChatDeletionFenceAuthority).toHaveBeenCalled()
+ expect(retry).toHaveBeenCalledWith(TEAM_A, 'agent-1')
expect(warn).not.toHaveBeenCalled()
})
@@ -101,6 +118,7 @@ describe('chat:getIndex', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
const index = await call('chat:getIndex', { agentRef: 'agent-1' })
+ await settleCleanupRetries()
// Liveness witness: the retry ran and failed, and the failure was reported.
const report = warn.mock.calls.find(entry => String(entry[0]).includes('[chat:getIndex]'))
@@ -111,13 +129,16 @@ describe('chat:getIndex', () => {
it('runs the cleanup retry without a failure report after a team switch of the same user', async () => {
authority = { authorityScope: TEAM_B, sessionGeneration: 2 }
- service.getChatDeletionFenceAuthority.mockClear()
+ const binding = await import('../chatStoreBinding.js')
+ const retry = vi.mocked(binding.requireChatStore().retryPendingDeleteCleanups)
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
const index = await call('chat:getIndex', { agentRef: 'agent-1' })
+ await settleCleanupRetries()
- // Liveness witness: the retry consulted the authority (it did run) and returned the index.
+ // Liveness witness: the retry ran with the new team's authority and returned the index.
expect(service.getChatDeletionFenceAuthority).toHaveBeenCalledTimes(1)
+ expect(retry).toHaveBeenCalledWith(TEAM_B, 'agent-1')
expect(tombstones(index)).toEqual(['deleted-chat'])
expect(warn).not.toHaveBeenCalled()
})
@@ -127,6 +148,7 @@ describe('chat:getIndex', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
const index = await call('chat:getIndex', { agentRef: 'agent-1' })
+ await settleCleanupRetries()
const report = warn.mock.calls.find(entry => String(entry[0]).includes('[chat:getIndex]'))
expect(String((report?.[1] as Error).message)).toBe('Not authenticated')
@@ -140,6 +162,7 @@ describe('chat:getIndex', () => {
const readdir = vi.spyOn(fs, 'readdir')
const index = await call('chat:getIndex', { agentRef: 'agent-1' })
+ await settleCleanupRetries()
expect(retry).toHaveBeenCalledWith(TEAM_A, 'agent-1')
expect(readdir.mock.calls.map(entry => String(entry[0]))).not.toContain(
@@ -159,12 +182,14 @@ describe('chat:getIndex', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
const index = await call('chat:getIndex', { agentRef: 'agent-1' })
+ await settleCleanupRetries()
const report = warn.mock.calls.find(entry => String(entry[0]).includes('[chat:getIndex]'))
expect(String(report?.[0])).toContain('(EACCES)')
expect(tombstones(index)).toEqual(['deleted-chat'])
// The sweep that failed stays armed: the next read walks the directory again.
await call('chat:getIndex', { agentRef: 'agent-1' })
+ await settleCleanupRetries()
const userDir = join(base, 'env-a', 'user-a')
expect(readdir.mock.calls.map(entry => String(entry[0]))).toEqual([userDir, userDir])
})
diff --git a/desktop-app/src/chatGetIndexHandler.ts b/desktop-app/src/chatGetIndexHandler.ts
index 28f5d2c36..ddb1cac50 100644
--- a/desktop-app/src/chatGetIndexHandler.ts
+++ b/desktop-app/src/chatGetIndexHandler.ts
@@ -13,13 +13,12 @@ export interface ChatGetIndexDependencies {
* chat:getIndex logic without Electron dependencies, so the chat-open benchmark
* (scripts/bench-chat-open.mjs) measures the same code the IPC handler runs.
*
- * The catalog read must never depend on the deleted-chat artifact cleanup:
- * a cleanup failure (scope changed mid-flight, no session, an unreadable
- * chats directory) would otherwise reject the read and leave the renderer
- * without the tombstones it filters deleted chats with. The failure is
- * reported and the cleanup stays queued for the next read. Only this agent's
- * queue is retried (the renderer reads every agent in a fan-out); the store
- * walks every agent once per bind on its own.
+ * Start deleted-chat artifact cleanup in the background and return the index
+ * without waiting for it. The index keeps the durable tombstones the renderer
+ * filters deleted chats with while their artifacts are still being removed.
+ * Attach the rejection handler immediately; an authority or cleanup failure
+ * is reported and the cleanup stays queued for a later read. Only this agent's
+ * queue is retried, except that the store walks every agent once per bind.
*/
export async function runChatGetIndex(
deps: ChatGetIndexDependencies,
@@ -27,7 +26,9 @@ export async function runChatGetIndex(
): Promise<ChatIndex> {
try {
const authority = deps.getChatDeletionFenceAuthority()
- await deps.store.retryPendingDeleteCleanups(authority.authorityScope, agentRef)
+ void deps.store
+ .retryPendingDeleteCleanups(authority.authorityScope, agentRef)
+ .catch(deps.warnCleanupFailure)
} catch (error) {
deps.warnCleanupFailure(error)
}