codewhale-tui 0.9.8

Terminal UI for open-source and open-weight coding models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
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
//! `/fleet` roster — the barracks view of the saved agent party.
//!
//! The roster view is the primary `/fleet` face. The first row is the
//! **operator** — the Fleet leader (your live session model). When a user
//! picks a session model they are picking the operator, and every member
//! below is that leader's team. The header names the selected saved Fleet and
//! whether it is user-global or folder-scoped, so scope is never ambiguous.
//! Below the operator sits the merged [`FleetRoster`] (built-in <
//! `[fleet.profiles]` config < `$CODEWHALE_HOME/agents/*.toml` personal <
//! `.codewhale/agents/*.toml` project members)
//! as a scrollable list with a detail pane for the selected row. The view
//! never writes anything; `s` / Enter on a member hands off to the
//! `/fleet setup` wizard for authoring and overrides (the operator row is
//! display-only — its route changes via `/model` or `/provider`). Switch
//! named Fleets with `/fleet fleets`.
//!
//! NOTE: like `fleet_setup.rs`, the copy below is intentionally English for
//! now (#3167 reworks Fleet UI localization); the command entry
//! (`CmdFleetDescription`) is already localized.

use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
    buffer::Buffer,
    layout::{Constraint, Direction, Layout, Rect},
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Clear, Paragraph, Widget, Wrap},
};

use crate::config::Config;
use crate::fleet::profile::AgentProfile;
use crate::fleet::roster::{FleetRoster, ProfileOrigin};
use crate::fleet::worker_runtime::roster_member_agent_type;
use crate::localization::{Locale, MessageId, tr};
use crate::palette;
use crate::tui::app::App;
use crate::tui::menu_style;
use crate::tui::views::{
    ActionHint, ModalKind, ModalView, ViewAction, ViewEvent, render_modal_footer,
    truncate_view_text,
};
use crate::tui::whales;
use crate::worker_profile::{ShellPolicy, WorkerRuntimeProfile};

/// The live session route — the operator the roster works for. Read once at
/// open, the same way [`super::fleet_setup::FleetSetupSnapshot`] snapshots it.
#[derive(Debug, Clone)]
struct OperatorInfo {
    provider: String,
    /// Exact canonical route key, kept separate from the display label so
    /// capability lookup can use provider-scoped catalog facts.
    provider_id: String,
    model: String,
    reasoning: String,
}

impl OperatorInfo {
    fn from_app(app: &App) -> Self {
        let model = if app.auto_model {
            app.last_effective_model
                .as_deref()
                .map(|effective| format!("auto -> {effective}"))
                .unwrap_or_else(|| "auto".to_string())
        } else {
            app.model.clone()
        };
        let route_provider = if app.auto_model {
            app.last_effective_provider.unwrap_or(app.api_provider)
        } else {
            app.api_provider
        };
        let provider_id = if app.auto_model {
            app.last_effective_provider_identity
                .clone()
                .unwrap_or_else(|| {
                    if route_provider == crate::config::ApiProvider::Custom {
                        app.provider_identity_for_persistence().to_string()
                    } else {
                        route_provider.as_str().to_string()
                    }
                })
        } else {
            app.provider_identity_for_persistence().to_string()
        };
        let provider = if route_provider == crate::config::ApiProvider::Custom {
            provider_id.clone()
        } else {
            route_provider.display_name().to_string()
        };
        Self {
            provider,
            provider_id,
            model,
            reasoning: app.reasoning_effort_display_label(),
        }
    }
}

/// Which named Fleet (if any) this session is using, and where that selection
/// is pinned — user-global vs this folder only.
#[derive(Debug, Clone)]
struct SelectedFleetSummary {
    name: String,
    scope: crate::fleet::store::FleetScope,
}

pub struct FleetRosterView {
    operator: OperatorInfo,
    members: Vec<AgentProfile>,
    /// Shadow records from the roster load (#5098): which lower-precedence
    /// files the displayed members are ignoring.
    shadowed: Vec<crate::fleet::roster::ShadowedProfile>,
    /// Selected named Fleet + scope, when one is active for this session.
    selected_fleet: Option<SelectedFleetSummary>,
    /// Selected row: 0 is the pinned operator row, members follow at 1..
    selected: usize,
    detail_scroll: usize,
    /// UI locale captured from the app at construction (#4057 wave 2).
    locale: Locale,
}

impl FleetRosterView {
    #[must_use]
    pub fn new(app: &App, config: &Config) -> Self {
        let selected_fleet =
            crate::fleet::store::selected_fleet(&app.workspace).map(|sel| SelectedFleetSummary {
                name: sel.name,
                scope: sel.scope,
            });
        let mut view = Self::from_parts(
            OperatorInfo::from_app(app),
            FleetRoster::load(&config.fleet_config(), &app.workspace),
            selected_fleet,
        );
        view.locale = app.ui_locale;
        view
    }

    fn from_parts(
        operator: OperatorInfo,
        roster: FleetRoster,
        selected_fleet: Option<SelectedFleetSummary>,
    ) -> Self {
        Self {
            operator,
            // The operator is pinned as its own row 0 (the live session route),
            // so exclude the built-in "operator" profile from the dispatchable
            // member list to avoid rendering it twice (#dogfood 0.8.67). The
            // engine's FleetRoster is untouched, so role/dispatch semantics are
            // unchanged; only this view drops the duplicate.
            members: roster
                .members()
                .iter()
                .filter(|m| !m.id.trim().eq_ignore_ascii_case("operator"))
                .cloned()
                .collect(),
            shadowed: roster.shadowed().to_vec(),
            selected_fleet,
            selected: 0,
            detail_scroll: 0,
            locale: Locale::En,
        }
    }

    /// Total selectable rows: the operator plus every roster member.
    fn row_count(&self) -> usize {
        1 + self.members.len()
    }

    fn operator_selected(&self) -> bool {
        self.selected == 0
    }

    fn selected_member(&self) -> Option<&AgentProfile> {
        self.selected.checked_sub(1).and_then(|idx| {
            self.members
                .get(idx.min(self.members.len().saturating_sub(1)))
        })
    }

    fn move_up(&mut self) {
        self.selected = crate::tui::list_nav::wrap_index(self.selected, self.row_count(), -1);
        self.detail_scroll = 0;
    }

    fn move_down(&mut self) {
        self.selected = crate::tui::list_nav::wrap_index(self.selected, self.row_count(), 1);
        self.detail_scroll = 0;
    }

    fn footer_hints(&self) -> Vec<ActionHint> {
        vec![
            ActionHint::new("↑/↓", "move"),
            ActionHint::new("s/Enter", "setup"),
            ActionHint::new("f", "saved Fleets"),
            ActionHint::new("w", tr(self.locale, MessageId::FleetRosterWorkers)),
            ActionHint::new("PgUp/PgDn", "scroll detail"),
            ActionHint::new("Esc", "close"),
        ]
    }
}

impl ModalView for FleetRosterView {
    fn kind(&self) -> ModalKind {
        ModalKind::FleetRoster
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }

    fn handle_key(&mut self, key: KeyEvent) -> ViewAction {
        match key.code {
            KeyCode::Esc | KeyCode::Char('q') => ViewAction::Close,
            KeyCode::Up | KeyCode::Char('k') => {
                self.move_up();
                ViewAction::None
            }
            KeyCode::Down | KeyCode::Char('j') => {
                self.move_down();
                ViewAction::None
            }
            KeyCode::Enter | KeyCode::Char('s') => {
                if let Some(member) = self.selected_member() {
                    let role = member.profile.role.name.clone();
                    // Carry the role the operator already chose. The setup
                    // wizard can still step back to Role when they want to
                    // change it, but does not force a duplicate selection.
                    ViewAction::EmitAndClose(ViewEvent::FleetRosterOpenSetupRequested { role })
                } else {
                    // The operator is not a wizard-authored profile; its
                    // route changes via /model or /provider (the detail pane
                    // says so).
                    ViewAction::None
                }
            }
            KeyCode::Char('w') => {
                ViewAction::EmitAndClose(ViewEvent::FleetRosterOpenWorkersRequested)
            }
            KeyCode::Char('f') => {
                ViewAction::EmitAndClose(ViewEvent::FleetRosterOpenFleetsRequested)
            }
            KeyCode::Home => {
                self.detail_scroll = 0;
                ViewAction::None
            }
            KeyCode::PageUp => {
                self.detail_scroll = self.detail_scroll.saturating_sub(8);
                ViewAction::None
            }
            KeyCode::PageDown => {
                self.detail_scroll = self.detail_scroll.saturating_add(8);
                ViewAction::None
            }
            _ => ViewAction::None,
        }
    }

