clauth 0.7.4

Manage multiple Claude Code accounts, monitor 5h/7d usage with configurable auto-switch and delegation with an MCP plugin. CLI + TUI.
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
//! Token-usage statistics for the "tokens" TUI tab.
//!
//! # Design
//!
//! Two-phase load, emitted as two events per run so the tab paints instantly:
//! 1. **Base stats** ([`load_base`]) — parsed from `~/.claude/stats-cache.json`,
//!    which Claude Code maintains as a pre-aggregated lifetime snapshot. A single
//!    small JSON file, so it returns in well under a millisecond and is the tab's
//!    first paint instead of a blank "reading ~/.claude".
//! 2. **Live top-up** ([`merge_topup`]) — appends days from `~/.claude/projects/`
//!    transcripts strictly newer than the cache's `lastComputedDate`, avoiding
//!    double-counting days already in the snapshot. The recursive sweep also
//!    reaches subagent/workflow transcripts under `<session>/subagents/`. Each
//!    response is deduplicated by `message.id` (a content composite when absent,
//!    so an id-less line still dedups), and each message by line `uuid`, so a
//!    response mirrored into more than one file is counted once. Beyond tokens,
//!    the top-up reconstructs post-cutoff message/session/hour counts so the
//!    lifetime card and activity graph track the same live window as the token
//!    bars rather than freezing at `lastComputedDate`.
//!
//! The background sweep keeps a per-file contribution cache ([`TopUpCache`]) so
//! each 90s refresh re-reads only transcripts whose mtime advanced — the rest are
//! re-merged from memory, avoiding a full multi-hundred-MB re-read every cycle.
//!
//! # Caveat
//!
//! `stats-cache.json` reflects **all** Claude Code usage across all profiles and
//! accounts on this machine (global pool). The top-up reads every JSONL file
//! reachable via the home directory at load time, which may span all profiles if
//! they share a home. This is intentional — the tokens tab shows aggregate usage.

use std::collections::HashMap;
use std::collections::HashSet;
use std::io::BufRead as _;
use std::path::{Path, PathBuf};
use std::sync::mpsc::{Receiver, RecvTimeoutError, Sender};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use serde::Deserialize;

use crate::usage::{epoch_secs_to_iso, iso_to_epoch_secs, now_epoch_secs};

// ── Refresh cadence ─────────────────────────────────────────────────────────

const REFRESH_INTERVAL: Duration = Duration::from_secs(90);

// ── Public types ─────────────────────────────────────────────────────────────

/// Per-model lifetime aggregate. `input`/`output` exclude cache; cache is separate.
#[derive(Debug, Clone, Default)]
pub(crate) struct ModelTokens {
    pub(crate) model: String,
    pub(crate) input: u64,
    pub(crate) output: u64,
    pub(crate) cache_read: u64,
    pub(crate) cache_create: u64,
}

impl ModelTokens {
    /// input + output (matches stats-cache `dailyModelTokens` semantics).
    pub(crate) fn in_out(&self) -> u64 {
        self.input.saturating_add(self.output)
    }

    /// input + output + cache_read + cache_create.
    pub(crate) fn total(&self) -> u64 {
        self.input
            .saturating_add(self.output)
            .saturating_add(self.cache_read)
            .saturating_add(self.cache_create)
    }
}

/// Per-day in+out token total summed across all models that day.
#[derive(Debug, Clone)]
pub(crate) struct DayTokens {
    pub(crate) date: String, // "YYYY-MM-DD"
    pub(crate) tokens: u64,
}

/// Per-day activity counts (from stats-cache `dailyActivity`).
#[derive(Debug, Clone)]
pub(crate) struct DayActivity {
    pub(crate) date: String,
    pub(crate) messages: u64,
    pub(crate) sessions: u64,
    pub(crate) tool_calls: u64,
}

/// Single-day token + message rollup, built live from today's transcripts during
/// the top-up pass (so it carries the full in/out/cache split, unlike `DayTokens`).
#[derive(Debug, Clone, Default)]
pub(crate) struct DaySummary {
    pub(crate) date: String,
    pub(crate) input: u64,
    pub(crate) output: u64,
    pub(crate) cache_read: u64,
    pub(crate) cache_create: u64,
    /// User/assistant messages seen for the day (transcript-derived).
    pub(crate) messages: u64,
    /// Per-model breakdown of the day's tokens. Carried so cost can be priced
    /// per model (rates differ by family) — the day's lifetime totals can't be
    /// isolated from `TokenStats::models`. Empty until the top-up populates it.
    pub(crate) models: Vec<ModelTokens>,
}

