Skip to content

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

test(tinyagents): add test helper for tool approval context

Steven Enamakel155c2c2c677c
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/operation_context.rs b/crates/openhuman-core/src/agent/tinyagents/host/operation_context.rs
index f9d9030802..1666236528 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/operation_context.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/operation_context.rs
@@ -62,3 +62,19 @@ pub(crate) fn approval_for_tool(
.map(|host| host.approval.clone())
.unwrap_or_default()
}
+
+/// A tool execution context for a run whose approval context is `approval`: what
+/// the harness hands a tool that is dispatched inside that run. Tests drive a
+/// tool through it to prove the turn's approval context reaches the tool.
+#[cfg(test)]
+pub(crate) fn tool_context_with_approval(
+ approval: crate::security::approval::ApprovalTurnContext,
+) -> tinyagents_harness::tool::ToolExecutionContext {
+ let mut host = super::OpenHumanRunContext::new();
+ host.approval = approval;
+ let run = host.into_tinyagents(tinyagents_harness::context::RunConfig::new("approval-test"));
+ tinyagents_harness::tool::ToolExecutionContext::from_run_context(
+ &run,
+ tinyagents_harness::ids::CallId::new("approval-test-call"),
+ )
+}
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
index a270e83a05..42addc6da4 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
@@ -410,6 +410,8 @@ pub(crate) struct HarnessHostPolicy {
pub(crate) use super::operation_context::{
approval_for_tool, parent_agent_id_for_tool, sandbox_mode_for_tool, HostOperationContext,
};
+#[cfg(test)]
+pub(crate) use super::operation_context::tool_context_with_approval;
/// OpenHuman product values that tools read from the run through
/// `ToolExecutionContext::host::<OpenHumanRunExt>()`, installed by
diff --git a/crates/openhuman-core/src/tools/per_call_tests.rs b/crates/openhuman-core/src/tools/per_call_tests.rs
new file mode 100644
index 0000000000..4c51a3fc9c
--- /dev/null
+++ b/crates/openhuman-core/src/tools/per_call_tests.rs
@@ -0,0 +1,114 @@
+use super::*;
+use crate::agent::tinyagents::host::run_context::tool_context_with_approval;
+use crate::security::approval::ApprovalTurnContext;
+use serde_json::json;
+
+/// Reports the chat it was built for, so a test can see which call built it.
+struct Probe {
+ built_for: Option<ApprovalChatContext>,
+}
+
+#[async_trait]
+impl Tool for Probe {
+ fn name(&self) -> &str {
+ "probe"
+ }
+
+ fn description(&self) -> &str {
+ "reports the chat it was built for"
+ }
+
+ fn parameters_schema(&self) -> Value {
+ json!({"type": "object"})
+ }
+
+ fn permission_level(&self) -> PermissionLevel {
+ PermissionLevel::Write
+ }
+
+ fn external_effect(&self) -> bool {
+ true
+ }
+
+ async fn execute(&self, _args: Value) -> anyhow::Result<ToolResult> {
+ let thread = self
+ .built_for
+ .as_ref()
+ .map_or_else(|| "none".to_string(), |chat| chat.thread_id.clone());
+ Ok(ToolResult::success(thread))
+ }
+}
+
+fn chat(thread: &str) -> ApprovalChatContext {
+ ApprovalChatContext {
+ thread_id: thread.to_string(),
+ client_id: format!("client-{thread}"),
+ request_id: None,
+ }
+}
+
+fn tool() -> PerCallTool<Probe> {
+ PerCallTool::new(|chat| Probe {
+ built_for: chat.cloned(),
+ })
+}
+
+#[tokio::test]
+async fn each_call_is_built_around_its_own_turns_chat() {
+ let tool = tool();
+ let a = tool_context_with_approval(ApprovalTurnContext::chat(chat("thread-a")));
+ let b = tool_context_with_approval(ApprovalTurnContext::chat(chat("thread-b")));
+
+ // Two turns running at once must not see each other's chat: the chat is
+ // read from each call's own context, not from anything the tool remembers.
+ let (first, second) = tokio::join!(
+ tool.execute_with_context(json!({}), ToolCallOptions::default(), Some(&a)),
+ tool.execute_with_context(json!({}), ToolCallOptions::default(), Some(&b)),
+ );
+ assert_eq!(first.unwrap().text(), "thread-a");
+ assert_eq!(second.unwrap().text(), "thread-b");
+}
+
+#[tokio::test]
+async fn a_call_with_no_run_context_is_built_without_a_chat() {
+ let tool = tool();
+ assert_eq!(tool.execute(json!({})).await.unwrap().text(), "none");
+ assert_eq!(
+ tool.execute_with_options(json!({}), ToolCallOptions::default())
+ .await
+ .unwrap()
+ .text(),
+ "none"
+ );
+ let non_chat = tool_context_with_approval(ApprovalTurnContext::default());
+ assert_eq!(
+ tool.execute_with_context(json!({}), ToolCallOptions::default(), Some(&non_chat))
+ .await
+ .unwrap()
+ .text(),
+ "none"
+ );
+}
+
+#[tokio::test]
+async fn a_chat_survives_await_points_inside_the_call() {
+ let tool = tool();
+ let context = tool_context_with_approval(ApprovalTurnContext::chat(chat("thread-c")));
+ let result = tokio::spawn(async move {
+ tokio::task::yield_now().await;
+ tool.execute_with_context(json!({}), ToolCallOptions::default(), Some(&context))
+ .await
+ })
+ .await
+ .unwrap()
+ .unwrap();
+ assert_eq!(result.text(), "thread-c");
+}
+
+#[test]
+fn static_metadata_comes_from_the_prototype() {
+ let tool = tool();
+ assert_eq!(tool.name(), "probe");
+ assert_eq!(tool.permission_level(), PermissionLevel::Write);
+ assert!(tool.external_effect());
+}