Skip to content

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

feat(config): add atomic config file loading

Steven Enamakel0ba7916b36b1
diff --git a/crates/openhuman-core/src/channels/host/adapters.rs b/crates/openhuman-core/src/channels/host/adapters.rs
index f3b9a470b4..ba67129d19 100644
--- a/crates/openhuman-core/src/channels/host/adapters.rs
+++ b/crates/openhuman-core/src/channels/host/adapters.rs
@@ -249,8 +249,8 @@ impl AllowlistStore for ConfigAllowlistStore {
let contents = tokio::fs::read_to_string(&config_path)
.await
.with_context(|| format!("failed to read config file: {}", config_path.display()))?;
- let mut config: Config =
- toml::from_str(&contents).context("failed to parse config.toml for allowlist")?;
+ let mut config: Config = crate::config::schema::load::config_from_toml_str(&contents)
+ .context("failed to parse config.toml for allowlist")?;
config.config_path = config_path;
config.workspace_dir = openhuman_dir.join("workspace");
diff --git a/crates/openhuman-core/src/config/schema/load/atomic_commit.rs b/crates/openhuman-core/src/config/schema/load/atomic_commit.rs
index 14b9d5e99c..84e5df0a39 100644
--- a/crates/openhuman-core/src/config/schema/load/atomic_commit.rs
+++ b/crates/openhuman-core/src/config/schema/load/atomic_commit.rs
@@ -127,7 +127,7 @@ async fn back_up_previous(config_path: &Path, backup_path: &Path) {
let Ok(existing) = fs::read_to_string(config_path).await else {
return;
};
- let Ok(mut previous) = toml::from_str::<crate::config::Config>(&existing) else {
+ let Ok(mut previous) = super::parse::config_from_toml_str(&existing) else {
// Unparseable, so it cannot be checked for at-rest secrets and cannot
// be trusted not to hold one. It is also worthless to recovery, which
// parses what it finds here.
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 5dfad03b99..efe2863271 100644
--- a/crates/openhuman-core/src/config/schema/load/impl_load.rs
+++ b/crates/openhuman-core/src/config/schema/load/impl_load.rs
@@ -224,7 +224,7 @@ pub(super) async fn migration_source(config_path: &Path, contents: &str) -> Stri
}
async fn parse_toml_off_worker(contents: String) -> Result<Box<Config>, String> {
- match tokio::task::spawn_blocking(move || toml::from_str::<Config>(&contents).map(Box::new))
+ match tokio::task::spawn_blocking(move || super::parse::config_from_toml_str(&contents).map(Box::new))
.await
{
Ok(Ok(config)) => Ok(config),
diff --git a/crates/openhuman-core/src/config/schema/load/mod.rs b/crates/openhuman-core/src/config/schema/load/mod.rs
index cb99bf5607..37521e541b 100644
--- a/crates/openhuman-core/src/config/schema/load/mod.rs
+++ b/crates/openhuman-core/src/config/schema/load/mod.rs
@@ -7,6 +7,7 @@ mod env;
mod env_overlay;
mod impl_load;
mod migrate;
+mod parse;
mod secrets;
pub use active_workspace::active_workspace_dir_cached;
@@ -54,6 +55,7 @@ pub(crate) use super::Config;
pub(crate) use dirs::ACTIVE_USER_STATE_FILE;
#[cfg(test)]
pub(crate) use impl_load::parse_config_with_recovery;
+pub(crate) use parse::config_from_toml_str;
#[cfg(test)]
pub(crate) use migrate::{migrate_cloud_provider_slugs, migrate_legacy_inference_url};
#[cfg(test)]
diff --git a/crates/openhuman-core/src/config/schema/load/parse.rs b/crates/openhuman-core/src/config/schema/load/parse.rs
new file mode 100644
index 0000000000..3fc7245627
--- /dev/null
+++ b/crates/openhuman-core/src/config/schema/load/parse.rs
@@ -0,0 +1,22 @@
+//! The one place a `config.toml` document becomes a [`Config`].
+//!
+//! Deserializing the config tree straight from TOML instantiates every derived
+//! `Deserialize` impl in it once per `toml` map-access type (about six), and the
+//! tree has well over a hundred types. Parsing into a [`toml::Value`] first and
+//! decoding that through [`serde_json::Value`] keeps one decoder instantiation
+//! per type. Syntax errors still carry TOML line and column; a type error keeps
+//! the serde message but not the location.
+
+use super::Config;
+
+/// Parse a `config.toml` document into a [`Config`].
+#[inline(never)]
+pub(crate) fn config_from_toml_str(contents: &str) -> anyhow::Result<Config> {
+ let document: toml::Value = toml::from_str(contents)?;
+ let json = serde_json::to_value(document)?;
+ Ok(serde_json::from_value(json)?)
+}
+
+#[cfg(test)]
+#[path = "parse_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/config/schema/load/parse_tests.rs b/crates/openhuman-core/src/config/schema/load/parse_tests.rs
new file mode 100644
index 0000000000..d44da94acc
--- /dev/null
+++ b/crates/openhuman-core/src/config/schema/load/parse_tests.rs
@@ -0,0 +1,40 @@
+use super::*;
+
+const SAMPLE: &str = r#"
+default_model = "parse-test-model"
+default_temperature = 0.4
+
+[agent]
+agent_timeout_secs = 77
+"#;
+
+#[test]
+fn parses_the_same_config_as_direct_toml() {
+ let via_value = config_from_toml_str(SAMPLE).expect("sample parses");
+ let direct: Config = toml::from_str(SAMPLE).expect("sample parses directly");
+ assert_eq!(
+ serde_json::to_value(&via_value).unwrap(),
+ serde_json::to_value(&direct).unwrap()
+ );
+ assert_eq!(via_value.default_model.as_deref(), Some("parse-test-model"));
+}
+
+#[test]
+fn an_empty_document_yields_defaults() {
+ let parsed = config_from_toml_str("").expect("empty document parses");
+ assert_eq!(
+ serde_json::to_value(&parsed).unwrap(),
+ serde_json::to_value(&Config::default()).unwrap()
+ );
+}
+
+#[test]
+fn a_syntax_error_reports_the_toml_location() {
+ let err = config_from_toml_str("default_model = \n[broken").unwrap_err();
+ assert!(err.to_string().contains("line"), "{err}");
+}
+
+#[test]
+fn a_type_error_is_an_error() {
+ assert!(config_from_toml_str("default_temperature = \"hot\"").is_err());
+}
diff --git a/crates/openhuman-core/src/tools/impl/system/proxy_config.rs b/crates/openhuman-core/src/tools/impl/system/proxy_config.rs
index 2185d5ff4a..9a41de8f85 100644
--- a/crates/openhuman-core/src/tools/impl/system/proxy_config.rs
+++ b/crates/openhuman-core/src/tools/impl/system/proxy_config.rs
@@ -27,7 +27,7 @@ impl ProxyConfigTool {
)
})?;
- let mut parsed: Config = toml::from_str(&contents).map_err(|error| {
+ let mut parsed: Config = crate::config::schema::load::config_from_toml_str(&contents).map_err(|error| {
anyhow::anyhow!(
"Failed to parse config file {}: {error}",
self.config.config_path.display()