ai-agent 0.88.0

Idiomatic agent sdk inspired by the claude code source leak
Documentation
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// Source: ~/claudecode/openclaudecode/src/utils/permissions/filesystem.ts
#![allow(dead_code)]

//! Filesystem-related permission utilities.
//!
//! Handles path validation, dangerous file detection, auto-edit safety checks,
//! and working directory permission validation.

use crate::types::permissions::{
    PermissionDecision, PermissionDecisionReason, PermissionRule, PermissionUpdate,
    PermissionUpdateDestination, ToolPermissionContext,
};
use std::path::{MAIN_SEPARATOR, Path, PathBuf};

/// Dangerous files that should be protected from auto-editing.
/// These files can be used for code execution or data exfiltration.
pub const DANGEROUS_FILES: &[&str] = &[
    ".gitconfig",
    ".gitmodules",
    ".bashrc",
    ".bash_profile",
    ".zshrc",
    ".zprofile",
    ".profile",
    ".ripgreprc",
    ".mcp.json",
    ".claude.json",
];

/// Dangerous directories that should be protected from auto-editing.
pub const DANGEROUS_DIRECTORIES: &[&str] = &[".git", ".vscode", ".idea", ".claude"];

/// Normalizes a path for case-insensitive comparison.
/// Prevents bypassing security checks using mixed-case paths
/// on case-insensitive filesystems (macOS/Windows).
pub fn normalize_case_for_comparison(path: &str) -> String {
    path.to_lowercase()
}

/// If file_path is inside a .claude/skills/{name}/ directory (project or global),
/// return the skill name and a session-allow pattern scoped to just that skill.
pub fn get_claude_skill_scope(file_path: &str) -> Option<(String, String)> {
    let absolute_path = expand_path(file_path);
    let absolute_path_lower = normalize_case_for_comparison(&absolute_path);

    let cwd = std::env::current_dir().ok()?;
    let home = dirs::home_dir()?;

    let bases = [
        (
            cwd.join(".claude").join("skills"),
            "/.claude/skills/".to_string(),
        ),
        (
            home.join(".claude").join("skills"),
            "~/.claude/skills/".to_string(),
        ),
    ];

    for (dir, prefix) in &bases {
        let dir_lower = normalize_case_for_comparison(&dir.to_string_lossy());
        for sep_char in [MAIN_SEPARATOR, '/'] {
            let sep_lower = sep_char.to_lowercase().to_string();
            if absolute_path_lower.starts_with(&format!("{}{}", dir_lower, sep_lower)) {
                let dir_str = dir.to_string_lossy();
                let rest = &absolute_path[dir_str.len() + 1..];
                let slash = rest.find('/');
                let bslash = if MAIN_SEPARATOR == '\\' {
                    rest.find('\\')
                } else {
                    None
                };
                let cut = match (slash, bslash) {
                    (None, None) => return None,
                    (Some(s), None) => s,
                    (None, Some(b)) => b,
                    (Some(s), Some(b)) => s.min(b),
                };
                if cut == 0 {
                    return None;
                }
                let skill_name = &rest[..cut];
                if skill_name.is_empty() || skill_name == "." || skill_name.contains("..") {
                    return None;
                }
                // Reject glob metacharacters
                if skill_name.contains('*')
                    || skill_name.contains('?')
                    || skill_name.contains('[')
                    || skill_name.contains(']')
                {
                    return None;
                }
                return Some((
                    skill_name.to_string(),
                    format!("{}{}/**", prefix, skill_name),
                ));
            }
        }
    }

    None
}

/// Expands tilde (~) at the start of a path to the user's home directory.
pub fn expand_tilde(path: &str) -> String {
    if path == "~" || path.starts_with("~/") || (cfg!(windows) && path.starts_with("~\\")) {
        if let Some(home) = dirs::home_dir() {
            return format!("{}{}", home.to_string_lossy(), &path[1..]);
        }
    }
    path.to_string()
}

/// Expands a path, resolving tilde and making absolute.
pub fn expand_path(path: &str) -> String {
    let expanded = expand_tilde(path);
    let p = Path::new(&expanded);
    if p.is_absolute() {
        p.to_string_lossy().to_string()
    } else {
        std::env::current_dir()
            .ok()
            .map(|cwd| cwd.join(p).to_string_lossy().to_string())
            .unwrap_or(expanded)
    }
}

