The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
chore: files changed crates/openhuman-core/src/storage/fence.rs
Steven Enamakel001bd1e2956b
diff --git a/crates/openhuman-core/src/storage/fence.rs b/crates/openhuman-core/src/storage/fence.rsnew file mode 100644index 0000000000..349f5a8106--- /dev/null+++ b/crates/openhuman-core/src/storage/fence.rs@@ -0,0 +1,378 @@+//! Epoch fencing for writes to leased storage scopes.+//!+//! A lease ([`super::lease`]) says which node owns a key; it does not stop a+//! node that lost the key from writing. A paused or partitioned holder keeps+//! running its in-process work until its heartbeat notices the loss, and in+//! that window its writes would land on top of the new holder's.+//!+//! A [`LeaseFence`] closes most of that window for the storage ports. It is+//! one grant (key, node, epoch) plus the scopes that grant protects. While a+//! fence is registered in a [`FenceRegistry`], every write a+//! [`FencedBackend`](super::fenced_backend::FencedBackend) sends to one of+//! those scopes first calls [`LeaseFence::check`], which refuses the write+//! with a typed [`FenceError`] when:+//!+//! 1. the fence is latched ([`LeaseFence::fence`]: the heartbeat lost the+//! lease, or an earlier check saw it superseded);+//! 2. the grant ran out by this node's clock, less a skew margin+//! ([`LeaseFence::check_local`], no I/O); or+//! 3. the stored lease record no longer names this node at this epoch, or+//! has expired, or cannot be read (fail closed).+//!+//! # What this does not guarantee+//!+//! The check and the write are two operations. A holder that passes the+//! check and is then paused for longer than the remaining grant can still+//! land one write after a takeover. The ports offer no way to make the+//! write conditional on a document in another scope: `atomic_batch` is+//! optional (`Capability::Transactions`, MongoDB replica sets only) and has+//! no read-only guard op, and streams and blobs take no precondition at+//! all. Closing the race needs a fencing primitive in tinystoragedrivers+//! (a write guarded by a fence document's epoch, enforced by the driver).+//! The skew margin makes the race need a pause longer than the margin+//! *after* a successful check, rather than any pause at all.+//!+//! Scopes nobody registered pass through untouched, so a backend with an+//! empty registry (every single-user host) behaves exactly as before.++use std::any::Any;+use std::sync::atomic::{AtomicU64, Ordering};+use std::sync::{Arc, LazyLock, Mutex, PoisonError, Weak};++use super::lease::LeaseStore;+use super::StorageError;++/// Why a fenced write was refused.+#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]+pub enum FenceError {+ /// This node lost the lease (latched).+ #[error("lease fence: key lost at epoch {epoch}; this node no longer holds it")]+ Fenced {+ /// The epoch this node held.+ epoch: u64,+ },+ /// The grant ran out by this node's clock (less the skew margin) before+ /// it was renewed.+ #[error("lease fence: grant at epoch {epoch} expired before renewal")]+ Expired {+ /// The epoch this node holds.+ epoch: u64,+ },+ /// The stored record names another holder or a newer epoch.+ #[error("lease fence: epoch {held} superseded (stored epoch {stored:?})")]+ Superseded {+ /// The epoch this node held.+ held: u64,+ /// The stored record's epoch, if there is a record.+ stored: Option<u64>,+ },+ /// The lease record could not be read, so the write is refused.+ #[error("lease fence: could not verify the lease: {0}")]+ Unverified(String),+}++impl FenceError {+ /// This refusal as a port error ([`ErrorKind::Backend`](tinystoragedrivers::ErrorKind),+ /// not retryable, so compare-and-swap loops do not spin on it) carrying+ /// `self` as its source; [`fence_error`] recovers it.+ pub fn into_storage(self) -> StorageError {+ StorageError::backend(self.to_string()).with_source(self)+ }+}++/// The [`FenceError`] behind `error`, when a fence refused the write.+pub fn fence_error(error: &StorageError) -> Option<&FenceError> {+ std::error::Error::source(error).and_then(|source| source.downcast_ref::<FenceError>())+}++/// Which scopes a fence protects.+#[derive(Debug, Clone, PartialEq, Eq)]+pub enum ScopeMatch {+ /// Exactly this scope.+ Exact(String),+ /// Every scope starting with this prefix.+ Prefix(String),+}++impl ScopeMatch {+ fn covers(&self, scope: &str) -> bool {+ match self {+ Self::Exact(exact) => scope == exact,+ Self::Prefix(prefix) => scope.starts_with(prefix.as_str()),+ }+ }+}++/// One node's grant on a key, guarding the scopes it protects. See the+/// module docs.+pub struct LeaseFence {+ key: String,+ node: String,+ epoch: u64,+ expires_at_ms: AtomicU64,+ margin_ms: u64,+ scopes: Vec<ScopeMatch>,+ leases: Arc<dyn LeaseStore>,+ latch: tokio::sync::watch::Sender<bool>,+ /// While the fence is latched, it stays registered (refusing writes)+ /// until this anchor is gone: the work that could still write is what+ /// holds it.+ anchor: Mutex<Option<Weak<dyn Any + Send + Sync>>>,+}++impl std::fmt::Debug for LeaseFence {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.debug_struct("LeaseFence")+ .field("node", &self.node)+ .field("epoch", &self.epoch)+ .field("fenced", &self.is_fenced())+ .finish_non_exhaustive()+ }+}++impl LeaseFence {+ /// A fence for `node`'s grant on `key` at `epoch`, valid until+ /// `expires_at_ms` less `margin_ms`, verified against `leases`.+ pub fn new(+ leases: Arc<dyn LeaseStore>,+ key: impl Into<String>,+ node: impl Into<String>,+ epoch: u64,+ expires_at_ms: u64,+ margin_ms: u64,+ scopes: Vec<ScopeMatch>,+ ) -> Self {+ Self {+ key: key.into(),+ node: node.into(),+ epoch,+ expires_at_ms: AtomicU64::new(expires_at_ms),+ margin_ms,+ scopes,+ leases,+ latch: tokio::sync::watch::channel(false).0,+ anchor: Mutex::new(None),+ }+ }++ /// The leased key.+ pub fn key(&self) -> &str {+ &self.key+ }++ /// The epoch this fence guards with.+ pub fn epoch(&self) -> u64 {+ self.epoch+ }++ /// Whether this fence guards `scope`.+ pub fn covers(&self, scope: &str) -> bool {+ self.scopes.iter().any(|m| m.covers(scope))+ }++ /// Records a renewal: the grant now lasts until `expires_at_ms`.+ pub fn renewed(&self, expires_at_ms: u64) {+ self.expires_at_ms.store(expires_at_ms, Ordering::SeqCst);+ }++ /// Latches the fence: every later check and write is refused. Idempotent.+ pub fn fence(&self) {+ if !self.latch.send_replace(true) {+ tracing::warn!(+ target: "openhuman::storage::fence",+ epoch = self.epoch,+ "[storage][fence] latched; writes to the fenced scopes are refused"+ );+ }+ }++ /// Whether the fence is latched.+ pub fn is_fenced(&self) -> bool {+ *self.latch.borrow()+ }++ /// Resolves once the fence latches (at once when it already has), so+ /// in-process work can stop promptly.+ pub async fn fenced(&self) {+ let mut rx = self.latch.subscribe();+ // `wait_for` only fails when the sender is gone, and `self` owns it.+ let _ = rx.wait_for(|fenced| *fenced).await;+ }++ /// Keeps this fence registered after it latches for as long as `anchor`+ /// is alive (see [`FenceRegistry::lookup`]).+ pub fn anchor_to(&self, anchor: Weak<dyn Any + Send + Sync>) {+ *self.anchor.lock().unwrap_or_else(PoisonError::into_inner) = Some(anchor);+ }++ fn anchor_alive(&self) -> bool {+ self.anchor+ .lock()+ .unwrap_or_else(PoisonError::into_inner)+ .as_ref()+ .is_some_and(|anchor| anchor.strong_count() > 0)+ }++ /// The checks that need no I/O: the latch, and the grant's expiry by+ /// this node's clock less the margin.+ ///+ /// # Errors+ ///+ /// [`FenceError::Fenced`] or [`FenceError::Expired`].+ pub fn check_local(&self, now_ms: u64) -> Result<(), FenceError> {+ if self.is_fenced() {+ return Err(FenceError::Fenced { epoch: self.epoch });+ }+ let deadline = self+ .expires_at_ms+ .load(Ordering::SeqCst)+ .saturating_sub(self.margin_ms);+ if now_ms >= deadline {+ return Err(FenceError::Expired { epoch: self.epoch });+ }+ Ok(())+ }++ /// [`Self::check_local`], then the stored record: it must still name+ /// this node at this epoch, unreleased and unexpired. A record that moved+ /// on latches the fence.+ ///+ /// # Errors+ ///+ /// Any [`FenceError`]; a lease store failure is+ /// [`FenceError::Unverified`] (the write is refused, not risked).+ pub async fn check(&self, now_ms: u64) -> Result<(), FenceError> {+ self.check_local(now_ms)?;+ let record = self+ .leases+ .holder(&self.key)+ .await+ .map_err(|error| FenceError::Unverified(error.to_string()))?;+ match record {+ Some(record)+ if record.owner == self.node && record.epoch == self.epoch && !record.released =>+ {+ if now_ms >= record.expires_at_ms {+ return Err(FenceError::Expired { epoch: self.epoch });+ }+ Ok(())+ }+ other => {+ let stored = other.map(|record| record.epoch);+ tracing::warn!(+ target: "openhuman::storage::fence",+ held = self.epoch,+ stored = ?stored,+ "[storage][fence] lease superseded; refusing the write"+ );+ self.fence();+ Err(FenceError::Superseded {+ held: self.epoch,+ stored,+ })+ }+ }+ }+}++/// The fences a process holds. The process has one ([`registry`]); tests+/// make their own so they never see each other's.+pub struct FenceRegistry {+ fences: Mutex<Vec<Arc<LeaseFence>>>,+ clock: Box<dyn Fn() -> u64 + Send + Sync>,+}++impl std::fmt::Debug for FenceRegistry {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.debug_struct("FenceRegistry")+ .field("fences", &self.lock().len())+ .finish_non_exhaustive()+ }+}++impl FenceRegistry {+ /// A registry reading `clock` (milliseconds since the epoch) for checks.+ pub fn new(clock: impl Fn() -> u64 + Send + Sync + 'static) -> Self {+ Self {+ fences: Mutex::new(Vec::new()),+ clock: Box::new(clock),+ }+ }++ /// The registry's clock.+ pub fn now_ms(&self) -> u64 {+ (self.clock)()+ }++ fn lock(&self) -> std::sync::MutexGuard<'_, Vec<Arc<LeaseFence>>> {+ self.fences.lock().unwrap_or_else(PoisonError::into_inner)+ }++ /// Registers `fence`, replacing any fence on the same key.+ pub fn register(&self, fence: Arc<LeaseFence>) {+ let mut fences = self.lock();+ fences.retain(|held| held.key != fence.key);+ tracing::debug!(+ target: "openhuman::storage::fence",+ epoch = fence.epoch,+ "[storage][fence] registered"+ );+ fences.push(fence);+ }++ /// Drops the fence on `key` at `epoch` (a clean release); a fence+ /// registered since under another epoch stays.+ pub fn retire(&self, key: &str, epoch: u64) {+ self.lock()+ .retain(|held| !(held.key == key && held.epoch == epoch));+ }++ /// The fence guarding `scope`, if any. A latched fence whose anchor is+ /// gone (nothing that could still write remains) is pruned here.+ pub fn lookup(&self, scope: &str) -> Option<Arc<LeaseFence>> {+ let mut fences = self.lock();+ fences.retain(|held| !held.is_fenced() || held.anchor_alive());+ fences.iter().find(|held| held.covers(scope)).cloned()+ }++ /// Refuses a write to `scope` unless its fence (when it has one) passes+ /// [`LeaseFence::check`].+ ///+ /// # Errors+ ///+ /// The fence's refusal, as a port error ([`FenceError::into_storage`]).+ pub async fn guard(&self, scope: &str, op: &'static str) -> Result<(), StorageError> {+ let Some(fence) = self.lookup(scope) else {+ return Ok(());+ };+ fence.check(self.now_ms()).await.map_err(|error| {+ tracing::warn!(+ target: "openhuman::storage::fence",+ op,+ epoch = fence.epoch,+ "[storage][fence] refused a write: {error}"+ );+ error.into_storage()+ })+ }+}++/// Milliseconds since the Unix epoch.+pub fn system_now_ms() -> u64 {+ std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .map(|d| u64::try_from(d.as_millis()).unwrap_or(u64::MAX))+ .unwrap_or_default()+}++static REGISTRY: LazyLock<Arc<FenceRegistry>> =+ LazyLock::new(|| Arc::new(FenceRegistry::new(system_now_ms)));++/// The process's fence registry, which [`super::open`]'s backends consult.+pub fn registry() -> Arc<FenceRegistry> {+ Arc::clone(®ISTRY)+}++#[cfg(test)]+#[path = "fence_tests.rs"]+mod tests;