Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(embed): count abstract-only pages in the dry-run report
Rudy Mizrahi Celekli80fbf56d83e8
diff --git a/crates/ai-memory-consolidate/src/embed.rs b/crates/ai-memory-consolidate/src/embed.rsindex 0302fac6..1899d5d2 100644--- a/crates/ai-memory-consolidate/src/embed.rs+++ b/crates/ai-memory-consolidate/src/embed.rs@@ -32,8 +32,9 @@ pub struct EmbedBackfillCounts {pub skipped: usize,/// Pages that failed to embed (read error or provider error).pub failed: usize,- /// Pages that would be embedded in a live run (only meaningful- /// when `dry_run` was requested).+ /// Pages the live run would call the embedder for (only meaningful+ /// when `dry_run` was requested) — a missing body vector, a missing L0+ /// abstract vector, or both.pub would_embed: usize,/// L0 abstracts (frontmatter `abstract:`) embedded into/// `page_abstract_embeddings` — the opt-in fifth retrieval stream.@@ -150,9 +151,12 @@ pub async fn run_embedding_backfill(continue;}if options.dry_run {- if need_body {- counts.would_embed += 1;- }+ // Either vector means the live run calls the embedder for this+ // page. Counting only `need_body` dropped an abstract-only page+ // from the report entirely: the `skipped` branch above fires only+ // when NEITHER vector is missing, so the page appeared in no+ // count at all and the dry run reported no work to do.+ counts.would_embed += 1;continue;}let md = match wiki.read_page(workspace_id, project_id, &cand.path) {diff --git a/crates/ai-memory-consolidate/tests/suite/abstract_backfill.rs b/crates/ai-memory-consolidate/tests/suite/abstract_backfill.rsindex 1a9a45c5..8e24f16d 100644--- a/crates/ai-memory-consolidate/tests/suite/abstract_backfill.rs+++ b/crates/ai-memory-consolidate/tests/suite/abstract_backfill.rs@@ -108,3 +108,129 @@ async fn embedding_backfill_embeds_frontmatter_abstract() {assert_eq!(counts.embedded, 1, "plain page body embedded");assert_eq!(counts.abstracts_embedded, 0, "no abstract to embed");}++/// A dry run must count the page it would call the embedder for even when only+/// the L0 abstract vector is missing. That is the population the abstract+/// backfill documents itself as serving — pages written before the feature, or+/// with the embedder detached — so reporting zero work tells the operator to+/// skip a run that would in fact write.+#[tokio::test]+async fn dry_run_counts_a_page_needing_only_an_abstract() {+ 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, "scratch", None)+ .await+ .unwrap();+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();+ wiki.write_page(WritePageRequest {+ workspace_id: ws,+ project_id: proj,+ path: PagePath::new("notes/with-abstract.md").unwrap(),+ frontmatter: serde_json::json!({+ "title": "with abstract",+ "abstract": "one-line summary of the page for the abstract stream"+ }),+ body: "alpha bravo".to_string(),+ tier: Tier::Semantic,+ pinned: false,+ title: None,+ admission_ctx: None,+ author_id: None,+ actor: ai_memory_core::ActorContext::anonymous(),+ evidence: Vec::new(),+ })+ .await+ .unwrap();++ let embedder: Arc<dyn Embedder> = Arc::new(SyntheticEmbedder::new(64));+ let opts = ai_memory_consolidate::EmbedBackfillOptions::default();++ // A live run lands both vectors, so the page is fully up to date.+ let counts = ai_memory_consolidate::run_embedding_backfill(+ &store.reader,+ &store.writer,+ &wiki,+ &embedder,+ ws,+ proj,+ opts,+ )+ .await+ .unwrap();+ assert_eq!(counts.embedded, 1);+ assert_eq!(counts.abstracts_embedded, 1);++ // Control: with both vectors present the dry run reports no work.+ let dry = ai_memory_consolidate::run_embedding_backfill(+ &store.reader,+ &store.writer,+ &wiki,+ &embedder,+ ws,+ proj,+ ai_memory_consolidate::EmbedBackfillOptions {+ dry_run: true,+ ..opts+ },+ )+ .await+ .unwrap();+ assert_eq!(+ dry.would_embed, 0,+ "control: nothing to do while both vectors are current"+ );+ assert_eq!(dry.skipped, 1, "control: the page is counted as up to date");++ // Drop ONLY the abstract vector: `need_body` is false, `need_abstract` is+ // true — the case the dry run used to drop from every count.+ let conn = rusqlite::Connection::open(store.db_path()).unwrap();+ let removed = conn+ .execute("DELETE FROM page_abstract_embeddings", [])+ .unwrap();+ assert_eq!(removed, 1, "exactly the one abstract vector was removed");++ let dry = ai_memory_consolidate::run_embedding_backfill(+ &store.reader,+ &store.writer,+ &wiki,+ &embedder,+ ws,+ proj,+ ai_memory_consolidate::EmbedBackfillOptions {+ dry_run: true,+ ..opts+ },+ )+ .await+ .unwrap();+ assert_eq!(+ dry.would_embed, 1,+ "the abstract-only page is counted as work the live run would do"+ );+ assert_eq!(+ dry.skipped, 0,+ "and it is not reported as already up to date"+ );++ // And a live run does in fact write that vector, so the dry run was right.+ let counts = ai_memory_consolidate::run_embedding_backfill(+ &store.reader,+ &store.writer,+ &wiki,+ &embedder,+ ws,+ proj,+ opts,+ )+ .await+ .unwrap();+ assert_eq!(counts.abstracts_embedded, 1);+ assert_eq!(counts.embedded, 0, "the body vector was already current");+}