Skip to content

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

feat(profiles): add host profile storage and fence support

Steven Enamakele8dd974870be
diff --git a/crates/openhuman-core/src/profiles/host.rs b/crates/openhuman-core/src/profiles/host.rs
index 835c44d6bd..b0ff7adf38 100644
--- a/crates/openhuman-core/src/profiles/host.rs
+++ b/crates/openhuman-core/src/profiles/host.rs
@@ -362,15 +362,6 @@ impl ProfileHost {
.profile(id.as_str())
.agent_policy(profile_policy(&config)),
);
- if grant.previous_unclean {
- log::info!(
- "[profiles] profile={id} taken over from a holder that never released it (epoch {}); recovering",
- grant.epoch
- );
- recover_workspace(id, &layout.workspace_dir);
- recover_session_store(id, &context);
- }
- crate::platform::cost::seed_tenant_tracker(&context, &config);
let fence = super::fence::for_grant(
Arc::clone(&self.leases),
&self.node,
@@ -378,11 +369,21 @@ impl ProfileHost {
&grant,
self.saas.lease_ttl(),
);
+ // Registered before recovery so its writes are already guarded.
// Once fenced, it refuses writes for as long as work on this context
// could still issue them.
let anchor: Arc<dyn std::any::Any + Send + Sync> = context.clone();
fence.anchor_to(Arc::downgrade(&anchor));
self.fences.register(Arc::clone(&fence));
+ if grant.previous_unclean {
+ log::info!(
+ "[profiles] profile={id} taken over from a holder that never released it (epoch {}); recovering",
+ grant.epoch
+ );
+ recover_workspace(id, &layout.workspace_dir);
+ recover_session_store(id, &context);
+ }
+ crate::platform::cost::seed_tenant_tracker(&context, &config);
let state = Arc::new(Profile {
id: id.clone(),
layout,
diff --git a/crates/openhuman-core/src/storage/fence.rs b/crates/openhuman-core/src/storage/fence.rs
index 7ce5532760..ebf3ff06c6 100644
--- a/crates/openhuman-core/src/storage/fence.rs
+++ b/crates/openhuman-core/src/storage/fence.rs
@@ -314,9 +314,22 @@ impl FenceRegistry {
self.fences.lock().unwrap_or_else(PoisonError::into_inner)
}
- /// Registers `fence`, replacing any fence on the same key.
+ /// Registers `fence`, replacing any fence on the same key unless that
+ /// one is from a newer epoch.
pub fn register(&self, fence: Arc<LeaseFence>) {
let mut fences = self.lock();
+ // A fence from a newer epoch is never displaced by an older grant's.
+ if fences
+ .iter()
+ .any(|held| held.key == fence.key && held.epoch > fence.epoch)
+ {
+ tracing::debug!(
+ target: "openhuman::storage::fence",
+ epoch = fence.epoch,
+ "[storage][fence] stale registration ignored"
+ );
+ return;
+ }
fences.retain(|held| held.key != fence.key);
tracing::debug!(
target: "openhuman::storage::fence",