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
fix(memory): forget a connection's items under every root it filed them under
M3gA-Mind18a7b1a9cc50
diff --git a/crates/openhuman-core/src/memory/sources/composio.rs b/crates/openhuman-core/src/memory/sources/composio.rsindex 59b11a7b39..02afe65440 100644--- a/crates/openhuman-core/src/memory/sources/composio.rs+++ b/crates/openhuman-core/src/memory/sources/composio.rs@@ -82,6 +82,15 @@ pub async fn store_records(.iter().filter_map(|record| record_item(toolkit, connection_id, source_id, record)).collect();+ if !items.is_empty() {+ // Before the write, so an item stored is never under an unrecorded+ // root.+ super::roots::record(+ &config.workspace_dir,+ connection_id,+ &layout.root().to_string(),+ );+ }super::sync::store_all(config,bound,@@ -159,11 +168,12 @@ pub fn source_id_for_toolkit(config: &Config, toolkit: &str) -> String {/// nothing and is not an error.////// With the connection's `toolkit` known, only that toolkit's brain source-/// is read (`source:<toolkit>`, where its items are filed): one source,-/// not every scope memory holds. Without it, or when that source holds-/// none of the connection's items (they were filed under a root that is no-/// longer configured, such as a removed source's own namespace), the whole-/// tree is searched, so a connection's items are never left behind.+/// is read (`source:<toolkit>`): under the current root and under every+/// root the connection filed items under before ([`super::roots`]), so+/// items left under a root no longer configured go too. Without the+/// toolkit, or when those sources hold none of the connection's items+/// (stored before roots were recorded), the whole tree is searched, so a+/// connection's items are never left behind.pub async fn forget_connection(config: &Config,connection_id: &str,@@ -174,27 +184,19 @@ pub async fn forget_connection(Err(MemoryError::Off(_)) => return Ok(0),Err(error) => return Err(error),};- let reach = match toolkit {- Some(toolkit) => {- let layout = super::layout_of(config, &source_id_for_toolkit(config, toolkit));- let source = crate::memory::brain::brain_source(- crate::config::schema::MemorySourceKind::Composio,- toolkit,- );- Some(tinymemory_api::Reach::subtree(layout.brain(&source)?))- }+ let reaches = match toolkit {+ Some(toolkit) => toolkit_reaches(config, connection_id, toolkit)?,None => {tracing::warn!(connection_id = %connection_id,"[memory:sources] toolkit unknown; forgetting the connection across all memory");- None+ Vec::new()}};- let scoped = reach.is_some();tracing::debug!(connection_id = %connection_id,- scoped,+ roots = reaches.len(),"[memory:sources] forgetting a connection's items");let filter = |reach| tinymemory_api::MetaFilter {@@ -203,25 +205,65 @@ pub async fn forget_connection(tags_any: vec![connection_tag(connection_id)],..tinymemory_api::MetaFilter::default()};- let mut forgotten = bound- .engine- .forget(tinymemory_api::ForgetTarget::Filter(filter(reach)))- .await?- .forgotten;- if scoped && forgotten == 0 {- tracing::debug!(- connection_id = %connection_id,- "[memory:sources] nothing in the toolkit's source; searching all memory"- );+ let mut forgotten = 0;+ for reach in &reaches {+ forgotten += bound+ .engine+ .forget(tinymemory_api::ForgetTarget::Filter(filter(Some(+ reach.clone(),+ ))))+ .await?+ .forgotten;+ }+ if forgotten == 0 {+ if !reaches.is_empty() {+ tracing::debug!(+ connection_id = %connection_id,+ "[memory:sources] nothing in the toolkit's sources; searching all memory"+ );+ }forgotten = bound.engine.forget(tinymemory_api::ForgetTarget::Filter(filter(None))).await?.forgotten;}+ super::roots::drop_connection(&config.workspace_dir, connection_id);Ok(forgotten)}+/// `source:<toolkit>` under the current root and under every root+/// `connection_id` filed items under.+fn toolkit_reaches(+ config: &Config,+ connection_id: &str,+ toolkit: &str,+) -> MemoryResult<Vec<tinymemory_api::Reach>> {+ let source = crate::memory::brain::brain_source(+ crate::config::schema::MemorySourceKind::Composio,+ toolkit,+ );+ let current = super::layout_of(config, &source_id_for_toolkit(config, toolkit));+ let mut roots = super::roots::of(&config.workspace_dir, connection_id);+ roots.insert(current.root().to_string());+ let mut reaches = Vec::with_capacity(roots.len());+ for root in roots {+ let layout = root+ .parse::<tinymemory_api::Namespace>()+ .map_err(|error| error.to_string())+ .and_then(|root| {+ tinymemory_tools::MemoryLayout::new(root).map_err(|error| error.to_string())+ });+ match layout {+ Ok(layout) => reaches.push(tinymemory_api::Reach::subtree(layout.brain(&source)?)),+ Err(error) => {+ tracing::warn!(%error, "[memory:sources] skipping an unreadable recorded root")+ }+ }+ }+ Ok(reaches)+}+#[cfg(test)]#[path = "composio_tests.rs"]mod tests;diff --git a/crates/openhuman-core/src/memory/sources/composio_tests.rs b/crates/openhuman-core/src/memory/sources/composio_tests.rsindex ca96aa1531..ed2dd3ba00 100644--- a/crates/openhuman-core/src/memory/sources/composio_tests.rs+++ b/crates/openhuman-core/src/memory/sources/composio_tests.rs@@ -225,6 +225,56 @@ async fn forget_connection_removes_only_that_connections_items() {assert!(left[0].meta.tags.contains(&"connection:conn-b".to_string()));}+#[tokio::test]+async fn forget_connection_reads_every_root_its_items_were_filed_under() {+ let tmp = tempfile::tempdir().unwrap();+ let config = config_in(&tmp);+ let engine = bind_reference(&config);+ let bound = crate::memory::engine::resolve(&config).engine().unwrap();+ // One connection's items under the current root and under one it used+ // before (a source namespace since changed), plus another connection.+ for (connection, layout) in [+ ("conn-c", MemoryLayout::default()),+ (+ "conn-c",+ MemoryLayout::new("team:old".parse().unwrap()).unwrap(),+ ),+ ("conn-d", MemoryLayout::default()),+ ] {+ store_records(+ &config,+ &bound,+ "gmail",+ connection,+ "src",+ &layout,+ &[record(+ &format!("{connection}-{}", layout.root()),+ "m",+ "mail",+ )],+ )+ .await+ .unwrap();+ }+ assert_eq!(+ super::super::roots::of(&config.workspace_dir, "conn-c").len(),+ 2+ );++ assert_eq!(+ forget_connection(&config, "conn-c", Some("gmail"))+ .await+ .unwrap(),+ 2,+ "both roots' items, though the current one alone found some"+ );+ let left = stored(&engine, MetaFilter::default()).await;+ assert_eq!(left.len(), 1);+ assert!(left[0].meta.tags.contains(&"connection:conn-d".to_string()));+ assert!(super::super::roots::of(&config.workspace_dir, "conn-c").is_empty());+}+#[tokio::test]async fn forget_connection_with_memory_off_forgets_nothing() {let tmp = tempfile::tempdir().unwrap();diff --git a/crates/openhuman-core/src/memory/sources/mod.rs b/crates/openhuman-core/src/memory/sources/mod.rsindex 94b3bea980..cf8adb5c94 100644--- a/crates/openhuman-core/src/memory/sources/mod.rs+++ b/crates/openhuman-core/src/memory/sources/mod.rs@@ -9,6 +9,7 @@//! `memory_sources_sync` cron job ([`sync_due`]).pub mod composio;+pub mod roots;pub mod state;mod sync;diff --git a/crates/openhuman-core/src/memory/sources/roots.rs b/crates/openhuman-core/src/memory/sources/roots.rsnew file mode 100644index 0000000000..83c3f9bf81--- /dev/null+++ b/crates/openhuman-core/src/memory/sources/roots.rs@@ -0,0 +1,74 @@+//! Which layout roots each Composio connection's items were filed under.+//!+//! A connection's items go to its toolkit's brain source in the layout of+//! the source configured for it, and that layout can change between syncs+//! (a source's own `namespace` set, changed or removed). Forgetting a+//! connection reads `source:<toolkit>` under every root recorded here, so+//! items filed under an earlier root are forgotten with the rest. It lives+//! in `<workspace>/memory/connection_roots.json`.++use std::collections::{BTreeMap, BTreeSet};+use std::path::{Path, PathBuf};+use std::sync::{LazyLock, Mutex};++/// Serialises read-modify-writes of the file in this process.+static LOCK: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));++fn path(workspace_dir: &Path) -> PathBuf {+ workspace_dir.join("memory").join("connection_roots.json")+}++fn read(workspace_dir: &Path) -> BTreeMap<String, BTreeSet<String>> {+ std::fs::read_to_string(path(workspace_dir))+ .ok()+ .and_then(|text| serde_json::from_str(&text).ok())+ .unwrap_or_default()+}++fn write(workspace_dir: &Path, all: &BTreeMap<String, BTreeSet<String>>) {+ let file = path(workspace_dir);+ let result = file+ .parent()+ .map_or(Ok(()), std::fs::create_dir_all)+ .and_then(|()| {+ let json = serde_json::to_vec_pretty(all).map_err(std::io::Error::other)?;+ std::fs::write(&file, json)+ });+ if let Err(error) = result {+ tracing::warn!(error = %error, "[memory:sources] writing connection roots failed");+ }+}++/// Records that `connection_id` filed items under `root`.+pub fn record(workspace_dir: &Path, connection_id: &str, root: &str) {+ let _guard = LOCK+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ let mut all = read(workspace_dir);+ if all+ .entry(connection_id.to_string())+ .or_default()+ .insert(root.to_string())+ {+ write(workspace_dir, &all);+ }+}++/// Every root `connection_id` filed items under.+#[must_use]+pub fn of(workspace_dir: &Path, connection_id: &str) -> BTreeSet<String> {+ read(workspace_dir)+ .remove(connection_id)+ .unwrap_or_default()+}++/// Forgets `connection_id`'s roots, once its items are gone.+pub fn drop_connection(workspace_dir: &Path, connection_id: &str) {+ let _guard = LOCK+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ let mut all = read(workspace_dir);+ if all.remove(connection_id).is_some() {+ write(workspace_dir, &all);+ }+}