carte 0.5.0

Runs your apps' processes on demand behind *.localhost hostnames, one stack per git worktree
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//! Desktop notifications when a process crashes, carte gives up on it, or it stops
//! at a debugger prompt.

use std::collections::BTreeMap;
use std::collections::btree_map::Entry;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::time::Duration;

use carte_api::{Event, EventKind, EventSubject};
use carte_core::model::{InstanceKey, Stack, World};
use carte_core::naming;
use tokio::process::Command;
use tokio::sync::broadcast::error::RecvError;
use tracing::{debug, warn};

use crate::clock::now_ms;
use crate::supervisor::Handle;

/// After a notification, how long further crashes of the instance only count
/// towards one summary.
const QUIET_MS: u64 = 5 * 60 * 1000;

/// Something to tell about one instance.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Notice {
    pub key: InstanceKey,
    pub subtitle: String,
    pub message: String,
}

struct Quiet {
    until_ms: u64,
    crashes: u32,
    latest: String,
}

/// After a pause notification, how long further pauses of the instance stay quiet.
const PAUSE_QUIET_MS: u64 = 60 * 1000;

/// Decides which crashes to tell about: the first one right away, later ones within
/// the quiet period as one summary when it ends, and every give-up. A pause is told
/// about unless the same process paused within the last minute.
#[derive(Default)]
pub struct Throttle {
    quiet: BTreeMap<InstanceKey, Quiet>,
    paused: BTreeMap<(InstanceKey, String), u64>,
}

impl Throttle {
    /// What to tell after `event`, or after a quiet period ends when there is none.
    /// While notifications are disabled it drops everything it was holding, so it
    /// starts fresh when they're enabled again.
    pub fn step(&mut self, event: Option<&Event>, enabled: bool, now_ms: u64) -> Vec<Notice> {
        if !enabled {
            self.quiet.clear();
            return Vec::new();
        }
        match event {
            Some(event) => self.event(event, now_ms),
            None => self.due(now_ms),
        }
    }

    pub fn event(&mut self, event: &Event, now_ms: u64) -> Vec<Notice> {
        let mut notices = self.due(now_ms);
        let Some(subject) = event.subjects.first() else {
            return notices;
        };
        let (Some(key), Some(process)) = (instance_key(subject), subject.process.as_deref()) else {
            return notices;
        };
        let label = instance_label(subject);
        match event.kind {
            EventKind::Crashed => {
                if let Some(quiet) = self.quiet.get_mut(&key) {
                    quiet.crashes += 1;
                    quiet.latest = event.message.clone();
                } else {
                    self.quiet.insert(
                        key.clone(),
                        Quiet {
                            until_ms: now_ms + QUIET_MS,
                            crashes: 0,
                            latest: String::new(),
                        },
                    );
                    notices.push(Notice {
                        key,
                        subtitle: format!("{label} crashed"),
                        message: event.message.clone(),
                    });
                }
            }
            EventKind::Paused => {
                self.paused
                    .retain(|_, at| now_ms.saturating_sub(*at) < PAUSE_QUIET_MS);
                if let Entry::Vacant(entry) = self.paused.entry((key.clone(), process.to_string()))
                {
                    entry.insert(now_ms);
                    notices.push(Notice {
                        subtitle: format!("{label} paused in {process}"),
                        message: format!("carte attach {label}/{process}"),
                        key,
                    });
                }
            }
            EventKind::GaveUp => {
                self.quiet.remove(&key);
                notices.push(Notice {
                    key,
                    subtitle: format!("{label}: gave up restarting {process}"),
                    message: event.message.clone(),
                });
            }
            _ => {}
        }
        notices
    }

    /// Summaries of the quiet periods that have ended.
    pub fn due(&mut self, now_ms: u64) -> Vec<Notice> {
        let ended: Vec<InstanceKey> = self
            .quiet
            .iter()
            .filter(|(_, quiet)| quiet.until_ms <= now_ms)
            .map(|(key, _)| key.clone())
            .collect();
        ended
            .into_iter()
            .filter_map(|key| {
                let quiet = self.quiet.remove(&key)?;
                (quiet.crashes > 0).then(|| {
                    let label = instance_label(&subject(&key));
                    let times = match quiet.crashes {
                        1 => "once more".to_string(),
                        n => format!("{n} more times"),
                    };
                    Notice {
                        key,
                        subtitle: format!("{label} crashed {times}"),
                        message: quiet.latest,
                    }
                })
            })
            .collect()
    }

