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
docs(storage): document lease storage backends
Steven Enamakel67126261379f
diff --git a/crates/openhuman-core/src/storage/README.md b/crates/openhuman-core/src/storage/README.mdindex 711e29f560..937088fb30 100644--- a/crates/openhuman-core/src/storage/README.md+++ b/crates/openhuman-core/src/storage/README.md@@ -74,7 +74,7 @@ unique among live processes.| Store | Where | Use || --- | --- | --- || `DocumentLeases` | one document per key, scope `cluster`, collection `leases`; every write carries `Precondition::Absent` or `Version` | clustered nodes on a driver with cross-process CAS |-| `LocalLeases` | an exclusive `fs2` flock on `<root>/<key>/.lease`, record in `.lease.json` beside it; no expiry, the OS drops the lock when the process dies | hosts without a backend |+| `LocalLeases` | an exclusive `fs2` flock on `<root>/<sha256(key) hex>/.lease`, record in `.lease.json` beside it; no expiry, the OS drops the lock when the process dies | hosts without a backend |Keys are 1 to 200 bytes of ASCII letters, digits and `- _ . @`, not startingwith `.`, so they are safe as directory names and document ids. The scopediff --git a/crates/openhuman-core/src/storage/lease.rs b/crates/openhuman-core/src/storage/lease.rsindex 09def54fc4..e7b1a548e4 100644--- a/crates/openhuman-core/src/storage/lease.rs+++ b/crates/openhuman-core/src/storage/lease.rs@@ -9,7 +9,7 @@//! only by compare-and-swap, so it is correct across processes on a driver//! with cross-process CAS ([`super::driver_has_cross_process_cas`]).//! - [`LocalLeases`] is for hosts without a backend: an exclusive `flock` on-//! `<root>/<key>/.lease`, which the OS drops when the holder dies.+//! `<root>/<sha256(key) hex>/.lease`, which the OS drops when the holder dies.//!//! No wall clock is read here: every operation takes `now_ms`, so behaviour//! is a function of its inputs and the tests are deterministic.diff --git a/crates/openhuman-core/src/storage/lease_documents.rs b/crates/openhuman-core/src/storage/lease_documents.rsindex 5118e79a66..7109b34c2a 100644--- a/crates/openhuman-core/src/storage/lease_documents.rs+++ b/crates/openhuman-core/src/storage/lease_documents.rs@@ -34,6 +34,9 @@ pub struct DocumentLeases {/// re-entrant and lets a restarted node (same id, fresh instance) see its/// own leftover record as unclean.held: Mutex<HashMap<String, u64>>,+ /// Serializes `acquire` on this instance so an older completion cannot+ /// overwrite newer held state.+ acquire_gate: tokio::sync::Mutex<()>,declared: tokio::sync::OnceCell<()>,}@@ -65,6 +68,7 @@ impl DocumentLeases {// the instant it is issued.ttl_ms: u64::try_from(ttl.as_millis()).unwrap_or(u64::MAX).max(1),held: Mutex::new(HashMap::new()),+ acquire_gate: tokio::sync::Mutex::new(()),declared: tokio::sync::OnceCell::new(),}}@@ -216,6 +220,7 @@ impl LeaseStore for DocumentLeases {async fn acquire(&self, key: &str, now_ms: u64) -> Result<LeaseGrant, LeaseError> {validate_key(key)?;self.declare().await?;+ let _gate = self.acquire_gate.lock().await;let expires_at_ms = now_ms.saturating_add(self.ttl_ms);for attempt in 0..ACQUIRE_ATTEMPTS {let found = self.read(key).await?;diff --git a/crates/openhuman-core/src/storage/lease_local.rs b/crates/openhuman-core/src/storage/lease_local.rsindex 3e44cb1726..8569254e73 100644--- a/crates/openhuman-core/src/storage/lease_local.rs+++ b/crates/openhuman-core/src/storage/lease_local.rs@@ -1,6 +1,6 @@//! [`LocalLeases`]: leases as OS file locks, for hosts without a backend.//!-//! The holder keeps an exclusive `flock` (`fs2`) on `<root>/<key>/.lease`+//! The holder keeps an exclusive `flock` (`fs2`) on `<root>/<sha256(key) hex>/.lease`//! open for as long as it holds the key; the OS drops it when the process//! dies, so liveness needs no clock and these leases never expire//! (`expires_at_ms == u64::MAX`). The record itself sits beside the lock in@@ -93,20 +93,19 @@ impl LocalLeases {.map_err(|error| io_error("open", &path, &error))}- /// The record on disk; an unreadable one reads as an unknown owner, so a- /// torn read can never let a caller through.- fn read_record(&self, key: &str) -> Option<LeaseRecord> {+ /// The record on disk: `None` only when there is none. An unreadable or+ /// malformed record is an error, never a fresh start, so a fencing epoch+ /// is not reused and crash recovery is not skipped.+ fn read_record(&self, key: &str) -> Result<Option<LeaseRecord>, StorageError> {let path = local_dir(&self.root, key).join(RECORD_FILE);- let bytes = fs::read(&path).ok()?;- Some(- serde_json::from_slice(&bytes).unwrap_or_else(|_| LeaseRecord {- owner: "unknown".to_string(),- endpoint: None,- epoch: 0,- expires_at_ms: u64::MAX,- released: false,- }),- )+ let bytes = match fs::read(&path) {+ Ok(bytes) => bytes,+ Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(None),+ Err(error) => return Err(io_error("read", &path, &error)),+ };+ serde_json::from_slice(&bytes).map(Some).map_err(|error| {+ StorageError::serialization(format!("lease record {}: {error}", path.display()))+ })}fn write_record(&self, key: &str, record: &LeaseRecord) -> Result<(), StorageError> {@@ -165,7 +164,8 @@ impl LeaseStore for LocalLeases {let path = local_dir(&self.root, key).join(LOCK_FILE);return Err(io_error("lock", &path, &error).into());}- let record = self.read_record(key).unwrap_or_else(|| LeaseRecord {+ // Locked elsewhere: an unreadable record reads as an unknown owner.+ let record = self.read_record(key).ok().flatten().unwrap_or_else(|| LeaseRecord {owner: "unknown".to_string(),endpoint: None,epoch: 0,@@ -181,7 +181,13 @@ impl LeaseStore for LocalLeases {);return Err(LeaseError::Held(record));}- let previous = self.read_record(key);+ let previous = match self.read_record(key) {+ Ok(previous) => previous,+ Err(error) => {+ let _ = FileExt::unlock(&file);+ return Err(error.into());+ }+ };let unclean = previous.as_ref().is_some_and(|record| !record.released);let epoch = match previous {None => 1,@@ -248,14 +254,17 @@ impl LeaseStore for LocalLeases {async fn release(&self, grant: LeaseGrant) -> Result<(), LeaseError> {self.with_current(&grant, |held| {- let holding = held.remove(&grant.key).ok_or(LeaseError::Lost)?;+ let holding = held.get(&grant.key).ok_or(LeaseError::Lost)?;let record = LeaseRecord {released: true,- ..holding.record+ ..holding.record.clone()};- let written = self.write_record(&grant.key, &record);- let _ = FileExt::unlock(&holding.file);- written?;+ // Persist first: on failure the lock and holding stay, so no one+ // can take the key over a record still saying "not released".+ self.write_record(&grant.key, &record)?;+ if let Some(holding) = held.remove(&grant.key) {+ let _ = FileExt::unlock(&holding.file);+ }tracing::debug!(target: "openhuman::storage::lease",key = %grant.key,@@ -272,7 +281,7 @@ impl LeaseStore for LocalLeases {if let Some(holding) = self.held().get(key) {return Ok(Some(holding.record.clone()));}- let Some(mut record) = self.read_record(key) else {+ let Some(mut record) = self.read_record(key)? else {return Ok(None);};if !record.released {