- Notifications
You must be signed in to change notification settings - Fork 0
fix(rag): restore bounded live retrieval reranking#919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
752367b
fix(rag): restore bounded release reranking
BigSimmo 8a99227
fix(rag): keep monitoring on medication subject
BigSimmo 7cf8e55
fix(rag): require explicit monitoring medication
BigSimmo 72bca3a
fix(rag): gate release sorting on current result set
cursoragent 4f10137
style(rag): format release-order regression
BigSimmo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -549,15 +549,21 @@ function recordRetrievalLayer( | ||
| } | ||
| } | ||
| /** Whether the current result set carries bounded second-stage release scores. */ | ||
| function resultsHaveReleaseRankScore(results: SearchResult[]) { | ||
| return results.some((result) => result.score_explanation?.releaseRankScore !== undefined); | ||
| } | ||
| /** | ||
| * Keep distinct results in the current ranked order while upgrading duplicate chunks to the | ||
| * strongest released-hybrid copy. | ||
| * Keep the released result order on the live-eval-proven hybrid and bounded second-stage signals. | ||
| * | ||
| * App-layer rank scores remain available to answer evidence ranking and telemetry, but the | ||
| * live corpus gate has not validated replacing the final distinct-result order with them. | ||
| * live corpus gate has not validated using them as the final retrieval order. Resolve duplicate | ||
| * chunks to their strongest released-hybrid copy before sorting the distinct results. | ||
| */ | ||
| export function stabilizeReleasedSearchOrder(results: SearchResult[]) { | ||
| const compareReleasedSearchStrength = (left: SearchResult, right: SearchResult) => { | ||
| export function stabilizeReleasedSearchOrder(results: SearchResult[], preferSecondStageScore = false) { | ||
| const useSecondStageReleaseOrder = preferSecondStageScore && resultsHaveReleaseRankScore(results); | ||
| const compareReleasedHybridStrength = (left: SearchResult, right: SearchResult) => { | ||
| const leftHybrid = left.hybrid_score ?? left.similarity ?? 0; | ||
| const rightHybrid = right.hybrid_score ?? right.similarity ?? 0; | ||
| if (rightHybrid !== leftHybrid) return rightHybrid - leftHybrid; | ||
| @@ -568,18 +574,30 @@ export function stabilizeReleasedSearchOrder(results: SearchResult[]) { | ||
| return (right.relevance?.score ?? 0) - (left.relevance?.score ?? 0); | ||
| return left.id.localeCompare(right.id); | ||
| }; | ||
| const compareReleasedSearchOrder = (left: SearchResult, right: SearchResult) => { | ||
| if (!useSecondStageReleaseOrder) return compareReleasedHybridStrength(left, right); | ||
| const leftReleaseScore = left.score_explanation?.releaseRankScore ?? left.hybrid_score ?? left.similarity ?? 0; | ||
| const rightReleaseScore = right.score_explanation?.releaseRankScore ?? right.hybrid_score ?? right.similarity ?? 0; | ||
| if (rightReleaseScore !== leftReleaseScore) return rightReleaseScore - leftReleaseScore; | ||
| const leftSimilarity = left.similarity ?? 0; | ||
| const rightSimilarity = right.similarity ?? 0; | ||
| if (rightSimilarity !== leftSimilarity) return rightSimilarity - leftSimilarity; | ||
| if (right.relevance?.score !== left.relevance?.score) | ||
| return (right.relevance?.score ?? 0) - (left.relevance?.score ?? 0); | ||
| return left.id.localeCompare(right.id); | ||
| }; | ||
| const strongestById = new Map<string, SearchResult>(); | ||
| for (const result of results) { | ||
| const current = strongestById.get(result.id); | ||
| if (!current || compareReleasedSearchStrength(result, current) < 0) strongestById.set(result.id, result); | ||
| } | ||
| const deduped: SearchResult[] = []; | ||
| const seen = new Set<string>(); | ||
| for (const result of results) { | ||
| if (seen.has(result.id)) continue; | ||
| seen.add(result.id); | ||
| deduped.push(strongestById.get(result.id) ?? result); | ||
| if (!current || compareReleasedHybridStrength(result, current) < 0) strongestById.set(result.id, result); | ||
| } | ||
| const deduped = [...strongestById.values()] | ||
| .sort(compareReleasedSearchOrder) | ||
| .map((result, index) => | ||
| result.score_explanation | ||
| ? { ...result, score_explanation: { ...result.score_explanation, finalRank: index + 1 } } | ||
| : result, | ||
| ); | ||
| results.length = 0; | ||
| results.push(...deduped); | ||
| return results; | ||
| @@ -601,7 +619,9 @@ function recordSearchScoreTelemetry(telemetry: SearchTelemetry, results: SearchR | ||
| return; | ||
| } | ||
| stabilizeReleasedSearchOrder(results); | ||
| const useSecondStageReleaseOrder = resultsHaveReleaseRankScore(results); | ||
| telemetry.second_stage_rerank_used = useSecondStageReleaseOrder; | ||
| stabilizeReleasedSearchOrder(results, useSecondStageReleaseOrder); | ||
BigSimmo marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const coverageScores = results | ||
| .map((result) => Math.max(0, result.hybrid_score ?? result.similarity ?? 0)) | ||
| .sort((left, right) => right - left); | ||
| @@ -719,6 +739,12 @@ export function applySecondStageRerankIfNeeded(args: { | ||
| const secondStage = secondStageScore(result, args.queryClass, index); | ||
| let rankScore = secondStage.rankScore; | ||
| let confidenceAdjustment = secondStage.adjustment; | ||
| const releasedHybridScore = result.hybrid_score ?? result.similarity ?? 0; | ||
| let releaseRankScore = Math.max( | ||
| releasedHybridScore, | ||
| (result.score_explanation?.finalScore ?? result.hybrid_score ?? result.similarity ?? 0) + | ||
| secondStage.adjustment, | ||
| ); | ||
| const priorOccurrences = seenPerDocument.get(result.document_id) ?? 0; | ||
| seenPerDocument.set(result.document_id, priorOccurrences + 1); | ||
| if (rankingConfig.documentDiversityPenalty > 0 && priorOccurrences > 0) { | ||
| @@ -728,6 +754,16 @@ export function applySecondStageRerankIfNeeded(args: { | ||
| ); | ||
| rankScore -= diversityPenalty; | ||
| confidenceAdjustment -= diversityPenalty; | ||
| releaseRankScore -= diversityPenalty; | ||
| } | ||
| const selectionReasons = result.match_explanation?.reasons ?? []; | ||
| const clinicalSubjectRequired = selectionReasons.includes("retrieval_required_signal:clinical_subject"); | ||
| const clinicalSubjectMatched = selectionReasons.includes("retrieval_signal:clinical_subject"); | ||
| if (clinicalSubjectRequired && !clinicalSubjectMatched) { | ||
| // A wrong-medication chunk can carry attractive numeric dose/monitoring signals. Keep it | ||
| // available at its released hybrid strength, but do not let second-stage evidence boosts | ||
| // promote it above chunks that contain the medication subject requested by the query. | ||
| releaseRankScore = Math.min(releaseRankScore, releasedHybridScore); | ||
| } | ||
| const finalScore = Math.min( | ||
| 1, | ||
| @@ -745,6 +781,7 @@ export function applySecondStageRerankIfNeeded(args: { | ||
| ? { | ||
| ...result.score_explanation, | ||
| rankScore: Number(rankScore.toFixed(4)), | ||
| releaseRankScore: Number(releaseRankScore.toFixed(4)), | ||
| preClampFinalScore: Number(rankScore.toFixed(4)), | ||
| finalScore: Number(finalScore.toFixed(4)), | ||
| } | ||
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
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
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
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.