swarmy 0.2.11

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
//! Where a Claude Code runs, so `J` and `swarmy jump` can take the user there: a tmux pane,
//! a Zellij pane, a kitty window, a WezTerm pane, an iTerm2 session, or a Warp session. Plain
//! data; finding and focusing it is `claude::pane`.

use std::fmt;

use serde::{Deserialize, Serialize};

use super::ids::short;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum Pane {
    /// `session:@window.%pane`, as Claude Code records it in its registry.
    Tmux { pane: String },
    /// `ZELLIJ_SESSION_NAME` + `ZELLIJ_PANE_ID`.
    Zellij { session: String, pane_id: String },
    /// `KITTY_WINDOW_ID` (with optional `KITTY_LISTEN_ON` and `KITTY_PID`).
    Kitty { window: KittyWindow },
    /// `WEZTERM_PANE` (with optional `WEZTERM_UNIX_SOCKET`).
    WezTerm {
        pane_id: u32,
        #[serde(default)]
        socket: Option<String>,
    },
    /// iTerm2 session unique ID (extracted from `ITERM_SESSION_ID`).
    ITerm2 { session_id: String },
    /// `WARP_TERMINAL_SESSION_UUID`.
    Warp { session_id: String },
}

/// Whether `id` looks like a Warp session id (hex, maybe with dashes), so only such an id ends up
/// in the `warp://session/<id>` URL.
pub fn is_warp_session_id(id: &str) -> bool {
    (8..=64).contains(&id.len()) && id.chars().all(|c| c.is_ascii_hexdigit() || c == '-')
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct KittyWindow {
    /// `KITTY_WINDOW_ID` of the process.
    pub id: u32,
    /// `KITTY_LISTEN_ON` of its kitty, e.g. `unix:@kitty-1234`; set only with `listen_on`.
    pub socket: Option<String>,
    /// `KITTY_PID` of its kitty OS process, so a no-socket jump never targets another kitty.
    #[serde(default)]
    pub kitty_pid: Option<u32>,
}

impl Pane {
    /// Detect a terminal or multiplexer pane from `(key, value)` environment pairs.
    ///
    /// Precedence follows nesting (innermost multiplexer wins over outer terminal):
    /// Zellij -> Kitty -> WezTerm -> iTerm2 -> Warp. (`tmux` is checked first from Claude Code's
    /// registry.)
    pub fn from_environ_pairs<'a, I>(vars: I) -> Option<Pane>
    where
        I: IntoIterator<Item = (&'a str, &'a str)>,
    {
        let mut zellij_session: Option<String> = None;
        let mut zellij_pane: Option<String> = None;
        let mut kitty_id: Option<u32> = None;
        let mut kitty_socket: Option<String> = None;
        let mut kitty_pid: Option<u32> = None;
        let mut wezterm_pane: Option<u32> = None;
        let mut wezterm_socket: Option<String> = None;
        let mut iterm_session: Option<String> = None;
        let mut warp_session: Option<String> = None;
        let mut in_tmux = false;
        let mut tmux_pane: Option<String> = None;

        for (k, v) in vars {
            if v.is_empty() {
                continue;
            }
            if k == "TMUX" {
                in_tmux = true;
            } else if k == "TMUX_PANE" {
                tmux_pane = Some(v.to_string());
            } else if k == "ZELLIJ_SESSION_NAME" {
                zellij_session = Some(v.to_string());
            } else if k == "ZELLIJ_PANE_ID" {
                zellij_pane = Some(v.to_string());
            } else if k == "KITTY_WINDOW_ID" {
                kitty_id = v.parse().ok();
            } else if k == "KITTY_LISTEN_ON" {
                kitty_socket = Some(v.to_string());
            } else if k == "KITTY_PID" {
                kitty_pid = v.parse().ok();
            } else if k == "WEZTERM_PANE" {
                wezterm_pane = v.parse().ok();
            } else if k == "WEZTERM_UNIX_SOCKET" {
                wezterm_socket = Some(v.to_string());
            } else if k == "ITERM_SESSION_ID" {
                // `ITERM_SESSION_ID` looks like `w0t0p0:UUID`; iTerm2's AppleScript `unique ID`
                // matches the UUID part after `:`.
                let id = match v.split_once(':') {
                    Some((_, rhs)) if !rhs.is_empty() => rhs,
                    Some(_) | None => v,
                };
                iterm_session = Some(id.to_string());
            } else if k == "WARP_TERMINAL_SESSION_UUID" && is_warp_session_id(v) {
                warp_session = Some(v.to_string());
            }
        }

        // tmux runs inside the terminal: its pane is the nearer one (review B-L9: a Codex inside
        // tmux inside kitty jumped to the kitty window).
        if let Some(pane) = tmux_pane.filter(|_| in_tmux) {
            return Some(Pane::Tmux { pane });
        }
        if let (Some(session), Some(pane_id)) = (zellij_session, zellij_pane) {
            return Some(Pane::Zellij { session, pane_id });
        }
        if let Some(id) = kitty_id {
            return Some(Pane::Kitty {
                window: KittyWindow {
                    id,
                    socket: kitty_socket,
                    kitty_pid,
                },
            });
        }
        if let Some(pane_id) = wezterm_pane {
            return Some(Pane::WezTerm {
                pane_id,
                socket: wezterm_socket,
            });
        }
        if let Some(session_id) = iterm_session {
            return Some(Pane::ITerm2 { session_id });
        }
        warp_session.map(|session_id| Pane::Warp { session_id })
    }

    /// Detect a pane from `/proc/<pid>/environ` bytes (NUL-separated `KEY=VALUE` entries).
    pub fn from_proc_environ(environ: &[u8]) -> Option<Pane> {
        let decoded: Vec<String> = environ
            .split(|b| *b == 0)
            .filter(|entry| !entry.is_empty())
            .map(|entry| String::from_utf8_lossy(entry).into_owned())
            .collect();
        Self::from_environ_pairs(decoded.iter().filter_map(|entry| entry.split_once('=')))
    }

    /// The terminal or multiplexer it lives in, for labels.
    pub fn terminal(&self) -> &'static str {
        match self {
            Pane::Tmux { .. } => "tmux",
            Pane::Zellij { .. } => "zellij",
            Pane::Kitty { .. } => "kitty",
            Pane::WezTerm { .. } => "wezterm",
            Pane::ITerm2 { .. } => "iTerm2",
            Pane::Warp { .. } => "Warp",
        }
    }

    /// Where it is, without the terminal's name.
    pub fn place(&self) -> String {
        match self {
            Pane::Tmux { pane } => pane.clone(),
            Pane::Zellij { session, pane_id } => format!("{session} ยท pane {pane_id}"),
            Pane::Kitty { window } => format!("window {}", window.id),
            Pane::WezTerm { pane_id, .. } => format!("pane {pane_id}"),
            Pane::ITerm2 { session_id } | Pane::Warp { session_id } => {
                format!("session {}", short(session_id))
            }
        }
    }
}

