Skip to content

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

refactor(memory-sources): extract reconcile helpers into tinymemory-sources crate

Steven Enamakela1f27fb53303
diff --git a/crates/openhuman-core/src/memory/sources/reconcile.rs b/crates/openhuman-core/src/memory/sources/reconcile.rs
index 6313cc7292..c81f1d2e5c 100644
--- a/crates/openhuman-core/src/memory/sources/reconcile.rs
+++ b/crates/openhuman-core/src/memory/sources/reconcile.rs
@@ -36,7 +36,6 @@ use std::collections::HashSet;
use crate::config::rpc as config_rpc;
use crate::memory::sources::registry;
-use crate::memory::sources::types::{MemorySourceEntry, SourceKind};
/// Current version of the caps migration. Bump when the migration logic
/// changes, so installs that ran an earlier revision re-run it exactly once.
@@ -83,7 +82,15 @@ pub async fn ensure_composio_sources() -> Option<HashSet<String>> {
// load + save via the batch path. The per-call upsert does its own
// load-modify-save, so a per-target loop costs 2N config round-trips for N
// connections; batching collapses that to 2.
- let upsert_targets = build_upsert_targets(&targets);
+ let upsert_targets: Vec<registry::ComposioUpsertTarget> = targets
+ .iter()
+ .map(|target| {
+ tinymemory_sources::reconcile::composio_upsert_target(
+ &target.toolkit,
+ &target.connection_id,
+ )
+ })
+ .collect();
let upserted = match registry::upsert_composio_sources_batch(&upsert_targets).await {
Ok(n) => n,
Err(e) => {
@@ -123,94 +130,6 @@ pub async fn ensure_composio_sources() -> Option<HashSet<String>> {
Some(targets.iter().map(|t| t.connection_id.clone()).collect())
}
-/// Build the `(toolkit, connection_id, label)` upsert targets for a batch
-/// reconcile from the scanned Composio sync targets.
-///
-/// The label is a title-cased toolkit name plus the truncated connection id so
-/// distinct accounts of the same toolkit (e.g. two Gmail logins) don't all show
-/// as "Gmail connection". Pure (no I/O) so it can be unit-tested directly.
-fn build_upsert_targets(
- targets: &[crate::memory::sync::composio::SyncTarget],
-) -> Vec<registry::ComposioUpsertTarget> {
- targets
- .iter()
- .map(|target| {
- let label = format!(
- "{} · {}",
- title_case(&target.toolkit),
- short_id(&target.connection_id)
- );
- (target.toolkit.clone(), target.connection_id.clone(), label)
- })
- .collect()
-}
-
-fn title_case(s: &str) -> String {
- let mut chars = s.chars();
- match chars.next() {
- None => String::new(),
- Some(c) => c.to_uppercase().chain(chars).collect(),
- }
-}
-
-fn short_id(id: &str) -> &str {
- // Show only the last 8 Unicode scalar values to keep labels compact.
- // Byte-slicing would panic if the cut point isn't a UTF-8 boundary.
- let n = id.chars().count();
- if n <= 8 {
- return id;
- }
- let skip = n - 8;
- let start = id.char_indices().nth(skip).map(|(idx, _)| idx).unwrap_or(0);
- &id[start..]
-}
-
-/// Apply conservative default caps in place to every cap-less source.
-///
-/// For a Composio source with no `max_items` / `sync_depth_days`, writes the
-/// per-toolkit defaults **and enables it** (a no-op when already enabled) — an
-/// already-enabled, cap-less source would otherwise sync at the provider's
-/// large internal ceiling instead of the cheap default, which is the cost this
-/// migration exists to avoid. For other kinds it fills any unset kind-specific
-/// caps through [`registry::apply_kind_defaults`]. Caps the user has
-/// customised (any non-`None` value) are never overwritten.
-///
-/// Returns the number of Composio entries that received defaults. Pure (no
-/// I/O) so it can be unit-tested directly.
-fn apply_caps_defaults_to_entries(sources: &mut [MemorySourceEntry]) -> u32 {
- let mut applied = 0u32;
- for source in sources.iter_mut() {
- match source.kind {
- SourceKind::Composio => {
- // Applies to enabled AND disabled cap-less sources; skips
- // entries the user has already customised (any non-None cap).
- if source.max_items.is_none() && source.sync_depth_days.is_none() {
- let toolkit = source.toolkit.as_deref().unwrap_or("");
- let (max_items, sync_depth_days) =
- registry::memory_sync_defaults_for_toolkit(toolkit);
- tracing::debug!(
- id = %source.id,
- toolkit = %toolkit,
- was_enabled = source.enabled,
- max_items = ?max_items,
- sync_depth_days = ?sync_depth_days,
- "[memory_sources:reconcile] caps migration: applying conservative defaults"
- );
- source.enabled = true;
- source.max_items = max_items;
- source.sync_depth_days = sync_depth_days;
- applied += 1;
- }
- }
- // Non-composio kinds get their kind defaults through the same
- // helper the CRUD path uses, so one table of conservative values
- // serves both.
- _ => registry::apply_kind_defaults(source),
- }
- }
- applied
-}
-
/// Retroactive migration: give any cap-less Composio source — enabled or
/// disabled — conservative per-toolkit caps so its first sync stays cheap.
///
@@ -245,7 +164,7 @@ pub async fn apply_composio_source_caps_migration() -> Result<(), String> {
"[memory_sources:reconcile] applying composio source caps migration"
);
- let migrated_count = apply_caps_defaults_to_entries(&mut config.memory_sources);
+ let migrated_count = tinymemory_sources::reconcile::apply_caps_defaults_to_entries(&mut config.memory_sources);
config.composio_source_caps_migration_version = CURRENT_CAPS_MIGRATION_VERSION;
config
diff --git a/crates/openhuman-core/src/memory/sources/reconcile_tests.rs b/crates/openhuman-core/src/memory/sources/reconcile_tests.rs
deleted file mode 100644
index 7085871cd3..0000000000
--- a/crates/openhuman-core/src/memory/sources/reconcile_tests.rs
+++ /dev/null
@@ -1,180 +0,0 @@
-use super::*;
-use crate::memory::sync::composio::SyncTarget;
-
-fn sync_target(toolkit: &str, connection_id: &str) -> SyncTarget {
- SyncTarget {
- toolkit: toolkit.to_string(),
- connection_id: connection_id.to_string(),
- }
-}
-
-#[test]
-fn build_upsert_targets_formats_label_and_preserves_order() {
- let targets = vec![
- sync_target("gmail", "ca_WaktIDFlZwXO"),
- sync_target("slack", "short"),
- ];
- let out = build_upsert_targets(&targets);
- assert_eq!(out.len(), 2);
- // (toolkit, connection_id, label) — toolkit/connection_id carried through verbatim.
- assert_eq!(out[0].0, "gmail");
- assert_eq!(out[0].1, "ca_WaktIDFlZwXO");
- assert_eq!(out[0].2, "Gmail · IDFlZwXO");
- assert_eq!(out[1].0, "slack");
- assert_eq!(out[1].1, "short");
- assert_eq!(out[1].2, "Slack · short");
-}
-
-#[test]
-fn build_upsert_targets_empty_is_empty() {
- let out = build_upsert_targets(&[]);
- assert!(out.is_empty());
-}
-
-#[test]
-fn short_id_truncates_ascii() {
- assert_eq!(short_id("ca_WaktIDFlZwXO"), "IDFlZwXO");
-}
-
-#[test]
-fn short_id_short_input_passthrough() {
- assert_eq!(short_id("abc"), "abc");
- assert_eq!(short_id("12345678"), "12345678");
-}
-
-#[test]
-fn short_id_utf8_safe() {
- // Multi-byte chars would have panicked with byte-slicing.
- let s = "🦀🐢🐙🦊🐼🐰🐯🐸🦁";
- let out = short_id(s);
- assert_eq!(out.chars().count(), 8);
-}
-
-// ── Caps migration ──────────────────────────────────────────────────────────
-//
-// The transform came home with `apply_composio_source_caps_migration` (#5560),
-// so its predicate is this crate's to pin. These exercise
-// `apply_caps_defaults_to_entries` — the real production function, not a
-// re-statement of it — because the migration's whole risk is in which entries
-// it decides to touch: an over-eager pass overwrites a cap the user chose, and
-// a shy one leaves an enabled, cap-less connector syncing at the provider's
-// internal ceiling.
-
-fn composio_entry(
- id: &str,
- toolkit: &str,
- enabled: bool,
- max_items: Option<u32>,
- sync_depth_days: Option<u32>,
-) -> MemorySourceEntry {
- MemorySourceEntry {
- id: id.to_string(),
- kind: SourceKind::Composio,
- label: toolkit.to_string(),
- enabled,
- toolkit: Some(toolkit.to_string()),
- connection_id: Some(format!("conn_{id}")),
- path: None,
- glob: None,
- url: None,
- branch: None,
- paths: Vec::new(),
- max_commits: None,
- max_issues: None,
- max_prs: None,
- query: None,
- since_days: None,
- max_items,
- selector: None,
- max_tokens_per_sync: None,
- max_cost_per_sync_usd: None,
- sync_depth_days,
- }
-}
-
-#[test]
-fn migration_flips_disabled_capless_entry_to_enabled_with_caps() {
- let mut sources = vec![composio_entry("s1", "gmail", false, None, None)];
- let count = apply_caps_defaults_to_entries(&mut sources);
- assert_eq!(count, 1);
- assert!(sources[0].enabled);
- assert_eq!(sources[0].max_items, Some(100));
- assert_eq!(sources[0].sync_depth_days, Some(30));
-}
-
-#[test]
-fn migration_applies_defaults_to_enabled_capless_entry() {
- // An already-enabled but cap-less source must also receive defaults —
- // otherwise its first sync runs at the provider's large internal ceiling.
- let mut sources = vec![composio_entry("s2", "slack", true, None, None)];
- let count = apply_caps_defaults_to_entries(&mut sources);
- assert_eq!(count, 1);
- assert!(sources[0].enabled);
- assert_eq!(sources[0].max_items, Some(50));
- assert_eq!(sources[0].sync_depth_days, Some(14));
-}
-
-#[test]
-fn migration_leaves_user_customised_caps_untouched() {
- // The user set max_items explicitly, so the migration must not override it
- // — and must not flip `enabled` on its way past either.
- let mut sources = vec![composio_entry("s3", "notion", false, Some(5), None)];
- let count = apply_caps_defaults_to_entries(&mut sources);
- assert_eq!(count, 0, "entry with user-set caps must not be migrated");
- assert!(!sources[0].enabled, "enabled must not be flipped");
- assert_eq!(sources[0].max_items, Some(5), "user cap must be preserved");
-}
-
-#[test]
-fn migration_is_noop_on_empty_list() {
- let mut sources: Vec<MemorySourceEntry> = vec![];
- assert_eq!(apply_caps_defaults_to_entries(&mut sources), 0);
-}
-
-#[test]
-fn migration_applies_correct_defaults_per_toolkit() {
- let toolkits = [
- ("gmail", Some(100u32), Some(30u32)),
- ("slack", Some(50), Some(14)),
- ("notion", Some(30), Some(30)),
- ("linear", Some(50), Some(30)),
- ("clickup", Some(50), Some(30)),
- ("github", Some(50), Some(30)),
- ("unknown", Some(30), Some(14)),
- ];
- for (toolkit, exp_items, exp_days) in &toolkits {
- let mut sources = vec![composio_entry("sid", toolkit, false, None, None)];
- apply_caps_defaults_to_entries(&mut sources);
- assert_eq!(
- sources[0].max_items, *exp_items,
- "max_items mismatch for toolkit={toolkit}"
- );
- assert_eq!(
- sources[0].sync_depth_days, *exp_days,
- "sync_depth_days mismatch for toolkit={toolkit}"
- );
- }
-}
-
-/// The non-Composio arm goes through `apply_kind_defaults`, which fills the
-/// kind's own caps and — unlike the Composio arm — never enables anything and
-/// never counts towards the returned tally.
-#[test]
-fn migration_fills_kind_defaults_without_counting_them() {
- let mut repo = composio_entry("s4", "github", false, None, None);
- repo.kind = SourceKind::GithubRepo;
- repo.toolkit = None;
- repo.connection_id = None;
- repo.url = Some("https://github.com/tinyhumansai/openhuman".to_string());
-
- let mut sources = vec![repo];
- let count = apply_caps_defaults_to_entries(&mut sources);
-
- assert_eq!(count, 0, "only composio entries count as migrated");
- assert!(
- !sources[0].enabled,
- "kind defaults must not enable a source"
- );
- assert_eq!(sources[0].max_prs, Some(10));
- assert!(sources[0].max_issues.is_some());
-}