fix(agent): enforce tag scopes across knowledge tools

This commit is contained in:
wizardchen
2026-06-29 18:35:05 +08:00
committed by lyingbug
parent 17ffd902ac
commit c29761331c
9 changed files with 486 additions and 40 deletions
+32
View File
@@ -3,6 +3,7 @@ package utils
import (
"encoding/json"
"fmt"
"strings"
"testing"
)
@@ -449,6 +450,37 @@ func TestInjectAndConditions(t *testing.T) {
}
}
func TestValidateAndSecureSQL_WithStructuredSearchScopes(t *testing.T) {
securedSQL, validation, err := ValidateAndSecureSQL(
"SELECT id FROM chunks",
WithSearchScopes([]SearchScope{
{KnowledgeBaseID: "kb-full"},
{KnowledgeBaseID: "kb-doc", KnowledgeIDs: []string{"doc-1"}},
{KnowledgeBaseID: "kb-tag", TagIDs: []string{"tag-a", "tag-b"}},
}),
)
if err != nil {
t.Fatalf("ValidateAndSecureSQL() error = %v", err)
}
if !validation.Valid {
t.Fatalf("expected validation to pass, got %#v", validation.Errors)
}
for _, want := range []string{
"chunks.knowledge_base_id = 'kb-full'",
"chunks.knowledge_base_id = 'kb-doc' AND chunks.knowledge_id IN ('doc-1')",
"chunks.knowledge_base_id = 'kb-tag' AND EXISTS",
"knowledge_tag_relations",
"ktr.knowledge_id = chunks.knowledge_id",
"ktr.tag_id IN ('tag-a', 'tag-b')",
" OR ",
} {
if !strings.Contains(securedSQL, want) {
t.Fatalf("secured SQL missing %q:\n%s", want, securedSQL)
}
}
}
func BenchmarkInjectAndConditions(b *testing.B) {
const sql = "SELECT id, title FROM docs WHERE status = 'active' ORDER BY created_at LIMIT 50"
for i := 0; i < b.N; i++ {