Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): recover corrupt chat indexes
Josué2b756341b2bf
diff --git a/desktop-app/src/__tests__/chatStore.test.ts b/desktop-app/src/__tests__/chatStore.test.tsindex d37b23843..79f6aff21 100644--- a/desktop-app/src/__tests__/chatStore.test.ts+++ b/desktop-app/src/__tests__/chatStore.test.ts@@ -1502,21 +1502,59 @@ describe('onboarding', () => {// ── corrupt/missing files ────────────────────────────────────────────────────describe('corrupt/missing files', () => {- it('fails closed and preserves a corrupt index during later mutations', async () => {+ it('quarantines a torn index once and preserves paged transcripts while writes recover', async () => {+ const originalMessage: ChatMessage = {+ id: 'local-only-message',+ role: 'user',+ content: 'keep me',+ timestamp: 1,+ preserveLocal: true,+ }+ await store.createChat('agent-1', 'kept-chat')+ await store.appendMessages('agent-1', 'kept-chat', [originalMessage])+const agentDir = join(tempDir, 'agent-1')- await fs.mkdir(agentDir, { recursive: true })const indexPath = join(agentDir, 'index.json')- const corrupt = 'NOT VALID JSON{{{'+ const corrupt = '{"version":2,"chats":[{"id":"kept-chat"'await fs.writeFile(indexPath, corrupt)- await expect(store.getIndex('agent-1')).rejects.toThrow(/Unable to read chat index/)- await expect(store.createChat('agent-1', 'new-chat')).rejects.toThrow(- /Unable to read chat index/+ await expect(store.getIndex('agent-1')).resolves.toEqual({+ version: 2,+ lastActiveChatId: null,+ onboardingDismissed: false,+ chats: [],+ })+ await expect(store.getIndex('agent-1')).resolves.toEqual({+ version: 2,+ lastActiveChatId: null,+ onboardingDismissed: false,+ chats: [],+ })+ const quarantinedIndexes = (await fs.readdir(agentPath('.corrupt'))).filter(name =>+ name.startsWith('index-'))- expect(await fs.readFile(indexPath, 'utf8')).toBe(corrupt)+ expect(quarantinedIndexes).toHaveLength(1)+ expect(await fs.readFile(agentPath('.corrupt', quarantinedIndexes[0]!), 'utf8')).toBe(corrupt)++ await expect(+ store.appendMessages('agent-1', 'kept-chat', [+ {+ id: 'new-message',+ role: 'assistant',+ content: 'still writable',+ timestamp: 2,+ },+ ])+ ).resolves.toBeUndefined()+ expect((await store.loadMessages('agent-1', 'kept-chat')).map(message => message.id)).toEqual([+ 'local-only-message',+ 'new-message',+ ])+ await expect(store.createChat('agent-1', 'new-chat')).resolves.toMatchObject({ id: 'new-chat' })+ await expect(fs.access(chatCacheDir('kept-chat'))).resolves.toBeUndefined()})- it('keeps a v3 index readable but refuses to normalize it during ordinary RMW', async () => {+ it('normalizes a readable v3 index during the next ordinary RMW', async () => {const agentDir = join(tempDir, 'agent-1')await fs.mkdir(agentDir, { recursive: true })const indexPath = join(agentDir, 'index.json')@@ -1536,14 +1574,18 @@ describe('corrupt/missing files', () => {})await fs.writeFile(indexPath, future)- expect((await store.getIndex('agent-1')).chats[0]?.id).toBe('kept')- await expect(store.renameChat('agent-1', 'kept', 'Changed')).rejects.toThrow(- /unsupported chat index version 3/- )- expect(await fs.readFile(indexPath, 'utf8')).toBe(future)+ expect(await store.getIndex('agent-1')).toMatchObject({+ version: 2,+ chats: [expect.objectContaining({ id: 'kept' })],+ })+ await expect(store.renameChat('agent-1', 'kept', 'Changed')).resolves.toBeUndefined()+ expect(await readJsonFile(indexPath)).toMatchObject({+ version: 2,+ chats: [expect.objectContaining({ id: 'kept', title: 'Changed' })],+ })})- it('rejects structurally invalid index counters before an RMW can normalize them', async () => {+ it('quarantines a structurally invalid index before rebuilding the catalog', async () => {const agentDir = join(tempDir, 'agent-1')await fs.mkdir(agentDir, { recursive: true })const indexPath = join(agentDir, 'index.json')@@ -1563,10 +1605,16 @@ describe('corrupt/missing files', () => {})await fs.writeFile(indexPath, invalid)- await expect(store.renameChat('agent-1', 'kept', 'Changed')).rejects.toThrow(- /Unable to read chat index/+ await expect(store.createChat('agent-1', 'new-chat')).resolves.toMatchObject({ id: 'new-chat' })+ const quarantinedIndexes = (await fs.readdir(agentPath('.corrupt'))).filter(name =>+ name.startsWith('index-'))- expect(await fs.readFile(indexPath, 'utf8')).toBe(invalid)+ expect(quarantinedIndexes).toHaveLength(1)+ expect(await fs.readFile(agentPath('.corrupt', quarantinedIndexes[0]!), 'utf8')).toBe(invalid)+ expect(await readJsonFile(indexPath)).toMatchObject({+ version: 2,+ chats: [expect.objectContaining({ id: 'new-chat' })],+ })})it('does not overwrite an index after a transient read failure', async () => {diff --git a/desktop-app/src/chatStore.ts b/desktop-app/src/chatStore.tsindex f51007211..43236a2c1 100644--- a/desktop-app/src/chatStore.ts+++ b/desktop-app/src/chatStore.ts@@ -179,7 +179,7 @@ function parseChatIndex(raw: string): ChatIndex {throw new Error('Invalid chat index entry')}}- return candidate as unknown as ChatIndex+ return { ...candidate, version: INDEX_VERSION } as unknown as ChatIndex}/**@@ -358,6 +358,10 @@ export class ChatStore {return join(this.agentDir(agentRef), 'index.json')}+ private corruptIndexPath(agentRef: string): string {+ return join(this.agentDir(agentRef), '.corrupt', `index-${randomUUID()}.json`)+ }+private chatFilePath(agentRef: string, chatId: string): string {assertSafeFilesystemSegment('chatId', chatId, {reservedNames: ['index', 'chats'],@@ -1424,11 +1428,25 @@ export class ChatStore {try {return parseChatIndex(raw)} catch (error) {- // Bind-time migration deliberately preserves malformed and future catalogs- // because their paged transcripts may contain local-only work. Fail closed:- // treating those bytes as an empty writable index would let the next RMW- // silently orphan the preserved chats.- throw new Error(`Unable to read chat index for ${agentRef}`, { cause: error })+ // Preserve unreadable catalog bytes for bounded recovery/diagnostics, but+ // detach them from the writable path so a torn index cannot brick every+ // chat mutation. Paged transcripts live under chats/ and remain untouched;+ // the catalog can be repopulated by the server as normal writes resume.+ const source = this.indexPath(agentRef)+ const target = this.corruptIndexPath(agentRef)+ await fs.mkdir(dirname(target), { recursive: true, mode: 0o700 })+ try {+ await fs.rename(source, target)+ await this.syncRenamedEntry(source, target)+ console.warn(`[chatStore] Quarantined unreadable chat index for "${agentRef}"`)+ } catch (renameError) {+ if (!isNotFoundError(renameError)) {+ throw new Error(`Unable to quarantine chat index for ${agentRef}`, {+ cause: renameError,+ })+ }+ }+ return emptyIndex()}}