Skip to content

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

refactor(agent): migrate tool policy tests to run context API

Steven Enamakelbab34753f51d
diff --git a/crates/openhuman-core/src/agent/session_host/builder/builder_tests_host_only_tests.rs b/crates/openhuman-core/src/agent/session_host/builder/builder_tests_host_only_tests.rs
index 450cb30a7e..8275695291 100644
--- a/crates/openhuman-core/src/agent/session_host/builder/builder_tests_host_only_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/builder/builder_tests_host_only_tests.rs
@@ -7,11 +7,12 @@
//! session runs under can neither delegate nor act.
use super::*;
-use crate::agent::tool_policy::{
- ToolCallContext, ToolPolicy, ToolPolicyDecision, ToolPolicyRequest,
-};
+use crate::agent::tinyagents::host::OpenHumanRunContext;
use std::sync::Arc;
use tinyagents_definition::{SandboxMode, ToolScope};
+use tinyagents_harness::context::{RunConfig, RunContext};
+use tinyagents_harness::middleware::{PolicyDecision, ToolCallPolicy};
+use tinyinference_llm::tool::ToolCall;
#[derive(Debug)]
struct Marker(&'static str);
@@ -50,11 +51,10 @@ fn sorted(names: impl IntoIterator<Item = String>) -> Vec<String> {
names
}
-fn request(tool: &str) -> ToolPolicyRequest {
- ToolPolicyRequest::new(
- tool,
- serde_json::json!({}),
- ToolCallContext::session("session", "chat", "reviewer", "call-1", 1),
+fn request(tool: &str) -> (RunContext<OpenHumanRunContext>, ToolCall) {
+ (
+ RunContext::new(RunConfig::new("host-only-test"), OpenHumanRunContext::new()),
+ ToolCall::new("call-1", tool, serde_json::json!({})),
)
}
@@ -145,14 +145,20 @@ async fn the_host_only_policy_admits_only_host_names() {
);
assert_eq!(
- policy.check(&request("read_file")).await,
- ToolPolicyDecision::Allow
+ {
+ let (ctx, call) = request("read_file");
+ policy.check(&ctx, &call).await
+ },
+ PolicyDecision::Allow
);
for refused in ["shell", "write_file", "spawn_subagent", "memory_store"] {
assert!(
matches!(
- policy.check(&request(refused)).await,
- ToolPolicyDecision::Deny { .. }
+ {
+ let (ctx, call) = request(refused);
+ policy.check(&ctx, &call).await
+ },
+ PolicyDecision::Deny { .. }
),
"{refused} must be denied"
);
@@ -163,12 +169,12 @@ async fn the_host_only_policy_admits_only_host_names() {
async fn the_host_only_policy_still_consults_the_hosts_own_gate() {
struct DenyAll;
#[async_trait::async_trait]
- impl ToolPolicy for DenyAll {
+ impl ToolCallPolicy<OpenHumanRunContext> for DenyAll {
fn name(&self) -> &str {
"deny_all"
}
- async fn check(&self, _: &ToolPolicyRequest) -> ToolPolicyDecision {
- ToolPolicyDecision::deny("host says no")
+ async fn check(&self, _: &RunContext<OpenHumanRunContext>, _: &ToolCall) -> PolicyDecision {
+ PolicyDecision::deny("host says no")
}
}
let policy = super::super::host_only::HostOnlyToolPolicy::new(
@@ -177,8 +183,11 @@ async fn the_host_only_policy_still_consults_the_hosts_own_gate() {
);
assert!(matches!(
- policy.check(&request("read_file")).await,
- ToolPolicyDecision::Deny { .. }
+ {
+ let (ctx, call) = request("read_file");
+ policy.check(&ctx, &call).await
+ },
+ PolicyDecision::Deny { .. }
));
}
@@ -223,14 +232,14 @@ async fn a_host_only_session_enforces_the_deny_by_default_gate() {
assert_eq!(agent.tool_policy.name(), "host_only");
assert_eq!(
- agent.tool_policy.check(&request("read_file")).await,
- ToolPolicyDecision::Allow
+ agent.tool_{ let (ctx, call) = request("read_file"); policy.check(&ctx, &call).await },
+ PolicyDecision::Allow
);
for built_in in ["shell", "write_file", "spawn_subagent", "tool_search"] {
assert!(
matches!(
- agent.tool_policy.check(&request(built_in)).await,
- ToolPolicyDecision::Deny { .. }
+ agent.tool_{ let (ctx, call) = request(built_in); policy.check(&ctx, &call).await },
+ PolicyDecision::Deny { .. }
),
"{built_in} must be refused by the session's gate"
);
diff --git a/crates/openhuman-embed/src/agent/attachments.rs b/crates/openhuman-embed/src/agent/attachments.rs
index f06ac80567..4784167b7e 100644
--- a/crates/openhuman-embed/src/agent/attachments.rs
+++ b/crates/openhuman-embed/src/agent/attachments.rs
@@ -1,12 +1,12 @@
//! Permanent tool sources attached to an already configured agent.
use super::Agent;
use openhuman_core::agent::tinyagents::host::OpenHumanRunContext;
-use tinyagents_harness::context::RunContext;
-use tinyagents_harness::middleware::{PolicyDecision, ToolCallPolicy};
-use tinyinference_llm::tool::ToolCall;
use openhuman_core::agent::{HostTools, TurnContext};
use std::collections::{BTreeMap, HashSet};
use std::sync::{Arc, Mutex};
+use tinyagents_harness::context::RunContext;
+use tinyagents_harness::middleware::{PolicyDecision, ToolCallPolicy};
+use tinyinference_llm::tool::ToolCall;
/// An invalid or conflicting permanent tool registration.
#[derive(Debug, thiserror::Error)]
@@ -153,7 +153,10 @@ impl super::AgentInner {
}))
}
}
-type SourcePolicy = (HashSet<String>, Option<Arc<dyn ToolCallPolicy<OpenHumanRunContext>>>);
+type SourcePolicy = (
+ HashSet<String>,
+ Option<Arc<dyn ToolCallPolicy<OpenHumanRunContext>>>,
+);
struct AttachmentPolicy {
original: Option<Arc<dyn ToolCallPolicy<OpenHumanRunContext>>>,
policies: Vec<SourcePolicy>,
@@ -163,7 +166,11 @@ impl ToolCallPolicy<OpenHumanRunContext> for AttachmentPolicy {
fn name(&self) -> &str {
"permanent_attachments"
}
- async fn check(&self, ctx: &RunContext<OpenHumanRunContext>, call: &ToolCall) -> PolicyDecision {
+ async fn check(
+ &self,
+ ctx: &RunContext<OpenHumanRunContext>,
+ call: &ToolCall,
+ ) -> PolicyDecision {
for (names, policy) in &self.policies {
if names.contains(&call.name) {
return match policy {
diff --git a/crates/openhuman-embed/src/agent/attachments_tests.rs b/crates/openhuman-embed/src/agent/attachments_tests.rs
index 8623a37e19..cad9171494 100644
--- a/crates/openhuman-embed/src/agent/attachments_tests.rs
+++ b/crates/openhuman-embed/src/agent/attachments_tests.rs
@@ -33,15 +33,24 @@ async fn attachment_policy_does_not_replace_original_host_authority() {
],
};
assert!(matches!(
- { let (ctx, call) = request("hivemind_message"); policy.check(&ctx, &call).await },
+ {
+ let (ctx, call) = request("hivemind_message");
+ policy.check(&ctx, &call).await
+ },
PolicyDecision::Allow
));
assert!(matches!(
- { let (ctx, call) = request("host_original"); policy.check(&ctx, &call).await },
+ {
+ let (ctx, call) = request("host_original");
+ policy.check(&ctx, &call).await
+ },
PolicyDecision::Deny { .. }
));
assert!(matches!(
- { let (ctx, call) = request("hivemind_create"); policy.check(&ctx, &call).await },
+ {
+ let (ctx, call) = request("hivemind_create");
+ policy.check(&ctx, &call).await
+ },
PolicyDecision::Deny { .. }
));
}