1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use serde::{Deserialize, Serialize};
use super::{MatchingRules, PermissionChecker, PermissionDecision, PermissionRule};
use crate::queue::SessionLane;
fn yolo_lane_rule(lane: SessionLane) -> String {
match lane {
SessionLane::Control => "lane:control".to_string(),
SessionLane::Query => "lane:query".to_string(),
SessionLane::Execute => "lane:execute".to_string(),
SessionLane::Generate => "lane:generate".to_string(),
}
}
/// Permission policy configuration
///
/// Evaluation order:
/// 1. Deny rules - any match results in denial
/// 2. Allow rules - any match results in auto-approval
/// 3. Ask rules - any match requires user confirmation
/// 4. Default - falls back to default_decision
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PermissionPolicy {
/// Rules that always deny (checked first)
#[serde(default)]
pub deny: Vec<PermissionRule>,
/// Rules that auto-approve without confirmation
#[serde(default)]
pub allow: Vec<PermissionRule>,
/// Rules that always require confirmation
#[serde(default)]
pub ask: Vec<PermissionRule>,
/// Default decision when no rules match
#[serde(default = "default_decision")]
pub default_decision: PermissionDecision,
/// Whether the permission system is enabled
#[serde(default = "default_enabled")]
pub enabled: bool,
}
fn default_decision() -> PermissionDecision {
PermissionDecision::Ask
}
fn default_enabled() -> bool {
true
}
impl Default for PermissionPolicy {
fn default() -> Self {
Self {
deny: Vec::new(),
allow: Vec::new(),
ask: Vec::new(),
default_decision: PermissionDecision::Ask,
enabled: true,
}
}
}
impl PermissionPolicy {
/// Create a new permission policy
pub fn new() -> Self {
Self::default()
}
/// Create a strict policy that asks for everything
pub fn strict() -> Self {
Self {
deny: Vec::new(),
allow: Vec::new(),
ask: Vec::new(),
default_decision: PermissionDecision::Ask,
enabled: true,
}
}
/// Add a deny rule
pub fn deny(mut self, rule: &str) -> Self {
self.deny.push(PermissionRule::new(rule));
self
}
/// Add an allow rule
pub fn allow(mut self, rule: &str) -> Self {
self.allow.push(PermissionRule::new(rule));
self
}
/// Record YOLO lanes as Allow. Deny rules still win. The lane of a tool
/// comes from [`SessionLane::from_tool_name`].
pub fn allow_yolo_lanes(mut self, lanes: impl IntoIterator<Item = SessionLane>) -> Self {
for lane in lanes {
self.allow.push(PermissionRule::new(&yolo_lane_rule(lane)));
}
self
}
/// Add an ask rule
pub fn ask(mut self, rule: &str) -> Self {
self.ask.push(PermissionRule::new(rule));
self
}
/// Add multiple deny rules
pub fn deny_all(mut self, rules: &[&str]) -> Self {
for rule in rules {
self.deny.push(PermissionRule::new(rule));
}
self
}
/// Add multiple allow rules
pub fn allow_all(mut self, rules: &[&str]) -> Self {
for rule in rules {
self.allow.push(PermissionRule::new(rule));
}
self
}
/// Add multiple ask rules
pub fn ask_all(mut self, rules: &[&str]) -> Self {
for rule in rules {
self.ask.push(PermissionRule::new(rule));
}
self
}
/// Check permission for a tool invocation
///
/// Returns the permission decision based on rule evaluation order:
/// 1. Deny rules (any match = Deny)
/// 2. Allow rules (any match = Allow)
/// 3. Ask rules (any match = Ask)
/// 4. Default decision
pub fn check(&self, tool_name: &str, args: &serde_json::Value) -> PermissionDecision {
if !self.enabled {
return PermissionDecision::Allow;
}
// 1. Check deny rules first
for rule in &self.deny {
if rule.matches(tool_name, args) {
return PermissionDecision::Deny;
}
}
// 2. Check allow rules. A YOLO lane rule is Allow for every tool in
// that lane, including names `from_tool_name` maps by default.
let lane_rule = yolo_lane_rule(SessionLane::from_tool_name(tool_name));
for rule in &self.allow {
if rule.rule == lane_rule || rule.matches(tool_name, args) {
return PermissionDecision::Allow;
}
}
// 3. Check ask rules
for rule in &self.ask {
if rule.matches(tool_name, args) {
return PermissionDecision::Ask;
}
}
// 4. Fall back to default
self.default_decision
}
/// Check if a tool invocation is allowed (Allow or not Deny)
pub fn is_allowed(&self, tool_name: &str, args: &serde_json::Value) -> bool {
matches!(self.check(tool_name, args), PermissionDecision::Allow)
}
/// Check if a tool invocation is denied
pub fn is_denied(&self, tool_name: &str, args: &serde_json::Value) -> bool {
matches!(self.check(tool_name, args), PermissionDecision::Deny)
}
/// Check if a tool invocation requires confirmation
pub fn requires_confirmation(&self, tool_name: &str, args: &serde_json::Value) -> bool {
matches!(self.check(tool_name, args), PermissionDecision::Ask)
}
/// Get matching rules for debugging/logging
pub fn get_matching_rules(&self, tool_name: &str, args: &serde_json::Value) -> MatchingRules {
let mut result = MatchingRules::default();
for rule in &self.deny {
if rule.matches(tool_name, args) {
result.deny.push(rule.rule.clone());
}
}
for rule in &self.allow {
if rule.matches(tool_name, args) {
result.allow.push(rule.rule.clone());
}
}
for rule in &self.ask {
if rule.matches(tool_name, args) {
result.ask.push(rule.rule.clone());
}
}
result
}
/// Whether this policy explicitly declares that a tool may be considered
/// through an Allow or Ask rule.
///
/// This ignores argument patterns and does not authorize execution. It is
/// used when composing parent and delegated-worker visibility: an explicit
/// worker capability can override a parent host's ordinary model hiding,
/// while execution-time checks from both scopes remain authoritative.
pub fn declares_tool_access(&self, tool_name: &str) -> bool {
self.allow
.iter()
.chain(&self.ask)
.any(|rule| rule.matches_tool(tool_name))
}
}
impl PermissionChecker for PermissionPolicy {
fn expose_to_model(&self, tool_name: &str) -> bool {
if !self.enabled {
return true;
}
// A deny that covers every argument makes the entire capability
// unavailable. Argument-scoped denies keep the tool visible so an
// allowed invocation can still be formed and checked at execution.
if self
.deny
.iter()
.any(|rule| rule.matches_tool(tool_name) && rule.matches_all_args())
{
return false;
}
if self.default_decision != PermissionDecision::Deny {
return true;
}
// Under a deny-by-default worker policy, expose only tools for which
// at least one Allow or Ask rule can match. Argument patterns are
// intentionally ignored here; execution-time checks remain
// authoritative for the actual arguments.
self.declares_tool_access(tool_name)
}
fn check(&self, tool_name: &str, args: &serde_json::Value) -> PermissionDecision {
self.check(tool_name, args)
}
}