Skip to content

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

fix(config): guard config document writes with preconditions

Steven Enamakel978f7110890b
diff --git a/crates/openhuman-core/src/config/schema/load/source/document.rs b/crates/openhuman-core/src/config/schema/load/source/document.rs
index 6726885f6b..4ad788e80c 100644
--- a/crates/openhuman-core/src/config/schema/load/source/document.rs
+++ b/crates/openhuman-core/src/config/schema/load/source/document.rs
@@ -191,12 +191,20 @@ impl ConfigSource for DocumentConfigSource {
.ensure_collection(&CollectionSpec::new(CONFIG_COLLECTION))
.await
.context("declare the config collection")?;
+ let id = document_id(self.scope.as_str())?;
+ let current = self.docs
+ .get(CONFIG_COLLECTION, &id)
+ .await
+ .context("read the config document before writing")?;
+ let precondition = current
+ .as_ref()
+ .map_or(Precondition::Absent, |stored| stored.unchanged());
self.docs
.put(
CONFIG_COLLECTION,
- &document_id(self.scope.as_str())?,
+ &id,
json!({ BODY_FIELD: sealed }),
- Precondition::None,
+ precondition,
)
.await
.context("write the config document")?;
diff --git a/crates/openhuman-core/src/config/schema/load/source/mod_tests.rs b/crates/openhuman-core/src/config/schema/load/source/mod_tests.rs
index 1211307057..dd7a3e5fc2 100644
--- a/crates/openhuman-core/src/config/schema/load/source/mod_tests.rs
+++ b/crates/openhuman-core/src/config/schema/load/source/mod_tests.rs
@@ -252,6 +252,27 @@ async fn two_scopes_keep_their_config_apart() {
assert!(!bob.read().await.unwrap().contents.contains("alice-model"));
}
+#[tokio::test]
+async fn concurrent_first_config_writes_use_insert_preconditions() {
+ let tmp = tempfile::tempdir().unwrap();
+ let storage = MemoryStorage::new();
+ let first = document_source(&storage, "alice", &tmp.path().join("a.toml"));
+ let second = document_source(&storage, "alice", &tmp.path().join("b.toml"));
+
+ let (first, second) = tokio::join!(
+ first.write("default_model = \"first\"\n"),
+ second.write("default_model = \"second\"\n"),
+ );
+
+ assert_ne!(first.is_ok(), second.is_ok(), "one insert must win CAS");
+ let saved = document_source(&storage, "alice", &tmp.path().join("c.toml"))
+ .read()
+ .await
+ .unwrap()
+ .contents;
+ assert!(saved.contains("first") ^ saved.contains("second"), "{saved}");
+}
+
// ── Through Config ───────────────────────────────────────────────────────────
#[tokio::test]