impl DaySummary {
    pub(crate) fn in_out(&self) -> u64 {
        self.input.saturating_add(self.output)
    }

    pub(crate) fn total(&self) -> u64 {
        self.in_out()
            .saturating_add(self.cache_read)
            .saturating_add(self.cache_create)
    }
}

/// Aggregated token statistics view-model.
#[derive(Debug, Clone, Default)]
pub(crate) struct TokenStats {
    /// All models individually, sorted DESC by total(). Grouping is a render concern.
    pub(crate) models: Vec<ModelTokens>,
    pub(crate) daily: Vec<DayTokens>, // chronological ASC by date
    pub(crate) activity: Vec<DayActivity>, // chronological ASC by date
    pub(crate) hour_counts: [u64; 24], // index = hour of day 0..23
    pub(crate) total_input: u64,
    pub(crate) total_output: u64,
    pub(crate) total_cache_read: u64,
    pub(crate) total_cache_create: u64,
    pub(crate) total_sessions: u64,
    pub(crate) total_messages: u64,
    pub(crate) first_session_date: Option<String>, // raw ISO from stats-cache
    pub(crate) last_computed_date: Option<String>, // "YYYY-MM-DD" from stats-cache
    pub(crate) topped_up_through: Option<String>,  // latest "YYYY-MM-DD" added by top-up
    /// Today's usage, built live from transcripts; `None` when idle today.
    pub(crate) today: Option<DaySummary>,
}

impl TokenStats {
    /// input + output across all models — the "work" metric that matches the
    /// daily trend (`dailyModelTokens` is in+out only). The dashboard headlines
    /// use this so today/total/daily/models all share one basis; cache is shown
    /// separately (cache-hit badge + composition card).
    pub(crate) fn total_in_out(&self) -> u64 {
        self.total_input.saturating_add(self.total_output)
    }

    /// input + output + cache_read + cache_create across all models — the full
    /// throughput, used only by the cache lens (composition card, cache-hit).
    pub(crate) fn total_tokens(&self) -> u64 {
        self.total_input
            .saturating_add(self.total_output)
            .saturating_add(self.total_cache_read)
            .saturating_add(self.total_cache_create)
    }

    /// Cache-hit ratio in 0.0..=1.0: cache_read / (cache_read + cache_create + input).
    /// Returns 0.0 when the denominator is 0.
    pub(crate) fn cache_hit_ratio(&self) -> f64 {
        let denom = self
            .total_cache_read
            .saturating_add(self.total_cache_create)
            .saturating_add(self.total_input);
        if denom == 0 {
            return 0.0;
        }
        self.total_cache_read as f64 / denom as f64
    }
}

/// Models below this lifetime total fold into the "others" row.
const OTHERS_THRESHOLD: u64 = 1_000_000;

/// Display grouping: keep Anthropic models individual, keep any other model that
/// has moved more than [`OTHERS_THRESHOLD`] tokens individual too, and fold only
/// the long tail of tiny non-Anthropic models into one "others" row.
/// Returns rows sorted DESC by `in_out()`. Pure fn, unit-testable.
pub(crate) fn group_models(models: &[ModelTokens]) -> Vec<ModelTokens> {
    let mut out: Vec<ModelTokens> = Vec::new();
    let mut others = ModelTokens {
        model: "others".to_owned(),
        ..Default::default()
    };

    for m in models {
        if is_anthropic(&m.model) || m.total() > OTHERS_THRESHOLD {
            out.push(m.clone());
        } else {
            others.input = others.input.saturating_add(m.input);
            others.output = others.output.saturating_add(m.output);
            others.cache_read = others.cache_read.saturating_add(m.cache_read);
            others.cache_create = others.cache_create.saturating_add(m.cache_create);
        }
    }

    if others.total() > 0 {
        out.push(others);
    }

    // Rank by in+out ("work"), matching the dashboard's token basis, so the
    // bars descend by the value actually shown.
    out.sort_unstable_by_key(|m| std::cmp::Reverse(m.in_out()));
    out
}

/// True when the model name denotes an Anthropic model (starts with "claude").
pub(crate) fn is_anthropic(model: &str) -> bool {
    model.starts_with("claude")
}

