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 resolver index during shallow refresh

nash_suaced640c6262
diff --git a/src/lib/project-file-tree-refresh.test.ts b/src/lib/project-file-tree-refresh.test.ts
index d3c36685..6b1d946b 100644
--- a/src/lib/project-file-tree-refresh.test.ts
+++ b/src/lib/project-file-tree-refresh.test.ts
@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"
import { refreshProjectFileTree } from "@/lib/project-file-tree-refresh"
import { useWikiStore } from "@/stores/wiki-store"
import type { FileNode, WikiProject } from "@/types/wiki"
+import { buildProjectPathIndexFromTree } from "@/lib/wiki-page-resolver"
const mocks = vi.hoisted(() => ({
listDirectory: vi.fn(),
@@ -101,4 +102,59 @@ describe("refreshProjectFileTree", () => {
expect(useWikiStore.getState().projectPathIndex.byPath.size).toBe(0)
expect(useWikiStore.getState().dataVersion).toBe(0)
})
+
+ it("keeps the existing full resolver index when shallow refresh succeeds but full refresh fails", async () => {
+ vi.useFakeTimers()
+ const existingIndex = buildProjectPathIndexFromTree(fullTree)
+ try {
+ useWikiStore.setState({
+ projectPathIndex: existingIndex,
+ })
+ mocks.listDirectory.mockImplementation(async (_path: string, options?: { maxDepth?: number }) => {
+ if (options?.maxDepth === 2) return shallowTree
+ throw new Error("full scan failed")
+ })
+
+ await refreshProjectFileTree(project.path, { projectId: project.id })
+ await flushMicrotasks()
+ await vi.advanceTimersByTimeAsync(250)
+ await flushMicrotasks()
+ await vi.advanceTimersByTimeAsync(250)
+ await flushMicrotasks()
+
+ expect(mocks.listDirectory).toHaveBeenNthCalledWith(1, project.path, { maxDepth: 2 })
+ expect(mocks.listDirectory).toHaveBeenNthCalledWith(2, project.path, undefined)
+ expect(mocks.listDirectory).toHaveBeenNthCalledWith(3, project.path, undefined)
+ expect(mocks.listDirectory).toHaveBeenNthCalledWith(4, project.path, undefined)
+ expect(useWikiStore.getState().fileTree).toEqual(shallowTree)
+ expect(useWikiStore.getState().projectPathIndex.byPath.has("/tmp/project/wiki/entities/alpha.md")).toBe(true)
+ } finally {
+ vi.useRealTimers()
+ }
+ })
+
+ it("does not start the background full scan once the shallow refresh reveals a stale project", async () => {
+ mocks.listDirectory.mockImplementation(async (_path: string, options?: { maxDepth?: number }) => {
+ if (options?.maxDepth === 2) {
+ // Simulate switching projects while the shallow tree request is in flight.
+ useWikiStore.setState({
+ project: { ...project, id: "other-project", path: "/tmp/other" },
+ })
+ return shallowTree
+ }
+ return fullTree
+ })
+
+ await refreshProjectFileTree(project.path, {
+ projectId: project.id,
+ bumpDataVersion: true,
+ })
+ await flushMicrotasks()
+
+ expect(mocks.listDirectory).toHaveBeenCalledTimes(1)
+ expect(mocks.listDirectory).toHaveBeenCalledWith(project.path, { maxDepth: 2 })
+ expect(useWikiStore.getState().fileTree).toEqual([])
+ expect(useWikiStore.getState().projectPathIndex.byPath.size).toBe(0)
+ expect(useWikiStore.getState().dataVersion).toBe(0)
+ })
})
diff --git a/src/lib/project-file-tree-refresh.ts b/src/lib/project-file-tree-refresh.ts
index 29c2e8a6..35e8a545 100644
--- a/src/lib/project-file-tree-refresh.ts
+++ b/src/lib/project-file-tree-refresh.ts
@@ -53,7 +53,7 @@ export async function refreshProjectFileTree(
try {
const shallowTree = await listDirectory(normalizedProjectPath, { maxDepth: 2 })
if (isStillCurrentProject(currentProjectId, normalizedProjectPath)) {
- useWikiStore.getState().setFileTree(shallowTree)
+ useWikiStore.getState().setFileTree(shallowTree, { syncPathIndex: false })
}
} catch (err) {
console.error("Failed to refresh project file tree:", err)
@@ -61,6 +61,7 @@ export async function refreshProjectFileTree(
}
if (refreshPathIndex) {
+ if (!isStillCurrentProject(currentProjectId, normalizedProjectPath)) return
listDirectoryWithRetry(normalizedProjectPath, undefined, 3)
.then((fullTree) => {
if (!isStillCurrentProject(currentProjectId, normalizedProjectPath)) return