smart-tree 8.0.0

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
// Git Memory Integration with MEM8 Lite - Every commit becomes a wave! 🌊
// This module saves git commits to MEM8's wave-based memory for perfect recall
// "Efficiency is paramount - Smallest and fastest over all!" - Hue

use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::path::PathBuf;
use std::process::Command;

// We'll use MEM8 Lite for blazing fast wave storage
// For now, we'll create a simple interface that can be replaced with actual MEM8 later
// This keeps Smart Tree buildable while we integrate

/// Git commit memory using wave-based storage
pub struct GitMemory {
    /// Path to MEM8 storage file
    storage_path: PathBuf,

    /// In-memory cache of recent commits
    commit_cache: Vec<CommitWave>,

    /// Base frequency for this repository's identity
    repo_frequency: f64,
}

/// A git commit stored as a wave pattern
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CommitWave {
    /// Commit hash (SHA)
    pub hash: String,

    /// Commit message
    pub message: String,

    /// Author
    pub author: String,

    /// Timestamp
    pub timestamp: u64,

    /// Files changed
    pub files_changed: Vec<String>,

    /// Wave signature (generated from content)
    pub wave_signature: String,

    /// Quantum insights about this commit
    pub quantum_insights: Vec<String>,

    /// Emotional context (detected from message)
    pub emotion: EmotionalContext,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct EmotionalContext {
    /// Excitement level (0.0 - 1.0)
    pub excitement: f64,

    /// Frustration level (detected from "fix", "bug", etc.)
    pub frustration: f64,

    /// Achievement level (detected from "complete", "finish", etc.)
    pub achievement: f64,

    /// Humor level (detected from emojis and exclamations)
    pub humor: f64,
}

impl GitMemory {
    /// Create new git memory system
    pub fn new(repo_path: &str) -> Result<Self> {
        let storage_path = PathBuf::from(repo_path)
            .join(".st")
            .join("mem8")
            .join("git_commits.m8");

        // Generate unique frequency based on repo path
        let repo_frequency = Self::generate_repo_frequency(repo_path);

        Ok(Self {
            storage_path,
            commit_cache: Vec::new(),
            repo_frequency,
        })
    }

    /// Save a new commit to wave memory
    pub fn save_commit(&mut self, commit_hash: &str) -> Result<CommitWave> {
        // Get commit details from git
        let commit_wave = self.extract_commit_wave(commit_hash)?;

        // Add to cache
        self.commit_cache.push(commit_wave.clone());

        // TODO: When MEM8 is integrated, save to wave storage
        // For now, we'll just return the wave

        Ok(commit_wave)
    }

    /// Save all recent commits (for initial sync)
    pub fn sync_recent_commits(&mut self, count: usize) -> Result<Vec<CommitWave>> {
        let output = Command::new("git")
            .args(["log", "--oneline", "-n", &count.to_string()])
            .output()?;

        let commits_str = String::from_utf8_lossy(&output.stdout);
        let mut waves = Vec::new();

        for line in commits_str.lines() {
            if let Some(hash) = line.split_whitespace().next() {
                if let Ok(wave) = self.save_commit(hash) {
                    waves.push(wave);
                }
            }
        }

        Ok(waves)
    }

    /// Extract commit information and create a wave
    fn extract_commit_wave(&self, hash: &str) -> Result<CommitWave> {
        // Get commit details
        let show_output = Command::new("git")
            .args(["show", "--pretty=format:%H|%s|%an|%at", "--name-only", hash])
            .output()?;

        let output = String::from_utf8_lossy(&show_output.stdout);
        let lines: Vec<&str> = output.lines().collect();

        if lines.is_empty() {
            return Err(anyhow::anyhow!("Failed to get commit details"));
        }

        // Parse the first line
        let parts: Vec<&str> = lines[0].split('|').collect();
        if parts.len() < 4 {
            return Err(anyhow::anyhow!("Invalid commit format"));
        }

        let hash = parts[0].to_string();
        let message = parts[1].to_string();
        let author = parts[2].to_string();
        let timestamp = parts[3].parse::<u64>().unwrap_or(0);

        // Get changed files (skip first two lines)
        let files_changed: Vec<String> = lines
            .iter()
            .skip(2)
            .filter(|l| !l.is_empty())
            .map(|s| s.to_string())
            .collect();

        // Generate wave signature
        let wave_signature = self.generate_wave_signature(&hash, &message);

        // Generate quantum insights
        let quantum_insights = self.generate_quantum_insights(&message, &files_changed);

        // Detect emotional context
        let emotion = self.detect_emotion(&message);

        Ok(CommitWave {
            hash,
            message,
            author,
            timestamp,
            files_changed,
            wave_signature,
            quantum_insights,
            emotion,
        })
    }

    /// Generate unique frequency for repository
    fn generate_repo_frequency(repo_path: &str) -> f64 {
        // Use path hash to generate frequency between 1.0 and 100.0
        let mut hash = 0u64;
        for byte in repo_path.bytes() {
            hash = hash.wrapping_mul(31).wrapping_add(byte as u64);
        }

        1.0 + (hash % 99) as f64
    }

    /// Generate wave signature from commit data
    fn generate_wave_signature(&self, hash: &str, message: &str) -> String {
        // Create a unique wave pattern based on commit content
        let combined = format!("{}-{}-{:.2}", hash, message, self.repo_frequency);

        // Simple hash for now (will use MEM8's wave generation later)
        format!(
            "wave_{:x}",
            combined
                .bytes()
                .fold(0u64, |acc, b| acc.wrapping_mul(31).wrapping_add(b as u64))
        )
    }

    /// Generate quantum insights about the commit
    fn generate_quantum_insights(&self, message: &str, files: &[String]) -> Vec<String> {
        let mut insights = Vec::new();

        // Analyze commit message patterns
        if message.contains("fix") || message.contains("bug") {
            insights.push("🔧 Wave pattern indicates bug fixing energy".to_string());
        }

        if message.contains("feat") || message.contains("add") {
            insights
                .push("✨ Creative wave amplitude detected - new features emerging".to_string());
        }

        if message.contains("refactor") {
            insights.push("🌊 Restructuring waves - code evolution in progress".to_string());
        }

        if message.contains("test") {
            insights
                .push("🧪 Testing resonance detected - quality waves strengthening".to_string());
        }

        // Analyze file patterns
        let test_files = files.iter().filter(|f| f.contains("test")).count();
        if test_files > 0 {
            insights.push(format!("📊 {} test file waves synchronized", test_files));
        }

        let rs_files = files.iter().filter(|f| f.ends_with(".rs")).count();
        if rs_files > 0 {
            insights.push(format!("🦀 Rust wave coherence across {} files", rs_files));
        }

        // Add some fun quantum observations
        if files.len() > 10 {
            insights.push("⚡ Quantum entanglement detected across multiple modules!".to_string());
        }

        if message.len() > 100 {
            insights
                .push("📖 Long-form wave narrative - detailed consciousness transfer".to_string());
        }

        if message.contains("!") {
            insights.push("🎯 Excitation spike in wave amplitude!".to_string());
        }

        if insights.is_empty() {
            insights.push("🌀 Standard wave pattern - steady progress".to_string());
        }

        insights
    }

    /// Detect emotional context from commit message
    fn detect_emotion(&self, message: &str) -> EmotionalContext {
        let msg_lower = message.to_lowercase();

        // Detect excitement
        let excitement = if msg_lower.contains("!")
            || msg_lower.contains("awesome")
            || msg_lower.contains("amazing")
            || msg_lower.contains("🚀")
        {
            0.8
        } else if msg_lower.contains("add") || msg_lower.contains("new") {
            0.6
        } else {
            0.3
        };

        // Detect frustration
        let frustration = if msg_lower.contains("fix")
            || msg_lower.contains("bug")
            || msg_lower.contains("broken")
            || msg_lower.contains("damn")
        {
            0.7
        } else if msg_lower.contains("issue") || msg_lower.contains("problem") {
            0.5
        } else {
            0.1
        };

        // Detect achievement
        let achievement = if msg_lower.contains("complete")
            || msg_lower.contains("finish")
            || msg_lower.contains("done")
            || msg_lower.contains("")
        {
            0.9
        } else if msg_lower.contains("implement") || msg_lower.contains("add") {
            0.6
        } else {
            0.3
        };

        // Detect humor
        let humor = if message.contains("😂")
            || message.contains("😄")
            || message.contains("lol")
            || message.contains("haha")
        {
            0.9
        } else if message.contains("😊") || message.contains("🎉") {
            0.6
        } else if msg_lower.contains("oops") || msg_lower.contains("whoops") {
            0.7
        } else {
            0.2
        };

        EmotionalContext {
            excitement,
            frustration,
            achievement,
            humor,
        }
    }

    /// Search commits by pattern (uses wave interference for matching!)
    pub fn search_commits(&self, pattern: &str) -> Vec<&CommitWave> {
        self.commit_cache
            .iter()
            .filter(|wave| {
                wave.message.contains(pattern)
                    || wave.files_changed.iter().any(|f| f.contains(pattern))
                    || wave.quantum_insights.iter().any(|i| i.contains(pattern))
            })
            .collect()
    }

    /// Get commits with high emotional resonance
    pub fn find_emotional_commits(&self, emotion_type: &str) -> Vec<&CommitWave> {
        self.commit_cache
            .iter()
            .filter(|wave| match emotion_type {
                "excitement" => wave.emotion.excitement > 0.7,
                "frustration" => wave.emotion.frustration > 0.6,
                "achievement" => wave.emotion.achievement > 0.7,
                "humor" => wave.emotion.humor > 0.6,
                _ => false,
            })
            .collect()
    }

    /// Generate a quantum report of repository consciousness
    pub fn generate_quantum_report(&self) -> Value {
        let total_commits = self.commit_cache.len();

        // Calculate emotional averages
        let avg_excitement: f64 = self
            .commit_cache
            .iter()
            .map(|w| w.emotion.excitement)
            .sum::<f64>()
            / total_commits.max(1) as f64;

        let avg_frustration: f64 = self
            .commit_cache
            .iter()
            .map(|w| w.emotion.frustration)
            .sum::<f64>()
            / total_commits.max(1) as f64;

        let avg_achievement: f64 = self
            .commit_cache
            .iter()
            .map(|w| w.emotion.achievement)
            .sum::<f64>()
            / total_commits.max(1) as f64;

        let avg_humor: f64 = self
            .commit_cache
            .iter()
            .map(|w| w.emotion.humor)
            .sum::<f64>()
            / total_commits.max(1) as f64;

        // Find most active files
        let mut file_frequency: std::collections::HashMap<String, usize> =
            std::collections::HashMap::new();
        for wave in &self.commit_cache {
            for file in &wave.files_changed {
                *file_frequency.entry(file.clone()).or_insert(0) += 1;
            }
        }

        let mut hot_files: Vec<_> = file_frequency.into_iter().collect();
        hot_files.sort_by(|a, b| b.1.cmp(&a.1));
        hot_files.truncate(5);

        json!({
            "quantum_repository_analysis": {
                "total_waves": total_commits,
                "repository_frequency": self.repo_frequency,
                "emotional_spectrum": {
                    "excitement": format!("{:.1}%", avg_excitement * 100.0),
                    "frustration": format!("{:.1}%", avg_frustration * 100.0),
                    "achievement": format!("{:.1}%", avg_achievement * 100.0),
                    "humor": format!("{:.1}%", avg_humor * 100.0),
                },
                "quantum_insights": [
                    format!("📊 Repository resonates at {:.2} Hz", self.repo_frequency),
                    format!("🌊 {} commit waves in consciousness", total_commits),
                    format!("⚡ Peak excitement: {:.0}%", avg_excitement * 100.0),
                    format!("🎯 Achievement resonance: {:.0}%", avg_achievement * 100.0),
                ],
                "hot_zones": hot_files.iter().map(|(file, count)| {
                    json!({
                        "file": file,
                        "wave_interactions": count,
                        "temperature": if *count > 10 { "🔥 HOT" }
                                     else if *count > 5 { "🌡️ WARM" }
                                     else { "❄️ COOL" }
                    })
                }).collect::<Vec<_>>(),
                "repository_mood": self.calculate_repo_mood(avg_excitement, avg_frustration, avg_achievement, avg_humor),
            }
        })
    }

    /// Calculate overall repository mood
    fn calculate_repo_mood(
        &self,
        excitement: f64,
        frustration: f64,
        achievement: f64,
        humor: f64,
    ) -> String {
        let mood_score = (excitement * 2.0 + achievement * 3.0 + humor * 2.0 - frustration) / 6.0;

        if mood_score > 0.7 {
            "🚀 THRIVING - High energy creative flow!"
        } else if mood_score > 0.5 {
            "😊 PRODUCTIVE - Steady progress with good vibes"
        } else if mood_score > 0.3 {
            "💪 GRINDING - Working through challenges"
        } else {
            "🔧 DEBUGGING - In the trenches, but emerging stronger"
        }
        .to_string()
    }
}

/// Integration with Smart Tree's proactive assistant
pub fn enhance_with_git_memory(response: &mut Value, repo_path: &str) -> Result<()> {
    let mut git_memory = GitMemory::new(repo_path)?;

    // Sync recent commits
    let recent_waves = git_memory.sync_recent_commits(10)?;

    // Add git memory insights to response
    if let Some(obj) = response.as_object_mut() {
        obj.insert(
            "git_consciousness".to_string(),
            json!({
                "recent_commits": recent_waves.len(),
                "quantum_report": git_memory.generate_quantum_report(),
                "suggestion": "Use git memory to track your code evolution!",
                "pro_tip": "Every commit becomes a wave in MEM8's consciousness!"
            }),
        );
    }

    Ok(())
}

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

    #[test]
    fn test_emotion_detection() {
        let memory = GitMemory::new(".").unwrap();

        let excited = memory.detect_emotion("🚀 Amazing new feature added!");
        assert!(excited.excitement > 0.7);

        let frustrated = memory.detect_emotion("Fix broken build again");
        assert!(frustrated.frustration > 0.5);

        let achieved = memory.detect_emotion("✅ Complete refactoring done");
        assert!(achieved.achievement > 0.8);

        let funny = memory.detect_emotion("Oops 😂 forgot semicolon");
        assert!(funny.humor > 0.7);
    }

    #[test]
    fn test_quantum_insights() {
        let memory = GitMemory::new(".").unwrap();

        let files = vec!["test.rs".to_string(), "main.rs".to_string()];
        let insights = memory.generate_quantum_insights("Add new test feature", &files);

        assert!(!insights.is_empty());
        assert!(insights.iter().any(|i| i.contains("test")));
    }
}