Skip to content

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

feat(memory): add channel lifecycle job tracking

Steven Enamakel21490d389833
diff --git a/crates/openhuman-core/src/memory/channels.rs b/crates/openhuman-core/src/memory/channels.rs
index d9624e2ea4..b20906a337 100644
--- a/crates/openhuman-core/src/memory/channels.rs
+++ b/crates/openhuman-core/src/memory/channels.rs
@@ -20,23 +20,19 @@ use super::error::{MemoryError, MemoryResult};
/// Serialises read-modify-writes of the file in this process.
static LOCK: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
-fn path(workspace_dir: &Path) -> PathBuf {
- workspace_dir.join("memory").join("channel_threads.json")
-}
+const STATE: &str = "channel_threads";
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())
+ super::state_store::load(workspace_dir, STATE)
+ .unwrap_or_else(|error| {
+ tracing::warn!(%error, "[memory:channels] reading channel threads failed");
+ None
+ })
.unwrap_or_default()
}
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));
- if let Err(error) = result {
+ if let Err(error) = super::state_store::save(workspace_dir, STATE, all) {
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..f56e97fca7 100644
--- a/crates/openhuman-core/src/memory/lifecycle/jobs.rs
+++ b/crates/openhuman-core/src/memory/lifecycle/jobs.rs
@@ -40,31 +40,26 @@ pub const BACKGROUND_INTERVAL_MINS: u64 = 5;
/// The persisted queue as the UI reads it.
pub type JobQueue = QueueState;
-/// The queue file of a workspace.
-fn path(workspace_dir: &Path) -> PathBuf {
- workspace_dir.join("memory").join("jobs.json")
-}
+const STATE: &str = "jobs";
-/// A [`QueueStore`] over `<workspace>/memory/jobs.json`.
+/// A [`QueueStore`] over the workspace's `jobs` state value.
struct FileQueueStore {
- file: PathBuf,
+ workspace_dir: PathBuf,
}
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())
+ crate::memory::state_store::load(&self.workspace_dir, STATE)
+ .unwrap_or_else(|error| {
+ tracing::warn!(%error, "[memory:jobs] reading the queue failed");
+ None
+ })
.unwrap_or_default()
}
fn save(&self, state: &QueueState) -> Result<(), String> {
- serde_json::to_vec_pretty(state)
+ crate::memory::state_store::save(&self.workspace_dir, STATE, state)
.map_err(|error| error.to_string())
- .and_then(|json| {
- crate::memory::files::write_private(&self.file, &json)
- .map_err(|error| error.to_string())
- })
}
}
@@ -80,7 +75,7 @@ fn queue_of(workspace_dir: &Path) -> Arc<WorkspaceQueue> {
.entry(workspace_dir.to_path_buf())
.or_insert_with(|| {
Arc::new(Queue::new(FileQueueStore {
- file: path(workspace_dir),
+ workspace_dir: workspace_dir.to_path_buf(),
}))
})
.clone()
diff --git a/crates/openhuman-core/src/memory/mod.rs b/crates/openhuman-core/src/memory/mod.rs
index 78efe893e1..cc13844136 100644
--- a/crates/openhuman-core/src/memory/mod.rs
+++ b/crates/openhuman-core/src/memory/mod.rs
@@ -40,6 +40,7 @@ pub mod ops;
pub mod schemas;
pub mod scope;
pub mod sources;
+pub(crate) mod state_store;
pub mod status;
mod tool_budget;
pub(crate) mod tool_writes;
diff --git a/crates/openhuman-core/src/memory/sources/state.rs b/crates/openhuman-core/src/memory/sources/state.rs
index ec77123717..d32e301c9f 100644
--- a/crates/openhuman-core/src/memory/sources/state.rs
+++ b/crates/openhuman-core/src/memory/sources/state.rs
@@ -34,23 +34,19 @@ pub struct SourceState {
pub items: u64,
}
-fn path(workspace_dir: &Path) -> PathBuf {
- workspace_dir.join("memory").join("sources_state.json")
-}
+const STATE: &str = "sources_state";
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())
+ crate::memory::state_store::load(workspace_dir, STATE)
+ .unwrap_or_else(|error| {
+ tracing::warn!(error = %error, "[memory:sources] reading sync state failed");
+ None
+ })
.unwrap_or_default()
}
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));
- if let Err(error) = result {
+ if let Err(error) = crate::memory::state_store::save(workspace_dir, STATE, all) {
tracing::warn!(error = %error, "[memory:sources] writing sync state failed");
}
}