From de616ed4b7d2a6e55c9d380bd6068f792fe9f3a1 Mon Sep 17 00:00:00 2001 From: iceriverlin <4452839+iceriverlin@users.noreply.github.com> Date: Fri, 9 Oct 2026 20:59:58 +0800 Subject: [PATCH] test(service): assert RRF ranks ignore list arrival order fuseWithRRF reads each list's ranks through bestRanks, which ranks every list on its own by score. Retrievers run concurrently and their results are appended in goroutine completion order, so neither the order of the lists nor the order of the hits inside a list may change a chunk's rank. These tests fail on the pre-#3687 concatenation-order ranking and pass on main. Refs #3796. --- .../knowledgebase_search_fusion_test.go | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/internal/application/service/knowledgebase_search_fusion_test.go b/internal/application/service/knowledgebase_search_fusion_test.go index 3c251a61..1ad82138 100644 --- a/internal/application/service/knowledgebase_search_fusion_test.go +++ b/internal/application/service/knowledgebase_search_fusion_test.go @@ -3,6 +3,7 @@ package service import ( "context" "math" + "slices" "testing" "github.com/Tencent/WeKnora/internal/types" @@ -156,3 +157,78 @@ func TestRescaleUnboundedScores_IgnoresNonFiniteWhenFindingMax(t *testing.T) { require.InDelta(t, 1.0, hits[1].Score, 1e-9) require.InDelta(t, 0.5, hits[2].Score, 1e-9) } + +// The retrievers run concurrently and their lists are appended in goroutine +// completion order; the order of the hits inside a list is whatever the engine +// returned. Neither may influence a chunk's rank: ranks are per-list and read +// after sorting by score (#3796, fixed by #3687). +func TestBestRanks_IsOrderInvariant(t *testing.T) { + t.Parallel() + + rankLists := func(reversed bool) [][]*types.IndexWithScore { + docList := []*types.IndexWithScore{ + {ChunkID: "doc-1", Score: 0.81}, + {ChunkID: "doc-2", Score: 0.80}, + {ChunkID: "doc-3", Score: 0.79}, + } + faqList := []*types.IndexWithScore{ + {ChunkID: "faq-1", Score: 0.93}, + {ChunkID: "faq-2", Score: 0.70}, + } + keywordList := []*types.IndexWithScore{ + {ChunkID: "faq-1", Score: 7}, + {ChunkID: "doc-3", Score: 3}, + } + if reversed { + slices.Reverse(docList) + slices.Reverse(faqList) + slices.Reverse(keywordList) + return [][]*types.IndexWithScore{keywordList, faqList, docList} + } + return [][]*types.IndexWithScore{docList, faqList, keywordList} + } + + // faq-1 tops both of its lists. doc-3 holds rank 2 in the keyword list and + // rank 3 in the document list, so its best rank is 2. + want := map[string]int{"doc-1": 1, "doc-2": 2, "doc-3": 2, "faq-1": 1, "faq-2": 2} + require.Equal(t, want, bestRanks(rankLists(false))) + require.Equal(t, want, bestRanks(rankLists(true)), + "shuffling the lists must not change the ranks") +} + +// The same property end to end: the list that finishes last must not push its +// best hit behind every hit of the list that finished first. +func TestFuseOrDeduplicate_HybridIgnoresArrivalOrder(t *testing.T) { + t.Parallel() + + fuse := func(reversed bool) map[string]float64 { + t.Helper() + docList := []*types.IndexWithScore{ + {ChunkID: "doc-1", Score: 0.81}, + {ChunkID: "doc-2", Score: 0.80}, + } + faqList := []*types.IndexWithScore{ + {ChunkID: "faq-1", Score: 0.93}, + {ChunkID: "faq-2", Score: 0.70}, + } + keywordList := []*types.IndexWithScore{{ChunkID: "faq-1", Score: 7}} + + vectorLists := [][]*types.IndexWithScore{docList, faqList} + if reversed { + slices.Reverse(docList) + slices.Reverse(faqList) + // The FAQ list is the one that arrives first here. + vectorLists = [][]*types.IndexWithScore{faqList, docList} + } + scores := map[string]float64{} + for _, hit := range fuseOrDeduplicate(context.Background(), vectorLists, + [][]*types.IndexWithScore{keywordList}, nil) { + scores[hit.ChunkID] = hit.Score + } + return scores + } + + // fuseOrDeduplicate writes its RRF scores into the input hits, so each call + // builds its own fixtures. + require.Equal(t, fuse(false), fuse(true)) +}