    fn render(&self, area: Rect, buf: &mut Buffer) {
        Clear.render(area, buf);
        Block::default()
            .style(Style::default().bg(palette::WHALE_BG))
            .render(area, buf);

        let hints = self.footer_hints();
        let content = render_modal_footer(area, buf, &hints);

        // Hairline shell shared with the HTML route/config/Fleet surfaces.
        // This replaces the centered legacy card: Fleet is a product room,
        // not a popup floating over an unrelated transcript.
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Length(3), Constraint::Min(1)])
            .split(content);
        let header = vec![
            Line::from(vec![
                Span::styled(
                    format!("{} ", tr(self.locale, MessageId::FleetRosterHeaderLabel)),
                    Style::default().fg(palette::WHALE_ACTION).bold(),
                ),
                Span::styled(
                    "──────────────────────── ",
                    Style::default().fg(palette::BORDER_COLOR),
                ),
                Span::styled(
                    tr(self.locale, MessageId::FleetRosterTabRoster),
                    Style::default().fg(palette::WHALE_INFO).bold(),
                ),
                Span::styled(
                    format!(
                        "  {}  {} ",
                        tr(self.locale, MessageId::FleetRosterTabSetup),
                        tr(self.locale, MessageId::FleetRosterWorkers)
                    ),
                    Style::default().fg(palette::TEXT_MUTED),
                ),
                Span::styled("".repeat(24), Style::default().fg(palette::BORDER_COLOR)),
            ]),
            Line::from(""),
            Line::from(vec![
                Span::styled(
                    format!("  {}", self.selected_fleet_line()),
                    Style::default().fg(palette::TEXT_SECONDARY),
                ),
                Span::styled(
                    format!(
                        " · {}",
                        tr(self.locale, MessageId::FleetRosterMembersCount)
                            .replace("{count}", &(self.members.len() + 1).to_string())
                    ),
                    Style::default().fg(palette::TEXT_MUTED),
                ),
                Span::styled(
                    format!(
                        " · {}",
                        tr(self.locale, MessageId::FleetRosterOperatorFirst)
                    ),
                    Style::default().fg(palette::TEXT_MUTED),
                ),
            ]),
        ];
        Paragraph::new(header)
            .wrap(Wrap { trim: false })
            .render(chunks[0], buf);

        self.render_body(chunks[1], buf);
    }
}

impl FleetRosterView {
    /// Scope-explicit selected Fleet line. Paths stay out — receipts name them.
    fn selected_fleet_line(&self) -> String {
        match &self.selected_fleet {
            Some(sel) => format!("Fleet `{}` · {}", sel.name, sel.scope.long_label()),
            None => "no saved Fleet selected · legacy roster".to_string(),
        }
    }

