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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//
// -----------------------------------------------------------------------------
//  INTEREST SCORING: Surfacing What Matters
//
//  This module is the heart of Smart Tree's intelligent scanning. Instead of
//  listing everything, we score each file/folder by "interest" - how relevant
//  is this to the developer or AI right now?
//
//  Key concepts:
//  - TraversalPath: How did we reach this location? (direct, symlink, mount, etc.)
//  - InterestScore: A 0.0-1.0 score with breakdown of contributing factors
//  - InterestLevel: Human-friendly categorization (Boring → Critical)
//
//  "The goal is signal, not noise." - Omni
// -----------------------------------------------------------------------------
//

use crate::scanner::FilesystemType;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::time::SystemTime;

/// How we reached this location during traversal.
/// This context helps understand if a path is "real" or indirect.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TraversalPath {
    /// Directly under the scan root via normal directory traversal
    Direct,

    /// Reached via symbolic link
    Symlink {
        /// The actual target of the symlink
        target: PathBuf,
        /// Whether the target exists
        target_exists: bool,
    },

    /// Crossed a mount point boundary
    Mount {
        /// The filesystem type we're now on
        filesystem: FilesystemType,
        /// Mount point path
        mount_point: PathBuf,
    },

    /// Reached via recursive traversal into a nested structure
    Recursive {
        /// How deep we are from the original interesting location
        depth: usize,
        /// The original path that led us here
        original: PathBuf,
    },

    /// Inside a dependency/vendor directory (node_modules, vendor, etc.)
    Dependency {
        /// Type of dependency manager
        manager: DependencyManager,
        /// Root of the dependency tree
        dep_root: PathBuf,
    },
}

/// Types of dependency managers we recognize
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DependencyManager {
    /// npm/yarn/pnpm (node_modules)
    Npm,
    /// Cargo (target/debug, target/release)
    Cargo,
    /// pip/venv/virtualenv
    Python,
    /// Go modules (vendor)
    Go,
    /// Ruby gems (vendor/bundle)
    Ruby,
    /// Composer (vendor)
    Composer,
    /// Maven/Gradle (.m2, build)
    Java,
    /// Unknown dependency manager
    Unknown,
}

impl DependencyManager {
    /// Get the typical directory name for this dependency manager
    pub fn dir_name(&self) -> &'static str {
        match self {
            Self::Npm => "node_modules",
            Self::Cargo => "target",
            Self::Python => ".venv",
            Self::Go => "vendor",
            Self::Ruby => "vendor",
            Self::Composer => "vendor",
            Self::Java => "build",
            Self::Unknown => "",
        }
    }

    /// Detect dependency manager from a directory name
    pub fn from_dir_name(name: &str) -> Option<Self> {
        match name {
            "node_modules" => Some(Self::Npm),
            "target" => Some(Self::Cargo),
            ".venv" | "venv" | ".virtualenv" | "virtualenv" => Some(Self::Python),
            "vendor" => Some(Self::Go), // Could also be Ruby/Composer - context needed
            ".m2" | "build" | "out" => Some(Self::Java),
            _ => None,
        }
    }
}

/// Full traversal context for a node
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TraversalContext {
    /// How we reached this location
    pub path: TraversalPath,

    /// Depth from scan root
    pub depth_from_root: usize,

    /// Is this path inside a git worktree?
    pub in_git_worktree: bool,

    /// Is this inside a submodule?
    pub in_submodule: bool,

    /// Parent directory interest level (for inheritance)
    pub parent_interest: Option<InterestLevel>,
}

impl Default for TraversalContext {
    fn default() -> Self {
        Self {
            path: TraversalPath::Direct,
            depth_from_root: 0,
            in_git_worktree: false,
            in_submodule: false,
            parent_interest: None,
        }
    }
}

