Skip to content

fix: preserve retrieval scores on flat rerank#798

Open
mturac wants to merge 2 commits into
rohitg00:mainfrom
mturac:fix/issue-724-rerank-constant-scores
Open

fix: preserve retrieval scores on flat rerank#798
mturac wants to merge 2 commits into
rohitg00:mainfrom
mturac:fix/issue-724-rerank-constant-scores

Conversation

@mturac
Copy link
Copy Markdown

@mturac mturac commented Jun 3, 2026

Summary

  • keep retrieval ordering when the reranker returns the same score for every candidate
  • preserve the original combinedScore and expose discriminative reranker output as rerankScore
  • add coverage for flat and discriminative reranker behavior

Tests

  • git diff --check
  • npm test -- test/reranker.test.ts
  • npm run build

Closes #724

Summary by CodeRabbit

  • New Features

    • Search results now include an optional rerank score for clearer ranking insight.
  • Bug Fixes

    • More robust extraction of reranker scores with automatic fallback to original scores.
    • Preserves original retrieval scores while adding rerank scores.
    • When all reranker scores are identical, results keep original ordering and only mark rerank positions.
  • Tests

    • Updated tests to cover reranker availability, same-score behavior, and discriminative ordering.

@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 3, 2026

@mturac is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 3, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 32913dd8-e6b4-416e-9650-ccf2afe978bd

📥 Commits

Reviewing files that changed from the base of the PR and between 10f637f and b5c253a.

📒 Files selected for processing (1)
  • test/reranker.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/reranker.test.ts

📝 Walkthrough

Walkthrough

Reranker now preserves the retrieval combinedScore and stores discriminative model scores in a new optional rerankScore field. Score extraction is defensive (fallback to combinedScore if output is invalid), constant-score cases skip sorting, and tests mock the transformer pipeline to verify behavior.

Changes

Reranker Result Preservation

Layer / File(s) Summary
Type contract for rerank score
src/types.ts
HybridSearchResult gains optional rerankScore?: number to store model-derived scores separately from the original retrieval combinedScore.
Defensive score extraction and result mapping
src/state/reranker.ts
Reranker extracts scores defensively from model output (fallback to combinedScore if not finite), detects identical scores and skips sorting, and maps results to preserve combinedScore while setting rerankScore and rerankPosition.
Test mocking and assertion updates
test/reranker.test.ts
Test suite mocks @xenova/transformers for deterministic scoring. Tests verify constant-score cases preserve combinedScore and discriminative cases preserve retrieval scores while reordering by rerankScore. Reranker availability test updated to expect true with mocked pipeline.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 Scores once trapped in constant one,
Now flow with care, preserved not undone,
A rerank score blooms, a new field born,
While combinedScore weathers the storm!
Tests mock the logic, sorting knows when to rest,
This reranker, at last, could pass the test!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: preserve retrieval scores on flat rerank' accurately describes the main change: preventing overwriting of combinedScore with constant reranker output and preserving retrieval signals.
Linked Issues check ✅ Passed The PR addresses the core requirements from #724: preserves combinedScore instead of overwriting it, handles flat reranker output by returning top candidates without score-based sorting, and adds comprehensive test coverage for both scenarios.
Out of Scope Changes check ✅ Passed All changes are directly aligned with the linked issue objectives: reranker logic defensively extracts scores with fallback, new rerankScore field tracks reranker output separately, and tests verify both flat and discriminative reranking behavior.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@test/reranker.test.ts`:
- Around line 85-87: The test currently calls isRerankerAvailable() without
setting up the singleton pipeline, so make it self-contained by initializing or
mocking the reranker pipeline before the assertion: update the test to invoke
the same pipeline setup function used by other tests (the singleton pipeline
loader) or explicitly mock the module state, then call isRerankerAvailable() and
assert true; ensure teardown/reset (jest.resetModules or equivalent) so the test
does not rely on ordering.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7296a76f-97f6-4e59-af27-ead6c0cfe8a9

📥 Commits

Reviewing files that changed from the base of the PR and between d442fee and 10f637f.

📒 Files selected for processing (3)
  • src/state/reranker.ts
  • src/types.ts
  • test/reranker.test.ts

Comment thread test/reranker.test.ts Outdated
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.

Reranker is a no-op: text-classification softmax over single-logit cross-encoder always returns 1.0

1 participant