cctop 0.2.0

An htop-like terminal monitor for AI coding agent sessions (Claude Code, Codex, Cursor, Gemini CLI, OpenCode, Pi, Windsurf)
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
//! Session-table column definitions: rendering, sorting, and tooltips.

use crate::session::{ActivityState, Session, Subagent, SubagentStatus};
use crate::util;
use chrono::{DateTime, Utc};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{LazyLock, Mutex, PoisonError};
use std::time::{Duration, Instant};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColumnId {
    Status,
    Last,
    Duration,
    Cost,
    CostHour,
    CostToday,
    Context,
    Cpu,
    Memory,
    Tools,
    TokenTotal,
    TokenRate,
    Model,
    Harness,
    Branch,
    Project,
}

pub struct Column {
    pub id: ColumnId,
    pub label: &'static str,
    /// `None` for the flexible column that absorbs leftover width.
    pub width: Option<u16>,
    pub right_align: bool,
    pub desc: &'static str,
}

/// Columns shown in the table, in display order. Also the sortable set.
pub const COLUMNS: &[Column] = &[
    Column {
        id: ColumnId::Status,
        label: " ",
        // Two wide so a subagent's dot can sit one cell in from its parent's.
        // The tree glyph that says "child" lives out in the Project column, and
        // reading the far side of the row to find out what the near side is
        // describing is the confusion this indent removes.
        width: Some(2),
        right_align: false,
        desc: "Status: ● working (green = fresh, greyer = idle), amber ● awaiting input, red ● API error, ○ stopped; indented one cell for a subagent",
    },
    Column {
        id: ColumnId::Last,
        label: "LAST",
        width: Some(5),
        right_align: true,
        desc: "Time since last activity",
    },
    Column {
        id: ColumnId::Duration,
        label: "DUR",
        width: Some(6),
        right_align: true,
        desc: "Session duration (first to last activity)",
    },
    Column {
        id: ColumnId::Cost,
        label: "$",
        width: Some(9),
        right_align: true,
        desc: "Estimated cost from per-token API pricing (LiteLLM).\nFlat-rate plans (Max, Pro, Team) bill differently,\nso this may not match your invoice.",
    },
    Column {
        id: ColumnId::CostHour,
        label: "$/1H",
        width: Some(7),
        right_align: true,
        desc: "Estimated cost in the current local clock hour",
    },
    Column {
        id: ColumnId::CostToday,
        label: "$/24H",
        width: Some(7),
        right_align: true,
        desc: "Estimated cost since midnight (local time)",
    },
    Column {
        id: ColumnId::Context,
        label: "CTX%",
        width: Some(6),
        right_align: true,
        desc: "Context window used, as a share of the auto-compact threshold",
    },
    Column {
        id: ColumnId::Cpu,
        label: "CPU%",
        width: Some(5),
        right_align: true,
        desc: "CPU usage across the session's process tree",
    },
    Column {
        id: ColumnId::Memory,
        label: "MEM",
        width: Some(6),
        right_align: true,
        desc: "Resident memory across the session's process tree",
    },
    Column {
        id: ColumnId::Tools,
        label: "TOOLS",
        width: Some(6),
        right_align: true,
        desc: "Total tool invocations in the session",
    },
    Column {
        id: ColumnId::TokenTotal,
        label: "TOKENS",
        width: Some(8),
        right_align: true,
        desc: "Total input and output tokens used by the session",
    },
    Column {
        id: ColumnId::TokenRate,
        label: "TOK/m",
        width: Some(7),
        right_align: true,
        desc: "Token rate per minute (exponential moving average)",
    },
    Column {
        id: ColumnId::Model,
        label: "MODEL",
        width: Some(14),
        right_align: false,
        desc: "Model used by the session",
    },
    Column {
        id: ColumnId::Harness,
        label: "HARNESS",
        width: Some(10),
        right_align: false,
        desc: "Where the agent is hosted, such as Cursor or a terminal CLI",
    },
    Column {
        id: ColumnId::Branch,
        label: "BRANCH",
        width: Some(12),
        right_align: false,
        desc: "Git branch checked out in the session's working directory,\nor @<commit> when HEAD is detached",
    },
    Column {
        id: ColumnId::Project,
        label: "PROJECT",
        width: None,
        right_align: false,
        desc: "Session title if renamed, otherwise the working directory",
    },
];

