mnml-rs 0.2.14

A NvChad-style terminal IDE in Rust — vim or standard editing, LSP, git, and an embedded HTTP client.
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
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
//! Workspace management methods on `App` (A-4 of the file-split
//! refactor — 2026-06-28). Owns runtime add/remove/switch/promote
//! across primary + extra workspaces, plus the workspaces-editor
//! overlay (rename, path edit, group edit, kebab menu).
//!
//! Extracted from `src/app/mod.rs`. Pure non-destructive move — every
//! method keeps its signature + visibility, only the file changes.

use super::*;

impl App {
    pub fn toggle_extra_workspace(&mut self, ws_idx: usize) {
        if let Some(ws) = self.extra_workspaces.get_mut(ws_idx) {
            ws.expanded = !ws.expanded;
        }
    }

    /// Handle a click on a row inside an extra-workspace's body. Updates that
    /// tree's cursor, then opens the file or toggles the dir under it.
    /// `recursive = true` triggers recursive expand/collapse on the dir
    /// (Alt+click gesture). Repo-dir clicks also switch the active repo
    /// (sibling of the primary-tree behavior in `tui::dispatch_mouse`).
    pub fn click_extra_workspace_row_ex(&mut self, ws_idx: usize, row_idx: usize, recursive: bool) {
        let Some(ws) = self.extra_workspaces.get_mut(ws_idx) else {
            return;
        };
        let rows = ws.tree.visible_rows();
        if row_idx >= rows.len() {
            return;
        }
        ws.tree.set_cursor(row_idx);
        // Park keyboard focus on this extra workspace so the
        // renderer draws a cursor highlight + so future arrow
        // keys move within this tree (not the primary one).
        self.focus_tree();
        self.focused_extra_ws = Some(ws_idx);
        self.rail_section = RailSection::Workspace;
        let row = rows[row_idx].clone();
        if row.is_dir {
            // Multi-repo: clicking a depth-0 repo dir activates that repo so
            // the git rail follows. Same gesture as the primary tree.
            if row.depth == 0 && self.repos.len() > 1 {
                let repo_hit = self.repos.iter().position(|r| r.path == row.path);
                if let Some(idx) = repo_hit
                    && idx != self.active_repo
                {
                    self.switch_active_repo(idx);
                }
            }
            // Refetch the tree (may have been mutated by switch_active_repo)
            // and toggle. We only need the path's dir state to decide.
            if let Some(ws) = self.extra_workspaces.get_mut(ws_idx) {
                if recursive {
                    ws.tree.toggle_current_recursive();
                } else {
                    ws.tree.toggle_current();
                }
            }
        } else {
            self.open_path(&row.path);
        }
    }

    /// Runtime add: append a new extra workspace at `path` with a name
    /// derived from the path's basename (or the user-supplied name). Builds
    /// the tree + appends repos to the unified `repos` list. The new entry
    /// shows up as a new collapsible section in the rail; not persisted to
    /// config.toml — the user has to add the `[[workspaces]]` entry there
    /// for it to survive a relaunch (caller toasts the hint).
    pub fn add_workspace_runtime(&mut self, path: PathBuf, name: Option<String>) {
        let root = match path.canonicalize() {
            Ok(p) => p,
            Err(e) => {
                self.toast(format!("can't open workspace: {e}"));
                return;
            }
        };
        if root == self.workspace || self.extra_workspaces.iter().any(|w| w.root == root) {
            self.toast("workspace already open");
            return;
        }
        let resolved_name = name.unwrap_or_else(|| {
            root.file_name()
                .map(|n| n.to_string_lossy().into_owned())
                .unwrap_or_else(|| root.to_string_lossy().into_owned())
        });
        // Empty-state special case: when the primary workspace is
        // $HOME (the "no workspace open" landing), promote the new
        // path to primary rather than adding as an extra. Otherwise
        // the empty-state widget stays visible alongside the new
        // tree, which is the user-confusing state described in the
        // bug report. From a real primary workspace, fall through
        // to the existing add-as-extra path.
        if is_home_workspace(&self.workspace) {
            self.promote_to_primary_workspace(root, resolved_name);
            return;
        }
        // qa-feature 2026-07-01 — new workspaces open COLLAPSED at
        // the top level. Was: `expanded: true` + auto-expand of the
        // first sub-repo, which slammed the rail with a full tree the
        // moment you opened a second workspace. User asked for each
        // workspace to sit as a collapsed root; the user drills in
        // manually.
        let tree = Tree::open(&root);
        let mut found = crate::git::repos::discover_repos(&root);
        let position = self.next_free_workspace_position();
        self.extra_workspaces.push(ExtraWorkspace {
            name: resolved_name.clone(),
            root,
            tree,
            expanded: false,
            position,
        });
        self.repos.append(&mut found);
        self.toast(format!(
            "workspace added: {resolved_name} (also add to `[[workspaces]]` in config.toml to persist)"
        ));
    }

    /// Replace the PRIMARY workspace root with `path`. Used by
    /// [`Self::add_workspace_runtime`] when the user picks a folder
    /// while sitting on the empty-state landing ($HOME-as-workspace);
    /// promoting-to-primary is what the user expects instead of
    /// stacking the new folder as an extra.
    ///
    /// Side effects:
    ///   * `self.workspace` swaps to the new canonical root
    ///   * the primary tree is re-opened on the new root
    ///   * `self.repos` is replaced with `discover_repos(new root)`
    ///   * the empty-state predicate now returns false, so the
    ///     landing widget hides on the next render
    ///
    /// Anything keyed to the old workspace path that wants to
    /// survive ($HOME .mnml/ipc, session.json, git CWD context, etc.)
    /// would need to be re-initialized here. v0.1 takes the simpler
    /// path: we toast the user to relaunch if they care about a
    /// fresh session for the new workspace, and refresh the tree +
    /// repos. The user's mental model is "I just opened the
    /// workspace I wanted" — the rest of the side effects can be
    /// addressed in v0.2 once we see what breaks.
    pub(crate) fn promote_to_primary_workspace(&mut self, root: PathBuf, name: String) {
        // qa-feature 2026-07-01 — SWAP POSITIONS ONLY. The
        // primary + extras share a single stable ordering (each
        // has a `.position`); promoting an extra swaps its
        // `position` with `self.primary_position` and moves the
        // OLD primary into that extra slot, so the visible list
        // never reshuffles. Only the `●` marker moves. See the
        // `preserve original order` design decision — the earlier
        // "swap slots" version reads as weird because the
        // demoted workspace lands in the promoted one's OLD
        // slot instead of staying where it lives in the list.
        let tree = Tree::open(&root);
        let found = crate::git::repos::discover_repos(&root);
        let old_primary_root = std::mem::replace(&mut self.workspace, root.clone());
        let old_primary_name = old_primary_root
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("workspace")
            .to_string();
        let old_primary_position = self.primary_position;
        if let Some(pos) = self.extra_workspaces.iter().position(|w| w.root == root) {
            // Target was an extra. Promote its `.position` to
            // primary_position; replace its slot with the demoted
            // old primary carrying the new primary's OLD
            // position. Net: both positions swap.
            let target_position = self.extra_workspaces[pos].position;
            self.primary_position = target_position;
            self.extra_workspaces[pos] = ExtraWorkspace {
                name: old_primary_name.clone(),
                root: old_primary_root.clone(),
                tree: Tree::open(&old_primary_root),
                expanded: false,
                position: old_primary_position,
            };
        } else if old_primary_root != root {
            // Target came from outside the current extras (e.g. a
            // freshly-picked folder). Give the new primary a
            // fresh slot at the bottom; the OLD primary keeps its
            // original position but now sits in extras.
            let new_primary_position = self.next_free_workspace_position();
            self.primary_position = new_primary_position;
            self.extra_workspaces.push(ExtraWorkspace {
                name: old_primary_name.clone(),
                root: old_primary_root.clone(),
                tree: Tree::open(&old_primary_root),
                expanded: false,
                position: old_primary_position,
            });
        }

        // Rebuild the flat repo list from the NEW primary + all
        // extras in position order so tree-side lookups (green
        // dot, active repo) map to the right rows.
        self.tree = tree;
        self.repos = found;
        // api-workflow SEV-2 2026-07-11: `http_env_override` used to
        // survive workspace swaps. A user picking `dev` in workspace
        // A, then jumping to workspace B, silently resolved B's
        // requests against `.mnml/env/dev.env` there — either
        // masking B's real defaults or blowing up on a missing file.
        // Reset per-workspace state that doesn't make sense
        // cross-workspace.
        self.http_env_override = None;
        let mut extras_by_pos: Vec<&ExtraWorkspace> = self.extra_workspaces.iter().collect();
        extras_by_pos.sort_by_key(|w| w.position);
        let extra_roots: Vec<PathBuf> = extras_by_pos.iter().map(|w| w.root.clone()).collect();
        for extra_root in &extra_roots {
            let mut extra_repos = crate::git::repos::discover_repos(extra_root);
            self.repos.append(&mut extra_repos);
        }
        self.active_repo = 0;
        let new_root = self.active_repo_path().to_path_buf();
        self.git.retarget(&new_root);
        self.git_rail.refresh(&new_root);
        self.git_palette_selected = None;
        self.refresh_rail_pulls();
        for pane in &mut self.panes {
            match pane {
                Pane::GitStatus(g) => g.retarget(&new_root),
                Pane::GitGraph(g) => g.retarget(&new_root),
                _ => {}
            }
        }
        // qa-feature 2026-07-01 — drop any stale "workspace opened:"
        // toasts from the stack so back-to-back promotes don't leave
        // the previous name lingering next to the new one. Without
        // this the user saw two stacked toast boxes after clicking
        // a second `○` while the first was still within its 4s TTL.
        self.toast_stack
            .retain(|e| !e.text.starts_with("workspace opened:"));
        self.toast(format!("workspace opened: {name}"));
    }

