Skip to content

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

feat(memory): persist thread state in the state store

Steven Enamakel24dfb23add94
diff --git a/crates/openhuman-core/src/memory/state_store.rs b/crates/openhuman-core/src/memory/state_store.rs
index d3d19faa56..87a24a8aec 100644
--- a/crates/openhuman-core/src/memory/state_store.rs
+++ b/crates/openhuman-core/src/memory/state_store.rs
@@ -38,6 +38,44 @@ fn open_strict(workspace_dir: &Path, name: &str) -> Result<StateKv> {
Ok(kv)
}
+/// Imports every `<workspace>/memory/*.json` state file into the `local` scope
+/// of an explicit `scoped` handle, once each. For the layout migration
+/// (`storage::layout`), which deletes those files afterwards; a file that is
+/// not JSON is an error here (nothing may be deleted unimported), so it is
+/// imported strictly. Returns the names imported.
+///
+/// # Errors
+///
+/// A storage error or an unparsable file.
+pub(crate) fn import_all(
+ scoped: &crate::storage::ScopedStorage,
+ workspace_dir: &Path,
+) -> Result<Vec<String>> {
+ let kv = StateKv::over(scoped, OWNER, NS);
+ let mut names = Vec::new();
+ let Ok(entries) = std::fs::read_dir(workspace_dir.join("memory")) else {
+ return Ok(names);
+ };
+ for entry in entries.flatten() {
+ let path = entry.path();
+ let Some(name) = path
+ .file_name()
+ .and_then(|f| f.to_str())
+ .and_then(|f| f.strip_suffix(".json"))
+ .map(str::to_owned)
+ else {
+ continue;
+ };
+ if !path.is_file() {
+ continue;
+ }
+ kv.import_file_strict(&name, &path)?;
+ names.push(name);
+ }
+ names.sort();
+ Ok(names)
+}
+
/// [`load`], failing on a damaged legacy file instead of starting over.
///
/// # Errors
diff --git a/crates/openhuman-core/src/threads/store/backend.rs b/crates/openhuman-core/src/threads/store/backend.rs
index 065f6d3eba..9ca5dfabda 100644
--- a/crates/openhuman-core/src/threads/store/backend.rs
+++ b/crates/openhuman-core/src/threads/store/backend.rs
@@ -91,6 +91,36 @@ fn describe(error: &ConversationError, subject: Subject<'_>) -> String {
error.to_string()
}
+/// Imports the legacy JSONL conversations under `workspace_dir` into the
+/// `local` conversation scope of an explicit `backend`, once (the same
+/// `tsd.migrations` entry [`ConversationStore`] checks). For the layout
+/// migration (`storage::layout`), which runs before any store opens the new
+/// database; the source files are only read.
+///
+/// # Errors
+///
+/// A storage failure or an unreadable legacy file; the next call retries.
+pub(crate) fn import_legacy_into(
+ backend: &Arc<dyn storage::StorageBackend>,
+ workspace_dir: &Path,
+) -> Result<(), String> {
+ let scope = Scope::local();
+ let scoped = backend
+ .for_scope(&scope)
+ .map_err(|e| format!("{LOG_PREFIX} open the storage scope: {e}"))?;
+ let repo = Repo::on(backend, &scope, DOMAIN, super::legacy_import::collections)
+ .map_err(|e| format!("{LOG_PREFIX} {e:#}"))?;
+ let store = DriverConversationStore::new(scoped.clone());
+ let docs = Arc::clone(scoped.documents());
+ storage::block_on(async move {
+ DriverConversationStore::prepare_schema(docs.as_ref()).await?;
+ DriverConversationStore::prepare_append_schema(docs.as_ref()).await
+ })
+ .map_err(|e| format!("{LOG_PREFIX} declare the conversation schema: {e}"))?;
+ super::legacy_import::import_once(workspace_dir, &repo, &store)
+ .map_err(|e| format!("{LOG_PREFIX} legacy import: {e:#}"))
+}
+
/// A conversation store bound to one workspace's storage backend.
#[derive(Clone)]
pub struct ConversationStore {