/// Seconds since a session last did anything.
fn age_secs(s: &Session, now: &DateTime<Utc>) -> Option<i64> {
    util::parse_ts(&s.last_active).map(|d| (now.timestamp() - d.timestamp()).max(0))
}

/// Cell text for one column. Empty means "nothing worth showing".
pub fn render_cell(id: ColumnId, s: &Session, now: &DateTime<Utc>) -> String {
    match id {
        ColumnId::Status => match s.activity_state {
            ActivityState::WaitingForInput | ActivityState::ApiError => "".into(),
            ActivityState::Working if s.is_running() => "".into(),
            ActivityState::Working => "".into(),
        },
        ColumnId::Last => util::relative_age(&s.last_active, now),
        ColumnId::Duration => util::session_duration(&s.started_at, &s.last_active),
        ColumnId::Cost => match s.total_cost {
            _ if !s.cost_available => "".into(),
            _ if s.cost_is_free => "FREE".into(),
            Some(c) => util::compact_usd(c),
            None => "incl".into(),
        },
        ColumnId::CostHour => {
            if !s.cost_available {
                "".into()
            } else if s.cost_is_free {
                "FREE".into()
            } else if s.total_cost.is_none() {
                "incl".into()
            } else if s.cost_hour > 0.0 {
                util::compact_usd(s.cost_hour)
            } else {
                "".into()
            }
        }
        ColumnId::CostToday => {
            if !s.cost_available {
                "".into()
            } else if s.cost_is_free {
                "FREE".into()
            } else if s.total_cost.is_none() {
                "incl".into()
            } else if s.cost_today > 0.0 {
                util::compact_usd(s.cost_today)
            } else {
                "".into()
            }
        }
        ColumnId::Context => match &s.context {
            None => "".into(),
            // Only while something is there to finish it. A session that
            // compacted and stopped keeps its last measured percentage, which is
            // what the context panel breaks down for the same session.
            Some(_) if s.is_compacting() => "COMPCT".into(),
            Some(c) => {
                let pct = c.percent_to_compact().round() as i64;
                if pct > 100 {
                    ">100%".into()
                } else {
                    format!("{pct}%")
                }
            }
        },
        ColumnId::Cpu => match &s.process {
            Some(p) => format!("{:.1}", p.cpu),
            None => "".into(),
        },
        ColumnId::Memory => s
            .process
            .as_ref()
            .map(|p| util::compact_bytes(p.memory))
            .unwrap_or_default(),
        ColumnId::Tools => {
            if s.tool_count > 0 {
                s.tool_count.to_string()
            } else {
                String::new()
            }
        }
        ColumnId::TokenTotal => {
            let total = s.input_tokens + s.output_tokens;
            if total > 0 {
                util::compact_tokens(total)
            } else {
                String::new()
            }
        }
        ColumnId::TokenRate => {
            if s.tokens_per_min > 0.0 {
                util::compact_tokens(s.tokens_per_min.round() as u64)
            } else {
                String::new()
            }
        }
        ColumnId::Model => util::short_model(&s.model),
        ColumnId::Harness => {
            if s.harness.is_empty() {
                "".into()
            } else {
                s.harness.clone()
            }
        }
        ColumnId::Branch => branch_of(s).unwrap_or_else(|| "".into()),
        ColumnId::Project => s.display_label().to_string(),
    }
}

