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: prevent page merge scaffolding leakage
nash_su8f2cf9e367bf
diff --git a/src/lib/ingest.prompt.test.ts b/src/lib/ingest.prompt.test.tsindex 241875c1..57e77ac6 100644--- a/src/lib/ingest.prompt.test.ts+++ b/src/lib/ingest.prompt.test.ts@@ -189,11 +189,15 @@ describe("analysis + generation prompt consistency", () => {describe("page merge prompt", () => {it("keeps comparisons attribution-exact instead of folding them into the main subject", () => {const prompt = buildPageMergeSystemPrompt()- expect(prompt).toContain("Both versions target the same wiki page")+ expect(prompt).toContain("inputs are internal containers, not revisions or competing versions")expect(prompt).toContain("may mention additional subjects for comparison or context")expect(prompt).toContain("keep those comparisons attribution-exact")expect(prompt).toContain("do not fold them into claims about the main page subject")expect(prompt).toContain("prefer keeping them separate")+ expect(prompt).toContain("attribute a claim to a real source filename only when the supporting file is unambiguous")+ expect(prompt).toContain("otherwise do not guess")+ expect(prompt).toContain("Never create comparison sections/tables about the merge inputs")+ expect(prompt).toContain("Never invent URLs")expect(prompt).not.toContain("describe the same entity")})})diff --git a/src/lib/ingest.ts b/src/lib/ingest.tsindex 3e09f4b5..2b12ddd5 100644--- a/src/lib/ingest.ts+++ b/src/lib/ingest.ts@@ -3078,15 +3078,31 @@ async function analyzeLongSourceInChunks(function buildPageMerger(llmConfig: LlmConfig): MergeFn {return async (existingContent, incomingContent, sourceFileName, signal) => {const systemPrompt = buildPageMergeSystemPrompt()+ const sanitizeProvenance = (value: string) => value+ .replace(/[\r\n#`]+/g, " ")+ .replace(/---+/g, " ")+ .replace(/\s+/g, " ")+ .trim()+ .slice(0, 240)+ const safeSourceFileName = sanitizeProvenance(sourceFileName)+ const existingSources = parseFrontmatter(existingContent).frontmatter?.sources+ const existingProvenance = Array.isArray(existingSources)+ ? existingSources+ .filter((source): source is string => typeof source === "string")+ .map(sanitizeProvenance)+ .filter(Boolean)+ .join(", ")+ .slice(0, 1_000)+ : "sources listed in the existing page frontmatter"const userMessage = [- `## Existing version on disk`,+ `## Previously collected material (provenance: ${existingProvenance || "existing page sources"})`,"",existingContent,"","---","",- `## Newly generated version (from ${sourceFileName})`,+ `## Additional material (provenance: ${safeSourceFileName || "additional source"})`,"",incomingContent,"",@@ -3130,21 +3146,23 @@ function buildPageMerger(llmConfig: LlmConfig): MergeFn {export function buildPageMergeSystemPrompt(): string {return [- "You are merging two versions of the same wiki page into one coherent document.",- "Both versions target the same wiki page; one is already on disk,",- "the other was just generated from a different source document.",- "Either version may mention additional subjects for comparison or context.",+ "You are merging source-backed material for the same wiki page into one coherent document.",+ "The inputs are internal containers, not revisions or competing versions.",+ "Either input may mention additional subjects for comparison or context.","","Output ONE merged version that:",- "- Preserves every factual claim from both versions (do not drop content)",- "- Eliminates redundancy when both versions state the same fact",- "- Preserves subject/source boundaries: if either version mentions other entities/models/products/methods for comparison, keep those comparisons attribution-exact and do not fold them into claims about the main page subject",- "- When claims conflict or apply to different subjects, keep them separated and say which source version supports each one instead of synthesizing a single generalized conclusion",+ "- Preserves every factual claim from both inputs (do not drop content)",+ "- Eliminates redundancy when both inputs state the same fact",+ "- Preserves subject/source boundaries: if either input mentions other entities/models/products/methods for comparison, keep those comparisons attribution-exact and do not fold them into claims about the main page subject",+ "- When claims conflict or apply to different subjects, keep them separated; attribute a claim to a real source filename only when the supporting file is unambiguous, otherwise do not guess","- When in doubt whether two similar-looking claims describe the same fact, prefer keeping them separate","- Reorganizes sections so the structure is logical for the merged topic,"," not just a concatenation of the two inputs","- Uses consistent markdown structure (headings, tables, lists, callouts)","- Keeps `[[wikilink]]` references intact",+ "- Never describe the merge inputs as existing/new/original/old versions in headings, prose, notes, or table columns",+ "- Never create comparison sections/tables about the merge inputs themselves",+ "- Never invent URLs, citations, source names, or placeholder references","","Output requirements:","- The FIRST character of your response MUST be `-` (the opening of `---`)",diff --git a/src/lib/page-merge.test.ts b/src/lib/page-merge.test.tsindex f1a8acbd..797d3be0 100644--- a/src/lib/page-merge.test.ts+++ b/src/lib/page-merge.test.ts@@ -8,7 +8,7 @@* actual generation model.*/import { describe, it, expect, vi } from "vitest"-import { mergePageContent } from "./page-merge"+import { mergePageContent, stripMergeScaffolding } from "./page-merge"const PAGE = (fm: string, body: string) => `---\n${fm}\n---\n\n${body}`@@ -203,6 +203,116 @@ describe("mergePageContent — LLM merge", () => {expect(out).toContain("retained [1] with sources [2], [3].")})+ it("strips internal version labels from accepted merged content", async () => {+ const existing = PAGE("type: entity\ntitle: Foo", "Existing detailed evidence for the topic.")+ const incoming = PAGE("type: entity\ntitle: Foo", "Incoming detailed evidence for the topic.")+ const merger = vi.fn().mockResolvedValue(PAGE(+ "type: entity\ntitle: Foo",+ "## Classification(新生成版本)\n\nRetained evidence.\n\n*(来源:现有版本)*",+ ))+ const out = await mergePageContent(incoming, existing, merger, baseOpts)+ expect(out).toContain("## Classification")+ expect(out).not.toMatch(/新生成版本|现有版本/)+ })++ it("removes reserved example-domain URLs invented by the merge model", async () => {+ const existing = PAGE("type: entity\ntitle: Foo", "Existing detailed evidence for the topic.")+ const incoming = PAGE("type: entity\ntitle: Foo", "Incoming detailed evidence for the topic.")+ const merger = vi.fn().mockResolvedValue(PAGE(+ "type: entity\ntitle: Foo",+ "Existing and incoming evidence retained in detail.\n\n## References\n- https://example.com/version1",+ ))+ const backup = vi.fn().mockResolvedValue(undefined)+ const out = await mergePageContent(incoming, existing, merger, { ...baseOpts, backup })+ expect(out).toContain("Existing and incoming evidence retained in detail.")+ expect(out).not.toContain("example.com")+ expect(backup).not.toHaveBeenCalled()+ })++ it("removes only the invented URL range and preserves legitimate longer URLs", async () => {+ const legitimate = "https://example.com/docs/v1/guide"+ const existing = PAGE("type: entity\ntitle: Foo", `Existing evidence at ${legitimate}.`)+ const incoming = PAGE("type: entity\ntitle: Foo", "Incoming detailed evidence for the topic.")+ const merger = vi.fn().mockResolvedValue(PAGE(+ "type: entity\ntitle: Foo",+ `Existing evidence at ${legitimate}.\n\nIncoming detailed evidence.\n\n[Invented citation](https://example.com/docs/v1)`,+ ))+ const out = await mergePageContent(incoming, existing, merger, baseOpts)+ expect(out).toContain(legitimate)+ expect(out).toContain("Invented citation")+ expect(out).not.toContain("[Invented citation]()")+ expect(out).not.toContain("https://example.com/docs/v1)")+ })++ it("preserves lookalike domains while removing reserved subdomains", async () => {+ const existing = PAGE("type: entity\ntitle: Foo", "Existing detailed evidence for the topic.")+ const incoming = PAGE("type: entity\ntitle: Foo", "Incoming detailed evidence for the topic.")+ const merger = vi.fn().mockResolvedValue(PAGE(+ "type: entity\ntitle: Foo",+ [+ "Existing and incoming evidence retained in detail.",+ "https://notexample.com/reference",+ "https://example.com.evil.test/reference",+ "<https://docs.example.org/invented>",+ ].join("\n"),+ ))+ const out = await mergePageContent(incoming, existing, merger, baseOpts)+ expect(out).toContain("https://notexample.com/reference")+ expect(out).toContain("https://example.com.evil.test/reference")+ expect(out).not.toContain("docs.example.org")+ expect(out).not.toContain("<>")+ })++ it("preserves version-labelled headings that were already in source material", async () => {+ const existing = PAGE("type: entity\ntitle: Foo", "## 旧版本\n\nHistorical product edition details.")+ const incoming = PAGE("type: entity\ntitle: Foo", "Incoming detailed evidence for the topic.")+ const merger = vi.fn().mockResolvedValue(PAGE(+ "type: entity\ntitle: Foo",+ "## 旧版本\n\nHistorical product edition details.\n\n## Analysis(新生成版本)\n\nIncoming detailed evidence.",+ ))+ const out = await mergePageContent(incoming, existing, merger, baseOpts)+ expect(out).toContain("## 旧版本")+ expect(out).toContain("## Analysis\n")+ expect(out).not.toContain("Analysis(新生成版本)")+ })++ it("preserves legitimate version headings when the model changes heading depth", async () => {+ const existing = PAGE("type: entity\ntitle: Foo", "## iPhone 12 (old version)\n\nHistorical product details.")+ const incoming = PAGE("type: entity\ntitle: Foo", "Incoming detailed evidence for the topic.")+ const merger = vi.fn().mockResolvedValue(PAGE(+ "type: entity\ntitle: Foo",+ "### iPhone 12 (old version)\n\nHistorical product details.\n\nIncoming detailed evidence.",+ ))+ const out = await mergePageContent(incoming, existing, merger, baseOpts)+ expect(out).toContain("### iPhone 12 (old version)")+ })++ it("cleans invented Markdown URL forms without changing fenced examples", async () => {+ const existing = PAGE("type: entity\ntitle: Foo", "Existing detailed evidence for the topic.")+ const incoming = PAGE("type: entity\ntitle: Foo", "Incoming detailed evidence for the topic.")+ const merger = vi.fn().mockResolvedValue(PAGE(+ "type: entity\ntitle: Foo",+ [+ "Existing and incoming evidence retained in detail.",+ "",+ "[citation](https://example.org/page \"Title\")",+ "[1]: https://example.net/reference",+ "- https://example.com/bare",+ "```sh",+ "curl https://api.example.com/example",+ "```",+ ].join("\n"),+ ))+ const out = await mergePageContent(incoming, existing, merger, baseOpts)+ expect(out).toContain("diagram")+ expect(out).toContain("citation")+ expect(out).not.toContain("!diagram")+ expect(out).not.toContain("[citation](")+ expect(out).not.toMatch(/^\[1\]:\s*$/m)+ expect(out).not.toMatch(/^[-*+]\s*$/m)+ expect(out).toContain("curl https://api.example.com/example")+ })+it("strips directory prefixes from merged body wikilinks only", async () => {const existing = PAGE("type: entity\ntitle: Foo",@@ -229,7 +339,7 @@ describe("mergePageContent — LLM merge", () => {"~~~"," [[examples/indented-code-link]]","Keep escaped \\[[examples/escaped-link]] unchanged.",- "Keep URI-like [[https://example.com/wiki/page]] targets unchanged.",+ "Keep URI-like [[https://developer.mozilla.org/wiki/page]] targets unchanged.","Keep attachment [[attachments/report.pdf]] targets unchanged.",].join("\n")const merger = vi.fn().mockResolvedValue(@@ -251,11 +361,46 @@ describe("mergePageContent — LLM merge", () => {expect(out).toContain("[[examples/tilde-fenced-link]]")expect(out).toContain(" [[examples/indented-code-link]]")expect(out).toContain("\\[[examples/escaped-link]]")- expect(out).toContain("[[https://example.com/wiki/page]]")+ expect(out).toContain("[[https://developer.mozilla.org/wiki/page]]")expect(out).toContain("[[attachments/report.pdf]]")})})+describe("stripMergeScaffolding", () => {+ it("removes source-version notes and heading labels without changing factual prose", () => {+ const input = [+ "## 分类标准(新生成版本)",+ "",+ "事实内容。",+ "",+ "*(来源:原始版本)*",+ "",+ "### GAP-3 — 旧版本",+ ].join("\n")+ expect(stripMergeScaffolding(input)).toBe([+ "## 分类标准",+ "",+ "事实内容。",+ "",+ "",+ "### GAP-3",+ ].join("\n"))+ })++ it.each([+ "The threshold (threshold = 0.5) remains factual.",+ "Values (gold standard) hold.",+ "The model (original paper by Smith) reported 90%.",+ "该方法(原始数据集)准确率 90%。",+ "[[page (old version)]]",+ "```js\nfoo(old_value)\n```",+ "---\ntitle: Foo (old)\nsources: [report (original).pdf]\n---\nBody",+ "## 旧版本",+ ])("preserves non-scaffolding content byte-identically: %s", (input) => {+ expect(stripMergeScaffolding(input)).toBe(input)+ })+})+// ──────────────────────────────────────────────────────────────────// LLM failure / sanity rejection — always falls back safely// ──────────────────────────────────────────────────────────────────diff --git a/src/lib/page-merge.ts b/src/lib/page-merge.tsindex 4883bc56..6f4d8cbf 100644--- a/src/lib/page-merge.ts+++ b/src/lib/page-merge.ts@@ -52,6 +52,7 @@ const LOCKED_FIELDS = ["type", "title", "created"] as const* compression while catching obvious truncation / lazy summaries.*/const BODY_SHRINK_THRESHOLD = 0.7+const HTTP_URL_RE = /https?:\/\/[^\s<>"'\])]+/giexport interface MergeFn {/**@@ -178,6 +179,12 @@ export async function mergePageContent(return normalizeMalformedWikilinks(arrayMerged)}+ llmOutput = removeIntroducedReservedExampleUrls(+ llmOutput,+ existingContent,+ arrayMerged,+ )+// Step 3 — apply deterministic post-processing: lock fields back// to existing values, force array fields to the union, set// updated to today.@@ -198,7 +205,143 @@ export async function mergePageContent(const todayFn = opts.today ?? defaultTodayfinal = setFrontmatterScalar(final, "updated", todayFn())- return normalizeMalformedWikilinks(stripBodyWikilinkPathPrefixes(final))+ return normalizeMalformedWikilinks(+ stripBodyWikilinkPathPrefixes(+ stripMergeScaffolding(final, [existingContent, arrayMerged]),+ ),+ )+}++interface ReservedUrlMatch {+ canonical: string+ raw: string+ start: number+ end: number+}++function reservedExampleUrls(content: string): ReservedUrlMatch[] {+ const urls: ReservedUrlMatch[] = []+ for (const match of content.matchAll(HTTP_URL_RE)) {+ if (match.index === undefined) continue+ const raw = match[0].replace(/[.,;:!?]+$/, "")+ try {+ const parsed = new URL(raw)+ const host = parsed.hostname.replace(/\.$/, "").toLowerCase()+ if (["example.com", "example.org", "example.net"].some(+ (domain) => host === domain || host.endsWith(`.${domain}`),+ )) urls.push({+ canonical: parsed.toString(),+ raw,+ start: match.index,+ end: match.index + raw.length,+ })+ } catch {+ // Invalid URLs are left to the ordinary Markdown/content validators.+ }+ }+ return urls+}++function removeIntroducedReservedExampleUrls(+ output: string,+ existing: string,+ incoming: string,+): string {+ const inputUrls = new Set([+ ...reservedExampleUrls(existing).map((match) => match.canonical),+ ...reservedExampleUrls(incoming).map((match) => match.canonical),+ ])+ const introduced = reservedExampleUrls(output)+ .filter((match) => !inputUrls.has(match.canonical))+ if (introduced.length === 0) return output+ console.warn("[page-merge] removed invented reserved example-domain URL(s) from merge output")+ const introducedUrls = new Set(introduced.map((match) => match.canonical))+ const frontmatter = output.match(/^---\r?\n[\s\S]*?\r?\n---(?:\r?\n|$)/)+ const prefix = frontmatter?.[0] ?? ""+ const body = output.slice(prefix.length)+ let fence: { marker: string; length: number } | null = null+ const cleanedBody = body.split(/(\r?\n)/).map((part) => {+ if (/^\r?\n$/.test(part)) return part+ const marker = part.match(/^ {0,3}(`{3,}|~{3,})/)+ if (marker) {+ if (!fence) fence = { marker: marker[1][0], length: marker[1].length }+ else if (+ marker[1][0] === fence.marker &&+ marker[1].length >= fence.length &&+ part.slice(marker[0].length).trim() === ""+ ) fence = null+ return part+ }+ if (fence || /^(?: {4}|\t)/.test(part)) return part++ const shouldRemove = (raw: string): boolean => {+ try {+ return introducedUrls.has(new URL(raw.replace(/[.,;:!?]+$/, "")).toString())+ } catch {+ return false+ }+ }+ let line = part.replace(+ /!?\[([^\]\r\n]+)\]\((https?:\/\/[^\s<>")]+)(?:\s+["'][^"']*["'])?\)/gi,+ (whole, label: string, url: string) => shouldRemove(url) ? label : whole,+ )+ line = line.replace(/<(https?:\/\/[^\s<>]+)>/gi, (whole, url: string) => (+ shouldRemove(url) ? "" : whole+ ))+ line = line.replace(HTTP_URL_RE, (url) => shouldRemove(url) ? "" : url)+ if (/^\s*\[[^\]]+\]:\s*$/.test(line) || /^\s*[-*+]\s*$/.test(line)) return ""+ return line.replace(/[ \t]+$/, "")+ }).join("")+ return `${prefix}${cleanedBody}`+}++export function stripMergeScaffolding(+ content: string,+ protectedInputs: string[] = [],+): string {+ const frontmatter = content.match(/^---\r?\n[\s\S]*?\r?\n---(?:\r?\n|$)/)+ const prefix = frontmatter?.[0] ?? ""+ const body = content.slice(prefix.length)+ let fence: { marker: string; length: number } | null = null+ const label = "(?:现有|新生成|新|原始|旧|已有)(?:内容|版本|材料)(?:的?(?:定义|说明|描述))?|(?:补充材料|此前收集的材料)|(?:existing|newly generated|new|original|old|incoming|additional|previously collected) (?:content|version|material)(?: (?:definition|description))?"+ const sourceNote = new RegExp(+ `^[ \\t]*(?:[-*][ \\t]*)?\\*?[((]?[ \\t]*(?:来源|source)[ \\t]*[::][ \\t]*(?:${label})[ \\t]*[))]?\\*?[ \\t]*$`,+ "i",+ )+ const bracketedSuffix = new RegExp(`[ \\t]*[((【][ \\t]*(?:${label})[ \\t]*[))】][ \\t]*$`, "i")+ const bareSuffix = new RegExp(`^(#{1,6}\\s+.+?)[ \\t]+[-—:][ \\t]*(?:${label})[ \\t]*$`, "i")+ const protectedLines = new Set(+ protectedInputs.flatMap((input) => input.split(/\r?\n/).map((line) => line.trim())),+ )+ const protectedHeadings = new Set(+ protectedInputs.flatMap((input) => input.split(/\r?\n/))+ .map((line) => line.match(/^#{1,6}\s+(.+?)\s*$/)?.[1])+ .filter((heading): heading is string => Boolean(heading)),+ )+ const cleanedBody = body.replace(/.*(?:\r?\n|$)/g, (line) => {+ const lineContent = line.replace(/\r?\n$/, "")+ const newline = line.slice(lineContent.length)+ const marker = lineContent.match(/^ {0,3}(`{3,}|~{3,})/)+ if (marker) {+ if (!fence) fence = { marker: marker[1][0], length: marker[1].length }+ else if (+ marker[1][0] === fence.marker &&+ marker[1].length >= fence.length &&+ lineContent.slice(marker[0].length).trim() === ""+ ) fence = null+ return line+ }+ if (fence || /^(?: {4}|\t)/.test(lineContent)) return line+ if (protectedLines.has(lineContent.trim())) return line+ if (sourceNote.test(lineContent)) return ""+ if (!/^#{1,6}\s+/.test(lineContent)) return line+ const headingText = lineContent.replace(/^#{1,6}\s+/, "").trim()+ if (protectedHeadings.has(headingText)) return line+ const withoutBracket = lineContent.replace(bracketedSuffix, "")+ const withoutBare = withoutBracket.replace(bareSuffix, "$1")+ return `${withoutBare}${newline}`+ })+ return `${prefix}${cleanedBody}`}/**