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:
asepharyana
2026-07-17 09:08:41 +07:00
parent 1f0ae9f551
commit 9a67137954
139 changed files with 9704 additions and 8858 deletions
+10 -388
View File
@@ -1,384 +1,22 @@
//! Subagent execution loop: drive an LLM conversation, gate tool calls
//! against the context's allowlist, run tools, and stream progress events
//! to the parent via an mpsc channel.
//! Subagent execution loop: drive an LLM conversation, run tools, and stream
//! progress events to the parent via an mpsc channel.
//!
//! Security: subagent tool gating mirrors the main agent's `Harness` checks
//! (path traversal, reason validation, stub/denial/assumption scanning,
//! bash exfiltration and destructive-pattern detection) so that subagents
//! are not a weaker link than the main agent.
//! Tool gating and pattern-constant definitions live in sibling modules
//! (`gating`, `provider`, `tools`, `workspace`) rather than here, so each
//! concern is independently testable and maintainable.
use super::context::SubagentContext;
use super::event::SubagentEvent;
use super::gating::gate_subagent_tool_call;
use super::provider::{require_api_key, resolve_provider_config};
use super::tools::build_subagent_tools;
use super::workspace::generate_workspace_tree;
use crate::dto::chat::message::ChatMessage;
use crate::dto::provider::request::ToolDef;
use crate::tool::{all_tools, tool_defs, tool_is_risky};
use crate::tool::tool_is_risky;
use sha2::Digest;
use std::fmt::Write;
use tokio::sync::mpsc;
use zesdex_cms::domain::repository::AppConfigRepository;
use zesdex_cms::domain::repository::EditLogRepository;
use zesdex_cms::domain::repository::SettingsRepository;
/// Maps a subagent's allowed tool names to concrete Tool trait objects and
/// OpenAI-style tool definitions.
///
/// Flow: load `all_tools()` → if `allowed_tools` is empty, use all; else
/// filter by membership → derive `ToolDef`s for the LLM.
///
/// Why: an empty allowlist means "no restriction" (matches
/// `build_subagent_context`'s default for non-reviewer roles).
///
/// Return: `(tool impls, schema defs)` for the subagent to use.
fn build_subagent_tools(
allowed_tools: &[String],
) -> (Vec<Box<dyn crate::tool::Tool>>, Vec<ToolDef>) {
let all = all_tools();
let filtered: Vec<Box<dyn crate::tool::Tool>> = if allowed_tools.is_empty() {
all.into_iter()
.filter(|t| t.name() != "hive_mind" && t.name() != "workflow_run")
.collect()
} else {
all.into_iter()
.filter(|t| {
allowed_tools.contains(&t.name().to_string())
&& t.name() != "hive_mind"
&& t.name() != "workflow_run"
})
.collect()
};
let defs = tool_defs(&filtered);
(filtered, defs)
}
/// Resolve the API key, model, and base URL from persisted app config.
///
/// Flow: try the settings key for the active provider → fall back to the
/// provider's `api_key_env` env-var → fall back to the provider's
/// `default_api_key` → fall back to an empty string.
///
/// Why: matches the main agent's credential resolution exactly, so
/// subagents automatically inherit the same provider settings.
///
/// Return: `(api_key, model, optional_base_url, provider_name)`. `api_key`
/// is empty when every resolution path was exhausted — callers must check
/// for this before issuing requests (see `run_subagent`).
fn resolve_provider_config() -> (String, String, Option<String>, String) {
let store_base_dir = zesdex_entities::seaorm::common::store::Store::new().base_dir;
let settings =
zesdex_cms::infrastructure::persistence::settings_repo::JsonSettingsRepository::new()
.load(&store_base_dir)
.unwrap_or_default();
let app_config =
zesdex_cms::infrastructure::persistence::app_config_repo::JsonAppConfigRepository::new()
.load(&store_base_dir)
.unwrap_or_default();
let mut api_key = settings
.api_keys
.get(&settings.provider)
.cloned()
.unwrap_or_else(|| {
tracing::warn!(
"[subagent] no API key for provider '{}' in settings, trying env/default",
settings.provider
);
String::new()
});
let model = settings.model.clone();
let base_url = app_config
.providers
.get(&settings.provider)
.map(|p| p.api_base.clone());
if api_key.is_empty() {
if let Some(provider_cfg) = app_config.providers.get(&settings.provider) {
api_key = provider_cfg
.api_key_env
.as_ref()
.and_then(|env| std::env::var(env).ok())
.or_else(|| provider_cfg.default_api_key.clone())
.unwrap_or_else(|| {
tracing::warn!(
"[subagent] all API key resolution paths exhausted for '{}'",
settings.provider
);
String::new()
});
}
}
(api_key, model, base_url, settings.provider)
}
/// Reject an empty API key with an actionable error instead of letting the
/// caller send a request that is guaranteed to fail once it reaches the network.
///
/// Return: `Ok(())` if `api_key` is non-empty, `Err` with a message naming
/// `provider` and where to fix it otherwise.
fn require_api_key(api_key: &str, provider: &str) -> anyhow::Result<()> {
if api_key.is_empty() {
anyhow::bail!(
"no API key configured for provider '{provider}' — set one in Settings or ~/.claude/settings.json"
);
}
Ok(())
}
// ─── Subagent-level tool gating (mirrors Harness checks) ───
const STUB_PATTERNS: &[&str] = &[
"todo!()",
"todo!(",
"unimplemented!()",
"unimplemented!(",
"FIXME",
"fixme:",
"XXX:",
"PLACEHOLDER",
"REPLACE_ME",
"stub_value",
"stub_function",
"fake_response",
"fake_data",
"not implemented",
"not yet implemented",
"to be implemented",
"to be done",
];
const DENIAL_PATTERNS: &[&str] = &[
"// skip",
"// skipping",
"// skipping for now",
"// for now just",
"// punt",
"// hack:",
"// workaround:",
"// cba",
"// later",
"// do later",
"// ignore for now",
"// disable",
"// bypass",
"// quick fix",
"// temp fix",
"// temporary fix",
"// temp:",
"// temporary:",
"// noop",
];
const ASSUMPTION_PATTERNS: &[&str] = &[
"// assume",
"// probably",
"// guess",
"// should work",
"// hopefully",
"// i think",
"// should be fine",
"// likely",
];
const EXFIL_PATTERNS: &[&str] = &[
"curl ",
"wget ",
"nc -e ",
"ncat ",
"/dev/tcp/",
"base64 -d |",
"base64 --decode |",
"openssl s_client",
"ssh -R ",
"scp /",
"rsync /",
];
const SENSITIVE_PATH_PATTERNS: &[&str] = &[
".ssh/id_rsa",
".ssh/id_ed25519",
".aws/credentials",
".aws/config",
".kube/config",
".docker/config.json",
"/etc/shadow",
"/etc/passwd",
"/proc/self/environ",
];
const MIN_REASON_LEN: usize = 8;
/// Gate a tool call in the subagent context. Returns `Some(block_reason)` if
/// the call should be blocked, `None` to allow.
///
/// Flow: always blocks dangerous patterns — path traversal, stub/denial/
/// assumption language, bash exfiltration, destructive commands, sensitive
/// path reads — regardless of the allowed-tools list. Tools that are not
/// risky only get the basic allowlist check.
fn gate_subagent_tool_call(tool_name: &str, args: &serde_json::Value) -> Option<String> {
// File-mutating tools: write / edit / delete
if matches!(tool_name, "write" | "edit" | "delete") {
if let Some(path) = args.get("path").and_then(|v| v.as_str()) {
if path.contains("..") {
return Some("path traversal detected in 'path' argument".to_string());
}
}
}
// write / edit require a non-trivial `reason`
if matches!(tool_name, "write" | "edit" | "delete") {
let reason = args.get("reason").and_then(|v| v.as_str()).unwrap_or("");
if reason.trim().len() < MIN_REASON_LEN {
return Some(format!(
"{tool_name} requires a non-trivial 'reason' (>= {MIN_REASON_LEN} chars) explaining why",
));
}
}
// write / edit content must not contain stubs, denial, or assumption language
if matches!(tool_name, "write" | "edit") {
let content = match tool_name {
"write" => args.get("content").and_then(|v| v.as_str()).unwrap_or(""),
"edit" => {
let old = args.get("old").and_then(|v| v.as_str()).unwrap_or("");
let new = args.get("new").and_then(|v| v.as_str()).unwrap_or("");
// For edits, scanning old+new together catches stubs in both
return if contains_any(old, STUB_PATTERNS) || contains_any(new, STUB_PATTERNS) {
Some("content contains stub/placeholder pattern; production code must be fully implemented".to_string())
} else if contains_any(new, DENIAL_PATTERNS) {
Some("content contains denial/punt pattern; implement properly instead of skipping".to_string())
} else if contains_any(new, ASSUMPTION_PATTERNS) {
Some("content contains assumption pattern; verify against data instead of guessing".to_string())
} else {
return None;
};
}
_ => "",
};
if contains_any(content, STUB_PATTERNS) {
return Some("content contains stub/placeholder pattern; production code must be fully implemented".to_string());
}
if contains_any(content, DENIAL_PATTERNS) {
return Some(
"content contains denial/punt pattern; implement properly instead of skipping"
.to_string(),
);
}
if contains_any(content, ASSUMPTION_PATTERNS) {
return Some(
"content contains assumption pattern; verify against data instead of guessing"
.to_string(),
);
}
}
// Bash: exfiltration, sensitive paths, destructive commands
if tool_name == "bash" {
let cmd = args.get("command").and_then(|v| v.as_str()).unwrap_or("");
if cmd.contains("..") {
return Some("path traversal detected in bash command".to_string());
}
// Only check exfiltration for non-standard commands
let is_standard = cmd.trim_start().starts_with("cargo")
|| cmd.trim_start().starts_with("rustc")
|| cmd.trim_start().starts_with("git ")
|| cmd.trim_start().starts_with("ls")
|| cmd.trim_start().starts_with("pwd")
|| cmd.trim_start().starts_with("echo")
|| cmd.trim_start().starts_with("cat")
|| cmd.trim_start().starts_with("find")
|| cmd.trim_start().starts_with("grep")
|| cmd.trim_start().starts_with("test");
if !is_standard {
for pat in EXFIL_PATTERNS {
if cmd.contains(pat) {
return Some(format!(
"potential data-exfiltration command blocked (matched '{pat}')"
));
}
}
}
for pat in SENSITIVE_PATH_PATTERNS {
if cmd.contains(pat) {
return Some(format!("refused to read/write sensitive path '{pat}'"));
}
}
let dangerous = [
"rm -rf /",
"rm -rf --no-preserve-root",
"rm -rf ~",
"rm -fr /",
"mkfs.",
"dd if=",
":(){",
"> /dev/sda",
"chmod -R 000 /",
"shutdown ",
"poweroff ",
"reboot ",
"halt ",
];
for pat in &dangerous {
if cmd.contains(pat) {
return Some(format!("destructive command pattern blocked: {pat}"));
}
}
if contains_any(cmd, STUB_PATTERNS) {
return Some("bash command contains stub pattern".to_string());
}
}
// git_operator: require reason
if tool_name == "git_operator" {
let reason = args.get("reason").and_then(|v| v.as_str()).unwrap_or("");
if reason.trim().len() < MIN_REASON_LEN {
return Some("git_operator requires a non-trivial 'reason' (>= 8 chars)".to_string());
}
}
None
}
/// Check if `text` matches any pattern (case-insensitive substring).
fn contains_any(text: &str, patterns: &[&str]) -> bool {
let lower = text.to_lowercase();
patterns.iter().any(|p| lower.contains(&p.to_lowercase()))
}
/// Build an ASCII tree of the workspace directory structure for the
/// system prompt, so the LLM can see the file layout.
///
/// Flow: for each root, walk using `ignore::WalkBuilder` (respecting
/// `.gitignore` and hidden files) → prefix `[DIR]` for directories →
/// truncate after 1000 entries.
fn generate_workspace_tree(roots: &[std::path::PathBuf]) -> String {
let mut out = String::new();
out.push_str("Current Workspace Directory Structure:\n");
for root in roots {
writeln!(out, "Root: {}", root.display()).unwrap();
let walker = ignore::WalkBuilder::new(root)
.hidden(true)
.git_ignore(true)
.build();
let mut count = 0;
for entry in walker.flatten() {
let path = entry.path();
if let Ok(rel) = path.strip_prefix(root) {
if rel.as_os_str().is_empty() {
continue;
}
let is_dir = entry.file_type().is_some_and(|ft| ft.is_dir());
let prefix = if is_dir { "[DIR] " } else { " " };
writeln!(out, " {}{}", prefix, rel.display()).unwrap();
count += 1;
if count > 1000 {
out.push_str(" ... (truncated)\n");
break;
}
}
}
}
out
}
fn format_subagent_progress(prefix: &str, text: &str) -> String {
let lines: Vec<&str> = text.lines().filter(|l| !l.trim().is_empty()).collect();
@@ -786,19 +424,3 @@ pub fn run_subagent(
let _ = tx.blocking_send(SubagentEvent::Completed);
Ok(output)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn require_api_key_rejects_empty_key_with_provider_named_in_message() {
let err = require_api_key("", "claude").unwrap_err();
assert!(err.to_string().contains("claude"));
}
#[test]
fn require_api_key_accepts_non_empty_key() {
assert!(require_api_key("sk-live-abc123", "claude").is_ok());
}
}