/// One cell of a subagent's row, under the same columns as its parent.
///
/// A subagent is not a session and most columns have no answer for it: it runs
/// inside the parent's process, so it has no CPU or memory of its own, and its
/// branch and project are the parent's. Those read as `─` rather than repeating
/// the parent's figure down every child row, which would look like the cost of
/// the session had multiplied.
///
/// `last` is the tree glyph plus the agent's type and description, because the
/// Project column is where the eye already looks for what a row *is*.
pub fn render_subagent_cell(
    id: ColumnId,
    sub: &Subagent,
    last: bool,
    now: &DateTime<Utc>,
) -> String {
    match id {
        // Indented into the second cell of the column, so the left edge alone
        // says child-of-the-row-above without hunting for the tree glyph.
        ColumnId::Status => match sub.status {
            SubagentStatus::Running => "".into(),
            SubagentStatus::Done => "".into(),
        },
        ColumnId::Last => match &sub.last_active {
            Some(ts) => util::relative_age(ts, now),
            None => "".into(),
        },
        ColumnId::Duration => {
            if sub.duration_ms > 0 {
                util::compact_duration(sub.duration_ms)
            } else {
                "".into()
            }
        }
        ColumnId::Cost => {
            if sub.cost > 0.0 {
                util::compact_usd(sub.cost)
            } else {
                "".into()
            }
        }
        ColumnId::Context => match &sub.context {
            Some(c) => format!("{}%", c.percent_to_compact().round() as i64),
            None => "".into(),
        },
        ColumnId::Tools => {
            if sub.tool_count > 0 {
                sub.tool_count.to_string()
            } else {
                "".into()
            }
        }
        ColumnId::Model => util::short_model(&sub.model),
        ColumnId::Project => {
            let branch = if last { "└─" } else { "├─" };
            let what = if sub.description.is_empty() {
                sub.agent_type.clone()
            } else {
                format!("{}: {}", sub.agent_type, sub.description)
            };
            format!("{branch} {what}")
        }
        // Belongs to the parent, or is not measured per subagent. Left blank
        // rather than dashed: a dozen `─` down a child row is noise the eye has
        // to step over to reach the columns that do say something.
        ColumnId::CostHour
        | ColumnId::CostToday
        | ColumnId::Cpu
        | ColumnId::Memory
        | ColumnId::TokenTotal
        | ColumnId::TokenRate
        | ColumnId::Harness
        | ColumnId::Branch => String::new(),
    }
}

// ---------------------------------------------------------------------------
// Git branch
// ---------------------------------------------------------------------------

/// How long a branch reading is trusted.
///
/// A checkout is a rare event and a name that is a few seconds out of date is
/// harmless; walking the filesystem once per row per frame is not.
const BRANCH_TTL: Duration = Duration::from_secs(15);

/// A branch reading, and when it was taken. `None` is a real answer — the
/// directory is not in a repository — and is cached just as a name is.
type Reading = (Option<String>, Instant);

/// Working directory -> its last reading.
static BRANCHES: LazyLock<Mutex<HashMap<String, Reading>>> =
    LazyLock::new(|| Mutex::new(HashMap::new()));

/// Branch checked out in a session's working directory, or `None` when it is
/// not in a repository. Cached, so the filter can ask per row per keystroke.
pub fn branch_of(s: &crate::session::Session) -> Option<String> {
    branch(&s.label_source)
}

/// Branch of a working directory, cached.
///
/// `git rev-parse` would be a subprocess per row per frame, and `git2` is a C
/// library and a build step for what is one short file in a documented format.
fn branch(dir: &str) -> Option<String> {
    if dir.is_empty() {
        return None;
    }
    let mut cache = BRANCHES.lock().unwrap_or_else(PoisonError::into_inner);
    if let Some((branch, at)) = cache.get(dir)
        && at.elapsed() < BRANCH_TTL
    {
        return branch.clone();
    }
    let branch = read_head(Path::new(dir));
    cache.insert(dir.to_string(), (branch.clone(), Instant::now()));
    branch
}

