Skip to content

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

feat(agent): add session host builder and tool policy

Steven Enamakelaf88c78cda5c
diff --git a/crates/openhuman-core/src/agent/mod.rs b/crates/openhuman-core/src/agent/mod.rs
index e6c64cb47a..35fb0c0a92 100644
--- a/crates/openhuman-core/src/agent/mod.rs
+++ b/crates/openhuman-core/src/agent/mod.rs
@@ -77,7 +77,6 @@ pub mod stop_hooks;
pub mod subagent_host;
pub mod tinyagents;
pub mod todos;
-pub mod tool_policy;
pub mod tool_snapshot_scope;
pub mod tools;
pub mod triage;
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 0e95c2207e..3dd96ec644 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::tool_policy::AllowAllToolPolicy)),
+ .unwrap_or_else(|| Arc::new(crate::agent::tinyagents::AllowAllToolPolicy)),
last_seen_integrations_hash: 0,
composio_integrations_rx: None,
skill_events_rx: None,
diff --git a/crates/openhuman-core/src/agent/session_host/builder/host_only.rs b/crates/openhuman-core/src/agent/session_host/builder/host_only.rs
index ba9917e3c0..1f916aa7e3 100644
--- a/crates/openhuman-core/src/agent/session_host/builder/host_only.rs
+++ b/crates/openhuman-core/src/agent/session_host/builder/host_only.rs
@@ -25,7 +25,11 @@ use std::sync::Arc;
use async_trait::async_trait;
use crate::agent::harness::host_definition::AgentDefinition;
-use crate::agent::tool_policy::{ToolPolicy, ToolPolicyDecision, ToolPolicyRequest};
+use tinyagents_harness::context::RunContext;
+use tinyagents_harness::middleware::{PolicyDecision, ToolCallPolicy};
+use tinyinference_llm::tool::ToolCall;
+
+use crate::agent::tinyagents::host::OpenHumanRunContext;
use crate::agent::OpenHumanSessionHost;
use crate::config::Config;
use anyhow::Result;
@@ -38,37 +42,37 @@ use tinyagents_definition::{SandboxMode, ToolScope};
/// host's policy and never widens it.
pub struct HostOnlyToolPolicy {
allowed: HashSet<String>,
- host: Option<Arc<dyn ToolPolicy>>,
+ host: Option<Arc<dyn tinyagents_harness::middleware::ToolCallPolicy<crate::agent::tinyagents::host::OpenHumanRunContext>>>,
}
impl HostOnlyToolPolicy {
/// Admit exactly `allowed`, subject to `host`'s own decision.
#[must_use]
- pub fn new(allowed: HashSet<String>, host: Option<Arc<dyn ToolPolicy>>) -> Self {
+ pub fn new(allowed: HashSet<String>, host: Option<Arc<dyn tinyagents_harness::middleware::ToolCallPolicy<crate::agent::tinyagents::host::OpenHumanRunContext>>>) -> Self {
Self { allowed, host }
}
}
#[async_trait]
-impl ToolPolicy for HostOnlyToolPolicy {
+impl ToolCallPolicy<OpenHumanRunContext> for HostOnlyToolPolicy {
fn name(&self) -> &str {
"host_only"
}
- async fn check(&self, request: &ToolPolicyRequest) -> ToolPolicyDecision {
- if !self.allowed.contains(&request.tool_name) {
+ async fn check(&self, ctx: &RunContext<OpenHumanRunContext>, call: &ToolCall) -> PolicyDecision {
+ if !self.allowed.contains(&call.name) {
log::debug!(
"[agent::host_only] denied tool outside the host belt: {}",
- request.tool_name
+ call.name
);
- return ToolPolicyDecision::deny(format!(
+ return PolicyDecision::deny(format!(
"tool `{}` is not available to this agent",
- request.tool_name
+ call.name
));
}
match &self.host {
- Some(host) => host.check(request).await,
- None => ToolPolicyDecision::Allow,
+ Some(host) => host.check(ctx, call).await,
+ None => PolicyDecision::Allow,
}
}
}
@@ -86,8 +90,8 @@ pub(super) fn empty_belt() -> HashSet<String> {
pub(super) fn session_policy(
host_only: bool,
tools: &[Box<dyn tinytools::Tool>],
- host: Option<Arc<dyn ToolPolicy>>,
-) -> Option<Arc<dyn ToolPolicy>> {
+ host: Option<Arc<dyn tinyagents_harness::middleware::ToolCallPolicy<crate::agent::tinyagents::host::OpenHumanRunContext>>>,
+) -> Option<Arc<dyn tinyagents_harness::middleware::ToolCallPolicy<crate::agent::tinyagents::host::OpenHumanRunContext>>> {
if !host_only {
return host;
}
diff --git a/crates/openhuman-core/src/agent/session_host/builder/host_tools.rs b/crates/openhuman-core/src/agent/session_host/builder/host_tools.rs
index 5ea81e9d89..22f826deda 100644
--- a/crates/openhuman-core/src/agent/session_host/builder/host_tools.rs
+++ b/crates/openhuman-core/src/agent/session_host/builder/host_tools.rs
@@ -31,7 +31,6 @@ use std::sync::Arc;
use tinytools::Tool;
-use crate::agent::tool_policy::ToolPolicy;
use crate::agent::OpenHumanSessionHost;
use crate::config::Config;
use anyhow::Result;
@@ -62,7 +61,7 @@ pub struct HostTurnTools {
/// enforce one through the session's tool policy.
pub withheld: HashSet<String>,
/// The session's admission gate, if the host sets one.
- pub policy: Option<Arc<dyn ToolPolicy>>,
+ pub policy: Option<Arc<dyn tinyagents_harness::middleware::ToolCallPolicy<crate::agent::tinyagents::host::OpenHumanRunContext>>>,
}
impl HostTurnTools {
@@ -171,7 +170,7 @@ impl HostTurnTools {
/// session should keep an existing policy for calls the host does not own,
/// the host composes the two and passes the result here.
#[must_use]
- pub fn with_policy(mut self, policy: Arc<dyn ToolPolicy>) -> Self {
+ pub fn with_policy(mut self, policy: Arc<dyn tinyagents_harness::middleware::ToolCallPolicy<crate::agent::tinyagents::host::OpenHumanRunContext>>) -> Self {
self.policy = Some(policy);
self
}
@@ -190,7 +189,7 @@ impl HostTurnTools {
/// The parts of a host turn belt that the session builder applies after
/// expanding the agent's static scope.
pub(super) struct MergedHostTurnTools {
- pub policy: Option<Arc<dyn ToolPolicy>>,
+ pub policy: Option<Arc<dyn tinyagents_harness::middleware::ToolCallPolicy<crate::agent::tinyagents::host::OpenHumanRunContext>>>,
pub withheld: HashSet<String>,
pub permanent: HashSet<String>,
pub scope_additions: HashSet<String>,
diff --git a/crates/openhuman-core/src/agent/session_host/builder/setters.rs b/crates/openhuman-core/src/agent/session_host/builder/setters.rs
index 13b41b3a7c..19ab57288e 100644
--- a/crates/openhuman-core/src/agent/session_host/builder/setters.rs
+++ b/crates/openhuman-core/src/agent/session_host/builder/setters.rs
@@ -379,7 +379,7 @@ impl SessionHostBuilder {
///
/// The default policy allows all calls. Custom policies can deny a call
/// before `Tool::execute_with_options` runs.
- pub fn tool_policy(mut self, policy: Arc<dyn crate::agent::tool_policy::ToolPolicy>) -> Self {
+ pub fn tool_policy(mut self, policy: Arc<dyn tinyagents_harness::middleware::ToolCallPolicy<crate::agent::tinyagents::host::OpenHumanRunContext>>) -> Self {
self.tool_policy = Some(policy);
self
}
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session.rs b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
index 44507c9e40..2da484143a 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
@@ -74,7 +74,7 @@ struct OpenHumanTurnPrelude {
/// Host prompt/utilisation state, shared by the runtime hooks. It is not
/// generic conversation state and the runtime never persists it.
context: Arc<std::sync::Mutex<crate::agent::context::ContextManager>>,
- tool_policy: Arc<dyn crate::agent::tool_policy::ToolPolicy>,
+ tool_policy: Arc<dyn tinyagents_harness::middleware::ToolCallPolicy<crate::agent::tinyagents::host::OpenHumanRunContext>>,
tool_dispatcher: Arc<dyn tinytools_agent::dialect::ToolDialect>,
workspace_dir: std::path::PathBuf,
action_dir: std::path::PathBuf,
diff --git a/crates/openhuman-core/src/agent/session_host/types.rs b/crates/openhuman-core/src/agent/session_host/types.rs
index da088df2ec..d287aea21d 100644
--- a/crates/openhuman-core/src/agent/session_host/types.rs
+++ b/crates/openhuman-core/src/agent/session_host/types.rs
@@ -11,7 +11,6 @@ use crate::agent::hooks::PostTurnHook;
use crate::agent::progress::AgentProgress;
use crate::agent::prompts::SystemPromptBuilder;
use crate::agent::tinyagents::TurnModelSource;
-use crate::agent::tool_policy::ToolPolicy;
use std::sync::Arc;
use tinytools::ToolPolicySession;
use tinytools::{Tool, ToolSpec};
@@ -446,7 +445,7 @@ pub struct OpenHumanSessionHost {
/// Pre-execution policy hook for tool calls in this session. The
/// default policy allows all calls so existing agents keep their
/// behaviour unless a caller opts into stricter policy.
- pub(super) tool_policy: Arc<dyn ToolPolicy>,
+ pub(super) tool_policy: Arc<dyn tinyagents_harness::middleware::ToolCallPolicy<crate::agent::tinyagents::host::OpenHumanRunContext>>,
/// Hash of the Composio connection set this OpenHumanSessionHost last reconciled
/// against. Compared at top-of-turn to a fresh hash computed from
/// [`crate::integrations::composio::cached_active_integrations`]; on
@@ -631,7 +630,7 @@ pub struct SessionHostBuilder {
/// Per-agent TokenJuice tool-output compression profile.
pub(super) tokenjuice_compression: tinyjuice_bus::types::AgentTokenjuiceCompression,
/// Optional pre-execution tool policy. Defaults to allow-all.
- pub(super) tool_policy: Option<Arc<dyn ToolPolicy>>,
+ pub(super) tool_policy: Option<Arc<dyn tinyagents_harness::middleware::ToolCallPolicy<crate::agent::tinyagents::host::OpenHumanRunContext>>>,
}
impl Default for SessionHostBuilder {
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs b/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs
index 298b9c3874..918bce7378 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_policy.rs
@@ -14,14 +14,14 @@ use tinyagents_harness::turn_policy::{
use crate::agent::tinyagents::budget::MAX_SPAWN_DEPTH;
-/// The builder-configured [`ToolPolicy`](crate::agent::tool_policy::ToolPolicy)
+/// The builder-configured [`ToolCallPolicy`](tinyagents_harness::middleware::ToolCallPolicy)
/// plus the session context a policy check needs, handed to the shared turn seam
/// so it can install the [`ToolPolicyMiddleware`](super::middleware::ToolPolicyMiddleware).
/// `None` means "no policy enforcement on this turn" (the channel/CLI + sub-agent
/// paths, which carry their own gating).
#[derive(Clone)]
pub(crate) struct ToolPolicyEnforcement {
- pub policy: std::sync::Arc<dyn crate::agent::tool_policy::ToolPolicy>,
+ pub policy: std::sync::Arc<dyn tinyagents_harness::middleware::ToolCallPolicy<crate::agent::tinyagents::host::OpenHumanRunContext>>,
/// The session's channel-permission snapshot — enforces the per-channel
/// permission ceiling (deny + per-call permission-level gate) the in-house
/// engine ran in `agent_tool_exec`.
diff --git a/crates/openhuman-core/src/agent/tool_policy.rs b/crates/openhuman-core/src/agent/tool_policy.rs
deleted file mode 100644
index 4d3401f674..0000000000
--- a/crates/openhuman-core/src/agent/tool_policy.rs
+++ /dev/null
@@ -1,141 +0,0 @@
-//! Generic pre-execution policy hook for agent tool calls.
-//!
-//! The default policy preserves existing behaviour. Callers that need a
-//! narrower runtime can install a custom policy through `AgentBuilder` and
-//! deny a tool before any side effect reaches the tool implementation.
-
-use async_trait::async_trait;
-use std::fmt;
-
-/// Structured context for a tool call before it reaches the tool
-/// implementation.
-#[derive(Clone, PartialEq, Eq)]
-pub struct ToolCallContext {
- pub session_id: String,
- pub channel: String,
- pub agent_definition_id: String,
- pub call_id: String,
- pub iteration: u32,
- pub source: ToolCallSource,
-}
-
-impl ToolCallContext {
- pub fn session(
- session_id: impl Into<String>,
- channel: impl Into<String>,
- agent_definition_id: impl Into<String>,
- call_id: impl Into<String>,
- iteration: u32,
- ) -> Self {
- Self {
- session_id: session_id.into(),
- channel: channel.into(),
- agent_definition_id: agent_definition_id.into(),
- call_id: call_id.into(),
- iteration,
- source: ToolCallSource::Session,
- }
- }
-}
-
-impl fmt::Debug for ToolCallContext {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- f.debug_struct("ToolCallContext")
- .field("session_id", &redact_for_debug(&self.session_id))
- .field("channel", &redact_for_debug(&self.channel))
- .field("agent_definition_id", &self.agent_definition_id)
- .field("call_id", &self.call_id)
- .field("iteration", &self.iteration)
- .field("source", &self.source)
- .finish()
- }
-}
-
-/// Entry point that produced a tool call.
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-#[allow(dead_code)] // Reserved for non-session tool ingress paths wired in follow-up PRs.
-pub enum ToolCallSource {
- Session,
- Bus,
- Channel,
- Cron,
- Webhook,
- Unknown,
-}
-
-/// Snapshot of the tool call and session context a policy can inspect.
-#[derive(Clone)]
-pub struct ToolPolicyRequest {
- pub tool_name: String,
- pub arguments: serde_json::Value,
- pub context: ToolCallContext,
-}
-
-impl fmt::Debug for ToolPolicyRequest {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- f.debug_struct("ToolPolicyRequest")
- .field("tool_name", &self.tool_name)
- .field("arguments", &"<redacted>")
- .field("context", &self.context)
- .finish()
- }
-}
-
-impl ToolPolicyRequest {
- pub fn new(
- tool_name: impl Into<String>,
- arguments: serde_json::Value,
- context: ToolCallContext,
- ) -> Self {
- Self {
- tool_name: tool_name.into(),
- arguments,
- context,
- }
- }
-}
-
-fn redact_for_debug(value: &str) -> String {
- let trimmed = value.trim();
- if trimmed.is_empty() {
- return "<empty>".to_string();
- }
- let prefix: String = trimmed.chars().take(4).collect();
- format!("{prefix}...")
-}
-
-/// Decision returned by a [`ToolPolicy`].
-///
-/// The vocabulary (allow / deny / require approval) is owned by the harness
-/// (`tinyagents_harness::middleware::PolicyDecision`); this alias keeps the
-/// host-facing name.
-pub type ToolPolicyDecision = tinyagents_harness::middleware::PolicyDecision;
-
-/// Policy middleware invoked before an agent executes a tool.
-#[async_trait]
-pub trait ToolPolicy: Send + Sync {
- /// Stable policy name for logs and user-visible denial messages.
- fn name(&self) -> &str;
-
- /// Inspect a tool call and decide whether it can execute.
- async fn check(&self, request: &ToolPolicyRequest) -> ToolPolicyDecision;
-}
-
-/// Default policy used when no caller installs a stricter one.
-#[derive(Debug, Default)]
-pub struct AllowAllToolPolicy;
-
-#[async_trait]
-impl ToolPolicy for AllowAllToolPolicy {
- fn name(&self) -> &str {
- "allow_all"
- }
-
- async fn check(&self, _request: &ToolPolicyRequest) -> ToolPolicyDecision {
- ToolPolicyDecision::Allow
- }
-}
-
-#[cfg(test)]
-#[path = "tool_policy_tests.rs"]
-mod tests;
diff --git a/crates/openhuman-core/src/agent/tool_policy_tests.rs b/crates/openhuman-core/src/agent/tool_policy_tests.rs
deleted file mode 100644
index 2666fd2552..0000000000
--- a/crates/openhuman-core/src/agent/tool_policy_tests.rs
+++ /dev/null
@@ -1,40 +0,0 @@
-use super::*;
-
-#[tokio::test]
-async fn allow_all_policy_allows_every_call() {
- let policy = AllowAllToolPolicy;
- let request = ToolPolicyRequest::new(
- "echo",
- serde_json::json!({ "value": 1 }),
- ToolCallContext::session("session", "chat", "orchestrator", "call-1", 1),
- );
-
- assert_eq!(policy.check(&request).await, ToolPolicyDecision::Allow);
- assert_eq!(request.context.session_id, "session");
- assert_eq!(request.context.channel, "chat");
- assert_eq!(request.context.agent_definition_id, "orchestrator");
- assert_eq!(request.context.source, ToolCallSource::Session);
- assert_eq!(request.context.call_id, "call-1");
-}
-
-#[test]
-fn debug_redacts_sensitive_context_fields() {
- let request = ToolPolicyRequest::new(
- "secrets.lookup",
- serde_json::json!({ "secret": "super-secret-token" }),
- ToolCallContext::session(
- "session-secret-123",
- "private-channel",
- "orchestrator",
- "call-1",
- 1,
- ),
- );
-
- let rendered = format!("{request:?}");
- assert!(rendered.contains("sess..."));
- assert!(rendered.contains("priv..."));
- assert!(!rendered.contains("session-secret-123"));
- assert!(!rendered.contains("private-channel"));
- assert!(!rendered.contains("super-secret-token"));
-}