Skip to content

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

Merge origin/saas-tenant-key into saas-profile-host

Steven Enamakel01919d64ebea
diff --cc crates/openhuman-core/src/core/runtime/README.md
index a52fc97b53,d9af8f12f1..16543028b9
--- a/crates/openhuman-core/src/core/runtime/README.md
+++ b/crates/openhuman-core/src/core/runtime/README.md
@@@ -172,17 -172,16 +172,17 @@@ through `runtime/saas.rs`
bearer is the RPC token.
The SaaS presets are closed. `DomainSet::saas()` registers the operator plane
-(`DomainGroup::Operator`, the `user_agents.*` controllers) and the user
+(`DomainGroup::Operator`, the `profiles.*` controllers) and the user
families whose per-user isolation has landed (threads, channels for web chat,
-memory). `user_agents::surface`
+memory). `profiles::surface`
keeps the two planes apart: the operator scope reaches only the operator
plane, and a user's scope only the reviewed `USER_METHODS`.
-`saas::build` installs the process's `user_agents::AgentHost`. Each open user
-agent runs under a context derived from the operator's, with its own forced
-config and `session_agent` (see `user_agents/README.md`).
+`saas::build` installs the process's `profiles::ProfileHost`. Each open
+profile (`<root>/users/<profile-id>/`, the desktop's user layout) runs under a
+context derived from the operator's, with its own forced config and
+`session_agent` (see `profiles/README.md`).
- Two guards keep SaaS work from falling back to process-wide state:
+ Three guards keep SaaS work from falling back to process-wide state:
- **Config redirect.** In SaaS, `Config::load_or_init()` returns the config the
current context carries and fails outside any context
diff --cc crates/openhuman-core/src/profiles/host.rs
index 47f7bebccb,d41a022edc..5778735f30
--- a/crates/openhuman-core/src/profiles/host.rs
+++ b/crates/openhuman-core/src/profiles/host.rs
@@@ -127,18 -129,18 +127,18 @@@ impl ProfileHost
Ok(true)
}
- /// Close agent `id` and archive its state under `<root>/deprovisioned/`.
- /// Nothing is deleted. Returns whether there was such an agent.
+ /// Close profile `id` and archive its state under `<root>/deprovisioned/`.
+ /// Nothing is deleted. Returns whether there was such a profile.
///
- /// The open-agent lock is held for the whole operation, so no `open` can
- /// re-open the agent between closing it and moving its directory. An
- /// agent still in use (a request holds its state) is not archived from
+ /// The open-profile lock is held for the whole operation, so no `open` can
+ /// re-open the profile between closing it and moving its directory. An
+ /// profile still in use (a request holds its state) is not archived from
/// under it: deprovisioning fails and can be retried.
- pub fn deprovision(&self, id: &UserAgentId) -> Result<bool, String> {
+ pub fn deprovision(&self, id: &ProfileId) -> Result<bool, String> {
let mut open = self.lock();
if let Some(slot) = open.get(id) {
- if Arc::strong_count(&slot.state) > 1 {
+ if in_use(slot) {
- return Err(format!("agent {id} is in use; try again shortly"));
+ return Err(format!("profile {id} is in use; try again shortly"));
}
}
open.remove(id);
@@@ -216,9 -218,12 +216,12 @@@
let context = self.operator.derive_with(
ContextOverlay::new(config.clone(), user_domains(), ToolGroups::none())
.without_user_skill_roots()
- .session_agent(id.as_str()),
+ .session_agent(id.as_str())
+ .profile(id.as_str())
+ .agent_policy(profile_policy(&config)),
);
+ crate::platform::cost::seed_tenant_tracker(&context, &config);
- let state = Arc::new(UserAgentState {
+ let state = Arc::new(Profile {
id: id.clone(),
layout,
config,
@@@ -300,24 -305,22 +303,22 @@@
self.evict_locked(&mut open, Instant::now());
}
- /// Close agents idle past `idle_evict_secs` that nothing is using.
- fn sweep_idle_locked(&self, open: &mut HashMap<UserAgentId, Slot>, now: Instant) {
+ /// Close profiles idle past `idle_evict_secs` that nothing is using.
+ fn sweep_idle_locked(&self, open: &mut HashMap<ProfileId, Slot>, now: Instant) {
let idle_limit = Duration::from_secs(self.saas.idle_evict_secs);
open.retain(|id, slot| {
- let keep = Arc::strong_count(&slot.state) > 1
- || now.duration_since(slot.last_used) < idle_limit;
+ let keep = in_use(slot) || now.duration_since(slot.last_used) < idle_limit;
if !keep {
- log::debug!("[user_agents] evicted idle agent={id}");
+ log::debug!("[profiles] evicted idle profile={id}");
}
keep
});
}
- fn evict_locked(&self, open: &mut HashMap<UserAgentId, Slot>, now: Instant) {
+ fn evict_locked(&self, open: &mut HashMap<ProfileId, Slot>, now: Instant) {
- let in_use = |slot: &Slot| Arc::strong_count(&slot.state) > 1;
self.sweep_idle_locked(open, now);
// Still full: make room by closing the least recently used idle one.
- if open.len() >= self.saas.max_agents_open.max(1) {
+ if open.len() >= self.saas.max_profiles_open.max(1) {
let victim = open
.iter()
.filter(|(_, slot)| !in_use(slot))
@@@ -335,11 -338,31 +336,31 @@@
}
}
-/// Whether anything still uses an open agent: a request holding its state,
++/// Whether anything still uses an open profile: a request holding its state,
+ /// or a turn still running on its context (a detached turn outlives the
+ /// request that started it).
+ fn in_use(slot: &Slot) -> bool {
+ Arc::strong_count(&slot.state) > 1 || slot.state.context.tenant_in_use()
+ }
+
-/// The security policy agent work is gated by: its own forced autonomy over
++/// The security policy profile work is gated by: its own forced autonomy over
+ /// its workspace and sandbox, never the operator's live policy.
+ fn profile_policy(config: &Config) -> Arc<crate::security::SecurityPolicy> {
+ Arc::new(
+ crate::security::SecurityPolicy::from_config(
+ &config.autonomy,
+ &config.workspace_dir,
+ &config.action_dir,
+ )
+ .with_privacy_mode(config.privacy.mode),
+ )
+ }
+
-/// Settle what a previous process left in agent `id`'s workspace: turns that
+/// Settle what a previous process left in profile `id`'s workspace: turns that
/// were mid-flight become interrupted and run-ledger rows left running are
-/// closed. The sweep a single-user core runs at boot, run per agent on its
-/// first open in this process. Failures are logged; the agent still opens.
-pub(crate) fn recover_workspace(id: &UserAgentId, workspace_dir: &std::path::Path) {
+/// closed. The sweep a single-user core runs at boot, run per profile on its
+/// first open in this process. Failures are logged; the profile still opens.
+pub(crate) fn recover_workspace(id: &ProfileId, workspace_dir: &std::path::Path) {
let now = chrono::Utc::now().to_rfc3339();
match tinyagents_session::turn_state::store::mark_all_interrupted(
workspace_dir.to_path_buf(),
@@@ -371,10 -394,10 +392,10 @@@ pub fn host() -> Option<Arc<ProfileHost
HOST.get().cloned()
}
-/// The user agent the current work runs for, if any.
-pub fn current() -> Option<Arc<UserAgentState>> {
+/// The profile the current work runs for, if any.
+pub fn current() -> Option<Arc<Profile>> {
- let profile = CoreContext::current()?.session_agent()?.to_owned();
+ let profile = crate::core::runtime::current_tenant().ok()?.profile?;
- let id = UserAgentId::parse(&profile).ok()?;
+ let id = ProfileId::parse(&profile).ok()?;
host()?.get(&id)
}
diff --cc crates/openhuman-core/src/profiles/host_tests.rs
index 5a1111e2db,1da470faa1..bd7319a7c4
--- a/crates/openhuman-core/src/profiles/host_tests.rs
+++ b/crates/openhuman-core/src/profiles/host_tests.rs
@@@ -255,5 -259,60 +255,60 @@@ fn a_provisioned_config_needs_no_profil
let _held = host.open(&a).unwrap();
assert!(host.open(&b).is_err(), "the only slot is taken");
assert!(host.provisioned_config(&b).is_ok());
- assert!(host.provisioned_config(&agent("nobody")).is_err());
+ assert!(host.provisioned_config(&profile("nobody")).is_err());
}
+
+ #[tokio::test]
-async fn an_open_agent_is_gated_by_its_own_policy_not_the_operators() {
++async fn an_open_profile_is_gated_by_its_own_policy_not_the_operators() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp, 4, 60);
- let id = agent("alice");
++ let id = profile("alice");
+ host.provision(&id).unwrap();
+ let state = host.open(&id).unwrap();
+ assert_eq!(state.context().profile(), Some(id.as_str()));
+
+ let own = state
+ .context()
+ .agent_policy()
+ .expect("the profile carries a policy");
+ assert!(own.enabled, "the forced autonomy policy is on");
+ assert!(own.workspace_only);
+ let effective = CoreContext::scope(Arc::clone(state.context()), async {
+ crate::security::live_policy::effective()
+ })
+ .await
+ .expect("a policy is in effect");
+ assert!(
+ Arc::ptr_eq(&effective, &own),
+ "inside the profile's scope the gate answers with its policy"
+ );
+ }
+
+ #[tokio::test]
-async fn an_agent_with_a_live_turn_is_not_evicted() {
++async fn a_profile_with_a_live_turn_is_not_evicted() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp, 1, 0);
- let (a, b) = (agent("a"), agent("b"));
++ let (a, b) = (profile("a"), profile("b"));
+ host.provision(&a).unwrap();
+ host.provision(&b).unwrap();
+ // The request that started the turn has answered and let go of the
- // state; the turn keeps running on a context derived from the agent's.
++ // state; the turn keeps running on a context derived from the profile's.
+ let context = Arc::clone(host.open(&a).unwrap().context());
+ let (release, released) = tokio::sync::oneshot::channel::<()>();
+ let turn = tokio::spawn(CoreContext::scope_with_turn_origin(context, None, async {
+ let _ = released.await;
+ }));
+ tokio::task::yield_now().await;
+
+ host.evict_idle();
- assert!(host.is_open(&a), "a live turn keeps its agent open");
++ assert!(host.is_open(&a), "a live turn keeps its profile open");
+ let err = host.open(&b).unwrap_err();
+ assert!(err.contains("slots are in use"), "{err}");
+ assert!(host.deprovision(&a).unwrap_err().contains("in use"));
+
+ release.send(()).unwrap();
+ turn.await.unwrap();
+ host.evict_idle();
+ assert!(!host.is_open(&a), "evicted once the turn ended");
+ host.open(&b).unwrap();
+ }
diff --cc tests/saas_mode_e2e.rs
index ca757b4320,6271310b50..2bb6617573
--- a/tests/saas_mode_e2e.rs
+++ b/tests/saas_mode_e2e.rs
@@@ -435,6 -428,24 +435,24 @@@ fn gateway_requests_run_under_the_named
assert_eq!(status, 401, "alice's signature does not cover bob: {body}");
let (status, body) = user_rpc(&client, &base, BEARER, "bob", None, "core.ping");
assert_eq!(status, 403, "bob is not provisioned: {body}");
+ // A repeated signature header is refused, not resolved to the first value.
+ {
- use openhuman_core::user_agents::gateway::{sign, USER_HEADER, USER_SIG_HEADER};
++ use openhuman_core::profiles::gateway::{sign, USER_HEADER, USER_SIG_HEADER};
+ let response = client
+ .post(format!("{base}/rpc"))
+ .bearer_auth(BEARER)
+ .header(USER_HEADER, "alice")
+ .header(USER_SIG_HEADER, sign(BEARER, "alice", now()))
+ .header(USER_SIG_HEADER, sign(BEARER, "alice", now()))
+ .json(&json!({ "jsonrpc": "2.0", "id": 1, "method": "core.ping", "params": {} }))
+ .send()
+ .expect("POST /rpc");
+ assert_eq!(
+ response.status().as_u16(),
+ 400,
+ "a repeated signature header"
+ );
+ }
// Single-user surfaces are closed.
for path in ["/events", "/events/domain", "/v1/models", "/dev/connect"] {