Skip to content

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

fix: harden session store runtime ownership

Steven Enamakel75c3b5c65137
diff --git a/crates/openhuman-core/src/agent/session_store/mod.rs b/crates/openhuman-core/src/agent/session_store/mod.rs
index 09c48b1a23..5a2f574f88 100644
--- a/crates/openhuman-core/src/agent/session_store/mod.rs
+++ b/crates/openhuman-core/src/agent/session_store/mod.rs
@@ -63,6 +63,25 @@ pub fn clear() -> bool {
had
}
+/// Clears the installed provider only when it is still the provider claimed
+/// by the caller. This keeps a runtime from removing a replacement installed
+/// by another owner after the runtime itself has been dropped.
+pub fn clear_if(expected: &Arc<dyn SessionStoreProvider>) -> bool {
+ let mut installed = PROVIDER
+ .write()
+ .unwrap_or_else(PoisonError::into_inner);
+ if installed
+ .as_ref()
+ .is_some_and(|current| Arc::ptr_eq(current, expected))
+ {
+ installed.take();
+ tracing::info!("[session_store] host session store removed");
+ true
+ } else {
+ false
+ }
+}
+
pub fn restore(provider: Option<Arc<dyn SessionStoreProvider>>) {
*PROVIDER.write().unwrap_or_else(PoisonError::into_inner) = provider;
}
diff --git a/crates/openhuman-core/src/core/runtime/context.rs b/crates/openhuman-core/src/core/runtime/context.rs
index 3c5a3696e2..b358412497 100644
--- a/crates/openhuman-core/src/core/runtime/context.rs
+++ b/crates/openhuman-core/src/core/runtime/context.rs
@@ -228,7 +228,7 @@ impl CoreContext {
// 2. Load the master encryption key before any config/credential op that
// needs to decrypt secrets. No-op if already called (e.g. from
// run_core_from_args for the CLI).
- crate::security::keyring::init_master_key();
+ crate::security::keyring::init_master_key().map_err(anyhow::Error::msg)?;
// 4. Seed the per-process RPC bearer. `Fixed` seeds the in-memory value
// directly (never touches the env); `EnvOrFile` reads
@@ -306,18 +306,13 @@ impl CoreContext {
};
let workspace_dir = config.as_ref().map(|cfg| cfg.workspace_dir.clone());
- // 6. Long-lived runtime infrastructure: event bus, domain subscribers,
- // ledgers, agent-definition registry, live security policy, approval
- // gate, socket manager. Idempotent (Once-guarded internally). Selected
- // background jobs start later, from CoreRuntime::start_services(), after a transport binds
- // succeeds.
- let runtime_config = config.clone();
- super::bootstrap::bootstrap_core_runtime(host_kind, config, domains).await;
-
+ // Construct and publish the context before bootstrap. Host-installed
+ // session providers may resolve their cold-boot recovery workspace via
+ // CoreContext, which does not exist until this point.
let ctx = Arc::new(CoreContext {
host_kind,
workspace_binding: RwLock::new(Arc::new(RwLock::new(WorkspaceBinding {
- workspace_dir,
+ workspace_dir: workspace_dir.clone(),
}))),
domains,
tool_groups,
@@ -327,11 +322,16 @@ impl CoreContext {
turn_origin: None,
session_agent: None,
});
-
- // Register the process default context (first build wins). Dispatch
- // resolves to this when no per-call context is scoped.
let _ = DEFAULT_CONTEXT.set(ctx.clone());
+ // 6. Long-lived runtime infrastructure: event bus, domain subscribers,
+ // ledgers, agent-definition registry, live security policy, approval
+ // gate, socket manager. Idempotent (Once-guarded internally). Selected
+ // background jobs start later, from CoreRuntime::start_services(), after a transport binds
+ // succeeds.
+ let runtime_config = config.clone();
+ super::bootstrap::bootstrap_core_runtime(host_kind, config, domains).await;
+
Ok((ctx, has_operator_token, runtime_config))
}
diff --git a/crates/openhuman-embed/src/runtime/builder.rs b/crates/openhuman-embed/src/runtime/builder.rs
index 4f3185fbc2..77b506ba7f 100644
--- a/crates/openhuman-embed/src/runtime/builder.rs
+++ b/crates/openhuman-embed/src/runtime/builder.rs
@@ -20,6 +20,22 @@ use crate::harness::workspace::ResolvedWorkspace;
use crate::harness::{Access, Provider, Workspace};
use crate::{Core, Session};
+struct SessionStoreCleanup {
+ installed: Option<Arc<dyn SessionStoreProvider>>,
+ previous: Option<Arc<dyn SessionStoreProvider>>,
+}
+
+impl Drop for SessionStoreCleanup {
+ fn drop(&mut self) {
+ let Some(installed) = self.installed.take() else {
+ return;
+ };
+ if openhuman_core::agent::session_store::clear_if(&installed) {
+ openhuman_core::agent::session_store::restore(self.previous.take());
+ }
+ }
+}
+
/// Builder for a [`Runtime`]. Obtain with [`Runtime::builder`].
pub struct RuntimeBuilder {
workspace: Workspace,
@@ -244,7 +260,6 @@ impl RuntimeBuilder {
if let Some(engine) = self.memory_engine.clone() {
openhuman_core::memory::engine::install_host_engine(engine);
}
- let session_store = self.session_store.is_some();
let inherit = self.workspace.is_operator_owned();
let resolved = ResolvedWorkspace::resolve(&self.workspace, None).map_err(map_ws)?;
@@ -313,10 +328,14 @@ impl RuntimeBuilder {
// Install only after all fallible workspace/config resolution has
// completed; core boot performs recovery against this provider.
- let previous_session_store = self
- .session_store
+ let installed_session_store = self.session_store.clone();
+ let previous_session_store = installed_session_store
.clone()
.and_then(openhuman_core::agent::session_store::install);
+ let mut session_store_cleanup = SessionStoreCleanup {
+ installed: installed_session_store,
+ previous: previous_session_store,
+ };
log::debug!(
"[embed][runtime] building host_kind={:?} inherit_workspace={inherit} \
@@ -337,10 +356,6 @@ impl RuntimeBuilder {
let runtime = match builder.build().await {
Ok(runtime) => runtime,
Err(error) => {
- if session_store {
- openhuman_core::agent::session_store::clear();
- openhuman_core::agent::session_store::restore(previous_session_store);
- }
return Err(RuntimeError::Build(error));
}
};
@@ -348,18 +363,17 @@ impl RuntimeBuilder {
if let Some(session) = self.session {
if let Err(error) = core.auth().store(session).await {
- if session_store {
- openhuman_core::agent::session_store::clear();
- openhuman_core::agent::session_store::restore(previous_session_store);
- }
return Err(error.into());
}
}
+ let installed_session_store = session_store_cleanup.installed.take();
+ let previous_session_store = session_store_cleanup.previous.take();
+
Ok(Runtime::new(
core,
resolved,
- session_store,
+ installed_session_store,
previous_session_store,
config,
inherit,
diff --git a/crates/openhuman-embed/src/runtime/mod.rs b/crates/openhuman-embed/src/runtime/mod.rs
index 309434d3f8..a9641696b2 100644
--- a/crates/openhuman-embed/src/runtime/mod.rs
+++ b/crates/openhuman-embed/src/runtime/mod.rs
@@ -153,9 +153,9 @@ pub(crate) struct CoreGuard {
/// Held for its `Drop`: an ephemeral workspace lives exactly as long as
/// the last owner of this guard.
workspace: ResolvedWorkspace,
- /// Whether this runtime installed the process's session store, which is
- /// removed with it.
- session_store: bool,
+ /// The provider this runtime installed, if any. It is removed only while
+ /// it still owns the process slot.
+ session_store: Option<Arc<dyn openhuman_core::agent::session_store::SessionStoreProvider>>,
previous_session_store:
Option<Arc<dyn openhuman_core::agent::session_store::SessionStoreProvider>>,
}
@@ -166,9 +166,10 @@ impl Drop for CoreGuard {
// first lets another builder initialize process-scoped state while
// this runtime's keyring, bearer, event bus and subscribers are live.
drop(self.core.take());
- if self.session_store {
- openhuman_core::agent::session_store::clear();
- openhuman_core::agent::session_store::restore(self.previous_session_store.take());
+ if let Some(installed) = self.session_store.take() {
+ if openhuman_core::agent::session_store::clear_if(&installed) {
+ openhuman_core::agent::session_store::restore(self.previous_session_store.take());
+ }
}
// For an ephemeral workspace, take ownership of the temp path and
// remove it with a short retry. The core's memory/session writers keep
@@ -370,7 +371,9 @@ impl Runtime {
pub(crate) fn new(
core: Core,
workspace: ResolvedWorkspace,
- session_store: bool,
+ session_store: Option<Arc<dyn openhuman_core::agent::session_store::SessionStoreProvider>>,
+ previous_session_store:
+ Option<Arc<dyn openhuman_core::agent::session_store::SessionStoreProvider>>,
base_config: Config,
inherited: bool,
domains: DomainSet,
@@ -384,6 +387,7 @@ impl Runtime {
core: Some(core),
workspace,
session_store,
+ previous_session_store,
}),
base_config,
inherited,
diff --git a/crates/openhuman-rpc/src/lib.rs b/crates/openhuman-rpc/src/lib.rs
index 4157cb8bb6..897da54b6d 100644
--- a/crates/openhuman-rpc/src/lib.rs
+++ b/crates/openhuman-rpc/src/lib.rs
@@ -18,8 +18,10 @@
//! [`http_host`], the static-directory file server whose `http_host.*`
//! controllers the server registers with the core.
//! - Behind `session-store` (on with `server`): [`session_store`], the
-//! on-disk session store the app, the CLI and the TUI install, so the core
-//! itself carries no storage layout.
+//! on-disk session store the app, the CLI and the TUI install. The core has
+//! no storage layout of its own, but falls back to workspace files when a
+//! host installs no provider; this is not a guarantee that the core is
+//! persistence-free.
#[cfg(feature = "http-client")]
mod client;