crew-watch 0.2.1

Terminal monitor for firstmate fleets: htop-style system overview plus one row per running AI agent session, with subtree-aggregated CPU and memory, task and activity.
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
//! Layered "what is it working on" resolution.
//!
//! Sources are consulted in order; the first to answer wins:
//! 1. **Firstmate fleet records** ([`crate::meta`]), matched by the agent's cwd.
//!    The task line is prefixed with the project so a human can see at a
//!    glance which project a worker is on: when the record carries `project=`,
//!    its basename prefixes the line (`crew-watch: right-align the numeric
//!    columns`). The project comes from the record, not the task id — ids only
//!    *happen* to start with the project name. With a project known, the
//!    bracketed task-id suffix is dropped (it only ever carried the project);
//!    a record without `project=` degrades to the older `title [task-id]`
//!    form so the id still serves as secondary reference.
//! 2. **Unmatched with a cwd**: the project name (git repo name via
//!    [`crate::project`], else the cwd basename), labelled
//!    `interactive @ <project>` when no prompt arg is detected (an interactive
//!    session a human is driving). Bare flag noise like `-p --verbose` is
//!    never shown alone.
//! 3. **No cwd at all**: a trimmed positional argv excerpt, else the runtime
//!    name.
//!
//! The project prefix travels as data ([`ResolvedTask::project`]), separate
//! from the title text: titles themselves may contain `": "` (e.g. `away mode
//! unusable: resurface fires`), so the prefix cannot be recovered by parsing
//! the composed line. Renderers compose and width-fit through
//! [`compose_task_line`] / [`fit_task_line`], which guarantee that under
//! truncation **the project survives while the title is what shortens**.
//!
//! The layering mirrors the detection table: to support a new fleet format,
//! prepend another lookup step at the top of [`TaskInfo::resolve`].
//!
//! ### Record freshness (why the caller reloads per tick)
//!
//! Layer 1 matches an agent to a task by the process's cwd, and firstmate
//! hands out worker copies from a **recycled worktree pool**: the same path is
//! reused by task after task. So the fleet records and titles passed in here
//! must reflect *current* on-disk state, not whatever was read when crew-watch
//! launched — otherwise an agent spawned after launch inherits a path that, in
//! a stale snapshot, still belongs to a task that finished hours ago, and gets
//! rendered with that dead task's title at full confidence. [`crate::app::App`]
//! therefore re-reads both sources from the firstmate home on every tick;
//! [`crate::meta::load_fm_home`] and [`crate::titles::load_task_titles`] are
//! best-effort, so a vanished source degrades to the lower layers instead of
//! keeping a ghost label. The resolution logic itself is correct as written —
//! only the age of its inputs was the bug.

use std::path::Path;

use crate::detect::AgentKind;
use crate::meta::{find_by_cwd, TaskRecord};
use crate::titles::TaskTitles;

/// A one-line description of an agent's current work.
pub struct TaskInfo;

/// Result of [`TaskInfo::resolve`]: the task text plus, for fleet-matched
/// sessions, the project basename to render as its protected prefix.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct ResolvedTask {
    /// The task line without its project prefix (title, degraded
    /// `title [task-id]`, task id, `interactive @ <project>`, argv excerpt,
    /// or runtime name).
    pub line: String,
    /// Basename of the fleet record's `project=` path; `None` for non-fleet
    /// or project-less rows (their line already stands alone).
    pub project: Option<String>,
    /// True only for the layer-2 `interactive @ <project>` form: a non-fleet
    /// session with no prompt/headless argv marker, i.e. one a human is
    /// driving. Drives the STATE column's interactive glyph.
    pub interactive: bool,
}

/// Title is capped so the TASK column stays readable even when the backlog
/// entry is a multi-sentence paragraph. Width-aware fitting to the actual
/// column happens at render time via [`fit_task_line`].
const TITLE_DISPLAY_MAX: usize = 80;
/// A positional argv token longer than this is treated as a prompt blob (not
/// shown, and its presence marks the session as non-interactive).
const PROMPT_LEN_THRESHOLD: usize = 100;
/// Max chars for the positional argv excerpt when no cwd is available.
const ARGV_EXCERPT_MAX_CHARS: usize = 40;

