Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

fix: preserve review items when switching projects

simplelife677877b3128718
diff --git a/src/App.tsx b/src/App.tsx
index 2257afae..ce31aceb 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -290,6 +290,13 @@ function App() {
}, [])
async function handleProjectOpened(proj: WikiProject) {
+ // Flush the OUTGOING project's review/lint/chat state to disk and suspend
+ // auto-save before reset empties the stores — otherwise the debounced
+ // writers would persist empty arrays back over the old project's pending
+ // review / deep-research items.
+ const { flushAndSuspendAutoSave, resumeAutoSave } = await import("@/lib/auto-save")
+ await flushAndSuspendAutoSave()
+
// Clear all per-project state BEFORE loading new project data
// to prevent cross-project contamination. MUST be awaited so the
// ingest queue / graph cache are actually cleared before the new
@@ -424,6 +431,9 @@ function App() {
} catch {
// ignore, start fresh
}
+ // New project's persisted state is loaded — re-arm auto-save so further
+ // edits persist to THIS project.
+ resumeAutoSave()
}
async function handleSelectRecent(proj: WikiProject) {
@@ -463,6 +473,12 @@ function App() {
saveScheduledImportConfig(currentProject.path, currentConfig).catch(() => {})
}
+ // Flush outgoing project's review/lint/chat to disk and suspend auto-save
+ // before reset empties the stores. resumeAutoSave() runs when the next
+ // project opens via handleProjectOpened.
+ const { flushAndSuspendAutoSave } = await import("@/lib/auto-save")
+ await flushAndSuspendAutoSave()
+
// Clear all per-project state BEFORE flipping back to the welcome screen
// so old data cannot leak in via any async render pass.
const { resetProjectState } = await import("@/lib/reset-project-state")
diff --git a/src/lib/auto-save.test.ts b/src/lib/auto-save.test.ts
new file mode 100644
index 00000000..b8de752d
--- /dev/null
+++ b/src/lib/auto-save.test.ts
@@ -0,0 +1,103 @@
+/**
+ * Regression test for the project-switch data-loss bug:
+ *
+ * When switching wiki projects, resetProjectState() empties the review/lint/chat
+ * stores. The auto-save subscriptions are debounced, so without a guard their
+ * timers fired AFTER the store was cleared but while project?.path still pointed
+ * at the OUTGOING project — persisting empty arrays over that project's pending
+ * review / deep-research items. Switching back then loaded an emptied review.json.
+ *
+ * flushAndSuspendAutoSave() must (1) persist the real current state to disk and
+ * (2) suspend the subscriptions so the subsequent clear-to-empty does not write.
+ */
+import { describe, it, expect, beforeEach, vi } from "vitest"
+
+const { saveReviewItems, saveLintItems, saveChatHistory } = vi.hoisted(() => ({
+ saveReviewItems: vi.fn().mockResolvedValue(undefined),
+ saveLintItems: vi.fn().mockResolvedValue(undefined),
+ saveChatHistory: vi.fn().mockResolvedValue(undefined),
+}))
+
+vi.mock("./persist", () => ({ saveReviewItems, saveLintItems, saveChatHistory }))
+
+import { setupAutoSave, flushAndSuspendAutoSave, resumeAutoSave } from "./auto-save"
+import { useReviewStore } from "@/stores/review-store"
+import { useLintStore } from "@/stores/lint-store"
+import { useChatStore } from "@/stores/chat-store"
+import { useWikiStore } from "@/stores/wiki-store"
+import type { ReviewItem } from "@/stores/review-store"
+
+function setProjectPath(path: string | null): void {
+ useWikiStore.setState({ project: path ? ({ id: "p", name: "p", path } as never) : null })
+}
+
+function review(id: string): ReviewItem {
+ return { id, type: "missing-page", title: id, description: "", options: [], resolved: false, createdAt: 0 }
+}
+
+let registered = false
+
+beforeEach(() => {
+ saveReviewItems.mockClear()
+ saveLintItems.mockClear()
+ saveChatHistory.mockClear()
+ useReviewStore.setState({ items: [] })
+ useLintStore.setState({ items: [] })
+ useChatStore.setState({ conversations: [], messages: [], isStreaming: false })
+ resumeAutoSave()
+ vi.useFakeTimers()
+ // setupAutoSave registers permanent subscriptions; only do it once.
+ if (!registered) {
+ setupAutoSave()
+ registered = true
+ }
+})
+
+describe("auto-save project-switch guard", () => {
+ it("flushes current review state to the outgoing project before suspend", async () => {
+ setProjectPath("/proj/A")
+ useReviewStore.setState({ items: [review("a1"), review("a2")] })
+
+ await flushAndSuspendAutoSave()
+
+ expect(saveReviewItems).toHaveBeenCalledWith("/proj/A", [review("a1"), review("a2")])
+ })
+
+ it("does NOT persist the empty store after suspend (the data-loss bug)", async () => {
+ setProjectPath("/proj/A")
+ useReviewStore.setState({ items: [review("a1")] })
+
+ await flushAndSuspendAutoSave()
+ saveReviewItems.mockClear()
+ saveLintItems.mockClear()
+
+ // resetProjectState would do this — clear stores while path is still A.
+ useReviewStore.setState({ items: [] })
+ useLintStore.setState({ items: [] })
+ vi.runAllTimers()
+
+ expect(saveReviewItems).not.toHaveBeenCalled()
+ expect(saveLintItems).not.toHaveBeenCalled()
+ })
+
+ it("resumes persisting after resumeAutoSave", () => {
+ setProjectPath("/proj/B")
+ flushAndSuspendAutoSave()
+ resumeAutoSave()
+
+ useReviewStore.setState({ items: [review("b1")] })
+ vi.runAllTimers()
+
+ expect(saveReviewItems).toHaveBeenCalledWith("/proj/B", [review("b1")])
+ })
+
+ it("skips chat flush while streaming", async () => {
+ setProjectPath("/proj/A")
+ useChatStore.setState({ isStreaming: true })
+
+ await flushAndSuspendAutoSave()
+
+ expect(saveChatHistory).not.toHaveBeenCalled()
+ expect(saveReviewItems).toHaveBeenCalled()
+ })
+})
diff --git a/src/lib/auto-save.ts b/src/lib/auto-save.ts
index b2b21bdd..fad75385 100644
--- a/src/lib/auto-save.ts
+++ b/src/lib/auto-save.ts
@@ -8,9 +8,50 @@ let reviewTimer: ReturnType<typeof setTimeout> | null = null
let lintTimer: ReturnType<typeof setTimeout> | null = null
let chatTimer: ReturnType<typeof setTimeout> | null = null
+// While suspended, the store subscriptions skip writing. This is essential
+// during a project switch: resetProjectState() clears every store to empty,
+// and without this guard the debounced callbacks would persist those empty
+// arrays back to the OUTGOING project's .llm-wiki/*.json — wiping its pending
+// review / deep-research items. The switch flow flushes real data to disk via
+// flushAndSuspendAutoSave() first, then resumes once the new project loads.
+let suspended = false
+
+function clearTimers(): void {
+ if (reviewTimer) { clearTimeout(reviewTimer); reviewTimer = null }
+ if (lintTimer) { clearTimeout(lintTimer); lintTimer = null }
+ if (chatTimer) { clearTimeout(chatTimer); chatTimer = null }
+}
+
+/**
+ * Immediately persist the current stores to the current project, then stop
+ * auto-save from firing until resumeAutoSave() is called. Must be invoked
+ * before resetProjectState() clears the stores on a project switch.
+ */
+export async function flushAndSuspendAutoSave(): Promise<void> {
+ suspended = true
+ clearTimers()
+ const projectPath = useWikiStore.getState().project?.path
+ if (!projectPath) return
+ const review = useReviewStore.getState().items
+ const lint = useLintStore.getState().items
+ const chat = useChatStore.getState()
+ await Promise.allSettled([
+ saveReviewItems(projectPath, review),
+ saveLintItems(projectPath, lint),
+ chat.isStreaming
+ ? Promise.resolve()
+ : saveChatHistory(projectPath, chat.conversations, chat.messages),
+ ])
+}
+
+export function resumeAutoSave(): void {
+ suspended = false
+}
+
export function setupAutoSave(): void {
// Auto-save review items (debounced 1s)
useReviewStore.subscribe((state) => {
+ if (suspended) return
const projectPath = useWikiStore.getState().project?.path
if (reviewTimer) clearTimeout(reviewTimer)
reviewTimer = setTimeout(() => {
@@ -22,6 +63,7 @@ export function setupAutoSave(): void {
// Auto-save lint items (debounced 1s)
useLintStore.subscribe((state) => {
+ if (suspended) return
const projectPath = useWikiStore.getState().project?.path
if (lintTimer) clearTimeout(lintTimer)
lintTimer = setTimeout(() => {
@@ -33,6 +75,7 @@ export function setupAutoSave(): void {
// Auto-save chat conversations and messages (debounced 2s, skip during streaming)
useChatStore.subscribe((state) => {
+ if (suspended) return
if (state.isStreaming) return
const projectPath = useWikiStore.getState().project?.path
if (chatTimer) clearTimeout(chatTimer)