    /// qa-feature 2026-07-01 — Remove the currently-primary workspace.
    /// Promotes the first extra (in position order) to primary, then
    /// drops the just-demoted OLD primary from the list. No-op when
    /// there are no extras — the context-menu item is hidden in that
    /// case, but we double-guard here so a stale command / rebind
    /// can't leave the app with nothing loaded.
    /// #polish 2026-07-06 — right-click "Set as default workspace"
    /// on the workspace-header rail row. Toggles the persisted
    /// `[startup] default_workspace` in `~/.config/mnml/config.toml`:
    /// - if not currently the default → set it
    /// - if currently the default → clear it
    ///
    /// Writes via `crate::config::persist_default_workspace`, updates
    /// the in-memory config, toasts the result.
    pub fn toggle_default_workspace(&mut self) {
        let ws = self.workspace.clone();
        self.toggle_default_workspace_for(&ws);
    }

    /// Same as `toggle_default_workspace` but targeting an
    /// arbitrary path (used by the extra-workspace right-click and
    /// the Manage-workspaces kebab).
    pub fn toggle_default_workspace_for(&mut self, target: &std::path::Path) {
        let current = self
            .config
            .default_workspace
            .clone()
            .and_then(|p| std::fs::canonicalize(&p).ok());
        let target_canon = std::fs::canonicalize(target).ok();
        let already_default = current.is_some() && current == target_canon;
        let new_value = if already_default { None } else { Some(target) };
        match crate::config::persist_default_workspace(new_value) {
            Ok(_) => {
                self.config.default_workspace = new_value.map(|p| p.to_path_buf());
                let ws_label = target
                    .file_name()
                    .and_then(|s| s.to_str())
                    .unwrap_or("workspace");
                if already_default {
                    self.toast("default workspace cleared");
                } else {
                    self.toast(format!("default workspace: {ws_label}"));
                }
            }
            Err(e) => self.toast(format!("default workspace: {e}")),
        }
    }

    /// Workspaces-editor kebab → "Set as default" / "Unset as
    /// default". Persists to `~/.config/mnml/config.toml` and
    /// updates the in-memory config.
    pub fn workspaces_editor_toggle_default(&mut self, idx: usize) {
        let Some(entry) = self.config.workspaces.get(idx).cloned() else {
            return;
        };
        self.toggle_default_workspace_for(&entry.path);
    }

    pub fn remove_primary_workspace(&mut self) {
        if self.extra_workspaces.is_empty() {
            self.toast("can't remove: no other workspace to fall back on");
            return;
        }
        // Pick the extra with the smallest .position — the visually
        // topmost row after the primary.
        let Some(target_idx) = self
            .extra_workspaces
            .iter()
            .enumerate()
            .min_by_key(|(_, w)| w.position)
            .map(|(i, _)| i)
        else {
            return;
        };
        let target = self.extra_workspaces[target_idx].root.clone();
        let target_name = self.extra_workspaces[target_idx].name.clone();
        // Snapshot the OLD primary's root before promotion swaps them.
        let old_primary_root = self.workspace.clone();
        // Promote — swaps `.position` and moves the OLD primary into the
        // target's slot in `extra_workspaces`.
        self.promote_to_primary_workspace(target, target_name);
        // Drop the demoted OLD primary from the extras.
        self.extra_workspaces.retain(|w| w.root != old_primary_root);
        // Rebuild the flat repo list since we dropped a workspace's repos.
        let mut fresh_repos = crate::git::repos::discover_repos(&self.workspace);
        for extra in &self.extra_workspaces {
            let mut extra_repos = crate::git::repos::discover_repos(&extra.root);
            fresh_repos.append(&mut extra_repos);
        }
        self.repos = fresh_repos;
        self.active_repo = 0;
        let name = old_primary_root
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("workspace")
            .to_string();
        self.toast(format!("workspace removed: {name}"));
    }

    /// Smallest positive integer not already in use by
    /// `primary_position` or any extra's `.position`.
    pub(crate) fn next_free_workspace_position(&self) -> usize {
        let mut used: std::collections::HashSet<usize> =
            self.extra_workspaces.iter().map(|w| w.position).collect();
        used.insert(self.primary_position);
        (0..).find(|p| !used.contains(p)).unwrap_or(0)
    }

    /// Right-click → "Set as workspace" from the tree context menu.
    /// Promotes `path` to the primary workspace regardless of the
    /// current empty-state / has-extras situation. Canonicalises the
    /// path so the resolved root is consistent with everything else
    /// in App that reads `self.workspace`.
    /// `..` tree row: navigate the workspace root up one level (e.g.
    /// from `~/Projects/mnml` → `~/Projects`). Uses `set_workspace_to`
    /// so tree, repos, git state, integrations all reload consistently.
    /// No-op at filesystem root. 2026-07-07.
    pub fn navigate_workspace_up(&mut self) {
        let Some(parent) = self.workspace.parent() else {
            self.toast("already at filesystem root");
            return;
        };
        let parent = parent.to_path_buf();
        self.set_workspace_to(parent);
    }

    pub fn set_workspace_to(&mut self, path: PathBuf) {
        let root = match path.canonicalize() {
            Ok(p) => p,
            Err(e) => {
                self.toast(format!("can't open workspace: {e}"));
                return;
            }
        };
        if root == self.workspace {
            self.toast("workspace already active");
            return;
        }
        let name = root
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_else(|| root.to_string_lossy().into_owned());
        // Reuse `promote_to_primary_workspace` so the side-effects
        // (tree reload, repos rescan, toast) are consistent with the
        // existing workspace-replacement path.
        self.promote_to_primary_workspace(root, name);
    }

