claude-code-acp-rs 0.1.22

Use Claude Code from any ACP client - A Rust implementation of Claude Code ACP Agent
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! Permission handling for tool execution
//!
//! This module provides permission checking using a strategy pattern,
//! where each permission mode has its own strategy implementation.

use serde::{Deserialize, Serialize};
use std::fmt;
use std::sync::Arc;
use tokio::sync::RwLock;

use crate::permissions::strategies::{
    AcceptEditsModeStrategy, BypassPermissionsModeStrategy, DefaultModeStrategy,
    DontAskModeStrategy, PermissionModeStrategy, PlanModeStrategy,
};
use crate::settings::{PermissionChecker, PermissionDecision};
use claude_code_agent_sdk::PermissionMode as SdkPermissionMode;

/// Permission mode for tool execution
///
/// Controls how tool calls are approved during a session.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PermissionMode {
    /// Default mode - prompt for dangerous operations
    Default,
    /// Auto-approve file edits
    AcceptEdits,
    /// Planning mode - read-only operations
    Plan,
    /// Don't ask mode - deny if not pre-approved
    DontAsk,
    /// Bypass all permission checks (default mode for development)
    #[default]
    BypassPermissions,
}

impl PermissionMode {
    /// Parse from string (ACP setMode request)
    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "default" => Some(Self::Default),
            "acceptEdits" => Some(Self::AcceptEdits),
            "plan" => Some(Self::Plan),
            "dontAsk" => Some(Self::DontAsk),
            "bypassPermissions" => Some(Self::BypassPermissions),
            _ => None,
        }
    }

    /// Convert to string for SDK
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Default => "default",
            Self::AcceptEdits => "acceptEdits",
            Self::Plan => "plan",
            Self::DontAsk => "dontAsk",
            Self::BypassPermissions => "bypassPermissions",
        }
    }

    /// Convert to SDK PermissionMode
    ///
    /// Note: SDK doesn't support DontAsk mode yet, so we map it to Default
    pub fn to_sdk_mode(&self) -> SdkPermissionMode {
        match self {
            PermissionMode::Default => SdkPermissionMode::Default,
            PermissionMode::AcceptEdits => SdkPermissionMode::AcceptEdits,
            PermissionMode::Plan => SdkPermissionMode::Plan,
            PermissionMode::DontAsk => {
                // SDK doesn't support DontAsk yet, treat as Default
                SdkPermissionMode::Default
            }
            PermissionMode::BypassPermissions => SdkPermissionMode::BypassPermissions,
        }
    }

    /// Check if this mode allows write operations
    pub fn allows_writes(&self) -> bool {
        matches!(
            self,
            Self::Default | Self::AcceptEdits | Self::BypassPermissions
        )
    }

    /// Check if this mode auto-approves edits
    pub fn auto_approve_edits(&self) -> bool {
        matches!(self, Self::AcceptEdits | Self::BypassPermissions)
    }
}

/// Permission check result from the handler
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ToolPermissionResult {
    /// Tool execution is allowed (auto-approved or by rule)
    Allowed,
    /// Tool execution is blocked (by rule or mode)
    Blocked { reason: String },
    /// User should be asked for permission
    NeedsPermission,
}

/// Permission handler for tool execution
///
/// Uses a strategy pattern where each permission mode has its own strategy.
pub struct PermissionHandler {
    mode: PermissionMode,
    /// Strategy for current permission mode
    strategy: Arc<dyn PermissionModeStrategy>,
    /// Shared permission checker from settings (shared with hook)
    checker: Option<Arc<RwLock<PermissionChecker>>>,
}

impl fmt::Debug for PermissionHandler {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PermissionHandler")
            .field("mode", &self.mode)
            .field("strategy", &"<strategy>")
            .field("checker", &self.checker)
            .finish()
    }
}

impl Default for PermissionHandler {
    fn default() -> Self {
        Self {
            mode: PermissionMode::Default,
            strategy: Arc::new(DefaultModeStrategy),
            checker: None,
        }
    }
}

impl PermissionHandler {
    /// Create a new permission handler
    ///
    /// Uses Default mode (standard behavior with permission prompts).
    pub fn new() -> Self {
        Self::default()
    }

    /// Create with a specific mode
    pub fn with_mode(mode: PermissionMode) -> Self {
        Self {
            mode,
            strategy: Self::create_strategy(mode),
            checker: None,
        }
    }

    /// Create with settings-based checker
    ///
    /// Uses Default mode (standard behavior with permission prompts).
    pub fn with_checker(checker: Arc<RwLock<PermissionChecker>>) -> Self {
        Self {
            mode: PermissionMode::Default,
            strategy: Arc::new(DefaultModeStrategy),
            checker: Some(checker),
        }
    }

    /// Create with settings-based checker (non-async, for convenience)
    ///
    /// Uses Default mode (standard behavior with permission prompts).
    pub fn with_checker_owned(checker: PermissionChecker) -> Self {
        Self {
            mode: PermissionMode::Default,
            strategy: Arc::new(DefaultModeStrategy),
            checker: Some(Arc::new(RwLock::new(checker))),
        }
    }

