fix(search): harden tag scope resolution after #1907

Default missing KB metadata to document tag relation lookup, propagate tag resolution failures instead of returning empty results, and reject ambiguous bare tag_ids when multiple knowledge bases are in scope.
This commit is contained in:
wizardchen
2026-07-06 23:38:43 +08:00
committed by lyingbug
parent 0db71bf2f8
commit d9bb7aa8f2
6 changed files with 142 additions and 22 deletions
+23 -5
View File
@@ -98,12 +98,11 @@ 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 {
// orphanTagIDsForScope returns tag IDs from the request that are not already
// covered by scoped mentions.
func orphanTagIDsForScope(tagIDs []string, scopes []types.TagScope) []string {
if len(tagIDs) == 0 {
return scopes
return nil
}
covered := make(map[string]bool)
for _, scope := range scopes {
@@ -117,6 +116,25 @@ func mergeTagScopesFromRequestIDs(scopes []types.TagScope, tagIDs, kbIDs []strin
orphan = append(orphan, id)
}
}
return orphan
}
// validateUnscopedTagIDs rejects bare tag_ids that cannot be attached to a KB.
func validateUnscopedTagIDs(orphan []string, kbIDs []string) error {
if len(orphan) == 0 {
return nil
}
if len(kbIDs) == 1 {
return nil
}
return fmt.Errorf("tag_ids must be scoped via mentioned_items or exactly one knowledge_base_id")
}
// 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 {
orphan := orphanTagIDsForScope(tagIDs, scopes)
if len(orphan) == 0 {
return scopes
}
+8
View File
@@ -44,3 +44,11 @@ func TestMergeTagScopesFromRequestIDs_AmbiguousKBIgnored(t *testing.T) {
scopes := mergeTagScopesFromRequestIDs(nil, []string{"tag-9"}, []string{"kb-1", "kb-2"})
assert.Empty(t, scopes)
}
func TestValidateUnscopedTagIDs(t *testing.T) {
assert.NoError(t, validateUnscopedTagIDs(nil, nil))
assert.NoError(t, validateUnscopedTagIDs(nil, []string{"kb-1", "kb-2"}))
assert.NoError(t, validateUnscopedTagIDs([]string{"tag-9"}, []string{"kb-1"}))
assert.Error(t, validateUnscopedTagIDs([]string{"tag-9"}, []string{"kb-1", "kb-2"}))
assert.Error(t, validateUnscopedTagIDs([]string{"tag-9"}, nil))
}
+14 -10
View File
@@ -250,11 +250,12 @@ 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 := mergeTagScopesFromRequestIDs(
tagScopesFromMentionedItems(request.MentionedItems),
dedupRequestStrings(request.TagIDs),
secutils.SanitizeForLogArray(kbIDs),
)
mentionScopes := tagScopesFromMentionedItems(request.MentionedItems)
requestTagIDs := dedupRequestStrings(request.TagIDs)
if err := validateUnscopedTagIDs(orphanTagIDsForScope(requestTagIDs, mentionScopes), secutils.SanitizeForLogArray(kbIDs)); err != nil {
return nil, nil, errors.NewBadRequestError(err.Error())
}
tagScopes := mergeTagScopesFromRequestIDs(mentionScopes, requestTagIDs, 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")...))
@@ -522,11 +523,14 @@ func (h *Handler) SearchKnowledge(c *gin.Context) {
}
}
tagScopes := mergeTagScopesFromRequestIDs(
tagScopesFromMentionedItems(request.MentionedItems),
dedupRequestStrings(request.TagIDs),
secutils.SanitizeForLogArray(knowledgeBaseIDs),
)
mentionScopes := tagScopesFromMentionedItems(request.MentionedItems)
requestTagIDs := dedupRequestStrings(request.TagIDs)
if err := validateUnscopedTagIDs(orphanTagIDsForScope(requestTagIDs, mentionScopes), secutils.SanitizeForLogArray(knowledgeBaseIDs)); err != nil {
logger.Error(ctx, err.Error())
c.Error(errors.NewBadRequestError(err.Error()))
return
}
tagScopes := mergeTagScopesFromRequestIDs(mentionScopes, requestTagIDs, secutils.SanitizeForLogArray(knowledgeBaseIDs))
if len(knowledgeBaseIDs) == 0 && len(request.KnowledgeIDs) == 0 && len(tagScopes) == 0 {
logger.Error(ctx, "No knowledge base IDs, knowledge IDs, or tag scopes provided")