swarmy 0.2.10

Real-time analytics dashboard for coding agents (Claude Code, Antigravity, Codex): live swarm, history, API-equivalent spend, weekly limits, and a vault for OAuth logins
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
//! Drawing the status line: segments in their forms from full to shortest, shortened in a fixed
//! order to the width Claude Code gives. Pure: everything comes in through `Input` and `Extra`.

use chrono::{DateTime, Utc};

use super::input::{Input, Window};
use crate::config::StatusSegment;
use crate::core::ids::SeatId;
use crate::core::limits::Level;

/// One piece of the line: its forms from full to shortest (text with colour, plain width).
struct Segment {
    key: StatusSegment,
    forms: Vec<(String, usize)>,
    form: usize,
    dropped: bool,
}

/// Everything the line is made from besides stdin.
pub struct Extra {
    pub fleet: Option<crate::fleet::Fleet>,
    /// Sessions waiting for the user when there is no fresh fleet summary:
    /// (session id, name, what, pane).
    pub waiting: Vec<(String, String, String, Option<crate::core::pane::Pane>)>,
    /// This seat's key in the fleet summary.
    pub seat_key: SeatId,
    pub mask: bool,
    pub color: bool,
    pub segments: Vec<StatusSegment>,
    pub width: usize,
    pub lines: u8,
    /// The git branch of the session's directory (second line only).
    pub branch: Option<String>,
}

struct Paint(bool);

impl Paint {
    fn c(&self, s: &str, code: &str) -> String {
        if self.0 {
            format!("\x1b[{code}m{s}\x1b[0m")
        } else {
            s.to_string()
        }
    }
}

fn form(p: &Paint, parts: &[(&str, &str)]) -> (String, usize) {
    let text: String = parts
        .iter()
        .map(|(t, c)| {
            if c.is_empty() {
                t.to_string()
            } else {
                p.c(t, c)
            }
        })
        .collect();
    let width = parts
        .iter()
        .map(|(t, _)| unicode_width::UnicodeWidthStr::width(*t))
        .sum();
    (text, width)
}

/// A context window this full turns amber, then red (not a limit: its own thresholds).
const CONTEXT_WARN: f64 = 70.0;
const CONTEXT_BAD: f64 = 85.0;

/// The ANSI code of a limit's level (`core::limits::Level`), the same colours as the dashboard.
fn level(pct: f64) -> &'static str {
    match Level::of(pct) {
        Level::Ok => "32",
        Level::Warn => "33",
        Level::Bad | Level::Reached => "1;31",
    }
}

fn hm(d: chrono::Duration) -> String {
    let m = d.num_minutes().max(0);
    if m >= 24 * 60 {
        format!("{}d {}h", m / 1440, (m % 1440) / 60)
    } else if m >= 60 {
        format!("{}h {:02}m", m / 60, m % 60)
    } else {
        format!("{m}m")
    }
}

