Skip to content

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

refactor(storage): route subagent and thread stores through shared storage module

Steven Enamakela6726984224c
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store.rs
index d83e4ec835..1c628aa36e 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store.rs
@@ -20,7 +20,7 @@ impl SubagentSessionStore {
/// so it stays byte-identical. A missing file is the common case and a
/// no-op.
fn import_legacy(&self, docs: &super::store_documents::Docs) -> Result<(), String> {
- if docs.legacy_imported().map_err(|err| format!("{err:#}"))? {
+ if docs.legacy_imported(&self.workspace_dir).map_err(|err| format!("{err:#}"))? {
return Ok(());
}
let path = self.legacy_path();
@@ -31,7 +31,7 @@ impl SubagentSessionStore {
};
let sessions: Vec<DurableSubagentSession> = serde_json::from_str(&raw)
.map_err(|err| format!("failed to parse legacy subagent sessions: {err}"))?;
- docs.import_legacy(&sessions)
+ docs.import_legacy(&self.workspace_dir, &sessions)
.map_err(|err| format!("{err:#}"))?;
log::debug!(
"[subagent_sessions] imported legacy sessions count={}",
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
index 5a0058a295..e17a48f96b 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
@@ -10,7 +10,7 @@
//! | Collection | Document id | Holds |
//! | --- | --- | --- |
//! | `subagent_sessions` | the session id | one [`DurableSubagentSession`] plus `created_at` for ordering |
-//! | `subagent_sessions_import` | `subagent_sessions.json` | marker: the legacy file was imported (it is left byte-identical) |
+//! | `subagent_sessions_import` | `subagent_sessions.json:<workspace key>` | marker: the legacy file was imported (it is left byte-identical) |
//!
//! The callers in `ops.rs` load and save the whole list. `save` writes each session as
//! its own document, so a session's history stays well under any document-size
@@ -32,6 +32,12 @@ const SESSIONS: &str = "subagent_sessions";
/// a collection of its own so `load` never sees it as a session.
const IMPORTED: &str = "subagent_sessions_import";
const LEGACY_MARKER: &str = "subagent_sessions.json";
+
+/// The marker id for one workspace's legacy file, so workspaces sharing a
+/// backend each import their own.
+fn marker_id(source: &std::path::Path) -> String {
+ format!("{LEGACY_MARKER}:{}", crate::storage::source_key(source))
+}
const DOMAIN: &str = "subagent_sessions::store";
fn collections() -> Vec<CollectionSpec> {
@@ -90,22 +96,28 @@ impl Docs {
}
/// Whether the one-time legacy file import finished in this scope.
- pub(super) fn legacy_imported(&self) -> Result<bool> {
+ pub(super) fn legacy_imported(&self, source: &std::path::Path) -> Result<bool> {
+ let id = marker_id(source);
let found = self
.0
- .run(|docs| async move { docs.get(IMPORTED, LEGACY_MARKER).await })?;
+ .run(|docs| async move { docs.get(IMPORTED, &id).await })?;
Ok(found.is_some())
}
/// Imports `sessions` read from the legacy file (a stored newer copy
/// wins), then records the import in a marker document.
- pub(super) fn import_legacy(&self, sessions: &[DurableSubagentSession]) -> Result<()> {
+ pub(super) fn import_legacy(
+ &self,
+ source: &std::path::Path,
+ sessions: &[DurableSubagentSession],
+ ) -> Result<()> {
self.save(sessions)?;
+ let id = marker_id(source);
self.0.run(|docs| async move {
match docs
.put(
IMPORTED,
- LEGACY_MARKER,
+ &id,
serde_json::json!({ "imported": true }),
Precondition::Absent,
)
diff --git a/crates/openhuman-core/src/storage/mod.rs b/crates/openhuman-core/src/storage/mod.rs
index 6de7ad9b53..ce18622110 100644
--- a/crates/openhuman-core/src/storage/mod.rs
+++ b/crates/openhuman-core/src/storage/mod.rs
@@ -339,6 +339,16 @@ pub fn scope_for_profile(profile_id: &str) -> Scope {
.unwrap_or_else(|_| unreachable!("a sha256 hex scope is always valid"))
}
+/// A stable key for a legacy import source (a workspace directory), for the
+/// marker document and once-per-process key of an importer. Two workspaces
+/// that share one explicit backend and scope must each import their own legacy
+/// files, so a marker names its source.
+pub fn source_key(source: &std::path::Path) -> String {
+ use sha2::Digest;
+ let digest = sha2::Sha256::digest(source.to_string_lossy().as_bytes());
+ hex::encode(&digest[..8])
+}
+
#[cfg(test)]
#[path = "mod_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/threads/store/blocking.rs b/crates/openhuman-core/src/threads/store/blocking.rs
index f1a798694c..673f6674ce 100644
--- a/crates/openhuman-core/src/threads/store/blocking.rs
+++ b/crates/openhuman-core/src/threads/store/blocking.rs
@@ -69,7 +69,7 @@ where
F: FnOnce() -> Result<T, String> + Send + 'static,
T: Send + 'static,
{
- match tokio::task::spawn_blocking(f).await {
+ match crate::core::runtime::spawn_blocking_scoped(f).await {
Ok(result) => result,
Err(error) => {
tracing::warn!(
diff --git a/crates/openhuman-core/src/threads/store/legacy_import.rs b/crates/openhuman-core/src/threads/store/legacy_import.rs
index 7f94028325..f6181efd91 100644
--- a/crates/openhuman-core/src/threads/store/legacy_import.rs
+++ b/crates/openhuman-core/src/threads/store/legacy_import.rs
@@ -22,7 +22,7 @@ use tinystoragedrivers::{CollectionSpec, ErrorKind, Precondition};
use crate::storage::documents::Repo;
const MARKERS: &str = "threads_import";
-const MARKER_ID: &str = "legacy-conversations";
+const MARKER_PREFIX: &str = "legacy-conversations";
const LOG_PREFIX: &str = "[conversations:import]";
/// The collection that holds the completion marker.
@@ -147,11 +147,21 @@ pub(super) fn import_once(
store: &DriverConversationStore,
) -> Result<()> {
let root = super::backend::legacy_root(workspace_dir);
- repo.once_per_backend("legacy-import", || {
- if !root.join("threads.jsonl").is_file() {
- return Ok(());
+ let source = crate::storage::source_key(&root);
+ let marker_id = format!("{MARKER_PREFIX}:{source}");
+ repo.once_per_backend(&format!("legacy-import:{source}"), || {
+ match std::fs::metadata(root.join("threads.jsonl")) {
+ Ok(meta) if meta.is_file() => {}
+ Ok(_) => return Ok(()),
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
+ Err(error) => {
+ return Err(anyhow::Error::new(error).context("stat the legacy threads log"))
+ }
}
- let marked = repo.run(|docs| async move { docs.get(MARKERS, MARKER_ID).await })?;
+ let marked = repo.run(|docs| {
+ let marker_id = marker_id.clone();
+ async move { docs.get(MARKERS, &marker_id).await }
+ })?;
if marked.is_some() {
log::debug!("{LOG_PREFIX} already imported");
return Ok(());
@@ -170,7 +180,7 @@ pub(super) fn import_once(
match docs
.put(
MARKERS,
- MARKER_ID,
+ &marker_id,
serde_json::json!({ "threads": threads_n, "messages": messages_n }),
Precondition::Absent,
)