fix(agent): harden @mention scope and simplify must_use hints
Tighten per-turn @MCP/@Skill/@tag handling after review: enforce agent MCP/Skill whitelist, ignore mentions when selection mode is none, tag-scope grep_chunks, and simplified English must_use lines without tool dumps. Skip persisting scope envelopes to history rendered_content.
This commit is contained in:
@@ -98,6 +98,42 @@ func tagScopesFromMentionedItems(items []MentionedItemRequest) []types.TagScope
|
||||
return scopes
|
||||
}
|
||||
|
||||
// mergeTagScopesFromRequestIDs supplements tag scopes built from mentioned_items
|
||||
// with bare tag_ids when the client did not send kb_id on each tag mention.
|
||||
// Orphan tag IDs are attached to the sole knowledge_base_id when unambiguous.
|
||||
func mergeTagScopesFromRequestIDs(scopes []types.TagScope, tagIDs, kbIDs []string) []types.TagScope {
|
||||
if len(tagIDs) == 0 {
|
||||
return scopes
|
||||
}
|
||||
covered := make(map[string]bool)
|
||||
for _, scope := range scopes {
|
||||
for _, id := range scope.TagIDs {
|
||||
covered[id] = true
|
||||
}
|
||||
}
|
||||
orphan := make([]string, 0, len(tagIDs))
|
||||
for _, id := range tagIDs {
|
||||
if id != "" && !covered[id] {
|
||||
orphan = append(orphan, id)
|
||||
}
|
||||
}
|
||||
if len(orphan) == 0 {
|
||||
return scopes
|
||||
}
|
||||
if len(kbIDs) != 1 {
|
||||
return scopes
|
||||
}
|
||||
kbID := kbIDs[0]
|
||||
for i, scope := range scopes {
|
||||
if scope.KnowledgeBaseID == kbID {
|
||||
merged := append(append([]string(nil), scope.TagIDs...), orphan...)
|
||||
scopes[i].TagIDs = dedupRequestStrings(merged)
|
||||
return scopes
|
||||
}
|
||||
}
|
||||
return append(scopes, types.TagScope{KnowledgeBaseID: kbID, TagIDs: dedupRequestStrings(orphan)})
|
||||
}
|
||||
|
||||
func mentionedIDsByType(items []MentionedItemRequest, itemType string) []string {
|
||||
seen := make(map[string]bool)
|
||||
result := make([]string, 0)
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTagScopesFromMentionedItems(t *testing.T) {
|
||||
scopes := tagScopesFromMentionedItems([]MentionedItemRequest{
|
||||
{Type: "tag", ID: "tag-1", KBID: "kb-1"},
|
||||
{Type: "tag", ID: "tag-2", KBID: "kb-1"},
|
||||
{Type: "tag", ID: "tag-3", KBID: "kb-2"},
|
||||
{Type: "tag", ID: "orphan", KBID: ""},
|
||||
})
|
||||
assert.Len(t, scopes, 2)
|
||||
byKB := make(map[string][]string)
|
||||
for _, scope := range scopes {
|
||||
byKB[scope.KnowledgeBaseID] = scope.TagIDs
|
||||
}
|
||||
assert.ElementsMatch(t, []string{"tag-1", "tag-2"}, byKB["kb-1"])
|
||||
assert.Equal(t, []string{"tag-3"}, byKB["kb-2"])
|
||||
}
|
||||
|
||||
func TestMergeTagScopesFromRequestIDs_SingleKB(t *testing.T) {
|
||||
scopes := mergeTagScopesFromRequestIDs(
|
||||
[]types.TagScope{{KnowledgeBaseID: "kb-1", TagIDs: []string{"tag-1"}}},
|
||||
[]string{"tag-2"},
|
||||
[]string{"kb-1"},
|
||||
)
|
||||
assert.Len(t, scopes, 1)
|
||||
assert.ElementsMatch(t, []string{"tag-1", "tag-2"}, scopes[0].TagIDs)
|
||||
}
|
||||
|
||||
func TestMergeTagScopesFromRequestIDs_OrphanWithSingleKB(t *testing.T) {
|
||||
scopes := mergeTagScopesFromRequestIDs(nil, []string{"tag-9"}, []string{"kb-1"})
|
||||
assert.Len(t, scopes, 1)
|
||||
assert.Equal(t, "kb-1", scopes[0].KnowledgeBaseID)
|
||||
assert.Equal(t, []string{"tag-9"}, scopes[0].TagIDs)
|
||||
}
|
||||
|
||||
func TestMergeTagScopesFromRequestIDs_AmbiguousKBIgnored(t *testing.T) {
|
||||
scopes := mergeTagScopesFromRequestIDs(nil, []string{"tag-9"}, []string{"kb-1", "kb-2"})
|
||||
assert.Empty(t, scopes)
|
||||
}
|
||||
@@ -247,7 +247,11 @@ func (h *Handler) parseQARequest(c *gin.Context, logPrefix string) (*qaRequestCo
|
||||
// had memory enabled in practice, keep that behaviour.
|
||||
enableMemory := h.resolveEnableMemory(ctx, request.EnableMemory)
|
||||
|
||||
tagScopes := tagScopesFromMentionedItems(request.MentionedItems)
|
||||
tagScopes := mergeTagScopesFromRequestIDs(
|
||||
tagScopesFromMentionedItems(request.MentionedItems),
|
||||
dedupRequestStrings(request.TagIDs),
|
||||
secutils.SanitizeForLogArray(kbIDs),
|
||||
)
|
||||
tagIDs := dedupRequestStrings(append(request.TagIDs, mentionedIDsByType(request.MentionedItems, "tag")...))
|
||||
mcpServiceIDs := dedupRequestStrings(append(request.MCPServiceIDs, mentionedIDsByType(request.MentionedItems, "mcp")...))
|
||||
skillNames := dedupRequestStrings(append(request.SkillNames, mentionedIDsByType(request.MentionedItems, "skill")...))
|
||||
|
||||
Reference in New Issue
Block a user