Skip to content

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

chore: files changed crates/openhuman-core/src/memory/state_store.rs,crates/openhuman-core/src/stora

Steven Enamakel4f8179f3084c
diff --git a/crates/openhuman-core/src/memory/state_store.rs b/crates/openhuman-core/src/memory/state_store.rs
index 8f10ba609b..42ab339ccc 100644
--- a/crates/openhuman-core/src/memory/state_store.rs
+++ b/crates/openhuman-core/src/memory/state_store.rs
@@ -31,6 +31,12 @@ fn open(workspace_dir: &Path, name: &str) -> Result<StateKv> {
Ok(kv)
}
+fn open_quarantining<T: DeserializeOwned>(workspace_dir: &Path, name: &str) -> Result<StateKv> {
+ let kv = StateKv::open(workspace_dir, OWNER, NS)?;
+ kv.import_file_quarantining::<T>(name, &legacy_path(workspace_dir, name))?;
+ Ok(kv)
+}
+
/// [`open`] for a value a damaged legacy file must not reset.
fn open_strict(workspace_dir: &Path, name: &str) -> Result<StateKv> {
let kv = StateKv::open(workspace_dir, OWNER, NS)?;
@@ -98,7 +104,7 @@ pub(crate) fn load<T: DeserializeOwned + Send + 'static>(
workspace_dir: &Path,
name: &str,
) -> Result<Option<T>> {
- open(workspace_dir, name)?.get(name)
+ open_quarantining::<T>(workspace_dir, name)?.get(name)
}
/// The names of the state values starting with `prefix`: those stored, plus
diff --git a/crates/openhuman-core/src/storage/state_kv.rs b/crates/openhuman-core/src/storage/state_kv.rs
index 68ab6b06f0..80edf00c12 100644
--- a/crates/openhuman-core/src/storage/state_kv.rs
+++ b/crates/openhuman-core/src/storage/state_kv.rs
@@ -228,6 +228,78 @@ impl StateKv {
self.import_file_with(name, legacy, false)
}
+ /// Imports a typed legacy JSON value once, preserving invalid input beside
+ /// the source as `<name>.json.corrupt` so the state port can start fresh.
+ /// Existing generic and strict import paths keep their original behavior.
+ ///
+ /// # Errors
+ ///
+ /// A storage error, a legacy read error, or a failure to quarantine an
+ /// invalid legacy file.
+ pub fn import_file_quarantining<T: DeserializeOwned>(
+ &self,
+ name: &str,
+ legacy: &Path,
+ ) -> Result<()> {
+ self.import_once(name, || {
+ match std::fs::read(legacy) {
+ Ok(bytes) if bytes.iter().all(u8::is_ascii_whitespace) => {}
+ Ok(bytes) => match serde_json::from_slice::<Value>(&bytes) {
+ Ok(value) if serde_json::from_value::<T>(value.clone()).is_ok() => {
+ let wrote = self.insert_if_absent(name, &value)?;
+ log::debug!(
+ "[storage::state_kv] imported {}/{}/{name} (written={wrote})",
+ self.owner,
+ self.ns
+ );
+ }
+ Ok(_) => log::warn!(
+ "[storage::state_kv] legacy {}/{}/{name} has the wrong value type; quarantining",
+ self.owner,
+ self.ns
+ ),
+ Err(error) => log::warn!(
+ "[storage::state_kv] legacy {}/{}/{name} is not JSON ({error}); quarantining",
+ self.owner,
+ self.ns
+ ),
+ },
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
+ Err(error) => {
+ return Err(error)
+ .with_context(|| format!("read the legacy state file {}", legacy.display()));
+ }
+ }
+ Ok(())
+ })?;
+ self.quarantine_invalid_legacy::<T>(legacy)
+ }
+
+ fn quarantine_invalid_legacy<T: DeserializeOwned>(&self, legacy: &Path) -> Result<()> {
+ let bytes = match std::fs::read(legacy) {
+ Ok(bytes) => bytes,
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
+ Err(error) => {
+ return Err(error)
+ .with_context(|| format!("read the legacy state file {}", legacy.display()));
+ }
+ };
+ if bytes.iter().all(u8::is_ascii_whitespace)
+ || serde_json::from_slice::<Value>(&bytes)
+ .and_then(|value| serde_json::from_value::<T>(value))
+ .is_ok()
+ {
+ return Ok(());
+ }
+
+ let backup = legacy.with_extension("json.corrupt");
+ if backup.exists() {
+ anyhow::bail!("the corrupt legacy backup {} already exists", backup.display());
+ }
+ std::fs::rename(legacy, &backup)
+ .with_context(|| format!("quarantine corrupt legacy state {}", legacy.display()))
+ }
+
/// [`Self::import_file`] for a value that must not start over from a
/// damaged file: an unparsable file is an error and is not recorded, so
/// every read fails closed until the file is repaired or removed.