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:
Kent Overstreet 2026-04-13 18:05:04 -04:00
parent 5db00e083f
commit b8db8754be
17 changed files with 282 additions and 295 deletions

View file

@ -1,25 +1,26 @@
// cli/journal.rs — journal subcommand handlers
use anyhow::{bail, Context, Result};
use crate::hippocampus as memory;
pub fn cmd_tail(n: usize, full: bool, provenance: Option<&str>, dedup: bool) -> Result<(), String> {
pub fn cmd_tail(n: usize, full: bool, provenance: Option<&str>, dedup: bool) -> Result<()> {
let path = crate::store::nodes_path();
if !path.exists() {
return Err("No node log found".into());
bail!("No node log found");
}
use std::io::BufReader;
let file = std::fs::File::open(&path)
.map_err(|e| format!("open {}: {}", path.display(), e))?;
.with_context(|| format!("open {}", path.display()))?;
let mut reader = BufReader::new(file);
// Read all entries, keep last N
let mut entries: Vec<crate::store::Node> = Vec::new();
while let Ok(msg) = capnp::serialize::read_message(&mut reader, capnp::message::ReaderOptions::new()) {
let log = msg.get_root::<crate::memory_capnp::node_log::Reader>()
.map_err(|e| format!("read log: {}", e))?;
.with_context(|| "read log")?;
for node_reader in log.get_nodes()
.map_err(|e| format!("get nodes: {}", e))? {
.with_context(|| "get nodes")? {
let node = crate::store::Node::from_capnp_migrate(node_reader)?;
entries.push(node);
}
@ -67,9 +68,8 @@ pub fn cmd_tail(n: usize, full: bool, provenance: Option<&str>, dedup: bool) ->
Ok(())
}
pub async fn cmd_journal_tail(n: usize, full: bool, level: u8) -> Result<(), String> {
let entries = memory::journal_tail(None, Some(n as u64), Some(level as u64), None).await
.map_err(|e| e.to_string())?;
pub async fn cmd_journal_tail(n: usize, full: bool, level: u8) -> Result<()> {
let entries = memory::journal_tail(None, Some(n as u64), Some(level as u64), None).await?;
for entry in entries {
if full {
println!("--- {} ---", entry.key);
@ -82,15 +82,14 @@ pub async fn cmd_journal_tail(n: usize, full: bool, level: u8) -> Result<(), Str
Ok(())
}
pub async fn cmd_journal_write(name: &str, text: &[String]) -> Result<(), String> {
pub async fn cmd_journal_write(name: &str, text: &[String]) -> Result<()> {
if text.is_empty() {
return Err("journal write requires text".into());
bail!("journal write requires text");
}
super::check_dry_run();
let body = text.join(" ");
let result = memory::journal_new(None, name, name, &body, Some(0)).await
.map_err(|e| e.to_string())?;
let result = memory::journal_new(None, name, name, &body, Some(0)).await?;
println!("{}", result);
Ok(())
}