Ranking & Tie-Breaking
Deterministic scoring and Reciprocal Rank Fusion implementation.
Implementation Sourcesrc/core/retrieval/scorer.ts
Reciprocal Rank Fusion (RRF)
Because ContextOS merges signals from distinct algorithms (FTS5 BM25 vs BFS Graph Expansion), their raw scores cannot be directly compared. FTS5 produces unbounded negative floats, while BFS produces exponential decay vectors.
We normalize these signals using Reciprocal Rank Fusion:
const K = 60;
function rrfScore(rank: number): number {
return 1 / (K + rank);
}The Determinism Problem
ContextOS adds an explicit identifier tie-breaker when scores are equal, so output ordering does not depend on incidental input order. This keeps repeated queries stable across multi-turn chats.
ContextOS enforces strict determinism via a stable tie-breaker:
results.sort((a, b) => {
if (Math.abs(a.score - b.score) > 1e-9) {
return b.score - a.score;
}
// Deterministic tie-breaker
return a.id.localeCompare(b.id);
});Complexity Analysis
Time Complexity
O(N log N)
Space Complexity
O(N)
Average Case
< 1ms
Worst Case
< 5ms
Heuristic Boosts
Scores are aggressively modified by deterministic heuristics:
- Layer Promotion: Session, repository, workspace, and global layers use the configured multipliers; the default workspace multiplier is
1.1x. - Intent Detection: Intent signals affect matching and query terms. There is no separate default AST type multiplier.
- File Extensions: Binary and unreadable files are hard-capped at
score = 0.