    /// Runtime remove: drop the extra workspace at index `idx` (1-based,
    /// matching the workspace-switcher picker convention where 0 is the
    /// primary). Removes its repos from `App.repos`. Primary workspace
    /// can't be removed.
    pub fn remove_workspace_runtime(&mut self, idx: usize) {
        if idx == 0 {
            self.toast("can't remove the primary (launched) workspace");
            return;
        }
        let ws_idx = idx - 1;
        if ws_idx >= self.extra_workspaces.len() {
            return;
        }
        let removed = self.extra_workspaces.remove(ws_idx);
        // Strip repos that lived under this workspace's root.
        let was_active = self
            .repos
            .get(self.active_repo)
            .map(|r| r.path.starts_with(&removed.root))
            .unwrap_or(false);
        self.repos.retain(|r| !r.path.starts_with(&removed.root));
        if was_active {
            self.active_repo = 0;
            if let Some(p) = self.repos.first().map(|r| r.path.clone()) {
                self.git.retarget(&p);
            }
        } else if self.active_repo >= self.repos.len() {
            self.active_repo = self.repos.len().saturating_sub(1);
        }
        // Persist the removal to `[[workspaces]]` in the global
        // config — WITHOUT this the entry sticks around and the
        // startup path re-adds it on the next launch, which reads
        // as a bug ("I removed it, why is it back?"). Match by
        // canonical path so name-only edits still resolve. Silent
        // no-op when the entry isn't in the config (a workspace
        // added via `:view.add_workspace` and never persisted).
        let removed_root = removed.root.clone();
        let cfg_position = self.config.workspaces.iter().position(|w| {
            std::fs::canonicalize(&w.path)
                .map(|p| p == removed_root)
                .unwrap_or(false)
        });
        let restored_config = cfg_position.map(|i| self.config.workspaces.remove(i));
        if restored_config.is_some()
            && let Err(e) = crate::config::persist_workspaces_to_global(&self.config.workspaces)
        {
            self.toast(format!("save workspaces: {e}"));
        }
        // #20 — offer undo for the removal. Captures both the
        // config entry and its index so the restore path can put
        // it back exactly where the user had it.
        let name = removed.name.clone();
        if let (Some(cfg), Some(pos)) = (restored_config, cfg_position) {
            self.set_pending_undo(
                format!("removed workspace {name}"),
                crate::app::UndoAction::RestoreWorkspace {
                    config: cfg,
                    position: pos,
                },
            );
        }
        self.toast(format!("workspace removed: {name}"));
    }

    /// Picker accept handler for [`PickerKind::Workspaces`]. Expands the
    /// chosen workspace's tree section (collapses other extras so the rail
    /// reads as "this is the one I'm working in"). Primary workspace just
    /// gets focused.
    /// qa-feature 2026-07-02 — notification-only update flow. Fires
    /// one toast per session with a channel-appropriate upgrade
    /// instruction (`cargo install …`, `brew upgrade …`, or a GitHub
    /// URL for .app users). No in-app installer.
    pub(crate) fn maybe_announce_update(&mut self) {
        let Some(uc) = self.update_check.as_ref() else {
            return;
        };
        let Some(latest) = uc.take_pending_announcement() else {
            return;
        };
        let channel = uc.channel;
        self.toast(format!(
            "mnml v{latest} available — {}",
            channel.upgrade_hint(&latest)
        ));
    }

    pub fn switch_workspace(&mut self, idx: usize) {
        // 0 = primary, 1+ = extras (offset by -1 into `extra_workspaces`).
        self.focus_tree();
        self.rail_section = RailSection::Workspace;
        if idx == 0 {
            self.tree_root_expanded = true;
            for w in &mut self.extra_workspaces {
                w.expanded = false;
            }
            return;
        }
        let ws_idx = idx - 1;
        if ws_idx >= self.extra_workspaces.len() {
            return;
        }
        self.tree_root_expanded = false;
        for (i, w) in self.extra_workspaces.iter_mut().enumerate() {
            w.expanded = i == ws_idx;
        }
    }

    pub fn open_workspaces_editor(&mut self) {
        // Close settings first so the new overlay shows on top
        // cleanly.
        self.settings_overlay = None;
        self.workspaces_editor_open = true;
        self.workspaces_editor_selected = 0;
    }

    pub fn close_workspaces_editor(&mut self) {
        self.workspaces_editor_open = false;
    }

    /// Move the workspace at `idx` up by one row (no-op when
    /// already at the top). Persists immediately so reordering
    /// survives a restart.
    pub fn workspaces_editor_move_up(&mut self, idx: usize) {
        if idx == 0 || idx >= self.config.workspaces.len() {
            return;
        }
        self.config.workspaces.swap(idx, idx - 1);
        self.workspaces_editor_selected = idx - 1;
        if let Err(e) = crate::config::persist_workspaces_to_global(&self.config.workspaces) {
            self.toast(format!("save workspaces: {e}"));
        }
    }

    /// Move the workspace at `idx` down by one row (no-op at the
    /// last position). Persists immediately.
    pub fn workspaces_editor_move_down(&mut self, idx: usize) {
        if idx + 1 >= self.config.workspaces.len() {
            return;
        }
        self.config.workspaces.swap(idx, idx + 1);
        self.workspaces_editor_selected = idx + 1;
        if let Err(e) = crate::config::persist_workspaces_to_global(&self.config.workspaces) {
            self.toast(format!("save workspaces: {e}"));
        }
    }

    /// #polish 2026-07-06 — reorder an extra workspace up (or down)
    /// by swapping its `.position` with the adjacent extra. Persists
    /// the swap to `[[workspaces]]` in the global config so the
    /// ordering survives a relaunch. `direction = -1` means "up",
    /// `+1` means "down". No-op when the target is the primary or
    /// there's nothing to swap with.
    pub fn move_extra_workspace(&mut self, ws_idx: usize, direction: i32) {
        if ws_idx >= self.extra_workspaces.len() {
            return;
        }
        let src_pos = self.extra_workspaces[ws_idx].position;
        // Find the extra with the "adjacent" position — nearest
        // position value in the requested direction that isn't the
        // primary's slot. Skips the primary so extras only swap
        // with extras.
        let mut best: Option<usize> = None;
        let mut best_pos: usize = 0;
        for (i, w) in self.extra_workspaces.iter().enumerate() {
            if i == ws_idx {
                continue;
            }
            let adjacent = if direction < 0 {
                w.position < src_pos && best.map(|_| w.position > best_pos).unwrap_or(true)
            } else {
                w.position > src_pos && best.map(|_| w.position < best_pos).unwrap_or(true)
            };
            if adjacent {
                best = Some(i);
                best_pos = w.position;
            }
        }
        let Some(other_idx) = best else {
            self.toast(if direction < 0 {
                "already at the top"
            } else {
                "already at the bottom"
            });
            return;
        };
        // Swap positions in the runtime rail.
        let a = self.extra_workspaces[ws_idx].position;
        let b = self.extra_workspaces[other_idx].position;
        self.extra_workspaces[ws_idx].position = b;
        self.extra_workspaces[other_idx].position = a;
        // Also swap the corresponding config entries so a relaunch
        // uses the same order. Match by canonical path.
        let (src_root, dst_root) = (
            self.extra_workspaces[ws_idx].root.clone(),
            self.extra_workspaces[other_idx].root.clone(),
        );
        let src_cfg = self.config.workspaces.iter().position(|w| {
            std::fs::canonicalize(&w.path)
                .map(|p| p == src_root)
                .unwrap_or(false)
        });
        let dst_cfg = self.config.workspaces.iter().position(|w| {
            std::fs::canonicalize(&w.path)
                .map(|p| p == dst_root)
                .unwrap_or(false)
        });
        if let (Some(s), Some(d)) = (src_cfg, dst_cfg) {
            self.config.workspaces.swap(s, d);
            if let Err(e) = crate::config::persist_workspaces_to_global(&self.config.workspaces) {
                self.toast(format!("save workspaces: {e}"));
                return;
            }
        }
        let name = self.extra_workspaces[ws_idx].name.clone();
        self.toast(format!(
            "moved {name} {}",
            if direction < 0 { "up" } else { "down" }
        ));
    }

