Skip to content

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

Rust

fix(wiki): delete_page drops the index row, not just the file

Djalma Júnior16ec603f3165
diff --git a/crates/ai-memory-mcp/src/server.rs b/crates/ai-memory-mcp/src/server.rs
index 36faebb3..890cfdb6 100644
--- a/crates/ai-memory-mcp/src/server.rs
+++ b/crates/ai-memory-mcp/src/server.rs
@@ -2053,6 +2053,28 @@ mod tests {
}))
.await;
assert!(read.is_err(), "deleted page must not be readable");
+
+ // Regression: the derived index row must also be gone — the watcher
+ // does not reconcile deletions, so a file-only delete would leave the
+ // page surfacing in recent/search with stale content.
+ let recent = server
+ .memory_recent(Parameters(RecentArgs {
+ limit: Some(10),
+ project: None,
+ workspace: None,
+ }))
+ .await
+ .unwrap();
+ let recent_text = recent
+ .content
+ .first()
+ .and_then(|c| c.as_text())
+ .map(|t| t.text.clone())
+ .unwrap();
+ assert!(
+ !recent_text.contains("notes/temp.md"),
+ "deleted page must not linger in the index; got {recent_text}"
+ );
}
/// `memory_handoff_begin` must resolve the same project as
diff --git a/crates/ai-memory-store/src/ops.rs b/crates/ai-memory-store/src/ops.rs
index 8a13150f..913e3776 100644
--- a/crates/ai-memory-store/src/ops.rs
+++ b/crates/ai-memory-store/src/ops.rs
@@ -549,6 +549,29 @@ pub fn soft_delete_for_decay(conn: &mut Connection, page_ids: &[PageId]) -> Stor
Ok(affected)
}
+/// Delete every version of a page (by path) from the index. Used when the
+/// wiki file is removed (`Wiki::delete_page`): the watcher does not handle
+/// file deletions, so the derived rows must be dropped explicitly or the
+/// page keeps surfacing in search/recent with stale content. FK cascades
+/// drop outgoing links + embeddings; the `pages_fts_ad` trigger keeps FTS in
+/// sync; incoming links are set to NULL (unresolved). Idempotent.
+pub fn delete_page(
+ conn: &Connection,
+ workspace_id: WorkspaceId,
+ project_id: ProjectId,
+ path: &PagePath,
+) -> StoreResult<()> {
+ conn.execute(
+ "DELETE FROM pages WHERE workspace_id = ?1 AND project_id = ?2 AND path = ?3",
+ params![
+ workspace_id.as_bytes(),
+ project_id.as_bytes(),
+ path.as_str()
+ ],
+ )?;
+ Ok(())
+}
+
/// Hard-delete rows that were soft-deleted by an earlier sweep at
/// least `hard_delete_after_days` ago AND received zero subsequent
/// accesses. Safe: M7 supersedes-chain pages have a non-null
diff --git a/crates/ai-memory-store/src/writer.rs b/crates/ai-memory-store/src/writer.rs
index 127badae..94ffff27 100644
--- a/crates/ai-memory-store/src/writer.rs
+++ b/crates/ai-memory-store/src/writer.rs
@@ -12,7 +12,7 @@ use std::thread::{self, JoinHandle};
use ai_memory_core::{
AgentKind, HandoffId, NewHandoff, NewObservation, NewPage, NewSession, ObservationId, PageId,
- ProjectId, SessionId, WorkspaceId,
+ PagePath, ProjectId, SessionId, WorkspaceId,
};
use rusqlite::Connection;
use tokio::sync::{mpsc, oneshot};
@@ -40,6 +40,12 @@ pub(crate) enum WriteCmd {
pages: Vec<NewPage>,
reply: oneshot::Sender<StoreResult<Vec<PageId>>>,
},
+ DeletePage {
+ workspace_id: WorkspaceId,
+ project_id: ProjectId,
+ path: PagePath,
+ reply: oneshot::Sender<StoreResult<()>>,
+ },
BeginSession {
session: NewSession,
reply: oneshot::Sender<StoreResult<()>>,
@@ -494,6 +500,31 @@ impl WriterHandle {
rx.await.map_err(|_| StoreError::WriterClosed)?
}
+ /// Delete every version of a page (by path) from the index. The wiki file
+ /// removal is the caller's concern; this drops the derived rows so the
+ /// page stops appearing in search/recent (the watcher does NOT reconcile
+ /// file deletions — it only handles create/modify events).
+ ///
+ /// # Errors
+ /// Returns [`StoreError::WriterClosed`] if the actor has shut down, or a
+ /// SQL error from the delete.
+ pub async fn delete_page(
+ &self,
+ workspace_id: WorkspaceId,
+ project_id: ProjectId,
+ path: PagePath,
+ ) -> StoreResult<()> {
+ let (tx, rx) = oneshot::channel();
+ self.send(WriteCmd::DeletePage {
+ workspace_id,
+ project_id,
+ path,
+ reply: tx,
+ })
+ .await?;
+ rx.await.map_err(|_| StoreError::WriterClosed)?
+ }
+
async fn send(&self, cmd: WriteCmd) -> StoreResult<()> {
self.inner
.tx
@@ -553,6 +584,15 @@ fn worker_loop(mut conn: Connection, mut rx: mpsc::Receiver<WriteCmd>) {
let result = ops::upsert_page(&mut conn, &page);
send_or_warn(reply, result, "upsert_page");
}
+ WriteCmd::DeletePage {
+ workspace_id,
+ project_id,
+ path,
+ reply,
+ } => {
+ let result = ops::delete_page(&conn, workspace_id, project_id, &path);
+ send_or_warn(reply, result, "delete_page");
+ }
WriteCmd::UpsertPageBatch { pages, reply } => {
let result = ops::upsert_pages_batch(&mut conn, &pages);
send_or_warn(reply, result, "upsert_pages_batch");
diff --git a/crates/ai-memory-wiki/src/wiki.rs b/crates/ai-memory-wiki/src/wiki.rs
index b2a5eedd..eb767e98 100644
--- a/crates/ai-memory-wiki/src/wiki.rs
+++ b/crates/ai-memory-wiki/src/wiki.rs
@@ -241,10 +241,17 @@ impl Wiki {
}
let abs = self.abs_path(workspace_id, project_id, path);
match std::fs::remove_file(&abs) {
- Ok(()) => Ok(()),
- Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
- Err(e) => Err(crate::WikiError::Io(e)),
+ Ok(()) => {}
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
+ Err(e) => return Err(crate::WikiError::Io(e)),
}
+ // The watcher only reconciles create/modify events, not deletions, so
+ // drop the derived index rows here — otherwise the page lingers in
+ // search/recent with stale content after its file is gone.
+ self.writer
+ .delete_page(workspace_id, project_id, path.clone())
+ .await?;
+ Ok(())
}
/// Purge a whole project's wiki directory. When an admission chain is