impl TaskInfo {
    /// Resolve the task line for an agent process. Pure over its inputs so it
    /// can be unit-tested with fixture data.
    ///
    /// `project_name` lazily supplies a pre-computed project name (the caller
    /// may resolve it from git); it is only invoked for sessions that are not
    /// fleet-matched, and when it yields `None` the cwd basename is used.
    pub fn resolve(
        records: &[TaskRecord],
        titles: &TaskTitles,
        cwd: Option<&Path>,
        project_name: impl FnOnce() -> Option<String>,
        cmdline: &[String],
        kind: &AgentKind,
    ) -> ResolvedTask {
        // Layer 1: firstmate fleet records, matched by worktree == cwd.
        if let Some(cwd) = cwd {
            if let Some(rec) = find_by_cwd(records, cwd) {
                return fleet_task_parts(rec, titles);
            }
        }

        // Layer 2: unmatched — prefer the project name over argv.
        let project = project_name().or_else(|| {
            cwd.and_then(|p| p.file_name())
                .map(|s| s.to_string_lossy().into_owned())
        });
        if let Some(project) = project {
            let (line, interactive) = if has_prompt_arg(cmdline) {
                // Autonomous session whose task we could not identify: show
                // where it is working without the misleading "interactive".
                (project.clone(), false)
            } else {
                (format!("interactive @ {}", project), true)
            };
            return ResolvedTask {
                line,
                project: None,
                interactive,
            };
        }

        // Layer 3: no cwd — positional argv excerpt, else the runtime name.
        let line = positional_argv_excerpt(cmdline).unwrap_or_else(|| kind.display.to_string());
        ResolvedTask {
            line,
            project: None,
            interactive: false,
        }
    }
}

/// Split a fleet-matched record into its renderable parts: the task text and
/// the project-basename prefix. With a project known, the task id is not
/// appended to a found title (the project prefix replaces its role); without
/// one, the older `title [task-id]` form keeps the id as secondary reference.
fn fleet_task_parts(rec: &TaskRecord, titles: &TaskTitles) -> ResolvedTask {
    let project = project_basename(rec.project.as_deref());
    let line = match titles.title_for(&rec.task_id) {
        Some(title) => {
            let title = truncate_with_ellipsis(title.trim(), TITLE_DISPLAY_MAX);
            match project.as_deref() {
                Some(_) => title,
                None => format!("{} [{}]", title, rec.task_id),
            }
        }
        None => rec.task_id.clone(),
    };
    ResolvedTask {
        line,
        project,
        interactive: false,
    }
}

/// Basename of a fleet record's `project=` path (`…/projects/crew-watch` →
/// `crew-watch`). `None` when absent, empty, or not a path with a final
/// component (so the row simply renders unprefixed).
fn project_basename(project: Option<&str>) -> Option<String> {
    let p = project?;
    let name = Path::new(p).file_name()?.to_string_lossy().into_owned();
    if name.is_empty() {
        None
    } else {
        Some(name)
    }
}

/// Compose the full, unfitted task line: `<project>: <line>` for fleet rows
/// with a project, the line as-is otherwise.
pub fn compose_task_line(project: Option<&str>, line: &str) -> String {
    match project.filter(|p| !p.is_empty()) {
        Some(p) => format!("{}: {}", p, line),
        None => line.to_string(),
    }
}

/// Fit a task line into `width` display columns, with the project prefix
/// protected: when space is tight the task text is what shortens (bracketed
/// id dropped first, then ellipsis), and only when the width cannot even hold
/// the project does the project itself ellipsize. Lines without a project
/// keep the historical fitting: full line when it fits, id dropped, then
/// ellipsis.
pub fn fit_task_line(project: Option<&str>, line: &str, width: usize) -> String {
    let Some(p) = project.filter(|p| !p.is_empty()) else {
        return fit_plain(line, width);
    };
    let prefix_len = p.chars().count() + 2; // "project: "
    if line.chars().count() + prefix_len <= width {
        return compose_task_line(Some(p), line);
    }
    if width > prefix_len {
        return format!("{}: {}", p, fit_plain(line, width - prefix_len));
    }
    truncate_with_ellipsis(p, width)
}

