refactor: apply 5-pass audit optimizations across mcp-memory codebase

This commit is contained in:
Riz Ashraf committed 2026-10-06 06:05:38 +01:00
1 parent 924b6d09fa
commit 5bd8b1587a
43 files changed
+1866 -1658

No files matched your search

+21 -8
View File
@@ -4,16 +4,18 @@ use std::path::Path;
use std::sync::Arc;
use tracing::{error, info};
pub fn spawn_watcher(_state: Arc<MemoryState>) {
pub fn spawn_watcher(state: Arc<MemoryState>) {
let watch_path = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));
info!("Spawning proactive daemon watcher on {:?}", watch_path);
tokio::spawn(async move {
let (tx, mut rx) = tokio::sync::mpsc::channel::<notify::Result<Event>>(100);
let (tx, mut rx) = tokio::sync::mpsc::channel::<notify::Result<Event>>(500);
let mut watcher = match RecommendedWatcher::new(
move |res| {
let _ = tx.try_send(res);
if let Err(e) = tx.try_send(res) {
tracing::warn!("Watcher event dropped due to channel backpressure: {}", e);
}
},
Config::default(),
) {
@@ -29,14 +31,26 @@ pub fn spawn_watcher(_state: Arc<MemoryState>) {
return;
}
let mut last_processed: std::collections::HashMap<std::path::PathBuf, std::time::Instant> =
std::collections::HashMap::new();
while let Some(res) = rx.recv().await {
match res {
Ok(event) => {
if event.kind.is_modify() {
let now = std::time::Instant::now();
for path in event.paths {
if should_review(&path) {
// 250ms debouncing window per file path
if let Some(last) = last_processed.get(&path) {
if now.duration_since(*last) < std::time::Duration::from_millis(250) {
continue;
}
}
last_processed.insert(path.clone(), now);
info!("Proactive Daemon Hooks: File modified: {:?}", path);
trigger_autonomous_review(&path).await;
trigger_autonomous_review(&path, Arc::clone(&state)).await;
}
}
}
@@ -49,7 +63,7 @@ pub fn spawn_watcher(_state: Arc<MemoryState>) {
fn should_review(path: &Path) -> bool {
let path_str = path.to_string_lossy();
if path_str.contains(".git") || path_str.contains("target") {
if path_str.contains(".git") || path_str.contains("target") || path_str.contains(".gemini") || path_str.contains("node_modules") {
return false;
}
@@ -62,10 +76,9 @@ fn should_review(path: &Path) -> bool {
false
}
async fn trigger_autonomous_review(path: &Path) {
async fn trigger_autonomous_review(path: &Path, state: Arc<MemoryState>) {
info!("Triggering autonomous review for {:?}", path);
// Simulate invoking a Neovim Lua diagnostic via mcp-memory-win-nvim daemon
// MVP: Just write an info log if Neovim isn't reachable.
state.broadcast_activity("AUTONOMOUS", &format!("Modified: {:?}", path.file_name().unwrap_or_default()));
info!("Autonomous review complete for {:?}", path);
}