Skip to content

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

feat(config): track document version for optimistic concurrency

Steven Enamakele439b4b7ecaf
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 4ad788e80c..8a1c501d88 100644
--- a/crates/openhuman-core/src/config/schema/load/source/document.rs
+++ b/crates/openhuman-core/src/config/schema/load/source/document.rs
@@ -94,7 +94,7 @@ impl DocumentConfigSource {
}
/// The decrypted stored body, or `None` when the scope has no document.
- async fn stored_body(&self) -> Result<Option<zeroize::Zeroizing<String>>> {
+ async fn stored_body(&self) -> Result<Option<(zeroize::Zeroizing<String>, tinystoragedrivers::Version)>> {
self.docs
.ensure_collection(&CollectionSpec::new(CONFIG_COLLECTION))
.await
@@ -121,7 +121,7 @@ impl DocumentConfigSource {
.map_err(|error| anyhow!("the config document does not decrypt: {error}"))?;
let body = String::from_utf8(plain.to_vec())
.map_err(|_| anyhow!("the config document is not UTF-8"))?;
- Ok(Some(zeroize::Zeroizing::new(body)))
+ Ok(Some((zeroize::Zeroizing::new(body), versioned.version)))
}
}
@@ -154,7 +154,7 @@ impl ConfigSource for DocumentConfigSource {
}
async fn read(&self) -> Result<ConfigRead> {
- let Some(body) = self.stored_body().await? else {
+ let Some((body, document_version)) = self.stored_body().await? else {
tracing::debug!(scope = %self.scope.as_str(), "[config] no config document; reading the file");
return self.file.read().await;
};
@@ -177,10 +177,12 @@ impl ConfigSource for DocumentConfigSource {
contents,
recovered: false,
from_document: true,
+ document_version: Some(document_version),
+ document_version: Some(document_version),
})
}
- async fn write(&self, toml: &str) -> Result<()> {
+ async fn write(&self, toml: &str, expected: Option<tinystoragedrivers::Version>) -> Result<Option<tinystoragedrivers::Version>> {
let body = zeroize::Zeroizing::new(
strip_bootstrap(toml).context("strip bootstrap tables from the config")?,
);
@@ -192,14 +194,8 @@ impl ConfigSource for DocumentConfigSource {
.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
+ let precondition = expected.map_or(Precondition::Absent, Precondition::Version);
+ let version = self.docs
.put(
CONFIG_COLLECTION,
&id,
@@ -209,6 +205,6 @@ impl ConfigSource for DocumentConfigSource {
.await
.context("write the config document")?;
tracing::debug!(scope = %self.scope.as_str(), "[config] wrote the config document");
- Ok(())
+ Ok(Some(version))
}
}
diff --git a/crates/openhuman-core/src/config/schema/types/config.rs b/crates/openhuman-core/src/config/schema/types/config.rs
index 6164681f1c..4febd8d8f0 100644
--- a/crates/openhuman-core/src/config/schema/types/config.rs
+++ b/crates/openhuman-core/src/config/schema/types/config.rs
@@ -54,6 +54,10 @@ pub struct CustomEmbeddingsConfig {
/// `Clone` is implemented by hand in `config_clone.rs` so it is emitted once.
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct Config {
+ /// Revision of the shared config document this snapshot was loaded from.
+ #[serde(skip)]
+ #[schemars(skip)]
+ pub(crate) document_version: std::sync::Mutex<Option<tinystoragedrivers::Version>>,
/// Immutable operator provider binding, absent from the user wire format.
#[serde(skip)]
#[schemars(skip)]
diff --git a/crates/openhuman-core/src/config/schema/types/config_clone.rs b/crates/openhuman-core/src/config/schema/types/config_clone.rs
index 3c4cd44ea3..9c038750f0 100644
--- a/crates/openhuman-core/src/config/schema/types/config_clone.rs
+++ b/crates/openhuman-core/src/config/schema/types/config_clone.rs
@@ -12,6 +12,7 @@ impl Clone for Config {
#[inline(never)]
fn clone(&self) -> Self {
Self {
+ document_version: std::sync::Mutex::new(*self.document_version.lock().unwrap()),
profile_providers: self.profile_providers.clone(),
workspace_dir: self.workspace_dir.clone(),
action_dir: self.action_dir.clone(),
diff --git a/crates/openhuman-core/src/config/schema/types/defaults.rs b/crates/openhuman-core/src/config/schema/types/defaults.rs
index 2be9b50145..6f6f057752 100644
--- a/crates/openhuman-core/src/config/schema/types/defaults.rs
+++ b/crates/openhuman-core/src/config/schema/types/defaults.rs
@@ -25,6 +25,7 @@ impl Default for Config {
});
Self {
+ document_version: std::sync::Mutex::new(None),
profile_providers: None,
workspace_dir: openhuman_dir.join("workspace"),
action_dir: crate::config::default_action_dir(),