    /// When the next quiet period ends.
    pub fn next_due(&self) -> Option<u64> {
        self.quiet.values().map(|quiet| quiet.until_ms).min()
    }
}

fn instance_key(subject: &EventSubject) -> Option<InstanceKey> {
    let stack = match subject.stack.as_deref()? {
        "main" => Stack::Main,
        slug => Stack::Branch(slug.to_string()),
    };
    Some(InstanceKey {
        project: subject.project.clone(),
        app: subject.app.clone()?,
        stack,
    })
}

fn subject(key: &InstanceKey) -> EventSubject {
    EventSubject {
        project: key.project.clone(),
        stack: Some(key.stack.slug().to_string()),
        app: Some(key.app.clone()),
        process: None,
    }
}

fn instance_label(subject: &EventSubject) -> String {
    EventSubject {
        process: None,
        ..subject.clone()
    }
    .label()
}

/// A notification as the desktop shows it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Notification {
    pub title: String,
    pub subtitle: String,
    pub message: String,
    /// Notification Center keeps one entry per group.
    pub group: String,
    /// What a click opens.
    pub url: Option<String>,
}

impl Notification {
    pub fn new(notice: Notice, world: &World) -> Self {
        let key = &notice.key;
        Notification {
            title: "carte".into(),
            subtitle: notice.subtitle,
            message: notice.message,
            group: format!("carte.{}.{}.{}", key.project, key.stack.slug(), key.app),
            url: url(world, key),
        }
    }
}

fn url(world: &World, key: &InstanceKey) -> Option<String> {
    let state = world.project(&key.project)?;
    let app = state.project.app(&key.app)?;
    Some(naming::instance_url(
        &world.machine,
        &state.project,
        app,
        &key.stack,
    ))
}

pub fn terminal_notifier_args(n: &Notification) -> Vec<String> {
    let mut args = vec![
        "-title".into(),
        n.title.clone(),
        "-subtitle".into(),
        n.subtitle.clone(),
        "-message".into(),
        n.message.clone(),
        "-group".into(),
        n.group.clone(),
    ];
    if let Some(url) = &n.url {
        args.extend(["-open".into(), url.clone()]);
    }
    args
}

pub fn osascript_args(n: &Notification) -> Vec<String> {
    vec![
        "-e".into(),
        "on run argv".into(),
        "-e".into(),
        "display notification (item 2 of argv) with title (item 1 of argv) subtitle (item 3 of argv)"
            .into(),
        "-e".into(),
        "end run".into(),
        n.title.clone(),
        n.message.clone(),
        n.subtitle.clone(),
    ]
}

pub fn notify_send_args(n: &Notification) -> Vec<String> {
    vec![
        "--app-name".into(),
        "carte".into(),
        "--".into(),
        format!("{}: {}", n.title, n.subtitle),
        n.message.clone(),
    ]
}

/// The program and arguments that show `n` on macOS: terminal-notifier when it's
/// installed, for the click action, otherwise AppleScript.
pub fn macos_command(
    n: &Notification,
    terminal_notifier: Option<PathBuf>,
) -> (PathBuf, Vec<String>) {
    match terminal_notifier {
        Some(program) => (program, terminal_notifier_args(n)),
        None => ("/usr/bin/osascript".into(), osascript_args(n)),
    }
}

/// The program and arguments that show `n` on Linux; none without a display.
pub fn linux_command(n: &Notification, display: bool) -> Option<(PathBuf, Vec<String>)> {
    display.then(|| ("notify-send".into(), notify_send_args(n)))
}

