Skip to content

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

refactor(core): improve delivery import logic and error handling

Steven Enamakele416a37a8c35
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 f035b75f25..c25a03774b 100644
--- a/crates/openhuman-core/src/channels/runtime/startup/delivery_import.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup/delivery_import.rs
@@ -18,14 +18,14 @@
use std::collections::BTreeMap;
use std::path::Path;
-use serde_json::{json, Value};
+use crate::storage::{Scope, documents::Repo};
+use serde_json::{Value, json};
use sha2::Digest;
use tinychannels_bus::module_delivery::{
- DeliveryQueueEntry, DeliveryTerminalResult, DELIVERY_PENDING_CAPACITY, DELIVERY_RECORD_BYTES,
- DELIVERY_RESULT_CAPACITY, DELIVERY_RESULT_TTL_SECONDS,
+ DELIVERY_PENDING_CAPACITY, DELIVERY_RECORD_BYTES, DELIVERY_RESULT_CAPACITY,
+ DELIVERY_RESULT_TTL_SECONDS, DeliveryQueueEntry, DeliveryTerminalResult,
};
use tinystoragedrivers::{CollectionSpec, ErrorKind, Precondition, StorageBackend};
-use crate::storage::{Scope, documents::Repo};
const LOG_PREFIX: &str = "[channel-delivery-import]";
const MODULE_ID: &str = "tinychannels";
@@ -111,9 +111,9 @@ async fn import_into(
std::sync::Arc::clone(backend),
condition.clone(),
);
- let scoped = claimed_backend
- .for_scope(scope)
- .map_err(|error| anyhow::anyhow!("open claimed module scope: {:?}", error.kind()))?;
+ let scoped = claimed_backend.for_scope(scope).map_err(|error| {
+ anyhow::anyhow!("open claimed module scope: {:?}", error.kind())
+ })?;
crate::storage::block_on_anyhow(async move {
if crate::storage::import_state::host_import_done(
claimed_markers.scoped(),
@@ -152,7 +152,10 @@ async fn import_payload(
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(Outcome::NoSource),
Err(error) => return Err(error.into()),
};
- anyhow::ensure!(bytes.len() as u64 <= MAX_LEGACY_BYTES, "legacy delivery file is oversized");
+ anyhow::ensure!(
+ bytes.len() as u64 <= MAX_LEGACY_BYTES,
+ "legacy delivery file is oversized"
+ );
let legacy: LegacyState = serde_json::from_slice(&bytes)
.map_err(|error| anyhow::anyhow!("legacy delivery file is unreadable: {error}"))?;
let docs = scoped.documents();
@@ -193,7 +196,12 @@ async fn import_payload(
Ok(_) => written += 1,
Err(error)
if matches!(error.kind(), ErrorKind::Conflict | ErrorKind::AlreadyExists) => {}
- Err(error) => return Err(anyhow::anyhow!("write {collection} record: {:?}", error.kind())),
+ Err(error) => {
+ return Err(anyhow::anyhow!(
+ "write {collection} record: {:?}",
+ error.kind()
+ ));
+ }
}
}
tracing::info!("{LOG_PREFIX} imported found={found} written={written}");
diff --git a/crates/openhuman-core/src/channels/runtime/startup/delivery_import_tests.rs b/crates/openhuman-core/src/channels/runtime/startup/delivery_import_tests.rs
index ad8439eb49..96c6e0e12b 100644
--- a/crates/openhuman-core/src/channels/runtime/startup/delivery_import_tests.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup/delivery_import_tests.rs
@@ -1,6 +1,6 @@
use super::*;
-use tinychannels_bus::{ChannelOutboundIntent, DeliveryDurability, OutboundPayload};
use std::sync::Arc;
+use tinychannels_bus::{ChannelOutboundIntent, DeliveryDurability, OutboundPayload};
use tinystoragedrivers::{Scope, StorageBackend};
fn entry(id: &str, at: u64) -> DeliveryQueueEntry {
@@ -148,10 +148,7 @@ async fn an_existing_module_record_is_never_overwritten() {
async fn a_missing_file_imports_nothing_and_writes_no_marker() {
let dir = tempfile::tempdir().unwrap();
let rig = rig();
- assert_eq!(
- import(&rig, dir.path()).await.unwrap(),
- Outcome::NoSource
- );
+ assert_eq!(import(&rig, dir.path()).await.unwrap(), Outcome::NoSource);
write_legacy(dir.path(), &legacy_state());
assert!(matches!(
import(&rig, dir.path()).await.unwrap(),
diff --git a/crates/openhuman-core/src/modules/storage_import_tinymcp.rs b/crates/openhuman-core/src/modules/storage_import_tinymcp.rs
index aa4e8af210..b0538473c4 100644
--- a/crates/openhuman-core/src/modules/storage_import_tinymcp.rs
+++ b/crates/openhuman-core/src/modules/storage_import_tinymcp.rs
@@ -29,7 +29,9 @@
use std::path::{Path, PathBuf};
use std::sync::Arc;
-use crate::storage::{CollectionSpec, DocumentStore, ErrorKind, Precondition, Storage, StorageBackend};
+use crate::storage::{
+ CollectionSpec, DocumentStore, ErrorKind, Precondition, Storage, StorageBackend,
+};
use anyhow::{Context, Result, anyhow};
use serde_json::{Value, json};
use sha2::Digest;