forked from kent/consciousness
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
|
|
@ -3,46 +3,43 @@
|
|||
// render, write, node-delete, node-rename, history, list-keys,
|
||||
// list-edges, dump-json, lookup-bump, lookups.
|
||||
|
||||
use anyhow::{bail, Context, Result};
|
||||
use crate::hippocampus as memory;
|
||||
use crate::store;
|
||||
|
||||
pub async fn cmd_weight_set(key: &str, weight: f32) -> Result<(), String> {
|
||||
pub async fn cmd_weight_set(key: &str, weight: f32) -> Result<()> {
|
||||
super::check_dry_run();
|
||||
let result = memory::memory_weight_set(None, key, weight).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let result = memory::memory_weight_set(None, key, weight).await?;
|
||||
println!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_node_delete(key: &[String]) -> Result<(), String> {
|
||||
pub async fn cmd_node_delete(key: &[String]) -> Result<()> {
|
||||
if key.is_empty() {
|
||||
return Err("node-delete requires a key".into());
|
||||
bail!("node-delete requires a key");
|
||||
}
|
||||
super::check_dry_run();
|
||||
let key = key.join(" ");
|
||||
let result = memory::memory_delete(None, &key).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let result = memory::memory_delete(None, &key).await?;
|
||||
println!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_node_rename(old_key: &str, new_key: &str) -> Result<(), String> {
|
||||
pub async fn cmd_node_rename(old_key: &str, new_key: &str) -> Result<()> {
|
||||
super::check_dry_run();
|
||||
let result = memory::memory_rename(None, old_key, new_key).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let result = memory::memory_rename(None, old_key, new_key).await?;
|
||||
println!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_render(key: &[String]) -> Result<(), String> {
|
||||
pub async fn cmd_render(key: &[String]) -> Result<()> {
|
||||
if key.is_empty() {
|
||||
return Err("render requires a key".into());
|
||||
bail!("render requires a key");
|
||||
}
|
||||
let key = key.join(" ");
|
||||
let bare = store::strip_md_suffix(&key);
|
||||
|
||||
let rendered = memory::memory_render(None, &bare, None).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let rendered = memory::memory_render(None, &bare, None).await?;
|
||||
print!("{}", rendered);
|
||||
|
||||
// Mark as seen if we're inside a Claude session (not an agent subprocess —
|
||||
|
|
@ -66,40 +63,38 @@ pub async fn cmd_render(key: &[String]) -> Result<(), String> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_history(key: &[String], full: bool) -> Result<(), String> {
|
||||
pub async fn cmd_history(key: &[String], full: bool) -> Result<()> {
|
||||
if key.is_empty() {
|
||||
return Err("history requires a key".into());
|
||||
bail!("history requires a key");
|
||||
}
|
||||
let key = key.join(" ");
|
||||
let result = memory::memory_history(None, &key, Some(full)).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let result = memory::memory_history(None, &key, Some(full)).await?;
|
||||
print!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_write(key: &[String]) -> Result<(), String> {
|
||||
pub async fn cmd_write(key: &[String]) -> Result<()> {
|
||||
if key.is_empty() {
|
||||
return Err("write requires a key (reads content from stdin)".into());
|
||||
bail!("write requires a key (reads content from stdin)");
|
||||
}
|
||||
let key = key.join(" ");
|
||||
let mut content = String::new();
|
||||
std::io::Read::read_to_string(&mut std::io::stdin(), &mut content)
|
||||
.map_err(|e| format!("read stdin: {}", e))?;
|
||||
.context("read stdin")?;
|
||||
|
||||
if content.trim().is_empty() {
|
||||
return Err("No content on stdin".into());
|
||||
bail!("No content on stdin");
|
||||
}
|
||||
super::check_dry_run();
|
||||
|
||||
let result = memory::memory_write(None, &key, &content).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let result = memory::memory_write(None, &key, &content).await?;
|
||||
println!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_edit(key: &[String]) -> Result<(), String> {
|
||||
pub async fn cmd_edit(key: &[String]) -> Result<()> {
|
||||
if key.is_empty() {
|
||||
return Err("edit requires a key".into());
|
||||
bail!("edit requires a key");
|
||||
}
|
||||
let key = key.join(" ");
|
||||
|
||||
|
|
@ -109,21 +104,21 @@ pub async fn cmd_edit(key: &[String]) -> Result<(), String> {
|
|||
|
||||
let tmp = std::env::temp_dir().join(format!("poc-memory-edit-{}.md", key.replace('/', "_")));
|
||||
std::fs::write(&tmp, &content)
|
||||
.map_err(|e| format!("write temp file: {}", e))?;
|
||||
.with_context(|| format!("write temp file {}", tmp.display()))?;
|
||||
|
||||
let editor = std::env::var("EDITOR").unwrap_or_else(|_| "vi".into());
|
||||
let status = std::process::Command::new(&editor)
|
||||
.arg(&tmp)
|
||||
.status()
|
||||
.map_err(|e| format!("spawn {}: {}", editor, e))?;
|
||||
.with_context(|| format!("spawn {}", editor))?;
|
||||
|
||||
if !status.success() {
|
||||
let _ = std::fs::remove_file(&tmp);
|
||||
return Err(format!("{} exited with {}", editor, status));
|
||||
bail!("{} exited with {}", editor, status);
|
||||
}
|
||||
|
||||
let new_content = std::fs::read_to_string(&tmp)
|
||||
.map_err(|e| format!("read temp file: {}", e))?;
|
||||
.with_context(|| format!("read temp file {}", tmp.display()))?;
|
||||
let _ = std::fs::remove_file(&tmp);
|
||||
|
||||
if new_content == content {
|
||||
|
|
@ -132,34 +127,31 @@ pub async fn cmd_edit(key: &[String]) -> Result<(), String> {
|
|||
}
|
||||
|
||||
if new_content.trim().is_empty() {
|
||||
return Err("Content is empty, aborting".into());
|
||||
bail!("Content is empty, aborting");
|
||||
}
|
||||
|
||||
super::check_dry_run();
|
||||
let result = memory::memory_write(None, &key, &new_content).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let result = memory::memory_write(None, &key, &new_content).await?;
|
||||
println!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_search(keys: &[String]) -> Result<(), String> {
|
||||
pub async fn cmd_search(keys: &[String]) -> Result<()> {
|
||||
if keys.is_empty() {
|
||||
return Err("search requires seed keys".into());
|
||||
bail!("search requires seed keys");
|
||||
}
|
||||
let result = memory::memory_search(None, keys.to_vec(), None, None, None, None).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let result = memory::memory_search(None, keys.to_vec(), None, None, None, None).await?;
|
||||
print!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_query(expr: &[String]) -> Result<(), String> {
|
||||
pub async fn cmd_query(expr: &[String]) -> Result<()> {
|
||||
if expr.is_empty() {
|
||||
return Err("query requires an expression (try: poc-memory query --help)".into());
|
||||
bail!("query requires an expression (try: poc-memory query --help)");
|
||||
}
|
||||
|
||||
let query_str = expr.join(" ");
|
||||
let result = memory::memory_query(None, &query_str, None).await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let result = memory::memory_query(None, &query_str, None).await?;
|
||||
print!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -210,7 +202,7 @@ pub async fn get_group_content(group: &crate::config::ContextGroup, cfg: &crate:
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn cmd_load_context(stats: bool) -> Result<(), String> {
|
||||
pub async fn cmd_load_context(stats: bool) -> Result<()> {
|
||||
let cfg = crate::config::get();
|
||||
|
||||
if stats {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue