Skip to content

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

feat(tinyagents): add tool policy middleware to harness assembly

Steven Enamakel8cfcc644e92b
diff --git a/crates/openhuman-core/src/agent/session_host/builder/builder_build.rs b/crates/openhuman-core/src/agent/session_host/builder/builder_build.rs
index 3dd96ec644..21db75e4fc 100644
--- a/crates/openhuman-core/src/agent/session_host/builder/builder_build.rs
+++ b/crates/openhuman-core/src/agent/session_host/builder/builder_build.rs
@@ -486,7 +486,7 @@ impl SessionHostBuilder {
tokenjuice_compression: self.tokenjuice_compression,
tool_policy: self
.tool_policy
- .unwrap_or_else(|| Arc::new(crate::agent::tinyagents::AllowAllToolPolicy)),
+ .unwrap_or_else(|| Arc::new(crate::agent::tinyagents::middleware::AllowAllToolPolicy)),
last_seen_integrations_hash: 0,
composio_integrations_rx: None,
skill_events_rx: None,
diff --git a/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs b/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs
index 0a20baf60f..7955b7ea4b 100644
--- a/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs
@@ -577,9 +577,7 @@ pub(super) fn assemble_turn_harness(
enforcement.policy,
enforcement.session,
tool_sets.clone(),
- enforcement.session_id,
enforcement.channel,
- enforcement.agent_definition_id,
)));
}
diff --git a/crates/openhuman-core/src/agent/tinyagents/live_harness.rs b/crates/openhuman-core/src/agent/tinyagents/live_harness.rs
index efa119d78e..79f1a979d4 100644
--- a/crates/openhuman-core/src/agent/tinyagents/live_harness.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/live_harness.rs
@@ -109,9 +109,7 @@ pub(crate) fn assemble_live_tool_harness(
enforcement.policy,
enforcement.session,
tool_sets.clone(),
- enforcement.session_id,
enforcement.channel,
- enforcement.agent_definition_id,
)));
}
harness.push_tool_middleware(Arc::new(middleware::credential_scrub_middleware()));
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/tool_policy.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/tool_policy.rs
index 7ca61e59f0..8f916d8e32 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/tool_policy.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/tool_policy.rs
@@ -1,4 +1,4 @@
-//! [`ToolPolicyMiddleware`]: enforce the builder-configured `ToolPolicy` and
+//! [`ToolPolicyMiddleware`]: enforce the builder-configured `ToolCallPolicy` and
//! the session's channel-permission ceiling at the tool boundary, and render
//! session-scoped `use_skill` listings.
@@ -19,7 +19,7 @@ use crate::agent::tinyagents::host::OpenHumanRunContext;
use crate::agent::tinyagents::policy_denial::PolicyDenial;
use tinytools::Tool;
-/// `wrap_tool`: enforce the agent's builder-configured [`ToolPolicy`] at the tool
+/// `wrap_tool`: enforce the agent's builder-configured `ToolCallPolicy` at the tool
/// boundary (issue #4249). The in-house engine ran this check in
/// `agent_tool_exec` (`ctx.tool_policy.check(...)`); the tinyagents path bypassed
/// it, so a `.tool_policy()` deny/require-approval silently no-opped and the tool
@@ -28,8 +28,7 @@ use tinytools::Tool;
/// same `"Tool '<name>' <denied|requires approval> by policy '<policy>': <reason>"`
/// wording the engine produced.
pub(crate) struct ToolPolicyMiddleware {
- /// The builder policy behind the harness gate, adapted to the harness
- /// `ToolCallPolicy` seam (see [`SessionToolPolicy`]).
+ /// The builder policy behind the harness gate.
gate: ToolPolicyGate<OpenHumanRunContext>,
/// The session's channel-permission snapshot — enforces the per-channel deny
/// + per-call permission-level ceiling the engine ran in `agent_tool_exec`.
@@ -41,56 +40,34 @@ pub(crate) struct ToolPolicyMiddleware {
channel: String,
}
-/// Adapter from the host's [`ToolPolicy`](crate::agent::tool_policy::ToolPolicy)
-/// (request + session context) to the harness `ToolCallPolicy` seam.
-struct SessionToolPolicy {
- policy: Arc<dyn crate::agent::tool_policy::ToolPolicy>,
- session_id: String,
- channel: String,
- agent_definition_id: String,
-}
+/// The session-less allow-all gate for sessions with no builder policy.
+#[derive(Debug, Default)]
+pub(crate) struct AllowAllToolPolicy;
#[async_trait]
-impl ToolCallPolicy<OpenHumanRunContext> for SessionToolPolicy {
+impl ToolCallPolicy<OpenHumanRunContext> for AllowAllToolPolicy {
fn name(&self) -> &str {
- self.policy.name()
+ "allow_all"
}
async fn check(
&self,
_ctx: &RunContext<OpenHumanRunContext>,
- call: &TaToolCall,
+ _call: &TaToolCall,
) -> PolicyDecision {
- use crate::agent::tool_policy::{ToolCallContext, ToolPolicyRequest};
- let context = ToolCallContext::session(
- self.session_id.clone(),
- self.channel.clone(),
- self.agent_definition_id.clone(),
- call.id.clone(),
- 1,
- );
- let request = ToolPolicyRequest::new(call.name.clone(), call.arguments.clone(), context);
- self.policy.check(&request).await
+ PolicyDecision::Allow
}
}
impl ToolPolicyMiddleware {
pub(crate) fn new(
- policy: Arc<dyn crate::agent::tool_policy::ToolPolicy>,
+ policy: Arc<dyn ToolCallPolicy<OpenHumanRunContext>>,
session: tinytools::ToolPolicySession,
tool_sets: Vec<Arc<Vec<Box<dyn Tool>>>>,
- session_id: String,
channel: String,
- agent_definition_id: String,
) -> Self {
- let adapter = SessionToolPolicy {
- policy,
- session_id,
- channel: channel.clone(),
- agent_definition_id,
- };
Self {
- gate: ToolPolicyGate::new(Arc::new(adapter)),
+ gate: ToolPolicyGate::new(policy),
session,
tool_sets,
channel,
@@ -351,8 +328,6 @@ impl ToolMiddleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>
call: TaToolCall,
next: ToolHandler<'_, (), crate::agent::tinyagents::host::OpenHumanRunContext>,
) -> TaResult<MiddlewareToolOutcome> {
- use crate::agent::tool_policy::ToolPolicyDecision;
-
// Channel-permission ceiling first (session deny + per-call permission
// level), mirroring the engine order in `agent_tool_exec`.
#[cfg(feature = "modules")]
@@ -375,9 +350,9 @@ impl ToolMiddleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>
let policy_name = self.gate.policy_name();
if let Some(reason) = decision.blocking_reason() {
let blocked_action = match &decision {
- ToolPolicyDecision::RequireApproval { .. } => "requires approval",
- ToolPolicyDecision::Deny { .. } => "denied",
- ToolPolicyDecision::Allow => "allowed",
+ PolicyDecision::RequireApproval { .. } => "requires approval",
+ PolicyDecision::Deny { .. } => "denied",
+ PolicyDecision::Allow => "allowed",
};
tinytools::registry::denials::record(
call.name.as_str(),
@@ -393,7 +368,7 @@ impl ToolMiddleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>
"[tinyagents::mw] tool blocked by policy"
);
let content = match &decision {
- ToolPolicyDecision::RequireApproval { .. } => PolicyDenial::ApprovalRequired {
+ PolicyDecision::RequireApproval { .. } => PolicyDenial::ApprovalRequired {
tool: &call.name,
policy: policy_name,
reason,
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware_tool_policy_tests.rs b/crates/openhuman-core/src/agent/tinyagents/middleware_tool_policy_tests.rs
index 3a2761f20c..9a6b2e8697 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware_tool_policy_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware_tool_policy_tests.rs
@@ -102,12 +102,10 @@ fn non_owner_middleware_at(allowed: PermissionLevel) -> ToolPolicyMiddleware {
};
ToolPolicyMiddleware::new(
- Arc::new(crate::agent::tool_policy::AllowAllToolPolicy),
+ Arc::new(AllowAllToolPolicy),
session,
vec![tools],
- "sess".to_string(),
"web_chat".to_string(),
- "orchestrator".to_string(),
)
}
@@ -140,7 +138,7 @@ fn desktop_approval_bypass_skips_only_require_approval_channel_verdict() {
.collect(),
};
ToolPolicyMiddleware::new(
- Arc::new(crate::agent::tool_policy::AllowAllToolPolicy),
+ Arc::new(AllowAllToolPolicy),
session,
vec![Arc::new(vec![Box::new(RoutingFakeTool(name))])],
"sess".to_owned(),
@@ -288,12 +286,10 @@ async fn a_session_without_the_delegate_is_not_told_to_call_it() {
decisions: [allow(USE_SKILL)].into_iter().collect(),
};
let mw = ToolPolicyMiddleware::new(
- Arc::new(crate::agent::tool_policy::AllowAllToolPolicy),
+ Arc::new(AllowAllToolPolicy),
session,
vec![tools],
- "sess".to_string(),
"web_chat".to_string(),
- "orchestrator".to_string(),
);
let message = mw
@@ -349,12 +345,10 @@ async fn use_skill_reaches_a_withheld_packed_tool() {
);
let mw = ToolPolicyMiddleware::new(
- Arc::new(crate::agent::tool_policy::AllowAllToolPolicy),
+ Arc::new(AllowAllToolPolicy),
session,
vec![tools],
- "sess".to_string(),
"web_chat".to_string(),
- "orchestrator".to_string(),
);
assert!(
@@ -411,12 +405,10 @@ async fn a_prompt_hidden_delegate_is_not_offered_as_a_direct_route() {
&visible,
);
let mw = ToolPolicyMiddleware::new(
- Arc::new(crate::agent::tool_policy::AllowAllToolPolicy),
+ Arc::new(AllowAllToolPolicy),
session,
vec![tools],
- "sess".to_string(),
"web_chat".to_string(),
- "orchestrator".to_string(),
);
let pack = crate::tools::toolpacks::pack("workflows").expect("workflows pack");