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

@ -384,11 +384,11 @@ fn print_help() {
// ── Dispatch ─────────────────────────────────────────────────────────
trait Run {
async fn run(self) -> Result<(), String>;
async fn run(self) -> anyhow::Result<()>;
}
impl Run for Command {
async fn run(self) -> Result<(), String> {
async fn run(self) -> anyhow::Result<()> {
match self {
Self::Search { keys } => cli::node::cmd_search(&keys).await,
Self::Render { key } => cli::node::cmd_render(&key).await,
@ -411,7 +411,7 @@ impl Run for Command {
}
impl Run for NodeCmd {
async fn run(self) -> Result<(), String> {
async fn run(self) -> anyhow::Result<()> {
match self {
Self::Delete { key } => cli::node::cmd_node_delete(&key).await,
Self::Rename { old_key, new_key } => cli::node::cmd_node_rename(&old_key, &new_key).await,
@ -420,7 +420,7 @@ impl Run for NodeCmd {
}
impl Run for JournalCmd {
async fn run(self) -> Result<(), String> {
async fn run(self) -> anyhow::Result<()> {
match self {
Self::Write { name, text } => cli::journal::cmd_journal_write(&name, &text).await,
Self::Tail { n, full, level } => cli::journal::cmd_journal_tail(n, full, level).await,
@ -429,7 +429,7 @@ impl Run for JournalCmd {
}
impl Run for GraphCmd {
async fn run(self) -> Result<(), String> {
async fn run(self) -> anyhow::Result<()> {
match self {
Self::Link { key } => cli::graph::cmd_link(&key).await,
Self::LinkAdd { source, target, reason }
@ -437,7 +437,7 @@ impl Run for GraphCmd {
Self::LinkSet { source, target, strength }
=> cli::graph::cmd_link_set(&source, &target, strength).await,
Self::LinkImpact { source, target } => cli::graph::cmd_link_impact(&source, &target).await,
Self::CapDegree { max_degree } => cli::graph::cmd_cap_degree(max_degree),
Self::CapDegree { max_degree } => cli::graph::cmd_cap_degree(max_degree).await,
Self::NormalizeStrengths { apply } => cli::graph::cmd_normalize_strengths(apply).await,
Self::Trace { key } => cli::graph::cmd_trace(&key).await,
Self::Communities { top_n, min_size } => cli::graph::cmd_communities(top_n, min_size).await,
@ -446,7 +446,7 @@ impl Run for GraphCmd {
}
impl Run for AgentCmd {
async fn run(self) -> Result<(), String> {
async fn run(self) -> anyhow::Result<()> {
match self {
Self::Run { agent, count, target, query, dry_run, local, state_dir }
=> cli::agent::cmd_run_agent(&agent, count, &target, query.as_deref(), dry_run, local, state_dir.as_deref()).await,
@ -455,14 +455,14 @@ impl Run for AgentCmd {
}
impl Run for AdminCmd {
async fn run(self) -> Result<(), String> {
async fn run(self) -> anyhow::Result<()> {
match self {
Self::Init => cli::admin::cmd_init(),
Self::Init => cli::admin::cmd_init().await,
Self::Health => cli::admin::cmd_health().await,
Self::Topology => cli::admin::cmd_topology().await,
Self::Fsck => cli::admin::cmd_fsck(),
Self::Dedup { apply } => cli::admin::cmd_dedup(apply),
Self::DailyCheck => cli::admin::cmd_daily_check(),
Self::Dedup { apply } => cli::admin::cmd_dedup(apply).await,
Self::DailyCheck => cli::admin::cmd_daily_check().await,
Self::Import { files } => cli::admin::cmd_import(&files),
Self::Export { files, all } => cli::admin::cmd_export(&files, all),
Self::LoadContext { stats } => cli::node::cmd_load_context(stats).await,