omni-dev 0.46.0

AI-powered git commit rewriter, PR generator, and MCP server for Jira, Confluence, Datadog, Gmail, and Drive.
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
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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
//! The pane stack (issue #1585 §4, Phase 4b): groups of terminal tabs, the
//! tab strip over each, and the focus/lifecycle rules that decide which tab
//! a key, a PTY event or a resize belongs to.
//!
//! [`super::layout`] owns the geometry (weights, splitters, drags);
//! this owns the *tabs*. The two are separate so the maths stays testable
//! with no PTY in sight, and this file's rules stay testable with scripted
//! children rather than a real terminal.
//!
//! A tab remembers the worktree it was opened in, which is what feeds the
//! tree's `here` cue without registering with the daemon
//! ([ADR-0072](../../../../docs/adrs/adr-0072.md) §1).

use std::collections::HashSet;
use std::path::{Path, PathBuf};

use alacritty_terminal::event::Event as TermEvent;
use anyhow::Result;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Paragraph};
use ratatui::Frame;
use unicode_width::UnicodeWidthStr as _;

use super::layout::{self, MIN_GROUP_HEIGHT};
use super::session_layout;
use super::terminal::{GridSize, TabEffect, TabId, TabKind, TerminalTab};

/// One vertical slice of the terminal side: a tab strip and the active
/// tab's grid under it.
pub struct PaneGroup {
    pub tabs: Vec<TerminalTab>,
    /// Index into `tabs`; always valid while `tabs` is non-empty.
    pub active: usize,
}

impl PaneGroup {
    fn new(tab: TerminalTab) -> Self {
        Self {
            tabs: vec![tab],
            active: 0,
        }
    }

    pub fn active_tab(&self) -> Option<&TerminalTab> {
        self.tabs.get(self.active)
    }

    pub fn active_tab_mut(&mut self) -> Option<&mut TerminalTab> {
        self.tabs.get_mut(self.active)
    }

    /// Clamps `active` back into range after a removal.
    fn clamp_active(&mut self) {
        if self.active >= self.tabs.len() {
            self.active = self.tabs.len().saturating_sub(1);
        }
    }
}

/// Where a tab lives.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TabAddr {
    pub group: usize,
    pub tab: usize,
}

/// The stack of groups, their relative weights, and which group has focus.
///
/// Empty (no groups) is the normal starting state: the tree pane then takes
/// the full width, exactly as in Phases 1–3.
#[derive(Default)]
pub struct PaneLayout {
    pub groups: Vec<PaneGroup>,
    /// One weight per group, top to bottom — see [`layout::split_groups`].
    pub weights: Vec<u16>,
    pub focused: usize,
    next_id: TabId,
}

impl PaneLayout {
    pub fn is_empty(&self) -> bool {
        self.groups.is_empty()
    }

    pub fn group_count(&self) -> usize {
        self.groups.len()
    }

    /// The next tab id, unique for this process's lifetime — ids are never
    /// reused, so a PTY event from a closed tab can always be told apart
    /// from one for a live tab that took its slot.
    fn take_id(&mut self) -> TabId {
        self.next_id += 1;
        self.next_id
    }

    fn focused_group(&self) -> Option<&PaneGroup> {
        self.groups.get(self.focused)
    }

    /// The focused group's active tab — the one a key or a copy acts on.
    pub fn active_tab(&self) -> Option<&TerminalTab> {
        self.focused_group()?.active_tab()
    }

    /// Group `index`'s active tab — how a mouse event, which names the
    /// group it landed in, reaches a tab without changing focus first.
    pub fn group_tab(&self, index: usize) -> Option<&TerminalTab> {
        self.groups.get(index)?.active_tab()
    }

    /// Every tab, in stack order.
    pub fn tabs(&self) -> impl Iterator<Item = &TerminalTab> {
        self.groups.iter().flat_map(|g| g.tabs.iter())
    }

    /// Whether any tab still has a live child — what `q` asks about.
    pub fn any_alive(&self) -> bool {
        self.tabs().any(TerminalTab::is_alive)
    }

    /// Finds a tab by id.
    pub fn find(&self, id: TabId) -> Option<TabAddr> {
        self.groups.iter().enumerate().find_map(|(group, g)| {
            g.tabs
                .iter()
                .position(|t| t.id() == id)
                .map(|tab| TabAddr { group, tab })
        })
    }

    pub fn tab_mut(&mut self, addr: TabAddr) -> Option<&mut TerminalTab> {
        self.groups.get_mut(addr.group)?.tabs.get_mut(addr.tab)
    }

    /// The first tab opened in `worktree`, if any — so `enter` on a row
    /// that already has a tab focuses it instead of opening a second.
    pub fn find_in_worktree(&self, worktree: &Path, kind: TabKind) -> Option<TabAddr> {
        self.groups.iter().enumerate().find_map(|(group, g)| {
            g.tabs
                .iter()
                .position(|t| t.opened_in == worktree && t.kind == kind && t.is_alive())
                .map(|tab| TabAddr { group, tab })
        })
    }

    /// Focuses `addr`, making it its group's active tab.
    pub fn focus(&mut self, addr: TabAddr) {
        if let Some(group) = self.groups.get_mut(addr.group) {
            if addr.tab < group.tabs.len() {
                group.active = addr.tab;
                self.focused = addr.group;
            }
        }
    }

    /// Opens a tab in the focused group (or the first group when there is
    /// none yet), and focuses it. `spawn` builds the tab from the id this
    /// allocates — injected so tests can script the child.
    pub fn open_tab(
        &mut self,
        spawn: impl FnOnce(TabId) -> Result<TerminalTab>,
    ) -> Result<TabAddr> {
        let id = self.take_id();
        let tab = spawn(id)?;
        if self.groups.is_empty() {
            self.groups.push(PaneGroup::new(tab));
            self.weights = layout::even_weights(1);
            self.focused = 0;
            return Ok(TabAddr { group: 0, tab: 0 });
        }
        let group = self.focused.min(self.groups.len() - 1);
        self.groups[group].tabs.push(tab);
        let tab = self.groups[group].tabs.len() - 1;
        self.groups[group].active = tab;
        self.focused = group;
        Ok(TabAddr { group, tab })
    }

    /// Opens a tab in a **new group** below the focused one — `alt-s`.
    pub fn split(&mut self, spawn: impl FnOnce(TabId) -> Result<TerminalTab>) -> Result<TabAddr> {
        if self.groups.is_empty() {
            return self.open_tab(spawn);
        }
        let id = self.take_id();
        let tab = spawn(id)?;
        let at = (self.focused + 1).min(self.groups.len());
        self.groups.insert(at, PaneGroup::new(tab));
        self.weights.insert(at, average_weight(&self.weights));
        self.focused = at;
        Ok(TabAddr { group: at, tab: 0 })
    }

    /// Closes the tab at `addr`, shutting its child down. Returns the
    /// worktree it was opened in, so the caller can update the `here` cue.
    /// An emptied group is removed, and its weight with it.
    pub fn close_tab(&mut self, addr: TabAddr) -> Option<PathBuf> {
        let group = self.groups.get_mut(addr.group)?;
        if addr.tab >= group.tabs.len() {
            return None;
        }
        let mut tab = group.tabs.remove(addr.tab);
        tab.shutdown();
        group.clamp_active();
        if group.tabs.is_empty() {
            self.groups.remove(addr.group);
            if addr.group < self.weights.len() {
                self.weights.remove(addr.group);
            }
            if self.focused >= self.groups.len() {
                self.focused = self.groups.len().saturating_sub(1);
            }
        } else {
            self.focused = addr.group;
        }
        Some(tab.opened_in)
    }

    /// Closes every tab in `addr.group` except `addr.tab`, shutting each one
    /// down exactly as [`Self::close_tab`] does. Returns the `opened_in` of
    /// every tab closed, in close order — a `Vec` rather than an `Option`
    /// because closing several tabs at once (the tab-strip menu's "Close
    /// Other Tabs") can retire several different worktrees' `here` cues, and
    /// a caller that only saw the last one would leave the others lit for a
    /// tab that no longer exists.
    ///
    /// The anchor tab always survives, so unlike `close_tab` this can never
    /// empty the group — there is deliberately no group-removal or
    /// weight-vector handling here.
    pub fn close_other_tabs(&mut self, addr: TabAddr) -> Vec<PathBuf> {
        let Some(group) = self.groups.get_mut(addr.group) else {
            return Vec::new();
        };
        if addr.tab >= group.tabs.len() {
            return Vec::new();
        }
        let mut closed = Vec::with_capacity(group.tabs.len() - 1);
        // Remove from the back so earlier indices — including the anchor —
        // never shift out from under us.
        for index in (0..group.tabs.len()).rev() {
            if index == addr.tab {
                continue;
            }
            let mut tab = group.tabs.remove(index);
            tab.shutdown();
            closed.push(tab.opened_in);
        }
        closed.reverse(); // report in close order, not removal order
                          // Only the anchor is left, so it is now the group's sole tab; run it
                          // through clamp_active anyway rather than assuming, since a future
                          // reader would otherwise have to re-derive that 0 is always in range.
        group.active = 0;
        group.clamp_active();
        self.focused = addr.group;
        closed
    }

    /// Closes every tab in `addr.group` after index `addr.tab`, shutting
    /// each one down exactly as [`Self::close_tab`] does. Returns the
    /// `opened_in` of every tab closed, in close order — see
    /// [`Self::close_other_tabs`] for why a `Vec` rather than an `Option`.
    ///
    /// The anchor tab and everything left of it always survive, so unlike
    /// `close_tab` this can never empty the group — there is deliberately
    /// no group-removal or weight-vector handling here.
    pub fn close_tabs_to_right(&mut self, addr: TabAddr) -> Vec<PathBuf> {
        let Some(group) = self.groups.get_mut(addr.group) else {
            return Vec::new();
        };
        if addr.tab >= group.tabs.len() {
            return Vec::new();
        }
        let mut closed = Vec::with_capacity(group.tabs.len() - addr.tab - 1);
        for index in (addr.tab + 1..group.tabs.len()).rev() {
            let mut tab = group.tabs.remove(index);
            tab.shutdown();
            closed.push(tab.opened_in);
        }
        closed.reverse(); // report in close order, not removal order
        group.clamp_active();
        self.focused = addr.group;
        closed
    }

