Skip to content

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

chore: files changed crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs,crates/op

Steven Enamakel10f9412d8d25
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/guard_tool.rs b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/guard_tool.rs
new file mode 100644
index 0000000000..bc15b03185
--- /dev/null
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/guard_tool.rs
@@ -0,0 +1,102 @@
+//! [`CeilingGuard`]'s `Tool` impl: everything forwards except execution,
+//! which is refused above the ceiling.
+
+use serde_json::Value;
+use std::any::Any;
+use tinytools::{
+ PermissionLevel, Tool, ToolCallOptions, ToolCategory, ToolExposure, ToolInjectedArgument,
+ ToolPolicy, ToolResult, ToolRunContext, ToolScope, ToolSpec, ToolTimeout,
+};
+
+use super::CeilingGuard;
+
+#[async_trait::async_trait]
+impl Tool for CeilingGuard {
+ fn name(&self) -> &str {
+ self.inner.name()
+ }
+ fn description(&self) -> &str {
+ self.inner.description()
+ }
+ fn parameters_schema(&self) -> Value {
+ self.inner.parameters_schema()
+ }
+ async fn execute(&self, args: Value) -> anyhow::Result<ToolResult> {
+ self.inner.execute(args).await
+ }
+ async fn execute_with_options(
+ &self,
+ args: Value,
+ options: ToolCallOptions,
+ ) -> anyhow::Result<ToolResult> {
+ self.inner.execute_with_options(args, options).await
+ }
+ async fn execute_with_context(
+ &self,
+ args: Value,
+ options: ToolCallOptions,
+ context: Option<&dyn ToolRunContext>,
+ ) -> anyhow::Result<ToolResult> {
+ self.inner.execute_with_context(args, options, context).await
+ }
+ fn policy(&self) -> ToolPolicy {
+ self.inner.policy()
+ }
+ fn injected_arguments(&self) -> Vec<ToolInjectedArgument> {
+ self.inner.injected_arguments()
+ }
+ fn supports_markdown(&self) -> bool {
+ self.inner.supports_markdown()
+ }
+ fn permission_level(&self) -> PermissionLevel {
+ self.inner.permission_level()
+ }
+ fn permission_level_with_args(&self, args: &Value) -> PermissionLevel {
+ self.inner.permission_level_with_args(args)
+ }
+ fn scope(&self) -> ToolScope {
+ self.inner.scope()
+ }
+ fn category(&self) -> ToolCategory {
+ self.inner.category()
+ }
+ fn exposure(&self) -> ToolExposure {
+ self.inner.exposure()
+ }
+ fn family(&self) -> Option<&str> {
+ self.inner.family()
+ }
+ fn is_concurrency_safe(&self, args: &Value) -> bool {
+ self.inner.is_concurrency_safe(args)
+ }
+ fn external_effect(&self) -> bool {
+ self.inner.external_effect()
+ }
+ fn external_effect_with_args(&self, args: &Value) -> bool {
+ self.inner.external_effect_with_args(args)
+ }
+ fn max_result_size_chars(&self) -> Option<usize> {
+ self.inner.max_result_size_chars()
+ }
+ fn timeout_policy(&self, args: &Value) -> ToolTimeout {
+ self.inner.timeout_policy(args)
+ }
+ fn host_extension(&self) -> Option<&(dyn Any + Send + Sync)> {
+ self.inner.host_extension()
+ }
+ fn host_call_extension(&self, args: &Value) -> Option<Box<dyn Any + Send + Sync>> {
+ self.inner.host_call_extension(args)
+ }
+ fn spec(&self) -> ToolSpec {
+ self.inner.spec()
+ }
+ fn display_label(&self, args: &Value) -> Option<String> {
+ self.inner.display_label(args)
+ }
+ fn display_detail(&self, args: &Value) -> Option<String> {
+ self.inner.display_detail(args)
+ }
+ fn return_direct(&self) -> bool {
+ self.inner.return_direct()
+ }
+}
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs
index e87af0faf6..823c69835f 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/mod.rs
@@ -2,6 +2,8 @@
use crate::config::Config;
+pub(crate) mod posture;
+
/// The agent `channel` is bound to in `config.agent.channel_agents`, if any.
pub(crate) fn bound_agent_id<'a>(config: &'a Config, channel: &str) -> Option<&'a str> {
config
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/posture.rs b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/posture.rs
new file mode 100644
index 0000000000..d128aa0d62
--- /dev/null
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/posture.rs
@@ -0,0 +1,53 @@
+//! What a host agent may do on a turn an outside sender started.
+//!
+//! A channel turn is [`AgentTurnOrigin::ExternalChannel`]: whoever can message
+//! the bot wrote its text. On the orchestrator path an acting tool parks an
+//! approval and, with no one to answer it, is denied when the park expires. A
+//! host agent bound to a public channel must not wait on that: the sender is
+//! the only person in the chat, and they are the one who must not approve it.
+//! So the turn is capped at [`PermissionLevel::ReadOnly`] and anything above
+//! that, or anything with an external effect, is refused on the spot with a
+//! tool error the model can relay.
+
+use tinytools::{PermissionLevel, Tool};
+
+use crate::agent::turn_origin::AgentTurnOrigin;
+use crate::agent::HostTools;
+use crate::config::Config;
+
+/// The most a host agent may do on a turn from `origin`; `None` leaves the
+/// agent's own limits alone.
+pub(crate) fn tool_ceiling(origin: &AgentTurnOrigin) -> Option<PermissionLevel> {
+ let _ = origin;
+ None
+}
+
+/// Cap `channel` at `ceiling` in this turn's `config`.
+pub(crate) fn cap_channel(config: &mut Config, channel: &str, ceiling: PermissionLevel) {
+ let _ = (config, channel, ceiling);
+}
+
+/// `host` with every tool it builds refusing calls above `ceiling`.
+pub(crate) fn guard_host_tools(host: HostTools, ceiling: PermissionLevel) -> HostTools {
+ let _ = ceiling;
+ host
+}
+
+/// A tool that refuses calls above its ceiling instead of running them.
+pub(crate) struct CeilingGuard {
+ inner: Box<dyn Tool>,
+ ceiling: PermissionLevel,
+}
+
+impl CeilingGuard {
+ pub(crate) fn new(inner: Box<dyn Tool>, ceiling: PermissionLevel) -> Self {
+ Self { inner, ceiling }
+ }
+}
+
+#[path = "guard_tool.rs"]
+mod guard_tool;
+
+#[cfg(test)]
+#[path = "posture_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/posture_tests.rs b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/posture_tests.rs
new file mode 100644
index 0000000000..e86ceb9a1a
--- /dev/null
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/posture_tests.rs
@@ -0,0 +1,149 @@
+use super::*;
+use crate::agent::HostTurnTools;
+use serde_json::{json, Value};
+use std::sync::atomic::{AtomicUsize, Ordering};
+use std::sync::Arc;
+use tinytools::ToolResult;
+
+fn external() -> AgentTurnOrigin {
+ AgentTurnOrigin::ExternalChannel {
+ channel: "telegram".into(),
+ sender: Some("alice".into()),
+ reply_target: "42".into(),
+ message_id: "m1".into(),
+ history_key: Some("telegram_alice".into()),
+ }
+}
+
+/// Records how often it actually ran.
+struct Probe {
+ level: PermissionLevel,
+ effect: bool,
+ runs: Arc<AtomicUsize>,
+}
+
+#[async_trait::async_trait]
+impl Tool for Probe {
+ fn name(&self) -> &str {
+ "probe"
+ }
+ fn description(&self) -> &str {
+ "a probe"
+ }
+ fn parameters_schema(&self) -> Value {
+ json!({ "type": "object", "properties": {} })
+ }
+ fn permission_level(&self) -> PermissionLevel {
+ self.level
+ }
+ fn external_effect(&self) -> bool {
+ self.effect
+ }
+ fn external_effect_with_args(&self, _args: &Value) -> bool {
+ self.effect
+ }
+ async fn execute(&self, _args: Value) -> anyhow::Result<ToolResult> {
+ self.runs.fetch_add(1, Ordering::SeqCst);
+ Ok(ToolResult::success("ran"))
+ }
+}
+
+fn guarded(level: PermissionLevel, effect: bool) -> (CeilingGuard, Arc<AtomicUsize>) {
+ let runs = Arc::new(AtomicUsize::new(0));
+ let probe = Probe {
+ level,
+ effect,
+ runs: Arc::clone(&runs),
+ };
+ (
+ CeilingGuard::new(Box::new(probe), PermissionLevel::ReadOnly),
+ runs,
+ )
+}
+
+#[test]
+fn an_external_channel_turn_is_capped_at_read_only() {
+ assert_eq!(tool_ceiling(&external()), Some(PermissionLevel::ReadOnly));
+}
+
+#[test]
+fn other_origins_keep_the_agents_own_limits() {
+ assert_eq!(tool_ceiling(&AgentTurnOrigin::Cli), None);
+ assert_eq!(tool_ceiling(&AgentTurnOrigin::DirectChat), None);
+}
+
+#[test]
+fn the_channel_is_capped_in_the_turns_config() {
+ let mut config = Config::default();
+ config
+ .agent
+ .channel_permissions
+ .insert("telegram".into(), "execute".into());
+ cap_channel(&mut config, "telegram", PermissionLevel::ReadOnly);
+ assert_eq!(config.agent.channel_permissions["telegram"], "readonly");
+
+ // Never widened: an operator's `none` stays `none`.
+ config
+ .agent
+ .channel_permissions
+ .insert("telegram".into(), "none".into());
+ cap_channel(&mut config, "telegram", PermissionLevel::ReadOnly);
+ assert_eq!(config.agent.channel_permissions["telegram"], "none");
+}
+
+#[tokio::test]
+async fn a_read_only_call_runs() {
+ let (tool, runs) = guarded(PermissionLevel::ReadOnly, false);
+ let result = tool.execute(json!({})).await.unwrap();
+ assert!(!result.is_error);
+ assert_eq!(runs.load(Ordering::SeqCst), 1);
+}
+
+#[tokio::test]
+async fn a_write_call_is_refused_without_running() {
+ let (tool, runs) = guarded(PermissionLevel::Write, false);
+ let result = tool
+ .execute_with_options(json!({}), ToolCallOptions::default())
+ .await
+ .unwrap();
+ assert!(result.is_error);
+ let text = result.output_for_llm(true);
+ assert!(text.contains("probe"), "{text}");
+ assert!(text.contains("not available"), "{text}");
+ assert_eq!(runs.load(Ordering::SeqCst), 0);
+}
+
+#[tokio::test]
+async fn an_external_effect_is_refused_and_never_parks() {
+ let (tool, runs) = guarded(PermissionLevel::ReadOnly, true);
+ assert!(
+ !tool.external_effect_with_args(&json!({})),
+ "the guard answers the call itself, so the approval gate is never asked"
+ );
+ let result = tool
+ .execute_with_context(json!({}), ToolCallOptions::default(), None)
+ .await
+ .unwrap();
+ assert!(result.is_error);
+ assert_eq!(runs.load(Ordering::SeqCst), 0);
+}
+
+#[test]
+fn guarded_host_tools_keep_the_rest_of_the_belt() {
+ let runs = Arc::new(AtomicUsize::new(0));
+ let probe_runs = Arc::clone(&runs);
+ let host: HostTools = Arc::new(move |_turn| {
+ HostTurnTools::advertised(vec![Box::new(Probe {
+ level: PermissionLevel::Write,
+ effect: false,
+ runs: Arc::clone(&probe_runs),
+ })])
+ });
+ let guarded = guard_host_tools(host, PermissionLevel::ReadOnly);
+ let belt = guarded(crate::agent::TurnContext::new("teeny", Some("s1")));
+ assert_eq!(belt.tools.len(), 1);
+ assert!(belt.visible.contains("probe"));
+ let result = futures::executor::block_on(belt.tools[0].execute(json!({}))).unwrap();
+ assert!(result.is_error, "the belt's tools are guarded");
+ assert_eq!(runs.load(Ordering::SeqCst), 0);
+}