fix(chat): show normalized confidence in source citations, not raw score#846
Merged
Merged
Conversation
The sources list under a chat answer rendered percentages far above 100% (1808%, 1475%, 1374%), because it multiplied `relevance_score` by 100 as if it were always a 0-1 fraction. It is not. `relevance_score` carries the raw backend score, and the scale depends on which retriever answered: embedding hits are cosine (0-1), but the BM25 fallback is unbounded, so a score of 18.08 rendered as "1808%". `artifacts.tsx` already rendered the same field raw, so the two surfaces disagreed. search_codebase already derives `confidence_score` (raw / max_score, rounded), which is bounded 0-1 whichever retriever produced the hits. Read that instead, and rename the field to `confidence` so a value that is not a raw score stops being called one. The `?? r.score` fallback is dropped rather than kept: that field is unbounded too and would reintroduce the bug whenever `confidence_score` is absent, so the badge now hides instead. Ranking is unaffected: results were already sorted server-side, and confidence is monotonic in the raw score. The same ten sources that read 1808%..949% now read 100%..53%. Adds a regression test. `source-citations.tsx` had no coverage, which is how this shipped; the existing `source-citation.test.tsx` covers the unrelated singular component.
|
✅ Health: 7.6 (unchanged) 📋 At a glance 🔎 More signals (1)🔥 Hotspot touched (1)
📊 Full report · ⭐ Star Repowise · 📥 Install bot · Last updated 2026-07-15 08:07 UTC |
swati510
approved these changes
Jul 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
The sources list under a chat answer rendered percentages far above 100%:
Root cause
source-citations.tsxmultipliedrelevance_scoreby 100 as if it were always a 0-1 fraction:It is not.
relevance_scorecarries the raw backend score, and its scale depends on which retriever answered._retrieve_with_methodtries semantic search and falls back to FTS: embedding hits are cosine (0-1), but the BM25 fallback is unbounded. A score of 18.08 rendered as "1808%".The two chat surfaces already disagreed about this field, which is the tell:
artifacts.tsx:270renders the samerelevance_scoreraw asscore 18.08, whilesource-citations.tsx:181treated it as a fraction.The fix
search_codebasealready derivesconfidence_scorealongside it:That is bounded 0-1 whichever retriever produced the hits, which is exactly what a percentage badge needs. This reads
confidence_scoreinstead, and renames the fieldscore->confidenceso a value that is not a raw score stops being called one.The
?? r.scorefallback is dropped rather than kept: that field is unbounded too, so it would reintroduce the bug wheneverconfidence_scoreis absent. The badge now hides in that case.Only the
search_codebasebranch ofextractSourcesever set this field, so the blast radius is one file. Nothing outsidesource-citations.tsxreads it.Verified
Ranking is unaffected: results are sorted server-side and confidence is monotonic in the raw score. Against a live index, the same ten sources now read:
Each matches
raw / max_scoreexactly (1475/1808 = 82%, 949/1808 = 53%), and all ten badges still render, so nothing dropped.Tests
Adds
__tests__/chat/source-citations.test.tsx. The component had no coverage, which is how this shipped; the existingsource-citation.test.tsxcovers the unrelated singular component. The new test pins a BM25-scale payload (relevance_score: 18.08) and asserts the badge stays within 0-100%.