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
}