/// Read the branch straight out of the repository's `HEAD`.
///
/// Three shapes have to survive this: an ordinary `.git` directory; a `.git`
/// *file* holding a `gitdir:` pointer, which is what a linked worktree or a
/// submodule has and where the interesting branches usually live; and a HEAD
/// carrying a commit id instead of a ref, as during a rebase or a bisect —
/// reported as a short id, because "not on a branch" is itself worth seeing.
fn read_head(start: &Path) -> Option<String> {
    // A session is often launched from a subdirectory of its repository.
    let git = start
        .ancestors()
        .map(|dir| dir.join(".git"))
        .find(|p| p.exists())?;
    let head = if git.is_dir() {
        git.join("HEAD")
    } else {
        let pointer = std::fs::read_to_string(&git).ok()?;
        let target = PathBuf::from(pointer.trim().strip_prefix("gitdir:")?.trim());
        // Absolute for a worktree, relative to the containing directory for a
        // submodule.
        let target = if target.is_absolute() {
            target
        } else {
            git.parent()?.join(target)
        };
        target.join("HEAD")
    };

    let head = std::fs::read_to_string(head).ok()?;
    let head = head.trim();
    if let Some(name) = head.strip_prefix("ref: refs/heads/") {
        Some(name.to_string())
    } else if let Some(other) = head.strip_prefix("ref: ") {
        // Some other ref namespace; show it whole rather than guess at a name.
        Some(other.to_string())
    } else if head.is_empty() {
        None
    } else {
        // Detached. Take chars, not bytes: a corrupt HEAD must not panic here.
        Some(std::iter::once('@').chain(head.chars().take(7)).collect())
    }
}