    /// Remove the workspace at `idx`. Persists immediately.
    /// Offers undo via `pending_undo` (#20).
    pub fn workspaces_editor_delete(&mut self, idx: usize) {
        if idx >= self.config.workspaces.len() {
            return;
        }
        let removed = self.config.workspaces.remove(idx);
        let name = removed.name.clone();
        if self.workspaces_editor_selected >= self.config.workspaces.len() {
            self.workspaces_editor_selected = self.config.workspaces.len().saturating_sub(1);
        }
        match crate::config::persist_workspaces_to_global(&self.config.workspaces) {
            Ok(_) => {
                self.set_pending_undo(
                    format!("removed workspace {name}"),
                    crate::app::UndoAction::RestoreWorkspace {
                        config: removed,
                        position: idx,
                    },
                );
                self.toast(format!("removed workspace {name}"));
            }
            Err(e) => self.toast(format!("save workspaces: {e}")),
        }
    }

    /// Open the rename prompt for workspace `idx`. Commit handler
    /// (`commit_workspace_rename`) applies + persists.
    pub fn workspaces_editor_open_rename(&mut self, idx: usize) {
        let Some(w) = self.config.workspaces.get(idx) else {
            return;
        };
        let seed = w.name.clone();
        self.workspaces_edit_target_name = Some(idx);
        let prompt = crate::prompt::Prompt::seeded(
            crate::prompt::PromptKind::WorkspaceRename,
            "Workspace name (empty = revert to basename)",
            seed,
        );
        self.prompt = Some(prompt);
    }

    /// Open the path-edit prompt for workspace `idx`.
    pub fn workspaces_editor_open_path(&mut self, idx: usize) {
        let Some(w) = self.config.workspaces.get(idx) else {
            return;
        };
        let seed = w.path.to_string_lossy().into_owned();
        self.workspaces_edit_target_path = Some(idx);
        let prompt = crate::prompt::Prompt::seeded(
            crate::prompt::PromptKind::WorkspacePathEdit,
            "Path (tilde-expanded; must exist)",
            seed,
        );
        self.prompt = Some(prompt);
    }

    /// Open the group-edit prompt for workspace `idx`.
    pub fn workspaces_editor_open_group(&mut self, idx: usize) {
        let Some(w) = self.config.workspaces.get(idx) else {
            return;
        };
        let seed = w.group.clone().unwrap_or_default();
        self.workspaces_edit_target_group = Some(idx);
        let prompt = crate::prompt::Prompt::seeded(
            crate::prompt::PromptKind::WorkspaceGroupEdit,
            "Group (e.g. 'work', 'personal'; empty = ungrouped)",
            seed,
        );
        self.prompt = Some(prompt);
    }

    pub fn commit_workspace_rename(&mut self, typed: &str) {
        let Some(idx) = self.workspaces_edit_target_name.take() else {
            return;
        };
        let Some(w) = self.config.workspaces.get_mut(idx) else {
            return;
        };
        let trimmed = typed.trim();
        w.name = if trimmed.is_empty() {
            w.path
                .file_name()
                .map(|s| s.to_string_lossy().into_owned())
                .unwrap_or_else(|| w.path.to_string_lossy().into_owned())
        } else {
            trimmed.to_string()
        };
        let _ = crate::config::persist_workspaces_to_global(&self.config.workspaces);
    }

    pub fn commit_workspace_path_edit(&mut self, typed: &str) {
        let Some(idx) = self.workspaces_edit_target_path.take() else {
            return;
        };
        let Some(w) = self.config.workspaces.get_mut(idx) else {
            return;
        };
        let expanded = if let Some(rest) = typed.strip_prefix("~/")
            && let Some(home) = std::env::var_os("HOME")
        {
            std::path::PathBuf::from(home).join(rest)
        } else {
            std::path::PathBuf::from(typed.trim())
        };
        if !expanded.exists() {
            self.toast(format!("path doesn't exist: {}", expanded.display()));
            return;
        }
        w.path = expanded;
        let _ = crate::config::persist_workspaces_to_global(&self.config.workspaces);
    }

    pub fn commit_workspace_group_edit(&mut self, typed: &str) {
        let Some(idx) = self.workspaces_edit_target_group.take() else {
            return;
        };
        let Some(w) = self.config.workspaces.get_mut(idx) else {
            return;
        };
        let trimmed = typed.trim();
        w.group = if trimmed.is_empty() {
            None
        } else {
            Some(trimmed.to_string())
        };
        let _ = crate::config::persist_workspaces_to_global(&self.config.workspaces);
    }

    /// Open the kebab menu for a workspace row in the editor.
    pub fn open_workspaces_editor_kebab(&mut self, idx: usize, anchor: (u16, u16)) {
        use crate::context_menu::{ContextMenu, MenuAction, MenuItem};
        let Some(w) = self.config.workspaces.get(idx) else {
            return;
        };
        let title = Some(w.name.clone());
        // #polish 2026-07-06 — "Set / Unset default" label matches
        // whether THIS entry's path is the persisted default.
        let entry_canon = std::fs::canonicalize(&w.path).ok();
        let default_canon = self
            .config
            .default_workspace
            .as_deref()
            .and_then(|p| std::fs::canonicalize(p).ok());
        let is_default = entry_canon.is_some() && entry_canon == default_canon;
        let set_default_label = if is_default {
            "Unset as default"
        } else {
            "Set as default"
        };
        let mut items = vec![
            MenuItem::new(set_default_label, MenuAction::WorkspaceSetDefault(idx)),
            MenuItem::new("Edit name…", MenuAction::WorkspaceEditName(idx)),
            MenuItem::new("Edit path…", MenuAction::WorkspaceEditPath(idx)),
            MenuItem::new("Edit group…", MenuAction::WorkspaceEditGroup(idx)),
        ];
        if idx > 0 {
            items.push(MenuItem::new("Move up", MenuAction::WorkspaceMoveUp(idx)));
        }
        if idx + 1 < self.config.workspaces.len() {
            items.push(MenuItem::new(
                "Move down",
                MenuAction::WorkspaceMoveDown(idx),
            ));
        }
        items.push(MenuItem::new("Delete", MenuAction::WorkspaceDelete(idx)));
        self.context_menu = Some(ContextMenu::new(title, anchor, items));
    }

    /// HTTP panel `+ New request` action — spawns a blank
    /// form-style Request pane (Edit view, no source file). The
    /// pane exists only in memory until the user hits Save-As;
    /// nothing lands on disk on click. This replaces the earlier
    /// "write scratch-N.http + open editor + fire send + close
    /// editor" dance, which was littering the workspace with
    /// scratch-1.http … scratch-N.http files as the user
    /// explored.
    ///
    /// If a Request pane is already active we don't spawn a new
    /// one — matches the "reuse the current request pane"
    /// semantics of `open_curl_scratch` + `paste_curl`. The
    /// active pane just gets its fields cleared back to defaults.
    pub fn http_panel_new_request(&mut self) {
        use crate::pane::Pane;
        use crate::request_pane::{EditField, EditTab, RunState, ViewMode};
        let has_request = matches!(
            self.active.and_then(|i| self.panes.get(i)),
            Some(Pane::Request(_))
        );
        if !has_request {
            self.open_new_request_pane();
            return;
        }
        // Reset the active Request pane back to a blank template.
        let Some(cur) = self.active else { return };
        if let Some(Pane::Request(rp)) = self.panes.get_mut(cur) {
            rp.request = crate::http::Request {
                method: "GET".to_string(),
                url: String::new(),
                headers: Vec::new(),
                body: None,
                insecure: false,
            };
            rp.headers_buffer = String::new();
            rp.headers_cursor = 0;
            rp.url_cursor = 0;
            rp.body_cursor = 0;
            rp.source_path = None;
            rp.source_block_name = None;
            rp.view = ViewMode::Edit;
            rp.focus = EditField::Url;
            rp.edit_tab = EditTab::Body;
            rp.state = RunState::Failed("not sent yet · press `r` to fire".to_string());
        }
    }

