warren-cli 0.1.6

Install any CLI tool unlimited times. Every instance is its own world.
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
618
619
620
621
622
623
624
use std::collections::BTreeSet;
use std::os::unix::fs::MetadataExt;
use std::path::Path;

use serde::{Deserialize, Serialize};

/// One restorable application captured in a session snapshot.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct SessionApp {
    /// Short binary name, e.g. `google-chrome`, `gnome-terminal`, `code`.
    pub name: String,
    pub kind: AppKind,
    /// Binary to relaunch (resolved to a bare name on PATH where possible).
    pub binary: String,
    /// Working directory the process was sitting in, if known.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cwd: Option<String>,
    /// Workspace folders/files for editors (only paths that existed at save time).
    #[serde(default)]
    pub workspaces: Vec<String>,
    /// Full argv at capture time (informational; restore builds a clean command).
    #[serde(default)]
    pub argv: Vec<String>,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum AppKind {
    Browser,
    Terminal,
    Editor,
    Files,
    Other,
}

impl std::fmt::Display for AppKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AppKind::Browser => write!(f, "browser"),
            AppKind::Terminal => write!(f, "terminal"),
            AppKind::Editor => write!(f, "editor"),
            AppKind::Files => write!(f, "files"),
            AppKind::Other => write!(f, "other"),
        }
    }
}

impl std::str::FromStr for AppKind {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "browser" => Ok(AppKind::Browser),
            "terminal" => Ok(AppKind::Terminal),
            "editor" => Ok(AppKind::Editor),
            "files" => Ok(AppKind::Files),
            "other" => Ok(AppKind::Other),
            _ => Err(format!("unknown app kind '{}'", s)),
        }
    }
}

/// Scan `/proc` for user-owned, restorable GUI/terminal applications.
///
/// Best-effort and rootless: anything unreadable is skipped. Multi-process
/// apps (browsers, VS Code helpers) are deduplicated so restore launches
/// each app once instead of replaying every `--type=renderer` child.
pub fn scan_running_apps() -> Vec<SessionApp> {
    let self_uid = proc_dir_uid(&std::process::id().to_string());
    let self_pid = std::process::id();
    let own_exe = std::env::current_exe().ok();

    let mut seen: BTreeSet<(String, String, String)> = BTreeSet::new();
    let mut apps: Vec<SessionApp> = Vec::new();

    let entries = std::fs::read_dir("/proc").map(|rd| {
        rd.filter_map(|e| e.ok())
            .filter(|e| {
                e.file_name()
                    .to_string_lossy()
                    .chars()
                    .all(|c| c.is_ascii_digit())
            })
            .collect::<Vec<_>>()
    });

    let entries = match entries {
        Ok(e) => e,
        Err(_) => return fallback_shell_app(),
    };

    for entry in entries {
        let pid: u32 = match entry.file_name().to_string_lossy().parse() {
            Ok(p) => p,
            Err(_) => continue,
        };
        if pid == self_pid || pid <= 1 {
            continue;
        }
        // Only capture our own user's processes.
        if let (Some(want), Some(got)) = (self_uid, proc_dir_uid(&pid.to_string()))
            && want != got
        {
            continue;
        }
        let Some(raw) = read_proc(pid) else { continue };
        if raw.argv.is_empty() {
            continue; // kernel thread
        }
        // Skip multiprocess children: they cannot be relaunched directly
        // and would spam restore with dozens of entries. The `contains`
        // arm catches collapsed Chromium-style cmdlines that were not
        // NUL-separated (`/usr/bin/chromium --type=renderer ...`).
        if raw.argv.iter().any(|a| {
            a.starts_with("--type=")
                || a.contains(" --type=")
                || a == "--renderer"
                || a.starts_with("--extension-")
        }) {
            continue;
        }
        let binary = binary_name(&raw.argv[0]);
        if binary.is_empty() {
            continue;
        }
        if is_helper_binary(&binary) {
            continue;
        }
        // Never capture the `warren session save` invocation itself.
        if binary == "warren"
            && raw.argv.iter().any(|a| a == "session")
            && raw.argv.iter().any(|a| a == "save")
        {
            continue;
        }
        // Skip our own executable when invoked under a different name
        // (e.g. tests) — compare canonical exe paths.
        if let (Some(own), Some(exe)) = (own_exe.as_ref(), raw.exe.as_ref())
            && same_file(own, exe)
        {
            continue;
        }

        let kind = classify(&binary);
        // Non-interactive helpers and one-shot commands are noise.
        if kind == AppKind::Other && is_transient(&binary) {
            continue;
        }
        // For browsers keep a single entry per binary: tabs are restored
        // by the browser itself ("continue where you left off").
        let cwd = raw.cwd.filter(|c| !c.is_empty());
        let workspaces = if matches!(kind, AppKind::Editor) {
            extract_workspaces(&binary, &raw.argv)
        } else {
            Vec::new()
        };
        let dedup_key = match kind {
            AppKind::Browser => (binary.clone(), String::new(), String::new()),
            _ => (
                binary.clone(),
                cwd.clone().unwrap_or_default(),
                workspaces.join("\n"),
            ),
        };
        if !seen.insert(dedup_key) {
            continue;
        }
        apps.push(SessionApp {
            name: binary.clone(),
            kind,
            binary,
            cwd,
            workspaces,
            argv: raw.argv,
        });
        // Hard cap: snapshots must stay tiny so retention stays cheap.
        if apps.len() >= 100 {
            break;
        }
    }

    apps.sort_by(|a, b| {
        (
            kind_rank(a.kind),
            &a.name,
            a.cwd.as_deref().unwrap_or_default(),
        )
            .cmp(&(
                kind_rank(b.kind),
                &b.name,
                b.cwd.as_deref().unwrap_or_default(),
            ))
    });

    if apps.is_empty() {
        return fallback_shell_app();
    }
    apps
}

