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:
wizardchen
2026-06-29 16:54:08 +08:00
committed by lyingbug
parent 049f85139d
commit b86efe3f92
14 changed files with 442 additions and 104 deletions
+2 -1
View File
@@ -32,10 +32,11 @@ func (e *AgentEngine) streamFinalAnswerToEventBus(
// Build messages with all context
systemPrompt := e.buildSystemPrompt(ctx)
userTurn := e.RenderUserTurnContent(sessionID, query)
messages := []chat.Message{
{Role: "system", Content: systemPrompt},
{Role: "user", Content: query},
{Role: "user", Content: userTurn},
}
// Add all tool call results as context
+51 -41
View File
@@ -206,19 +206,10 @@ func escapeXMLAttr(s string) string {
}
// buildRuntimeContextBlock builds a metadata block with current time, session
// info, and the *active retrieval scope for this turn*. The scope snapshot is
// critical for multi-turn correctness: when the user switches their @mention
// to a different KB or document between turns, earlier turns still carry
// their own scope snapshot in history, so the model can see the scope change
// and avoid reusing last turn's answer against the new scope.
//
// The detailed bound-KB metadata (capabilities, recent documents, summaries)
// also lives here — it is turn state, not instructions, so it belongs next
// to the user query rather than baked into the system prompt. Keeping it in
// the user message keeps the system prompt stable/cacheable and lets the
// model see exactly which KBs were in scope at the time of each historical
// turn. Per-turn @mentioned MCP/Skill requirements are emitted separately via
// buildMustUseBlock (sibling to runtime_context in the user message).
// info, and the *active retrieval scope for this turn only*. It is injected
// into the current user message for the LLM call and is not persisted into
// conversation history — replayed user turns keep bare Content so stale scope
// snapshots do not steer follow-up questions.
//
// Per-turn communication_instruction and answer_instruction remind the model
// not to leak internal tool names or IDs in user-visible text, and to end the
@@ -283,42 +274,64 @@ func buildRuntimeContextBlock(
return sb.String()
}
// buildMustUseBlock emits per-turn @mentioned MCP/Skill requirements as a
// sibling envelope outside runtime_context.
// buildMustUseBlock emits a short per-turn hint when the user @mentioned MCP/Skill.
// Tool names are not listed here — they are already in the function-calling schema.
func buildMustUseBlock(mcpServices []*PinnedMCPServiceInfo, skills []*PinnedSkillInfo) string {
if len(mcpServices) == 0 && len(skills) == 0 {
return ""
}
var sb strings.Builder
sb.WriteString("<must_use>\n")
sb.WriteString(" <instruction>REQUIRED this turn: the user @selected these capabilities. ")
sb.WriteString("For EACH listed MCP service you MUST call at least one tool from its tools= list ")
sb.WriteString("in your FIRST tool round (before grep_chunks / knowledge_search / wiki_search). ")
sb.WriteString("For EACH listed skill call read_skill(skill_name=...) before your final answer. ")
sb.WriteString("The @mention means the user wants that source — do not answer using only local KB retrieval when an MCP service is listed.</instruction>\n")
var lines []string
for _, svc := range mcpServices {
if svc == nil {
continue
}
name := svc.Name
if name == "" {
name = svc.ID
prefix := mcpToolNamePrefix(svc)
if prefix == "" {
continue
}
if len(svc.ToolNames) > 0 {
fmt.Fprintf(&sb, " <mcp name=\"%s\" tools=\"%s\" />\n",
escapeXMLAttr(name), escapeXMLAttr(strings.Join(svc.ToolNames, ", ")))
} else {
fmt.Fprintf(&sb, " <mcp name=\"%s\" />\n", escapeXMLAttr(name))
display := svc.Name
if display == "" {
display = svc.ID
}
lines = append(lines, fmt.Sprintf("Must use MCP tools whose names start with %s (@%s) to answer the question below.", prefix, display))
}
for _, skill := range skills {
if skill == nil || skill.Name == "" {
continue
}
fmt.Fprintf(&sb, " <skill name=\"%s\" />\n", escapeXMLAttr(skill.Name))
lines = append(lines, fmt.Sprintf("Must call read_skill(skill_name=\"%s\") for @Skill \"%s\" before answering.", skill.Name, skill.Name))
}
sb.WriteString("</must_use>")
return sb.String()
if len(lines) == 0 {
return ""
}
return "<must_use>\n" + strings.Join(lines, "\n") + "\n</must_use>"
}
// mcpToolNamePrefix returns the shared prefix for an MCP service's registered tools
// (e.g. mcp_iwiki_ from mcp_iwiki_getdocument).
func mcpToolNamePrefix(svc *PinnedMCPServiceInfo) string {
if svc == nil || len(svc.ToolNames) == 0 {
return ""
}
const head = "mcp_"
for _, toolName := range svc.ToolNames {
if !strings.HasPrefix(toolName, head) {
continue
}
rest := toolName[len(head):]
idx := strings.Index(rest, "_")
if idx <= 0 {
continue
}
return head + rest[:idx+1]
}
return ""
}
// RenderUserTurnContent builds the user-turn payload for the current LLM call
// (runtime_context + must_use + query). Used by Execute and finalize paths only;
// not written to rendered_content / history.
func (e *AgentEngine) RenderUserTurnContent(sessionID, query string) string {
runtimeCtx := buildRuntimeContextBlock(sessionID, e.knowledgeBasesInfo, e.selectedDocs)
mustUse := buildMustUseBlock(e.pinnedMCPServices, e.pinnedSkills)
return composeUserTurnContent(runtimeCtx, mustUse, query)
}
func composeUserTurnContent(parts ...string) string {
@@ -491,11 +504,8 @@ func (e *AgentEngine) buildMessagesWithLLMContext(
}
}
// Build user message with runtime context safety tag.
// The runtime context carries a per-turn scope snapshot so that multi-turn
// history preserves the (kb, pinned docs) that each earlier turn ran under;
// this is what lets the model detect a scope switch instead of silently
// answering the new question against last turn's retrieval.
// Build user message with per-turn scope envelopes (current turn only).
// Historical user messages in llmContext stay as bare Content from the DB.
runtimeCtx := buildRuntimeContextBlock(sessionID, e.knowledgeBasesInfo, e.selectedDocs)
mustUse := buildMustUseBlock(e.pinnedMCPServices, e.pinnedSkills)
userMsg := chat.Message{
+35 -9
View File
@@ -170,8 +170,9 @@ func TestBuildRuntimeContextBlock_PinnedDocuments(t *testing.T) {
func TestBuildMustUseBlock_MCPAndSkills(t *testing.T) {
block := buildMustUseBlock(
[]*PinnedMCPServiceInfo{{
ID: "mcp-1",
Name: "ChemDB",
ID: "mcp-1",
Name: "ChemDB",
ToolNames: []string{"mcp_chemdb_search"},
}},
[]*PinnedSkillInfo{{
Name: "data-analysis",
@@ -180,14 +181,14 @@ func TestBuildMustUseBlock_MCPAndSkills(t *testing.T) {
assert.Contains(t, block, "<must_use>")
assert.NotContains(t, block, "<runtime_context")
assert.Contains(t, block, "<instruction>REQUIRED this turn")
assert.Contains(t, block, "FIRST tool round")
assert.Contains(t, block, "read_skill")
assert.Contains(t, block, `<mcp name="ChemDB"`)
assert.Contains(t, block, `<skill name="data-analysis"`)
assert.NotContains(t, block, "<instruction>")
assert.Contains(t, block, "Must use MCP tools whose names start with mcp_chemdb_")
assert.Contains(t, block, "@ChemDB")
assert.Contains(t, block, `Must call read_skill(skill_name="data-analysis")`)
assert.Contains(t, block, `@Skill "data-analysis"`)
}
func TestBuildMustUseBlock_MCPToolNames(t *testing.T) {
func TestBuildMustUseBlock_MCPToolPrefixOnly(t *testing.T) {
block := buildMustUseBlock(
[]*PinnedMCPServiceInfo{{
ID: "mcp-1",
@@ -196,5 +197,30 @@ func TestBuildMustUseBlock_MCPToolNames(t *testing.T) {
}},
nil,
)
assert.Contains(t, block, `tools="mcp_iwiki_aisearchdocument, mcp_iwiki_getdocument"`)
assert.Contains(t, block, "mcp_iwiki_")
assert.NotContains(t, block, "aisearchdocument")
assert.NotContains(t, block, `tools="`)
}
func TestBuildMustUseBlock_SkipsMCPWithoutTools(t *testing.T) {
block := buildMustUseBlock(
[]*PinnedMCPServiceInfo{{
ID: "mcp-1",
Name: "DisabledMCP",
}},
[]*PinnedSkillInfo{{Name: "data-analysis"}},
)
assert.Contains(t, block, `Must call read_skill(skill_name="data-analysis")`)
assert.NotContains(t, block, "DisabledMCP")
}
func TestRenderUserTurnContent_IncludesScopeBlocks(t *testing.T) {
engine := &AgentEngine{
knowledgeBasesInfo: []*KnowledgeBaseInfo{{ID: "kb-1", Name: "Docs"}},
pinnedSkills: []*PinnedSkillInfo{{Name: "analysis"}},
}
out := engine.RenderUserTurnContent("sess-1", "hello")
assert.Contains(t, out, "<runtime_context")
assert.Contains(t, out, "<must_use>")
assert.Contains(t, out, "hello")
}
+75 -10
View File
@@ -126,20 +126,24 @@ func (t *GrepChunksTool) Execute(ctx context.Context, args json.RawMessage) (*ty
// bounded so the LLM context stays small regardless of regex breadth.
const limit = 30
kbIDs := t.searchTargets.GetAllKnowledgeBaseIDs()
kbTenantMap := t.searchTargets.GetKBTenantMap()
var allowedKnowledgeIDs []string
for _, target := range t.searchTargets {
if target.Type == types.SearchTargetTypeKnowledge && len(target.KnowledgeIDs) > 0 {
allowedKnowledgeIDs = append(allowedKnowledgeIDs, target.KnowledgeIDs...)
}
fullKBIDs, knowledgeIDs, err := t.resolveGrepScope(ctx)
if err != nil {
logger.Errorf(ctx, "[Tool][GrepChunks] Failed to resolve search scope: %v", err)
return &types.ToolResult{
Success: false,
Error: fmt.Sprintf("Failed to resolve search scope: %v", err),
}, err
}
kbIDsForMeta := fullKBIDs
if len(kbIDsForMeta) == 0 {
kbIDsForMeta = t.searchTargets.GetAllKnowledgeBaseIDs()
}
logger.Infof(ctx, "[Tool][GrepChunks] Queries: %v, Limit: %d, KBs: %v, KnowledgeIDs: %v",
queries, limit, kbIDs, allowedKnowledgeIDs)
queries, limit, fullKBIDs, knowledgeIDs)
results, err := t.searchChunks(ctx, queries, kbIDs, allowedKnowledgeIDs, kbTenantMap)
results, err := t.searchChunks(ctx, queries, fullKBIDs, knowledgeIDs, kbTenantMap)
if err != nil {
logger.Errorf(ctx, "[Tool][GrepChunks] Search failed: %v", err)
return &types.ToolResult{
@@ -217,7 +221,7 @@ func (t *GrepChunksTool) Execute(ctx context.Context, args json.RawMessage) (*ty
"result_count": len(chunkResults),
"document_count": documentCount,
"total_matches": len(finalResults),
"knowledge_base_ids": kbIDs,
"knowledge_base_ids": kbIDsForMeta,
"limit": limit,
"max_results": limit, // legacy alias
"display_type": "grep_results",
@@ -254,6 +258,67 @@ func (t *GrepChunksTool) regexOperatorForDialect() string {
}
}
// resolveGrepScope splits search targets into full-KB IDs vs specific knowledge IDs.
// Tag-scoped targets are resolved to knowledge IDs so grep_chunks honors @tag scope.
func (t *GrepChunksTool) resolveGrepScope(ctx context.Context) (fullKBIDs, knowledgeIDs []string, err error) {
seenKB := make(map[string]bool)
seenKnowledge := make(map[string]bool)
for _, target := range t.searchTargets {
if target == nil || target.KnowledgeBaseID == "" {
continue
}
switch {
case len(target.KnowledgeIDs) > 0:
for _, kid := range target.KnowledgeIDs {
if kid != "" && !seenKnowledge[kid] {
seenKnowledge[kid] = true
knowledgeIDs = append(knowledgeIDs, kid)
}
}
case len(target.TagIDs) > 0:
tenantID := target.TenantID
if tenantID == 0 {
tenantID = t.searchTargets.GetTenantIDForKB(target.KnowledgeBaseID)
}
tagKnowledgeIDs, listErr := t.listKnowledgeIDsByTags(ctx, target.KnowledgeBaseID, tenantID, target.TagIDs)
if listErr != nil {
return nil, nil, listErr
}
for _, kid := range tagKnowledgeIDs {
if kid != "" && !seenKnowledge[kid] {
seenKnowledge[kid] = true
knowledgeIDs = append(knowledgeIDs, kid)
}
}
default:
if !seenKB[target.KnowledgeBaseID] {
seenKB[target.KnowledgeBaseID] = true
fullKBIDs = append(fullKBIDs, target.KnowledgeBaseID)
}
}
}
return fullKBIDs, knowledgeIDs, nil
}
func (t *GrepChunksTool) listKnowledgeIDsByTags(
ctx context.Context,
kbID string,
tenantID uint64,
tagIDs []string,
) ([]string, error) {
if len(tagIDs) == 0 {
return nil, nil
}
var ids []string
err := t.db.WithContext(ctx).Model(&types.Knowledge{}).
Joins("JOIN knowledge_tag_relations ktr ON knowledges.id = ktr.knowledge_id").
Where("knowledges.tenant_id = ? AND knowledges.knowledge_base_id = ? AND ktr.tag_id IN ? AND knowledges.deleted_at IS NULL",
tenantID, kbID, tagIDs).
Distinct("knowledges.id").
Pluck("knowledges.id", &ids).Error
return ids, err
}
// searchChunks performs the database search using regex queries.
func (t *GrepChunksTool) searchChunks(
ctx context.Context,
@@ -129,8 +129,8 @@ func buildUserHistoryMessage(m *types.Message) chat.Message {
}
// Only append fallbacks when RenderedContent is absent — when present, it
// already carries the augmented version persisted by the original turn.
// Agent-mode turns currently do not persist RenderedContent, so attachments
// and image captions would otherwise be invisible to subsequent rounds.
// Agent-mode turns do not persist RenderedContent (scope envelopes are
// injected only for the current LLM call, not replayed from history).
if m.RenderedContent == "" {
if captions := extractImageCaptionsFromMessage(m.Images); captions != "" {
content += "\n\n[用户上传图片内容]\n" + captions
@@ -183,6 +183,10 @@ func (s *sessionService) AgentQA(
logger.Infof(ctx, "Appended %d attachment(s) to agent query", len(req.Attachments))
}
// Scope envelopes (runtime_context / must_use) are injected per LLM call inside
// the agent engine only; we intentionally do not persist them on user messages
// so multi-turn history stays clean and is not skewed by stale @mention scope.
// Execute agent with streaming (asynchronously)
// Events will be emitted to EventBus and handled by the Handler layer
logger.Info(ctx, "Executing agent with streaming")
@@ -245,36 +249,45 @@ func (s *sessionService) buildAgentConfig(
} else {
agentConfig.AllowedTools = tools.DefaultAllowedTools()
}
if len(req.SkillNames) > 0 && agentConfig.SkillsEnabled && customAgent != nil {
switch customAgent.Config.SkillsSelectionMode {
case "selected":
agentConfig.AllowedSkills = intersectPreservingRequestOrder(req.SkillNames, agentConfig.AllowedSkills)
if len(agentConfig.AllowedSkills) == 0 {
agentConfig.SkillsEnabled = false
if len(req.SkillNames) > 0 {
skillsMode := customAgent.Config.SkillsSelectionMode
if skillsMode == "none" || skillsMode == "" {
logger.Warnf(ctx, "Ignoring @skill mention: agent skills selection is disabled (mode=%s)", skillsMode)
} else if agentConfig.SkillsEnabled {
switch skillsMode {
case "selected":
agentConfig.AllowedSkills = intersectPreservingRequestOrder(req.SkillNames, agentConfig.AllowedSkills)
if len(agentConfig.AllowedSkills) == 0 {
agentConfig.SkillsEnabled = false
}
case "all":
agentConfig.AllowedSkills = dedupPreservingOrder(req.SkillNames)
}
case "all":
agentConfig.AllowedSkills = dedupPreservingOrder(req.SkillNames)
logger.Infof(ctx, "Applied per-request @skill scope: requested=%v effective=%v", req.SkillNames, agentConfig.AllowedSkills)
}
logger.Infof(ctx, "Applied per-request @skill scope: requested=%v effective=%v", req.SkillNames, agentConfig.AllowedSkills)
}
if len(req.MCPServiceIDs) > 0 {
mentioned := dedupPreservingOrder(req.MCPServiceIDs)
switch agentConfig.MCPSelectionMode {
case "selected":
narrowed := intersectPreservingRequestOrder(mentioned, customAgent.Config.MCPServices)
if len(narrowed) > 0 {
agentConfig.MCPServices = narrowed
} else {
// Explicit @mention outside agent preset — still honor user intent.
agentConfig.MCPServices = mentioned
if agentConfig.MCPSelectionMode == "none" {
logger.Warnf(ctx, "Ignoring @MCP mention: agent MCP selection is disabled (mode=none)")
} else {
mentioned := dedupPreservingOrder(req.MCPServiceIDs)
isSharedAgent := req.Session != nil && req.Session.TenantID != customAgent.TenantID
effectiveMCP, mcpMode := resolvePerRequestMCPScope(
mentioned,
customAgent.Config.MCPServices,
agentConfig.MCPSelectionMode,
isSharedAgent,
)
if len(effectiveMCP) > 0 {
agentConfig.MCPSelectionMode = mcpMode
agentConfig.MCPServices = effectiveMCP
} else if len(mentioned) > 0 {
logger.Warnf(ctx, "Ignoring @MCP scope outside agent preset: requested=%v agent=%v shared=%v",
req.MCPServiceIDs, customAgent.Config.MCPServices, isSharedAgent)
}
default:
// "all", "none", or unset: per-request @mention narrows registration.
agentConfig.MCPSelectionMode = "selected"
agentConfig.MCPServices = mentioned
logger.Infof(ctx, "Applied per-request @MCP scope: requested=%v mode=%s effective=%v",
req.MCPServiceIDs, agentConfig.MCPSelectionMode, agentConfig.MCPServices)
}
logger.Infof(ctx, "Applied per-request @MCP scope: requested=%v mode=%s effective=%v",
req.MCPServiceIDs, agentConfig.MCPSelectionMode, agentConfig.MCPServices)
}
// Use custom agent's system prompt if specified
@@ -327,16 +340,53 @@ func (s *sessionService) buildAgentConfig(
agentConfig.MaxContextTokens = types.DefaultMaxContextTokens
}
if len(req.MCPServiceIDs) > 0 {
agentConfig.PinnedMCPServiceIDs = dedupPreservingOrder(req.MCPServiceIDs)
if len(agentConfig.MCPServices) > 0 && len(req.MCPServiceIDs) > 0 {
agentConfig.PinnedMCPServiceIDs = intersectPreservingRequestOrder(req.MCPServiceIDs, agentConfig.MCPServices)
}
if len(req.SkillNames) > 0 {
agentConfig.PinnedSkillNames = dedupPreservingOrder(req.SkillNames)
if len(req.SkillNames) > 0 && agentConfig.SkillsEnabled {
if len(agentConfig.AllowedSkills) > 0 {
agentConfig.PinnedSkillNames = intersectPreservingRequestOrder(req.SkillNames, agentConfig.AllowedSkills)
} else if customAgent.Config.SkillsSelectionMode == "all" {
agentConfig.PinnedSkillNames = dedupPreservingOrder(req.SkillNames)
}
}
return agentConfig, nil
}
// resolvePerRequestMCPScope narrows MCP registration for a per-turn @mention.
// selectionMode "none" rejects all mentions. Shared agents never register MCP
// services outside the agent preset.
func resolvePerRequestMCPScope(
mentioned, agentMCPs []string,
selectionMode string,
isSharedAgent bool,
) (effective []string, mode string) {
if len(mentioned) == 0 {
return nil, selectionMode
}
if isSharedAgent {
mentioned = intersectPreservingRequestOrder(mentioned, agentMCPs)
if len(mentioned) == 0 {
return nil, selectionMode
}
}
switch selectionMode {
case "none":
return nil, selectionMode
case "selected":
effective = intersectPreservingRequestOrder(mentioned, agentMCPs)
case "all", "":
effective = mentioned
default:
effective = mentioned
}
if len(effective) == 0 {
return nil, selectionMode
}
return effective, "selected"
}
func intersectPreservingRequestOrder(requested []string, allowed []string) []string {
allowedSet := make(map[string]bool, len(allowed))
for _, value := range allowed {
@@ -0,0 +1,62 @@
package service
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestResolvePerRequestMCPScope_SelectedIntersection(t *testing.T) {
effective, mode := resolvePerRequestMCPScope(
[]string{"mcp-b", "mcp-c"},
[]string{"mcp-a", "mcp-b"},
"selected",
false,
)
assert.Equal(t, "selected", mode)
assert.Equal(t, []string{"mcp-b"}, effective)
}
func TestResolvePerRequestMCPScope_SelectedRejectsOutsidePreset(t *testing.T) {
effective, mode := resolvePerRequestMCPScope(
[]string{"mcp-x"},
[]string{"mcp-a"},
"selected",
false,
)
assert.Empty(t, effective)
assert.Equal(t, "selected", mode)
}
func TestResolvePerRequestMCPScope_NoneRejectsMention(t *testing.T) {
effective, mode := resolvePerRequestMCPScope(
[]string{"mcp-iwiki"},
nil,
"none",
false,
)
assert.Empty(t, effective)
assert.Equal(t, "none", mode)
}
func TestResolvePerRequestMCPScope_SharedAgentBlocksOutsidePreset(t *testing.T) {
effective, mode := resolvePerRequestMCPScope(
[]string{"mcp-x"},
[]string{"mcp-a"},
"all",
true,
)
assert.Empty(t, effective)
assert.Equal(t, "all", mode)
}
func TestResolvePerRequestMCPScope_SharedAgentAllowsPreset(t *testing.T) {
effective, mode := resolvePerRequestMCPScope(
[]string{"mcp-a", "mcp-x"},
[]string{"mcp-a", "mcp-b"},
"all",
true,
)
assert.Equal(t, "selected", mode)
assert.Equal(t, []string{"mcp-a"}, effective)
}
+36
View File
@@ -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)
+46
View File
@@ -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)
}
+5 -1
View File
@@ -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")...))