    /// Scan the workspace for TODO markers and repopulate
    /// `todos_hits`. Bounded walk (skips huge files, target,
    /// node_modules, dotdirs) — under a second on typical
    /// workspaces. (#9) On first activation this runs synchronously
    /// (blocks one frame's render) but subsequent \`todos.refresh\`
    /// clicks are cheap enough on typical trees. If it starts to
    /// hurt, extract to a background thread + mpsc.
    pub fn todos_panel_refresh(&mut self) {
        let mut hits = Vec::new();
        walk_for_todos(&self.workspace, 0, &mut hits);
        hits.sort_by(|a, b| a.path.cmp(&b.path).then(a.line.cmp(&b.line)));
        self.todos_hits = hits;
        self.todos_panel_scanned_once = true;
        self.todos_panel_cursor = 0;
    }

    // vscode-user-keyboard SEV-2 fix 2026-07-09 — j/k / arrow
    // nav on the three activity panels. Cursor is an index into
    // the currently-visible filtered list; each `_cursor_down`
    // clamps to the list length so filtering doesn't leave the
    // cursor pointing past the end.
    fn todos_filtered_len(&self) -> usize {
        let f = self.todos_panel_filter.to_ascii_lowercase();
        if f.is_empty() {
            return self.todos_hits.len();
        }
        self.todos_hits
            .iter()
            .filter(|h| {
                h.tag.to_ascii_lowercase().contains(&f)
                    || h.path.to_string_lossy().to_ascii_lowercase().contains(&f)
                    || h.title.to_ascii_lowercase().contains(&f)
            })
            .count()
    }

    pub fn todos_panel_cursor_down(&mut self) {
        let n = self.todos_filtered_len();
        if n == 0 {
            self.todos_panel_cursor = 0;
            return;
        }
        self.todos_panel_cursor = (self.todos_panel_cursor + 1).min(n - 1);
    }

    pub fn todos_panel_cursor_up(&mut self) {
        self.todos_panel_cursor = self.todos_panel_cursor.saturating_sub(1);
    }

    pub fn todos_panel_activate(&mut self) {
        let f = self.todos_panel_filter.to_ascii_lowercase();
        let idx = self.todos_panel_cursor;
        let picked = self
            .todos_hits
            .iter()
            .filter(|h| {
                f.is_empty()
                    || h.tag.to_ascii_lowercase().contains(&f)
                    || h.path.to_string_lossy().to_ascii_lowercase().contains(&f)
                    || h.title.to_ascii_lowercase().contains(&f)
            })
            .nth(idx)
            .cloned();
        if let Some(hit) = picked {
            let path = hit.path.clone();
            let line = hit.line.to_string();
            self.open_path(&path);
            self.goto_line_str(&line);
        }
    }

    pub fn notes_panel_cursor_down(&mut self) {
        let n = self.notes_filtered().len();
        if n == 0 {
            self.notes_panel_cursor = 0;
            return;
        }
        self.notes_panel_cursor = (self.notes_panel_cursor + 1).min(n - 1);
    }

    pub fn notes_panel_cursor_up(&mut self) {
        self.notes_panel_cursor = self.notes_panel_cursor.saturating_sub(1);
    }

    pub fn notes_panel_activate(&mut self) {
        if let Some(path) = self
            .notes_filtered()
            .into_iter()
            .nth(self.notes_panel_cursor)
        {
            self.open_path(&path);
        }
    }

    fn notes_filtered(&self) -> Vec<std::path::PathBuf> {
        let f = self.notes_panel_filter.to_ascii_lowercase();
        if f.is_empty() {
            return self.notes_panel_files_cache.clone();
        }
        self.notes_panel_files_cache
            .iter()
            .filter(|p| {
                p.file_name()
                    .and_then(|s| s.to_str())
                    .unwrap_or("")
                    .to_ascii_lowercase()
                    .contains(&f)
            })
            .cloned()
            .collect()
    }

    pub fn sessions_panel_cursor_down(&mut self) {
        // vscode-user-keyboard 2026-07-30 KB-08 — extend cursor range
        // by 1 so ↓ reaches the `+ New session` chip at the bottom.
        // Empty state: cursor 0 = the chip (only interactive row).
        let n = self.sessions_filtered_ids().len();
        let max = n; // valid range: 0..=n, where n = "on + New session"
        self.sessions_panel_cursor = (self.sessions_panel_cursor + 1).min(max);
    }

    pub fn sessions_panel_cursor_up(&mut self) {
        self.sessions_panel_cursor = self.sessions_panel_cursor.saturating_sub(1);
    }

    pub fn sessions_panel_activate(&mut self) {
        let ids = self.sessions_filtered_ids();
        // Cursor past the last session = the `+ New session` chip.
        // Mirrors the mouse-click path (down_left.rs). Empty state
        // starts here so Enter immediately spawns.
        if self.sessions_panel_cursor >= ids.len() {
            crate::command::run("ai.claude_code_new", self);
            return;
        }
        if let Some(pid) = ids.into_iter().nth(self.sessions_panel_cursor) {
            self.reveal_pane(pid);
        }
    }

    fn sessions_filtered_ids(&self) -> Vec<usize> {
        let f = self.sessions_panel_filter.to_ascii_lowercase();
        self.panes
            .iter()
            .enumerate()
            .filter_map(|(i, p)| matches!(p, crate::pane::Pane::Pty(_)).then_some(i))
            .filter(|pid| {
                if f.is_empty() {
                    return true;
                }
                let Some(crate::pane::Pane::Pty(s)) = self.panes.get(*pid) else {
                    return false;
                };
                let cwd = s.profile.cwd.as_ref();
                let cwd_basename = cwd
                    .and_then(|p| p.file_name().and_then(|n| n.to_str()))
                    .unwrap_or_default();
                [
                    s.display_name.as_deref().unwrap_or_default(),
                    s.profile.label.as_str(),
                    cwd_basename,
                ]
                .iter()
                .any(|c| !c.is_empty() && c.to_ascii_lowercase().contains(&f))
            })
            .collect()
    }

    /// Refresh the HTTP panel caches (files + recent history +
    /// captured log). Called from the panel renderer only when the
    /// cache is empty (first activation) or via `http.refresh`.
    /// Keeps per-frame IO off the render path. (#10)
    ///
    /// Recent + captured are bounded (10 rows each in the sidebar);
    /// the reads are cheap even on large logs because
    /// `history::tail` tail-truncates and `captured::load` parses
    /// linewise — but we still gate on `http_panel_scanned_once` so
    /// they only run on activation, not every frame.
    pub fn http_panel_refresh(&mut self) {
        // Walk the workspace tree for `.http` / `.curl` / `.rest` files.
        let mut all_workspace_files = Vec::new();
        walk_for_http(&self.workspace, 0, &mut all_workspace_files);
        all_workspace_files.sort();
        // Recent + Captured share a ceiling of 20 rows — the sidebar
        // clips to 10 at display time, the home pane shows all 20.
        // Both loaders cap at load time so a multi-GB capture / a
        // never-rotated history.jsonl don't blow through allocator.
        self.http_panel_recent_cache = crate::http::history::tail(&self.workspace, 20);
        let cap_path = crate::http::proxy::captured_log_path(&self.workspace);
        self.http_panel_captured_cache = crate::http::captured::load_tail(&cap_path, 20);
        // Env cache — walks both `.mnml/env/` and `.rqst/env/`,
        // dedupes by basename, sorted alphabetically. Same shape
        // as `open_http_env_picker`.
        let mut envs = std::collections::BTreeSet::new();
        for sub in [".mnml", ".rqst"] {
            let dir = self.workspace.join(sub).join("env");
            if let Ok(rd) = std::fs::read_dir(&dir) {
                for entry in rd.flatten() {
                    let path = entry.path();
                    if path.extension().and_then(|e| e.to_str()) == Some("env")
                        && let Some(stem) = path.file_stem().and_then(|s| s.to_str())
                    {
                        envs.insert(stem.to_string());
                    }
                }
            }
        }
        self.http_panel_envs_cache = envs.into_iter().collect();
        // Chains — flat scan of `.mnml/chains/*.chain.json`. Small
        // dir; cheap.
        let chains_dir = self.workspace.join(".mnml").join("chains");
        let mut chains: Vec<std::path::PathBuf> = std::fs::read_dir(&chains_dir)
            .map(|rd| {
                rd.flatten()
                    .map(|e| e.path())
                    .filter(|p| {
                        p.file_name()
                            .and_then(|s| s.to_str())
                            .is_some_and(|n| n.ends_with(".chain.json"))
                    })
                    .collect()
            })
            .unwrap_or_default();
        chains.sort();
        self.http_panel_chains_cache = chains;
        // Mocks — collect all `*.mock.json` picked up by the http
        // file walk. R7 api-workflow SEV-2 2026-08-09: was reading
        // `self.http_panel_files_cache` — which is only assigned at
        // line 1242 below — so the first refresh always saw the
        // PRIOR call's cache. Now reads from `all_workspace_files`
        // (fresh, sorted at line 1007). Mocks live as siblings of
        // the source files they mock, so the walk covers them.
        let mut mocks: Vec<std::path::PathBuf> = all_workspace_files
            .iter()
            .filter(|p| {
                p.file_name()
                    .and_then(|s| s.to_str())
                    .is_some_and(|n| n.ends_with(".mock.json"))
            })
            .cloned()
            .collect();
        // As of R6 api-workflow SEV-2 (2026-08-09) `walk_for_http`
        // ALSO collects `*.mock.json` sidecars, which is where
        // `sibling_path_for_block` actually writes them. This
        // `.rqst/mocks` / `.mnml/mocks` shallow-walk stays as a
        // fallback for mocks manually placed into those legacy dirs.
        for sub in [".rqst", ".mnml"] {
            let dir = self.workspace.join(sub).join("mocks");
            if let Ok(rd) = std::fs::read_dir(&dir) {
                for entry in rd.flatten() {
                    let p = entry.path();
                    if p.file_name()
                        .and_then(|s| s.to_str())
                        .is_some_and(|n| n.ends_with(".mock.json"))
                    {
                        mocks.push(p);
                    }
                }
            }
        }
        mocks.sort();
        mocks.dedup();
        self.http_panel_mocks_cache = mocks;
        // #polish 2026-07-06 — universal collection discovery. A
        // "collection" is either:
        //   (a) a subdir of `.mnml/collections/*` (Hidden — per-user)
        //   (b) any workspace folder with ≥2 request files (InTree
        //       — Bruno-flavor, git-tracked)
        // Files not inside any collection root land in the FILES
        // section as stragglers.
        use crate::app::HttpCollectionKind;
        let mut roots: Vec<(std::path::PathBuf, HttpCollectionKind)> = Vec::new();

        // (a) Hidden collections — each direct subdir of
        // `.mnml/collections/`. Empty subdirs still count as a
        // collection (user just created it, no requests yet).
        let hidden_root = self.workspace.join(".mnml").join("collections");
        if let Ok(rd) = std::fs::read_dir(&hidden_root) {
            for entry in rd.flatten() {
                let p = entry.path();
                if p.is_dir() {
                    roots.push((p, HttpCollectionKind::Hidden));
                }
            }
        }

        // #polish 2026-07-06 — `.rqst/` support (legacy .rqst-style
        // request stores). Classic layout:
        //   .rqst/requests/<name>/*.curl — each <name> is a collection
        //   .rqst/snippets/*.curl        — snippets is a collection
        //   .rqst/lookups/*.curl         — lookups is a collection
        // Skip the structural dirs (env / mocks / captured / ipc /
        // config) that mnml handles separately.
        let rqst_root = self.workspace.join(".rqst");
        if rqst_root.exists() {
            let has_request_file = |dir: &std::path::Path| -> bool {
                std::fs::read_dir(dir)
                    .map(|rd| {
                        rd.flatten().any(|e| {
                            let p = e.path();
                            p.is_file()
                                && matches!(
                                    p.extension().and_then(|s| s.to_str()),
                                    Some("http") | Some("curl") | Some("rest")
                                )
                        })
                    })
                    .unwrap_or(false)
            };
            // Same predicate as `has_request_file` but recurses so a
            // dir that contains request files ONLY in deeper subfolders
            // still qualifies as a collection root. Bug 2026-07-08 —
            // integrations-api/Admin/*.curl was invisible in the HTTP
            // panel because integrations-api/ had no direct files and
            // the old scanner only looked one level down.
            fn has_request_file_recursive(dir: &std::path::Path, depth: u32) -> bool {
                if depth > 6 {
                    return false;
                }
                let Ok(rd) = std::fs::read_dir(dir) else {
                    return false;
                };
                for entry in rd.flatten() {
                    let p = entry.path();
                    if p.is_file() {
                        if matches!(
                            p.extension().and_then(|s| s.to_str()),
                            Some("http") | Some("curl") | Some("rest")
                        ) {
                            return true;
                        }
                    } else if p.is_dir() && has_request_file_recursive(&p, depth + 1) {
                        return true;
                    }
                }
                false
            }
            let skip = ["env", "mocks", "captured", "ipc", "config"];
            if let Ok(rd) = std::fs::read_dir(&rqst_root) {
                for entry in rd.flatten() {
                    let p = entry.path();
                    if !p.is_dir() {
                        continue;
                    }
                    let name = p.file_name().and_then(|s| s.to_str()).unwrap_or("");
                    if skip.iter().any(|s| s == &name) {
                        continue;
                    }
                    // Two shapes: either the dir itself has direct
                    // request files (snippets, lookups), or its
                    // children do (requests/<sub>/). Sub-level is
                    // checked recursively so a `requests/<api>/` whose
                    // .curl files live in `requests/<api>/<endpoint>/`
                    // still surfaces as its own collection.
                    if has_request_file(&p) {
                        roots.push((p, HttpCollectionKind::Hidden));
                    } else if let Ok(sub_rd) = std::fs::read_dir(&p) {
                        for sub in sub_rd.flatten() {
                            let sp = sub.path();
                            if sp.is_dir() && has_request_file_recursive(&sp, 0) {
                                roots.push((sp, HttpCollectionKind::Hidden));
                            }
                        }
                    }
                }
            }
        }

        // (b) In-tree collections — group workspace files by parent
        // dir; parents with ≥2 request files become collection roots.
        // Skip the workspace root itself (that would eat every loose
        // .http; the "workspace as one big collection" case isn't
        // what the user wants). R7 api-workflow SEV-3 2026-08-09:
        // `.mock.json` sidecars are NOT request files and shouldn't
        // count toward the ≥2 threshold — a folder with 1 `.curl` +
        // 1 `foo.curl.mock.json` isn't a collection.
        let is_mock_sidecar = |p: &std::path::Path| -> bool {
            p.file_name()
                .and_then(|s| s.to_str())
                .is_some_and(|n| n.ends_with(".mock.json"))
        };
        let mut by_parent: std::collections::HashMap<std::path::PathBuf, usize> =
            std::collections::HashMap::new();
        for f in &all_workspace_files {
            if is_mock_sidecar(f) {
                continue;
            }
            if let Some(parent) = f.parent()
                && parent != self.workspace
            {
                *by_parent.entry(parent.to_path_buf()).or_insert(0) += 1;
            }
        }
        for (parent, n) in by_parent {
            if n >= 2 {
                roots.push((parent, HttpCollectionKind::InTree));
            }
        }
        roots.sort_by(|a, b| a.0.cmp(&b.0));
        self.http_panel_collection_roots = roots;

        // Partition all files into (in a collection) vs (straggler).
        // A file belongs to a collection iff any collection root is
        // its ancestor.
        let is_in_collection = |p: &std::path::Path| -> bool {
            self.http_panel_collection_roots
                .iter()
                .any(|(root, _)| p.starts_with(root))
        };
        let mut in_collection: Vec<std::path::PathBuf> = Vec::new();
        let mut stragglers: Vec<std::path::PathBuf> = Vec::new();
        for f in all_workspace_files {
            // R7 api-workflow SEV-3 2026-08-09: exclude .mock.json
            // sidecars from both FILES and COLLECTIONS — they live
            // in the MOCKS section only. `all_workspace_files`
            // includes them (needed for the mocks-collect above)
            // but they shouldn't render as clickable request files.
            if is_mock_sidecar(&f) {
                continue;
            }
            if is_in_collection(&f) {
                in_collection.push(f);
            } else {
                stragglers.push(f);
            }
        }
        // Also pull hidden-collection files (they weren't in the
        // workspace walk — walk_for_http skips hidden dirs). Two
        // sources now: .mnml/collections/*/ and .rqst/**/.
        for (root, kind) in &self.http_panel_collection_roots {
            if *kind == HttpCollectionKind::Hidden {
                let mut hidden_files = Vec::new();
                walk_collections(root, &mut hidden_files);
                in_collection.extend(hidden_files);
            }
        }
        in_collection.sort();
        in_collection.dedup();
        self.http_panel_files_cache = stragglers;
        self.http_panel_collections_cache = in_collection;
        self.http_panel_scanned_once = true;
    }

