bosshogg 2026.5.3

BossHogg — the agent-first PostHog CLI. Feature flags, HogQL queries, insights, dashboards, cohorts, persons, events, experiments, and more — from the terminal or from a Claude Code / Cursor / other coding-agent loop. Ships with a Claude Code skill (~200 idle tokens) that teaches models how to use it.
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
// src/commands/cohort.rs
use clap::{Args, Subcommand};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::path::PathBuf;

use crate::client::Client;
use crate::commands::context::CommandContext;
use crate::commands::util::read_json_file;
use crate::error::{BosshoggError, Result};
use crate::output;

// ── Typed struct ────────────────────────────────────────────────────────────

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Cohort {
    pub id: i64,
    pub name: String,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub groups: Option<Value>, // fluid — legacy
    #[serde(default)]
    pub deleted: bool,
    #[serde(default)]
    pub filters: Value, // fluid
    #[serde(default)]
    pub query: Option<Value>,
    #[serde(default)]
    pub is_calculating: Option<bool>,
    #[serde(default)]
    pub created_by: Option<Value>,
    #[serde(default)]
    pub created_at: Option<String>,
    #[serde(default)]
    pub last_calculation: Option<String>,
    #[serde(default)]
    pub errors_calculating: Option<i32>,
    #[serde(default)]
    pub count: Option<i64>, // member count
    #[serde(default)]
    pub is_static: Option<bool>,
    #[serde(default)]
    pub experiment_set: Option<Vec<i64>>,
}

// ── Clap tree ────────────────────────────────────────────────────────────────

#[derive(Args, Debug)]
pub struct CohortArgs {
    #[command(subcommand)]
    pub command: CohortCommand,
}

#[derive(Subcommand, Debug)]
pub enum CohortCommand {
    /// List cohorts with optional search filter.
    List {
        #[arg(long)]
        search: Option<String>,
        /// Cap results at N rows (default: fetch all pages).
        #[arg(long)]
        limit: Option<usize>,
    },
    /// Get a single cohort by numeric id or exact name.
    Get {
        /// Cohort numeric id or exact name string.
        identifier: String,
    },
    /// Create a new cohort.
    Create {
        #[arg(long)]
        name: String,
        /// Path to a JSON file containing the filters object.
        #[arg(long)]
        filters_file: Option<PathBuf>,
        /// Create a static cohort (mutually exclusive with --dynamic).
        #[arg(long, conflicts_with = "dynamic")]
        r#static: bool,
        /// Create a dynamic cohort (default).
        #[arg(long, conflicts_with = "static")]
        dynamic: bool,
    },
    /// Update cohort fields.
    Update {
        id: i64,
        #[arg(long)]
        name: Option<String>,
        /// Path to a JSON file containing updated filters.
        #[arg(long)]
        filters_file: Option<PathBuf>,
    },
    /// Soft-delete a cohort (PATCH deleted=true).
    Delete { id: i64 },
    /// List members (persons) of a cohort (paginated).
    Members { id: i64 },
    /// Add a person to a static cohort by person UUID.
    AddPerson {
        id: i64,
        /// Person UUID (obtain from `bosshogg person` in M4).
        #[arg(long, conflicts_with = "distinct_id")]
        person_id: Option<String>,
        /// Distinct ID (not yet supported — use --person-id with a UUID instead).
        #[arg(long, conflicts_with = "person_id")]
        distinct_id: Option<String>,
    },
    /// Remove a person from a static cohort by person UUID.
    RemovePerson {
        id: i64,
        /// Person UUID (obtain from `bosshogg person` in M4).
        #[arg(long, conflicts_with = "distinct_id")]
        person_id: Option<String>,
        /// Distinct ID (not yet supported — use --person-id with a UUID instead).
        #[arg(long, conflicts_with = "person_id")]
        distinct_id: Option<String>,
    },
    /// View calculation history for a cohort.
    CalculationHistory { id: i64 },
    /// View the activity log for a cohort.
    Activity { id: i64 },
}

// ── Dispatch ─────────────────────────────────────────────────────────────────

