Skip to content

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

TypeScript

fix(desktop): durably normalize v3 chat indexes

Josuée46b7e449238
diff --git a/desktop-app/src/__tests__/chatStoreBinding.test.ts b/desktop-app/src/__tests__/chatStoreBinding.test.ts
index 5ee2b5f76..f31c44040 100644
--- a/desktop-app/src/__tests__/chatStoreBinding.test.ts
+++ b/desktop-app/src/__tests__/chatStoreBinding.test.ts
@@ -336,7 +336,7 @@ describe('chatStoreBinding', () => {
expect(preserved.version).toBe(2)
})
- it('normalizes an earlier paging v3 index back to downgrade-compatible v2', async () => {
+ it('normalizes a v3 index through the store durable writer on ordinary RMW', async () => {
const v3Index = JSON.stringify({
version: 3,
chats: [{ id: 'keep', title: 'Keep', createdAt: 'x', updatedAt: 'x', messageCount: 1 }],
@@ -350,49 +350,41 @@ describe('chatStoreBinding', () => {
messages: [{ id: 'm1', role: 'user', content: 'keep me', timestamp: 1 }],
}),
})
+ const indexPath = path.join(tmpBase, ENV_A, 'user-v3', 'agent-x', 'index.json')
+ const originalOpen = fs.open.bind(fs)
+ const syncedPaths: string[] = []
+ vi.spyOn(fs, 'open').mockImplementation(async (...args) => {
+ const filePath = String(args[0])
+ const handle = await originalOpen(...(args as Parameters<typeof fs.open>))
+ return {
+ sync: async () => {
+ syncedPaths.push(filePath)
+ await handle.sync()
+ },
+ close: () => handle.close(),
+ } as Awaited<ReturnType<typeof fs.open>>
+ })
await bindChatStoreForUser('user-v3', ENV_A)
- const normalized = JSON.parse(
- await fs.readFile(path.join(tmpBase, ENV_A, 'user-v3', 'agent-x', 'index.json'), 'utf-8')
- ) as { version: number }
- expect(normalized.version).toBe(2)
- expect(await exists(path.join(tmpBase, ENV_A, 'user-v3', 'agent-x', 'keep.json'))).toBe(true)
- })
+ expect(JSON.parse(await fs.readFile(indexPath, 'utf-8')).version).toBe(3)
+ await requireChatStore().renameChat('agent-x', 'keep', 'Changed')
- it('preserves a valid v3 cache when its index normalization cannot be renamed', async () => {
- const v3Index = JSON.stringify({
- version: 3,
- chats: [{ id: 'keep', title: 'Keep', createdAt: 'x', updatedAt: 'x', messageCount: 1 }],
- lastActiveChatId: 'keep',
- onboardingDismissed: false,
- })
- await seedAgentDir(ENV_A, 'user-v3-failure', 'agent-x', v3Index, {
- 'keep.json': '{"version":2,"chatId":"keep","messages":[]}',
- })
- const originalRename = fs.rename.bind(fs)
- vi.spyOn(fs, 'rename').mockImplementation(async (from, to) => {
- if (String(from).endsWith('index.json.v2.tmp')) {
- const error = new Error('read only') as NodeJS.ErrnoException
- error.code = 'EACCES'
- throw error
- }
- return originalRename(from, to)
+ const normalized = JSON.parse(await fs.readFile(indexPath, 'utf-8')) as {
+ version: number
+ chats: Array<{ id: string; title: string }>
+ }
+ expect(normalized.version).toBe(2)
+ expect(normalized.chats).toContainEqual({
+ id: 'keep',
+ title: 'Changed',
+ createdAt: 'x',
+ updatedAt: expect.any(String),
+ messageCount: 1,
})
-
- await bindChatStoreForUser('user-v3-failure', ENV_A)
-
- expect(
- await exists(path.join(tmpBase, ENV_A, 'user-v3-failure', 'agent-x', 'keep.json'))
- ).toBe(true)
- expect(
- JSON.parse(
- await fs.readFile(
- path.join(tmpBase, ENV_A, 'user-v3-failure', 'agent-x', 'index.json'),
- 'utf-8'
- )
- ).version
- ).toBe(3)
+ expect(syncedPaths).toContain(`${indexPath}.tmp`)
+ expect(syncedPaths).toContain(path.dirname(indexPath))
+ expect(await exists(path.join(tmpBase, ENV_A, 'user-v3', 'agent-x', 'keep.json'))).toBe(true)
})
it('does not delete a current cache after a transient index read failure', async () => {
diff --git a/desktop-app/src/chatStoreBinding.ts b/desktop-app/src/chatStoreBinding.ts
index 8ac5d15f0..0a7b61d26 100644
--- a/desktop-app/src/chatStoreBinding.ts
+++ b/desktop-app/src/chatStoreBinding.ts
@@ -244,24 +244,11 @@ async function maybeWipeLegacyCache(userDir: string): Promise<void> {
const raw = await fs.readFile(indexPath, 'utf-8')
const parsed = JSON.parse(raw) as { version?: number }
if (parsed.version === PREVIOUS_PAGED_INDEX_VERSION) {
- // A parsed v3 catalog is valid even if the compatibility-marker rewrite
- // fails. Keep the cache and retry the atomic normalization next launch.
+ // A parsed v3 catalog is valid. Leave it in place during binding so the
+ // next ordinary ChatStore RMW normalizes it through the same crash-durable
+ // temp-write, file-fsync, rename, and directory-fsync path as every other
+ // index update.
isLegacy = false
- const migratedIndexPath = `${indexPath}.v2.tmp`
- try {
- await fs.writeFile(
- migratedIndexPath,
- JSON.stringify({ ...parsed, version: SCHEMA_VERSION }, null, 2),
- { mode: 0o600 }
- )
- await fs.rename(migratedIndexPath, indexPath)
- } catch (error) {
- await fs.rm(migratedIndexPath, { force: true }).catch(() => undefined)
- console.warn(
- `[chatStore] Failed to normalize v3 index for "${entry.name}"; retrying next launch`,
- error
- )
- }
} else if (LEGACY_INDEX_VERSIONS.has(parsed.version ?? Number.NaN)) {
isLegacy = true
} else if (parsed.version !== SCHEMA_VERSION) {