ruvector-memopt 0.5.0

Intelligent cross-platform memory optimizer with neural learning capabilities for smart optimization decisions
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
//! Electron app detection and memory management
//!
//! Electron apps are essentially embedded Chromium browsers and often
//! consume significant memory. This module detects common Electron apps:
//! - VS Code
//! - Discord
//! - Slack
//! - Microsoft Teams
//! - Notion
//! - Figma
//! - Spotify
//! - Obsidian
//! - 1Password
//! - Postman
//! - And many more...

use super::{AppCategory, AppInfo, OptimizationAction, OptimizationResult};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use sysinfo::{System, ProcessesToUpdate, Pid};

/// Known Electron app patterns
#[derive(Debug, Clone)]
pub struct ElectronAppPattern {
    pub name: &'static str,
    pub display_name: &'static str,
    pub patterns: &'static [&'static str],
    pub category: AppCategory,
    /// Expected baseline memory (MB) - for leak detection
    pub baseline_memory_mb: f64,
}

/// Known Electron applications
pub const ELECTRON_APPS: &[ElectronAppPattern] = &[
    ElectronAppPattern {
        name: "vscode",
        display_name: "Visual Studio Code",
        patterns: &["code", "code.exe", "code helper", "code - insiders"],
        category: AppCategory::Development,
        baseline_memory_mb: 300.0,
    },
    ElectronAppPattern {
        name: "discord",
        display_name: "Discord",
        patterns: &["discord", "discord.exe", "discord helper"],
        category: AppCategory::Communication,
        baseline_memory_mb: 200.0,
    },
    ElectronAppPattern {
        name: "slack",
        display_name: "Slack",
        patterns: &["slack", "slack.exe", "slack helper"],
        category: AppCategory::Communication,
        baseline_memory_mb: 250.0,
    },
    ElectronAppPattern {
        name: "teams",
        display_name: "Microsoft Teams",
        patterns: &["teams", "ms-teams", "microsoft teams", "teams.exe"],
        category: AppCategory::Communication,
        baseline_memory_mb: 300.0,
    },
    ElectronAppPattern {
        name: "notion",
        display_name: "Notion",
        patterns: &["notion", "notion.exe", "notion helper"],
        category: AppCategory::Development,
        baseline_memory_mb: 200.0,
    },
    ElectronAppPattern {
        name: "figma",
        display_name: "Figma",
        patterns: &["figma", "figma.exe", "figma helper", "figma agent"],
        category: AppCategory::Creative,
        baseline_memory_mb: 400.0,
    },
    ElectronAppPattern {
        name: "spotify",
        display_name: "Spotify",
        patterns: &["spotify", "spotify.exe", "spotify helper"],
        category: AppCategory::Media,
        baseline_memory_mb: 150.0,
    },
    ElectronAppPattern {
        name: "obsidian",
        display_name: "Obsidian",
        patterns: &["obsidian", "obsidian.exe", "obsidian helper"],
        category: AppCategory::Development,
        baseline_memory_mb: 150.0,
    },
    ElectronAppPattern {
        name: "1password",
        display_name: "1Password",
        patterns: &["1password", "1password.exe", "1password helper"],
        category: AppCategory::System,
        baseline_memory_mb: 100.0,
    },
    ElectronAppPattern {
        name: "postman",
        display_name: "Postman",
        patterns: &["postman", "postman.exe", "postman helper"],
        category: AppCategory::Development,
        baseline_memory_mb: 300.0,
    },
    ElectronAppPattern {
        name: "whatsapp",
        display_name: "WhatsApp",
        patterns: &["whatsapp", "whatsapp.exe", "whatsapp helper"],
        category: AppCategory::Communication,
        baseline_memory_mb: 150.0,
    },
    ElectronAppPattern {
        name: "signal",
        display_name: "Signal",
        patterns: &["signal", "signal.exe", "signal helper"],
        category: AppCategory::Communication,
        baseline_memory_mb: 150.0,
    },
    ElectronAppPattern {
        name: "telegram",
        display_name: "Telegram Desktop",
        patterns: &["telegram", "telegram.exe", "telegram desktop"],
        category: AppCategory::Communication,
        baseline_memory_mb: 150.0,
    },
    ElectronAppPattern {
        name: "cursor",
        display_name: "Cursor",
        patterns: &["cursor", "cursor.exe", "cursor helper"],
        category: AppCategory::Development,
        baseline_memory_mb: 300.0,
    },
    ElectronAppPattern {
        name: "windsurf",
        display_name: "Windsurf",
        patterns: &["windsurf", "windsurf.exe", "windsurf helper"],
        category: AppCategory::Development,
        baseline_memory_mb: 300.0,
    },
    ElectronAppPattern {
        name: "zed",
        display_name: "Zed",
        patterns: &["zed", "zed.exe", "zed helper"],
        category: AppCategory::Development,
        baseline_memory_mb: 200.0,
    },
    ElectronAppPattern {
        name: "linear",
        display_name: "Linear",
        patterns: &["linear", "linear.exe", "linear helper"],
        category: AppCategory::Development,
        baseline_memory_mb: 200.0,
    },
    ElectronAppPattern {
        name: "hyper",
        display_name: "Hyper Terminal",
        patterns: &["hyper", "hyper.exe", "hyper helper"],
        category: AppCategory::Development,
        baseline_memory_mb: 150.0,
    },
    ElectronAppPattern {
        name: "atom",
        display_name: "Atom",
        patterns: &["atom", "atom.exe", "atom helper"],
        category: AppCategory::Development,
        baseline_memory_mb: 250.0,
    },
    ElectronAppPattern {
        name: "bitwarden",
        display_name: "Bitwarden",
        patterns: &["bitwarden", "bitwarden.exe", "bitwarden helper"],
        category: AppCategory::System,
        baseline_memory_mb: 100.0,
    },
    ElectronAppPattern {
        name: "mongodb-compass",
        display_name: "MongoDB Compass",
        patterns: &["mongodb compass", "mongodb-compass", "compass"],
        category: AppCategory::Development,
        baseline_memory_mb: 300.0,
    },
    ElectronAppPattern {
        name: "insomnia",
        display_name: "Insomnia",
        patterns: &["insomnia", "insomnia.exe"],
        category: AppCategory::Development,
        baseline_memory_mb: 200.0,
    },
    ElectronAppPattern {
        name: "loom",
        display_name: "Loom",
        patterns: &["loom", "loom.exe", "loom helper"],
        category: AppCategory::Media,
        baseline_memory_mb: 200.0,
    },
    ElectronAppPattern {
        name: "gitkraken",
        display_name: "GitKraken",
        patterns: &["gitkraken", "gitkraken.exe"],
        category: AppCategory::Development,
        baseline_memory_mb: 250.0,
    },
];