/// Historical fitting for an unprefixed task line: the full `title [task-id]`
/// line is kept when it fits; when space is tight the bracketed id is dropped
/// first, and only then is the remaining title ellipsized to the width.
fn fit_plain(line: &str, width: usize) -> String {
    if line.chars().count() <= width {
        return line.to_string();
    }
    if let Some(title) = strip_id_suffix(line) {
        if title.chars().count() <= width {
            return title.to_string();
        }
        return truncate_with_ellipsis(title, width);
    }
    truncate_with_ellipsis(line, width)
}

/// Strip a trailing ` [task-id]` suffix from a task line, returning the title
/// part. Returns `None` when the line has no such suffix.
fn strip_id_suffix(line: &str) -> Option<&str> {
    if !line.ends_with(']') {
        return None;
    }
    let idx = line.rfind(" [")?;
    Some(line[..idx].trim_end())
}

/// Truncate a string to `max_chars` Unicode scalar values, appending `...` if
/// truncation occurred. Truncation always lands on a UTF-8 char boundary.
pub fn truncate_with_ellipsis(s: &str, max_chars: usize) -> String {
    if s.chars().count() <= max_chars {
        return s.to_string();
    }
    if max_chars <= 3 {
        return s.chars().take(max_chars).collect();
    }
    let cut = max_chars - 3;
    let mut out: String = s.chars().take(cut).collect();
    out.push_str("...");
    out
}

/// Detect whether argv marks the session as autonomous rather than
/// interactive. True when:
/// - a `--prompt` flag is present (with or without a following value), or
/// - a headless/print-mode flag (`-p`, `--print`, `--headless`) is present, or
/// - a positional token (not starting with `-`) is longer than the threshold.
fn has_prompt_arg(cmdline: &[String]) -> bool {
    for tok in cmdline.iter().skip(1) {
        if tok == "--prompt" || tok == "-p" || tok == "--print" || tok == "--headless" {
            return true;
        }
        if tok.starts_with("--prompt=") {
            return true;
        }
        if !tok.starts_with('-') && tok.len() > PROMPT_LEN_THRESHOLD {
            return true;
        }
    }
    false
}

