Skip to content

fix(retrieval): keep a genuine 0.0 rerank score in the pool ordering - #499

Open
gouyu-hou wants to merge 1 commit into
Tencent-Hunyuan:mainfrom
gouyu-hou:fix/rerank-zero-score-sort
Open

gouyu-hou wants to merge 1 commit into
Tencent-Hunyuan:mainfrom
gouyu-hou:fix/rerank-zero-score-sort

Conversation

@gouyu-hou

Copy link
Copy Markdown

Problem

_read_once sorts the post-rerank pool with this inline key:

-float(item.get("_local_rerank_score") or float("-inf"))

_local_rerank_score comes straight from the reranker (float(score) in
rerank_items_with_scores / rerank_with_lexical_survival), so 0.0 is a
perfectly valid score. But 0.0 or float("-inf") evaluates to -inf, which
makes the sort key +inf: a zero-scored item is pushed behind items with
negative scores. When ranked_pool is later consumed by
compete_hierarchy_bundle / truncated to top-K, the strictly worse negative
items are kept and the better zero-scored one is dropped.

This also contradicts a rule already documented in the same file:
_resolve_normal_min_score is explicitly written "without treating zero as
absent".

Reproducing the old inline lambda on three items:

items = [{"node_id": "pos",  "_local_rerank_score": 0.8},
         {"node_id": "zero", "_local_rerank_score": 0.0},
         {"node_id": "neg",  "_local_rerank_score": -0.3}]
# old key -> ['pos', 'neg', 'zero']   <- the 0.0 item is last

Fix

Extract the inline lambda into a module-level _reranked_pool_sort_key that
treats only None as missing:

score = item.get("_local_rerank_score")
return (
    -float(score) if score is not None else float("inf"),
    str(item.get("node_id") or ""),
)

The -inf fallback for genuinely unscored items is preserved. Extracting the
key makes the ordering unit-testable; the lambda was buried inside the large
_read_once method.

Evidence

tests/test_rerank_pool_sort_order.py (3 cases):

  • test_zero_score_ranks_above_negative_scores: 0.0 sorts between 0.8 and
    -0.3. On main the test cannot even import the helper, and the old inline
    lambda produces the wrong order shown above.
  • test_unscored_item_still_sorts_last: an item with no score keeps the
    -inf fallback and stays last, so the fix does not promote unscored items.
  • test_equal_scores_keep_node_id_order: the node-id tie-breaker is unchanged.

With the patch: 3 passed. ruff check passes on both changed files.

Scope

Pure extraction plus the falsy fix. No public API, config, storage, or
dependency changes; ordering is identical for every score except a genuine
0.0 (and the still-last unscored case).

The post-rerank pool was sorted with -(score or -inf). A reranker score of 0.0 is valid but falsy, so it collapsed to -inf and sorted behind negative scores; truncating the pool to top-K then kept strictly worse items. This contradicts the same file's _resolve_normal_min_score rule that zero is not absent.

Extract the inline key into _reranked_pool_sort_key, which treats only None as missing. The helper is testable; ordering and the node-id tie-breaker are otherwise unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant