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
// src/commands/session_recording.rs
//! `bosshogg session-recording` — list / get / update / delete.
//!
//! Session recordings are environment-scoped.
//! SAFETY: responses can include a `snapshots` field containing large
//! compressed rrweb JSON. We NEVER let that hit stdout unguarded.
//! `get` strips `snapshots` by default; opt-in via `--with-snapshots`
//! combined with `--out <file>`.

use clap::{Args, Subcommand};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::path::PathBuf;

use crate::commands::context::CommandContext;
use crate::commands::util::env_id_required;
use crate::error::{BosshoggError, Result};
use crate::output;

// ── Typed struct ─────────────────────────────────────────────────────────────
// `snapshots` intentionally excluded — callers opt in via --with-snapshots.

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct SessionRecording {
    pub id: String,
    #[serde(default)]
    pub distinct_id: Option<String>,
    #[serde(default)]
    pub viewed: Option<bool>,
    #[serde(default)]
    pub recording_duration: Option<i64>,
    #[serde(default)]
    pub active_seconds: Option<i64>,
    #[serde(default)]
    pub inactive_seconds: Option<i64>,
    #[serde(default)]
    pub start_time: Option<String>,
    #[serde(default)]
    pub end_time: Option<String>,
    #[serde(default)]
    pub click_count: Option<i64>,
    #[serde(default)]
    pub keypress_count: Option<i64>,
    #[serde(default)]
    pub console_log_count: Option<i64>,
    #[serde(default)]
    pub console_warn_count: Option<i64>,
    #[serde(default)]
    pub console_error_count: Option<i64>,
    #[serde(default)]
    pub start_url: Option<String>,
    #[serde(default)]
    pub person: Option<Value>,
    #[serde(default)]
    pub storage: Option<String>,
    #[serde(default)]
    pub pinned_count: Option<i64>,
    #[serde(default)]
    pub ongoing: Option<bool>,
    #[serde(default)]
    pub activity_score: Option<f64>,
    #[serde(default)]
    pub snapshot_source: Option<String>,
}

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

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

#[derive(Subcommand, Debug)]
pub enum SessionRecordingCommand {
    /// List session recordings with optional filters.
    List {
        /// Filter by person UUID.
        #[arg(long)]
        person_id: Option<String>,
        /// Filter by distinct ID.
        #[arg(long)]
        distinct_id: Option<String>,
        /// Only recordings before this timestamp (ISO 8601).
        #[arg(long)]
        before: Option<String>,
        /// Only recordings after this timestamp (ISO 8601).
        #[arg(long)]
        after: Option<String>,
        /// Maximum number of results.
        #[arg(long)]
        limit: Option<usize>,
    },
    /// Get a single session recording by ID.
    ///
    /// SAFETY: snapshots (large rrweb JSON) are excluded by default.
    /// Use --with-snapshots --out <file> to write the full payload to disk.
    Get {
        id: String,
        /// Include raw snapshot blobs in output (can be very large).
        #[arg(long)]
        with_snapshots: bool,
        /// Write full JSON (including snapshots if --with-snapshots) to this file.
        #[arg(long)]
        out: Option<PathBuf>,
    },
    /// Update a session recording (e.g. mark as viewed or un-delete).
    Update {
        id: String,
        /// Set deleted=false to un-delete (restore) a soft-deleted recording.
        #[arg(long)]
        deleted: Option<bool>,
        /// Mark recording as viewed.
        #[arg(long)]
        viewed: Option<bool>,
    },
    /// Soft-delete a session recording (PATCH {"deleted": true}).
    Delete { id: String },
}

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

pub async fn execute(args: SessionRecordingArgs, cx: &CommandContext) -> Result<()> {
    match args.command {
        SessionRecordingCommand::List {
            person_id,
            distinct_id,
            before,
            after,
            limit,
        } => list_recordings(cx, person_id, distinct_id, before, after, limit).await,
        SessionRecordingCommand::Get {
            id,
            with_snapshots,
            out,
        } => get_recording(cx, id, with_snapshots, out).await,
        SessionRecordingCommand::Update {
            id,
            deleted,
            viewed,
        } => update_recording(cx, id, deleted, viewed).await,
        SessionRecordingCommand::Delete { id } => delete_recording(cx, id).await,
    }
}

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

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

async fn list_recordings(
    cx: &CommandContext,
    person_id: Option<String>,
    distinct_id: Option<String>,
    before: Option<String>,
    after: Option<String>,
    limit: Option<usize>,
) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;

    let mut params: Vec<String> = Vec::new();
    if let Some(p) = person_id {
        params.push(format!("person_uuid={}", urlencoding::encode(&p)));
    }
    if let Some(d) = distinct_id {
        params.push(format!("distinct_id={}", urlencoding::encode(&d)));
    }
    if let Some(b) = before {
        params.push(format!("before={}", urlencoding::encode(&b)));
    }
    if let Some(a) = after {
        params.push(format!("after={}", urlencoding::encode(&a)));
    }
    let query = if params.is_empty() {
        String::new()
    } else {
        format!("?{}", params.join("&"))
    };

    let path = format!("/api/environments/{env_id}/session_recordings/{query}");
    let results: Vec<SessionRecording> = client.get_paginated(&path, limit).await?;

    if cx.json_mode {
        output::print_json(&ListOutput {
            count: results.len(),
            results,
        });
    } else {
        let headers = &["ID", "DISTINCT_ID", "DURATION_S", "VIEWED", "START_TIME"];
        let rows: Vec<Vec<String>> = results
            .iter()
            .map(|r| {
                vec![
                    r.id.clone(),
                    r.distinct_id.clone().unwrap_or_else(|| "-".into()),
                    r.recording_duration
                        .map(|d| d.to_string())
                        .unwrap_or_else(|| "-".into()),
                    r.viewed
                        .map(|v| v.to_string())
                        .unwrap_or_else(|| "-".into()),
                    r.start_time.clone().unwrap_or_else(|| "-".into()),
                ]
            })
            .collect();
        output::table::print(headers, &rows);
    }
    Ok(())
}

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

async fn get_recording(
    cx: &CommandContext,
    id: String,
    with_snapshots: bool,
    out: Option<PathBuf>,
) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;

    // Always fetch the full raw Value so we can handle snapshots safely.
    let raw: Value = client
        .get(&format!(
            "/api/environments/{env_id}/session_recordings/{id}/"
        ))
        .await?;

    if with_snapshots {
        // User explicitly opted in to snapshots.
        if let Some(out_path) = out {
            // Write full JSON to disk.
            let content = serde_json::to_string(&raw).map_err(BosshoggError::Json)?;
            tokio::fs::write(&out_path, content)
                .await
                .map_err(BosshoggError::Io)?;
            if cx.json_mode {
                output::print_json(&json!({
                    "ok": true,
                    "written_to": out_path.display().to_string(),
                    "id": id,
                }));
            } else {
                println!(
                    "Full recording (with snapshots) written to {}",
                    out_path.display()
                );
            }
        } else {
            // TTY or piped: warn that snapshots can be huge; show terse summary.
            let snapshot_count = raw
                .get("snapshots")
                .and_then(Value::as_array)
                .map(|a| a.len())
                .unwrap_or(0);
            let snapshot_bytes = raw
                .get("snapshots")
                .map(|s| serde_json::to_string(s).unwrap_or_default().len())
                .unwrap_or(0);

            if cx.json_mode {
                // Strip snapshots before printing to stdout — safety rule.
                let mut stripped = raw.clone();
                if let Some(obj) = stripped.as_object_mut() {
                    obj.remove("snapshots");
                }
                output::print_json(&stripped);
                eprintln!(
                    "note: {} snapshot blob(s), ~{} bytes — use --out <file> to write full payload",
                    snapshot_count, snapshot_bytes
                );
            } else {
                // Print metadata summary; snapshots suppressed.
                let meta: SessionRecording =
                    serde_json::from_value(strip_snapshots(raw)).map_err(BosshoggError::Json)?;
                print_recording(&meta, false);
                eprintln!(
                    "note: {} snapshot blob(s), ~{} bytes — redirect to --out <file> to capture",
                    snapshot_count, snapshot_bytes
                );
            }
        }
    } else {
        // Default mode: strip snapshots entirely.
        let stripped = strip_snapshots(raw);
        if cx.json_mode {
            output::print_json(&stripped);
        } else {
            let meta: SessionRecording =
                serde_json::from_value(stripped).map_err(BosshoggError::Json)?;
            print_recording(&meta, false);
        }
    }

    Ok(())
}

/// Remove the `snapshots` key from a JSON Value (non-destructive clone if needed).
fn strip_snapshots(mut v: Value) -> Value {
    if let Some(obj) = v.as_object_mut() {
        obj.remove("snapshots");
    }
    v
}

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

async fn update_recording(
    cx: &CommandContext,
    id: String,
    deleted: Option<bool>,
    viewed: Option<bool>,
) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;

    let mut body = serde_json::Map::new();
    if let Some(d) = deleted {
        body.insert("deleted".into(), Value::Bool(d));
    }
    if let Some(v) = viewed {
        body.insert("viewed".into(), Value::Bool(v));
    }

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

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

    let updated: Value = client
        .patch(
            &format!("/api/environments/{env_id}/session_recordings/{id}/"),
            &Value::Object(body),
        )
        .await?;

    // Strip snapshots from the response before printing.
    let stripped = strip_snapshots(updated);

    if cx.json_mode {
        output::print_json(&stripped);
    } else {
        let meta: SessionRecording =
            serde_json::from_value(stripped).map_err(BosshoggError::Json)?;
        println!("Updated session recording {}", meta.id);
        print_recording(&meta, false);
    }
    Ok(())
}

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

async fn delete_recording(cx: &CommandContext, id: String) -> Result<()> {
    let client = &cx.client;
    let env_id = env_id_required(client)?;

    cx.confirm(&format!("delete session recording `{id}`; continue?"))?;

    // session_recordings is NOT in SOFT_DELETE_RESOURCES — use hard delete.
    client
        .delete(&format!(
            "/api/environments/{env_id}/session_recordings/{id}/"
        ))
        .await?;

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

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

fn print_recording(r: &SessionRecording, _json_mode: bool) {
    println!("ID:             {}", r.id);
    if let Some(d) = r.distinct_id.as_deref() {
        println!("Distinct ID:    {d}");
    }
    if let Some(v) = r.viewed {
        println!("Viewed:         {v}");
    }
    if let Some(dur) = r.recording_duration {
        println!("Duration (s):   {dur}");
    }
    if let Some(st) = r.start_time.as_deref() {
        println!("Start:          {st}");
    }
    if let Some(et) = r.end_time.as_deref() {
        println!("End:            {et}");
    }
    if let Some(url) = r.start_url.as_deref() {
        println!("Start URL:      {url}");
    }
}

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

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

    #[test]
    fn session_recording_roundtrip_minimal() {
        let raw = r#"{"id": "rec-1"}"#;
        let r: SessionRecording = serde_json::from_str(raw).unwrap();
        assert_eq!(r.id, "rec-1");
        assert!(r.distinct_id.is_none());
    }

    #[test]
    fn session_recording_roundtrip_full() {
        let raw = r#"{
            "id": "rec-full",
            "distinct_id": "user123",
            "viewed": true,
            "recording_duration": 120,
            "active_seconds": 80,
            "inactive_seconds": 40,
            "start_time": "2026-01-01T10:00:00Z",
            "end_time": "2026-01-01T10:02:00Z",
            "click_count": 5,
            "keypress_count": 20,
            "console_log_count": 3,
            "console_warn_count": 1,
            "console_error_count": 0,
            "start_url": "https://example.com",
            "person": {"id": "person-1"},
            "storage": "object_storage",
            "pinned_count": 0,
            "ongoing": false,
            "activity_score": 7.5,
            "snapshot_source": "realtime"
        }"#;
        let r: SessionRecording = serde_json::from_str(raw).unwrap();
        assert_eq!(r.id, "rec-full");
        assert_eq!(r.distinct_id.as_deref(), Some("user123"));
        assert_eq!(r.viewed, Some(true));
        assert_eq!(r.recording_duration, Some(120));
        assert_eq!(r.activity_score, Some(7.5));
    }

    #[test]
    fn session_recording_snapshots_not_in_struct() {
        // Even if the API returns `snapshots`, our struct does not capture it.
        let raw = r#"{
            "id": "rec-with-snaps",
            "snapshots": [{"type": 2, "data": {"source": 1}}]
        }"#;
        let r: SessionRecording = serde_json::from_str(raw).unwrap();
        assert_eq!(r.id, "rec-with-snaps");
        // No panic — snapshots field silently ignored.
        let out = serde_json::to_string(&r).unwrap();
        assert!(!out.contains("snapshots"));
    }

    #[test]
    fn strip_snapshots_removes_key() {
        let v = serde_json::json!({
            "id": "rec-1",
            "distinct_id": "u1",
            "snapshots": [{"data": "huge"}]
        });
        let stripped = strip_snapshots(v);
        assert!(stripped.get("snapshots").is_none());
        assert_eq!(stripped["id"], "rec-1");
    }

    #[test]
    fn strip_snapshots_noop_when_absent() {
        let v = serde_json::json!({"id": "rec-2"});
        let stripped = strip_snapshots(v);
        assert_eq!(stripped["id"], "rec-2");
    }
}