Skip to content

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

feat(profiles): add release operation to hand off a profile lease

Steven Enamakel533a4aac7fa5
diff --git a/crates/openhuman-core/src/profiles/background.rs b/crates/openhuman-core/src/profiles/background.rs
index 180bd13a29..eec55aa43b 100644
--- a/crates/openhuman-core/src/profiles/background.rs
+++ b/crates/openhuman-core/src/profiles/background.rs
@@ -60,9 +60,9 @@ pub fn spawn(host: Arc<ProfileHost>) -> tokio::task::JoinHandle<()> {
/// One visit to every provisioned profile.
pub async fn tick(host: &ProfileHost) -> TickReport {
- host.evict_idle();
+ host.evict_idle().await;
let mut report = TickReport::default();
- let profiles = match host.list() {
+ let profiles = match host.list().await {
Ok(profiles) => profiles,
Err(error) => {
log::warn!("[profiles][background] listing profiles failed: {error}");
@@ -74,7 +74,7 @@ pub async fn tick(host: &ProfileHost) -> TickReport {
if !jobs::has_pending(&host.layout_of(&id).workspace_dir) {
continue;
}
- let state = match host.open(&id) {
+ let state = match host.open(&id).await {
Ok(state) => state,
Err(error) => {
log::debug!("[profiles][background] profile={id} not opened: {error}");
diff --git a/crates/openhuman-core/src/profiles/mod.rs b/crates/openhuman-core/src/profiles/mod.rs
index e1dab54ef6..565813c013 100644
--- a/crates/openhuman-core/src/profiles/mod.rs
+++ b/crates/openhuman-core/src/profiles/mod.rs
@@ -12,9 +12,13 @@
//! and inspects them through the `profiles.*` controllers ([`schemas`]).
//!
//! The isolation boundary is the profile's own [`CoreContext`]: its config,
-//! workspace and `session_agent` (set to the profile id). Work for one user
-//! runs under that context, which is what the config loader, the session
-//! store and the per-thread caches key on.
+//! workspace, `profile` (the tenant key) and `session_agent` (both set to the
+//! profile id). Work for one user runs under that context, which is what the
+//! config loader, the session store and the per-thread caches key on.
+//!
+//! One process hosts a profile at a time: opening one takes its lease
+//! ([`lease`]), and the provisioned set lives in the [`registry`] — the
+//! shared storage backend when one is configured, else `profile.toml` files.
//!
//! [`CoreContext`]: crate::core::runtime::CoreContext
@@ -23,13 +27,17 @@ pub mod credentials;
pub mod gateway;
pub mod host;
pub mod layout;
+pub mod lease;
pub mod ops;
+pub mod registry;
pub mod schemas;
pub mod surface;
pub mod tools;
pub mod types;
pub use host::{current, Profile, ProfileHost};
+pub use lease::OpenError;
+pub use registry::ProfileRegistry;
pub use layout::ProfileLayout;
pub use schemas::{all_profiles_controller_schemas, all_profiles_registered_controllers};
pub use types::{ProfileId, ProfileIdMode, ProfileSummary};
diff --git a/crates/openhuman-core/src/profiles/ops.rs b/crates/openhuman-core/src/profiles/ops.rs
index 79676afcac..767fc7ff0f 100644
--- a/crates/openhuman-core/src/profiles/ops.rs
+++ b/crates/openhuman-core/src/profiles/ops.rs
@@ -7,6 +7,7 @@ use super::credentials::{self, UserCredentialKind};
use super::host::{self, ProfileHost};
use super::types::{
CredentialResult, DeprovisionResult, ProfileId, ProfileSummary, ProvisionResult,
+ ReleaseResult,
};
use crate::core::Outcome;
@@ -15,16 +16,16 @@ fn require_host() -> Result<std::sync::Arc<ProfileHost>, String> {
}
/// Create the profile for gateway user `user_id`, if it does not exist yet.
-pub fn provision(user_id: &str) -> Result<Outcome<ProvisionResult>, String> {
- provision_on(&*require_host()?, user_id)
+pub async fn provision(user_id: &str) -> Result<Outcome<ProvisionResult>, String> {
+ provision_on(&*require_host()?, user_id).await
}
-pub(crate) fn provision_on(
+pub(crate) async fn provision_on(
host: &ProfileHost,
user_id: &str,
) -> Result<Outcome<ProvisionResult>, String> {
let profile_id = ProfileId::for_user(user_id, host.saas().profile_ids)?;
- let created = host.provision(&profile_id)?;
+ let created = host.provision(&profile_id).await?;
let log = if created {
format!("provisioned {profile_id}")
} else {
@@ -40,16 +41,16 @@ pub(crate) fn provision_on(
}
/// Close profile `profile_id` and archive its state.
-pub fn deprovision(profile_id: &str) -> Result<Outcome<DeprovisionResult>, String> {
- deprovision_on(&*require_host()?, profile_id)
+pub async fn deprovision(profile_id: &str) -> Result<Outcome<DeprovisionResult>, String> {
+ deprovision_on(&*require_host()?, profile_id).await
}
-pub(crate) fn deprovision_on(
+pub(crate) async fn deprovision_on(
host: &ProfileHost,
profile_id: &str,
) -> Result<Outcome<DeprovisionResult>, String> {
let profile_id = ProfileId::parse(profile_id)?;
- let removed = host.deprovision(&profile_id)?;
+ let removed = host.deprovision(&profile_id).await?;
let log = if removed {
format!("archived {profile_id}")
} else {
@@ -65,24 +66,25 @@ pub(crate) fn deprovision_on(
}
/// Every provisioned profile.
-pub fn list() -> Result<Outcome<Vec<ProfileSummary>>, String> {
- let profiles = require_host()?.list()?;
+pub async fn list() -> Result<Outcome<Vec<ProfileSummary>>, String> {
+ let profiles = require_host()?.list().await?;
let log = format!("{} profile(s)", profiles.len());
Ok(Outcome::single_log(profiles, log))
}
/// One profile, or an error when it is not provisioned.
-pub fn status(profile_id: &str) -> Result<Outcome<ProfileSummary>, String> {
- status_on(&*require_host()?, profile_id)
+pub async fn status(profile_id: &str) -> Result<Outcome<ProfileSummary>, String> {
+ status_on(&*require_host()?, profile_id).await
}
-pub(crate) fn status_on(
+pub(crate) async fn status_on(
host: &ProfileHost,
profile_id: &str,
) -> Result<Outcome<ProfileSummary>, String> {
let profile_id = ProfileId::parse(profile_id)?;
let summary = host
- .summary(&profile_id)?
+ .summary(&profile_id)
+ .await?
.ok_or_else(|| format!("profile {profile_id} is not provisioned"))?;
Ok(Outcome::single_log(
summary,
@@ -91,16 +93,16 @@ pub(crate) fn status_on(
}
/// Install the backend credential the gateway holds for profile `profile_id`.
-pub fn set_credential(
+pub async fn set_credential(
profile_id: &str,
kind: UserCredentialKind,
token: &str,
expires_at: Option<&str>,
) -> Result<Outcome<CredentialResult>, String> {
- set_credential_on(&*require_host()?, profile_id, kind, token, expires_at)
+ set_credential_on(&*require_host()?, profile_id, kind, token, expires_at).await
}
-pub(crate) fn set_credential_on(
+pub(crate) async fn set_credential_on(
host: &ProfileHost,
profile_id: &str,
kind: UserCredentialKind,
@@ -110,7 +112,7 @@ pub(crate) fn set_credential_on(
let profile_id = ProfileId::parse(profile_id)?;
// From the layout, not `open`: installing or revoking a credential must
// work even when every profile slot is busy.
- let config = host.provisioned_config(&profile_id)?;
+ let config = host.provisioned_config(&profile_id).await?;
credentials::store(&config, kind, token, expires_at)?;
log::info!("[profiles] credential installed for profile={profile_id} kind={kind:?}");
Ok(Outcome::single_log(
@@ -123,16 +125,16 @@ pub(crate) fn set_credential_on(
}
/// Remove every credential profile `profile_id` holds.
-pub fn clear_credential(profile_id: &str) -> Result<Outcome<CredentialResult>, String> {
- clear_credential_on(&*require_host()?, profile_id)
+pub async fn clear_credential(profile_id: &str) -> Result<Outcome<CredentialResult>, String> {
+ clear_credential_on(&*require_host()?, profile_id).await
}
-pub(crate) fn clear_credential_on(
+pub(crate) async fn clear_credential_on(
host: &ProfileHost,
profile_id: &str,
) -> Result<Outcome<CredentialResult>, String> {
let profile_id = ProfileId::parse(profile_id)?;
- let config = host.provisioned_config(&profile_id)?;
+ let config = host.provisioned_config(&profile_id).await?;
let removed = credentials::clear(&config)?;
log::info!("[profiles] credential cleared for profile={profile_id} removed={removed}");
let log = if removed {
@@ -149,6 +151,32 @@ pub(crate) fn clear_credential_on(
))
}
+/// Close profile `profile_id` on this node and release its lease, so another
+/// node can host it at once.
+pub async fn release(profile_id: &str) -> Result<Outcome<ReleaseResult>, String> {
+ release_on(&*require_host()?, profile_id).await
+}
+
+pub(crate) async fn release_on(
+ host: &ProfileHost,
+ profile_id: &str,
+) -> Result<Outcome<ReleaseResult>, String> {
+ let profile_id = ProfileId::parse(profile_id)?;
+ let released = host.release(&profile_id).await?;
+ let log = if released {
+ format!("released {profile_id}")
+ } else {
+ format!("{profile_id} was not open on this node")
+ };
+ Ok(Outcome::single_log(
+ ReleaseResult {
+ profile_id,
+ released,
+ },
+ log,
+ ))
+}
+
#[cfg(test)]
#[path = "ops_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/profiles/schemas.rs b/crates/openhuman-core/src/profiles/schemas.rs
index 4cab769bfd..a3b7184dcc 100644
--- a/crates/openhuman-core/src/profiles/schemas.rs
+++ b/crates/openhuman-core/src/profiles/schemas.rs
@@ -31,13 +31,14 @@ struct CredentialParams {
expires_at: Option<String>,
}
-const FUNCTIONS: [&str; 6] = [
+const FUNCTIONS: [&str; 7] = [
"provision",
"deprovision",
"list",
"status",
"set_credential",
"clear_credential",
+ "release",
];
pub fn all_profiles_controller_schemas() -> Vec<ControllerSchema> {
@@ -70,6 +71,10 @@ pub fn all_profiles_registered_controllers() -> Vec<RegisteredController> {
schema: profiles_schemas("clear_credential"),
handler: handle_clear_credential,
},
+ RegisteredController {
+ schema: profiles_schemas("release"),
+ handler: handle_release,
+ },
]
}
@@ -162,6 +167,17 @@ pub fn profiles_schemas(function: &str) -> ControllerSchema {
bool_field("has_credential", "Always false on success."),
],
},
+ "release" => ControllerSchema {
+ namespace: "profiles",
+ function: "release",
+ description: "Close a profile on this node and release its lease, so another node can host it at once. \
+ Refused while the profile is in use.",
+ inputs: vec![string_field("profile_id", "The profile to release.")],
+ outputs: vec![
+ string_field("profile_id", "The profile."),
+ bool_field("released", "False when it was not open on this node."),
+ ],
+ },
_ => ControllerSchema {
namespace: "profiles",
function: "unknown",
@@ -175,25 +191,25 @@ pub fn profiles_schemas(function: &str) -> ControllerSchema {
fn handle_provision(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let payload = deserialize_params::<UserParams>(params)?;
- to_json(super::ops::provision(&payload.user_id)?)
+ to_json(super::ops::provision(&payload.user_id).await?)
})
}
fn handle_deprovision(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let payload = deserialize_params::<ProfileParams>(params)?;
- to_json(super::ops::deprovision(&payload.profile_id)?)
+ to_json(super::ops::deprovision(&payload.profile_id).await?)
})
}
fn handle_list(_params: Map<String, Value>) -> ControllerFuture {
- Box::pin(async move { to_json(super::ops::list()?) })
+ Box::pin(async move { to_json(super::ops::list().await?) })
}
fn handle_status(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let payload = deserialize_params::<ProfileParams>(params)?;
- to_json(super::ops::status(&payload.profile_id)?)
+ to_json(super::ops::status(&payload.profile_id).await?)
})
}
@@ -205,14 +221,22 @@ fn handle_set_credential(params: Map<String, Value>) -> ControllerFuture {
payload.kind,
&payload.token,
payload.expires_at.as_deref(),
- )?)
+ )
+ .await?)
})
}
fn handle_clear_credential(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let payload = deserialize_params::<ProfileParams>(params)?;
- to_json(super::ops::clear_credential(&payload.profile_id)?)
+ to_json(super::ops::clear_credential(&payload.profile_id).await?)
+ })
+}
+
+fn handle_release(params: Map<String, Value>) -> ControllerFuture {
+ Box::pin(async move {
+ let payload = deserialize_params::<ProfileParams>(params)?;
+ to_json(super::ops::release(&payload.profile_id).await?)
})
}
diff --git a/crates/openhuman-core/src/profiles/types.rs b/crates/openhuman-core/src/profiles/types.rs
index 5efbfeb8f3..cd56445c7f 100644
--- a/crates/openhuman-core/src/profiles/types.rs
+++ b/crates/openhuman-core/src/profiles/types.rs
@@ -161,6 +161,14 @@ pub struct DeprovisionResult {
pub removed: bool,
}
+/// What [`release`](super::ops::release) did.
+#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
+pub struct ReleaseResult {
+ pub profile_id: ProfileId,
+ /// `false` when the profile was not open on this node.
+ pub released: bool,
+}
+
/// One provisioned profile, as the operator plane sees it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ProfileSummary {