claude-smart 0.1.1

Cross-platform Claude Code smart session manager
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
//! `csm usage` — multi-profile usage report.
//!
//! Joins the **registry** ([`ProfileMap`]) with the hub's [`UsageData`] blob
//! into one view, one row per profile (registry ∪ hub). A registered profile
//! with no hub data shows `—`/`no data`; a hub profile not in the registry is
//! appended with an `(unregistered)` tag (visibility over silent drop).
//!
//! ## Layering (testability)
//!
//! The pure core — [`build_report`] (join) + [`render_table`] / [`render_json`]
//! (format) — takes a `ProfileMap` + `Option<UsageData>` + freshness/config
//! flags and never touches the network. `fetch()` and stdout live only in
//! `main::cmd_usage`. Every formatting branch is unit-tested against fixtures.
//!
//! ## Status column
//!
//! Reuses the scoring thresholds (`SATURATION_PCT` = 95) so "near-limit" in the
//! table matches what the picker would refuse to launch under:
//! - `Errored`  — the profile is in `UsageData.errors`.
//! - `NoData`   — registered, but absent from the hub (or no sections).
//! - `NearLimit`— any of session/week_all/week_sonnet pct ≥ `WARN_PCT`.
//! - `Ok`       — otherwise.
//!
//! # Spec reference
//! `docs/2026-06-19-csm-usage-and-interactive-cas-edit.md` §1.

use serde::Serialize;

use crate::account::profiles::ProfileMap;
use crate::account::scoring::SATURATION_PCT;
use crate::usage::model::UsageData;

/// pct ≥ this → `NearLimit`. Matches the picker saturation gate so the table's
/// warning and the launcher's refusal agree.
pub const WARN_PCT: i64 = SATURATION_PCT;

// ─── view model ────────────────────────────────────────────────────────────────

/// Per-profile status classification for the STATUS column.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Status {
    /// Healthy — no section at/over the warn threshold.
    Ok,
    /// At least one quota section is at/over [`WARN_PCT`].
    NearLimit,
    /// The hub reported an error scraping this profile.
    Errored,
    /// Registered but the hub has no row (or no section data) for it.
    NoData,
}

impl Status {
    /// The compact glyph+word used in the human table.
    pub fn label(self) -> &'static str {
        match self {
            Status::Ok => "ok",
            Status::NearLimit => "\u{26a0} near-limit",
            Status::Errored => "\u{2716} errored",
            Status::NoData => "\u{00b7} no data",
        }
    }
}

/// One joined row: a profile's registry membership + its hub usage slice.
#[derive(Debug, Clone, Serialize)]
pub struct Row {
    /// Profile name (registry key, or a hub-only name).
    pub name: String,
    /// `true` iff the profile is in the registry (`profiles.json`).
    pub registered: bool,
    /// Session quota pct, or `None` when absent.
    pub session_pct: Option<i64>,
    /// Weekly (all tiers) quota pct, or `None` when absent.
    pub week_all_pct: Option<i64>,
    /// Weekly Sonnet quota pct, or `None` when absent.
    pub week_sonnet_pct: Option<i64>,
    /// A human reset hint (the soonest meaningful `resets` string), or `None`.
    pub resets: Option<String>,
    /// Classified status.
    pub status: Status,
    /// Error message when `status == Errored`.
    pub error: Option<String>,
}

/// The full report: rows + freshness/config metadata for the header/footer.
#[derive(Debug, Clone, Serialize)]
pub struct Report {
    /// Joined rows, sorted: registered (by name) first, then unregistered.
    pub rows: Vec<Row>,
    /// Age of the served data in seconds, when known (cache mtime). `None` when
    /// the data is live-fresh (hub-local) or there is no usable data.
    pub stale_secs: Option<u64>,
    /// `true` when metering is configured (hub env present). When `false`, the
    /// usage columns are all `—` and the report explains how to enable it.
    pub configured: bool,
    /// `true` when we have NO usage data at all (fetch failed + no cache, or
    /// unconfigured) — the table shows registry info only.
    pub no_usage: bool,
    /// Top-level capture timestamp from the hub, when present.
    pub captured_at: Option<String>,
}

// ─── pure core: join ─────────────────────────────────────────────────────────

/// Build the joined report from the registry and an optional usage blob.
///
/// `usage = None` means "no usable usage data" (fetch failed with no cache, or
/// metering disabled) — every row gets `NoData`/`—`. `configured` and
/// `stale_secs` are passed in by the caller (which owns the fetch + cache mtime).
///
/// Pure: no I/O, no network, no clock. Fully unit-testable.
pub fn build_report(
    profiles: &ProfileMap,
    usage: Option<&UsageData>,
    configured: bool,
    stale_secs: Option<u64>,
) -> Report {
    let mut rows: Vec<Row> = Vec::new();

    let errors = usage.and_then(|u| u.errors.as_ref());

    // 1. One row per registered profile (so a registered-but-unused profile is
    //    visible as `no data`, never silently dropped).
    for name in profiles.names_sorted() {
        rows.push(join_one(name, true, usage, errors));
    }

    // 2. Append hub profiles NOT in the registry, tagged unregistered.
    if let Some(u) = usage {
        let mut extra: Vec<&str> = u
            .profiles
            .keys()
            .map(String::as_str)
            .filter(|n| !profiles.contains(n))
            .collect();
        // Also surface error-only hub profiles that never produced a row.
        if let Some(errs) = errors {
            for n in errs.keys() {
                if !profiles.contains(n) && !u.profiles.contains_key(n) {
                    extra.push(n.as_str());
                }
            }
        }
        extra.sort_unstable();
        extra.dedup();
        for name in extra {
            rows.push(join_one(name, false, usage, errors));
        }
    }

    let no_usage = usage.is_none();

    Report {
        rows,
        stale_secs,
        configured,
        no_usage,
        captured_at: usage.and_then(|u| u.captured_at.clone()),
    }
}

/// Join a single profile name against the usage blob.
fn join_one(
    name: &str,
    registered: bool,
    usage: Option<&UsageData>,
    errors: Option<&std::collections::HashMap<String, String>>,
) -> Row {
    // Errored profile: short-circuit (no section data trustworthy).
    if let Some(err) = errors.and_then(|e| e.get(name)) {
        return Row {
            name: name.to_owned(),
            registered,
            session_pct: None,
            week_all_pct: None,
            week_sonnet_pct: None,
            resets: None,
            status: Status::Errored,
            error: Some(err.clone()),
        };
    }

    let pu = usage.and_then(|u| u.profiles.get(name));
    let session_pct = pu.and_then(|p| p.session.as_ref()).map(|s| s.pct);
    let week_all_pct = pu.and_then(|p| p.week_all.as_ref()).map(|s| s.pct);
    let week_sonnet_pct = pu.and_then(|p| p.week_sonnet.as_ref()).map(|s| s.pct);

    // Reset hint: prefer the session reset, else the weekly one.
    let resets = pu.and_then(|p| {
        p.session
            .as_ref()
            .and_then(|s| s.resets.clone())
            .or_else(|| p.week_all.as_ref().and_then(|s| s.resets.clone()))
    });

    let has_any = session_pct.is_some() || week_all_pct.is_some() || week_sonnet_pct.is_some();
    let status = if !has_any {
        Status::NoData
    } else if [session_pct, week_all_pct, week_sonnet_pct]
        .into_iter()
        .flatten()
        .any(|p| p >= WARN_PCT)
    {
        Status::NearLimit
    } else {
        Status::Ok
    };

    Row {
        name: name.to_owned(),
        registered,
        session_pct,
        week_all_pct,
        week_sonnet_pct,
        resets,
        status,
        error: None,
    }
}

// ─── pure core: human table render ──────────────────────────────────────────