/// Interest level - human-readable categorization of importance
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum InterestLevel {
    /// Not worth showing (generated files, caches, etc.)
    Boring = 0,

    /// Exists but rarely relevant (most dependencies, build artifacts)
    #[default]
    Background = 1,

    /// Worth knowing about but not urgent
    Notable = 2,

    /// Should be surfaced to the user
    Important = 3,

    /// Must be shown - security issues, breaking changes, critical files
    Critical = 4,
}

impl InterestLevel {
    /// Get emoji representation
    pub fn emoji(&self) -> &'static str {
        match self {
            Self::Boring => "💤",
            Self::Background => "📦",
            Self::Notable => "📝",
            Self::Important => "🔥",
            Self::Critical => "⚠️",
        }
    }

    /// Get color name for terminal output
    pub fn color(&self) -> &'static str {
        match self {
            Self::Boring => "bright_black",
            Self::Background => "white",
            Self::Notable => "cyan",
            Self::Important => "yellow",
            Self::Critical => "red",
        }
    }

    /// Convert from float score to level
    pub fn from_score(score: f32) -> Self {
        match score {
            s if s >= 0.8 => Self::Critical,
            s if s >= 0.6 => Self::Important,
            s if s >= 0.4 => Self::Notable,
            s if s >= 0.2 => Self::Background,
            _ => Self::Boring,
        }
    }
}

/// Risk level for security-related factors
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum RiskLevel {
    /// No risk detected
    None = 0,
    /// Informational finding
    Info = 1,
    /// Low risk - worth noting
    Low = 2,
    /// Medium risk - should be reviewed
    Medium = 3,
    /// High risk - needs attention
    High = 4,
    /// Critical risk - immediate action needed
    Critical = 5,
}

impl RiskLevel {
    pub fn emoji(&self) -> &'static str {
        match self {
            Self::None => "",
            Self::Info => "ℹ️",
            Self::Low => "🔵",
            Self::Medium => "🟡",
            Self::High => "🟠",
            Self::Critical => "🔴",
        }
    }
}

/// Factors that contribute to a node's interest score
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum InterestFactor {
    /// File was recently modified
    RecentlyModified {
        /// Hours since last modification
        hours_ago: f32,
        /// Contribution to score (0.0-1.0)
        weight: f32,
    },

    /// Security pattern detected
    SecurityPattern {
        /// Risk level of the finding
        risk: RiskLevel,
        /// Brief description
        description: String,
        /// Contribution to score
        weight: f32,
    },

    /// This is a key project file (README, Cargo.toml, package.json, etc.)
    KeyProjectFile {
        /// Type of key file
        file_type: KeyFileType,
        /// Contribution to score
        weight: f32,
    },

    /// Changed since last scan
    ChangedSinceLastScan {
        /// Type of change
        change: ChangeType,
        /// Contribution to score
        weight: f32,
    },

    /// In a "hot" directory with frequent changes
    HotDirectory {
        /// Number of changes in recent period
        change_count: u32,
        /// Contribution to score
        weight: f32,
    },

    /// Suspicious dependency detected
    SuspiciousDependency {
        /// Reason for suspicion
        reason: String,
        /// Contribution to score
        weight: f32,
    },

    /// Git-related interest (uncommitted changes, conflicts, etc.)
    GitStatus {
        /// Type of git status
        status: GitStatusType,
        /// Contribution to score
        weight: f32,
    },

    /// Code complexity or size concern
    Complexity {
        /// Description of the complexity factor
        description: String,
        /// Contribution to score
        weight: f32,
    },

    /// Inside a dependency tree (usually reduces interest)
    InDependencyTree {
        /// Depth inside dependency tree
        depth: usize,
        /// Negative contribution to score
        weight: f32,
    },

    /// Custom user-defined interest factor
    Custom {
        /// Name of the custom factor
        name: String,
        /// Contribution to score
        weight: f32,
    },
}

