Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(chat-pipeline): honor normalized graph search targets (#4530)

Lucien8317220bc8fb
diff --git a/internal/application/service/chat_pipeline/extract_entity.go b/internal/application/service/chat_pipeline/extract_entity.go
index a7b5bcd2e..9f438192e 100644
--- a/internal/application/service/chat_pipeline/extract_entity.go
+++ b/internal/application/service/chat_pipeline/extract_entity.go
@@ -22,8 +22,6 @@ type PluginExtractEntity struct {
modelService interfaces.ModelService // Model service for calling large language models
template *types.PromptTemplateStructured // Template for generating prompts
knowledgeBaseRepo interfaces.KnowledgeBaseRepository
- knowledgeService interfaces.KnowledgeService // For shared KB document resolution
- knowledgeRepo interfaces.KnowledgeRepository
}
// NewPluginExtractEntity creates a new extract-entity plugin instance
@@ -32,16 +30,12 @@ func NewPluginExtractEntity(
eventManager *EventManager,
modelService interfaces.ModelService,
knowledgeBaseRepo interfaces.KnowledgeBaseRepository,
- knowledgeService interfaces.KnowledgeService,
- knowledgeRepo interfaces.KnowledgeRepository,
config *config.Config,
) *PluginExtractEntity {
res := &PluginExtractEntity{
modelService: modelService,
template: config.ExtractManager.ExtractEntity,
knowledgeBaseRepo: knowledgeBaseRepo,
- knowledgeService: knowledgeService,
- knowledgeRepo: knowledgeRepo,
}
eventManager.Register(res)
return res
@@ -68,6 +62,12 @@ func (p *PluginExtractEntity) OnEvent(ctx context.Context,
logger.Debugf(ctx, "skipping extract entity, intent %q needs no KB retrieval", chatManage.Intent)
return next()
}
+ // SearchTargets already include access checks and resolved document/tag
+ // scopes. An empty scope must not fall back to the raw request's KB IDs.
+ allKBIDs := chatManage.SearchTargets.GetAllKnowledgeBaseIDs()
+ if len(allKBIDs) == 0 {
+ return next()
+ }
// Prefer the rewritten query: it resolves pronouns and ellipsis from the
// conversation, which the raw follow-up question lacks.
@@ -82,33 +82,6 @@ func (p *PluginExtractEntity) OnEvent(ctx context.Context,
return next()
}
- // Collect all knowledge base IDs to query
- kbIDSet := make(map[string]struct{})
- for _, id := range chatManage.KnowledgeBaseIDs {
- kbIDSet[id] = struct{}{}
- }
-
- // If KnowledgeIDs is specified, retrieve them and collect their knowledge base IDs (include shared KB docs)
- // Also build a mapping from KnowledgeID to KnowledgeBaseID
- knowledgeToKBMap := make(map[string]string)
- if len(chatManage.KnowledgeIDs) > 0 {
- knowledges, err := p.knowledgeService.GetKnowledgeBatchWithSharedAccess(ctx, chatManage.TenantID, chatManage.KnowledgeIDs)
- if err != nil {
- logger.Errorf(ctx, "failed to get knowledges: %v", err)
- return next()
- }
- for _, k := range knowledges {
- kbIDSet[k.KnowledgeBaseID] = struct{}{}
- knowledgeToKBMap[k.ID] = k.KnowledgeBaseID
- }
- }
-
- // Convert set to slice
- allKBIDs := make([]string, 0, len(kbIDSet))
- for id := range kbIDSet {
- allKBIDs = append(allKBIDs, id)
- }
-
// Batch retrieve all knowledge bases
kbs, err := p.knowledgeBaseRepo.GetKnowledgeBaseByIDs(ctx, allKBIDs)
if err != nil {
@@ -135,15 +108,6 @@ func (p *PluginExtractEntity) OnEvent(ctx context.Context,
}
chatManage.EntityKBIDs = enabledKBIDs
- // Filter knowledgeToKBMap to only include files from enabled knowledge bases
- entityKnowledge := make(map[string]string)
- for knowledgeID, kbID := range knowledgeToKBMap {
- if _, ok := enabledKBSet[kbID]; ok {
- entityKnowledge[knowledgeID] = kbID
- }
- }
- chatManage.EntityKnowledge = entityKnowledge
-
template := &types.PromptTemplateStructured{
Description: p.template.Description,
Examples: p.template.Examples,
diff --git a/internal/application/service/chat_pipeline/search_entity.go b/internal/application/service/chat_pipeline/search_entity.go
index 59b20022e..fb6f33045 100644
--- a/internal/application/service/chat_pipeline/search_entity.go
+++ b/internal/application/service/chat_pipeline/search_entity.go
@@ -2,12 +2,14 @@ package chatpipeline
import (
"context"
+ "slices"
"sync"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/searchutil"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
+ "golang.org/x/sync/errgroup"
)
// PluginSearch implements search functionality for chat pipeline
@@ -48,84 +50,70 @@ func (p *PluginSearchEntity) OnEvent(ctx context.Context,
return next()
}
- // Use EntityKBIDs (knowledge bases with ExtractConfig enabled)
- knowledgeBaseIDs := chatManage.EntityKBIDs
- // Use EntityKnowledge (KnowledgeID -> KnowledgeBaseID mapping for graph-enabled files)
- entityKnowledge := chatManage.EntityKnowledge
-
- if len(knowledgeBaseIDs) == 0 && len(entityKnowledge) == 0 {
- logger.Warnf(ctx, "No knowledge base IDs or knowledge IDs with ExtractConfig enabled for entity search")
+ // Graph-enabled KBs are a capability filter, not a replacement for the
+ // normalized document/tag scopes in SearchTargets.
+ enabledKBs := make(map[string]bool, len(chatManage.EntityKBIDs))
+ for _, kbID := range chatManage.EntityKBIDs {
+ enabledKBs[kbID] = true
+ }
+ namespaces := make(map[types.NameSpace]bool)
+ for _, target := range chatManage.SearchTargets {
+ if target == nil || target.KnowledgeBaseID == "" || !enabledKBs[target.KnowledgeBaseID] {
+ continue
+ }
+ switch target.Type {
+ case types.SearchTargetTypeKnowledgeBase:
+ namespaces[types.NameSpace{KnowledgeBase: target.KnowledgeBaseID}] = true
+ case types.SearchTargetTypeKnowledge:
+ for _, knowledgeID := range target.KnowledgeIDs {
+ if knowledgeID != "" {
+ namespaces[types.NameSpace{KnowledgeBase: target.KnowledgeBaseID, Knowledge: knowledgeID}] = true
+ }
+ }
+ }
+ }
+ if len(namespaces) == 0 {
+ logger.Debugf(ctx, "No graph-enabled search targets")
return next()
}
// Parallel search across multiple knowledge bases and individual files
- var wg sync.WaitGroup
+ var group errgroup.Group
+ // A tag can resolve to many files; bound the graph-store fan-out.
+ group.SetLimit(4)
var mu sync.Mutex
var allNodes []*types.GraphNode
var allRelations []*types.GraphRelation
- // If specific KnowledgeIDs are provided, search by individual files
- if len(entityKnowledge) > 0 {
- logger.Infof(ctx, "Searching entities across %d knowledge file(s)", len(entityKnowledge))
- for knowledgeID, kbID := range entityKnowledge {
- wg.Add(1)
- go func(knowledgeBaseID, knowledgeID string) {
- defer wg.Done()
-
- graph, err := p.graphRepo.SearchNode(ctx, types.NameSpace{
- KnowledgeBase: knowledgeBaseID,
- Knowledge: knowledgeID,
- }, entity)
- if err != nil {
- logger.Errorf(ctx, "Failed to search entity in Knowledge %s: %v", knowledgeID, err)
- return
- }
-
- logger.Infof(
- ctx,
- "Knowledge %s entity search result count: %d nodes, %d relations",
- knowledgeID,
- len(graph.Node),
- len(graph.Relation),
- )
-
- mu.Lock()
- allNodes = append(allNodes, graph.Node...)
- allRelations = append(allRelations, graph.Relation...)
- mu.Unlock()
- }(kbID, knowledgeID)
+ for namespace := range namespaces {
+ // A whole-KB query already includes its explicitly selected files.
+ if namespace.Knowledge != "" && namespaces[types.NameSpace{KnowledgeBase: namespace.KnowledgeBase}] {
+ continue
}
- } else {
- // Otherwise, search by knowledge base
- logger.Infof(ctx, "Searching entities across %d knowledge base(s): %v", len(knowledgeBaseIDs), knowledgeBaseIDs)
- for _, kbID := range knowledgeBaseIDs {
- wg.Add(1)
- go func(knowledgeBaseID string) {
- defer wg.Done()
-
- graph, err := p.graphRepo.SearchNode(ctx, types.NameSpace{KnowledgeBase: knowledgeBaseID}, entity)
- if err != nil {
- logger.Errorf(ctx, "Failed to search entity in KB %s: %v", knowledgeBaseID, err)
- return
- }
-
- logger.Infof(
+ group.Go(func() error {
+ graph, err := p.graphRepo.SearchNode(ctx, namespace, entity)
+ if err != nil {
+ logger.Errorf(
ctx,
- "KB %s entity search result count: %d nodes, %d relations",
- knowledgeBaseID,
- len(graph.Node),
- len(graph.Relation),
+ "Failed to search entity in KB %s, knowledge %s: %v",
+ namespace.KnowledgeBase,
+ namespace.Knowledge,
+ err,
)
-
- mu.Lock()
- allNodes = append(allNodes, graph.Node...)
- allRelations = append(allRelations, graph.Relation...)
- mu.Unlock()
- }(kbID)
- }
+ return nil
+ }
+ if graph == nil {
+ return nil
+ }
+ mu.Lock()
+ allNodes = append(allNodes, graph.Node...)
+ allRelations = append(allRelations, graph.Relation...)
+ mu.Unlock()
+ return nil
+ })
}
- wg.Wait()
+ _ = group.Wait()
// Merge graph data
chatManage.GraphResult = &types.GraphData{
@@ -141,15 +129,8 @@ func (p *PluginSearchEntity) OnEvent(ctx context.Context,
}
// Graph hits from an org-shared KB belong to the sharing workspace, so
// the lookups are not tenant scoped (as for parents, #3342). The graph
- // namespaces are the KBs in this turn's scope; a chunk is only used when
- // its row belongs to one of them.
- allowedKBs := make(map[string]bool, len(knowledgeBaseIDs)+len(entityKnowledge))
- for _, id := range knowledgeBaseIDs {
- allowedKBs[id] = true
- }
- for _, kbID := range entityKnowledge {
- allowedKBs[kbID] = true
- }
+ // namespaces come from this turn's targets, but stored graph evidence may
+ // be stale. Recheck its current document and physical tag scope below.
listed, err := p.chunkRepo.ListChunksByIDOnly(ctx, chunkIDs)
if err != nil {
logger.Errorf(ctx, "Failed to list chunks, session_id: %s, error: %v", chatManage.SessionID, err)
@@ -158,7 +139,7 @@ func (p *PluginSearchEntity) OnEvent(ctx context.Context,
chunks := make([]*types.Chunk, 0, len(listed))
knowledgeIDs := []string{}
for _, chunk := range listed {
- if chunk == nil || !allowedKBs[chunk.KnowledgeBaseID] {
+ if chunk == nil || !enabledKBs[chunk.KnowledgeBaseID] || !entityChunkInScope(chunk, chatManage.SearchTargets) {
continue
}
chunks = append(chunks, chunk)
@@ -201,6 +182,27 @@ func (p *PluginSearchEntity) OnEvent(ctx context.Context,
return next()
}
+func entityChunkInScope(chunk *types.Chunk, targets types.SearchTargets) bool {
+ for _, target := range targets {
+ if target == nil || target.KnowledgeBaseID != chunk.KnowledgeBaseID {
+ continue
+ }
+ switch target.Type {
+ case types.SearchTargetTypeKnowledgeBase:
+ case types.SearchTargetTypeKnowledge:
+ if !slices.Contains(target.KnowledgeIDs, chunk.KnowledgeID) {
+ continue
+ }
+ default:
+ continue
+ }
+ if len(target.TagIDs) == 0 || slices.Contains(target.TagIDs, chunk.TagID) {
+ return true
+ }
+ }
+ return false
+}
+
// maxEntityChunks bounds the chunks graph entity search adds to a turn.
const maxEntityChunks = 30
diff --git a/internal/application/service/chat_pipeline/search_entity_scope_test.go b/internal/application/service/chat_pipeline/search_entity_scope_test.go
index 521175ae9..c5ef7dc5e 100644
--- a/internal/application/service/chat_pipeline/search_entity_scope_test.go
+++ b/internal/application/service/chat_pipeline/search_entity_scope_test.go
@@ -2,18 +2,32 @@ package chatpipeline
import (
"context"
+ "fmt"
+ "slices"
+ "sync"
"testing"
+ "time"
+ "github.com/Tencent/WeKnora/internal/models/chat"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
type entityGraphRepo struct {
interfaces.RetrieveGraphRepository
- graph *types.GraphData
+ graph *types.GraphData
+ mu sync.Mutex
+ queries []types.NameSpace
}
-func (r *entityGraphRepo) SearchNode(context.Context, types.NameSpace, []string) (*types.GraphData, error) {
+func (r *entityGraphRepo) SearchNode(
+ _ context.Context,
+ namespace types.NameSpace,
+ _ []string,
+) (*types.GraphData, error) {
+ r.mu.Lock()
+ r.queries = append(r.queries, namespace)
+ r.mu.Unlock()
return r.graph, nil
}
@@ -49,9 +63,12 @@ func TestSearchEntityResolvesSharedChunksWithinScope(t *testing.T) {
"doc-x": {ID: "doc-x", KnowledgeBaseID: "kb-other", Title: "Other"},
}},
}
- cm := &types.ChatManage{PipelineState: types.PipelineState{
- Entity: []string{"Docker"}, EntityKBIDs: []string{"kb-shared"},
- }}
+ cm := &types.ChatManage{
+ PipelineRequest: types.PipelineRequest{SearchTargets: types.SearchTargets{
+ {Type: types.SearchTargetTypeKnowledgeBase, KnowledgeBaseID: "kb-shared", TenantID: 2},
+ }},
+ PipelineState: types.PipelineState{Entity: []string{"Docker"}, EntityKBIDs: []string{"kb-shared"}},
+ }
ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(1))
if err := plugin.OnEvent(ctx, types.ENTITY_SEARCH, cm, func() *PluginError { return nil }); err != nil {
@@ -68,3 +85,269 @@ func entityChunk(id string, tenantID uint64, kbID, knowledgeID string) *types.Ch
ID: id, TenantID: tenantID, KnowledgeBaseID: kbID, KnowledgeID: knowledgeID, IsEnabled: true, Content: id,
}
}
+
+type entityTargetKBRepo struct {
+ interfaces.KnowledgeBaseRepository
+ requested []string
+}
+
+func (r *entityTargetKBRepo) GetKnowledgeBaseByIDs(_ context.Context, ids []string) ([]*types.KnowledgeBase, error) {
+ r.requested = append([]string(nil), ids...)
+ var out []*types.KnowledgeBase
+ for _, id := range ids {
+ out = append(out, &types.KnowledgeBase{
+ ID: id, ExtractConfig: &types.ExtractConfig{Enabled: id != "kb-disabled"},
+ })
+ }
+ return out, nil
+}
+
+type entityTargetChat struct {
+ openStreamChat
+ calls int
+}
+
+func (m *entityTargetChat) Chat(context.Context, []chat.Message, *chat.ChatOptions) (*types.ChatResponse, error) {
+ m.calls++
+ return &types.ChatResponse{Content: `[{"entity":"Docker"}]`}, nil
+}
+
+// Exercise extraction and search together: both stages must keep the same
+// normalized scope, even when legacy request IDs disagree with it.
+func TestEntityRetrievalUsesSearchTargets(t *testing.T) {
+ t.Setenv("NEO4J_ENABLE", "true")
+ tests := []struct {
+ name string
+ rawKBs []string
+ rawDocs []string
+ targets types.SearchTargets
+ wantIDs []string
+ wantScopes []types.NameSpace
+ }{
+ {
+ name: "tagged documents exclude same-KB evidence outside the tag",
+ rawKBs: []string{"kb-shared"},
+ targets: types.SearchTargets{{
+ Type: types.SearchTargetTypeKnowledge,
+ KnowledgeBaseID: "kb-shared",
+ TenantID: 2,
+ KnowledgeIDs: []string{"doc-shared", "doc-disabled", "doc-gone"},
+ ScopeTagIDs: []string{"tag-selected"},
+ }},
+ wantIDs: []string{"shared"},
+ wantScopes: []types.NameSpace{
+ {KnowledgeBase: "kb-shared", Knowledge: "doc-shared"},
+ {KnowledgeBase: "kb-shared", Knowledge: "doc-disabled"},
+ {KnowledgeBase: "kb-shared", Knowledge: "doc-gone"},
+ },
+ },
+ {
+ name: "tag-only mention uses resolved targets without raw IDs",
+ targets: types.SearchTargets{{
+ Type: types.SearchTargetTypeKnowledge, KnowledgeBaseID: "kb-shared",
+ TenantID: 2, KnowledgeIDs: []string{"doc-shared"}, ScopeTagIDs: []string{"tag-selected"},
+ }},
+ wantIDs: []string{"shared"},
+ wantScopes: []types.NameSpace{{KnowledgeBase: "kb-shared", Knowledge: "doc-shared"}},
+ },
+ {
+ name: "normalized targets override raw request IDs",
+ rawKBs: []string{"kb-other"},
+ rawDocs: []string{"doc-other"},
+ targets: types.SearchTargets{{
+ Type: types.SearchTargetTypeKnowledge, KnowledgeBaseID: "kb-shared",
+ KnowledgeIDs: []string{"doc-shared"},
+ }},
+ wantIDs: []string{"shared"},
+ wantScopes: []types.NameSpace{{KnowledgeBase: "kb-shared", Knowledge: "doc-shared"}},
+ },
+ {
+ name: "empty tag results never fall back to raw KB or file IDs",
+ rawKBs: []string{"kb-shared"}, rawDocs: []string{"doc-shared"},
+ },
+ {
+ name: "empty document target is not a whole-KB target",
+ rawKBs: []string{"kb-shared"},
+ targets: types.SearchTargets{{Type: types.SearchTargetTypeKnowledge, KnowledgeBaseID: "kb-shared"}},
+ },
+ {
+ name: "whole KB and file in another KB are both searched",
+ rawKBs: []string{"kb-full"}, rawDocs: []string{"doc-shared"},
+ targets: types.SearchTargets{
+ {Type: types.SearchTargetTypeKnowledgeBase, KnowledgeBaseID: "kb-full"},
+ {
+ Type: types.SearchTargetTypeKnowledge,
+ KnowledgeBaseID: "kb-shared",
+ TenantID: 2,
+ KnowledgeIDs: []string{"doc-shared"},
+ },
+ },
+ wantIDs: []string{"full", "shared"},
+ wantScopes: []types.NameSpace{
+ {KnowledgeBase: "kb-full"}, {KnowledgeBase: "kb-shared", Knowledge: "doc-shared"},
+ },
+ },
+ {
+ name: "whole KB covers repeated KB and file targets without extra queries",
+ targets: types.SearchTargets{
+ {Type: types.SearchTargetTypeKnowledgeBase, KnowledgeBaseID: "kb-full"},
+ {Type: types.SearchTargetTypeKnowledgeBase, KnowledgeBaseID: "kb-full"},
+ {
+ Type: types.SearchTargetTypeKnowledge, KnowledgeBaseID: "kb-full",
+ KnowledgeIDs: []string{"doc-full", "doc-full"},
+ },
+ },
+ wantIDs: []string{"full"}, wantScopes: []types.NameSpace{{KnowledgeBase: "kb-full"}},
+ },
+ {
+ name: "physical chunk tags remain constrained in KB targets",
+ targets: types.SearchTargets{{
+ Type: types.SearchTargetTypeKnowledgeBase, KnowledgeBaseID: "kb-shared",
+ TenantID: 2, TagIDs: []string{"tag-selected"},
+ }},
+ wantIDs: []string{"shared"}, wantScopes: []types.NameSpace{{KnowledgeBase: "kb-shared"}},
+ },
+ {
+ name: "KBs without graph extraction do not produce graph queries",
+ targets: types.SearchTargets{{Type: types.SearchTargetTypeKnowledgeBase, KnowledgeBaseID: "kb-disabled"}},
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ model := &entityTargetChat{}
+ kbRepo := &entityTargetKBRepo{}
+ extract := &PluginExtractEntity{
+ modelService: &stubModelService{model: model}, knowledgeBaseRepo: kbRepo,
+ template: &types.PromptTemplateStructured{Description: "Extract entities"},
+ }
+ graphRepo := &entityGraphRepo{graph: &types.GraphData{Node: []*types.GraphNode{
+ {Name: "Docker", Chunks: []string{"shared", "outside", "disabled", "orphan", "foreign", "full"}},
+ }}}
+ chunks := map[string]*types.Chunk{
+ "shared": entityChunk("shared", 2, "kb-shared", "doc-shared"),
+ "outside": entityChunk("outside", 2, "kb-shared", "doc-outside"),
+ "disabled": entityChunk("disabled", 2, "kb-shared", "doc-disabled"),
+ "orphan": entityChunk("orphan", 2, "kb-shared", "doc-gone"),
+ "foreign": entityChunk("foreign", 3, "kb-other", "doc-other"),
+ "full": entityChunk("full", 1, "kb-full", "doc-full"),
+ }
+ chunks["shared"].TagID = "tag-selected"
+ chunks["disabled"].IsEnabled = false
+ knowledge := map[string]*types.Knowledge{}
+ for _, chunk := range chunks {
+ if chunk.KnowledgeID != "doc-gone" {
+ knowledge[chunk.KnowledgeID] = &types.Knowledge{
+ ID: chunk.KnowledgeID, KnowledgeBaseID: chunk.KnowledgeBaseID,
+ }
+ }
+ }
+ search := &PluginSearchEntity{
+ graphRepo: graphRepo, chunkRepo: &expandChunkRepo{chunks: chunks},
+ knowledgeRepo: &entityKnowledgeRepo{knowledge: knowledge},
+ }
+ cm := &types.ChatManage{PipelineRequest: types.PipelineRequest{
+ Query: "Docker", TenantID: 1, KnowledgeBaseIDs: tt.rawKBs,
+ KnowledgeIDs: tt.rawDocs, SearchTargets: tt.targets,
+ }}
+ ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(1))
+ if err := extract.OnEvent(ctx, types.QUERY_UNDERSTAND, cm, func() *PluginError { return nil }); err != nil {
+ t.Fatalf("extract: %v", err)
+ }
+ if err := search.OnEvent(ctx, types.ENTITY_SEARCH, cm, func() *PluginError { return nil }); err != nil {
+ t.Fatalf("search: %v", err)
+ }
+ var gotIDs []string
+ for _, result := range cm.SearchResult {
+ gotIDs = append(gotIDs, result.ID)
+ }
+ slices.Sort(gotIDs)
+ if !slices.Equal(gotIDs, tt.wantIDs) {
+ t.Fatalf("result IDs = %v, want %v", gotIDs, tt.wantIDs)
+ }
+ if len(graphRepo.queries) != len(tt.wantScopes) {
+ t.Fatalf("graph queries = %+v, want %+v", graphRepo.queries, tt.wantScopes)
+ }
+ for _, scope := range tt.wantScopes {
+ if !slices.Contains(graphRepo.queries, scope) {
+ t.Errorf("missing graph namespace %+v in %+v", scope, graphRepo.queries)
+ }
+ }
+ if len(tt.targets) == 0 && (model.calls != 0 || len(kbRepo.requested) != 0) {
+ t.Fatal("empty normalized scope must not trigger graph extraction or KB lookups")
+ }
+ })
+ }
+}
+
+type blockingEntityGraphRepo struct {
+ interfaces.RetrieveGraphRepository
+ entered chan types.NameSpace
+ mu sync.Mutex
+ active int
+ peak int
+ queries []types.NameSpace
+}
+
+func (r *blockingEntityGraphRepo) SearchNode(
+ ctx context.Context,
+ namespace types.NameSpace,
+ _ []string,
+) (*types.GraphData, error) {
+ r.mu.Lock()
+ r.active++
+ r.peak = max(r.peak, r.active)
+ r.queries = append(r.queries, namespace)
+ r.mu.Unlock()
+ defer func() {
+ r.mu.Lock()
+ r.active--
+ r.mu.Unlock()
+ }()
+ r.entered <- namespace
+ <-ctx.Done()
+ return nil, ctx.Err()
+}
+
+func TestSearchEntityBoundsTagFanoutAndFinishesCanceledQueries(t *testing.T) {
+ var knowledgeIDs []string
+ for i := range 12 {
+ knowledgeIDs = append(knowledgeIDs, fmt.Sprintf("doc-%d", i))
+ }
+ repo := &blockingEntityGraphRepo{entered: make(chan types.NameSpace, len(knowledgeIDs))}
+ plugin := &PluginSearchEntity{graphRepo: repo}
+ cm := &types.ChatManage{
+ PipelineRequest: types.PipelineRequest{SearchTargets: types.SearchTargets{{
+ Type: types.SearchTargetTypeKnowledge, KnowledgeBaseID: "kb", KnowledgeIDs: knowledgeIDs,
+ ScopeTagIDs: []string{"tag-many-documents"},
+ }}},
+ PipelineState: types.PipelineState{Entity: []string{"Docker"}, EntityKBIDs: []string{"kb"}},
+ }
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ done := make(chan *PluginError, 1)
+ go func() { done <- plugin.OnEvent(ctx, types.ENTITY_SEARCH, cm, func() *PluginError { return nil }) }()
+ for range 4 {
+ select {
+ case <-repo.entered:
+ case <-time.After(5 * time.Second):
+ t.Fatal("graph searches did not start")
+ }
+ }
+ cancel()
+ select {
+ case err := <-done:
+ if err != nil {
+ t.Fatalf("graph failures should preserve the existing partial-success behavior: %v", err)
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatal("canceled graph searches did not finish")
+ }
+ if repo.peak != 4 || repo.active != 0 || len(repo.queries) != len(knowledgeIDs) {
+ t.Fatalf("graph fan-out: peak=%d, active=%d, queries=%v", repo.peak, repo.active, repo.queries)
+ }
+ for _, knowledgeID := range knowledgeIDs {
+ if !slices.Contains(repo.queries, types.NameSpace{KnowledgeBase: "kb", Knowledge: knowledgeID}) {
+ t.Errorf("missing graph query for %s", knowledgeID)
+ }
+ }
+}
diff --git a/internal/types/chat_manage.go b/internal/types/chat_manage.go
index 2c9153714..fd8908c04 100644
--- a/internal/types/chat_manage.go
+++ b/internal/types/chat_manage.go
@@ -186,15 +186,14 @@ type PipelineState struct {
// History (a first turn) is not fetched again by a later stage.
HistoryLoaded bool `json:"-"`
- SearchResult []*SearchResult `json:"-"`
- RerankResult []*SearchResult `json:"-"`
- MergeResult []*SearchResult `json:"-"`
- Entity []string `json:"-"`
- EntityKBIDs []string `json:"-"`
- EntityKnowledge map[string]string `json:"-"`
- GraphResult *GraphData `json:"-"`
- UserContent string `json:"-"`
- RenderedContexts string `json:"-"`
+ SearchResult []*SearchResult `json:"-"`
+ RerankResult []*SearchResult `json:"-"`
+ MergeResult []*SearchResult `json:"-"`
+ Entity []string `json:"-"`
+ EntityKBIDs []string `json:"-"`
+ GraphResult *GraphData `json:"-"`
+ UserContent string `json:"-"`
+ RenderedContexts string `json:"-"`
// ContextImages are retrieved images, as data URIs, shown to a vision
// chat model beside the contexts they belong to; see INTO_CHAT_MESSAGE.
ContextImages []string `json:"-"`
@@ -304,9 +303,6 @@ func (c *ChatManage) Clone() *ChatManage {
entityKBIDs := make([]string, len(c.EntityKBIDs))
copy(entityKBIDs, c.EntityKBIDs)
- entityKnowledge := make(map[string]string)
- maps.Copy(entityKnowledge, c.EntityKnowledge)
-
return &ChatManage{
PipelineRequest: PipelineRequest{
Query: c.Query,
@@ -367,7 +363,6 @@ func (c *ChatManage) Clone() *ChatManage {
ContextImageChunkIDs: slices.Clone(c.ContextImageChunkIDs),
Entity: entity,
EntityKBIDs: entityKBIDs,
- EntityKnowledge: entityKnowledge,
},
}
}