Skip to content

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

test(config): cover malformed document handling in config sources

Steven Enamakel9f82bc135bca
diff --git a/crates/openhuman-core/src/config/schema/README.md b/crates/openhuman-core/src/config/schema/README.md
index e5f7b66f0e..b3c4be13c6 100644
--- a/crates/openhuman-core/src/config/schema/README.md
+++ b/crates/openhuman-core/src/config/schema/README.md
@@ -198,4 +198,4 @@ Per-section `*_tests.rs` files, plus [`load_tests.rs`](./load_tests.rs) (split i
## Config sources
-`load/source/` defines the `ConfigSource` port: where the TOML text of a config lives. `FileConfigSource` is `config.toml` with the atomic replace and `.bak` of `load/atomic_commit.rs` (the file stays hand-editable). `DocumentConfigSource` is the `config/{scope}` document on a shared (multi-tenant) storage backend, used by `Config::save` and by snapshot reloads (`load_from_config_path`). The first load of a process (`load_or_init`) always reads the file. Bootstrap tables (`[storage]`) are never written to a document and are always re-applied from the file on a document read. `SaasConfig` is not part of this.
+`load/source/` defines the `ConfigSource` port: where the TOML text of a config lives. `FileConfigSource` is `config.toml` with the atomic replace and `.bak` of `load/atomic_commit.rs` (the file stays hand-editable). `DocumentConfigSource` is the `config/{scope}` document on a shared (multi-tenant) storage backend, used by `Config::save`, snapshot reloads (`load_from_config_path`) and `load_or_init`. A process boots before its backend is installed, so its first load normally reads the file. A document that does not parse is an error (no file-style recovery, which would touch the bootstrap file). Bootstrap tables (`[storage]`) are never written to a document and are always re-applied from the file on a document read. `SaasConfig` is not part of this.
diff --git a/crates/openhuman-core/src/config/schema/load/source/file.rs b/crates/openhuman-core/src/config/schema/load/source/file.rs
index a314e7e42b..9f0c682b18 100644
--- a/crates/openhuman-core/src/config/schema/load/source/file.rs
+++ b/crates/openhuman-core/src/config/schema/load/source/file.rs
@@ -142,8 +142,7 @@ impl ConfigSource for FileConfigSource {
// `commit_replacement` owns the swap, and returns `Err` only while the
// old config is still in place — see its docs for why callers that roll
// back on `Err` depend on that.
- let committed =
- commit_replacement(&temp_path, config_path, parent_dir, &backup_path).await;
+ let committed = commit_replacement(&temp_path, config_path, parent_dir, &backup_path).await;
if committed.is_err() {
// The helper removes the staged file after a failed rename but can
// return earlier (directory recreation); either way it must not be
diff --git a/crates/openhuman-core/src/config/schema/load/source/mod.rs b/crates/openhuman-core/src/config/schema/load/source/mod.rs
index 58de088611..ea054f9393 100644
--- a/crates/openhuman-core/src/config/schema/load/source/mod.rs
+++ b/crates/openhuman-core/src/config/schema/load/source/mod.rs
@@ -15,8 +15,9 @@
//! The `[storage]` table decides which backend exists, so it cannot be read
//! from one (and its URL can carry a database password). [`BOOTSTRAP_TABLES`]
//! are therefore stripped before a document is written and re-applied from the
-//! file on every document read. The first load of a process (`load_or_init`)
-//! always reads the file: it runs before any backend is installed.
+//! file on every document read. A process boots before its backend is installed,
+//! so its first load (`load_or_init`) normally reads the file; nothing
+//! enforces that ordering.
//!
//! # Why text, not a JSON tree
//!
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 2a619f35b8..1035ff592a 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
@@ -340,3 +340,64 @@ fn forced_sources_nest_and_restore() {
drop(outer);
assert!(tests_support::forced_document_scope().is_none());
}
+
+#[tokio::test]
+async fn a_present_but_malformed_document_is_an_error_not_absence() {
+ let tmp = tempfile::tempdir().unwrap();
+ let file = tmp.path().join("config.toml");
+ std::fs::write(&file, "default_model = \"stale-file\"\n").unwrap();
+ let storage = MemoryStorage::new();
+ let source = document_source(&storage, "alice", &file);
+ let docs = Arc::clone(scoped(&storage, "alice").documents());
+ docs.put(
+ "config",
+ "alice",
+ serde_json::json!({ "toml_enc": 123 }),
+ tinystoragedrivers::Precondition::None,
+ )
+ .await
+ .unwrap();
+ assert!(source.exists().await.unwrap());
+ assert!(
+ source.read().await.is_err(),
+ "must not fall back to the file"
+ );
+}
+
+#[tokio::test]
+async fn a_document_that_does_not_parse_is_an_error_and_leaves_the_bootstrap_file_alone() {
+ let tmp = tempfile::tempdir().unwrap();
+ let storage = MemoryStorage::new();
+ let _forced = ForcedDocumentSource::new(
+ scoped(&storage, "tenant-a"),
+ Scope::new("tenant-a").unwrap(),
+ keys(3),
+ );
+ let config = config_at(tmp.path(), "m");
+ let docs = Arc::clone(scoped(&storage, "tenant-a").documents());
+ let key = keys(3).data_key(&Scope::new("tenant-a").unwrap()).unwrap();
+ let sealed = crypto::encrypt_enc2(&key, b"default_model = 5\n").unwrap();
+ docs.put(
+ "config",
+ "tenant-a",
+ serde_json::json!({ "toml_enc": sealed }),
+ tinystoragedrivers::Precondition::None,
+ )
+ .await
+ .unwrap();
+ let bootstrap = "[storage]\nurl = \"sqlite:/b\"\n";
+ std::fs::write(&config.config_path, bootstrap).unwrap();
+
+ let error = Config::load_from_config_path(&config.config_path, &config.workspace_dir)
+ .await
+ .unwrap_err();
+ assert!(
+ format!("{error:#}").contains("could not be parsed"),
+ "{error:#}"
+ );
+ assert_eq!(
+ std::fs::read_to_string(&config.config_path).unwrap(),
+ bootstrap
+ );
+ assert!(!tmp.path().join("config.toml.bak").exists());
+}