Skip to content

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

fix(agent): prefer scoped CoreContext authority for turn origin

Steven Enamakel4ec11569a39e
diff --git a/crates/openhuman-core/src/agent/turn_origin.rs b/crates/openhuman-core/src/agent/turn_origin.rs
index 797e51138c..f73be3d5ef 100644
--- a/crates/openhuman-core/src/agent/turn_origin.rs
+++ b/crates/openhuman-core/src/agent/turn_origin.rs
@@ -216,11 +216,14 @@ async fn scope_origin<F: std::future::Future>(origin: AgentTurnOrigin, fut: F) -
AGENT_TURN_ORIGIN.scope(origin, Box::pin(fut)).await
}
-/// Try to read the current origin. Returns `None` when no caller scoped one
+/// Read scoped CoreContext authority before the legacy task-local.
+/// The process default never supplies authority. Returns `None` when no caller scoped one
/// (legacy callers that haven't been migrated yet — the gate maps this to
/// [`AgentTurnOrigin::Unknown`] / fail-closed).
pub fn current() -> Option<AgentTurnOrigin> {
- AGENT_TURN_ORIGIN.try_with(|o| o.clone()).ok()
+ crate::core::runtime::CoreContext::scoped()
+ .and_then(|_| crate::core::runtime::CoreContext::current_turn_origin())
+ .or_else(|| AGENT_TURN_ORIGIN.try_with(|o| o.clone()).ok())
}
/// Capture the ambient origin so it can be carried across a `tokio::spawn`
diff --git a/crates/openhuman-core/src/core/runtime/context_turn_origin_tests.rs b/crates/openhuman-core/src/core/runtime/context_turn_origin_tests.rs
index f1ef4beb83..179eb1f833 100644
--- a/crates/openhuman-core/src/core/runtime/context_turn_origin_tests.rs
+++ b/crates/openhuman-core/src/core/runtime/context_turn_origin_tests.rs
@@ -86,3 +86,134 @@ async fn explicit_origin_scope_bridges_legacy_and_core_context_without_cross_tal
run("/tmp/origin-b", "channel-b")
);
}
+
+#[tokio::test]
+async fn bound_external_authority_wins_over_legacy_trusted_automation() {
+ use crate::agent::turn_origin::{self, AgentTurnOrigin, TrustedAutomationSource};
+
+ CoreContext::scope(ctx("/tmp/origin-priority"), async {
+ turn_origin::with_origin(
+ AgentTurnOrigin::TrustedAutomation {
+ job_id: "authorized-automation".into(),
+ source: TrustedAutomationSource::Background,
+ },
+ async {
+ let parent = CoreContext::scoped().unwrap();
+ let external = AgentTurnOrigin::ExternalChannel {
+ channel: "telegram".into(),
+ sender: Some("external-sender".into()),
+ sender_name: None,
+ reply_target: "room".into(),
+ message_id: "message".into(),
+ history_key: None,
+ };
+ CoreContext::scope_with_turn_origin(parent, Some(external), async {
+ assert!(matches!(
+ turn_origin::current(),
+ Some(AgentTurnOrigin::ExternalChannel { sender, .. })
+ if sender.as_deref() == Some("external-sender")
+ ));
+ let child =
+ crate::core::runtime::spawn_scoped(async { turn_origin::current() })
+ .await
+ .unwrap();
+ assert!(matches!(
+ child,
+ Some(AgentTurnOrigin::ExternalChannel { .. })
+ ));
+ })
+ .await;
+ assert!(matches!(
+ turn_origin::current(),
+ Some(AgentTurnOrigin::TrustedAutomation { .. })
+ ));
+ },
+ )
+ .await;
+ assert!(turn_origin::current().is_none());
+ })
+ .await;
+ assert!(CoreContext::scoped().is_none());
+}
+
+#[tokio::test]
+async fn concurrent_channel_turns_keep_tenant_agent_thread_and_policy_isolated() {
+ use crate::agent::turn_origin::{self, AgentTurnOrigin};
+ use crate::core::runtime::{ContextOverlay, DomainSet};
+
+ let barrier = Arc::new(tokio::sync::Barrier::new(2));
+ let run = |label: &'static str, barrier: Arc<tokio::sync::Barrier>| async move {
+ let directory = tempfile::tempdir().unwrap();
+ let workspace = directory.path().to_path_buf();
+ let config = crate::config::Config {
+ workspace_dir: workspace.clone(),
+ action_dir: workspace.clone(),
+ config_path: workspace.join("config.toml"),
+ ..Default::default()
+ };
+ let policy = Arc::new(crate::security::SecurityPolicy::from_config(
+ &config.autonomy,
+ &workspace,
+ &workspace,
+ ));
+ let context = CoreContext::for_test(DomainSet::none(), Some(workspace.clone()))
+ .derive_with(
+ ContextOverlay::new(config, DomainSet::full(), Default::default())
+ .session_agent(format!("agent-{label}"))
+ .profile(format!("profile-{label}"))
+ .agent_policy(policy.clone()),
+ );
+ let thread = format!("channel:{label}");
+ let origin = AgentTurnOrigin::ExternalChannel {
+ channel: label.into(),
+ sender: Some(format!("sender-{label}")),
+ sender_name: None,
+ reply_target: format!("room-{label}"),
+ message_id: format!("message-{label}"),
+ history_key: None,
+ };
+ let turn = context.derive_channel_turn(Some(thread.clone()), Some(origin));
+ CoreContext::scope(turn, async move {
+ crate::core::runtime::spawn_scoped(async move {
+ barrier.wait().await;
+ let child = CoreContext::scoped().unwrap();
+ assert_eq!(child.profile(), Some(format!("profile-{label}").as_str()));
+ assert_eq!(
+ child.session_agent(),
+ Some(format!("agent-{label}").as_str())
+ );
+ assert_eq!(CoreContext::current_channel_thread(), Some(thread));
+ assert_eq!(child.domains(), DomainSet::none());
+ assert_eq!(child.tool_groups(), Default::default());
+ assert_eq!(child.embedder_config().unwrap().workspace_dir, workspace);
+ assert!(Arc::ptr_eq(&child.agent_policy().unwrap(), &policy));
+ assert!(matches!(
+ turn_origin::current(),
+ Some(AgentTurnOrigin::ExternalChannel { channel, sender, .. })
+ if channel == label && sender == Some(format!("sender-{label}"))
+ ));
+ })
+ .await
+ .unwrap();
+ })
+ .await;
+ assert!(context.turn_origin.is_none());
+ assert!(context.channel_thread.is_none());
+ assert!(CoreContext::scoped().is_none());
+ assert!(turn_origin::current().is_none());
+ };
+ tokio::join!(run("one", barrier.clone()), run("two", barrier));
+}
+
+#[tokio::test]
+async fn scoped_spawn_does_not_invent_missing_turn_authority() {
+ CoreContext::scope(ctx("/tmp/origin-missing"), async {
+ let child =
+ crate::core::runtime::spawn_scoped(async { crate::agent::turn_origin::current() })
+ .await
+ .unwrap();
+ assert!(child.is_none());
+ })
+ .await;
+ assert!(crate::agent::turn_origin::current().is_none());
+}