    /// Truncate the captured-traffic log for this workspace. Fires
    /// from the CAPTURED section header's `✕` chip. Silent no-op
    /// when the file doesn't exist (nothing to clear).
    /// Snapshots the file into `pending_undo` so the user can bring
    /// the traffic back (#20).
    pub fn http_panel_clear_captured(&mut self) {
        let cap_path = crate::http::proxy::captured_log_path(&self.workspace);
        if cap_path.exists() {
            let snapshot = std::fs::read(&cap_path).unwrap_or_default();
            match std::fs::write(&cap_path, "") {
                Ok(_) => {
                    if !snapshot.is_empty() {
                        self.set_pending_undo(
                            "cleared captured traffic".to_string(),
                            crate::app::UndoAction::RestoreCapturedFile { bytes: snapshot },
                        );
                    }
                    self.toast("captured traffic cleared");
                }
                Err(e) => self.toast(format!("clear captured: {e}")),
            }
        }
        self.http_panel_captured_cache.clear();
    }

    /// Truncate the workspace-local request history (`.rqst/history.jsonl`).
    /// Fires from the RECENT section header's `✕` chip. Silent no-op
    /// when the file doesn't exist. Does NOT touch the global mirror
    /// (`~/.local/state/mnml/history.jsonl`) — that's cross-workspace
    /// and clearing it would surprise other workspaces sharing the
    /// mirror. To wipe the global one too, add a modifier + prompt.
    /// Snapshots the file into `pending_undo` (#20).
    pub fn http_panel_clear_recent(&mut self) {
        let hist_path = self.workspace.join(".rqst").join("history.jsonl");
        if hist_path.exists() {
            let snapshot = std::fs::read(&hist_path).unwrap_or_default();
            match std::fs::write(&hist_path, "") {
                Ok(_) => {
                    if !snapshot.is_empty() {
                        self.set_pending_undo(
                            "cleared request history".to_string(),
                            crate::app::UndoAction::RestoreHistoryFile { bytes: snapshot },
                        );
                    }
                    self.toast("request history cleared");
                }
                Err(e) => self.toast(format!("clear recent: {e}")),
            }
        }
        self.http_panel_recent_cache.clear();
    }

    /// Refresh the Findings panel file cache. Walks
    /// `.mnml/findings/` recursively (depth cap 4) picking up every
    /// `*.md` — tester agents commonly nest reports under
    /// per-round subdirectories (`2026-07-21-16-09-design-round-1/*.md`),
    /// so a flat scan misses everything but the top-level `README.md`.
    /// Sorted by mtime desc so the freshest finding is first. Task #908.
    pub fn findings_panel_refresh(&mut self) {
        let dir = crate::ui::findings_panel::findings_dir(&self.workspace);
        let mut out: Vec<std::path::PathBuf> = Vec::new();
        walk_findings(&dir, 0, &mut out);
        out.sort_by_key(|p| {
            std::fs::metadata(p)
                .and_then(|m| m.modified())
                .ok()
                .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
                .map(|d| std::cmp::Reverse(d.as_secs()))
                .unwrap_or(std::cmp::Reverse(0))
        });
        self.findings_panel_files_cache = out;
        self.findings_panel_scanned_once = true;
    }

    /// Refresh the Notes panel file cache. Same lazy pattern as the
    /// HTTP one. (#8)
    pub fn notes_panel_refresh(&mut self) {
        let dir = crate::ui::notes_panel::notes_dir(&self.workspace);
        let mut out: Vec<std::path::PathBuf> = match std::fs::read_dir(&dir) {
            Ok(rd) => rd
                .flatten()
                .map(|e| e.path())
                .filter(|p| p.extension().and_then(|e| e.to_str()) == Some("md") && p.is_file())
                .collect(),
            Err(_) => Vec::new(),
        };
        // Sort by modified time descending — most-recently-worked-on first.
        out.sort_by_key(|p| {
            std::fs::metadata(p)
                .and_then(|m| m.modified())
                .ok()
                .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
                .map(|d| std::cmp::Reverse(d.as_secs()))
                .unwrap_or(std::cmp::Reverse(0))
        });
        self.notes_panel_files_cache = out;
        self.notes_panel_scanned_once = true;
    }

    /// Notes panel `+ New note` action — creates a numbered markdown
    /// file under `<workspace>/.mnml/notes/` and opens it. Directory
    /// is created on demand. (#8)
    pub fn notes_panel_new_note(&mut self) {
        let dir = crate::ui::notes_panel::notes_dir(&self.workspace);
        if let Err(e) = std::fs::create_dir_all(&dir) {
            self.toast(format!("notes: create dir failed: {e}"));
            return;
        }
        let mut i = 1;
        let mut path = dir.join("note-1.md");
        while path.exists() {
            i += 1;
            path = dir.join(format!("note-{i}.md"));
        }
        let stub = format!("# Note {i}\n\n");
        if let Err(e) = std::fs::write(&path, stub) {
            self.toast(format!("notes: create failed: {e}"));
            return;
        }
        self.notes_panel_refresh();
        self.open_path(&path);
    }
}

/// Task #908 — recursive `.md` collector for the Findings panel.
/// Depth-limited (max 4) as an upper bound on nesting; symlinks are
/// safe by construction — `DirEntry::file_type` is lstat-based, so
/// symlinks report `is_symlink()`, not `is_dir()`, and the recursion
/// branch never follows them (cycles unreachable, not just
/// depth-bounded). Also caps output at 500 rows — a runaway workspace
/// with thousands of findings shouldn't produce a Vec that dwarfs the
/// panel viewport. Skips any `README.md` at the root so the shipped
/// index file doesn't clutter the row list. Matches the size-cap
/// idiom in the integration `walk_for_http` / `walk_for_todos` walkers.
fn walk_findings(dir: &std::path::Path, depth: usize, out: &mut Vec<std::path::PathBuf>) {
    if depth > 4 || out.len() > 500 {
        return;
    }
    let Ok(entries) = std::fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        if out.len() > 500 {
            return;
        }
        let path = entry.path();
        let Ok(ft) = entry.file_type() else { continue };
        if ft.is_dir() {
            walk_findings(&path, depth + 1, out);
            continue;
        }
        if path.extension().and_then(|e| e.to_str()) != Some("md") {
            continue;
        }
        // Skip README.md at the root — it's the shipped index/help
        // page, not a finding. Nested READMEs (a per-round index)
        // still surface because their parent dir names them
        // meaningfully.
        if depth == 0
            && path
                .file_name()
                .and_then(|n| n.to_str())
                .is_some_and(|n| n.eq_ignore_ascii_case("README.md"))
        {
            continue;
        }
        out.push(path);
    }
}

