refactor: resolve critical concurrency, data loss, schema, and search audit passes

This commit is contained in:
Riz Ashraf committed 2026-10-07 07:09:28 +01:00
1 parent e4a0fe72df
commit d80915635f
7 files changed
+846 -450

No files matched your search

+68 -44
View File
@@ -139,67 +139,91 @@ pub async fn condense_graph_worker(state: Arc<MemoryState>) {
.unwrap_or_default()
.as_secs();
let mut condensed_sticky_content = String::new();
state.code.sticky.modify(|notes| {
let sticky_condensation = state.code.sticky.read_with(|notes| {
if notes.len() > threshold {
notes.sort_by_key(|n| n.timestamp);
let to_remove = notes.len() - (threshold / 2);
let removed: Vec<_> = notes.drain(0..to_remove).collect();
for r in removed {
condensed_sticky_content.push_str(&format!("{}\n", r.content));
let mut sorted = notes.clone();
sorted.sort_by_key(|n| n.timestamp);
let to_remove = sorted.len() - (threshold / 2);
let removed: Vec<_> = sorted.into_iter().take(to_remove).collect();
let mut content = String::new();
let mut ids = Vec::new();
for r in &removed {
content.push_str(&format!("{}\n", r.content));
ids.push(r.id.clone());
}
Some((content, ids))
} else {
None
}
});
if !condensed_sticky_content.is_empty() {
state.modify_graph(|graph| {
if let Some((content, ids)) = sticky_condensation {
if !content.is_empty() {
let name = format!("StickyNote History {}", now);
graph.entities.insert(
name.clone(),
crate::models::Entity {
name: name.clone(),
entity_type: "Historical Summary".to_string(),
observations: vec![condensed_sticky_content],
namespace: crate::models::default_namespace(),
git_branch: None,
..Default::default()
},
);
});
tracing::info!("Condensed sticky notes into Historical Summary.");
state.modify_graph(|graph| {
graph.entities.insert(
name.clone(),
crate::models::Entity {
name: name.clone(),
entity_type: "Historical Summary".to_string(),
observations: vec![content],
namespace: crate::models::default_namespace(),
git_branch: None,
..Default::default()
},
);
});
let id_set: std::collections::HashSet<String> = ids.into_iter().collect();
state.code.sticky.modify(|notes| {
notes.retain(|n| !id_set.contains(&n.id));
});
tracing::info!("Condensed sticky notes into Historical Summary.");
}
}
let mut condensed_snippet_content = String::new();
state.code.snippets.modify(|snippets| {
let snippet_condensation = state.code.snippets.read_with(|snippets| {
if snippets.len() > threshold {
snippets.sort_by_key(|s| s.updated_at);
let to_remove = snippets.len() - (threshold / 2);
let removed: Vec<_> = snippets.drain(0..to_remove).collect();
for r in removed {
condensed_snippet_content.push_str(&format!(
let mut sorted = snippets.clone();
sorted.sort_by_key(|s| s.updated_at);
let to_remove = sorted.len() - (threshold / 2);
let removed: Vec<_> = sorted.into_iter().take(to_remove).collect();
let mut content = String::new();
let mut names = Vec::new();
for r in &removed {
content.push_str(&format!(
"Name: {}\nDesc: {}\nCode: {}\n",
r.name, r.description, r.code
));
names.push(r.name.clone());
}
Some((content, names))
} else {
None
}
});
if !condensed_snippet_content.is_empty() {
state.modify_graph(|graph| {
if let Some((content, names)) = snippet_condensation {
if !content.is_empty() {
let name = format!("Snippet History {}", now);
graph.entities.insert(
name.clone(),
crate::models::Entity {
name: name.clone(),
entity_type: "Historical Summary".to_string(),
observations: vec![condensed_snippet_content],
namespace: crate::models::default_namespace(),
git_branch: None,
..Default::default()
},
);
});
tracing::info!("Condensed snippets into Historical Summary.");
state.modify_graph(|graph| {
graph.entities.insert(
name.clone(),
crate::models::Entity {
name: name.clone(),
entity_type: "Historical Summary".to_string(),
observations: vec![content],
namespace: crate::models::default_namespace(),
git_branch: None,
..Default::default()
},
);
});
let name_set: std::collections::HashSet<String> = names.into_iter().collect();
state.code.snippets.modify(|snippets| {
snippets.retain(|s| !name_set.contains(&s.name));
});
tracing::info!("Condensed snippets into Historical Summary.");
}
}
}
}