    fn render_body(&self, area: Rect, buf: &mut Buffer) {
        if area.width == 0 || area.height == 0 {
            return;
        }

        // Two columns when there is room, stacked otherwise — same responsive
        // shape as the setup wizard's choice step so nothing truncates at
        // 80x24.
        let (list_area, detail_area) = if area.width >= 56 {
            let cols = Layout::default()
                .direction(Direction::Horizontal)
                .constraints([
                    Constraint::Percentage(45),
                    Constraint::Length(2),
                    Constraint::Min(20),
                ])
                .split(area);
            (cols[0], cols[2])
        } else {
            let list_height =
                (self.row_count() as u16 + 1).min(area.height.saturating_sub(1).max(1));
            let rows = Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Length(list_height), Constraint::Min(1)])
                .split(area);
            (rows[0], rows[1])
        };

        // Row list: the pinned operator first, then one row per member,
        // scrolled so the selection stays visible when the party outgrows
        // the pane.
        let visible_rows = usize::from(list_area.height).max(1);
        let first = self
            .selected
            .saturating_sub(visible_rows.saturating_sub(1))
            .min(
                self.row_count()
                    .saturating_sub(visible_rows.min(self.row_count())),
            );
        let list_width = usize::from(list_area.width);
        let mut list_lines: Vec<Line> = Vec::with_capacity(visible_rows);
        for idx in first..(first + visible_rows).min(self.row_count()) {
            let is_selected = idx == self.selected;
            let pointer = format!("{} ", crate::tui::glyphs::selection_marker(is_selected));
            let (text, base_style) = if idx == 0 {
                (
                    format!(
                        "{pointer}@ {}  {}",
                        tr(self.locale, MessageId::FleetRosterOperatorRow),
                        self.operator.model
                    ),
                    Style::default()
                        .fg(palette::WHALE_ACTION)
                        .add_modifier(Modifier::BOLD),
                )
            } else {
                let member = &self.members[idx - 1];
                let mark = member_role_mark(member);
                // #5098: badge rows whose winning layer is ignoring a
                // lower-precedence file, so a shadowed personal/project edit
                // is visible from the list, not just the detail pane.
                let shadow_badge = if self
                    .shadowed
                    .iter()
                    .any(|shadow| shadow.id.trim().eq_ignore_ascii_case(member.id.trim()))
                {
                    " ⚠shadows"
                } else {
                    ""
                };
                // Whale Teams: the species badge sits between the charter role
                // mark and the id, so a Scout, Patch, or Lantern reads at a
                // glance even before the detail pane opens.
                let species = member_species(member);
                let badge_cells = whales::BADGE_WIDTH + 1;
                let text = format!(
                    "{pointer}{mark} {}  {}{}",
                    member.id,
                    member_routing(member),
                    shadow_badge
                );
                let text = truncate_view_text(&text, list_width.saturating_sub(badge_cells));
                let base_style = if is_selected {
                    menu_style::selected_row_style()
                } else {
                    Style::default().fg(palette::TEXT_PRIMARY)
                };
                let split = pointer.len() + mark.len() + 1;
                let (head, tail) = if text.len() >= split && text.is_char_boundary(split) {
                    text.split_at(split)
                } else {
                    (text.as_str(), "")
                };
                let mut spans = vec![Span::styled(head.to_string(), base_style)];
                for span in whales::badge(species, &palette::UI_THEME) {
                    spans.push(if is_selected {
                        Span::styled(
                            span.content,
                            span.style
                                .bg(palette::SELECTION_BG)
                                .add_modifier(Modifier::BOLD),
                        )
                    } else {
                        span
                    });
                }
                spans.push(Span::styled(format!(" {tail}"), base_style));
                list_lines.push(Line::from(spans));
                continue;
            };
            let style = if is_selected {
                menu_style::selected_row_style()
            } else {
                base_style
            };
            list_lines.push(Line::from(Span::styled(
                truncate_view_text(&text, list_width),
                style,
            )));
        }
        Paragraph::new(list_lines).render(list_area, buf);

        // Detail pane for the selected row.
        let lines = if self.operator_selected() {
            operator_detail_lines(&self.operator)
        } else if let Some(member) = self.selected_member() {
            // Whale Teams identity first: the portrait (or, in the Compact
            // tier, only the badge) plus species and job. Rendered without a
            // state — a roster member is a profile, not a runtime, so this
            // claims nothing about whether anyone is working.
            let mut lines = whale_identity_lines(member, self.locale, area.width);
            // Session model is the operator route so "fast" loadouts resolve
            // to the fast sibling the runtime will actually launch.
            lines.extend(member_detail_lines_with_session(
                member,
                Some(self.operator.model.as_str()),
                &self.shadowed,
            ));
            lines
        } else {
            vec![Line::from(Span::styled(
                "Roster is empty.",
                Style::default().fg(palette::TEXT_MUTED),
            ))]
        };

        // Same wrapped-row scroll bound as the setup review step: count
        // visual rows so the tail stays reachable.
        let wrap_width = usize::from(detail_area.width).max(1);
        let visual_rows: usize = lines
            .iter()
            .map(|line| line.width().div_ceil(wrap_width).max(1))
            .sum();
        let max_scroll = visual_rows.saturating_sub(usize::from(detail_area.height).max(1));
        let scroll = self.detail_scroll.min(max_scroll);
        Paragraph::new(lines)
            .wrap(Wrap { trim: true })
            .scroll((scroll as u16, 0))
            .render(detail_area, buf);
    }
}

/// Species for a roster member: the profile id first (built-in ids are role
/// names), then the resolved worker agent type. Unknown → the plain whale.
fn member_species(member: &AgentProfile) -> whales::WhaleSpecies {
    match whales::WhaleSpecies::for_role_id(&member.id) {
        whales::WhaleSpecies::Plain => {
            whales::WhaleSpecies::for_fleet_role(&roster_member_agent_type(member))
        }
        species => species,
    }
}

