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(multimodal): cascade-delete extracted images when source page is deleted

nash_su7a81126cb9af
diff --git a/src/lib/wiki-page-delete.test.ts b/src/lib/wiki-page-delete.test.ts
index 548ce96f..f1bbfe33 100644
--- a/src/lib/wiki-page-delete.test.ts
+++ b/src/lib/wiki-page-delete.test.ts
@@ -122,4 +122,91 @@ describe("cascadeDeleteWikiPage", () => {
expect(mockDeleteFile).toHaveBeenCalled()
expect(mockRemovePageEmbedding).not.toHaveBeenCalled()
})
+
+ // ── Media cascade: source-summary deletion drops images too ──────
+ //
+ // The image-extraction step writes to wiki/media/<slug>/ keyed by
+ // the SOURCE document's slug. The source-summary page at
+ // wiki/sources/<slug>.md is the canonical home for those images
+ // (we append a markdown section to it post-write). When the
+ // source page is deleted (either via source-delete cascade in
+ // sources-view, or a manual delete), the matching media directory
+ // becomes orphaned — these tests pin that we drop it too.
+
+ it("deleting wiki/sources/<slug>.md also deletes wiki/media/<slug>/", async () => {
+ await cascadeDeleteWikiPage("/proj", "/proj/wiki/sources/rope-paper.md")
+
+ expect(mockDeleteFile).toHaveBeenCalledTimes(2)
+ expect(mockDeleteFile).toHaveBeenNthCalledWith(1, "/proj/wiki/sources/rope-paper.md")
+ expect(mockDeleteFile).toHaveBeenNthCalledWith(2, "/proj/wiki/media/rope-paper")
+ expect(mockRemovePageEmbedding).toHaveBeenCalledWith("/proj", "rope-paper")
+ })
+
+ it("does NOT cascade media when deleting a non-source page (concept / entity / queries)", async () => {
+ // Concept / entity pages don't own a media directory of their own.
+ // Multiple pages can reference images from any source's media/.
+ // Deleting one concept page must not destroy the source's images.
+ await cascadeDeleteWikiPage("/proj", "/proj/wiki/concepts/rope.md")
+
+ expect(mockDeleteFile).toHaveBeenCalledTimes(1)
+ expect(mockDeleteFile).toHaveBeenCalledWith("/proj/wiki/concepts/rope.md")
+
+ await cascadeDeleteWikiPage("/proj", "/proj/wiki/entities/transformer.md")
+ expect(mockDeleteFile).toHaveBeenCalledTimes(2)
+ expect(mockDeleteFile).toHaveBeenLastCalledWith("/proj/wiki/entities/transformer.md")
+
+ await cascadeDeleteWikiPage("/proj", "/proj/wiki/queries/some-query-2026-04-27-150000.md")
+ expect(mockDeleteFile).toHaveBeenCalledTimes(3)
+ })
+
+ it("media-cascade tolerates a missing media directory (no images were extracted)", async () => {
+ // Most wiki/sources/ pages won't have an associated media/<slug>/
+ // — only PDF/PPTX/DOCX sources with embedded images do. The
+ // delete attempt fails with ENOENT and we swallow it silently.
+ mockDeleteFile
+ .mockResolvedValueOnce(undefined) // source page delete: OK
+ .mockRejectedValueOnce(new Error("ENOENT: no such directory")) // media: doesn't exist
+
+ // Should NOT throw — media absence is normal.
+ await expect(
+ cascadeDeleteWikiPage("/proj", "/proj/wiki/sources/text-only-source.md"),
+ ).resolves.toBeUndefined()
+ // Both attempts happened.
+ expect(mockDeleteFile).toHaveBeenCalledTimes(2)
+ // Embedding cascade still ran in between.
+ expect(mockRemovePageEmbedding).toHaveBeenCalledWith("/proj", "text-only-source")
+ })
+
+ it("handles Windows backslash paths in the source-page detection", async () => {
+ // sources-view in some flows may pass paths that haven't been
+ // normalized yet. The detector flips backslashes via
+ // normalizePath before matching `/wiki/sources/`.
+ await cascadeDeleteWikiPage(
+ "C:/proj",
+ "C:\\proj\\wiki\\sources\\winsrc.md",
+ )
+
+ expect(mockDeleteFile).toHaveBeenCalledTimes(2)
+ // Second call is the media dir, normalized to forward slashes
+ // because we built it from project path + literal path.
+ expect(mockDeleteFile).toHaveBeenNthCalledWith(2, "C:/proj/wiki/media/winsrc")
+ })
+
+ it("does not attempt media deletion when slug is empty or hidden (defensive)", async () => {
+ // wiki/sources/.md → getFileStem returns ".md" (since lastIndexOf
+ // is at position 0, the function falls back to the full name).
+ // Without the dot-prefix guard we'd build a media path of
+ // `wiki/media/.md`, which is at best a leak and at worst risks
+ // touching dotfiles. Both `.md` (slug-from-pure-ext) and `.foo`
+ // (hidden-name) must be rejected by the media cascade even though
+ // the file delete still happens.
+ await cascadeDeleteWikiPage("/proj", "/proj/wiki/sources/.md")
+ expect(mockDeleteFile).toHaveBeenCalledTimes(1)
+ expect(mockDeleteFile).toHaveBeenCalledWith("/proj/wiki/sources/.md")
+
+ mockDeleteFile.mockClear()
+ await cascadeDeleteWikiPage("/proj", "/proj/wiki/sources/.hidden.md")
+ expect(mockDeleteFile).toHaveBeenCalledTimes(1)
+ expect(mockDeleteFile).toHaveBeenCalledWith("/proj/wiki/sources/.hidden.md")
+ })
})
diff --git a/src/lib/wiki-page-delete.ts b/src/lib/wiki-page-delete.ts
index 98ea38bd..2a5ebc66 100644
--- a/src/lib/wiki-page-delete.ts
+++ b/src/lib/wiki-page-delete.ts
@@ -20,14 +20,33 @@
* next file in a batch, or surface to the user via toast).
*/
import { deleteFile } from "@/commands/fs"
-import { getFileStem } from "@/lib/path-utils"
+import { getFileStem, normalizePath } from "@/lib/path-utils"
import { removePageEmbedding } from "@/lib/embedding"
/**
- * Delete a wiki page from disk and drop its embedding chunks.
+ * Detect whether a wiki page lives under `wiki/sources/`. We treat
+ * those as source-summary pages — each owns its source's extracted
+ * images at `wiki/media/<slug>/`. Other wiki paths (concepts,
+ * entities, queries, …) don't own image directories of their own,
+ * so the media cascade is scoped to source pages only.
+ *
+ * Tolerates both `/` and `\` separators for Windows.
+ */
+function isSourcePage(pagePath: string): boolean {
+ const normalized = normalizePath(pagePath)
+ return normalized.includes("/wiki/sources/")
+}
+
+/**
+ * Delete a wiki page from disk and drop its embedding chunks. If the
+ * page is a source-summary (`wiki/sources/<slug>.md`), ALSO removes
+ * the corresponding `wiki/media/<slug>/` directory containing the
+ * source's extracted images — those are owned by the source and have
+ * no other consumer once the source page is gone.
*
* `projectPath` is the project root (used to scope the embedding
- * cascade to the right LanceDB instance).
+ * cascade to the right LanceDB instance, and to locate the media
+ * directory).
*
* `pagePath` may be absolute or relative; only its basename is used
* for the page-id lookup, so callers don't need to normalize before
@@ -43,4 +62,33 @@ export async function cascadeDeleteWikiPage(
if (slug.length > 0) {
await removePageEmbedding(projectPath, slug)
}
+
+ // Media cascade: source-summary deletion → drop the source's
+ // image directory. Done AFTER the file delete (and after the
+ // embedding cascade) so a failure in either of those — already
+ // best-effort tolerated by callers — leaves us in a consistent
+ // state. Failures here are logged + swallowed because the source
+ // page is already gone; an orphaned media directory is a leak,
+ // not a correctness problem.
+ //
+ // Defensive on the slug: must be non-empty AND not start with `.`
+ // — a path like `wiki/sources/.md` (pure extension, no name) or
+ // `.git`-style hidden entries should NEVER produce a media path
+ // that resolves to a hidden directory under `wiki/media/`. The
+ // worst case (slug == ".") would target `wiki/media/.` and delete
+ // the entire media root.
+ if (isSourcePage(pagePath) && slug.length > 0 && !slug.startsWith(".")) {
+ const pp = normalizePath(projectPath)
+ const mediaDir = `${pp}/wiki/media/${slug}`
+ try {
+ // delete_file in fs.rs auto-detects directories and uses
+ // remove_dir_all under the hood — see fs.rs L989. So a single
+ // deleteFile call handles "may or may not be present" + "may
+ // or may not be a directory" gracefully.
+ await deleteFile(mediaDir)
+ } catch {
+ // Most common cause: the directory never existed because no
+ // images were extracted from this source. Not an error.
+ }
+ }
}