fn fallback_shell_app() -> Vec<SessionApp> {
    // Always record at least the current shell so restore can reopen a
    // terminal in the right place even on headless/CI machines.
    let shell = std::env::var("SHELL")
        .ok()
        .and_then(|s| {
            Path::new(&s)
                .file_name()
                .and_then(|n| n.to_str())
                .map(|n| n.to_string())
        })
        .unwrap_or_else(|| "sh".to_string());
    let cwd = std::env::current_dir()
        .ok()
        .map(|p| p.to_string_lossy().to_string());
    vec![SessionApp {
        name: shell.clone(),
        kind: AppKind::Terminal,
        binary: shell,
        cwd,
        workspaces: Vec::new(),
        argv: Vec::new(),
    }]
}

struct RawProc {
    argv: Vec<String>,
    cwd: Option<String>,
    exe: Option<std::path::PathBuf>,
}

fn read_proc(pid: u32) -> Option<RawProc> {
    let base = format!("/proc/{}", pid);
    let cmdline = std::fs::read(format!("{}/cmdline", base)).ok()?;
    let mut argv: Vec<String> = cmdline
        .split(|b| *b == 0)
        .filter(|s| !s.is_empty())
        .map(|s| String::from_utf8_lossy(s).to_string())
        .collect();
    if argv.is_empty() {
        return None;
    }
    // Chromium (and some Electron apps) rewrite their own argv into a
    // single space-separated string instead of NUL-separated entries,
    // e.g. `/usr/lib/chromium/chromium --type=renderer ...\0`.
    // Split those collapsed segments so child-process filtering and
    // binary extraction below see real tokens.
    argv = split_collapsed_argv(argv);
    if argv.is_empty() {
        return None;
    }
    // argv[0] can be empty for some daemons; fall back to comm or exe.
    if argv[0].is_empty()
        && let Ok(comm) = std::fs::read_to_string(format!("{}/comm", base))
    {
        let comm = comm.trim().to_string();
        if !comm.is_empty() {
            argv[0] = comm;
        }
    }
    let cwd = std::fs::read_link(format!("{}/cwd", base))
        .ok()
        .map(|p| p.to_string_lossy().to_string());
    let exe = std::fs::read_link(format!("{}/exe", base)).ok();
    Some(RawProc { argv, cwd, exe })
}

fn proc_dir_uid(pid: &str) -> Option<u32> {
    std::fs::metadata(format!("/proc/{}", pid))
        .ok()
        .map(|m| m.uid())
}

fn same_file(a: &Path, b: &Path) -> bool {
    if a == b {
        return true;
    }
    match (std::fs::metadata(a), std::fs::metadata(b)) {
        (Ok(ma), Ok(mb)) => {
            use std::os::unix::fs::MetadataExt;
            ma.dev() == mb.dev() && ma.ino() == mb.ino()
        }
        _ => false,
    }
}

