Skip to content

fix: reindex search after import (fix #938)#971

Open
serhiizghama wants to merge 2 commits into
rohitg00:mainfrom
serhiizghama:fix/import-reindex-search
Open

fix: reindex search after import (fix #938)#971
serhiizghama wants to merge 2 commits into
rohitg00:mainfrom
serhiizghama:fix/import-reindex-search

Conversation

@serhiizghama

@serhiizghama serhiizghama commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Problem

After mem::import, smart-search is wrong until the process restarts (#938):

  • replace strategy wipes KV across every namespace in src/functions/export-import.ts, but never touches the search indices — BM25 and the vector index keep holding the deleted entries, so search returns ghosts of records that no longer exist.
  • Data imported by any strategy is written straight to KV via kv.set(...) without being indexed, so freshly imported memories/observations are invisible to search.

The net effect for a replace-import: search surfaces deleted records and misses everything just imported, for the whole process lifetime.

Solution

Rebuild both indices from KV at the end of a successful import, using the existing rebuildIndex(kv) helper exported from search.ts. It clears BM25 + vector and repopulates them from KV, which is exactly the correct post-import state and fixes both halves (ghosts removed, imported data indexed) for every strategy.

A reindex failure is caught and logged rather than failing the import — the imported data is already durable in KV, so the import itself should still report success.

Testing

  • New regression test in test/export-import.test.ts: after a replace import, a replaced-out memory no longer surfaces from getSearchIndex().search(...), and an imported memory is searchable immediately (no restart).
  • Negative control: reverting the rebuildIndex call makes the new test fail.
  • Full suite green: 1416 tests across 128 files (npm test).

Summary by CodeRabbit

  • Bug Fixes

    • Imported data is now immediately searchable in search results, fixing a previous issue where search indices remained stale after import operations.
  • Tests

    • Added test case verifying that importing data correctly rebuilds search indices, confirming imported content is immediately accessible in search results.

A replace import wipes KV but leaves the BM25/vector indices holding the
deleted entries, and data imported by any strategy is written straight to
KV without being indexed. Until the next process restart, smart-search
returns ghosts of replaced-out records and misses everything just
imported.

Rebuild both indices from KV at the end of a successful import via the
existing rebuildIndex helper, which clears and repopulates them to match
KV exactly. A reindex failure is logged but does not fail the import,
since the imported data is already durable in KV.
Asserts that a replaced-out memory no longer surfaces from search and an
imported memory is searchable immediately, without a process restart.
@vercel

vercel Bot commented Jun 22, 2026

Copy link
Copy Markdown

@serhiizghama is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

After a successful mem::import operation, rebuildIndex is now called to synchronize the live search index with the updated KV state. The reindex step is wrapped in a try/catch so import success is unaffected by indexing failures. A new test verifies that ghost entries disappear and imported data becomes immediately searchable.

Changes

Post-import search reindex

Layer / File(s) Summary
Post-import rebuildIndex in mem::import
src/functions/export-import.ts
Adds rebuildIndex import from ./search.js and inserts a try/catch call to it after a successful import, logging the strategy and indexed count on success or a warning on failure without breaking the import return path.
Test: replace import reindexes search index
test/export-import.test.ts
Adds getSearchIndex and rebuildIndex imports and a new test that primes the index, runs mem::import with "replace" strategy, then asserts the removed memory is no longer searchable and the imported memory is immediately present.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Suggested reviewers

  • rohitg00

Poem

🐇 Hop hop, the index was stale,
Old ghosts would haunt every search trail.
Now after import we rebuild with care,
New memories bloom fresh in the air!
No phantom results, no lingering fright—
The search index shines, crisp and right. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and specifically describes the main change: reindexing the search index after an import operation to fix issue #938.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/functions/export-import.ts (1)

580-585: 🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Remove WHAT-style inline comments in this source block.

This block adds explanatory inline comments that describe behavior directly in code; the project rule asks to avoid that in src/**/*.ts.

♻️ Proposed cleanup
-      // KV is now the source of truth, but the in-memory search indices still
-      // reflect the pre-import state: a "replace" wipe leaves ghosts of deleted
-      // entries in BM25/vector, and freshly imported entries from any strategy
-      // were written straight to KV without being indexed. Rebuild both indices
-      // from KV so smart-search reflects the imported data immediately instead
-      // of only after a process restart.
       try {
         const indexed = await rebuildIndex(kv);
         logger.info("Import reindex complete", { strategy, indexed });
       } catch (err) {
-        // The import itself succeeded and is durable in KV; a reindex failure
-        // must not fail the import. Surface it so the gap is diagnosable.
         logger.warn("Import reindex failed", {
           strategy,
           error: err instanceof Error ? err.message : String(err),
         });
       }

As per coding guidelines, In TypeScript source code, avoid code comments explaining WHAT — use clear naming instead.

Also applies to: 590-592

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/functions/export-import.ts` around lines 580 - 585, Remove the WHAT-style
inline comments in the block at lines 580-585 and 590-592 that explain the
behavior of the search index rebuild logic. According to project guidelines,
avoid code comments that describe WHAT the code does in src/**/*.ts files.
Instead, refactor the code to be self-documenting through clear naming
conventions and logical structure that makes the intent obvious without
requiring explanatory comments. Consider whether the code sections being
commented could benefit from better variable names, helper functions, or
restructuring to eliminate the need for these inline comments altogether.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/functions/export-import.ts`:
- Around line 580-585: Remove the WHAT-style inline comments in the block at
lines 580-585 and 590-592 that explain the behavior of the search index rebuild
logic. According to project guidelines, avoid code comments that describe WHAT
the code does in src/**/*.ts files. Instead, refactor the code to be
self-documenting through clear naming conventions and logical structure that
makes the intent obvious without requiring explanatory comments. Consider
whether the code sections being commented could benefit from better variable
names, helper functions, or restructuring to eliminate the need for these inline
comments altogether.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: edeb6963-d7f5-4647-b655-6dfde917eba5

📥 Commits

Reviewing files that changed from the base of the PR and between f6f9e3c and 5870b7e.

📒 Files selected for processing (2)
  • src/functions/export-import.ts
  • test/export-import.test.ts

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.

1 participant