Skip to content

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

refactor(memory): move state kv store into storage module

Steven Enamakelb281c8e09f2b
diff --git a/crates/openhuman-core/src/memory/layout_migration/state.rs b/crates/openhuman-core/src/memory/layout_migration/state.rs
index c8e9c9421f..6a24ab60f4 100644
--- a/crates/openhuman-core/src/memory/layout_migration/state.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/state.rs
@@ -18,7 +18,7 @@ const STATE: &str = "layout_migration";
/// begun there.
#[must_use]
pub fn exists(workspace_dir: &Path) -> bool {
- crate::memory::state_store::load::<serde_json::Value>(workspace_dir, STATE)
+ crate::memory::state_store::load_strict::<serde_json::Value>(workspace_dir, STATE)
.map(|state| state.is_some())
.unwrap_or(false)
}
@@ -63,7 +63,7 @@ fn detail(error: MemoryError) -> String {
/// A state that cannot be read or parsed: the migration must not start
/// over and copy everything again on a damaged value.
pub fn load(workspace_dir: &Path) -> MemoryResult<MigrationState> {
- crate::memory::state_store::load(workspace_dir, STATE)
+ crate::memory::state_store::load_strict(workspace_dir, STATE)
.map(Option::unwrap_or_default)
.map_err(|error| {
MemoryError::Engine(format!("layout migration state unreadable: {error:#}"))
diff --git a/crates/openhuman-core/src/memory/local_root.rs b/crates/openhuman-core/src/memory/local_root.rs
index 259ede8ea5..77c41758db 100644
--- a/crates/openhuman-core/src/memory/local_root.rs
+++ b/crates/openhuman-core/src/memory/local_root.rs
@@ -156,7 +156,7 @@ fn valid(record: &LocalRoot) -> std::io::Result<()> {
}
fn read(workspace_dir: &Path) -> anyhow::Result<Option<LocalRoot>> {
- let record: Option<LocalRoot> = super::state_store::load(workspace_dir, STATE)?;
+ let record: Option<LocalRoot> = super::state_store::load_strict(workspace_dir, STATE)?;
if let Some(record) = &record {
valid(record)?;
}
diff --git a/crates/openhuman-core/src/memory/state_store.rs b/crates/openhuman-core/src/memory/state_store.rs
index d44b7e8f64..028ec29aef 100644
--- a/crates/openhuman-core/src/memory/state_store.rs
+++ b/crates/openhuman-core/src/memory/state_store.rs
@@ -31,6 +31,26 @@ fn open(workspace_dir: &Path, name: &str) -> Result<StateKv> {
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)?;
+ kv.import_file_strict(name, &legacy_path(workspace_dir, name))?;
+ Ok(kv)
+}
+
+/// [`load`], failing on a damaged legacy file instead of starting over.
+///
+/// # Errors
+///
+/// A storage error, an unparsable legacy file, or a stored value that is not
+/// a `T`.
+pub(crate) fn load_strict<T: DeserializeOwned + Send + 'static>(
+ workspace_dir: &Path,
+ name: &str,
+) -> Result<Option<T>> {
+ open_strict(workspace_dir, name)?.get(name)
+}
+
/// State value `name`, or `None` when none was stored.
///
/// # Errors
@@ -70,7 +90,7 @@ pub(crate) fn record_once<T>(workspace_dir: &Path, name: &str, value: &T) -> Res
where
T: Serialize + DeserializeOwned + Clone + Send + 'static,
{
- let kv = open(workspace_dir, name)?;
+ let kv = open_strict(workspace_dir, name)?;
if kv.insert_if_absent(name, value)? {
return Ok(value.clone());
}
diff --git a/crates/openhuman-core/src/storage/state_kv.rs b/crates/openhuman-core/src/storage/state_kv.rs
index a1ea36fdb0..b6550be94e 100644
--- a/crates/openhuman-core/src/storage/state_kv.rs
+++ b/crates/openhuman-core/src/storage/state_kv.rs
@@ -225,6 +225,21 @@ impl StateKv {
///
/// A storage error.
pub fn import_file(&self, name: &str, legacy: &Path) -> Result<()> {
+ self.import_file_with(name, legacy, false)
+ }
+
+ /// [`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.
+ ///
+ /// # Errors
+ ///
+ /// A storage error, or an unparsable legacy file.
+ pub fn import_file_strict(&self, name: &str, legacy: &Path) -> Result<()> {
+ self.import_file_with(name, legacy, true)
+ }
+
+ fn import_file_with(&self, name: &str, legacy: &Path, strict: bool) -> Result<()> {
let step = import_state::step_id(IMPORT_KIND, &format!("{}/{}/{name}", self.owner, self.ns));
let key = format!("{}/{}/{name}", self.owner, self.ns);
self.repo.once_per_backend(&key, || {
@@ -246,6 +261,11 @@ impl StateKv {
self.ns
);
}
+ Err(error) if strict => {
+ return Err(error).with_context(|| {
+ format!("the legacy state file {} is not JSON", legacy.display())
+ });
+ }
Err(error) => log::warn!(
"[storage::state_kv] legacy {}/{}/{name} is not JSON ({error}); skipped",
self.owner,