Skip to content

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

feat(memory): add memory schema definitions and handlers

Steven Enamakel7d8976451b07
diff --git a/crates/openhuman-core/src/memory/ops.rs b/crates/openhuman-core/src/memory/ops.rs
index c9b1bbeb1f..6b02991ad6 100644
--- a/crates/openhuman-core/src/memory/ops.rs
+++ b/crates/openhuman-core/src/memory/ops.rs
@@ -1,5 +1,5 @@
//! Engine-facing operations: list and select engines, recall, fetch, learn,
-//! forget and list items.
+//! forget, erase everything and list items.
//!
//! Every operation takes the [`Config`] it runs against, so the RPC handlers
//! (which load config per call) and the agent tool (which carries its
@@ -8,7 +8,7 @@
use chrono::Utc;
use tinymemory_api::{
- FetchRequest, ForgetTarget, ItemId, LearningKind, ListRequest, MemoryMeta, RecallRequest,
+ EraseRequest, FetchRequest, ForgetTarget, ItemId, LearningKind, ListRequest, MemoryMeta, RecallRequest,
StoreItem, StoreReceipt, TimeHint, WriteOptions,
};
@@ -17,7 +17,7 @@ use crate::config::Config;
use super::engine::{self, Binding, BoundEngine, CORTEXDB_ENGINE, TINYHUMANS_ENGINE};
use super::error::{MemoryError, MemoryResult};
use super::types::{
- clamp_limit, EngineSetParams, EngineStatus, EngineView, EnginesListView, FetchParams,
+ clamp_limit, EngineSetParams, EraseAllParams, EraseAllView, EngineStatus, EngineView, EnginesListView, FetchParams,
FetchView, ForgetParams, ForgetView, ItemsListParams, ItemsListView, LearnParams, LearnView,
RecallParams, RecallView, RefersTo,
};
@@ -373,6 +373,42 @@ pub async fn forget(config: &Config, params: ForgetParams) -> MemoryResult<Forge
})
}
+/// `memory_erase_all`: erases everything the bound engine holds, the whole
+/// tree, every kind. Needs `confirm: true`.
+///
+/// On the hosted engine this is one `DELETE /memory`, which erases the
+/// caller's entire hosted memory (every scope under their tenant, the
+/// pre-v3 layout's included). On CortexDB reached directly it erases every
+/// kind scope of the bound layout (the person's `user:<id>` subtree under
+/// layout v3); a legacy tree is left alone, because on a self-hosted CortexDB
+/// other accounts may share it (see `layout_migration`). An engine that
+/// cannot erase answers `UNSUPPORTED`.
+pub async fn erase_all(config: &Config, params: EraseAllParams) -> MemoryResult<EraseAllView> {
+ if !params.confirm {
+ return Err(MemoryError::invalid(
+ "erasing all memory needs confirm: true; nothing erased comes back",
+ ));
+ }
+ let bound = bound(config)?;
+ let mut request = EraseRequest::new(tinymemory_api::Reach::subtree(
+ tinymemory_api::Namespace::ROOT,
+ ));
+ request.whole_tree = true;
+ tracing::info!(engine = %bound.id, "[memory:ops] erase_all: erasing the whole memory");
+ let report = bound.engine.erase(request).await.map_err(|error| {
+ tracing::warn!(engine = %bound.id, error = %error, "[memory:ops] erase_all failed");
+ MemoryError::from(error)
+ })?;
+ tracing::info!(
+ engine = %bound.id,
+ erased_scopes = report.erased_scopes,
+ "[memory:ops] erase_all: done"
+ );
+ Ok(EraseAllView {
+ erased_scopes: report.erased_scopes,
+ })
+}
+
/// The ids among `ids` naming an item in `reach`, read through `get` in
/// chunks of its id limit.
async fn within_reach(
diff --git a/crates/openhuman-core/src/memory/schemas.rs b/crates/openhuman-core/src/memory/schemas.rs
index eed4022a2f..e55ddac282 100644
--- a/crates/openhuman-core/src/memory/schemas.rs
+++ b/crates/openhuman-core/src/memory/schemas.rs
@@ -38,6 +38,7 @@ fn handler_for(function: &str) -> crate::core::all::ControllerHandler {
"fetch" => handlers::fetch,
"learn" => handlers::learn,
"forget" => handlers::forget,
+ "erase_all" => handlers::erase_all,
"items_list" => handlers::items_list,
"explore" => handlers::explore,
"items_get" => handlers::items_get,
diff --git a/crates/openhuman-core/src/memory/schemas/defs.rs b/crates/openhuman-core/src/memory/schemas/defs.rs
index 4c46c619b5..adc8a2cb9c 100644
--- a/crates/openhuman-core/src/memory/schemas/defs.rs
+++ b/crates/openhuman-core/src/memory/schemas/defs.rs
@@ -4,7 +4,7 @@
use crate::core::{ControllerSchema, FieldSchema, TypeSchema};
/// Every function of the namespace, in spec order.
-pub const FUNCTIONS: [&str; 34] = [
+pub const FUNCTIONS: [&str; 35] = [
"engines_list",
"engine_get",
"engine_set",
@@ -15,6 +15,7 @@ pub const FUNCTIONS: [&str; 34] = [
"fetch",
"learn",
"forget",
+ "erase_all",
"items_list",
"explore",
"items_get",
@@ -174,6 +175,13 @@ pub fn schema(function: &str) -> ControllerSchema {
inputs: vec![req("ids", TypeSchema::Array(Box::new(TypeSchema::String)), "Item ids."), reach()],
outputs: out("{forgotten: number}"),
},
+ "erase_all" => ControllerSchema {
+ namespace: "memory",
+ function: "erase_all",
+ description: "Erase all memory the bound engine holds, for good. On the hosted engine this erases the account's entire hosted memory.",
+ inputs: vec![req("confirm", TypeSchema::Bool, "Must be true: nothing erased comes back.")],
+ outputs: out("{erased_scopes: number}"),
+ },
"items_list" => ControllerSchema {
namespace: "memory",
function: "items_list",
diff --git a/crates/openhuman-core/src/memory/schemas/handlers.rs b/crates/openhuman-core/src/memory/schemas/handlers.rs
index 0436144694..4d9ee106a8 100644
--- a/crates/openhuman-core/src/memory/schemas/handlers.rs
+++ b/crates/openhuman-core/src/memory/schemas/handlers.rs
@@ -99,6 +99,13 @@ pub(super) fn forget(params: Map<String, Value>) -> ControllerFuture {
})
}
+pub(super) fn erase_all(params: Map<String, Value>) -> ControllerFuture {
+ Box::pin(async move {
+ let params = parse::<EraseAllParams>(params)?;
+ finish(ops::erase_all(&load().await?, params).await)
+ })
+}
+
pub(super) fn items_list(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let params = parse::<ItemsListParams>(params)?;
diff --git a/crates/openhuman-core/src/memory/types.rs b/crates/openhuman-core/src/memory/types.rs
index cf0b4fef51..0a6df8add3 100644
--- a/crates/openhuman-core/src/memory/types.rs
+++ b/crates/openhuman-core/src/memory/types.rs
@@ -198,6 +198,22 @@ pub struct ForgetView {
pub forgotten: usize,
}
+/// `memory_erase_all` params.
+#[derive(Debug, Clone, Deserialize)]
+#[serde(deny_unknown_fields)]
+pub struct EraseAllParams {
+ /// Must be `true`: an interlock, because nothing erased comes back.
+ #[serde(default)]
+ pub confirm: bool,
+}
+
+/// `memory_erase_all` result.
+#[derive(Debug, Clone, Serialize)]
+pub struct EraseAllView {
+ /// How many of the engine's scopes (kind stores) were erased.
+ pub erased_scopes: usize,
+}
+
/// `memory_items_list` params.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct ItemsListParams {