swarmy 0.2.12

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
//! End-to-end scenario tests for mixed Claude Code + Google Antigravity (`agy`) indices:
//! ingestion, unified projects, `Dim::Harness` / `Filter::harness` across raw and compacted
//! rollups, subagent parent-child hierarchy, unpriced preview models, and cross-harness edit
//! conflict detection. All paths and accounts are synthetic.

#[cfg(test)]
mod tests {
    use std::io::Write as _;
    use std::path::Path;

    use chrono::{DateTime, Duration, Utc};
    use rusqlite::Connection;

    use crate::claude::project::Resolver;
    use crate::claude::seats::Seat;
    use crate::core::harness::Harness;
    use crate::core::period::Span;
    use crate::core::pricing::PriceBook;
    use crate::index::compact::CompactOpts;
    use crate::index::live::AgentKey;
    use crate::index::query::{Dim, Filter};
    use crate::index::scenarios::{account, assistant, at, owner};
    use crate::index::{Index, SeatInfo};

    fn write_lines(path: &Path, lines: &[String]) {
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        let mut f = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(path)
            .unwrap();
        for l in lines {
            writeln!(f, "{l}").unwrap();
        }
    }

    fn agy_planner(
        step: u64,
        ts: &str,
        model: &str,
        input: u64,
        cread: u64,
        output: u64,
        tools: &[(&str, serde_json::Value)],
    ) -> String {
        let calls: Vec<serde_json::Value> = tools
            .iter()
            .map(|(name, args)| {
                serde_json::json!({
                    "name": name,
                    "args": args,
                })
            })
            .collect();
        serde_json::json!({
            "step_index": step,
            "type": "PLANNER_RESPONSE",
            "created_at": ts,
            "model": model,
            "input_tokens": input,
            "cache_read_tokens": cread,
            "output_tokens": output,
            "tool_calls": calls,
        })
        .to_string()
    }

    fn agy_generic(step: u64, ts: &str, completed_at: &str) -> String {
        serde_json::json!({
            "step_index": step,
            "type": "GENERIC",
            "created_at": ts,
            "content": format!("Tool finished.\nCompleted At: {completed_at}"),
        })
        .to_string()
    }

    fn agy_user(step: u64, ts: &str, text: &str) -> String {
        serde_json::json!({
            "step_index": step,
            "type": "USER_INPUT",
            "created_at": ts,
            "content": text,
        })
        .to_string()
    }

