Skip to content

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

fix(storage): guard legacy imports with a cross-process claim

Steven Enamakelfa58736b8ce7
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 e17a48f96b..73d0407c51 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
@@ -111,23 +111,29 @@ impl Docs {
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,
- &id,
- serde_json::json!({ "imported": true }),
- Precondition::Absent,
- )
- .await
- {
- Ok(_) => Ok(()),
- Err(error) if error.kind() == tinystoragedrivers::ErrorKind::Conflict => Ok(()),
- Err(error) => Err(error),
- }
- })
+ let claimed = crate::storage::import_claim::with_claim(&self.0, IMPORTED, &id.clone(), || {
+ self.save(sessions)?;
+ self.0.run(|docs| async move {
+ match docs
+ .put(
+ IMPORTED,
+ &id,
+ serde_json::json!({ "imported": true }),
+ Precondition::Absent,
+ )
+ .await
+ {
+ Ok(_) => Ok(()),
+ Err(error) if error.kind() == tinystoragedrivers::ErrorKind::Conflict => Ok(()),
+ Err(error) => Err(error),
+ }
+ })
+ })?;
+ if claimed.is_none() {
+ log::debug!("[subagent_sessions] another process is importing the legacy file; skipped");
+ }
+ Ok(())
}
pub(super) fn save(&self, sessions: &[DurableSubagentSession]) -> Result<()> {
diff --git a/crates/openhuman-core/src/integrations/composio/file_store_documents.rs b/crates/openhuman-core/src/integrations/composio/file_store_documents.rs
index d8564938d1..ed5c5b883b 100644
--- a/crates/openhuman-core/src/integrations/composio/file_store_documents.rs
+++ b/crates/openhuman-core/src/integrations/composio/file_store_documents.rs
@@ -141,28 +141,31 @@ impl Docs {
let id = name.to_string();
let doc = json!({ "value": value });
log::debug!("[composio:store] document import id={id}");
- self.0.run(|docs| async move {
- let wrote = match docs.put(STATE, &id, doc, Precondition::Absent).await {
- Ok(_) => true,
- Err(error) if error.kind() == ErrorKind::Conflict => false,
- Err(error) => return Err(error),
- };
- match docs
- .put(
- IMPORTED,
- &id,
- json!({ "imported": true }),
- Precondition::Absent,
- )
- .await
- {
- Ok(_) => {}
- // Another core finished first.
- Err(error) if error.kind() == ErrorKind::Conflict => {}
- Err(error) => return Err(error),
- }
- Ok(wrote)
- })
+ let claimed = crate::storage::import_claim::with_claim(&self.0, IMPORTED, &id.clone(), || {
+ self.0.run(|docs| async move {
+ let wrote = match docs.put(STATE, &id, doc, Precondition::Absent).await {
+ Ok(_) => true,
+ Err(error) if error.kind() == ErrorKind::Conflict => false,
+ Err(error) => return Err(error),
+ };
+ match docs
+ .put(
+ IMPORTED,
+ &id,
+ json!({ "imported": true }),
+ Precondition::Absent,
+ )
+ .await
+ {
+ Ok(_) => {}
+ // Another core finished first.
+ Err(error) if error.kind() == ErrorKind::Conflict => {}
+ Err(error) => return Err(error),
+ }
+ Ok(wrote)
+ })
+ })?;
+ Ok(claimed.unwrap_or(false))
}
#[cfg(test)]
diff --git a/crates/openhuman-core/src/threads/store/legacy_import.rs b/crates/openhuman-core/src/threads/store/legacy_import.rs
index f6181efd91..317f0619d6 100644
--- a/crates/openhuman-core/src/threads/store/legacy_import.rs
+++ b/crates/openhuman-core/src/threads/store/legacy_import.rs
@@ -166,32 +166,45 @@ pub(super) fn import_once(
log::debug!("{LOG_PREFIX} already imported");
return Ok(());
}
- let threads = fold_threads(&root)?;
- let mut messages = Vec::with_capacity(threads.len());
- for thread in &threads {
- messages.push(read_jsonl::<ThreadMessage>(&messages_path(
- &root, &thread.id,
- ))?);
- }
- let store = store.clone();
- let (threads_n, messages_n) =
- crate::storage::block_on_anyhow(async move { copy(&store, threads, messages).await })?;
- repo.run(|docs| async move {
- match docs
- .put(
- MARKERS,
- &marker_id,
- serde_json::json!({ "threads": threads_n, "messages": messages_n }),
- Precondition::Absent,
- )
- .await
- {
- Ok(_) => Ok(()),
- Err(error) if error.kind() == ErrorKind::Conflict => Ok(()),
- Err(error) => Err(error),
+ let claimed = crate::storage::import_claim::with_claim(repo, MARKERS, &marker_id.clone(), || {
+ let marked = repo.run(|docs| {
+ let marker_id = marker_id.clone();
+ async move { docs.get(MARKERS, &marker_id).await }
+ })?;
+ if marked.is_some() {
+ return Ok(());
+ }
+ let threads = fold_threads(&root)?;
+ let mut messages = Vec::with_capacity(threads.len());
+ for thread in &threads {
+ messages.push(read_jsonl::<ThreadMessage>(&messages_path(
+ &root, &thread.id,
+ ))?);
}
+ let store = store.clone();
+ let (threads_n, messages_n) =
+ crate::storage::block_on_anyhow(async move { copy(&store, threads, messages).await })?;
+ repo.run(|docs| async move {
+ match docs
+ .put(
+ MARKERS,
+ &marker_id,
+ serde_json::json!({ "threads": threads_n, "messages": messages_n }),
+ Precondition::Absent,
+ )
+ .await
+ {
+ Ok(_) => Ok(()),
+ Err(error) if error.kind() == ErrorKind::Conflict => Ok(()),
+ Err(error) => Err(error),
+ }
+ })?;
+ log::info!("{LOG_PREFIX} imported threads={threads_n} messages={messages_n}");
+ Ok(())
})?;
- log::info!("{LOG_PREFIX} imported threads={threads_n} messages={messages_n}");
+ if claimed.is_none() {
+ log::debug!("{LOG_PREFIX} another process is importing; skipped");
+ }
Ok(())
})
}