The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(memory): add billing module for memory engine usage
Steven Enamakel15f3cb78a24b
diff --git a/crates/openhuman-core/src/memory/billing.rs b/crates/openhuman-core/src/memory/billing.rsindex 757725f082..40d64c2617 100644--- a/crates/openhuman-core/src/memory/billing.rs+++ b/crates/openhuman-core/src/memory/billing.rs@@ -51,6 +51,12 @@ pub async fn free_period_active(config: &Config) -> bool {Binding::On(bound) => bound,Binding::Off { .. } => return false,};+ if let Some(entry) = super::engine_registry::lookup(&bound.id) {+ return match entry.free_background {+ Some(free) => free(config.clone(), bound).await,+ None => true,+ };+ }if bound.id != TINYHUMANS_ENGINE {return true;}diff --git a/crates/openhuman-core/src/memory/engine.rs b/crates/openhuman-core/src/memory/engine.rsindex 03cd5a2a6b..3b8e342473 100644--- a/crates/openhuman-core/src/memory/engine.rs+++ b/crates/openhuman-core/src/memory/engine.rs@@ -349,6 +349,12 @@ pub(crate) fn host_binding_with<T>(/// The configured engine with its scope root (`None` legacy).fn resolve_configured(config: &Config, root: Option<&str>) -> Binding {let engine_id = config.memory.engine.trim().to_string();+ // A registered engine (`engine_registry`) wins over the built-in match, and+ // is resolved per config, so SaaS admits it for every tenant.+ if let Some(entry) = super::engine_registry::lookup(&engine_id) {+ tracing::debug!(engine = %engine_id, "[memory:engine] binding a registered engine");+ return (entry.build)(&super::engine_registry::EngineRequest { config, root });+ }match engine_id.as_str() {TINYHUMANS_ENGINE => resolve_tinyhumans(config, root),CORTEXDB_ENGINE => resolve_cortexdb(config, root),diff --git a/crates/openhuman-core/src/memory/engine_registry.rs b/crates/openhuman-core/src/memory/engine_registry.rsnew file mode 100644index 0000000000..ba1d8de240--- /dev/null+++ b/crates/openhuman-core/src/memory/engine_registry.rs@@ -0,0 +1,102 @@+//! Registered memory engines, keyed by engine id.+//!+//! [`engine::resolve`](super::engine::resolve) builds the engines the core+//! knows (`cortexdb`, `none`). An outside crate registers another under its own+//! id with [`register_memory_engine`]: the builder turns the `[memory]` config+//! (and the layout root) into a [`Binding`], and an optional predicate says+//! whether background memory work is free for it.+//!+//! Unlike [`install_host_engine`](super::engine::install_host_engine), which+//! binds one engine for the whole process and is ignored under SaaS, a+//! registered engine is resolved per config like any configured engine, so it+//! is admitted for every tenant: the builder receives that tenant's config and+//! root. Registered ids win over the built-in match, and with nothing+//! registered the engine selection and the free-period check are unchanged.+//!+//! Free background work ([`free_period_active`](super::billing::free_period_active)):+//! an engine with no registration is free, a registered engine is free unless+//! its predicate says otherwise (a registration without one is free), and an+//! unbound engine is not free.++use std::future::Future;+use std::pin::Pin;+use std::sync::Arc;++use tinymemory_api::MemoryEngine;++use super::engine::{Binding, BoundEngine};+use crate::config::Config;+use crate::core::slots::{Extensions, HookSlot};++/// A boxed `Send` future.+pub type EngineFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;++/// What a registered engine builder is asked to bind.+pub struct EngineRequest<'a> {+ /// The tenant's configuration.+ pub config: &'a Config,+ /// The layout root: `None` the legacy tree, `Some("user:<id>")` the+ /// person's subtree.+ pub root: Option<&'a str>,+}++/// Binds the engine for a config and layout root.+pub type EngineBuilder = Arc<dyn for<'a> Fn(&EngineRequest<'a>) -> Binding + Send + Sync>;++/// Whether background memory work is free for the bound engine and config.+pub type FreeBackground = Arc<dyn Fn(Config, BoundEngine) -> EngineFuture<bool> + Send + Sync>;++/// A registered memory engine.+#[derive(Clone)]+pub struct EngineEntry {+ /// Binds the engine.+ pub build: EngineBuilder,+ /// Optional "background work is free" predicate; `None` means always free.+ pub free_background: Option<FreeBackground>,+}++static ENGINES: HookSlot<EngineEntry> = HookSlot::new("memory::engines");++/// Registers the memory engine `engine_id` for the whole process.+pub fn register_memory_engine(engine_id: impl Into<String>, entry: EngineEntry) {+ ENGINES.register(engine_id, entry);+}++/// Registers the memory engine `engine_id` for the context built from+/// `extensions`.+pub fn register_memory_engine_in(+ extensions: &Extensions,+ engine_id: impl Into<String>,+ entry: EngineEntry,+) {+ ENGINES.register_in(extensions, engine_id, entry);+}++/// Removes the process-wide memory engine `engine_id`.+pub fn unregister_memory_engine(engine_id: &str) -> bool {+ ENGINES.unregister(engine_id)+}++/// The registered entry for `engine_id`, if any.+pub(crate) fn lookup(engine_id: &str) -> Option<EngineEntry> {+ ENGINES.get(engine_id)+}++/// Wraps `engine` as a [`BoundEngine`] with the same write scrubbing every+/// configured engine gets. A registered builder returns+/// `Binding::On(bound_engine(..))`.+pub fn bound_engine(+ id: impl Into<String>,+ endpoint: impl Into<String>,+ engine: Arc<dyn MemoryEngine>,+) -> BoundEngine {+ BoundEngine {+ id: id.into(),+ endpoint: endpoint.into(),+ engine: super::guard::ScrubbingEngine::wrap(engine),+ }+}++#[cfg(test)]+#[path = "engine_registry_tests.rs"]+mod tests;diff --git a/crates/openhuman-core/src/memory/mod.rs b/crates/openhuman-core/src/memory/mod.rsindex 78efe893e1..97cec408cd 100644--- a/crates/openhuman-core/src/memory/mod.rs+++ b/crates/openhuman-core/src/memory/mod.rs@@ -28,6 +28,7 @@ pub mod confine;pub(crate) mod convert;pub mod deletion;pub mod engine;+pub mod engine_registry;pub mod error;pub mod explore;pub(crate) mod files;