/// Extract a short excerpt of meaningful positional arguments from argv[1..]:
/// flags, their values, and prompt blobs are skipped, so the result never
/// contains opaque flag noise. Returns `None` when nothing meaningful remains.
fn positional_argv_excerpt(cmdline: &[String]) -> Option<String> {
    /// Flags that consume the following token as their value (separate form).
    const VALUE_FLAGS: &[&str] = &["--model", "--effort", "--prompt"];

    let mut out = String::new();
    let mut skip_next = false;
    for tok in cmdline.iter().skip(1) {
        if skip_next {
            skip_next = false;
            continue;
        }
        if tok.starts_with('-') {
            if VALUE_FLAGS.contains(&tok.as_str()) {
                skip_next = true;
            }
            continue;
        }
        if tok.len() > PROMPT_LEN_THRESHOLD {
            continue;
        }
        if !out.is_empty() {
            out.push(' ');
        }
        out.push_str(tok);
        if out.len() >= ARGV_EXCERPT_MAX_CHARS {
            break;
        }
    }
    if out.is_empty() {
        None
    } else {
        Some(truncate_with_ellipsis(&out, ARGV_EXCERPT_MAX_CHARS))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::detect::AGENT_KINDS;
    use crate::meta::parse_meta;
    use crate::titles::parse_backlog;
    use std::path::PathBuf;

    fn kind(id: &str) -> &'static AgentKind {
        AGENT_KINDS.iter().find(|k| k.id == id).unwrap()
    }

    fn titles_from_backlog(content: &str) -> TaskTitles {
        TaskTitles::from_pairs(parse_backlog(content))
    }

    const SAMPLE_META: &str = "endpoint_task_id=fm-afk-resurface-loop\n\
worktree=/home/crew/wt/firstmate\n\
project=/home/crew/agents/firstmate/projects/firstmate\nharness=opencode\n";

    const SAMPLE_BACKLOG: &str = "\
- [ ] fm-afk-resurface-loop - away mode unusable: resurface fires ~1/sec (repo: firstmate) (kind: ship)
- [ ] crew-watch-v1 - build crew-watch v1 (repo: crew-watch) (kind: ship)
";

    // --- Layer 1: fleet-matched ---

    #[test]
    fn fleet_matched_resolves_title_and_project_prefix() {
        let recs = vec![parse_meta(SAMPLE_META)];
        let titles = titles_from_backlog(SAMPLE_BACKLOG);
        let cwd = PathBuf::from("/home/crew/wt/firstmate");
        let cmdline = vec![
            "opencode".to_string(),
            "--model".to_string(),
            "zai-coding-plan/glm-5.2".to_string(),
        ];
        let resolved = TaskInfo::resolve(
            &recs,
            &titles,
            Some(&cwd),
            || None,
            &cmdline,
            kind("opencode"),
        );
        // Project from the record's `project=` basename; no id suffix (the
        // prefix replaces its role).
        assert_eq!(resolved.project.as_deref(), Some("firstmate"));
        assert_eq!(resolved.line, "away mode unusable: resurface fires ~1/sec");
        assert_eq!(
            compose_task_line(resolved.project.as_deref(), &resolved.line),
            "firstmate: away mode unusable: resurface fires ~1/sec"
        );
    }

    #[test]
    fn fleet_matched_no_title_falls_back_to_id() {
        let recs = vec![parse_meta(SAMPLE_META)];
        let titles = TaskTitles::default();
        let cwd = PathBuf::from("/home/crew/wt/firstmate");
        let resolved =
            TaskInfo::resolve(&recs, &titles, Some(&cwd), || None, &[], kind("opencode"));
        assert_eq!(resolved.line, "fm-afk-resurface-loop");
        assert_eq!(resolved.project.as_deref(), Some("firstmate"));
        assert_eq!(
            compose_task_line(resolved.project.as_deref(), &resolved.line),
            "firstmate: fm-afk-resurface-loop"
        );
    }

    #[test]
    fn fleet_matched_long_title_is_truncated() {
        let recs = vec![parse_meta(SAMPLE_META)];
        let long_title = format!(
            "- [ ] fm-afk-resurface-loop - {} (repo: firstmate) (kind: ship)",
            "a".repeat(200)
        );
        let titles = titles_from_backlog(&long_title);
        let cwd = PathBuf::from("/home/crew/wt/firstmate");
        let resolved =
            TaskInfo::resolve(&recs, &titles, Some(&cwd), || None, &[], kind("opencode"));
        // Title capped to 80 chars (77 + "..."); no id suffix to strip.
        assert_eq!(resolved.line.chars().count(), 80);
        assert!(resolved.line.ends_with("..."));
        assert!(!resolved.line.contains('['));
    }

    #[test]
    fn fleet_record_without_project_keeps_id_suffix() {
        // Degrade: a record lacking `project=` renders the older
        // `title [task-id]` form so the id still carries its reference role.
        let meta = "endpoint_task_id=fm-afk-resurface-loop\nworktree=/home/crew/wt/firstmate\n";
        let recs = vec![parse_meta(meta)];
        let titles = titles_from_backlog(SAMPLE_BACKLOG);
        let cwd = PathBuf::from("/home/crew/wt/firstmate");
        let resolved =
            TaskInfo::resolve(&recs, &titles, Some(&cwd), || None, &[], kind("opencode"));
        assert_eq!(resolved.project, None);
        assert_eq!(
            resolved.line,
            "away mode unusable: resurface fires ~1/sec [fm-afk-resurface-loop]"
        );
    }

    #[test]
    fn project_basename_handles_nested_paths_and_trailing_slashes() {
        assert_eq!(
            project_basename(Some("/home/crew/agents/firstmate/projects/crew-watch")),
            Some("crew-watch".to_string())
        );
        assert_eq!(
            project_basename(Some("/home/crew/agents/firstmate/projects/crew-watch/")),
            Some("crew-watch".to_string())
        );
        assert_eq!(
            project_basename(Some("crew-watch")),
            Some("crew-watch".to_string())
        );
        assert_eq!(project_basename(Some("")), None);
        assert_eq!(project_basename(Some("/")), None);
        assert_eq!(project_basename(Some(".")), None);
        assert_eq!(project_basename(None), None);
    }

    // --- Layer 2: unmatched with cwd ---

    #[test]
    fn unmatched_interactive_shows_project() {
        let cwd = PathBuf::from("/home/crew/firstmate");
        let cmdline = vec![
            "claude".to_string(),
            "--model".to_string(),
            "opus".to_string(),
        ];
        let line = TaskInfo::resolve(
            &[],
            &TaskTitles::default(),
            Some(&cwd),
            || None,
            &cmdline,
            kind("claude"),
        );
        assert_eq!(line.line, "interactive @ firstmate");
    }

    #[test]
    fn unmatched_no_args_shows_interactive() {
        let cwd = PathBuf::from("/home/crew/firstmate");
        let line = TaskInfo::resolve(
            &[],
            &TaskTitles::default(),
            Some(&cwd),
            || None,
            &["claude".to_string()],
            kind("claude"),
        );
        assert_eq!(line.line, "interactive @ firstmate");
    }

    #[test]
    fn unmatched_uses_project_name_override() {
        // A no-mistakes worktree: cwd basename is a ULID, but the caller
        // resolved the real repo name from git.
        let cwd = PathBuf::from("/home/crew/.nm/wt/abc/01KZRC1AP2FQZESTNY9C9HXA7A");
        let line = TaskInfo::resolve(
            &[],
            &TaskTitles::default(),
            Some(&cwd),
            || Some("firstmate".to_string()),
            &["claude".to_string()],
            kind("claude"),
        );
        assert_eq!(line.line, "interactive @ firstmate");
    }

    #[test]
    fn unmatched_never_shows_bare_flags() {
        // The captain's observed complaint: `-p --verbose` alone. Only the
        // project name is shown, and `-p` marks the session headless, so no
        // "interactive" prefix either.
        let cwd = PathBuf::from("/wt/crew-watch");
        let cmdline = vec![
            "opencode".to_string(),
            "-p".to_string(),
            "--verbose".to_string(),
        ];
        let line = TaskInfo::resolve(
            &[],
            &TaskTitles::default(),
            Some(&cwd),
            || None,
            &cmdline,
            kind("opencode"),
        );
        assert_eq!(line.line, "crew-watch");
    }

    #[test]
    fn unmatched_headless_flags_show_project_only() {
        // `--print` / `--headless` also mark the session autonomous even when
        // the prompt itself arrives via stdin.
        let cwd = PathBuf::from("/wt/crew-watch");
        for flag in ["--print", "--headless"] {
            let cmdline = vec!["claude".to_string(), flag.to_string()];
            let line = TaskInfo::resolve(
                &[],
                &TaskTitles::default(),
                Some(&cwd),
                || None,
                &cmdline,
                kind("claude"),
            );
            assert_eq!(line.line, "crew-watch");
        }
    }

    #[test]
    fn unmatched_with_prompt_arg_shows_project_only() {
        // A firstmate-launched task whose meta file we could not read: argv has
        // a prompt blob. Don't say "interactive" (it is autonomous), but show
        // where it is working.
        let cwd = PathBuf::from("/home/crew/.treehouse/firstmate-x/1/firstmate");
        let huge = "x".repeat(500);
        let cmdline = vec![
            "opencode".to_string(),
            "--model".to_string(),
            "glm-5.2".to_string(),
            "--prompt".to_string(),
            huge,
        ];
        let line = TaskInfo::resolve(
            &[],
            &TaskTitles::default(),
            Some(&cwd),
            || None,
            &cmdline,
            kind("opencode"),
        );
        assert_eq!(line.line, "firstmate");
    }

    #[test]
    fn unmatched_with_positional_prompt_shows_project_only() {
        // claude-style: prompt is a trailing positional arg (no --prompt flag).
        let cwd = PathBuf::from("/wt/proj");
        let cmdline = vec![
            "claude".to_string(),
            "--model".to_string(),
            "opus".to_string(),
            "x".repeat(500),
        ];
        let line = TaskInfo::resolve(
            &[],
            &TaskTitles::default(),
            Some(&cwd),
            || None,
            &cmdline,
            kind("claude"),
        );
        assert_eq!(line.line, "proj");
    }

    // --- Layer 3: no cwd ---

    #[test]
    fn no_cwd_shows_positional_argv_excerpt() {
        let cmdline = vec!["claude".to_string(), "fix the bug".to_string()];
        let line = TaskInfo::resolve(
            &[],
            &TaskTitles::default(),
            None,
            || None,
            &cmdline,
            kind("claude"),
        );
        assert_eq!(line.line, "fix the bug");
    }

    #[test]
    fn no_cwd_skips_flags_and_prompt_blobs() {
        let cmdline = vec![
            "opencode".to_string(),
            "--model".to_string(),
            "glm-5.2".to_string(),
            "-p".to_string(),
            "--verbose".to_string(),
            "x".repeat(500),
            "do thing".to_string(),
        ];
        let line = TaskInfo::resolve(
            &[],
            &TaskTitles::default(),
            None,
            || None,
            &cmdline,
            kind("opencode"),
        );
        // Only positional, non-huge tokens survive: "do thing".
        assert_eq!(line.line, "do thing");
    }

    #[test]
    fn no_cwd_no_meaningful_args_uses_kind() {
        let cmdline = vec![
            "claude".to_string(),
            "--model".to_string(),
            "opus".to_string(),
        ];
        let line = TaskInfo::resolve(
            &[],
            &TaskTitles::default(),
            None,
            || None,
            &cmdline,
            kind("claude"),
        );
        assert_eq!(line.line, "claude");
    }

    #[test]
    fn no_cwd_empty_cmdline_uses_kind() {
        let line = TaskInfo::resolve(
            &[],
            &TaskTitles::default(),
            None,
            || None,
            &[],
            kind("claude"),
        );
        assert_eq!(line.line, "claude");
    }

    // --- fit_task_line ---

    #[test]
    fn fit_full_line_kept_when_it_fits() {
        let line = "fix flood [fm-afk-resurface-loop]";
        assert_eq!(fit_task_line(None, line, 40), line);
        assert_eq!(fit_task_line(None, line, line.chars().count()), line);
    }

    #[test]
    fn fit_drops_id_first_when_tight() {
        let line = "fix away-mode resurface flood [fm-afk-resurface-loop]";
        assert_eq!(
            fit_task_line(None, line, 30),
            "fix away-mode resurface flood"
        );
    }

    #[test]
    fn fit_ellipsizes_title_when_even_title_is_too_wide() {
        let line = "fix away-mode resurface flood [fm-afk-resurface-loop]";
        assert_eq!(fit_task_line(None, line, 10), "fix awa...");
    }

    #[test]
    fn fit_line_without_id_suffix_gets_ellipsis() {
        assert_eq!(
            fit_task_line(None, "interactive @ firstmate", 12),
            "interacti..."
        );
    }

    #[test]
    fn fit_multibyte_title_is_char_boundary_safe() {
        let line = format!("{} [task-x]", "ä".repeat(30));
        let fitted = fit_task_line(None, &line, 10);
        assert_eq!(fitted.chars().count(), 10);
        assert!(fitted.ends_with("..."));
    }

    // --- fit_task_line with a protected project prefix ---

    const PROJECT: &str = "crew-watch";
    const TITLE: &str = "right-align the ELAPSED, CPU% and MEM columns";
    const FULL: usize = PROJECT.len() + 2 + TITLE.len(); // "crew-watch: " + title

    #[test]
    fn fit_project_line_kept_whole_when_it_fits() {
        assert_eq!(
            fit_task_line(Some(PROJECT), TITLE, FULL),
            "crew-watch: right-align the ELAPSED, CPU% and MEM columns"
        );
        // One column to spare keeps it too.
        assert_eq!(fit_task_line(Some(PROJECT), TITLE, FULL + 1).len(), FULL);
    }

    #[test]
    fn fit_project_survives_while_title_shortens() {
        // A width that cannot hold the whole line still holds the full
        // project frame; the title ellipsizes inside it.
        let fitted = fit_task_line(Some(PROJECT), TITLE, FULL - 10);
        assert!(fitted.starts_with("crew-watch: "));
        assert!(fitted.ends_with("..."));
        assert_eq!(fitted.chars().count(), FULL - 10);
        // Tighter still: more title goes, the prefix never does.
        let tighter = fit_task_line(Some(PROJECT), TITLE, PROJECT.len() + 2 + 8);
        assert_eq!(tighter, "crew-watch: right...");
    }

    #[test]
    fn fit_project_line_shortens_title_id_drop_first() {
        // Defensive: even if a line still carries an id suffix (the degraded,
        // project-less resolve form), the fitter drops it before ellipsizing,
        // inside the protected project frame.
        let line = "fix away-mode resurface flood [fm-afk-resurface-loop]";
        // "firstmate: " (11) + "fix away-mode resurface flood" (29) = 40.
        assert_eq!(
            fit_task_line(Some("firstmate"), line, 40),
            "firstmate: fix away-mode resurface flood"
        );
    }

    #[test]
    fn fit_project_itself_ellipsizes_only_when_width_cannot_hold_it() {
        // Width below "project: " + one title char: the project is what the
        // captain wants to read, so it is the last thing to shorten — and it
        // never overflows.
        assert_eq!(fit_task_line(Some(PROJECT), TITLE, 8), "crew-...");
        assert_eq!(fit_task_line(Some(PROJECT), TITLE, PROJECT.len()), PROJECT);
        // Exactly the prefix width: no room for a title char, project stays.
        assert_eq!(
            fit_task_line(Some(PROJECT), TITLE, PROJECT.len() + 2),
            PROJECT
        );
        // One char of budget left: prefix plus a single title character.
        assert_eq!(
            fit_task_line(Some(PROJECT), TITLE, PROJECT.len() + 3),
            "crew-watch: r"
        );
    }

    #[test]
    fn fit_project_multibyte_is_char_boundary_safe() {
        let title = "ä".repeat(30);
        let fitted = fit_task_line(Some("flotte"), &title, 12);
        assert!(fitted.starts_with("flotte: "));
        assert_eq!(fitted.chars().count(), 12);
        assert!(fitted.ends_with("..."));
    }

    #[test]
    fn fit_empty_project_falls_back_to_plain_fitting() {
        let line = "fix flood [fm-afk-resurface-loop]";
        assert_eq!(fit_task_line(Some(""), line, 40), line);
        assert_eq!(fit_task_line(None, line, 30), "fix flood".to_string());
    }

    // --- truncate_with_ellipsis ---

    #[test]
    fn truncate_short_string_unchanged() {
        assert_eq!(truncate_with_ellipsis("hello", 10), "hello");
        assert_eq!(truncate_with_ellipsis("hello", 5), "hello");
    }

    #[test]
    fn truncate_long_string_gets_ellipsis() {
        assert_eq!(truncate_with_ellipsis("hello world", 8), "hello...");
    }

    #[test]
    fn truncate_exact_boundary_no_ellipsis() {
        assert_eq!(truncate_with_ellipsis("hello", 5), "hello");
    }

    #[test]
    fn truncate_multibyte_safe() {
        let s = "ä".repeat(25);
        let t = truncate_with_ellipsis(&s, 10);
        assert_eq!(t.chars().count(), 10);
        assert!(t.ends_with("..."));
    }

    #[test]
    fn truncate_tiny_max() {
        assert_eq!(truncate_with_ellipsis("hello", 3), "hel");
        assert_eq!(truncate_with_ellipsis("hello", 0), "");
    }
}