Skip to content

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

fix(config): refresh context after document conflict

Steven Enamakela1a585312564
diff --git a/crates/openhuman-core/src/config/schema/load/impl_load.rs b/crates/openhuman-core/src/config/schema/load/impl_load.rs
index 2a347f42d4..f508777bc8 100644
--- a/crates/openhuman-core/src/config/schema/load/impl_load.rs
+++ b/crates/openhuman-core/src/config/schema/load/impl_load.rs
@@ -439,7 +439,15 @@ impl Config {
} else {
None
};
- let version = source.write_versioned(&toml_str, expected).await?;
+ let version = match source.write_versioned(&toml_str, expected).await {
+ Ok(version) => version,
+ Err(error) => {
+ if is_document_conflict(&error) {
+ self.refresh_context_after_conflict(&expected_snapshot).await;
+ }
+ return Err(error);
+ }
+ };
if !update_snapshot {
return Ok(());
}
@@ -468,6 +476,55 @@ impl Config {
Ok(())
}
+
+ async fn refresh_context_after_conflict(
+ &self,
+ expected: &super::super::ConfigDocumentSnapshot,
+ ) {
+ let Some(snapshot) = self
+ .context_snapshot
+ .as_ref()
+ .and_then(std::sync::Weak::upgrade)
+ else {
+ return;
+ };
+ let (config_path, workspace_dir, runtime) = {
+ let current = snapshot.lock().unwrap();
+ if *current.document_snapshot.lock().unwrap() != *expected {
+ return;
+ }
+ (current.config_path.clone(), current.workspace_dir.clone(), current.clone())
+ };
+ let Ok(mut refreshed) = Self::load_from_config_path(&config_path, &workspace_dir).await
+ else {
+ return;
+ };
+ if refreshed.document_snapshot.lock().unwrap().anchor != expected.anchor {
+ return;
+ }
+ refreshed.profile_providers = runtime.profile_providers.clone();
+ refreshed.workspace_dir = runtime.workspace_dir;
+ refreshed.action_dir = runtime.action_dir;
+ refreshed.config_path = runtime.config_path;
+ refreshed.cli_inference_snapshot = runtime.cli_inference_snapshot;
+ refreshed.recovered_from_corruption = runtime.recovered_from_corruption;
+ refreshed.ephemeral_route = runtime.ephemeral_route;
+ refreshed.resolved_endpoints = runtime.resolved_endpoints;
+ refreshed.context_snapshot = Some(std::sync::Arc::downgrade(&snapshot));
+
+ let mut current = snapshot.lock().unwrap();
+ if *current.document_snapshot.lock().unwrap() == *expected {
+ *current = refreshed;
+ }
+ }
+}
+
+fn is_document_conflict(error: &anyhow::Error) -> bool {
+ error.chain().any(|cause| {
+ cause
+ .downcast_ref::<tinystoragedrivers::StorageError>()
+ .is_some_and(|error| error.kind() == tinystoragedrivers::ErrorKind::Conflict)
+ })
}
impl Config {
diff --git a/crates/openhuman-core/src/config/schema/load/source/concurrency_tests.rs b/crates/openhuman-core/src/config/schema/load/source/concurrency_tests.rs
index 577eefc015..db4268a805 100644
--- a/crates/openhuman-core/src/config/schema/load/source/concurrency_tests.rs
+++ b/crates/openhuman-core/src/config/schema/load/source/concurrency_tests.rs
@@ -302,6 +302,33 @@ async fn config_snapshot_is_bound_to_its_backend_instance() {
assert_typed_conflict(error);
}
+#[tokio::test]
+async fn a_file_snapshot_can_seed_an_absent_document_after_storage_bootstrap() {
+ let tmp = tempfile::tempdir().unwrap();
+ let path = tmp.path().join("config.toml");
+ let workspace = tmp.path().join("workspace");
+ let mut file_config = config_at(tmp.path(), "file-seed");
+ file_config.save().await.unwrap();
+ let mut snapshot = Config::load_from_config_path(&path, &workspace)
+ .await
+ .unwrap();
+ assert!(matches!(
+ snapshot.document_snapshot.lock().unwrap().anchor.clone(),
+ Some(ConfigSourceAnchor::File(_))
+ ));
+
+ let storage = MemoryStorage::new();
+ let _source = ForcedDocumentSource::new(
+ scoped(&storage, "tenant-file-bootstrap"),
+ Scope::new("tenant-file-bootstrap").unwrap(),
+ keys(15),
+ );
+ snapshot.default_model = Some("imported-to-document".into());
+ snapshot.save().await.unwrap();
+ let loaded = Config::load_from_config_path(&path, &workspace).await.unwrap();
+ assert_eq!(loaded.default_model.as_deref(), Some("imported-to-document"));
+}
+
#[tokio::test]
async fn document_source_write_uses_its_last_read_revision() {
let tmp = tempfile::tempdir().unwrap();