Skip to content

perf(memory): keep the BM25 order of code recall candidates - #470

Merged
mrnasil merged 1 commit into
mainfrom
perf/code-recall-precision
Nov 25, 2026
Merged

mrnasil merged 1 commit into
mainfrom
perf/code-recall-precision

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 25, 2026

Copy link
Copy Markdown
Member

Problem

Turn recall injects up to 4 code chunks into each user message. Code search took FTS5's candidates (already BM25-ranked) and re-sorted them by the hash-embedding cosine. That cosine is a bag of hashed words, so common words ("where", "does", "how", "file") dominate it. For "where does edit matching live" it injected tui/app.rs, config/merge.rs, debug_tests.rs and glob.rs, all scoring 0.39–0.41. Off-topic and on-topic chunks share the same score band, so no code_min_score separates them.

Fix

  • When FTS ranked the page, keep its BM25 order. The cosine still sets the floor (code_min_score) and the shown score.
  • When FTS did not rank it (no indexable word → browse, or no index → scan), sort by cosine as before. FtsPage reports which case it is in the new ranked field.
  • Memory and session search are unchanged.

Measured

scripts/eval_code_recall.py (new): 22 questions about this repo, each with the paths that answer it; top 4, floor 0.22, release build.

injected chunks on topic questions with a relevant chunk
main (cosine re-rank) 25/88 (28%) 13/22
this PR (BM25 order) 41/88 (47%) 19/22

Token use is unchanged (same number of chunks; bench_tokens.py --workload dev 248k → 251k, within recall noise). This is a recall-quality change.

The cases were written knowing the repo, so read the table as a comparison of two rankings on one fixed set. Next step, measured offline on the same set: index the path too (FTS column), 47% → ~52–57% depending on weight. That needs a migration, so it gets its own PR.

Checks

  • cargo test -p whycodes-memory --lib (90). The new ranked_hits_keep_bm25_order_and_a_browse_sorts_by_score stores vectors that disagree with the text, and it fails on the old re-rank (checked).
  • cargo test -p whycodes-storage --lib (45, ranked asserted for match / browse / scan)
  • clippy (memory, storage), fmt, cargo check --workspace --tests, ratchets

🤖 Generated with Claude Code

Code search took FTS5's BM25-ranked candidates and re-sorted them by the
hash-embedding cosine. Common words ("where", "how", "file") dominate
that cosine, so turn recall injected off-topic chunks: on 22 questions
about this repo only 28% of injected chunks were on topic and 9
questions got no relevant chunk at all. Keeping the BM25 order raises
that to 47% and 19/22 answered. The cosine still sets the floor and the
shown score, and still orders a page FTS did not rank (a browse or a
scan without the index), which FtsPage now reports as `ranked`.

scripts/eval_code_recall.py reproduces the measurement.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil merged commit 76f15aa into main Nov 25, 2026
7 checks passed
@mrnasil
mrnasil deleted the perf/code-recall-precision branch November 25, 2026 02:45
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