pub async fn execute(args: CohortArgs, cx: &CommandContext) -> Result<()> {
    match args.command {
        CohortCommand::List { search, limit } => list_cohorts(cx, search, limit).await,
        CohortCommand::Get { identifier } => get_cohort_by_identifier(cx, &identifier).await,
        CohortCommand::Create {
            name,
            filters_file,
            r#static: is_static,
            dynamic: _,
        } => create_cohort(cx, name, filters_file, is_static).await,
        CohortCommand::Update {
            id,
            name,
            filters_file,
        } => update_cohort(cx, id, name, filters_file).await,
        CohortCommand::Delete { id } => delete_cohort(cx, id).await,
        CohortCommand::Members { id } => members_cohort(cx, id).await,
        CohortCommand::AddPerson {
            id,
            person_id,
            distinct_id,
        } => add_person(cx, id, person_id, distinct_id).await,
        CohortCommand::RemovePerson {
            id,
            person_id,
            distinct_id,
        } => remove_person(cx, id, person_id, distinct_id).await,
        CohortCommand::CalculationHistory { id } => calculation_history(cx, id).await,
        CohortCommand::Activity { id } => activity_cohort(cx, id).await,
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn project_id_required(client: &Client) -> Result<&str> {
    client.project_id().ok_or_else(|| {
        BosshoggError::Config("no project_id configured; run `bosshogg configure`".into())
    })
}

// ── list ──────────────────────────────────────────────────────────────────────

#[derive(Serialize)]
struct ListOutput {
    count: usize,
    results: Vec<Cohort>,
}

async fn list_cohorts(
    cx: &CommandContext,
    search: Option<String>,
    limit: Option<usize>,
) -> Result<()> {
    let client = &cx.client;
    let project_id = project_id_required(client)?;

    let query = if let Some(s) = search {
        format!("?search={}", urlencoding::encode(&s))
    } else {
        String::new()
    };

    let path = format!("/api/projects/{project_id}/cohorts/{query}");
    let results: Vec<Cohort> = client.get_paginated(&path, limit).await?;

    if cx.json_mode {
        output::print_json(&ListOutput {
            count: results.len(),
            results,
        });
    } else {
        let headers = &["ID", "NAME", "COUNT", "IS_STATIC", "CREATED_AT"];
        let rows: Vec<Vec<String>> = results
            .iter()
            .map(|c| {
                vec![
                    c.id.to_string(),
                    c.name.clone(),
                    c.count.map(|n| n.to_string()).unwrap_or_else(|| "-".into()),
                    c.is_static
                        .map(|s| s.to_string())
                        .unwrap_or_else(|| "-".into()),
                    c.created_at.clone().unwrap_or_default(),
                ]
            })
            .collect();
        output::table::print(headers, &rows);
    }
    Ok(())
}

// ── get ───────────────────────────────────────────────────────────────────────

/// Resolve a cohort by numeric id or by exact name (list-then-filter fallback),
/// mirroring `bosshogg project get` name-resolution UX.
async fn resolve_cohort_by_identifier(client: &Client, identifier: &str) -> Result<Cohort> {
    let project_id = project_id_required(client)?;

    // Numeric id → direct GET
    if let Ok(id) = identifier.parse::<i64>() {
        let cohort: Cohort = client
            .get(&format!("/api/projects/{project_id}/cohorts/{id}/"))
            .await?;
        return Ok(cohort);
    }

    // Name → list with ?search=<name> and filter by exact name
    let query = format!("?search={}", urlencoding::encode(identifier));
    let path = format!("/api/projects/{project_id}/cohorts/{query}");
    let candidates: Vec<Cohort> = client.get_paginated(&path, None).await?;
    candidates
        .into_iter()
        .find(|c| c.name == identifier)
        .ok_or_else(|| BosshoggError::NotFound(format!("cohort '{identifier}'")))
}

async fn get_cohort_by_identifier(cx: &CommandContext, identifier: &str) -> Result<()> {
    let cohort = resolve_cohort_by_identifier(&cx.client, identifier).await?;
    print_cohort(&cohort, cx.json_mode);
    Ok(())
}

// ── create ────────────────────────────────────────────────────────────────────

async fn create_cohort(
    cx: &CommandContext,
    name: String,
    filters_file: Option<PathBuf>,
    is_static: bool,
) -> Result<()> {
    let client = &cx.client;
    let project_id = project_id_required(client)?;

    let mut body = json!({ "name": name });

    if let Some(p) = filters_file.as_deref() {
        body["filters"] = read_json_file(p).await?;
    }

    if is_static {
        body["is_static"] = Value::Bool(true);
    }

    let created: Cohort = client
        .post(&format!("/api/projects/{project_id}/cohorts/"), &body)
        .await?;

    if cx.json_mode {
        #[derive(Serialize)]
        struct Out {
            ok: bool,
            action: &'static str,
            id: i64,
            name: String,
        }
        output::print_json(&Out {
            ok: true,
            action: "create",
            id: created.id,
            name: created.name,
        });
    } else {
        println!("Created cohort '{}' (id {})", created.name, created.id);
    }
    Ok(())
}

// ── update ────────────────────────────────────────────────────────────────────

async fn update_cohort(
    cx: &CommandContext,
    id: i64,
    name: Option<String>,
    filters_file: Option<PathBuf>,
) -> Result<()> {
    let client = &cx.client;
    let project_id = project_id_required(client)?;

    let mut body = serde_json::Map::new();
    if let Some(n) = name {
        body.insert("name".into(), Value::String(n));
    }
    if let Some(p) = filters_file.as_deref() {
        body.insert("filters".into(), read_json_file(p).await?);
    }

    if body.is_empty() {
        return Err(BosshoggError::BadRequest(
            "no update flags provided (try --name, --filters-file)".into(),
        ));
    }

    cx.confirm(&format!("update cohort `{id}`; continue?"))?;

    let updated: Cohort = client
        .patch(
            &format!("/api/projects/{project_id}/cohorts/{id}/"),
            &Value::Object(body),
        )
        .await?;

    if cx.json_mode {
        output::print_json(&updated);
    } else {
        println!("Updated cohort '{}' (id {})", updated.name, updated.id);
    }
    Ok(())
}

// ── delete ────────────────────────────────────────────────────────────────────

async fn delete_cohort(cx: &CommandContext, id: i64) -> Result<()> {
    let client = &cx.client;
    let project_id = project_id_required(client)?;

    cx.confirm(&format!("soft-delete cohort `{id}`; continue?"))?;

    client
        .delete(&format!("/api/projects/{project_id}/cohorts/{id}/"))
        .await?;

    if cx.json_mode {
        #[derive(Serialize)]
        struct Out {
            ok: bool,
            action: &'static str,
            id: i64,
        }
        output::print_json(&Out {
            ok: true,
            action: "delete",
            id,
        });
    } else {
        println!("Deleted cohort {id}");
    }
    Ok(())
}

// ── members ───────────────────────────────────────────────────────────────────

async fn members_cohort(cx: &CommandContext, id: i64) -> Result<()> {
    let client = &cx.client;
    let project_id = project_id_required(client)?;
    let path = format!("/api/projects/{project_id}/cohorts/{id}/persons/");
    let results: Vec<Value> = client.get_paginated(&path, None).await?;

    if cx.json_mode {
        #[derive(Serialize)]
        struct Out {
            count: usize,
            results: Vec<Value>,
        }
        output::print_json(&Out {
            count: results.len(),
            results,
        });
    } else {
        println!("Members of cohort {id}: {}", results.len());
        for person in &results {
            let did = person
                .get("distinct_ids")
                .and_then(Value::as_array)
                .and_then(|a| a.first())
                .and_then(Value::as_str)
                .unwrap_or("-");
            let pid = person
                .get("id")
                .and_then(Value::as_str)
                .unwrap_or_else(|| person.get("uuid").and_then(Value::as_str).unwrap_or("-"));
            println!("  {pid}  {did}");
        }
    }
    Ok(())
}

// ── add-person ────────────────────────────────────────────────────────────────

async fn add_person(
    cx: &CommandContext,
    id: i64,
    person_id: Option<String>,
    distinct_id: Option<String>,
) -> Result<()> {
    if distinct_id.is_some() {
        return Err(BosshoggError::BadRequest(
            "--distinct-id is not yet supported for add-person; use --person-id <uuid> instead. \
             You can obtain person UUIDs with `bosshogg person list` (available in M4)."
                .into(),
        ));
    }

    let uuid =
        person_id.ok_or_else(|| BosshoggError::BadRequest("provide --person-id <uuid>".into()))?;

    let client = &cx.client;
    let project_id = project_id_required(client)?;

    // Validate that the cohort is static before mutating.
    let get_path = format!("/api/projects/{project_id}/cohorts/{id}/");
    let cohort: Cohort = client.get::<Cohort>(&get_path).await?;
    if cohort.is_static != Some(true) {
        return Err(BosshoggError::BadRequest(format!(
            "cohort {id} is dynamic; add-person / remove-person only work on static cohorts"
        )));
    }

    cx.confirm(&format!(
        "add person {uuid} to static cohort `{id}`; continue?"
    ))?;

    let body = json!({ "person_uuids": [uuid] });
    let v: Value = client
        .patch(
            &format!("/api/projects/{project_id}/cohorts/{id}/add_persons_to_static_cohort/"),
            &body,
        )
        .await?;

    if cx.json_mode {
        output::print_json(&v);
    } else {
        println!("Added person {uuid} to cohort {id}");
    }
    Ok(())
}

// ── remove-person ─────────────────────────────────────────────────────────────

async fn remove_person(
    cx: &CommandContext,
    id: i64,
    person_id: Option<String>,
    distinct_id: Option<String>,
) -> Result<()> {
    if distinct_id.is_some() {
        return Err(BosshoggError::BadRequest(
            "--distinct-id is not yet supported for remove-person; use --person-id <uuid> instead. \
             You can obtain person UUIDs with `bosshogg person list` (available in M4)."
                .into(),
        ));
    }

    let uuid =
        person_id.ok_or_else(|| BosshoggError::BadRequest("provide --person-id <uuid>".into()))?;

    let client = &cx.client;
    let project_id = project_id_required(client)?;

    // Validate that the cohort is static before mutating.
    let get_path = format!("/api/projects/{project_id}/cohorts/{id}/");
    let cohort: Cohort = client.get::<Cohort>(&get_path).await?;
    if cohort.is_static != Some(true) {
        return Err(BosshoggError::BadRequest(format!(
            "cohort {id} is dynamic; add-person / remove-person only work on static cohorts"
        )));
    }

    cx.confirm(&format!(
        "remove person {uuid} from static cohort `{id}`; continue?"
    ))?;

    let body = json!({ "person_uuid": uuid });
    let v: Value = client
        .patch(
            &format!("/api/projects/{project_id}/cohorts/{id}/remove_person_from_static_cohort/"),
            &body,
        )
        .await?;

    if cx.json_mode {
        output::print_json(&v);
    } else {
        println!("Removed person {uuid} from cohort {id}");
    }
    Ok(())
}

// ── calculation-history ───────────────────────────────────────────────────────

async fn calculation_history(cx: &CommandContext, id: i64) -> Result<()> {
    let client = &cx.client;
    let project_id = project_id_required(client)?;
    let v: Value = client
        .get(&format!(
            "/api/projects/{project_id}/cohorts/{id}/calculation_history/"
        ))
        .await?;
    if cx.json_mode {
        output::print_json(&v);
    } else if let Some(results) = v.get("results").and_then(Value::as_array) {
        println!("Calculation history for cohort {id}:");
        for entry in results {
            let completed = entry
                .get("last_calculation")
                .and_then(Value::as_str)
                .unwrap_or("-");
            let count = entry
                .get("count")
                .and_then(Value::as_i64)
                .map(|n| n.to_string())
                .unwrap_or_else(|| "-".into());
            let errors = entry
                .get("errors_calculating")
                .and_then(Value::as_i64)
                .map(|n| n.to_string())
                .unwrap_or_else(|| "0".into());
            println!("  {completed}  count={count}  errors={errors}");
        }
    } else {
        output::print_json(&v);
    }
    Ok(())
}

// ── activity ──────────────────────────────────────────────────────────────────

async fn activity_cohort(cx: &CommandContext, id: i64) -> Result<()> {
    let client = &cx.client;
    let project_id = project_id_required(client)?;
    let v: Value = client
        .get(&format!(
            "/api/projects/{project_id}/cohorts/{id}/activity/"
        ))
        .await?;
    if cx.json_mode {
        output::print_json(&v);
    } else if let Some(results) = v.get("results").and_then(Value::as_array) {
        for e in results {
            let a = e.get("activity").and_then(Value::as_str).unwrap_or("-");
            let t = e.get("created_at").and_then(Value::as_str).unwrap_or("-");
            let u = e
                .pointer("/user/email")
                .and_then(Value::as_str)
                .unwrap_or("-");
            println!("{t}  {a:<12}  {u}");
        }
    }
    Ok(())
}

// ── print helper ──────────────────────────────────────────────────────────────

fn print_cohort(cohort: &Cohort, json_mode: bool) {
    if json_mode {
        output::print_json(cohort);
    } else {
        println!("ID:              {}", cohort.id);
        println!("Name:            {}", cohort.name);
        if let Some(d) = cohort.description.as_deref() {
            println!("Description:     {d}");
        }
        println!("Static:          {}", cohort.is_static.unwrap_or(false));
        println!(
            "Count:           {}",
            cohort
                .count
                .map(|n| n.to_string())
                .unwrap_or_else(|| "-".into())
        );
        println!(
            "Is Calculating:  {}",
            cohort.is_calculating.unwrap_or(false)
        );
        if let Some(ca) = cohort.created_at.as_deref() {
            println!("Created:         {ca}");
        }
        if let Some(lc) = cohort.last_calculation.as_deref() {
            println!("Last Calc:       {lc}");
        }
        println!("Deleted:         {}", cohort.deleted);
    }
}

// ── unit tests ────────────────────────────────────────────────────────────────

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

    #[test]
    fn cohort_roundtrip_minimal() {
        let raw = r#"{
            "id": 1,
            "name": "My Cohort",
            "deleted": false,
            "filters": {}
        }"#;
        let c: Cohort = serde_json::from_str(raw).unwrap();
        assert_eq!(c.id, 1);
        assert_eq!(c.name, "My Cohort");
        assert!(!c.deleted);
    }

    #[test]
    fn cohort_roundtrip_full() {
        let raw = r#"{
            "id": 42,
            "name": "Power Users",
            "description": "Users who logged in 10+ times",
            "groups": [{"action_id": 1}],
            "deleted": false,
            "filters": {"properties": []},
            "query": null,
            "is_calculating": false,
            "created_by": {"id": 1, "email": "test@example.com"},
            "created_at": "2026-01-01T00:00:00Z",
            "last_calculation": "2026-04-01T00:00:00Z",
            "errors_calculating": 0,
            "count": 1234,
            "is_static": false,
            "experiment_set": [1, 2]
        }"#;
        let c: Cohort = serde_json::from_str(raw).unwrap();
        assert_eq!(c.id, 42);
        assert_eq!(c.count, Some(1234));
        assert_eq!(c.is_static, Some(false));
        assert_eq!(c.experiment_set, Some(vec![1, 2]));
        assert_eq!(c.errors_calculating, Some(0));
    }

    #[test]
    fn cohort_static_flag_parsed() {
        let raw = r#"{
            "id": 7,
            "name": "Static Cohort",
            "deleted": false,
            "filters": {},
            "is_static": true
        }"#;
        let c: Cohort = serde_json::from_str(raw).unwrap();
        assert_eq!(c.is_static, Some(true));
    }

    #[test]
    fn cohort_identifier_numeric() {
        // parse::<i64>() succeeds for numeric strings
        assert!("42".parse::<i64>().is_ok());
        assert!("power-users".parse::<i64>().is_err());
    }

    #[test]
    fn cohort_name_resolution_finds_exact_match() {
        // Simulates the exact-name filter in resolve_cohort_by_identifier.
        let candidates = vec![
            Cohort {
                id: 1,
                name: "power users".to_string(),
                description: None,
                groups: None,
                deleted: false,
                filters: serde_json::json!({}),
                query: None,
                is_calculating: None,
                created_by: None,
                created_at: None,
                last_calculation: None,
                errors_calculating: None,
                count: None,
                is_static: None,
                experiment_set: None,
            },
            Cohort {
                id: 42,
                name: "power-users".to_string(),
                description: None,
                groups: None,
                deleted: false,
                filters: serde_json::json!({}),
                query: None,
                is_calculating: None,
                created_by: None,
                created_at: None,
                last_calculation: None,
                errors_calculating: None,
                count: None,
                is_static: None,
                experiment_set: None,
            },
        ];

        let found = candidates.into_iter().find(|c| c.name == "power-users");
        assert!(found.is_some());
        assert_eq!(found.unwrap().id, 42);
    }

    #[test]
    fn cohort_name_resolution_not_found() {
        let candidates: Vec<Cohort> = vec![];
        let found = candidates.into_iter().find(|c| c.name == "nonexistent");
        assert!(found.is_none());
    }
}