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

Harden nested source identity handling

nash_sue3403cb76f27
diff --git a/src/lib/ingest-source-path-collision.test.ts b/src/lib/ingest-source-path-collision.test.ts
index db540ba1..0124ae1d 100644
--- a/src/lib/ingest-source-path-collision.test.ts
+++ b/src/lib/ingest-source-path-collision.test.ts
@@ -168,6 +168,43 @@ describe("autoIngest source summary paths", () => {
expect(allSummaries).toContain("project-b/config.yaml")
})
+ it("migrates a safe legacy basename source summary to the canonical nested source path", async () => {
+ if (!tmp) throw new Error("missing temp project")
+ sourceMarkers = ["project-a config"]
+ await fs.rm(path.join(tmp.path, "raw", "sources", "project-b", "config.yaml"))
+
+ const legacySummaryPath = path.join(tmp.path, "wiki", "sources", "config.md")
+ await writeFileRaw(
+ legacySummaryPath,
+ [
+ "---",
+ 'title: "Source: config.yaml"',
+ 'sources: ["config.yaml"]',
+ "---",
+ "",
+ "# Legacy config",
+ "",
+ "Legacy source summary body.",
+ ].join("\n"),
+ )
+
+ await autoIngest(
+ tmp.path,
+ `${tmp.path}/raw/sources/project-a/config.yaml`,
+ useWikiStore.getState().llmConfig,
+ undefined,
+ "project-a",
+ )
+
+ const canonicalSummary = `wiki/sources/${sourceSummarySlugFromIdentity("project-a/config.yaml")}.md`
+ const canonicalSummaryPath = path.join(tmp.path, canonicalSummary)
+ const content = await fs.readFile(canonicalSummaryPath, "utf8")
+
+ await expect(fs.access(legacySummaryPath)).rejects.toThrow()
+ expect(content).toContain('sources: ["project-a/config.yaml"]')
+ expect(content).toContain("project-a config")
+ })
+
it("canonicalizes interactive source summary paths and sources frontmatter", async () => {
if (!tmp) throw new Error("missing temp project")
diff --git a/src/lib/ingest.ts b/src/lib/ingest.ts
index 5b5ade67..3557b142 100644
--- a/src/lib/ingest.ts
+++ b/src/lib/ingest.ts
@@ -1,4 +1,4 @@
-import { readFile, writeFile, listDirectory } from "@/commands/fs"
+import { deleteFile, fileExists, readFile, writeFile, listDirectory } from "@/commands/fs"
import { streamChat } from "@/lib/llm-client"
import type { LlmConfig } from "@/stores/wiki-store"
import { useWikiStore } from "@/stores/wiki-store"
@@ -15,6 +15,7 @@ import { checkIngestCache, saveIngestCache } from "@/lib/ingest-cache"
import { sanitizeIngestedFileContent } from "@/lib/ingest-sanitize"
import { mergePageContent, type MergeFn } from "@/lib/page-merge"
import { withProjectLock } from "@/lib/project-mutex"
+import type { FileNode } from "@/types/wiki"
import {
extractAndSaveSourceImages,
buildImageMarkdownSection,
@@ -628,6 +629,7 @@ async function autoIngestImpl(
// ── Step 3: Write files ───────────────────────────────────────
activity.updateItem(activityId, { detail: "Writing files..." })
+ await migrateLegacySourceSummaryIfSafe(pp, sourceIdentity, sourceSummaryPath)
const { writtenPaths, warnings: writeWarnings, hardFailures } = await writeFileBlocks(
pp,
generation,
@@ -836,6 +838,101 @@ function canonicalizeSourcesField(content: string, sourceIdentity: string): stri
return writeSources(content, deduped)
}
+async function migrateLegacySourceSummaryIfSafe(
+ projectPath: string,
+ sourceIdentity: string,
+ sourceSummaryPath: string,
+): Promise<void> {
+ const normalizedIdentity = normalizePath(sourceIdentity)
+ if (!normalizedIdentity.includes("/")) return
+
+ const basename = getFileName(normalizedIdentity)
+ const legacySlug = basename.replace(/\.[^.]+$/, "")
+ const legacyPath = `wiki/sources/${legacySlug}.md`
+ if (legacyPath === sourceSummaryPath) return
+
+ const pp = normalizePath(projectPath)
+ const legacyFullPath = `${pp}/${legacyPath}`
+ const canonicalFullPath = `${pp}/${sourceSummaryPath}`
+
+ const matchingIdentities = await matchingRawSourceIdentitiesForBasename(pp, basename)
+ const normalizedIdentityKey = normalizedIdentity.toLowerCase()
+ if (
+ matchingIdentities.length !== 1 ||
+ normalizePath(matchingIdentities[0]).toLowerCase() !== normalizedIdentityKey
+ ) {
+ return
+ }
+
+ try {
+ if (await fileExists(canonicalFullPath)) return
+ if (await fileExists(`${pp}/raw/sources/${basename}`)) return
+ } catch {
+ return
+ }
+
+ const legacyContent = await tryReadFile(legacyFullPath)
+ if (!legacyContent) return
+
+ const sources = parseSources(legacyContent)
+ const basenameKey = basename.toLowerCase()
+ const legacyOnlyReferencesBasename =
+ sources.length > 0 &&
+ sources.every(
+ (source) =>
+ !normalizePath(source).includes("/") &&
+ getFileName(source).toLowerCase() === basenameKey,
+ )
+ if (!legacyOnlyReferencesBasename) return
+
+ try {
+ await writeFile(canonicalFullPath, canonicalizeSourcesField(legacyContent, sourceIdentity))
+ await deleteFile(legacyFullPath)
+ } catch (err) {
+ console.warn(
+ `[ingest] failed to migrate legacy source summary ${legacyPath} -> ${sourceSummaryPath}:`,
+ err instanceof Error ? err.message : err,
+ )
+ }
+}
+
+async function matchingRawSourceIdentitiesForBasename(
+ projectPath: string,
+ basename: string,
+): Promise<string[]> {
+ const rawRoot = `${projectPath}/raw/sources`
+ let nodes: FileNode[]
+ try {
+ nodes = await listDirectory(rawRoot)
+ } catch {
+ return []
+ }
+
+ const rootPrefix = `${normalizePath(rawRoot).replace(/\/+$/, "")}/`
+ const rootPrefixKey = rootPrefix.toLowerCase()
+ const basenameKey = basename.toLowerCase()
+ const matches: string[] = []
+
+ const visit = (items: FileNode[]) => {
+ for (const item of items) {
+ if (item.is_dir) {
+ if (item.children) visit(item.children)
+ continue
+ }
+ const normalizedPath = normalizePath(item.path)
+ if (
+ getFileName(normalizedPath).toLowerCase() === basenameKey &&
+ normalizedPath.toLowerCase().startsWith(rootPrefixKey)
+ ) {
+ matches.push(normalizedPath.slice(rootPrefix.length))
+ }
+ }
+ }
+
+ visit(nodes)
+ return matches
+}
+
async function writeFileBlocks(
projectPath: string,
text: string,
diff --git a/src/lib/source-identity.test.ts b/src/lib/source-identity.test.ts
index 5871f8d5..beb38ff9 100644
--- a/src/lib/source-identity.test.ts
+++ b/src/lib/source-identity.test.ts
@@ -21,6 +21,18 @@ describe("source identity helpers", () => {
)
})
+ it("matches raw/sources prefixes case-insensitively while preserving returned case", () => {
+ expect(
+ sourceIdentityForPath(
+ "C:/Users/Me/Wiki",
+ "c:/users/me/wiki/raw/sources/Project-A/Config.yaml",
+ ),
+ ).toBe("Project-A/Config.yaml")
+ expect(sourceReferenceIdentity("RAW/SOURCES/Project-A/Config.yaml")).toBe(
+ "Project-A/Config.yaml",
+ )
+ })
+
it("keeps legacy basename slugs for root-level sources", () => {
expect(sourceSummarySlugFromIdentity("config.yaml")).toBe("config")
})
diff --git a/src/lib/source-identity.ts b/src/lib/source-identity.ts
index c9cfe509..21031b06 100644
--- a/src/lib/source-identity.ts
+++ b/src/lib/source-identity.ts
@@ -7,25 +7,29 @@ export function sourceIdentityForPath(projectPath: string, sourcePath: string):
const pp = normalizePath(projectPath).replace(/\/+$/, "")
const sp = normalizePath(sourcePath)
const projectRawSourcesPrefix = `${pp}/${RAW_SOURCES_PREFIX}`
- if (sp.startsWith(projectRawSourcesPrefix)) {
+ const spKey = sp.toLowerCase()
+ if (spKey.startsWith(projectRawSourcesPrefix.toLowerCase())) {
return sp.slice(projectRawSourcesPrefix.length)
}
- if (sp.startsWith(RAW_SOURCES_PREFIX)) {
+ if (spKey.startsWith(RAW_SOURCES_PREFIX)) {
return sp.slice(RAW_SOURCES_PREFIX.length)
}
- if (sp.includes(RAW_SOURCES_MARKER)) {
- return sp.slice(sp.indexOf(RAW_SOURCES_MARKER) + RAW_SOURCES_MARKER.length)
+ const markerIndex = spKey.indexOf(RAW_SOURCES_MARKER)
+ if (markerIndex >= 0) {
+ return sp.slice(markerIndex + RAW_SOURCES_MARKER.length)
}
return getFileName(sp)
}
export function sourceReferenceIdentity(sourceReference: string): string {
const ref = normalizePath(sourceReference)
- if (ref.startsWith(RAW_SOURCES_PREFIX)) {
+ const refKey = ref.toLowerCase()
+ if (refKey.startsWith(RAW_SOURCES_PREFIX)) {
return ref.slice(RAW_SOURCES_PREFIX.length)
}
- if (ref.includes(RAW_SOURCES_MARKER)) {
- return ref.slice(ref.indexOf(RAW_SOURCES_MARKER) + RAW_SOURCES_MARKER.length)
+ const markerIndex = refKey.indexOf(RAW_SOURCES_MARKER)
+ if (markerIndex >= 0) {
+ return ref.slice(markerIndex + RAW_SOURCES_MARKER.length)
}
return ref
}
diff --git a/src/test-helpers/fs-temp.ts b/src/test-helpers/fs-temp.ts
index 290926e4..09e68c98 100644
--- a/src/test-helpers/fs-temp.ts
+++ b/src/test-helpers/fs-temp.ts
@@ -69,6 +69,12 @@ export const realFs = {
deleteFile: async (p: string): Promise<void> => {
await fs.unlink(p).catch(() => {})
},
+ fileExists: async (p: string): Promise<boolean> => {
+ return fs.access(p).then(
+ () => true,
+ () => false,
+ )
+ },
findRelatedWikiPages: async (): Promise<string[]> => {
return []
},