Format massive JSON arrays into Markdown natively to avoid context blowouts

This commit is contained in:
Riz Ashraf committed 2026-10-10 18:45:00 +01:00
1 parent 760ebfcd40
commit 3ee95f5c39
4 files changed
+157 -42

No files matched your search

+76 -18
View File
@@ -498,15 +498,37 @@ impl McpTool for FindSymbolReferencesHandler {
if matches.len() < limit {
let remaining = limit - matches.len();
let target_sym_clone = target_sym.clone();
let disk_matches = tokio::task::spawn_blocking(move || {
scan_workspace_for_symbol(&target_sym, remaining, false, custom_dir)
scan_workspace_for_symbol(&target_sym_clone, remaining, false, custom_dir)
})
.await
.unwrap_or_default();
matches.extend(disk_matches);
}
Ok(serde_yaml::to_string(&matches)?)
let mut out = String::new();
out.push_str(&format!("## Symbol References for `{}`\n\n", target_sym));
for match_item in &matches {
if let Some(source) = match_item.get("source").and_then(|s| s.as_str()) {
if source == "snippet" {
let name = match_item.get("name").and_then(|n| n.as_str()).unwrap_or("Unknown");
let code = match_item.get("code").and_then(|c| c.as_str()).unwrap_or("");
out.push_str(&format!("### Snippet: {}\n```rust\n{}\n```\n\n", name, code));
}
} else {
let file = match_item.get("file_path").and_then(|f| f.as_str()).unwrap_or("Unknown");
let line = match_item.get("line").and_then(|l| l.as_u64()).unwrap_or(0);
let content = match_item.get("content").and_then(|c| c.as_str()).unwrap_or("");
out.push_str(&format!("- `{}:{}`: `{}`\n", file, line, content));
}
}
if matches.is_empty() {
out.push_str("No references found.\n");
}
Ok(out)
}
}
@@ -554,15 +576,37 @@ impl McpTool for GetCallersHandler {
if callers.len() < limit {
let remaining = limit - callers.len();
let target_fn_clone = target_fn.clone();
let disk_callers = tokio::task::spawn_blocking(move || {
scan_workspace_for_symbol(&target_fn, remaining, true, custom_dir)
scan_workspace_for_symbol(&target_fn_clone, remaining, true, custom_dir)
})
.await
.unwrap_or_default();
callers.extend(disk_callers);
}
Ok(serde_yaml::to_string(&callers)?)
let mut out = String::new();
out.push_str(&format!("## Callers for `{}`\n\n", target_fn));
for caller in &callers {
if let Some(source) = caller.get("source").and_then(|s| s.as_str()) {
if source == "snippet" {
let name = caller.get("name").and_then(|n| n.as_str()).unwrap_or("Unknown");
let code = caller.get("code").and_then(|c| c.as_str()).unwrap_or("");
out.push_str(&format!("### Snippet: {}\n```rust\n{}\n```\n\n", name, code));
}
} else {
let file = caller.get("file_path").and_then(|f| f.as_str()).unwrap_or("Unknown");
let line = caller.get("line").and_then(|l| l.as_u64()).unwrap_or(0);
let content = caller.get("content").and_then(|c| c.as_str()).unwrap_or("");
out.push_str(&format!("- `{}:{}`: `{}`\n", file, line, content));
}
}
if callers.is_empty() {
out.push_str("No callers found.\n");
}
Ok(out)
}
}
@@ -637,21 +681,35 @@ impl McpTool for AnalyzeImpactHandler {
"LOW"
};
let result = serde_json::json!({
"target_symbol": sym,
"risk_level": risk_level,
"ast_callers_count": caller_count,
"ast_callers_sample": callers.into_iter().take(10).collect::<Vec<_>>(),
"graph_relations_count": graph_count,
"graph_relations": kg_connected,
"recommendation": match risk_level {
"CRITICAL" | "HIGH" => "Requires comprehensive unit test verification and backwards compatibility checks before modifying.",
"MEDIUM" => "Verify direct call sites and run affected module tests.",
_ => "Safe to modify with standard unit test verification.",
}
});
let mut out = String::new();
out.push_str(&format!("## Impact Analysis for `{}`\n\n", sym));
out.push_str(&format!("**Risk Level:** {}\n\n", risk_level));
let rec = match risk_level {
"CRITICAL" | "HIGH" => "Requires comprehensive unit test verification and backwards compatibility checks before modifying.",
"MEDIUM" => "Verify direct call sites and run affected module tests.",
_ => "Safe to modify with standard unit test verification.",
};
out.push_str(&format!("**Recommendation:** {}\n\n", rec));
out.push_str(&format!("### AST Callers ({} total, showing up to 10)\n", caller_count));
for c in callers.into_iter().take(10) {
out.push_str(&format!("- `{}`\n", c));
}
if caller_count == 0 {
out.push_str("No callers found.\n");
}
out.push_str("\n");
out.push_str(&format!("### Graph Relations ({})\n", graph_count));
for g in kg_connected {
out.push_str(&format!("- {}\n", g));
}
if graph_count == 0 {
out.push_str("No graph relations found.\n");
}
Ok(serde_yaml::to_string(&result)?)
Ok(out)
}
}