Skip to content

fix(recall): report similarity for memories in unified search, not RRF rank - #831

Merged
ajianaz merged 1 commit into
codecoradev:developfrom
themiguelamador:fix/unified-recall-reports-rrf-rank-as-score
Aug 3, 2026
Merged

ajianaz merged 1 commit into
codecoradev:developfrom
themiguelamador:fix/unified-recall-reports-rrf-rank-as-score

Conversation

@themiguelamador

@themiguelamador themiguelamador commented Aug 2, 2026 •

Copy link
Copy Markdown
Contributor

The bug

recall_unified_all merges memory and document hits with RRF, then reports rrf_sum / (1/(k+1)) as the score. When a result appears in only one of the two lists — every result in any store without documents — that sum is 1/(k+1+rank), so the reported score collapses to (k+1)/(k+1+rank): a pure function of rank, with the cosine similarity discarded.

Every query returns the same score sequence regardless of what it matched, or whether it matched anything:

$ uteke recall "purple giraffe tax law sonata"
  1. (score: 1.000) <arbitrary memory>
  2. (score: 0.984) <arbitrary memory>
  3. (score: 0.968) <arbitrary memory>

Those are exactly 61/61, 61/62, 61/63.

Two consequences

1. min_score cannot filter. It's applied to the rank number, so at the documented 0.30 default a result must rank ~140th to be excluded. This silently defeats the configurable similarity threshold from #252 — the "I don't know" behavior — for every consumer on the unified path, which is the default for the CLI (commands/recall.rs:113), the MCP server (uteke-mcp/src/lib.rs:765), and the HTTP server (handlers.rs:292).

2. No way to distinguish a good match from a bad one. Recall always returns limit results at maximum apparent confidence. An agent consuming uteke_recall gets no signal that nothing relevant was found — the nonsense query above looks identical to a perfect hit.

The fix

Keep RRF for ordering; report the memory's real cosine for scoring. Result order is unchanged; only the reported number differs.

Measured over 67 real memories after the fix:

query top score
git branch naming (direct hit) 0.614
my safety copy vanished after rewriting history (pure synonym match) 0.477
purple giraffe tax law sonata (nonsense) 0.379

With min_score = 0.45 the nonsense query correctly returns No matching results found.

Scope: memories only

Documents deliberately keep the normalized RRF. DocumentSearchResult::score means three different things depending on mode — cosine from doc_search_semantic, 1/(i+1) rank from doc_search_fts, an RRF sum from doc_search_hybrid — and an FTS-only hit has no similarity to report at all. Putting documents on the same scale means unifying that first, which is a larger change than this one. I'd rather not smuggle it in here; happy to follow up if you want it.

This does leave memory and document scores on different scales within one result set. That's pre-existing, not introduced here — but worth being explicit about.

Compatibility

This lowers reported memory scores from ~1.0 to real similarity, so any config with a min_score tuned against the old numbers becomes meaningfully stricter. Worth a release-note line.

The 0.30 default sits below the noise floor measured here (~0.38); 0.45 fit that corpus better. The right default depends on the embedding model, so I've left it unchanged rather than guess for other setups.

Test

Adds test_unified_recall_reports_similarity_not_rrf_rank, asserting a matching query outscores an unrelated one and that the top score isn't the RRF constant. It fails on develop with top score 1 is the normalized RRF constant, not a similarity, and passes here.

One note that may be useful beyond this PR: the test uses a tempfile store rather than :memory:. The usearch index path is derived from the SQLite path, so Uteke::open(":memory:") gets no vector index and every recall returns empty. The existing #[ignore]d recall tests around it (test_recall_threshold_filters_low_scores, test_recall_threshold_zero_returns_all, test_recall_threshold_specific_score) are written that way and cannot pass as written — their assert!(!results.is_empty()) will always fail. I've left them alone here.

cargo fmt --check and cargo clippy --workspace --all-targets -- -D warnings both clean.

@themiguelamador
themiguelamador force-pushed the fix/unified-recall-reports-rrf-rank-as-score branch from 1b35228 to 24255aa Compare August 2, 2026 23:55
@themiguelamador themiguelamador changed the title fix(recall): report similarity in unified search, not normalized RRF rank fix(recall): report similarity for memories in unified search, not RRF rank Aug 2, 2026
…F rank