    /// Closes every tab in group `index`, shutting each one down exactly as
    /// [`Self::close_tab`] does, then removes the group itself. Returns the
    /// `opened_in` of every tab closed, in close order — see
    /// [`Self::close_other_tabs`] for why a `Vec` rather than an `Option`: a
    /// whole group can retire several different worktrees' `here` cues at
    /// once, and a caller that only saw the last one would leave the others
    /// lit for a tab that no longer exists.
    ///
    /// Unlike `close_other_tabs`/`close_tabs_to_right`, this always empties
    /// the group, so — like [`Self::close_tab`] when a group empties — it
    /// removes the group, removes its entry from `self.weights`, and clamps
    /// `self.focused` back into range. Closing the last group is legal and
    /// leaves the layout empty, exactly as closing the last tab in the last
    /// group already does via `close_tab`, so that case gets no special
    /// handling here beyond the same clamp every other case goes through.
    ///
    /// A no-op returning an empty `Vec` for an out-of-range `index`.
    pub fn close_group(&mut self, index: usize) -> Vec<PathBuf> {
        let Some(group) = self.groups.get_mut(index) else {
            return Vec::new();
        };
        let mut closed = Vec::with_capacity(group.tabs.len());
        // Remove from the back so earlier indices never shift out from
        // under us.
        for tab_index in (0..group.tabs.len()).rev() {
            let mut tab = group.tabs.remove(tab_index);
            tab.shutdown();
            closed.push(tab.opened_in);
        }
        closed.reverse(); // report in close order, not removal order
        self.groups.remove(index);
        if index < self.weights.len() {
            self.weights.remove(index);
        }
        if self.focused >= self.groups.len() {
            self.focused = self.groups.len().saturating_sub(1);
        }
        closed
    }

    /// Closes the focused group's active tab.
    pub fn close_active(&mut self) -> Option<PathBuf> {
        let addr = TabAddr {
            group: self.focused,
            tab: self.focused_group()?.active,
        };
        self.close_tab(addr)
    }

    /// The set of worktrees that still have a tab open — the `here` cue's
    /// truth after any close, since one worktree may have several tabs.
    pub fn open_worktrees(&self) -> HashSet<PathBuf> {
        self.tabs().map(|t| t.opened_in.clone()).collect()
    }

    /// Cycles the focused group's active tab by `delta` (`alt-[` / `alt-]`),
    /// wrapping.
    pub fn cycle_tab(&mut self, delta: isize) {
        let Some(group) = self.groups.get_mut(self.focused) else {
            return;
        };
        let len = group.tabs.len();
        if len == 0 {
            return;
        }
        let len_i = isize::try_from(len).unwrap_or(isize::MAX);
        let current = isize::try_from(group.active).unwrap_or(0);
        group.active = usize::try_from((current + delta).rem_euclid(len_i)).unwrap_or(0);
    }

    /// Selects the focused group's `index`-th tab (`alt-1`…`alt-9`).
    pub fn select_tab(&mut self, index: usize) -> bool {
        match self.groups.get_mut(self.focused) {
            Some(group) if index < group.tabs.len() => {
                group.active = index;
                true
            }
            _ => false,
        }
    }

    /// Moves focus between groups by `delta`, clamped.
    pub fn cycle_group(&mut self, delta: isize) {
        if self.groups.is_empty() {
            return;
        }
        let max = isize::try_from(self.groups.len() - 1).unwrap_or(0);
        let current = isize::try_from(self.focused).unwrap_or(0);
        self.focused = usize::try_from((current + delta).clamp(0, max)).unwrap_or(0);
    }

    /// Moves the active tab into the adjacent group (`alt-⇧↑` / `alt-⇧↓`),
    /// creating no groups and removing one that empties. Returns whether
    /// anything moved.
    pub fn move_tab_to_group(&mut self, delta: isize) -> bool {
        if self.groups.len() < 2 {
            return false;
        }
        let from = self.focused;
        let Ok(target) = usize::try_from(isize::try_from(from).unwrap_or(0) + delta) else {
            return false;
        };
        if target >= self.groups.len() {
            return false;
        }
        let Some(group) = self.groups.get_mut(from) else {
            return false;
        };
        if group.tabs.is_empty() {
            return false;
        }
        let tab = group.tabs.remove(group.active);
        group.clamp_active();
        let emptied = group.tabs.is_empty();
        // Removing the source group shifts every later index down by one.
        let target = if emptied && target > from {
            target - 1
        } else {
            target
        };
        if emptied {
            self.groups.remove(from);
            if from < self.weights.len() {
                self.weights.remove(from);
            }
        }
        let Some(dest) = self.groups.get_mut(target) else {
            return false; // unreachable: the target existed a moment ago
        };
        dest.tabs.push(tab);
        dest.active = dest.tabs.len() - 1;
        self.focused = target;
        true
    }

    /// Resets the stack to equal weights — `alt-0`.
    pub fn reset_weights(&mut self) {
        self.weights = layout::even_weights(self.groups.len());
    }

    /// The stack's persistable shape: which worktree each tab runs in, how
    /// the groups are weighted, and where focus is. No process state — a
    /// restored tab is a new child (ADR-0072 §2).
    pub fn to_saved(&self) -> session_layout::SavedLayout {
        session_layout::SavedLayout {
            version: 1,
            groups: self
                .groups
                .iter()
                .enumerate()
                .map(|(index, group)| session_layout::SavedGroup {
                    tabs: group
                        .tabs
                        .iter()
                        .map(|tab| session_layout::SavedTab {
                            path: tab.opened_in.clone(),
                            kind: tab.kind.into(),
                        })
                        .collect(),
                    active: group.active,
                    weight: self.weights.get(index).copied().unwrap_or(1),
                })
                .collect(),
            focused: self.focused,
        }
    }

