Skip to content

fix: eliminate stale FTS results after consolidation#45

Merged
pszymkowiak merged 1 commit intomainfrom
fix/stale-fts-after-consolidation
Mar 20, 2026
Merged

fix: eliminate stale FTS results after consolidation#45
pszymkowiak merged 1 commit intomainfrom
fix/stale-fts-after-consolidation

Conversation

@pszymkowiak
Copy link
Contributor

Summary

Fixes #44 — after icm consolidate, icm recall could return both the consolidated memory and stale fragments from the originals.

Root cause: The FTS5 external content table (content='memories') could accumulate ghost entries in its inverted index because:

  1. The memories_au UPDATE trigger fired on every update (including update_access and apply_decay), needlessly churning the FTS index on non-indexed columns
  2. No FTS rebuild was performed after the bulk delete+insert in consolidate_topic()

Fix (3 parts):

  • Rebuild FTS after consolidationINSERT INTO memories_fts(memories_fts) VALUES('rebuild') inside the consolidate_topic() transaction guarantees index consistency
  • Scope UPDATE trigger — changed from AFTER UPDATE ON memories to AFTER UPDATE OF topic, summary, keywords ON memories so update_access/apply_decay no longer touch the FTS index
  • Auto-migration — existing databases get the trigger replaced and FTS rebuilt on first open

Test plan

  • New test test_consolidate_no_stale_fts_results reproducing the exact issue scenario (store 3, consolidate, verify recall returns only consolidated)
  • All 82 icm-store tests pass
  • All 53 icm-mcp tests pass
  • cargo fmt clean

🤖 Generated with Claude Code

- Rebuild FTS index inside consolidate_topic() transaction to purge ghost entries
- Scope memories_au trigger to UPDATE OF topic, summary, keywords only, preventing
  unnecessary FTS churn from update_access/apply_decay
- Add migration to fix trigger on existing databases and rebuild FTS index
- Add test reproducing the exact issue scenario
@pszymkowiak pszymkowiak added bug Something isn't working database search labels Mar 20, 2026
@pszymkowiak
Copy link
Contributor Author

pszymkowiak commented Mar 20, 2026

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes stale FTS5 search results after memory consolidation by scoping the SQLite UPDATE trigger to only indexed columns (topic, summary, keywords) and rebuilding the FTS index after bulk delete+insert operations. Includes an auto-migration path for existing databases.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #44


Analyzed automatically by wshm · This is an automated analysis, not a human review.

@pszymkowiak pszymkowiak merged commit be06151 into main Mar 20, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working database search

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory search returns stale results after consolidation

1 participant