/// The first executable `name` on `PATH` or in Homebrew's directories.
fn find_program(name: &str) -> Option<PathBuf> {
    let path = std::env::var_os("PATH").unwrap_or_default();
    std::env::split_paths(&path)
        .chain([
            PathBuf::from("/opt/homebrew/bin"),
            PathBuf::from("/usr/local/bin"),
        ])
        .map(|dir| dir.join(name))
        .find(|candidate| is_executable(candidate))
}

fn is_executable(path: &Path) -> bool {
    use std::os::unix::fs::PermissionsExt;
    path.metadata()
        .is_ok_and(|meta| meta.is_file() && meta.permissions().mode() & 0o111 != 0)
}

fn command(n: &Notification) -> Option<(PathBuf, Vec<String>)> {
    if cfg!(target_os = "macos") {
        Some(macos_command(n, find_program("terminal-notifier")))
    } else {
        let display = ["DISPLAY", "WAYLAND_DISPLAY"]
            .iter()
            .any(|var| std::env::var_os(var).is_some_and(|v| !v.is_empty()));
        linux_command(n, display)
    }
}

async fn deliver(n: Notification) {
    let Some((program, args)) = command(&n) else {
        debug!("no display for a notification: {}", n.subtitle);
        return;
    };
    let child = Command::new(&program)
        .args(&args)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .kill_on_drop(true)
        .spawn();
    let mut child = match child {
        Ok(child) => child,
        Err(err) => {
            debug!("couldn't run {}: {err}", program.display());
            return;
        }
    };
    match tokio::time::timeout(Duration::from_secs(5), child.wait()).await {
        Ok(Ok(status)) if status.success() => {}
        Ok(Ok(status)) => warn!("{} failed with {status}", program.display()),
        Ok(Err(err)) => warn!("{} failed: {err}", program.display()),
        Err(_) => warn!("{} didn't finish in time", program.display()),
    }
}

/// Follows the supervisor's events and notifies, while the machine config allows it.
pub async fn run(supervisor: Handle) {
    let mut events = supervisor.follow_events();
    let mut throttle = Throttle::default();
    loop {
        let wait = throttle.next_due().map(|at| at.saturating_sub(now_ms()));
        let event = tokio::select! {
            received = events.recv() => match received {
                Ok(event) => Some(event),
                Err(RecvError::Lagged(_)) => continue,
                Err(RecvError::Closed) => return,
            },
            () = sleep_ms(wait) => None,
        };
        let world = supervisor.world();
        let notices = throttle.step(event.as_ref(), world.machine.notifications, now_ms());
        for notice in notices {
            tokio::spawn(deliver(Notification::new(notice, &world)));
        }
    }
}

