Skip to main content

agent_runtime/policy/
allow_list.rs

1//! Allow-list policy that intersects agent tool grants with platform policy.
2
3use std::collections::HashSet;
4
5use super::{Authorization, AuthorizationError, PolicyEngine};
6use crate::{Agent, ToolCall};
7
8/// Allows execution of explicitly configured tools.
9pub struct AllowListPolicy {
10    allowed_tools: HashSet<String>,
11}
12
13impl AllowListPolicy {
14    pub fn new<I, S>(allowed_tools: I) -> Self
15    where
16        I: IntoIterator<Item = S>,
17        S: Into<String>,
18    {
19        Self {
20            allowed_tools: allowed_tools.into_iter().map(Into::into).collect(),
21        }
22    }
23}
24
25impl PolicyEngine for AllowListPolicy {
26    fn authorize(&self, agent: &Agent, call: &ToolCall) -> Authorization {
27        if !agent.tool_names.iter().any(|name| name == &call.name) {
28            return Authorization::Denied(AuthorizationError {
29                tool_name: call.name.clone(),
30                reason: format!("Tool is not available to agent '{}'", agent.id),
31            });
32        }
33
34        if !self.allowed_tools.contains(&call.name) {
35            return Authorization::Denied(AuthorizationError {
36                tool_name: call.name.clone(),
37                reason: "Tool is not allowed by policy".into(),
38            });
39        }
40
41        Authorization::Allowed
42    }
43}