/// Friendly display name for a model id — the single place that maps raw ids
/// to nice labels, used everywhere a model is shown.
///
/// Anthropic ids collapse to `family version` (`claude-opus-4-8` → `opus 4.8`,
/// `claude-sonnet-4-5-20250929` → `sonnet 4.5`, `claude-opus-4-6-thinking`
/// → `opus 4.6 thinking`). A trailing 8-digit date stamp is dropped. The
/// `others` bucket and any unrecognized id pass through (date-stripped).
pub(crate) fn model_display_name(model: &str) -> String {
    if model == "others" {
        return "others".to_string();
    }
    // Drop a trailing 8-digit date stamp (e.g. `-20250929`); version segments
    // are 1–2 digits, so this never eats a real version component.
    let base = match model.rsplit_once('-') {
        Some((head, tail)) if tail.len() == 8 && tail.bytes().all(|b| b.is_ascii_digit()) => head,
        _ => model,
    };
    let Some(rest) = base.strip_prefix("claude-") else {
        return base.to_string();
    };
    let mut parts = rest.split('-');
    let Some(family) = parts.next() else {
        return base.to_string();
    };
    // Leading numeric/dotted segments form the version (joined by `.`); any
    // trailing words (e.g. `thinking`) are appended verbatim.
    let mut version: Vec<&str> = Vec::new();
    let mut extras: Vec<&str> = Vec::new();
    for p in parts {
        if extras.is_empty() && !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit() || b == b'.')
        {
            version.push(p);
        } else {
            extras.push(p);
        }
    }
    let mut out = family.to_string();
    if !version.is_empty() {
        out.push(' ');
        out.push_str(&version.join("."));
    }
    for e in extras {
        out.push(' ');
        out.push_str(e);
    }
    out
}

// ── stats-cache.json wire types ──────────────────────────────────────────────

#[derive(Deserialize, Default)]
#[serde(rename_all = "camelCase", default)]
struct StatsCacheFile {
    last_computed_date: Option<String>,
    first_session_date: Option<String>,
    total_sessions: u64,
    total_messages: u64,
    daily_activity: Vec<WireActivity>,
    daily_model_tokens: Vec<WireDayTokens>,
    model_usage: HashMap<String, WireModelUsage>,
    hour_counts: HashMap<String, u64>,
}

#[derive(Deserialize, Default)]
#[serde(rename_all = "camelCase", default)]
struct WireActivity {
    date: String,
    message_count: u64,
    session_count: u64,
    tool_call_count: u64,
}

#[derive(Deserialize, Default)]
#[serde(rename_all = "camelCase", default)]
struct WireDayTokens {
    date: String,
    tokens_by_model: HashMap<String, u64>,
}

#[derive(Deserialize, Default)]
#[serde(rename_all = "camelCase", default)]
struct WireModelUsage {
    input_tokens: u64,
    output_tokens: u64,
    cache_read_input_tokens: u64,
    cache_creation_input_tokens: u64,
}

// ── JSONL transcript wire types ──────────────────────────────────────────────

#[derive(Deserialize, Default)]
#[serde(default)]
struct TranscriptLine {
    timestamp: Option<String>,
    uuid: Option<String>,
    #[serde(rename = "sessionId")]
    session_id: Option<String>,
    message: Option<TranscriptMsg>,
}

#[derive(Deserialize, Default)]
#[serde(default)]
struct TranscriptMsg {
    id: Option<String>,
    model: Option<String>,
    role: Option<String>,
    usage: Option<TranscriptUsage>,
    content: Option<Vec<ContentBlock>>,
}

/// One content block of an assistant message; only its `type` is read, to count
/// `tool_use` invocations for the per-day tool-call total.
#[derive(Deserialize, Default)]
#[serde(default)]
struct ContentBlock {
    #[serde(rename = "type")]
    kind: Option<String>,
}

#[derive(Deserialize, Default)]
#[serde(default)]
struct TranscriptUsage {
    input_tokens: u64,
    output_tokens: u64,
    cache_read_input_tokens: u64,
    cache_creation_input_tokens: u64,
}

// ── Load ─────────────────────────────────────────────────────────────────────

