Skip to content

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

TypeScript

fix(desktop): write the tombstone sidecar before the chat index

Alfredo Lopez0432fdbcdd33
diff --git a/desktop-app/src/__tests__/chatStore.test.ts b/desktop-app/src/__tests__/chatStore.test.ts
index 8fdd16907..8b89329cd 100644
--- a/desktop-app/src/__tests__/chatStore.test.ts
+++ b/desktop-app/src/__tests__/chatStore.test.ts
@@ -801,6 +801,41 @@ describe('tombstone durability', () => {
const unreadable = warn.mock.calls.filter(call => String(call[0]).includes('tombstone sidecar'))
expect(unreadable).toHaveLength(1)
})
+
+ it('commits nothing when the sidecar write of a delete fails, and a retry commits both', async () => {
+ await store.createChat('agent-1', 'a')
+ await store.createChat('agent-1', 'b')
+ const originalWriteFile = fs.writeFile.bind(fs)
+ const written: string[] = []
+ const writeFile = vi.spyOn(fs, 'writeFile').mockImplementation(async (...args) => {
+ const filePath = String(args[0])
+ written.push(filePath)
+ if (filePath.endsWith('.tombstones.json.tmp')) {
+ throw Object.assign(new Error('no space left on device'), { code: 'ENOSPC' })
+ }
+ return originalWriteFile(...(args as Parameters<typeof fs.writeFile>))
+ })
+
+ await expect(store.deleteChat('agent-1', 'a', TEAM_A_SCOPE)).rejects.toMatchObject({
+ code: 'ENOSPC',
+ })
+
+ // Witness: the rejected save reached the sidecar write.
+ expect(written.some(path => path.endsWith('.tombstones.json.tmp'))).toBe(true)
+ // The rejected delete left the catalog as it was.
+ expect((await store.listChats('agent-1')).map(chat => chat.id)).toEqual(['a', 'b'])
+ expect(written.some(path => path.endsWith('index.json.tmp'))).toBe(false)
+
+ writeFile.mockRestore()
+ await expect(store.deleteChat('agent-1', 'a', TEAM_A_SCOPE)).resolves.toEqual({
+ cleanupPending: false,
+ })
+ const sidecar = await readJsonFile<{ deletedChatTombstones: unknown[] }>(
+ agentPath('.tombstones.json')
+ )
+ expect(sidecar.deletedChatTombstones).toEqual([tombstone('a')])
+ expect((await store.listChats('agent-1')).map(chat => chat.id)).toEqual(['b'])
+ })
})
describe('deleted-chat cleanup failure reporting', () => {
diff --git a/desktop-app/src/chatStore.ts b/desktop-app/src/chatStore.ts
index ce3ed2ba8..5f3cb81e8 100644
--- a/desktop-app/src/chatStore.ts
+++ b/desktop-app/src/chatStore.ts
@@ -1711,13 +1711,17 @@ export class ChatStore {
// Flush the temp file before rename and the containing directory afterward.
// Visibility atomicity alone does not make the catalog durable across power loss.
const target = this.indexPath(agentRef)
- await this.writeJsonAtomic(target, normalized, 2)
await this.saveTombstoneSidecar(agentRef, normalized)
+ await this.writeJsonAtomic(target, normalized, 2)
}
/**
- * Mirrors the tombstone lists into the sidecar (atomically, after the index)
- * whenever they change. Skipped for an agent that never had a tombstone.
+ * Mirrors the tombstone lists into the sidecar (atomically) whenever they
+ * change. Skipped for an agent that never had a tombstone. It is written
+ * before the index, so a rejected save has committed no catalog change. A
+ * crash between the two writes leaves the sidecar ahead of the index; an
+ * index read without dropped tombstone entries is authoritative and ignores
+ * it, and the next save realigns the sidecar with the index.
*/
private async saveTombstoneSidecar(agentRef: string, index: ChatIndex): Promise<void> {
const sidecar: TombstoneSidecar = {