Skip to content

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

fix(config): surface config save conflicts as structured errors

Steven Enamakeld385cdb163bb
diff --git a/crates/openhuman-core/src/agent_registry/ops.rs b/crates/openhuman-core/src/agent_registry/ops.rs
index 4838a4f152..79141b6b0c 100644
--- a/crates/openhuman-core/src/agent_registry/ops.rs
+++ b/crates/openhuman-core/src/agent_registry/ops.rs
@@ -58,7 +58,11 @@ pub async fn upsert_custom_agent(
config
.save()
.await
- .map_err(|e| format!("failed to save config: {e:#}"))?;
+ .map_err(|error| crate::config::ops::config_save_error_with_context(
+ error,
+ "failed to save config",
+ true,
+ ))?;
Ok(entry)
}
@@ -103,7 +107,11 @@ pub async fn update_agent(
config
.save()
.await
- .map_err(|e| format!("failed to save config: {e:#}"))?;
+ .map_err(|error| crate::config::ops::config_save_error_with_context(
+ error,
+ "failed to save config",
+ true,
+ ))?;
Ok(updated)
}
@@ -137,7 +145,11 @@ pub async fn remove_agent(ctx: &DispatchCtx, id: &str) -> Result<bool, String> {
config
.save()
.await
- .map_err(|e| format!("failed to save config: {e:#}"))?;
+ .map_err(|error| crate::config::ops::config_save_error_with_context(
+ error,
+ "failed to save config",
+ true,
+ ))?;
}
Ok(removed)
}
diff --git a/crates/openhuman-core/src/channels/controllers/ops/connect/connect_channel.rs b/crates/openhuman-core/src/channels/controllers/ops/connect/connect_channel.rs
index 8f3cbf8f05..14dcd3afa2 100644
--- a/crates/openhuman-core/src/channels/controllers/ops/connect/connect_channel.rs
+++ b/crates/openhuman-core/src/channels/controllers/ops/connect/connect_channel.rs
@@ -116,7 +116,13 @@ pub(crate) async fn persist_prepared_connection(
persisted
.save()
.await
- .map_err(|error| format!("failed to persist {channel_id} config.toml: {error}"))?;
+ .map_err(|error| {
+ crate::config::ops::config_save_error_with_context(
+ error,
+ &format!("failed to persist {channel_id} config.toml"),
+ false,
+ )
+ })?;
}
}
let log = if local_only {
diff --git a/crates/openhuman-core/src/channels/controllers/ops/connect/disconnect.rs b/crates/openhuman-core/src/channels/controllers/ops/connect/disconnect.rs
index d1c223a575..d8396cacac 100644
--- a/crates/openhuman-core/src/channels/controllers/ops/connect/disconnect.rs
+++ b/crates/openhuman-core/src/channels/controllers/ops/connect/disconnect.rs
@@ -36,7 +36,13 @@ pub async fn disconnect_channel(
persisted
.save()
.await
- .map_err(|e| format!("failed to clear telegram config.toml: {e}"))?;
+ .map_err(|error| {
+ crate::config::ops::config_save_error_with_context(
+ error,
+ "failed to clear telegram config.toml",
+ false,
+ )
+ })?;
tracing::info!(
target: "openhuman::channels",
"[telegram] disconnect_channel: cleared channels_config.telegram"
@@ -48,7 +54,13 @@ pub async fn disconnect_channel(
persisted
.save()
.await
- .map_err(|e| format!("failed to clear discord config.toml: {e}"))?;
+ .map_err(|error| {
+ crate::config::ops::config_save_error_with_context(
+ error,
+ "failed to clear discord config.toml",
+ false,
+ )
+ })?;
tracing::info!(
target: "openhuman::channels",
"[discord] disconnect_channel: cleared channels_config.discord"
@@ -60,7 +72,13 @@ pub async fn disconnect_channel(
persisted
.save()
.await
- .map_err(|e| format!("failed to clear imessage config.toml: {e}"))?;
+ .map_err(|error| {
+ crate::config::ops::config_save_error_with_context(
+ error,
+ "failed to clear imessage config.toml",
+ false,
+ )
+ })?;
tracing::info!(
target: "openhuman::channels",
"[imessage] disconnect_channel: cleared channels_config.imessage"
@@ -72,7 +90,13 @@ pub async fn disconnect_channel(
persisted
.save()
.await
- .map_err(|e| format!("failed to clear yuanbao config.toml: {e}"))?;
+ .map_err(|error| {
+ crate::config::ops::config_save_error_with_context(
+ error,
+ "failed to clear yuanbao config.toml",
+ false,
+ )
+ })?;
tracing::info!(
target: "openhuman::channels",
"[yuanbao] disconnect_channel: cleared channels_config.yuanbao"
@@ -84,7 +108,13 @@ pub async fn disconnect_channel(
persisted
.save()
.await
- .map_err(|e| format!("failed to clear email config.toml: {e}"))?;
+ .map_err(|error| {
+ crate::config::ops::config_save_error_with_context(
+ error,
+ "failed to clear email config.toml",
+ false,
+ )
+ })?;
tracing::info!(
target: "openhuman::channels",
"[email] disconnect_channel: cleared channels_config.email"
diff --git a/crates/openhuman-core/src/channels/controllers/ops/connect/email.rs b/crates/openhuman-core/src/channels/controllers/ops/connect/email.rs
index f65f9bded7..e81fff9599 100644
--- a/crates/openhuman-core/src/channels/controllers/ops/connect/email.rs
+++ b/crates/openhuman-core/src/channels/controllers/ops/connect/email.rs
@@ -31,7 +31,13 @@ pub(crate) async fn persist_email_config(
persisted
.save()
.await
- .map_err(|e| format!("failed to persist email config.toml: {e}"))?;
+ .map_err(|error| {
+ crate::config::ops::config_save_error_with_context(
+ error,
+ "failed to persist email config.toml",
+ false,
+ )
+ })?;
tracing::info!(
target: "openhuman::channels",
diff --git a/crates/openhuman-core/src/channels/controllers/ops/connect/status.rs b/crates/openhuman-core/src/channels/controllers/ops/connect/status.rs
index abad25c6f8..b445047f12 100644
--- a/crates/openhuman-core/src/channels/controllers/ops/connect/status.rs
+++ b/crates/openhuman-core/src/channels/controllers/ops/connect/status.rs
@@ -93,7 +93,13 @@ pub async fn set_default_channel(
config
.save()
.await
- .map_err(|e| format!("failed to persist default channel: {e}"))?;
+ .map_err(|error| {
+ crate::config::ops::config_save_error_with_context(
+ error,
+ "failed to persist default channel",
+ true,
+ )
+ })?;
// Apply live so proactive routing follows the new default immediately.
crate::channels::proactive::set_runtime_active_channel(Some(canonical.clone()));
diff --git a/crates/openhuman-core/src/config/ops/mod.rs b/crates/openhuman-core/src/config/ops/mod.rs
index daac049ffd..6642b5a34d 100644
--- a/crates/openhuman-core/src/config/ops/mod.rs
+++ b/crates/openhuman-core/src/config/ops/mod.rs
@@ -11,23 +11,50 @@ mod tool_dispatcher;
mod ui;
pub(crate) fn config_save_error(error: anyhow::Error) -> String {
+ config_save_error_with_retryability(error, true)
+}
+
+pub(crate) fn config_save_error_with_retryability(
+ error: anyhow::Error,
+ retryable: bool,
+) -> String {
+ if let Some(conflict) = config_save_conflict(&error, retryable) {
+ return conflict.encode();
+ }
+ error.to_string()
+}
+
+pub(crate) fn config_save_error_with_context(
+ error: anyhow::Error,
+ context: &str,
+ retryable: bool,
+) -> String {
+ if let Some(conflict) = config_save_conflict(&error, retryable) {
+ return conflict.encode();
+ }
+ format!("{context}: {error:#}")
+}
+
+pub(crate) fn config_save_conflict(
+ error: &anyhow::Error,
+ retryable: bool,
+) -> Option<crate::core::StructuredRpcError> {
let conflict = error.chain().any(|cause| {
cause
.downcast_ref::<tinystoragedrivers::StorageError>()
.is_some_and(|error| error.kind() == tinystoragedrivers::ErrorKind::Conflict)
});
- if conflict {
- return crate::core::StructuredRpcError {
- message: "Configuration changed on another instance; reload it and retry".into(),
- data: Some(serde_json::json!({
- "kind": "ConfigConflict",
- "retryable": true,
- })),
- expected_user_state: true,
- }
- .encode();
+ if !conflict {
+ return None;
}
- error.to_string()
+ Some(crate::core::StructuredRpcError {
+ message: "Configuration changed on another instance; reload it and retry".into(),
+ data: Some(serde_json::json!({
+ "kind": "ConfigConflict",
+ "retryable": retryable,
+ })),
+ expected_user_state: true,
+ })
}
// ── Public re-exports (preserving the flat external API) ─────────────────────
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 cde4213e5e..634b113bef 100644
--- a/crates/openhuman-core/src/config/schema/load/source/document.rs
+++ b/crates/openhuman-core/src/config/schema/load/source/document.rs
@@ -1,6 +1,6 @@
//! [`DocumentConfigSource`]: the config as the `config/{scope}` document.
-use std::sync::Arc;
+use std::sync::{Arc, Mutex};
use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
@@ -36,6 +36,7 @@ pub(crate) struct DocumentConfigSource {
scope: Scope,
keys: Arc<dyn KeyProvider>,
file: FileConfigSource,
+ observed_version: Mutex<Option<tinystoragedrivers::Version>>,
}
impl DocumentConfigSource {
@@ -50,6 +51,7 @@ impl DocumentConfigSource {
scope,
keys,
file,
+ observed_version: Mutex::new(None),
}
}
@@ -59,22 +61,6 @@ impl DocumentConfigSource {
.map_err(|error| anyhow!("no data key for the config document: {error}"))
}
- /// Read the current document revision for callers that intentionally
- /// replace the latest value rather than saving a previously loaded config
- /// snapshot. `Config::save` uses the revision captured at load time.
- async fn current_version(&self) -> Result<Option<tinystoragedrivers::Version>> {
- self.docs
- .ensure_collection(&CollectionSpec::new(CONFIG_COLLECTION))
- .await
- .context("declare the config collection")?;
- let stored = self
- .docs
- .get(CONFIG_COLLECTION, &document_id(self.scope.as_str())?)
- .await
- .context("read the config document")?;
- Ok(stored.map(|versioned| versioned.version))
- }
-
/// The local file's text for its bootstrap tables.
///
/// When the file read had to recover (non-UTF-8 content renamed aside, the
@@ -174,7 +160,9 @@ impl ConfigSource for DocumentConfigSource {
async fn read(&self) -> Result<ConfigRead> {
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;
+ let read = self.file.read().await?;
+ *self.observed_version.lock().unwrap() = None;
+ return Ok(read);
};
// Bootstrap tables come from the file, whatever the document says.
// Through the file source, so its corruption recovery (a `.bak`, a
@@ -191,17 +179,19 @@ impl ConfigSource for DocumentConfigSource {
// here: the loader's file-corruption branch would rename the local
// `config.toml` aside and save only to the document, losing the
// bootstrap tables on the next boot.
- Ok(ConfigRead {
+ let read = ConfigRead {
contents,
recovered: false,
from_document: true,
document_version: Some(document_version),
- })
+ };
+ *self.observed_version.lock().unwrap() = Some(document_version);
+ Ok(read)
}
async fn write(&self, toml: &str) -> Result<()> {
- let current_version = self.current_version().await?;
- self.write_versioned(toml, current_version).await?;
+ let expected = *self.observed_version.lock().unwrap();
+ self.write_versioned(toml, expected).await?;
Ok(())
}
@@ -232,6 +222,7 @@ impl ConfigSource for DocumentConfigSource {
)
.await
.context("write the config document")?;
+ *self.observed_version.lock().unwrap() = Some(version);
tracing::debug!(scope = %self.scope.as_str(), "[config] wrote the config document");
Ok(Some(version))
}
diff --git a/crates/openhuman-core/src/inference/embedding_host/rpc/settings.rs b/crates/openhuman-core/src/inference/embedding_host/rpc/settings.rs
index 779c8434da..d8453949e5 100644
--- a/crates/openhuman-core/src/inference/embedding_host/rpc/settings.rs
+++ b/crates/openhuman-core/src/inference/embedding_host/rpc/settings.rs
@@ -405,7 +405,10 @@ pub async fn update_settings(
}
}
- config.save().await.map_err(|e| e.to_string())?;
+ config
+ .save()
+ .await
+ .map_err(crate::config::ops::config_save_error)?;
tracing::info!(
provider = config.memory.embedding_provider.as_str(),
diff --git a/crates/openhuman-core/src/inference/tokenjuice/schemas.rs b/crates/openhuman-core/src/inference/tokenjuice/schemas.rs
index 512826a1ff..f98567f48b 100644
--- a/crates/openhuman-core/src/inference/tokenjuice/schemas.rs
+++ b/crates/openhuman-core/src/inference/tokenjuice/schemas.rs
@@ -398,7 +398,9 @@ fn handle_settings_update(ctx: DispatchCtx, params: Map<String, Value>) -> Contr
config
.save()
.await
- .map_err(|e| format!("save config: {e}"))?;
+ .map_err(|error| {
+ crate::config::ops::config_save_error_with_context(error, "save config", true)
+ })?;
// Re-install so router flags / CCR limits / threshold take effect live.
crate::inference::tokenjuice::install_from_config(&config).await?;
diff --git a/crates/openhuman-core/src/integrations/composio/ops/direct_mode.rs b/crates/openhuman-core/src/integrations/composio/ops/direct_mode.rs
index 99fc1ae3d0..2ab21caddf 100644
--- a/crates/openhuman-core/src/integrations/composio/ops/direct_mode.rs
+++ b/crates/openhuman-core/src/integrations/composio/ops/direct_mode.rs
@@ -102,7 +102,13 @@ pub async fn composio_set_api_key(
cfg_mut
.save()
.await
- .map_err(|e| format!("[composio-direct] save config failed: {e}"))?;
+ .map_err(|error| {
+ crate::config::ops::config_save_error_with_context(
+ error,
+ "[composio-direct] save config failed",
+ false,
+ )
+ })?;
"mode=direct"
} else {
"mode unchanged"
@@ -152,7 +158,13 @@ pub async fn composio_clear_api_key(
cfg_mut
.save()
.await
- .map_err(|e| format!("[composio-direct] save config failed: {e}"))?;
+ .map_err(|error| {
+ crate::config::ops::config_save_error_with_context(
+ error,
+ "[composio-direct] save config failed",
+ false,
+ )
+ })?;
direct_auth::reset_all_direct_auth_failures();
crate::core::bus::BUS.publish(crate::core::events::DomainEvent::ComposioConfigChanged {
diff --git a/crates/openhuman-core/src/mcp/registry/ops.rs b/crates/openhuman-core/src/mcp/registry/ops.rs
index cae896cb5e..f4ed9b1f75 100644
--- a/crates/openhuman-core/src/mcp/registry/ops.rs
+++ b/crates/openhuman-core/src/mcp/registry/ops.rs
@@ -429,7 +429,10 @@ pub async fn mcp_clients_registry_settings_set(
apply(&mut auth.mcp_official_base, mcp_official_base.clone());
apply(&mut auth.mcp_official_token, mcp_official_token.clone());
- config.save().await.map_err(|error| error.to_string())?;
+ config
+ .save()
+ .await
+ .map_err(crate::config::ops::config_save_error)?;
let settings: tinymcp_bus::RegistrySettings = module_call_confidential(
config,
diff --git a/crates/openhuman-core/src/voice/live/ops.rs b/crates/openhuman-core/src/voice/live/ops.rs
index 8e041620cf..11b55bd196 100644
--- a/crates/openhuman-core/src/voice/live/ops.rs
+++ b/crates/openhuman-core/src/voice/live/ops.rs
@@ -92,7 +92,10 @@ pub async fn live_settings_set(
patch: LiveSettingsPatch,
) -> Result<Outcome<LiveVoiceConfig>, String> {
apply_patch(config, patch)?;
- config.save().await.map_err(|e| e.to_string())?;
+ config
+ .save()
+ .await
+ .map_err(crate::config::ops::config_save_error)?;
tracing::debug!(
default_provider = %config.voice_live.default_provider,
"[voice-live] settings saved"
diff --git a/crates/openhuman-core/src/voice/schemas/handlers/provider_server.rs b/crates/openhuman-core/src/voice/schemas/handlers/provider_server.rs
index 41bca07ded..ceddf042a3 100644
--- a/crates/openhuman-core/src/voice/schemas/handlers/provider_server.rs
+++ b/crates/openhuman-core/src/voice/schemas/handlers/provider_server.rs
@@ -63,7 +63,10 @@ pub(crate) fn handle_voice_set_providers(
config.local_ai.tts_voice_id = voice.to_string();
}
- config.save().await.map_err(|e| e.to_string())?;
+ config
+ .save()
+ .await
+ .map_err(crate::config::ops::config_save_error)?;
log::debug!(
"[voice-factory] persisted providers stt={} tts={} stt_model={} tts_voice={}",
config.local_ai.stt_provider,
@@ -200,7 +203,10 @@ pub(crate) fn handle_voice_update_provider_settings(
}
}
- config.save().await.map_err(|e| e.to_string())?;
+ config
+ .save()
+ .await
+ .map_err(crate::config::ops::config_save_error)?;
log::debug!(
"[voice-factory] persisted voice provider settings: {} providers, stt={:?}, tts={:?}",