Skip to content

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

feat(memory): add source sync for brain memory

Steven Enamakel297c58060ea5
diff --git a/crates/openhuman-core/src/memory/brain.rs b/crates/openhuman-core/src/memory/brain.rs
index c0da42e150..2452313641 100644
--- a/crates/openhuman-core/src/memory/brain.rs
+++ b/crates/openhuman-core/src/memory/brain.rs
@@ -34,25 +34,29 @@ pub fn files_source() -> BrainSource {
BrainSource::Other("files".to_string())
}
-/// The brain source a synced item belongs to: the connector it came from,
-/// so disconnecting or removing it erases one source. GitHub (the reader
-/// and the Composio toolkit alike) to `github`, links and feeds to `web`, a
-/// Composio toolkit to its canonical slug (`gmail`, `googledrive`), and
-/// local files, whatever their format, to `files`.
+/// The brain source a synced item belongs to. Only local folders and files
+/// are synced, so every source files under `files`, whatever the format.
#[must_use]
-pub fn brain_source(kind: MemorySourceKind, target: &str) -> BrainSource {
+pub fn brain_source(kind: MemorySourceKind) -> BrainSource {
match kind {
- MemorySourceKind::Github => BrainSource::Github,
- MemorySourceKind::Link | MemorySourceKind::Rss => BrainSource::Web,
- MemorySourceKind::Composio => {
- crate::integrations::composio::tools::canonicalize_toolkit_slug(target)
- .parse()
- .unwrap_or_else(|_| BrainSource::Other("composio".to_string()))
- }
MemorySourceKind::Folder | MemorySourceKind::File => files_source(),
}
}
+/// Canonical form of a connector slug found on a legacy brain node
+/// (`google_drive` becomes `googledrive`), kept so documents filed by the
+/// removed Composio sync still migrate to the node they were filed under.
+fn legacy_connector_slug(slug: &str) -> String {
+ let key = slug.trim().to_ascii_lowercase();
+ match key.as_str() {
+ "feishu" | "lark" => "larksuite".to_string(),
+ "google_calendar" => "googlecalendar".to_string(),
+ "google_drive" => "googledrive".to_string(),
+ "google_sheets" => "googlesheets".to_string(),
+ _ => key,
+ }
+}
+
/// The brain source a document filed under the old per-type layout belongs
/// to now, for the migration that moves it: `old_source_id` is the id of
/// the `source:<id>` node it sits at, `item` the document itself.
@@ -65,7 +69,7 @@ pub fn brain_source(kind: MemorySourceKind, target: &str) -> BrainSource {
/// becomes `googledrive`), so `notion`, `github` and `gmail` stay put.
#[must_use]
pub fn legacy_brain_node(old_source_id: &str, item: &StoreItem) -> BrainSource {
- let id = crate::integrations::composio::tools::canonicalize_toolkit_slug(old_source_id);
+ let id = legacy_connector_slug(old_source_id);
match id.as_str() {
"files" | "pdf" | "markdown" | "md" | "docx" | "xlsx" | "pptx" | "code" | "other" => {
files_source()
diff --git a/crates/openhuman-core/src/memory/brain_tests.rs b/crates/openhuman-core/src/memory/brain_tests.rs
index 0c4713a4ee..bc15a3c598 100644
--- a/crates/openhuman-core/src/memory/brain_tests.rs
+++ b/crates/openhuman-core/src/memory/brain_tests.rs
@@ -17,30 +17,9 @@ fn document(mime: Option<&str>, path: Option<&str>) -> StoreItem {
}
#[test]
-fn synced_items_are_filed_by_their_connector() {
- for (kind, target, want) in [
- (MemorySourceKind::Github, "o/r", BrainSource::Github),
- (MemorySourceKind::Link, "https://x", BrainSource::Web),
- (MemorySourceKind::Rss, "https://x/feed", BrainSource::Web),
- (MemorySourceKind::Composio, "Notion", BrainSource::Notion),
- // The Composio toolkit and the GitHub reader share one source.
- (MemorySourceKind::Composio, "github", BrainSource::Github),
- (
- MemorySourceKind::Composio,
- "gmail",
- BrainSource::Other("gmail".into()),
- ),
- // An alias files under the slug Composio itself uses.
- (
- MemorySourceKind::Composio,
- "google_drive",
- BrainSource::Other("googledrive".into()),
- ),
- // Local files, whatever their format, share one source.
- (MemorySourceKind::Folder, "/n", files_source()),
- (MemorySourceKind::File, "/n/deck/Q3.PDF", files_source()),
- ] {
- assert_eq!(brain_source(kind, target), want, "{kind:?} {target}");
+fn synced_items_are_filed_under_files() {
+ for kind in MemorySourceKind::ALL {
+ assert_eq!(brain_source(kind), files_source(), "{kind:?}");
}
assert_eq!(files_source().to_string(), "files");
}
diff --git a/crates/openhuman-core/src/memory/sources/sync.rs b/crates/openhuman-core/src/memory/sources/sync.rs
index 040737e9f4..fc83a0a4a3 100644
--- a/crates/openhuman-core/src/memory/sources/sync.rs
+++ b/crates/openhuman-core/src/memory/sources/sync.rs
@@ -67,7 +67,7 @@ pub async fn sync_one(config: &Config, source: &MemorySourceConfig) -> MemoryRes
config,
&bound,
collected.items,
- (source.kind, &source.target, &source.id),
+ (source.kind, &source.id),
&super::layout_of_source(config, source),
)
.await
@@ -80,7 +80,7 @@ pub(crate) async fn store_all(
config: &Config,
bound: &BoundEngine,
items: Vec<tinymemory_api::StoreItem>,
- (kind, target, source_id): (crate::config::schema::MemorySourceKind, &str, &str),
+ (kind, source_id): (crate::config::schema::MemorySourceKind, &str),
layout: &tinymemory_tools::MemoryLayout,
) -> MemoryResult<u64> {
let mut stored = 0u64;
@@ -88,7 +88,7 @@ pub(crate) async fn store_all(
let mut last_error = None;
let mut touched = std::collections::BTreeSet::new();
for item in items {
- let brain_source = crate::memory::brain::brain_source(kind, target);
+ let brain_source = crate::memory::brain::brain_source(kind);
let node = crate::memory::brain::brain_node(config, layout, &brain_source, &item)?;
let item = crate::memory::brain::file_into(node.clone(), item);
match store_on(bound, item).await {
diff --git a/crates/openhuman-core/src/memory/sources/sync_tests.rs b/crates/openhuman-core/src/memory/sources/sync_tests.rs
index a2dc8fcb3d..f1a106d6f0 100644
--- a/crates/openhuman-core/src/memory/sources/sync_tests.rs
+++ b/crates/openhuman-core/src/memory/sources/sync_tests.rs
@@ -51,25 +51,10 @@ fn reader_entry_maps_every_kind() {
schedule_mins: None,
namespace: None,
};
- let folder = reader_entry(&mk(MemorySourceKind::Folder, "/p"))
- .unwrap()
- .unwrap();
+ let folder = reader_entry(&mk(MemorySourceKind::Folder, "/p")).unwrap();
assert_eq!(folder.path.as_deref(), Some("/p"));
- let file = reader_entry(&mk(MemorySourceKind::File, "/p/a.md"))
- .unwrap()
- .unwrap();
+ let file = reader_entry(&mk(MemorySourceKind::File, "/p/a.md")).unwrap();
assert_eq!(file.path.as_deref(), Some("/p/a.md"));
- for (kind, target) in [
- (MemorySourceKind::Link, "https://example.com/"),
- (MemorySourceKind::Github, "https://github.com/o/r"),
- (MemorySourceKind::Rss, "https://example.com/feed"),
- ] {
- let entry = reader_entry(&mk(kind, target)).unwrap().unwrap();
- assert_eq!(entry.url.as_deref(), Some(target));
- }
- assert!(reader_entry(&mk(MemorySourceKind::Composio, "gmail"))
- .unwrap()
- .is_none());
}
#[tokio::test]
@@ -261,7 +246,7 @@ async fn store_all_skips_a_bad_item_but_fails_when_nothing_stored() {
&config,
&bound,
vec![bad.clone(), good],
- (MemorySourceKind::Folder, "/n", "src"),
+ (MemorySourceKind::Folder, "src"),
&tinymemory_tools::MemoryLayout::default()
)
.await
@@ -272,7 +257,7 @@ async fn store_all_skips_a_bad_item_but_fails_when_nothing_stored() {
&config,
&bound,
vec![bad],
- (MemorySourceKind::Folder, "/n", "src"),
+ (MemorySourceKind::Folder, "src"),
&tinymemory_tools::MemoryLayout::default()
)
.await
@@ -282,7 +267,7 @@ async fn store_all_skips_a_bad_item_but_fails_when_nothing_stored() {
&config,
&bound,
Vec::new(),
- (MemorySourceKind::Folder, "/n", "src"),
+ (MemorySourceKind::Folder, "src"),
&tinymemory_tools::MemoryLayout::default()
)
.await
@@ -334,7 +319,7 @@ async fn a_sync_queues_a_belief_build_only_for_an_engine_that_waits_for_one() {
&config,
&bound,
vec![note()],
- (MemorySourceKind::Folder, "/n", "src"),
+ (MemorySourceKind::Folder, "src"),
&tinymemory_tools::MemoryLayout::default(),
)
.await
@@ -347,71 +332,3 @@ async fn a_sync_queues_a_belief_build_only_for_an_engine_that_waits_for_one() {
assert_eq!(pending, queued, "{consolidation:?}");
}
}
-
-#[tokio::test]
-async fn github_files_per_repository_only_when_switched_on() {
- let tmp = tempfile::tempdir().unwrap();
- let mut config = config_in(&tmp);
- let engine = bind_reference(&config);
- let bound = engine::resolve(&config).engine().unwrap();
- let issue = |repo: &str, text: &str| {
- let mut meta = tinymemory_api::MemoryMeta::default();
- meta.repo = Some(repo.to_string());
- tinymemory_api::StoreItem::document(text, meta)
- };
- let sync = |config: Config, items| {
- let bound = bound.clone();
- async move {
- store_all(
- &config,
- &bound,
- items,
- (
- MemorySourceKind::Github,
- "https://github.com/acme/api",
- "src",
- ),
- &tinymemory_tools::MemoryLayout::default(),
- )
- .await
- .unwrap()
- }
- };
- let nodes = || async {
- let mut nodes: Vec<String> = stored(&engine, MetaFilter::kinds([ItemKind::Document]))
- .await
- .into_iter()
- .map(|hit| hit.meta.namespace.to_string())
- .collect();
- nodes.sort();
- nodes
- };
-
- // Off: one GitHub source.
- config.memory.split_github_by_repo = false;
- sync(config.clone(), vec![issue("acme/api", "off")]).await;
- assert_eq!(nodes().await, ["source:github"]);
-
- config.memory.split_github_by_repo = true;
- sync(
- config.clone(),
- vec![issue("Acme/API", "one"), issue("acme/web", "two")],
- )
- .await;
- assert_eq!(
- nodes().await,
- [
- "source:github",
- "source:github/project:acme--api",
- "source:github/project:acme--web"
- ]
- );
- assert_eq!(
- crate::memory::lifecycle::jobs::snapshot(&config)
- .await
- .pending
- .len(),
- 3,
- "one belief build per node written"
- );
-}