fix(agent): ensure wiki summary slugs are preserved during citation compaction
This update addresses the issue where wiki summary-page slugs (summary/<knowledgeID>) were being mangled during the citation compaction process, leading to dead links. The changes include: - Reordering the message encoding process to ensure resource references are aliased before source references, preventing the UUID from being replaced in the summary slug. - Implementing a new function to selectively replace document IDs while preserving the integrity of summary slugs. - Adding unit tests to verify that summary slugs remain intact and are not altered during compaction. This fix enhances the reliability of cross-links in the wiki system and prevents potential dead links from being generated.
This commit is contained in:
@@ -536,12 +536,47 @@ func (r *Registry) CompactKnownText(text string) string {
|
||||
sort.SliceStable(pairs, func(i, j int) bool { return len(pairs[i].real) > len(pairs[j].real) })
|
||||
for _, item := range pairs {
|
||||
if item.real != "" {
|
||||
text = strings.ReplaceAll(text, item.real, item.alias)
|
||||
text = replaceIDExceptWikiSummarySlug(text, item.real, item.alias)
|
||||
}
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
// wikiSummarySlugPrefix guards wiki summary-page slugs (summary/<knowledgeID>)
|
||||
// from citation compaction. A summary slug embeds a document's knowledge ID
|
||||
// verbatim, so a blind ReplaceAll of that ID into its citation alias (d1) would
|
||||
// mangle the slug into summary/d1 — an unrecoverable dead link the model then
|
||||
// copies into wiki tool calls. Only the slug-embedded occurrence is preserved;
|
||||
// the same document ID elsewhere in the text still compacts normally.
|
||||
const wikiSummarySlugPrefix = "summary/"
|
||||
|
||||
// replaceIDExceptWikiSummarySlug replaces every occurrence of real with alias,
|
||||
// except one that is immediately preceded by "summary/" (i.e. it is the
|
||||
// identifier segment of a wiki summary-page slug). Go's regexp package has no
|
||||
// lookbehind, so the scan is done manually.
|
||||
func replaceIDExceptWikiSummarySlug(text, real, alias string) string {
|
||||
if real == "" || !strings.Contains(text, real) {
|
||||
return text
|
||||
}
|
||||
var b strings.Builder
|
||||
b.Grow(len(text))
|
||||
for {
|
||||
i := strings.Index(text, real)
|
||||
if i < 0 {
|
||||
b.WriteString(text)
|
||||
break
|
||||
}
|
||||
b.WriteString(text[:i])
|
||||
if strings.HasSuffix(text[:i], wikiSummarySlugPrefix) {
|
||||
b.WriteString(real) // part of a summary slug — keep intact
|
||||
} else {
|
||||
b.WriteString(alias)
|
||||
}
|
||||
text = text[i+len(real):]
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
var (
|
||||
refTagRE = regexp.MustCompile(`(?i)<ref\s+id\s*=\s*"([^"]+)"\s*/?>`)
|
||||
refCandidateRE = regexp.MustCompile(`(?is)<ref(?:\s|$)[^>]*(?:>|$)`)
|
||||
|
||||
@@ -32,6 +32,30 @@ func TestRegistryChunkAliasIsStableAndExpandsCanonicalCitation(t *testing.T) {
|
||||
)
|
||||
}
|
||||
|
||||
func TestCompactKnownTextPreservesWikiSummarySlug(t *testing.T) {
|
||||
const knowledgeID = "07a20bb1-a662-47cf-9929-06fb5d5b5b5e"
|
||||
const kbID = "250368ff-f5a2-4e9e-868a-07bc9b857c44"
|
||||
|
||||
registry := NewRegistry()
|
||||
require.Equal(t, "d1", registry.RegisterDocument(knowledgeID))
|
||||
require.Equal(t, "b1", registry.RegisterKnowledgeBase(kbID))
|
||||
|
||||
// A wiki_search result: the summary slug embeds the document UUID, and the
|
||||
// same UUID also appears as a standalone <knowledge_id> label.
|
||||
in := "<knowledge_base_id>" + kbID + "</knowledge_base_id>\n" +
|
||||
"<link>[[summary/" + knowledgeID + "|Doc - Summary]]</link>\n" +
|
||||
"<knowledge_id>" + knowledgeID + "</knowledge_id>"
|
||||
|
||||
got := registry.CompactKnownText(in)
|
||||
|
||||
// The slug-embedded UUID must survive verbatim — no summary/d1 mangling.
|
||||
require.Contains(t, got, "[[summary/"+knowledgeID+"|Doc - Summary]]")
|
||||
require.NotContains(t, got, "summary/d1")
|
||||
// The standalone label and the KB id still compact normally.
|
||||
require.Contains(t, got, "<knowledge_id>d1</knowledge_id>")
|
||||
require.Contains(t, got, "<knowledge_base_id>b1</knowledge_base_id>")
|
||||
}
|
||||
|
||||
func TestRegistrySuppressesSourceCitationsWhenDisabled(t *testing.T) {
|
||||
registry := NewRegistry(false)
|
||||
registry.RegisterChunk(ChunkReference{ChunkID: "chunk-1", DocumentTitle: "Doc"})
|
||||
|
||||
Reference in New Issue
Block a user