/// Identity block for the detail pane: portrait when the view is at least
/// the Compact tier width, badge otherwise; then `Name · species · job`. No
/// state is drawn or claimed — a roster member is a profile, not a runtime.
fn whale_identity_lines(
    member: &AgentProfile,
    locale: Locale,
    view_width: u16,
) -> Vec<Line<'static>> {
    let species = member_species(member);
    let theme = &palette::UI_THEME;
    let mut lines: Vec<Line> = Vec::new();
    if whales::portrait_fits(view_width) {
        lines.extend(whales::portrait(species, None, 0, theme));
    }
    let mut caption = whales::badge(species, theme);
    caption.push(Span::styled(
        format!(
            " {} · {} · {}",
            species.name(),
            species.animal(locale),
            species.job(locale)
        ),
        Style::default().fg(palette::TEXT_PRIMARY),
    ));
    lines.push(Line::from(caption));
    lines.push(Line::from(""));
    lines
}

fn member_role_mark(member: &AgentProfile) -> &'static str {
    match member.id.as_str() {
        "manager" | "scout" => crate::tui::glyphs::ROLE_MANAGER,
        "builder" => crate::tui::glyphs::ROLE_BUILDER,
        "reviewer" => crate::tui::glyphs::ROLE_REVIEWER,
        "verifier" => crate::tui::glyphs::ROLE_VERIFIER,
        "synthesizer" => crate::tui::glyphs::ROLE_SYNTHESIZER,
        _ => match roster_member_agent_type(member).as_str() {
            "scout" | "manager" => crate::tui::glyphs::ROLE_MANAGER,
            "builder" => crate::tui::glyphs::ROLE_BUILDER,
            "reviewer" => crate::tui::glyphs::ROLE_REVIEWER,
            "verifier" => crate::tui::glyphs::ROLE_VERIFIER,
            "synthesizer" => crate::tui::glyphs::ROLE_SYNTHESIZER,
            _ => crate::tui::glyphs::NEUTRAL,
        },
    }
}

/// Shared field renderer for the detail pane.
fn detail_field(lines: &mut Vec<Line<'static>>, label: &str, body: String) {
    lines.push(Line::from(Span::styled(
        label.to_string(),
        Style::default().fg(palette::WHALE_INFO).bold(),
    )));
    lines.push(Line::from(Span::styled(
        body,
        Style::default().fg(palette::TEXT_PRIMARY),
    )));
    lines.push(Line::from(""));
}

/// Detail pane for the pinned operator row: the live session route, plus the
/// product truth that the operator is this Fleet's leader.
fn operator_detail_lines(operator: &OperatorInfo) -> Vec<Line<'static>> {
    let mut lines: Vec<Line> = Vec::new();
    detail_field(
        &mut lines,
        "Role",
        "operator · Fleet leader (session route)".to_string(),
    );
    detail_field(&mut lines, "Origin", "session".to_string());
    detail_field(&mut lines, "Posture", "full session authority".to_string());
    detail_field(&mut lines, "Provider", operator.provider.clone());
    detail_field(&mut lines, "Model", operator.model.clone());
    // Session-route capability badges (#5038). Use the exact route key rather
    // than the display label so built-in routes get provider-scoped catalog
    // facts; custom routes still fall back conservatively to registry facts.
    if let Some(badges) = crate::fleet::capability_badges::resolve_route_capability_badges(
        Some(&operator.provider_id),
        &operator.model,
    ) {
        detail_field(&mut lines, "Capabilities", badges.summary());
    }
    detail_field(&mut lines, "Reasoning", operator.reasoning.clone());
    detail_field(
        &mut lines,
        "Description",
        "The operator is the Fleet leader — your main session model. Every member \
         below works for this route. It dispatches workers via `agent` profile \
         spawns and Workflow task({profile}). Change its route with /model or \
         /provider; persist with /fleet save."
            .to_string(),
    );
    lines
}

/// The resolved worker posture for a roster member: what the runtime would
/// actually grant when this member is dispatched (role posture, not the
/// profile's requested permissions).
fn member_posture(member: &AgentProfile) -> String {
    let agent_type = roster_member_agent_type(member);
    let runtime = WorkerRuntimeProfile::for_role(agent_type.clone());
    let write = if runtime.permissions.write {
        "write"
    } else {
        "read-only"
    };
    let shell = match runtime.shell {
        ShellPolicy::None => "shell none",
        ShellPolicy::ReadOnly => "shell read-only",
        ShellPolicy::Full => "shell full",
    };
    format!("{} worker · {write} · {shell}", agent_type.as_str())
}

