Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

v2.5.0 fix (CRITICAL): chats + memories wiped on every restart — JSON-wrap createSafeStorage

purplea69a434a6247
diff --git a/src/lib/__tests__/storage-quota.test.ts b/src/lib/__tests__/storage-quota.test.ts
index bf1038e4..4cd9d384 100644
--- a/src/lib/__tests__/storage-quota.test.ts
+++ b/src/lib/__tests__/storage-quota.test.ts
@@ -1,4 +1,5 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
+import { createJSONStorage } from 'zustand/middleware'
import { getStorageUsage, createSafeStorage } from '../storage-quota'
// Mock localStorage for tests
@@ -397,6 +398,32 @@ describe('storage-quota', () => {
expect(ids).not.toContain('m-0')
})
+ // ─── REGRESSION (FIX-3): the storage MUST be JSON-wrapped for zustand v5 ───
+ // The stores configure `storage: createJSONStorage(() => createSafeStorage())`.
+ // Earlier they passed the raw StateStorage, so zustand v5 handed the
+ // {state,version} OBJECT to setItem → localStorage.setItem(name, object) →
+ // "[object Object]" → chat-conversations + memory never hydrated → wiped on
+ // every restart. This asserts the wrapped shape the stores use round-trips an
+ // object to VALID JSON (and back), which the raw form cannot.
+ it('createJSONStorage(() => createSafeStorage()) round-trips an object as valid JSON (not "[object Object]")', () => {
+ const mock = createMockStorage({})
+ Object.defineProperty(globalThis, 'localStorage', {
+ value: mock, writable: true, configurable: true,
+ })
+ const wrapped = createJSONStorage(() => createSafeStorage())!
+ const value = { state: { conversations: [{ id: 'c1', title: 'hi' }] }, version: 0 }
+ wrapped.setItem('chat-conversations', value as any)
+
+ const raw = globalThis.localStorage.getItem('chat-conversations')
+ expect(raw).not.toBe('[object Object]')
+ // Must be parseable JSON — the bug stored "[object Object]" which throws here.
+ const parsed = JSON.parse(raw as string)
+ expect(parsed.state.conversations[0].id).toBe('c1')
+ // And getItem hydrates back to the original object (what zustand needs).
+ const readBack = wrapped.getItem('chat-conversations') as any
+ expect(readBack?.state?.conversations?.[0]?.title).toBe('hi')
+ })
+
it('dispatches lu:storage-quota-exceeded once when pruning cannot free space', () => {
// Nothing prunable (no chat-conversations, no memory store), so both
// prune tiers no-op and the write is genuinely lost — must signal UI.
diff --git a/src/stores/chatStore.ts b/src/stores/chatStore.ts
index 8c834349..f871d533 100644
--- a/src/stores/chatStore.ts
+++ b/src/stores/chatStore.ts
@@ -1,5 +1,5 @@
import { create } from 'zustand'
-import { persist } from 'zustand/middleware'
+import { persist, createJSONStorage } from 'zustand/middleware'
import { v4 as uuid } from 'uuid'
import type { Conversation, Message } from '../types/chat'
import type { AgentBlock } from '../types/agent-mode'
@@ -208,7 +208,12 @@ export const useChatStore = create<ChatState>()(
}),
{
name: 'chat-conversations',
- storage: createSafeStorage(),
+ // zustand v5: `storage` expects a PersistStorage (object-based). createSafeStorage
+ // is a STRING-based StateStorage, so it MUST be wrapped in createJSONStorage.
+ // Passing it raw made zustand hand the {state,version} OBJECT straight to
+ // setItem → localStorage.setItem(name, object) → "[object Object]" → chats
+ // never hydrated and were wiped on every restart (v2.5.0 regression).
+ storage: createJSONStorage(() => createSafeStorage()),
// Phase 1 (v2.4.0) — rehydrate legacy singular `toolCall` into `toolCalls[]`.
// Persisted shape is whatever was last written; migration runs on every load
// and is idempotent, so version bumps are not required.
diff --git a/src/stores/memoryStore.ts b/src/stores/memoryStore.ts
index ebfc1572..bd15d9e2 100644
--- a/src/stores/memoryStore.ts
+++ b/src/stores/memoryStore.ts
@@ -1,5 +1,5 @@
import { create } from 'zustand'
-import { persist } from 'zustand/middleware'
+import { persist, createJSONStorage } from 'zustand/middleware'
import { v4 as uuid } from 'uuid'
import type { MemoryEntry, MemoryCategory, MemoryFile, MemoryType, MemorySettings } from '../types/agent-mode'
import { MEMORY_MIGRATION_MAP, MEMORY_BUDGET_TIERS } from '../types/agent-mode'
@@ -674,7 +674,11 @@ export const useMemoryStore = create<MemoryState>()(
// bump exists only to run migrateV2toV3 so the shape is explicit and
// future migrations have a clean baseline.
version: 3,
- storage: createSafeStorage(),
+ // zustand v5: wrap the STRING-based createSafeStorage in createJSONStorage,
+ // else zustand passes the {state,version} object to setItem → "[object Object]"
+ // → memories never hydrate and are wiped on restart (v2.5.0 regression). Same
+ // root cause + fix as chatStore.
+ storage: createJSONStorage(() => createSafeStorage()),
migrate: (persistedState, version) => {
let state: any = persistedState
if (version < 2) {