fix(search): prevent memory leaks and duplication in tantivy search index by explicitly deleting prior terms during updates and syncing store deletions
This commit is contained in:
1 parent
7dc0c329ad
commit
af2c4ab046
4 files changed
+42
-5
No files matched your search
@@ -66,6 +66,7 @@ impl McpTool for DeleteTaskHandler {
|
||||
async fn execute(&self, args: Value, state: Arc<MemoryState>) -> Result<String, String> {
|
||||
let req: DeleteTaskTool = serde_json::from_value(args).map_err(|e| e.to_string())?;
|
||||
let mut deleted_count = 0;
|
||||
let mut actually_deleted = Vec::new();
|
||||
state.tasks.modify(|tasks| {
|
||||
let initial_len = tasks.len();
|
||||
// Collect IDs of tasks to delete (this task + all its recursive children)
|
||||
@@ -93,12 +94,17 @@ impl McpTool for DeleteTaskHandler {
|
||||
queue.extend(children.iter().cloned());
|
||||
}
|
||||
}
|
||||
|
||||
tasks.retain(|t| !to_delete.contains(&t.id));
|
||||
|
||||
actually_deleted = to_delete.into_iter().collect();
|
||||
tasks.retain(|t| !actually_deleted.contains(&t.id));
|
||||
deleted_count = initial_len - tasks.len();
|
||||
});
|
||||
|
||||
if deleted_count > 0 {
|
||||
let idx = state.search_index.read().unwrap_or_else(|e| e.into_inner()).clone();
|
||||
for id in actually_deleted {
|
||||
let _ = idx.delete_document(&id).await;
|
||||
}
|
||||
Ok(vec![
|
||||
format!("Deleted task and its children ({} total).", deleted_count).to_string(),
|
||||
][0]
|
||||
|
||||
Reference in new issue
Block a user