Skip to content

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

fix(memory): never store a deleted connection's records after its disconnect

M3gA-Mind853f18e202f8
diff --git a/crates/openhuman-core/src/integrations/composio/ops/sync.rs b/crates/openhuman-core/src/integrations/composio/ops/sync.rs
index 180004fae2..bee540c8cf 100644
--- a/crates/openhuman-core/src/integrations/composio/ops/sync.rs
+++ b/crates/openhuman-core/src/integrations/composio/ops/sync.rs
@@ -16,7 +16,7 @@
use crate::config::Config;
use crate::core::Outcome;
use crate::memory::engine::BoundEngine;
-use crate::memory::sources::composio::{source_id_for_toolkit, store_records};
+use crate::memory::sources::composio::{is_disconnected, source_id_for_toolkit, store_records};
use super::super::module_client::{self as connectors, methods};
use super::super::providers::{SyncOutcome, SyncReason};
@@ -214,6 +214,12 @@ pub async fn run_sync_pass(
reason: &str,
pass_budget: usize,
) -> Result<SyncPassOutcome, String> {
+ // A connection deleted meanwhile is not read again: an empty pass with
+ // nothing pending ends every caller's loop.
+ if is_disconnected(config, connection_id) {
+ tracing::debug!(connection_id = %connection_id, "[composio] connection deleted; sync pass skipped");
+ return Ok(SyncPassOutcome::default());
+ }
// Sync pages inside the call; the default 30s bus deadline reported
// failure on runs the module then finished successfully.
let response = connectors::call_slow::<_, ConnectorSyncResponse>(
diff --git a/crates/openhuman-core/src/memory/sources/composio.rs b/crates/openhuman-core/src/memory/sources/composio.rs
index b76b63a183..e5fb374538 100644
--- a/crates/openhuman-core/src/memory/sources/composio.rs
+++ b/crates/openhuman-core/src/memory/sources/composio.rs
@@ -32,6 +32,30 @@ use crate::memory::error::{MemoryError, MemoryResult};
static STORE: std::sync::LazyLock<tokio::sync::Mutex<()>> =
std::sync::LazyLock::new(|| tokio::sync::Mutex::new(()));
+/// Connections deleted in this process, per workspace. A sync pass that
+/// read a connection's records before its disconnect must not store them
+/// after it, so [`forget_connection`] records the connection (under
+/// [`STORE`]) and [`store_records`] drops any later batch for it. Composio
+/// never reuses a connection id, so an entry stays for the life of the
+/// process.
+static DISCONNECTED: std::sync::LazyLock<
+ std::sync::Mutex<std::collections::HashSet<(std::path::PathBuf, String)>>,
+> = std::sync::LazyLock::new(Default::default);
+
+fn disconnected(
+) -> std::sync::MutexGuard<'static, std::collections::HashSet<(std::path::PathBuf, String)>> {
+ DISCONNECTED
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+}
+
+/// Whether `connection_id` was disconnected in this process: its records
+/// are no longer read or stored.
+#[must_use]
+pub fn is_disconnected(config: &Config, connection_id: &str) -> bool {
+ disconnected().contains(&(config.workspace_dir.clone(), connection_id.to_string()))
+}
+
/// Most connector passes one source sync runs per connection.
const MAX_PASSES_PER_CONNECTION: usize = 25;
@@ -92,6 +116,14 @@ pub async fn store_records(
records: &[ConnectorRecord],
) -> MemoryResult<u64> {
let _serial = STORE.lock().await;
+ if is_disconnected(config, connection_id) {
+ tracing::debug!(
+ connection_id = %connection_id,
+ records = records.len(),
+ "[memory:sources] dropping records read before their connection was deleted"
+ );
+ return Ok(0);
+ }
let mut keys = Vec::with_capacity(records.len());
let mut items = Vec::with_capacity(records.len());
let mut emptied = Vec::new();
@@ -256,8 +288,10 @@ pub async fn forget_connection(
Err(error) => return Err(error),
};
// A sync pass storing this connection's records finishes first, so none
- // of its items lands after the forget.
+ // of its items lands after the forget; any pass after it, even one that
+ // read its records before, stores nothing (`DISCONNECTED`).
let _serial = STORE.lock().await;
+ disconnected().insert((config.workspace_dir.clone(), connection_id.to_string()));
let recorded = super::roots::of(&config.workspace_dir, connection_id);
let reaches = match toolkit {
// An unreadable roots record cannot bound the search: search all.
diff --git a/crates/openhuman-core/src/memory/sources/composio_tests.rs b/crates/openhuman-core/src/memory/sources/composio_tests.rs
index 644edd630b..accead4163 100644
--- a/crates/openhuman-core/src/memory/sources/composio_tests.rs
+++ b/crates/openhuman-core/src/memory/sources/composio_tests.rs
@@ -430,3 +430,48 @@ async fn sync_toolkit_without_a_connector_is_an_error_not_a_panic() {
};
assert!(sync_toolkit(&config, &bound, &source).await.is_err());
}
+
+#[tokio::test]
+async fn a_disconnect_waits_for_a_store_in_progress() {
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_in(&tmp);
+ bind_reference(&config);
+ // A store of this connection's records is running.
+ let held = STORE.lock().await;
+ let forget = {
+ let config = config.clone();
+ tokio::spawn(async move { forget_connection(&config, "conn-w", Some("gmail")).await })
+ };
+ tokio::time::sleep(std::time::Duration::from_millis(100)).await;
+ assert!(!forget.is_finished(), "the disconnect waits for the store");
+ drop(held);
+ assert_eq!(forget.await.unwrap().unwrap(), 0);
+}
+
+#[tokio::test]
+async fn records_read_before_a_disconnect_are_not_stored_after_it() {
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_in(&tmp);
+ let engine = bind_reference(&config);
+ let bound = crate::memory::engine::resolve(&config).engine().unwrap();
+ forget_connection(&config, "conn-gone", Some("gmail"))
+ .await
+ .unwrap();
+ assert!(is_disconnected(&config, "conn-gone"));
+ // A pass that read its records before the disconnect stores nothing.
+ let stored_now = store_records(
+ &config,
+ &bound,
+ "gmail",
+ "conn-gone",
+ "src",
+ &MemoryLayout::default(),
+ &[record("late", "Late", "read before the disconnect")],
+ )
+ .await
+ .unwrap();
+ assert_eq!(stored_now, 0);
+ assert!(stored(&engine, MetaFilter::default()).await.is_empty());
+ // Another connection, or the same id in another workspace, is unaffected.
+ assert!(!is_disconnected(&config, "conn-other"));
+}