    /// Create strategy for a given mode
    fn create_strategy(mode: PermissionMode) -> Arc<dyn PermissionModeStrategy> {
        match mode {
            PermissionMode::Default => Arc::new(DefaultModeStrategy),
            PermissionMode::AcceptEdits => Arc::new(AcceptEditsModeStrategy),
            PermissionMode::Plan => Arc::new(PlanModeStrategy),
            PermissionMode::DontAsk => Arc::new(DontAskModeStrategy),
            PermissionMode::BypassPermissions => Arc::new(BypassPermissionsModeStrategy),
        }
    }

    /// Get current permission mode
    pub fn mode(&self) -> PermissionMode {
        self.mode
    }

    /// Set permission mode
    pub fn set_mode(&mut self, mode: PermissionMode) {
        self.mode = mode;
        self.strategy = Self::create_strategy(mode);
    }

    /// Set the permission checker
    pub fn set_checker(&mut self, checker: Arc<RwLock<PermissionChecker>>) {
        self.checker = Some(checker);
    }

    /// Get mutable reference to checker (for adding runtime rules)
    pub async fn checker_mut(
        &mut self,
    ) -> Option<tokio::sync::RwLockWriteGuard<'_, PermissionChecker>> {
        if let Some(ref checker) = self.checker {
            Some(checker.write().await)
        } else {
            None
        }
    }

    /// Check if a tool operation should be auto-approved
    ///
    /// Returns true if the operation should proceed without user prompt.
    ///
    /// Delegates to the current strategy.
    pub fn should_auto_approve(&self, tool_name: &str, input: &serde_json::Value) -> bool {
        self.strategy.should_auto_approve(tool_name, input)
    }

    /// Check if a tool is blocked in current mode
    ///
    /// Returns true if the tool is blocked.
    ///
    /// Note: This method doesn't take tool_input, so it's less precise than
    /// the strategy method. For plan mode, it conservatively blocks all writes
    /// since it can't check if the file is in the plans directory.
    pub fn is_tool_blocked(&self, tool_name: &str) -> bool {
        self.strategy
            .is_tool_blocked(tool_name, &serde_json::Value::Null)
            .is_some()
    }

    /// Check permission for a tool with full context
    ///
    /// Combines strategy-based checking with settings rules.
    /// Returns the permission result.
    pub async fn check_permission(
        &self,
        tool_name: &str,
        tool_input: &serde_json::Value,
    ) -> ToolPermissionResult {
        // Check settings rules first (if available)
        if let Some(ref checker) = self.checker {
            let checker_read = checker.read().await;
            let result = checker_read.check_permission(tool_name, tool_input);
            match result.decision {
                PermissionDecision::Deny => {
                    return ToolPermissionResult::Blocked {
                        reason: result
                            .rule
                            .map(|r| format!("Denied by rule: {}", r))
                            .unwrap_or_else(|| "Denied by settings".to_string()),
                    };
                }
                PermissionDecision::Allow => {
                    return ToolPermissionResult::Allowed;
                }
                PermissionDecision::Ask => {
                    // Fall through to strategy-based check
                }
            }
        }

        // Use strategy for mode-specific logic
        let strategy_result = self.strategy.check_permission(tool_name, tool_input);

        // Special handling for DontAsk mode: convert NeedsPermission to Blocked
        if self.mode == PermissionMode::DontAsk {
            if strategy_result == ToolPermissionResult::NeedsPermission {
                return ToolPermissionResult::Blocked {
                    reason: "Tool not pre-approved by settings rules in DontAsk mode".to_string(),
                };
            }
        }

        // User interaction tools should always be allowed
        if matches!(
            tool_name,
            "AskUserQuestion" | "Task" | "TodoWrite" | "SlashCommand"
        ) {
            return ToolPermissionResult::Allowed;
        }

        strategy_result
    }

    /// Add a runtime allow rule (e.g., from user's "Always Allow" choice)
    pub async fn add_allow_rule(&self, tool_name: &str) {
        if let Some(ref checker) = self.checker {
            let mut checker_write = checker.write().await;
            checker_write.add_allow_rule(tool_name);
        }
    }

    /// Add a fine-grained allow rule based on tool call details
    /// This is used for "Always Allow" with specific parameters
    pub async fn add_allow_rule_for_tool_call(
        &self,
        tool_name: &str,
        tool_input: &serde_json::Value,
    ) {
        if let Some(ref checker) = self.checker {
            let mut checker_write = checker.write().await;
            checker_write.add_allow_rule_for_tool_call(tool_name, tool_input);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_permission_mode_parse() {
        assert_eq!(
            PermissionMode::parse("default"),
            Some(PermissionMode::Default)
        );
        assert_eq!(
            PermissionMode::parse("acceptEdits"),
            Some(PermissionMode::AcceptEdits)
        );
        assert_eq!(PermissionMode::parse("plan"), Some(PermissionMode::Plan));
        assert_eq!(
            PermissionMode::parse("bypassPermissions"),
            Some(PermissionMode::BypassPermissions)
        );
        assert_eq!(PermissionMode::parse("invalid"), None);
    }

    #[test]
    fn test_permission_mode_str() {
        assert_eq!(PermissionMode::Default.as_str(), "default");
        assert_eq!(PermissionMode::AcceptEdits.as_str(), "acceptEdits");
        assert_eq!(PermissionMode::Plan.as_str(), "plan");
        assert_eq!(
            PermissionMode::BypassPermissions.as_str(),
            "bypassPermissions"
        );
    }

    #[test]
    fn test_permission_handler_default() {
        let handler = PermissionHandler::new();
        let input = json!({});

        // Default mode auto-approves reads
        assert!(handler.should_auto_approve("Read", &input));
        assert!(handler.should_auto_approve("Glob", &input));
        assert!(handler.should_auto_approve("Grep", &input));
        assert!(handler.should_auto_approve("LS", &input));
        assert!(handler.should_auto_approve("NotebookRead", &input));
        // But not writes - these require permission
        assert!(!handler.should_auto_approve("Edit", &input));
        assert!(!handler.should_auto_approve("Bash", &input));
    }

    #[test]
    fn test_permission_handler_accept_edits() {
        let handler = PermissionHandler::with_mode(PermissionMode::AcceptEdits);
        let input = json!({});

        // AcceptEdits now auto-approves ALL tools (same as BypassPermissions)
        // This is needed for root user compatibility
        assert!(handler.should_auto_approve("Read", &input));
        assert!(handler.should_auto_approve("Edit", &input));
        assert!(handler.should_auto_approve("Write", &input));
        assert!(handler.should_auto_approve("Bash", &input));
    }

    #[test]
    fn test_permission_handler_bypass() {
        let handler = PermissionHandler::with_mode(PermissionMode::BypassPermissions);
        let input = json!({});

        // Everything auto-approved
        assert!(handler.should_auto_approve("Read", &input));
        assert!(handler.should_auto_approve("Edit", &input));
        assert!(handler.should_auto_approve("Bash", &input));
    }

    #[test]
    fn test_permission_handler_plan_mode() {
        let handler = PermissionHandler::with_mode(PermissionMode::Plan);
        let input = json!({});

        // Only reads auto-approved
        assert!(handler.should_auto_approve("Read", &input));
        assert!(handler.should_auto_approve("Glob", &input));
        assert!(handler.should_auto_approve("Grep", &input));
        assert!(handler.should_auto_approve("LS", &input));
        assert!(handler.should_auto_approve("NotebookRead", &input));
        assert!(!handler.should_auto_approve("Edit", &input));

        // Writes are blocked
        assert!(handler.is_tool_blocked("Edit"));
        assert!(handler.is_tool_blocked("Bash"));
        assert!(!handler.is_tool_blocked("Read"));
        assert!(!handler.is_tool_blocked("LS"));
    }

    #[tokio::test]
    async fn test_plan_mode_strategy_allows_plan_file_writes() {
        let handler = PermissionHandler::with_mode(PermissionMode::Plan);
        let home = dirs::home_dir().unwrap();
        let plan_file = home.join(".claude").join("plans").join("test.md");

        match handler
            .check_permission(
                "Write",
                &json!({"file_path": plan_file.to_str().unwrap(), "content": "test"}),
            )
            .await
        {
            ToolPermissionResult::Allowed => {}
            _ => panic!("Expected Allowed for plan file writes"),
        }
    }

    #[tokio::test]
    async fn test_plan_mode_strategy_blocks_non_plan_writes() {
        let handler = PermissionHandler::with_mode(PermissionMode::Plan);

        match handler
            .check_permission(
                "Write",
                &json!({"file_path": "/tmp/test.txt", "content": "test"}),
            )
            .await
        {
            ToolPermissionResult::Blocked { .. } => {}
            _ => panic!("Expected Blocked for non-plan file writes"),
        }
    }

    #[tokio::test]
    async fn test_default_mode_strategy() {
        let handler = PermissionHandler::new();

        // Reads are auto-approved
        match handler.check_permission("Read", &json!({})).await {
            ToolPermissionResult::Allowed => {}
            _ => panic!("Expected Allowed for Read"),
        }

        // Writes need permission
        match handler.check_permission("Write", &json!({})).await {
            ToolPermissionResult::NeedsPermission => {}
            _ => panic!("Expected NeedsPermission for Write"),
        }
    }

    #[tokio::test]
    async fn test_bypass_permissions_strategy() {
        let handler = PermissionHandler::with_mode(PermissionMode::BypassPermissions);

        // Everything is allowed
        match handler
            .check_permission("Bash", &json!({"command": "rm -rf /"}))
            .await
        {
            ToolPermissionResult::Allowed => {}
            _ => panic!("Expected Allowed for Bash in BypassPermissions mode"),
        }
    }

    #[tokio::test]
    async fn test_accept_edits_strategy() {
        let handler = PermissionHandler::with_mode(PermissionMode::AcceptEdits);

        // Everything is allowed
        match handler.check_permission("Write", &json!({})).await {
            ToolPermissionResult::Allowed => {}
            _ => panic!("Expected Allowed for Write in AcceptEdits mode"),
        }
    }
}