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/core/runtime/services.rs,crates/openhuman-core/src/st

Steven Enamakel32a94acedc66
diff --git a/crates/openhuman-core/src/core/runtime/services.rs b/crates/openhuman-core/src/core/runtime/services.rs
index 9d9d946050..634ef2d0ae 100644
--- a/crates/openhuman-core/src/core/runtime/services.rs
+++ b/crates/openhuman-core/src/core/runtime/services.rs
@@ -118,6 +118,15 @@ pub(crate) async fn start_selected_services(
}
if services.cron {
tasks.track("cron", spawn_cron_service(std::sync::Arc::clone(ctx)));
+ // Storage housekeeping rides the same switch as the other periodic
+ // maintenance: lapsed blobs stay in the backend until swept.
+ tasks.track(
+ "blob_sweep",
+ crate::storage::blob_sweep::spawn(
+ crate::storage::blob_sweep::FIRST_SWEEP_DELAY,
+ crate::storage::blob_sweep::SWEEP_INTERVAL,
+ ),
+ );
}
// Flow-run boot reconciliation is selected by the flows *domain*, not by
// a background service — runs can be started without cron in the
diff --git a/crates/openhuman-core/src/storage/blob_sweep.rs b/crates/openhuman-core/src/storage/blob_sweep.rs
new file mode 100644
index 0000000000..a58a1469ea
--- /dev/null
+++ b/crates/openhuman-core/src/storage/blob_sweep.rs
@@ -0,0 +1,103 @@
+//! Periodic reclamation of expired blobs.
+//!
+//! A blob written with a lifetime reads as absent once it lapses, but its
+//! bytes stay in the backend until something sweeps them
+//! (`StorageBackend::sweep_expired_blobs`, the trait-level hook the
+//! tinystoragedrivers drivers implement). The host owns the schedule: this
+//! module visits every storage scope background work covers
+//! ([`super::agents::for_each_scope`]) on a fixed tick and asks the installed
+//! backend to reclaim that scope's lapsed blobs.
+//!
+//! It is best-effort maintenance. A scope that fails to sweep is logged and
+//! retried on the next tick; nothing here blocks boot or a request.
+
+use std::sync::Arc;
+use std::time::Duration;
+
+use tinystoragedrivers::{Scope, StorageBackend};
+
+/// How often the sweeper wakes. Lapsed blobs are already invisible to readers,
+/// so this only bounds how long their bytes linger.
+pub const SWEEP_INTERVAL: Duration = Duration::from_secs(60 * 60);
+
+/// Delay before the first sweep, so boot work is not competing with it.
+pub const FIRST_SWEEP_DELAY: Duration = Duration::from_secs(5 * 60);
+
+/// Reclaims the lapsed blobs of `scope`, returning how many went. A failure
+/// is logged and counts as zero.
+pub async fn sweep_scope(backend: &dyn StorageBackend, scope: &Scope) -> u64 {
+ match backend.sweep_expired_blobs(scope).await {
+ Ok(removed) => {
+ if removed > 0 {
+ tracing::debug!(
+ scope = scope.as_str(),
+ removed,
+ "[storage::blob_sweep] reclaimed expired blobs"
+ );
+ }
+ removed
+ }
+ Err(error) => {
+ tracing::warn!(
+ scope = scope.as_str(),
+ %error,
+ "[storage::blob_sweep] sweep failed; retrying on the next tick"
+ );
+ 0
+ }
+ }
+}
+
+/// One pass over every scope background work covers (`local` outside SaaS
+/// mode, then each known agent), against `backend`. Returns the total number
+/// of blobs reclaimed.
+pub async fn sweep_all(backend: Arc<dyn StorageBackend>) -> u64 {
+ super::agents::for_each_scope("blob sweep", || {
+ let backend = Arc::clone(&backend);
+ async move {
+ match super::current_scope() {
+ Ok(scope) => sweep_scope(backend.as_ref(), &scope).await,
+ Err(error) => {
+ tracing::debug!(%error, "[storage::blob_sweep] no scope for this visit; skipped");
+ 0
+ }
+ }
+ }
+ })
+ .await
+ .into_iter()
+ .map(|(_, removed)| removed)
+ .sum()
+}
+
+/// [`sweep_all`] against the installed backend; zero when none is installed.
+pub async fn sweep_installed() -> u64 {
+ match super::installed() {
+ Some(backend) => sweep_all(backend).await,
+ None => 0,
+ }
+}
+
+/// Spawns the sweeper: after `first_delay`, then every `interval`, until the
+/// task is aborted. Missed ticks are skipped, not replayed.
+pub fn spawn(first_delay: Duration, interval: Duration) -> tokio::task::JoinHandle<()> {
+ tokio::spawn(async move {
+ tracing::debug!(
+ first_delay_secs = first_delay.as_secs(),
+ interval_secs = interval.as_secs(),
+ "[storage::blob_sweep] scheduled"
+ );
+ tokio::time::sleep(first_delay).await;
+ let mut tick = tokio::time::interval(interval);
+ tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
+ loop {
+ tick.tick().await;
+ let removed = sweep_installed().await;
+ tracing::trace!(removed, "[storage::blob_sweep] tick complete");
+ }
+ })
+}
+
+#[cfg(test)]
+#[path = "blob_sweep_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/storage/blob_sweep_tests.rs b/crates/openhuman-core/src/storage/blob_sweep_tests.rs
new file mode 100644
index 0000000000..80a919baa2
--- /dev/null
+++ b/crates/openhuman-core/src/storage/blob_sweep_tests.rs
@@ -0,0 +1,61 @@
+use super::*;
+use std::sync::atomic::{AtomicU64, Ordering};
+use tinystoragedrivers::MemoryStorage;
+
+/// A memory backend on a clock the test advances by hand.
+fn backend_with_clock() -> (Arc<AtomicU64>, Arc<dyn StorageBackend>) {
+ let now = Arc::new(AtomicU64::new(1_000));
+ let clock = Arc::clone(&now);
+ let backend: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::with_clock(Arc::new(
+ move || clock.load(Ordering::SeqCst),
+ )));
+ (now, backend)
+}
+
+async fn put_expiring(backend: &Arc<dyn StorageBackend>, scope: &Scope, key: &str, ms: u64) {
+ backend
+ .for_scope(scope)
+ .unwrap()
+ .blobs()
+ .put_expiring(key, b"x".to_vec(), None, Duration::from_millis(ms))
+ .await
+ .unwrap();
+}
+
+#[tokio::test]
+async fn sweep_scope_reclaims_only_lapsed_blobs_of_that_scope() {
+ let (now, backend) = backend_with_clock();
+ let local = Scope::local();
+ let other = crate::storage::scope_for_agent("blob-sweep-other");
+ put_expiring(&backend, &local, "short", 10).await;
+ put_expiring(&backend, &local, "long", 10_000).await;
+ put_expiring(&backend, &other, "short", 10).await;
+
+ now.store(2_000, Ordering::SeqCst);
+ assert_eq!(sweep_scope(backend.as_ref(), &local).await, 1);
+ assert_eq!(sweep_scope(backend.as_ref(), &local).await, 0, "idempotent");
+ assert_eq!(
+ sweep_scope(backend.as_ref(), &other).await,
+ 1,
+ "the other scope's lapsed blob was untouched until its own sweep"
+ );
+ let still = backend.for_scope(&local).unwrap().blobs().get("long").await;
+ assert!(still.unwrap().is_some(), "an unexpired blob survives");
+}
+
+#[tokio::test]
+async fn sweep_all_visits_the_local_scope() {
+ let (now, backend) = backend_with_clock();
+ put_expiring(&backend, &Scope::local(), "gone", 10).await;
+ now.store(2_000, Ordering::SeqCst);
+ assert_eq!(sweep_all(Arc::clone(&backend)).await, 1);
+ assert_eq!(sweep_all(backend).await, 0);
+}
+
+#[tokio::test(start_paused = true)]
+async fn spawned_sweeper_waits_for_its_first_delay_then_keeps_ticking() {
+ let handle = spawn(Duration::from_secs(10), Duration::from_secs(20));
+ tokio::time::sleep(Duration::from_secs(65)).await;
+ assert!(!handle.is_finished(), "the sweeper keeps running");
+ handle.abort();
+}
diff --git a/crates/openhuman-core/src/storage/mod.rs b/crates/openhuman-core/src/storage/mod.rs
index 8deb9e447e..e9c572f504 100644
--- a/crates/openhuman-core/src/storage/mod.rs
+++ b/crates/openhuman-core/src/storage/mod.rs
@@ -25,6 +25,7 @@
//! misconfigured deployment stops at boot instead of at its first write.
pub mod agents;
+pub mod blob_sweep;
pub mod config;
pub mod documents;
pub mod fence;