Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(store): make the FTS drift counters able to detect drift

adrianogomes-NE9b9d29520c87
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 65490de4..489fec5a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Fixed
+- The `pages_fts_rows` and `observations_fts_rows` status counters now count
+ indexed documents instead of content-table rows. Both FTS tables use external
+ content, so `SELECT COUNT(*)` against them was answered from `pages` /
+ `observations` and the `fts: N/M` health pair could never diverge, no matter
+ how far the index had drifted.
+
## [1.26.0] - 2026-08-12
### Added
diff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rs
index 41d0d664..c3fa98c5 100644
--- a/crates/ai-memory-store/src/reader.rs
+++ b/crates/ai-memory-store/src/reader.rs
@@ -5850,9 +5850,17 @@ impl ReaderPool {
Ok(DerivedIndexStatus {
pages_rows: count(conn, "SELECT COUNT(*) FROM pages")?,
- pages_fts_rows: count(conn, "SELECT COUNT(*) FROM pages_fts")?,
+ // `pages_fts` is an external-content FTS5 table, so
+ // `COUNT(*)` on it is answered from `pages` and can never
+ // diverge. The `_docsize` shadow table holds one row per
+ // indexed document, which is what makes this pair a drift
+ // check rather than a tautology. Same for `observations_fts`.
+ pages_fts_rows: count(conn, "SELECT COUNT(*) FROM pages_fts_docsize")?,
observations_rows: count(conn, "SELECT COUNT(*) FROM observations")?,
- observations_fts_rows: count(conn, "SELECT COUNT(*) FROM observations_fts")?,
+ observations_fts_rows: count(
+ conn,
+ "SELECT COUNT(*) FROM observations_fts_docsize",
+ )?,
latest_pages_missing_embeddings: count(
conn,
"SELECT COUNT(*) \
diff --git a/crates/ai-memory-store/tests/fts_drift_status.rs b/crates/ai-memory-store/tests/fts_drift_status.rs
new file mode 100644
index 00000000..f326277c
--- /dev/null
+++ b/crates/ai-memory-store/tests/fts_drift_status.rs
@@ -0,0 +1,78 @@
+//! `derived_index_status` must be able to report FTS drift.
+//!
+//! `pages_fts` and `observations_fts` are external-content FTS5 tables
+//! (`content='pages'` / `content='observations'`), so `SELECT COUNT(*)`
+//! against them is answered from the content table and can never diverge from
+//! it. Counting the `_docsize` shadow table is what turns the `fts: N/M`
+//! status pair into a drift check instead of a tautology.
+
+use ai_memory_core::{NewPage, PagePath, Tier};
+use ai_memory_store::Store;
+
+#[tokio::test]
+async fn page_fts_rows_report_index_drift() {
+ let tmp = tempfile::tempdir().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let ws = store
+ .writer
+ .get_or_create_workspace("default".to_string())
+ .await
+ .unwrap();
+ let proj = store
+ .writer
+ .get_or_create_project(ws, "app".to_string(), None)
+ .await
+ .unwrap();
+ store
+ .writer
+ .upsert_page(NewPage {
+ workspace_id: ws,
+ project_id: proj,
+ path: PagePath::new("notes/one.md").unwrap(),
+ title: "one".into(),
+ body: "alpha zebra".into(),
+ tier: Tier::Semantic,
+ frontmatter_json: serde_json::json!({}),
+ pinned: false,
+ links: Vec::new(),
+ author_id: None,
+ expires_at: None,
+ entities: Vec::new(),
+ })
+ .await
+ .unwrap();
+
+ let before = store.reader.derived_index_status().await.unwrap();
+ assert_eq!(before.pages_rows, 1);
+ assert_eq!(
+ before.pages_fts_rows, 1,
+ "a freshly written page is indexed",
+ );
+
+ // Drop the page's entry from the FTS index while leaving the row in
+ // `pages`: exactly the drift this status pair exists to surface.
+ {
+ let conn = rusqlite::Connection::open(store.db_path()).unwrap();
+ conn.execute(
+ "INSERT INTO pages_fts(pages_fts, rowid, title, body, path_search) \
+ SELECT 'delete', rowid, title, body, path_search FROM pages",
+ [],
+ )
+ .unwrap();
+ let hits: i64 = conn
+ .query_row(
+ "SELECT COUNT(*) FROM pages_fts WHERE pages_fts MATCH 'alpha'",
+ [],
+ |row| row.get(0),
+ )
+ .unwrap();
+ assert_eq!(hits, 0, "the entry really left the index");
+ }
+
+ let after = store.reader.derived_index_status().await.unwrap();
+ assert_eq!(after.pages_rows, 1, "the source row is untouched");
+ assert_eq!(
+ after.pages_fts_rows, 0,
+ "the status pair must surface the drift, not mirror the content table",
+ );
+}