/// #22 — walker for `.mnml/collections/`. Recurses through
/// subdirs (a collection is a folder) and collects `.http` /
/// `.curl` / `.rest` request files. Unlike `walk_for_http`,
/// this one doesn't skip hidden dirs — the whole tree lives
/// under `.mnml/`, so hidden-dot filtering would trip on the
/// parent path itself.
fn walk_collections(dir: &std::path::Path, out: &mut Vec<std::path::PathBuf>) {
    let Ok(entries) = std::fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_dir() {
            walk_collections(&path, out);
        } else if let Some(ext) = path.extension().and_then(|e| e.to_str())
            && (ext == "http" || ext == "curl" || ext == "rest")
        {
            out.push(path);
        }
    }
}

fn walk_for_http(dir: &std::path::Path, depth: u32, out: &mut Vec<std::path::PathBuf>) {
    if depth > 4 || out.len() > 200 {
        return;
    }
    let Ok(entries) = std::fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        let name = entry.file_name();
        let name_str = name.to_string_lossy();
        if name_str.starts_with('.') || name_str == "target" || name_str == "node_modules" {
            continue;
        }
        if path.is_dir() {
            walk_for_http(&path, depth + 1, out);
        } else if let Some(ext) = path.extension().and_then(|e| e.to_str())
            && (ext == "http" || ext == "curl" || ext == "rest")
        {
            out.push(path);
        } else if name_str.ends_with(".mock.json") {
            // R6 api-workflow SEV-2 2026-08-09 — sidecar mocks live
            // next to their source (e.g. `get.curl.mock.json` in the
            // workspace root, not under `.rqst/mocks/`). Without this
            // branch, `App::http_panel_refresh` filters an already-
            // empty `http_panel_files_cache` for `.mock.json` and
            // always renders MOCKS `(0)` — the actual save path
            // (`sibling_path_for_block`) and the panel's discovery
            // never intersect. `path.extension()` on `foo.curl.mock.json`
            // returns `"json"`, not `"mock.json"`, so match on the
            // full-name suffix instead.
            out.push(path);
        }
    }
}

fn walk_for_todos(
    dir: &std::path::Path,
    depth: u32,
    out: &mut Vec<crate::ui::todos_panel::TodoHit>,
) {
    if depth > 6 || out.len() > 1000 {
        return;
    }
    let Ok(entries) = std::fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        let path = entry.path();
        let name = entry.file_name();
        let name_str = name.to_string_lossy();
        if name_str.starts_with('.')
            || name_str == "target"
            || name_str == "node_modules"
            || name_str == "dist"
            || name_str == "build"
        {
            continue;
        }
        if path.is_dir() {
            walk_for_todos(&path, depth + 1, out);
        } else if let Some(ext) = path.extension().and_then(|e| e.to_str())
            && matches!(
                ext,
                "rs" | "ts"
                    | "tsx"
                    | "js"
                    | "jsx"
                    | "py"
                    | "go"
                    | "java"
                    | "kt"
                    | "swift"
                    | "cs"
                    | "cpp"
                    | "c"
                    | "h"
                    | "hpp"
                    | "rb"
                    | "sh"
                    | "yml"
                    | "yaml"
                    | "toml"
                    | "md"
            )
        {
            out.extend(crate::ui::todos_panel::scan_file(&path));
        }
    }
}

#[cfg(test)]
mod notes_new_note_tests {
    #[test]
    fn new_note_creates_file_and_opens_preview_pane() {
        let d = tempfile::tempdir().unwrap();
        let cfg = crate::config::Config::default();
        let mut app = crate::app::App::new(d.path().to_path_buf(), cfg).unwrap();
        let before_panes = app.panes.len();
        app.notes_panel_new_note();
        let note_path = d.path().join(".mnml").join("notes").join("note-1.md");
        assert!(note_path.exists(), "note file should be created");
        assert!(
            app.panes.len() > before_panes,
            "opening the note should push a new pane; before={} after={}",
            before_panes,
            app.panes.len()
        );
        // Match preview by file name — canonicalization on macOS may
        // give `/private/var/…` vs `/var/…` differences.
        let preview_name = app.panes.iter().find_map(|p| match p {
            crate::pane::Pane::MdPreview(mp) => mp
                .path
                .file_name()
                .and_then(|n| n.to_str())
                .map(str::to_string),
            _ => None,
        });
        assert_eq!(preview_name.as_deref(), Some("note-1.md"));
    }

    #[test]
    fn new_note_numbers_sequentially_when_pressed_repeatedly() {
        let d = tempfile::tempdir().unwrap();
        let cfg = crate::config::Config::default();
        let mut app = crate::app::App::new(d.path().to_path_buf(), cfg).unwrap();
        app.notes_panel_new_note();
        app.notes_panel_new_note();
        app.notes_panel_new_note();
        let dir = d.path().join(".mnml").join("notes");
        assert!(dir.join("note-1.md").exists());
        assert!(dir.join("note-2.md").exists());
        assert!(dir.join("note-3.md").exists());
    }
}

#[cfg(test)]
mod findings_walk_tests {
    use super::walk_findings;
    use std::fs;

    #[test]
    fn walks_nested_subdirs_and_skips_root_readme() {
        let d = tempfile::tempdir().unwrap();
        let root = d.path();
        fs::write(root.join("README.md"), "# index").unwrap();
        fs::write(root.join("top.md"), "# top").unwrap();
        let round = root.join("2026-08-14-round-1");
        fs::create_dir(&round).unwrap();
        fs::write(round.join("finding-a.md"), "a").unwrap();
        fs::write(round.join("finding-b.md"), "b").unwrap();
        // Non-md siblings are ignored.
        fs::write(round.join("notes.txt"), "x").unwrap();
        let mut out = Vec::new();
        walk_findings(root, 0, &mut out);
        // 3 md files: top.md + 2 in round. README.md skipped.
        assert_eq!(out.len(), 3, "walked: {out:?}");
        assert!(out.iter().any(|p| p.ends_with("top.md")));
        assert!(out.iter().any(|p| p.ends_with("finding-a.md")));
        assert!(out.iter().any(|p| p.ends_with("finding-b.md")));
        assert!(!out.iter().any(|p| p.ends_with("README.md")));
    }

    #[test]
    fn depth_cap_prevents_runaway_walk() {
        let d = tempfile::tempdir().unwrap();
        let mut path = d.path().to_path_buf();
        // Create 7 nested dirs, each with a .md file. Depth cap is 4,
        // so files past depth 4 shouldn't show up.
        for i in 0..7 {
            path.push(format!("d{i}"));
            fs::create_dir(&path).unwrap();
            fs::write(path.join("f.md"), "x").unwrap();
        }
        let mut out = Vec::new();
        walk_findings(d.path(), 0, &mut out);
        // Files at depths 1..=4 land (4 files); depths 5,6,7 don't.
        assert_eq!(out.len(), 4, "walked: {out:?}");
    }

    #[test]
    fn missing_dir_returns_empty() {
        let d = tempfile::tempdir().unwrap();
        let mut out = Vec::new();
        walk_findings(&d.path().join("does-not-exist"), 0, &mut out);
        assert!(out.is_empty());
    }
}