Skip to content

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

feat(runtime): track profile admission lineage across derived contexts

Steven Enamakelaa2405e76a44
diff --git a/crates/openhuman-core/src/core/runtime/context.rs b/crates/openhuman-core/src/core/runtime/context.rs
index 8714008268..a3583eda48 100644
--- a/crates/openhuman-core/src/core/runtime/context.rs
+++ b/crates/openhuman-core/src/core/runtime/context.rs
@@ -123,6 +123,10 @@ pub struct CoreContext {
/// The tenant (SaaS profile) this context serves. `None` for booted
/// contexts and every single-user agent. See [`crate::core::runtime::tenant`].
profile: Option<String>,
+ /// Private admission generation shared only by contexts derived within
+ /// this profile generation. Profile reopen creates a fresh context from
+ /// the host root and therefore a fresh token.
+ profile_admission: Arc<()>,
/// What the agent this context was derived for owns.
agent: agent_parts::AgentParts,
/// A context that exists only to carry a turn origin where no core was
@@ -307,6 +311,7 @@ impl CoreContext {
channel_thread: None,
session_agent: None,
profile: None,
+ profile_admission: Arc::new(()),
agent: Default::default(),
bare: false,
});
@@ -386,6 +391,13 @@ impl CoreContext {
self.derive_with_parent_config_inner(Some(parent_config), overlay)
}
+ /// Whether `other` descends from the same admitted profile context
+ /// generation. This differs from context pointer identity: turn contexts
+ /// may be derived while the profile remains admitted.
+ pub(crate) fn shares_profile_admission(&self, other: &Self) -> bool {
+ Arc::ptr_eq(&self.profile_admission, &other.profile_admission)
+ }
+
fn derive_with_parent_config_inner(
&self,
parent_config: Option<&crate::config::Config>,
@@ -444,6 +456,15 @@ impl CoreContext {
if let Some(snapshot) = &latest_embedder_config {
crate::config::Config::bind_context_snapshot(snapshot);
}
+ let profile_admission = if overlay
+ .profile
+ .as_deref()
+ .is_none_or(|profile| Some(profile) == self.profile())
+ {
+ Arc::clone(&self.profile_admission)
+ } else {
+ Arc::new(())
+ };
Arc::new(CoreContext {
host_kind: self.host_kind,
workspace_binding: RwLock::new(shared_binding),
@@ -465,6 +486,7 @@ impl CoreContext {
channel_thread: self.channel_thread.clone(),
session_agent: overlay.session_agent.or_else(|| self.session_agent.clone()),
profile: overlay.profile.or_else(|| self.profile.clone()),
+ profile_admission,
agent,
bare: self.bare,
})
diff --git a/crates/openhuman-core/src/core/runtime/context_for_agent.rs b/crates/openhuman-core/src/core/runtime/context_for_agent.rs
index 897a962608..31165701e2 100644
--- a/crates/openhuman-core/src/core/runtime/context_for_agent.rs
+++ b/crates/openhuman-core/src/core/runtime/context_for_agent.rs
@@ -41,6 +41,7 @@ impl CoreContext {
channel_thread: self.channel_thread.clone(),
session_agent: Some(agent.to_string()),
profile: self.profile.clone(),
+ profile_admission: Arc::clone(&self.profile_admission),
agent: parts,
bare: self.bare,
})
diff --git a/crates/openhuman-core/src/core/runtime/context_test_support.rs b/crates/openhuman-core/src/core/runtime/context_test_support.rs
index 12d2a8f3c9..a7fd4c346d 100644
--- a/crates/openhuman-core/src/core/runtime/context_test_support.rs
+++ b/crates/openhuman-core/src/core/runtime/context_test_support.rs
@@ -31,6 +31,7 @@ impl CoreContext {
channel_thread: None,
session_agent: None,
profile: None,
+ profile_admission: Arc::new(()),
agent: Default::default(),
bare: false,
})
@@ -95,6 +96,7 @@ impl CoreContext {
channel_thread: None,
session_agent: None,
profile: None,
+ profile_admission: Arc::new(()),
agent: Default::default(),
bare: false,
})
diff --git a/crates/openhuman-core/src/core/runtime/context_tests.rs b/crates/openhuman-core/src/core/runtime/context_tests.rs
index 075d4e6df1..e267f72759 100644
--- a/crates/openhuman-core/src/core/runtime/context_tests.rs
+++ b/crates/openhuman-core/src/core/runtime/context_tests.rs
@@ -20,6 +20,7 @@ fn ctx(dir: &str) -> Arc<CoreContext> {
channel_thread: None,
session_agent: None,
profile: None,
+ profile_admission: Arc::new(()),
agent: Default::default(),
bare: false,
})
@@ -211,6 +212,44 @@ fn derive_with_defaults_to_visible_user_skill_roots() {
assert!(ctx("/tmp/ws").derive_with(overlay).user_skill_roots());
}
+#[test]
+fn profile_admission_lineage_is_inherited_only_within_the_same_profile() {
+ let root = ctx("/tmp/profile-root");
+ let profile = root.derive_with(
+ ContextOverlay::new(
+ crate::config::Config::default(),
+ crate::core::runtime::DomainSet::full(),
+ Default::default(),
+ )
+ .profile("profile-a"),
+ );
+ let derived = profile.derive_with(ContextOverlay::new(
+ crate::config::Config::default(),
+ crate::core::runtime::DomainSet::full(),
+ Default::default(),
+ ));
+ let same_profile = profile.derive_with(
+ ContextOverlay::new(
+ crate::config::Config::default(),
+ crate::core::runtime::DomainSet::full(),
+ Default::default(),
+ )
+ .profile("profile-a"),
+ );
+ let switched = profile.derive_with(
+ ContextOverlay::new(
+ crate::config::Config::default(),
+ crate::core::runtime::DomainSet::full(),
+ Default::default(),
+ )
+ .profile("profile-b"),
+ );
+
+ assert!(profile.shares_profile_admission(&derived));
+ assert!(profile.shares_profile_admission(&same_profile));
+ assert!(!profile.shares_profile_admission(&switched));
+}
+
#[tokio::test]
async fn two_derived_contexts_serve_their_own_config_to_the_dispatch() {
// The read path `load_config_with_timeout` uses. Two agents scoped one
@@ -383,6 +422,7 @@ fn degraded_context_rejects_workspace_bound_stores() {
channel_thread: None,
session_agent: None,
profile: None,
+ profile_admission: Arc::new(()),
agent: Default::default(),
bare: false,
};
diff --git a/crates/openhuman-core/src/core/runtime/context_turn_origin.rs b/crates/openhuman-core/src/core/runtime/context_turn_origin.rs
index c4f85620cc..e7a35737b5 100644
--- a/crates/openhuman-core/src/core/runtime/context_turn_origin.rs
+++ b/crates/openhuman-core/src/core/runtime/context_turn_origin.rs
@@ -50,6 +50,7 @@ impl CoreContext {
channel_thread: None,
session_agent: None,
profile: None,
+ profile_admission: Arc::new(()),
agent: Default::default(),
bare: true,
})
@@ -115,6 +116,7 @@ impl CoreContext {
channel_thread: thread.or_else(|| ctx.channel_thread.clone()),
session_agent: ctx.session_agent.clone(),
profile: ctx.profile.clone(),
+ profile_admission: Arc::clone(&ctx.profile_admission),
agent: ctx.agent.clone(),
bare: ctx.bare,
})
diff --git a/crates/openhuman-core/src/modules/storage_import_support.rs b/crates/openhuman-core/src/modules/storage_import_support.rs
index bc93ec9501..ea4c2ec93b 100644
--- a/crates/openhuman-core/src/modules/storage_import_support.rs
+++ b/crates/openhuman-core/src/modules/storage_import_support.rs
@@ -96,7 +96,10 @@ pub(super) fn require_profile_context(
caller: &Arc<crate::core::runtime::CoreContext>,
profile_id: &str,
) -> Result<()> {
- if Arc::ptr_eq(admitted, caller) {
+ if admitted.profile() == Some(profile_id)
+ && caller.profile() == Some(profile_id)
+ && admitted.shares_profile_admission(caller)
+ {
Ok(())
} else {
Err(anyhow!(
diff --git a/crates/openhuman-core/src/modules/storage_import_tinymcp_tests.rs b/crates/openhuman-core/src/modules/storage_import_tinymcp_tests.rs
index 2365c7a5bb..fde1370864 100644
--- a/crates/openhuman-core/src/modules/storage_import_tinymcp_tests.rs
+++ b/crates/openhuman-core/src/modules/storage_import_tinymcp_tests.rs
@@ -63,7 +63,8 @@ struct Rig {
fn rig() -> Rig {
let dir = tempfile::tempdir().unwrap();
let url = format!("sqlite:{}", dir.path().join("storage.sqlite").display());
- let backend = crate::storage::block_on(crate::storage::open(&url)).unwrap();
+ let backend =
+ crate::storage::block_on(async move { crate::storage::open(&url).await }).unwrap();
Rig {
_dir: dir,
backend,
@@ -268,21 +269,42 @@ async fn an_unreadable_file_writes_no_marker_so_the_next_start_retries() {
#[test]
fn a_context_from_before_profile_takeover_cannot_import_under_the_new_lease() {
- use crate::core::runtime::{CoreContext, DomainSet};
+ use crate::core::runtime::{ContextOverlay, CoreContext, DomainSet};
+
+ fn profile_context() -> Arc<CoreContext> {
+ CoreContext::for_test(DomainSet::full(), None).derive_with(
+ ContextOverlay::new(
+ crate::config::Config::default(),
+ DomainSet::full(),
+ Default::default(),
+ )
+ .profile("profile-1"),
+ )
+ }
- let old_context = CoreContext::for_test(DomainSet::full(), None);
- let reopened_context = CoreContext::for_test(DomainSet::full(), None);
+ let old_context = profile_context();
+ let old_turn = old_context.derive_with(ContextOverlay::new(
+ crate::config::Config::default(),
+ DomainSet::full(),
+ Default::default(),
+ ));
+ let reopened_context = profile_context();
+ let current_turn = reopened_context.derive_with(ContextOverlay::new(
+ crate::config::Config::default(),
+ DomainSet::full(),
+ Default::default(),
+ ));
assert!(
super::super::storage_import_support::require_profile_context(
&reopened_context,
- &reopened_context,
+ &current_turn,
"profile-1"
)
.is_ok()
);
let error = super::super::storage_import_support::require_profile_context(
&reopened_context,
- &old_context,
+ &old_turn,
"profile-1",
)
.unwrap_err();
@@ -311,6 +333,11 @@ async fn an_import_cannot_recapture_a_reopened_profiles_new_fence() {
let old_profile = host.open(&id).await.unwrap();
let old_context = Arc::clone(old_profile.context());
+ let old_turn = old_context.derive_with(crate::core::runtime::ContextOverlay::new(
+ crate::config::Config::default(),
+ DomainSet::full(),
+ Default::default(),
+ ));
drop(old_profile);
assert!(host.release(&id).await.unwrap());
let current_profile = host.open(&id).await.unwrap();
@@ -322,7 +349,7 @@ async fn an_import_cannot_recapture_a_reopened_profiles_new_fence() {
&host,
&id,
id.as_str(),
- &old_context,
+ &old_turn,
module_scope.clone(),
);
assert!(
@@ -330,11 +357,16 @@ async fn an_import_cannot_recapture_a_reopened_profiles_new_fence() {
"old dispatch context cannot borrow the new fence"
);
+ let current_turn = current_context.derive_with(crate::core::runtime::ContextOverlay::new(
+ crate::config::Config::default(),
+ DomainSet::full(),
+ Default::default(),
+ ));
let captured = super::super::storage_import_support::capture_profile_module_storage(
&host,
&id,
id.as_str(),
- &current_context,
+ &current_turn,
module_scope,
)
.unwrap();
diff --git a/crates/openhuman-core/src/storage/import/mod_tests.rs b/crates/openhuman-core/src/storage/import/mod_tests.rs
index 0122661484..fc50b7a569 100644
--- a/crates/openhuman-core/src/storage/import/mod_tests.rs
+++ b/crates/openhuman-core/src/storage/import/mod_tests.rs
@@ -1,13 +1,13 @@
#![cfg(feature = "storage-sqlite")]
use super::*;
+use crate::storage::DocumentStoreExt as _;
use crate::storage::Scope;
use crate::storage::rusqlite;
use crate::storage::test_support::{SqliteBackend, sqlite_backend};
use std::collections::BTreeMap;
use std::ops::Deref;
use tempfile::TempDir;
-use crate::storage::DocumentStoreExt as _;
use tinystoragedrivers::Query;
struct TestBackend(SqliteBackend);
@@ -368,7 +368,8 @@ fn records_the_previous_release_wrote_as_documents_are_imported_and_win_over_leg
{
use tinystoragedrivers::StorageBackend as _;
let url = format!("sqlite:{}", db.display());
- let storage = crate::storage::block_on(crate::storage::open(&url)).unwrap();
+ let storage =
+ crate::storage::block_on(async move { crate::storage::open(&url).await }).unwrap();
let docs = Arc::clone(storage.for_scope(&Scope::local()).unwrap().documents());
crate::storage::block_on(async move {
docs.ensure_collection(&(source.collections)().remove(0))
diff --git a/crates/openhuman-core/src/storage/import_state_tests.rs b/crates/openhuman-core/src/storage/import_state_tests.rs
index a30e85b751..e842e19eab 100644
--- a/crates/openhuman-core/src/storage/import_state_tests.rs
+++ b/crates/openhuman-core/src/storage/import_state_tests.rs
@@ -21,7 +21,8 @@ fn sqlite_repo() -> (tempfile::TempDir, Arc<dyn StorageBackend>, Repo) {
let dir = tempfile::tempdir().unwrap();
let database = dir.path().join("claims.sqlite");
let url = format!("sqlite:{}", database.display());
- let backend = crate::storage::block_on(crate::storage::open(&url)).unwrap();
+ let backend =
+ crate::storage::block_on(async move { crate::storage::open(&url).await }).unwrap();
let repo = Repo::on(&backend, &Scope::local(), "claim-test", spec).unwrap();
(dir, backend, repo)
}
@@ -137,7 +138,8 @@ fn many_racing_processes_run_the_body_once_at_a_time() {
.collect();
let ran = handles
.into_iter()
- .filter(|handle| handle.join().unwrap())
+ .map(|handle| handle.join().unwrap())
+ .filter(|ran| *ran)
.count();
assert!(ran >= 1, "somebody imports");
assert!(!overlap.load(std::sync::atomic::Ordering::SeqCst));