/// Parse `stats-cache.json` into the base view-model — the fast first phase of
/// the load. No transcript sweep, so it returns in well under a millisecond; the
/// background loader emits this for an instant first paint, then merges the live
/// transcript top-up ([`merge_topup`]). Returns `None` when the cache file is
/// missing or unparseable.
pub(crate) fn load_base(claude_dir: &Path) -> Option<TokenStats> {
    let cache_path = claude_dir.join("stats-cache.json");
    let raw = std::fs::read_to_string(&cache_path).ok()?;
    let wire: StatsCacheFile = serde_json::from_str(&raw).ok()?;

    let mut models: Vec<ModelTokens> = wire
        .model_usage
        .iter()
        .map(|(name, u)| ModelTokens {
            model: name.clone(),
            input: u.input_tokens,
            output: u.output_tokens,
            cache_read: u.cache_read_input_tokens,
            cache_create: u.cache_creation_input_tokens,
        })
        .collect();
    models.sort_unstable_by_key(|m| std::cmp::Reverse(m.total()));

    let mut daily: Vec<DayTokens> = wire
        .daily_model_tokens
        .iter()
        .map(|d| DayTokens {
            date: d.date.clone(),
            tokens: d.tokens_by_model.values().copied().sum(),
        })
        .collect();
    daily.sort_unstable_by_key(|d| d.date.clone());

    let mut activity: Vec<DayActivity> = wire
        .daily_activity
        .iter()
        .map(|a| DayActivity {
            date: a.date.clone(),
            messages: a.message_count,
            sessions: a.session_count,
            tool_calls: a.tool_call_count,
        })
        .collect();
    activity.sort_unstable_by_key(|d| d.date.clone());

    let mut hour_counts = [0u64; 24];
    for (k, v) in &wire.hour_counts {
        if let Ok(h) = k.parse::<usize>()
            && h < 24
        {
            hour_counts[h] = *v;
        }
    }

    let total_input: u64 = wire.model_usage.values().map(|u| u.input_tokens).sum();
    let total_output: u64 = wire.model_usage.values().map(|u| u.output_tokens).sum();
    let total_cache_read: u64 = wire
        .model_usage
        .values()
        .map(|u| u.cache_read_input_tokens)
        .sum();
    let total_cache_create: u64 = wire
        .model_usage
        .values()
        .map(|u| u.cache_creation_input_tokens)
        .sum();

    let stats = TokenStats {
        models,
        daily,
        activity,
        hour_counts,
        total_input,
        total_output,
        total_cache_read,
        total_cache_create,
        total_sessions: wire.total_sessions,
        total_messages: wire.total_messages,
        first_session_date: wire.first_session_date,
        last_computed_date: wire.last_computed_date.clone(),
        topped_up_through: None,
        today: None,
    };

    Some(stats)
}

/// Full synchronous load: parse the stats-cache, sweep transcripts once, merge.
/// Test-only — the background [`spawn`] loader uses `load_base` + `merge_topup`
/// directly so it can paint the base instantly and reuse its per-file cache.
#[cfg(test)]
pub(crate) fn load(claude_dir: &Path) -> Option<TokenStats> {
    let mut base = load_base(claude_dir)?;
    let today = today_date();
    let lcd = base.last_computed_date.clone();
    let mut cache = TopUpCache::default();
    refresh_topup_cache(claude_dir, lcd.as_deref(), &mut cache);
    merge_topup(&mut base, &cache, lcd.as_deref(), &today);
    Some(base)
}

/// Current UTC calendar date as "YYYY-MM-DD".
fn today_date() -> String {
    let iso = epoch_secs_to_iso(now_epoch_secs());
    iso.get(..10).map(str::to_owned).unwrap_or(iso)
}

// ── Recent transcript top-up ─────────────────────────────────────────────────

/// Derive a `SystemTime` cutoff from a "YYYY-MM-DD" string (00:00 UTC of that day).
fn date_to_cutoff(date: &str) -> Option<SystemTime> {
    let ts = format!("{date}T00:00:00+00:00");
    let secs = iso_to_epoch_secs(&ts)?;
    if secs < 0 {
        return None;
    }
    Some(UNIX_EPOCH + Duration::from_secs(secs as u64))
}

