use super::*;
struct CountingTool {
calls: Arc<AtomicUsize>,
}
#[async_trait]
impl ToolExecutor for CountingTool {
fn name(&self) -> &str {
"echo"
}
fn schema(&self) -> Value {
json!({
"type": "function",
"function": {
"name": "echo",
"description": "Echo the provided text back.",
"parameters": {
"type": "object",
"properties": { "text": { "type": "string" } },
"required": ["text"]
}
}
})
}
async fn execute(&self, _args: Value) -> ToolResult {
self.calls.fetch_add(1, Ordering::SeqCst);
ToolResult::ok("echo: ran")
}
}
fn counting_loop(yaml: Option<&str>) -> (AgentLoop, Arc<ScriptedLlm>, Arc<AtomicUsize>) {
let calls = Arc::new(AtomicUsize::new(0));
let mut registry = ToolRegistry::new();
registry.register(Arc::new(CountingTool {
calls: Arc::clone(&calls),
}));
let llm = Arc::new(ScriptedLlm::from_json(&[
tool_call_response("call-1", "hello"),
stop_response("done"),
]));
let mut agent = make_loop(llm.clone(), Arc::new(registry), AgentLoopConfig::default());
if let Some(yaml) = yaml {
let document = format!("---\nname: t\nrole: engineer\n{yaml}---\n\nBody.\n");
let config = crate::agents::config::AgentConfig {
permissions: crate::permissions::parse_permissions("test-agent", &document)
.expect("fixture must parse"),
..Default::default()
};
let gate = crate::permissions::PermissionContext::headless(
crate::permissions::PermissionMode::Default,
)
.gate_for(Arc::new(config), "agent-1");
agent = agent.with_permission_gate(Arc::new(gate));
}
(agent, llm, calls)
}
#[tokio::test]
async fn deny_never_dispatches() {
let (agent, llm, calls) = counting_loop(Some("permissions:\n echo: deny\n"));
let out = agent
.run("system", "task")
.await
.expect("the loop must not abort");
assert_eq!(
calls.load(Ordering::SeqCst),
0,
"a denied tool must never execute"
);
let refusal = llm
.requests()
.into_iter()
.flatten()
.filter_map(|m| m.content.clone())
.find(|c| c.contains("denied by policy"))
.expect("the refusal must be fed back to the model as a tool result");
assert!(refusal.contains("echo"), "must name the rule: {refusal}");
assert!(
!out.content.is_empty(),
"the loop still produced its final turn"
);
}
#[tokio::test]
async fn headless_ask_never_dispatches() {
let (agent, _llm, calls) = counting_loop(Some("permissions:\n echo: ask\n"));
agent
.run("system", "task")
.await
.expect("the loop must not abort");
assert_eq!(calls.load(Ordering::SeqCst), 0);
}
#[tokio::test]
async fn allow_rule_dispatches() {
let (agent, _llm, calls) = counting_loop(Some("permissions:\n echo: allow\n"));
agent
.run("system", "task")
.await
.expect("the loop must not abort");
assert_eq!(calls.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn ungated_loop_still_dispatches() {
let (agent, _llm, calls) = counting_loop(None);
agent
.run("system", "task")
.await
.expect("the loop must not abort");
assert_eq!(calls.load(Ordering::SeqCst), 1);
}