Repository navigation
fix(memory): Check vector size before the recall distance cutoff - #2543
Merged
Merged
Conversation
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.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
thomasjiangcy
approved these changes
Nov 2, 2026
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_embeddingsnow 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 evaluatedimensions = Nbefore the otherWHEREterms, so recall over mixed sizes could fail. For prod, that would happen on a rollback totext-embedding-3-small.Change:
embeddingWithinDistance()wraps the cutoff inCASE WHEN dimensions = N THEN <distance> END <= max.CASEis the documented way to fix evaluation order.ORDER BYkeeps the plain cast expression, so 1024 queries can still match the partial HNSW index.Notes
dimensionscheck first. So this makes an ordering guarantee explicit; it doesn't fix a failure I reproduced.EXPLAINgives the same plan shape with and without theCASEguard.Checks:
@sentry/junior-memorytypecheck, oxlint, and all 97 tests pass.--
View Session [Sentry]