Skip to content

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

test(agent): cover nested tool call hook decisions

Steven Enamakel887d400b8b4c
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware_memory_and_hooks_tests.rs b/crates/openhuman-core/src/agent/tinyagents/middleware_memory_and_hooks_tests.rs
index c439050712..f9bcd2d417 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware_memory_and_hooks_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware_memory_and_hooks_tests.rs
@@ -95,3 +95,88 @@ async fn embedder_tool_hooks_post_use_without_pre_call_falls_back_to_null() {
assert_eq!(post[0].1, serde_json::Value::Null);
assert_eq!(post[0].2, Some(true));
}
+
+fn nested_call(name: &str) -> TaToolCall {
+ TaToolCall {
+ id: "parent/1".into(),
+ name: name.into(),
+ arguments: json!({"path": "/"}),
+ invalid: None,
+ }
+}
+
+/// A hook whose pre-tool decision is fixed, to exercise every arm.
+struct FixedDecisionHook(crate::agent::hooks::ToolHookDecision);
+
+#[async_trait]
+impl crate::agent::hooks::ToolHook for FixedDecisionHook {
+ fn name(&self) -> &str {
+ "fixed"
+ }
+ async fn before_tool(
+ &self,
+ _context: &crate::agent::hooks::ToolHookContext,
+ ) -> anyhow::Result<()> {
+ Ok(())
+ }
+ async fn after_tool(
+ &self,
+ _context: &crate::agent::hooks::ToolHookContext,
+ ) -> anyhow::Result<()> {
+ Ok(())
+ }
+ async fn before_tool_decision(
+ &self,
+ _context: &crate::agent::hooks::ToolHookContext,
+ ) -> crate::agent::hooks::ToolHookDecision {
+ self.0.clone()
+ }
+}
+
+#[tokio::test]
+async fn embedder_tool_hooks_nested_call_is_admitted_when_hooks_proceed() {
+ let pre = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
+ let post = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
+ let mw = embedder_hook_mw(pre.clone(), post, false);
+ mw.check_nested_tool(&ctx(), &(), &nested_call("lookup"))
+ .await
+ .unwrap();
+ assert_eq!(pre.lock().unwrap().len(), 1, "hook consulted for nested call");
+ assert!(
+ mw.arguments_by_call_id.lock().unwrap().is_empty(),
+ "a nested check caches nothing"
+ );
+}
+
+#[tokio::test]
+async fn embedder_tool_hooks_nested_call_honours_a_veto() {
+ let pre = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
+ let post = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
+ let mw = embedder_hook_mw(pre, post, true);
+ let error = mw
+ .check_nested_tool(&ctx(), &(), &nested_call("rm"))
+ .await
+ .unwrap_err();
+ assert!(error.to_string().contains("vetoed"), "{error}");
+}
+
+#[tokio::test]
+async fn embedder_tool_hooks_nested_call_refuses_ask_and_rewrite() {
+ use crate::agent::hooks::ToolHookDecision;
+ for decision in [
+ ToolHookDecision::Ask("confirm".into()),
+ ToolHookDecision::ProceedWith(json!({"path": "."})),
+ ] {
+ let mw = EmbedderToolHooksMiddleware::new(vec![std::sync::Arc::new(FixedDecisionHook(
+ decision,
+ ))]);
+ let error = mw
+ .check_nested_tool(&ctx(), &(), &nested_call("write"))
+ .await
+ .unwrap_err();
+ assert!(
+ error.to_string().contains("refused nested call"),
+ "{error}"
+ );
+ }
+}