WeKnora icon indicating copy to clipboard operation
WeKnora copied to clipboard

[Bug]: RRF assigns different ranks and scores to identical tied retrieval inputs

Open OUAO-FRANK opened this issue 1 month ago • 3 comments

Affected Component

Backend Service & API — hybrid retrieval / RRF fusion

Bug Description

On main f17ee8a8da901bd658a4cb25f40b5fd41adbfdd7, identical retrieval lists containing equal scores can receive different RRF ranks and different fused scores on repeated calls.

deduplicateByScore collects hits in a Go map and sorts the values with a comparator that returns zero for equal scores. bestRanks then converts that unspecified map order into distinct ordinal ranks. The vector and keyword ranks can change independently, so the weighted RRF score and winning chunk change even when neither retriever's input changes.

Equal scores are ordinary input: the fusion code explicitly supports keyword engines that return a score of 1.0 for each match. This is not limited to malformed scores or an unstable provider response.

Steps to Reproduce

I executed the unchanged knowledgebase_search_fusion.go, its existing tests, and two additional tests in a temporary package below this module's internal/ directory. The original internal/types and internal/logger packages were used, without stubbing the fusion logic or calling a model/database.

The new tests construct 16 hits with IDs chunk-00 through chunk-15, all with Score: 1:

hits := make([]*types.IndexWithScore, 16)
for i := range hits {
    hits[i] = &types.IndexWithScore{
        ChunkID: fmt.Sprintf("chunk-%02d", i), Score: 1,
    }
}
ranks := bestRanks([][]*types.IndexWithScore{hits})
  1. Call bestRanks repeatedly with freshly constructed identical lists and compare the chunk-to-rank maps.
  2. Call fuseWithRRF repeatedly with those identical tied lists as both vector and keyword inputs, using default weights; compare chunk-to-score maps.

Both repeatability tests failed on their first comparison in the local run. For example, chunk-00 moved from rank 13 to rank 3. In the separate fusion test its score changed from approximately 0.961844 to 0.962349, and the highest-scoring chunk also changed.

Expected Behavior

Unchanged candidate lists should produce stable ranks and fused scores, including when input scores tie. A deterministic chunk-ID tiebreaker when sorting deduplicated hits would remove the map-order dependency while preserving ordering for unequal scores and the existing RRF formula.

Actual Behavior

Random map iteration participates in rank calculation, changing relevance scores before downstream sorting or TopK selection.

Version and Validation

  • Source: current main f17ee8a8da901bd658a4cb25f40b5fd41adbfdd7.
  • Windows amd64, Go 1.26.9.
  • Two focused tests executed against an exact copy of the current fusion source and the real imported types/logger: both failed as described.
  • This is a local fusion-layer reproduction, not a live knowledge-search endpoint or retrieval-quality benchmark. Full service-package validation and a fix are pending.

Related Work / Duplicate Checks

  • #2091 stabilizes final chat-pipeline ordering in filter_top_k.go. It does not stabilize the earlier ranks and numeric scores calculated here, so sorting the final results cannot recover those scores.
  • #4089 tests arrival-order invariance using unequal scores. Its cases pass through a different boundary: tied scores still reach the map-order-dependent rank assignment.
  • #4146 changes Qdrant keyword scoring, rather than this shared fusion logic.
  • Searched open/closed PRs and issues for RRF ties, deterministic retrieval, deduplicateByScore, and bestRanks; no patch for this shared tied-rank calculation was found.

Confirmation

  • [x] Reproduced against current main using synthetic inputs and the actual fusion implementation.
  • [x] Checked related open and closed contributions before reporting.

OUAO-FRANK avatar Oct 29 '26 07:10 OUAO-FRANK

I'd like to work on this. Plan: add a deterministic tie-breaker in deduplicateByScore (equal scores ordered by ChunkID) and a Go unit test covering the tied-score case from the report.

Frank-zhu2000 avatar Oct 29 '26 07:10 Frank-zhu2000

I've opened draft #4869 with the fix and regression tests I had prepared locally earlier today. It covers deterministic tied ranks and the resulting numeric scores across vector-only, keyword-only and hybrid fusion. The PR records the passing focused/race/mutation/vet checks and the broader local validation still blocked by the Windows environment. Linking it here so we can coordinate on one patch and avoid duplicating work.

OUAO-FRANK avatar Oct 29 '26 08:10 OUAO-FRANK

Thanks @OUAO-FRANK for the detailed report and for opening #4869 with regression tests. To avoid duplicate work, I'm withdrawing my claim on this issue and will leave the fix to #4869. Happy to help review if useful.

Frank-zhu2000 avatar Oct 29 '26 09:10 Frank-zhu2000