/// The status line: who needs you, this session's agents, the week, 5 hours, context, cost and
/// burn; shortened to `x.width` in a fixed order; optionally a second line.
pub fn render(input: &Input, x: &Extra, now: DateTime<Utc>) -> String {
    let p = Paint(x.color);
    let me = input.session_id.clone().unwrap_or_default();
    let seat = x.fleet.as_ref().and_then(|f| f.seats.get(&x.seat_key));
    let mut segs: Vec<Segment> = Vec::new();
    for &key in &x.segments {
        let forms: Vec<(String, usize)> = match key {
            StatusSegment::Attention => {
                let others: Vec<(String, String, Option<crate::core::pane::Pane>)> = match &x.fleet
                {
                    Some(f) => f
                        .needs_you
                        .iter()
                        .filter(|n| n.session_id.as_str() != me)
                        .map(|n| (n.name.clone(), n.what.clone(), n.pane.clone()))
                        .collect(),
                    None => x
                        .waiting
                        .iter()
                        .filter(|w| w.0 != me)
                        .map(|w| (w.1.clone(), w.2.clone(), w.3.clone()))
                        .collect(),
                };
                if others.is_empty() {
                    continue;
                }
                let n = others.len();
                let names: Vec<String> = others
                    .iter()
                    .map(|o| {
                        if x.mask {
                            "•••".into()
                        } else {
                            o.0.clone()
                        }
                    })
                    .collect();
                let full = if n == 1 {
                    format!("▲ {} needs you: {}", names[0], others[0].1)
                } else {
                    format!("▲ {n} need you: {}", names.join(", "))
                };
                let mut v = Vec::new();
                if !x.mask {
                    v.push(form(&p, &[(&full, "1;33"), (" · swarmy n J", "2")]));
                }
                v.push(form(
                    &p,
                    &[
                        (
                            &format!("▲ {n} need{} you", if n == 1 { "s" } else { "" }),
                            "1;33",
                        ),
                        (" · n J", "2"),
                    ],
                ));
                v.push(form(&p, &[(&format!("▲{n}"), "1;33")]));
                v
            }
            StatusSegment::Agents => {
                let Some(sc) = x
                    .fleet
                    .as_ref()
                    .and_then(|f| f.sessions.get(me.as_str()))
                    .filter(|s| s.agents > 0)
                else {
                    continue;
                };
                let mut parts: Vec<(String, &str)> = vec![(
                    format!(
                        "◐ {} agent{} ",
                        sc.agents,
                        if sc.agents == 1 { "" } else { "s" }
                    ),
                    "36",
                )];
                if sc.working > 0 {
                    parts.push((format!("●{} ", sc.working), "32"));
                }
                if sc.thinking > 0 {
                    parts.push((format!("◌{} ", sc.thinking), "36"));
                }
                if sc.waiting > 0 {
                    parts.push((format!("◐{} ", sc.waiting), "36"));
                }
                if sc.stalled > 0 {
                    parts.push((format!("✖{} ", sc.stalled), "1;31"));
                }
                let last = parts.len() - 1;
                parts[last].0 = parts[last].0.trim_end().to_string();
                let refs: Vec<(&str, &str)> = parts.iter().map(|(t, c)| (t.as_str(), *c)).collect();
                vec![
                    form(&p, &refs),
                    form(&p, &[(&format!("◐{}", sc.agents), "36")]),
                ]
            }
            StatusSegment::Week => {
                // Exact from stdin; else the dashboard's estimate (≈).
                let (pct, resets, exact) = match input
                    .rate_limits
                    .as_ref()
                    .and_then(|r| r.seven_day.as_ref())
                    .and_then(Window::pair)
                {
                    Some((p, t)) => (p, Some(t), true),
                    None => match seat.and_then(|s| s.week_used.map(|w| (w, s.resets_at))) {
                        Some((p, t)) => (p, t, false),
                        None => continue,
                    },
                };
                let approx = if exact { "" } else { "≈" };
                let head = format!("week used {approx}{pct:.0}%");
                let code = level(pct);
                let when = |t: DateTime<Utc>| {
                    t.with_timezone(&chrono::Local)
                        .format("%a %H:%M")
                        .to_string()
                };
                if Level::of(pct) == Level::Reached {
                    let r = resets
                        .map(|t| format!(" · resets {}", when(t)))
                        .unwrap_or_default();
                    vec![
                        form(
                            &p,
                            &[(&head, "1;31"), (" · limit reached", "1;31"), (&r, "2")],
                        ),
                        form(&p, &[(&head, "1;31"), (" · limit reached", "1;31")]),
                        form(&p, &[(&format!("week {approx}{pct:.0}%"), "1;31")]),
                    ]
                } else if let Some(out) = seat.and_then(|s| s.runs_out_at).filter(|t| *t > now) {
                    let before_reset = resets.is_none_or(|r| out < r);
                    let code = if before_reset && out - now < chrono::Duration::hours(24) {
                        "1;31"
                    } else {
                        code
                    };
                    vec![
                        form(
                            &p,
                            &[
                                (&head, code),
                                (
                                    &format!(" · runs out in {} ({})", hm(out - now), when(out)),
                                    code,
                                ),
                            ],
                        ),
                        form(
                            &p,
                            &[
                                (&head, code),
                                (&format!(" · runs out in {}", hm(out - now)), code),
                            ],
                        ),
                        form(&p, &[(&format!("week {approx}{pct:.0}%"), code)]),
                    ]
                } else {
                    let r = resets
                        .map(|t| format!(" · resets {}", when(t)))
                        .unwrap_or_default();
                    vec![
                        form(&p, &[(&head, code), (&r, "2")]),
                        form(&p, &[(&head, code)]),
                        form(&p, &[(&format!("week {approx}{pct:.0}%"), code)]),
                    ]
                }
            }
            StatusSegment::FiveHour => {
                let Some((pct, _)) = input
                    .rate_limits
                    .as_ref()
                    .and_then(|r| r.five_hour.as_ref())
                    .and_then(Window::pair)
                else {
                    continue;
                };
                vec![form(&p, &[(&format!("5h {pct:.0}%"), level(pct))])]
            }
            StatusSegment::Context => {
                let Some(c) = input
                    .context_window
                    .as_ref()
                    .and_then(|c| c.used_percentage)
                else {
                    continue;
                };
                let code = if c >= CONTEXT_BAD {
                    "1;31"
                } else if c >= CONTEXT_WARN {
                    "33"
                } else {
                    ""
                };
                vec![form(&p, &[(&format!("ctx {c:.0}%"), code)])]
            }
            StatusSegment::Cost => {
                let Some(c) = input.cost.as_ref().and_then(|c| c.total_cost_usd) else {
                    continue;
                };
                vec![form(&p, &[(&format!("${c:.2}"), "")])]
            }
            StatusSegment::Burn => {
                let Some(b) = x
                    .fleet
                    .as_ref()
                    .and_then(|f| f.sessions.get(me.as_str()))
                    .map(|s| s.burn_usd_h)
                    .filter(|b| *b > 0.0)
                else {
                    continue;
                };
                vec![form(&p, &[(&format!("${b:.1}/h"), "2")])]
            }
        };
        segs.push(Segment {
            key,
            forms,
            form: 0,
            dropped: false,
        });
    }
    // Shorten until it fits: drop or shorten pieces, least useful first; the alert and the week
    // never go.
    let sep = 3usize;
    let width = |segs: &[Segment]| -> usize {
        let live: Vec<&Segment> = segs.iter().filter(|s| !s.dropped).collect();
        live.iter().map(|s| s.forms[s.form].1).sum::<usize>() + sep * live.len().saturating_sub(1)
    };
    let pct_of = |k: StatusSegment| -> f64 {
        match k {
            StatusSegment::FiveHour => input
                .rate_limits
                .as_ref()
                .and_then(|r| r.five_hour.as_ref())
                .map(|w| w.used_percentage)
                .unwrap_or(0.0),
            StatusSegment::Context => input
                .context_window
                .as_ref()
                .and_then(|c| c.used_percentage)
                .unwrap_or(0.0),
            StatusSegment::Attention
            | StatusSegment::Agents
            | StatusSegment::Week
            | StatusSegment::Cost
            | StatusSegment::Burn => 0.0,
        }
    };
    let steps: [(StatusSegment, bool); 10] = [
        (StatusSegment::Burn, true),
        (StatusSegment::Cost, true),
        (StatusSegment::FiveHour, true),
        (StatusSegment::Week, false),
        (StatusSegment::Agents, false),
        (StatusSegment::Attention, false),
        (StatusSegment::Context, true),
        (StatusSegment::Week, false),
        (StatusSegment::Attention, false),
        (StatusSegment::Agents, true),
    ];
    for (key, drop) in steps {
        if width(&segs) <= x.width {
            break;
        }
        if let Some(sg) = segs.iter_mut().find(|s| s.key == key && !s.dropped) {
            if drop {
                // A 5-hour limit from amber on and a context past 70% stay.
                let keep = (key == StatusSegment::FiveHour
                    && Level::of(pct_of(key)) >= Level::Warn)
                    || (key == StatusSegment::Context && pct_of(key) >= CONTEXT_WARN);
                if !keep {
                    sg.dropped = true;
                }
            } else if sg.form + 1 < sg.forms.len() {
                sg.form += 1;
            }
        }
    }
    let line: Vec<String> = segs
        .iter()
        .filter(|s| !s.dropped)
        .map(|s| s.forms[s.form].0.clone())
        .collect();
    let mut out = if line.is_empty() {
        p.c("swarmy", "2")
    } else {
        line.join(&p.c(" │ ", "2"))
    };
    if x.lines >= 2 {
        let mut second: Vec<String> = Vec::new();
        if let Some(m) = input.model.as_ref().and_then(|m| m.display_name.clone()) {
            let e = input
                .effort
                .as_ref()
                .and_then(|e| e.level.clone())
                .map(|e| format!(" {e}"))
                .unwrap_or_default();
            second.push(format!("{m}{e}"));
        }
        if let Some(b) = x.branch.clone() {
            let wt = input
                .workspace
                .as_ref()
                .and_then(|w| w.git_worktree.as_ref())
                .is_some_and(|v| !v.is_null());
            second.push(format!("⎇ {b}{}", if wt { " (worktree)" } else { "" }));
        }
        if let Some(n) = input.session_name.clone().filter(|_| !x.mask) {
            second.push(n);
        }
        if input.prompt_cache.as_ref().and_then(|c| c.warm) == Some(false) {
            second.push("cache cold".into());
        }
        if !second.is_empty() {
            let l = truncate_to_width(&second.join(" · "), x.width);
            out.push('\n');
            out.push_str(&p.c(&l, "2"));
        }
    }
    out
}

