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
feat: reduce stale review noise with dedupe + queue-drain sweep
nash_suecd3222622a1
diff --git a/src/lib/ingest-queue.ts b/src/lib/ingest-queue.tsindex 412a35aa..829af72f 100644--- a/src/lib/ingest-queue.ts+++ b/src/lib/ingest-queue.ts@@ -22,6 +22,9 @@ let processing = falselet currentProjectPath = ""let currentAbortController: AbortController | null = nulllet lastWrittenFiles: string[] = [] // track files written by current ingest for cleanup+// Track whether any task has been processed since the last drain.+// Prevents the sweep from running on every idle/no-op call.+let processedSinceDrain = false// ── Persistence ───────────────────────────────────────────────────────────@@ -211,6 +214,7 @@ export function clearQueueState(): void {currentProjectPath = ""currentAbortController = nulllastWrittenFiles = []+ processedSinceDrain = false}// ── Restore on startup ───────────────────────────────────────────────────@@ -257,11 +261,29 @@ export async function restoreQueue(projectPath: string): Promise<void> {const MAX_RETRIES = 3+async function onQueueDrained(projectPath: string): Promise<void> {+ if (!processedSinceDrain) return+ processedSinceDrain = false++ try {+ const { sweepResolvedReviews } = await import("@/lib/sweep-reviews")+ await sweepResolvedReviews(projectPath)+ } catch (err) {+ console.error("[Ingest Queue] Failed to load sweep-reviews:", err)+ }+}+async function processNext(projectPath: string): Promise<void> {if (processing) returnconst next = queue.find((t) => t.status === "pending")- if (!next) return+ if (!next) {+ // Queue drained — trigger review cleanup (auto-resolve stale items)+ onQueueDrained(projectPath).catch((err) =>+ console.error("[Ingest Queue] sweep failed:", err)+ )+ return+ }processing = truenext.status = "processing"@@ -298,6 +320,7 @@ async function processNext(projectPath: string): Promise<void> {currentAbortController = nulllastWrittenFiles = []queue = queue.filter((t) => t.id !== next.id)+ processedSinceDrain = trueawait saveQueue(pp)console.log(`[Ingest Queue] Done: ${next.sourcePath}`)diff --git a/src/lib/sweep-reviews.ts b/src/lib/sweep-reviews.tsnew file mode 100644index 00000000..f2b94fa8--- /dev/null+++ b/src/lib/sweep-reviews.ts@@ -0,0 +1,161 @@+/**+ * Sweep pending review items and auto-resolve those whose underlying+ * condition has been addressed by subsequent ingests.+ *+ * Triggered when the ingest queue drains.+ * Conservative: only auto-resolves high-certainty cases. Preserves+ * contradiction / suggestion / confirm types that need human judgment.+ */++import { listDirectory, readFile } from "@/commands/fs"+import { useReviewStore, type ReviewItem } from "@/stores/review-store"+import { useActivityStore } from "@/stores/activity-store"+import type { FileNode } from "@/types/wiki"+import { normalizePath } from "@/lib/path-utils"++// ── Helpers ────────────────────────────────────────────────────────────────++function flattenMdFiles(nodes: FileNode[]): FileNode[] {+ const files: FileNode[] = []+ for (const node of nodes) {+ if (node.is_dir && node.children) {+ files.push(...flattenMdFiles(node.children))+ } else if (!node.is_dir && node.name.endsWith(".md")) {+ files.push(node)+ }+ }+ return files+}++/** Build an index of wiki pages: id (filename without .md) + title → normalized */+async function buildWikiIndex(projectPath: string): Promise<{+ byId: Set<string>+ byTitle: Set<string>+}> {+ const pp = normalizePath(projectPath)+ const byId = new Set<string>()+ const byTitle = new Set<string>()++ try {+ const tree = await listDirectory(`${pp}/wiki`)+ const files = flattenMdFiles(tree)++ for (const file of files) {+ const id = file.name.replace(/\.md$/, "").toLowerCase()+ byId.add(id)++ // Also capture frontmatter title for fuzzy matching+ try {+ const content = await readFile(file.path)+ const match = content.match(/^---\n[\s\S]*?^title:\s*["']?(.+?)["']?\s*$/m)+ if (match) {+ byTitle.add(match[1].trim().toLowerCase())+ }+ } catch {+ // skip unreadable files+ }+ }+ } catch {+ // no wiki directory yet+ }++ return { byId, byTitle }+}++/**+ * Extract candidate page names from a review item's title / description.+ * Conservative — only flags items where we can confidently identify a page name.+ */+function extractCandidateNames(item: ReviewItem): string[] {+ const names = new Set<string>()++ // The review title itself is often the missing page name+ // e.g. "Missing page: 注意力机制" or "注意力机制"+ const cleaned = item.title+ .replace(/^(missing[\s-]?page[::]\s*|缺失页面[::]\s*|缺少页面[::]\s*)/i, "")+ .trim()++ if (cleaned && cleaned.length <= 100) {+ names.add(cleaned.toLowerCase())+ }++ // Also check affectedPages — these reference files directly+ for (const page of item.affectedPages ?? []) {+ const base = page.split("/").pop()?.replace(/\.md$/, "")+ if (base) names.add(base.toLowerCase())+ }++ return Array.from(names)+}++/** Check if a candidate name matches an existing wiki page */+function pageExists(name: string, index: { byId: Set<string>; byTitle: Set<string> }): boolean {+ const normalized = name.trim().toLowerCase()+ if (!normalized) return false++ // Exact filename match (kebab-case or matching existing id)+ if (index.byId.has(normalized)) return true+ if (index.byId.has(normalized.replace(/\s+/g, "-"))) return true++ // Exact title match (from frontmatter)+ if (index.byTitle.has(normalized)) return true++ return false+}++// ── Main ──────────────────────────────────────────────────────────────────++/**+ * Scan pending review items and auto-resolve those whose condition+ * no longer holds. Called when the ingest queue drains.+ */+export async function sweepResolvedReviews(projectPath: string): Promise<number> {+ const store = useReviewStore.getState()+ const pending = store.items.filter((i) => !i.resolved)++ if (pending.length === 0) return 0++ const index = await buildWikiIndex(projectPath)++ let resolvedCount = 0++ for (const item of pending) {+ // Only auto-resolve types where we can make a high-certainty judgment+ if (item.type === "missing-page") {+ const names = extractCandidateNames(item)+ if (names.length > 0 && names.some((n) => pageExists(n, index))) {+ store.resolveItem(item.id, "auto-resolved")+ resolvedCount+++ }+ } else if (item.type === "duplicate") {+ // If any affected page no longer exists, the duplicate situation changed —+ // auto-resolve (user or cascade-delete took care of it).+ const affected = item.affectedPages ?? []+ if (affected.length > 0) {+ const allStillExist = affected.every((p) => {+ const base = p.split("/").pop()?.replace(/\.md$/, "").toLowerCase()+ return base ? index.byId.has(base) : false+ })+ if (!allStillExist) {+ store.resolveItem(item.id, "auto-resolved")+ resolvedCount+++ }+ }+ }+ // contradiction / suggestion / confirm → keep, need human judgment+ }++ if (resolvedCount > 0) {+ // Log to activity panel so user sees it happened+ useActivityStore.getState().addItem({+ type: "query",+ title: "Review cleanup",+ status: "done",+ detail: `Auto-resolved ${resolvedCount} stale review item${resolvedCount > 1 ? "s" : ""}`,+ filesWritten: [],+ })+ console.log(`[Sweep Reviews] Auto-resolved ${resolvedCount} review items`)+ }++ return resolvedCount+}diff --git a/src/stores/review-store.ts b/src/stores/review-store.tsindex e6bfc70c..fa7e266b 100644--- a/src/stores/review-store.ts+++ b/src/stores/review-store.ts@@ -48,17 +48,54 @@ export const useReviewStore = create<ReviewState>((set) => ({})),addItems: (items) =>- set((state) => ({- items: [- ...state.items,- ...items.map((item) => ({- ...item,- id: `review-${++counter}`,- resolved: false,- createdAt: Date.now(),- })),- ],- })),+ set((state) => {+ // De-dupe against pending items with same type + normalized title+ // Merge affectedPages / searchQueries / sourcePath instead of creating duplicates+ const result = [...state.items]+ const keyFor = (t: string, title: string) => `${t}::${title.trim().toLowerCase()}`++ // Build index of existing pending items for fast lookup+ const pendingIndex = new Map<string, number>()+ result.forEach((it, idx) => {+ if (!it.resolved && (it.type === "missing-page" || it.type === "duplicate" || it.type === "suggestion")) {+ pendingIndex.set(keyFor(it.type, it.title), idx)+ }+ })++ for (const incoming of items) {+ const k = keyFor(incoming.type, incoming.title)+ const existingIdx = (incoming.type === "missing-page" || incoming.type === "duplicate" || incoming.type === "suggestion")+ ? pendingIndex.get(k)+ : undefined++ if (existingIdx !== undefined) {+ // Merge into existing+ const old = result[existingIdx]+ const mergedPages = Array.from(new Set([...(old.affectedPages ?? []), ...(incoming.affectedPages ?? [])]))+ const mergedQueries = Array.from(new Set([...(old.searchQueries ?? []), ...(incoming.searchQueries ?? [])]))+ result[existingIdx] = {+ ...old,+ description: incoming.description || old.description, // prefer newer description+ sourcePath: incoming.sourcePath ?? old.sourcePath,+ affectedPages: mergedPages.length > 0 ? mergedPages : undefined,+ searchQueries: mergedQueries.length > 0 ? mergedQueries : undefined,+ }+ } else {+ const newItem = {+ ...incoming,+ id: `review-${++counter}`,+ resolved: false,+ createdAt: Date.now(),+ }+ result.push(newItem)+ if (incoming.type === "missing-page" || incoming.type === "duplicate" || incoming.type === "suggestion") {+ pendingIndex.set(k, result.length - 1)+ }+ }+ }++ return { items: result }+ }),setItems: (items) => set({ items }),