    /// Rebuilds the stack from a saved layout, spawning one child per tab.
    /// A tab whose spawn fails is skipped and counted rather than aborting
    /// the restore: a missing shell should cost you that tab, not the
    /// session. Returns how many tabs failed to spawn.
    pub fn restore(
        &mut self,
        saved: &session_layout::SavedLayout,
        mut spawn: impl FnMut(TabId, TabKind, &Path) -> Result<TerminalTab>,
    ) -> usize {
        let mut failed = 0;
        for group in &saved.groups {
            let mut tabs = Vec::with_capacity(group.tabs.len());
            for saved_tab in &group.tabs {
                let id = self.take_id();
                match spawn(id, saved_tab.kind.into(), &saved_tab.path) {
                    Ok(tab) => tabs.push(tab),
                    Err(_) => failed += 1,
                }
            }
            if tabs.is_empty() {
                continue;
            }
            let active = group.active.min(tabs.len() - 1);
            self.groups.push(PaneGroup { tabs, active });
            self.weights.push(group.weight.max(1));
        }
        self.focused = saved.focused.min(self.groups.len().saturating_sub(1));
        failed
    }

    /// Routes one emulator event to the tab it belongs to. Returns `None`
    /// when no live tab has that id (an event from a tab already closed).
    pub fn handle_event(&mut self, id: TabId, event: TermEvent) -> Option<TabEffect> {
        let addr = self.find(id)?;
        Some(self.tab_mut(addr)?.handle_event(event))
    }

    /// Lays the stack out in `area` and resizes every visible group's
    /// active tab to the grid it will be drawn into. Returns one
    /// [`GroupRects`] per rendered group.
    pub fn arrange(&mut self, area: Rect) -> Vec<GroupRects> {
        let rects = layout::split_groups(area, &self.weights);
        let mut out = Vec::with_capacity(rects.len());
        for (index, rect) in rects.into_iter().enumerate() {
            let strip = Rect::new(rect.x, rect.y, rect.width, 1);
            let body = Rect::new(
                rect.x,
                rect.y + 1,
                rect.width,
                rect.height.saturating_sub(1),
            );
            let grid = Block::default().borders(Borders::ALL).inner(body);
            if let Some(tab) = self
                .groups
                .get_mut(index)
                .and_then(PaneGroup::active_tab_mut)
            {
                tab.resize(GridSize {
                    cols: grid.width,
                    lines: grid.height,
                });
            }
            let tab_spans = self
                .groups
                .get(index)
                .map(|g| strip_cells(g, strip).iter().map(|c| c.span).collect())
                .unwrap_or_default();
            out.push(GroupRects {
                strip,
                body,
                grid,
                index,
                tab_spans,
            });
        }
        out
    }

    /// Draws every group: its tab strip, then its active tab.
    pub fn draw(&self, frame: &mut Frame<'_>, rects: &[GroupRects], focused_pane: bool) {
        for group_rects in rects {
            let Some(group) = self.groups.get(group_rects.index) else {
                continue;
            };
            let is_focused = focused_pane && group_rects.index == self.focused;
            draw_tab_strip(frame, group_rects.strip, group, is_focused);
            if let Some(tab) = group.active_tab() {
                tab.draw(frame, group_rects.body, is_focused);
            }
        }
    }
}

/// A group's three rects — its tab strip, the bordered body, and the grid
/// inside that border — plus where each tab sits on the strip.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GroupRects {
    pub strip: Rect,
    pub body: Rect,
    pub grid: Rect,
    pub index: usize,
    /// Each tab's `[start, end)` columns, from the same computation that
    /// renders them — so a click can never resolve to a different tab than
    /// the one under the pointer.
    pub tab_spans: Vec<(u16, u16)>,
}

/// One tab's strip cell: the text drawn and the columns it occupies.
struct StripCell {
    text: String,
    span: (u16, u16),
}

/// Lays the tab strip out for `group` within `area`, in display columns.
/// The single source of truth for both rendering and hit-testing.
fn strip_cells(group: &PaneGroup, area: Rect) -> Vec<StripCell> {
    let mut cells = Vec::with_capacity(group.tabs.len());
    let mut x = area.x;
    let end = area.x.saturating_add(area.width);
    for (index, tab) in group.tabs.iter().enumerate() {
        let text = format!(" {} {} ", index + 1, tab.strip_label());
        let width = u16::try_from(text.width()).unwrap_or(u16::MAX);
        if x >= end {
            break; // the strip is full; the rest are not drawn or clickable
        }
        let stop = x.saturating_add(width).min(end);
        cells.push(StripCell {
            text,
            span: (x, stop),
        });
        // One column of gap between tabs.
        x = stop.saturating_add(1);
    }
    cells
}

/// The mean of the current weights — what a newly split group starts at, so
/// a split neither dominates nor vanishes.
fn average_weight(weights: &[u16]) -> u16 {
    if weights.is_empty() {
        return 1;
    }
    let total: u32 = weights.iter().map(|w| u32::from(*w)).sum();
    u16::try_from(total / u32::try_from(weights.len()).unwrap_or(1))
        .unwrap_or(1)
        .max(1)
}

/// One line of `[1 shell·repo] [2 claude·repo]`, the active tab reversed.
fn draw_tab_strip(frame: &mut Frame<'_>, area: Rect, group: &PaneGroup, focused: bool) {
    if area.height == 0 {
        return;
    }
    let mut spans = Vec::with_capacity(group.tabs.len() * 2);
    for (index, cell) in strip_cells(group, area).into_iter().enumerate() {
        let mut style = Style::default();
        if index == group.active {
            style = style.add_modifier(Modifier::REVERSED);
            if focused {
                style = style.fg(Color::Cyan);
            }
        } else {
            style = style.fg(Color::DarkGray);
        }
        spans.push(Span::styled(cell.text, style));
        spans.push(Span::raw(" "));
    }
    frame.render_widget(Paragraph::new(Line::from(spans)), area);
}