/// Render the human-readable table (with header/footer lines) to a `String`.
///
/// Pure: returns the full multi-line block; the caller prints it. This keeps
/// every formatting branch unit-testable.
pub fn render_table(report: &Report) -> String {
    let mut out = String::new();

    // Stale header (offline degrade — first-class, per spec §1).
    if let Some(age) = report.stale_secs {
        // Only warn once the data is meaningfully old (> one positive-TTL window).
        if age >= 60 {
            out.push_str(&format!(
                "\u{26a0} hub data is {} old (showing last-known cache)\n",
                humanize_age(age)
            ));
        }
    }

    if !report.configured {
        out.push_str(
            "usage metering disabled — set CLAUDE_USAGE_URL + CLAUDE_HUB_HOSTNAME to enable\n",
        );
    }

    if report.rows.is_empty() {
        out.push_str("(no profiles configured — `csm profiles add <name>`)\n");
        return out;
    }

    // Column widths (name column sizes to content, min 8).
    let name_w = report
        .rows
        .iter()
        .map(|r| display_name(r).len())
        .max()
        .unwrap_or(8)
        .max(8);

    out.push_str(&format!(
        "{:<nw$}  {:>7}  {:>9}  {:>9}  {:<20}  {}\n",
        "PROFILE",
        "SESSION",
        "WEEK(all)",
        "WK(sonnet)",
        "RESETS",
        "STATUS",
        nw = name_w,
    ));

    for r in &report.rows {
        out.push_str(&format!(
            "{:<nw$}  {:>7}  {:>9}  {:>9}  {:<20}  {}\n",
            display_name(r),
            pct(r.session_pct),
            pct(r.week_all_pct),
            pct(r.week_sonnet_pct),
            truncate(r.resets.as_deref().unwrap_or("\u{2014}"), 20),
            status_cell(r),
            nw = name_w,
        ));
    }

    if report.no_usage && report.configured {
        out.push_str("usage metering unavailable (hub unreachable, no cache)\n");
    }

    out
}

/// The name as shown in the table (unregistered profiles get a tag).
fn display_name(r: &Row) -> String {
    if r.registered {
        r.name.clone()
    } else {
        format!("{} (unreg)", r.name)
    }
}

/// Status cell: the label, with the error message appended for errored rows.
fn status_cell(r: &Row) -> String {
    match (&r.status, &r.error) {
        (Status::Errored, Some(msg)) => format!("{}: {}", Status::Errored.label(), msg),
        (s, _) => s.label().to_owned(),
    }
}

/// Format an optional percent as `NN%` or the em-dash placeholder.
fn pct(v: Option<i64>) -> String {
    match v {
        Some(p) => format!("{p}%"),
        None => "\u{2014}".to_owned(),
    }
}

/// Truncate `s` to `max` display chars (ASCII-safe; resets strings are ASCII).
fn truncate(s: &str, max: usize) -> String {
    if s.chars().count() <= max {
        s.to_owned()
    } else {
        let mut t: String = s.chars().take(max.saturating_sub(1)).collect();
        t.push('\u{2026}');
        t
    }
}

/// Humanize an age in seconds → `"7m"`, `"3h"`, `"2d"`, `"45s"`.
fn humanize_age(secs: u64) -> String {
    if secs >= 86_400 {
        format!("{}d", secs / 86_400)
    } else if secs >= 3_600 {
        format!("{}h", secs / 3_600)
    } else if secs >= 60 {
        format!("{}m", secs / 60)
    } else {
        format!("{secs}s")
    }
}

// ─── pure core: JSON render ────────────────────────────────────────────────────

/// The stable `--json` wire shape (decoupled from the internal `Report`/`Row`
/// structs so renames there don't silently break the JSON contract).
#[derive(Debug, Serialize)]
struct JsonReport<'a> {
    captured_at: Option<&'a str>,
    stale_secs: Option<u64>,
    configured: bool,
    profiles: std::collections::BTreeMap<&'a str, JsonRow<'a>>,
}

#[derive(Debug, Serialize)]
struct JsonRow<'a> {
    registered: bool,
    session_pct: Option<i64>,
    week_all_pct: Option<i64>,
    week_sonnet_pct: Option<i64>,
    resets: Option<&'a str>,
    status: Status,
    error: Option<&'a str>,
}

