Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
perf(admin): move-project carries source embeddings instead of re-embedding
Djalma Júniorff490de2a705
diff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rsindex 87fbfc3f..d4f2f120 100644--- a/crates/ai-memory-mcp/src/admin.rs+++ b/crates/ai-memory-mcp/src/admin.rs@@ -1694,8 +1694,46 @@ async fn handle_move_project(Err(e) => return internal_err(e.to_string()),};- // COPY each page through the canonical write path so sanitization, link- // re-resolution, FTS upsert (and admission/git-mirror on deploy) all fire.+ // Carry the source page embeddings over verbatim instead of recomputing+ // them — embedding is the dominant per-page cost of a bulk move. Writes go+ // through an embedder-less Wiki so `write_page` never re-embeds; we then+ // store each source vector against the new page id. Only embeddings+ // computed with the CURRENTLY configured embedder ({provider,model,dim})+ // are loaded (load_embeddings filters on them), so the "one model per+ // index" invariant holds; any page lacking a current-model embedding is+ // simply copied without one (backfill later via `ai-memory embed`).+ let copy_wiki = state.wiki.clone().without_embedder();+ let mut src_embeddings: std::collections::HashMap<String, Vec<u8>> =+ std::collections::HashMap::new();+ let embed_meta: Option<(String, String, u32)> = if let Some(embedder) = &state.embedder {+ let (provider, model, dim) = (+ embedder.provider().to_string(),+ embedder.model().to_string(),+ embedder.dim(),+ );+ match state+ .reader+ .load_embeddings(src_ws, src_proj, provider.clone(), model.clone(), dim)+ .await+ {+ Ok(rows) => {+ for e in rows {+ src_embeddings.insert(e.path.to_string(), f32_vec_to_bytes(&e.vector));+ }+ Some((provider, model, dim))+ }+ Err(e) => {+ warn!(error = %e, "move-project: failed to load source embeddings; copies land without vectors");+ None+ }+ }+ } else {+ None+ };++ // COPY each page through the (embedder-less) write path so sanitization,+ // link re-resolution, FTS upsert (and admission/git-mirror on deploy) all+ // fire — minus the per-page embed, which we carry over below.let mut pages_copied = 0u64;let mut pages_skipped: Vec<String> = Vec::new();for s in &summaries {@@ -1719,8 +1757,7 @@ async fn handle_move_project(continue;}};- if let Err(e) = state- .wiki+ let new_page_id = match copy_wiki.write_page(WritePageRequest {workspace_id: dst_ws,project_id: dst_proj,@@ -1737,8 +1774,26 @@ async fn handle_move_project(}).await{+ Ok(pid) => pid,// ANY copy failure aborts BEFORE the purge — the source survives.- return internal_err(format!("copy of {} failed: {e}", s.path));+ Err(e) => return internal_err(format!("copy of {} failed: {e}", s.path)),+ };+ // Carry the source embedding over (skip the re-embed) when the source+ // had one for the current model.+ if let (Some((provider, model, dim)), Some(bytes)) =+ (&embed_meta, src_embeddings.get(&s.path))+ && let Err(e) = state+ .writer+ .store_embedding(+ new_page_id,+ bytes.clone(),+ provider.clone(),+ model.clone(),+ *dim,+ )+ .await+ {+ warn!(path = %s.path, error = %e, "move-project: failed to carry embedding; page copied without it");}pages_copied += 1;}diff --git a/crates/ai-memory-mcp/tests/admin_move.rs b/crates/ai-memory-mcp/tests/admin_move.rsindex 624557b1..388e0c64 100644--- a/crates/ai-memory-mcp/tests/admin_move.rs+++ b/crates/ai-memory-mcp/tests/admin_move.rs@@ -303,3 +303,124 @@ async fn move_project_same_workspace_rejected() {.await;assert_eq!(resp.status(), StatusCode::UNPROCESSABLE_ENTITY);}++/// The move carries the source page's existing embedding over instead of+/// recomputing it. Proven by overwriting the source embedding with a+/// recognisable marker vector: if the move re-embedded, the destination would+/// hold the synthetic bag-of-words vector, not the marker.+#[tokio::test]+async fn move_project_carries_source_embedding() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let embedder: std::sync::Arc<dyn ai_memory_llm::Embedder> =+ std::sync::Arc::new(ai_memory_llm::SyntheticEmbedder::new(8));+ let wiki = Wiki::new(tmp.path(), store.writer.clone())+ .unwrap()+ .with_embedder(embedder.clone());+ let db_path = store.db_path().to_path_buf();+ let state = AdminState {+ writer: store.writer.clone(),+ reader: store.reader.clone(),+ wiki,+ llm: None,+ embedder: Some(embedder),+ provider_health: ai_memory_llm::ProviderHealth::default(),+ decay_params: DecayParams::default(),+ data_dir: tmp.path().to_path_buf(),+ db_path,+ bind: "127.0.0.1:0".to_string(),+ bootstrap_lock: std::sync::Arc::new(tokio::sync::Mutex::new(())),+ token_pepper: None,+ };++ // Seed source page (gets a synthetic embedding on write).+ seed_page(+ &store,+ &state.wiki,+ "src",+ "proj",+ "notes/a.md",+ "hello world embed",+ )+ .await;++ let src_ws = store+ .reader+ .find_workspace("src".to_string())+ .await+ .unwrap()+ .unwrap();+ let src_proj = store+ .reader+ .find_project(src_ws, "proj".to_string())+ .await+ .unwrap()+ .unwrap();+ let src = store+ .reader+ .load_embeddings(+ src_ws,+ src_proj,+ "synthetic".to_string(),+ "bag-of-words-v1".to_string(),+ 8,+ )+ .await+ .unwrap();+ assert_eq!(src.len(), 1, "source page must have an embedding to carry");++ // Overwrite with a recognisable marker vector.+ let marker: Vec<f32> = vec![9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0];+ store+ .writer+ .store_embedding(+ src[0].id,+ ai_memory_store::f32_vec_to_bytes(&marker),+ "synthetic".to_string(),+ "bag-of-words-v1".to_string(),+ 8,+ )+ .await+ .unwrap();++ let resp = post(+ state,+ "/admin/move-project",+ json!({ "from_workspace": "src", "project": "proj", "to_workspace": "dst", "confirm": true }),+ )+ .await;+ assert_eq!(resp.status(), StatusCode::OK);++ // The destination page must carry the MARKER (not a recomputed vector).+ let dst_ws = store+ .reader+ .find_workspace("dst".to_string())+ .await+ .unwrap()+ .unwrap();+ let dst_proj = store+ .reader+ .find_project(dst_ws, "proj".to_string())+ .await+ .unwrap()+ .unwrap();+ let dst = store+ .reader+ .load_embeddings(+ dst_ws,+ dst_proj,+ "synthetic".to_string(),+ "bag-of-words-v1".to_string(),+ 8,+ )+ .await+ .unwrap();+ assert_eq!(dst.len(), 1, "dest page must carry an embedding");+ for (got, want) in dst[0].vector.iter().zip(marker.iter()) {+ assert!(+ (got - want).abs() < 1e-4,+ "carried embedding must equal the source marker (not a recompute); got {:?}",+ dst[0].vector+ );+ }+}diff --git a/crates/ai-memory-wiki/src/wiki.rs b/crates/ai-memory-wiki/src/wiki.rsindex fddaa7ce..bd69b661 100644--- a/crates/ai-memory-wiki/src/wiki.rs+++ b/crates/ai-memory-wiki/src/wiki.rs@@ -129,6 +129,17 @@ impl Wiki {self.embedder.as_ref()}+ /// Return a clone-friendly handle with the embedder detached, so+ /// `write_page` skips the per-page `embed_document` call. Used by+ /// bulk copy paths (e.g. `move-project`) that carry the source page's+ /// existing embedding over verbatim instead of recomputing it — the+ /// caller is then responsible for `store_embedding` on the new page.+ #[must_use]+ pub fn without_embedder(mut self) -> Self {+ self.embedder = None;+ self+ }+/// Borrow the git adapter (for callers wiring auto-commit).#[must_use]pub fn git(&self) -> &GitAdapter {