Skip to content

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

Size the legacy import with the importer's counts

M3gA-Mindc78e4464a896
diff --git a/crates/openhuman-core/src/memory/import.rs b/crates/openhuman-core/src/memory/import.rs
index 3d905e8231..825293598d 100644
--- a/crates/openhuman-core/src/memory/import.rs
+++ b/crates/openhuman-core/src/memory/import.rs
@@ -21,7 +21,6 @@ use std::path::{Path, PathBuf};
use std::sync::{Arc, LazyLock, Mutex};
use serde::{Deserialize, Serialize};
-use tinymemory_api::ItemKind;
use tinymemory_integrations::cortex::is_insufficient_credits;
use tinymemory_integrations::import::{Checkpoint, ImportedItem, LegacyWorkspace};
@@ -264,30 +263,28 @@ fn write_file(workspace_dir: &Path, file: &ImportFile) {
}
/// Counts what a legacy store at `workspace_dir` holds, or `None` when there
-/// is no v1 store there. Blocking (SQLite).
+/// is no v1 store there (neither `memory/memory.db` nor a usable
+/// `memory_tree/chunks.db`). Sized by the importer's own counts, one query
+/// per section rather than a pass over every item, and exactly what the
+/// import then yields. Blocking (SQLite, and memory-tree chunk files).
pub fn count_legacy(workspace_dir: &Path) -> Option<ImportCounts> {
- let workspace = match LegacyWorkspace::open(workspace_dir) {
- Ok(workspace) => workspace,
- Err(error) => {
- tracing::debug!(error = %error, "[memory:import] no legacy store");
- return None;
- }
- };
- let mut counts = ImportCounts::default();
- for imported in workspace.items() {
- match imported {
- Ok(ImportedItem { item, .. }) => match item.kind() {
- ItemKind::Document => counts.documents += 1,
- ItemKind::Conversation => counts.conversations += 1,
- ItemKind::Learning => counts.learnings += 1,
- },
- Err(error) => {
- tracing::warn!(error = %error, "[memory:import] legacy store unreadable mid-scan");
- break;
- }
- }
- }
- Some(counts)
+ let counts = LegacyWorkspace::open(workspace_dir)
+ .and_then(|workspace| workspace.counts())
+ .map_err(|error| tracing::debug!(error = %error, "[memory:import] no legacy store"))
+ .ok()?;
+ // Memory-tree chunk sources count as documents: most are, and a chat
+ // one is a conversation the import still stores. The three add up to
+ // the total either way.
+ Some(ImportCounts {
+ documents: counts.documents + counts.chunks,
+ conversations: counts.conversations,
+ learnings: counts.learnings
+ + counts.profile
+ + counts.events
+ + counts.lessons
+ + counts.relations
+ + counts.files,
+ })
}
/// `memory_import_scan`.
diff --git a/crates/openhuman-core/src/memory/import_tests.rs b/crates/openhuman-core/src/memory/import_tests.rs
index cf8c82efe4..2892f31182 100644
--- a/crates/openhuman-core/src/memory/import_tests.rs
+++ b/crates/openhuman-core/src/memory/import_tests.rs
@@ -99,6 +99,51 @@ async fn scan_counts_what_a_legacy_store_holds() {
assert_eq!(counts.learnings, 2, "a learning doc and a profile facet");
}
+/// A store from the later v1 engine: only `memory_tree/chunks.db`, with an
+/// email source of two chunks and a chat source of one.
+fn chunk_only_workspace(workspace_dir: &Path) {
+ std::fs::create_dir_all(workspace_dir.join("memory_tree")).unwrap();
+ let conn = Connection::open(workspace_dir.join("memory_tree").join("chunks.db")).unwrap();
+ conn.execute_batch(
+ "CREATE TABLE mem_tree_chunks (id TEXT PRIMARY KEY, source_kind TEXT NOT NULL,
+ source_id TEXT NOT NULL, path_scope TEXT, source_ref TEXT, owner TEXT NOT NULL,
+ timestamp_ms INTEGER NOT NULL, time_range_start_ms INTEGER NOT NULL,
+ time_range_end_ms INTEGER NOT NULL, tags_json TEXT NOT NULL DEFAULT '[]',
+ content TEXT NOT NULL, token_count INTEGER NOT NULL, seq_in_source INTEGER NOT NULL,
+ created_at_ms INTEGER NOT NULL);
+ INSERT INTO mem_tree_chunks VALUES
+ ('k1', 'email', 'e1', NULL, NULL, 'me', 1, 1, 1, '[]', 'launch moved', 3, 0, 1),
+ ('k2', 'email', 'e1', NULL, NULL, 'me', 2, 2, 2, '[]', 'to friday', 3, 1, 2),
+ ('k3', 'chat', 'c1', NULL, NULL, 'me', 3, 3, 3, '[]', 'hi there', 2, 0, 3);",
+ )
+ .unwrap();
+}
+
+#[tokio::test]
+async fn a_store_with_only_a_chunk_store_is_found_and_imported() {
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_in(&tmp);
+ chunk_only_workspace(&config.workspace_dir);
+
+ let view = scan(&config).await.unwrap();
+ assert!(view.found, "the later v1 engine wrote no memory.db");
+ let counts = view.counts.expect("counts");
+ assert_eq!(
+ (counts.documents, counts.conversations, counts.learnings),
+ (2, 0, 0),
+ "two chunk sources, counted as documents"
+ );
+
+ let engine = bind_reference(&config);
+ start(&config, true).await.unwrap();
+ let done = wait_until_settled(&config).await;
+ assert_eq!(
+ (done.phase, done.imported, done.total),
+ (ImportPhase::Done, 2, 2)
+ );
+ assert_eq!(stored(&engine, MetaFilter::default()).await.len(), 2);
+}
+
#[test]
fn status_of_an_untouched_workspace_is_idle() {
let tmp = tempfile::tempdir().unwrap();
diff --git a/crates/openhuman-core/src/memory/types.rs b/crates/openhuman-core/src/memory/types.rs
index 650bb62eeb..0d417ab5d5 100644
--- a/crates/openhuman-core/src/memory/types.rs
+++ b/crates/openhuman-core/src/memory/types.rs
@@ -315,11 +315,12 @@ pub struct SourcesSyncView {
/// Item counts a legacy store would import.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImportCounts {
- /// Documents (and chunked source documents).
+ /// Documents, and every memory-tree chunk source.
pub documents: u64,
/// Conversations.
pub conversations: u64,
- /// Learnings (including profile facets).
+ /// Learnings: distilled facts, profile facets, extracted events, turn
+ /// lessons, graph relations, and the goals and persona files.
pub learnings: u64,
}