/// Recursively collect `*.jsonl` paths under `dir`, descending at most
/// `max_depth` directory levels. Subagent and workflow transcripts are nested
/// under `projects/<slug>/<session>/subagents/`, deeper than the main-session
/// files, so a single-level read would miss them. `DirEntry::file_type` does not
/// follow symlinks, so a symlinked directory is treated as a file and never
/// recursed — this bounds the walk and avoids cycles.
fn collect_jsonl(dir: &Path, max_depth: usize, out: &mut Vec<PathBuf>) {
    if max_depth == 0 {
        return;
    }
    let entries = match std::fs::read_dir(dir) {
        Ok(it) => it,
        Err(_) => return,
    };
    for entry in entries.flatten() {
        let Ok(file_type) = entry.file_type() else {
            continue;
        };
        let path = entry.path();
        if file_type.is_dir() {
            collect_jsonl(&path, max_depth - 1, out);
        } else if path.extension().and_then(|e| e.to_str()) == Some("jsonl") {
            out.push(path);
        }
    }
}

/// One transcript line's contribution, cached per file so an unchanged file is
/// re-merged from memory instead of re-read on the next 90s sweep. The dedup keys
/// are carried so the cross-file merge counts each response / message once even
/// when resumed or forked sessions copy lines forward.
struct LineRec {
    date: String,    // "YYYY-MM-DD"
    hour: u8,        // 0..=23
    uuid: String,    // line uuid ("" when absent) — message/hour/session dedup
    session: String, // sessionId ("" when absent)
    /// A user/assistant turn — counts toward messages, hours, sessions.
    is_message: bool,
    /// Token fields below are valid only when set (an assistant `usage` line).
    has_usage: bool,
    tok_key: String, // message.id, or a composite when absent — token dedup
    model: String,
    input: u64,
    output: u64,
    cache_read: u64,
    cache_create: u64,
    tool_calls: u64,
}

struct FileContrib {
    mtime: SystemTime,
    recs: Vec<LineRec>,
}

/// Per-file transcript contributions persisted across the loader's 90s sweeps.
/// Only files whose mtime advanced (or are new) get re-read; everything else is
/// re-merged from memory, so the multi-hundred-MB full re-read that the old sweep
/// paid every cycle is gone. Bounded by the post-cutoff transcript volume of the
/// session — a few MB; rebuilt fresh on a cold start.
#[derive(Default)]
struct TopUpCache {
    files: HashMap<PathBuf, FileContrib>,
}

/// Re-stat every `*.jsonl` under `projects/` (recursive, so subagent and workflow
/// transcripts count too) and re-read only those modified since the last sweep,
/// refreshing their cached records. Files at/under the cutoff or no longer present
/// are dropped. The stat pass is cheap; the file IO that dominated the old sweep
/// now runs only for what actually changed between ticks.
fn refresh_topup_cache(
    claude_dir: &Path,
    last_computed_date: Option<&str>,
    cache: &mut TopUpCache,
) {
    let Some(cutoff_date) = last_computed_date else {
        cache.files.clear();
        return;
    };
    let Some(cutoff_st) = date_to_cutoff(cutoff_date) else {
        return;
    };
    let projects_dir = claude_dir.join("projects");
    let mut paths: Vec<PathBuf> = Vec::new();
    collect_jsonl(&projects_dir, 8, &mut paths);

    // Drop cache entries for files that vanished since the last sweep.
    let present: HashSet<&PathBuf> = paths.iter().collect();
    cache.files.retain(|p, _| present.contains(p));

    for path in paths {
        let mtime = match std::fs::metadata(&path)
            .ok()
            .and_then(|m| m.modified().ok())
        {
            Some(t) => t,
            None => {
                cache.files.remove(&path);
                continue;
            }
        };
        // mtime guard: a file untouched since the cutoff can hold no post-cutoff
        // lines, so it never enters the cache (and its today lines, if any, would
        // also predate the cutoff day).
        if mtime <= cutoff_st {
            cache.files.remove(&path);
            continue;
        }
        match cache.files.get(&path) {
            Some(fc) if fc.mtime == mtime => {} // unchanged — keep cached records
            _ => {
                let recs = parse_file(&path);
                cache.files.insert(path, FileContrib { mtime, recs });
            }
        }
    }
}