/// Converts a path to POSIX format for pattern matching.
pub fn to_posix_path(path: &str) -> String {
    if cfg!(windows) {
        path.replace('\\', "/")
    } else {
        path.to_string()
    }
}

/// Calculates a relative path using POSIX separators.
pub fn relative_path(from: &str, to: &str) -> String {
    let from_path = Path::new(from);
    let to_path = Path::new(to);
    if let Ok(rel) = to_path.strip_prefix(from_path) {
        to_posix_path(&rel.to_string_lossy())
    } else {
        to.to_string()
    }
}

/// Checks if the file path is a Claude settings path.
pub fn is_claude_settings_path(file_path: &str) -> bool {
    let expanded = expand_path(file_path);
    let normalized = normalize_case_for_comparison(&expanded);
    let sep = MAIN_SEPARATOR.to_string();

    normalized.ends_with(&format!("{}{}claude{}settings.json", sep, sep, sep))
        || normalized.ends_with(&format!("{}{}claude{}settings.local.json", sep, sep, sep))
}

/// Checks if the file path is a Claude config file path.
pub fn is_claude_config_file_path(file_path: &str) -> bool {
    if is_claude_settings_path(file_path) {
        return true;
    }

    let cwd = std::env::current_dir().ok().unwrap_or_default();
    let commands_dir = cwd.join(".claude").join("commands");
    let agents_dir = cwd.join(".claude").join("agents");
    let skills_dir = cwd.join(".claude").join("skills");

    path_in_working_path(file_path, &commands_dir.to_string_lossy())
        || path_in_working_path(file_path, &agents_dir.to_string_lossy())
        || path_in_working_path(file_path, &skills_dir.to_string_lossy())
}

/// Checks if a path is within a working path.
pub fn path_in_working_path(path: &str, working_path: &str) -> bool {
    let absolute_path = expand_path(path);
    let absolute_working_path = expand_path(working_path);

    // Handle macOS symlink issues
    let normalized_path = absolute_path
        .replace("/private/var/", "/var/")
        .replace("/private/tmp/", "/tmp/")
        .replace("/private/tmp", "/tmp");
    let normalized_working_path = absolute_working_path
        .replace("/private/var/", "/var/")
        .replace("/private/tmp/", "/tmp/")
        .replace("/private/tmp", "/tmp");

    let case_normalized_path = normalize_case_for_comparison(&normalized_path);
    let case_normalized_working_path = normalize_case_for_comparison(&normalized_working_path);

    let relative = relative_path(&case_normalized_working_path, &case_normalized_path);
    if relative.is_empty() {
        return true;
    }

    if contains_path_traversal(&relative) {
        return false;
    }

    !Path::new(&relative).is_absolute()
}

/// Checks if a path contains traversal sequences.
pub fn contains_path_traversal(path: &str) -> bool {
    path.split(MAIN_SEPARATOR).any(|c| c == "..")
        || path.split('/').any(|c| c == "..")
        || path.split('\\').any(|c| c == "..")
}

