Skip to content

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

feat(agent): track turn origin in runtime context

Steven Enamakel4a55c68472ca
diff --git a/crates/openhuman-core/src/agent/turn_origin.rs b/crates/openhuman-core/src/agent/turn_origin.rs
index 23c8db6fc5..ad689fd521 100644
--- a/crates/openhuman-core/src/agent/turn_origin.rs
+++ b/crates/openhuman-core/src/agent/turn_origin.rs
@@ -197,7 +197,8 @@ pub fn current_is_user_authored() -> bool {
/// single-point remediation that covers every caller (web channel, channel
/// runtime, cron, background jobs, CLI).
pub async fn with_origin<F: std::future::Future>(origin: AgentTurnOrigin, fut: F) -> F::Output {
- let context = crate::core::runtime::CoreContext::current()
+ let context = crate::core::runtime::CoreContext::scoped_for_turn()
+ .or_else(crate::core::runtime::CoreContext::default_context)
.unwrap_or_else(crate::core::runtime::CoreContext::bare);
crate::core::runtime::CoreContext::scope_with_turn_origin(context, Some(origin), Box::pin(fut))
.await
@@ -208,7 +209,8 @@ pub async fn with_origin<F: std::future::Future>(origin: AgentTurnOrigin, fut: F
/// caller scoped one; the gate maps that to [`AgentTurnOrigin::Unknown`]
/// (fail closed).
pub fn current() -> Option<AgentTurnOrigin> {
- crate::core::runtime::CoreContext::scoped().and_then(|context| context.turn_origin().cloned())
+ crate::core::runtime::CoreContext::scoped_for_turn()
+ .and_then(|context| context.turn_origin().cloned())
}
/// Capture the ambient origin so it can be carried across a `tokio::spawn`
@@ -298,7 +300,7 @@ pub fn propagate<F: std::future::Future>(fut: F) -> impl std::future::Future<Out
// The origin is a field of the scoped context, so carrying the context
// carries the origin and nothing else: a task with no scoped context stays
// unlabelled.
- let context = crate::core::runtime::CoreContext::scoped();
+ let context = crate::core::runtime::CoreContext::scoped_for_turn();
async move {
match context {
Some(context) => crate::core::runtime::CoreContext::scope(context, fut).await,
diff --git a/crates/openhuman-core/src/core/runtime/context.rs b/crates/openhuman-core/src/core/runtime/context.rs
index f7d269bd11..b8c75f9f7a 100644
--- a/crates/openhuman-core/src/core/runtime/context.rs
+++ b/crates/openhuman-core/src/core/runtime/context.rs
@@ -119,6 +119,11 @@ pub struct CoreContext {
profile: Option<String>,
/// 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
+ /// booted ([`CoreContext::bare`]). Every reader other than the turn-origin
+ /// ones treats it as if nothing were scoped: [`CoreContext::current`] and
+ /// [`CoreContext::scoped`] skip it.
+ bare: bool,
}
/// The workspace a context is bound to.
@@ -290,6 +295,7 @@ impl CoreContext {
session_agent: None,
profile: None,
agent: Default::default(),
+ bare: false,
});
let _ = DEFAULT_CONTEXT.set(ctx.clone());
@@ -415,6 +421,7 @@ impl CoreContext {
session_agent: overlay.session_agent.or_else(|| self.session_agent.clone()),
profile: overlay.profile.or_else(|| self.profile.clone()),
agent,
+ bare: self.bare,
})
}
@@ -571,6 +578,7 @@ impl CoreContext {
session_agent: None,
profile: None,
agent: Default::default(),
+ bare: false,
})
}
@@ -615,6 +623,7 @@ impl CoreContext {
session_agent: None,
profile: None,
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 4a02e99275..1412a87a3f 100644
--- a/crates/openhuman-core/src/core/runtime/context_tests.rs
+++ b/crates/openhuman-core/src/core/runtime/context_tests.rs
@@ -18,6 +18,7 @@ fn ctx(dir: &str) -> Arc<CoreContext> {
session_agent: None,
profile: None,
agent: Default::default(),
+ bare: false,
})
}
@@ -53,6 +54,7 @@ fn ctx_with_config(config: crate::config::Config) -> Arc<CoreContext> {
session_agent: None,
profile: None,
agent: Default::default(),
+ bare: false,
})
}
@@ -393,6 +395,7 @@ fn degraded_context_rejects_workspace_bound_stores() {
session_agent: None,
profile: None,
agent: Default::default(),
+ bare: false,
};
// `workspace_dir()` is the gate every workspace-bound store goes
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 8f1fbdf319..761ee07b9a 100644
--- a/crates/openhuman-core/src/core/runtime/context_turn_origin.rs
+++ b/crates/openhuman-core/src/core/runtime/context_turn_origin.rs
@@ -2,16 +2,20 @@ use super::*;
impl CoreContext {
pub fn current() -> Option<Arc<CoreContext>> {
- CURRENT_CONTEXT
- .try_with(|ctx| ctx.clone())
- .ok()
- .or_else(|| DEFAULT_CONTEXT.get().cloned())
+ Self::scoped().or_else(|| DEFAULT_CONTEXT.get().cloned())
}
/// The context scoped onto the current task, with no fallback to the
/// process default. SaaS uses it: there, a task that lost its scope must
/// fail rather than act as the operator.
pub fn scoped() -> Option<Arc<CoreContext>> {
+ Self::scoped_for_turn().filter(|ctx| !ctx.bare)
+ }
+
+ /// [`scoped`](Self::scoped) including a [`bare`](Self::bare) context: what
+ /// the turn-origin readers and the spawn helpers that carry an origin use.
+ /// Everything else must treat a bare context as absent.
+ pub(crate) fn scoped_for_turn() -> Option<Arc<CoreContext>> {
CURRENT_CONTEXT.try_with(|ctx| ctx.clone()).ok()
}
@@ -23,10 +27,10 @@ impl CoreContext {
/// A minimal context for a caller that needs to carry per-turn authority
/// but runs where no core was booted (a unit test, a library caller that
- /// never built a runtime). It has no workspace, no embedder config and the
- /// full domain set, which is what every reader already assumes when no
- /// context is scoped at all, so scoping it changes no answer except the
- /// one it exists to carry (the turn origin).
+ /// never built a runtime). It carries the turn origin and nothing else:
+ /// [`current`](Self::current) and [`scoped`](Self::scoped) skip it, so every
+ /// other reader (state slots, workspace, config, tenant) answers exactly as
+ /// it does when nothing is scoped.
pub(crate) fn bare() -> Arc<CoreContext> {
Arc::new(CoreContext {
host_kind: HostKind::Cli,
@@ -44,6 +48,7 @@ impl CoreContext {
session_agent: None,
profile: None,
agent: Default::default(),
+ bare: true,
})
}
@@ -55,7 +60,9 @@ impl CoreContext {
/// Typed origin bound to this immutable per-turn context, if supplied by
/// its entrypoint. Missing authority retains library/CLI behavior.
pub fn current_turn_origin() -> Option<crate::agent::turn_origin::AgentTurnOrigin> {
- Self::current().and_then(|ctx| ctx.turn_origin.clone())
+ Self::scoped_for_turn()
+ .or_else(|| DEFAULT_CONTEXT.get().cloned())
+ .and_then(|ctx| ctx.turn_origin.clone())
}
/// Run a turn under a derived immutable context carrying its explicit
@@ -103,6 +110,7 @@ impl CoreContext {
session_agent: ctx.session_agent.clone(),
profile: ctx.profile.clone(),
agent: ctx.agent.clone(),
+ bare: ctx.bare,
})
}
}