/// Ordering for a column, ascending. `Session` has no total order of its own,
/// so every comparison funnels through here.
pub fn compare(id: ColumnId, a: &Session, b: &Session, now: &DateTime<Utc>) -> Ordering {
    let num = |x: f64, y: f64| x.partial_cmp(&y).unwrap_or(Ordering::Equal);
    match id {
        ColumnId::Status => a.is_running().cmp(&b.is_running()),
        // Newest-first reads as "ascending" for an age column.
        ColumnId::Last => b.last_active.cmp(&a.last_active),
        ColumnId::Duration => {
            let span = |s: &Session| {
                let start = util::parse_ts(&s.started_at)
                    .map(|d| d.timestamp())
                    .unwrap_or(0);
                let end = util::parse_ts(&s.last_active)
                    .map(|d| d.timestamp())
                    .unwrap_or(start);
                end - start
            };
            span(a).cmp(&span(b))
        }
        // Bundled sessions sort below any priced one.
        ColumnId::Cost => num(a.total_cost.unwrap_or(-1.0), b.total_cost.unwrap_or(-1.0)),
        ColumnId::CostHour => num(a.cost_hour, b.cost_hour),
        ColumnId::CostToday => num(a.cost_today, b.cost_today),
        ColumnId::Context => {
            // A compacting session is the most urgent thing on screen — but only
            // while it is running, or every session that ever ended on a
            // compaction would sit above the live ones forever.
            let rank = |s: &Session| match &s.context {
                _ if s.is_compacting() => f64::INFINITY,
                Some(c) => c.percent_to_compact(),
                None => -1.0,
            };
            num(rank(a), rank(b))
        }
        ColumnId::Cpu => num(
            a.process.as_ref().map(|p| p.cpu as f64).unwrap_or(0.0),
            b.process.as_ref().map(|p| p.cpu as f64).unwrap_or(0.0),
        ),
        ColumnId::Memory => a
            .process
            .as_ref()
            .map(|p| p.memory)
            .unwrap_or(0)
            .cmp(&b.process.as_ref().map(|p| p.memory).unwrap_or(0)),
        ColumnId::Tools => a.tool_count.cmp(&b.tool_count),
        ColumnId::TokenTotal => {
            (a.input_tokens + a.output_tokens).cmp(&(b.input_tokens + b.output_tokens))
        }
        ColumnId::TokenRate => num(a.tokens_per_min, b.tokens_per_min),
        ColumnId::Model => a.model.cmp(&b.model),
        ColumnId::Harness => a.harness.cmp(&b.harness),
        // Sessions outside a repository sort together, below every branch.
        ColumnId::Branch => branch(&a.label_source).cmp(&branch(&b.label_source)),
        ColumnId::Project => a
            .display_label()
            .to_ascii_lowercase()
            .cmp(&b.display_label().to_ascii_lowercase()),
    }
    // Stable tiebreak so rows never swap places between identical refreshes.
    .then_with(|| age_secs(a, now).cmp(&age_secs(b, now)))
    .then_with(|| a.session_id.cmp(&b.session_id))
}

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

    fn session(id: &str) -> Session {
        let mut s = Session::new(Provider::Claude, id.into());
        s.started_at = "2026-01-01T00:00:00Z".into();
        s.last_active = "2026-01-01T00:00:00Z".into();
        s
    }

    #[test]
    fn every_column_has_a_tooltip() {
        for c in COLUMNS {
            assert!(!c.desc.is_empty(), "{} has no description", c.label);
        }
        // Exactly one flexible column, or layout breaks.
        assert_eq!(COLUMNS.iter().filter(|c| c.width.is_none()).count(), 1);
    }

    #[test]
    fn bundled_cost_sorts_below_priced() {
        let now = Utc::now();
        let mut free = session("a");
        free.total_cost = None;
        let mut paid = session("b");
        paid.total_cost = Some(0.0);
        assert_eq!(compare(ColumnId::Cost, &free, &paid, &now), Ordering::Less);
    }

    #[test]
    fn free_usage_is_labeled_in_every_cost_column() {
        let mut s = session("free");
        s.cost_is_free = true;
        s.total_cost = Some(0.0);

        let now = chrono::Utc::now();
        assert_eq!(render_cell(ColumnId::Cost, &s, &now), "FREE");
        assert_eq!(render_cell(ColumnId::CostHour, &s, &now), "FREE");
        assert_eq!(render_cell(ColumnId::CostToday, &s, &now), "FREE");
    }

    #[test]
    fn compacting_sessions_sort_highest_on_context() {
        let now = Utc::now();
        let mut a = session("a");
        a.inferred_running = true;
        a.context = Some(crate::session::ContextUsage {
            used: 10,
            max: 200_000,
            compacted: true,
        });
        let mut b = session("b");
        b.context = Some(crate::session::ContextUsage {
            used: 199_000,
            max: 200_000,
            compacted: false,
        });
        assert_eq!(compare(ColumnId::Context, &a, &b, &now), Ordering::Greater);
    }

    #[test]
    fn ordering_is_total_and_stable() {
        let now = Utc::now();
        let a = session("a");
        let b = session("b");
        // Identical except for id: the tiebreak must still order them.
        assert_eq!(compare(ColumnId::Cpu, &a, &b, &now), Ordering::Less);
        assert_eq!(
            compare(ColumnId::Cpu, &a, &a.clone(), &now),
            Ordering::Equal
        );
    }

    #[test]
    fn bundled_plan_shows_incl_not_a_dash() {
        let now = Utc::now();
        let mut s = session("a");
        s.total_cost = None;
        assert_eq!(render_cell(ColumnId::Cost, &s, &now), "incl");
        assert_eq!(render_cell(ColumnId::CostHour, &s, &now), "incl");
    }

    #[test]
    fn unsupported_cursor_cost_shows_unavailable() {
        let now = Utc::now();
        let mut s = Session::new(Provider::Cursor, "cursor".into());
        s.cost_available = false;
        s.total_cost = None;
        assert_eq!(render_cell(ColumnId::Cost, &s, &now), "");
        assert_eq!(render_cell(ColumnId::CostHour, &s, &now), "");
        assert_eq!(render_cell(ColumnId::CostToday, &s, &now), "");
    }

    #[test]
    fn token_total_combines_input_and_output() {
        let now = Utc::now();
        let mut s = session("a");
        s.input_tokens = 12_000;
        s.output_tokens = 345;
        assert_eq!(render_cell(ColumnId::TokenTotal, &s, &now), "12.3K");
    }

    /// The three HEAD shapes cctop actually meets: a checkout, a linked
    /// worktree pointing elsewhere, and a detached HEAD mid-rebase.
    #[test]
    fn head_is_read_for_a_checkout_a_worktree_and_a_detached_commit() {
        let root = std::env::temp_dir().join(format!("cctop-branch-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);

        let repo = root.join("repo");
        std::fs::create_dir_all(repo.join(".git")).unwrap();
        std::fs::write(repo.join(".git/HEAD"), "ref: refs/heads/feat/idle\n").unwrap();
        assert_eq!(read_head(&repo).as_deref(), Some("feat/idle"));

        // From a subdirectory, since that is where agents are usually started.
        let deep = repo.join("src/ui");
        std::fs::create_dir_all(&deep).unwrap();
        assert_eq!(read_head(&deep).as_deref(), Some("feat/idle"));

        // A linked worktree: `.git` is a file pointing at the real git dir.
        let gitdir = repo.join(".git/worktrees/wt");
        std::fs::create_dir_all(&gitdir).unwrap();
        std::fs::write(gitdir.join("HEAD"), "ref: refs/heads/other\n").unwrap();
        let worktree = root.join("wt");
        std::fs::create_dir_all(&worktree).unwrap();
        std::fs::write(
            worktree.join(".git"),
            format!("gitdir: {}\n", gitdir.display()),
        )
        .unwrap();
        assert_eq!(read_head(&worktree).as_deref(), Some("other"));

        std::fs::write(repo.join(".git/HEAD"), "0123456789abcdef0123\n").unwrap();
        assert_eq!(read_head(&repo).as_deref(), Some("@0123456"));

        assert_eq!(read_head(&root.join("nowhere")), None);
        std::fs::remove_dir_all(&root).ok();
    }

    #[test]
    fn a_session_outside_a_repository_shows_no_branch() {
        let now = Utc::now();
        let mut s = session("a");
        s.label_source = "/nonexistent/definitely/not/a/repo".into();
        assert_eq!(render_cell(ColumnId::Branch, &s, &now), "");
    }

    #[test]
    fn context_cell_flags_compaction_and_overflow() {
        let now = Utc::now();
        let mut s = session("a");
        s.inferred_running = true;
        s.context = Some(crate::session::ContextUsage {
            used: 0,
            max: 200_000,
            compacted: true,
        });
        assert_eq!(render_cell(ColumnId::Context, &s, &now), "COMPCT");
        s.context = Some(crate::session::ContextUsage {
            used: 400_000,
            max: 200_000,
            compacted: false,
        });
        assert_eq!(render_cell(ColumnId::Context, &s, &now), ">100%");
    }

    /// A transcript that ends on a compaction never changes again, so a session
    /// that compacted and then stopped would claim to be compacting for as long
    /// as cctop listed it — and being pinned to the top of CTX% by that claim,
    /// it would push every live session off the screen.
    #[test]
    fn a_stopped_session_that_compacted_no_longer_claims_to_be_compacting() {
        let now = Utc::now();
        let mut stopped = session("a");
        stopped.context = Some(crate::session::ContextUsage {
            used: 100_000,
            max: 200_000,
            compacted: true,
        });
        assert_eq!(render_cell(ColumnId::Context, &stopped, &now), "60%");

        let mut live = session("b");
        live.inferred_running = true;
        live.context = Some(crate::session::ContextUsage {
            used: 199_000,
            max: 200_000,
            compacted: false,
        });
        assert_eq!(
            compare(ColumnId::Context, &stopped, &live, &now),
            Ordering::Less
        );
    }
}