Refactor and clean up code across multiple modules

- Simplified token type assignment in OAuth service.
- Removed unused session_lock module and re-exported Session from zesdex_entities.
- Cleaned up session entity by removing unnecessary comments and code.
- Consolidated session handling in HTTP handlers for better readability.
- Improved formatting and readability in OAuth repository tests.
- Enhanced session lock repository with clearer match statements.
- Streamlined session repository error handling.
- Refined RNG tests for better clarity.
- Adjusted module visibility and organization in lib.rs.
- Updated IPC client and connection code for better error handling and clarity.
- Improved frame handling in IPC for better readability.
- Organized module imports and added test utilities for IPC.
- Enhanced database connection error handling.
- Simplified JWT token creation error handling.
- Improved password verification error handling.
- Cleaned up state management code for better readability.
- Refactored middleware for session authentication and rate limiting.
- Simplified clipboard utility for better error handling.
- Enhanced logging initialization for better error reporting.
- Improved pagination utility with clearer method annotations.
- Cleaned up sanitization functions for filenames and paths.
- Enhanced slug generation functions for better clarity and usability.
This commit is contained in:
asepharyana
2026-07-17 09:08:41 +07:00
parent 22dd6fdda7
commit 1f0ae9f551
95 changed files with 1792 additions and 2131 deletions
@@ -54,9 +54,15 @@ pub fn get_learning_items(state: &AppStateRest) -> Vec<LearningItem> {
}
// 2. Load stored memory lessons from long-term memory directory
let names = zesdex_cms::infrastructure::persistence::memory_repo::MarkdownMemoryRepository::new().list(&state.memory_dir).unwrap_or_default();
let names =
zesdex_cms::infrastructure::persistence::memory_repo::MarkdownMemoryRepository::new()
.list(&state.memory_dir)
.unwrap_or_default();
for name in names {
if let Ok(mem) = zesdex_cms::infrastructure::persistence::memory_repo::MarkdownMemoryRepository::new().load(&state.memory_dir, &name) {
if let Ok(mem) =
zesdex_cms::infrastructure::persistence::memory_repo::MarkdownMemoryRepository::new()
.load(&state.memory_dir, &name)
{
if mem.kind == "lesson" {
items.push(LearningItem::Stored {
name: mem.name,
+2 -1
View File
@@ -101,7 +101,8 @@ pub fn rewind_to(state: &mut AppStateRest, index: usize) {
}
// Log the rewind itself as an edit entry
let repo = zesdex_cms::infrastructure::persistence::edit_log_repo::JsonlEditLogRepository::new();
let repo =
zesdex_cms::infrastructure::persistence::edit_log_repo::JsonlEditLogRepository::new();
if let Ok(mut el) = repo.open(&state.session_dir) {
let entry = zesdex_cms::domain::edit_log::EditLogEntry {
ts: chrono::Utc::now().timestamp_millis(),
+129 -48
View File
@@ -1,7 +1,11 @@
#![allow(clippy::cast_possible_truncation, clippy::cast_sign_loss, clippy::cast_precision_loss, clippy::cast_possible_wrap)]
#![allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_precision_loss,
clippy::cast_possible_wrap
)]
//! Adaptive quality-review triggering, build/test probing, staleness
//! sweeps for stored lessons, and the pending-lesson approval workflow.
use std::process::Command;
use crate::app::state::rest::AppStateRest;
use crate::app::state::runtime::TurnEvent;
use crate::app::state::types::{Origin, Toast, ToastKind};
@@ -9,6 +13,7 @@ use crate::app::subagent::context::build_subagent_context;
use crate::app::subagent::engine::run_subagent;
use crate::app::subagent::spawn::AgentDefinition;
use serde::{Deserialize, Serialize};
use std::process::Command;
use zesdex_cms::domain::memory::Memory;
use zesdex_cms::domain::repository::MemoryRepository;
use zesdex_cms::infrastructure::persistence::memory_repo::MarkdownMemoryRepository;
@@ -74,11 +79,12 @@ pub struct Lesson {
///
/// Return: `true` if a review should be triggered this turn.
pub fn should_trigger_review(state: &AppStateRest, origin: Origin) -> bool {
if origin != Origin::Main {
return false;
}
let Some(runtime) = &state.session_runtime else { return false };
let Some(runtime) = &state.session_runtime else {
return false;
};
if !state.settings.flags.review_enabled {
return false;
}
@@ -119,18 +125,28 @@ pub struct ProbeResult {
/// Return: `None` if no workspace exists, no command could be resolved,
/// or the process failed to spawn/poll; otherwise `Some(ProbeResult)`
/// describing pass/fail/timeout and truncated output.
pub fn probe_build_test(workspaces: &[std::path::PathBuf], verify_command: Option<&str>, timeout_ms: u64) -> Option<ProbeResult> {
pub fn probe_build_test(
workspaces: &[std::path::PathBuf],
verify_command: Option<&str>,
timeout_ms: u64,
) -> Option<ProbeResult> {
let probe_dir = workspaces.first()?;
let cmd = resolve_verify_command(probe_dir, verify_command)?;
let (cmd_prog, cmd_args) = cmd.split_once(' ').map_or_else(|| (cmd.clone(), String::new()), |(p, a)| (p.to_string(), a.to_string()));
let (cmd_prog, cmd_args) = cmd.split_once(' ').map_or_else(
|| (cmd.clone(), String::new()),
|(p, a)| (p.to_string(), a.to_string()),
);
let Ok(mut child) = Command::new(&cmd_prog)
.args(cmd_args.split_whitespace())
.current_dir(probe_dir)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn() else { return None };
.spawn()
else {
return None;
};
let start = std::time::Instant::now();
let timed_out = loop {
@@ -141,9 +157,19 @@ pub fn probe_build_test(workspaces: &[std::path::PathBuf], verify_command: Optio
match child.try_wait() {
Ok(Some(status)) => {
let output = child.wait_with_output().ok();
let stdout = output.as_ref().map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string()).unwrap_or_default();
let stderr = output.as_ref().map(|o| String::from_utf8_lossy(&o.stderr).trim().to_string()).unwrap_or_default();
let combined = if stderr.is_empty() { stdout } else { format!("{stdout}\n{stderr}") };
let stdout = output
.as_ref()
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
.unwrap_or_default();
let stderr = output
.as_ref()
.map(|o| String::from_utf8_lossy(&o.stderr).trim().to_string())
.unwrap_or_default();
let combined = if stderr.is_empty() {
stdout
} else {
format!("{stdout}\n{stderr}")
};
return Some(ProbeResult {
command: cmd.clone(),
passed: status.success(),
@@ -151,7 +177,9 @@ pub fn probe_build_test(workspaces: &[std::path::PathBuf], verify_command: Optio
timed_out: false,
});
}
Ok(None) => { std::thread::sleep(std::time::Duration::from_millis(50)); }
Ok(None) => {
std::thread::sleep(std::time::Duration::from_millis(50));
}
Err(_) => return None,
}
};
@@ -180,7 +208,10 @@ pub fn probe_build_test(workspaces: &[std::path::PathBuf], verify_command: Optio
///
/// Return: `Some(command)` if a command could be determined, `None` if
/// no marker files matched (e.g. plain Python project with no test dir).
fn resolve_verify_command(probe_dir: &std::path::Path, override_cmd: Option<&str>) -> Option<String> {
fn resolve_verify_command(
probe_dir: &std::path::Path,
override_cmd: Option<&str>,
) -> Option<String> {
if let Some(cmd) = override_cmd {
if !cmd.trim().is_empty() {
return Some(cmd.trim().to_string());
@@ -201,18 +232,35 @@ fn resolve_verify_command(probe_dir: &std::path::Path, override_cmd: Option<&str
let pkg = std::fs::read_to_string(probe_dir.join("package.json")).ok()?;
if let Ok(v) = serde_json::from_str::<serde_json::Value>(&pkg) {
let scripts = v.get("scripts")?;
if scripts.get("test").and_then(|s| s.as_str()).as_ref().is_some_and(|s| !s.is_empty()) {
if scripts
.get("test")
.and_then(|s| s.as_str())
.as_ref()
.is_some_and(|s| !s.is_empty())
{
return Some("npm test 2>&1".to_string());
}
if scripts.get("build").and_then(|s| s.as_str()).as_ref().is_some_and(|s| !s.is_empty()) {
if scripts
.get("build")
.and_then(|s| s.as_str())
.as_ref()
.is_some_and(|s| !s.is_empty())
{
return Some("npm run build 2>&1".to_string());
}
}
return Some("npm test 2>&1".to_string());
}
if has_file("pyproject.toml") || has_file("requirements.txt") || has_file("setup.py") || has_file("setup.cfg") || has_file("Pipfile") || has_file("poetry.lock") {
if has_file("pyproject.toml")
|| has_file("requirements.txt")
|| has_file("setup.py")
|| has_file("setup.cfg")
|| has_file("Pipfile")
|| has_file("poetry.lock")
{
if has_file("pyproject.toml") {
let content = std::fs::read_to_string(probe_dir.join("pyproject.toml")).unwrap_or_default();
let content =
std::fs::read_to_string(probe_dir.join("pyproject.toml")).unwrap_or_default();
if content.contains("[tool.pytest") {
return Some("python -m pytest --tb=short -q 2>&1".to_string());
}
@@ -307,10 +355,7 @@ fn truncate_output(s: &str, max: usize) -> String {
/// propagate from constructing the subagent context, not from the review
/// itself (that failure is reported via a `SystemNote` instead).
/// Compose the system prompt for the quality-review subagent.
fn compose_review_prompt(
state: &AppStateRest,
probe_note: &str,
) -> String {
fn compose_review_prompt(state: &AppStateRest, probe_note: &str) -> String {
let diff_output = if let Some(workspace) = state.workspace_roots.first() {
std::process::Command::new("git")
.arg("diff")
@@ -323,10 +368,15 @@ fn compose_review_prompt(
} else {
String::new()
};
let history_output = if let Some(rt) = &state.session_runtime {
let msgs: Vec<String> = rt.messages.iter()
.filter(|m| m.role == crate::dto::chat::message::Role::Assistant || m.role == crate::dto::chat::message::Role::User)
let msgs: Vec<String> = rt
.messages
.iter()
.filter(|m| {
m.role == crate::dto::chat::message::Role::Assistant
|| m.role == crate::dto::chat::message::Role::User
})
.rev()
.take(10)
.map(|m| format!("{:?}: {}", m.role, m.content.as_deref().unwrap_or("")))
@@ -371,7 +421,6 @@ fn compose_review_prompt(
/// Return: `Ok(())` once the review has been kicked off; errors only
/// propagate from constructing the subagent context, not from the review
/// itself (that failure is reported via a `SystemNote` instead).
#[allow(clippy::unnecessary_debug_formatting)]
pub fn trigger_review(state: &mut AppStateRest) {
state.misc.lesson_running = true;
@@ -380,17 +429,22 @@ pub fn trigger_review(state: &mut AppStateRest) {
let content = std::fs::read_to_string(&gitignore_path).unwrap_or_default();
if !content.contains("docs/lesson") {
use std::io::Write;
if let Ok(mut file) = std::fs::OpenOptions::new().create(true).append(true).open(&gitignore_path) {
let prefix = if content.is_empty() || content.ends_with('\n') { "" } else { "\n" };
if let Ok(mut file) = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&gitignore_path)
{
let prefix = if content.is_empty() || content.ends_with('\n') {
""
} else {
"\n"
};
let _ = writeln!(file, "{prefix}docs/lesson/");
}
}
}
let mut def = AgentDefinition::new(
"lesson-generator".to_string(),
"reviewer".to_string(),
);
let mut def = AgentDefinition::new("lesson-generator".to_string(), "reviewer".to_string());
// Explicitly allow write_file for docs/lesson
def.allowed_tools = Some(vec![
"read".to_string(),
@@ -402,7 +456,7 @@ pub fn trigger_review(state: &mut AppStateRest) {
let mut ctx = build_subagent_context(&def);
ctx.session_dir.clone_from(&state.session_dir);
ctx.workspaces.clone_from(&state.workspace_roots);
let probe_result = probe_build_test(
&state.workspace_roots,
state.settings.verify_command.as_deref(),
@@ -416,7 +470,10 @@ pub fn trigger_review(state: &mut AppStateRest) {
} else if r.timed_out {
format!("Build/test verification timed out ({}).", r.command)
} else {
format!("Build/test verification failed ({}). Output: {}", r.command, r.output)
format!(
"Build/test verification failed ({}). Output: {}",
r.command, r.output
)
}
}
None => "No build/test probe matched.".to_string(),
@@ -432,13 +489,22 @@ pub fn trigger_review(state: &mut AppStateRest) {
let mut rx = rx;
while let Some(event) = rx.blocking_recv() {
match &event {
SubagentEvent::ToolCall { tool, .. } => tracing::debug!("[review] tool call: {}", tool),
SubagentEvent::ToolResult { tool, .. } => tracing::debug!("[review] tool result: {}", tool),
SubagentEvent::ToolCall { tool, .. } => {
tracing::debug!("[review] tool call: {}", tool)
}
SubagentEvent::ToolResult { tool, .. } => {
tracing::debug!("[review] tool result: {}", tool)
}
SubagentEvent::StepCompleted { .. } => tracing::trace!("[review] step completed"),
SubagentEvent::StepFailed { step, error } => tracing::warn!("[review] step {} failed: {}", step, error),
SubagentEvent::StepFailed { step, error } => {
tracing::warn!("[review] step {} failed: {}", step, error)
}
SubagentEvent::Progress(_) => {}
SubagentEvent::Completed { .. } => tracing::debug!("[review] completed"),
SubagentEvent::Usage { tokens_in, tokens_out } => {
SubagentEvent::Completed => tracing::debug!("[review] completed"),
SubagentEvent::Usage {
tokens_in,
tokens_out,
} => {
if let Ok(mut q) = turn_events_for_drain.lock() {
q.push_back(TurnEvent::ReviewUsage {
tokens_in: *tokens_in,
@@ -449,7 +515,7 @@ pub fn trigger_review(state: &mut AppStateRest) {
}
}
});
let turn_events = state.turn_events.clone();
std::thread::spawn(move || {
@@ -487,15 +553,18 @@ const STALE_AFTER_DAYS: i64 = 60;
/// `mem.write`.
pub fn run_staleness_sweep(memory_dir: &std::path::Path) -> std::io::Result<Vec<String>> {
let mut flagged = Vec::new();
let names = MarkdownMemoryRepository::new().list(memory_dir).unwrap_or_default();
let names = MarkdownMemoryRepository::new()
.list(memory_dir)
.unwrap_or_default();
let now = chrono::Utc::now().timestamp_millis();
let cutoff = now - STALE_AFTER_DAYS * 24 * 3600 * 1000;
for name in names {
if let Ok(mut mem) = MarkdownMemoryRepository::new().load(memory_dir, &name) {
if mem.updated_at < cutoff && mem.lifecycle != "stale" {
mem.lifecycle = "stale".to_string();
MarkdownMemoryRepository::new().save(memory_dir, &mem)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
MarkdownMemoryRepository::new()
.save(memory_dir, &mem)
.map_err(|e| std::io::Error::other(e.to_string()))?;
flagged.push(name);
}
}
@@ -521,7 +590,11 @@ pub fn maybe_run_staleness_sweep(state: &mut AppStateRest) {
if !flagged.is_empty() {
state.push_toast(Toast::new(
ToastKind::Info,
format!("Staleness sweep: {} lesson(s) flagged as stale: {}", flagged.len(), flagged.join(", ")),
format!(
"Staleness sweep: {} lesson(s) flagged as stale: {}",
flagged.len(),
flagged.join(", ")
),
));
}
}
@@ -551,7 +624,10 @@ pub fn load_pending_lessons(session_dir: &std::path::Path) -> Vec<PendingLesson>
/// Write the session's pending-lessons queue to disk as pretty JSON.
///
/// Return: `Ok(())`, or an I/O error from writing the file.
pub fn save_pending_lessons(session_dir: &std::path::Path, pending: &[PendingLesson]) -> std::io::Result<()> {
pub fn save_pending_lessons(
session_dir: &std::path::Path,
pending: &[PendingLesson],
) -> std::io::Result<()> {
let path = session_dir.join("pending_lessons.json");
let data = serde_json::to_string_pretty(pending)?;
std::fs::write(&path, data)
@@ -570,7 +646,10 @@ pub fn save_pending_lessons(session_dir: &std::path::Path, pending: &[PendingLes
///
/// Return: the still-pending lessons (post-commit), or an I/O error from
/// writing memory files or the queue.
pub fn process_pending_lessons(session_dir: &std::path::Path, memory_dir: &std::path::Path) -> std::io::Result<Vec<PendingLesson>> {
pub fn process_pending_lessons(
session_dir: &std::path::Path,
memory_dir: &std::path::Path,
) -> std::io::Result<Vec<PendingLesson>> {
let pending = load_pending_lessons(session_dir);
let now = chrono::Utc::now().timestamp_millis();
let grace_window = 5_000;
@@ -599,8 +678,9 @@ pub fn process_pending_lessons(session_dir: &std::path::Path, memory_dir: &std::
after_snippet: None,
provenances: vec![],
};
MarkdownMemoryRepository::new().save(memory_dir, &mem)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
MarkdownMemoryRepository::new()
.save(memory_dir, &mem)
.map_err(|e| std::io::Error::other(e.to_string()))?;
}
save_pending_lessons(session_dir, &remaining)?;
@@ -644,8 +724,9 @@ pub fn resolve_pending_lesson(
after_snippet: None,
provenances: vec![],
};
MarkdownMemoryRepository::new().save(memory_dir, &mem)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
MarkdownMemoryRepository::new()
.save(memory_dir, &mem)
.map_err(|e| std::io::Error::other(e.to_string()))?;
}
} else {
remaining.push(p);
@@ -100,7 +100,6 @@ pub enum Action {
/// need to know how to *produce* actions.
///
/// Return: nothing; `state` is mutated in place.
#[allow(clippy::too_many_lines)]
pub fn apply_action(state: &mut AppStateRest, action: Action) {
match action {
Action::ForceQuit => {
@@ -869,7 +868,7 @@ fn refresh_lesson_counters(memory_dir: &std::path::Path, rt: &mut crate::app::st
/// Flow: if `db` is `Some`, lock the mutex and call `insert_message`.
/// Errors are silently ignored.
fn archive_message(db: Option<&std::sync::Arc<std::sync::Mutex<rusqlite::Connection>>>, session_id: &str, msg: &ChatMessage) {
if let Some(arc) = db {
if let Some(arc) = sess.db {
if let Ok(conn) = arc.lock() {
let _ = crate::model::msglog::insert_message(&conn, session_id, msg);
}
@@ -913,7 +912,6 @@ const HIVE_MIND_KICKOFF_NOTE: &str = "The Hive is stirring — Core Intelligence
///
/// Return: `Ok(())` on successful completion, or an error from the LLM
/// API after retries are exhausted.
#[allow(clippy::too_many_lines)]
fn run_agent_turn(
tc: &TurnCtx,
messages: &[ChatMessage],
@@ -1328,9 +1326,11 @@ fn run_agent_turn(
&tool_name,
&tool_call.id,
&args,
&tc_ref.edit_log_session_dir,
&tc_ref.session_id,
tc_ref.db.as_ref(),
&ToolExecSession {
dir: &tc_ref.edit_log_session_dir,
id: &tc_ref.session_id,
db: tc_ref.db.as_ref(),
},
) {
Ok(result) => (result, false, is_edit_tool),
Err(e) => (e.to_string(), true, false),
@@ -1538,28 +1538,31 @@ fn run_agent_turn(
///
/// Return: the tool's stdout string, or an error if no matching tool was
/// found or the tool run itself failed.
#[allow(clippy::too_many_arguments)]
struct ToolExecSession<'a> {
dir: &'a std::path::Path,
id: &'a str,
db: Option<&'a std::sync::Arc<std::sync::Mutex<rusqlite::Connection>>>,
}
fn execute_one_tool(
tools: &[Box<dyn crate::tool::Tool>],
ctx: &crate::tool::ToolCtx,
name: &str,
tool_call_id: &str,
args: &serde_json::Value,
session_dir: &std::path::Path,
session_id: &str,
db: Option<&std::sync::Arc<std::sync::Mutex<rusqlite::Connection>>>,
sess: &ToolExecSession<'_>,
) -> anyhow::Result<String> {
for tool in tools {
if tool.name() == name {
// Snapshot current file content before write/edit for rewind
if (name == "write" || name == "edit") && !tool_call_id.is_empty() {
if let Some(arc) = db {
if let Some(arc) = sess.db {
if let Ok(conn) = arc.lock() {
let path = args.get("path").and_then(|v| v.as_str()).unwrap_or("");
if let Ok(abs_path) = crate::tool::resolve_path(&ctx.workspaces, path) {
if let Ok(bytes) = std::fs::read(&abs_path) {
let _ = crate::model::msglog::store_blob(
&conn, session_id, tool_call_id, &bytes, None,
&conn, sess.id, tool_call_id, &bytes, None,
);
}
}
@@ -1600,11 +1603,11 @@ fn execute_one_tool(
content_sha256,
bytes_delta,
origin: ctx.origin.tag(),
session_id: session_id.to_string(),
session_id: sess.id.to_string(),
};
let repo = zesdex_cms::infrastructure::persistence::edit_log_repo::JsonlEditLogRepository::new();
if let Ok(mut el) = repo.open(session_dir) {
let _ = repo.append(session_dir, &mut el, entry);
if let Ok(mut el) = repo.open(sess.dir) {
let _ = repo.append(sess.dir, &mut el, entry);
}
}
return Ok(result);
@@ -10,7 +10,7 @@
//! `o200k_base` is an approximation for non-OpenAI providers but is far
//! closer than a flat byte-per-token guess; it's only used for the
//! 85%/95% budget thresholds, not for billing-accurate counts.
use crate::dto::chat::message::ChatMessage;
/// Count tokens in a single string under `o200k_base`.
///
@@ -25,20 +25,16 @@ pub fn count_tokens(text: &str) -> usize {
.len()
}
/// Count tokens in a `ChatMessage`'s text content.
///
/// Return: 0 for a message with no `content` (e.g. an assistant message
/// that only carries `tool_calls`).
#[allow(dead_code)]
pub fn count_message_tokens(msg: &ChatMessage) -> usize {
msg.content.as_deref().map_or(0, count_tokens)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dto::chat::message::ChatMessage;
/// Count tokens in a `ChatMessage`'s text content.
fn count_message_tokens(msg: &ChatMessage) -> usize {
msg.content.as_deref().map_or(0, count_tokens)
}
#[test]
fn empty_string_has_zero_tokens() {
assert_eq!(count_tokens(""), 0);
@@ -43,9 +43,11 @@ mod tests {
temperature: None,
},
);
let mut settings = Settings::default();
settings.provider = "zen".to_string();
settings.model = "deepseek-v4-flash-free".to_string();
let settings = Settings {
provider: "zen".to_string(),
model: "deepseek-v4-flash-free".to_string(),
..Default::default()
};
assert_eq!(resolve(&app_config, &settings), 128_000);
}
@@ -53,9 +55,11 @@ mod tests {
#[test]
fn falls_back_to_default_context_window_when_no_role_matches() {
let app_config = AppConfig::default();
let mut settings = Settings::default();
settings.provider = "nonexistent".to_string();
settings.model = "nonexistent-model".to_string();
let settings = Settings {
provider: "nonexistent".to_string(),
model: "nonexistent-model".to_string(),
..Default::default()
};
assert_eq!(
resolve(&app_config, &settings),
@@ -76,9 +80,11 @@ mod tests {
temperature: None,
},
);
let mut settings = Settings::default();
settings.provider = "zen".to_string();
settings.model = "deepseek-v4-flash-free".to_string();
let settings = Settings {
provider: "zen".to_string(),
model: "deepseek-v4-flash-free".to_string(),
..Default::default()
};
assert_eq!(
resolve(&app_config, &settings),
@@ -2,355 +2,4 @@
//! typed `StreamEvent` variants (tokens, reasoning, tool calls, usage, done).
pub mod turn;
use serde::{Deserialize, Serialize};
use serde_json::Value;
/// One atomic event extracted from an LLM streaming response stream.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StreamEvent {
Token(String),
Reasoning(String),
ToolCallDelta {
index: usize,
id: Option<String>,
name: Option<String>,
arguments_delta: String,
},
Usage {
prompt_tokens: u64,
completion_tokens: u64,
total_tokens: u64,
},
Done,
Error(String),
}
/// Buffered SSE frame parser that accumulates raw `data:` lines and
/// flushes a `StreamEvent` on each blank-line boundary.
pub struct SseParser {
buffer: String,
event_type: Option<String>,
data_lines: Vec<String>,
}
impl SseParser {
/// Create a new parser with an empty buffer.
pub fn new() -> Self {
SseParser {
buffer: String::new(),
event_type: None,
data_lines: Vec::new(),
}
}
/// Feed a raw SSE chunk and produce any completed events.
///
/// Flow: append chunk to buffer → scan for '\n' → strip '\r' → on
/// blank line, call `flush_event` to parse the accumulated data →
/// on `event:` line, store the event type → on `data:` line, append
/// to data accumulator → continue until buffer exhausted.
///
/// Edge case: a chunk may split mid-line; the remainder stays in the
/// buffer for the next `feed()` call.
///
/// Return: all `StreamEvent`s completed by this chunk.
pub fn feed(&mut self, chunk: &str) -> Vec<StreamEvent> {
self.buffer.push_str(chunk);
let mut events = Vec::new();
while let Some(line_end) = self.buffer.find('\n') {
let line = self.buffer[..line_end].trim_end_matches('\r').to_string();
self.buffer = self.buffer[line_end + 1..].to_string();
if line.is_empty() {
events.extend(self.flush_event());
} else if let Some(ty) = line.strip_prefix("event: ") {
self.event_type = Some(ty.trim().to_string());
} else if let Some(data) = line.strip_prefix("data:") {
// Handle both "data: {...}" (with space) and "data:{...}"
// (without space). Some providers omit the trailing space.
let data = data.trim_start().to_string();
self.data_lines.push(data);
}
}
events
}
/// Flush the current buffered `data:` lines as one or more `StreamEvent`s.
///
/// Flow: join data lines → handle `[DONE]` sentinel → JSON-parse →
/// emit `Usage` if a usage object is present → else match `event_type`
/// ("message.stop", "message.delta", etc.) → extract content,
/// reasoning, tool-call deltas, or finish-reason from the delta
/// structure (supporting both Anthropic-style top-level delta and
/// OpenAI-style `choices` array).
///
/// Why: dual-format support in one method avoids a separate
/// provider-specific parsing layer.
///
/// Return: 0, 1, or more `StreamEvent`s from the flushed frame.
fn flush_event(&mut self) -> Vec<StreamEvent> {
let data = self.data_lines.join("\n");
self.data_lines.clear();
let event_type = self.event_type.take().unwrap_or_default();
if data.is_empty() || data == "[DONE]" {
if data == "[DONE]" {
return vec![StreamEvent::Done];
}
return vec![];
}
let value: Value = match serde_json::from_str(&data) {
Ok(v) => v,
Err(e) => {
tracing::warn!("[stream] failed to parse chunk: {}", e);
return vec![];
}
};
let mut events = Vec::new();
if let Some(usage) = value.get("usage") {
if !usage.is_null() {
let prompt_tokens = usage
.get("prompt_tokens")
.and_then(serde_json::Value::as_u64)
.unwrap_or_else(|| {
tracing::warn!("[stream] prompt_tokens missing in usage chunk");
0
});
let completion_tokens = usage
.get("completion_tokens")
.and_then(serde_json::Value::as_u64)
.unwrap_or_else(|| {
tracing::warn!("[stream] completion_tokens missing in usage chunk");
0
});
let total_tokens = usage
.get("total_tokens")
.and_then(serde_json::Value::as_u64)
.unwrap_or_else(|| {
tracing::warn!("[stream] total_tokens missing in usage chunk");
prompt_tokens + completion_tokens
});
events.push(StreamEvent::Usage {
prompt_tokens,
completion_tokens,
total_tokens,
});
}
}
let mut other_events = match event_type.as_str() {
"message.stop" => vec![StreamEvent::Done],
"message.delta" | "" => {
let mut d_events = Vec::new();
if let Some(delta) = value.get("delta").or_else(|| value.get("choices")) {
if let Some(choices) = delta.as_array() {
if let Some(choice) = choices.first() {
if let Some(d) = choice.get("delta") {
// Content token
if let Some(content) = d.get("content").and_then(|c| c.as_str()) {
d_events.push(StreamEvent::Token(content.to_string()));
}
// Reasoning token
if let Some(reasoning) =
d.get("reasoning_content").and_then(|r| r.as_str())
{
d_events.push(StreamEvent::Reasoning(reasoning.to_string()));
}
// Tool calls — iterate ALL entries, not just first()
if let Some(tool_calls) =
d.get("tool_calls").and_then(|tc| tc.as_array())
{
for tc in tool_calls {
let index = tc.get("index").and_then(serde_json::Value::as_u64).unwrap_or_else(|| {
tracing::warn!("[stream] tool call delta missing index, defaulting to 0");
0
}) as usize;
let id = tc
.get("id")
.and_then(|i| i.as_str())
.map(std::string::ToString::to_string);
let name = tc
.get("function")
.and_then(|f| f.get("name"))
.and_then(|n| n.as_str())
.map(std::string::ToString::to_string);
let args_delta = tc
.get("function")
.and_then(|f| f.get("arguments"))
.and_then(|a| a.as_str())
.unwrap_or("")
.to_string();
d_events.push(StreamEvent::ToolCallDelta {
index,
id,
name,
arguments_delta: args_delta,
});
}
}
// Finish reason
if let Some(reason) =
choice.get("finish_reason").and_then(|r| r.as_str())
{
if reason == "stop" || reason == "tool_calls" {
d_events.push(StreamEvent::Done);
}
}
}
}
} else if let Some(content) = delta.get("content").and_then(|c| c.as_str()) {
d_events.push(StreamEvent::Token(content.to_string()));
}
}
d_events
}
_ => vec![],
};
events.append(&mut other_events);
events
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn feed_parses_single_token_chunk() {
let mut p = SseParser::new();
let events = p.feed("data: {\"choices\":[{\"delta\":{\"content\":\"hello\"}}]}\n\n");
assert_eq!(events.len(), 1);
match &events[0] {
StreamEvent::Token(t) => assert_eq!(t, "hello"),
other => panic!("expected Token, got {other:?}"),
}
}
#[test]
fn feed_handles_chunk_split_mid_line() {
let mut p = SseParser::new();
let e1 = p.feed("data: {\"choices\":[{\"delta\":{\"content\":\"partial");
assert!(
e1.is_empty(),
"no event until the line and blank separator complete"
);
let e2 = p.feed("\"}}]}\n\n");
assert_eq!(e2.len(), 1);
match &e2[0] {
StreamEvent::Token(t) => assert_eq!(t, "partial"),
other => panic!("expected Token, got {other:?}"),
}
}
#[test]
fn feed_emits_done_on_done_sentinel() {
let mut p = SseParser::new();
let events = p.feed("data: [DONE]\n\n");
assert_eq!(events.len(), 1);
assert!(matches!(events[0], StreamEvent::Done));
}
#[test]
fn feed_emits_done_on_finish_reason_stop() {
let mut p = SseParser::new();
let events = p.feed("data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n");
assert_eq!(events.len(), 1);
assert!(matches!(events[0], StreamEvent::Done));
}
#[test]
fn feed_parses_tool_call_delta() {
let mut p = SseParser::new();
let events = p.feed(
"data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"function\":{\"name\":\"bash\",\"arguments\":\"{\\\"cmd\\\"\"}}]}}]}\n\n",
);
assert_eq!(events.len(), 1);
match &events[0] {
StreamEvent::ToolCallDelta {
index,
id,
name,
arguments_delta,
} => {
assert_eq!(*index, 0);
assert_eq!(id.as_deref(), Some("call_1"));
assert_eq!(name.as_deref(), Some("bash"));
assert_eq!(arguments_delta, "{\"cmd\"");
}
other => panic!("expected ToolCallDelta, got {other:?}"),
}
}
#[test]
fn feed_parses_usage_chunk() {
let mut p = SseParser::new();
let events = p.feed(
"data: {\"choices\":[],\"usage\":{\"prompt_tokens\":10,\"completion_tokens\":5,\"total_tokens\":15}}\n\n",
);
assert_eq!(events.len(), 1);
match &events[0] {
StreamEvent::Usage {
prompt_tokens,
completion_tokens,
total_tokens,
} => {
assert_eq!(*prompt_tokens, 10);
assert_eq!(*completion_tokens, 5);
assert_eq!(*total_tokens, 15);
}
other => panic!("expected Usage, got {other:?}"),
}
}
#[test]
fn feed_parses_usage_and_content_bundled_chunk() {
let mut p = SseParser::new();
let events = p.feed(
"data: {\"choices\":[{\"delta\":{\"content\":\"hello\"}}],\"usage\":{\"prompt_tokens\":10,\"completion_tokens\":5,\"total_tokens\":15}}\n\n",
);
assert_eq!(events.len(), 2);
match (&events[0], &events[1]) {
(
StreamEvent::Usage {
prompt_tokens,
completion_tokens,
total_tokens,
},
StreamEvent::Token(t),
) => {
assert_eq!(*prompt_tokens, 10);
assert_eq!(*completion_tokens, 5);
assert_eq!(*total_tokens, 15);
assert_eq!(t, "hello");
}
other => panic!("expected [Usage, Token], got {other:?}"),
}
}
#[test]
fn feed_ignores_empty_data_lines() {
let mut p = SseParser::new();
let events = p.feed(": comment\n\n");
assert!(events.is_empty());
}
#[test]
fn feed_multiple_events_across_one_chunk() {
let mut p = SseParser::new();
let chunk = "data: {\"choices\":[{\"delta\":{\"content\":\"a\"}}]}\n\ndata: {\"choices\":[{\"delta\":{\"content\":\"b\"}}]}\n\n";
let events = p.feed(chunk);
assert_eq!(events.len(), 2);
match (&events[0], &events[1]) {
(StreamEvent::Token(a), StreamEvent::Token(b)) => {
assert_eq!(a, "a");
assert_eq!(b, "b");
}
other => panic!("expected two Tokens, got {other:?}"),
}
}
}
pub use zesdex_entities::{SseParser, StreamEvent};
+40 -15
View File
@@ -1,9 +1,9 @@
//! Application-level "miscellaneous" state: scroll, input buffer,
//! overlay stack, toasts, editor, and autocomplete.
use super::types::Overlay;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::RwLock;
use super::types::Overlay;
/// A shared, async-writable cache of directory entries, used to avoid
/// re-reading a directory every render frame.
@@ -134,7 +134,6 @@ const COMMANDS: &[&str] = &[
"/model",
"/model ls",
"/model add",
"/todo",
"/usage",
"/compact",
@@ -211,7 +210,10 @@ impl InputState {
return None;
}
let boundary_ok = at_pos == 0
|| before_cursor[..at_pos].chars().next_back().is_some_and(char::is_whitespace);
|| before_cursor[..at_pos]
.chars()
.next_back()
.is_some_and(char::is_whitespace);
if !boundary_ok {
return None;
}
@@ -225,8 +227,8 @@ impl InputState {
/// if none, close and return → otherwise fuzzy-match `query` against
/// `files` via `nucleo-matcher`, keep the top 10 by score.
pub fn open_mention_autocomplete(&mut self, files: &[String]) {
use nucleo_matcher::{Config, Matcher};
use nucleo_matcher::pattern::{CaseMatching, Normalization, Pattern};
use nucleo_matcher::{Config, Matcher};
let Some((start, query)) = self.mention_query_at_cursor() else {
self.close_autocomplete();
return;
@@ -234,7 +236,11 @@ impl InputState {
let mut matcher = Matcher::new(Config::DEFAULT.match_paths());
let pattern = Pattern::parse(&query, CaseMatching::Smart, Normalization::Smart);
let matched_files = pattern.match_list(files.iter(), &mut matcher);
self.autocomplete_candidates = matched_files.into_iter().take(10).map(|(f, _)| f.clone()).collect();
self.autocomplete_candidates = matched_files
.into_iter()
.take(10)
.map(|(f, _)| f.clone())
.collect();
self.autocomplete_kind = AutocompleteKind::FileMention;
self.mention_start = start;
self.autocomplete_idx = 0;
@@ -245,11 +251,17 @@ impl InputState {
/// Wraps around at the boundaries.
pub fn cycle_autocomplete(&mut self, forward: bool) {
let n = self.autocomplete_candidates.len();
if n == 0 { return; }
if n == 0 {
return;
}
if forward {
self.autocomplete_idx = (self.autocomplete_idx + 1) % n;
} else {
self.autocomplete_idx = if self.autocomplete_idx == 0 { n - 1 } else { self.autocomplete_idx - 1 };
self.autocomplete_idx = if self.autocomplete_idx == 0 {
n - 1
} else {
self.autocomplete_idx - 1
};
}
}
@@ -261,7 +273,11 @@ impl InputState {
///
/// Return: `true` if a candidate was selected, `false` if none existed.
pub fn select_autocomplete(&mut self) -> bool {
let Some(candidate) = self.autocomplete_candidates.get(self.autocomplete_idx).cloned() else {
let Some(candidate) = self
.autocomplete_candidates
.get(self.autocomplete_idx)
.cloned()
else {
return false;
};
match self.autocomplete_kind {
@@ -282,7 +298,8 @@ impl InputState {
return false;
}
let replacement = format!("@{candidate} ");
self.buffer.replace_range(self.mention_start..self.cursor, &replacement);
self.buffer
.replace_range(self.mention_start..self.cursor, &replacement);
self.cursor = self.mention_start + replacement.len();
}
}
@@ -406,8 +423,6 @@ pub struct MiscState {
pub selected_index: usize,
pub editor: Option<super::super::mode::editor::EditorState>,
pub api_connected: bool,
#[allow(dead_code)]
pub api_context_length: Option<u32>,
pub tick_count: u64,
pub todo_content: String,
pub lesson_running: bool,
@@ -427,7 +442,6 @@ impl MiscState {
selected_index: 0,
editor: None,
api_connected: false,
api_context_length: None,
tick_count: 0,
todo_content: String::new(),
lesson_running: false,
@@ -443,7 +457,12 @@ impl MiscState {
///
/// Return: the expired toasts (after removal).
pub fn drain_expired_toasts(&mut self, now_ms: i64) -> Vec<super::types::Toast> {
let expired: Vec<_> = self.toasts.iter().filter(|t| t.expired(now_ms)).cloned().collect();
let expired: Vec<_> = self
.toasts
.iter()
.filter(|t| t.expired(now_ms))
.cloned()
.collect();
self.toasts.retain(|t| !t.expired(now_ms));
expired
}
@@ -463,13 +482,19 @@ mod tests {
#[test]
fn mention_at_buffer_start_triggers() {
let input = input_with("@mai", 4);
assert_eq!(input.mention_query_at_cursor(), Some((0, "mai".to_string())));
assert_eq!(
input.mention_query_at_cursor(),
Some((0, "mai".to_string()))
);
}
#[test]
fn mention_after_space_mid_sentence_triggers() {
let input = input_with("look at @read", 13);
assert_eq!(input.mention_query_at_cursor(), Some((8, "read".to_string())));
assert_eq!(
input.mention_query_at_cursor(),
Some((8, "read".to_string()))
);
}
#[test]
+94 -38
View File
@@ -17,12 +17,12 @@ use crate::app::mcp::manager::McpManager;
use crate::app::workflow::engine::WorkflowEngine;
use zesdex_cms::domain::app_config::AppConfig;
use zesdex_cms::domain::edit_log::EditLog;
use zesdex_cms::domain::repository::EditLogRepository;
use zesdex_cms::infrastructure::persistence::edit_log_repo::JsonlEditLogRepository;
use zesdex_cms::domain::repository::AppConfigRepository;
use zesdex_cms::domain::repository::EditLogRepository;
use zesdex_cms::domain::repository::SettingsRepository;
use zesdex_cms::domain::settings::Settings;
use zesdex_cms::infrastructure::persistence::app_config_repo::JsonAppConfigRepository;
use zesdex_cms::infrastructure::persistence::edit_log_repo::JsonlEditLogRepository;
use zesdex_cms::infrastructure::persistence::settings_repo::JsonSettingsRepository;
/// A single transcript entry rendered in the TUI chat pane.
@@ -51,7 +51,6 @@ impl ChatMessageDisplay {
/// other module.
#[derive(Clone)]
pub struct AppStateRest {
pub settings: Settings,
pub app_config: AppConfig,
pub workspace_roots: Vec<PathBuf>,
@@ -91,7 +90,11 @@ impl AppStateRest {
/// Why: falls back to `memory_dir` itself (with a warning) when it has
/// no parent, and to an empty session id when the dir name can't be
/// read, so construction never fails.
pub fn new(workspace_roots: Vec<PathBuf>, session_dir: &std::path::Path, memory_dir: PathBuf) -> Self {
pub fn new(
workspace_roots: Vec<PathBuf>,
session_dir: &std::path::Path,
memory_dir: PathBuf,
) -> Self {
let store_base_dir = zesdex_entities::seaorm::common::store::Store::new().base_dir;
let settings = JsonSettingsRepository::new()
.load(&store_base_dir)
@@ -99,18 +102,27 @@ impl AppStateRest {
let app_config = JsonAppConfigRepository::new()
.load(&store_base_dir)
.unwrap_or_default();
let worktrees_dir = memory_dir.parent().unwrap_or_else(|| {
tracing::warn!("[state] memory_dir '{}' has no parent, using it for worktrees", memory_dir.display());
&memory_dir
}).join("worktrees");
let worktrees_dir = memory_dir
.parent()
.unwrap_or_else(|| {
tracing::warn!(
"[state] memory_dir '{}' has no parent, using it for worktrees",
memory_dir.display()
);
&memory_dir
})
.join("worktrees");
let dir_cache = DirCache::new();
let session_id = session_dir
.file_name().map_or_else(|| {
tracing::warn!("[state] session_dir has no file_name component, using empty session_id");
let session_id = session_dir.file_name().map_or_else(
|| {
tracing::warn!(
"[state] session_dir has no file_name component, using empty session_id"
);
String::new()
}, |n| n.to_string_lossy().to_string());
},
|n| n.to_string_lossy().to_string(),
);
let mut state = AppStateRest {
settings,
app_config,
workspace_roots,
@@ -123,10 +135,15 @@ impl AppStateRest {
abort_flag: Arc::new(std::sync::atomic::AtomicBool::new(false)),
dir_cache: Arc::new(RwLock::new(dir_cache)),
mention_index: MentionIndex::new(),
edit_log: JsonlEditLogRepository::new().open(session_dir).unwrap_or_else(|e| {
tracing::warn!("[state] failed to open edit log at '{}': {e}", session_dir.display());
EditLog::new()
}),
edit_log: JsonlEditLogRepository::new()
.open(session_dir)
.unwrap_or_else(|e| {
tracing::warn!(
"[state] failed to open edit log at '{}': {e}",
session_dir.display()
);
EditLog::new()
}),
session_runtime: Some(SessionRuntime::new(session_dir.to_path_buf())),
workflow_engine: WorkflowEngine::new(),
mcp_manager: McpManager::new(),
@@ -149,7 +166,9 @@ impl AppStateRest {
let mut hasher = sha2::Sha256::new();
hasher.update(abs_root.to_string_lossy().as_bytes());
let hash_hex = hex::encode(hasher.finalize());
let folder_name = abs_root.file_name().map_or_else(|| "root".to_string(), |n| n.to_string_lossy().to_string());
let folder_name = abs_root
.file_name()
.map_or_else(|| "root".to_string(), |n| n.to_string_lossy().to_string());
let history_filename = format!("{}-{}.txt", folder_name, &hash_hex[..8]);
let history_dir = base_dir.join("history");
let _ = std::fs::create_dir_all(&history_dir);
@@ -194,9 +213,14 @@ impl AppStateRest {
}
// Wrap the msg_queue in a static-lifetime closure for use as ProgressFn.
let progress: provisioner::ProgressFn = Some(&|msg: &str| push_msg(&msg_queue, msg));
let progress: provisioner::ProgressFn =
Some(&|msg: &str| push_msg(&msg_queue, msg));
let report = |msg: &str| { if let Some(f) = &progress { f(msg); }};
let report = |msg: &str| {
if let Some(f) = &progress {
f(msg);
}
};
report("LSP: provisioning servers...");
let results = provisioner::provision_all_with_progress(progress);
@@ -204,18 +228,29 @@ impl AppStateRest {
let connected = provisioner::auto_connect(&lsp_mgr, &results);
for name in &connected {
tracing::info!("LSP: {} connected", name);
let m = format!("LSP: {name} connected ✓"); push_msg(&msg_queue, &m);
let m = format!("LSP: {name} connected ✓");
push_msg(&msg_queue, &m);
}
for r in &results {
if let ProvisionResult::Failed { language, server_name, reason, .. } = r {
if let ProvisionResult::Failed {
language,
server_name,
reason,
..
} = r
{
tracing::warn!("LSP {} ({}): {}", server_name, language, reason);
let m = format!("LSP: {server_name} ({language}) ✗ - {reason}"); push_msg(&msg_queue, &m);
let m = format!("LSP: {server_name} ({language}) ✗ - {reason}");
push_msg(&msg_queue, &m);
}
}
if connected.is_empty() {
let m = "LSP: no servers available — install manually or check prerequisites".to_string(); push_msg(&msg_queue, &m);
let m = "LSP: no servers available — install manually or check prerequisites"
.to_string();
push_msg(&msg_queue, &m);
} else {
let m = format!("LSP: {} server(s) connected", connected.len()); push_msg(&msg_queue, &m);
let m = format!("LSP: {} server(s) connected", connected.len());
push_msg(&msg_queue, &m);
}
});
}
@@ -259,7 +294,11 @@ impl AppStateRest {
}
let rel = entry.path().strip_prefix(root).unwrap_or(entry.path());
let rel_str = rel.display().to_string();
let formatted = if i == 0 { rel_str } else { format!("[{i}]{rel_str}") };
let formatted = if i == 0 {
rel_str
} else {
format!("[{i}]{rel_str}")
};
paths.push(formatted);
if paths.len() >= MAX_MENTION_ENTRIES {
break 'roots;
@@ -275,10 +314,13 @@ impl AppStateRest {
/// Return: `false` (and logs a warning) if the mutex is poisoned, rather
/// than propagating a panic.
pub fn turn_in_flight(&self) -> bool {
self.turn_in_flight.lock().map_or_else(|_| {
tracing::warn!("[state] turn_in_flight mutex poisoned");
false
}, |g| *g)
self.turn_in_flight.lock().map_or_else(
|_| {
tracing::warn!("[state] turn_in_flight mutex poisoned");
false
},
|g| *g,
)
}
/// Shut down every running LSP server process.
@@ -317,14 +359,28 @@ impl AppStateRest {
/// `session_dir` itself -- logging a warning at each step down, so this
/// never fails even on a shallow path.
pub fn store_base_dir(&self) -> std::path::PathBuf {
self.session_dir.parent()
.and_then(|p| p.parent()).map_or_else(|| {
tracing::warn!("[state] session_dir '{}' has no grandparent, using parent", self.session_dir.display());
self.session_dir.parent().map_or_else(|| {
tracing::warn!("[state] session_dir '{}' has no parent at all, using itself", self.session_dir.display());
self.session_dir.clone()
}, std::path::Path::to_path_buf)
}, std::path::Path::to_path_buf)
self.session_dir
.parent()
.and_then(|p| p.parent())
.map_or_else(
|| {
tracing::warn!(
"[state] session_dir '{}' has no grandparent, using parent",
self.session_dir.display()
);
self.session_dir.parent().map_or_else(
|| {
tracing::warn!(
"[state] session_dir '{}' has no parent at all, using itself",
self.session_dir.display()
);
self.session_dir.clone()
},
std::path::Path::to_path_buf,
)
},
std::path::Path::to_path_buf,
)
}
/// Build a `ToolCtx` for tool calls originating from the main agent.
+2 -24
View File
@@ -4,20 +4,7 @@
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
/// Cumulative token/latency counters for a session, persisted alongside it.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub struct UsageStats {
pub tokens_in: u64,
pub tokens_out: u64,
#[serde(default)]
pub last_tokens_in: u64,
#[serde(default)]
pub last_tokens_out: u64,
pub api_calls: u64,
pub review_tokens: u64,
pub total_ms: u64,
}
pub use zesdex_entities::seaorm::common::usage::UsageStats;
/// Mutable, serializable state for one session: chat history, tool
/// results, pending tools, background jobs, and lesson/review counters
/// shown in the TUI status bar.
@@ -55,16 +42,7 @@ pub struct SessionRuntime {
pub hive_mind_converged: bool,
}
/// Record of one completed tool invocation, kept for transcript/history.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCallResult {
pub tool_call_id: String,
pub tool_name: String,
pub output: String,
pub is_error: bool,
pub duration_ms: u64,
}
pub use zesdex_entities::seaorm::common::tool_result::ToolCallResult;
/// A tool call awaiting execution, along with which execution model
/// (inline, deferred, async) it should run under.
#[derive(Debug, Clone, Serialize, Deserialize)]
+179 -75
View File
@@ -7,14 +7,14 @@
//! bash exfiltration and destructive-pattern detection) so that subagents
//! are not a weaker link than the main agent.
use std::fmt::Write;
use sha2::Digest;
use tokio::sync::mpsc;
use super::context::SubagentContext;
use super::event::SubagentEvent;
use crate::dto::chat::message::ChatMessage;
use crate::dto::provider::request::ToolDef;
use crate::tool::{all_tools, tool_defs, tool_is_risky};
use super::context::SubagentContext;
use super::event::SubagentEvent;
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;
@@ -29,7 +29,9 @@ use zesdex_cms::domain::repository::SettingsRepository;
/// `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>) {
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()
@@ -62,28 +64,44 @@ fn build_subagent_tools(allowed_tools: &[String]) -> (Vec<Box<dyn crate::tool::T
/// 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 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 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)
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()
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);
tracing::warn!(
"[subagent] all API key resolution paths exhausted for '{}'",
settings.provider
);
String::new()
});
}
@@ -109,43 +127,82 @@ fn require_api_key(api_key: &str, provider: &str) -> anyhow::Result<()> {
// ─── 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",
"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:",
"// 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",
"// 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 /",
"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",
".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;
@@ -157,10 +214,7 @@ const MIN_REASON_LEN: usize = 8;
/// 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> {
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()) {
@@ -204,10 +258,16 @@ fn gate_subagent_tool_call(
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());
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());
return Some(
"content contains assumption pattern; verify against data instead of guessing"
.to_string(),
);
}
}
@@ -231,7 +291,9 @@ fn gate_subagent_tool_call(
if !is_standard {
for pat in EXFIL_PATTERNS {
if cmd.contains(pat) {
return Some(format!("potential data-exfiltration command blocked (matched '{pat}')"));
return Some(format!(
"potential data-exfiltration command blocked (matched '{pat}')"
));
}
}
}
@@ -240,9 +302,21 @@ fn gate_subagent_tool_call(
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 "];
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}"));
@@ -289,7 +363,9 @@ fn generate_workspace_tree(roots: &[std::path::PathBuf]) -> String {
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; }
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();
@@ -331,8 +407,10 @@ fn format_subagent_progress(prefix: &str, text: &str) -> String {
///
/// Return: the concatenated text output, or an `anyhow::Error` if the LLM
/// call fails at any step.
#[allow(clippy::too_many_lines)]
pub fn run_subagent(ctx: &SubagentContext, tx: &mpsc::Sender<SubagentEvent>) -> anyhow::Result<String> {
pub fn run_subagent(
ctx: &SubagentContext,
tx: &mpsc::Sender<SubagentEvent>,
) -> anyhow::Result<String> {
let mut output = String::new();
let mut messages: Vec<ChatMessage> = Vec::new();
@@ -370,17 +448,23 @@ pub fn run_subagent(ctx: &SubagentContext, tx: &mpsc::Sender<SubagentEvent>) ->
// surfaces past a buried WARN log.
if let Err(error) = require_api_key(&api_key, &provider) {
let error = error.to_string();
let _ = tx.blocking_send(SubagentEvent::StepFailed { step: 0, error: error.clone() });
let _ = tx.blocking_send(SubagentEvent::StepFailed {
step: 0,
error: error.clone(),
});
anyhow::bail!(error);
}
let client = crate::service::provider::LlmClient::new(api_key, model, base_url);
for step in 0..ctx.max_steps {
// Check abort flag before each LLM call so a stuck subagent can
// be cancelled from the parent (mirrors main agent behaviour).
if ctx.abort_flag.as_ref().is_some_and(|f| f.load(std::sync::atomic::Ordering::SeqCst)) {
if ctx
.abort_flag
.as_ref()
.is_some_and(|f| f.load(std::sync::atomic::Ordering::SeqCst))
{
let _ = tx.blocking_send(SubagentEvent::StepFailed {
step,
error: "subagent aborted by parent".to_string(),
@@ -403,7 +487,11 @@ pub fn run_subagent(ctx: &SubagentContext, tx: &mpsc::Sender<SubagentEvent>) ->
Some(4096),
|event| -> bool {
// Check abort on every SSE event for responsive cancellation.
if ctx.abort_flag.as_ref().is_some_and(|f| f.load(std::sync::atomic::Ordering::SeqCst)) {
if ctx
.abort_flag
.as_ref()
.is_some_and(|f| f.load(std::sync::atomic::Ordering::SeqCst))
{
return false; // signals provider to abort
}
match event {
@@ -417,7 +505,11 @@ pub fn run_subagent(ctx: &SubagentContext, tx: &mpsc::Sender<SubagentEvent>) ->
let prog = format_subagent_progress("replying", &current_token);
let _ = tx_clone.blocking_send(SubagentEvent::Progress(prog));
}
crate::app::runtime::stream::StreamEvent::Usage { prompt_tokens, completion_tokens, .. } => {
crate::app::runtime::stream::StreamEvent::Usage {
prompt_tokens,
completion_tokens,
..
} => {
// Capture usage so the drain thread can route it
// to the parent's `UsageStats::review_tokens`.
// Last writer wins — providers send exactly one
@@ -433,7 +525,10 @@ pub fn run_subagent(ctx: &SubagentContext, tx: &mpsc::Sender<SubagentEvent>) ->
let (response, returned_usage) = match stream_result {
Ok(result) => result,
Err(e) => {
let is_abort = ctx.abort_flag.as_ref().is_some_and(|f| f.load(std::sync::atomic::Ordering::SeqCst))
let is_abort = ctx
.abort_flag
.as_ref()
.is_some_and(|f| f.load(std::sync::atomic::Ordering::SeqCst))
|| e.to_string().contains("aborted");
let _ = tx.blocking_send(SubagentEvent::StepFailed {
step,
@@ -459,7 +554,8 @@ pub fn run_subagent(ctx: &SubagentContext, tx: &mpsc::Sender<SubagentEvent>) ->
// subagent never tells the parent about the tokens consumed.
let (mut tok_in, mut tok_out) = returned_usage.unwrap_or((0, 0));
if tok_in == 0 {
let prompt_chars: usize = messages.iter()
let prompt_chars: usize = messages
.iter()
.filter_map(|m| m.content.as_deref())
.map(str::len)
.sum();
@@ -475,7 +571,10 @@ pub fn run_subagent(ctx: &SubagentContext, tx: &mpsc::Sender<SubagentEvent>) ->
});
let has_tool_calls = response.tool_calls.is_some()
&& response.tool_calls.as_ref().is_some_and(|tc| !tc.is_empty());
&& response
.tool_calls
.as_ref()
.is_some_and(|tc| !tc.is_empty());
let content = response.content.clone().unwrap_or_default();
@@ -608,7 +707,8 @@ pub fn run_subagent(ctx: &SubagentContext, tx: &mpsc::Sender<SubagentEvent>) ->
for (tool_call, result) in results_vec {
let tool_name = &tool_call.function.name;
let args = crate::dto::chat::tool::sanitize_tool_arguments(&tool_call.function.arguments);
let args =
crate::dto::chat::tool::sanitize_tool_arguments(&tool_call.function.arguments);
let _ = tx.blocking_send(SubagentEvent::ToolCall {
tool: tool_name.clone(),
@@ -617,24 +717,28 @@ pub fn run_subagent(ctx: &SubagentContext, tx: &mpsc::Sender<SubagentEvent>) ->
match result {
Ok(output_text) => {
messages.push(ChatMessage::tool_result(tool_call.id.clone(), output_text.clone()));
messages.push(ChatMessage::tool_result(
tool_call.id.clone(),
output_text.clone(),
));
let _ = tx.blocking_send(SubagentEvent::ToolResult {
tool: tool_name.clone(),
args: args.clone(),
});
let is_readonly = tool_name == "read"
|| tool_name == "view_file"
|| tool_name == "grep"
|| tool_name == "grep_search"
|| tool_name == "glob"
|| tool_name == "dir_list"
let is_readonly = tool_name == "read"
|| tool_name == "view_file"
|| tool_name == "grep"
|| tool_name == "grep_search"
|| tool_name == "glob"
|| tool_name == "dir_list"
|| tool_name == "list_dir";
if is_readonly {
if let Some(ref findings) = ctx.workflow_findings {
if let Ok(mut f) = findings.lock() {
let args_json = serde_json::to_string(&args).unwrap_or_default();
let args_json =
serde_json::to_string(&args).unwrap_or_default();
let mut shared_text = output_text;
if shared_text.len() > 50_000 {
shared_text.truncate(50_000);
@@ -41,16 +41,9 @@ impl AgentDefinition {
}
/// Builder method: set the maximum step count for this agent.
#[allow(dead_code)]
pub fn with_max_steps(mut self, steps: usize) -> Self {
self.max_steps = Some(steps);
self
}
/// Builder method: set the temperature for this agent.
#[allow(dead_code)]
pub fn with_temperature(mut self, temp: f32) -> Self {
self.temperature = Some(temp);
self
}
}
@@ -7,9 +7,9 @@
//! Core Intelligence can omit or reshape — and always runs after any
//! hive-mind convergence completes.
use crate::app::workflow::hive_mind::NodeReport;
use zesdex_cms::domain::memory::Memory;
use std::fmt::Write as _;
use std::path::{Path, PathBuf};
use zesdex_cms::domain::memory::Memory;
/// Write a markdown report of one hive-mind convergence to
/// `<workspace_root>/docs/runs/<timestamp>-<slug>.md`.
+150 -141
View File
@@ -210,20 +210,23 @@ fn format_tool_call_progress(prefix: &str, tool: &str, args: &serde_json::Value)
/// a stuck stage from blocking the entire pipeline forever.
///
/// Return: the agent's text output, or an error on failure.
fn spawn_single_agent(
agent_id: &str,
agent_name: &str,
prompt: &str,
role: &str,
allowed_tools: Option<Vec<String>>,
findings_snapshot: &[String],
findings: &Arc<Mutex<Vec<String>>>,
abort_flag: &Option<Arc<AtomicBool>>,
live: Option<&LiveStateFn>,
session_dir: &std::path::Path,
workspaces: &[std::path::PathBuf],
timeout_ms: Option<u64>,
) -> anyhow::Result<String> {
/// Bundled context for spawning a single subagent.
pub(crate) struct SpawnCtx<'a> {
pub agent_id: &'a str,
pub agent_name: &'a str,
pub prompt: &'a str,
pub role: &'a str,
pub allowed_tools: Option<Vec<String>>,
pub findings_snapshot: &'a [String],
pub findings: &'a Arc<Mutex<Vec<String>>>,
pub abort_flag: &'a Option<Arc<AtomicBool>>,
pub live: Option<&'a LiveStateFn>,
pub session_dir: &'a std::path::Path,
pub workspaces: &'a [std::path::PathBuf],
pub timeout_ms: Option<u64>,
}
fn spawn_single_agent(sp: SpawnCtx<'_>) -> anyhow::Result<String> {
use crate::app::subagent::context::build_subagent_context;
use crate::app::subagent::engine::run_subagent;
use crate::app::subagent::spawn::AgentDefinition;
@@ -233,10 +236,10 @@ fn spawn_single_agent(
// Notify UI: this agent is now running.
// Pass both the unique agent_id (UUID for stable key) and agent_name
// (human-readable display name, e.g. a hive-mind node designation).
if let Some(f) = live {
if let Some(f) = &sp.live {
f(
agent_id.to_string(),
agent_name.to_string(),
sp.agent_id.to_string(),
sp.agent_name.to_string(),
AgentStatus {
state: AgentState::Running,
started_at: Some(started_at),
@@ -247,20 +250,20 @@ fn spawn_single_agent(
);
}
let mut def = AgentDefinition::new(agent_name.to_string(), role.to_string());
if let Some(tools) = allowed_tools {
def = def.with_allowed_tools(tools);
let mut def = AgentDefinition::new(sp.agent_name.to_string(), sp.role.to_string());
if let Some(tools) = &sp.allowed_tools {
def = def.with_allowed_tools(tools.clone());
}
let mut ctx = build_subagent_context(&def);
ctx.session_dir = session_dir.to_path_buf();
ctx.workspaces = workspaces.to_vec();
ctx.session_dir = sp.session_dir.to_path_buf();
ctx.workspaces = sp.workspaces.to_vec();
let findings_section = if findings_snapshot.is_empty() {
let findings_section = if sp.findings_snapshot.is_empty() {
String::new()
} else {
format!(
"\n\nFindings from sibling drones in this Hive run:\n{}",
findings_snapshot
sp.findings_snapshot
.iter()
.enumerate()
.map(|(i, f)| format!("{}. {}", i + 1, f))
@@ -269,20 +272,20 @@ fn spawn_single_agent(
)
};
ctx.system_prompt = format!("{prompt}{findings_section}");
ctx.system_prompt = format!("{}{}", sp.prompt, findings_section);
// Link the shared findings Arc so note_finding calls within this
// subagent write into the same vec visible to sibling agents.
ctx.workflow_findings = Some(findings.clone());
ctx.abort_flag.clone_from(abort_flag);
ctx.workflow_findings = Some(sp.findings.clone());
ctx.abort_flag.clone_from(sp.abort_flag);
// Create an mpsc channel and drain events in a background thread.
// The drain thread also pushes intra-division progress updates to the
// live callback (current tool being executed), so the TUI panel shows
// real-time "editing X" or "running build" instead of just "Running…".
let (tx, rx) = tokio::sync::mpsc::channel(64);
let drain_agent_id = agent_id.to_string();
let drain_agent_name = agent_name.to_string();
let drain_live = live.cloned();
let drain_agent_id = sp.agent_id.to_string();
let drain_agent_name = sp.agent_name.to_string();
let drain_live = sp.live.cloned();
let drain_started_at = started_at;
let _drain_thread = std::thread::spawn(move || {
use crate::app::subagent::event::SubagentEvent;
@@ -380,11 +383,12 @@ fn spawn_single_agent(
});
// Check abort before even starting the subagent.
if abort_flag
if sp
.abort_flag
.as_ref()
.is_some_and(|f| f.load(Ordering::SeqCst))
{
anyhow::bail!("subagent '{agent_name}' aborted before start");
anyhow::bail!("subagent '{}' aborted before start", sp.agent_name);
}
// Run subagent on a separate thread so the abort flag can be polled.
@@ -393,14 +397,14 @@ fn spawn_single_agent(
let (done_tx, done_rx) = std::sync::mpsc::channel::<anyhow::Result<String>>();
let bg_ctx = ctx;
let bg_tx = tx;
let bg_name = agent_name.to_string();
let bg_abort = abort_flag.clone();
let bg_name = sp.agent_name.to_string();
let bg_abort = sp.abort_flag.clone();
std::thread::spawn(move || {
let _ = done_tx.send(run_subagent(&bg_ctx, &bg_tx));
});
let poll_interval = Duration::from_millis(200);
let result = if let Some(timeout) = timeout_ms {
let result = if let Some(timeout) = sp.timeout_ms {
let deadline = Duration::from_millis(timeout);
let mut elapsed = Duration::ZERO;
loop {
@@ -431,7 +435,7 @@ fn spawn_single_agent(
let completed_at = chrono::Utc::now().timestamp_millis();
// Notify UI: agent completed or failed
if let Some(f) = live {
if let Some(f) = &sp.live {
let summary_from = |text: &str| {
text.lines()
.next()
@@ -444,8 +448,8 @@ fn spawn_single_agent(
Ok(text) => {
let summary = summary_from(text);
f(
agent_id.to_string(),
agent_name.to_string(),
sp.agent_id.to_string(),
sp.agent_name.to_string(),
AgentStatus {
state: AgentState::Completed,
started_at: Some(started_at),
@@ -457,8 +461,8 @@ fn spawn_single_agent(
}
Err(e) => {
f(
agent_id.to_string(),
agent_name.to_string(),
sp.agent_id.to_string(),
sp.agent_name.to_string(),
AgentStatus {
state: AgentState::Failed,
started_at: Some(started_at),
@@ -476,6 +480,20 @@ fn spawn_single_agent(
type ParallelResult = (usize, anyhow::Result<Vec<String>>);
/// Bundled context for executing a script primitive.
pub(crate) struct PrimitiveCtx<'a> {
pub primitive: &'a ScriptPrimitive,
pub args: &'a HashMap<String, String>,
pub concurrency_cap: usize,
pub continue_on_error: bool,
pub abort_flag: &'a Option<Arc<AtomicBool>>,
pub live: Option<&'a LiveStateFn>,
pub session_dir: &'a std::path::Path,
pub workspaces: &'a [std::path::PathBuf],
pub findings: &'a Arc<Mutex<Vec<String>>>,
pub timeout_ms: Option<u64>,
}
/// Recursively execute a `ScriptPrimitive` tree, respecting an overall
/// concurrency cap for parallel branches.
///
@@ -497,22 +515,11 @@ type ParallelResult = (usize, anyhow::Result<Vec<String>>);
///
/// Return: a `Vec<String>` of all agent outputs (or error strings) in
/// the order they were submitted.
pub fn execute_primitive(
primitive: &ScriptPrimitive,
args: &HashMap<String, String>,
concurrency_cap: usize,
continue_on_error: bool,
abort_flag: &Option<Arc<AtomicBool>>,
live: Option<&LiveStateFn>,
session_dir: &std::path::Path,
workspaces: &[std::path::PathBuf],
findings: &Arc<Mutex<Vec<String>>>,
timeout_ms: Option<u64>,
) -> anyhow::Result<Vec<String>> {
match primitive {
pub fn execute_primitive(pc: PrimitiveCtx<'_>) -> anyhow::Result<Vec<String>> {
match pc.primitive {
ScriptPrimitive::Agent(prompt) => {
let mut resolved_args = args.clone();
let findings_snapshot = findings.lock().map(|f| f.clone()).unwrap_or_default();
let mut resolved_args = pc.args.clone();
let findings_snapshot = pc.findings.lock().map(|f| f.clone()).unwrap_or_default();
if !resolved_args.contains_key("findings") {
let formatted_findings = if findings_snapshot.is_empty() {
"None".to_string()
@@ -529,23 +536,23 @@ pub fn execute_primitive(
let resolved = resolve_template(prompt, &resolved_args);
let agent_id = uuid::Uuid::new_v4().to_string();
let agent_name = resolved.chars().take(40).collect::<String>();
match spawn_single_agent(
&agent_id,
&agent_name,
&resolved,
"coder",
None,
&findings_snapshot,
findings,
abort_flag,
live,
session_dir,
workspaces,
timeout_ms,
) {
match spawn_single_agent(SpawnCtx {
agent_id: &agent_id,
agent_name: &agent_name,
prompt: &resolved,
role: "coder",
allowed_tools: None,
findings_snapshot: &findings_snapshot,
findings: pc.findings,
abort_flag: pc.abort_flag,
live: pc.live,
session_dir: pc.session_dir,
workspaces: pc.workspaces,
timeout_ms: pc.timeout_ms,
}) {
Ok(text) => Ok(vec![text]),
Err(e) => {
if continue_on_error {
if pc.continue_on_error {
Ok(vec![format!("agent error: {}", e)])
} else {
Err(e)
@@ -559,8 +566,8 @@ pub fn execute_primitive(
node_id,
tool_scope,
} => {
let mut resolved_args = args.clone();
let findings_snapshot = findings.lock().map(|f| f.clone()).unwrap_or_default();
let mut resolved_args = pc.args.clone();
let findings_snapshot = pc.findings.lock().map(|f| f.clone()).unwrap_or_default();
if !resolved_args.contains_key("findings") {
let formatted_findings = if findings_snapshot.is_empty() {
"None".to_string()
@@ -580,20 +587,20 @@ pub fn execute_primitive(
tracing::debug!("[hive] deploying drone {node_id}: {truncated}");
let agent_name = format!("{node_id}: {truncated}");
let allowed_tools = crate::app::subagent::division::tool_scope::tools_for(tool_scope);
match spawn_single_agent(
&agent_id,
&agent_name,
&resolved,
node_id,
Some(allowed_tools),
&findings_snapshot,
findings,
abort_flag,
live,
session_dir,
workspaces,
timeout_ms,
) {
match spawn_single_agent(SpawnCtx {
agent_id: &agent_id,
agent_name: &agent_name,
prompt: &resolved,
role: node_id,
allowed_tools: Some(allowed_tools),
findings_snapshot: &findings_snapshot,
findings: pc.findings,
abort_flag: pc.abort_flag,
live: pc.live,
session_dir: pc.session_dir,
workspaces: pc.workspaces,
timeout_ms: pc.timeout_ms,
}) {
Ok(text) => {
tracing::debug!(
"[hive] drone {node_id} completed — merging into collective state"
@@ -604,14 +611,14 @@ pub fn execute_primitive(
// still running (via read_findings) or any drone spawned
// afterward sees this immediately, making the collective
// state genuinely continuous rather than batch-synced.
if let Ok(mut f) = findings.lock() {
if let Ok(mut f) = pc.findings.lock() {
f.push(format!("[{node_id}]: {text}"));
}
Ok(vec![text])
}
Err(e) => {
tracing::warn!("[hive] drone {node_id} failed: {e}");
if continue_on_error {
if pc.continue_on_error {
Ok(vec![format!("drone error: {}", e)])
} else {
Err(e)
@@ -626,7 +633,7 @@ pub fn execute_primitive(
// independent subagents work simultaneously.
// Each branch shares the same `findings` Arc so note_finding
// calls within any branch are visible to all other branches.
let semaphore = Arc::new(Semaphore::new(concurrency_cap.max(1)));
let semaphore = Arc::new(Semaphore::new(pc.concurrency_cap.max(1)));
let results: Arc<Mutex<Vec<ParallelResult>>> = Arc::new(Mutex::new(Vec::new()));
let handles: Vec<_> = scripts
@@ -634,31 +641,32 @@ pub fn execute_primitive(
.enumerate()
.map(|(idx, script)| {
let script = script.clone();
let args = args.clone();
let args = pc.args.clone();
let sem = Arc::clone(&semaphore);
let results = Arc::clone(&results);
let cap = concurrency_cap;
let abort = abort_flag.clone();
let live_clone = live.cloned();
let session_dir = session_dir.to_path_buf();
let workspaces = workspaces.to_vec();
let findings = Arc::clone(findings);
let to = timeout_ms;
let cap = pc.concurrency_cap;
let continue_on_error = pc.continue_on_error;
let abort = pc.abort_flag.clone();
let live_clone = pc.live.cloned();
let session_dir = pc.session_dir.to_path_buf();
let workspaces = pc.workspaces.to_vec();
let findings = Arc::clone(pc.findings);
let to = pc.timeout_ms;
std::thread::spawn(move || {
let _permit = sem.acquire();
let result = execute_primitive(
&script,
&args,
cap,
let result = execute_primitive(PrimitiveCtx {
primitive: &script,
args: &args,
concurrency_cap: cap,
continue_on_error,
&abort,
live_clone.as_ref(),
&session_dir,
&workspaces,
&findings,
to,
);
abort_flag: &abort,
live: live_clone.as_ref(),
session_dir: &session_dir,
workspaces: &workspaces,
findings: &findings,
timeout_ms: to,
});
if let Ok(mut locked) = results.lock() {
locked.push((idx, result));
}
@@ -699,31 +707,32 @@ pub fn execute_primitive(
for (idx, script) in scripts.iter().enumerate() {
// Check abort before each pipeline stage so we don't
// launch the next division after the user cancelled.
if abort_flag
if pc
.abort_flag
.as_ref()
.is_some_and(|f| f.load(Ordering::SeqCst))
{
if continue_on_error {
if pc.continue_on_error {
all.push(format!("pipeline aborted at stage {idx}"));
break;
}
anyhow::bail!("pipeline aborted by user at stage {idx}");
}
match execute_primitive(
script,
args,
concurrency_cap,
continue_on_error,
abort_flag,
live,
session_dir,
workspaces,
findings,
timeout_ms,
) {
match execute_primitive(PrimitiveCtx {
primitive: script,
args: pc.args,
concurrency_cap: pc.concurrency_cap,
continue_on_error: pc.continue_on_error,
abort_flag: pc.abort_flag,
live: pc.live,
session_dir: pc.session_dir,
workspaces: pc.workspaces,
findings: pc.findings,
timeout_ms: pc.timeout_ms,
}) {
Ok(outputs) => all.extend(outputs),
Err(e) => {
if continue_on_error {
if pc.continue_on_error {
all.push(format!("pipeline stage {idx} error: {e}"));
} else {
return Err(e);
@@ -737,18 +746,18 @@ pub fn execute_primitive(
ScriptPrimitive::Phase {
name: _name,
script,
} => execute_primitive(
script,
args,
concurrency_cap,
continue_on_error,
abort_flag,
live,
session_dir,
workspaces,
findings,
timeout_ms,
),
} => execute_primitive(PrimitiveCtx {
primitive: script,
args: pc.args,
concurrency_cap: pc.concurrency_cap,
continue_on_error: pc.continue_on_error,
abort_flag: pc.abort_flag,
live: pc.live,
session_dir: pc.session_dir,
workspaces: pc.workspaces,
findings: pc.findings,
timeout_ms: pc.timeout_ms,
}),
}
}
@@ -792,18 +801,18 @@ pub fn run_workflow_tracked(
};
let findings = Arc::new(Mutex::new(Vec::new()));
let results = execute_primitive(
&script.script,
let results = execute_primitive(PrimitiveCtx {
primitive: &script.script,
args,
concurrency_cap,
script.options.continue_on_error,
continue_on_error: script.options.continue_on_error,
abort_flag,
live,
session_dir,
workspaces,
&findings,
script.options.timeout_ms,
)?;
findings: &findings,
timeout_ms: script.options.timeout_ms,
})?;
let summary = if results.is_empty() {
"workflow completed with no output".to_string()
@@ -25,7 +25,7 @@
//! Synthesis node reads the complete collective state and converges it
//! into one unified voice — returned to LO and persisted to docs/runs/*.md.
//! ```
use crate::app::workflow::engine::{execute_primitive, AgentStatus, LiveStateFn};
use crate::app::workflow::engine::{execute_primitive, AgentStatus, LiveStateFn, PrimitiveCtx};
use crate::app::workflow::script::ScriptPrimitive;
use serde::Deserialize;
use std::collections::HashMap;
@@ -211,18 +211,18 @@ fn execute_cycle(
let args: HashMap<String, String> = HashMap::new();
let abort_owned = ctx.abort_flag.cloned();
let results = execute_primitive(
&cycle_primitive,
&args,
directives.len().clamp(1, ctx.max_cycle_concurrency),
true,
&abort_owned,
ctx.live,
ctx.session_dir,
ctx.workspaces,
ctx.collective_state,
ctx.node_timeout_ms,
)?;
let results = execute_primitive(PrimitiveCtx {
primitive: &cycle_primitive,
args: &args,
concurrency_cap: directives.len().clamp(1, ctx.max_cycle_concurrency),
continue_on_error: true,
abort_flag: &abort_owned,
live: ctx.live,
session_dir: ctx.session_dir,
workspaces: ctx.workspaces,
findings: ctx.collective_state,
timeout_ms: ctx.node_timeout_ms,
})?;
let mut reports = Vec::new();
for (node_id, output) in node_ids.iter().zip(results.iter()) {
@@ -280,9 +280,10 @@ pub fn run_hive_mind(
}
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 settings =
zesdex_cms::infrastructure::persistence::settings_repo::JsonSettingsRepository::new()
.load(&store_base_dir)
.unwrap_or_default();
let node_timeout_ms = Some(settings.hive_mind_node_timeout_ms);
let max_cycle_concurrency = settings.workflow_max_concurrency.max(1);
@@ -404,18 +405,18 @@ fn synthesize_consensus(
let args: HashMap<String, String> = HashMap::new();
let abort_owned: Option<Arc<AtomicBool>> = abort_flag.cloned();
let results = execute_primitive(
&synthesis,
&args,
1,
false,
&abort_owned,
let results = execute_primitive(PrimitiveCtx {
primitive: &synthesis,
args: &args,
concurrency_cap: 1,
continue_on_error: false,
abort_flag: &abort_owned,
live,
session_dir,
workspaces,
collective_state,
node_timeout_ms,
)?;
findings: collective_state,
timeout_ms: node_timeout_ms,
})?;
Ok(results.into_iter().next().unwrap_or_default())
}