memorix icon indicating copy to clipboard operation
memorix copied to clipboard

Hybrid search applies the project-affinity multiplier (0.65) on top of RRF scores, demoting a lexical rank-1 exact-title match below rank-30 hits

Open chenshuangci opened this issue 1 month ago • 1 comments

Environment

  • memorix 1.10.2 (npm global), Windows 11, Node v24.15.0
  • Control-plane / HTTP MCP, team tool profile
  • One project with 55.6k observations (> 1e4, persistent lexical + semantic path)
  • Embedding provider: qwen3-embedding:8b (4096d), API mode
  • Observed 2026-11-04, FTS generation 97455–97458

Summary

In --quality balanced (hybrid) search, the RRF fusion score is multiplied by calculateProjectAffinity(). RRF scores are rank-based and span a very narrow interval (≈0.00984 at lexical rank 1 down to ≈0.00621 at rank 30), so the 0.65 multiplier for no_project_reference is equivalent to demoting a rank-1 hit to the equivalent of rank ≈33. A record whose title exactly matches the query ends up below several records that only share a few tokens in narrative — the only difference being that their text mentions the project name.

--quality fast (lexical only) ranks the exact match first with a 11× score lead, which confirms the lexical layer is fine and the demotion happens in the hybrid post-processing.

Steps to reproduce

  1. Use a project with ≥ 1e4 observations (so the persistent hybrid path is used) and at least:
    • A — an observation whose title is an exact match for the query and whose content (title / narrative / facts / concepts / filesModified) does not contain the project name (a generic, cross-project lesson is a natural example);
    • B — several observations that match only some query tokens in narrative but whose text contains the project name.
  2. Run the same query in both profiles:
memorix memory search --query "<exact title of A>" --limit 12 --quality balanced --json
memorix memory search --query "<exact title of A>" --limit 12 --quality fast --json

Expected

A is ranked first in both profiles. Its BM25 is ~2× the runner-up and 10× the records that outrank it in hybrid mode.

Actual

--quality fast (lexical):

1  A   score 39.33   (next result 3.6)

--quality balanced (hybrid):

rank id score matched fields lexical rank content mentions project name
1 B1 0.0082192 narrative 13 yes
2 B2 0.0068182 core, narrative 28 yes
3 B3 0.0067105 narrative 16 yes
4 B4 0.0066667 core, narrative 30 yes
5 A 0.0063934 title, concept, narrative, fact 1 no

Every score above is reproduced exactly by 0.6 / (60 + lexical_rank + 1) × recency × affinity:

  • A: 0.6/61 = 0.0098361, × 0.65 = 0.0063934 (measured 0.0063934426)
  • B1: 0.6/73 = 0.0082192 (affinity 1.0)

Effective rank of A: solve 0.6/(61+j) = 0.6/61 × 0.65 → j = 32.85.

Root cause

  • RRF fusion: dist/index.js:8962-8971 — const RRF_K = 60, lexical 0.6/(RRF_K+index+1), vector 0.4/(RRF_K+index+1).
  • Project affinity: calculateProjectAffinity, dist/index.js:6067; the no_project_reference branch returns 0.65 at dist/index.js:6107.
  • Applied as a multiplier after fusion: dist/index.js:9629 — score: entry.score * affinityScore.
  • Recency is applied the same way (dist/index.js:9517-9535: <1d 1.0, <7d 0.85, <30d 0.6), so the two multipliers compound on an already flat, rank-based score.

Because the whole RRF range is only ≈37% wide, any multiplier step of ~0.1 is worth 6–7 lexical positions.

Suggested fix

Pick any one:

  1. Apply affinity before fusion (weighted RRF) instead of multiplying the fused score.
  2. Keep it as a bounded tie-break — only reorder within a small score window, or clamp the factor to e.g. 0.95–1.05.
  3. Exempt top-N exact/strong lexical matches from the multiplier.

Why the obvious alternative explanations are ruled out

  • Tokenizer / FTS: a direct observations_fts MATCH '<title tokens>' returns row A; --quality fast ranks it first with a 11× lead.
  • Vector arm: A receives no vector hit — its score equals the pure lexical RRF term; a vector hit would add ≥ 0.4/61 = 0.00656 and push it above 0.0131.
  • core / git boosts: the 1.03 / 1.06 boosts are gated on tier === "standard" (dist/index.js:9654) and do not fire for this query (CJK queries classify as heavy).
  • entity boost (1.08): requires an ASCII entityName match; the query tokens are CJK, so no entity is matched.
  • rerank / command-style / surfaced penalties: balanced does not trigger HTTP/LLM rerank (dist/index.js:7110-7114), no surfacedIds were passed, and the measured score equals the RRF × affinity product exactly.

chenshuangci avatar Nov 03 '26 20:11 chenshuangci

Fixed in main via PR #376. Hybrid project affinity is now a bounded tie-break only for candidates within 92% of the top fused score, with a 2% maximum adjustment; rank-1 strong lexical matches are no longer demoted by the 0.65 multiplier. The change is included in tag v1.10.3.

AVIDS2 avatar Nov 05 '26 05:11 AVIDS2