Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(memory): standardize error handling with MemoryError

Steven Enamakelb069592f96ea
diff --git a/crates/openhuman-core/src/memory/confine.rs b/crates/openhuman-core/src/memory/confine.rs
index 1ffcb18c81..25daa3b589 100644
--- a/crates/openhuman-core/src/memory/confine.rs
+++ b/crates/openhuman-core/src/memory/confine.rs
@@ -120,7 +120,9 @@ pub async fn recall(
params.filter,
&allowed_reach(config, identity),
)?);
- ops::recall(config, params).await
+ ops::recall(config, params)
+ .await
+ .map_err(MemoryError::from)
}
/// `memory_fetch`, confined.
@@ -137,7 +139,9 @@ pub async fn fetch(
params.filter,
&allowed_reach(config, identity),
)?);
- ops::fetch(config, params).await
+ ops::fetch(config, params)
+ .await
+ .map_err(MemoryError::from)
}
/// `memory_learn`, confined: the learning lands inside the caller's layout.
@@ -151,7 +155,9 @@ pub async fn learn(
mut params: LearnParams,
) -> MemoryResult<LearnView> {
params.meta = Some(confine_learn_meta(config, identity, params.meta)?);
- ops::learn(config, params, None).await
+ ops::learn(config, params, None)
+ .await
+ .map_err(MemoryError::from)
}
/// `memory_forget`, confined: an id outside the caller's root is not
@@ -169,7 +175,9 @@ pub async fn forget(
params.reach,
&allowed_reach(config, identity),
)?);
- ops::forget(config, params).await
+ ops::forget(config, params)
+ .await
+ .map_err(MemoryError::from)
}
/// `memory_items_list`, confined. The explorer path narrows first (a
@@ -190,7 +198,9 @@ pub async fn items_list(
&allowed_reach(config, identity),
)?);
params.path = Vec::new();
- ops::items_list(config, params).await
+ ops::items_list(config, params)
+ .await
+ .map_err(MemoryError::from)
}
/// `memory_explore`, confined like [`items_list`].
@@ -210,7 +220,9 @@ pub async fn explore(
&allowed_reach(config, identity),
)?);
params.path = Vec::new();
- explore::explore(config, params).await
+ explore::explore(config, params)
+ .await
+ .map_err(MemoryError::from)
}
/// `memory_items_get`, confined: an id outside the caller's root reads as
@@ -229,9 +241,11 @@ pub async fn items_get(
params.reach,
&allowed_reach(config, identity),
)?);
- explore::items_get(config, params).await
+ explore::items_get(config, params)
+ .await
+ .map_err(MemoryError::from)
}
#[cfg(test)]
-#[path = "confine_tests.rs"]
+#[path = "confinement_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/memory/error.rs b/crates/openhuman-core/src/memory/error.rs
index 6b65801879..446bbe97bf 100644
--- a/crates/openhuman-core/src/memory/error.rs
+++ b/crates/openhuman-core/src/memory/error.rs
@@ -113,15 +113,14 @@ impl MemoryError {
impl From<tinymemory_api::Error> for MemoryError {
fn from(error: tinymemory_api::Error) -> Self {
use tinymemory_api::Error as E;
- // A 402 arrives as `Engine` with the backend's code on its message.
- let credits = tinymemory_integrations::import::is_insufficient_credits(&error);
match error {
+ E::Off(message) => Self::Off(message),
E::Unsupported(message) => Self::Unsupported(message),
E::InvalidRequest(message) | E::NotFound(message) => Self::InvalidRequest(message),
E::Unauthorized(message) => Self::Unauthorized(message),
+ E::InsufficientCredits(message) => Self::InsufficientCredits(message),
E::Config(message) => Self::InvalidRequest(message),
E::Unavailable(message) => Self::Unavailable(message),
- E::Engine(message) if credits => Self::InsufficientCredits(message),
E::Conflict(message) | E::Engine(message) => Self::Engine(message),
}
}
@@ -143,9 +142,15 @@ impl MemoryError {
E::Unauthorized(message.to_string())
} else if let Some(message) = reason.strip_prefix("unavailable: ") {
E::Unavailable(message.to_string())
+ } else if let Some(message) = reason.strip_prefix("insufficient credits: ") {
+ E::InsufficientCredits(message.to_string())
} else {
let message = reason.strip_prefix("engine error: ")?;
- E::Engine(message.to_string())
+ if let Some(message) = message.strip_prefix("[USER_INSUFFICIENT_CREDITS]") {
+ E::InsufficientCredits(message.trim().to_string())
+ } else {
+ E::Engine(message.to_string())
+ }
};
Some(Self::from(error)).filter(Self::is_account_wide)
}
diff --git a/crates/openhuman-core/src/memory/error_tests.rs b/crates/openhuman-core/src/memory/error_tests.rs
index 1868b21486..59ab83b3c3 100644
--- a/crates/openhuman-core/src/memory/error_tests.rs
+++ b/crates/openhuman-core/src/memory/error_tests.rs
@@ -56,8 +56,8 @@ fn memory_error_string_encodes_the_structured_error() {
#[test]
fn typed_credit_errors_are_account_wide_but_other_engine_errors_are_not() {
use tinymemory_api::Error as E;
- let credits = MemoryError::from(E::Engine(
- "[USER_INSUFFICIENT_CREDITS] memory API recall on h: the account has insufficient credits (HTTP 402)".into(),
+ let credits = MemoryError::from(E::InsufficientCredits(
+ "memory API recall on h: the account has insufficient credits (HTTP 402)".into(),
));
assert_eq!(credits.code(), INSUFFICIENT_CREDITS);
assert!(credits.is_account_wide());
@@ -81,6 +81,10 @@ fn recall_skip_reasons_recover_only_account_wide_refusals() {
code("engine error: [USER_INSUFFICIENT_CREDITS] memory API fetch (HTTP 402)"),
Some(INSUFFICIENT_CREDITS)
);
+ assert_eq!(
+ code("insufficient credits: memory API fetch (HTTP 402)"),
+ Some(INSUFFICIENT_CREDITS)
+ );
assert_eq!(code("unavailable: connection refused"), Some(UNAVAILABLE));
assert_eq!(code("unauthorized: key rejected"), Some(UNAUTHORIZED));
assert_eq!(code("engine error: index corrupt"), None);
diff --git a/crates/openhuman-core/src/memory/layout_migration/app_host.rs b/crates/openhuman-core/src/memory/layout_migration/app_host.rs
index 275e6d9f16..974fd21323 100644
--- a/crates/openhuman-core/src/memory/layout_migration/app_host.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/app_host.rs
@@ -103,7 +103,10 @@ impl lm::LayoutHost for AppHost {
async fn switch(&self) -> lm::Result<()> {
// This person's own config file, loaded fresh: never the active
// account's (someone else's after an account switch mid-move).
- scope::switch_to_v3(&self.config).await.map_err(to_module)
+ scope::switch_to_v3(&self.config)
+ .await
+ .map_err(MemoryError::from)
+ .map_err(to_module)
}
async fn free_now(&self) -> bool {
diff --git a/crates/openhuman-core/src/memory/layout_migration/claim.rs b/crates/openhuman-core/src/memory/layout_migration/claim.rs
index 918c792b71..eacb32d59e 100644
--- a/crates/openhuman-core/src/memory/layout_migration/claim.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/claim.rs
@@ -16,6 +16,7 @@ use sha2::Digest;
use tinymemory_tools::layout_migration::{LegacyClaim, Result as ModuleResult};
use super::app_host::to_module;
+use crate::memory::error::MemoryError;
use tinymemory_api::{Error, Result};
/// Where a legacy tree's marker lives and who would own it.
@@ -129,11 +130,13 @@ pub fn take(key: &ClaimKey) -> Result<bool> {
impl LegacyClaim for ClaimKey {
fn held_by_other(&self) -> ModuleResult<bool> {
- held_by_other(self).map_err(to_module)
+ held_by_other(self)
+ .map_err(MemoryError::from)
+ .map_err(to_module)
}
fn take(&self) -> ModuleResult<bool> {
- take(self).map_err(to_module)
+ take(self).map_err(MemoryError::from).map_err(to_module)
}
}
diff --git a/crates/openhuman-core/src/memory/layout_migration/service.rs b/crates/openhuman-core/src/memory/layout_migration/service.rs
index c31160916d..e08b4bc975 100644
--- a/crates/openhuman-core/src/memory/layout_migration/service.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/service.rs
@@ -23,7 +23,7 @@ use tinymemory_tools::layout_migration as lm;
use super::app_host::{from_module, AppHost};
use super::{LayoutHost, MigrationState, MigrationStatus, ScanView, Trigger};
use crate::config::Config;
-use tinymemory_api::Result;
+use crate::memory::error::MemoryResult;
/// Workspaces with a migration running now.
static RUNNING: LazyLock<Mutex<HashSet<PathBuf>>> = LazyLock::new(|| Mutex::new(HashSet::new()));
@@ -42,7 +42,7 @@ pub struct StartParams {
///
/// When the state cannot be read, the engines cannot be bound, or the legacy
/// tree cannot be read.
-pub async fn scan(host: &dyn LayoutHost) -> Result<ScanView> {
+pub async fn scan(host: &dyn LayoutHost) -> MemoryResult<ScanView> {
lm::scan(host).await.map_err(from_module)
}
@@ -51,7 +51,7 @@ pub async fn scan(host: &dyn LayoutHost) -> Result<ScanView> {
/// # Errors
///
/// When the state file cannot be read.
-pub fn status(config: &Config) -> Result<MigrationStatus> {
+pub fn status(config: &Config) -> MemoryResult<MigrationStatus> {
lm::status(&AppHost::new(config.clone()), is_running(config)).map_err(from_module)
}
@@ -126,7 +126,7 @@ pub fn start(
/// # Errors
///
/// When the state cannot be read or saved, or a run is going now.
-pub fn retry(config: &Config) -> Result<MigrationState> {
+pub fn retry(config: &Config) -> MemoryResult<MigrationState> {
lm::retry(&AppHost::new(config.clone()), is_running(config)).map_err(from_module)
}