Skip to content

fix: skip 75ms retry when store is empty (Bug 2)#582

Merged
rwmjhb merged 1 commit intoCortexReach:masterfrom
ether-btc:fix/empty-store-retry-skip
Apr 12, 2026
Merged

fix: skip 75ms retry when store is empty (Bug 2)#582
rwmjhb merged 1 commit intoCortexReach:masterfrom
ether-btc:fix/empty-store-retry-skip

Conversation

@ether-btc
Copy link
Copy Markdown
Contributor

Bug 2: 75ms retry on empty results

Reported in community code audit

Problem

In retrieveWithRetry() (tools.ts), when the first retrieval returns 0 results, it always sleeps 75ms and retries — even when the store is completely empty. This 75ms is pure wasted latency on a cold/empty store.

Fix

Added count() method to MemoryStore using LanceDB's countRows() (O(1) metadata operation). retrieveWithRetry now accepts an optional countStore callback — if the store has 0 rows, the retry is skipped entirely.

// store.ts
async count(): Promise<number> {
  await this.ensureInitialized();
  return await this.table!.countRows();
}

// tools.ts — retrieveWithRetry
if (results.length === 0) {
  if (countStore) {
    const total = await countStore();
    if (total === 0) return results;  // skip retry
  }
  await sleep(75);
  results = await retriever.retrieve(params);
}

Testing

  • TypeScript: 0 new errors on changed files
  • Reviewed by Staff Engineer: APPROVED
  • Backward compatible: countStore is optional, all 5 existing call sites still work

Files changed

  • src/store.ts: +6 lines (count() method)
  • src/tools.ts: +11/-5 lines (optional param + guard + 5 call sites)

Audit reference: memory-lancedb-pro code audit — Bug 2, reported 2026-04-11

Empty store: no write-ahead lag to wait for, so skip the retry
sleep entirely. countRows() is O(1) metadata op — negligible cost.

Logic: if results.length === 0 AND store.count() === 0 → return
empty immediately, no sleep.

Fixes Bug 2 from community code audit.
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