impl InterestFactor {
    /// Get the weight contribution of this factor
    pub fn weight(&self) -> f32 {
        match self {
            Self::RecentlyModified { weight, .. } => *weight,
            Self::SecurityPattern { weight, .. } => *weight,
            Self::KeyProjectFile { weight, .. } => *weight,
            Self::ChangedSinceLastScan { weight, .. } => *weight,
            Self::HotDirectory { weight, .. } => *weight,
            Self::SuspiciousDependency { weight, .. } => *weight,
            Self::GitStatus { weight, .. } => *weight,
            Self::Complexity { weight, .. } => *weight,
            Self::InDependencyTree { weight, .. } => *weight,
            Self::Custom { weight, .. } => *weight,
        }
    }

    /// Get a short description of this factor
    pub fn description(&self) -> String {
        match self {
            Self::RecentlyModified { hours_ago, .. } => {
                format!("Modified {:.1}h ago", hours_ago)
            }
            Self::SecurityPattern { description, .. } => description.clone(),
            Self::KeyProjectFile { file_type, .. } => {
                format!("Key file: {:?}", file_type)
            }
            Self::ChangedSinceLastScan { change, .. } => {
                format!("Changed: {:?}", change)
            }
            Self::HotDirectory { change_count, .. } => {
                format!("{} recent changes", change_count)
            }
            Self::SuspiciousDependency { reason, .. } => reason.clone(),
            Self::GitStatus { status, .. } => format!("Git: {:?}", status),
            Self::Complexity { description, .. } => description.clone(),
            Self::InDependencyTree { depth, .. } => {
                format!("Dependency depth: {}", depth)
            }
            Self::Custom { name, .. } => name.clone(),
        }
    }
}

/// Types of key project files
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum KeyFileType {
    /// README, CHANGELOG, etc.
    Documentation,
    /// Cargo.toml, package.json, etc.
    BuildConfig,
    /// .env, config.toml, etc.
    Configuration,
    /// main.rs, index.js, etc.
    EntryPoint,
    /// LICENSE, COPYING
    License,
    /// .github/workflows, .gitlab-ci.yml
    CiConfig,
    /// Dockerfile, docker-compose.yml
    Container,
    /// CLAUDE.md, .cursorrules, etc.
    AiConfig,
}

/// Types of changes detected between scans
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ChangeType {
    /// File was added
    Added,
    /// File content was modified
    Modified,
    /// File was deleted
    Deleted,
    /// File permissions changed
    PermissionChanged,
    /// File was renamed/moved
    Renamed,
    /// File type changed (e.g., regular file to symlink)
    TypeChanged,
}

impl ChangeType {
    pub fn emoji(&self) -> &'static str {
        match self {
            Self::Added => "+",
            Self::Modified => "~",
            Self::Deleted => "-",
            Self::PermissionChanged => "🔐",
            Self::Renamed => "",
            Self::TypeChanged => "",
        }
    }
}

/// Git status types that affect interest
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum GitStatusType {
    /// Uncommitted changes
    Uncommitted,
    /// Merge conflict
    Conflict,
    /// Staged for commit
    Staged,
    /// Untracked file
    Untracked,
    /// Ahead of remote
    Ahead,
    /// Behind remote
    Behind,
}

/// Interest score with breakdown of contributing factors
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterestScore {
    /// Overall interest score (0.0 = boring, 1.0 = critical)
    pub score: f32,

    /// Factors that contributed to this score
    pub factors: Vec<InterestFactor>,

    /// Human-friendly interest level
    pub level: InterestLevel,

    /// When this score was calculated
    pub calculated_at: SystemTime,
}

impl InterestScore {
    /// Create a new interest score from factors
    pub fn from_factors(factors: Vec<InterestFactor>) -> Self {
        let score = factors.iter().map(|f| f.weight()).sum::<f32>().clamp(0.0, 1.0);
        let level = InterestLevel::from_score(score);

        Self {
            score,
            factors,
            level,
            calculated_at: SystemTime::now(),
        }
    }

    /// Create a default "boring" score
    pub fn boring() -> Self {
        Self {
            score: 0.0,
            factors: vec![],
            level: InterestLevel::Boring,
            calculated_at: SystemTime::now(),
        }
    }

