refactor: address 5-pass audit findings for antipatterns, bottlenecks, memory efficiency, and LLM handlers
This commit is contained in:
1 parent
626403900f
commit
924b6d09fa
30 files changed
+1120
-503
No files matched your search
+36
-29
@@ -72,7 +72,9 @@ pub struct MemoryState {
|
||||
impl MemoryState {
|
||||
pub fn new(base_dir_str: &str) -> Self {
|
||||
let base = std::path::PathBuf::from(base_dir_str);
|
||||
std::fs::create_dir_all(&base).expect("Failed to create store dir");
|
||||
if let Err(e) = std::fs::create_dir_all(&base) {
|
||||
tracing::error!("Failed to create store directory at {:?}: {}", base, e);
|
||||
}
|
||||
|
||||
let db = crate::db::init_redb(&base);
|
||||
|
||||
@@ -93,7 +95,7 @@ impl MemoryState {
|
||||
.join(".gemini/mcp_memory/daemon_error.log");
|
||||
let _ =
|
||||
std::fs::write(&log_path, format!("Failed to create MemoryIndex: {}\n", e));
|
||||
std::process::exit(1);
|
||||
crate::search::MemoryIndex::new_in_ram().expect("Failed to create RAM MemoryIndex")
|
||||
}
|
||||
}),
|
||||
vector_db: tokio::sync::RwLock::new(None),
|
||||
@@ -174,7 +176,7 @@ impl MemoryState {
|
||||
}
|
||||
|
||||
pub async fn rebuild_index(self: &Arc<Self>) {
|
||||
let idx = self.search_index.read().unwrap().clone();
|
||||
let idx = self.get_search_index();
|
||||
idx.delete_all();
|
||||
|
||||
let entities: Vec<_> = self
|
||||
@@ -242,14 +244,16 @@ impl MemoryState {
|
||||
}
|
||||
});
|
||||
|
||||
let payload = serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"method": "notifications/activity",
|
||||
"params": activity
|
||||
})
|
||||
.to_string();
|
||||
if self.activity_tx.receiver_count() > 0 {
|
||||
let payload = serde_json::json!({
|
||||
"jsonrpc": "2.0",
|
||||
"method": "notifications/activity",
|
||||
"params": activity
|
||||
})
|
||||
.to_string();
|
||||
|
||||
let _ = self.activity_tx.send(payload);
|
||||
let _ = self.activity_tx.send(payload);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn broadcast_task_event(&self, event: TaskEvent) {
|
||||
@@ -460,27 +464,30 @@ impl SearchService {
|
||||
}
|
||||
|
||||
if !vdb_search {
|
||||
let mut texts_to_embed = Vec::new();
|
||||
let mut metadata = Vec::new();
|
||||
let (mut texts_to_embed, mut metadata) = self.state.code.snippets.read_with(|snips| {
|
||||
let mut texts = Vec::with_capacity(snips.len().min(50));
|
||||
let mut meta = Vec::with_capacity(snips.len().min(50));
|
||||
for snippet in snips.iter().take(50) {
|
||||
texts.push(format!("{} {} {}", snippet.name, snippet.description, snippet.code));
|
||||
meta.push((snippet.name.clone(), "snippet".to_string(), snippet.description.clone()));
|
||||
}
|
||||
(texts, meta)
|
||||
});
|
||||
|
||||
let snippets = self.state.code.snippets.read_with(|snips| snips.clone());
|
||||
for snippet in snippets {
|
||||
let combined = format!("{} {} {}", snippet.name, snippet.description, snippet.code);
|
||||
texts_to_embed.push(combined);
|
||||
metadata.push((snippet.name, "snippet".to_string(), snippet.description));
|
||||
}
|
||||
self.state.code.sticky.read_with(|sticky| {
|
||||
for note in sticky.iter().take(50) {
|
||||
texts_to_embed.push(note.content.clone());
|
||||
metadata.push((
|
||||
"StickyNote".to_string(),
|
||||
"sticky".to_string(),
|
||||
note.content.chars().take(200).collect::<String>(),
|
||||
));
|
||||
}
|
||||
});
|
||||
|
||||
let sticky = self.state.code.sticky.read_with(|s| s.clone());
|
||||
for note in sticky {
|
||||
texts_to_embed.push(note.content.clone());
|
||||
metadata.push((
|
||||
"StickyNote".to_string(),
|
||||
"sticky".to_string(),
|
||||
note.content.chars().take(200).collect::<String>(),
|
||||
));
|
||||
}
|
||||
|
||||
if let Ok(embeddings) = generate_embeddings_async(texts_to_embed).await {
|
||||
if !texts_to_embed.is_empty()
|
||||
&& let Ok(embeddings) = generate_embeddings_async(texts_to_embed).await
|
||||
{
|
||||
for (emb, meta) in embeddings.into_iter().zip(metadata) {
|
||||
let sim = cosine_similarity(&query_emb, &emb);
|
||||
results.push(UnifiedSearchResult {
|
||||
|
||||
Reference in new issue
Block a user