/// Electron app instance info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ElectronAppInfo {
    pub name: String,
    pub display_name: String,
    pub category: AppCategory,
    pub total_memory_mb: f64,
    pub total_cpu_percent: f32,
    pub process_count: usize,
    pub main_pid: Option<u32>,
    pub pids: Vec<u32>,
    pub baseline_memory_mb: f64,
    pub memory_overhead_percent: f64,
    pub is_running: bool,
}

impl ElectronAppInfo {
    /// Check if app is using more memory than expected
    pub fn is_bloated(&self) -> bool {
        self.total_memory_mb > self.baseline_memory_mb * 2.0
    }

    /// Check if likely has a memory leak
    pub fn likely_memory_leak(&self) -> bool {
        self.memory_overhead_percent > 200.0
    }

    /// Get suggested action
    pub fn get_suggested_action(&self) -> OptimizationAction {
        if self.total_memory_mb > 1500.0 {
            OptimizationAction::Restart
        } else if self.is_bloated() {
            OptimizationAction::TrimMemory
        } else {
            OptimizationAction::None
        }
    }
}

/// Electron app manager
pub struct ElectronManager {
    system: System,
    apps: HashMap<String, ElectronAppInfo>,
    last_update: std::time::Instant,
}

impl ElectronManager {
    pub fn new() -> Self {
        let mut system = System::new_all();
        system.refresh_processes(ProcessesToUpdate::All, true);

        Self {
            system,
            apps: HashMap::new(),
            last_update: std::time::Instant::now(),
        }
    }

    /// Refresh process data
    pub fn refresh(&mut self) {
        self.system.refresh_processes(ProcessesToUpdate::All, true);
        self.detect_electron_apps();
        self.last_update = std::time::Instant::now();
    }

    /// Detect all running Electron apps
    fn detect_electron_apps(&mut self) {
        self.apps.clear();

        for pattern in ELECTRON_APPS {
            let mut app_info = ElectronAppInfo {
                name: pattern.name.to_string(),
                display_name: pattern.display_name.to_string(),
                category: pattern.category,
                total_memory_mb: 0.0,
                total_cpu_percent: 0.0,
                process_count: 0,
                main_pid: None,
                pids: Vec::new(),
                baseline_memory_mb: pattern.baseline_memory_mb,
                memory_overhead_percent: 0.0,
                is_running: false,
            };

            for (pid, process) in self.system.processes() {
                let name = process.name().to_string_lossy().to_lowercase();

                // Check if process matches any pattern
                let matches = pattern.patterns.iter().any(|p| name.contains(p));

                if matches {
                    let memory_mb = process.memory() as f64 / (1024.0 * 1024.0);
                    let cpu_percent = process.cpu_usage();

                    app_info.total_memory_mb += memory_mb;
                    app_info.total_cpu_percent += cpu_percent;
                    app_info.process_count += 1;
                    app_info.pids.push(pid.as_u32());
                    app_info.is_running = true;

                    // First matching process is usually the main process
                    if app_info.main_pid.is_none() && !name.contains("helper") {
                        app_info.main_pid = Some(pid.as_u32());
                    }
                }
            }

            if app_info.is_running {
                // Calculate memory overhead
                app_info.memory_overhead_percent =
                    (app_info.total_memory_mb / app_info.baseline_memory_mb) * 100.0;

                self.apps.insert(pattern.name.to_string(), app_info);
            }
        }
    }

