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,6 +7,61 @@ import (
"github.com/Tencent/WeKnora/internal/types"
)
func TestDedupMergeRejectReason(t *testing.T) {
cases := []struct {
name string
src, dst string
candidates map[string]bool
wantAllowed bool
}{
{
name: "allowed: dst is a candidate for this item, same type prefix",
src: "entity/acme-corp",
dst: "entity/acme-corporation",
candidates: map[string]bool{"entity/acme-corporation": true},
wantAllowed: true,
},
{
name: "rejected: dst similar to a DIFFERENT item (union hallucination)",
src: "entity/tencent-open",
dst: "entity/hiring-agent",
candidates: map[string]bool{"entity/tencent-ur": true}, // hiring-agent not here
wantAllowed: false,
},
{
name: "rejected: dst is not a candidate at all (pure hallucination)",
src: "entity/hy3-preview",
dst: "entity/llm-cli-tool",
candidates: nil,
wantAllowed: false,
},
{
name: "rejected: type mismatch even when dst is a candidate",
src: "entity/foo",
dst: "concept/foo",
candidates: map[string]bool{"concept/foo": true},
wantAllowed: false,
},
{
name: "rejected: missing type prefix",
src: "foo",
dst: "entity/foo",
candidates: map[string]bool{"entity/foo": true},
wantAllowed: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
reason := dedupMergeRejectReason(tc.src, tc.dst, tc.candidates)
gotAllowed := reason == ""
if gotAllowed != tc.wantAllowed {
t.Fatalf("dedupMergeRejectReason(%q, %q) allowed=%v (reason=%q), want allowed=%v",
tc.src, tc.dst, gotAllowed, reason, tc.wantAllowed)
}
})
}
}
// helper: build a minimal entity/concept WikiPage.
func makePage(slug, title, typ string, aliases ...string) *types.WikiPage {
return &types.WikiPage{