Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(status): count a page with only a foreign-model vector as missing
Djalma Júnior08ff0a2c8de8
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex da1fca12..551e61eb 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -114,6 +114,10 @@ version in [`docs/changelog/`](docs/changelog/):page, aligning belief input aggregation with the lint's resolved/stale split.([#1386](https://github.com/akitaonrails/ai-memory/pull/1386))- Kept the previous backup or OKF export intact when an archive download is interrupted, and publish completed downloads atomically. Successful downloads replace destination symlinks with private files rather than changing their targets or inheriting their permissions. ([#1381](https://github.com/akitaonrails/ai-memory/pull/1381))+- Counted a latest page whose only stored vector belongs to another provider,+ model, or dimension as missing an embedding in `ai-memory status` and the+ web `/health` page, so the count no longer reads zero after an+ embedding-model switch.## [3.1.1] - 2026-10-17diff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rsindex e74963bc..fdd035a7 100644--- a/crates/ai-memory-mcp/src/admin.rs+++ b/crates/ai-memory-mcp/src/admin.rs@@ -1309,8 +1309,16 @@ async fn handle_list_projects(State(state): State<Arc<AdminState>>) -> impl Into}async fn handle_status(State(state): State<Arc<AdminState>>) -> impl IntoResponse {+ // Count "missing" against the configured embedder's stored triple, so+ // a model swap cannot read as a fully covered index; with no embedder+ // configured the historical "no vector at all" count is kept.+ let configured_embedding = state.embedder.as_deref().map(stored_embedding_identity);match state.reader.status_counts().await {- Ok(counts) => match state.reader.derived_index_status().await {+ Ok(counts) => match state+ .reader+ .derived_index_status(configured_embedding)+ .await+ {Ok(derived) => {// Three pragma reads; a failure here must not take down the// whole status response, which is also the health probe.@@ -4096,6 +4104,21 @@ async fn embed_project_pages(.map_err(|e| internal_err(e.to_string()))}+/// The `(provider, model, dim)` triple rows are stored under for one+/// embedder: `model_identity()` (which folds in a configured document+/// prefix), never the bare wire model name. `handle_embed`'s stale-row+/// purge and `handle_status`'s missing-vector count must both match rows+/// the way they are written, so they share this instead of each spelling+/// the triple (a vector counts as covering a page only when this very+/// identity would store one for it).+fn stored_embedding_identity(embedder: &dyn Embedder) -> (String, String, u32) {+ (+ embedder.provider().to_string(),+ embedder.model_identity(),+ embedder.dim(),+ )+}+async fn handle_embed(State(state): State<Arc<AdminState>>,Json(req): Json<EmbedRequest>,@@ -4112,12 +4135,7 @@ async fn handle_embed(}};- let provider = embedder.provider().to_string();- // Not `.model()`: the purge below must match the identity rows were- // actually stored under (a document-prefix change), not the wire- // model name. See `Embedder::model_identity`.- let model = embedder.model_identity();- let dim = embedder.dim();+ let (provider, model, dim) = stored_embedding_identity(&*embedder);let mut totals = EmbedBackfillCounts::default();diff --git a/crates/ai-memory-mcp/tests/suite/admin_status_search.rs b/crates/ai-memory-mcp/tests/suite/admin_status_search.rsindex 3ef12b85..f742a07e 100644--- a/crates/ai-memory-mcp/tests/suite/admin_status_search.rs+++ b/crates/ai-memory-mcp/tests/suite/admin_status_search.rs@@ -6,9 +6,10 @@use ai_memory_core::repository_identity::{IdentitySource, IdentityStyle, RepositoryIdentity};use ai_memory_core::{ActorContext, AuthLevel, NewPage, NewUser, PagePath, Tier};+use ai_memory_llm::SyntheticEmbedder;use ai_memory_mcp::{AdminState, admin_router};use ai_memory_store::{AccessMode, DecayParams, GrantLevel, Store};-use ai_memory_wiki::Wiki;+use ai_memory_wiki::{Wiki, WritePageRequest};use axum::body::Body;use axum::http::{Request, StatusCode};use tempfile::TempDir;@@ -156,6 +157,103 @@ async fn status_returns_counts_and_paths() {);}+/// After an embedding-model switch, `/admin/status` counts a page whose+/// only vector was stored under the old triple as pending — status must+/// match the embedder's stored identity the way `handle_embed` writes it —+/// and reports it covered again once a vector for the configured triple+/// exists.+#[tokio::test]+async fn status_counts_missing_embeddings_against_configured_triple() {+ let tmp = TempDir::new().unwrap();+ let (mut state, store) = make_admin_state(&tmp).await;+ let ws = store+ .writer+ .get_or_create_workspace("default".to_string())+ .await+ .unwrap();+ let proj = store+ .writer+ .get_or_create_project(ws, "scratch".to_string(), None)+ .await+ .unwrap();+ // One page, written while the OLD model (8-dim synthetic) was+ // configured: its only vector is stale once the model switches.+ let old_model_wiki = Wiki::new(tmp.path(), store.writer.clone())+ .unwrap()+ .with_embedder(std::sync::Arc::new(SyntheticEmbedder::new(8)));+ old_model_wiki+ .write_page(WritePageRequest {+ workspace_id: ws,+ project_id: proj,+ path: PagePath::new("notes/old-model.md").unwrap(),+ frontmatter: serde_json::json!({ "title": "Old model page" }),+ body: "embedded under the old model".to_string(),+ tier: Tier::Semantic,+ pinned: false,+ title: Some("Old model page".into()),+ admission_ctx: None,+ author_id: None,+ actor: ActorContext::anonymous(),+ evidence: Vec::new(),+ })+ .await+ .unwrap();+ // The server now runs the NEW model (16-dim synthetic): the same+ // provider/model identity, a different dimension.+ state.embedder = Some(std::sync::Arc::new(SyntheticEmbedder::new(16)));++ let missing_count = |state: AdminState| async move {+ let app = admin_router(state);+ let resp = app+ .oneshot(+ Request::builder()+ .method("GET")+ .uri("/admin/status")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();+ assert_eq!(resp.status(), StatusCode::OK);+ let body: serde_json::Value = serde_json::from_slice(&body_bytes(resp).await).unwrap();+ body["derived"]["latest_pages_missing_embeddings"]+ .as_u64()+ .unwrap()+ };++ assert_eq!(+ missing_count(state.clone()).await,+ 1,+ "a page whose only vector is from the old triple counts as pending"+ );++ // Legitimate control: re-embedded under the configured model, the+ // page is covered again.+ old_model_wiki+ .with_embedder(std::sync::Arc::new(SyntheticEmbedder::new(16)))+ .write_page(WritePageRequest {+ workspace_id: ws,+ project_id: proj,+ path: PagePath::new("notes/old-model.md").unwrap(),+ frontmatter: serde_json::json!({ "title": "Old model page" }),+ body: "re-embedded under the new model".to_string(),+ tier: Tier::Semantic,+ pinned: false,+ title: Some("Old model page".into()),+ admission_ctx: None,+ author_id: None,+ actor: ActorContext::anonymous(),+ evidence: Vec::new(),+ })+ .await+ .unwrap();+ assert_eq!(+ missing_count(state).await,+ 0,+ "a vector under the configured triple covers the page"+ );+}+#[tokio::test]async fn list_projects_returns_workspace_project_pairs() {let tmp = TempDir::new().unwrap();diff --git a/crates/ai-memory-mcp/tests/suite/web_search_parity.rs b/crates/ai-memory-mcp/tests/suite/web_search_parity.rsindex b5226433..cc0022da 100644--- a/crates/ai-memory-mcp/tests/suite/web_search_parity.rs+++ b/crates/ai-memory-mcp/tests/suite/web_search_parity.rs@@ -148,7 +148,7 @@ async fn seed() -> Seeded {.await.unwrap();}- let status = store.reader.derived_index_status().await.unwrap();+ let status = store.reader.derived_index_status(None).await.unwrap();assert_eq!(status.latest_pages_missing_embeddings, 0,"every seeded page must carry a vector"diff --git a/crates/ai-memory-store/src/lib.rs b/crates/ai-memory-store/src/lib.rsindex 4c1a4210..304a61c9 100644--- a/crates/ai-memory-store/src/lib.rs+++ b/crates/ai-memory-store/src/lib.rs@@ -8742,11 +8742,113 @@ mod tests {.upsert_page(sample_page(ws, proj, "notes/empty.md", " ")).await.unwrap();- let derived = store.reader.derived_index_status().await.unwrap();+ let derived = store.reader.derived_index_status(None).await.unwrap();assert_eq!(derived.latest_pages_missing_embeddings, 1, "{derived:?}");assert_eq!(derived.latest_pages_unembeddable, 1, "{derived:?}");}+ /// A model swap must not read as a covered index: with the configured+ /// `(provider, model, dim)` triple, a latest page whose only vector is+ /// from another triple counts as missing, while the historical `None`+ /// count (no vector at all) is unchanged.+ #[tokio::test]+ async fn missing_embeddings_counted_against_configured_triple() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let proj = store+ .writer+ .get_or_create_project(ws, "s", None)+ .await+ .unwrap();+ store+ .writer+ .upsert_page(sample_page(ws, proj, "notes/stale.md", "body a"))+ .await+ .unwrap();++ let db = rusqlite::Connection::open(tmp.path().join("db").join("memory.sqlite")).unwrap();+ let insert_embedding = |path: &str, provider: &str, model: &str, dim: u32| {+ let page_id: Vec<u8> = db+ .query_row(+ "SELECT id FROM pages WHERE path = ?1 AND is_latest = 1",+ [path],+ |r| r.get(0),+ )+ .unwrap();+ db.execute(+ "INSERT INTO page_embeddings (page_id, vector, provider, model, dim, created_at) \+ VALUES (?1, ?2, ?3, ?4, ?5, 0) \+ ON CONFLICT(page_id) DO UPDATE SET \+ vector = excluded.vector, provider = excluded.provider, \+ model = excluded.model, dim = excluded.dim, \+ created_at = excluded.created_at",+ rusqlite::params![+ page_id,+ crate::f32_vec_to_bytes(&vec![0.5f32; dim as usize]),+ provider,+ model,+ dim,+ ],+ )+ .unwrap();+ };+ // One latest page whose only vector is triple A.+ let triple_a = ("prov-a".to_string(), "model-a".to_string(), 8);+ let triple_b = ("prov-b".to_string(), "model-b".to_string(), 16);+ insert_embedding("notes/stale.md", "prov-a", "model-a", 8);++ // Historical behaviour: the page carries a vector, so nothing is+ // missing.+ let derived = store.reader.derived_index_status(None).await.unwrap();+ assert_eq!(derived.latest_pages_missing_embeddings, 0, "{derived:?}");++ // Configured triple A: the page is covered by its own vector.+ let derived = store+ .reader+ .derived_index_status(Some(triple_a.clone()))+ .await+ .unwrap();+ assert_eq!(derived.latest_pages_missing_embeddings, 0, "{derived:?}");++ // Configured triple B: the same page now counts as missing — its+ // only vector is stale for the model in use.+ let derived = store+ .reader+ .derived_index_status(Some(triple_b.clone()))+ .await+ .unwrap();+ assert_eq!(derived.latest_pages_missing_embeddings, 1, "{derived:?}");++ // A second page under triple B only: each configured triple then+ // sees exactly the page holding the other triple's vector, and a+ // page covered by its own triple never counts.+ store+ .writer+ .upsert_page(sample_page(ws, proj, "notes/fresh.md", "body b"))+ .await+ .unwrap();+ insert_embedding("notes/fresh.md", "prov-b", "model-b", 16);+ let derived = store+ .reader+ .derived_index_status(Some(triple_a.clone()))+ .await+ .unwrap();+ assert_eq!(derived.latest_pages_missing_embeddings, 1, "{derived:?}");+ let derived = store+ .reader+ .derived_index_status(Some(triple_b.clone()))+ .await+ .unwrap();+ assert_eq!(derived.latest_pages_missing_embeddings, 1, "{derived:?}");+ let derived = store.reader.derived_index_status(None).await.unwrap();+ assert_eq!(derived.latest_pages_missing_embeddings, 0, "{derived:?}");+ }+/// Typed edges surface in the derived status by relation.#[tokio::test]async fn typed_links_are_counted_by_relation() {@@ -8778,7 +8880,7 @@ mod tests {},];store.writer.upsert_page(page).await.unwrap();- let derived = store.reader.derived_index_status().await.unwrap();+ let derived = store.reader.derived_index_status(None).await.unwrap();assert_eq!(derived.typed_links_from_latest_pages,vec![("fixes".to_string(), 1)],diff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rsindex e4869f86..91fbc96d 100644--- a/crates/ai-memory-store/src/reader.rs+++ b/crates/ai-memory-store/src/reader.rs@@ -1643,8 +1643,11 @@ pub struct DerivedIndexStatus {pub observations_rows: u64,/// Rows currently present in the observation FTS5 index.pub observations_fts_rows: u64,- /// Latest pages without any embedding row whose body is non-empty —- /// i.e. the pages a backfill can actually act on.+ /// Latest pages without an embedding row for the configured+ /// `(provider, model, dim)` triple, whose body is non-empty — i.e. the+ /// pages a backfill can actually act on. A vector stored under another+ /// triple is stale and does not cover the page. Without a configured+ /// embedder (the `None` query) this is "without any embedding row".pub latest_pages_missing_embeddings: u64,/// Latest pages without an embedding whose body is empty; no/// embedder can ever cover these (the backfill skips them by rule).@@ -11415,14 +11418,26 @@ impl ReaderPool {/// Return health counters for derived indexes and link/embedding state.///+ /// `configured_embedding` is the `(provider, model, dim)` triple the+ /// configured embedder stores rows under (`model_identity`, not the+ /// wire model name), or `None` when no embedder is configured. With a+ /// triple, a latest page counts as missing an embedding when it has no+ /// vector under *that* triple: a vector from an old model is stale and+ /// ignored by retrieval, so it must not hide the gap a backfill has to+ /// fill. With `None`, "missing" keeps its historical meaning: no vector+ /// at all.+ ////// These checks are intentionally read-only and derived-index-safe: they/// report drift but do not repair it. Rebuild/backfill paths stay behind/// explicit admin operations.////// # Errors/// Propagates any SQL or pool error.- pub async fn derived_index_status(&self) -> StoreResult<DerivedIndexStatus> {- self.with_conn(|conn| {+ pub async fn derived_index_status(+ &self,+ configured_embedding: Option<(String, String, u32)>,+ ) -> StoreResult<DerivedIndexStatus> {+ self.with_conn(move |conn| {let mut triples_stmt = conn.prepare("SELECT provider, model, dim, COUNT(*) \FROM page_embeddings \@@ -11481,14 +11496,38 @@ impl ReaderPool {// as \"missing\" overstated the actionable number forever// (observed live: a stable 427 that no backfill could// ever clear). Unembeddable pages are reported apart.- latest_pages_missing_embeddings: count(- conn,- "SELECT COUNT(*) \- FROM pages pg \- LEFT JOIN page_embeddings pe ON pe.page_id = pg.id \- WHERE pg.is_latest = 1 AND pe.page_id IS NULL \- AND TRIM(pg.body) != ''",- )?,+ // With a configured triple, "has a vector" means "has a+ // vector the current model would produce": a row stored+ // under an old triple is stale, and without the triple the+ // count would read zero after every model switch.+ latest_pages_missing_embeddings: match configured_embedding {+ Some((provider, model, dim)) => {+ let provider = provider.as_str();+ let model = model.as_str();+ let dim = i64::from(dim);+ count_bound(+ conn,+ "SELECT COUNT(*) \+ FROM pages pg \+ WHERE pg.is_latest = 1 AND TRIM(pg.body) != '' \+ AND NOT EXISTS ( \+ SELECT 1 FROM page_embeddings pe \+ WHERE pe.page_id = pg.id \+ AND pe.provider = ?1 \+ AND pe.model = ?2 \+ AND pe.dim = ?3)",+ &[&provider, &model, &dim],+ )+ }+ None => count(+ conn,+ "SELECT COUNT(*) \+ FROM pages pg \+ LEFT JOIN page_embeddings pe ON pe.page_id = pg.id \+ WHERE pg.is_latest = 1 AND pe.page_id IS NULL \+ AND TRIM(pg.body) != ''",+ ),+ }?,latest_pages_unembeddable: count(conn,"SELECT COUNT(*) \diff --git a/crates/ai-memory-store/tests/suite/fts_drift_status.rs b/crates/ai-memory-store/tests/suite/fts_drift_status.rsindex b7cb88ee..c233fa17 100644--- a/crates/ai-memory-store/tests/suite/fts_drift_status.rs+++ b/crates/ai-memory-store/tests/suite/fts_drift_status.rs@@ -46,7 +46,7 @@ async fn page_fts_rows_report_index_drift() {.await.unwrap();- let before = store.reader.derived_index_status().await.unwrap();+ let before = store.reader.derived_index_status(None).await.unwrap();assert_eq!(before.pages_rows, 1);assert_eq!(before.pages_fts_rows, 1,@@ -73,7 +73,7 @@ async fn page_fts_rows_report_index_drift() {assert_eq!(hits, 0, "the entry really left the index");}- let after = store.reader.derived_index_status().await.unwrap();+ let after = store.reader.derived_index_status(None).await.unwrap();assert_eq!(after.pages_rows, 1, "the source row is untouched");assert_eq!(after.pages_fts_rows, 0,@@ -129,7 +129,7 @@ async fn observation_fts_rows_report_index_drift() {.await.unwrap();- let before = store.reader.derived_index_status().await.unwrap();+ let before = store.reader.derived_index_status(None).await.unwrap();assert_eq!(before.observations_rows, 1);assert_eq!(before.observations_fts_rows, 1,@@ -154,7 +154,7 @@ async fn observation_fts_rows_report_index_drift() {assert_eq!(hits, 0, "the entry really left the index");}- let after = store.reader.derived_index_status().await.unwrap();+ let after = store.reader.derived_index_status(None).await.unwrap();assert_eq!(after.observations_rows, 1, "the source row is untouched");assert_eq!(after.observations_fts_rows, 0,diff --git a/crates/ai-memory-store/tests/suite/reclaim_ledger_versions.rs b/crates/ai-memory-store/tests/suite/reclaim_ledger_versions.rsindex 99a03d75..74417d96 100644--- a/crates/ai-memory-store/tests/suite/reclaim_ledger_versions.rs+++ b/crates/ai-memory-store/tests/suite/reclaim_ledger_versions.rs@@ -483,7 +483,7 @@ async fn a_reclaimed_ledger_row_leaves_no_searchable_text_behind() {.await.unwrap();- let drift = store.reader.derived_index_status().await.unwrap();+ let drift = store.reader.derived_index_status(None).await.unwrap();assert_eq!(drift.pages_fts_rows, drift.pages_rows,"the rebuilt index agrees with the content table again",diff --git a/crates/ai-memory-store/tests/suite/stress_writer_throughput.rs b/crates/ai-memory-store/tests/suite/stress_writer_throughput.rsindex d4ad56a2..4691ff64 100644--- a/crates/ai-memory-store/tests/suite/stress_writer_throughput.rs+++ b/crates/ai-memory-store/tests/suite/stress_writer_throughput.rs@@ -143,7 +143,7 @@ async fn a_burst_larger_than_the_queue_applies_backpressure_and_loses_nothing()t.await.unwrap();}- let status = store.reader.derived_index_status().await.unwrap();+ let status = store.reader.derived_index_status(None).await.unwrap();assert_eq!(status.observations_rows, BURST as u64,"every write in a burst larger than the queue must land: backpressure, \diff --git a/crates/ai-memory-web/src/routes/health.rs b/crates/ai-memory-web/src/routes/health.rsindex c508388a..d507fe83 100644--- a/crates/ai-memory-web/src/routes/health.rs+++ b/crates/ai-memory-web/src/routes/health.rs@@ -34,7 +34,23 @@ pub(crate) async fn handler(tracing::error!(%error, "web health read failed");StatusCode::INTERNAL_SERVER_ERROR.into_response()};- let derived = match state.reader.derived_index_status().await {+ // The configured embedder's stored-vector triple, read once so the+ // missing-vector count and the "embedder" row below both match the+ // identity rows are stored under (`model_identity`, not the wire+ // model name). With no embedder the historical "no vector at all"+ // count is kept. The count takes a clone: the row keeps the original.+ let configured_embedding = state.recall.embedder.as_deref().map(|embedder| {+ (+ embedder.provider().to_owned(),+ embedder.model_identity(),+ embedder.dim(),+ )+ });+ let derived = match state+ .reader+ .derived_index_status(configured_embedding.clone())+ .await+ {Ok(derived) => derived,Err(error) => return internal(error),};@@ -71,15 +87,11 @@ pub(crate) async fn handler(derived,storage,consolidation,- embedder: state- .recall- .embedder- .as_ref()- .map(|embedder| EmbedderIdentity {- provider: embedder.provider().to_owned(),- model: embedder.model_identity(),- dim: embedder.dim(),- }),+ embedder: configured_embedding.map(|(provider, model, dim)| EmbedderIdentity {+ provider,+ model,+ dim,+ }),};let view = HealthView {wired: state.health.is_some(),diff --git a/crates/ai-memory-web/tests/suite/health.rs b/crates/ai-memory-web/tests/suite/health.rsindex d92e3496..183a24bb 100644--- a/crates/ai-memory-web/tests/suite/health.rs+++ b/crates/ai-memory-web/tests/suite/health.rs@@ -358,6 +358,74 @@ async fn health_page_renders_each_failing_check_with_its_fix_line() {assert!(!body.contains("data-check=\"none\""), "{body}");}+/// A page whose only vector was made under another triple must fire+/// `missing_vectors` now that the count is computed against the configured+/// embedder's triple: before the count knew the triple, such a page read+/// as covered and the check stayed silent.+#[tokio::test]+async fn missing_vectors_fires_when_the_only_vector_is_from_another_triple() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let proj = store+ .writer+ .get_or_create_project(ws, "models", None)+ .await+ .unwrap();+ // A single page; its only vector is the old model's (8-dim synthetic)+ // triple.+ let embedding_wiki = Wiki::new(tmp.path(), store.writer.clone())+ .unwrap()+ .with_embedder(Arc::new(SyntheticEmbedder::new(8)));+ embedding_wiki+ .write_page(page(+ ws,+ proj,+ "notes/old-model.md",+ "the only vector here is stale",+ ))+ .await+ .unwrap();+ let wiki = embedding_wiki.without_embedder();+ let f = Fixture {+ _tmp: tmp,+ store,+ wiki,+ };+ let sources = HealthSources {+ ingest: Arc::new(IngestMetrics::default()),+ providers: ProviderHealth::default(),+ probe: Arc::new(FixedProbe(ProcessState {+ write_queue: Some((0, 100)),+ spool: Some(SpoolState {+ capacity: 500,+ ..SpoolState::default()+ }),+ journal: Some(JournalState::default()),+ })),+ };+ // The configured embedder is the new model (16-dim synthetic).+ let (status, _, body) = get(+ app(&f, AuthLevel::Root, Some(sources), configured_embedder()),+ true,+ )+ .await;+ assert_eq!(status, StatusCode::OK, "{body}");+ assert_check(&body, "missing_vectors", Some("ai-memory embed"));+ // The counter behind it: one latest page without a 16-dim vector.+ assert!(body.contains("1 latest pages missing"), "{body}");+ // The row still shows up as a stored triple — the page is not+ // silently dropped from the index picture.+ assert!(+ body.contains("synthetic/bag-of-words-v1 dim 8: 1 rows"),+ "{body}"+ );+}+#[tokio::test]async fn health_page_renders_pending_spool_and_journal_checks() {let f = fixture().await;