/// Checks if a path has suspicious Windows patterns.
pub fn has_suspicious_windows_path_pattern(path: &str) -> bool {
    // Check for NTFS Alternate Data Streams (Windows/WSL only)
    if cfg!(windows) || std::env::var("WSL_DISTRO_NAME").is_ok() {
        let colon_index = path[2..].find(':');
        if colon_index.is_some() {
            return true;
        }
    }

    // Check for 8.3 short names
    if path.contains("~") {
        let re = regex::Regex::new(r"~\d").unwrap();
        if re.is_match(path) {
            return true;
        }
    }

    // Check for long path prefixes
    if path.starts_with(r"\\?\")
        || path.starts_with(r"\\.\")
        || path.starts_with("//?/")
        || path.starts_with("//./")
    {
        return true;
    }

    // Check for trailing dots and spaces
    if path.ends_with(|c: char| c == '.' || c.is_whitespace()) {
        return true;
    }

    // Check for DOS device names
    let dos_device_re = regex::Regex::new(r"\.(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])$").unwrap();
    if dos_device_re.is_match(path) {
        return true;
    }

    // Check for three or more consecutive dots as path component
    let dots_re = regex::Regex::new(r"(^|/|\\)\.{3,}(/|\\|$)").unwrap();
    if dots_re.is_match(path) {
        return true;
    }

    false
}

/// Checks if a file path is dangerous for auto-edit.
fn is_dangerous_file_path_to_auto_edit(path: &str) -> bool {
    let absolute_path = expand_path(path);
    let path_segments: Vec<&str> = absolute_path.split(MAIN_SEPARATOR).collect();
    let file_name = path_segments.last().copied().unwrap_or("");

    // Block UNC paths
    if path.starts_with("\\\\") || path.starts_with("//") {
        return true;
    }

    // Check dangerous directories
    for segment in &path_segments {
        let normalized_segment = normalize_case_for_comparison(segment);
        for dir in DANGEROUS_DIRECTORIES {
            if normalized_segment == normalize_case_for_comparison(dir) {
                // Special case: .claude/worktrees/ is not dangerous
                if *dir == ".claude" {
                    let idx = path_segments
                        .iter()
                        .position(|&s| s == *segment)
                        .unwrap_or(0);
                    if idx + 1 < path_segments.len() {
                        let next = path_segments[idx + 1];
                        if normalize_case_for_comparison(next) == "worktrees" {
                            continue;
                        }
                    }
                }
                return true;
            }
        }
    }

    // Check dangerous files
    if !file_name.is_empty() {
        let normalized_file_name = normalize_case_for_comparison(file_name);
        if DANGEROUS_FILES
            .iter()
            .any(|df| normalize_case_for_comparison(df) == normalized_file_name)
        {
            return true;
        }
    }

    false
}

/// Checks if a path is safe for auto-editing.
pub fn check_path_safety_for_auto_edit(
    path: &str,
    _precomputed_paths_to_check: Option<&[String]>,
) -> PathSafetyResult {
    let path_to_check = path.to_string();

    // Check for suspicious Windows path patterns
    if has_suspicious_windows_path_pattern(&path_to_check) {
        return PathSafetyResult::Unsafe {
            message: format!(
                "Claude requested permissions to write to {}, which contains a suspicious Windows path pattern that requires manual approval.",
                path
            ),
            classifier_approvable: false,
        };
    }

    // Check for Claude config files
    if is_claude_config_file_path(&path_to_check) {
        return PathSafetyResult::Unsafe {
            message: format!(
                "Claude requested permissions to write to {}, but you haven't granted it yet.",
                path
            ),
            classifier_approvable: true,
        };
    }

    // Check for dangerous files
    if is_dangerous_file_path_to_auto_edit(&path_to_check) {
        return PathSafetyResult::Unsafe {
            message: format!(
                "Claude requested permissions to edit {} which is a sensitive file.",
                path
            ),
            classifier_approvable: true,
        };
    }

    PathSafetyResult::Safe
}

/// Result of a path safety check.
pub enum PathSafetyResult {
    Safe,
    Unsafe {
        message: String,
        classifier_approvable: bool,
    },
}

/// Checks if a resolved path is dangerous for removal operations.
pub fn is_dangerous_removal_path(resolved_path: &str) -> bool {
    let forward_slashed = resolved_path.replace(&['\\', '/'][..], "/");

    if forward_slashed == "*" || forward_slashed.ends_with("/*") {
        return true;
    }

    let normalized_path = if forward_slashed == "/" {
        forward_slashed.clone()
    } else {
        forward_slashed.trim_end_matches('/').to_string()
    };

    if normalized_path == "/" {
        return true;
    }

    let drive_root_re = regex::Regex::new(r"^[A-Za-z]:/?$").unwrap();
    if drive_root_re.is_match(&normalized_path) {
        return true;
    }

    if let Some(home) = dirs::home_dir() {
        let normalized_home = home.to_string_lossy().replace('\\', "/");
        if normalized_path == normalized_home {
            return true;
        }
    }

    let parent = Path::new(&normalized_path)
        .parent()
        .map(|p| p.to_string_lossy().to_string());
    if parent.as_deref() == Some("/") {
        return true;
    }

    let drive_child_re = regex::Regex::new(r"^[A-Za-z]:/[^/]+$").unwrap();
    if drive_child_re.is_match(&normalized_path) {
        return true;
    }

    false
}

/// Validates a glob pattern by checking its base directory.
pub fn get_glob_base_directory(path: &str) -> String {
    let glob_pattern_re = regex::Regex::new(r"[*?\[\]{}]").unwrap();
    if let Some(m) = glob_pattern_re.find(path) {
        let before_glob = &path[..m.start()];
        let last_sep = before_glob.rfind('/');
        if let Some(idx) = last_sep {
            if idx == 0 {
                return "/".to_string();
            }
            return before_glob[..idx].to_string();
        }
        return ".".to_string();
    }
    path.to_string()
}

/// Checks if a resolved path is allowed for the given operation type.
pub fn is_path_allowed(
    resolved_path: &str,
    _context: &ToolPermissionContext,
    _operation_type: FileOperationType,
    _precomputed_paths_to_check: Option<&[String]>,
) -> PathCheckResult {
    // Simplified implementation — full implementation requires tool context integration
    PathCheckResult {
        allowed: false,
        decision_reason: None,
    }
}

/// Type of file operation.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum FileOperationType {
    Read,
    Write,
    Create,
}

/// Result of a path check.
pub struct PathCheckResult {
    pub allowed: bool,
    pub decision_reason: Option<PermissionDecisionReason>,
}

/// Session memory directory path.
pub fn get_session_memory_dir() -> String {
    let project_dir = std::env::current_dir()
        .ok()
        .unwrap_or_default()
        .to_string_lossy()
        .to_string();
    format!("{}/session-memory/", project_dir)
}

/// Session memory file path.
pub fn get_session_memory_path() -> String {
    format!("{}summary.md", get_session_memory_dir())
}

/// Checks if path is within the session memory directory.
fn is_session_memory_path(absolute_path: &str) -> bool {
    let normalized = Path::new(absolute_path).to_string_lossy().to_string();
    normalized.starts_with(&get_session_memory_dir())
}

/// Checks if a path is an internal editable path.
pub fn check_editable_internal_path(_path: &str, _input: &serde_json::Value) -> InternalPathResult {
    InternalPathResult::Passthrough
}

/// Checks if a path is an internal readable path.
pub fn check_readable_internal_path(_path: &str, _input: &serde_json::Value) -> InternalPathResult {
    InternalPathResult::Passthrough
}

/// Result of internal path check.
pub enum InternalPathResult {
    Allow {
        decision_reason: PermissionDecisionReason,
    },
    Passthrough,
}

/// Gets all working directories from context.
pub fn all_working_directories(context: &ToolPermissionContext) -> Vec<String> {
    let mut dirs = vec![
        std::env::current_dir()
            .ok()
            .unwrap_or_default()
            .to_string_lossy()
            .to_string(),
    ];
    dirs.extend(context.additional_working_directories.keys().cloned());
    dirs
}

/// Generates permission suggestions for a path.
pub fn generate_suggestions(
    _file_path: &str,
    _operation_type: &str,
    _tool_permission_context: &ToolPermissionContext,
    _paths_to_check: Option<&[String]>,
) -> Vec<PermissionUpdate> {
    vec![]
}

/// Matching rule for input path.
pub fn matching_rule_for_input(
    _path: &str,
    _tool_permission_context: &ToolPermissionContext,
    _tool_type: &str,
    _behavior: &str,
) -> Option<PermissionRule> {
    None
}

/// Checks if path is in allowed working path.
pub fn path_in_allowed_working_path(
    path: &str,
    tool_permission_context: &ToolPermissionContext,
    _precomputed_paths_to_check: Option<&[String]>,
) -> bool {
    let working_paths = all_working_directories(tool_permission_context);
    for working_path in &working_paths {
        if path_in_working_path(path, working_path) {
            return true;
        }
    }
    false
}

/// Formats directory list for display.
pub fn format_directory_list(directories: &[String]) -> String {
    const MAX_DIRS: usize = 5;
    let dir_count = directories.len();

    if dir_count <= MAX_DIRS {
        return directories
            .iter()
            .map(|d| format!("'{}'", d))
            .collect::<Vec<_>>()
            .join(", ");
    }

    let first_dirs = directories[..MAX_DIRS]
        .iter()
        .map(|d| format!("'{}'", d))
        .collect::<Vec<_>>()
        .join(", ");

    format!("{}, and {} more", first_dirs, dir_count - MAX_DIRS)
}