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(config): extract OpenClaw and Hermes importers into tinymemory-import
Steven Enamakel6a26c643b2a1
diff --git a/Cargo.lock b/Cargo.lockindex bcd91982e8..c49beb6ccb 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -4071,6 +4071,7 @@ dependencies = ["tinymemory-conversations","tinymemory-gate","tinymemory-guard",+ "tinymemory-import","tinymemory-safety","tinymemory-sources","tinyruntime-bus",@@ -7052,6 +7053,18 @@ dependencies = ["tracing",]+[[package]]+name = "tinymemory-import"+version = "0.1.0"+dependencies = [+ "anyhow",+ "directories",+ "log",+ "rusqlite",+ "serde",+ "tinymemory-api",+]+[[package]]name = "tinymemory-remote"version = "0.1.0"diff --git a/crates/openhuman-core/Cargo.toml b/crates/openhuman-core/Cargo.tomlindex 5b5a169031..6287b0cbeb 100644--- a/crates/openhuman-core/Cargo.toml+++ b/crates/openhuman-core/Cargo.toml@@ -289,6 +289,11 @@ tinymemory-conformance = { path = "../../vendor/tinymemory/crates/tinymemory-con# — stay here, behind `memory::guard::HostGuardPolicy`. Only `tinymemory-api`,# `async-trait`, `serde_json`, `chrono`, `log` and `tracing`, all already here.tinymemory-guard = { path = "../../vendor/tinymemory/crates/tinymemory-guard" }+# `tinymemory-import` holds the OpenClaw / Hermes workspace importers behind+# `config::migration_helpers`. It writes into whichever `Memory` the host+# opens (`target_memory_backend`, which refuses a null driver) and adds no+# dependency beyond rusqlite, already linked here.+tinymemory-import = { path = "../../vendor/tinymemory/crates/tinymemory-import" }# `tinymemory-safety` is the secret and PII scrubber (credential patterns,# sensitive-key classifier, checksum-gated multilingual national-ID redaction)# that `memory::safety` used to carry a private copy of. `regex`, `serde_json`diff --git a/crates/openhuman-core/src/config/migration_helpers/core.rs b/crates/openhuman-core/src/config/migration_helpers/core.rsindex 3d68d836c2..19a51c7d3d 100644--- a/crates/openhuman-core/src/config/migration_helpers/core.rs+++ b/crates/openhuman-core/src/config/migration_helpers/core.rs@@ -1,121 +1,44 @@use crate::config::Config;-use crate::memory::{Memory, MemoryCategory};-use anyhow::{bail, Context, Result};-use directories::UserDirs;-use rusqlite::{Connection, OpenFlags, OptionalExtension};-use serde::{Deserialize, Serialize};-use std::collections::HashSet;-use std::fs;-use std::path::{Path, PathBuf};+use crate::memory::Memory;+use anyhow::Result;+use std::path::PathBuf;use std::sync::Arc;-#[derive(Debug, Clone)]-struct SourceEntry {- key: String,- content: String,- category: MemoryCategory,-}--#[derive(Debug, Default, Clone, Serialize, Deserialize)]-pub struct MigrationStats {- pub from_sqlite: usize,- pub from_markdown: usize,- pub imported: usize,- pub skipped_unchanged: usize,- pub renamed_conflicts: usize,-}--#[derive(Debug, Clone, Serialize, Deserialize)]-pub struct MigrationReport {- pub source_workspace: PathBuf,- pub target_workspace: PathBuf,- pub dry_run: bool,- pub stats: MigrationStats,- pub warnings: Vec<String>,-}+pub use tinymemory_import::{MigrationReport, MigrationStats};+/// Import an OpenClaw workspace into this host's memory.+///+/// The importer is `tinymemory-import`'s; the host supplies the target+/// workspace and [`target_memory_backend`], which it opens only once there is+/// something to write.pub async fn migrate_openclaw_memory(config: &Config,source_workspace: Option<PathBuf>,dry_run: bool,) -> Result<MigrationReport> {- let source_workspace = resolve_openclaw_workspace(source_workspace)?;- if !source_workspace.exists() {- bail!(- "OpenClaw workspace not found at {}. Provide a valid source workspace.",- source_workspace.display()- );- }-- if paths_equal(&source_workspace, &config.workspace_dir) {- bail!("Source workspace matches current OpenHuman workspace; refusing self-migration");- }-- let mut stats = MigrationStats::default();- let entries = collect_source_entries(&source_workspace, &mut stats)?;- let mut warnings = Vec::new();-- if entries.is_empty() {- warnings.push(format!(- "No importable memory found in {}",- source_workspace.display()- ));- warnings.push("Checked for: memory/brain.db, MEMORY.md, memory/*.md".to_string());- return Ok(MigrationReport {- source_workspace,- target_workspace: config.workspace_dir.clone(),- dry_run,- stats,- warnings,- });- }-- if dry_run {- return Ok(MigrationReport {- source_workspace,- target_workspace: config.workspace_dir.clone(),- dry_run,- stats,- warnings,- });- }-- if let Some(backup_dir) = backup_target_memory(&config.workspace_dir)? {- warnings.push(format!("Backup created: {}", backup_dir.display()));- }-- let memory = target_memory_backend(config)?;-- for (idx, entry) in entries.into_iter().enumerate() {- let mut key = entry.key.trim().to_string();- if key.is_empty() {- key = format!("openclaw_{idx}");- }-- if let Some(existing) = memory.get("", &key).await? {- if existing.content.trim() == entry.content.trim() {- stats.skipped_unchanged += 1;- continue;- }-- let renamed = next_available_key(memory.as_ref(), &key).await?;- key = renamed;- stats.renamed_conflicts += 1;- }-- memory- .store("", &key, &entry.content, entry.category, None)- .await?;- stats.imported += 1;- }+ tinymemory_import::migrate_openclaw_memory(+ &config.workspace_dir,+ source_workspace,+ dry_run,+ || target_memory_backend(config),+ )+ .await+}- Ok(MigrationReport {+/// Import a Hermes workspace into this host's memory. See+/// [`migrate_openclaw_memory`].+pub async fn migrate_hermes_memory(+ config: &Config,+ source_workspace: Option<PathBuf>,+ dry_run: bool,+) -> Result<MigrationReport> {+ tinymemory_import::migrate_hermes_memory(+ &config.workspace_dir,source_workspace,- target_workspace: config.workspace_dir.clone(),dry_run,- stats,- warnings,- })+ || target_memory_backend(config),+ )+ .await}/// The memory the import writes into.@@ -214,427 +137,3 @@ fn target_memory_backend(config: &Config) -> Result<Arc<dyn Memory>> {crate::agent::experience::ops::DriverMemory::for_config(config).map_err(|e| anyhow::anyhow!("bind memory for migration import: {e}"))}--fn collect_source_entries(- source_workspace: &Path,- stats: &mut MigrationStats,-) -> Result<Vec<SourceEntry>> {- let mut entries = Vec::new();-- let sqlite_path = source_workspace.join("memory").join("brain.db");- let sqlite_entries = read_openclaw_sqlite_entries(&sqlite_path)?;- stats.from_sqlite = sqlite_entries.len();- entries.extend(sqlite_entries);-- let markdown_entries = read_openclaw_markdown_entries(source_workspace)?;- stats.from_markdown = markdown_entries.len();- entries.extend(markdown_entries);-- // De-dup exact duplicates to make re-runs deterministic.- let mut seen = HashSet::new();- entries.retain(|entry| {- let sig = format!("{}\u{0}{}\u{0}{}", entry.key, entry.content, entry.category);- seen.insert(sig)- });-- Ok(entries)-}--fn read_openclaw_sqlite_entries(db_path: &Path) -> Result<Vec<SourceEntry>> {- if !db_path.exists() {- return Ok(Vec::new());- }-- let conn = Connection::open_with_flags(db_path, OpenFlags::SQLITE_OPEN_READ_ONLY)- .with_context(|| format!("Failed to open source db {}", db_path.display()))?;-- let table_exists: Option<String> = conn- .query_row(- "SELECT name FROM sqlite_master WHERE type='table' AND name='memories' LIMIT 1",- [],- |row| row.get(0),- )- .optional()?;-- if table_exists.is_none() {- return Ok(Vec::new());- }-- let columns = table_columns(&conn, "memories")?;- let key_expr = pick_column_expr(&columns, &["key", "id", "name"], "CAST(rowid AS TEXT)");- let Some(content_expr) =- pick_optional_column_expr(&columns, &["content", "value", "text", "memory"])- else {- bail!("OpenClaw memories table found but no content-like column was detected");- };- let category_expr = pick_column_expr(&columns, &["category", "kind", "type"], "'core'");-- let sql = format!(- "SELECT {key_expr} AS key, {content_expr} AS content, {category_expr} AS category FROM memories"- );-- let mut stmt = conn.prepare(&sql)?;- let mut rows = stmt.query([])?;-- let mut entries = Vec::new();- let mut idx = 0_usize;-- while let Some(row) = rows.next()? {- let key: String = row- .get(0)- .unwrap_or_else(|_| format!("openclaw_sqlite_{idx}"));- let content: String = row.get(1).unwrap_or_default();- let category_raw: String = row.get(2).unwrap_or_else(|_| "core".to_string());-- if content.trim().is_empty() {- continue;- }-- entries.push(SourceEntry {- key: normalize_key(&key, idx),- content: content.trim().to_string(),- category: parse_category(&category_raw),- });-- idx += 1;- }-- Ok(entries)-}--fn read_openclaw_markdown_entries(workspace: &Path) -> Result<Vec<SourceEntry>> {- let mut entries = Vec::new();-- let top_level = workspace.join("MEMORY.md");- if top_level.exists() {- let content = fs::read_to_string(&top_level)- .with_context(|| format!("Failed to read {}", top_level.display()))?;- if !content.trim().is_empty() {- entries.push(SourceEntry {- key: "openclaw_memory_md".to_string(),- content: content.trim().to_string(),- category: MemoryCategory::Core,- });- }- }-- let memory_dir = workspace.join("memory");- if !memory_dir.exists() {- return Ok(entries);- }-- let mut idx = 0_usize;- for entry in fs::read_dir(&memory_dir)? {- let entry = entry?;- let path = entry.path();-- if path.extension().and_then(|s| s.to_str()) != Some("md") {- continue;- }-- let content = fs::read_to_string(&path)- .with_context(|| format!("Failed to read {}", path.display()))?;- if content.trim().is_empty() {- continue;- }-- let file_stem = path- .file_stem()- .and_then(|s| s.to_str())- .unwrap_or("openclaw");-- entries.push(SourceEntry {- key: normalize_key(file_stem, idx),- content: content.trim().to_string(),- category: MemoryCategory::Core,- });-- idx += 1;- }-- Ok(entries)-}--fn resolve_openclaw_workspace(source: Option<PathBuf>) -> Result<PathBuf> {- if let Some(path) = source {- return Ok(path);- }-- let Some(user_dirs) = UserDirs::new() else {- bail!("Failed to determine user home directory");- };-- Ok(user_dirs.home_dir().join(".openclaw").join("workspace"))-}--fn resolve_hermes_workspace(source: Option<PathBuf>) -> Result<PathBuf> {- if let Some(path) = source {- return Ok(path);- }-- let Some(user_dirs) = UserDirs::new() else {- bail!("Failed to determine user home directory");- };-- #[cfg(windows)]- {- if let Some(local_app_data) = std::env::var_os("LOCALAPPDATA") {- return Ok(PathBuf::from(local_app_data).join("hermes"));- }- }-- Ok(user_dirs.home_dir().join(".hermes"))-}--fn hermes_file_mappings() -> Vec<(&'static str, &'static str, MemoryCategory)> {- vec![- ("MEMORY.md", "hermes_memory", MemoryCategory::Core),- (- "USER.md",- "hermes_user_profile",- MemoryCategory::Custom("user_profile".to_string()),- ),- (- "SOUL.md",- "hermes_persona",- MemoryCategory::Custom("persona".to_string()),- ),- ]-}--pub async fn migrate_hermes_memory(- config: &Config,- source_workspace: Option<PathBuf>,- dry_run: bool,-) -> Result<MigrationReport> {- let source_workspace = resolve_hermes_workspace(source_workspace)?;- if !source_workspace.exists() {- bail!(- "Hermes workspace not found at {}. Provide a valid source workspace.",- source_workspace.display()- );- }-- if paths_equal(&source_workspace, &config.workspace_dir) {- bail!("Source workspace matches current OpenHuman workspace; refusing self-migration");- }-- let mut stats = MigrationStats::default();- let mut warnings = Vec::new();- let mut entries = Vec::new();-- for (filename, key, category) in hermes_file_mappings() {- let path = source_workspace.join(filename);- if !path.exists() {- warnings.push(format!(- "{filename} not found in {}",- source_workspace.display()- ));- continue;- }- let content = fs::read_to_string(&path)- .with_context(|| format!("Failed to read {}", path.display()))?;- if content.trim().is_empty() {- warnings.push(format!("{filename} is empty, skipping"));- continue;- }- entries.push(SourceEntry {- key: key.to_string(),- content: content.trim().to_string(),- category,- });- }-- stats.from_markdown = entries.len();-- if entries.is_empty() {- warnings.push(format!(- "No importable memory found in {}",- source_workspace.display()- ));- warnings.push("Checked for: MEMORY.md, USER.md, SOUL.md".to_string());- return Ok(MigrationReport {- source_workspace,- target_workspace: config.workspace_dir.clone(),- dry_run,- stats,- warnings,- });- }-- if dry_run {- return Ok(MigrationReport {- source_workspace,- target_workspace: config.workspace_dir.clone(),- dry_run,- stats,- warnings,- });- }-- if let Some(backup_dir) = backup_target_memory(&config.workspace_dir)? {- warnings.push(format!("Backup created: {}", backup_dir.display()));- }-- let memory = target_memory_backend(config)?;-- for entry in entries {- let mut key = entry.key;-- if let Some(existing) = memory.get("", &key).await? {- if existing.content.trim() == entry.content.trim() {- stats.skipped_unchanged += 1;- continue;- }- let renamed = next_available_key(memory.as_ref(), &key).await?;- key = renamed;- stats.renamed_conflicts += 1;- }-- memory- .store("", &key, &entry.content, entry.category, None)- .await?;- stats.imported += 1;- }-- Ok(MigrationReport {- source_workspace,- target_workspace: config.workspace_dir.clone(),- dry_run,- stats,- warnings,- })-}--fn paths_equal(left: &Path, right: &Path) -> bool {- if let (Ok(left), Ok(right)) = (left.canonicalize(), right.canonicalize()) {- left == right- } else {- left == right- }-}--fn normalize_key(raw: &str, idx: usize) -> String {- let trimmed = raw.trim();- if trimmed.is_empty() {- return format!("openclaw_{idx}");- }-- trimmed- .chars()- .map(|c| {- if c.is_alphanumeric() || c == '-' || c == '_' {- c- } else {- '_'- }- })- .collect::<String>()- .trim_matches('_')- .to_string()-}--fn parse_category(raw: &str) -> MemoryCategory {- match raw.trim().to_lowercase().as_str() {- "core" => MemoryCategory::Core,- "daily" => MemoryCategory::Daily,- "conversation" => MemoryCategory::Conversation,- "personal" => MemoryCategory::Custom("personal".to_string()),- "project" => MemoryCategory::Custom("project".to_string()),- "episode" => MemoryCategory::Custom("episode".to_string()),- other => MemoryCategory::Custom(other.to_string()),- }-}--fn backup_target_memory(workspace_dir: &Path) -> Result<Option<PathBuf>> {- let mem_dir = workspace_dir.join("memory");- let markdown = workspace_dir.join("MEMORY.md");- let sqlite = mem_dir.join("brain.db");-- if !mem_dir.exists() && !markdown.exists() && !sqlite.exists() {- return Ok(None);- }-- let backup_dir = workspace_dir.join("memory_backup");- fs::create_dir_all(&backup_dir)?;-- if markdown.exists() {- let dest = backup_dir.join("MEMORY.md");- fs::copy(&markdown, &dest).ok();- }-- if sqlite.exists() {- let dest = backup_dir.join("brain.db");- fs::copy(&sqlite, &dest).ok();- }-- if mem_dir.exists() {- let dest_dir = backup_dir.join("memory");- if !dest_dir.exists() {- fs::create_dir_all(&dest_dir).ok();- }- for entry in fs::read_dir(&mem_dir)? {- let entry = entry?;- let path = entry.path();- if path.extension().and_then(|s| s.to_str()) != Some("md") {- continue;- }- let dest = dest_dir.join(- path.file_name()- .and_then(|s| s.to_str())- .unwrap_or("memory.md"),- );- fs::copy(&path, &dest).ok();- }- }-- Ok(Some(backup_dir))-}--fn table_columns(conn: &Connection, table: &str) -> Result<Vec<String>> {- let mut stmt = conn.prepare(&format!("PRAGMA table_info({table})"))?;- let mut rows = stmt.query([])?;-- let mut columns = Vec::new();- while let Some(row) = rows.next()? {- let name: String = row.get(1)?;- columns.push(name);- }-- Ok(columns)-}--fn pick_column_expr<'a>(- columns: &'a [String],- candidates: &[&'a str],- fallback: &'a str,-) -> &'a str {- for candidate in candidates {- if columns.iter().any(|c| c.eq_ignore_ascii_case(candidate)) {- return candidate;- }- }- fallback-}--fn pick_optional_column_expr<'a>(columns: &'a [String], candidates: &[&'a str]) -> Option<&'a str> {- candidates- .iter()- .find(|&candidate| columns.iter().any(|c| c.eq_ignore_ascii_case(candidate)))- .map(|v| v as _)-}--async fn next_available_key(memory: &dyn Memory, key: &str) -> Result<String> {- let mut idx = 1u32;- loop {- let candidate = format!("{key}_{idx}");- if memory.get("", &candidate).await?.is_none() {- return Ok(candidate);- }- idx += 1;- }-}--#[cfg(test)]-#[path = "core_tests.rs"]-mod tests;diff --git a/crates/openhuman-core/src/config/migration_helpers/core_tests.rs b/crates/openhuman-core/src/config/migration_helpers/core_tests.rsdeleted file mode 100644index a5be4061ba..0000000000--- a/crates/openhuman-core/src/config/migration_helpers/core_tests.rs+++ /dev/null@@ -1,36 +0,0 @@-use super::*;--#[test]-fn normalize_key_replaces_non_alnum() {- let key = normalize_key("hello/world", 0);- assert_eq!(key, "hello_world");-}--#[test]-fn parse_category_defaults_to_core() {- assert_eq!(- parse_category("unknown"),- MemoryCategory::Custom("unknown".to_string())- );-}--#[test]-fn resolve_hermes_workspace_returns_override_when_provided() {- let custom = PathBuf::from("/custom/hermes");- let result = resolve_hermes_workspace(Some(custom.clone())).unwrap();- assert_eq!(result, custom);-}--#[test]-fn resolve_hermes_workspace_defaults_to_home_dot_hermes() {- let result = resolve_hermes_workspace(None).unwrap();- #[cfg(windows)]- {- if let Some(local_app_data) = std::env::var_os("LOCALAPPDATA") {- assert_eq!(result, PathBuf::from(local_app_data).join("hermes"));- return;- }- }- let home = directories::UserDirs::new().unwrap();- assert_eq!(result, home.home_dir().join(".hermes"));-}diff --git a/vendor/tinymemory b/vendor/tinymemoryindex 3ba711d4c2..2a6a2c6b13 160000--- a/vendor/tinymemory+++ b/vendor/tinymemory@@ -1 +1 @@-Subproject commit 3ba711d4c2650d2b5660e31af5971f8f87236489+Subproject commit 2a6a2c6b13a5b23051bd7475a860dfa60df4c6cc