fix(wiki): harden slug integrity across ingest and agent write paths

Weak LLMs routinely mangle high-entropy wiki slugs (especially UUID-based
summary/<id> slugs), producing dead cross-links and cross-item dedup
hallucinations. This tightens integrity end to end:

- Ingest dedup now groups each new item with only its own trigram
  candidates and validates merges against that per-item scope, rejecting
  cross-item mispairings; candidate top-K narrowed 20 -> 5.
- Editor path aliases out-link slugs behind low-entropy ref-N tokens and
  translates them back after generation, removing the chance to mangle.
- Agent write tool normalizes/validates slugs, blocks manual creation of
  system-owned summary pages, and auto-repairs dead [[slug]] links
  (rewrite-only, never strips) via bigram/display-text resolution.
- llmresource registry aliases summary/<uuid> slugs the same way as
  storage handles so they round-trip safely through chat.

Adds unit tests for slug validation, aliasing round-trip, dead-link
repair, and dedup rejection rules.
This commit is contained in:
wizardchen
2026-07-24 15:06:49 +08:00
committed by lyingbug
parent d283fabc99
commit 5d0326f987
14 changed files with 750 additions and 68 deletions
+7
View File
@@ -39,6 +39,13 @@ type WikiPageService interface {
// GetPageBySlug retrieves a wiki page by its slug within a knowledge base.
GetPageBySlug(ctx context.Context, kbID string, slug string) (*types.WikiPage, error)
// RepairContentLinks rewrites dead [[slug]] references in content to their
// most likely live target (rewrite-only — never strips). Used by the agent
// write path to auto-correct LLM-mangled slugs (especially UUID-based
// summary slugs) before persistence. Returns the possibly-updated content
// and whether any rewrite happened. Best-effort: callers may ignore errors.
RepairContentLinks(ctx context.Context, kbID, selfSlug, content string) (string, bool, error)
// GetPageByID retrieves a wiki page by its unique ID.
GetPageByID(ctx context.Context, id string) (*types.WikiPage, error)