Skip to content

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

refactor(storage): update document storage logic

Steven Enamakel4c5cff5dbbc4
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.rs
index 3c11ef1cfb..6f1191223d 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,14 +232,16 @@ impl Docs {
{
let id = key_of(subagent_session_id)?;
let updated = self.0.run(|docs| async move {
- crate::storage::documents::compare_and_swap(&docs, SESSIONS, &id, move |current| {
+ crate::storage::documents::update_document(&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 None;
+ return Ok(None);
}
- serde_json::to_value(StoredSession::from(&session)).ok()
+ let next = serde_json::to_value(StoredSession::from(&session))
+ .map_err(|error| crate::storage::StorageError::backend(error.to_string()))?;
+ Ok(Some(next))
})
.await
.map(|updated| updated.map(|record| record.doc))
diff --git a/crates/openhuman-core/src/storage/documents.rs b/crates/openhuman-core/src/storage/documents.rs
index 3f3d9315d6..0283b1b345 100644
--- a/crates/openhuman-core/src/storage/documents.rs
+++ b/crates/openhuman-core/src/storage/documents.rs
@@ -290,12 +290,23 @@ pub async fn compare_and_swap(
collection: &str,
id: &str,
change: impl Fn(&Value) -> Option<Value> + Send + Sync,
+) -> Result<Option<Versioned<Value>>, StorageError> {
+ update_document(docs, collection, id, |current| Ok(change(current))).await
+}
+
+/// Applies a fallible transformation to an existing document under
+/// compare-and-swap. Returning `Ok(None)` declines the write.
+pub async fn update_document(
+ docs: &Arc<dyn DocumentStore>,
+ collection: &str,
+ id: &str,
+ mut change: impl FnMut(&Value) -> Result<Option<Value>, StorageError> + Send,
) -> Result<Option<Versioned<Value>>, StorageError> {
for _ in 0..32 {
let Some(current) = docs.get(collection, id).await? else {
return Ok(None);
};
- let Some(next) = change(&current.doc) else {
+ let Some(next) = change(&current.doc)? else {
return Ok(None);
};
match docs