impl fmt::Display for Pane {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} {}", self.terminal(), self.place())
    }
}

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

    #[test]
    fn tmux_inside_a_terminal_is_the_pane() {
        let env = [
            ("KITTY_WINDOW_ID", "3"),
            ("TMUX", "/tmp/tmux-1/default,1,0"),
            ("TMUX_PANE", "%12"),
        ];
        assert_eq!(
            Pane::from_environ_pairs(env),
            Some(Pane::Tmux { pane: "%12".into() })
        );
        // A TMUX_PANE left over without TMUX (a shell started from tmux, then detached) is not.
        let env = [("KITTY_WINDOW_ID", "3"), ("TMUX_PANE", "%12")];
        assert!(matches!(
            Pane::from_environ_pairs(env),
            Some(Pane::Kitty { .. })
        ));
    }

    #[test]
    fn reads_kitty_window_socket_and_pid_from_proc_environ() {
        let env = b"PATH=/usr/bin\0KITTY_WINDOW_ID=7\0KITTY_LISTEN_ON=unix:@kitty-42\0KITTY_PID=42\0TERM=xterm-kitty\0";
        assert_eq!(
            Pane::from_proc_environ(env),
            Some(Pane::Kitty {
                window: KittyWindow {
                    id: 7,
                    socket: Some("unix:@kitty-42".into()),
                    kitty_pid: Some(42),
                },
            })
        );
    }

    #[test]
    fn kitty_without_listen_on_has_no_socket() {
        assert_eq!(
            Pane::from_proc_environ(b"KITTY_WINDOW_ID=3\0KITTY_PID=99\0"),
            Some(Pane::Kitty {
                window: KittyWindow {
                    id: 3,
                    socket: None,
                    kitty_pid: Some(99),
                },
            })
        );
    }

    #[test]
    fn reads_wezterm_pane_and_socket() {
        let env = b"WEZTERM_PANE=12\0WEZTERM_UNIX_SOCKET=/run/user/1000/wezterm/gui-sock-1\0";
        assert_eq!(
            Pane::from_proc_environ(env),
            Some(Pane::WezTerm {
                pane_id: 12,
                socket: Some("/run/user/1000/wezterm/gui-sock-1".into()),
            })
        );
        assert_eq!(
            Pane::from_proc_environ(b"WEZTERM_PANE=0\0"),
            Some(Pane::WezTerm {
                pane_id: 0,
                socket: None,
            })
        );
    }

    #[test]
    fn reads_zellij_session_and_pane() {
        let env = b"ZELLIJ_SESSION_NAME=dev\0ZELLIJ_PANE_ID=3\0";
        assert_eq!(
            Pane::from_proc_environ(env),
            Some(Pane::Zellij {
                session: "dev".into(),
                pane_id: "3".into(),
            })
        );
        // Incomplete Zellij env is ignored.
        assert_eq!(Pane::from_proc_environ(b"ZELLIJ_SESSION_NAME=dev\0"), None);
    }

    #[test]
    fn reads_iterm2_session_uuid() {
        let env = b"ITERM_SESSION_ID=w0t1p2:11223344-5566-7788-99AA-BBCCDDEEFF00\0";
        assert_eq!(
            Pane::from_proc_environ(env),
            Some(Pane::ITerm2 {
                session_id: "11223344-5566-7788-99AA-BBCCDDEEFF00".into(),
            })
        );
        // Bare ID without colon prefix is kept as-is.
        assert_eq!(
            Pane::from_proc_environ(b"ITERM_SESSION_ID=abcdef1234\0"),
            Some(Pane::ITerm2 {
                session_id: "abcdef1234".into(),
            })
        );
    }

    #[test]
    fn reads_warp_session_uuid() {
        let env = b"TERM_PROGRAM=WarpTerminal\0WARP_TERMINAL_SESSION_UUID=0123456789abcdef0123456789abcdef\0WARP_FOCUS_URL=warp://session/0123456789abcdef0123456789abcdef\0";
        assert_eq!(
            Pane::from_proc_environ(env),
            Some(Pane::Warp {
                session_id: "0123456789abcdef0123456789abcdef".into(),
            })
        );
        // Anything but a hex id is ignored, so it never reaches the focus URL.
        assert_eq!(
            Pane::from_proc_environ(b"WARP_TERMINAL_SESSION_UUID=abc/../x?y\0"),
            None
        );
    }

    #[test]
    fn multiplexer_wins_over_warp() {
        let zellij_in_warp = b"WARP_TERMINAL_SESSION_UUID=0123456789abcdef0123456789abcdef\0ZELLIJ_SESSION_NAME=work\0ZELLIJ_PANE_ID=5\0";
        assert_eq!(
            Pane::from_proc_environ(zellij_in_warp),
            Some(Pane::Zellij {
                session: "work".into(),
                pane_id: "5".into(),
            })
        );
    }

    #[test]
    fn multiplexer_wins_over_outer_terminal() {
        let zellij_in_kitty =
            b"KITTY_WINDOW_ID=7\0KITTY_LISTEN_ON=unix:@k\0ZELLIJ_SESSION_NAME=work\0ZELLIJ_PANE_ID=5\0";
        assert_eq!(
            Pane::from_proc_environ(zellij_in_kitty),
            Some(Pane::Zellij {
                session: "work".into(),
                pane_id: "5".into(),
            })
        );
        let zellij_in_wezterm =
            b"WEZTERM_PANE=2\0ZELLIJ_SESSION_NAME=work\0ZELLIJ_PANE_ID=terminal_9\0";
        assert_eq!(
            Pane::from_proc_environ(zellij_in_wezterm),
            Some(Pane::Zellij {
                session: "work".into(),
                pane_id: "terminal_9".into(),
            })
        );
    }

    #[test]
    fn outside_supported_terminals_there_is_no_pane() {
        assert_eq!(
            Pane::from_proc_environ(b"TERM=xterm-256color\0KITTY_LISTEN_ON=unix:@x\0"),
            None
        );
        assert_eq!(Pane::from_proc_environ(b"KITTY_WINDOW_ID=\0"), None);
        assert_eq!(Pane::from_proc_environ(b"WEZTERM_PANE=notanumber\0"), None);
    }

    #[test]
    fn labels() {
        assert_eq!(
            Pane::Tmux {
                pane: "0:@1.%2".into()
            }
            .to_string(),
            "tmux 0:@1.%2"
        );
        let kitty = Pane::Kitty {
            window: KittyWindow {
                id: 4,
                socket: None,
                kitty_pid: None,
            },
        };
        assert_eq!(kitty.to_string(), "kitty window 4");
        assert_eq!(
            (kitty.terminal(), kitty.place().as_str()),
            ("kitty", "window 4")
        );
        let wez = Pane::WezTerm {
            pane_id: 9,
            socket: None,
        };
        assert_eq!(wez.to_string(), "wezterm pane 9");
        let zellij = Pane::Zellij {
            session: "dev".into(),
            pane_id: "3".into(),
        };
        assert_eq!(zellij.to_string(), "zellij dev ยท pane 3");
        let iterm = Pane::ITerm2 {
            session_id: "11223344-5566-7788-99AA-BBCCDDEEFF00".into(),
        };
        assert_eq!(iterm.to_string(), "iTerm2 session 11223344");
        let warp = Pane::Warp {
            session_id: "0123456789abcdef0123456789abcdef".into(),
        };
        assert_eq!(warp.to_string(), "Warp session 01234567");
    }

    #[test]
    fn round_trips_through_the_fleet_summary() {
        let cases = [
            Pane::Tmux {
                pane: "0:@1.%2".into(),
            },
            Pane::Zellij {
                session: "dev".into(),
                pane_id: "3".into(),
            },
            Pane::Kitty {
                window: KittyWindow {
                    id: 4,
                    socket: Some("unix:@k".into()),
                    kitty_pid: Some(42),
                },
            },
            Pane::WezTerm {
                pane_id: 2,
                socket: Some("/tmp/wez.sock".into()),
            },
            Pane::ITerm2 {
                session_id: "11223344-5566".into(),
            },
            Pane::Warp {
                session_id: "0123456789abcdef0123456789abcdef".into(),
            },
        ];
        for pane in cases {
            let json = serde_json::to_string(&pane).unwrap();
            assert_eq!(serde_json::from_str::<Pane>(&json).unwrap(), pane);
        }

        let without_pid = r#"{"kind":"kitty","window":{"id":4,"socket":"unix:@k"}}"#;
        assert_eq!(
            serde_json::from_str::<Pane>(without_pid).unwrap(),
            Pane::Kitty {
                window: KittyWindow {
                    id: 4,
                    socket: Some("unix:@k".into()),
                    kitty_pid: None,
                }
            }
        );
    }
}