Skip to content

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

fix(agent): skip startup run sweep on shared storage backends

Steven Enamakele9319427cde2
diff --git a/crates/openhuman-core/src/agent/tinyagents/reaper.rs b/crates/openhuman-core/src/agent/tinyagents/reaper.rs
index 82b91d91f5..266cf6c8c5 100644
--- a/crates/openhuman-core/src/agent/tinyagents/reaper.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/reaper.rs
@@ -13,7 +13,19 @@ use tinyagents_session::transcript::import::ops::open_session_stores;
/// Reap every run left non-terminal by a previous process, returning the number
/// of runs moved to `Cancelled`. Best-effort; never blocks boot.
+///
+/// Skipped on a storage backend other processes may share (MongoDB): there a
+/// non-terminal status can belong to a run another replica is still driving,
+/// and cancelling it would hide that run from active and late-attach status.
pub(crate) async fn reap_orphaned_runs(workspace: &Path) -> usize {
+ reap_unless_shared(workspace, crate::storage::installed_is_shared()).await
+}
+
+async fn reap_unless_shared(workspace: &Path, shared: bool) -> usize {
+ if shared {
+ log::info!("[agent] startup run sweep skipped: the storage backend is shared");
+ return 0;
+ }
log::debug!(
"[agent] startup run sweep workspace={}",
workspace.display()
diff --git a/crates/openhuman-core/src/agent/tinyagents/reaper_tests.rs b/crates/openhuman-core/src/agent/tinyagents/reaper_tests.rs
index f483b3b1dd..bf150571a9 100644
--- a/crates/openhuman-core/src/agent/tinyagents/reaper_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/reaper_tests.rs
@@ -35,6 +35,19 @@ async fn workspace_adapter_reaps_and_empty_workspace_is_a_noop() {
let _ = std::fs::remove_dir_all(&tmp);
}
+/// A shared backend never reaps: the run may belong to another replica.
+#[tokio::test]
+async fn a_shared_backend_leaves_running_work_alone() {
+ let tmp = std::env::temp_dir().join(format!("oh-reaper-{}", uuid::Uuid::new_v4()));
+ let store = FileStatusStore::new(open_session_stores(&tmp).kv);
+ let run = seed_status(&store, ExecutionStatus::Running).await;
+ assert_eq!(reap_unless_shared(&tmp, true).await, 0);
+ let status = store.get_status(&run).await.unwrap().expect("present");
+ assert_eq!(status.status, ExecutionStatus::Running);
+ assert_eq!(reap_unless_shared(&tmp, false).await, 1);
+ let _ = std::fs::remove_dir_all(&tmp);
+}
+
/// The sweep has to happen on the path a **build-only** embedder takes.
///
/// `CoreRuntime::invoke` dispatches `openhuman.agent_runs_active` the moment
diff --git a/crates/openhuman-core/src/storage/mod.rs b/crates/openhuman-core/src/storage/mod.rs
index a603f9a17c..0e8f26f4ea 100644
--- a/crates/openhuman-core/src/storage/mod.rs
+++ b/crates/openhuman-core/src/storage/mod.rs
@@ -91,6 +91,22 @@ pub fn clear() -> bool {
.is_some()
}
+/// Whether a backend with this driver name may be shared by several
+/// processes at once. A MongoDB database can be; SQLite files, the memory
+/// driver and plain files belong to the one process that opened them.
+///
+/// Boot-time recovery (interrupting in-flight turns, reaping orphaned runs)
+/// is only sound on a backend no other process can be writing to.
+pub fn driver_is_shared(driver: &str) -> bool {
+ driver == "mongodb"
+}
+
+/// Whether the installed backend may be shared with other processes; `false`
+/// when none is installed. See [`driver_is_shared`].
+pub fn installed_is_shared() -> bool {
+ installed().is_some_and(|backend| driver_is_shared(backend.driver()))
+}
+
/// The storage scope agent `agent_id`'s records live under — the same
/// mapping the session store uses, so every domain agrees on it.
pub fn scope_for_agent(agent_id: &str) -> Scope {
diff --git a/crates/openhuman-core/src/storage/mod_tests.rs b/crates/openhuman-core/src/storage/mod_tests.rs
index a9535025e4..c9e2b8e1c1 100644
--- a/crates/openhuman-core/src/storage/mod_tests.rs
+++ b/crates/openhuman-core/src/storage/mod_tests.rs
@@ -57,3 +57,11 @@ fn agent_scopes_match_the_session_store() {
.as_str()
.starts_with("sha256:"));
}
+
+#[test]
+fn only_mongodb_is_shared_between_processes() {
+ assert!(driver_is_shared("mongodb"));
+ for driver in ["sqlite", "memory", "file"] {
+ assert!(!driver_is_shared(driver), "{driver}");
+ }
+}
diff --git a/crates/openhuman-rpc/src/session_store/mod.rs b/crates/openhuman-rpc/src/session_store/mod.rs
index 2f9d161f44..a65ef0bb3e 100644
--- a/crates/openhuman-rpc/src/session_store/mod.rs
+++ b/crates/openhuman-rpc/src/session_store/mod.rs
@@ -197,7 +197,7 @@ pub async fn install_for_url(url: Option<String>) -> anyhow::Result<()> {
let backend = openhuman_core::storage::open(&url)
.await
.context("opening the configured storage backend")?;
- let single_process = backend.driver() != "mongodb";
+ let single_process = !openhuman_core::storage::driver_is_shared(backend.driver());
let provider = tinyagents_session::DriverSessionStores::new(Arc::clone(&backend))
.context("starting the session store bridge")?
.recover_on_open(single_process);