WeKnora icon indicating copy to clipboard operation
WeKnora copied to clipboard

[Bug]: Deleting a source document deletes wiki pages that other pages still link to — 1,267 dangling links, then AutoFix flattens them permanently

Open JSJ189 opened this issue 1 week ago • 0 comments

Problem

On a 2,604-page Wiki (engineering-drawing knowledge base, PostgreSQL, v0.9.1 71c32af9), GET /knowledgebase/{kb}/wiki/lint returned health_score: 0 with 1,267 broken_link errors. Investigated, and the failures are not bad links — they are pages that were deleted while other pages still linked to them.

Facts:

  • 635 pages were soft-deleted in a single day (2026-11-17): 478 concept, 156 entity, 1 summary.
  • Every one of them was single-source: expanding their source_refs gives 635 references, and all 635 point at knowledge_ids that no longer exist in knowledges (doc_alive = 0, doc_gone = 635).
  • The 1,267 dangling links point at 266 distinct slugs. Top targets:
target slug title dangling links
concept/danwei 单位 89
concept/tu-zhi-suo-shi 图纸所示 79
concept/guishu 归属 65
concept/zimu-mingcheng 子目名称 64
concept/zimu 子目 58
concept/lai-yuan 来源 44
entity/hun-ning-tu 混凝土 39
concept/yanghu 养护 34

These are generic field pages — every bill-of-quantities page in the KB is written as - [[concept/danwei|单位]]:m³. They carry no document-specific content, but they are owned by whichever document happened to be extracted first. Delete (or replace) that one document and the page disappears, taking dozens of references from other documents with it.

Then the second half of the damage: POST /wiki/auto-fix did what it says — {"fixed":1268} — and flattened all 1,267 [[slug|文字]] into plain text. Preserved text, lost edge. There is no way afterwards to tell a flattened reference from authored prose, and the KB is left with health_score: 100 while its graph has 1,267 fewer edges.

Root cause

Page liveness is decided by who contributed content, never by who links to the page.

internal/application/service/knowledge_delete.go:294-311 (delete path):

remaining := removeSourceRef(page.SourceRefs, knowledgeID)
if len(remaining) == 0 {
    s.wikiService.DeletePage(ctx, kbID, page.Slug)   // last source gone → page deleted
} else { /* strip the source, keep the page */ }

internal/application/service/wiki_ingest.go:2157-2163 (re-ingest path) states the same policy in prose:

// - Single-source stale pages → deleted
// - Multi-source stale pages → LLM retract to clean content synchronously

in_links is maintained and is not consulted at either site.

internal/application/service/wiki_lint.go:361-381 (AutoFix) rewrites each dangling link to its display text and never tries to repair it, although internal/application/service/slug_fuzzy.go:30-33 says cleanup callers must:

// Cleanup callers (stripDeadWikiLinks, cleanDeadLinks) consult
// resolveDeadSlug FIRST and only fall back to "strip to plain text"

Why this is not #5182 / #5172 / #5093 / #5161

  • #5182 (re-ingest blanks pages) — the pages stay, keep their title, and keep their in_links; recovery is per-page via revisions. Here the pages are gone and every referrer is stale.
  • #5172 (closed: AutoFix should repair before flattening) — still true at 0.9.1. Only the unconditional strings.ReplaceAll was replaced by a Markdown-aware rewrite; resolveDeadSlug is still never called from AutoFix. Our run flattened 1,267 links, and I could not find a live near-match for any of the top eight targets, so nothing was repairable in this KB — but the code path still destroys repairable links when one exists.
  • #5093 — folders left behind by a deleted document; different object.
  • #5161 — missing merge / batch-delete / soft-delete-restore ops; here the pages were deleted by the product itself.

Impact

  • Wiki search returns pages whose bodies now read as plain text where the graph used to point somewhere; readers cannot tell "单位" was once a link.
  • 563 → 560 orphan_page warnings stay, because pages only reachable through the deleted hub lose their inbound edge too.
  • Generic field pages are the worst case by construction: one deletion costs N links in unrelated documents, and the loss is proportional to how many documents share the vocabulary.

Proposal (any one closes the data loss; the first two also prevent it)

  1. Consider referrers before deleting. When a page's last content source disappears, check in_links: if it is non-empty, keep the page as unsourced (empty source_refs, content untouched) instead of deleting it, or
  2. Repoint referrers first: queue a retract/repair job that runs resolveDeadSlug over the live referrers, then delete.
  3. Make AutoFix call resolveDeadSlug before stripping — completes #5172 and shrinks this failure to "genuinely unrecoverable links".
  4. Optional: expose ?force= and refuse to delete a page with in_links > 0 unless forced, so bulk document replacement (which is what produced the 635 deletions) cannot silently gut the graph.

Secondary observation (mechanism unverified, may deserve its own issue)

auto-fix is not idempotent in one pass. First run on this KB: {"fixed":1268}; immediately re-linting still reported exactly one broken_link (concept/jiliang-guize → concept/zha-gai); a second run returned {"fixed":1} and the lint went to 0 errors. Same input, different outcome, so one fix per pass can be lost — AutoFix's read-modify-write loop (GetPageBySlug → rewrite → UpdateAutoLinkedContent, with the update error swallowed by if err == nil { fixed++ }) is worth a look.

Environment

  • v0.9.1 (71c32af9), PostgreSQL 17, storage/Qdrant unchanged
  • KB: 2,604 live wiki pages (concept 1,152 / entity 1,446 / index 1 / summary 5), 42,816 chunks, 5 source documents
  • Reproduced through the product's own endpoints only: GET /wiki/lint, POST /wiki/auto-fix, GET /wiki/search

JSJ189 avatar Nov 27 '26 01:11 JSJ189