    /// Get all detected Electron apps
    pub fn get_apps(&self) -> Vec<&ElectronAppInfo> {
        self.apps.values().collect()
    }

    /// Get app by name
    pub fn get_app(&self, name: &str) -> Option<&ElectronAppInfo> {
        self.apps.get(name)
    }

    /// Get total Electron app memory
    pub fn total_memory_mb(&self) -> f64 {
        self.apps.values().map(|a| a.total_memory_mb).sum()
    }

    /// Get apps that are bloated
    pub fn get_bloated_apps(&self) -> Vec<&ElectronAppInfo> {
        self.apps.values().filter(|a| a.is_bloated()).collect()
    }

    /// Get optimization suggestions
    pub fn get_suggestions(&self) -> Vec<(String, OptimizationAction, String)> {
        let mut suggestions = Vec::new();

        for app in self.apps.values() {
            let action = app.get_suggested_action();
            if action != OptimizationAction::None {
                let reason = match &action {
                    OptimizationAction::Restart => {
                        format!(
                            "{} is using {:.0} MB ({:.0}% of baseline) - restart recommended",
                            app.display_name,
                            app.total_memory_mb,
                            app.memory_overhead_percent
                        )
                    }
                    OptimizationAction::TrimMemory => {
                        format!(
                            "{} is using {:.0} MB ({:.0}% of expected {:.0} MB)",
                            app.display_name,
                            app.total_memory_mb,
                            app.memory_overhead_percent,
                            app.baseline_memory_mb
                        )
                    }
                    _ => continue,
                };

                suggestions.push((app.display_name.clone(), action, reason));
            }
        }

        suggestions.sort_by(|a, b| {
            let mem_a = self.apps.values().find(|app| app.display_name == a.0)
                .map(|a| a.total_memory_mb).unwrap_or(0.0);
            let mem_b = self.apps.values().find(|app| app.display_name == b.0)
                .map(|a| a.total_memory_mb).unwrap_or(0.0);
            mem_b.partial_cmp(&mem_a).unwrap()
        });

        suggestions
    }

    /// Print Electron apps summary
    pub fn print_summary(&self) {
        println!("\n⚡ Electron Apps Memory Usage\n");
        println!("┌──────────────────────┬───────────┬──────────┬───────────┬──────────┐");
        println!("│ Application          │ Memory    │ Baseline │ Overhead  │ Status   │");
        println!("├──────────────────────┼───────────┼──────────┼───────────┼──────────┤");

        let mut apps: Vec<_> = self.apps.values().collect();
        apps.sort_by(|a, b| b.total_memory_mb.partial_cmp(&a.total_memory_mb).unwrap());

        for app in &apps {
            let status = if app.total_memory_mb > 1000.0 {
                "🔴 Heavy"
            } else if app.is_bloated() {
                "🟡 Bloated"
            } else {
                "🟢 Normal"
            };

            println!(
                "│ {:20} │ {:>7.0} MB │ {:>6.0} MB │ {:>8.0}% │ {:8}",
                truncate(&app.display_name, 20),
                app.total_memory_mb,
                app.baseline_memory_mb,
                app.memory_overhead_percent,
                status
            );
        }

        println!("└──────────────────────┴───────────┴──────────┴───────────┴──────────┘");

        let total: f64 = apps.iter().map(|a| a.total_memory_mb).sum();
        println!(
            "\nTotal: {:.0} MB across {} Electron apps ({} processes)",
            total,
            apps.len(),
            apps.iter().map(|a| a.process_count).sum::<usize>()
        );

        let bloated = self.get_bloated_apps();
        if !bloated.is_empty() {
            println!("\n💡 Suggestions:");
            for app in bloated.iter().take(3) {
                println!(
                    "{} is using {:.0}% more memory than expected - consider restarting",
                    app.display_name,
                    app.memory_overhead_percent - 100.0
                );
            }
        }
    }
}

impl Default for ElectronManager {
    fn default() -> Self {
        Self::new()
    }
}

fn truncate(s: &str, max: usize) -> String {
    if s.len() <= max {
        format!("{:width$}", s, width = max)
    } else {
        format!("{}...", &s[..max - 3])
    }
}