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): optionally file GitHub documents one scope per repository
M3gA-Mind69d07a510d62
diff --git a/crates/openhuman-core/src/config/schema/memory.rs b/crates/openhuman-core/src/config/schema/memory.rsindex daa7caca08..352b9bd418 100644--- a/crates/openhuman-core/src/config/schema/memory.rs+++ b/crates/openhuman-core/src/config/schema/memory.rs@@ -133,6 +133,12 @@ pub struct MemoryConfig {/// Per-agent-definition memory settings, keyed by agent definition id.#[serde(skip_serializing_if = "BTreeMap::is_empty")]pub agents: BTreeMap<String, MemoryAgentConfig>,+ /// File GitHub documents one scope per repository+ /// (`source:github/project:<owner>-<repo>`) rather than all in+ /// `source:github`. Off by default: every turn reads each brain scope,+ /// so each repository adds a recall to every turn.+ #[serde(skip_serializing_if = "std::ops::Not::not")]+ pub split_github_by_repo: bool,}/// `[memory] layout`: where memory sits on the engine.@@ -202,6 +208,7 @@ impl Default for MemoryConfig {embedding_dimensions: DEFAULT_EMBEDDING_DIMENSIONS,embedding_rate_limit_per_min: DEFAULT_EMBEDDING_RATE_LIMIT_PER_MIN,agents: BTreeMap::new(),+ split_github_by_repo: false,}}}diff --git a/crates/openhuman-core/src/memory/brain.rs b/crates/openhuman-core/src/memory/brain.rsindex cb0a71363b..a80f21f864 100644--- a/crates/openhuman-core/src/memory/brain.rs+++ b/crates/openhuman-core/src/memory/brain.rs@@ -53,17 +53,55 @@ pub fn brain_source(kind: MemorySourceKind, target: &str) -> BrainSource {}}-/// `item` placed in `layout`'s brain under `source`: the source's node, and-/// no agent id (the brain belongs to every agent).-pub fn file_into(+/// The repository a GitHub document belongs to, as the collection id+/// `<owner>-<repo>` (lowercase, as GitHub's names are case-insensitive):+/// from its `repo` (`owner/name` or a URL), else its URL.+#[must_use]+pub fn github_collection(item: &StoreItem) -> Option<String> {+ let meta = item.meta();+ [meta.repo.as_deref(), meta.url.as_deref()]+ .into_iter()+ .flatten()+ .find_map(|raw| {+ let path = raw+ .trim()+ .trim_start_matches("https://")+ .trim_start_matches("http://")+ .trim_start_matches("www.")+ .trim_start_matches("github.com/");+ let mut parts = path.split('/').filter(|part| !part.is_empty());+ let (owner, repo) = (parts.next()?, parts.next()?);+ let repo = repo.trim_end_matches(".git");+ (!owner.contains('.') && !repo.is_empty())+ .then(|| format!("{owner}-{repo}").to_ascii_lowercase())+ })+}++/// The node `item` of `source` is filed at: the source's node, or, for+/// GitHub with `[memory] split_github_by_repo` on, its repository's+/// collection below it.+pub fn brain_node(+ config: &Config,layout: &MemoryLayout,source: &BrainSource,- mut item: StoreItem,-) -> MemoryResult<StoreItem> {+ item: &StoreItem,+) -> MemoryResult<Namespace> {+ if config.memory.split_github_by_repo && *source == BrainSource::Github {+ if let Some(repo) = github_collection(item) {+ return Ok(layout.brain_collection(source, &repo)?);+ }+ }+ Ok(layout.brain(source)?)+}++/// `item` placed in the brain at `node`, with no agent id (the brain+/// belongs to every agent).+#[must_use]+pub fn file_into(node: Namespace, mut item: StoreItem) -> StoreItem {let meta = item.meta_mut();- meta.namespace = layout.brain(source)?;+ meta.namespace = node;meta.agent_id = None;- Ok(item)+ item}/// The layout the brain RPCs act on: the in-scope identity's.@@ -114,24 +152,25 @@ pub async fn sources(config: &Config) -> MemoryResult<BrainSourcesView> {scan_limit: 20_000,}).await?;- let prefix = |source: &str| -> Option<String> {- let node: Namespace = source.parse().ok()?;- let last = node.segments().last()?.clone();- let parent_matches = node.depth() == layout.root().depth() + 1;- (parent_matches && last.kind() == tinymemory_api::SegmentKind::Source)- .then(|| last.id().to_string())+ // A source's collections (`source:github/project:…`) count as the+ // source's own documents.+ let source_of = |node: &str| -> Option<String> {+ let node: Namespace = node.parse().ok()?;+ let segment = node.segments().get(layout.root().depth())?;+ (segment.kind() == tinymemory_api::SegmentKind::Source).then(|| segment.id().to_string())};- let mut sources = Vec::new();+ let mut counts = std::collections::BTreeMap::<String, u64>::new();let mut unfiled = 0;for bucket in page.buckets {- match prefix(&bucket.value) {- Some(source) => sources.push(BrainSourceCount {- source,- documents: bucket.count,- }),+ match source_of(&bucket.value) {+ Some(source) => *counts.entry(source).or_default() += bucket.count,None => unfiled += bucket.count,}}+ let mut sources: Vec<BrainSourceCount> = counts+ .into_iter()+ .map(|(source, documents)| BrainSourceCount { source, documents })+ .collect();sources.sort_by(|a, b| b.documents.cmp(&a.documents).then(a.source.cmp(&b.source)));Ok(BrainSourcesView {root: layout.root().to_string(),diff --git a/crates/openhuman-core/src/memory/brain_tests.rs b/crates/openhuman-core/src/memory/brain_tests.rsindex 154a7af44c..b3a9bdc97d 100644--- a/crates/openhuman-core/src/memory/brain_tests.rs+++ b/crates/openhuman-core/src/memory/brain_tests.rs@@ -45,10 +45,80 @@ fn synced_items_are_filed_by_their_connector() {assert_eq!(files_source().to_string(), "files");}+#[test]+fn a_github_document_names_its_repository() {+ let with = |repo: Option<&str>, url: Option<&str>| {+ let mut item = document(None, None);+ item.meta_mut().repo = repo.map(str::to_string);+ item.meta_mut().url = url.map(str::to_string);+ github_collection(&item)+ };+ assert_eq!(with(Some("Acme/API"), None).as_deref(), Some("acme-api"));+ assert_eq!(+ with(Some("https://github.com/acme/api.git"), None).as_deref(),+ Some("acme-api")+ );+ // A Composio issue carries only its URL.+ assert_eq!(+ with(+ None,+ Some("https://github.com/tinyhumansai/openhuman/issues/12")+ )+ .as_deref(),+ Some("tinyhumansai-openhuman")+ );+ assert_eq!(with(None, Some("https://example.com/a/b")), None);+ assert_eq!(with(Some("acme"), None), None);+ assert_eq!(with(None, None), None);+}++#[tokio::test]+async fn a_sources_collections_count_and_forget_with_it() {+ use tinymemory_api::MemoryEngine as _;+ let tmp = tempfile::tempdir().unwrap();+ let config = config_in(&tmp);+ let engine = bind_reference(&config);+ let layout = MemoryLayout::default();+ for (node, text) in [+ (layout.brain(&BrainSource::Github).unwrap(), "org notes"),+ (+ layout+ .brain_collection(&BrainSource::Github, "acme-api")+ .unwrap(),+ "an issue",+ ),+ (layout.brain(&BrainSource::Notion).unwrap(), "a page"),+ ] {+ let meta = MemoryMeta {+ namespace: node,+ ..MemoryMeta::default()+ };+ engine.store(StoreItem::document(text, meta)).await.unwrap();+ }+ let view = sources(&config).await.unwrap();+ let counts: Vec<(&str, u64)> = view+ .sources+ .iter()+ .map(|s| (s.source.as_str(), s.documents))+ .collect();+ assert_eq!(counts, [("github", 2), ("notion", 1)]);+ assert_eq!(view.unfiled, 0);++ let gone = forget(+ &config,+ BrainForgetParams {+ source: "github".into(),+ },+ )+ .await+ .unwrap();+ assert_eq!(gone.forgotten, 2, "the source and its collection");+}+#[test]fn filing_moves_an_item_to_its_source_node_without_an_agent() {let team = MemoryLayout::new("team:acme".parse().unwrap()).unwrap();- let filed = file_into(&team, &BrainSource::Pdf, document(None, None)).unwrap();+ let filed = file_into(team.brain(&BrainSource::Pdf).unwrap(), document(None, None));assert_eq!(filed.meta().namespace.to_string(), "team:acme/source:pdf");assert_eq!(filed.meta().agent_id, None);}diff --git a/crates/openhuman-core/src/memory/sources/sync.rs b/crates/openhuman-core/src/memory/sources/sync.rsindex 07415a391b..12550c2c41 100644--- a/crates/openhuman-core/src/memory/sources/sync.rs+++ b/crates/openhuman-core/src/memory/sources/sync.rs@@ -103,8 +103,8 @@ pub async fn sync_one(config: &Config, source: &MemorySourceConfig) -> MemoryRes}/// Files `items` into `layout`'s brain, each under the brain source it-/// belongs to (`memory::brain::brain_source`), and queues one belief build-/// per source it touched. Returns how many were stored.+/// belongs to (`memory::brain::brain_node`), and queues one belief build+/// per node it touched. Returns how many were stored.pub(crate) async fn store_all(config: &Config,bound: &BoundEngine,@@ -117,11 +117,12 @@ pub(crate) async fn store_all(let mut touched = std::collections::BTreeSet::new();for item in items {let brain_source = crate::memory::brain::brain_source(kind, target);- let item = crate::memory::brain::file_into(layout, &brain_source, item)?;+ 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 {Ok(_) => {stored += 1;- touched.insert(brain_source);+ touched.insert(node);}// Out of credits or unreachable refuses every item, so stop// rather than fail each one in turn.@@ -137,9 +138,8 @@ pub(crate) async fn store_all(let automatic =bound.engine.descriptor().consolidation == tinymemory_api::Consolidation::Automatic;let jobs = touched- .iter()+ .into_iter().filter(|_| !automatic)- .filter_map(|source| layout.brain(source).ok()).map(|node| tinymemory_tools::BackgroundJob::BuildBeliefs {request: tinymemory_api::ConsolidateRequest::new(tinymemory_api::Reach::exact(node)).kinds([tinymemory_api::ItemKind::Document]),diff --git a/crates/openhuman-core/src/memory/sources/sync_tests.rs b/crates/openhuman-core/src/memory/sources/sync_tests.rsindex 5b49cf9b8f..45b48dc55d 100644--- a/crates/openhuman-core/src/memory/sources/sync_tests.rs+++ b/crates/openhuman-core/src/memory/sources/sync_tests.rs@@ -347,3 +347,70 @@ 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 (the default): one GitHub source.+ 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"+ );+}