/// The minimum terminal-side height that can host `count` groups — used to
/// refuse a split that would not fit rather than silently dropping a group.
pub fn min_height_for(count: usize) -> u16 {
    u16::try_from(count)
        .unwrap_or(u16::MAX)
        .saturating_mul(MIN_GROUP_HEIGHT)
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;

    #[cfg_attr(not(unix), allow(unused_imports))]
    use tokio::sync::mpsc;

    #[cfg(unix)]
    fn spawner(
        script: &'static str,
        cwd: PathBuf,
        tx: mpsc::UnboundedSender<(TabId, TermEvent)>,
    ) -> impl FnOnce(TabId) -> Result<TerminalTab> {
        move |id| {
            let request = super::super::terminal::pty::SpawnRequest {
                tab: id,
                program: Some((
                    "/bin/sh".to_string(),
                    vec!["-c".to_string(), script.to_string()],
                )),
                cwd,
                size: GridSize { cols: 40, lines: 6 },
                extra_env: Vec::new(),
            };
            TerminalTab::from_request(TabKind::Shell, request, tx)
        }
    }

    #[cfg(unix)]
    fn layout_with(count: usize) -> (PaneLayout, tempfile::TempDir) {
        let dir = tempfile::tempdir().unwrap();
        let (tx, _rx) = mpsc::unbounded_channel();
        let mut panes = PaneLayout::default();
        for _ in 0..count {
            panes
                .split(spawner("sleep 5", dir.path().to_path_buf(), tx.clone()))
                .unwrap();
        }
        (panes, dir)
    }

    #[test]
    fn an_empty_layout_has_no_groups_and_no_active_tab() {
        let panes = PaneLayout::default();
        assert!(panes.is_empty());
        assert_eq!(panes.group_count(), 0);
        assert!(panes.active_tab().is_none());
        assert!(!panes.any_alive());
        assert!(panes.find(1).is_none());
        assert!(panes.group_tab(0).is_none());
        assert!(panes.open_worktrees().is_empty());
    }

    #[test]
    fn average_weight_of_nothing_is_one() {
        assert_eq!(average_weight(&[]), 1);
        assert_eq!(average_weight(&[2, 4]), 3);
        assert_eq!(average_weight(&[0, 0]), 1, "never zero");
    }

    #[test]
    fn min_height_scales_with_the_group_count() {
        assert_eq!(min_height_for(1), MIN_GROUP_HEIGHT);
        assert_eq!(min_height_for(3), MIN_GROUP_HEIGHT * 3);
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn opening_adds_to_the_focused_group_and_splitting_makes_a_new_one() {
        let (mut panes, dir) = layout_with(0);
        let (tx, _rx) = mpsc::unbounded_channel();
        let spawn = || spawner("sleep 5", dir.path().to_path_buf(), tx.clone());

        // The first open creates the one group.
        let first = panes.open_tab(spawn()).unwrap();
        assert_eq!(first, TabAddr { group: 0, tab: 0 });
        assert_eq!(panes.group_count(), 1);
        assert_eq!(panes.weights, vec![1]);
        assert!(panes.any_alive());

        // The second joins it as a tab.
        let second = panes.open_tab(spawn()).unwrap();
        assert_eq!(second, TabAddr { group: 0, tab: 1 });
        assert_eq!(panes.group_count(), 1);
        assert_eq!(panes.groups[0].tabs.len(), 2);
        assert_eq!(panes.groups[0].active, 1, "a new tab takes focus");

        // A split makes a second group below, and focuses it.
        let split = panes.split(spawn()).unwrap();
        assert_eq!(split, TabAddr { group: 1, tab: 0 });
        assert_eq!(panes.group_count(), 2);
        assert_eq!(panes.weights.len(), 2);
        assert_eq!(panes.focused, 1);

        // Ids are unique and never reused.
        let ids: Vec<TabId> = panes.tabs().map(TerminalTab::id).collect();
        let unique: HashSet<TabId> = ids.iter().copied().collect();
        assert_eq!(ids.len(), unique.len());
        for tab in panes.groups.iter_mut().flat_map(|g| g.tabs.iter_mut()) {
            tab.shutdown();
        }
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn closing_removes_the_tab_then_its_group_and_reports_the_worktree() {
        let (mut panes, dir) = layout_with(2);
        let here = dir.path().to_path_buf();
        assert_eq!(panes.group_count(), 2);

        // One worktree, two tabs: closing one leaves the cue set.
        let closed = panes.close_active().unwrap();
        assert_eq!(closed, here);
        assert_eq!(panes.group_count(), 1, "the emptied group went with it");
        assert_eq!(panes.weights.len(), 1);
        assert_eq!(panes.focused, 0);
        assert!(panes.open_worktrees().contains(&here), "one tab remains");

        // Closing the last one empties the layout.
        assert_eq!(panes.close_active(), Some(here));
        assert!(panes.is_empty());
        assert!(panes.open_worktrees().is_empty());
        assert_eq!(panes.focused, 0);
        assert!(panes.close_active().is_none(), "nothing left to close");
        assert!(panes.close_tab(TabAddr { group: 9, tab: 9 }).is_none());
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn close_other_tabs_leaves_the_anchor_and_reports_every_closed_worktree() {
        let (mut panes, _dir) = layout_with(0);
        let (tx, _rx) = mpsc::unbounded_channel();
        let dirs: Vec<tempfile::TempDir> = (0..4).map(|_| tempfile::tempdir().unwrap()).collect();
        for d in &dirs {
            panes
                .open_tab(spawner("sleep 5", d.path().to_path_buf(), tx.clone()))
                .unwrap();
        }
        assert_eq!(panes.groups[0].tabs.len(), 4);
        let anchor = dirs[1].path().to_path_buf();

        let closed = panes.close_other_tabs(TabAddr { group: 0, tab: 1 });

        assert_eq!(panes.group_count(), 1, "never empties the group");
        assert_eq!(panes.groups[0].tabs.len(), 1, "only the anchor remains");
        assert_eq!(panes.groups[0].tabs[0].opened_in, anchor);
        assert_eq!(
            panes.groups[0].active, 0,
            "the survivor is now the active tab"
        );
        assert_eq!(panes.focused, 0);
        let expected: HashSet<PathBuf> = [
            dirs[0].path().to_path_buf(),
            dirs[2].path().to_path_buf(),
            dirs[3].path().to_path_buf(),
        ]
        .into_iter()
        .collect();
        let got: HashSet<PathBuf> = closed.iter().cloned().collect();
        assert_eq!(
            got, expected,
            "every non-anchor tab is reported, not just one"
        );
        assert_eq!(closed.len(), 3, "no duplicates, no anchor");

        // Out-of-range addresses are no-ops.
        assert!(panes
            .close_other_tabs(TabAddr { group: 9, tab: 0 })
            .is_empty());
        assert!(panes
            .close_other_tabs(TabAddr { group: 0, tab: 9 })
            .is_empty());
        assert_eq!(
            panes.groups[0].tabs.len(),
            1,
            "the bad addresses touched nothing"
        );

        for tab in panes.groups.iter_mut().flat_map(|g| g.tabs.iter_mut()) {
            tab.shutdown();
        }
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn close_tabs_to_right_leaves_the_anchor_and_everything_left_of_it() {
        let (mut panes, _dir) = layout_with(0);
        let (tx, _rx) = mpsc::unbounded_channel();
        let dirs: Vec<tempfile::TempDir> = (0..4).map(|_| tempfile::tempdir().unwrap()).collect();
        for d in &dirs {
            panes
                .open_tab(spawner("sleep 5", d.path().to_path_buf(), tx.clone()))
                .unwrap();
        }
        // The last-opened tab holds focus already; closing to its right of
        // the anchor leaves `active` pointing past the surviving tabs, so
        // this also exercises the clamp.
        assert_eq!(panes.groups[0].active, 3);

        let closed = panes.close_tabs_to_right(TabAddr { group: 0, tab: 1 });

        assert_eq!(panes.group_count(), 1, "never empties the group");
        assert_eq!(
            panes.groups[0].tabs.len(),
            2,
            "the anchor and everything left of it survive"
        );
        assert_eq!(panes.groups[0].tabs[0].opened_in, dirs[0].path());
        assert_eq!(panes.groups[0].tabs[1].opened_in, dirs[1].path());
        assert_eq!(
            panes.groups[0].active, 1,
            "active is clamped back into range"
        );
        assert_eq!(panes.focused, 0);
        assert_eq!(
            closed,
            vec![dirs[2].path().to_path_buf(), dirs[3].path().to_path_buf()],
            "every tab to the right is reported, in close order, not just one"
        );

        // Out-of-range addresses are no-ops.
        assert!(panes
            .close_tabs_to_right(TabAddr { group: 9, tab: 0 })
            .is_empty());
        assert!(panes
            .close_tabs_to_right(TabAddr { group: 0, tab: 9 })
            .is_empty());
        assert_eq!(
            panes.groups[0].tabs.len(),
            2,
            "the bad addresses touched nothing"
        );

        for tab in panes.groups.iter_mut().flat_map(|g| g.tabs.iter_mut()) {
            tab.shutdown();
        }
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn close_group_removes_the_group_and_reports_every_closed_worktree() {
        let (mut panes, _dir) = layout_with(0);
        let (tx, _rx) = mpsc::unbounded_channel();
        let dirs: Vec<tempfile::TempDir> = (0..3).map(|_| tempfile::tempdir().unwrap()).collect();
        for d in &dirs {
            panes
                .split(spawner("sleep 5", d.path().to_path_buf(), tx.clone()))
                .unwrap();
        }
        assert_eq!(panes.group_count(), 3);

        // Give the middle group a second tab so closing it retires more
        // than one worktree at once.
        panes.focused = 1;
        let extra_dir = tempfile::tempdir().unwrap();
        panes
            .open_tab(spawner("sleep 5", extra_dir.path().to_path_buf(), tx))
            .unwrap();
        assert_eq!(panes.groups[1].tabs.len(), 2);
        panes.weights = vec![1, 2, 3];
        panes.focused = 2; // the last group, so removing group 1 shifts it down

        let closed = panes.close_group(1);

        assert_eq!(panes.group_count(), 2, "the closed group is removed");
        assert_eq!(panes.weights, vec![1, 3], "its weight goes with it");
        let expected: HashSet<PathBuf> =
            [dirs[1].path().to_path_buf(), extra_dir.path().to_path_buf()]
                .into_iter()
                .collect();
        let got: HashSet<PathBuf> = closed.iter().cloned().collect();
        assert_eq!(
            got, expected,
            "every tab in the group is reported, not just one"
        );
        assert_eq!(closed.len(), 2, "no duplicates");
        assert_eq!(
            panes.focused, 1,
            "a later focus shifts down with the removed group"
        );

        for tab in panes.groups.iter_mut().flat_map(|g| g.tabs.iter_mut()) {
            tab.shutdown();
        }
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn close_group_clamps_focus_when_it_was_the_last_group() {
        let (mut panes, dir) = layout_with(1);
        let here = dir.path().to_path_buf();
        assert_eq!(panes.group_count(), 1);

        let closed = panes.close_group(0);

        assert_eq!(closed, vec![here], "the only tab is reported");
        assert!(panes.is_empty(), "the layout is now empty");
        assert!(panes.weights.is_empty());
        assert_eq!(
            panes.focused, 0,
            "focus is clamped back into range, not left dangling"
        );

        // Out-of-range addresses — including on an already-empty layout —
        // are no-ops.
        assert!(panes.close_group(0).is_empty());
        assert!(panes.close_group(9).is_empty());
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn close_group_with_an_out_of_range_index_is_a_no_op() {
        let (mut panes, _dir) = layout_with(2);

        assert!(panes.close_group(9).is_empty());
        assert_eq!(panes.group_count(), 2, "nothing was touched");
        assert_eq!(panes.weights.len(), 2);

        for tab in panes.groups.iter_mut().flat_map(|g| g.tabs.iter_mut()) {
            tab.shutdown();
        }
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn tabs_and_groups_cycle_select_and_move() {
        let (mut panes, dir) = layout_with(1);
        let (tx, _rx) = mpsc::unbounded_channel();
        let spawn = || spawner("sleep 5", dir.path().to_path_buf(), tx.clone());
        panes.open_tab(spawn()).unwrap();
        panes.open_tab(spawn()).unwrap(); // group 0 now has three tabs
        assert_eq!(panes.groups[0].tabs.len(), 3);

        // Cycling wraps in both directions.
        assert_eq!(panes.groups[0].active, 2);
        panes.cycle_tab(1);
        assert_eq!(panes.groups[0].active, 0, "wraps forward");
        panes.cycle_tab(-1);
        assert_eq!(panes.groups[0].active, 2, "wraps back");
        assert!(panes.select_tab(1));
        assert_eq!(panes.groups[0].active, 1);
        assert!(!panes.select_tab(9), "out of range selects nothing");
        assert_eq!(panes.groups[0].active, 1);

        // A second group: focus moves between them, clamped at the ends.
        panes.split(spawn()).unwrap();
        assert_eq!(panes.focused, 1);
        panes.cycle_group(-1);
        assert_eq!(panes.focused, 0);
        panes.cycle_group(-1);
        assert_eq!(panes.focused, 0, "clamped, not wrapped");
        panes.cycle_group(5);
        assert_eq!(panes.focused, 1);

        // Moving the only tab out of a group removes that group.
        assert!(panes.move_tab_to_group(-1));
        assert_eq!(panes.group_count(), 1);
        assert_eq!(panes.focused, 0);
        assert_eq!(panes.groups[0].tabs.len(), 4);
        assert!(
            !panes.move_tab_to_group(-1),
            "one group: nothing to move to"
        );

        panes.reset_weights();
        assert_eq!(panes.weights, vec![1]);
        for tab in panes.groups.iter_mut().flat_map(|g| g.tabs.iter_mut()) {
            tab.shutdown();
        }
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn events_route_by_tab_id_and_unknown_ids_are_ignored() {
        let (mut panes, _dir) = layout_with(2);
        let ids: Vec<TabId> = panes.tabs().map(TerminalTab::id).collect();
        assert_eq!(
            panes.handle_event(ids[0], TermEvent::Title("t".to_string())),
            Some(TabEffect::Redraw)
        );
        assert_eq!(panes.groups[0].tabs[0].title.as_deref(), Some("t"));
        assert!(
            panes.groups[1].tabs[0].title.is_none(),
            "the other tab is untouched"
        );
        assert_eq!(panes.handle_event(9999, TermEvent::Wakeup), None);
        for tab in panes.groups.iter_mut().flat_map(|g| g.tabs.iter_mut()) {
            tab.shutdown();
        }
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn arrange_sizes_every_group_and_draw_renders_the_strip() {
        use ratatui::backend::TestBackend;
        use ratatui::Terminal;

        let (mut panes, _dir) = layout_with(2);
        let area = Rect::new(0, 0, 50, 24);
        let rects = panes.arrange(area);
        assert_eq!(rects.len(), 2);
        assert_eq!(rects[0].strip.height, 1);
        assert_eq!(rects[1].body.y, rects[0].body.y + rects[0].body.height + 1);
        // Every group's grid is inside its body's border.
        for r in &rects {
            assert!(r.grid.width < r.body.width);
            assert_eq!(r.grid.y, r.body.y + 1);
        }

        let mut terminal = Terminal::new(TestBackend::new(50, 24)).unwrap();
        terminal
            .draw(|frame| {
                let rects = panes.arrange(frame.area());
                panes.draw(frame, &rects, true);
            })
            .unwrap();
        let text: String = terminal
            .backend()
            .buffer()
            .content
            .iter()
            .map(ratatui::buffer::Cell::symbol)
            .collect();
        assert!(text.contains("1 shell"), "the tab strip names the tab");

        // Too short for two groups: only what fits is arranged and drawn.
        let short = panes.arrange(Rect::new(0, 0, 50, 5));
        assert_eq!(short.len(), 1);
        for tab in panes.groups.iter_mut().flat_map(|g| g.tabs.iter_mut()) {
            tab.shutdown();
        }
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn a_layout_survives_a_save_and_restore_round_trip() {
        let (mut panes, dir) = layout_with(2);
        let here = dir.path().to_path_buf();
        // Give the first group a second tab and an uneven weight, so the
        // round trip has something to get wrong.
        let (tx, _rx) = mpsc::unbounded_channel();
        panes.focused = 0;
        panes
            .open_tab(spawner("sleep 5", here.clone(), tx.clone()))
            .unwrap();
        panes.weights = vec![3, 1];
        panes.focused = 1;

        let saved = panes.to_saved();
        assert_eq!(saved.groups.len(), 2);
        assert_eq!(saved.groups[0].tabs.len(), 2);
        assert_eq!(saved.groups[0].weight, 3);
        assert_eq!(saved.focused, 1);
        assert_eq!(saved.groups[0].tabs[0].path, here);

        for tab in panes.groups.iter_mut().flat_map(|g| g.tabs.iter_mut()) {
            tab.shutdown();
        }

        // Restoring rebuilds the same shape with fresh children.
        let mut restored = PaneLayout::default();
        let failed = restored.restore(&saved, |id, kind, path| {
            let request = super::super::terminal::pty::SpawnRequest {
                tab: id,
                program: Some((
                    "/bin/sh".to_string(),
                    vec!["-c".to_string(), "sleep 5".to_string()],
                )),
                cwd: path.to_path_buf(),
                size: GridSize { cols: 40, lines: 6 },
                extra_env: Vec::new(),
            };
            TerminalTab::from_request(kind, request, tx.clone())
        });
        assert_eq!(failed, 0);
        assert_eq!(restored.group_count(), 2);
        assert_eq!(restored.groups[0].tabs.len(), 2);
        assert_eq!(restored.weights, vec![3, 1]);
        assert_eq!(restored.focused, 1);
        assert!(restored.any_alive(), "restored tabs are live children");
        // Ids are freshly allocated, never carried over.
        assert!(restored.tabs().all(|t| t.id() > 0));
        for tab in restored.groups.iter_mut().flat_map(|g| g.tabs.iter_mut()) {
            tab.shutdown();
        }
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn restore_skips_tabs_that_will_not_spawn_and_drops_emptied_groups() {
        let dir = tempfile::tempdir().unwrap();
        let saved = session_layout::SavedLayout {
            version: 1,
            groups: vec![
                session_layout::SavedGroup {
                    tabs: vec![session_layout::SavedTab {
                        path: dir.path().to_path_buf(),
                        kind: session_layout::SavedTabKind::Shell,
                    }],
                    active: 0,
                    weight: 1,
                },
                session_layout::SavedGroup {
                    tabs: vec![session_layout::SavedTab {
                        path: dir.path().to_path_buf(),
                        kind: session_layout::SavedTabKind::Claude,
                    }],
                    active: 0,
                    weight: 1,
                },
            ],
            focused: 1,
        };
        let (tx, _rx) = mpsc::unbounded_channel();
        let mut panes = PaneLayout::default();
        // Fail every Claude tab, as a missing binary would.
        let failed = panes.restore(&saved, |id, kind, path| {
            if kind == TabKind::Claude {
                anyhow::bail!("no such program");
            }
            let request = super::super::terminal::pty::SpawnRequest {
                tab: id,
                program: Some((
                    "/bin/sh".to_string(),
                    vec!["-c".to_string(), "sleep 5".to_string()],
                )),
                cwd: path.to_path_buf(),
                size: GridSize { cols: 40, lines: 6 },
                extra_env: Vec::new(),
            };
            TerminalTab::from_request(kind, request, tx.clone())
        });
        assert_eq!(failed, 1, "the failure is counted, not swallowed");
        assert_eq!(panes.group_count(), 1, "its emptied group is dropped");
        assert_eq!(panes.focused, 0, "focus is clamped back into range");
        for tab in panes.groups.iter_mut().flat_map(|g| g.tabs.iter_mut()) {
            tab.shutdown();
        }
    }

    #[test]
    fn an_empty_layout_saves_as_empty() {
        let panes = PaneLayout::default();
        assert!(panes.to_saved().is_empty());
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn find_in_worktree_matches_kind_and_liveness() {
        let (mut panes, dir) = layout_with(1);
        let here = dir.path().to_path_buf();
        assert_eq!(
            panes.find_in_worktree(&here, TabKind::Shell),
            Some(TabAddr { group: 0, tab: 0 })
        );
        assert!(panes.find_in_worktree(&here, TabKind::Claude).is_none());
        assert!(panes
            .find_in_worktree(Path::new("/nowhere"), TabKind::Shell)
            .is_none());

        // An exited tab is not a match — the row should open a fresh one.
        panes.groups[0].tabs[0].exit_status = Some(std::process::ExitStatus::default());
        assert!(panes.find_in_worktree(&here, TabKind::Shell).is_none());
        assert!(!panes.any_alive());

        // Focusing an address out of range is a no-op, not a panic.
        panes.focus(TabAddr { group: 9, tab: 9 });
        assert_eq!(panes.focused, 0);
        panes.focus(TabAddr { group: 0, tab: 0 });
        for tab in panes.groups.iter_mut().flat_map(|g| g.tabs.iter_mut()) {
            tab.shutdown();
        }
    }
}