Convert store and CLI to anyhow::Result for cleaner error handling
Replace Result<_, String> with anyhow::Result throughout: - hippocampus/store module (persist, ops, types, view, mod) - CLI modules (admin, agent, graph, journal, node) - Run trait in main.rs Use .context() and .with_context() instead of .map_err(|e| format!(...)) patterns. Add bail!() for early error returns. Add access_local() helper in hippocampus/mod.rs that returns Result<Arc<Mutex<Store>>> for direct local store access. Fix store access patterns to properly lock Arc<Mutex<Store>> before accessing fields in mind/unconscious.rs, mind/mod.rs, subconscious/learn.rs, and hippocampus/memory.rs. Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
5db00e083f
commit
b8db8754be
17 changed files with 282 additions and 295 deletions
|
|
@ -4,32 +4,31 @@
|
|||
// link, link-add, link-impact, link-audit, cap-degree,
|
||||
// normalize-strengths, trace, spectral-*, organize, communities.
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use crate::hippocampus as memory;
|
||||
use crate::store;
|
||||
|
||||
pub fn cmd_cap_degree(max_deg: usize) -> Result<(), String> {
|
||||
let mut store = store::Store::load()?;
|
||||
pub async fn cmd_cap_degree(max_deg: usize) -> Result<()> {
|
||||
let arc = memory::access_local()?;
|
||||
let mut store = arc.lock().await;
|
||||
let (hubs, pruned) = store.cap_degree(max_deg)?;
|
||||
store.save()?;
|
||||
println!("Capped {} hubs, pruned {} weak Auto edges (max_degree={})", hubs, pruned, max_deg);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_normalize_strengths(apply: bool) -> Result<(), String> {
|
||||
pub async fn cmd_normalize_strengths(apply: bool) -> Result<()> {
|
||||
if apply { super::check_dry_run(); }
|
||||
let result = memory::graph_normalize_strengths(None, Some(apply)).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let result = memory::graph_normalize_strengths(None, Some(apply)).await?;
|
||||
print!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_link(key: &[String]) -> Result<(), String> {
|
||||
pub async fn cmd_link(key: &[String]) -> Result<()> {
|
||||
if key.is_empty() {
|
||||
return Err("link requires a key".into());
|
||||
bail!("link requires a key");
|
||||
}
|
||||
let key = key.join(" ");
|
||||
let links = memory::memory_links(None, &key).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let links = memory::memory_links(None, &key).await?;
|
||||
println!("Neighbors of '{}':", key);
|
||||
for link in links {
|
||||
println!(" ({:.2}) {} [w={:.2}]", link.link_strength, link.key, link.node_weight);
|
||||
|
|
@ -37,36 +36,32 @@ pub async fn cmd_link(key: &[String]) -> Result<(), String> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_link_add(source: &str, target: &str, _reason: &[String]) -> Result<(), String> {
|
||||
pub async fn cmd_link_add(source: &str, target: &str, _reason: &[String]) -> Result<()> {
|
||||
super::check_dry_run();
|
||||
let result = memory::memory_link_add(None, source, target).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let result = memory::memory_link_add(None, source, target).await?;
|
||||
println!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_link_set(source: &str, target: &str, strength: f32) -> Result<(), String> {
|
||||
pub async fn cmd_link_set(source: &str, target: &str, strength: f32) -> Result<()> {
|
||||
super::check_dry_run();
|
||||
let result = memory::memory_link_set(None, source, target, strength).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let result = memory::memory_link_set(None, source, target, strength).await?;
|
||||
println!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_link_impact(source: &str, target: &str) -> Result<(), String> {
|
||||
let result = memory::graph_link_impact(None, source, target).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
pub async fn cmd_link_impact(source: &str, target: &str) -> Result<()> {
|
||||
let result = memory::graph_link_impact(None, source, target).await?;
|
||||
print!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_trace(key: &[String]) -> Result<(), String> {
|
||||
pub async fn cmd_trace(key: &[String]) -> Result<()> {
|
||||
if key.is_empty() {
|
||||
return Err("trace requires a key".into());
|
||||
bail!("trace requires a key");
|
||||
}
|
||||
let key = key.join(" ");
|
||||
let result = memory::graph_trace(None, &key).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let result = memory::graph_trace(None, &key).await?;
|
||||
print!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -74,9 +69,8 @@ pub async fn cmd_trace(key: &[String]) -> Result<(), String> {
|
|||
/// Show communities sorted by isolation (most isolated first).
|
||||
/// Useful for finding poorly-integrated knowledge clusters that need
|
||||
/// organize agents aimed at them.
|
||||
pub async fn cmd_communities(top_n: usize, min_size: usize) -> Result<(), String> {
|
||||
let result = memory::graph_communities(None, Some(top_n), Some(min_size)).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
pub async fn cmd_communities(top_n: usize, min_size: usize) -> Result<()> {
|
||||
let result = memory::graph_communities(None, Some(top_n), Some(min_size)).await?;
|
||||
print!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue