Skip to content

fix: ground on a cosine margin and the matched document pool - #65

Merged
hmbanan666 merged 1 commit into
mainfrom
cursor/grounding-margin
Nov 15, 2026
Merged

hmbanan666 merged 1 commit into
mainfrom
cursor/grounding-margin

Conversation

@hmbanan666

Copy link
Copy Markdown
Owner

Project-scoped grounding counted document frequency on sources only, and absolute E5 cosines overlap. This uses the same pool search matches, keeps project identity terms rare, and scores embeddings by the margin over a cached centroid.

Document frequency

documentFrequency now counts the records that search can return. A project pool is that project's current sources plus its journals, decisions, facts, and other knowledge. An area pool leaves journals, decisions, and facts out. A document filter still counts only current documents. A term that occurs only in a distilled item is no longer df 0. The project's id, name, and glossary terms are never common. Synonyms are not protected.

Margin

With embeddings, strong / weak / none uses the evidence cosine minus the cosine of the query to a cached centroid of the passage vectors in that area, or in that project. The centroid is recomputed when the chunk count or max rowid changes. explain prints background and margin. explain --file already accepted a per-question project; the summary now adds margin distributions, a margin cut, and a combined rare-share and margin rule. Lexical-only grounding is unchanged.

Defaults, from the public fixture with Xenova/multilingual-e5-small (not from the 2374-source set):

  • CONTEXT_GROUNDING_MARGIN_WEAK=0.03
  • CONTEXT_GROUNDING_MARGIN_STRONG=0.08

Curated on-topic margin was 0.014–0.081 (median 0.032) and off-topic margin was about -0.81 to -0.75, because those neighbours stayed under the 0.80 retrieval floor. On the mixed corpus, off-topic cosine reached 0.928 while off-topic margin reached only 0.109 (median 0.069). Calibrate both variables with context grounding explain --file on the database you search. Not deployed.

Evidence

  • pnpm run check: lint, typecheck, 269 tests passed
  • pnpm run eval:grounding: curated lexical 10/10 and 10/10 none; curated semantic 10/10 and 10/10 none; mixed lexical 10/10 and 10/10 not-strong (none 9, weak 1, strong 0); mixed semantic 10/10 and 10/10 not-strong (none 2, weak 8, strong 0)
  • pnpm run eval:projects: context recall 1 pack-hit 1; expi recall 1 pack-hit 1
  • pnpm run eval:retrieval: completed, lexical only (semantic: unavailable because that fixture database has no vectors). Isolation violations 0

Friction

  1. Nothing waited for minutes. The stale snapshot (bddbfb4 vs de464de) was a fetch, not a rebuild. The first semantic grounding pass reported one mixed on-topic cosine of 0 (margin -0.801); the rerun of the same command put every on-topic cosine at 0.853–0.906 (margin min 0.067). That miss did not reproduce, so the cause is unknown.
  2. node is /exec-daemon/node v22 until PATH puts nvm 24.22.1 first. scripts/download-search-model.ts imports ../src/embedding-model.js, so it cannot run until tsc. The pinned files were fetched with curl instead. The ONNX file is 118,308,185 bytes, matching the documented q8 build.
  3. eval:retrieval prints semantic: unavailable when models/Xenova/multilingual-e5-small is on disk, because src/eval-search.ts loads the model only if the eval database already has vectors and it never reindexes. scripts/download-search-model.ts has no pnpm script and fails on the TypeScript source.
  4. A pnpm download script that works before tsc, plus an eval:retrieval line that says "fixture database has no vectors" when the model is present, would remove the two detours. Putting Node 24.22.1 on PATH in the environment would remove the third.
Open in Web Open in Cursor 

Project search counted document frequency on sources only, so a term that
matched a journal or distilled item showed df 0. Project ids, names, and
glossary terms are no longer common. With embeddings, strong and weak use
the margin over a cached area or project centroid instead of an absolute
cosine. Lexical-only grounding is unchanged.

Co-authored-by: Nick Kosarev <nick@kosarev.space>
@hmbanan666
hmbanan666 merged commit 0cc684b into main Nov 15, 2026
2 checks passed
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