Skip to content

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

refactor(storage): centralize host import markers in import_state

Steven Enamakelfe4cd5dfc416
diff --git a/crates/openhuman-core/src/channels/runtime/startup/delivery_import.rs b/crates/openhuman-core/src/channels/runtime/startup/delivery_import.rs
index a5af7e1111..10da286268 100644
--- a/crates/openhuman-core/src/channels/runtime/startup/delivery_import.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup/delivery_import.rs
@@ -76,20 +76,19 @@ pub(super) async fn import_legacy_delivery(
let scoped = backend
.for_scope(&scope)
.map_err(|error| anyhow::anyhow!("open the module scope: {:?}", error.kind()))?;
- import_into(scoped.documents(), workspace).await
+ import_into(&scoped, workspace).await
}
-async fn import_into(docs: &Arc<dyn DocumentStore>, workspace: &Path) -> anyhow::Result<Outcome> {
+async fn import_into(
+ scoped: &tinystoragedrivers::ScopedStorage,
+ workspace: &Path,
+) -> anyhow::Result<Outcome> {
+ let docs = scoped.documents();
let path = workspace.join(LEGACY_FILE);
let marker = marker_id(workspace);
- docs.ensure_collection(&CollectionSpec::new(MARKER_COLLECTION))
- .await
- .map_err(|error| anyhow::anyhow!("declare the marker collection: {:?}", error.kind()))?;
- if docs
- .get(MARKER_COLLECTION, &marker)
+ if crate::storage::import_state::host_import_done(scoped, MARKER_COLLECTION, &marker)
.await
- .map_err(|error| anyhow::anyhow!("read the marker: {:?}", error.kind()))?
- .is_some()
+ .map_err(|error| anyhow::anyhow!("read the import state: {:?}", error.kind()))?
{
return Ok(Outcome::AlreadyDone);
}
@@ -152,15 +151,9 @@ async fn import_into(docs: &Arc<dyn DocumentStore>, workspace: &Path) -> anyhow:
}
}
}
- let body = json!({ "found": found, "written": written, "at": now });
- match docs
- .put(MARKER_COLLECTION, &marker, body, Precondition::Absent)
+ crate::storage::import_state::host_import_mark(scoped, MARKER_COLLECTION, &marker)
.await
- {
- Ok(_) => {}
- Err(error) if matches!(error.kind(), ErrorKind::Conflict | ErrorKind::AlreadyExists) => {}
- Err(error) => return Err(anyhow::anyhow!("write the marker: {:?}", error.kind())),
- }
+ .map_err(|error| anyhow::anyhow!("write the import state: {:?}", error.kind()))?;
tracing::info!("{LOG_PREFIX} imported found={found} written={written}");
Ok(Outcome::Imported { found, written })
}
diff --git a/crates/openhuman-core/src/modules/storage_import_support.rs b/crates/openhuman-core/src/modules/storage_import_support.rs
index 2702a885e4..452aceee96 100644
--- a/crates/openhuman-core/src/modules/storage_import_support.rs
+++ b/crates/openhuman-core/src/modules/storage_import_support.rs
@@ -37,9 +37,6 @@ pub(super) fn marker_id(prefix: &str, source: &Path) -> String {
format!("{prefix}@{}", &hex::encode(digest)[..16])
}
-/// The `tsd.migrations` owner of the host's module importers.
-const IMPORT_OWNER: &str = "host";
-
/// Whether the importer `kind` already finished for marker `id`, recorded in
/// `tsd.migrations` of the module's scope. `kind` is the old marker
/// collection name (`tinybox_host_import`), so a marker row an older build
@@ -50,24 +47,16 @@ pub(super) async fn already_imported(
kind: &str,
id: &str,
) -> Result<bool> {
- crate::storage::import_state::done(
- scoped,
- IMPORT_OWNER,
- &crate::storage::import_state::step_id(kind, id),
- )
- .await
- .map_err(|error| anyhow!("read the import state: {:?}", error.kind()))
+ crate::storage::import_state::host_import_done(scoped, kind, id)
+ .await
+ .map_err(|error| anyhow!("read the import state: {:?}", error.kind()))
}
/// Records that the import of `id` finished. Marking twice is harmless.
pub(super) async fn mark_imported(scoped: &ScopedStorage, kind: &str, id: &str) -> Result<()> {
- crate::storage::import_state::mark_done(
- scoped,
- IMPORT_OWNER,
- &crate::storage::import_state::step_id(kind, id),
- )
- .await
- .map_err(|error| anyhow!("write the import state: {:?}", error.kind()))
+ crate::storage::import_state::host_import_mark(scoped, kind, id)
+ .await
+ .map_err(|error| anyhow!("write the import state: {:?}", error.kind()))
}
/// Writes `doc` only when `id` is absent; `true` when it was written.
diff --git a/crates/openhuman-core/src/modules/storage_import_tinymcp.rs b/crates/openhuman-core/src/modules/storage_import_tinymcp.rs
index af42d4a007..2459dcbfe0 100644
--- a/crates/openhuman-core/src/modules/storage_import_tinymcp.rs
+++ b/crates/openhuman-core/src/modules/storage_import_tinymcp.rs
@@ -93,7 +93,9 @@ async fn import_into(
) -> Result<Outcome> {
let docs = scoped.documents();
let marker = marker_id(data_dir);
- if support::already_imported(scoped, MARKER_COLLECTION, &marker).await? {
+ if crate::storage::import_state::host_import_done(scoped, MARKER_COLLECTION, &marker)
+ .await
+ .map_err(|error| anyhow!("read the import state: {:?}", error.kind()))? {
return Ok(Outcome::AlreadyDone);
}
let clients = data_dir.join(CLIENTS_DB);
@@ -166,7 +168,9 @@ async fn import_into(
}
}
- support::mark_imported(scoped, MARKER_COLLECTION, &marker).await?;
+ crate::storage::import_state::host_import_mark(scoped, MARKER_COLLECTION, &marker)
+ .await
+ .map_err(|error| anyhow!("write the import state: {:?}", error.kind()))?;
tracing::info!(
"{LOG_PREFIX} imported servers={servers} secrets={secret_count} audit={audited}"
);
diff --git a/crates/openhuman-core/src/storage/import_state.rs b/crates/openhuman-core/src/storage/import_state.rs
index d49f2b1b84..a7f4c59bbe 100644
--- a/crates/openhuman-core/src/storage/import_state.rs
+++ b/crates/openhuman-core/src/storage/import_state.rs
@@ -95,6 +95,37 @@ pub async fn mark_done(scoped: &ScopedStorage, owner: &str, step: &str) -> Resul
Migrations::new(scoped).mark_done(owner, step).await
}
+/// The `tsd.migrations` owner of the module importers, which run in a module's
+/// scope on behalf of the host (`kind` is the old marker collection name).
+pub const HOST_OWNER: &str = "host";
+
+/// Whether the module importer `kind` already finished for marker `id` in
+/// `scoped`'s (module) scope.
+///
+/// # Errors
+///
+/// A storage error.
+pub async fn host_import_done(
+ scoped: &ScopedStorage,
+ kind: &str,
+ id: &str,
+) -> Result<bool, StorageError> {
+ done(scoped, HOST_OWNER, &step_id(kind, id)).await
+}
+
+/// Records the module importer `kind` as finished for marker `id`.
+///
+/// # Errors
+///
+/// A storage error.
+pub async fn host_import_mark(
+ scoped: &ScopedStorage,
+ kind: &str,
+ id: &str,
+) -> Result<(), StorageError> {
+ mark_done(scoped, HOST_OWNER, &step_id(kind, id)).await
+}
+
/// [`done`] from synchronous code, over the scope `repo` was opened on.
///
/// # Errors