From 5723e837f89f5f3cfa0891defee37908d6adb79a Mon Sep 17 00:00:00 2001 From: wizardchen Date: Tue, 4 Aug 2026 21:06:39 +0800 Subject: [PATCH] fix(chat): cover web partial success and wiki-only embed fallback Refine embed-failure target filtering so wiki/graph-only KBs and missing KB metadata still reach HybridSearch instead of hard search_failed. Add regression tests for KB failure with concurrent web hits and wiki-only empty recall after embedding errors. --- .../service/chat_pipeline/search.go | 19 ++- .../chat_pipeline/search_error_test.go | 116 ++++++++++++++++++ 2 files changed, 134 insertions(+), 1 deletion(-) diff --git a/internal/application/service/chat_pipeline/search.go b/internal/application/service/chat_pipeline/search.go index e7635a91..a3b2423c 100644 --- a/internal/application/service/chat_pipeline/search.go +++ b/internal/application/service/chat_pipeline/search.go @@ -323,6 +323,23 @@ func logSearchScoreSample(ctx context.Context, action string, results []*types.S } } +// targetReportsEmbedFailure reports whether an embedding failure for a KB should +// be recorded as a retrieval error. Wiki/graph-only KBs have no vector or keyword +// index to degrade into; HybridSearch returns empty without error. When KB metadata +// is unavailable, callers still attempt keyword-degraded search. +func targetReportsEmbedFailure(kb *types.KnowledgeBase) bool { + if kb == nil { + return false + } + if kb.Type == types.KnowledgeBaseTypeFAQ { + return true + } + if kb.IsKeywordEnabled() { + return false + } + return kb.IsVectorEnabled() +} + // searchByTargets performs KB searches using pre-computed SearchTargets. // Targets sharing the same underlying embedding model (identified by model // name + endpoint, not just model ID) are grouped so the query embedding is @@ -403,7 +420,7 @@ func (p *PluginSearch) searchByTargets( searchableTargets = make([]*types.SearchTarget, 0, len(targets)) for _, target := range targets { kb := kbMap[target.KnowledgeBaseID] - if kb != nil && kb.Type != types.KnowledgeBaseTypeFAQ && kb.IsKeywordEnabled() { + if !targetReportsEmbedFailure(kb) { searchableTargets = append(searchableTargets, target) continue } diff --git a/internal/application/service/chat_pipeline/search_error_test.go b/internal/application/service/chat_pipeline/search_error_test.go index 581d9619..856755a0 100644 --- a/internal/application/service/chat_pipeline/search_error_test.go +++ b/internal/application/service/chat_pipeline/search_error_test.go @@ -257,6 +257,122 @@ func (s *partialSearchKnowledgeBaseService) HybridSearch( return []*types.SearchResult{{ID: "chunk-good", Content: "可用结果", KnowledgeID: "knowledge-good"}}, nil } +type stubWebSearchService struct { + interfaces.WebSearchService + results []*types.WebSearchResult +} + +func (s *stubWebSearchService) Search( + _ context.Context, _ string, _ *types.WebSearchConfig, _ string, +) ([]*types.WebSearchResult, error) { + return s.results, nil +} + +func TestSearchKeepsWebResultsWhenKnowledgeBaseFails(t *testing.T) { + rootCause := errors.New("kb store unavailable") + plugin := &PluginSearch{ + knowledgeBaseService: &failingSearchKnowledgeBaseService{err: rootCause}, + webSearchService: &stubWebSearchService{ + results: []*types.WebSearchResult{{ + URL: "https://example.com/doc", + Title: "Web hit", + Content: "web content", + }}, + }, + tenantService: &struct{ interfaces.TenantService }{}, + } + chatManage := &types.ChatManage{ + PipelineRequest: types.PipelineRequest{ + SearchTargets: types.SearchTargets{{ + Type: types.SearchTargetTypeKnowledgeBase, + KnowledgeBaseID: "kb-1", + }}, + EmbeddingTopK: 10, + WebSearchEnabled: true, + WebSearchProviderID: "provider-1", + }, + PipelineState: types.PipelineState{RewriteQuery: "并发检索"}, + } + nextCalled := false + + err := plugin.OnEvent(context.Background(), types.CHUNK_SEARCH, chatManage, func() *PluginError { + nextCalled = true + return nil + }) + if err != nil { + t.Fatalf("expected web results to keep pipeline alive, got %#v", err) + } + if !nextCalled { + t.Fatal("expected pipeline to continue with web results") + } + if len(chatManage.SearchResult) != 1 || chatManage.SearchResult[0].ID != "https://example.com/doc" { + t.Fatalf("expected web search result, got %#v", chatManage.SearchResult) + } +} + +type wikiOnlySearchKnowledgeBaseService struct { + interfaces.KnowledgeBaseService + embedErr error + hybridCalled bool +} + +func (s *wikiOnlySearchKnowledgeBaseService) GetKnowledgeBasesByIDsOnly( + context.Context, []string, +) ([]*types.KnowledgeBase, error) { + return []*types.KnowledgeBase{{ + ID: "wiki-1", + Type: types.KnowledgeBaseTypeDocument, + EmbeddingModelID: "embedding-1", + IndexingStrategy: types.IndexingStrategy{WikiEnabled: true}, + }}, nil +} + +func (s *wikiOnlySearchKnowledgeBaseService) ResolveEmbeddingModelKeys( + context.Context, []*types.KnowledgeBase, +) map[string]string { + return map[string]string{"wiki-1": "doubao|https://ark.cn-beijing.volces.com/api/v3"} +} + +func (s *wikiOnlySearchKnowledgeBaseService) GetQueryEmbedding( + context.Context, string, string, +) ([]float32, error) { + return nil, s.embedErr +} + +func (s *wikiOnlySearchKnowledgeBaseService) HybridSearch( + context.Context, string, types.SearchParams, +) ([]*types.SearchResult, error) { + s.hybridCalled = true + return nil, nil +} + +func TestSearchEmbeddingFailureOnWikiOnlyTargetReportsNoResults(t *testing.T) { + svc := &wikiOnlySearchKnowledgeBaseService{ + embedErr: errors.New("embedding endpoint unavailable"), + } + plugin := &PluginSearch{knowledgeBaseService: svc} + chatManage := &types.ChatManage{ + PipelineRequest: types.PipelineRequest{ + SearchTargets: types.SearchTargets{{ + Type: types.SearchTargetTypeKnowledgeBase, + KnowledgeBaseID: "wiki-1", + }}, + EmbeddingTopK: 10, + }, + PipelineState: types.PipelineState{RewriteQuery: "wiki only"}, + } + + err := plugin.OnEvent(context.Background(), types.CHUNK_SEARCH, chatManage, func() *PluginError { + return nil + }) + if err != ErrSearchNothing { + t.Fatalf("expected search_nothing for wiki-only KB, got %#v", err) + } + if !svc.hybridCalled { + t.Fatal("wiki-only target should still delegate empty recall to HybridSearch") + } +} + func TestSearchKeepsSuccessfulResultsWhenAnotherTargetFails(t *testing.T) { rootCause := errors.New("one store unavailable") plugin := &PluginSearch{knowledgeBaseService: &partialSearchKnowledgeBaseService{rootCause: rootCause}}