/// Merge the cached per-file records into `base` (already holding stats-cache
/// data). The cutoff/today split is applied here, not at parse time, so an
/// advancing `last_computed_date` needs no re-read — the same records just flow to
/// different buckets. Responses are deduped by token key, messages by line uuid,
/// across all files. Token totals/models/daily extend the base; messages,
/// sessions, hour buckets, and per-day activity for days strictly after the cutoff
/// are reconstructed from transcripts (close to, but not byte-identical with,
/// Claude Code's own counters) so the lifetime card and activity graph track the
/// same live window as the token bars instead of freezing at the cutoff.
fn merge_topup(
    base: &mut TokenStats,
    cache: &TopUpCache,
    last_computed_date: Option<&str>,
    today_date: &str,
) {
    let Some(cutoff_date) = last_computed_date else {
        return; // no safe boundary
    };

    // Token aggregates seeded from base so post-cutoff days extend, never replace.
    let mut daily_map: HashMap<String, u64> = base
        .daily
        .iter()
        .map(|d| (d.date.clone(), d.tokens))
        .collect();
    let mut model_map: HashMap<String, ModelTokens> = base
        .models
        .iter()
        .cloned()
        .map(|m| (m.model.clone(), m))
        .collect();

    // Per-day activity for post-cutoff days + lifetime deltas.
    let mut day_msgs: HashMap<String, u64> = HashMap::new();
    let mut day_sessions: HashMap<String, HashSet<String>> = HashMap::new();
    let mut day_tools: HashMap<String, u64> = HashMap::new();
    let mut post_sessions: HashSet<String> = HashSet::new();
    let mut new_hours = [0u64; 24];

    let mut today_acc = DaySummary {
        date: today_date.to_owned(),
        ..Default::default()
    };
    let mut today_models: HashMap<String, ModelTokens> = HashMap::new();

    let mut seen_tok: HashSet<&str> = HashSet::new();
    let mut seen_uuid: HashSet<&str> = HashSet::new();
    let mut max_date: Option<String> = None;

    for fc in cache.files.values() {
        for r in &fc.recs {
            // Message / hour / session counting (user+assistant), deduped by uuid.
            // An empty uuid can't be keyed, so it counts as-is.
            if r.is_message && (r.uuid.is_empty() || seen_uuid.insert(r.uuid.as_str())) {
                if r.date == today_date {
                    today_acc.messages = today_acc.messages.saturating_add(1);
                }
                if r.date.as_str() > cutoff_date {
                    *day_msgs.entry(r.date.clone()).or_insert(0) += 1;
                    *day_tools.entry(r.date.clone()).or_insert(0) += r.tool_calls;
                    if (r.hour as usize) < 24 {
                        new_hours[r.hour as usize] += 1;
                    }
                    if !r.session.is_empty() {
                        day_sessions
                            .entry(r.date.clone())
                            .or_default()
                            .insert(r.session.clone());
                        post_sessions.insert(r.session.clone());
                    }
                }
            }

            // Token / model accumulation (assistant usage lines), deduped by key.
            if r.has_usage && (r.tok_key.is_empty() || seen_tok.insert(r.tok_key.as_str())) {
                if r.date == today_date {
                    today_acc.input = today_acc.input.saturating_add(r.input);
                    today_acc.output = today_acc.output.saturating_add(r.output);
                    today_acc.cache_read = today_acc.cache_read.saturating_add(r.cache_read);
                    today_acc.cache_create = today_acc.cache_create.saturating_add(r.cache_create);
                    let tm = today_models
                        .entry(r.model.clone())
                        .or_insert_with(|| ModelTokens {
                            model: r.model.clone(),
                            ..Default::default()
                        });
                    tm.input = tm.input.saturating_add(r.input);
                    tm.output = tm.output.saturating_add(r.output);
                    tm.cache_read = tm.cache_read.saturating_add(r.cache_read);
                    tm.cache_create = tm.cache_create.saturating_add(r.cache_create);
                }
                if r.date.as_str() > cutoff_date {
                    *daily_map.entry(r.date.clone()).or_insert(0) +=
                        r.input.saturating_add(r.output);
                    let e = model_map
                        .entry(r.model.clone())
                        .or_insert_with(|| ModelTokens {
                            model: r.model.clone(),
                            ..Default::default()
                        });
                    e.input = e.input.saturating_add(r.input);
                    e.output = e.output.saturating_add(r.output);
                    e.cache_read = e.cache_read.saturating_add(r.cache_read);
                    e.cache_create = e.cache_create.saturating_add(r.cache_create);
                    if max_date
                        .as_deref()
                        .is_none_or(|prev| r.date.as_str() > prev)
                    {
                        max_date = Some(r.date.clone());
                    }
                }
            }
        }
    }

    // Publish today's rollup (independent of the cutoff, so even a no-new-history
    // pass can still carry today's data).
    if today_acc.messages > 0 || today_acc.total() > 0 {
        today_acc.models = today_models.into_values().collect();
        today_acc
            .models
            .sort_unstable_by_key(|m| std::cmp::Reverse(m.total()));
        base.today = Some(today_acc);
    }

    if max_date.is_none() && day_msgs.is_empty() {
        return;
    }

    // Flush token daily/model back, recompute totals from the merged models.
    for (date, tokens) in &daily_map {
        if let Some(existing) = base.daily.iter_mut().find(|d| &d.date == date) {
            existing.tokens = *tokens;
        } else {
            base.daily.push(DayTokens {
                date: date.clone(),
                tokens: *tokens,
            });
        }
    }
    base.daily.sort_unstable_by_key(|d| d.date.clone());

    base.models = model_map.into_values().collect();
    base.models
        .sort_unstable_by_key(|m| std::cmp::Reverse(m.total()));
    base.total_input = base.models.iter().map(|m| m.input).sum();
    base.total_output = base.models.iter().map(|m| m.output).sum();
    base.total_cache_read = base.models.iter().map(|m| m.cache_read).sum();
    base.total_cache_create = base.models.iter().map(|m| m.cache_create).sum();

    // Append post-cutoff activity days so the activity graph extends past the
    // cutoff. Base days (≤ cutoff) are authoritative and untouched.
    let mut added_msgs = 0u64;
    for (date, msgs) in &day_msgs {
        let sessions = day_sessions.get(date).map(|s| s.len() as u64).unwrap_or(0);
        let tools = day_tools.get(date).copied().unwrap_or(0);
        added_msgs = added_msgs.saturating_add(*msgs);
        if let Some(a) = base.activity.iter_mut().find(|a| &a.date == date) {
            a.messages = *msgs;
            a.sessions = sessions;
            a.tool_calls = tools;
        } else {
            base.activity.push(DayActivity {
                date: date.clone(),
                messages: *msgs,
                sessions,
                tool_calls: tools,
            });
        }
    }
    base.activity.sort_unstable_by_key(|d| d.date.clone());

    // Lifetime deltas. Sessions span days, so the total uses the global distinct
    // post-cutoff set (not the per-day sum). A session straddling the cutoff is
    // counted in both base and here — rare, since sessions are short-lived.
    base.total_messages = base.total_messages.saturating_add(added_msgs);
    base.total_sessions = base
        .total_sessions
        .saturating_add(post_sessions.len() as u64);
    for (h, c) in new_hours.iter().enumerate() {
        base.hour_counts[h] = base.hour_counts[h].saturating_add(*c);
    }

    base.topped_up_through = max_date;
}