async fn sleep_ms(ms: Option<u64>) {
    match ms {
        Some(ms) => tokio::time::sleep(Duration::from_millis(ms)).await,
        None => std::future::pending().await,
    }
}

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

    fn event(kind: EventKind, stack: &str, process: Option<&str>, message: &str) -> Event {
        Event {
            seq: 0,
            at_ms: 0,
            kind,
            message: message.into(),
            subjects: vec![EventSubject {
                project: "acme".into(),
                stack: Some(stack.into()),
                app: Some("acme-web".into()),
                process: process.map(str::to_string),
            }],
            boot_ms: None,
            exit_code: Some(1),
        }
    }

    fn crash(stack: &str, message: &str) -> Event {
        event(EventKind::Crashed, stack, Some("worker"), message)
    }

    fn subtitles(notices: &[Notice]) -> Vec<&str> {
        notices.iter().map(|n| n.subtitle.as_str()).collect()
    }

    #[test]
    fn tells_about_the_first_crash_and_sums_up_the_rest() {
        let mut throttle = Throttle::default();
        let first = throttle.event(&crash("feature-x", "worker exited with code 1"), 1_000);
        assert_eq!(subtitles(&first), ["acme-web@feature-x crashed"]);
        assert_eq!(first[0].message, "worker exited with code 1");

        assert!(
            throttle
                .event(&crash("feature-x", "worker exited with code 2"), 60_000)
                .is_empty()
        );
        assert!(
            throttle
                .event(&crash("feature-x", "worker exited with code 3"), 120_000)
                .is_empty()
        );
        assert_eq!(throttle.next_due(), Some(1_000 + QUIET_MS));
        assert!(throttle.due(QUIET_MS).is_empty());

        let summary = throttle.due(1_000 + QUIET_MS);
        assert_eq!(
            subtitles(&summary),
            ["acme-web@feature-x crashed 2 more times"]
        );
        assert_eq!(summary[0].message, "worker exited with code 3");
        assert_eq!(throttle.next_due(), None);

        let again = throttle.event(
            &crash("feature-x", "worker exited with code 1"),
            2 * QUIET_MS,
        );
        assert_eq!(subtitles(&again), ["acme-web@feature-x crashed"]);
    }

    #[test]
    fn a_quiet_period_without_crashes_ends_silently() {
        let mut throttle = Throttle::default();
        throttle.event(&crash("main", "worker was killed by signal 9"), 0);
        assert!(throttle.due(QUIET_MS).is_empty());
        assert_eq!(throttle.next_due(), None);
        assert_eq!(
            subtitles(&throttle.event(&crash("main", "again"), QUIET_MS + 1)),
            ["acme-web crashed"]
        );
    }

    #[test]
    fn a_crash_after_the_quiet_period_sums_up_first() {
        let mut throttle = Throttle::default();
        throttle.event(&crash("main", "one"), 0);
        throttle.event(&crash("main", "two"), 1_000);
        let notices = throttle.event(&crash("main", "three"), QUIET_MS + 1);
        assert_eq!(
            subtitles(&notices),
            ["acme-web crashed once more", "acme-web crashed"]
        );
        assert_eq!(notices[0].message, "two");
    }

    #[test]
    fn throttles_each_instance_on_its_own() {
        let mut throttle = Throttle::default();
        throttle.event(&crash("main", "one"), 0);
        assert_eq!(
            subtitles(&throttle.event(&crash("feature-x", "two"), 1_000)),
            ["acme-web@feature-x crashed"]
        );
    }

    #[test]
    fn a_give_up_always_tells_and_drops_the_summary() {
        let mut throttle = Throttle::default();
        throttle.event(&crash("feature-x", "one"), 0);
        throttle.event(&crash("feature-x", "two"), 1_000);
        let message = "worker crashed 5 times within a minute; it stays down";
        let notices = throttle.event(
            &event(EventKind::GaveUp, "feature-x", Some("worker"), message),
            2_000,
        );
        assert_eq!(
            subtitles(&notices),
            ["acme-web@feature-x: gave up restarting worker"]
        );
        assert_eq!(notices[0].message, message);
        assert_eq!(throttle.next_due(), None);
        assert!(throttle.due(QUIET_MS * 2).is_empty());
    }

    #[test]
    fn tells_about_a_pause_with_the_command_that_answers_it() {
        let mut throttle = Throttle::default();
        let pause =
            |stack: &str, process| event(EventKind::Paused, stack, Some(process), "at (rdbg)");
        let notices = throttle.event(&pause("feature-x", "worker"), 0);
        assert_eq!(subtitles(&notices), ["acme-web@feature-x paused in worker"]);
        assert_eq!(notices[0].message, "carte attach acme-web@feature-x/worker");
        assert!(
            throttle
                .event(&pause("feature-x", "worker"), 20_000)
                .is_empty(),
            "stepping through code doesn't notify at each step"
        );
        assert_eq!(
            subtitles(&throttle.event(&pause("feature-x", "web"), 30_000)),
            ["acme-web@feature-x paused in web"],
            "another process does"
        );
        assert_eq!(
            subtitles(&throttle.event(&pause("main", "web"), 30_000)),
            ["acme-web paused in web"]
        );
        assert_eq!(
            subtitles(&throttle.event(&pause("feature-x", "worker"), 60_000)),
            ["acme-web@feature-x paused in worker"]
        );
        assert_eq!(throttle.next_due(), None);
    }

    #[test]
    fn ignores_failed_starts_clean_exits_and_stops() {
        let mut throttle = Throttle::default();
        for event in [
            event(EventKind::Crashed, "main", None, "web exited with code 1"),
            event(EventKind::Exited, "main", Some("worker"), "worker exited"),
            event(EventKind::Stopped, "main", None, "idle for 15m"),
        ] {
            assert!(throttle.event(&event, 0).is_empty());
        }
        assert_eq!(throttle.next_due(), None);
    }

    #[test]
    fn keeps_nothing_while_disabled_and_starts_fresh() {
        let mut throttle = Throttle::default();
        throttle.step(Some(&crash("main", "one")), true, 0);
        assert!(
            throttle
                .step(Some(&crash("main", "two")), false, 1_000)
                .is_empty()
        );
        assert_eq!(throttle.next_due(), None);
        assert!(
            throttle
                .step(Some(&crash("main", "three")), false, 2_000)
                .is_empty()
        );
        assert_eq!(throttle.next_due(), None);

        let notices = throttle.step(Some(&crash("main", "four")), true, 3_000);
        assert_eq!(subtitles(&notices), ["acme-web crashed"]);
        assert_eq!(notices[0].message, "four");
        assert!(throttle.step(None, true, 3_000 + QUIET_MS).is_empty());
    }

    #[test]
    fn a_quiet_period_ending_while_disabled_tells_nothing() {
        let mut throttle = Throttle::default();
        throttle.step(Some(&crash("main", "one")), true, 0);
        throttle.step(Some(&crash("main", "two")), true, 1_000);
        assert!(throttle.step(None, false, QUIET_MS).is_empty());
        assert_eq!(throttle.next_due(), None);
    }

    fn notification(url: Option<&str>) -> Notification {
        Notification {
            title: "carte".into(),
            subtitle: "acme-web@feature-x crashed".into(),
            message: "worker exited with code 1".into(),
            group: "carte.acme.feature-x.acme-web".into(),
            url: url.map(str::to_string),
        }
    }

    #[test]
    fn uses_terminal_notifier_with_a_click_to_open() {
        let (program, args) = macos_command(
            &notification(Some("https://web.acme.test")),
            Some("/opt/homebrew/bin/terminal-notifier".into()),
        );
        assert_eq!(program, Path::new("/opt/homebrew/bin/terminal-notifier"));
        assert_eq!(
            args,
            [
                "-title",
                "carte",
                "-subtitle",
                "acme-web@feature-x crashed",
                "-message",
                "worker exited with code 1",
                "-group",
                "carte.acme.feature-x.acme-web",
                "-open",
                "https://web.acme.test",
            ]
        );
        let (_, args) = macos_command(&notification(None), Some("terminal-notifier".into()));
        assert!(!args.contains(&"-open".to_string()));
    }

    #[test]
    fn falls_back_to_applescript_with_the_text_as_arguments() {
        let mut n = notification(None);
        n.message = "worker said \"bye\" & left".into();
        let (program, args) = macos_command(&n, None);
        assert_eq!(program, Path::new("/usr/bin/osascript"));
        assert_eq!(
            args,
            [
                "-e",
                "on run argv",
                "-e",
                "display notification (item 2 of argv) with title (item 1 of argv) subtitle (item 3 of argv)",
                "-e",
                "end run",
                "carte",
                "worker said \"bye\" & left",
                "acme-web@feature-x crashed",
            ]
        );
    }

    #[test]
    fn uses_notify_send_only_with_a_display() {
        let n = notification(Some("https://web.acme.test"));
        assert_eq!(linux_command(&n, false), None);
        let (program, args) = linux_command(&n, true).unwrap();
        assert_eq!(program, Path::new("notify-send"));
        assert_eq!(
            args,
            [
                "--app-name",
                "carte",
                "--",
                "carte: acme-web@feature-x crashed",
                "worker exited with code 1",
            ]
        );
    }

    #[test]
    fn has_no_click_action_for_an_unknown_app() {
        let notice = Notice {
            key: InstanceKey {
                project: "acme".into(),
                app: "acme-web".into(),
                stack: Stack::Branch("feature-x".into()),
            },
            subtitle: "acme-web@feature-x crashed".into(),
            message: "worker exited with code 1".into(),
        };
        let n = Notification::new(notice, &World::default());
        assert_eq!(n, notification(None));
    }
}