Skip to content

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

fix(config): a second corruption keeps the first corrupted copy

M3gA-Mindfbe095770a93
diff --git a/crates/openhuman-core/src/config/schema/load/branches.rs b/crates/openhuman-core/src/config/schema/load/branches.rs
index 9019c5e7c2..8dd0cec966 100644
--- a/crates/openhuman-core/src/config/schema/load/branches.rs
+++ b/crates/openhuman-core/src/config/schema/load/branches.rs
@@ -199,7 +199,14 @@ impl Config {
);
}
} else {
- let corrupted_path = config_path.with_extension("toml.corrupted");
+ // Keep an earlier corrupted copy: `rename` would replace it.
+ let mut corrupted_path = config_path.with_extension("toml.corrupted");
+ if tokio::fs::try_exists(&corrupted_path).await.unwrap_or(true) {
+ let ts = std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .map_or(0, |d| d.as_secs());
+ corrupted_path = config_path.with_extension(format!("toml.corrupted.{ts}"));
+ }
match fs::rename(&config_path, &corrupted_path).await {
Ok(()) => {
tracing::debug!(
diff --git a/crates/openhuman-core/src/config/schema/load_corruption_recovery_tests.rs b/crates/openhuman-core/src/config/schema/load_corruption_recovery_tests.rs
index a7b9f23e61..944406738c 100644
--- a/crates/openhuman-core/src/config/schema/load_corruption_recovery_tests.rs
+++ b/crates/openhuman-core/src/config/schema/load_corruption_recovery_tests.rs
@@ -396,6 +396,36 @@ async fn load_or_init_falls_back_to_defaults_when_no_backup() {
);
}
+#[tokio::test]
+async fn a_second_corruption_keeps_the_first_corrupted_copy() {
+ let tmp = tempfile::tempdir().unwrap();
+ let root = tmp.path();
+ let first = root.join("config.toml.corrupted");
+ write_file(&first, "first corrupted copy").await;
+ write_file(&root.join("config.toml"), CORRUPTED_TOML).await;
+
+ load_or_init_for_workspace(root).await;
+
+ assert_eq!(
+ tokio::fs::read_to_string(&first).await.unwrap(),
+ "first corrupted copy",
+ "the earlier corrupted copy must not be overwritten"
+ );
+ let mut later = Vec::new();
+ let mut entries = tokio::fs::read_dir(root).await.unwrap();
+ while let Some(e) = entries.next_entry().await.unwrap() {
+ let name = e.file_name().to_string_lossy().into_owned();
+ if name.starts_with("config.toml.corrupted.") {
+ later.push(tokio::fs::read_to_string(e.path()).await.unwrap());
+ }
+ }
+ assert_eq!(
+ later,
+ vec![CORRUPTED_TOML.to_string()],
+ "the new corruption gets its own copy"
+ );
+}
+
#[tokio::test]
async fn load_or_init_does_not_trigger_recovery_on_valid_config() {
let tmp = tempfile::tempdir().unwrap();