Skip to content

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

feat(embed): expose per-turn origin to host tools

Steven Enamakel3726fdc441b1
diff --git a/crates/openhuman-core/src/profiles/README.md b/crates/openhuman-core/src/profiles/README.md
index 6580ad73ba..496a1928c2 100644
--- a/crates/openhuman-core/src/profiles/README.md
+++ b/crates/openhuman-core/src/profiles/README.md
@@ -421,3 +421,31 @@ channel threads, `409`) and `crates/openhuman-embed/tests/saas_profiles.rs`
installed through one node is only readable there unless secrets live in
the storage backend.
- Existing `agents/u-*` data is orphaned.
+
+
+## In-process host capabilities
+
+`openhuman_embed::ProfileRuntimeBuilder` accepts the existing `HostTools`
+factory, `ToolScopeSpec::Named` (or `HostOnly`) and an asynchronous
+`can_use_tool` callback. Registration contains exactly the selected admitted
+native and host tools. A wildcard is refused. SaaS hard denies, host group
+opt-ins, the profile lease fence and channel permission ceilings remain
+stronger than the callback. With no callback, execution is denied.
+
+The factory receives the exact durable chat or relay thread through
+`TurnContext::session_id`; its ambient tenant is the owning profile. During
+execution, `openhuman_embed::current_turn_origin` exposes the task's original
+`AgentTurnOrigin`, including channel sender and reply target. Concurrent
+messages do not mutate another turn's routing context.
+
+Channel permissions default to read-only, including unknown channels. Hosts
+must explicitly configure `web_channel` and their relayed channel to `Write`
+or `Execute` for memory or cron mutation. This grants only a ceiling: a named
+tool and the callback are still required. Ordinary public channel agents keep
+their read-only posture. Dangerous profile ceilings are refused.
+
+`memory_engine` supplies the existing `MemoryEngine` contract through each
+profile's local adapters. Requests retain the forced `user:<profile-id>` root;
+a mismatched config fails closed. The process-global host engine is still
+ignored in SaaS. Provider credentials and scheduled profile turn resolution
+are separate capabilities; selecting a native tool does not start a driver.
diff --git a/crates/openhuman-embed/src/lib.rs b/crates/openhuman-embed/src/lib.rs
index 150226287b..1a003b7314 100644
--- a/crates/openhuman-embed/src/lib.rs
+++ b/crates/openhuman-embed/src/lib.rs
@@ -66,6 +66,16 @@
/// Complete definition accepted by [`AgentDefinitionSpec::from_base`].
pub use openhuman_core::agent::harness::definition::AgentDefinition;
pub use openhuman_core::agent::turn_origin::{AgentTurnOrigin, TrustedAutomationSource};
+
+/// The origin of the executing turn, with its original chat or channel route.
+///
+/// Read this inside a host tool or permission callback. The value belongs to
+/// this task's turn, so overlapping messages never replace its sender or
+/// reply target. `None` means the caller installed no origin; it grants no
+/// authority. Scheduled turns retain their existing automation origin.
+pub fn current_turn_origin() -> Option<AgentTurnOrigin> {
+ openhuman_core::agent::turn_origin::current()
+}
pub use openhuman_core::backend::{
install_backend_transport, installed_backend_transport, BackendRequest, BackendTransport,
BackendTransportError, BaseUrlPurpose, TransportProfile,
diff --git a/crates/openhuman-embed/tests/profile_host_capabilities.rs b/crates/openhuman-embed/tests/profile_host_capabilities.rs
index 09f63f19ca..8f1a4253c4 100644
--- a/crates/openhuman-embed/tests/profile_host_capabilities.rs
+++ b/crates/openhuman-embed/tests/profile_host_capabilities.rs
@@ -40,6 +40,24 @@ impl Tool for TenantTool {
async fn execute(&self, _: Value) -> anyhow::Result<ToolResult> {
let tenant = openhuman_core::core::runtime::current_tenant()?;
let profile = tenant.profile.expect("profile tool always has a tenant");
+ match openhuman_embed::current_turn_origin().expect("a host turn has its original route") {
+ openhuman_embed::AgentTurnOrigin::WebChat { thread_id, .. } => {
+ assert_eq!(thread_id, "t1")
+ }
+ openhuman_embed::AgentTurnOrigin::ExternalChannel {
+ channel,
+ sender,
+ reply_target,
+ message_id,
+ ..
+ } => {
+ assert_eq!(channel, "telegram");
+ assert_eq!(sender.as_deref(), Some("sender"));
+ assert_eq!(reply_target, "chat");
+ assert_eq!(message_id, "message");
+ }
+ origin => panic!("unexpected turn origin: {origin:?}"),
+ }
self.calls
.lock()
.unwrap()
@@ -223,6 +241,45 @@ async fn scenario() {
("bob".into(), Some("channel:telegram/sender/chat".into())),
]
);
+ // Persistent hooks fail closed if reused under a different profile or
+ // after the owning lease is fenced, before the host callback is consulted.
+ let hook_context = serde_json::from_value(json!({"event":"PreToolUse", "call_id":"fixture", "tool_name":"tenant_probe", "arguments":{}, "success":null, "duration_ms":null})).unwrap();
+ let alice_hook = handles[0]
+ .context()
+ .host_overrides()
+ .unwrap()
+ .tool_hooks()
+ .into_iter()
+ .find(|hook| hook.name() == "profile.can_use_tool")
+ .unwrap();
+ assert!(matches!(
+ handles[0]
+ .scope(alice_hook.before_tool_decision(&hook_context))
+ .await,
+ ToolHookDecision::Proceed
+ ));
+ assert!(handles[1]
+ .scope(alice_hook.before_tool_decision(&hook_context))
+ .await
+ .is_deny());
+ let no_callback = Arc::new(
+ openhuman_core::profiles::capabilities::ProfileCapabilities::new(
+ None,
+ Some(["tenant_probe".to_string()].into_iter().collect()),
+ None,
+ Default::default(),
+ Vec::new(),
+ None,
+ ),
+ );
+ let alice_profile = handles[0]
+ .scope(async { openhuman_core::profiles::current().unwrap() })
+ .await;
+ let closed_hook = no_callback.hook(handles[0].id(), alice_profile.lease_fence().clone());
+ assert!(handles[0]
+ .scope(closed_hook.before_tool_decision(&hook_context))
+ .await
+ .is_deny());
let requests = chat_requests(&inference).await;
assert!(requests
.iter()
@@ -278,5 +335,10 @@ async fn scenario() {
};
assert!(!text.contains(&format!("{other}-secret")), "{response}");
}
+ alice_profile.lease_fence().fence();
+ assert!(handles[0]
+ .scope(alice_hook.before_tool_decision(&hook_context))
+ .await
+ .is_deny());
runtime.shutdown().await;
}