Skip to content

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

refactor(config): split config load sources into submodules

Steven Enamakel01a1e171607a
diff --git a/crates/openhuman-core/src/config/schema/load/branches.rs b/crates/openhuman-core/src/config/schema/load/branches.rs
index d1a17eb9c9..fccf0fc8eb 100644
--- a/crates/openhuman-core/src/config/schema/load/branches.rs
+++ b/crates/openhuman-core/src/config/schema/load/branches.rs
@@ -143,10 +143,7 @@ impl Config {
// `.corrupted.<timestamp>` and backup/defaults are attempted,
// with rate-limited error logging (#5167).
let source = super::source::for_config(&config_path)?;
- let super::source::ConfigRead {
- contents,
- recovered: read_was_recovered,
- } = source.read().await?;
+ let read = source.read().await?;
// When `read_config_with_recovery_or_default` returned an empty
// string (both primary and backup were unreadable), skip the TOML
@@ -154,13 +151,13 @@ impl Config {
// otherwise parse successfully with serde defaults (all fields at
// their `Option::None` / `vec![]` / `false` values) instead of
// the richer `Default` impl (issue #5167).
- let (config, config_was_corrupted) = super::impl_load::parse_source_contents(
- source.as_ref(),
- &config_path,
- &contents,
- read_was_recovered,
- )
- .await?;
+ let (config, config_was_corrupted) =
+ super::impl_load::parse_source_contents(&read, &config_path).await?;
+ let super::source::ConfigRead {
+ contents,
+ recovered: read_was_recovered,
+ ..
+ } = read;
let mut config = config;
// If the read itself was recovered (non-UTF-8 file renamed, backup
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 420c9d1933..3aaa718410 100644
--- a/crates/openhuman-core/src/config/schema/load/impl_load.rs
+++ b/crates/openhuman-core/src/config/schema/load/impl_load.rs
@@ -146,12 +146,15 @@ pub(super) async fn read_config_with_recovery_or_default(
/// node-local bootstrap `config.toml.bak`, rename the bootstrap file, and save
/// stale defaults over the shared document.
pub(super) async fn parse_source_contents(
- source: &dyn super::source::ConfigSource,
+ read: &super::source::ConfigRead,
config_path: &Path,
- contents: &str,
- read_was_recovered: bool,
) -> Result<(Box<Config>, bool)> {
- if source.encrypts_body() {
+ let contents = read.contents.as_str();
+ let read_was_recovered = read.recovered;
+ // By the text's origin, not the source's kind: a document source that
+ // fell back to the file (no document for the scope yet) returns file text,
+ // which keeps the file's `.bak` recovery.
+ if read.from_document {
let config = parse_toml_off_worker(contents.to_string())
.await
.map_err(|error| {
@@ -338,7 +341,15 @@ impl Config {
let config_path = openhuman_dir.join("config.toml");
- if resolution_source == ConfigResolutionSource::DefaultConfigDir && !config_path.exists() {
+ // With a shared backend the scope's document is the config even when
+ // this node has no local file: returning pre-login defaults here would
+ // let the next settings save overwrite that document.
+ let source = super::source::for_config(&config_path)?;
+ let document_exists = source.label() == "document" && source.exists().await?;
+ if resolution_source == ConfigResolutionSource::DefaultConfigDir
+ && !config_path.exists()
+ && !document_exists
+ {
let config = pre_login_config_boxed(config_path.clone(), workspace_dir.clone(), env);
tracing::debug!(
@@ -367,7 +378,7 @@ impl Config {
// shared backend and a scope are installed. A process boots before its
// backend is installed, so its first load normally reads the file (the
// bootstrap config); nothing here enforces that ordering.
- if super::source::for_config(&config_path)?.exists().await? {
+ if document_exists || source.exists().await? {
Box::pin(Self::load_existing_config(
openhuman_dir,
workspace_dir,
@@ -470,12 +481,13 @@ impl Config {
);
}
+ let read = Box::pin(source.read()).await?;
+ let (config, config_was_corrupted) = parse_source_contents(&read, &config_path).await?;
let super::source::ConfigRead {
contents: raw,
recovered: read_was_recovered,
- } = Box::pin(source.read()).await?;
- let (config, config_was_corrupted) =
- parse_source_contents(source.as_ref(), &config_path, &raw, read_was_recovered).await?;
+ ..
+ } = read;
let mut config = *config;
let config_was_corrupted = config_was_corrupted || read_was_recovered;
config.config_path = config_path.clone();
@@ -517,7 +529,18 @@ impl Config {
super::super::cli_overrides::restore_persisted_inference_fields(&mut config_to_save);
// A document source seals the whole body under the scope's data key;
// the process-local field key would make it unreadable on another node.
- if !source.encrypts_body() {
+ // A config seeded from the local file (or saved by a migration before
+ // the load decrypted it) can still hold process-key `enc2:` fields, so
+ // they are opened first: the document must never nest node-local
+ // ciphertext.
+ if source.encrypts_body() {
+ let openhuman_dir = config_to_save
+ .config_path
+ .parent()
+ .context("Config path must have a parent directory")?
+ .to_path_buf();
+ decrypt_config_secrets(&mut config_to_save, &openhuman_dir)?;
+ } else {
encrypt_config_secrets(&mut config_to_save)?;
}
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 189d0d0184..74ef4afa01 100644
--- a/crates/openhuman-core/src/config/schema/load/source/document.rs
+++ b/crates/openhuman-core/src/config/schema/load/source/document.rs
@@ -59,6 +59,40 @@ impl DocumentConfigSource {
.map_err(|error| anyhow!("no data key for the config document: {error}"))
}
+ /// The local file's text for its bootstrap tables.
+ ///
+ /// When the file read had to recover (non-UTF-8 content renamed aside, the
+ /// `.bak` used), the recovered text is written back to the file: saves go
+ /// to the document, so nothing else would ever recreate it, and a restart
+ /// whose storage URL lives only in the file's `[storage]` table would boot
+ /// without its shared backend. Nothing recoverable means no bootstrap
+ /// tables, the same as an absent file (the corrupt bytes stay preserved
+ /// beside it).
+ async fn read_bootstrap_file(&self) -> Result<String> {
+ let read = self.file.read().await?;
+ if read.recovered {
+ if read.contents.is_empty() {
+ tracing::error!(
+ path = %self.file.path().display(),
+ "[config] bootstrap config file unreadable and no backup; \
+ reading the config document without bootstrap tables"
+ );
+ } else if let Err(error) = self.file.write(&read.contents).await {
+ tracing::warn!(
+ path = %self.file.path().display(),
+ error = %error,
+ "[config] could not restore the recovered bootstrap config file"
+ );
+ } else {
+ tracing::warn!(
+ path = %self.file.path().display(),
+ "[config] bootstrap config file was corrupt; restored it from its backup"
+ );
+ }
+ }
+ Ok(read.contents)
+ }
+
/// The decrypted stored body, or `None` when the scope has no document.
async fn stored_body(&self) -> Result<Option<zeroize::Zeroizing<String>>> {
self.docs
@@ -128,15 +162,21 @@ impl ConfigSource for DocumentConfigSource {
// Through the file source, so its corruption recovery (a `.bak`, a
// non-UTF-8 file renamed aside) applies to the bootstrap read too.
let file_text = if self.file.exists().await? {
- Some(self.file.read().await?.contents)
+ Some(self.read_bootstrap_file().await?)
} else {
None
};
let contents = apply_bootstrap(&body, file_text.as_deref())
.context("apply the bootstrap tables to the config document")?;
+ // The document body itself was read intact. A recovered bootstrap file
+ // is handled (and restored) by `read_bootstrap_file`, not reported
+ // 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 {
contents,
recovered: false,
+ from_document: true,
})
}
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 9f0c682b18..07d2703d2e 100644
--- a/crates/openhuman-core/src/config/schema/load/source/file.rs
+++ b/crates/openhuman-core/src/config/schema/load/source/file.rs
@@ -51,6 +51,7 @@ impl ConfigSource for FileConfigSource {
Ok(ConfigRead {
contents,
recovered,
+ from_document: false,
})
}
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 ea054f9393..3cf3bf25e7 100644
--- a/crates/openhuman-core/src/config/schema/load/source/mod.rs
+++ b/crates/openhuman-core/src/config/schema/load/source/mod.rs
@@ -49,6 +49,10 @@ pub(crate) struct ConfigRead {
pub contents: String,
/// The source recovered from corruption (a `.bak`, or defaults).
pub recovered: bool,
+ /// The text is a stored config document's body. `false` for a file,
+ /// including the file a document source falls back to when the scope has
+ /// no document yet: that text keeps the file's corruption recovery.
+ pub from_document: bool,
}
/// A place a config's TOML text is read from and written to.
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 1035ff592a..a74caee709 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
@@ -1,5 +1,5 @@
-use super::tests_support::ForcedDocumentSource;
use super::*;
+use super::tests_support::ForcedDocumentSource;
use crate::config::Config;
use crate::storage::{MemoryStorage, Scope, ScopedStorage, StorageBackend};
use std::sync::Arc;