`recall_unified_all` merges memory and document hits with RRF, then reports
`rrf_sum / (1/(k+1))` as the result score. When a result appears in only one of
the two lists — every result in any store without documents — that sum is
`1/(k+1+rank)`, so the reported score collapses to `(k+1)/(k+1+rank)`: a pure
function of rank, with the cosine similarity discarded.

Every query therefore returns the identical score sequence 1.000, 0.9839,
0.9683, 0.9531 … regardless of what it matched, or whether it matched anything:

    uteke recall "purple giraffe tax law sonata"
      1. (score: 1.000) <arbitrary memory>
      2. (score: 0.984) <arbitrary memory>

Two consequences:

1. `min_score` cannot filter. It is applied to the rank number, so at the
   documented 0.30 default a result must rank ~140th to be excluded. This
   silently defeats the configurable similarity threshold added in codecoradev#252 — the
   "I don't know" behavior — for every consumer on the unified path, which is
   the default path for the CLI, the MCP server, and the HTTP server.
2. There is no way to tell a good match from a bad one. Recall always returns
   `limit` results at maximum apparent confidence, so an agent consuming
   `uteke_recall` has no signal that nothing relevant was found.

Fix: keep RRF for ordering, report the memory's real cosine for scoring.
Ordering is unchanged; only the reported number differs.

Documents deliberately keep the normalized RRF. `DocumentSearchResult::score`
means three different things by mode — cosine from `doc_search_semantic`,
`1/(i+1)` rank from `doc_search_fts`, an RRF sum from `doc_search_hybrid` — and
an FTS-only hit has no similarity to report at all. Putting documents on the
same scale requires unifying that first, which is a larger change than this one;
happy to follow up if you want it.

Measured over 67 real memories after the fix: unrelated queries top out at
~0.38, genuine matches land at 0.48–0.62, and `min_score = 0.45` correctly
returns "No matching results found." for the nonsense query above.

Note this lowers reported memory scores from ~1.0 to real similarity, so any
config carrying a `min_score` tuned against the old numbers becomes meaningfully
stricter. The 0.30 default is below the noise floor measured here; 0.45 was a
better fit on that corpus, but the right default depends on the embedding model
and is left unchanged in this PR.

Adds a regression test asserting a matching query outscores an unrelated one and
that the top score is not the RRF constant. It fails on develop with
"top score 1 is the normalized RRF constant, not a similarity" and passes here.

The test uses a `tempfile` store rather than `:memory:`. The usearch index path
is derived from the SQLite path, so `Uteke::open(":memory:")` has no vector index
and every recall returns empty — worth knowing, since the existing `#[ignore]`d
recall tests around it are written that way and cannot pass as written.
@themiguelamador
themiguelamador force-pushed the fix/unified-recall-reports-rrf-rank-as-score branch from 24255aa to 66cfcbf Compare August 3, 2026 00:13

@ajianaz ajianaz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. RRF rank was leaking into reported similarity score — this correctly surfaces the real cosine similarity for memory results while keeping normalized RRF for documents (which have mixed score semantics). Good test coverage with the ignored integration test.

@ajianaz
ajianaz merged commit e4e2a5a into codecoradev:develop Aug 3, 2026
9 checks passed
ajianaz added a commit that referenced this pull request Aug 5, 2026
- v0.12.0: add @KazamiHazaki (Windows installer #779) + @dependabot
- v0.11.0: add @themiguelamador (#830 #831) + @tomatotomata (#829)
- v0.12.0: add install.ps1 to Added section

Co-authored-by: ajianaz <[email protected]>
ajianaz added a commit that referenced this pull request Aug 13, 2026
- v0.12.0: add @KazamiHazaki (Windows installer #779) + @dependabot
- v0.11.0: add @themiguelamador (#830 #831) + @tomatotomata (#829)
- v0.12.0: add install.ps1 to Added section

Co-authored-by: ajianaz <[email protected]>
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.

2 participants