Skip to content

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

chore: files changed crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents

Steven Enamakel31027f6b72c4
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 f0efee62d5..098785ea25 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
@@ -89,18 +89,56 @@ impl Docs {
for session in sessions {
let mut doc = serde_json::to_value(session)?;
doc["created_at"] = Value::from(session.created_at.as_str());
- writes.push((session.subagent_session_id.clone(), doc));
+ let updated = chrono::DateTime::parse_from_rfc3339(&session.updated_at).ok();
+ writes.push((session.subagent_session_id.clone(), doc, updated));
}
log::debug!("[subagent_sessions] document save count={}", writes.len());
self.0.run(|docs| async move {
- for (id, doc) in writes {
- docs.put(SESSIONS, &id, doc, Precondition::None).await?;
+ for (id, doc, updated) in writes {
+ write_if_newer(&docs, &id, doc, updated).await?;
}
Ok(())
})
}
}
+/// Writes one session unless the stored copy is at least as new, under
+/// compare-and-swap. A caller holds a snapshot of the whole list, so a
+/// session it did not change is older than (or equal to) what another core may
+/// have stored since, and must not be put back.
+async fn write_if_newer(
+ docs: &std::sync::Arc<dyn crate::storage::DocumentStore>,
+ id: &str,
+ doc: Value,
+ updated: Option<chrono::DateTime<chrono::FixedOffset>>,
+) -> Result<(), crate::storage::StorageError> {
+ use tinystoragedrivers::ErrorKind;
+ for _ in 0..crate::storage::documents::CAS_ATTEMPTS {
+ let precondition = match docs.get(SESSIONS, id).await? {
+ None => Precondition::Absent,
+ Some(stored) => {
+ let stored_at = stored
+ .doc
+ .get("updatedAt")
+ .and_then(Value::as_str)
+ .and_then(|raw| chrono::DateTime::parse_from_rfc3339(raw).ok());
+ if matches!((stored_at, updated), (Some(have), Some(ours)) if have >= ours) {
+ return Ok(());
+ }
+ stored.unchanged()
+ }
+ };
+ match docs.put(SESSIONS, id, doc.clone(), precondition).await {
+ Ok(_) => return Ok(()),
+ Err(error) if error.kind() == ErrorKind::Conflict => {}
+ Err(error) => return Err(error),
+ }
+ }
+ Err(crate::storage::StorageError::conflict(format!(
+ "{SESSIONS}/{id} kept changing"
+ )))
+}
+
#[cfg(test)]
#[path = "store_documents_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents_tests.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents_tests.rs
index 0dc5167628..8420b1eb5a 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents_tests.rs
@@ -115,3 +115,24 @@ fn the_store_dispatches_to_documents_when_a_backend_is_pinned() {
.join(".openhuman/subagent_sessions.json")
.exists());
}
+
+#[test]
+fn a_stale_snapshot_does_not_revert_a_newer_session() {
+ let docs = docs_in(&MemoryStorage::new(), "local");
+ let old = session("a");
+ docs.save(&[old.clone()]).unwrap();
+
+ // Another core finishes the session.
+ let mut newer = old.clone();
+ newer.status = DurableSubagentStatus::Failed;
+ newer.updated_at = "2026-03-01T00:00:00Z".into();
+ docs.save(&[newer]).unwrap();
+
+ // This core still holds the old snapshot and saves it with a new session.
+ docs.save(&[old, session("b")]).unwrap();
+
+ let loaded = docs.load().unwrap();
+ assert_eq!(loaded.len(), 2);
+ let a = loaded.iter().find(|s| s.subagent_session_id == "a").unwrap();
+ assert_eq!(a.status, DurableSubagentStatus::Failed);
+}