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
@@ -8,6 +8,7 @@ import (
"github.com/Tencent/WeKnora/internal/application/repository"
"github.com/Tencent/WeKnora/internal/types"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
@@ -140,6 +141,60 @@ func TestParseOutLinks(t *testing.T) {
}
}
func TestRepairContentLinks(t *testing.T) {
db, err := gorm.Open(sqlite.Open(fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())), &gorm.Config{})
require.NoError(t, err)
require.NoError(t, db.AutoMigrate(&types.WikiFolder{}, &types.WikiPage{}))
ctx := context.Background()
repo := repository.NewWikiPageRepository(db)
svc := NewWikiPageService(repo, nil, nil, nil, nil)
const kbID = "kb-repair"
now := time.Now()
seed := func(slug, title, pageType string) {
require.NoError(t, repo.Create(ctx, &types.WikiPage{
ID: uuid.New().String(), TenantID: 1, KnowledgeBaseID: kbID,
Slug: slug, Title: title, PageType: pageType,
Status: types.WikiPageStatusPublished, Version: 1,
CreatedAt: now, UpdatedAt: now,
}))
}
// Real summary page (clean UUID slug) + a distractor summary + an entity.
realSummary := "summary/07a20bb1-a662-47cf-9929-06fb5d5b5b5e"
seed(realSummary, "Weknora 试错记录.md - Summary", types.WikiPageTypeSummary)
seed("summary/fcadcaab-e094-4037-b71c-9edfdcdba058", "Another Doc - Summary", types.WikiPageTypeSummary)
seed("entity/mongodb", "MongoDB", types.WikiPageTypeEntity)
t.Run("mangled uuid summary link repaired via bigram", func(t *testing.T) {
// One hex digit inserted into the last group — 404s on exact lookup.
content := "See [[summary/07a20bb1-a662-47cf-9929-06fb14d5b14b14e|Weknora 试错记录.md - Summary]] for context."
got, changed, err := svc.RepairContentLinks(ctx, kbID, "synthesis/notes", content)
require.NoError(t, err)
require.True(t, changed, "expected the mangled summary link to be rewritten")
require.Contains(t, got, "[["+realSummary+"|Weknora 试错记录.md - Summary]]")
require.NotContains(t, got, "06fb14d5b14b14e")
})
t.Run("live links untouched", func(t *testing.T) {
content := "Fine: [[entity/mongodb]] and [[" + realSummary + "]]."
got, changed, err := svc.RepairContentLinks(ctx, kbID, "synthesis/notes", content)
require.NoError(t, err)
require.False(t, changed)
require.Equal(t, content, got)
})
t.Run("unresolvable dead link left as-is, never stripped", func(t *testing.T) {
// A future entity page that doesn't exist and isn't similar to anything.
content := "Pending: [[entity/some-brand-new-topic|New Topic]]."
got, changed, err := svc.RepairContentLinks(ctx, kbID, "synthesis/notes", content)
require.NoError(t, err)
require.False(t, changed)
require.Equal(t, content, got, "unresolvable links must be preserved verbatim, not stripped")
})
}
func TestNormalizeSlug(t *testing.T) {
tests := []struct {
input string