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.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
fix(chat): a chat deleted on purpose is not offered back by the restore, on the desktop too
purple9b551614de8e
diff --git a/src/components/layout/ChatRescueHint.tsx b/src/components/layout/ChatRescueHint.tsxindex e600fda1..797f9430 100644--- a/src/components/layout/ChatRescueHint.tsx+++ b/src/components/layout/ChatRescueHint.tsx@@ -4,6 +4,7 @@ import { isTauri } from '../../api/backend'import {countRestorableChats, restoreChatsFromBackup, shouldShowRescueHint, readDismissedAt, rememberDismissed,} from '../../lib/chat-rescue'+import { installDeletionNote } from '../../lib/chat-deletion-note'/** After the restore at launch has had its turn (it may reload the window). */const LOOK_AFTER_MS = 4000@@ -15,6 +16,9 @@ const LOOK_AFTER_MS = 4000*/export function ChatRescueHint() {const importConversations = useChatStore((s) => s.importConversations)+ // A chat that leaves the list from here on was deleted on purpose: the restore does not offer it back (C46).+ // The shell mounts this once for the whole session.+ useEffect(() => { installDeletionNote() }, [])const [count, setCount] = useState(0)const [busy, setBusy] = useState(false)const [done, setDone] = useState<string | null>(null)diff --git a/src/lib/__tests__/chat-rescue.test.ts b/src/lib/__tests__/chat-rescue.test.tsindex 8456bc48..160c7270 100644--- a/src/lib/__tests__/chat-rescue.test.ts+++ b/src/lib/__tests__/chat-rescue.test.ts@@ -13,6 +13,9 @@ import { useChatStore } from '../../stores/chatStore'import type { Conversation } from '../../types/chat'import { parseImportedChats } from '../chat-export'import { chatsMissingFrom, restoreChatsFromBackup, countRestorableChats, shouldShowRescueHint, readDismissedAt, rememberDismissed } from '../chat-rescue'+// Static, like the store: a block below resets the modules, and a dynamic import would watch another store.+import { __resetDeletionNoteForTests, installDeletionNote } from '../chat-deletion-note'+import { readDeletedChats } from '../chat-deletions'/*** Conversations in the shape 3.0.8 persisted them (src/types/chat.ts at@@ -225,3 +228,71 @@ describe('the launch check sees what hydration sees', () => {expect(await hasReadableItem('chat-conversations')).toBe(true)})})++// ── Deleted is not lost (C46, 3.1.0) ───────────────────────────────────────++describe('a chat deleted on purpose is not offered back, a lost one still is', () => {+ const savedLs = Reflect.get(globalThis, 'localStorage')++ beforeEach(() => {+ backend.mockReset()+ Object.defineProperty(globalThis, 'localStorage', { value: fakeLocalStorage(), configurable: true, writable: true })+ __resetDeletionNoteForTests()+ })++ afterEach(() => {+ __resetDeletionNoteForTests()+ Object.defineProperty(globalThis, 'localStorage', { value: savedLs, configurable: true, writable: true })+ useChatStore.setState({ conversations: [], activeConversationId: null })+ })++ it('deleted ≠ lost: the deleted chat stays away, the lost one comes back', async () => {+ // The store came up with a and b; c was lost before this start (it never was in this list).+ useChatStore.setState({ conversations: [chat308('a', 100), chat308('b', 200)] })+ installDeletionNote()+ expect(useChatStore.persist.hasHydrated()).toBe(true)+ // The user deletes b. The backup archive keeps it, together with the lost c.+ useChatStore.getState().deleteConversation('b')+ backupAnswer([chat308('a', 100), chat308('b', 200), chat308('c', 300)])++ const live = () => useChatStore.getState().conversations+ expect(await countRestorableChats(live)).toBe(1)+ expect(shouldShowRescueHint(live().length, 1, null)).toBe(false)+ expect(await restoreChatsFromBackup(live, useChatStore.getState().importConversations)).toBe(1)+ expect(live().map((c) => c.id).sort()).toEqual(['a', 'c'])+ })++ it('"Delete all chats" does not come back as a hint at the next start', async () => {+ useChatStore.setState({ conversations: [chat308('a', 1), chat308('b', 2), chat308('c', 3)] })+ installDeletionNote()+ for (const id of ['a', 'b', 'c']) useChatStore.getState().deleteConversation(id)+ // The next start: an empty list, the archive holds all three.+ backupAnswer([chat308('a', 1), chat308('b', 2), chat308('c', 3)])+ const restorable = await countRestorableChats(() => useChatStore.getState().conversations)+ expect(restorable).toBe(0)+ expect(shouldShowRescueHint(0, restorable, null)).toBe(false)+ })++ it('the check the other way: a list that came up empty is a loss, nothing is noted, every chat is offered', async () => {+ // perrt: hydration brought nothing. That is the start value, not a deletion.+ useChatStore.setState({ conversations: [] })+ installDeletionNote()+ // He starts a new chat after the loss; that is no deletion either.+ useChatStore.setState({ conversations: [chat308('new', 900)] })+ expect(readDeletedChats().size).toBe(0)+ backupAnswer([chat308('a', 100), chat308('b', 200), chat308('c', 300)])+ const live = () => useChatStore.getState().conversations+ expect(await countRestorableChats(live)).toBe(3)+ expect(shouldShowRescueHint(live().length, 3, null)).toBe(true)+ expect(await restoreChatsFromBackup(live, useChatStore.getState().importConversations)).toBe(3)+ })++ it('a deleted chat that comes back (a file picked by hand) leaves the note', async () => {+ useChatStore.setState({ conversations: [chat308('a', 1)] })+ installDeletionNote()+ useChatStore.getState().deleteConversation('a')+ expect([...readDeletedChats()]).toEqual(['a'])+ useChatStore.getState().importConversations([chat308('a', 1)], 'merge')+ expect(readDeletedChats().size).toBe(0)+ })+})diff --git a/src/lib/chat-deletion-note.ts b/src/lib/chat-deletion-note.tsnew file mode 100644index 00000000..d670ece3--- /dev/null+++ b/src/lib/chat-deletion-note.ts@@ -0,0 +1,38 @@+/**+ * Watches the chat store and notes every chat that leaves the list as deleted+ * on purpose (lib/chat-deletions.ts). Installed once per app: by the chat list+ * on the phone, by the restore hint in the shell on the desktop.+ */+import { useChatStore } from '../stores/chatStore'+import { chatIdsChanged, noteChatsLeft } from './chat-deletions'++let installed = false+let stop: () => void = () => {}++/**+ * From the moment the chat store has hydrated. Before that the list is the+ * empty start value, and what hydration does not bring is exactly the loss+ * the restore is for.+ */+export function installDeletionNote(): void {+ if (installed) return+ installed = true+ const start = () => {+ let prev = useChatStore.getState().conversations+ stop = useChatStore.subscribe((state) => {+ if (state.conversations === prev) return+ const { gone, back } = chatIdsChanged(prev, state.conversations)+ prev = state.conversations+ noteChatsLeft(gone, back)+ })+ }+ if (useChatStore.persist.hasHydrated()) start()+ else useChatStore.persist.onFinishHydration(() => start())+}++/** Only for tests. */+export function __resetDeletionNoteForTests(): void {+ stop()+ stop = () => {}+ installed = false+}diff --git a/src/lib/chat-deletions.ts b/src/lib/chat-deletions.tsnew file mode 100644index 00000000..465f89ce--- /dev/null+++ b/src/lib/chat-deletions.ts@@ -0,0 +1,66 @@+/**+ * Chats deleted on purpose, so the restore from the backup does not offer them+ * back (C46, 3.1.0; phone and desktop alike).+ *+ * Since 3.0.10 the native backup keeps every chat that leaves the list in a+ * chat archive (backup_stores, store_backup.chats-*.json). That is what saves+ * a lost chat list (perrt, 05.11.2026), and it keeps a deleted chat the same+ * way: without this note the restore hint offered "Delete all chats" back at+ * the next start.+ *+ * Deleted means: it left the list while the app ran, after the chat store had+ * hydrated (lib/chat-deletion-note.ts watches for that). A chat that is lost+ * the way perrt's were never leaves a running list, it is missing when the+ * store comes up, so it is never noted and stays on offer.+ *+ * Pure apart from the storage it is handed; no store imports, so the restore+ * module and its tests stay light.+ */+import type { Conversation } from '../types/chat'++/** The ids of chats deleted on purpose, newest last. In the native backup (lib/store-backup.ts STORE_KEYS). */+export const DELETED_CHATS_KEY = 'lu-chats-deleted'+/** More would only grow the value: a chat deleted that long ago is no longer in the ring or the archives. */+export const DELETED_CHATS_MAX = 500++export type DeletionStorage = Pick<Storage, 'getItem' | 'setItem'> | null++export function defaultDeletionStorage(): DeletionStorage {+ try {+ return typeof localStorage !== 'undefined' ? localStorage : null+ } catch {+ return null+ }+}++export function readDeletedChats(store: DeletionStorage = defaultDeletionStorage()): Set<string> {+ try {+ const list: unknown = JSON.parse(store?.getItem(DELETED_CHATS_KEY) ?? '[]')+ return new Set(Array.isArray(list) ? list.filter((id): id is string => typeof id === 'string') : [])+ } catch {+ return new Set()+ }+}++/** Notes the ids that left and forgets the ones that came back, keeping the newest DELETED_CHATS_MAX. */+export function noteChatsLeft(gone: readonly string[], back: readonly string[], store: DeletionStorage = defaultDeletionStorage()): void {+ if (!store || (gone.length === 0 && back.length === 0)) return+ const returned = new Set(back)+ const left = new Set(gone)+ const list = [...readDeletedChats(store)].filter((id) => !returned.has(id) && !left.has(id))+ list.push(...gone.filter((id) => !returned.has(id)))+ try {+ store.setItem(DELETED_CHATS_KEY, JSON.stringify(list.slice(-DELETED_CHATS_MAX)))+ } catch {+ /* a full storage: the hint may offer a deleted chat once, the user dismisses it */+ }+}++/** Pure: the ids of `next` that `prev` did not have, and the other way round. */+export function chatIdsChanged(prev: readonly Conversation[], next: readonly Conversation[]): { gone: string[]; back: string[] } {+ // Every token of an answer makes a new list with the same chats in the same order: no sets for that.+ if (prev.length === next.length && prev.every((c, i) => c.id === next[i].id)) return { gone: [], back: [] }+ const before = new Set(prev.map((c) => c.id))+ const after = new Set(next.map((c) => c.id))+ return { gone: [...before].filter((id) => !after.has(id)), back: [...after].filter((id) => !before.has(id)) }+}diff --git a/src/lib/chat-rescue.ts b/src/lib/chat-rescue.tsindex 37df290f..f6bfd7fa 100644--- a/src/lib/chat-rescue.ts+++ b/src/lib/chat-rescue.ts@@ -19,6 +19,7 @@* back when the user presses the button (Settings, or the hint).*/import { backendCall, isTauri } from '../api/backend'+import { readDeletedChats } from './chat-deletions'import { log } from './logger'import type { Conversation } from '../types/chat'@@ -38,6 +39,15 @@ export function chatsMissingFrom(live: readonly Conversation[], backup: readonlyreturn out}+/**+ * What the restore offers: the chats the store does not have, without the ones+ * deleted on purpose (lib/chat-deletions.ts). A chat lost at the start was never+ * noted, so it stays on offer; a backup file picked by hand does not go through here.+ */+export function offeredChats(live: readonly Conversation[], backup: readonly unknown[], deleted: ReadonlySet<string> = readDeletedChats()): Conversation[] {+ return chatsMissingFrom(live, backup).filter((c) => !deleted.has(c.id))+}+/** Every chat the backup files hold, as one list. Empty outside the desktop app. */export async function readBackupChats(): Promise<unknown[]> {if (!isTauri()) return []@@ -46,9 +56,9 @@ export async function readBackupChats(): Promise<unknown[]> {return Array.isArray(list) ? list : []}-/** How many chats the backups hold that the store does not. */+/** How many chats the backups hold that the store does not and that were not deleted on purpose. */export async function countRestorableChats(live: () => readonly Conversation[]): Promise<number> {- return chatsMissingFrom(live(), await readBackupChats()).length+ return offeredChats(live(), await readBackupChats()).length}/**@@ -59,7 +69,7 @@ export async function restoreChatsFromBackup(live: () => readonly Conversation[],importConversations: (chats: Conversation[], mode: 'merge') => { added: number; skipped: number },): Promise<number> {- const missing = chatsMissingFrom(live(), await readBackupChats())+ const missing = offeredChats(live(), await readBackupChats())if (missing.length === 0) return 0const { added } = importConversations(missing, 'merge')log.warn('[chat-rescue] chats restored from the backup files', { count: added })diff --git a/src/lib/store-backup.ts b/src/lib/store-backup.tsindex 633ab1ab..aeb8df7c 100644--- a/src/lib/store-backup.ts+++ b/src/lib/store-backup.ts@@ -84,10 +84,10 @@ export const STORE_KEYS = [// lu-mobile-unload-in-background model settings: free the model in the background'lu-mobile-theme', 'lu-mobile-settings-prefs', 'lu-mobile-remote-desktops','lu-mobile-resume-seen', 'lu-mobile-download-wifi-only', 'lu-mobile-unload-in-background',- // C46 (3.1.0): the chats deleted on the phone (mobile/lib/chat-rescue.ts).- // The chat archives of the backup keep them; after a lost storage this note- // keeps the restore hint from offering them back.- 'lu-mobile-chats-deleted',+ // C46 (3.1.0): the chats deleted on purpose, phone and desktop+ // (lib/chat-deletions.ts). The chat archives of the backup keep them; after+ // a lost storage this note keeps the restore from offering them back.+ 'lu-chats-deleted',// Round 3 (M8). Found missing by the review: the text of the backup says// "chats and settings", and these did not come back.// lu-mobile-remote the chats held with a paired desktop, ondiff --git a/src/mobile/boot/__tests__/settings-durable.test.ts b/src/mobile/boot/__tests__/settings-durable.test.tsindex 2e52d9a3..5f989db7 100644--- a/src/mobile/boot/__tests__/settings-durable.test.ts+++ b/src/mobile/boot/__tests__/settings-durable.test.ts@@ -113,7 +113,7 @@ describe('which keys are kept', () => {'LU-create-orders': 'the orders of Create in chat, which is locked on the phone until it has the order card','lu-mobile-switched-under-answer': 'a note per answer; lost, the chat says "interrupted" instead of "you switched models"','lu-mobile-settings-stamp': 'the stamp of this copy itself; in the native backup it dates the settings there (L5)',- 'lu-mobile-chats-deleted': 'chats deleted on the phone (C46); lost, the restore hint offers them once and Not now keeps it away',+ 'lu-chats-deleted': 'chats deleted on purpose (C46); lost, the restore hint offers them once and Not now keeps it away',}for (const key of STORE_KEYS) {const copied = DURABLE_SETTINGS_KEYS.includes(key)diff --git a/src/mobile/lib/chat-rescue.ts b/src/mobile/lib/chat-rescue.tsindex 8b75e6ab..d37a9597 100644--- a/src/mobile/lib/chat-rescue.ts+++ b/src/mobile/lib/chat-rescue.ts@@ -8,94 +8,22 @@* Both run on the phone as they are. What the phone lacked was a way back:* the button on the backup page and the hint in the chat list use this module.*- * One thing differs from the desktop. The archive keeps every chat that left- * the list, also the ones the user deleted. A hint that offers "Delete all- * chats" back at the next start is wrong, so the phone notes the chats that- * leave the list while the app runs (`installDeletionNote`). Those are- * deliberate: a chat that is lost the way perrt's were never leaves a running- * list, it is missing when the store comes up. The noted ones are not offered- * again, neither by the hint nor by the button; a backup file picked by hand- * still brings them back.+ * Chats deleted on purpose are not offered back, here and on the desktop alike+ * (lib/chat-deletions.ts): the archive keeps them, and "Delete all chats" must+ * not return as a hint at the next start. A backup file picked by hand still+ * brings them back.*/-import { chatsMissingFrom, readBackupChats } from '../../lib/chat-rescue'+import { offeredChats, readBackupChats } from '../../lib/chat-rescue'import { useChatStore } from '../../stores/chatStore'import type { Conversation } from '../../types/chat'-/** The ids of chats that left the list while the app ran, newest last. */-export const DELETED_CHATS_KEY = 'lu-mobile-chats-deleted'-/** More would only grow the value: a chat deleted that long ago is no longer in the ring or the archives. */-export const DELETED_CHATS_MAX = 500--type Store = Pick<Storage, 'getItem' | 'setItem'> | null--function storage(): Store {- try {- return typeof localStorage !== 'undefined' ? localStorage : null- } catch {- return null- }-}--export function readDeletedChats(store: Store = storage()): Set<string> {- try {- const list: unknown = JSON.parse(store?.getItem(DELETED_CHATS_KEY) ?? '[]')- return new Set(Array.isArray(list) ? list.filter((id): id is string => typeof id === 'string') : [])- } catch {- return new Set()- }-}--/** Notes the ids that left and forgets the ones that came back, keeping the newest DELETED_CHATS_MAX. */-export function noteChatsLeft(gone: readonly string[], back: readonly string[], store: Store = storage()): void {- if (!store || (gone.length === 0 && back.length === 0)) return- const returned = new Set(back)- const list = [...readDeletedChats(store)].filter((id) => !returned.has(id) && !gone.includes(id))- list.push(...gone.filter((id) => !returned.has(id)))- try {- store.setItem(DELETED_CHATS_KEY, JSON.stringify(list.slice(-DELETED_CHATS_MAX)))- } catch {- /* a full storage: the hint may offer a deleted chat once, the user dismisses it */- }-}--/** Pure: the ids of `next` that `prev` did not have, and the other way round. */-export function chatIdsChanged(prev: readonly Conversation[], next: readonly Conversation[]): { gone: string[]; back: string[] } {- // Every token of an answer makes a new list with the same chats in the same order: no sets for that.- if (prev.length === next.length && prev.every((c, i) => c.id === next[i].id)) return { gone: [], back: [] }- const before = new Set(prev.map((c) => c.id))- const after = new Set(next.map((c) => c.id))- return { gone: [...before].filter((id) => !after.has(id)), back: [...after].filter((id) => !before.has(id)) }-}--let deletionNoteInstalled = false-let stopDeletionNote: () => void = () => {}--/**- * From the moment the chat store has hydrated, every chat that leaves the- * list is noted as deleted on purpose. Before that the list is the empty- * start value, and what hydration does not bring is exactly the loss this- * module is for.- */-export function installDeletionNote(): void {- if (deletionNoteInstalled) return- deletionNoteInstalled = true- const start = () => {- let prev = useChatStore.getState().conversations- stopDeletionNote = useChatStore.subscribe((state) => {- if (state.conversations === prev) return- const { gone, back } = chatIdsChanged(prev, state.conversations)- prev = state.conversations- noteChatsLeft(gone, back)- })- }- if (useChatStore.persist.hasHydrated()) start()- else useChatStore.persist.onFinishHydration(() => start())-}+// The note itself is shared with the desktop (lib/chat-deletions.ts, lib/chat-deletion-note.ts).+export { DELETED_CHATS_KEY, DELETED_CHATS_MAX, chatIdsChanged, noteChatsLeft, readDeletedChats } from '../../lib/chat-deletions'+export { __resetDeletionNoteForTests, installDeletionNote } from '../../lib/chat-deletion-note'/** The chats the backup files hold that are not in the list and were not deleted here on purpose. */export async function restorableChats(): Promise<Conversation[]> {- const deleted = readDeletedChats()- return chatsMissingFrom(useChatStore.getState().conversations, await readBackupChats()).filter((c) => !deleted.has(c.id))+ return offeredChats(useChatStore.getState().conversations, await readBackupChats())}/** Adds them to the list. Returns their ids, so the caller can wait until they are written. */@@ -105,10 +33,3 @@ export async function restoreChatsFromPhoneBackup(): Promise<string[]> {useChatStore.getState().importConversations(missing, 'merge')return missing.map((c) => c.id)}--/** Only for tests. */-export function __resetDeletionNoteForTests(): void {- stopDeletionNote()- stopDeletionNote = () => {}- deletionNoteInstalled = false-}diff --git a/src/mobile/screens/chat/ChatsPage.tsx b/src/mobile/screens/chat/ChatsPage.tsxindex d3b2ef62..4b347a52 100644--- a/src/mobile/screens/chat/ChatsPage.tsx+++ b/src/mobile/screens/chat/ChatsPage.tsx@@ -22,7 +22,7 @@ import { CloudSwitch } from '../settings/cloud/CloudSwitch'import { useChatsUnreadable } from '../../lib/storage-alerts'import { useBackupFound } from './parts/adapters/backup-found'import { dismissRescueHint, lookForRescue, restoreFromHint, useRescueHint } from './parts/adapters/chat-rescue-hint'-import { installDeletionNote } from '../../lib/chat-rescue'+import { installDeletionNote } from '../../../lib/chat-deletion-note'import './parts/chat.css'import { chatModelLabel } from './parts/model-name'import { useChatsHydrated } from './parts/hooks'diff --git a/src/mobile/strings/settings.ts b/src/mobile/strings/settings.tsindex 00187b81..c8b8c332 100644--- a/src/mobile/strings/settings.ts+++ b/src/mobile/strings/settings.ts@@ -667,7 +667,7 @@ export const SETTINGS_STRINGS = {autoNow: 'Back up now',autoDone: 'Backup written',autoUnavailable: 'The automatic backup is not available on this device.',- // C46: the chats of the automatic backup that are not in the list. Chats deleted on this phone are not offered.+ // C46: the chats of the automatic backup that are not in the list. Chats deleted on purpose are not offered.rescue: 'Restore chats from the automatic backup',rescueLooking: 'Looking in the backup',rescueFound: (n: number) => `${n} ${n === 1 ? 'chat' : 'chats'} not in your list`,