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
+31 -1
View File
@@ -3,12 +3,15 @@ package tools
import (
"strings"
"testing"
"github.com/Tencent/WeKnora/internal/types"
)
func TestScopeClause_KnowledgeAndFullKBOredTogether(t *testing.T) {
sql, args := scopeClause(
[]string{"kb-1"},
[]string{"doc-9"},
nil,
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) {
sql, args := scopeClause(
[]string{"kb-1", "kb-2"},
nil,
nil,
map[string]uint64{"kb-1": 7}, // kb-2 has no tenant mapping
)
if strings.Contains(sql, "kb-2") {
@@ -42,7 +72,7 @@ func TestScopeClause_SkipsKBWithoutTenant(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 {
t.Fatalf("expected empty scope, got sql=%q args=%v", sql, args)
}