Skip to content

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

feat(tinyagents): add security gate to host bundle

Steven Enamakel6bcaed7f1229
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/bundle.rs b/crates/openhuman-core/src/agent/tinyagents/host/bundle.rs
index f5a582e016..9787e2b5fb 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/bundle.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/bundle.rs
@@ -125,7 +125,7 @@ impl OpenHumanHostBundleFactory {
/// owned by `OpenhumanEventBridge` (see below).
pub fn build(
inputs: OpenHumanHostBundleInputs,
- _turn: &OpenHumanRunContext,
+ turn: &OpenHumanRunContext,
) -> OpenHumanHostBundle {
let context = Arc::new(OpenHumanContextComposer::new(Arc::clone(&inputs.config)));
let registered_tools = Arc::new(
@@ -163,7 +163,10 @@ impl OpenHumanHostBundleFactory {
definitions_adapter = definitions_adapter.with_session_definition(definition);
}
let definitions = Arc::new(definitions_adapter);
- let mut security = OpenHumanSecurityGate::new(inputs.security_policy, inputs.tool_sets);
+ // An untrusted-input turn is read from its own explicit run context:
+ // a screen may run on another task, so no ambient scope is consulted.
+ let mut security = OpenHumanSecurityGate::new(inputs.security_policy, inputs.tool_sets)
+ .with_input_screening(!turn.untrusted_input_turn);
if let Some(policy) = inputs.tool_policy {
security = security.with_tool_policy(policy);
}
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/mod.rs b/crates/openhuman-core/src/agent/tinyagents/host/mod.rs
index 44effcfe02..ed97e6e243 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/mod.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/mod.rs
@@ -55,6 +55,5 @@ pub use run_context::{
decide_dispatch, DispatchDecision, DispatchInputs, LastTurnUsage, OpenHumanRunContext,
OpenHumanRunExt, SubagentUsageEntry, TurnDispatchState,
};
-pub(crate) use security_gate::with_untrusted_input_turn;
pub use security_gate::OpenHumanSecurityGate;
pub use tool_outcome_classifier::OpenHumanToolOutcomeClassifier;
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 e5f95dcd06..df084f73a3 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/security_gate.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/security_gate.rs
@@ -144,21 +144,10 @@ pub struct OpenHumanSecurityGate {
/// mismatch (2) in the module header.
pending_audit: Mutex<HashMap<String, String>>,
/// `false` for a host-only turn reading untrusted data
- /// ([`with_untrusted_input_turn`]): its input is data, not instructions.
+ /// ([`Self::with_input_screening`]): its input is data, not instructions.
screens_input: bool,
}
-tokio::task_local! {
- static UNTRUSTED_INPUT_TURN: ();
-}
-
-/// Run `fut` as a turn whose input is untrusted data: gates built for it do
-/// not screen input. Scoped only by `agent_chat_reply_for`, and only after the
-/// session accepted untrusted input, which only a host-only session does.
-pub(crate) async fn with_untrusted_input_turn<F: std::future::Future>(fut: F) -> F::Output {
- UNTRUSTED_INPUT_TURN.scope((), Box::pin(fut)).await
-}
-
impl OpenHumanSecurityGate {
/// Builds a gate over `policy` and the runner's shared `tool_sets`.
///
@@ -173,11 +162,18 @@ impl OpenHumanSecurityGate {
tool_policy: None,
tool_sets,
pending_audit: Mutex::new(HashMap::new()),
- // Read here, on the turn's own task: a screen may run elsewhere.
- screens_input: UNTRUSTED_INPUT_TURN.try_with(|_| ()).is_err(),
+ screens_input: true,
}
}
+ /// Whether this gate screens the turn's input. A host-only turn reading
+ /// untrusted data (`OpenHumanRunContext::untrusted_input_turn`) passes
+ /// `false`: its input is data, not instructions.
+ pub fn with_input_screening(mut self, screens_input: bool) -> Self {
+ self.screens_input = screens_input;
+ self
+ }
+
/// Attaches the session's channel permission boundary.
pub fn with_tool_policy(mut self, session: Arc<ToolPolicySession>) -> Self {
self.tool_policy = Some(session);
diff --git a/crates/openhuman-core/src/inference/host_runtime/ops/complete_once_tests.rs b/crates/openhuman-core/src/inference/host_runtime/ops/complete_once_tests.rs
index 7719ef8a0d..c24775d2d9 100644
--- a/crates/openhuman-core/src/inference/host_runtime/ops/complete_once_tests.rs
+++ b/crates/openhuman-core/src/inference/host_runtime/ops/complete_once_tests.rs
@@ -66,7 +66,7 @@ async fn forwards_schema_max_tokens_and_provider_options() {
request.max_tokens = Some(321);
request.provider_options = json!({"provider": {"order": ["fast"]}, "usage": {"include": true}});
- let response = complete_once(&endpoint(&server), request)
+ let response = complete_once(&endpoint(&server), request, None)
.await
.expect("an injection-shaped message is data, not a refusal");
@@ -114,7 +114,7 @@ async fn surfaces_length_finish_reason() {
.await;
let request = ModelRequest::new(vec![Message::user("hi")]).with_model("m".to_string());
- let response = complete_once(&endpoint(&server), request).await.unwrap();
+ let response = complete_once(&endpoint(&server), request, None).await.unwrap();
assert_eq!(response.finish_reason.as_deref(), Some("length"));
}
@@ -126,7 +126,7 @@ async fn refuses_tool_declarations() {
"run a command",
json!({"type": "object"}),
)];
- let err = complete_once(&unreachable_endpoint(), request)
+ let err = complete_once(&unreachable_endpoint(), request, None)
.await
.unwrap_err();
assert!(err.contains("tools are not supported"), "{err}");
@@ -135,7 +135,7 @@ async fn refuses_tool_declarations() {
#[tokio::test]
async fn requires_a_model() {
let request = ModelRequest::new(vec![Message::user("hi")]);
- let err = complete_once(&unreachable_endpoint(), request)
+ let err = complete_once(&unreachable_endpoint(), request, None)
.await
.unwrap_err();
assert!(err.contains("request.model is required"), "{err}");
@@ -145,7 +145,7 @@ async fn requires_a_model() {
async fn refuses_tools_smuggled_through_provider_options() {
let mut request = ModelRequest::new(vec![Message::user("hi")]).with_model("m".to_string());
request.provider_options = json!({"tools": [{"type": "function"}]});
- let err = complete_once(&unreachable_endpoint(), request)
+ let err = complete_once(&unreachable_endpoint(), request, None)
.await
.expect_err("tools must not reach the provider via provider_options");
assert!(
@@ -163,7 +163,7 @@ async fn provider_error_is_returned_as_a_prefixed_string() {
.mount(&server)
.await;
let request = ModelRequest::new(vec![Message::user("hi")]).with_model("m".to_string());
- let err = complete_once(&endpoint(&server), request)
+ let err = complete_once(&endpoint(&server), request, None)
.await
.expect_err("a 400 from the provider must surface as an error");
assert!(err.starts_with("complete_once: "), "{err}");