Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
88c2a7d000 | ||
|
|
3b25e3898f | ||
|
|
d23d3855ec | ||
|
|
104af3abb9 | ||
|
|
62fa85867c | ||
|
|
f75ff740ac | ||
|
|
e349a35716 | ||
|
|
f1f58b9996 |
@@ -1,3 +1,31 @@
|
|||||||
|
## [1.21.2](https://github.com/asepharyana/zesdex/compare/v1.21.1...v1.21.2) (2026-08-28)
|
||||||
|
|
||||||
|
|
||||||
|
### Performance Improvements
|
||||||
|
|
||||||
|
* **agent:** symbol index tak pegang mutex global saat rebuild I/O ([3b25e38](https://github.com/asepharyana/zesdex/commit/3b25e3898f9ba2fc1c58b991ad95a8c0bbe98601))
|
||||||
|
|
||||||
|
## [1.21.1](https://github.com/asepharyana/zesdex/compare/v1.21.0...v1.21.1) (2026-08-28)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **agent:** semantic_search symbol index workspace-aware ([104af3a](https://github.com/asepharyana/zesdex/commit/104af3abb9e626c5d00ea87d523248d606d523ee))
|
||||||
|
|
||||||
|
# [1.21.0](https://github.com/asepharyana/zesdex/compare/v1.20.2...v1.21.0) (2026-08-28)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* **agent:** hive-mind consensus synthesis pakai LLM nyata ([f75ff74](https://github.com/asepharyana/zesdex/commit/f75ff740ac2fa63340656e1e8215440f5e48063d))
|
||||||
|
|
||||||
|
## [1.20.2](https://github.com/asepharyana/zesdex/compare/v1.20.1...v1.20.2) (2026-08-28)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **agent:** recall search + memory_dir fallback + bersihkan dead llm_client ([f1f58b9](https://github.com/asepharyana/zesdex/commit/f1f58b9996f8eb58871d44fdd41c2d863874f253))
|
||||||
|
|
||||||
## [1.20.1](https://github.com/asepharyana/zesdex/compare/v1.20.0...v1.20.1) (2026-08-28)
|
## [1.20.1](https://github.com/asepharyana/zesdex/compare/v1.20.0...v1.20.1) (2026-08-28)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Generated
+11
-11
@@ -4862,7 +4862,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zesdex-api"
|
name = "zesdex-api"
|
||||||
version = "1.19.6"
|
version = "1.21.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"argon2",
|
"argon2",
|
||||||
@@ -4885,7 +4885,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zesdex-application"
|
name = "zesdex-application"
|
||||||
version = "1.19.6"
|
version = "1.21.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"base64",
|
"base64",
|
||||||
@@ -4903,7 +4903,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zesdex-bootstrap"
|
name = "zesdex-bootstrap"
|
||||||
version = "1.19.6"
|
version = "1.21.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"chrono",
|
"chrono",
|
||||||
@@ -4920,7 +4920,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zesdex-daemon"
|
name = "zesdex-daemon"
|
||||||
version = "1.19.6"
|
version = "1.21.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"base64",
|
"base64",
|
||||||
@@ -4944,7 +4944,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zesdex-domain"
|
name = "zesdex-domain"
|
||||||
version = "1.19.6"
|
version = "1.21.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"base64",
|
"base64",
|
||||||
@@ -4960,7 +4960,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zesdex-gateway"
|
name = "zesdex-gateway"
|
||||||
version = "1.19.6"
|
version = "1.21.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"axum",
|
"axum",
|
||||||
@@ -4987,7 +4987,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zesdex-grpc"
|
name = "zesdex-grpc"
|
||||||
version = "1.19.6"
|
version = "1.21.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"axum",
|
"axum",
|
||||||
@@ -5004,7 +5004,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zesdex-infrastructure"
|
name = "zesdex-infrastructure"
|
||||||
version = "1.19.6"
|
version = "1.21.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"argon2",
|
"argon2",
|
||||||
@@ -5052,7 +5052,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zesdex-tui"
|
name = "zesdex-tui"
|
||||||
version = "1.19.6"
|
version = "1.21.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"base64",
|
"base64",
|
||||||
@@ -5078,7 +5078,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zesdex-web"
|
name = "zesdex-web"
|
||||||
version = "1.19.6"
|
version = "1.21.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"axum",
|
"axum",
|
||||||
@@ -5098,7 +5098,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zesdex-ws"
|
name = "zesdex-ws"
|
||||||
version = "1.19.6"
|
version = "1.21.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"axum",
|
"axum",
|
||||||
|
|||||||
+1
-1
@@ -15,7 +15,7 @@ members = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
version = "1.20.1"
|
version = "1.21.2"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["asepharyana <superaseph@gmail.com>"]
|
authors = ["asepharyana <superaseph@gmail.com>"]
|
||||||
|
|
||||||
|
|||||||
@@ -66,6 +66,14 @@ impl ToolCtxBuilder {
|
|||||||
self.session_dir = v;
|
self.session_dir = v;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
pub fn memory_dir(mut self, v: PathBuf) -> Self {
|
||||||
|
self.memory_dir = v;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
pub fn worktrees_dir(mut self, v: PathBuf) -> Self {
|
||||||
|
self.worktrees_dir = v;
|
||||||
|
self
|
||||||
|
}
|
||||||
pub fn workspaces(mut self, v: Vec<PathBuf>) -> Self {
|
pub fn workspaces(mut self, v: Vec<PathBuf>) -> Self {
|
||||||
self.workspaces = v;
|
self.workspaces = v;
|
||||||
self
|
self
|
||||||
|
|||||||
@@ -43,7 +43,8 @@ impl Tool for Forget {
|
|||||||
let name = crate::tools::arg_str(args, "name")?;
|
let name = crate::tools::arg_str(args, "name")?;
|
||||||
info!(name, "forget invoked");
|
info!(name, "forget invoked");
|
||||||
let repo = crate::persistence::cms::memory_repo::MarkdownMemoryRepository::new();
|
let repo = crate::persistence::cms::memory_repo::MarkdownMemoryRepository::new();
|
||||||
repo.delete(&ctx.memory_dir, &name)?;
|
let memory_dir = crate::tools::memory::resolve_memory_dir(&ctx.memory_dir);
|
||||||
|
repo.delete(&memory_dir, &name)?;
|
||||||
info!(name, "memory deleted");
|
info!(name, "memory deleted");
|
||||||
Ok(format!("Memory '{}' deleted", name))
|
Ok(format!("Memory '{}' deleted", name))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,21 @@
|
|||||||
//! Memory management tools — remember, recall, forget.
|
//! Memory management tools — remember, recall, forget.
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub mod forget;
|
pub mod forget;
|
||||||
pub mod recall;
|
pub mod recall;
|
||||||
pub mod remember;
|
pub mod remember;
|
||||||
|
|
||||||
|
/// Resolve the directory the memory tools should read/write.
|
||||||
|
///
|
||||||
|
/// Prefer an explicitly-configured `ToolCtx.memory_dir`. If that is empty
|
||||||
|
/// (a `ToolCtx` is often built without setting `memory_dir`), fall back to
|
||||||
|
/// the canonical persistent memory location from `Store` so memories are not
|
||||||
|
/// silently written into the current working directory.
|
||||||
|
pub fn resolve_memory_dir(ctx_memory_dir: &std::path::Path) -> PathBuf {
|
||||||
|
if ctx_memory_dir.as_os_str().is_empty() {
|
||||||
|
zesdex_domain::core::Store::new().memory_dir
|
||||||
|
} else {
|
||||||
|
ctx_memory_dir.to_path_buf()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -45,21 +45,132 @@ impl Tool for Recall {
|
|||||||
#[instrument(skip(self, ctx, args))]
|
#[instrument(skip(self, ctx, args))]
|
||||||
fn run(&self, ctx: &ToolCtx, args: &Value) -> Result<String> {
|
fn run(&self, ctx: &ToolCtx, args: &Value) -> Result<String> {
|
||||||
let repo = crate::persistence::cms::memory_repo::MarkdownMemoryRepository::new();
|
let repo = crate::persistence::cms::memory_repo::MarkdownMemoryRepository::new();
|
||||||
|
let memory_dir = crate::tools::memory::resolve_memory_dir(&ctx.memory_dir);
|
||||||
|
|
||||||
let specific_name = args.get("name").and_then(|v| v.as_str());
|
let specific_name = args.get("name").and_then(|v| v.as_str());
|
||||||
|
let search = args.get("search").and_then(|v| v.as_str());
|
||||||
|
|
||||||
if let Some(name) = specific_name {
|
if let Some(name) = specific_name {
|
||||||
info!(name, "recall loading specific memory");
|
info!(name, "recall loading specific memory");
|
||||||
let memory = repo.load(&ctx.memory_dir, name)?;
|
let memory = repo.load(&memory_dir, name)?;
|
||||||
Ok(serde_json::to_string_pretty(&memory)?)
|
return Ok(serde_json::to_string_pretty(&memory)?);
|
||||||
} else {
|
|
||||||
info!("recall listing all memories");
|
|
||||||
let names = repo.list(&ctx.memory_dir)?;
|
|
||||||
if names.is_empty() {
|
|
||||||
info!("no memories found");
|
|
||||||
return Ok("No memories saved yet".to_string());
|
|
||||||
}
|
|
||||||
Ok(format!("Available memories:\n{}", names.join("\n")))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(query) = search {
|
||||||
|
let query = query.trim().to_lowercase();
|
||||||
|
info!(search = %query, "recall searching memories");
|
||||||
|
if query.is_empty() {
|
||||||
|
return Ok("Search query is empty".to_string());
|
||||||
|
}
|
||||||
|
let names = repo.list(&memory_dir)?;
|
||||||
|
let mut matches: Vec<String> = Vec::new();
|
||||||
|
for name in &names {
|
||||||
|
// Load each memory and match against name/description/content.
|
||||||
|
if let Ok(m) = repo.load(&memory_dir, name) {
|
||||||
|
let haystack =
|
||||||
|
format!("{} {} {}", m.name, m.description, m.content).to_lowercase();
|
||||||
|
if haystack.contains(&query) {
|
||||||
|
matches.push(m.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if matches.is_empty() {
|
||||||
|
return Ok(format!("No memories match '{query}'"));
|
||||||
|
}
|
||||||
|
return Ok(format!(
|
||||||
|
"Memories matching '{query}' ({}):\n{}",
|
||||||
|
matches.len(),
|
||||||
|
matches.join("\n")
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
info!("recall listing all memories");
|
||||||
|
let names = repo.list(&memory_dir)?;
|
||||||
|
if names.is_empty() {
|
||||||
|
info!("no memories found");
|
||||||
|
return Ok("No memories saved yet".to_string());
|
||||||
|
}
|
||||||
|
Ok(format!("Available memories:\n{}", names.join("\n")))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::tools::ToolCtxBuilder;
|
||||||
|
use zesdex_domain::cms::MemoryRepository;
|
||||||
|
|
||||||
|
fn save_mem(name: &str, description: &str, content: &str, dir: &std::path::Path) {
|
||||||
|
let repo = crate::persistence::cms::memory_repo::MarkdownMemoryRepository::new();
|
||||||
|
let memory = zesdex_domain::cms::Memory {
|
||||||
|
name: name.to_string(),
|
||||||
|
description: description.to_string(),
|
||||||
|
content: content.to_string(),
|
||||||
|
kind: "reference".to_string(),
|
||||||
|
created_at: 0,
|
||||||
|
updated_at: 0,
|
||||||
|
outcome: None,
|
||||||
|
lifecycle: "active".to_string(),
|
||||||
|
scope: None,
|
||||||
|
before_snippet: None,
|
||||||
|
after_snippet: None,
|
||||||
|
provenances: Vec::new(),
|
||||||
|
};
|
||||||
|
repo.save(dir, &memory).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn search_filters_memories_by_keyword() {
|
||||||
|
let dir = std::env::temp_dir().join(format!("zdx-mem-test-{}", uuid::Uuid::new_v4()));
|
||||||
|
std::fs::create_dir_all(&dir).unwrap();
|
||||||
|
save_mem(
|
||||||
|
"rust-concurrency",
|
||||||
|
"tokio spawn patterns",
|
||||||
|
"how to use async tasks",
|
||||||
|
&dir,
|
||||||
|
);
|
||||||
|
save_mem(
|
||||||
|
"docker-deploy",
|
||||||
|
"deploy via compose",
|
||||||
|
"container orchestration",
|
||||||
|
&dir,
|
||||||
|
);
|
||||||
|
|
||||||
|
let tool_ctx = ToolCtxBuilder::default().memory_dir(dir.clone()).build();
|
||||||
|
let args = serde_json::json!({ "search": "tokio" });
|
||||||
|
|
||||||
|
let out = Recall.run(&tool_ctx, &args).unwrap();
|
||||||
|
assert!(
|
||||||
|
out.contains("rust-concurrency"),
|
||||||
|
"should match rust-concurrency, got: {out}"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!out.contains("docker-deploy"),
|
||||||
|
"docker-deploy should not match tokio"
|
||||||
|
);
|
||||||
|
|
||||||
|
// A query with no match reports so.
|
||||||
|
let no_match = Recall
|
||||||
|
.run(&tool_ctx, &serde_json::json!({ "search": "zzzznope" }))
|
||||||
|
.unwrap();
|
||||||
|
assert!(no_match.contains("No memories match"), "{no_match}");
|
||||||
|
|
||||||
|
std::fs::remove_dir_all(&dir).ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn resolve_memory_dir_falls_back_to_store_when_empty() {
|
||||||
|
// An empty ToolCtx.memory_dir is resolved to the canonical Store path.
|
||||||
|
let resolved = crate::tools::memory::resolve_memory_dir(std::path::Path::new(""));
|
||||||
|
assert!(!resolved.as_os_str().is_empty());
|
||||||
|
assert!(
|
||||||
|
resolved.ends_with("memory"),
|
||||||
|
"expected memory dir, got {resolved:?}"
|
||||||
|
);
|
||||||
|
|
||||||
|
// An explicit memory_dir is preserved.
|
||||||
|
let explicit =
|
||||||
|
crate::tools::memory::resolve_memory_dir(std::path::Path::new("/tmp/custom-memory"));
|
||||||
|
assert_eq!(explicit, std::path::Path::new("/tmp/custom-memory"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,7 +81,8 @@ impl Tool for Remember {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let repo = crate::persistence::cms::memory_repo::MarkdownMemoryRepository::new();
|
let repo = crate::persistence::cms::memory_repo::MarkdownMemoryRepository::new();
|
||||||
repo.save(&ctx.memory_dir, &memory)?;
|
let memory_dir = crate::tools::memory::resolve_memory_dir(&ctx.memory_dir);
|
||||||
|
repo.save(&memory_dir, &memory)?;
|
||||||
|
|
||||||
info!(name, "memory saved");
|
info!(name, "memory saved");
|
||||||
Ok(format!("Memory '{}' saved", name))
|
Ok(format!("Memory '{}' saved", name))
|
||||||
|
|||||||
@@ -121,7 +121,66 @@ pub struct CodeSymbol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The in-memory symbol index, shared via a global static.
|
/// The in-memory symbol index, shared via a global static.
|
||||||
static SYMBOL_INDEX: Mutex<Option<SymbolIndex>> = Mutex::new(None);
|
///
|
||||||
|
/// Keyed by workspace path: each workspace owns its own `SymbolIndex`, so
|
||||||
|
/// searching one repo never leaks stale symbols from another, and a rebuild
|
||||||
|
/// triggered for workspace A cannot clobber the index of B. The lock is only
|
||||||
|
/// ever held briefly (to check/insert/look up) — never across the I/O-heavy
|
||||||
|
/// walk in `rebuild`, which runs locally and is swapped in under a short lock.
|
||||||
|
static SYMBOL_INDEX: std::sync::OnceLock<Mutex<HashMap<String, SymbolIndex>>> =
|
||||||
|
std::sync::OnceLock::new();
|
||||||
|
|
||||||
|
/// Access the (lazily initialised) global per-workspace symbol index map.
|
||||||
|
fn symbol_index_map() -> &'static Mutex<HashMap<String, SymbolIndex>> {
|
||||||
|
SYMBOL_INDEX.get_or_init(|| Mutex::new(HashMap::new()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ensure the per-workspace symbol index is built, returning the symbol count.
|
||||||
|
///
|
||||||
|
/// If `force` is true, or the workspace has no cached (non-empty) index yet,
|
||||||
|
/// the index is rebuilt. The rebuild itself runs OUTSIDE the global lock
|
||||||
|
/// (the walk can take seconds on a large repo), then the result is stored
|
||||||
|
/// under a short lock so concurrent searches never block on the I/O. Returns
|
||||||
|
/// the number of symbols now cached for the workspace.
|
||||||
|
fn ensure_symbol_index(workspace: &str, force: bool) -> Result<usize> {
|
||||||
|
let ready = {
|
||||||
|
let map = symbol_index_map()
|
||||||
|
.lock()
|
||||||
|
.map_err(|e| anyhow::anyhow!("index lock failed: {e}"))?;
|
||||||
|
!force && map.get(workspace).is_some_and(|i| !i.is_empty())
|
||||||
|
};
|
||||||
|
|
||||||
|
if !ready {
|
||||||
|
// Rebuild locally, off the global lock (I/O heavy).
|
||||||
|
let mut fresh = SymbolIndex::new();
|
||||||
|
let count = fresh.rebuild(workspace)?;
|
||||||
|
// Swap in under a short lock; keep an existing non-empty index if a
|
||||||
|
// concurrent rebuild already populated this workspace.
|
||||||
|
let mut map = symbol_index_map()
|
||||||
|
.lock()
|
||||||
|
.map_err(|e| anyhow::anyhow!("index lock failed: {e}"))?;
|
||||||
|
if map.get(workspace).is_none_or(|i| i.is_empty()) {
|
||||||
|
map.insert(workspace.to_string(), fresh);
|
||||||
|
}
|
||||||
|
return Ok(count);
|
||||||
|
}
|
||||||
|
|
||||||
|
let map = symbol_index_map()
|
||||||
|
.lock()
|
||||||
|
.map_err(|e| anyhow::anyhow!("index lock failed: {e}"))?;
|
||||||
|
Ok(map.get(workspace).map_or(0, |i| i.len()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Lock and return a borrow to the global per-workspace symbol index map.
|
||||||
|
///
|
||||||
|
/// The caller must have called [`ensure_symbol_index`] first, then looks up
|
||||||
|
/// its workspace key; the lookup is short and in-memory, so holding the guard
|
||||||
|
/// for the search is fine.
|
||||||
|
fn symbol_index() -> Result<std::sync::MutexGuard<'static, HashMap<String, SymbolIndex>>> {
|
||||||
|
symbol_index_map()
|
||||||
|
.lock()
|
||||||
|
.map_err(|e| anyhow::anyhow!("index lock failed: {e}"))
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Language-specific regexes (lazily compiled)
|
// Language-specific regexes (lazily compiled)
|
||||||
@@ -270,6 +329,17 @@ impl SymbolIndex {
|
|||||||
self.symbols.is_empty()
|
self.symbols.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns true when the cached index must be rebuilt for the given
|
||||||
|
/// workspace — either because nothing has been indexed yet, or because the
|
||||||
|
/// requested workspace differs from the one the index was built for.
|
||||||
|
///
|
||||||
|
/// Without this, searching a *different* workspace after the first one
|
||||||
|
/// silently returns stale symbols from the previously indexed repo
|
||||||
|
/// (a misleading result for a coding agent).
|
||||||
|
pub fn needs_rebuild(&self, workspace: &str) -> bool {
|
||||||
|
self.is_empty() || self.workspace_path.as_deref() != Some(workspace)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
self.symbols.len()
|
self.symbols.len()
|
||||||
}
|
}
|
||||||
@@ -1250,15 +1320,11 @@ impl Tool for SemanticSearch {
|
|||||||
"semantic search"
|
"semantic search"
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut guard = SYMBOL_INDEX
|
// Build (or load) the per-workspace index without holding the global
|
||||||
.lock()
|
// lock across the I/O-heavy walk.
|
||||||
.map_err(|e| anyhow::anyhow!("index lock failed: {e}"))?;
|
let _count = ensure_symbol_index(&workspace, rebuild)?;
|
||||||
let index = guard.get_or_insert_with(SymbolIndex::new);
|
let map = symbol_index()?;
|
||||||
|
let index = map.get(&workspace).expect("index should be ensured");
|
||||||
if rebuild || index.is_empty() {
|
|
||||||
let count = index.rebuild(&workspace)?;
|
|
||||||
debug!(symbol_count = count, "symbol index rebuilt");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Map kind filter to enum
|
// Map kind filter to enum
|
||||||
let target_kind = match kind_filter {
|
let target_kind = match kind_filter {
|
||||||
@@ -1398,12 +1464,15 @@ impl Tool for RebuildIndex {
|
|||||||
|
|
||||||
info!("rebuilding multi-language symbol index");
|
info!("rebuilding multi-language symbol index");
|
||||||
|
|
||||||
let mut guard = SYMBOL_INDEX
|
// Force a rebuild of this workspace's index. The walk runs off the
|
||||||
.lock()
|
// global lock (via ensure_symbol_index) so it cannot stall concurrent
|
||||||
.map_err(|e| anyhow::anyhow!("index lock failed: {e}"))?;
|
// searches.
|
||||||
let index = guard.get_or_insert_with(SymbolIndex::new);
|
let count = ensure_symbol_index(&workspace, true)?;
|
||||||
let count = index.rebuild(&workspace)?;
|
let map = symbol_index()?;
|
||||||
let by_lang = index.count_by_language();
|
let by_lang = match map.get(&workspace) {
|
||||||
|
Some(i) => i.count_by_language(),
|
||||||
|
None => Vec::new(),
|
||||||
|
};
|
||||||
|
|
||||||
let mut out = format!(
|
let mut out = format!(
|
||||||
"Symbol index rebuilt successfully. {} symbols indexed.\n\n",
|
"Symbol index rebuilt successfully. {} symbols indexed.\n\n",
|
||||||
@@ -1497,15 +1566,11 @@ impl Tool for ListSymbols {
|
|||||||
.map(|p| p.to_string_lossy().to_string())
|
.map(|p| p.to_string_lossy().to_string())
|
||||||
.unwrap_or_else(|| ".".to_string());
|
.unwrap_or_else(|| ".".to_string());
|
||||||
|
|
||||||
let mut guard = SYMBOL_INDEX
|
// Build (or load) the per-workspace index without holding the global
|
||||||
.lock()
|
// lock across the I/O-heavy walk.
|
||||||
.map_err(|e| anyhow::anyhow!("index lock failed: {e}"))?;
|
let _count = ensure_symbol_index(&workspace, rebuild)?;
|
||||||
let index = guard.get_or_insert_with(SymbolIndex::new);
|
let map = symbol_index()?;
|
||||||
|
let index = map.get(&workspace).expect("index should be ensured");
|
||||||
if rebuild || index.is_empty() {
|
|
||||||
let count = index.rebuild(&workspace)?;
|
|
||||||
info!(symbol_count = count, "symbol index rebuilt for list");
|
|
||||||
}
|
|
||||||
|
|
||||||
let target_lang = match lang_filter {
|
let target_lang = match lang_filter {
|
||||||
"rust" => Some(Language::Rust),
|
"rust" => Some(Language::Rust),
|
||||||
@@ -1746,4 +1811,80 @@ mod tests {
|
|||||||
let index = SymbolIndex::new();
|
let index = SymbolIndex::new();
|
||||||
assert!(index.search("anything", 10).is_empty());
|
assert!(index.search("anything", 10).is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_needs_rebuild_workspace_aware() {
|
||||||
|
let ws_a = std::env::temp_dir().join(format!("ws_a_{}", uuid::Uuid::new_v4()));
|
||||||
|
let ws_b = std::env::temp_dir().join(format!("ws_b_{}", uuid::Uuid::new_v4()));
|
||||||
|
std::fs::create_dir_all(&ws_a).unwrap();
|
||||||
|
std::fs::create_dir_all(&ws_b).unwrap();
|
||||||
|
std::fs::write(ws_a.join("a.rs"), "pub fn fn_in_a() {}\n").unwrap();
|
||||||
|
std::fs::write(ws_b.join("b.rs"), "pub fn fn_in_b() {}\n").unwrap();
|
||||||
|
|
||||||
|
let mut index = SymbolIndex::new();
|
||||||
|
let a = ws_a.to_string_lossy().to_string();
|
||||||
|
let b = ws_b.to_string_lossy().to_string();
|
||||||
|
|
||||||
|
// Fresh index: needs rebuild for any workspace.
|
||||||
|
assert!(index.needs_rebuild(&a));
|
||||||
|
|
||||||
|
// After rebuilding A, searching A needs no rebuild...
|
||||||
|
index.rebuild(&a).unwrap();
|
||||||
|
assert!(!index.needs_rebuild(&a));
|
||||||
|
// ...but searching B DOES (stale index otherwise).
|
||||||
|
assert!(
|
||||||
|
index.needs_rebuild(&b),
|
||||||
|
"workspace switch must trigger rebuild"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Rebuilding B flips the cached workspace.
|
||||||
|
index.rebuild(&b).unwrap();
|
||||||
|
assert!(!index.needs_rebuild(&b));
|
||||||
|
assert!(index.needs_rebuild(&a));
|
||||||
|
|
||||||
|
std::fs::remove_dir_all(&ws_a).ok();
|
||||||
|
std::fs::remove_dir_all(&ws_b).ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ensure_symbol_index_per_workspace_isolation() {
|
||||||
|
let ws_a = std::env::temp_dir().join(format!("iso_a_{}", uuid::Uuid::new_v4()));
|
||||||
|
let ws_b = std::env::temp_dir().join(format!("iso_b_{}", uuid::Uuid::new_v4()));
|
||||||
|
std::fs::create_dir_all(&ws_a).unwrap();
|
||||||
|
std::fs::create_dir_all(&ws_b).unwrap();
|
||||||
|
std::fs::write(ws_a.join("a.rs"), "pub fn only_in_a() {}\n").unwrap();
|
||||||
|
std::fs::write(ws_b.join("b.rs"), "pub fn only_in_b() {}\n").unwrap();
|
||||||
|
|
||||||
|
let a = ws_a.to_string_lossy().to_string();
|
||||||
|
let b = ws_b.to_string_lossy().to_string();
|
||||||
|
|
||||||
|
// Build A and B independently through the shared global helper.
|
||||||
|
let count_a = ensure_symbol_index(&a, false).unwrap();
|
||||||
|
assert!(
|
||||||
|
count_a >= 1,
|
||||||
|
"workspace A should index its fn, got {count_a}"
|
||||||
|
);
|
||||||
|
let count_b = ensure_symbol_index(&b, false).unwrap();
|
||||||
|
assert!(
|
||||||
|
count_b >= 1,
|
||||||
|
"workspace B should index its fn, got {count_b}"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Rebuilding A must not have clobbered B and vice-versa.
|
||||||
|
let count_a_again = ensure_symbol_index(&a, true).unwrap();
|
||||||
|
assert!(count_a_again >= 1);
|
||||||
|
|
||||||
|
// Each workspace's cached index is independently correct.
|
||||||
|
{
|
||||||
|
let map = symbol_index().unwrap();
|
||||||
|
let idx_a = map.get(&a).unwrap();
|
||||||
|
assert!(!idx_a.search("only_in_a", 5).is_empty());
|
||||||
|
assert!(idx_a.search("only_in_b", 5).is_empty());
|
||||||
|
let idx_b = map.get(&b).unwrap();
|
||||||
|
assert!(!idx_b.search("only_in_b", 5).is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::fs::remove_dir_all(&ws_a).ok();
|
||||||
|
std::fs::remove_dir_all(&ws_b).ok();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ use anyhow::Result;
|
|||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
use tracing::{debug, info, instrument, warn};
|
use tracing::{debug, info, instrument, warn};
|
||||||
|
|
||||||
use crate::llm::provider::LlmClient;
|
|
||||||
use crate::tools::{arg_str, Tool, ToolCtx};
|
use crate::tools::{arg_str, Tool, ToolCtx};
|
||||||
use crate::workflow::engine::execution::execute_workflow;
|
use crate::workflow::engine::execution::execute_workflow;
|
||||||
use crate::workflow::hive_mind::cycle::execute_cycle;
|
use crate::workflow::hive_mind::cycle::execute_cycle;
|
||||||
@@ -60,14 +59,8 @@ impl Tool for WorkflowRun {
|
|||||||
phase_names.join(", ")
|
phase_names.join(", ")
|
||||||
);
|
);
|
||||||
|
|
||||||
let llm_client = LlmClient::new(
|
|
||||||
crate::llm::provider::DEFAULT_API_KEY.to_string(),
|
|
||||||
zesdex_domain::agent::defaults::DEFAULT_MODEL.to_string(),
|
|
||||||
None,
|
|
||||||
);
|
|
||||||
let rt = crate::runtime::runtime();
|
let rt = crate::runtime::runtime();
|
||||||
let result: Vec<String> =
|
let result: Vec<String> = rt.block_on(async { execute_workflow(&script, ctx).await })?;
|
||||||
rt.block_on(async { execute_workflow(&script, ctx, &llm_client).await })?;
|
|
||||||
|
|
||||||
info!(phase_count = result.len(), "Workflow completed");
|
info!(phase_count = result.len(), "Workflow completed");
|
||||||
Ok(format!(
|
Ok(format!(
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use tracing::{info, instrument};
|
use tracing::{info, instrument};
|
||||||
|
|
||||||
use crate::llm::provider::LlmClient;
|
|
||||||
use crate::tools::ToolCtx;
|
use crate::tools::ToolCtx;
|
||||||
use crate::workflow::engine::primitives::execute_primitive;
|
use crate::workflow::engine::primitives::execute_primitive;
|
||||||
use zesdex_domain::workflow::WorkflowScript;
|
use zesdex_domain::workflow::WorkflowScript;
|
||||||
@@ -11,12 +10,8 @@ use zesdex_domain::workflow::WorkflowScript;
|
|||||||
/// Execute each phase of a workflow script sequentially.
|
/// Execute each phase of a workflow script sequentially.
|
||||||
///
|
///
|
||||||
/// Flow: for each phase → execute_primitive → collect result.
|
/// Flow: for each phase → execute_primitive → collect result.
|
||||||
#[instrument(skip(tool_ctx, _llm_client))]
|
#[instrument(skip(tool_ctx))]
|
||||||
pub async fn execute_workflow(
|
pub async fn execute_workflow(script: &WorkflowScript, tool_ctx: &ToolCtx) -> Result<Vec<String>> {
|
||||||
script: &WorkflowScript,
|
|
||||||
tool_ctx: &ToolCtx,
|
|
||||||
_llm_client: &LlmClient,
|
|
||||||
) -> Result<Vec<String>> {
|
|
||||||
info!(
|
info!(
|
||||||
"Executing workflow: {} ({} phases)",
|
"Executing workflow: {} ({} phases)",
|
||||||
script.name,
|
script.name,
|
||||||
|
|||||||
@@ -1,28 +1,128 @@
|
|||||||
//! Consensus synthesis — reconciles multiple node outputs into one assessment.
|
//! Consensus synthesis — reconciles multiple node outputs into one assessment.
|
||||||
|
//!
|
||||||
|
//! Flow: load settings → resolve LLM credentials → ask the model to distill the
|
||||||
|
//! node outputs into a single consensus (conflicts, agreements, key findings)
|
||||||
|
//! → return the synthesized text. If the LLM call fails for any reason, we
|
||||||
|
//! degrade gracefully to a concatenation-based summary so consensus synthesis
|
||||||
|
//! never breaks the surrounding hive-mind cycle (mirrors the isolated-errors
|
||||||
|
//! philosophy used for the nodes themselves).
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use tracing::info;
|
use tracing::{info, warn};
|
||||||
|
|
||||||
use crate::tools::ToolCtx;
|
use zesdex_domain::cms::{AppConfigRepository, SettingsRepository};
|
||||||
|
use zesdex_domain::core::message::ChatMessage;
|
||||||
|
use zesdex_domain::core::Store;
|
||||||
use zesdex_domain::workflow::NodeOutput;
|
use zesdex_domain::workflow::NodeOutput;
|
||||||
|
|
||||||
/// Synthesize a consensus from all node outputs.
|
use crate::persistence::{JsonAppConfigRepository, JsonSettingsRepository};
|
||||||
|
use crate::tools::ToolCtx;
|
||||||
|
|
||||||
|
/// Maximum characters of node output to feed into the synthesis prompt per node.
|
||||||
|
/// Keeps the prompt bounded so a huge/talkative node cannot blow up the request.
|
||||||
|
const MAX_NODE_OUTPUT_CHARS: usize = 4000;
|
||||||
|
|
||||||
|
/// Synthesize a consensus from all node outputs using the LLM.
|
||||||
///
|
///
|
||||||
/// Flow: combine node outputs → return consensus text.
|
/// Flow: combine node outputs → ask the model to reconcile them into a single
|
||||||
/// Uses simple concatenation-based synthesis (avoids LLM call dependency).
|
/// consensus → return the synthesized text. Falls back to a plain
|
||||||
|
/// concatenation summary if the LLM is unreachable or the call fails.
|
||||||
pub async fn synthesize_consensus(nodes: &[NodeOutput], _tool_ctx: &ToolCtx) -> Result<String> {
|
pub async fn synthesize_consensus(nodes: &[NodeOutput], _tool_ctx: &ToolCtx) -> Result<String> {
|
||||||
info!("Synthesizing consensus from {} nodes", nodes.len());
|
info!("Synthesizing consensus from {} nodes", nodes.len());
|
||||||
|
|
||||||
|
let combined = build_combined_body(nodes);
|
||||||
|
|
||||||
|
// 1. Resolve LLM credentials (same source of truth as execute_cycle).
|
||||||
|
let store = Store::new();
|
||||||
|
let settings = JsonSettingsRepository::new()
|
||||||
|
.load(&store.base_dir)
|
||||||
|
.unwrap_or_default();
|
||||||
|
let app_config = JsonAppConfigRepository::new()
|
||||||
|
.load(&store.base_dir)
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
let (provider, model) =
|
||||||
|
crate::subagent::provider::resolve_subagent_provider(&settings, &app_config);
|
||||||
|
let base_url = app_config
|
||||||
|
.providers
|
||||||
|
.get(&provider)
|
||||||
|
.map(|p| p.api_base.clone())
|
||||||
|
.unwrap_or_else(|| zesdex_domain::agent::defaults::DEFAULT_API_BASE.to_string());
|
||||||
|
let api_key = crate::llm::provider::resolve_api_key(&settings, &app_config);
|
||||||
|
|
||||||
|
let client = crate::llm::provider::LlmClient::new(api_key, model, Some(base_url));
|
||||||
|
|
||||||
|
// 2. Build the synthesis prompt.
|
||||||
|
let system_msg = ChatMessage::system(
|
||||||
|
"You are a consensus synthesizer for a multi-agent hive mind. \
|
||||||
|
Several independent nodes analysed a problem and produced the outputs \
|
||||||
|
below. Distill them into ONE coherent consensus report with these \
|
||||||
|
sections:\n\
|
||||||
|
- AGREEMENTS: points multiple nodes converge on.\n\
|
||||||
|
- CONFLICTS: contradictory conclusions, with which node(s) support each side.\n\
|
||||||
|
- KEY FINDINGS: the most important, actionable takeaways.\n\
|
||||||
|
- RECOMMENDATION: a single recommended next action, or 'no clear consensus' \
|
||||||
|
if the outputs are too divergent.\n\
|
||||||
|
Be concise and factual. If a node errored, note it and ignore its content.\n\
|
||||||
|
Do not invent facts not present in the node outputs.",
|
||||||
|
);
|
||||||
|
let user_msg = ChatMessage::user(format!(
|
||||||
|
"Consolidate these {} node outputs into a single consensus:\n\n{}",
|
||||||
|
nodes.len(),
|
||||||
|
combined
|
||||||
|
));
|
||||||
|
|
||||||
|
// 3. Call the model and gracefully degrade on failure.
|
||||||
|
match call_consensus(&client, &[system_msg, user_msg]).await {
|
||||||
|
Ok(text) => {
|
||||||
|
let trimmed = text.trim();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
warn!("consensus LLM returned empty output; falling back to concat summary");
|
||||||
|
Ok(concat_summary(nodes))
|
||||||
|
} else {
|
||||||
|
Ok(format!(
|
||||||
|
"# Consensus Synthesis\n\n\
|
||||||
|
Nodes synthesized: {}\n\n{}",
|
||||||
|
nodes.len(),
|
||||||
|
trimmed
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
warn!(error = %e, "consensus LLM call failed; falling back to concat summary");
|
||||||
|
Ok(concat_summary(nodes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Run the LLM consensus call and return the assistant text.
|
||||||
|
async fn call_consensus(
|
||||||
|
client: &crate::llm::provider::LlmClient,
|
||||||
|
messages: &[ChatMessage],
|
||||||
|
) -> Result<String> {
|
||||||
|
use zesdex_application::ports::ProviderService;
|
||||||
|
let (msg, _) = client.chat(messages, None, Some(1024), Some(0.3)).await?;
|
||||||
|
Ok(msg.content.unwrap_or_default())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Build the concatenated node-output body for the prompt.
|
||||||
|
fn build_combined_body(nodes: &[NodeOutput]) -> String {
|
||||||
let mut combined = String::new();
|
let mut combined = String::new();
|
||||||
for node in nodes {
|
for node in nodes {
|
||||||
|
let output = crate::utils::truncate_chars(&node.output, MAX_NODE_OUTPUT_CHARS);
|
||||||
combined.push_str(&format!(
|
combined.push_str(&format!(
|
||||||
"\n## {} — {}\n\n{}\n",
|
"\n## {} — {}\n{}\n",
|
||||||
node.id, node.directive, node.output
|
node.id, node.directive, output
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
combined
|
||||||
|
}
|
||||||
|
|
||||||
Ok(format!(
|
/// Fallback: a plain concatenation summary (the behaviour of the original stub).
|
||||||
"# Consensus Synthesis\n\
|
fn concat_summary(nodes: &[NodeOutput]) -> String {
|
||||||
|
let combined = build_combined_body(nodes);
|
||||||
|
format!(
|
||||||
|
"# Consensus Synthesis\n\n\
|
||||||
Nodes synthesized: {}\n\n\
|
Nodes synthesized: {}\n\n\
|
||||||
## Summary\n\
|
## Summary\n\
|
||||||
The following node outputs were collected:\n\
|
The following node outputs were collected:\n\
|
||||||
@@ -31,5 +131,47 @@ pub async fn synthesize_consensus(nodes: &[NodeOutput], _tool_ctx: &ToolCtx) ->
|
|||||||
Review the individual node outputs above for detailed findings.",
|
Review the individual node outputs above for detailed findings.",
|
||||||
nodes.len(),
|
nodes.len(),
|
||||||
combined
|
combined
|
||||||
))
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use zesdex_domain::workflow::NodeOutput;
|
||||||
|
|
||||||
|
fn node(id: &str, output: &str) -> NodeOutput {
|
||||||
|
NodeOutput {
|
||||||
|
id: id.to_string(),
|
||||||
|
directive: "directive".to_string(),
|
||||||
|
output: output.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn build_combined_body_truncates_oversized_output() {
|
||||||
|
let long = "é".repeat(MAX_NODE_OUTPUT_CHARS + 500);
|
||||||
|
let body = build_combined_body(&[node("n1", &long)]);
|
||||||
|
// Must contain the header and a char-truncated (<= cap) payload without
|
||||||
|
// panicking on a multi-byte boundary.
|
||||||
|
assert!(body.contains("## n1"));
|
||||||
|
// Header "## n1 — directive\n" ~= 20 chars, so char count stays just above cap.
|
||||||
|
let char_count = body.chars().count();
|
||||||
|
assert!(
|
||||||
|
char_count <= MAX_NODE_OUTPUT_CHARS + 50,
|
||||||
|
"expected body near {MAX_NODE_OUTPUT_CHARS} chars, got {char_count}"
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
char_count > 1000,
|
||||||
|
"expected a many-node output, got small: {char_count}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn concat_summary_includes_all_node_ids() {
|
||||||
|
let nodes = vec![node("node-a", "a out"), node("node-b", "b out")];
|
||||||
|
let summary = concat_summary(&nodes);
|
||||||
|
assert!(summary.contains("node-a"));
|
||||||
|
assert!(summary.contains("node-b"));
|
||||||
|
assert!(summary.contains("Nodes synthesized: 2"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user