/// The routing truth for a member: explicit model pin, else route preset, else
/// same-route inheritance. `[subagents]` overrides still win at dispatch.
///
/// When the loadout is `fast`, show that the runtime resolves the **fast
/// sibling of the active session model** — not a stale on-disk profile name —
/// so the roster matches what Fleet will actually launch.
fn member_routing(member: &AgentProfile) -> String {
    member_routing_with_session(member, None)
}

fn member_routing_with_session(member: &AgentProfile, session_model: Option<&str>) -> String {
    if let Some(model) = member
        .profile
        .model
        .as_deref()
        .map(str::trim)
        .filter(|model| !model.is_empty())
    {
        return format!("model {model} (pinned)");
    }
    match member.profile.loadout.as_str() {
        "inherit" => "inherit session route".to_string(),
        "fast" => match session_model.map(str::trim).filter(|m| !m.is_empty()) {
            Some(session) => format!("fast sibling of {session} (resolved)"),
            None => "route preset fast (resolved at launch)".to_string(),
        },
        loadout => format!("route preset {loadout}"),
    }
}

fn member_detail_lines_with_session(
    member: &AgentProfile,
    session_model: Option<&str>,
    shadowed: &[crate::fleet::roster::ShadowedProfile],
) -> Vec<Line<'static>> {
    let mut lines: Vec<Line> = Vec::new();

    let name = match member.display_name.as_deref().map(str::trim) {
        Some(display_name) if !display_name.is_empty() && display_name != member.id => {
            format!("{display_name} ({})", member.id)
        }
        _ => member.id.clone(),
    };
    detail_field(&mut lines, "Member", name);
    detail_field(
        &mut lines,
        "Origin",
        match member.origin {
            ProfileOrigin::BuiltIn => "built-in (default party)".to_string(),
            _ => format!("{} · {}", member.origin, member.source.display()),
        },
    );
    // #5098: every layer this member is displacing, so a shadowed personal or
    // project file is visible instead of silently dropped from the merge.
    for shadow in shadowed
        .iter()
        .filter(|shadow| shadow.id.trim().eq_ignore_ascii_case(member.id.trim()))
    {
        detail_field(
            &mut lines,
            "Shadows",
            format!(
                "{} copy at {} (ignored)",
                shadow.shadowed_origin,
                shadow.shadowed_source.display()
            ),
        );
    }
    detail_field(&mut lines, "Slot", member.profile.slot.as_str().to_string());
    detail_field(&mut lines, "Posture", member_posture(member));
    detail_field(
        &mut lines,
        "Routing",
        member_routing_with_session(member, session_model),
    );

    // Capability badges for a pinned model, from the shared Fleet resolver
    // (#5038). Unknown models omit the field rather than fabricating facts.
    if let Some(model) = member
        .profile
        .model
        .as_deref()
        .map(str::trim)
        .filter(|model| !model.is_empty())
        && let Some(badges) = crate::fleet::capability_badges::resolve_route_capability_badges(
            member.profile.provider.as_deref(),
            model,
        )
    {
        detail_field(&mut lines, "Capabilities", badges.summary());
    }

    let delegation = &member.profile.delegation;
    if delegation.max_spawn_depth.is_some() || delegation.max_concurrency.is_some() {
        let mut bounds: Vec<String> = Vec::new();
        if let Some(depth) = delegation.max_spawn_depth {
            bounds.push(format!("spawn depth {depth}"));
        }
        if let Some(concurrency) = delegation.max_concurrency {
            bounds.push(format!("concurrency {concurrency}"));
        }
        detail_field(&mut lines, "Delegation", bounds.join(" · "));
    }

    detail_field(
        &mut lines,
        "Instructions",
        if member.profile.role.instructions.is_some() {
            match member.origin {
                ProfileOrigin::Workspace => {
                    format!("custom overlay ({})", member.source.display())
                }
                ProfileOrigin::Personal => {
                    format!("personal overlay ({})", member.source.display())
                }
                _ => "custom overlay".to_string(),
            }
        } else {
            "none (role posture only)".to_string()
        },
    );

    if let Some(description) = member
        .description
        .as_deref()
        .map(str::trim)
        .filter(|description| !description.is_empty())
    {
        detail_field(&mut lines, "Description", description.to_string());
    }

    lines
}

#[cfg(test)]
mod tests;