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: findRelatedWikiPages Strategy 3 scoped to the sources block

nash_su29fc53b301c6
diff --git a/src-tauri/src/commands/fs.rs b/src-tauri/src/commands/fs.rs
index 8d239a63..08916052 100644
--- a/src-tauri/src/commands/fs.rs
+++ b/src-tauri/src/commands/fs.rs
@@ -1050,12 +1050,54 @@ fn collect_related_pages(dir: &Path, source_name: &str, results: &mut Vec<String
let is_source_summary = is_in_sources_dir
&& fname.to_lowercase().starts_with(&file_stem_lower);
- // Match 3: page was generated from this source (check frontmatter sources field)
- let frontmatter_match = if let Some(fm_start) = content.find("---\n") {
- if let Some(fm_end) = content[fm_start + 4..].find("\n---") {
- let frontmatter = &content[fm_start..fm_start + 4 + fm_end].to_lowercase();
- frontmatter.contains("sources:")
- && frontmatter.contains(&file_name_lower)
+ // Match 3: the page's *sources block* mentions the
+ // filename. Covers the multi-line YAML list form
+ //
+ // sources:
+ // - test.md (unquoted, missed by Match 1)
+ // - "other.md"
+ //
+ // Previous version substring-matched against the ENTIRE
+ // frontmatter, which false-positived whenever the
+ // filename happened to appear in title / description /
+ // any other field — those pages were then handed to
+ // the TS delete flow and, because their actual sources
+ // list didn't include the deleted file, silently
+ // wiped. Tightened: scope the substring check to the
+ // `sources:` block only (inline line + any indented
+ // continuation lines of a YAML list).
+ let frontmatter_match = if content.starts_with("---\n") {
+ if let Some(fm_end_rel) = content[4..].find("\n---") {
+ let frontmatter = &content[4..4 + fm_end_rel].to_lowercase();
+ let mut found = false;
+ let mut in_sources_block = false;
+ for line in frontmatter.split('\n') {
+ if line.starts_with("sources:") {
+ // Inline-form `sources: [...]` lives
+ // entirely on this one line; check it.
+ if line.contains(&file_name_lower) {
+ found = true;
+ break;
+ }
+ in_sources_block = true;
+ continue;
+ }
+ if in_sources_block {
+ // Continuation lines of a YAML list are
+ // indented; an un-indented line means
+ // we've left the sources block for
+ // another top-level field.
+ if line.is_empty() || line.starts_with(' ') || line.starts_with('\t') {
+ if line.contains(&file_name_lower) {
+ found = true;
+ break;
+ }
+ } else {
+ in_sources_block = false;
+ }
+ }
+ }
+ found
} else {
false
}
@@ -1228,4 +1270,207 @@ mod tests {
eprintln!("\n[pdf_probe] summary: {} OK / {} ERR / {} PANIC (total {})", ok, err, panicked, pdfs.len());
}
+
+ // ── collect_related_pages: regression coverage for the three match ─────
+ // strategies used by findRelatedWikiPages.
+ //
+ // Strategy 1: quoted filename anywhere in content
+ // (e.g. `sources: ["test.md"]` inline form)
+ // Strategy 2: page lives under wiki/sources/ and starts with file stem
+ // (the source summary page)
+ // Strategy 3: filename appears inside the frontmatter's sources BLOCK
+ // (tightened: no longer false-positives on `title:`
+ // `description:` or any other field that happens to
+ // include the filename as a substring)
+ //
+ // These tests are the regression guard for the Strategy 3 fix — before
+ // the tightening, a page whose title included the deleted filename
+ // would be surfaced here and then wrongly deleted downstream.
+
+ fn make_wiki(files: &[(&str, &str)]) -> std::path::PathBuf {
+ let dir = std::env::temp_dir().join(format!(
+ "wiki-test-{}",
+ std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .unwrap()
+ .as_nanos()
+ ));
+ fs::create_dir_all(&dir).unwrap();
+ for (rel, body) in files {
+ let p = dir.join(rel);
+ if let Some(parent) = p.parent() { fs::create_dir_all(parent).unwrap(); }
+ fs::write(&p, body).unwrap();
+ }
+ dir
+ }
+
+ fn collect(wiki: &std::path::Path, source: &str) -> Vec<String> {
+ let mut out = Vec::new();
+ collect_related_pages(wiki, source, &mut out).unwrap();
+ // Normalize to the wiki-relative suffix so assertions are
+ // independent of the temp prefix.
+ let prefix = wiki.to_string_lossy().replace('\\', "/");
+ out.into_iter()
+ .map(|p| {
+ let p = p.replace('\\', "/");
+ p.strip_prefix(&format!("{}/", prefix))
+ .map(str::to_string)
+ .unwrap_or(p)
+ })
+ .collect()
+ }
+
+ #[test]
+ fn collect_related_strategy1_inline_quoted_sources() {
+ let wiki = make_wiki(&[
+ (
+ "concepts/rope.md",
+ "---\ntitle: RoPE\nsources: [\"test.md\"]\n---\nbody\n",
+ ),
+ (
+ "concepts/unrelated.md",
+ "---\ntitle: Unrelated\nsources: [\"other.md\"]\n---\nbody\n",
+ ),
+ ]);
+ let mut got = collect(&wiki, "test.md");
+ got.sort();
+ assert_eq!(got, vec!["concepts/rope.md"]);
+ let _ = fs::remove_dir_all(&wiki);
+ }
+
+ #[test]
+ fn collect_related_strategy1_single_quoted_sources() {
+ let wiki = make_wiki(&[(
+ "concepts/rope.md",
+ "---\ntitle: RoPE\nsources: ['test.md']\n---\nbody\n",
+ )]);
+ let got = collect(&wiki, "test.md");
+ assert_eq!(got, vec!["concepts/rope.md"]);
+ let _ = fs::remove_dir_all(&wiki);
+ }
+
+ #[test]
+ fn collect_related_strategy2_source_summary_page() {
+ // A page inside wiki/sources/ whose filename starts with the
+ // deleted source's stem counts as the source-summary page —
+ // kept linked even if its sources field happens to be missing.
+ let wiki = make_wiki(&[
+ (
+ "sources/test.md",
+ "---\ntitle: Test Summary\n---\nbody\n",
+ ),
+ (
+ "concepts/unrelated.md",
+ "---\ntitle: Unrelated\nsources: [\"other.md\"]\n---\nbody\n",
+ ),
+ ]);
+ let got = collect(&wiki, "test.md");
+ assert_eq!(got, vec!["sources/test.md"]);
+ let _ = fs::remove_dir_all(&wiki);
+ }
+
+ #[test]
+ fn collect_related_strategy3_multi_line_yaml_list() {
+ // Multi-line YAML sources block with an unquoted entry — Strategy
+ // 1 can't see this (no quotes), Strategy 3 has to walk the
+ // sources block line by line.
+ let wiki = make_wiki(&[(
+ "concepts/rope.md",
+ "---\ntitle: RoPE\nsources:\n - test.md\n - \"other.md\"\ntags: []\n---\nbody\n",
+ )]);
+ let got = collect(&wiki, "test.md");
+ assert_eq!(got, vec!["concepts/rope.md"]);
+ let _ = fs::remove_dir_all(&wiki);
+ }
+
+ #[test]
+ fn collect_related_strategy3_does_not_false_positive_on_title_substring() {
+ // Regression guard for the bug we just fixed: a page whose
+ // title / description contains the deleted filename MUST NOT
+ // be surfaced when its actual sources list is unrelated.
+ // Before the fix, the whole frontmatter was substring-scanned
+ // and this page would have been returned → downstream delete
+ // flow → silent data loss on an innocent page.
+ let wiki = make_wiki(&[
+ (
+ "concepts/rope.md",
+ "---\ntitle: Analysis of test.md\ndescription: Discusses test.md in depth\nsources: [\"other.md\"]\n---\nbody\n",
+ ),
+ (
+ "concepts/real-match.md",
+ "---\ntitle: Real\nsources: [\"test.md\"]\n---\nbody\n",
+ ),
+ ]);
+ let got = collect(&wiki, "test.md");
+ // Only the real-match page is surfaced. The title-substring
+ // page is correctly ignored now.
+ assert_eq!(got, vec!["concepts/real-match.md"]);
+ let _ = fs::remove_dir_all(&wiki);
+ }
+
+ #[test]
+ fn collect_related_strategy3_stops_at_next_top_level_field() {
+ // Scan must stop at the next top-level YAML key so that a
+ // filename appearing in a later field (e.g. `notes:`) doesn't
+ // get pulled into the sources block.
+ let wiki = make_wiki(&[(
+ "concepts/rope.md",
+ "---\ntitle: RoPE\nsources:\n - other.md\nnotes: See test.md for context\n---\nbody\n",
+ )]);
+ let got = collect(&wiki, "test.md");
+ // sources block has only other.md; test.md appears in `notes:`
+ // which is outside the block — must not match.
+ assert!(got.is_empty(), "expected empty, got {got:?}");
+ let _ = fs::remove_dir_all(&wiki);
+ }
+
+ #[test]
+ fn collect_related_returns_empty_when_nothing_matches() {
+ let wiki = make_wiki(&[(
+ "concepts/unrelated.md",
+ "---\ntitle: X\nsources: [\"other.md\"]\n---\nbody\n",
+ )]);
+ let got = collect(&wiki, "nonexistent.md");
+ assert!(got.is_empty());
+ let _ = fs::remove_dir_all(&wiki);
+ }
+
+ #[test]
+ fn collect_related_skips_index_log_overview() {
+ // Listing pages (index.md, log.md, overview.md) reference the
+ // filename heavily but should never be returned here — they're
+ // cleaned separately via the TS cleanup helpers.
+ let wiki = make_wiki(&[
+ (
+ "index.md",
+ "---\ntitle: Index\n---\n- [[Test]]\nsources: [\"test.md\"]\n",
+ ),
+ (
+ "log.md",
+ "---\ntitle: Log\n---\nIngested test.md on 2026-01-01\n",
+ ),
+ (
+ "overview.md",
+ "---\ntitle: Overview\n---\nCovers test.md and other.md\n",
+ ),
+ (
+ "concepts/real.md",
+ "---\ntitle: Real\nsources: [\"test.md\"]\n---\nbody\n",
+ ),
+ ]);
+ let got = collect(&wiki, "test.md");
+ assert_eq!(got, vec!["concepts/real.md"]);
+ let _ = fs::remove_dir_all(&wiki);
+ }
+
+ #[test]
+ fn collect_related_case_insensitive_filename_match() {
+ let wiki = make_wiki(&[(
+ "concepts/rope.md",
+ "---\ntitle: RoPE\nsources: [\"Test.md\"]\n---\nbody\n",
+ )]);
+ let got = collect(&wiki, "test.md");
+ assert_eq!(got, vec!["concepts/rope.md"]);
+ let _ = fs::remove_dir_all(&wiki);
+ }
}