Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(wiki): return WikiResult from purge_session; cover the write race
Samir Hanna Verza8017a8bde665
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex baccb86d..123d197f 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -17,9 +17,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0now uses the import instead of a prose pointer (#680).### Fixed-- `purge-session` held the wiki mutation guard across database deletion and file- cleanup, preventing an in-flight watcher reindex from restoring deleted rows- or a concurrent page write from losing its file during cleanup (#653).+- `purge-session` took the wiki mutation guard only for the file cleanup, after+ the database deletion had already committed. A watcher reindex could reinsert+ the still-present page in that gap and keep only the row, and a concurrent page+ write could lose its file to the cleanup. The purge now holds the guard across+ both steps, so reindexes, page writes and wiki moves finish before it starts+ and wait until it is done (#696, follow-up to #653).- The from-source AUR `PKGBUILD` now builds and tests on constrained AURbuilders. Release LTO was disabled (`options=('!debug' '!lto')`) so thefinal link no longer gets OOM-killed on low-memory build hosts, and thediff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rsindex b95575b0..441dcb5f 100644--- a/crates/ai-memory-mcp/src/admin.rs+++ b/crates/ai-memory-mcp/src/admin.rs@@ -3673,7 +3673,7 @@ async fn handle_purge_session({Ok(s) => s,// Absent from this scope (or already purged) is a 404, not a fault.- Err(e @ StoreError::NotFound(_)) => {+ Err(WikiError::Store(e @ StoreError::NotFound(_))) => {return (StatusCode::NOT_FOUND,Json(serde_json::json!({ "error": e.to_string() })),diff --git a/crates/ai-memory-store/src/writer.rs b/crates/ai-memory-store/src/writer.rsindex e8bb6941..027d7e06 100644--- a/crates/ai-memory-store/src/writer.rs+++ b/crates/ai-memory-store/src/writer.rs@@ -1535,6 +1535,11 @@ impl WriterHandle {/// deleted. See [`ops::purge_session`] for what is and is not removed —/// in particular, handoffs this session *accepted* are left alone.///+ /// Server callers must go through `Wiki::purge_session` instead, which+ /// holds the wiki mutation guard across this deletion and the page-file+ /// cleanup. Calling this directly commits the rows with no guard, so a+ /// watcher reindex can reinsert the page before the file is removed (#653).+ ////// # Errors/// [`StoreError::NotFound`] when the session is absent from that scope,/// [`StoreError::WriterClosed`], or a propagated SQL error.diff --git a/crates/ai-memory-wiki/src/wiki.rs b/crates/ai-memory-wiki/src/wiki.rsindex f0519503..c484c8ba 100644--- a/crates/ai-memory-wiki/src/wiki.rs+++ b/crates/ai-memory-wiki/src/wiki.rs@@ -11,8 +11,8 @@ use ai_memory_core::{use ai_memory_llm::Embedder;use ai_memory_store::{ApproveAutoImproveProposal, ApproveAutoImproveProposalResult, AutoImproveProposalDetail,- FailAutoImproveProposal, MoveSessionSummary, MoveSummary, PagesMode, ReaderPool, WriterHandle,- artifact_path_for, f32_vec_to_bytes,+ FailAutoImproveProposal, MoveSessionSummary, MoveSummary, PagesMode, PurgeSessionSummary,+ ReaderPool, WriterHandle, artifact_path_for, f32_vec_to_bytes,};use tokio::sync::RwLock;@@ -23,9 +23,10 @@ use crate::markdown::{Markdown, derive_title, emit, parse};use crate::watcher::is_pending_path;/// Store deletion and best-effort file cleanup from [`Wiki::purge_session`].+#[derive(Debug, Clone)]pub struct PurgeSessionOutcome {/// Committed database deletion counts and paths.- pub summary: ai_memory_store::PurgeSessionSummary,+ pub summary: PurgeSessionSummary,/// Paths successfully removed from disk.pub files_deleted: Vec<PagePath>,/// Paths whose file cleanup failed after the database committed.@@ -1223,7 +1224,8 @@ impl Wiki {/// page write or project move from landing between SQL and file cleanup.////// # Errors- /// Returns the store error without removing files if the database purge fails.+ /// Returns [`WikiError::Store`] without removing files if the database+ /// purge fails.pub async fn purge_session(&self,workspace_id: WorkspaceId,@@ -1231,13 +1233,13 @@ impl Wiki {session_id: SessionId,author_id: Option<UserId>,compaction: ai_memory_store::Compaction,- ) -> ai_memory_store::StoreResult<PurgeSessionOutcome> {+ ) -> WikiResult<PurgeSessionOutcome> {let _guard = self.mutation_lock.write().await;let summary = self.writer.purge_session(workspace_id, project_id, session_id, author_id, compaction).await?;- let mut files_deleted = Vec::new();+ let mut files_deleted = Vec::with_capacity(summary.removed_paths.len());let mut files_failed = Vec::new();for path in &summary.removed_paths {let abs = self.abs_path(workspace_id, project_id, path);@@ -1248,8 +1250,12 @@ impl Wiki {}Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}Err(error) => {- tracing::warn!(operation = "purge-session", path = path.as_str(),- error = %error, "session purge failed to remove wiki page file");+ tracing::warn!(+ operation = "purge-session",+ path = path.as_str(),+ error = %error,+ "session purge failed to remove wiki page file",+ );files_failed.push(path.clone());}}@@ -5177,7 +5183,7 @@ mod tests {tokio::pin!(purge);tokio::select! {biased;- result = &mut purge => panic!("purge bypassed an active reader: {}", result.is_ok()),+ result = &mut purge => panic!("purge bypassed an active reader: {result:?}"),() = std::future::ready(()) => {}}store@@ -5222,6 +5228,56 @@ mod tests {);}+ /// The writer's half of the same claim. `purge_session` holds the+ /// exclusive guard across SQL and cleanup — proved by+ /// `purge_session_waits_for_in_flight_reindex_before_deleting_rows` — and+ /// `write_page` takes the shared one, so a write cannot install its file+ /// while a purge owns the guard and would otherwise delete it during+ /// cleanup. Removing the shared guard from `write_page` makes this fail.+ #[tokio::test]+ async fn page_write_cannot_land_while_the_purge_guard_is_held() {+ let tmp = TempDir::new().unwrap();+ let (store, wiki, ws, proj, _, _sid, _path) = session_with_page(&tmp).await;+ let target = PagePath::new("decisions/keep.md").unwrap();+ let abs = wiki.abs_path(ws, proj, &target);++ let purge_guard = wiki.mutation_lock.write().await;+ let write = wiki.write_page(req(+ ws,+ proj,+ target.as_str(),+ "kept body",+ serde_json::json!({ "title": "Keep" }),+ ));+ tokio::pin!(write);+ // A single poll would only prove the write did not finish in one+ // step, which is true even without the guard: `write_page` parks on+ // the writer queue before it touches disk. Give it real time instead.+ assert!(+ tokio::time::timeout(std::time::Duration::from_millis(250), &mut write)+ .await+ .is_err(),+ "the page write must not complete while a purge owns the guard"+ );+ assert!(+ !abs.exists(),+ "no page file may be installed while a purge owns the guard"+ );++ drop(purge_guard);+ write.await.unwrap();+ assert!(abs.exists(), "the write completes once the purge releases");+ assert!(+ !store+ .reader+ .search_pages("kept".into(), 10)+ .await+ .unwrap()+ .is_empty(),+ "disk and index agree after the guard is released"+ );+ }+#[tokio::test]async fn purge_session_store_failure_preserves_page_file() {let tmp = TempDir::new().unwrap();@@ -5233,7 +5289,7 @@ mod tests {.await;assert!(matches!(result,- Err(ai_memory_store::StoreError::NotFound(_))+ Err(WikiError::Store(ai_memory_store::StoreError::NotFound(_)))));assert_eq!(std::fs::read(&abs).unwrap(), before);assert!(diff --git a/docs/lifecycle-ops.md b/docs/lifecycle-ops.mdindex f6ac7d27..b435f5e6 100644--- a/docs/lifecycle-ops.md+++ b/docs/lifecycle-ops.md@@ -96,6 +96,13 @@ purge with wiki project/session moves. Admission webhooks run before this guard.File cleanup failures still leave the database purge committed and are reportedin `files_failed`; this coordination does not provide crash-atomic rollback.+The guard is taken before the purge is submitted to the writer actor, so it also+covers the wait for whatever that single queue is already draining, and — with+`compact: true` — the `VACUUM` that runs after the delete commits. A purge on a+busy server therefore holds up wiki mutations for longer than the delete itself.+The git checkpoints taken before and after the purge sit outside the guard: they+bracket it, they do not snapshot it.+### Scope containmentThe session id is never authority on its own. Every statement is filtered on