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
+13 -1
View File
@@ -17,10 +17,22 @@ import (
// resource:// handles, but old chunks and message history can still contain a
// provider URL. Giving both forms the same request-local alias makes rollout
// safe without a blocking full-table content rewrite.
//
// The final alternative aliases wiki summary-page slugs (summary/<uuid>). They
// are not storage handles, but they share the exact failure mode this registry
// exists to prevent: a high-entropy identifier the model must reproduce
// verbatim (inside [[slug|display]] links and as wiki-tool slug arguments).
// Models routinely mangle the UUID by inserting or dropping hex digits, which
// yields dead cross-links. Aliasing the slug to a low-entropy res:// token
// removes the opportunity to mangle at the source; the token round-trips back
// to the real slug on stream output and in decoded tool-call arguments before
// anything is persisted. Entity slugs (entity/<readable-title>) are low-entropy
// and semantically meaningful, so they are deliberately left untouched.
var storedRefRE = regexp.MustCompile(
`resource://[0-9A-Za-z_-]{22}|` +
`(?:storage://[0-9A-Za-z_-]+/)?` +
`(?:local|minio|cos|tos|s3|oss|ks3|obs)://[^\s)\]>"']+`,
`(?:local|minio|cos|tos|s3|oss|ks3|obs)://[^\s)\]>"']+|` +
`summary/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}`,
)
// aliasShapeRE matches the alias syntax produced by EncodeText. It is used only
+19
View File
@@ -23,6 +23,25 @@ func TestRegistryAliasesLegacyPhysicalReferencesDuringRollout(t *testing.T) {
require.Equal(t, "![image]("+ref+")", r.DecodeText(encoded))
}
func TestRegistryAliasesWikiSummarySlug(t *testing.T) {
r := NewRegistry()
slug := "summary/07a20bb1-a662-47cf-9929-06fb5d5b5b5e"
// Inside a [[slug|display]] link the model must copy verbatim.
encoded := r.EncodeText("see [[" + slug + "|Foo.md - Summary]]")
require.Equal(t, "see [[res://0001|Foo.md - Summary]]", encoded)
require.Equal(t, "see [["+slug+"|Foo.md - Summary]]", r.DecodeText(encoded))
// The same slug as a tool-call argument value resolves to one alias.
require.Equal(t, "res://0001", r.EncodeText(slug))
}
func TestRegistryLeavesEntitySlugUntouched(t *testing.T) {
r := NewRegistry()
// Entity slugs are low-entropy and semantically meaningful; not aliased.
entity := "[[entity/weknora-error-log|WeKnora 试错记录]]"
require.Equal(t, entity, r.EncodeText(entity))
}
func TestRegistryEncodesMessageCopies(t *testing.T) {
r := NewRegistry()
ref := "resource://AbCdEfGhIjKlMnOpQrStUv"