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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! How an agent's state is decided: a pure function of what the index, the session registry and
//! the clock say, tested by table.

use chrono::{DateTime, Duration, Utc};
use serde::Serialize;

use crate::claude::registry::{LiveSession, SessionStatus};
use crate::index::from_ms;
use crate::index::live::OpenTool;

#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum State {
    /// Blocked on the human (permission, question, dialog).
    NeedsYou(String),
    /// Running a tool.
    Tool {
        name: String,
        summary: String,
    },
    /// Waiting for the model.
    Thinking,
    /// Idle main thread with background agents still running.
    WaitingAgents(usize),
    Idle,
    /// Unfinished, no transcript activity for a while.
    Stalled,
    Done,
    /// The Claude Code process is gone.
    Gone,
}

impl State {
    pub fn is_active(&self) -> bool {
        matches!(
            self,
            State::Tool { .. } | State::Thinking | State::WaitingAgents(_)
        )
    }
}

pub struct Options {
    pub stall_minutes: i64,
    /// Finished agents stay on screen this long.
    pub linger_minutes: i64,
}

impl Default for Options {
    fn default() -> Self {
        Options {
            stall_minutes: 20,
            linger_minutes: 3,
        }
    }
}

/// What decides an agent's state.
#[derive(Debug, Clone, Copy)]
pub struct StateInputs<'a> {
    /// The main thread of a session (else a subagent).
    pub is_main: bool,
    /// Subagents: the transcript says it finished.
    pub finished: bool,
    /// The running Claude Code session the agent belongs to (its registry entry, process alive).
    pub session: Option<&'a LiveSession>,
    /// The newest tool call since the agent's last request that has not finished.
    pub tool: Option<&'a OpenTool>,
    /// The agent's last activity (transcript, request or tool result).
    pub last: DateTime<Utc>,
}

/// The state of an agent and since when, or `None` when it is no longer shown.
///
/// - Main thread: exited (`Gone`) without a running session, else what the session registry says
///   (needs you, a tool or thinking while busy, idle).
/// - Subagent: `Done` once finished; `Gone` when its session exited before it finished (it can
///   not continue, so it is not "stalled"); `Stalled` when its session runs but the agent was
///   silent longer than `stall_minutes`; else a tool or thinking.
/// - Hidden: done agents after `linger_minutes`, stalled ones after a day, and subagents of an
///   exited session as long after as their main thread is (`stall_minutes`, at least 30).
pub fn agent_state(
    i: &StateInputs,
    opts: &Options,
    now: DateTime<Utc>,
) -> Option<(State, DateTime<Utc>)> {
    let silent = now - i.last;
    let status_since = || i.session.and_then(|l| l.status_since).unwrap_or(i.last);
    let tool = |t: &OpenTool| {
        (
            State::Tool {
                name: t.name.clone(),
                summary: t.summary.clone().unwrap_or_default(),
            },
            from_ms(t.ts),
        )
    };
    let (state, since) = if i.is_main {
        match i.session.map(|l| &l.status) {
            None => (State::Gone, i.last),
            Some(SessionStatus::Waiting(w)) => (State::NeedsYou(w.clone()), status_since()),
            Some(SessionStatus::Busy) => match i.tool {
                Some(t) => tool(t),
                None => (State::Thinking, status_since()),
            },
            Some(SessionStatus::Idle | SessionStatus::Other(_)) => (State::Idle, status_since()),
        }
    } else if i.finished {
        (State::Done, i.last)
    } else if i.session.is_none() {
        (State::Gone, i.last)
    } else if silent > Duration::minutes(opts.stall_minutes) {
        (State::Stalled, i.last)
    } else {
        match i.tool {
            Some(t) => tool(t),
            None => (State::Thinking, i.last),
        }
    };
    let hidden = match &state {
        State::Done => silent > Duration::minutes(opts.linger_minutes),
        State::Stalled => silent > Duration::hours(24),
        State::Gone => !i.is_main && silent > Duration::minutes(opts.stall_minutes.max(30)),
        State::NeedsYou(_)
        | State::Tool { .. }
        | State::Thinking
        | State::WaitingAgents(_)
        | State::Idle => false,
    };
    (!hidden).then_some((state, since))
}

/// One agent as the waiting rule sees it: whether it is a session's main thread, its session
/// (seat and session id), and its state.
pub struct Member<'a> {
    pub main: bool,
    pub session: (&'a str, &'a str),
    pub state: &'a State,
}