fn binary_name(argv0: &str) -> String {
    // Defense in depth: if a collapsed `argv0` like
    // `/usr/lib/chromium/chromium --type=renderer ...` slips through,
    // only the executable part is the binary name.
    let first_token = argv0.split_whitespace().next().unwrap_or(argv0);
    let base = first_token.rsplit('/').next().unwrap_or(first_token);
    // Some sandboxed processes wrap the name in parens: `(chrome)`.
    let trimmed = base.trim_matches(|c| c == '(' || c == ')').trim();
    // Flatpak child wrappers look like `bwrap` — not restorable directly.
    trimmed.to_string()
}

/// Split collapsed single-string cmdlines (`a --type=b ...`) into tokens.
///
/// Only segments containing `--type=` are split, so editor workspace paths
/// containing spaces are never broken apart.
fn split_collapsed_argv(argv: Vec<String>) -> Vec<String> {
    let mut out = Vec::with_capacity(argv.len());
    for arg in argv {
        if arg.contains("--type=") && arg.contains(' ') {
            out.extend(arg.split_whitespace().map(|t| t.to_string()));
        } else {
            out.push(arg);
        }
    }
    out
}

fn kind_rank(kind: AppKind) -> u8 {
    match kind {
        AppKind::Browser => 0,
        AppKind::Terminal => 1,
        AppKind::Editor => 2,
        AppKind::Files => 3,
        AppKind::Other => 4,
    }
}

pub fn classify(binary: &str) -> AppKind {
    let b = binary.to_lowercase();
    if matches!(
        b.as_str(),
        "google-chrome"
            | "google-chrome-stable"
            | "chrome"
            | "chromium"
            | "chromium-browser"
            | "firefox"
            | "firefox-esr"
            | "brave"
            | "brave-browser"
            | "microsoft-edge"
            | "microsoft-edge-stable"
            | "edge"
            | "opera"
            | "vivaldi"
            | "zen"
    ) {
        return AppKind::Browser;
    }
    if matches!(
        b.as_str(),
        "gnome-terminal"
            | "gnome-terminal-server"
            | "konsole"
            | "alacritty"
            | "kitty"
            | "wezterm"
            | "foot"
            | "footclient"
            | "xterm"
            | "uxterm"
            | "tilix"
            | "terminator"
            | "terminology"
            | "ghostty"
            | "ptyxis"
            | "kgx"
            | "lxterminal"
            | "xfce4-terminal"
            | "mate-terminal"
            | "qterminal"
    ) || b.ends_with("-terminal")
    {
        return AppKind::Terminal;
    }
    if matches!(
        b.as_str(),
        "code"
            | "code-insiders"
            | "vscodium"
            | "codium"
            | "cursor"
            | "zed"
            | "zeditor"
            | "subl"
            | "sublime_text"
            | "nvim"
            | "vim"
            | "emacs"
            | "emacsclient"
            | "gedit"
            | "kate"
            | "neovide"
            | "lapce"
            | "helix"
            | "hx"
    ) {
        return AppKind::Editor;
    }
    if matches!(
        b.as_str(),
        "nautilus" | "dolphin" | "thunar" | "nemo" | "pcmanfm" | "caja" | "ranger" | "nnn" | "yazi"
    ) {
        return AppKind::Files;
    }
    AppKind::Other
}

fn is_helper_binary(binary: &str) -> bool {
    matches!(
        binary,
        "ps" | "pgrep"
            | "pkill"
            | "pidof"
            | "pgrep-user"
            | "lsof"
            | "ss"
            | "netstat"
            | "which"
            | "which.debianutils"
            | "sleep"
            | "timeout"
            | "env"
            | "sh"
            | "-sh"
            | "dash"
    )
}

fn is_transient(binary: &str) -> bool {
    matches!(
        binary,
        "ls" | "cat"
            | "grep"
            | "rg"
            | "sed"
            | "awk"
            | "find"
            | "git"
            | "cargo"
            | "rustc"
            | "node"
            | "python3"
            | "python"
            | "curl"
            | "wget"
            | "tar"
            | "gzip"
            | "sudo"
            | "systemctl"
            | "journalctl"
            | "dmesg"
            | "id"
            | "whoami"
            | "hostname"
            | "uname"
            | "dbus-daemon"
            | "dbus-launch"
            | "at-spi-bus-launcher"
            | "dconf-service"
            | "gvfsd"
            | "gvfsd-fuse"
            | "xdg-desktop-portal"
            | "xdg-document-portal"
            | "xdg-permission-store"
            | "ibus-daemon"
            | "pulseaudio"
            | "pipewire"
            | "wireplumber"
            | "systemd"
            | "(sd-pam)"
    )
}

