Skip to content

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

refactor(storage): restructure journal maintenance logic

Steven Enamakelb8e384b857c8
diff --git a/crates/openhuman-core/src/storage/journal_maintenance.rs b/crates/openhuman-core/src/storage/journal_maintenance.rs
index 4e0cc594dd..cebd82c11c 100644
--- a/crates/openhuman-core/src/storage/journal_maintenance.rs
+++ b/crates/openhuman-core/src/storage/journal_maintenance.rs
@@ -55,11 +55,20 @@ pub async fn maintain_once(
now_ms: i64,
) -> Result<MaintenanceSummary, crate::storage::StorageError> {
let retention_ms = u64::from(retention_days).saturating_mul(DAY_MS);
- let mut summary = MaintenanceSummary::default();
// Keep these handles alive for the entire pass. An open profile handle
// keeps its host slot in use, which prevents its lease fence from being
// retired while this pass writes to that profile's scope.
let (_hosted_profiles, scopes) = known_scopes()?;
+ maintain_scopes(backend, scopes, retention_ms, now_ms).await
+}
+
+async fn maintain_scopes(
+ backend: &Arc<dyn StorageBackend>,
+ scopes: Vec<Scope>,
+ retention_ms: u64,
+ now_ms: i64,
+) -> Result<MaintenanceSummary, crate::storage::StorageError> {
+ let mut summary = MaintenanceSummary::default();
for scope in scopes {
match maintain_scope(backend, &scope, retention_ms, now_ms).await {
Ok(report) => {
@@ -161,50 +170,26 @@ fn known_scopes(
}
let now_ms = u64::try_from(chrono::Utc::now().timestamp_millis()).unwrap_or(u64::MAX);
- let hosted_profiles = crate::profiles::host::host()
+ let profiles = crate::profiles::host::host()
.map(|host| host.hosted_profiles())
- .unwrap_or_default()
- .into_iter()
- .filter(|profile| profile.lease_fence().check_local(now_ms).is_ok())
- .collect::<Vec<_>>();
- let profiles = hosted_profiles
- .iter()
- .map(|profile| profile.id.as_str().to_owned())
- .collect::<BTreeSet<_>>();
+ .unwrap_or_default();
+ let (hosted_profiles, profile_scopes) = profile_scope_snapshot(profiles, now_ms);
+ scopes.extend(profile_scopes);
for (agent, context) in crate::storage::agents::contexts() {
let dispatch = crate::core::runtime::DispatchCtx::new(context);
if let Ok(tenant) = dispatch.tenant() {
- if let Some(profile) = tenant.profile {
- // A profile context can outlive the host's lease. Only the
- // captured, currently fenced handles above authorize work.
- if profiles.contains(&profile) {
- scopes.insert(crate::storage::scope_for_profile(&profile));
+ if tenant.profile.is_none() {
+ if let Some(agent) = tenant.agent.or(Some(agent)) {
+ scopes.insert(crate::storage::scope_for_agent(&agent));
}
- } else if let Some(agent) = tenant.agent.or(Some(agent)) {
- scopes.insert(crate::storage::scope_for_agent(&agent));
}
}
}
- let bases: Vec<Scope> = if crate::core::runtime::mode::is_saas() {
- hosted_profiles
- .iter()
- .map(|profile| crate::storage::scope_for_profile(profile.id.as_str()))
- .collect()
- } else {
- std::iter::once(Scope::local())
- .chain(
- hosted_profiles
- .iter()
- .map(|profile| crate::storage::scope_for_profile(profile.id.as_str())),
- )
- .collect()
- };
- scopes.extend(bases.iter().cloned());
- for base in bases {
+ if !crate::core::runtime::mode::is_saas() {
for module in crate::modules::registry::ALL {
- if let Ok(scope) = base.module(module.id) {
+ if let Ok(scope) = Scope::local().module(module.id) {
scopes.insert(scope);
}
}
@@ -212,6 +197,30 @@ fn known_scopes(
Ok((hosted_profiles, scopes.into_iter().collect()))
}
+/// Retain only profile handles whose lease is still live and enumerate their
+/// tenant/module scopes. Keeping the handles alive prevents release while the
+/// caller is using those scopes.
+fn profile_scope_snapshot(
+ profiles: Vec<Arc<crate::profiles::Profile>>,
+ now_ms: u64,
+) -> (Vec<Arc<crate::profiles::Profile>>, Vec<Scope>) {
+ let live = profiles
+ .into_iter()
+ .filter(|profile| profile.lease_fence().check_local(now_ms).is_ok())
+ .collect::<Vec<_>>();
+ let mut scopes = BTreeSet::new();
+ for profile in &live {
+ let base = crate::storage::scope_for_profile(profile.id.as_str());
+ scopes.insert(base.clone());
+ for module in crate::modules::registry::ALL {
+ if let Ok(scope) = base.module(module.id) {
+ scopes.insert(scope);
+ }
+ }
+ }
+ (live, scopes.into_iter().collect())
+}
+
/// Retention days from the current config; default when it cannot load.
async fn configured_retention_days(ctx: &crate::core::runtime::DispatchCtx) -> u32 {
match crate::config::ops::load_current_or_init(ctx).await {
diff --git a/crates/openhuman-core/src/storage/journal_maintenance_tests.rs b/crates/openhuman-core/src/storage/journal_maintenance_tests.rs
index 1e2208cddc..f08243422e 100644
--- a/crates/openhuman-core/src/storage/journal_maintenance_tests.rs
+++ b/crates/openhuman-core/src/storage/journal_maintenance_tests.rs
@@ -1,10 +1,10 @@
use super::*;
-use tinyagents_harness::HarnessStatusStore;
use tinyagents_harness::events::{AgentEvent, EventSink, HarnessRunStatus};
use tinyagents_harness::ids::{ComponentId, HarnessPhase, RunId};
use tinyagents_harness::observability::replay::read_run_events_page;
use tinyagents_harness::observability::{AgentObservation, HarnessEventJournal, StoreEventJournal};
use tinyagents_harness::store::{AppendStore, DriverAppendStore};
+use tinyagents_harness::HarnessStatusStore;
use tinystoragedrivers_testkit::memory_backend as test_memory_backend;
fn backend() -> Arc<dyn StorageBackend> {
@@ -12,7 +12,14 @@ fn backend() -> Arc<dyn StorageBackend> {
}
fn journal(backend: &Arc<dyn StorageBackend>) -> StoreEventJournal<Arc<dyn AppendStore>> {
- let storage = backend.for_scope(&Scope::local()).unwrap();
+ journal_in_scope(backend, &Scope::local())
+}
+
+fn journal_in_scope(
+ backend: &Arc<dyn StorageBackend>,
+ scope: &Scope,
+) -> StoreEventJournal<Arc<dyn AppendStore>> {
+ let storage = backend.for_scope(scope).unwrap();
StoreEventJournal::new(
Arc::new(DriverAppendStore::journal(Arc::clone(storage.streams()))) as Arc<dyn AppendStore>,
)
@@ -151,14 +158,12 @@ async fn maintenance_deletes_a_sealed_journal_after_the_configured_retention() {
.unwrap();
assert_eq!(late.deleted, 1);
let storage = backend.for_scope(&Scope::local()).unwrap();
- assert!(
- storage
- .streams()
- .info(&format!("{JOURNAL_STREAM}/run.old"))
- .await
- .unwrap()
- .is_none()
- );
+ assert!(storage
+ .streams()
+ .info(&format!("{JOURNAL_STREAM}/run.old"))
+ .await
+ .unwrap()
+ .is_none());
}
#[tokio::test]
@@ -187,3 +192,72 @@ async fn zero_retention_days_keeps_sealed_journals_forever() {
assert_eq!(summary.deleted, 0);
assert!(sealed(&backend, "run.keep").await);
}
+
+#[tokio::test]
+async fn dormant_or_expired_profile_fences_receive_no_journal_maintenance_writes() {
+ let profile_root = tempfile::tempdir().unwrap();
+ let profile_host = crate::profiles::ProfileHost::new(
+ crate::core::runtime::SaasConfig::new(profile_root.path()),
+ crate::core::runtime::CoreContext::for_test(crate::core::runtime::DomainSet::full(), None),
+ );
+ let profile_id = crate::profiles::ProfileId::for_user(
+ "journal-dormant",
+ crate::profiles::ProfileIdMode::Raw,
+ )
+ .unwrap();
+ profile_host.provision(&profile_id).await.unwrap();
+
+ let backend = backend();
+ let profile_scope = crate::storage::scope_for_profile(profile_id.as_str());
+ let profile_journal = journal_in_scope(&backend, &profile_scope);
+ seed(&profile_journal, "run.foreign", 1).await;
+ profile_journal.seal_run("run.foreign").await.unwrap();
+ let stream = format!("{JOURNAL_STREAM}/run.foreign");
+ let sealed_at = backend
+ .for_scope(&profile_scope)
+ .unwrap()
+ .streams()
+ .info(&stream)
+ .await
+ .unwrap()
+ .unwrap()
+ .sealed_ms
+ .unwrap();
+ let expired_at = sealed_at + DAY_MS as i64 + 1;
+
+ // Provisioned in the registry, but never opened here: it may be leased by
+ // another host, so it must not appear in this process's maintenance pass.
+ let (handles, scopes) = profile_scope_snapshot(profile_host.hosted_profiles(), u64::MAX);
+ assert!(handles.is_empty());
+ assert!(scopes.is_empty());
+ let untouched = maintain_scopes(&backend, scopes, DAY_MS, expired_at)
+ .await
+ .unwrap();
+ assert_eq!(untouched.deleted, 0);
+
+ // Even a previously captured profile handle stops authorizing writes
+ // after its lease fence expires.
+ let opened = profile_host.open(&profile_id).await.unwrap();
+ opened.lease_fence().renewed(0);
+ let (handles, scopes) = profile_scope_snapshot(vec![opened], u64::MAX);
+ assert!(handles.is_empty());
+ assert!(scopes.is_empty());
+ let expired = maintain_scopes(&backend, scopes, DAY_MS, expired_at)
+ .await
+ .unwrap();
+ assert_eq!(expired.deleted, 0);
+ assert!(backend
+ .for_scope(&profile_scope)
+ .unwrap()
+ .streams()
+ .info(&stream)
+ .await
+ .unwrap()
+ .is_some());
+}
+
+#[test]
+fn journal_scope_inventory_excludes_cluster() {
+ let (_profiles, scopes) = known_scopes().unwrap();
+ assert!(!scopes.contains(&Scope::cluster()));
+}