Skip to content

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

fix(memory): recover from corrupt state files and write atomically

Steven Enamakel7452aaba117e
diff --git a/crates/openhuman-core/src/memory/channels.rs b/crates/openhuman-core/src/memory/channels.rs
index d9624e2ea4..32b51e1731 100644
--- a/crates/openhuman-core/src/memory/channels.rs
+++ b/crates/openhuman-core/src/memory/channels.rs
@@ -25,17 +25,27 @@ fn path(workspace_dir: &Path) -> PathBuf {
}
fn read(workspace_dir: &Path) -> BTreeMap<String, BTreeSet<String>> {
- std::fs::read_to_string(path(workspace_dir))
- .ok()
- .and_then(|text| serde_json::from_str(&text).ok())
- .unwrap_or_default()
+ let file = path(workspace_dir);
+ let Ok(text) = std::fs::read_to_string(&file) else {
+ return BTreeMap::new();
+ };
+ serde_json::from_str(&text).unwrap_or_else(|error| {
+ tracing::warn!(%error, "[memory:channels] channel threads unparsable; set aside");
+ if let Err(error) = std::fs::rename(&file, file.with_extension("json.corrupt")) {
+ tracing::warn!(%error, "[memory:channels] could not set channel threads file aside");
+ }
+ BTreeMap::new()
+ })
}
fn write(workspace_dir: &Path, all: &BTreeMap<String, BTreeSet<String>>) {
let file = path(workspace_dir);
let result = serde_json::to_vec_pretty(all)
.map_err(std::io::Error::other)
- .and_then(|json| crate::memory::files::write_private(&file, &json));
+ .and_then(|json| {
+ crate::security::secret_storage::file_store::write_atomic(&file, &json)
+ .map_err(std::io::Error::other)
+ });
if let Err(error) = result {
tracing::warn!(%error, "[memory:channels] writing channel threads failed");
}
diff --git a/crates/openhuman-core/src/memory/lifecycle/jobs.rs b/crates/openhuman-core/src/memory/lifecycle/jobs.rs
index d454a29df9..7b6467178e 100644
--- a/crates/openhuman-core/src/memory/lifecycle/jobs.rs
+++ b/crates/openhuman-core/src/memory/lifecycle/jobs.rs
@@ -52,17 +52,26 @@ struct FileQueueStore {
impl QueueStore for FileQueueStore {
fn load(&self) -> QueueState {
- std::fs::read_to_string(&self.file)
- .ok()
- .and_then(|text| serde_json::from_str(&text).ok())
- .unwrap_or_default()
+ let Ok(text) = std::fs::read_to_string(&self.file) else {
+ return QueueState::default();
+ };
+ serde_json::from_str(&text).unwrap_or_else(|error| {
+ tracing::warn!(%error, "[memory:jobs] queue unparsable; set aside");
+ if let Err(error) = std::fs::rename(
+ &self.file,
+ self.file.with_extension("json.corrupt"),
+ ) {
+ tracing::warn!(%error, "[memory:jobs] could not set queue file aside");
+ }
+ QueueState::default()
+ })
}
fn save(&self, state: &QueueState) -> Result<(), String> {
serde_json::to_vec_pretty(state)
.map_err(|error| error.to_string())
.and_then(|json| {
- crate::memory::files::write_private(&self.file, &json)
+ crate::security::secret_storage::file_store::write_atomic(&self.file, &json)
.map_err(|error| error.to_string())
})
}
diff --git a/crates/openhuman-core/src/memory/sources/state.rs b/crates/openhuman-core/src/memory/sources/state.rs
index 9940567d68..6b9d15f698 100644
--- a/crates/openhuman-core/src/memory/sources/state.rs
+++ b/crates/openhuman-core/src/memory/sources/state.rs
@@ -39,17 +39,27 @@ fn path(workspace_dir: &Path) -> PathBuf {
}
fn read_all(workspace_dir: &Path) -> BTreeMap<String, SourceState> {
- std::fs::read_to_string(path(workspace_dir))
- .ok()
- .and_then(|text| serde_json::from_str(&text).ok())
- .unwrap_or_default()
+ let file = path(workspace_dir);
+ let Ok(text) = std::fs::read_to_string(&file) else {
+ return BTreeMap::new();
+ };
+ serde_json::from_str(&text).unwrap_or_else(|error| {
+ tracing::warn!(%error, "[memory:sources] sync state unparsable; set aside");
+ if let Err(error) = std::fs::rename(&file, file.with_extension("json.corrupt")) {
+ tracing::warn!(%error, "[memory:sources] could not set sync state file aside");
+ }
+ BTreeMap::new()
+ })
}
fn write_all(workspace_dir: &Path, all: &BTreeMap<String, SourceState>) {
let file = path(workspace_dir);
let result = serde_json::to_vec_pretty(all)
.map_err(std::io::Error::other)
- .and_then(|json| crate::memory::files::write_private(&file, &json));
+ .and_then(|json| {
+ crate::security::secret_storage::file_store::write_atomic(&file, &json)
+ .map_err(std::io::Error::other)
+ });
if let Err(error) = result {
tracing::warn!(error = %error, "[memory:sources] writing sync state failed");
}