Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): fail closed on future chat indexes
Josuéc0d14222219d
diff --git a/desktop-app/src/__tests__/chatStore.test.ts b/desktop-app/src/__tests__/chatStore.test.tsindex f8671d8ae..08d131d39 100644--- a/desktop-app/src/__tests__/chatStore.test.ts+++ b/desktop-app/src/__tests__/chatStore.test.ts@@ -1586,6 +1586,60 @@ describe('onboarding', () => {// ── corrupt/missing files ────────────────────────────────────────────────────describe('corrupt/missing files', () => {+ it('fails closed without quarantining or downgrading a valid future index', async () => {+ const agentDir = join(tempDir, 'agent-1')+ const preservedChatDir = join(agentDir, 'chats', 'future-chat')+ await writePagedSnapshotDir('future-chat', preservedChatDir, [+ { id: 'future-local', role: 'user', content: 'keep me', timestamp: 1 },+ ])+ const preservedMeta = await fs.readFile(join(preservedChatDir, 'meta.json'), 'utf8')+ const preservedPage = await fs.readFile(join(preservedChatDir, 'pages', '000001.json'), 'utf8')+ const indexPath = join(agentDir, 'index.json')+ const futureIndex = JSON.stringify({+ version: 4,+ lastActiveChatId: 'future-chat',+ onboardingDismissed: false,+ chats: [+ {+ id: 'future-chat',+ title: 'Future chat',+ createdAt: '2026-01-01T00:00:00.000Z',+ updatedAt: '2026-01-01T00:00:00.000Z',+ messageCount: 1,+ },+ ],+ })+ await fs.writeFile(indexPath, futureIndex)++ await expect(store.getIndex('agent-1')).rejects.toThrow(/unsupported chat index version 4/i)+ await expect(store.createChat('agent-1', 'new-chat')).rejects.toThrow(+ /unsupported chat index version 4/i+ )+ await expect(+ store.appendMessages('agent-1', 'future-chat', [+ { id: 'new-local', role: 'assistant', content: 'do not write', timestamp: 2 },+ ])+ ).rejects.toThrow(/unsupported chat index version 4/i)+ await expect(+ store.replaceMessages('agent-1', 'future-chat', [+ {+ id: 'turn-1-user',+ role: 'user',+ content: 'server',+ timestamp: 3,+ serverTurnNumber: 1,+ },+ ])+ ).rejects.toThrow(/unsupported chat index version 4/i)++ expect(await fs.readFile(indexPath, 'utf8')).toBe(futureIndex)+ expect(await fs.readFile(join(preservedChatDir, 'meta.json'), 'utf8')).toBe(preservedMeta)+ expect(await fs.readFile(join(preservedChatDir, 'pages', '000001.json'), 'utf8')).toBe(+ preservedPage+ )+ await expect(fs.access(agentPath('.corrupt'))).rejects.toMatchObject({ code: 'ENOENT' })+ })+it('quarantines a torn index once and preserves paged transcripts while writes recover', async () => {const originalMessage: ChatMessage = {id: 'local-only-message',diff --git a/desktop-app/src/chatStore.ts b/desktop-app/src/chatStore.tsindex eb32e35ee..125d0319a 100644--- a/desktop-app/src/chatStore.ts+++ b/desktop-app/src/chatStore.ts@@ -14,6 +14,7 @@ import type {// Keep the catalog at v2 while paged transcripts are dual-readable by the// pre-paging desktop build. The page layout has its own versioned metadata.const INDEX_VERSION = 2+const MAX_READABLE_INDEX_VERSION = 3const PAGED_CHAT_VERSION = 1const DEFAULT_PAGE_SIZE = 100const MAX_COMPATIBILITY_WINDOWS = 100@@ -154,6 +155,13 @@ function parseChatIndex(raw: string): ChatIndex {throw new Error('Invalid chat index')}const candidate = parsed as Record<string, unknown>+ if (+ typeof candidate.version === 'number' &&+ Number.isSafeInteger(candidate.version) &&+ candidate.version > MAX_READABLE_INDEX_VERSION+ ) {+ throw new UnsupportedChatIndexVersionError(candidate.version)+ }if ((candidate.version !== 2 && candidate.version !== 3) ||(candidate.lastActiveChatId !== null && typeof candidate.lastActiveChatId !== 'string') ||@@ -182,6 +190,13 @@ function parseChatIndex(raw: string): ChatIndex {return { ...candidate, version: INDEX_VERSION } as unknown as ChatIndex}+class UnsupportedChatIndexVersionError extends Error {+ constructor(version: number) {+ super(`Unsupported chat index version ${version}`)+ this.name = 'UnsupportedChatIndexVersionError'+ }+}+/*** Reject any path-component (agentRef / chatId) that could escape the store* root via `path.join`. agentRefs are slugs and chatIds are UUIDs in practice,@@ -1436,6 +1451,7 @@ export class ChatStore {try {return parseChatIndex(raw)} catch (error) {+ if (error instanceof UnsupportedChatIndexVersionError) throw 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;@@ -1623,6 +1639,7 @@ export class ChatStore {options: { preserveExistingTotals?: boolean } = {}): Promise<void> {return this.serializeChat(agentRef, chatId, async () => {+ await this.getIndex(agentRef)const indexedCount = options.preserveExistingTotals? await this.indexedMessageCount(agentRef, chatId): undefined@@ -1655,6 +1672,7 @@ export class ChatStore {options: ReplaceChatMessagesOptions = {}): Promise<void> {await this.serializeChat(agentRef, chatId, async () => {+ await this.getIndex(agentRef)const existingMeta = await this.readOrMigratePagedChatUnlocked(agentRef, chatId)const existingMessages = existingMeta? await this.readMessagesFromPagedMeta(agentRef, chatId, existingMeta)@@ -1695,6 +1713,7 @@ export class ChatStore {newMessages: ChatMessage[]): Promise<void> {return this.serializeChat(agentRef, chatId, async () => {+ await this.getIndex(agentRef)const existing =(await this.readOrMigratePagedChatUnlocked(agentRef, chatId)) ??(await this.writePagedChatUnlocked(agentRef, chatId, []))