/// Parse one JSONL transcript into per-line contribution records. Pure read —
/// the cutoff/today split happens later in [`merge_topup`], so an advancing
/// `last_computed_date` never forces a re-read. Silently skips parse errors.
fn parse_file(path: &Path) -> Vec<LineRec> {
    let file = match std::fs::File::open(path) {
        Ok(f) => f,
        Err(_) => return Vec::new(),
    };
    let reader = std::io::BufReader::new(file);
    let mut out = Vec::new();
    for line in reader.lines() {
        let Ok(line) = line else { continue };
        let parsed: TranscriptLine = match serde_json::from_str(&line) {
            Ok(p) => p,
            Err(_) => continue,
        };
        let Some(ts) = parsed.timestamp.as_deref() else {
            continue;
        };
        if ts.len() < 10 {
            continue;
        }
        let date = ts[..10].to_owned();
        // ISO 8601 `YYYY-MM-DDThh:mm:ss…` — hour is bytes 11..13.
        let hour = ts
            .get(11..13)
            .and_then(|h| h.parse::<u8>().ok())
            .filter(|h| *h < 24)
            .unwrap_or(0);
        let Some(msg) = parsed.message.as_ref() else {
            continue;
        };
        let role = msg.role.as_deref().unwrap_or("");
        let usage = msg.usage.as_ref();
        // A user/assistant turn counts as a message; a usage-bearing line with no
        // role (some synthesized/streaming entries) is still a real response, so
        // count it too — in a normal transcript every usage line is role=assistant.
        let is_message = role == "user" || role == "assistant" || usage.is_some();
        if !is_message {
            continue;
        }

        let (has_usage, tok_key, model, input, output, cache_read, cache_create) =
            if let Some(u) = usage {
                let model = msg.model.clone().unwrap_or_else(|| "unknown".to_owned());
                // Dedup by `message.id` (present on every usage line); fall back to
                // a content-derived composite when absent so an id-less line still
                // dedups instead of bypassing the guard (the old over-count bug,
                // hit by usage lines that carry no `requestId`).
                let key = match msg.id.as_deref() {
                    Some(id) if !id.is_empty() => id.to_owned(),
                    _ => format!(
                        "{date}|{model}|{}|{}|{}|{}",
                        u.input_tokens,
                        u.output_tokens,
                        u.cache_read_input_tokens,
                        u.cache_creation_input_tokens
                    ),
                };
                (
                    true,
                    key,
                    model,
                    u.input_tokens,
                    u.output_tokens,
                    u.cache_read_input_tokens,
                    u.cache_creation_input_tokens,
                )
            } else {
                (false, String::new(), String::new(), 0, 0, 0, 0)
            };

        let tool_calls = msg
            .content
            .as_ref()
            .map(|c| {
                c.iter()
                    .filter(|b| b.kind.as_deref() == Some("tool_use"))
                    .count() as u64
            })
            .unwrap_or(0);

        out.push(LineRec {
            date,
            hour,
            uuid: parsed.uuid.clone().unwrap_or_default(),
            session: parsed.session_id.clone().unwrap_or_default(),
            is_message,
            has_usage,
            tok_key,
            model,
            input,
            output,
            cache_read,
            cache_create,
            tool_calls,
        });
    }
    out
}

