refactor: massive codebase restructuring — naming, splitting, DRY
Crate renames: - zesdex-entities::seaorm → domain (misleading name, no SeaORM used) - zesdex-dto → merged into zesdex-entities (100% re-exports) - zesdex-libs → zesdex-infra (vague name) Module renames: - app/harness → guard (misleading: safety gatekeeper, not test harness) - runtime/commands → action_dispatch (name clashed with controller/command) - resources → prompts (embedded prompt text, not general resources) - tool/seqthink → sequential_think (unreadable abbreviation) - msglog/query → insert (module only inserts, never queries) Dead code removal: - app/mode/help.rs (orphaned — not declared in mod.rs) - app/mode/loading.rs (orphaned — not declared in mod.rs) File splitting (71 new files, avg ~115 lines/file): - app/runtime/actions/: 1→8 files (was 2030 lines) - view/overlays/: 1→16 files (was 1167 lines) - tool/lsp/: 1→8 per-tool files (was 909 lines) - main.rs: 1→5 files (session, daemon, attach, event_loop) - workflow/engine + hive_mind: 2→10 files - subagent/engine + auto: 2→9 files - lsp/provisioner: 1→5 files - review/: 1→6 files - guard/: 1→2 files (extracted patterns) - state/misc: 1→3 files (input, scroll) - mcp/: 1→3 files (transport, adapter) - stream/json_repair extracted from turn.rs DRY: - Pattern constants (STUB_PATTERNS etc) in guard/patterns shared with subagent - 3 near-identical background spawners → 1 generic + thin wrappers - Shared spawn_subagent_with_drain() extracted - Shared create_session() in main - write_osc52 deduplicated Bug fixes: - archive_message(): sess.db → db (wrong variable name) - execute_one_tool(): wrong parameter name - check_credential_read() function was missing (restored from test expectations)
This commit is contained in:
@@ -3,47 +3,13 @@
|
||||
//! the crate's `Tool` trait.
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{json, Value};
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
const MCP_CONNECT_TIMEOUT_MS: u64 = 20_000;
|
||||
const MCP_CALL_TIMEOUT_MS: u64 = 60_000;
|
||||
use super::transport::{call_via_http, call_via_stdio, mcp_static_str, spawn_stdio_child};
|
||||
|
||||
/// Global cache for `&'static str` names/descriptions of MCP tools, so we
|
||||
/// never need `Box::leak`. Entries are never removed (small, bounded by the
|
||||
/// number of MCP tools ever registered in a session).
|
||||
fn mcp_static_str(s: &str) -> &'static str {
|
||||
static CACHE: OnceLock<Mutex<Vec<&'static str>>> = OnceLock::new();
|
||||
let mut cache = match CACHE.get_or_init(|| Mutex::new(Vec::new())).lock() {
|
||||
Ok(c) => c,
|
||||
Err(poisoned) => {
|
||||
tracing::warn!("[mcp] static string cache mutex poisoned, recovering");
|
||||
poisoned.into_inner()
|
||||
}
|
||||
};
|
||||
if let Some(&existing) = cache.iter().find(|e| **e == s) {
|
||||
return existing;
|
||||
}
|
||||
let leaked: &'static str = Box::leak(s.to_string().into_boxed_str());
|
||||
cache.push(leaked);
|
||||
leaked
|
||||
}
|
||||
|
||||
/// How an MCP server is reached: a spawned child process talking
|
||||
/// newline-delimited JSON-RPC over stdio, or a remote HTTP endpoint.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum McpTransport {
|
||||
Stdio { command: String, args: Vec<String> },
|
||||
StreamableHttp { url: String },
|
||||
}
|
||||
|
||||
/// A single tool advertised by an MCP server, as returned by `tools/list`.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct McpToolInfo {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub input_schema: Value,
|
||||
}
|
||||
// ---------------------------------------------------------------------------
|
||||
// MCP server descriptor
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// A connected MCP server: its transport, advertised tools, and (for stdio)
|
||||
/// a live handle to the child process.
|
||||
@@ -59,311 +25,9 @@ pub struct McpServer {
|
||||
pub child_handle: Option<Arc<Mutex<StdioChild>>>,
|
||||
}
|
||||
|
||||
/// Live handle to an MCP server child process communicating over stdio
|
||||
/// via newline-delimited JSON-RPC 2.0.
|
||||
#[derive(Debug)]
|
||||
pub struct StdioChild {
|
||||
stdin: std::process::ChildStdin,
|
||||
stdout: BufReader<std::process::ChildStdout>,
|
||||
next_id: u64,
|
||||
}
|
||||
|
||||
impl StdioChild {
|
||||
/// Send a JSON-RPC request to the child and block for its matching response.
|
||||
///
|
||||
/// Flow: assign the next request id → write request + newline to stdin →
|
||||
/// loop reading lines from stdout until one has a matching `id` or the
|
||||
/// timeout elapses → return its `result` (or error out on an `error` field).
|
||||
///
|
||||
/// Why: the child may interleave unrelated/malformed lines, so blank
|
||||
/// lines are skipped and non-matching ids are ignored rather than
|
||||
/// treated as a protocol violation.
|
||||
///
|
||||
/// Return: the `result` value of the matching response, or `Err` on
|
||||
/// timeout, EOF, JSON-RPC error, or I/O failure.
|
||||
pub fn call(&mut self, method: &str, params: &Value) -> anyhow::Result<Value> {
|
||||
const MAX_LINE_LENGTH: usize = 1_048_576; // 1 MiB
|
||||
self.next_id += 1;
|
||||
let id = self.next_id;
|
||||
let req = json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": id,
|
||||
"method": method,
|
||||
"params": params
|
||||
});
|
||||
let mut line = serde_json::to_string(&req)?;
|
||||
line.push('\n');
|
||||
self.stdin.write_all(line.as_bytes())?;
|
||||
self.stdin.flush()?;
|
||||
|
||||
let mut response_line = String::new();
|
||||
let deadline =
|
||||
std::time::Instant::now() + std::time::Duration::from_millis(MCP_CALL_TIMEOUT_MS);
|
||||
loop {
|
||||
if std::time::Instant::now() > deadline {
|
||||
anyhow::bail!("MCP call timed out after {MCP_CALL_TIMEOUT_MS}ms");
|
||||
}
|
||||
// Read one byte at a time up to MAX_LINE_LENGTH to prevent
|
||||
// OOM from a malicious server (CWE-400). BufReader already
|
||||
// buffers reads, so byte-by-byte over a buffered reader is
|
||||
// cheap (hits the in-memory buffer).
|
||||
response_line.clear();
|
||||
let mut line_truncated = false;
|
||||
loop {
|
||||
let byte = match self.stdout.fill_buf() {
|
||||
Ok([]) => {
|
||||
// EOF without newline
|
||||
anyhow::bail!("MCP stdio child process closed unexpectedly");
|
||||
}
|
||||
Ok(buf) => {
|
||||
let b = buf[0];
|
||||
self.stdout.consume(1);
|
||||
b
|
||||
}
|
||||
Err(e) => anyhow::bail!("MCP stdio read error: {e}"),
|
||||
};
|
||||
if byte == b'\n' {
|
||||
break;
|
||||
}
|
||||
if response_line.len() >= MAX_LINE_LENGTH {
|
||||
line_truncated = true;
|
||||
// Consume rest of line to keep stream in sync
|
||||
loop {
|
||||
let buf = self
|
||||
.stdout
|
||||
.fill_buf()
|
||||
.map_err(|e| anyhow::anyhow!("MCP stdio read error: {e}"))?;
|
||||
if buf.is_empty() {
|
||||
anyhow::bail!("MCP stdio child closed mid-line");
|
||||
}
|
||||
if buf[0] == b'\n' {
|
||||
self.stdout.consume(1);
|
||||
break;
|
||||
}
|
||||
self.stdout.consume(1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
response_line.push(byte as char);
|
||||
}
|
||||
if line_truncated {
|
||||
anyhow::bail!("MCP response line exceeded {MAX_LINE_LENGTH} byte limit");
|
||||
}
|
||||
let trimmed = response_line.trim();
|
||||
if trimmed.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let resp: Value = serde_json::from_str(trimmed)
|
||||
.map_err(|e| anyhow::anyhow!("invalid JSON from MCP server: {e}"))?;
|
||||
if resp.get("id") == Some(&json!(id)) {
|
||||
if let Some(err) = resp.get("error") {
|
||||
anyhow::bail!("MCP error: {err}");
|
||||
}
|
||||
return Ok(resp.get("result").cloned().unwrap_or_else(|| {
|
||||
tracing::warn!("[mcp] stdio response missing 'result' field: {}", trimmed);
|
||||
Value::Null
|
||||
}));
|
||||
}
|
||||
}
|
||||
} // close fn call
|
||||
} // close impl StdioChild
|
||||
|
||||
pub(crate) fn spawn_stdio_child(
|
||||
command: &str,
|
||||
extra_args: &[String],
|
||||
) -> anyhow::Result<StdioChild> {
|
||||
let parts: Vec<&str> = command.split_whitespace().collect();
|
||||
let (prog, prog_args) = parts
|
||||
.split_first()
|
||||
.ok_or_else(|| anyhow::anyhow!("MCP stdio command is empty"))?;
|
||||
|
||||
let mut cmd = std::process::Command::new(prog);
|
||||
cmd.args(prog_args);
|
||||
cmd.args(extra_args);
|
||||
cmd.stdin(std::process::Stdio::piped());
|
||||
cmd.stdout(std::process::Stdio::piped());
|
||||
// Pipe stderr so diagnostics from MCP servers are surfaced via tracing
|
||||
// rather than discarded silently, making connectivity issues debugable.
|
||||
cmd.stderr(std::process::Stdio::piped());
|
||||
|
||||
let mut child = cmd
|
||||
.spawn()
|
||||
.map_err(|e| anyhow::anyhow!("failed to spawn MCP stdio server '{command}': {e}"))?;
|
||||
|
||||
let stdin = child
|
||||
.stdin
|
||||
.take()
|
||||
.ok_or_else(|| anyhow::anyhow!("failed to get stdin for MCP server"))?;
|
||||
let stdout = child
|
||||
.stdout
|
||||
.take()
|
||||
.ok_or_else(|| anyhow::anyhow!("failed to get stdout for MCP server"))?;
|
||||
|
||||
let mut mcp = StdioChild {
|
||||
stdin,
|
||||
stdout: BufReader::new(stdout),
|
||||
next_id: 0,
|
||||
};
|
||||
|
||||
let deadline =
|
||||
std::time::Instant::now() + std::time::Duration::from_millis(MCP_CONNECT_TIMEOUT_MS);
|
||||
|
||||
let init_result = mcp.call(
|
||||
"initialize",
|
||||
&json!({
|
||||
"protocolVersion": "2024-11-05",
|
||||
"capabilities": {},
|
||||
"clientInfo": {
|
||||
"name": "zesdex",
|
||||
"version": "0.1.0"
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
if std::time::Instant::now() > deadline {
|
||||
anyhow::bail!("MCP initialize timed out");
|
||||
}
|
||||
|
||||
init_result.map_err(|e| anyhow::anyhow!("MCP initialize failed: {e}"))?;
|
||||
|
||||
let _ = mcp.call("notifications/initialized", &json!({}));
|
||||
|
||||
Ok(mcp)
|
||||
}
|
||||
|
||||
fn call_via_stdio(
|
||||
existing_handle: Option<&Mutex<StdioChild>>,
|
||||
command: &str,
|
||||
extra_args: &[String],
|
||||
tool_name: &str,
|
||||
tool_args: &Value,
|
||||
) -> anyhow::Result<String> {
|
||||
// Reuse the persistent child handle if available; otherwise spawn a new one.
|
||||
let mut guard;
|
||||
let child: &mut StdioChild = if let Some(mtx) = existing_handle {
|
||||
guard = mtx
|
||||
.lock()
|
||||
.map_err(|e| anyhow::anyhow!("MCP handle lock: {e}"))?;
|
||||
&mut guard
|
||||
} else {
|
||||
let mut fresh = spawn_stdio_child(command, extra_args)?;
|
||||
let result = fresh.call(
|
||||
"tools/call",
|
||||
&json!({
|
||||
"name": tool_name,
|
||||
"arguments": tool_args
|
||||
}),
|
||||
)?;
|
||||
return Ok(extract_text_content(&result));
|
||||
};
|
||||
|
||||
let result = child.call(
|
||||
"tools/call",
|
||||
&json!({
|
||||
"name": tool_name,
|
||||
"arguments": tool_args
|
||||
}),
|
||||
)?;
|
||||
|
||||
Ok(extract_text_content(&result))
|
||||
}
|
||||
|
||||
fn call_via_http(url: &str, tool_name: &str, tool_args: &Value) -> anyhow::Result<String> {
|
||||
let client = reqwest::blocking::Client::builder()
|
||||
.timeout(std::time::Duration::from_millis(MCP_CALL_TIMEOUT_MS))
|
||||
.connect_timeout(std::time::Duration::from_millis(MCP_CONNECT_TIMEOUT_MS))
|
||||
.build()
|
||||
.unwrap_or_else(|e| {
|
||||
tracing::warn!(
|
||||
"[mcp] HTTP client builder failed with connect timeout: {}. \
|
||||
retrying without connect timeout",
|
||||
e,
|
||||
);
|
||||
reqwest::blocking::Client::builder()
|
||||
.timeout(std::time::Duration::from_millis(MCP_CALL_TIMEOUT_MS))
|
||||
.build()
|
||||
.unwrap_or_else(|e2| {
|
||||
tracing::warn!(
|
||||
"[mcp] also failed: {}. using default client (no configured timeouts)",
|
||||
e2,
|
||||
);
|
||||
reqwest::blocking::Client::new()
|
||||
})
|
||||
});
|
||||
|
||||
let request_id: u64 = 1;
|
||||
let body = json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": request_id,
|
||||
"method": "tools/call",
|
||||
"params": {
|
||||
"name": tool_name,
|
||||
"arguments": tool_args
|
||||
}
|
||||
});
|
||||
|
||||
let resp = client
|
||||
.post(url)
|
||||
.header("Content-Type", "application/json")
|
||||
.json(&body)
|
||||
.send()
|
||||
.map_err(|e| anyhow::anyhow!("MCP HTTP request failed: {e}"))?;
|
||||
|
||||
if !resp.status().is_success() {
|
||||
let status = resp.status();
|
||||
let text = resp.text().unwrap_or_else(|e| {
|
||||
tracing::warn!("[mcp] failed to read HTTP response body: {}", e);
|
||||
String::new()
|
||||
});
|
||||
anyhow::bail!("MCP HTTP server returned {status}: {text}");
|
||||
}
|
||||
|
||||
let response: Value = resp
|
||||
.json()
|
||||
.map_err(|e| anyhow::anyhow!("invalid JSON from MCP HTTP server: {e}"))?;
|
||||
|
||||
if let Some(err) = response.get("error") {
|
||||
anyhow::bail!("MCP HTTP error: {err}");
|
||||
}
|
||||
|
||||
let result = response.get("result").cloned().unwrap_or_else(|| {
|
||||
tracing::warn!("[mcp] HTTP response missing 'result' field");
|
||||
Value::Null
|
||||
});
|
||||
Ok(extract_text_content(&result))
|
||||
}
|
||||
|
||||
fn extract_text_content(result: &Value) -> String {
|
||||
if let Some(content) = result.get("content") {
|
||||
if let Some(arr) = content.as_array() {
|
||||
let text: Vec<String> = arr
|
||||
.iter()
|
||||
.filter_map(|item| {
|
||||
if item.get("type").and_then(|t| t.as_str()) == Some("text") {
|
||||
item.get("text")
|
||||
.and_then(|t| t.as_str())
|
||||
.map(std::string::ToString::to_string)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
if !text.is_empty() {
|
||||
return text.join("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
serde_json::to_string_pretty(result).unwrap_or_else(|e| {
|
||||
tracing::warn!("[mcp] failed to pretty-print result: {}", e);
|
||||
result.to_string()
|
||||
})
|
||||
}
|
||||
|
||||
/// Registry of connected MCP servers and their tools for the current session.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct McpManager {
|
||||
pub servers: Vec<McpServer>,
|
||||
}
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tool adapter
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Adapts a single MCP-advertised tool to the crate's `Tool` trait so it can
|
||||
/// be dispatched through the same execution path as built-in tools.
|
||||
@@ -407,6 +71,16 @@ impl crate::tool::Tool for McpToolAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Manager
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Registry of connected MCP servers and their tools for the current session.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct McpManager {
|
||||
pub servers: Vec<McpServer>,
|
||||
}
|
||||
|
||||
impl McpManager {
|
||||
/// Create an empty manager with no connected servers.
|
||||
pub fn new() -> Self {
|
||||
@@ -505,3 +179,9 @@ impl McpManager {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Re-exports
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub use super::transport::{McpTransport, McpToolInfo, StdioChild};
|
||||
|
||||
Reference in New Issue
Block a user