    fn write_summaries_db(seat_dir: &Path, rows: &[(&str, &str, &str, &str, &str, i64, &str)]) {
        let db = seat_dir.join("conversation_summaries.db");
        let conn = Connection::open(&db).unwrap();
        conn.execute_batch(
            "CREATE TABLE conversation_summaries (
               conversation_id TEXT PRIMARY KEY,
               title TEXT NOT NULL DEFAULT '',
               workspace_uris TEXT NOT NULL DEFAULT '',
               status TEXT NOT NULL DEFAULT '',
               parent_conversation_id TEXT NOT NULL DEFAULT '',
               nesting_depth INTEGER NOT NULL DEFAULT 0,
               agent_name TEXT NOT NULL DEFAULT '',
               not_fully_idle NUMERIC NOT NULL DEFAULT 0,
               killed NUMERIC NOT NULL DEFAULT 0,
               last_modified_time TEXT NOT NULL DEFAULT ''
             );",
        )
        .unwrap();
        for (cid, title, uri, status, parent, depth, agent_name) in rows {
            conn.execute(
                "INSERT INTO conversation_summaries VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, 0, 0, '2026-10-01 12:00:00+00:00')",
                rusqlite::params![cid, title, uri, status, parent, depth, agent_name],
            )
            .unwrap();
        }
    }

    fn mixed_world() -> (tempfile::TempDir, Vec<Seat>, Vec<SeatInfo>, Index) {
        let dir = tempfile::tempdir().unwrap();
        let cc_dir = dir.path().join("claude-work");
        let agy_dir = dir.path().join("agy-main");
        std::fs::create_dir_all(&cc_dir).unwrap();
        std::fs::create_dir_all(&agy_dir).unwrap();

        let cc = Seat::new("work", cc_dir.clone(), cc_dir.join(".claude.json"), false);
        let agy = Seat::agy("agy", agy_dir.clone());

        // Claude seat: 1 request on /w/shop with claude-sonnet-5-5 ($2 input, $10 output per MTok)
        // 100k input ($0.20) + 10k output ($0.10) = $0.30.
        // Also edits /w/shop/src/lib.rs at a recent timestamp for conflict test.
        // Both recent edits share one UTC hour inside the last 24h (never straddling an hour mark).
        let hour_base =
            chrono::DateTime::from_timestamp((Utc::now().timestamp() / 3600 - 1) * 3600 + 60, 0)
                .unwrap();
        let cc_file = cc_dir.join("projects").join("p").join("cc-sess.jsonl");
        let recent_ts = hour_base.to_rfc3339();
        write_lines(
            &cc_file,
            &[
                owner("acc-cc"),
                assistant(
                    "cc-m1",
                    "2026-10-01T10:00:00Z",
                    "claude-sonnet-5-5",
                    "/w/shop",
                    (100_000, 10_000),
                    &[],
                ),
                serde_json::json!({
                    "type": "assistant",
                    "timestamp": recent_ts,
                    "cwd": "/w/shop",
                    "sessionId": "cc-sess",
                    "message": {
                        "id": "cc-m2",
                        "model": "claude-sonnet-5-5",
                        "content": [{
                            "type": "tool_use",
                            "id": "cc-t1",
                            "name": "Edit",
                            "input": {"file_path": "/w/shop/src/lib.rs"}
                        }],
                        "usage": {"input_tokens": 1000, "output_tokens": 100,
                                  "cache_creation_input_tokens": 0, "cache_read_input_tokens": 0}
                    }
                })
                .to_string(),
            ],
        );

        // Agy seat:
        // - root conversation `c-root` on /w/shop:
        //   * 1 request on gemini-2.5-pro ($1.25 input, $0.125 cread, $10.00 output):
        //     100k input ($0.125) + 80k cread at $0.125/MTok ($0.010) + 20k output ($0.200) = $0.335
        //   * 1 recent edit_file on /w/shop/src/lib.rs (triggers cross-harness conflict!)
        // - child subagent `c-sub` (parent = c-root) on `gemini-4-neon-high`, unpriced on 2026-10-01
        //   (its price applies from 2026-11-18):
        //   * 1 request -> cost = 0, priced = 0, effort = "high", model = "gemini-4-neon"
        write_summaries_db(
            &agy_dir,
            &[
                (
                    "c-root",
                    "Refactor checkout",
                    r#"["file:///w/shop"]"#,
                    "CASCADE_RUN_STATUS_IDLE",
                    "",
                    0,
                    "",
                ),
                (
                    "c-sub",
                    "Codebase check",
                    r#"["file:///w/shop"]"#,
                    "CASCADE_RUN_STATUS_IDLE",
                    "c-root",
                    1,
                    "research",
                ),
            ],
        );

        let root_path = agy_dir
            .join("brain")
            .join("c-root")
            .join(".system_generated")
            .join("logs")
            .join("transcript.jsonl");
        let agy_recent_ts = (hour_base + Duration::seconds(30)).to_rfc3339();
        let agy_recent_done = (hour_base + Duration::seconds(45)).to_rfc3339();
        write_lines(
            &root_path,
            &[
                agy_user(0, "2026-10-01T10:05:00Z", "Refactor the checkout handler"),
                agy_planner(
                    1,
                    "2026-10-01T10:05:10Z",
                    "gemini-2.5-pro",
                    100_000,
                    80_000,
                    20_000,
                    &[(
                        "edit_file",
                        serde_json::json!({"Path": "/w/shop/src/lib.rs"}),
                    )],
                ),
                agy_generic(2, "2026-10-01T10:05:12Z", "2026-10-01T10:05:15Z"),
                agy_planner(
                    3,
                    &agy_recent_ts,
                    "gemini-2.5-pro",
                    1000,
                    0,
                    100,
                    &[(
                        "edit_file",
                        serde_json::json!({"Path": "/w/shop/src/lib.rs"}),
                    )],
                ),
                agy_generic(4, &agy_recent_ts, &agy_recent_done),
            ],
        );

        let sub_path = agy_dir
            .join("brain")
            .join("c-sub")
            .join(".system_generated")
            .join("logs")
            .join("transcript.jsonl");
        write_lines(
            &sub_path,
            &[agy_planner(
                0,
                "2026-10-01T10:06:00Z",
                "gemini-4-neon-high",
                50_000,
                10_000,
                2_000,
                &[],
            )],
        );

        let info = vec![
            SeatInfo {
                seat: cc.id.clone(),
                label: cc.label.clone(),
                account: Some(account("acc-cc", "2026-10-01T00:00:00Z")),
                switched_at: None,
                utilization: None,
            },
            SeatInfo {
                seat: agy.id.clone(),
                label: agy.label.clone(),
                account: Some(account("agy:sub-1", "2026-10-01T00:00:00Z")),
                switched_at: None,
                utilization: None,
            },
        ];

        let mut idx = Index::open(&dir.path().join("index.sqlite")).unwrap();
        let seats = vec![cc, agy];
        let mut res = Resolver::new(Vec::new());
        idx.refresh(&seats, &info, &mut res, &PriceBook::builtin(), &mut |_| {})
            .unwrap();
        (dir, seats, info, idx)
    }

    #[test]
    fn mixed_claude_and_agy_unified_projects_harnesses_subagents_and_compaction() {
        let (_dir, seats, _info, mut idx) = mixed_world();
        let agy_seat = &seats[1];

        // 1. Historical day span (2026-10-01) isolates the hand-computed rows from the recent conflict rows.
        let oct1 = Span {
            start: at("2026-10-01T00:00:00Z"),
            end: at("2026-10-02T00:00:00Z"),
        };

        // Unified project spend: Claude ($0.30) + Agy ($0.335) = $0.635 under one project "shop".
        let by_proj = idx
            .spend_by(Dim::Project, oct1, &Filter::default())
            .unwrap();
        assert_eq!(by_proj.len(), 1);
        assert_eq!(by_proj[0].key, "/w/shop");
        assert_eq!(by_proj[0].label, "shop");
        assert!((by_proj[0].cost - 0.635).abs() < 1e-6, "{by_proj:?}");
        assert_eq!(by_proj[0].requests, 3);
        assert_eq!(by_proj[0].unpriced, 1);

        // Dim::Harness splits cleanly into "agy" ($0.335, 2 reqs, 1 unpriced) and "claude" ($0.30, 1 req).
        let by_harness_before = idx
            .spend_by(Dim::Harness, oct1, &Filter::default())
            .unwrap();
        assert_eq!(by_harness_before.len(), 2);
        let agy_row = by_harness_before.iter().find(|r| r.key == "agy").unwrap();
        let cc_row = by_harness_before
            .iter()
            .find(|r| r.key == "claude")
            .unwrap();
        assert!((agy_row.cost - 0.335).abs() < 1e-6, "{agy_row:?}");
        assert_eq!(agy_row.requests, 2);
        assert_eq!(agy_row.unpriced, 1);
        assert!((cc_row.cost - 0.30).abs() < 1e-6, "{cc_row:?}");
        assert_eq!(cc_row.requests, 1);
        assert_eq!(cc_row.unpriced, 0);

        // Filter::harness narrows totals cleanly.
        let f_agy = Filter {
            harness: Some(Harness::Agy),
            ..Default::default()
        };
        let agy_tot_before = idx.total(oct1, &f_agy).unwrap();
        assert!((agy_tot_before.cost - 0.335).abs() < 1e-6);
        assert_eq!(agy_tot_before.requests, 2);
        assert_eq!(agy_tot_before.unpriced, 1);

        // Subagent hierarchy: c-sub is indexed under session_id = "c-root", agent_id = "c-sub",
        // with model = "gemini-4-neon", effort = "high", priced = Some(0).
        let sub_key = AgentKey {
            seat: &agy_seat.id,
            session_id: &crate::core::ids::SessionId::new("c-root"),
            agent_id: "c-sub",
        };
        let last_sub = idx.last_request(sub_key).unwrap().unwrap();
        assert_eq!(last_sub.model.as_deref(), Some("gemini-4-neon"));
        assert_eq!(last_sub.effort.as_deref(), Some("high"));
        assert_eq!(last_sub.priced, Some(0));

        // Cross-harness file edit conflict: Claude (`Edit`) and Agy (`edit_file`) both edited
        // `/w/shop/src/lib.rs` in the last 30 minutes -> detected in efficiency().conflicts!
        let all_span = Span {
            start: DateTime::<Utc>::UNIX_EPOCH,
            end: Utc::now() + Duration::hours(1),
        };
        let eff = idx.efficiency(all_span).unwrap();
        assert!(
            eff.conflicts
                .iter()
                .any(|(path, n)| path == "/w/shop/src/lib.rs" && *n >= 2),
            "expected cross-harness edit conflict, got {:?}",
            eff.conflicts
        );

        // Compaction proof: compact everything before 2026-10-15 into rollups; Dim::Harness,
        // Dim::Project, and Filter::harness must return identical numbers post-compaction!
        let compact_now = at("2026-11-01T00:00:00Z");
        idx.compact(&CompactOpts {
            keep_raw_days: 14,
            now: compact_now,
            prices: &PriceBook::builtin(),
            seats: &seats,
            max_chunks: None,
        })
        .unwrap();

        let by_harness_after = idx
            .spend_by(Dim::Harness, oct1, &Filter::default())
            .unwrap();
        assert_eq!(by_harness_before.len(), by_harness_after.len());
        for (b, a) in by_harness_before.iter().zip(by_harness_after.iter()) {
            assert_eq!(b.key, a.key);
            assert!((b.cost - a.cost).abs() < 1e-9);
            assert_eq!(b.requests, a.requests);
            assert_eq!(b.unpriced, a.unpriced);
        }
        let agy_tot_after = idx.total(oct1, &f_agy).unwrap();
        assert!((agy_tot_before.cost - agy_tot_after.cost).abs() < 1e-9);
        assert_eq!(agy_tot_before.requests, agy_tot_after.requests);
        assert_eq!(agy_tot_before.unpriced, agy_tot_after.unpriced);
    }

    /// An Antigravity subagent's state lives in `conversation_summaries.db`, not in its
    /// transcript: one read while running that goes idle (or is killed) without another line
    /// is finished by the next refresh, hot or full, from what the listing read anyway.
    #[test]
    fn an_agy_subagent_that_goes_idle_without_a_new_line_is_finished() {
        let dir = tempfile::tempdir().unwrap();
        let agy_dir = dir.path().join("agy-main");
        std::fs::create_dir_all(&agy_dir).unwrap();
        let agy = Seat::agy("agy", agy_dir.clone());
        let running = "CASCADE_RUN_STATUS_RUNNING";
        let idle = "CASCADE_RUN_STATUS_IDLE";
        let uri = r#"["file:///w/shop"]"#;
        write_summaries_db(
            &agy_dir,
            &[
                ("c-root", "Task", uri, running, "", 0, ""),
                ("c-idle", "Check", uri, running, "c-root", 1, "research"),
                ("c-kill", "Probe", uri, running, "c-root", 1, "research"),
                ("c-busy", "Build", uri, running, "c-root", 1, "research"),
            ],
        );
        let transcript = |cid: &str| {
            agy_dir
                .join("brain")
                .join(cid)
                .join(".system_generated/logs/transcript.jsonl")
        };
        write_lines(
            &transcript("c-root"),
            &[agy_user(0, "2026-10-01T10:05:00Z", "Do the task")],
        );
        for cid in ["c-idle", "c-kill", "c-busy"] {
            write_lines(
                &transcript(cid),
                &[agy_planner(
                    0,
                    "2026-10-01T10:06:00Z",
                    "gemini-2.5-pro",
                    1000,
                    0,
                    100,
                    &[],
                )],
            );
        }
        let seats = vec![agy];
        let mut idx = Index::open(&dir.path().join("index.sqlite")).unwrap();
        let mut res = Resolver::new(Vec::new());
        let prices = PriceBook::builtin();
        idx.refresh(&seats, &[], &mut res, &prices, &mut |_| {})
            .unwrap();
        let finished = |idx: &Index| {
            crate::index::scenarios::dump(
                idx,
                "SELECT agent_id, finished FROM agents WHERE agent_id != '' ORDER BY 1",
            )
        };
        assert_eq!(finished(&idx), ["c-busy|0", "c-idle|0", "c-kill|0"]);
        let set = |sql: &str| {
            Connection::open(agy_dir.join("conversation_summaries.db"))
                .unwrap()
                .execute(sql, [])
                .unwrap();
        };

        // agy marks one idle and kills another; no transcript gets a line. The dashboard's hot
        // refresh of the running session finishes both.
        set(&format!(
            "UPDATE conversation_summaries SET status = '{idle}' WHERE conversation_id = 'c-idle'"
        ));
        set("UPDATE conversation_summaries SET killed = 1 WHERE conversation_id = 'c-kill'");
        let live = [(seats[0].id.clone(), "c-root".to_string())];
        idx.refresh_hot(&seats, &live, &[], &mut res, &prices)
            .unwrap();
        assert_eq!(finished(&idx), ["c-busy|0", "c-idle|1", "c-kill|1"]);

        // A full refresh does the same for the last one.
        set(&format!(
            "UPDATE conversation_summaries SET status = '{idle}' WHERE conversation_id = 'c-busy'"
        ));
        idx.refresh(&seats, &[], &mut res, &prices, &mut |_| {})
            .unwrap();
        assert_eq!(finished(&idx), ["c-busy|1", "c-idle|1", "c-kill|1"]);
    }
}