Skip to main content

a3s_code_core/permissions/
mod.rs

1//! Permission system for tool execution control
2//!
3//! Implements a declarative permission system similar to Claude Code's permissions.
4//! Supports pattern matching with wildcards and three-tier evaluation:
5//! 1. Deny rules - checked first, any match = immediate denial
6//! 2. Allow rules - checked second, any match = auto-approval
7//! 3. Ask rules - checked third, forces confirmation prompt
8//! 4. Default behavior - falls back to HITL policy
9
10mod interactive;
11mod manager;
12mod policy;
13mod risk;
14mod rule;
15mod style_specialty;
16
17#[cfg(test)]
18mod tests;
19
20use serde::{Deserialize, Serialize};
21use std::sync::Arc;
22
23pub use interactive::{InteractiveApprovalMode, InteractiveToolGuardrail};
24pub use manager::{MatchingRules, PermissionManager};
25pub use policy::PermissionPolicy;
26pub use risk::{
27    EnvironmentSensitivity, ImpactScope, OperationTarget, Reversibility, ToolRiskAction,
28    ToolRiskAssessment, ToolRiskDimensions, ToolRiskLevel, ToolRiskReason, ToolRiskType,
29};
30pub use rule::PermissionRule;
31pub use style_specialty::specialty_permission_policy;
32
33/// Trait for checking tool execution permissions.
34///
35/// Implement this trait to provide custom permission logic.
36/// The built-in `PermissionPolicy` implements this trait using
37/// declarative allow/deny/ask rules with pattern matching.
38pub trait PermissionChecker: Send + Sync {
39    /// Freeze any mutable host policy for one agent run.
40    ///
41    /// Stateless checkers can keep the default and will be shared as-is.
42    /// Interactive hosts whose policy changes between turns should return an
43    /// immutable checker here so an in-flight or background child cannot gain
44    /// or lose authority when the next turn selects a different mode.
45    fn snapshot_for_run(&self) -> Option<Arc<dyn PermissionChecker>> {
46        None
47    }
48
49    /// Whether a tool definition should be exposed to the model.
50    ///
51    /// This controls model-visible capabilities only. [`Self::check`] remains
52    /// the authoritative execution-time decision for any tool invocation.
53    /// Existing checkers expose every tool unless they explicitly override
54    /// this method.
55    fn expose_to_model(&self, _tool_name: &str) -> bool {
56        true
57    }
58
59    /// Check whether a tool invocation is allowed, denied, or requires confirmation.
60    fn check(&self, tool_name: &str, args: &serde_json::Value) -> PermissionDecision;
61}
62
63/// Permission decision result
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
65#[serde(rename_all = "snake_case")]
66pub enum PermissionDecision {
67    /// Automatically allow without user confirmation
68    Allow,
69    /// Deny execution
70    Deny,
71    /// Ask user for confirmation
72    Ask,
73}