Skip to content

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

refactor(profiles): move host backend construction into its own module

Steven Enamakel9c6e1a59b9e9
diff --git a/crates/openhuman-core/src/profiles/host.rs b/crates/openhuman-core/src/profiles/host.rs
index e33254c67b..8913e3b092 100644
--- a/crates/openhuman-core/src/profiles/host.rs
+++ b/crates/openhuman-core/src/profiles/host.rs
@@ -35,7 +35,6 @@ use super::types::{ProfileId, ProfileMeta, ProfileSummary};
use crate::config::Config;
use crate::core::runtime::{ContextOverlay, CoreContext, DomainSet, SaasConfig};
use crate::storage::fence::{FenceRegistry, LeaseFence};
-use crate::storage::fenced_backend::FencedBackend;
use crate::storage::lease::{LeaseGrant, LeaseStore};
use crate::storage::StorageBackend;
use crate::tools::toolpacks::ToolGroups;
@@ -84,7 +83,7 @@ pub(super) struct Slot {
/// The open profiles of one SaaS process.
pub struct ProfileHost {
pub(super) saas: SaasConfig,
- capabilities: Option<Arc<super::capabilities::ProfileCapabilities>>,
+ pub(super) capabilities: Option<Arc<super::capabilities::ProfileCapabilities>>,
pub(super) operator: Arc<CoreContext>,
pub(super) node: String,
pub(super) open: Mutex<HashMap<ProfileId, Slot>>,
@@ -98,7 +97,7 @@ pub struct ProfileHost {
/// [`super::lifecycle`].
pub(super) lifecycle: ProfileLocks,
/// The backend shared by the profile registry and cluster leases.
- backend: Option<Arc<dyn StorageBackend>>,
+ pub(super) backend: Option<Arc<dyn StorageBackend>>,
/// The same physical backend, guarded by this host's profile fences, for
/// cron admission policy writes. `None` uses the host-installed/default
/// workspace backend.
@@ -138,78 +137,6 @@ impl ProfileHost {
Self::with_backend(saas, operator, None).expect("file-backed profile host")
}
- /// A host over `backend` when given (the shared registry and cluster
- /// leases), else as [`Self::new`]. Supplied backends are guarded by this
- /// host's profile fences for cron policy writes; an already-fenced backend
- /// remains safe, with an additional host guard around it.
- ///
- /// # Errors
- ///
- /// When the backend refuses the cluster scope.
- pub fn with_backend(
- saas: SaasConfig,
- operator: Arc<CoreContext>,
- backend: Option<Arc<dyn StorageBackend>>,
- ) -> Result<Self, String> {
- Self::with_backend_inner(saas, operator, backend, false)
- }
-
- /// Construct a host over the already-fenced backend installed by the
- /// runtime. Kept crate-private so the installed backend is not wrapped a
- /// second time.
- pub(crate) fn with_fenced_backend(
- saas: SaasConfig,
- operator: Arc<CoreContext>,
- backend: Option<Arc<dyn StorageBackend>>,
- ) -> Result<Self, String> {
- Self::with_backend_inner(saas, operator, backend, true)
- }
-
- fn with_backend_inner(
- mut saas: SaasConfig,
- operator: Arc<CoreContext>,
- backend: Option<Arc<dyn StorageBackend>>,
- already_fenced: bool,
- ) -> Result<Self, String> {
- super::provider_settings::ProfileProviderSettings::from_saas(&saas)
- .map_err(|error| error.to_string())?;
- let node = saas.resolve_node_id(None).to_owned();
- let fences = crate::storage::fence::registry();
- let leases = profile_lease::store_for(
- &saas.root,
- backend.as_ref(),
- &node,
- saas.advertise_url.clone(),
- saas.lease_ttl(),
- )?;
- let registry = match &backend {
- Some(backend) => ProfileRegistry::cluster(backend.as_ref())?,
- None => ProfileRegistry::files(&saas.root),
- };
- let cron_backend = backend.as_ref().map(|backend| {
- if already_fenced {
- Arc::clone(backend)
- } else {
- Arc::new(FencedBackend::new(Arc::clone(backend), Arc::clone(&fences)))
- as Arc<dyn StorageBackend>
- }
- });
- Ok(Self {
- capabilities: None,
- saas,
- operator,
- node,
- open: Mutex::new(HashMap::new()),
- gate: tokio::sync::Mutex::new(()),
- lifecycle: ProfileLocks::default(),
- backend,
- cron_backend,
- leases,
- fences,
- registry,
- })
- }
-
/// Install immutable operator adapters before this host is shared.
pub fn with_capabilities(
mut self,
@@ -219,19 +146,6 @@ impl ProfileHost {
self
}
- /// This host registering its fences in `fences` instead of the process
- /// registry (tests, which must not share one).
- pub fn with_fences(mut self, fences: Arc<FenceRegistry>) -> Self {
- self.fences = fences;
- if let Some(backend) = &self.backend {
- self.cron_backend = Some(Arc::new(FencedBackend::new(
- Arc::clone(backend),
- Arc::clone(&self.fences),
- )));
- }
- self
- }
-
/// The operator's settings this host runs with.
pub fn saas(&self) -> &SaasConfig {
&self.saas
diff --git a/crates/openhuman-core/src/profiles/host_backend.rs b/crates/openhuman-core/src/profiles/host_backend.rs
new file mode 100644
index 0000000000..92963ea8d9
--- /dev/null
+++ b/crates/openhuman-core/src/profiles/host_backend.rs
@@ -0,0 +1,102 @@
+//! Profile-host construction with the storage backend that owns leases,
+//! profile records and cron admission policy.
+
+use std::collections::HashMap;
+use std::sync::{Arc, Mutex};
+
+use super::host::{ProfileHost, Slot};
+use super::lease as profile_lease;
+use super::lifecycle::ProfileLocks;
+use super::registry::ProfileRegistry;
+use crate::core::runtime::{CoreContext, SaasConfig};
+use crate::storage::fence::FenceRegistry;
+use crate::storage::fenced_backend::FencedBackend;
+use crate::storage::lease::LeaseStore;
+use crate::storage::StorageBackend;
+
+impl ProfileHost {
+ /// A host over `backend` when given (the shared registry and cluster
+ /// leases), else as [`Self::new`]. Supplied backends are guarded by this
+ /// host's profile fences for cron policy writes; an already-fenced backend
+ /// remains safe, with an additional host guard around it.
+ ///
+ /// # Errors
+ ///
+ /// When the backend refuses the cluster scope.
+ pub fn with_backend(
+ saas: SaasConfig,
+ operator: Arc<CoreContext>,
+ backend: Option<Arc<dyn StorageBackend>>,
+ ) -> Result<Self, String> {
+ Self::with_backend_inner(saas, operator, backend, false)
+ }
+
+ /// Construct a host over the already-fenced backend installed by the
+ /// runtime. Kept crate-private so the installed backend is not wrapped a
+ /// second time.
+ pub(crate) fn with_fenced_backend(
+ saas: SaasConfig,
+ operator: Arc<CoreContext>,
+ backend: Option<Arc<dyn StorageBackend>>,
+ ) -> Result<Self, String> {
+ Self::with_backend_inner(saas, operator, backend, true)
+ }
+
+ fn with_backend_inner(
+ mut saas: SaasConfig,
+ operator: Arc<CoreContext>,
+ backend: Option<Arc<dyn StorageBackend>>,
+ already_fenced: bool,
+ ) -> Result<Self, String> {
+ super::provider_settings::ProfileProviderSettings::from_saas(&saas)
+ .map_err(|error| error.to_string())?;
+ let node = saas.resolve_node_id(None).to_owned();
+ let fences = crate::storage::fence::registry();
+ let leases = profile_lease::store_for(
+ &saas.root,
+ backend.as_ref(),
+ &node,
+ saas.advertise_url.clone(),
+ saas.lease_ttl(),
+ )?;
+ let registry = match &backend {
+ Some(backend) => ProfileRegistry::cluster(backend.as_ref())?,
+ None => ProfileRegistry::files(&saas.root),
+ };
+ let cron_backend = backend.as_ref().map(|backend| {
+ if already_fenced {
+ Arc::clone(backend)
+ } else {
+ Arc::new(FencedBackend::new(Arc::clone(backend), Arc::clone(&fences)))
+ as Arc<dyn StorageBackend>
+ }
+ });
+ Ok(Self {
+ capabilities: None,
+ saas,
+ operator,
+ node,
+ open: Mutex::new(HashMap::<_, Slot>::new()),
+ gate: tokio::sync::Mutex::new(()),
+ lifecycle: ProfileLocks::default(),
+ backend,
+ cron_backend,
+ leases,
+ fences,
+ registry,
+ })
+ }
+
+ /// This host registering its fences in `fences` instead of the process
+ /// registry (tests, which must not share one).
+ pub fn with_fences(mut self, fences: Arc<FenceRegistry>) -> Self {
+ self.fences = fences;
+ if let Some(backend) = &self.backend {
+ self.cron_backend = Some(Arc::new(FencedBackend::new(
+ Arc::clone(backend),
+ Arc::clone(&self.fences),
+ )));
+ }
+ self
+ }
+}
diff --git a/crates/openhuman-core/src/profiles/mod.rs b/crates/openhuman-core/src/profiles/mod.rs
index b299908437..c11b70109e 100644
--- a/crates/openhuman-core/src/profiles/mod.rs
+++ b/crates/openhuman-core/src/profiles/mod.rs
@@ -30,6 +30,7 @@ pub mod cron;
pub mod fence;
pub mod gateway;
pub mod host;
+mod host_backend;
pub mod layout;
pub mod lease;
pub mod lifecycle;