Skip to content

fix(#894): bounded room search with fallback for complete coverage - #899

Merged
ajianaz merged 1 commit into
developfrom
fix/894-followup-room-first-bounded-search
Aug 5, 2026
Merged

ajianaz merged 1 commit into
developfrom
fix/894-followup-room-first-bounded-search

Conversation

@ajianaz

@ajianaz ajianaz commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

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, every room recall call 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)

fetch_limit = max(room_size * 10, effective_limit * 5)
                .min(total_memories)
                .min(MAX_ROOM_FETCH=5000)
  • room_size * 10: generous over-fetch ratio for post-filtering to room IDs
  • effective_limit * 5: ensures caller gets enough results even in large rooms
  • MAX_ROOM_FETCH = 5000: hard cap prevents unbounded scans on 50K+ DBs

Stage 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

Scenario Old (#897) New (this PR)
2K DB, 20-mem room Fetch 2K Fetch 200 (20×10)
50K DB, 20-mem room Fetch 50K Fetch 5000 (capped)
50K DB, 500-mem room Fetch 50K Fetch 5000 (capped)
Small room missed by hybrid Works (scans all) Works (fallback scores directly)

The fallback stage only runs when needed (room memories missing from primary results) and only processes the missing IDs, not the entire room.

Testing

  • cargo clippy --workspace --all-targets -- -D warnings — clean
  • cargo fmt --all — clean
  • cargo test --workspace — 453 passed, 0 failed, 26 ignored
  • E2E verification with production clone (2,486 memories, 74 rooms):
    • cora-code (20 mems): 5 results (primary path)
    • uteke-dev (small room): 1 result (fallback path)
    • codecora-dev: 2 results (fallback path)
    • uteke-marketing: 5 results (primary path)

Addresses reviewer concern on PR #897 about unbounded fetch_limit.

Ref #894

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.
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

🔍 Cora AI Code Review

⚠️ Review could not complete. Cora was unable to reach the LLM API after retries. Check the workflow logs for errors.


Review powered by cora-code · BYOK · MIT

@ajianaz
ajianaz merged commit 512e3ad into develop Aug 5, 2026
12 checks passed
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]>
@ajianaz
ajianaz deleted the fix/894-followup-room-first-bounded-search branch August 13, 2026 01:52
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.

fix: room recall_semantic top-200 cap kills small rooms in large DBs

1 participant