The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(storage): add support for conditional writes with options
Steven Enamakelae4cf0d7fa28
diff --git a/crates/openhuman-core/src/modules/storage_import_support.rs b/crates/openhuman-core/src/modules/storage_import_support.rsindex 1e6e20aa3a..9749b0926f 100644--- a/crates/openhuman-core/src/modules/storage_import_support.rs+++ b/crates/openhuman-core/src/modules/storage_import_support.rs@@ -13,6 +13,7 @@ use crate::storage::{DocumentStore, ErrorKind, Precondition, Storage};use anyhow::{Result, anyhow};use serde_json::Value;use sha2::Digest;+use tinystoragedrivers_types::WriteOptions;/// The module's scope of the installed backend, or `None` when the process/// runs without a storage backend (the module is then not using this storage).@@ -70,6 +71,27 @@ pub(super) async fn put_absent(}}+/// Writes `doc` with owner metadata only when `id` is absent; `true` when it+/// was written.+pub(super) async fn put_absent_with(+ docs: &Arc<dyn DocumentStore>,+ collection: &str,+ id: &str,+ doc: Value,+ options: WriteOptions,+) -> Result<bool> {+ match docs+ .put_with(collection, id, doc, Precondition::Absent, options)+ .await+ {+ Ok(_) => Ok(true),+ Err(error) if matches!(error.kind(), ErrorKind::Conflict | ErrorKind::AlreadyExists) => {+ Ok(false)+ }+ Err(error) => Err(anyhow!("write {collection}: {:?}", error.kind())),+ }+}+/// Wall clock in milliseconds since the Unix epoch.pub(super) fn now_ms() -> u64 {std::time::SystemTime::now()diff --git a/crates/openhuman-core/src/modules/storage_import_tinybox.rs b/crates/openhuman-core/src/modules/storage_import_tinybox.rsindex 83aafe33d5..5a843dd3bf 100644--- a/crates/openhuman-core/src/modules/storage_import_tinybox.rs+++ b/crates/openhuman-core/src/modules/storage_import_tinybox.rs@@ -24,8 +24,9 @@ use std::path::Path;use crate::storage::{CollectionSpec, Storage};use anyhow::{Context, Result};use serde_json::Value;+use tinystoragedrivers_types::WriteOptions;-use super::storage_grant_schemas::tinybox::{JAIL_INDEX_COLLECTION, MODULE_ID};+use super::storage_grant_schemas::tinybox::MODULE_ID;use super::storage_import_support as support;use crate::core::runtime::DispatchCtx;@@ -81,12 +82,22 @@ async fn import_into(scoped: &Storage, base: &Path) -> Result<Outcome> {let mut written = false;match serde_json::from_str::<Value>(&text) {Ok(doc) if doc.is_object() => {- docs.ensure_collection(&CollectionSpec::new(JAIL_INDEX_COLLECTION))+ let collection = tinybox_bus::storage::JAIL_INDEX_COLLECTION;+ docs.ensure_collection(&CollectionSpec::new(collection)).await.map_err(|error| {- anyhow::anyhow!("declare {JAIL_INDEX_COLLECTION}: {:?}", error.kind())+ anyhow::anyhow!("declare {collection}: {:?}", error.kind())})?;- written = support::put_absent(docs, JAIL_INDEX_COLLECTION, ®istry, doc).await?;+ let body = tinybox_bus::storage::jail_index::to_body(doc)+ .context("convert the jail index into the current owner body")?;+ written = support::put_absent_with(+ docs,+ collection,+ ®istry,+ body,+ WriteOptions::new().schema(tinybox_bus::storage::BODY_VERSION),+ )+ .await?;}_ => tracing::warn!("{LOG_PREFIX} skipped an index file that is not a JSON object"),}diff --git a/crates/openhuman-core/src/modules/storage_import_tinybox_tests.rs b/crates/openhuman-core/src/modules/storage_import_tinybox_tests.rsindex 5f741f7595..c18f92982d 100644--- a/crates/openhuman-core/src/modules/storage_import_tinybox_tests.rs+++ b/crates/openhuman-core/src/modules/storage_import_tinybox_tests.rs@@ -28,11 +28,22 @@ async fn the_jail_index_moves_into_one_document_with_the_same_shape() {assert_eq!(outcome, Outcome::Imported { written: true });let stored = scoped.documents()- .get(JAIL_INDEX_COLLECTION, "jails")+ .get(tinybox_bus::storage::JAIL_INDEX_COLLECTION, "jails").await.unwrap().unwrap();- assert_eq!(stored.doc, serde_json::from_str::<Value>(INDEX).unwrap());+ assert_eq!(+ stored.doc,+ json!({"jails": {"j1": {+ "id": "j1",+ "label": "build",+ "dir": "/w/jails/j1",+ "backend_at_create": "landlock",+ "created_ms": 10_000,+ "updated_ms": 20_000,+ "notes": "keep"+ }}})+ );}#[tokio::test]@@ -53,13 +64,15 @@ async fn an_index_the_module_already_wrote_is_kept_and_a_rerun_is_a_noop() {let scoped = scoped();scoped.documents()- .ensure_collection(&CollectionSpec::new(JAIL_INDEX_COLLECTION))+ .ensure_collection(&CollectionSpec::new(+ tinybox_bus::storage::JAIL_INDEX_COLLECTION,+ )).await.unwrap();scoped.documents().put(- JAIL_INDEX_COLLECTION,+ tinybox_bus::storage::JAIL_INDEX_COLLECTION,"jails",json!({"jails": {}}),Precondition::Absent,@@ -72,7 +85,7 @@ async fn an_index_the_module_already_wrote_is_kept_and_a_rerun_is_a_noop() {);let stored = scoped.documents()- .get(JAIL_INDEX_COLLECTION, "jails")+ .get(tinybox_bus::storage::JAIL_INDEX_COLLECTION, "jails").await.unwrap().unwrap();