/// The columns a terminal gives `s`: escape sequences (colours, OSC links) take none, wide
/// characters take two (review L3).
pub fn visible_width(s: &str) -> usize {
    unicode_width::UnicodeWidthStr::width(strip_escapes(s).as_str())
}

/// `s` without its terminal escape sequences: CSI (`ESC [ … final byte`) and OSC
/// (`ESC ] … BEL` or `ESC \\`).
fn strip_escapes(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    let mut it = s.chars().peekable();
    while let Some(c) = it.next() {
        if c != '\x1b' {
            out.push(c);
            continue;
        }
        match it.next() {
            Some('[') => {
                for c in it.by_ref() {
                    if ('\x40'..='\x7e').contains(&c) {
                        break;
                    }
                }
            }
            Some(']') => {
                while let Some(c) = it.next() {
                    if c == '\x07' || (c == '\x1b' && it.next_if_eq(&'\\').is_some()) {
                        break;
                    }
                }
            }
            Some(_) | None => {}
        }
    }
    out
}

/// The longest start of `s` that fits `width` columns, cut at a character, never inside a wide
/// one (review L3: a cut by character count made Chinese, Japanese or Korean text twice as wide).
pub fn truncate_to_width(s: &str, width: usize) -> String {
    let mut out = String::new();
    let mut w = 0;
    for c in s.chars() {
        let cw = unicode_width::UnicodeWidthChar::width(c).unwrap_or(0);
        if w + cw > width {
            break;
        }
        out.push(c);
        w += cw;
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::statusline::input::parse_input;
    use chrono::TimeZone;

    const INPUT: &str = r#"{"session_id":"s1","model":{"display_name":"Opus"},"cost":{"total_cost_usd":4.2},
        "context_window":{"used_percentage":42.0},
        "rate_limits":{"five_hour":{"used_percentage":12.5,"resets_at":1795400000},
                       "seven_day":{"used_percentage":3,"resets_at":1795960000}}}"#;

    fn extra(fleet: Option<crate::fleet::Fleet>, width: usize) -> Extra {
        Extra {
            fleet,
            waiting: Vec::new(),
            seat_key: SeatId::from_stored("0123456789abcdef"),
            mask: false,
            color: false,
            segments: crate::config::StatuslineConf::default().segments,
            width,
            lines: 1,
            branch: None,
        }
    }

    fn fleet(now: DateTime<Utc>) -> crate::fleet::Fleet {
        use crate::fleet::*;
        let mut f = Fleet {
            v: VERSION,
            written_at: now,
            ..Default::default()
        };
        f.needs_you = vec![
            NeedsYou {
                session_id: "other".into(),
                name: "shop-api".into(),
                what: "permission".into(),
                ..Default::default()
            },
            NeedsYou {
                session_id: "s1".into(),
                name: "me".into(),
                what: "question".into(),
                ..Default::default()
            },
            NeedsYou {
                session_id: "x".into(),
                name: "docs".into(),
                what: "question".into(),
                ..Default::default()
            },
        ];
        f.sessions.insert(
            "s1".into(),
            SessionCounts {
                agents: 3,
                working: 2,
                thinking: 1,
                burn_usd_h: 3.1,
                ..Default::default()
            },
        );
        f.seats.insert(
            SeatId::from_stored("0123456789abcdef"),
            SeatLimit {
                runs_out_at: Some(now + chrono::Duration::hours(7)),
                resets_at: Some(now + chrono::Duration::days(3)),
                ..Default::default()
            },
        );
        f
    }

    #[test]
    fn the_full_line_and_how_it_shortens() {
        let now = Utc.timestamp_opt(1_795_380_000, 0).unwrap();
        let input = parse_input(INPUT);
        let full = render(&input, &extra(Some(fleet(now)), 400), now);
        // The other two sessions need you (not this one), this session's agents, the week with
        // its runs-out time, 5 hours, context, cost, burn.
        assert!(full.starts_with("▲ 2 need you: shop-api, docs · swarmy n J │ ◐ 3 agents ●2 ◌1 │ week used 3% · runs out in 7h 00m ("), "{full}");
        assert!(
            full.ends_with(") │ 5h 12% │ ctx 42% │ $4.20 │ $3.1/h"),
            "{full}"
        );

        // Narrow: burn, cost, 5h go; then shorter forms; the alert and the week stay.
        let mid = render(&input, &extra(Some(fleet(now)), 80), now);
        assert!(mid.len() <= 80 * 3, "{mid}");
        assert!(mid.contains("▲") && mid.contains("week"), "{mid}");
        assert!(!mid.contains("$3.1/h") && !mid.contains("$4.20"), "{mid}");
        let tiny = render(&input, &extra(Some(fleet(now)), 30), now);
        assert!(tiny.starts_with("▲2 │ "), "{tiny}");
        assert!(tiny.contains("week 3%"), "{tiny}");
    }

    /// Review L3: the user's coloured status line is measured by what it shows, and the second
    /// line is cut to the width in columns, not characters.
    #[test]
    fn widths_count_columns_not_bytes_or_escapes() {
        let coloured = "\x1b[1;32mmain\x1b[0m \x1b]8;;https://example.com\x07link\x1b]8;;\x07 ok";
        assert_eq!(visible_width(coloured), "main link ok".len());
        assert_eq!(visible_width("項目の説明"), 10);
        assert_eq!(truncate_to_width("項目の説明", 5), "項目");
        assert_eq!(truncate_to_width("abc", 5), "abc");
        let now = Utc::now();
        let input = parse_input(r#"{"session_id":"s","session_name":"重要な機能の作業"}"#);
        let mut x = extra(None, 20);
        x.lines = 2;
        let out = render(&input, &x, now);
        let second = out.lines().nth(1).unwrap();
        assert!(visible_width(second) <= 20, "{second:?}");
        assert!(second.contains("重要な機能"), "{second:?}");
    }

    #[test]
    fn without_the_dashboard_or_limits() {
        let now = Utc::now();
        let input = parse_input(r#"{"session_id":"s","cost":{"total_cost_usd":0.5}}"#);
        assert_eq!(render(&input, &extra(None, 120), now), "$0.50");
        // The registry fallback still tells who waits; masked names are hidden.
        let mut x = extra(None, 120);
        x.waiting = vec![("other".into(), "client-x".into(), "permission".into(), None)];
        assert_eq!(
            render(&input, &x, now),
            "▲ client-x needs you: permission · swarmy n J │ $0.50"
        );
        x.mask = true;
        assert_eq!(render(&input, &x, now), "▲ 1 needs you · n J │ $0.50");
    }
}