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
refactor(storage): introduce update_or_insert for atomic document updates
Steven Enamakel9b4746333f31
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.rsindex 574aa0cec7..3c11ef1cfb 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@@ -232,23 +232,17 @@ impl Docs {{let id = key_of(subagent_session_id)?;let updated = self.0.run(|docs| async move {- let repo = tinystoragedrivers::Repo::<Value>::new(&docs, SESSIONS)?;- repo.update(&id, move |current| {- let Some(current) = current else {- return Ok(None);- };+ crate::storage::documents::compare_and_swap(&docs, SESSIONS, &id, move |current| {let stored: StoredSession = serde_json::from_value(current).map_err(|error| crate::storage::StorageError::backend(error.to_string()))?;let mut session = DurableSubagentSession::from(stored);if !update(&mut session, chrono::Utc::now().to_rfc3339()) {- return Ok(None);+ return None;}- let next = serde_json::to_value(StoredSession::from(&session))- .map_err(|error| crate::storage::StorageError::backend(error.to_string()))?;- Ok(Some(next))+ serde_json::to_value(StoredSession::from(&session)).ok()}).await- .map(|updated| updated.map(|(value, _)| value))+ .map(|updated| updated.map(|record| record.doc))})?;Ok(updated.map(|value| {@@ -316,17 +310,15 @@ async fn write_if_newer() -> Result<(), crate::storage::StorageError> {// A missing session is inserted; an existing one is replaced only while it// is older than ours. Both go through tsd's one retrying compare-and-swap.- let repo = tinystoragedrivers::Repo::<Value>::new(docs, SESSIONS)?;- repo.update(id, |stored| {+ crate::storage::documents::update_or_insert(docs, SESSIONS, id, |stored| {if stored- .as_ref().and_then(|stored| stored.get("updated_ms")).and_then(Value::as_i64).is_some_and(|have| have >= updated_ms){- return Ok(None);+ return None;}- Ok(Some(doc.clone()))+ Some(doc.clone())}).await.map(|_| ())diff --git a/crates/openhuman-core/src/desktop/notifications/store_documents.rs b/crates/openhuman-core/src/desktop/notifications/store_documents.rsindex b6c3d6fd98..4b038e409a 100644--- a/crates/openhuman-core/src/desktop/notifications/store_documents.rs+++ b/crates/openhuman-core/src/desktop/notifications/store_documents.rs@@ -40,7 +40,7 @@ use super::types::{NotificationStatus,};use crate::config::Config;-use crate::storage::documents::{compare_and_swap, text, Repo};+use crate::storage::documents::{compare_and_swap, text, update_or_insert, Repo};use crate::storage::{DocumentStore, DocumentStoreExt, StorageError};pub(super) use super::storage_schema::{core_id, CORE, DEDUP, NOTIFICATIONS, SETTINGS};@@ -225,11 +225,9 @@ async fn claim_content(skip_recent: bool,) -> Result<Option<Option<i64>>, StorageError> {let window_start = now_ms - DEDUP_WINDOW_SECS * 1000;- let repo = tinystoragedrivers::Repo::<Value>::new(docs, DEDUP)?;let mut previous: Option<Option<i64>> = None;- repo.update(id, |current| {+ update_or_insert(docs, DEDUP, id, |current| {let last = current- .as_ref().and_then(|doc| doc.get("last_ms")).and_then(Value::as_i64);if skip_recent && last.is_some_and(|last| last >= window_start) {diff --git a/crates/openhuman-core/src/storage/documents.rs b/crates/openhuman-core/src/storage/documents.rsindex 4e61a033d3..3f3d9315d6 100644--- a/crates/openhuman-core/src/storage/documents.rs+++ b/crates/openhuman-core/src/storage/documents.rs@@ -319,6 +319,39 @@ pub async fn compare_and_swap()))}+/// Repeatedly applies a transformation to the latest document, inserting it+/// when absent. Returning `None` declines the write. Conflicts restart the+/// transformation against a fresh version, so callers never overwrite a+/// concurrent update based on a stale read.+///+/// # Errors+/// A storage error, or a conflict when the document changed for the retry+/// budget.+pub async fn update_or_insert(+ docs: &Arc<dyn DocumentStore>,+ collection: &str,+ id: &str,+ mut change: impl FnMut(Option<&Value>) -> Option<Value> + Send,+) -> Result<bool, StorageError> {+ for _ in 0..32 {+ let current = docs.get(collection, id).await?;+ let Some(next) = change(current.as_ref().map(|record| &record.doc)) else {+ return Ok(false);+ };+ let precondition = current+ .as_ref()+ .map_or(Precondition::Absent, Versioned::unchanged);+ match docs.put(collection, id, next, precondition).await {+ Ok(_) => return Ok(true),+ Err(error) if error.kind() == ErrorKind::Conflict => continue,+ Err(error) => return Err(error),+ }+ }+ Err(StorageError::conflict(format!(+ "document `{collection}/{id}` kept changing during update"+ )))+}+/// The string field `field` of `doc`, when present.pub fn text<'a>(doc: &'a Value, field: &str) -> Option<&'a str> {doc.get(field).and_then(Value::as_str)