/// Pull workspace folders/files out of an editor's argv.
///
/// Keeps only args that look like existing paths and skips known flags
/// (and flags that consume the next arg, like `--user-data-dir <dir>`).
pub fn extract_workspaces(binary: &str, argv: &[String]) -> Vec<String> {
    let b = binary.to_lowercase();
    let editor = matches!(
        b.as_str(),
        "code"
            | "code-insiders"
            | "vscodium"
            | "codium"
            | "cursor"
            | "zed"
            | "zeditor"
            | "subl"
            | "sublime_text"
            | "nvim"
            | "vim"
            | "emacs"
            | "emacsclient"
            | "gedit"
            | "kate"
            | "neovide"
            | "lapce"
            | "helix"
            | "hx"
    );
    if !editor {
        return Vec::new();
    }
    // Flags that take a value in the next argv slot.
    const VALUE_FLAGS: &[&str] = &[
        "--user-data-dir",
        "--extensions-dir",
        "--locale",
        "--log",
        "--open-url",
        "--socket",
        "--server",
        "--remote",
    ];
    let mut out = Vec::new();
    let mut skip_next = false;
    for (i, arg) in argv.iter().enumerate() {
        if i == 0 {
            continue;
        }
        if skip_next {
            skip_next = false;
            continue;
        }
        if arg.starts_with('-') {
            if arg.contains('=') {
                continue;
            }
            if VALUE_FLAGS.contains(&arg.as_str()) {
                skip_next = true;
            }
            // Bare `--` separator, `--new-window`, `--reuse-window`, etc.
            continue;
        }
        // file:// URIs from desktop launches.
        let candidate = arg
            .strip_prefix("file://")
            .map(|s| s.to_string())
            .unwrap_or_else(|| arg.clone());
        if candidate.is_empty() {
            continue;
        }
        if Path::new(&candidate).exists() && !out.contains(&candidate) {
            out.push(candidate);
        }
        if out.len() >= 5 {
            break;
        }
    }
    out
}

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

    #[test]
    fn classifies_known_apps() {
        assert_eq!(classify("google-chrome"), AppKind::Browser);
        assert_eq!(classify("firefox"), AppKind::Browser);
        assert_eq!(classify("gnome-terminal"), AppKind::Terminal);
        assert_eq!(classify("alacritty"), AppKind::Terminal);
        assert_eq!(classify("code"), AppKind::Editor);
        assert_eq!(classify("nvim"), AppKind::Editor);
        assert_eq!(classify("nautilus"), AppKind::Files);
        assert_eq!(classify("my-server"), AppKind::Other);
    }

    #[test]
    fn extracts_editor_workspaces() {
        let dir = std::env::temp_dir();
        let dir_str = dir.to_string_lossy().to_string();
        let argv = vec![
            "code".to_string(),
            "--new-window".to_string(),
            "--user-data-dir".to_string(),
            "/tmp/does-not-exist-xyz".to_string(),
            dir_str.clone(),
            "--nonexistent-path-xyz-123".to_string(),
        ];
        let ws = extract_workspaces("code", &argv);
        assert_eq!(ws, vec![dir_str]);
    }

    #[test]
    fn scan_returns_at_least_shell_fallback() {
        // Must never be empty: worst case is the shell fallback entry.
        let apps = scan_running_apps();
        assert!(!apps.is_empty());
    }

    #[test]
    fn collapsed_chromium_argv_splits() {
        // Chromium rewrites argv into one space-separated string.
        let collapsed = vec![
            "/usr/lib/chromium/chromium --type=renderer --lang=en-US --renderer-client-id=15"
                .to_string(),
        ];
        let split = split_collapsed_argv(collapsed);
        assert_eq!(
            split,
            vec![
                "/usr/lib/chromium/chromium",
                "--type=renderer",
                "--lang=en-US",
                "--renderer-client-id=15",
            ]
        );
        // Binary extraction must not include flags.
        assert_eq!(
            binary_name("/usr/lib/chromium/chromium --type=renderer --lang=en-US"),
            "chromium"
        );
    }

    #[test]
    fn normal_argv_untouched_by_collapse_split() {
        // Paths with spaces (editors) must survive when there is no --type=.
        let argv = vec![
            "code".to_string(),
            "/home/user/my project".to_string(),
            "--new-window".to_string(),
        ];
        assert_eq!(split_collapsed_argv(argv.clone()), argv);
    }
}