Skip to content

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

feat(storage): add import support for tinybox and tiny connector modules

Steven Enamakel2326fc27bfcb
diff --git a/crates/openhuman-core/src/modules/storage_import_support.rs b/crates/openhuman-core/src/modules/storage_import_support.rs
index 5475864755..2702a885e4 100644
--- a/crates/openhuman-core/src/modules/storage_import_support.rs
+++ b/crates/openhuman-core/src/modules/storage_import_support.rs
@@ -10,7 +10,7 @@ use std::path::Path;
use std::sync::Arc;
use anyhow::{anyhow, Result};
-use serde_json::{json, Value};
+use serde_json::Value;
use sha2::Digest;
use tinystoragedrivers::{CollectionSpec, DocumentStore, ErrorKind, Precondition, ScopedStorage};
@@ -37,42 +37,37 @@ pub(super) fn marker_id(prefix: &str, source: &Path) -> String {
format!("{prefix}@{}", &hex::encode(digest)[..16])
}
-/// Declares `collection` (host-only: not in any module's grant) and reports
-/// whether it already holds the marker `id`.
+/// 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
+/// wrote ([`crate::storage::import_state::migrate_legacy_markers`]) answers
+/// the same question.
pub(super) async fn already_imported(
- docs: &Arc<dyn DocumentStore>,
- collection: &str,
+ scoped: &ScopedStorage,
+ kind: &str,
id: &str,
) -> Result<bool> {
- docs.ensure_collection(&CollectionSpec::new(collection))
- .await
- .map_err(|error| anyhow!("declare the marker collection: {:?}", error.kind()))?;
- Ok(docs
- .get(collection, id)
- .await
- .map_err(|error| anyhow!("read the marker: {:?}", error.kind()))?
- .is_some())
+ 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()))
}
-/// Records that the import of `id` finished. A marker another attempt wrote
-/// first is left alone.
-pub(super) async fn mark_imported(
- docs: &Arc<dyn DocumentStore>,
- collection: &str,
- id: &str,
- mut summary: Value,
-) -> Result<()> {
- summary["at"] = json!(now_ms());
- match docs
- .put(collection, id, summary, Precondition::Absent)
- .await
- {
- Ok(_) => Ok(()),
- Err(error) if matches!(error.kind(), ErrorKind::Conflict | ErrorKind::AlreadyExists) => {
- Ok(())
- }
- Err(error) => Err(anyhow!("write the marker: {:?}", 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()))
}
/// Writes `doc` only when `id` is absent; `true` when it was written.
diff --git a/crates/openhuman-core/src/modules/storage_import_tinybox.rs b/crates/openhuman-core/src/modules/storage_import_tinybox.rs
index 966b80beda..202cd691a3 100644
--- a/crates/openhuman-core/src/modules/storage_import_tinybox.rs
+++ b/crates/openhuman-core/src/modules/storage_import_tinybox.rs
@@ -62,7 +62,7 @@ pub(crate) async fn import_legacy(ctx: &DispatchCtx, workspace_dir: &Path) -> Re
async fn import_into(scoped: &ScopedStorage, base: &Path) -> Result<Outcome> {
let docs = scoped.documents();
let marker = support::marker_id("tinybox", base);
- if support::already_imported(docs, MARKER_COLLECTION, &marker).await? {
+ if support::already_imported(scoped, MARKER_COLLECTION, &marker).await? {
return Ok(Outcome::AlreadyDone);
}
let index = base.join(INDEX_FILE);
@@ -91,12 +91,7 @@ async fn import_into(scoped: &ScopedStorage, base: &Path) -> Result<Outcome> {
_ => tracing::warn!("{LOG_PREFIX} skipped an index file that is not a JSON object"),
}
- support::mark_imported(
- docs,
- MARKER_COLLECTION,
- &marker,
- json!({ "registry": registry, "written": written }),
- )
+ support::mark_imported(scoped, MARKER_COLLECTION, &marker)
.await?;
tracing::info!("{LOG_PREFIX} imported written={written}");
Ok(Outcome::Imported { written })
diff --git a/crates/openhuman-core/src/modules/storage_import_tinyconnectors.rs b/crates/openhuman-core/src/modules/storage_import_tinyconnectors.rs
index 2f850e5340..f131d49bca 100644
--- a/crates/openhuman-core/src/modules/storage_import_tinyconnectors.rs
+++ b/crates/openhuman-core/src/modules/storage_import_tinyconnectors.rs
@@ -89,7 +89,7 @@ async fn import_into(
) -> Result<Outcome> {
let docs = scoped.documents();
let marker = support::marker_id("connectors", state_dir);
- if support::already_imported(docs, MARKER_COLLECTION, &marker).await? {
+ if support::already_imported(scoped, MARKER_COLLECTION, &marker).await? {
return Ok(Outcome::AlreadyDone);
}
let prefs_dir = state_dir.join(PREFS_DIR);
@@ -118,12 +118,7 @@ async fn import_into(
triggers += support::append_if_empty(scoped, &day.stream, day.records).await?;
}
- support::mark_imported(
- docs,
- MARKER_COLLECTION,
- &marker,
- json!({ "prefs": prefs, "triggers": triggers, "skipped": legacy.skipped }),
- )
+ support::mark_imported(scoped, MARKER_COLLECTION, &marker)
.await?;
tracing::info!(
"{LOG_PREFIX} imported prefs={prefs} triggers={triggers} skipped={}",
diff --git a/crates/openhuman-core/src/modules/storage_import_tinyjuice.rs b/crates/openhuman-core/src/modules/storage_import_tinyjuice.rs
index b99f4ee5e4..3eb51f547c 100644
--- a/crates/openhuman-core/src/modules/storage_import_tinyjuice.rs
+++ b/crates/openhuman-core/src/modules/storage_import_tinyjuice.rs
@@ -86,7 +86,7 @@ async fn import_into(
) -> Result<Outcome> {
let docs = scoped.documents();
let marker = support::marker_id("ccr", root);
- if support::already_imported(docs, MARKER_COLLECTION, &marker).await? {
+ if support::already_imported(scoped, MARKER_COLLECTION, &marker).await? {
return Ok(Outcome::AlreadyDone);
}
if !root.is_dir() {
@@ -138,12 +138,7 @@ async fn import_into(
imported += 1;
}
- support::mark_imported(
- docs,
- MARKER_COLLECTION,
- &marker,
- json!({ "imported": imported, "expired": expired, "skipped": skipped }),
- )
+ support::mark_imported(scoped, MARKER_COLLECTION, &marker)
.await?;
tracing::info!("{LOG_PREFIX} imported={imported} expired={expired} skipped={skipped}");
Ok(Outcome::Imported {
diff --git a/crates/openhuman-core/src/modules/storage_import_tinywallet.rs b/crates/openhuman-core/src/modules/storage_import_tinywallet.rs
index bd7548c40b..15b2be0d11 100644
--- a/crates/openhuman-core/src/modules/storage_import_tinywallet.rs
+++ b/crates/openhuman-core/src/modules/storage_import_tinywallet.rs
@@ -64,7 +64,7 @@ async fn import_into(
) -> Result<Outcome> {
let docs = scoped.documents();
let marker = support::marker_id("x402", workspace_dir);
- if support::already_imported(docs, MARKER_COLLECTION, &marker).await? {
+ if support::already_imported(scoped, MARKER_COLLECTION, &marker).await? {
return Ok(Outcome::AlreadyDone);
}
let ledger = workspace_dir.join(LEDGER_FILE);
@@ -83,12 +83,7 @@ async fn import_into(
let payments = support::append_if_empty(scoped, PAYMENTS_STREAM, records)
.await
.map_err(|error| anyhow!("import the payments: {error:#}"))?;
- support::mark_imported(
- docs,
- MARKER_COLLECTION,
- &marker,
- json!({ "payments": payments, "skipped": skipped }),
- )
+ support::mark_imported(scoped, MARKER_COLLECTION, &marker)
.await?;
tracing::info!("{LOG_PREFIX} imported payments={payments} skipped={skipped}");
Ok(Outcome::Imported { payments, skipped })