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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! 权限模式与权限规则。一次调用的判定结果是
//! `interface::permission::PermissionOutcome`。
use crate::interface::config_source::Tier;
use serde::{Deserialize, Serialize};
/// Permission mode for tool execution.
#[derive(
Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, schemars::JsonSchema,
)]
#[serde(rename_all = "camelCase")]
pub enum PermissionMode {
/// Prompt user for each tool call that isn't explicitly allowed.
#[default]
Default,
/// Allow file writes inside the project (the working directory and
/// `files.extra_dirs`) and read-only calls without asking;
/// ask about the rest, as `default` does. Writes into the project's
/// `.git`, its project directory (`.agix` unless renamed; then `.agix`
/// too), `.claude` and `.agents/skills` still ask.
AcceptEdits,
/// Ask nothing and skip the rules of the user's tiers. Credentials
/// (`.ssh`, `.aws`, `.gnupg`, the state directory's settings and tokens)
/// are still refused, the deployment's managed `deny` / `ask` rules still
/// apply, input that is not valid is still refused, and `Bash` still runs
/// in the OS sandbox.
BypassPermissions,
/// Plan mode — only allow read-only tools.
Plan,
/// Don't ask — deny any tool not explicitly allowed (no prompt).
DontAsk,
/// Bubble mode — forward permission requests up to a parent agent.
/// **Program-only**: set by team/coordinator runtime; cannot be set by user.
#[schemars(skip)]
Bubble,
}
impl PermissionMode {
/// Whether the user can set this mode via settings.json / CLI.
/// `Bubble` is program-only — set by the team coordinator runtime and
/// rejected from user config.
pub fn is_user_settable(self) -> bool {
!matches!(self, Self::Bubble)
}
/// How permissive this mode is, on a total order — higher grants more
/// without asking.
///
/// Exists so a host can *clamp* a mode it did not choose: the daemon's
/// `session.create` RPC accepts `options.settings.permissions.mode`, and
/// without an ordering there was no way to express "a client may tighten
/// this, but may not loosen it past what `settings.json` configured".
/// Anyone who could reach the socket could simply ask for
/// `bypassPermissions` and get it, whatever the daemon was configured
/// with — see `assembly::session_pool::effective_permission_mode`.
///
/// The ranking:
/// - `DontAsk` refuses anything not explicitly allowed — nothing is more
/// restrictive.
/// - `Plan` refuses every non-read-only tool.
/// - `Default` asks about anything not explicitly allowed.
/// - `Bubble` is `Default` with the question routed to a parent agent
/// rather than a human; the same set of calls still needs approval.
/// - `AcceptEdits` additionally waves through `Write`/`Edit`.
/// - `BypassPermissions` asks nothing at all.
pub fn permissiveness(self) -> u8 {
match self {
Self::DontAsk => 0,
Self::Plan => 1,
Self::Default => 2,
Self::Bubble => 3,
Self::AcceptEdits => 4,
Self::BypassPermissions => 5,
}
}
/// The stricter (less permissive) of two modes. Ties keep `self`.
pub fn min_permissive(self, other: Self) -> Self {
if other.permissiveness() < self.permissiveness() {
other
} else {
self
}
}
/// The mode a delegate (sub-agent, team member) runs in when the session
/// that spawned it is in `self`: its own, wider or narrower — except that a
/// planning parent holds it to plan, a parent that asks nobody holds it to
/// `dontAsk`, and a parent that bypasses takes it along, unless it may only
/// tighten (`Delegate::mode_under`).
pub fn for_delegate(self, delegate: Self) -> Self {
match self {
Self::Plan | Self::DontAsk => delegate.min_permissive(self),
Self::BypassPermissions => self,
_ => delegate,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, schemars::JsonSchema)]
pub struct PermissionRule {
pub source: RuleSource,
pub behavior: RuleBehavior,
pub tool_name: String,
/// 例:Bash 用 "git push:*";Read 用 "/etc/**"
#[serde(skip_serializing_if = "Option::is_none", default)]
pub rule_content: Option<String>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash, schemars::JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum RuleSource {
/// `~/.agix/scenes/<scope>/settings.json`
UserSettings,
/// `settings.json` in the project directory (`.agix` by default)
ProjectSettings,
/// `settings.local.json` in the project directory
LocalSettings,
/// 设置的 `cli` 层:`agixd --set permissions.allow=…` 或 `EmbedConfig::settings`
CliArg,
/// 这个会话自己的:建会话时 `options.settings` 里的规则串,与会话里答应的「本会话都允许」
Session,
/// 部署方的 managed settings(`settings::managed`)
PolicySettings,
/// 由命令展开时临时注入
Command,
/// 由已安装插件贡献。卸载插件时用 `Policy::remove_rules_by_source`
/// 整批撤销。
Plugin,
/// 宿主自带的 `PermissionPolicy`:它的回答作为一层规则参与裁决。
Host,
}
impl RuleSource {
/// 同一行为有多条规则命中时,用它挑出报告哪一条:数值越大越先选中。
/// 不参与 allow / ask / deny 之间的裁决——那只看行为,deny 永远优先。
pub fn priority(self) -> u8 {
match self {
Self::PolicySettings => 70,
Self::Host => 65,
Self::CliArg => 60,
Self::Session => 50,
Self::Command => 45,
Self::LocalSettings => 40,
Self::ProjectSettings => 30,
Self::UserSettings => 20,
Self::Plugin => 5,
}
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash, schemars::JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum RuleBehavior {
Allow,
Deny,
Ask,
}
/// One rule string of a settings file's `permissions` block, with the list it
/// was written in: `allow`, `ask` and `deny` give the action, `allow_exact`
/// an allow whose content matches only the same text.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PermissionRuleEntry {
/// As written: `Bash`, `Bash(git push:*)`, `mcp__srv__tool`.
pub tool: String,
pub action: RuleBehavior,
pub matching: crate::interface::permission::GrantMatch,
}
/// A permission rule as loaded: the rule, the tier whose file it came from,
/// and whether that file was the tier's `settings.local.json`.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct LoadedRule {
pub rule: PermissionRuleEntry,
pub tier: Tier,
pub machine_local: bool,
}
impl LoadedRule {
pub fn new(rule: PermissionRuleEntry, tier: Tier, machine_local: bool) -> Self {
Self {
rule,
tier,
machine_local,
}
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ManagedSettings {
/// The file (or other source) the layer came from.
pub origin: String,
/// Why the layer cannot be honoured. While set, every tool call is refused.
pub error: Option<String>,
pub rules: Vec<PermissionRuleEntry>,
pub disable_bypass: bool,
/// Every other setting the layer sets, shaped as a settings file holds
/// it: put over every other layer.
pub values: serde_json::Map<String, serde_json::Value>,
}
impl ManagedSettings {
/// Whether a `bypassPermissions` session under this layer can still be
/// asked about a call: the layer takes bypass away, or holds an `ask` or
/// `deny` rule. A `deny` counts because a command it cannot read through
/// (`$CMD x`) is asked about rather than refused.
pub fn asks_under_bypass(&self) -> bool {
self.error.is_none()
&& (self.disable_bypass || self.rules.iter().any(|r| r.action != RuleBehavior::Allow))
}
}
/// What the permission engine reads of the loaded settings: every tier's
/// rules, the managed layer, and where a project-scoped grant is written. The
/// sandbox it reads off the call's `ToolContext::config`.
#[derive(Debug, Clone, Default)]
pub struct PermissionSettings {
pub rules: Vec<LoadedRule>,
pub managed: Option<ManagedSettings>,
/// `None` for a session with no project: a grant lasts the session only.
pub project_rules: Option<std::sync::Arc<dyn crate::interface::permission::RuleFile>>,
/// Whose worktrees a path rule also matches in, as the main checkout
/// holds them ([`crate::paths::Layout::main_checkout_copies`]).
pub layout: crate::paths::Layout,
}
/// Whether the project tier of a `LoadedSettings` is in effect as written.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ProjectTrust {
/// No project tier of its own: no project at all, or one whose `.agix`
/// is the global or scene directory.
#[default]
NoProject,
Trusted,
Untrusted,
}
impl ProjectTrust {
pub fn as_str(self) -> &'static str {
match self {
Self::NoProject => "none",
Self::Trusted => "trusted",
Self::Untrusted => "untrusted",
}
}
pub fn is_untrusted(self) -> bool {
self == Self::Untrusted
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rule_priority_ordering() {
assert!(RuleSource::CliArg.priority() > RuleSource::LocalSettings.priority());
assert!(RuleSource::LocalSettings.priority() > RuleSource::ProjectSettings.priority());
assert!(RuleSource::ProjectSettings.priority() > RuleSource::UserSettings.priority());
assert!(RuleSource::PolicySettings.priority() > RuleSource::CliArg.priority());
}
#[test]
fn permission_mode_serializes_camelcase() {
let s = serde_json::to_value(PermissionMode::BypassPermissions).unwrap();
assert_eq!(s, serde_json::Value::String("bypassPermissions".into()));
let back: PermissionMode = serde_json::from_value(s).unwrap();
assert_eq!(back, PermissionMode::BypassPermissions);
}
#[test]
fn only_the_claude_code_spellings_are_modes() {
for spelling in ["auto", "yolo", "bypass_permissions"] {
assert!(serde_json::from_value::<PermissionMode>(spelling.into()).is_err());
}
}
#[test]
fn rule_roundtrip() {
let r = PermissionRule {
source: RuleSource::ProjectSettings,
behavior: RuleBehavior::Deny,
tool_name: "Bash".into(),
rule_content: Some("rm -rf *".into()),
};
let s = serde_json::to_string(&r).unwrap();
let back: PermissionRule = serde_json::from_str(&s).unwrap();
assert_eq!(r, back);
}
}