    /// Create a critical score with a single reason
    pub fn critical(reason: String) -> Self {
        Self {
            score: 1.0,
            factors: vec![InterestFactor::SecurityPattern {
                risk: RiskLevel::Critical,
                description: reason,
                weight: 1.0,
            }],
            level: InterestLevel::Critical,
            calculated_at: SystemTime::now(),
        }
    }

    /// Check if this score indicates the node should be shown by default
    pub fn should_show(&self) -> bool {
        self.level >= InterestLevel::Notable
    }

    /// Get a summary of why this is interesting
    pub fn summary(&self) -> String {
        if self.factors.is_empty() {
            return String::from("No notable factors");
        }

        self.factors
            .iter()
            .map(|f| f.description())
            .collect::<Vec<_>>()
            .join(", ")
    }
}

impl Default for InterestScore {
    fn default() -> Self {
        Self {
            score: 0.1,
            factors: vec![],
            level: InterestLevel::Background,
            calculated_at: SystemTime::now(),
        }
    }
}

/// Default weights for interest calculation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterestWeights {
    /// Weight for recently modified files (within 24h)
    pub recent_modification: f32,

    /// Weight for critical security findings
    pub security_critical: f32,

    /// Weight for key project files
    pub key_file: f32,

    /// Weight for files changed since last scan
    pub changed_since_scan: f32,

    /// Weight for files in hot directories
    pub hot_directory: f32,

    /// Negative weight for dependency tree depth
    pub dependency_depth_penalty: f32,

    /// Base interest for files with git changes
    pub git_changes: f32,
}

impl Default for InterestWeights {
    fn default() -> Self {
        Self {
            recent_modification: 0.3,
            security_critical: 1.0,
            key_file: 0.5,
            changed_since_scan: 0.4,
            hot_directory: 0.3,
            dependency_depth_penalty: -0.1,
            git_changes: 0.35,
        }
    }
}

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

    #[test]
    fn test_interest_level_from_score() {
        assert_eq!(InterestLevel::from_score(0.0), InterestLevel::Boring);
        assert_eq!(InterestLevel::from_score(0.1), InterestLevel::Boring);
        assert_eq!(InterestLevel::from_score(0.3), InterestLevel::Background);
        assert_eq!(InterestLevel::from_score(0.5), InterestLevel::Notable);
        assert_eq!(InterestLevel::from_score(0.7), InterestLevel::Important);
        assert_eq!(InterestLevel::from_score(0.9), InterestLevel::Critical);
        assert_eq!(InterestLevel::from_score(1.0), InterestLevel::Critical);
    }

    #[test]
    fn test_interest_score_from_factors() {
        let factors = vec![
            InterestFactor::RecentlyModified {
                hours_ago: 2.0,
                weight: 0.3,
            },
            InterestFactor::KeyProjectFile {
                file_type: KeyFileType::BuildConfig,
                weight: 0.5,
            },
        ];

        let score = InterestScore::from_factors(factors);
        assert!((score.score - 0.8).abs() < 0.01);
        assert_eq!(score.level, InterestLevel::Critical);
    }

    #[test]
    fn test_interest_score_clamping() {
        let factors = vec![
            InterestFactor::SecurityPattern {
                risk: RiskLevel::Critical,
                description: "Bad thing".to_string(),
                weight: 1.0,
            },
            InterestFactor::HotDirectory {
                change_count: 100,
                weight: 0.5,
            },
        ];

        let score = InterestScore::from_factors(factors);
        // Should clamp to 1.0
        assert_eq!(score.score, 1.0);
    }

    #[test]
    fn test_dependency_manager_detection() {
        assert_eq!(
            DependencyManager::from_dir_name("node_modules"),
            Some(DependencyManager::Npm)
        );
        assert_eq!(
            DependencyManager::from_dir_name("target"),
            Some(DependencyManager::Cargo)
        );
        assert_eq!(
            DependencyManager::from_dir_name(".venv"),
            Some(DependencyManager::Python)
        );
        assert_eq!(DependencyManager::from_dir_name("src"), None);
    }
}