Repository navigation
fix(#894): bounded room search with fallback for complete coverage - #899
Merged
Merged
Conversation
Replace unbounded fetch_limit=total_memories with two-stage algorithm:
Stage 1 (Primary): Hybrid search with adaptive bounded limit:
fetch_limit = max(room_size * 10, effective_limit * 5)
.min(total_memories)
.min(MAX_ROOM_FETCH=5000)
Stage 2 (Fallback): If primary missed room memories (small room in
large DB — the original #894 bug), load missing IDs directly and score
them with cosine similarity against query embedding.
This guarantees complete room coverage without unbounded global fetch.
For a 50K+ DB with a 20-memory room, fetch_limit is capped at 5000
instead of scanning all 50K memories.
Addresses reviewer concern about PR #897 unbounded fetch_limit.
Tested: 4 rooms in production clone (2,486 memories) all return correct
results. cargo test 453 passed, clippy clean.
🔍 Cora AI Code ReviewReview powered by cora-code · BYOK · MIT |
ajianaz
added a commit
that referenced
this pull request
Aug 13, 2026
) Replace unbounded fetch_limit=total_memories with two-stage algorithm: Stage 1 (Primary): Hybrid search with adaptive bounded limit: fetch_limit = max(room_size * 10, effective_limit * 5) .min(total_memories) .min(MAX_ROOM_FETCH=5000) Stage 2 (Fallback): If primary missed room memories (small room in large DB — the original #894 bug), load missing IDs directly and score them with cosine similarity against query embedding. This guarantees complete room coverage without unbounded global fetch. For a 50K+ DB with a 20-memory room, fetch_limit is capped at 5000 instead of scanning all 50K memories. Addresses reviewer concern about PR #897 unbounded fetch_limit. Tested: 4 rooms in production clone (2,486 memories) all return correct results. cargo test 453 passed, clippy clean. Co-authored-by: ajianaz <[email protected]>
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.
What
Replace unbounded
fetch_limit = total_memories(PR #897) with a two-stage bounded algorithm that guarantees complete room coverage without scanning the entire database.Why
PR #897 fixed #894 (small rooms returning zero results) by setting
fetch_limit = total_memories. This works but is unbounded — for a 50K+ memory database, everyroom recallcall fetches, deserializes, and hybrid-scores all 50K memories, even when the caller only wants 5 results.The HNSW ANN lookup is O(log N), but downstream operations (row deserialization, FTS5 search, RRF score merging) all scale with
fetch_limit. A bounded approach is needed.How It Works
Stage 1: Primary (bounded hybrid search)
room_size * 10: generous over-fetch ratio for post-filtering to room IDseffective_limit * 5: ensures caller gets enough results even in large roomsMAX_ROOM_FETCH = 5000: hard cap prevents unbounded scans on 50K+ DBsStage 2: Fallback (direct cosine scoring)
If Stage 1's post-filter returns fewer room memories than the room contains, load the missing room memories by ID (their embeddings are stored in SQLite), embed the query, and compute cosine similarity directly.
This handles the edge case where room memories rank below position 5000 in global search — exactly the #894 scenario, but now bounded.
Performance Characteristics
The fallback stage only runs when needed (room memories missing from primary results) and only processes the missing IDs, not the entire room.
Testing
Addresses reviewer concern on PR #897 about unbounded fetch_limit.
Ref #894