Skip to content

fix(memory): Check vector size before the recall distance cutoff - #2543

Merged
thomasjiangcy merged 1 commit into
mainfrom
fix/memory-recall-distance-size-guard
Nov 2, 2026
Merged

thomasjiangcy merged 1 commit into
mainfrom
fix/memory-recall-distance-size-guard

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #2542, which merged before this fix could be pushed. It addresses Cursor Bugbot's finding on that PR.

Automatic recall puts the cosine distance cutoff in WHERE. That term casts the stored vector to the query's size (embedding::vector(N)), and the cast throws when a row holds a vector of another size. junior_memory_embeddings now holds 1024 and 1536 vectors side by side, and since #2542 the 1536 vectors have no HNSW index to narrow the scan. Postgres does not promise to evaluate dimensions = N before the other WHERE terms, so recall over mixed sizes could fail. For prod, that would happen on a rollback to text-embedding-3-small.

Change: embeddingWithinDistance() wraps the cutoff in CASE WHEN dimensions = N THEN <distance> END <= max. CASE is the documented way to fix evaluation order. ORDER BY keeps the plain cast expression, so 1024 queries can still match the partial HNSW index.

Notes

  • On pgvector pg17 with 3k rows of each size, I couldn't make the unguarded query fail. With seq and index scans disabled, and with the distance term written first, the planner still ran the cheaper dimensions check first. So this makes an ordering guarantee explicit; it doesn't fix a failure I reproduced.
  • EXPLAIN gives the same plan shape with and without the CASE guard.
  • I didn't add a test: the planner already orders these terms safely, so a test couldn't show the difference.

Checks: @sentry/junior-memory typecheck, oxlint, and all 97 tests pass.

--

View Session [Sentry]

Recall puts the cosine distance cutoff in WHERE. That term casts the
stored vector to the query size, and the cast fails on a vector of
another size. Postgres does not promise to check `dimensions` first,
and 1536 vectors now have no HNSW index to filter them, so a scan over
mixed sizes could fail recall. A CASE guard fixes the order.
@vercel

vercel Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Nov 2, 2026 2:31pm UTC

Request Review

@thomasjiangcy
thomasjiangcy merged commit 2c8a644 into main Nov 2, 2026
44 checks passed
@thomasjiangcy
thomasjiangcy deleted the fix/memory-recall-distance-size-guard branch November 2, 2026 14:41

This branch was successfully deployed

1 active deployment
Preview – junior-docs — f2e9b4d6 Deployed Nov 2, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant