smart-tree 8.0.1

Smart Tree - An intelligent, AI-friendly directory visualization tool
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
//! Permission-based tool gating system for MCP
//!
//! This module implements a smart permission checking system that:
//! 1. Requires digest/verification before other tools can be used
//! 2. Only exposes tools that are relevant based on permissions
//! 3. Saves context by hiding unavailable operations
//! 4. Provides helpful comments about why tools are unavailable

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};

/// Permission state for a path
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PathPermissions {
    /// Path that was checked
    pub path: PathBuf,
    /// Whether the path exists
    pub exists: bool,
    /// Whether we can read the path
    pub readable: bool,
    /// Whether we can write to the path
    pub writable: bool,
    /// Whether it's a directory
    pub is_directory: bool,
    /// Whether it's a file
    pub is_file: bool,
    /// When this was last verified
    pub verified_at: SystemTime,
    /// Any error messages
    pub error: Option<String>,
}

/// Permission cache for verified paths
#[derive(Debug, Default)]
pub struct PermissionCache {
    /// Cached permissions by path
    permissions: HashMap<PathBuf, PathPermissions>,
    /// How long to cache permissions (default: 5 minutes)
    cache_duration: Duration,
}

impl PermissionCache {
    pub fn new() -> Self {
        Self {
            permissions: HashMap::new(),
            cache_duration: Duration::from_secs(300), // 5 minutes
        }
    }

    /// Check if a path has been verified recently
    pub fn is_verified(&self, path: &Path) -> bool {
        if let Some(perms) = self.permissions.get(path) {
            if let Ok(elapsed) = perms.verified_at.elapsed() {
                return elapsed < self.cache_duration;
            }
        }
        false
    }

    /// Get cached permissions for a path
    pub fn get(&self, path: &Path) -> Option<&PathPermissions> {
        self.permissions
            .get(path)
            .filter(|p| p.verified_at.elapsed().unwrap_or(Duration::MAX) < self.cache_duration)
    }

    /// Verify and cache permissions for a path
    pub fn verify(&mut self, path: &Path) -> Result<PathPermissions> {
        // Check if path exists
        let exists = path.exists();
        if !exists {
            let perms = PathPermissions {
                path: path.to_path_buf(),
                exists: false,
                readable: false,
                writable: false,
                is_directory: false,
                is_file: false,
                verified_at: SystemTime::now(),
                error: Some("Path does not exist".to_string()),
            };
            self.permissions.insert(path.to_path_buf(), perms.clone());
            return Ok(perms);
        }

        // Get metadata
        let metadata = match fs::metadata(path) {
            Ok(m) => m,
            Err(e) => {
                let perms = PathPermissions {
                    path: path.to_path_buf(),
                    exists: true,
                    readable: false,
                    writable: false,
                    is_directory: false,
                    is_file: false,
                    verified_at: SystemTime::now(),
                    error: Some(format!("Cannot read metadata: {}", e)),
                };
                self.permissions.insert(path.to_path_buf(), perms.clone());
                return Ok(perms);
            }
        };

        let is_directory = metadata.is_dir();
        let is_file = metadata.is_file();

        // Check read permission
        let readable = if is_directory {
            fs::read_dir(path).is_ok()
        } else {
            fs::File::open(path).is_ok()
        };

        // Check write permission
        let writable = !metadata.permissions().readonly();

        let perms = PathPermissions {
            path: path.to_path_buf(),
            exists,
            readable,
            writable,
            is_directory,
            is_file,
            verified_at: SystemTime::now(),
            error: None,
        };

        self.permissions.insert(path.to_path_buf(), perms.clone());
        Ok(perms)
    }

    /// Clear expired entries
    pub fn cleanup(&mut self) {
        self.permissions
            .retain(|_, p| p.verified_at.elapsed().unwrap_or(Duration::MAX) < self.cache_duration);
    }
}

/// Tool availability based on permissions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolAvailability {
    /// Tool name
    pub name: String,
    /// Whether the tool is available
    pub available: bool,
    /// Reason why the tool is unavailable (if applicable)
    pub reason: Option<String>,
    /// Required permissions for this tool
    pub requires: Vec<String>,
}

/// Get available tools based on path permissions
pub fn get_available_tools(perms: &PathPermissions) -> Vec<ToolAvailability> {
    let mut tools = vec![];

    // Always available tools (verification tools)
    tools.push(ToolAvailability {
        name: "get_digest".to_string(),
        available: true,
        reason: None,
        requires: vec![],
    });

    tools.push(ToolAvailability {
        name: "server_info".to_string(),
        available: true,
        reason: None,
        requires: vec![],
    });

    // Read-only tools
    if perms.readable {
        tools.extend(vec![
            ToolAvailability {
                name: "analyze_directory".to_string(),
                available: perms.is_directory,
                reason: if !perms.is_directory {
                    Some("Path is not a directory".to_string())
                } else {
                    None
                },
                requires: vec!["read".to_string(), "directory".to_string()],
            },
            ToolAvailability {
                name: "quick_tree".to_string(),
                available: perms.is_directory,
                reason: if !perms.is_directory {
                    Some("Path is not a directory".to_string())
                } else {
                    None
                },
                requires: vec!["read".to_string(), "directory".to_string()],
            },
            ToolAvailability {
                name: "find_files".to_string(),
                available: perms.is_directory,
                reason: if !perms.is_directory {
                    Some("Path is not a directory".to_string())
                } else {
                    None
                },
                requires: vec!["read".to_string(), "directory".to_string()],
            },
            ToolAvailability {
                name: "search_in_files".to_string(),
                available: perms.is_directory,
                reason: if !perms.is_directory {
                    Some("Path is not a directory".to_string())
                } else {
                    None
                },
                requires: vec!["read".to_string(), "directory".to_string()],
            },
            ToolAvailability {
                name: "get_statistics".to_string(),
                available: perms.is_directory,
                reason: if !perms.is_directory {
                    Some("Path is not a directory".to_string())
                } else {
                    None
                },
                requires: vec!["read".to_string(), "directory".to_string()],
            },
            ToolAvailability {
                name: "get_function_tree".to_string(),
                available: perms.is_file,
                reason: if !perms.is_file {
                    Some("Path is not a file".to_string())
                } else {
                    None
                },
                requires: vec!["read".to_string(), "file".to_string()],
            },
        ]);
    } else {
        // Add read tools as unavailable with reason
        tools.extend(vec![
            ToolAvailability {
                name: "analyze_directory".to_string(),
                available: false,
                reason: Some("No read permission for this path".to_string()),
                requires: vec!["read".to_string()],
            },
            ToolAvailability {
                name: "quick_tree".to_string(),
                available: false,
                reason: Some("No read permission for this path".to_string()),
                requires: vec!["read".to_string()],
            },
        ]);
    }

    // Write tools
    if perms.writable && perms.readable {
        tools.extend(vec![
            ToolAvailability {
                name: "smart_edit".to_string(),
                available: perms.is_file,
                reason: if !perms.is_file {
                    Some("Can only edit files, not directories".to_string())
                } else {
                    None
                },
                requires: vec!["read".to_string(), "write".to_string(), "file".to_string()],
            },
            ToolAvailability {
                name: "insert_function".to_string(),
                available: perms.is_file,
                reason: if !perms.is_file {
                    Some("Can only edit files, not directories".to_string())
                } else {
                    None
                },
                requires: vec!["read".to_string(), "write".to_string(), "file".to_string()],
            },
            ToolAvailability {
                name: "remove_function".to_string(),
                available: perms.is_file,
                reason: if !perms.is_file {
                    Some("Can only edit files, not directories".to_string())
                } else {
                    None
                },
                requires: vec!["read".to_string(), "write".to_string(), "file".to_string()],
            },
            ToolAvailability {
                name: "track_file_operation".to_string(),
                available: perms.is_file,
                reason: if !perms.is_file {
                    Some("Can only track operations on files".to_string())
                } else {
                    None
                },
                requires: vec!["read".to_string(), "write".to_string(), "file".to_string()],
            },
        ]);
    } else if !perms.writable && perms.readable {
        // Add write tools as unavailable
        tools.extend(vec![
            ToolAvailability {
                name: "smart_edit".to_string(),
                available: false,
                reason: Some("File is read-only - no write permission".to_string()),
                requires: vec!["write".to_string()],
            },
            ToolAvailability {
                name: "insert_function".to_string(),
                available: false,
                reason: Some("File is read-only - no write permission".to_string()),
                requires: vec!["write".to_string()],
            },
            ToolAvailability {
                name: "remove_function".to_string(),
                available: false,
                reason: Some("File is read-only - no write permission".to_string()),
                requires: vec!["write".to_string()],
            },
        ]);
    }

    tools
}

/// Check if a specific tool is available for a path
pub fn _is_tool_available(tool_name: &str, perms: &PathPermissions) -> (bool, Option<String>) {
    let tools = get_available_tools(perms);
    for tool in tools {
        if tool.name == tool_name {
            return (tool.available, tool.reason);
        }
    }
    // Tool not found in permission system - might be always available
    (true, None)
}

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

    use tempfile::TempDir;

    #[test]
    fn test_permission_cache() {
        let mut cache = PermissionCache::new();
        let temp_dir = TempDir::new().unwrap();
        let path = temp_dir.path();

        // Verify directory permissions
        let perms = cache.verify(path).unwrap();
        assert!(perms.exists);
        assert!(perms.readable);
        assert!(perms.is_directory);
        assert!(!perms.is_file);

        // Check caching
        assert!(cache.is_verified(path));
    }

    #[test]
    fn test_tool_availability() {
        // Test with readable directory
        let dir_perms = PathPermissions {
            path: PathBuf::from("/test"),
            exists: true,
            readable: true,
            writable: true,
            is_directory: true,
            is_file: false,
            verified_at: SystemTime::now(),
            error: None,
        };

        let tools = get_available_tools(&dir_perms);

        // Check that directory tools are available
        let analyze = tools
            .iter()
            .find(|t| t.name == "analyze_directory")
            .unwrap();
        assert!(analyze.available);

        // Check that file tools are not available for directories
        let edit = tools.iter().find(|t| t.name == "smart_edit").unwrap();
        assert!(!edit.available);
        assert_eq!(
            edit.reason,
            Some("Can only edit files, not directories".to_string())
        );

        // Test with read-only file
        let ro_file_perms = PathPermissions {
            path: PathBuf::from("/test.txt"),
            exists: true,
            readable: true,
            writable: false,
            is_directory: false,
            is_file: true,
            verified_at: SystemTime::now(),
            error: None,
        };

        let tools = get_available_tools(&ro_file_perms);

        // Check that edit tools are unavailable
        let edit = tools.iter().find(|t| t.name == "smart_edit").unwrap();
        assert!(!edit.available);
        assert_eq!(
            edit.reason,
            Some("File is read-only - no write permission".to_string())
        );
    }
}