fix(agent): avoid expanding tag scopes for grep chunks

This commit is contained in:
wizardchen
2026-06-29 18:21:17 +08:00
committed by lyingbug
parent d3071fbe03
commit 17ffd902ac
2 changed files with 99 additions and 50 deletions
+68 -49
View File
@@ -127,23 +127,16 @@ func (t *GrepChunksTool) Execute(ctx context.Context, args json.RawMessage) (*ty
const limit = 30 const limit = 30
kbTenantMap := t.searchTargets.GetKBTenantMap() kbTenantMap := t.searchTargets.GetKBTenantMap()
fullKBIDs, knowledgeIDs, err := t.resolveGrepScope(ctx) fullKBIDs, knowledgeIDs, tagTargets := t.resolveGrepScope()
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 kbIDsForMeta := fullKBIDs
if len(kbIDsForMeta) == 0 { if len(kbIDsForMeta) == 0 {
kbIDsForMeta = t.searchTargets.GetAllKnowledgeBaseIDs() kbIDsForMeta = t.searchTargets.GetAllKnowledgeBaseIDs()
} }
logger.Infof(ctx, "[Tool][GrepChunks] Queries: %v, Limit: %d, KBs: %v, KnowledgeIDs: %v", logger.Infof(ctx, "[Tool][GrepChunks] Queries: %v, Limit: %d, fullKBs: %d, knowledgeIDs: %d, tagScopes: %d",
queries, limit, fullKBIDs, knowledgeIDs) queries, limit, len(fullKBIDs), len(knowledgeIDs), len(tagTargets))
results, err := t.searchChunks(ctx, queries, fullKBIDs, knowledgeIDs, kbTenantMap) results, err := t.searchChunks(ctx, queries, fullKBIDs, knowledgeIDs, tagTargets, kbTenantMap)
if err != nil { if err != nil {
logger.Errorf(ctx, "[Tool][GrepChunks] Search failed: %v", err) logger.Errorf(ctx, "[Tool][GrepChunks] Search failed: %v", err)
return &types.ToolResult{ return &types.ToolResult{
@@ -258,11 +251,14 @@ func (t *GrepChunksTool) regexOperatorForDialect() string {
} }
} }
// resolveGrepScope splits search targets into full-KB IDs vs specific knowledge IDs. // resolveGrepScope splits search targets into full-KB, specific-knowledge, and
// Tag-scoped targets are resolved to knowledge IDs so grep_chunks honors @tag scope. // tag-constrained KB scopes. Tag scopes stay as tag IDs so the chunk query can
func (t *GrepChunksTool) resolveGrepScope(ctx context.Context) (fullKBIDs, knowledgeIDs []string, err error) { // use the knowledge_tag_relations index instead of expanding a large tag into a
// huge knowledge_id IN list.
func (t *GrepChunksTool) resolveGrepScope() (fullKBIDs, knowledgeIDs []string, tagTargets []*types.SearchTarget) {
seenKB := make(map[string]bool) seenKB := make(map[string]bool)
seenKnowledge := make(map[string]bool) seenKnowledge := make(map[string]bool)
seenTagScope := make(map[string]bool)
for _, target := range t.searchTargets { for _, target := range t.searchTargets {
if target == nil || target.KnowledgeBaseID == "" { if target == nil || target.KnowledgeBaseID == "" {
continue continue
@@ -280,16 +276,21 @@ func (t *GrepChunksTool) resolveGrepScope(ctx context.Context) (fullKBIDs, knowl
if tenantID == 0 { if tenantID == 0 {
tenantID = t.searchTargets.GetTenantIDForKB(target.KnowledgeBaseID) tenantID = t.searchTargets.GetTenantIDForKB(target.KnowledgeBaseID)
} }
tagKnowledgeIDs, listErr := t.listKnowledgeIDsByTags(ctx, target.KnowledgeBaseID, tenantID, target.TagIDs) tagIDs := dedupNonEmptyStrings(target.TagIDs)
if listErr != nil { if len(tagIDs) == 0 || tenantID == 0 {
return nil, nil, listErr continue
} }
for _, kid := range tagKnowledgeIDs { scopeKey := fmt.Sprintf("%s:%d:%s", target.KnowledgeBaseID, tenantID, strings.Join(tagIDs, "\x00"))
if kid != "" && !seenKnowledge[kid] { if seenTagScope[scopeKey] {
seenKnowledge[kid] = true continue
knowledgeIDs = append(knowledgeIDs, kid)
}
} }
seenTagScope[scopeKey] = true
tagTargets = append(tagTargets, &types.SearchTarget{
Type: types.SearchTargetTypeKnowledgeBase,
KnowledgeBaseID: target.KnowledgeBaseID,
TenantID: tenantID,
TagIDs: tagIDs,
})
default: default:
if !seenKB[target.KnowledgeBaseID] { if !seenKB[target.KnowledgeBaseID] {
seenKB[target.KnowledgeBaseID] = true seenKB[target.KnowledgeBaseID] = true
@@ -297,39 +298,55 @@ func (t *GrepChunksTool) resolveGrepScope(ctx context.Context) (fullKBIDs, knowl
} }
} }
} }
return fullKBIDs, knowledgeIDs, nil return fullKBIDs, knowledgeIDs, tagTargets
} }
func (t *GrepChunksTool) listKnowledgeIDsByTags( func dedupNonEmptyStrings(values []string) []string {
ctx context.Context, seen := make(map[string]bool, len(values))
kbID string, out := make([]string, 0, len(values))
tenantID uint64, for _, value := range values {
tagIDs []string, if value == "" || seen[value] {
) ([]string, error) { continue
if len(tagIDs) == 0 { }
return nil, nil seen[value] = true
out = append(out, value)
} }
var ids []string return out
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
} }
// scopeClause builds the SQL predicate restricting chunks to the active scope. // scopeClause builds the SQL predicate restricting chunks to the active scope.
// Specific knowledge IDs and full-KB (kb, tenant) pairs are OR'd together so a // Specific knowledge IDs, tag-constrained KB scopes, and full-KB (kb, tenant)
// turn that mentions both an entire KB and a tag/file in another KB searches // pairs are OR'd together so a turn that mentions @KB + @tag + @file searches
// every requested scope. Returns ("", nil) when no usable scope exists. // every requested scope. Returns ("", nil) when no usable scope exists.
func scopeClause(kbIDs, knowledgeIDs []string, kbTenantMap map[string]uint64) (string, []interface{}) { func scopeClause(
kbIDs, knowledgeIDs []string,
tagTargets []*types.SearchTarget,
kbTenantMap map[string]uint64,
) (string, []interface{}) {
var clauses []string var clauses []string
var args []interface{} var args []interface{}
if len(knowledgeIDs) > 0 { if len(knowledgeIDs) > 0 {
clauses = append(clauses, "chunks.knowledge_id IN ?") clauses = append(clauses, "chunks.knowledge_id IN ?")
args = append(args, knowledgeIDs) args = append(args, knowledgeIDs)
} }
for _, target := range tagTargets {
if target == nil || target.KnowledgeBaseID == "" || len(target.TagIDs) == 0 {
continue
}
tenantID := target.TenantID
if tenantID == 0 {
tenantID = kbTenantMap[target.KnowledgeBaseID]
}
if tenantID == 0 {
continue
}
clauses = append(clauses,
"(chunks.knowledge_base_id = ? AND chunks.tenant_id = ? AND EXISTS ("+
"SELECT 1 FROM knowledge_tag_relations ktr "+
"WHERE ktr.knowledge_id = chunks.knowledge_id AND ktr.tag_id IN ?"+
"))")
args = append(args, target.KnowledgeBaseID, tenantID, target.TagIDs)
}
for _, kbID := range kbIDs { for _, kbID := range kbIDs {
tenantID := kbTenantMap[kbID] tenantID := kbTenantMap[kbID]
if tenantID == 0 { if tenantID == 0 {
@@ -350,10 +367,11 @@ func (t *GrepChunksTool) searchChunks(
queries []string, queries []string,
kbIDs []string, kbIDs []string,
knowledgeIDs []string, knowledgeIDs []string,
tagTargets []*types.SearchTarget,
kbTenantMap map[string]uint64, kbTenantMap map[string]uint64,
) ([]chunkWithTitle, error) { ) ([]chunkWithTitle, error) {
if len(kbIDs) == 0 && len(knowledgeIDs) == 0 { if len(kbIDs) == 0 && len(knowledgeIDs) == 0 && len(tagTargets) == 0 {
logger.Warnf(ctx, "[Tool][GrepChunks] No kbIDs or knowledgeIDs specified, returning empty results") logger.Warnf(ctx, "[Tool][GrepChunks] No kbIDs, knowledgeIDs, or tag scopes specified, returning empty results")
return nil, nil return nil, nil
} }
@@ -368,15 +386,16 @@ func (t *GrepChunksTool) searchChunks(
Where("chunks.deleted_at IS NULL"). Where("chunks.deleted_at IS NULL").
Where("knowledges.deleted_at IS NULL") Where("knowledges.deleted_at IS NULL")
// Combine specific knowledge IDs (incl. tag-resolved docs) and full-KB scopes // Combine specific knowledge IDs, tag scopes, and full-KB scopes with OR so
// with OR so that mixing @KB with @tag/@file searches BOTH, mirroring // that mixing @KB with @tag/@file searches BOTH, mirroring knowledge_search's
// knowledge_search's concurrent fan-out instead of dropping one side. // concurrent fan-out instead of dropping one side.
scopeSQL, scopeArgs := scopeClause(kbIDs, knowledgeIDs, kbTenantMap) scopeSQL, scopeArgs := scopeClause(kbIDs, knowledgeIDs, tagTargets, kbTenantMap)
if scopeSQL == "" { if scopeSQL == "" {
logger.Warnf(ctx, "[Tool][GrepChunks] No valid scope (kb-tenant pairs / knowledge IDs)") logger.Warnf(ctx, "[Tool][GrepChunks] No valid scope (kb-tenant pairs / knowledge IDs)")
return nil, nil return nil, nil
} }
logger.Infof(ctx, "[Tool][GrepChunks] Scope: %d knowledge IDs, %d KBs", len(knowledgeIDs), len(kbIDs)) logger.Infof(ctx, "[Tool][GrepChunks] Scope: %d knowledge IDs, %d tag scopes, %d KBs",
len(knowledgeIDs), len(tagTargets), len(kbIDs))
query = query.Where(scopeSQL, scopeArgs...) query = query.Where(scopeSQL, scopeArgs...)
// For MySQL/SQLite REGEXP case-insensitivity we rely on the column's default // For MySQL/SQLite REGEXP case-insensitivity we rely on the column's default
+31 -1
View File
@@ -3,12 +3,15 @@ package tools
import ( import (
"strings" "strings"
"testing" "testing"
"github.com/Tencent/WeKnora/internal/types"
) )
func TestScopeClause_KnowledgeAndFullKBOredTogether(t *testing.T) { func TestScopeClause_KnowledgeAndFullKBOredTogether(t *testing.T) {
sql, args := scopeClause( sql, args := scopeClause(
[]string{"kb-1"}, []string{"kb-1"},
[]string{"doc-9"}, []string{"doc-9"},
nil,
map[string]uint64{"kb-1": 7}, map[string]uint64{"kb-1": 7},
) )
@@ -27,10 +30,37 @@ func TestScopeClause_KnowledgeAndFullKBOredTogether(t *testing.T) {
} }
} }
func TestScopeClause_TagScopeUsesExistsInsteadOfExpandedKnowledgeIDs(t *testing.T) {
sql, args := scopeClause(
nil,
nil,
[]*types.SearchTarget{{
KnowledgeBaseID: "kb-1",
TenantID: 7,
TagIDs: []string{"tag-a", "tag-b"},
}},
map[string]uint64{"kb-1": 7},
)
if strings.Contains(sql, "chunks.knowledge_id IN ?") {
t.Fatalf("tag scope must not expand to knowledge-id IN predicate, got %q", sql)
}
if !strings.Contains(sql, "EXISTS") || !strings.Contains(sql, "knowledge_tag_relations") {
t.Fatalf("expected tag relation EXISTS predicate, got %q", sql)
}
if !strings.Contains(sql, "ktr.tag_id IN ?") {
t.Fatalf("expected tag-id predicate, got %q", sql)
}
if len(args) != 3 {
t.Fatalf("expected kbID, tenantID, tagIDs args, got %d: %v", len(args), args)
}
}
func TestScopeClause_SkipsKBWithoutTenant(t *testing.T) { func TestScopeClause_SkipsKBWithoutTenant(t *testing.T) {
sql, args := scopeClause( sql, args := scopeClause(
[]string{"kb-1", "kb-2"}, []string{"kb-1", "kb-2"},
nil, nil,
nil,
map[string]uint64{"kb-1": 7}, // kb-2 has no tenant mapping map[string]uint64{"kb-1": 7}, // kb-2 has no tenant mapping
) )
if strings.Contains(sql, "kb-2") { if strings.Contains(sql, "kb-2") {
@@ -42,7 +72,7 @@ func TestScopeClause_SkipsKBWithoutTenant(t *testing.T) {
} }
func TestScopeClause_EmptyWhenNoUsableScope(t *testing.T) { func TestScopeClause_EmptyWhenNoUsableScope(t *testing.T) {
sql, args := scopeClause([]string{"kb-1"}, nil, map[string]uint64{}) sql, args := scopeClause([]string{"kb-1"}, nil, nil, map[string]uint64{})
if sql != "" || args != nil { if sql != "" || args != nil {
t.Fatalf("expected empty scope, got sql=%q args=%v", sql, args) t.Fatalf("expected empty scope, got sql=%q args=%v", sql, args)
} }