/// Render the report as pretty JSON (stable key order via `BTreeMap`).
pub fn render_json(report: &Report) -> Result<String, serde_json::Error> {
    let profiles: std::collections::BTreeMap<&str, JsonRow> = report
        .rows
        .iter()
        .map(|r| {
            (
                r.name.as_str(),
                JsonRow {
                    registered: r.registered,
                    session_pct: r.session_pct,
                    week_all_pct: r.week_all_pct,
                    week_sonnet_pct: r.week_sonnet_pct,
                    resets: r.resets.as_deref(),
                    status: r.status,
                    error: r.error.as_deref(),
                },
            )
        })
        .collect();

    let wire = JsonReport {
        captured_at: report.captured_at.as_deref(),
        stale_secs: report.stale_secs,
        configured: report.configured,
        profiles,
    };
    serde_json::to_string_pretty(&wire)
}

// ─── tests ────────────────────────────────────────────────────────────────────

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

    fn registry(names: &[&str]) -> ProfileMap {
        let mut m = HashMap::new();
        for n in names {
            m.insert((*n).to_owned(), format!("/tmp/.claude.{n}"));
        }
        ProfileMap(m)
    }

    /// A usage blob: `personal` ok, `elyvian` near-limit (week_all=96), one
    /// errored profile, plus a hub-only `ghost` profile.
    fn sample_usage() -> UsageData {
        serde_json::from_str(
            r#"{
              "captured_at": "2026-06-19T07:00:00Z",
              "profiles": {
                "personal": {
                  "session": {"pct": 12, "resets": "9pm (Asia/Seoul)"},
                  "week_all": {"pct": 34, "resets": "Jun 22"},
                  "week_sonnet": {"pct": 8}
                },
                "elyvian": {
                  "session": {"pct": 40},
                  "week_all": {"pct": 96, "resets": "Jun 22"},
                  "week_sonnet": null
                },
                "ghost": {
                  "session": {"pct": 3},
                  "week_all": {"pct": 5}
                }
              },
              "errors": { "work": "HTTP 401: no credentials" }
            }"#,
        )
        .unwrap()
    }

    #[test]
    fn join_classifies_status_per_profile() {
        let reg = registry(&["personal", "elyvian", "work"]);
        let u = sample_usage();
        let report = build_report(&reg, Some(&u), true, Some(5));

        let by = |n: &str| report.rows.iter().find(|r| r.name == n).unwrap().clone();
        assert_eq!(by("personal").status, Status::Ok);
        assert_eq!(by("personal").session_pct, Some(12));
        assert_eq!(by("elyvian").status, Status::NearLimit); // week_all=96 ≥ 95
        assert_eq!(by("work").status, Status::Errored);
        assert_eq!(
            by("work").error.as_deref(),
            Some("HTTP 401: no credentials")
        );
    }

    #[test]
    fn registered_without_hub_data_is_no_data() {
        let reg = registry(&["personal", "lonely"]);
        let u = sample_usage(); // has personal but not "lonely"
        let report = build_report(&reg, Some(&u), true, None);
        let lonely = report.rows.iter().find(|r| r.name == "lonely").unwrap();
        assert_eq!(lonely.status, Status::NoData);
        assert!(lonely.registered);
        assert_eq!(lonely.session_pct, None);
    }

    #[test]
    fn unregistered_hub_profile_is_appended_and_tagged() {
        let reg = registry(&["personal", "elyvian"]); // ghost not registered
        let u = sample_usage();
        let report = build_report(&reg, Some(&u), true, None);
        let ghost = report.rows.iter().find(|r| r.name == "ghost").unwrap();
        assert!(!ghost.registered);
        // Registered rows come first, unregistered after.
        let names: Vec<&str> = report.rows.iter().map(|r| r.name.as_str()).collect();
        let ghost_idx = names.iter().position(|n| *n == "ghost").unwrap();
        let personal_idx = names.iter().position(|n| *n == "personal").unwrap();
        assert!(
            ghost_idx > personal_idx,
            "unregistered must sort after registered: {names:?}"
        );
        // And the rendered name carries the tag.
        let table = render_table(&report);
        assert!(table.contains("ghost (unreg)"), "table:\n{table}");
    }

    #[test]
    fn error_only_hub_profile_surfaces_as_row() {
        // "work" is errored and has NO profiles entry — it must still appear.
        let reg = registry(&["personal"]);
        let u = sample_usage();
        let report = build_report(&reg, Some(&u), true, None);
        let work = report.rows.iter().find(|r| r.name == "work");
        assert!(work.is_some(), "error-only hub profile must surface");
        assert_eq!(work.unwrap().status, Status::Errored);
    }

    #[test]
    fn no_usage_blob_makes_every_registered_row_no_data() {
        let reg = registry(&["personal", "elyvian"]);
        let report = build_report(&reg, None, true, None);
        assert!(report.no_usage);
        assert_eq!(report.rows.len(), 2);
        assert!(report.rows.iter().all(|r| r.status == Status::NoData));
        let table = render_table(&report);
        assert!(
            table.contains("usage metering unavailable"),
            "table:\n{table}"
        );
    }

    #[test]
    fn unconfigured_shows_disabled_message_and_registry() {
        let reg = registry(&["personal"]);
        let report = build_report(&reg, None, /*configured=*/ false, None);
        let table = render_table(&report);
        assert!(table.contains("usage metering disabled"), "table:\n{table}");
        assert!(
            table.contains("personal"),
            "registry must still render: {table}"
        );
        // Disabled never claims the hub is "unavailable" (that's a different state).
        assert!(!table.contains("unavailable"), "table:\n{table}");
    }

    #[test]
    fn empty_registry_renders_hint() {
        let reg = registry(&[]);
        let report = build_report(&reg, None, false, None);
        let table = render_table(&report);
        assert!(table.contains("no profiles configured"), "table:\n{table}");
    }

    #[test]
    fn stale_header_only_when_old() {
        let reg = registry(&["personal"]);
        let u = sample_usage();
        // Fresh (5s): no stale header.
        let fresh = render_table(&build_report(&reg, Some(&u), true, Some(5)));
        assert!(
            !fresh.contains("hub data is"),
            "fresh should not warn: {fresh}"
        );
        // Old (7m): stale header present.
        let stale = render_table(&build_report(&reg, Some(&u), true, Some(420)));
        assert!(stale.contains("hub data is 7m old"), "stale:\n{stale}");
    }

    #[test]
    fn humanize_age_units() {
        assert_eq!(humanize_age(45), "45s");
        assert_eq!(humanize_age(420), "7m");
        assert_eq!(humanize_age(7_200), "2h");
        assert_eq!(humanize_age(172_800), "2d");
    }

    #[test]
    fn json_shape_is_stable_and_sorted() {
        let reg = registry(&["personal", "elyvian"]);
        let u = sample_usage();
        let report = build_report(&reg, Some(&u), true, Some(30));
        let json = render_json(&report).unwrap();
        let v: serde_json::Value = serde_json::from_str(&json).unwrap();

        assert_eq!(v["configured"], serde_json::json!(true));
        assert_eq!(v["stale_secs"], serde_json::json!(30));
        assert_eq!(v["captured_at"], serde_json::json!("2026-06-19T07:00:00Z"));
        // BTreeMap → keys sorted: elyvian before ghost before personal.
        let keys: Vec<&str> = v["profiles"]
            .as_object()
            .unwrap()
            .keys()
            .map(String::as_str)
            .collect();
        let mut sorted = keys.clone();
        sorted.sort_unstable();
        assert_eq!(keys, sorted, "json profile keys must be sorted: {keys:?}");
        // Status serializes snake_case.
        assert_eq!(
            v["profiles"]["elyvian"]["status"],
            serde_json::json!("near_limit")
        );
        assert_eq!(
            v["profiles"]["personal"]["session_pct"],
            serde_json::json!(12)
        );
        assert_eq!(
            v["profiles"]["work"]["status"],
            serde_json::json!("errored")
        );
        assert_eq!(
            v["profiles"]["work"]["error"],
            serde_json::json!("HTTP 401: no credentials")
        );
    }

    #[test]
    fn truncate_respects_max() {
        assert_eq!(truncate("short", 20), "short");
        let long = "this is a very long reset string indeed";
        let t = truncate(long, 10);
        assert_eq!(t.chars().count(), 10);
        assert!(t.ends_with('\u{2026}'));
    }
}