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: defer restored startup maintenance work

nash_su2cb75a1f129c
diff --git a/src-tauri/src/commands/file_sync.rs b/src-tauri/src/commands/file_sync.rs
index 6bc8ce6a..614d32a0 100644
--- a/src-tauri/src/commands/file_sync.rs
+++ b/src-tauri/src/commands/file_sync.rs
@@ -200,7 +200,7 @@ pub fn start_project_file_watcher(
let watcher_generation = WATCHER_GENERATION.fetch_add(1, Ordering::SeqCst) + 1;
ensure_sync_dir(&root)?;
with_queue_lock(&root, || reset_processing_tasks(&root, &project_id))?;
- enqueue_rescan_changes(&root, &project_id, &source_watch_config)?;
+ enqueue_startup_rescan_changes(&root, &project_id, &source_watch_config)?;
let changed_tasks = process_queue(&app, &root, &project_id)?;
let (tx, rx) = mpsc::sync_channel::<PathBuf>(8_192);
@@ -636,6 +636,19 @@ fn enqueue_rescan_changes(
enqueue_paths(root, project_id, rels)
}
+fn enqueue_startup_rescan_changes(
+ root: &Path,
+ project_id: &str,
+ source_watch_config: &SourceWatchConfig,
+) -> Result<(), String> {
+ enqueue_rescan_changes_for_prefixes(
+ root,
+ project_id,
+ &["raw/sources", "wiki", "purpose.md", "schema.md"],
+ source_watch_config,
+ )
+}
+
fn enqueue_rescan_changes_for_prefixes(
root: &Path,
project_id: &str,
@@ -1510,6 +1523,35 @@ mod tests {
let _ = fs::remove_dir_all(root);
}
+ #[test]
+ fn startup_rescan_scans_watch_roots_without_full_project_walk() {
+ let root = temp_root("startup-prefixes");
+ fs::create_dir_all(root.join("wiki/entities")).unwrap();
+ fs::create_dir_all(root.join("unwatched/deep")).unwrap();
+ fs::write(root.join("raw/sources/source.md"), "source").unwrap();
+ fs::write(root.join("wiki/entities/topic.md"), "---\ntitle: Topic\n---\n").unwrap();
+ fs::write(root.join("purpose.md"), "purpose").unwrap();
+ fs::write(root.join("schema.md"), "schema").unwrap();
+ fs::write(root.join("unwatched/deep/note.md"), "ignore").unwrap();
+
+ ensure_sync_dir(&root).unwrap();
+ enqueue_startup_rescan_changes(&root, "p1", &default_watch_config()).unwrap();
+ let queue = read_queue(&root).unwrap();
+ let paths = queue
+ .tasks
+ .iter()
+ .map(|task| task.path.as_str())
+ .collect::<BTreeSet<_>>();
+
+ assert!(paths.contains("raw/sources/source.md"));
+ assert!(paths.contains("wiki/entities/topic.md"));
+ assert!(paths.contains("purpose.md"));
+ assert!(paths.contains("schema.md"));
+ assert!(!paths.contains("unwatched/deep/note.md"));
+
+ let _ = fs::remove_dir_all(root);
+ }
+
#[test]
fn app_written_paths_update_snapshot_without_queueing() {
let root = temp_root("app-write");
diff --git a/src/components/settings/sections/maintenance-section.tsx b/src/components/settings/sections/maintenance-section.tsx
index dc7ccc5c..e0eaef02 100644
--- a/src/components/settings/sections/maintenance-section.tsx
+++ b/src/components/settings/sections/maintenance-section.tsx
@@ -21,6 +21,8 @@ import {
cancelTask,
retryTask,
getQueue,
+ getQueueSummary,
+ resumeProcessing,
groupKey,
type DedupTask,
} from "@/lib/dedup-queue"
@@ -58,9 +60,14 @@ export function MaintenanceSection() {
// that completed while the user was on a different settings tab).
// Same pattern activity-panel uses for ingest-queue.
const [tasks, setTasks] = useState<readonly DedupTask[]>([])
+ const [queueSummary, setQueueSummary] = useState(() => getQueueSummary())
useEffect(() => {
setTasks([...getQueue()])
- const id = setInterval(() => setTasks([...getQueue()]), 1000)
+ setQueueSummary(getQueueSummary())
+ const id = setInterval(() => {
+ setTasks([...getQueue()])
+ setQueueSummary(getQueueSummary())
+ }, 1000)
return () => clearInterval(id)
}, [])
@@ -107,6 +114,7 @@ export function MaintenanceSection() {
// Refresh immediately so the card flips to "queued" without
// waiting for the next 1s poll tick.
setTasks([...getQueue()])
+ setQueueSummary(getQueueSummary())
} catch (err) {
console.error("[Maintenance] enqueue failed:", err)
}
@@ -117,11 +125,19 @@ export function MaintenanceSection() {
const handleCancel = useCallback(async (taskId: string) => {
await cancelTask(taskId)
setTasks([...getQueue()])
+ setQueueSummary(getQueueSummary())
}, [])
const handleRetry = useCallback(async (taskId: string) => {
await retryTask(taskId)
setTasks([...getQueue()])
+ setQueueSummary(getQueueSummary())
+ }, [])
+
+ const handleResumeRestoredQueue = useCallback(() => {
+ resumeProcessing()
+ setTasks([...getQueue()])
+ setQueueSummary(getQueueSummary())
}, [])
const handleNotDuplicate = useCallback(
@@ -278,6 +294,8 @@ export function MaintenanceSection() {
<QueueOrphanList
tasks={tasks}
groups={groups}
+ restoredBacklogWaiting={queueSummary.restoredBacklogWaiting}
+ onResumeRestored={handleResumeRestoredQueue}
onCancel={(id) => void handleCancel(id)}
onRetry={(id) => void handleRetry(id)}
pendingPositionByTaskId={pendingPositionByTaskId}
@@ -325,6 +343,8 @@ function useRefInit<T>(init: () => T): { current: T } {
interface QueueOrphanListProps {
tasks: readonly DedupTask[]
groups: GroupUiEntry[]
+ restoredBacklogWaiting: boolean
+ onResumeRestored: () => void
onCancel: (taskId: string) => void
onRetry: (taskId: string) => void
pendingPositionByTaskId: Map<string, number>
@@ -340,6 +360,8 @@ interface QueueOrphanListProps {
function QueueOrphanList({
tasks,
groups,
+ restoredBacklogWaiting,
+ onResumeRestored,
onCancel,
onRetry,
pendingPositionByTaskId,
@@ -366,6 +388,22 @@ function QueueOrphanList({
"Tasks queued from a previous scan that haven't finished yet. Merges run one at a time.",
})}
</p>
+ {restoredBacklogWaiting && (
+ <div className="flex flex-wrap items-center justify-between gap-2 rounded border border-amber-500/30 bg-amber-500/5 px-3 py-2 text-xs">
+ <span className="text-amber-800 dark:text-amber-300">
+ {t("settings.sections.maintenance.dedup.restoredBacklog", {
+ defaultValue:
+ "These merge tasks were restored from the previous session and are paused to avoid unexpected LLM usage.",
+ })}
+ </span>
+ <Button size="sm" variant="secondary" onClick={onResumeRestored}>
+ <RotateCcw className="h-3.5 w-3.5" />
+ {t("settings.sections.maintenance.dedup.resumeRestored", {
+ defaultValue: "Resume merges",
+ })}
+ </Button>
+ </div>
+ )}
{orphans.map((task) => (
<div
key={task.id}
diff --git a/src/lib/dedup-queue.test.ts b/src/lib/dedup-queue.test.ts
index d02bb022..69b3c055 100644
--- a/src/lib/dedup-queue.test.ts
+++ b/src/lib/dedup-queue.test.ts
@@ -35,6 +35,7 @@ import {
getQueueSummary,
pauseQueue,
restoreQueue,
+ resumeProcessing,
} from "./dedup-queue"
import { executeMerge } from "./dedup-runner"
import { readFile, writeFile } from "@/commands/fs"
@@ -285,7 +286,7 @@ describe("dedup-queue — pauseQueue / restoreQueue", () => {
expect(restored[0].group.slugs).toEqual(["a", "b"])
})
- it("restoreQueue reverts processing tasks to pending so they re-run", async () => {
+ it("restoreQueue reverts processing tasks to pending without auto-running them", async () => {
const persisted = JSON.stringify([
{
id: "dedup-old",
@@ -312,6 +313,118 @@ describe("dedup-queue — pauseQueue / restoreQueue", () => {
await restoreQueue(TEST_ID, TEST_PATH)
await flushMicrotasks(20)
+ expect(mockExecuteMerge).not.toHaveBeenCalled()
+ const restored = getQueue()
+ expect(restored).toHaveLength(1)
+ expect(restored[0]).toMatchObject({
+ id: "dedup-old",
+ status: "pending",
+ })
+ expect(getQueueSummary().restoredBacklogWaiting).toBe(true)
+ })
+
+ it("runs new live merge tasks while restored backlog waits", async () => {
+ const persisted = JSON.stringify([
+ {
+ id: "dedup-restored",
+ projectId: TEST_ID,
+ group: { slugs: ["old-a", "old-b"], confidence: "high", reason: "x" },
+ canonicalSlug: "old-a",
+ status: "pending",
+ addedAt: 1,
+ error: null,
+ retryCount: 0,
+ },
+ ])
+ mockReadFile.mockImplementation(async (path: string) =>
+ path.startsWith(TEST_PATH) ? persisted : Promise.reject(new Error("ENOENT")),
+ )
+ mockExecuteMerge.mockResolvedValue({
+ canonicalContent: "",
+ canonicalPath: "",
+ rewrites: [],
+ pagesToDelete: [],
+ backup: [],
+ })
+
+ await restoreQueue(TEST_ID, TEST_PATH)
+ await flushMicrotasks(5)
+ expect(mockExecuteMerge).not.toHaveBeenCalled()
+
+ await enqueueMerge(TEST_ID, makeGroup(["new-a", "new-b"]), "new-a")
+ await flushMicrotasks(20)
+
+ expect(mockExecuteMerge).toHaveBeenCalledOnce()
+ expect(mockExecuteMerge.mock.calls[0][1].slugs).toEqual(["new-a", "new-b"])
+ expect(getQueue().map((task) => task.id)).toEqual(["dedup-restored"])
+ expect(getQueueSummary().restoredBacklogWaiting).toBe(true)
+ })
+
+ it("promotes a restored merge when a live enqueue touches the same group", async () => {
+ const persisted = JSON.stringify([
+ {
+ id: "dedup-restored",
+ projectId: TEST_ID,
+ group: { slugs: ["a", "b"], confidence: "high", reason: "old" },
+ canonicalSlug: "a",
+ status: "pending",
+ addedAt: 1,
+ error: null,
+ retryCount: 0,
+ },
+ ])
+ mockReadFile.mockImplementation(async (path: string) =>
+ path.startsWith(TEST_PATH) ? persisted : Promise.reject(new Error("ENOENT")),
+ )
+ mockExecuteMerge.mockResolvedValue({
+ canonicalContent: "",
+ canonicalPath: "",
+ rewrites: [],
+ pagesToDelete: [],
+ backup: [],
+ })
+
+ await restoreQueue(TEST_ID, TEST_PATH)
+ const id = await enqueueMerge(TEST_ID, makeGroup(["b", "a"]), "b")
+ await flushMicrotasks(20)
+
+ expect(id).toBe("dedup-restored")
+ expect(mockExecuteMerge).toHaveBeenCalledOnce()
+ expect(getQueue()).toHaveLength(0)
+ expect(getQueueSummary().restoredBacklogWaiting).toBe(false)
+ })
+
+ it("resumeProcessing runs restored pending merge tasks", async () => {
+ const persisted = JSON.stringify([
+ {
+ id: "dedup-restored",
+ projectId: TEST_ID,
+ group: { slugs: ["a", "b"], confidence: "high", reason: "x" },
+ canonicalSlug: "a",
+ status: "pending",
+ addedAt: 1,
+ error: null,
+ retryCount: 0,
+ },
+ ])
+ mockReadFile.mockImplementation(async (path: string) =>
+ path.startsWith(TEST_PATH) ? persisted : Promise.reject(new Error("ENOENT")),
+ )
+ mockExecuteMerge.mockResolvedValue({
+ canonicalContent: "",
+ canonicalPath: "",
+ rewrites: [],
+ pagesToDelete: [],
+ backup: [],
+ })
+
+ await restoreQueue(TEST_ID, TEST_PATH)
+ await flushMicrotasks(5)
+ expect(mockExecuteMerge).not.toHaveBeenCalled()
+
+ resumeProcessing()
+ await flushMicrotasks(20)
+
expect(mockExecuteMerge).toHaveBeenCalledOnce()
expect(getQueue()).toHaveLength(0)
})
diff --git a/src/lib/dedup-queue.ts b/src/lib/dedup-queue.ts
index 77867f68..ad2fddf3 100644
--- a/src/lib/dedup-queue.ts
+++ b/src/lib/dedup-queue.ts
@@ -40,6 +40,11 @@ export interface DedupTask {
let queue: DedupTask[] = []
let processing = false
+/** Pending tasks restored from disk on startup/project open. These are
+ * intentionally hydrated without auto-running so opening a project does
+ * not immediately spend LLM tokens on historical merge work. A fresh
+ * enqueue for the same group promotes the restored task out of this set. */
+let restoredPausedTaskIds = new Set<string>()
let currentProjectId = ""
let currentProjectPath = ""
let currentAbortController: AbortController | null = null
@@ -113,7 +118,17 @@ export async function enqueueMerge(
t.status !== "done" &&
groupKey(t.group.slugs) === key,
)
- if (existing) return existing.id
+ if (existing) {
+ restoredPausedTaskIds.delete(existing.id)
+ if (existing.status === "failed") {
+ existing.status = "pending"
+ existing.error = null
+ existing.retryCount = 0
+ }
+ await saveQueue(currentProjectPath)
+ processNext(currentProjectId)
+ return existing.id
+ }
const task: DedupTask = {
id: generateId(),
@@ -142,6 +157,7 @@ export async function retryTask(taskId: string): Promise<void> {
if (!task) return
if (task.projectId !== currentProjectId) return
+ restoredPausedTaskIds.delete(task.id)
task.status = "pending"
task.error = null
task.retryCount = 0
@@ -149,6 +165,12 @@ export async function retryTask(taskId: string): Promise<void> {
processNext(currentProjectId)
}
+/** Resume merge tasks restored from disk during project open. */
+export function resumeProcessing(): void {
+ restoredPausedTaskIds.clear()
+ if (currentProjectId) processNext(currentProjectId)
+}
+
/**
* Cancel/delete a task. If it's currently running, abort the LLM call
* first — the merge writes will be left where they were when the
@@ -167,6 +189,7 @@ export async function cancelTask(taskId: string): Promise<void> {
processing = false
}
+ restoredPausedTaskIds.delete(taskId)
queue = queue.filter((t) => t.id !== taskId)
await saveQueue(currentProjectPath)
processNext(currentProjectId)
@@ -181,12 +204,16 @@ export function getQueueSummary(): {
processing: number
failed: number
total: number
+ restoredBacklogWaiting: boolean
} {
return {
pending: queue.filter((t) => t.status === "pending").length,
processing: queue.filter((t) => t.status === "processing").length,
failed: queue.filter((t) => t.status === "failed").length,
total: queue.length,
+ restoredBacklogWaiting: queue.some((t) =>
+ t.status === "pending" && restoredPausedTaskIds.has(t.id)
+ ),
}
}
@@ -200,6 +227,7 @@ export function clearQueueState(): void {
currentAbortController.abort()
}
queue = []
+ restoredPausedTaskIds.clear()
processing = false
currentProjectId = ""
currentProjectPath = ""
@@ -231,14 +259,15 @@ export async function pauseQueue(): Promise<void> {
await saveQueue(pausedProjectPath)
queue = []
+ restoredPausedTaskIds.clear()
currentProjectId = ""
currentProjectPath = ""
}
/**
- * Load a project's queue from disk and resume processing. Tasks left
- * in "processing" by an abrupt exit get reverted to "pending" so they
- * pick up on next process tick.
+ * Load a project's queue from disk. Restored pending tasks are visible
+ * but do not auto-run; a fresh enqueue for the same group promotes the
+ * existing task, and retryTask can also resume one explicitly.
*/
export async function restoreQueue(
projectId: string,
@@ -246,6 +275,7 @@ export async function restoreQueue(
): Promise<void> {
const pp = normalizePath(projectPath)
queue = []
+ restoredPausedTaskIds.clear()
processing = false
currentAbortController = null
currentProjectId = projectId
@@ -270,15 +300,19 @@ export async function restoreQueue(
}
queue = mine
+ restoredPausedTaskIds = new Set(
+ queue
+ .filter((t) => t.status === "pending")
+ .map((t) => t.id),
+ )
await saveQueue(pp)
const pending = queue.filter((t) => t.status === "pending").length
const failed = queue.filter((t) => t.status === "failed").length
if (pending > 0 || restored > 0) {
console.log(
- `[Dedup Queue] Restored: ${pending} pending, ${failed} failed, ${restored} resumed from interrupted`,
+ `[Dedup Queue] Restored: ${pending} pending paused for manual resume, ${failed} failed, ${restored} reset from interrupted`,
)
- processNext(projectId)
}
}
@@ -290,8 +324,10 @@ async function processNext(projectId: string): Promise<void> {
if (processing) return
if (currentProjectId !== projectId) return
- const next = queue.find(
- (t) => t.projectId === projectId && t.status === "pending",
+ const next = queue.find((t) =>
+ t.projectId === projectId &&
+ t.status === "pending" &&
+ !restoredPausedTaskIds.has(t.id)
)
if (!next) return
@@ -335,6 +371,7 @@ async function processNext(projectId: string): Promise<void> {
if (currentProjectId !== projectId) return
currentAbortController = null
+ restoredPausedTaskIds.delete(next.id)
queue = queue.filter((t) => t.id !== next.id)
await saveQueue(pp)
// Tell the rest of the app the wiki tree changed.