Skip to content

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

refactor(agent): thread turn environment through host agent contexts

Steven Enamakel31b1c7236d09
diff --git a/crates/openhuman-core/src/agent/host_agents.rs b/crates/openhuman-core/src/agent/host_agents.rs
index 469b75e68d..d2d1126006 100644
--- a/crates/openhuman-core/src/agent/host_agents.rs
+++ b/crates/openhuman-core/src/agent/host_agents.rs
@@ -93,7 +93,7 @@ impl HostAgent {
// the learning sink.
session.set_host_env(crate::agent::turn_env::HostTurnEnv {
hooks: self.hooks.clone(),
- core: Some(Arc::clone(&self.context)),
+ core: Some(Arc::downgrade(&self.context)),
..Default::default()
});
Ok(session)
diff --git a/crates/openhuman-core/src/agent/session_host/runtime/accessors.rs b/crates/openhuman-core/src/agent/session_host/runtime/accessors.rs
index 8f020efd5a..bf88800aa6 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime/accessors.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime/accessors.rs
@@ -281,10 +281,7 @@ impl OpenHumanSessionHost {
/// The context this session's turns run under: the one its host
/// environment carries, else the calling task's.
pub(crate) fn dispatch_ctx(&self) -> crate::core::runtime::DispatchCtx {
- match &self.host_env.core {
- Some(core) => crate::core::runtime::DispatchCtx::new(Arc::clone(core)),
- None => crate::core::runtime::DispatchCtx::ambient(),
- }
+ crate::core::runtime::DispatchCtx::from_weak(self.host_env.core.as_ref())
}
/// The host environment installed with [`Self::set_host_env`].
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest.rs b/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest.rs
index 48f03756d2..91a532a086 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session/memory_ingest.rs
@@ -79,10 +79,7 @@ impl OpenHumanTurnPrelude {
}
pub(in crate::agent::session_host) fn dispatch_ctx(&self) -> crate::core::runtime::DispatchCtx {
- match &self.host_env.core {
- Some(core) => crate::core::runtime::DispatchCtx::new(Arc::clone(core)),
- None => crate::core::runtime::DispatchCtx::ambient(),
- }
+ crate::core::runtime::DispatchCtx::from_weak(self.host_env.core.as_ref())
}
/// The config memory runs under: the session's own, else the
/// workspace's on disk.
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session_turn.rs b/crates/openhuman-core/src/agent/session_host/runtime_session_turn.rs
index 0382f1c785..b336676b4d 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session_turn.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session_turn.rs
@@ -31,7 +31,7 @@ impl OpenHumanSessionHost {
// snapshots the host environment: the one ambient read at the turn
// boundary, for a host that did not hand a context over.
if self.host_env.core.is_none() {
- self.host_env.core = crate::core::runtime::DispatchCtx::ambient().into_core();
+ self.host_env.core = crate::core::runtime::DispatchCtx::ambient().downgrade();
}
self.ensure_runtime_session()?;
let staged = super::attachment_input::stage_turn(self, user_message, origin).await?;
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/context_composer.rs b/crates/openhuman-core/src/agent/tinyagents/host/context_composer.rs
index 8bdba90316..4b81254389 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/context_composer.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/context_composer.rs
@@ -108,7 +108,7 @@ pub struct OpenHumanContextComposer {
connected_integrations: Vec<ConnectedIntegration>,
/// The turn's dispatch context for config reads; `None` falls back to the
/// calling task's.
- dispatch: Option<DispatchCtx>,
+ dispatch: Option<std::sync::Weak<crate::core::runtime::CoreContext>>,
/// The section chain. Built once so per-turn composition is just a render.
builder: SystemPromptBuilder,
}
@@ -135,7 +135,7 @@ impl OpenHumanContextComposer {
/// Binds the turn's dispatch context, which the composer reads config under.
pub fn with_dispatch(mut self, dispatch: DispatchCtx) -> Self {
- self.dispatch = Some(dispatch);
+ self.dispatch = dispatch.downgrade();
self
}
@@ -216,7 +216,7 @@ impl ContextComposer for OpenHumanContextComposer {
tool_call_format: self.tool_call_format,
connected_integrations: &self.connected_integrations,
connected_identities_md: render_connected_identities(
- &self.dispatch.clone().unwrap_or_else(DispatchCtx::ambient),
+ &DispatchCtx::from_weak(self.dispatch.as_ref()),
),
user_identity: crate::security::credentials::identity::peek_credential_user_identity(),
// TODO(phase4): the master agent's personality roster is built
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/operation_context.rs b/crates/openhuman-core/src/agent/tinyagents/host/operation_context.rs
index 43a0a20581..4b35b40452 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/operation_context.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/operation_context.rs
@@ -55,7 +55,7 @@ pub(crate) struct HostOperationContext {
pub(crate) memory_identity: Option<crate::memory::scope::MemoryIdentity>,
/// The context the run acts under; see
/// [`OpenHumanRunContext::core`](super::OpenHumanRunContext::core).
- pub(crate) dispatch: Option<crate::core::runtime::DispatchCtx>,
+ pub(crate) core: Option<std::sync::Weak<crate::core::runtime::CoreContext>>,
}
/// The dispatch context of the run a tool executes in, from the explicit tool
@@ -65,14 +65,14 @@ pub(crate) struct HostOperationContext {
pub(crate) fn dispatch_ctx_for_tool(
context: Option<&dyn tinytools::ToolRunContext>,
) -> crate::core::runtime::DispatchCtx {
- let bound = context
+ let weak = context
.and_then(tinytools::ToolRunContext::host_extension)
.and_then(|extension| {
extension.downcast_ref::<tinyagents_harness::tool::ToolExecutionContext>()
})
.and_then(|context| context.state::<HostOperationContext>())
- .and_then(|host| host.dispatch.clone());
- bound.unwrap_or_else(crate::core::runtime::DispatchCtx::ambient)
+ .and_then(|host| host.core.clone());
+ crate::core::runtime::DispatchCtx::from_weak(weak.as_ref())
}
/// Reads the spawning agent's definition id from the explicit tool carrier.
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
index da8746fe45..abe5c12e72 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
@@ -311,7 +311,7 @@ pub struct OpenHumanRunContext {
pub memory_identity: Option<crate::memory::scope::MemoryIdentity>,
/// The context the turn runs under, read by tools, middleware and the
/// provider factory instead of the ambient scope. Children inherit it.
- pub core: Option<Arc<crate::core::runtime::CoreContext>>,
+ pub core: Option<std::sync::Weak<crate::core::runtime::CoreContext>>,
/// File-state identity used to detect stale parent reads after child writes.
pub file_state_agent_id: Option<String>,
/// Host-owned artifact index for tool-result references.
@@ -486,20 +486,17 @@ impl OpenHumanRunContext {
self
}
- /// Binds the context this run acts under.
+ /// Binds the context this run acts under, held weakly so a run kept past its turn never pins the tenant.
#[must_use]
pub fn with_core(mut self, core: Option<Arc<crate::core::runtime::CoreContext>>) -> Self {
- self.core = core;
+ self.core = core.as_ref().map(Arc::downgrade);
self
}
/// The dispatch context this run's readers use: its own `core`, else the
/// calling task's (a run assembled by a caller that did not bind one).
pub fn dispatch_ctx(&self) -> crate::core::runtime::DispatchCtx {
- match &self.core {
- Some(core) => crate::core::runtime::DispatchCtx::new(Arc::clone(core)),
- None => crate::core::runtime::DispatchCtx::ambient(),
- }
+ crate::core::runtime::DispatchCtx::from_weak(self.core.as_ref())
}
/// The connected-identities prompt section, read under this run's context.
@@ -680,10 +677,7 @@ impl OpenHumanRunContext {
.as_ref()
.map(|parent| parent.agent_definition_id.clone()),
memory_identity: self.memory_identity.clone(),
- dispatch: self
- .core
- .clone()
- .map(crate::core::runtime::DispatchCtx::new),
+ core: self.core.clone(),
});
let context = tinyagents_harness::context::RunContext::new(config, self)
.with_cancellation(cancellation)
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/security_gate.rs b/crates/openhuman-core/src/agent/tinyagents/host/security_gate.rs
index 3e7bec4e5a..aaedbea2b8 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/security_gate.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/security_gate.rs
@@ -154,7 +154,7 @@ pub struct OpenHumanSecurityGate {
approval: ApprovalTurnContext,
/// The owning turn's dispatch context, read from the turn's run context
/// when the gate is built; `None` falls back to the calling task's.
- dispatch: Option<DispatchCtx>,
+ dispatch: Option<std::sync::Weak<crate::core::runtime::CoreContext>>,
}
impl OpenHumanSecurityGate {
@@ -179,7 +179,7 @@ impl OpenHumanSecurityGate {
/// Binds the owning turn's dispatch context.
pub fn with_dispatch(mut self, dispatch: DispatchCtx) -> Self {
- self.dispatch = Some(dispatch);
+ self.dispatch = dispatch.downgrade();
self
}
@@ -426,7 +426,7 @@ impl OpenHumanSecurityGate {
let desktop_approval_disabled = match self.resolve_tool(&call.tool_name) {
Some(tool) => {
crate::desktop::control::approvals_disabled_for(
- &self.dispatch.clone().unwrap_or_else(DispatchCtx::ambient),
+ &DispatchCtx::from_weak(self.dispatch.as_ref()),
tool,
)
.await
diff --git a/crates/openhuman-core/src/agent/turn_env.rs b/crates/openhuman-core/src/agent/turn_env.rs
index 9d9a926126..f01d542d01 100644
--- a/crates/openhuman-core/src/agent/turn_env.rs
+++ b/crates/openhuman-core/src/agent/turn_env.rs
@@ -58,11 +58,12 @@ pub struct HostTurnEnv {
/// from a parent snapshot keep the parent's team.
pub memory_identity: Option<crate::memory::scope::MemoryIdentity>,
/// The [`CoreContext`](crate::core::runtime::CoreContext) the turn runs
- /// under (the tenant's, an embedded agent's, or the host's own). The
- /// session copies it onto the turn's run context, from which tools,
- /// middleware and the provider factory read it; `None` leaves the session
- /// to bind the calling task's context when the turn starts.
- pub core: Option<std::sync::Arc<crate::core::runtime::CoreContext>>,
+ /// under (the tenant's, an embedded agent's, or the host's own), held
+ /// weakly: the env outlives a turn, and a strong handle would keep the
+ /// tenant in use. The session copies it onto the turn's run context, from
+ /// which tools, middleware and the provider factory read it; `None` leaves
+ /// the session to bind the calling task's context when the turn starts.
+ pub core: Option<std::sync::Weak<crate::core::runtime::CoreContext>>,
}
impl std::fmt::Debug for HostTurnEnv {
diff --git a/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs b/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
index 0f67768f5c..d12640f945 100644
--- a/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
+++ b/crates/openhuman-core/src/core/runtime/dispatch_ctx.rs
@@ -95,6 +95,24 @@ impl DispatchCtx {
crate::config::ops::load_config_in(self).await
}
+ /// A non-owning handle on the bound context, for state that outlives the
+ /// call (a session, a built turn). The tenant lease is the strong count of
+ /// the context ([`CoreContext::tenant_in_use`]), so a long-lived holder
+ /// must not keep it alive.
+ pub fn downgrade(&self) -> Option<std::sync::Weak<CoreContext>> {
+ self.core.as_ref().map(Arc::downgrade)
+ }
+
+ /// Dispatch under the context `weak` still points at, else the calling
+ /// task's ambient one (a holder that was never bound, or whose tenant is
+ /// gone).
+ pub fn from_weak(weak: Option<&std::sync::Weak<CoreContext>>) -> Self {
+ match weak.and_then(std::sync::Weak::upgrade) {
+ Some(core) => Self::new(core),
+ None => Self::ambient(),
+ }
+ }
+
/// The context this call runs under, if any.
pub fn core(&self) -> Option<&Arc<CoreContext>> {
self.core.as_ref()