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:
wizardchen
2026-07-24 15:53:49 +08:00
committed by lyingbug
parent 5d0326f987
commit 84f65a809f
5 changed files with 140 additions and 13 deletions
+10 -1
View File
@@ -39,8 +39,17 @@ func (e *AgentEngine) streamLLMToEventBus(
llmCtx, llmCancel := context.WithTimeout(ctx, e.getLLMCallTimeout())
defer llmCancel()
messages = e.sourceRefs.EncodeMessages(messages)
// Order matters. resourceRefs must alias durable resource handles FIRST,
// because wiki summary-page slugs (summary/<knowledgeID>) embed a document's
// UUID and sourceRefs.EncodeMessages compacts that same UUID into a document
// citation alias (d1, d2, …) via a blind substring replace. Running
// sourceRefs first mangled `[[summary/<uuid>|Title]]` into `[[summary/d1]]`
// — an unrecoverable dead link the model then copied into wiki tool calls.
// Encoding resourceRefs first turns the slug into a res:// token so the UUID
// is gone before citation compaction runs; the two alias spaces (res://NNNN
// vs d/c/b/w-N) are disjoint, so decode order is unaffected.
messages = e.resourceRefs.EncodeMessages(messages)
messages = e.sourceRefs.EncodeMessages(messages)
prefixFingerprint := chat.PromptPrefixFingerprint(messages, opts)
llmCtx = types.WithLLMCallMetadata(llmCtx, "agent_round", prefixFingerprint)
stream, err := e.chatModel.ChatStream(llmCtx, messages, opts)