/// For each agent, the number of its session's background agents at work when it is an idle
/// main thread (it then shows `WaitingAgents(n)`), else 0. Here, with the other state rules
/// (review B-M23: it lived inside `live::build`).
pub fn waiting_on_agents(agents: &[Member]) -> Vec<usize> {
    agents
        .iter()
        .map(|m| {
            if !m.main || *m.state != State::Idle {
                return 0;
            }
            agents
                .iter()
                .filter(|a| !a.main && a.session == m.session && a.state.is_active())
                .count()
        })
        .collect()
}

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

    #[test]
    fn an_idle_main_thread_waits_on_its_sessions_agents_at_work() {
        fn m<'a>(main: bool, s: &'a str, state: &'a State) -> Member<'a> {
            Member {
                main,
                session: ("seat", s),
                state,
            }
        }
        let (busy, idle) = (&State::Thinking, &State::Idle);
        let agents = [
            m(true, "s1", idle),
            m(false, "s1", busy),
            m(false, "s1", busy),
            m(false, "s1", idle),
            m(true, "s2", idle),
            m(false, "s3", busy),
            m(true, "s3", busy),
        ];
        // s1's main waits on two agents; s2 has none at work; s3's main is busy itself.
        assert_eq!(waiting_on_agents(&agents), [2, 0, 0, 0, 0, 0, 0]);
    }

    fn t(min: i64) -> DateTime<Utc> {
        DateTime::parse_from_rfc3339("2026-11-20T12:00:00Z")
            .unwrap()
            .with_timezone(&Utc)
            + Duration::minutes(min)
    }

    fn session(status: SessionStatus, since: Option<i64>) -> LiveSession {
        LiveSession {
            seat: "work".into(),
            harness: crate::core::harness::Harness::Claude,
            pid: 4242,
            session_id: "s1".into(),
            cwd: "/w/proj".into(),
            name: None,
            status,
            pane: None,
            version: None,
            kind: None,
            started_at: t(-120),
            status_since: since.map(t),
        }
    }

    fn bash(at: i64) -> OpenTool {
        OpenTool {
            name: "Bash".into(),
            summary: Some("cargo test".into()),
            ts: t(at).timestamp_millis(),
        }
    }

    fn running_bash() -> State {
        State::Tool {
            name: "Bash".into(),
            summary: "cargo test".into(),
        }
    }

    #[test]
    fn agent_states_by_table() {
        // Defaults: stalled after 20 min of silence, done agents linger 3 min. Now = t(0).
        let opts = Options::default();
        let busy = session(SessionStatus::Busy, Some(-5));
        let idle = session(SessionStatus::Idle, Some(-7));
        let asks = session(SessionStatus::Waiting("permission".into()), Some(-2));
        let other = session(SessionStatus::Other("compacting".into()), None);
        let tool = bash(-1);
        // (case, main, finished, session, tool, last activity, expected)
        type Case<'a> = (
            &'a str,
            bool,
            bool,
            Option<&'a LiveSession>,
            Option<&'a OpenTool>,
            i64,
            Option<(State, i64)>,
        );
        let cases: Vec<Case> = vec![
            // Main threads.
            (
                "main, session exited",
                true,
                false,
                None,
                None,
                -10,
                Some((State::Gone, -10)),
            ),
            (
                "main, needs you",
                true,
                false,
                Some(&asks),
                None,
                -10,
                Some((State::NeedsYou("permission".into()), -2)),
            ),
            (
                "main, busy in a tool",
                true,
                false,
                Some(&busy),
                Some(&tool),
                -1,
                Some((running_bash(), -1)),
            ),
            (
                "main, busy thinking",
                true,
                false,
                Some(&busy),
                None,
                -1,
                Some((State::Thinking, -5)),
            ),
            (
                "main, idle",
                true,
                false,
                Some(&idle),
                None,
                -30,
                Some((State::Idle, -7)),
            ),
            (
                "main, other status, no time",
                true,
                false,
                Some(&other),
                None,
                -4,
                Some((State::Idle, -4)),
            ),
            // Subagents of a running session.
            (
                "sub, in a tool",
                false,
                false,
                Some(&busy),
                Some(&tool),
                -1,
                Some((running_bash(), -1)),
            ),
            (
                "sub, thinking",
                false,
                false,
                Some(&busy),
                None,
                -3,
                Some((State::Thinking, -3)),
            ),
            (
                "sub, silent 20 min is not yet stalled",
                false,
                false,
                Some(&busy),
                None,
                -20,
                Some((State::Thinking, -20)),
            ),
            (
                "sub, silent 21 min",
                false,
                false,
                Some(&busy),
                None,
                -21,
                Some((State::Stalled, -21)),
            ),
            (
                "sub, stalled for a day",
                false,
                false,
                Some(&busy),
                None,
                -24 * 60,
                Some((State::Stalled, -24 * 60)),
            ),
            (
                "sub, stalled over a day: hidden",
                false,
                false,
                Some(&busy),
                None,
                -24 * 60 - 1,
                None,
            ),
            (
                "sub, done 3 min ago",
                false,
                true,
                Some(&busy),
                None,
                -3,
                Some((State::Done, -3)),
            ),
            (
                "sub, done 4 min ago: hidden",
                false,
                true,
                Some(&busy),
                None,
                -4,
                None,
            ),
            // Subagents of an exited session: gone with it, never stalled (review finding).
            (
                "sub, session exited",
                false,
                false,
                None,
                None,
                -2,
                Some((State::Gone, -2)),
            ),
            (
                "sub, session exited, a tool was open",
                false,
                false,
                None,
                Some(&tool),
                -1,
                Some((State::Gone, -1)),
            ),
            (
                "sub, session exited 30 min ago",
                false,
                false,
                None,
                None,
                -30,
                Some((State::Gone, -30)),
            ),
            (
                "sub, session exited 31 min ago: hidden",
                false,
                false,
                None,
                None,
                -31,
                None,
            ),
            (
                "sub, session exited 3 h ago: hidden, not stalled",
                false,
                false,
                None,
                None,
                -180,
                None,
            ),
            (
                "sub, finished, session exited",
                false,
                true,
                None,
                None,
                -1,
                Some((State::Done, -1)),
            ),
        ];
        for (name, is_main, finished, session, tool, last, want) in cases {
            let got = agent_state(
                &StateInputs {
                    is_main,
                    finished,
                    session,
                    tool,
                    last: t(last),
                },
                &opts,
                t(0),
            );
            assert_eq!(got, want.map(|(s, at)| (s, t(at))), "{name}");
        }
    }
}