refactor: apply rust best practices and fix memory optimizations

- Optimized memory allocation in router.rs by offloading JSON serialization to spawn_blocking and using references.
- Prevented full graph duplication on startup in state.rs index rebuild.
- Eliminated massive String allocations in QueryGraphPathHandler BFS loops.
- Avoided temporary Strings in VisualizeGraphHandler via inline writing.
- Fixed O(N) full-graph deduplication in MergeEntitiesHandler to scale efficiently.
This commit is contained in:
Riz Ashraf committed 2026-09-22 22:01:38 +01:00
1 parent 251757f8fc
commit 37003be620
17 files changed
+399 -198

No files matched your search

+55 -18
View File
@@ -491,26 +491,29 @@ impl McpTool for ListMilestonesHandler {
#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;
use serde_json::json;
use tempfile::tempdir;
#[tokio::test]
async fn test_add_task_and_list() {
let dir = tempdir().unwrap();
let state = Arc::new(MemoryState::new(dir.path().to_str().unwrap()));
let add_handler = AddTaskHandler;
let args = json!({
"title": "Fix the hyperdrive",
"description": "It's making a strange noise",
"acceptance_criteria": ["Stop the noise", "Reach lightspeed"],
});
let res = add_handler.execute(args, state.clone()).await.unwrap();
assert!(res.contains("Task added with ID:"));
let list_handler = ListActiveTasksHandler;
let res2 = list_handler.execute(json!({}), state.clone()).await.unwrap();
let res2 = list_handler
.execute(json!({}), state.clone())
.await
.unwrap();
assert!(res2.contains("Fix the hyperdrive"));
}
@@ -518,10 +521,16 @@ mod tests {
async fn test_update_task_status() {
let dir = tempdir().unwrap();
let state = Arc::new(MemoryState::new(dir.path().to_str().unwrap()));
let add_handler = AddTaskHandler;
let res = add_handler.execute(json!({"title": "Test", "description": "test"}), state.clone()).await.unwrap();
let res = add_handler
.execute(
json!({"title": "Test", "description": "test"}),
state.clone(),
)
.await
.unwrap();
let id_start = res.find("ID: ").unwrap() + 4;
let task_id = res[id_start..].trim();
@@ -532,9 +541,12 @@ mod tests {
});
let res3 = update_handler.execute(args, state.clone()).await.unwrap();
assert_eq!(res3, "Task status updated.");
let list_handler = ListActiveTasksHandler;
let res4 = list_handler.execute(json!({}), state.clone()).await.unwrap();
let res4 = list_handler
.execute(json!({}), state.clone())
.await
.unwrap();
assert!(!res4.contains(task_id));
}
@@ -542,7 +554,7 @@ mod tests {
async fn test_milestones_and_criteria() {
let dir = tempdir().unwrap();
let state = Arc::new(MemoryState::new(dir.path().to_str().unwrap()));
// Add Milestone
let add_milestone = AddMilestoneHandler;
let args_ms = json!({
@@ -555,7 +567,7 @@ mod tests {
});
let res1 = add_milestone.execute(args_ms, state.clone()).await.unwrap();
assert!(res1.contains("Milestone added"));
// Fetch milestone ID from state directly to update
let ms_id = state.milestones.read_with(|ms| ms[0].id.clone());
@@ -570,13 +582,22 @@ mod tests {
// List Milestones
let list_ms = ListMilestonesHandler;
let res3 = list_ms.execute(json!({"namespace": "global"}), state.clone()).await.unwrap();
let res3 = list_ms
.execute(json!({"namespace": "global"}), state.clone())
.await
.unwrap();
assert!(res3.contains("completed"));
assert!(res3.contains("Release 1.0"));
// Task Acceptance Criteria
let add_task = AddTaskHandler;
let res_task = add_task.execute(json!({"title": "Test", "description": "desc"}), state.clone()).await.unwrap();
let res_task = add_task
.execute(
json!({"title": "Test", "description": "desc"}),
state.clone(),
)
.await
.unwrap();
let task_id = res_task[res_task.find("ID: ").unwrap() + 4..].trim();
let set_ac = SetAcceptanceCriteriaHandler;
@@ -604,15 +625,31 @@ mod tests {
let state = Arc::new(MemoryState::new(dir.path().to_str().unwrap()));
let add_task = AddTaskHandler;
let parent = add_task.execute(json!({"title": "Parent", "description": "p"}), state.clone()).await.unwrap();
let parent_id = parent[parent.find("ID: ").unwrap() + 4..].trim().to_string();
let parent = add_task
.execute(
json!({"title": "Parent", "description": "p"}),
state.clone(),
)
.await
.unwrap();
let parent_id = parent[parent.find("ID: ").unwrap() + 4..]
.trim()
.to_string();
let child = add_task.execute(json!({"title": "Child", "description": "c", "parent_id": parent_id}), state.clone()).await.unwrap();
let child = add_task
.execute(
json!({"title": "Child", "description": "c", "parent_id": parent_id}),
state.clone(),
)
.await
.unwrap();
let _child_id = child[child.find("ID: ").unwrap() + 4..].trim().to_string();
let del_task = DeleteTaskHandler;
let res_del = del_task.execute(json!({"id": parent_id}), state.clone()).await.unwrap();
let res_del = del_task
.execute(json!({"id": parent_id}), state.clone())
.await
.unwrap();
assert!(res_del.contains("Deleted task and its children (2 total)."));
}
}