// ── Background thread ─────────────────────────────────────────────────────────

/// Events emitted by the background loader thread.
pub(crate) enum TokensEvent {
    /// Phase 1: stats-cache parsed, transcript sweep not yet run. Lets the tab
    /// paint lifetime/model data instantly instead of blocking on the sweep.
    Base(Box<TokenStats>),
    /// Phase 2: the live transcript top-up merged in (today card, recent days,
    /// reconstructed lifetime counts). Supersedes the matching `Base`.
    Loaded(Box<TokenStats>),
    Failed,
}

/// Spawn the token-stats background worker. Each run emits two events: `Base`
/// (the instant stats-cache parse) then `Loaded` (after the transcript top-up).
/// Loads once on start, then loops on `refresh_rx.recv_timeout(REFRESH_INTERVAL)`,
/// reusing a per-file cache so each sweep re-reads only changed transcripts. Exits
/// when `refresh_rx` disconnects (TUI shutdown).
///
/// Unlike `status`/`pricing`, this loop is match-first-then-send (not
/// `run_polling_loop`'s tick-first shape): the first reload after the cold load
/// happens only once the interval elapses or a signal arrives, avoiding a
/// duplicate emit at startup.
///
/// `claude_dir` must already be resolved by the caller — the worker never
/// re-resolves `home_dir()`, matching the pattern in `status::spawn`.
pub(crate) fn spawn(tx: Sender<TokensEvent>, refresh_rx: Receiver<()>, claude_dir: PathBuf) {
    std::thread::spawn(move || {
        let mut cache = TopUpCache::default();

        let run = |cache: &mut TopUpCache| {
            let Some(mut base) = load_base(&claude_dir) else {
                let _ = tx.send(TokensEvent::Failed);
                return;
            };
            // Phase 1 — instant lifetime/model data, no transcript IO.
            let _ = tx.send(TokensEvent::Base(Box::new(base.clone())));
            // Phase 2 — refresh the per-file cache (changed files only) and merge.
            let today = today_date();
            let lcd = base.last_computed_date.clone();
            refresh_topup_cache(&claude_dir, lcd.as_deref(), cache);
            merge_topup(&mut base, cache, lcd.as_deref(), &today);
            let _ = tx.send(TokensEvent::Loaded(Box::new(base)));
        };

        run(&mut cache);

        loop {
            match refresh_rx.recv_timeout(REFRESH_INTERVAL) {
                Ok(()) => while refresh_rx.try_recv().is_ok() {},
                Err(RecvTimeoutError::Timeout) => {}
                Err(RecvTimeoutError::Disconnected) => return,
            }
            run(&mut cache);
        }
    });
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
#[path = "../tests/inline/tokens.rs"]
mod tests;