voc 0.1.1

Voc Agent — the Rust terminal cockpit for Vibecrafted
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
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
use crate::config::{AppConfig, path_display};
use crate::launch::{
    LaunchCommand, LaunchKind, LaunchRequest, LaunchRuntime, build_launch_command,
};
use crate::mission_control::{self, ActionQueueItem, ActionQueueKind, MissionControlState};
use crate::polarize::{PolarizeBand, PolarizeIntent};
use crate::skills_catalog::{self, SkillAgent, SkillPayload, SkillPayloadKind};
use crate::state::{ControlPlaneState, RenderedRun, RunKind, render_runs};
use std::collections::BTreeMap;
use std::ffi::OsString;
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AppTab {
    Monitor,
    Dispatch,
    Controls,
    MissionControl,
}

impl AppTab {
    pub const TITLES: [&'static str; 4] = ["Monitor", "Dispatch", "Controls", "Mission Control"];

    pub fn label(self) -> &'static str {
        match self {
            Self::Monitor => "Monitor",
            Self::Dispatch => "Dispatch",
            Self::Controls => "Controls",
            Self::MissionControl => "Mission Control",
        }
    }

    pub fn from_index(index: usize) -> Self {
        match index % Self::TITLES.len() {
            0 => Self::Monitor,
            1 => Self::Dispatch,
            2 => Self::Controls,
            _ => Self::MissionControl,
        }
    }

    pub fn index(self) -> usize {
        match self {
            Self::Monitor => 0,
            Self::Dispatch => 1,
            Self::Controls => 2,
            Self::MissionControl => 3,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DispatchFocus {
    Kind,
    Agent,
    Runtime,
    Prompt,
}

impl DispatchFocus {
    pub const COUNT: usize = 4;

    pub fn from_index(index: usize) -> Self {
        match index % Self::COUNT {
            0 => Self::Kind,
            1 => Self::Agent,
            2 => Self::Runtime,
            _ => Self::Prompt,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LaunchFocus {
    Browse,
    EditPrompt,
    Help,
    Search,
    Error,
    Artifact,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QueueScope {
    Live,
    History,
    All,
}

impl QueueScope {
    pub fn label(self) -> &'static str {
        match self {
            QueueScope::Live => "live",
            QueueScope::History => "history",
            QueueScope::All => "all",
        }
    }

    pub fn title(self) -> &'static str {
        match self {
            QueueScope::Live => "Live queue",
            QueueScope::History => "History",
            QueueScope::All => "All runs",
        }
    }

    pub fn next(self) -> Self {
        match self {
            QueueScope::Live => QueueScope::History,
            QueueScope::History => QueueScope::All,
            QueueScope::All => QueueScope::Live,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DeepAction {
    AttachSession(String),
    ResumeSession {
        agent: String,
        session: String,
    },
    OpenReport(PathBuf),
    OpenTranscript(PathBuf),
    OpenRoot(PathBuf),
    /// Run `rmcp-mux health --service <name>` against a known MCP daemon.
    /// Available when at least one rmcp-mux status snapshot is cached on
    /// the App; not tied to the selected run, so the operator can health-
    /// check the supervisor even when no agent run is selected.
    MuxHealth {
        service: String,
    },
    MuxRestart(String),
    MuxVerifyClient(rmcp_mux::ipc::ClientKind),
    MuxFixClientDrift(rmcp_mux::ipc::ClientKind),
    /// Consumer-side rendering for a polarize prism emitted by Vibecrafted.
    /// The operator does not score or originate the band; it only surfaces
    /// the runner's prism payload and suggested action path.
    PolarizeIntent {
        band: PolarizeBand,
        score: u8,
        run_id: String,
        prism_path: PathBuf,
    },
    /// Launch a first-class Vibecrafted skill entrypoint.
    SkillLaunch {
        skill: String,
        agent: SkillAgent,
        payload: SkillPayload,
    },
}

impl DeepAction {
    pub fn label(&self) -> String {
        match self {
            DeepAction::AttachSession(session) => {
                format!("Attach operator session: vibecrafted dashboard attach {session}")
            }
            DeepAction::ResumeSession { agent, session } => {
                format!("Resume agent session: vibecrafted resume {agent} --session {session}")
            }
            DeepAction::OpenReport(path) => {
                format!("Open latest report: {}", path.to_string_lossy())
            }
            DeepAction::OpenTranscript(path) => {
                format!("Open latest transcript: {}", path.to_string_lossy())
            }
            DeepAction::OpenRoot(path) => format!("Open run root: {}", path.to_string_lossy()),
            DeepAction::MuxHealth { service } => {
                format!("Health-check MCP daemon: rmcp-mux health --service {service}")
            }
            DeepAction::MuxRestart(service) => {
                format!("Restart MCP daemon: rmcp-mux restart --service {service}")
            }
            DeepAction::MuxVerifyClient(_) => "Verify client routing through mux".to_string(),
            DeepAction::MuxFixClientDrift(_) => {
                "Fix client drift: rmcp-mux wizard --strategy auto-rewire".to_string()
            }
            DeepAction::PolarizeIntent {
                band,
                score,
                run_id,
                prism_path,
            } => format!(
                "Inspect polarize intent: {} score {} run {} -> {}",
                band.label(),
                score,
                run_id,
                prism_path.to_string_lossy()
            ),
            DeepAction::SkillLaunch {
                skill,
                agent,
                payload,
            } => {
                let payload_label = match payload {
                    SkillPayload::Prompt(prompt) if !prompt.trim().is_empty() => "prompt",
                    SkillPayload::File(_) => "file",
                    SkillPayload::Prompt(_) | SkillPayload::None => "no payload",
                };
                format!(
                    "Launch skill: vibecrafted {} {} ({payload_label})",
                    skill.trim_start_matches("vc-"),
                    agent.label()
                )
            }
        }
    }
}

#[derive(Debug)]
pub struct App {
    pub config: AppConfig,
    pub state: ControlPlaneState,
    pub runs: Vec<RenderedRun>,
    pub selected: usize,
    pub active_tab: usize,
    pub launch_kind: LaunchKind,
    pub launch_agent: usize,
    pub launch_prompt: String,
    pub launch_runtime: LaunchRuntime,
    pub dispatch_selected: usize,
    pub focus: LaunchFocus,
    pub status_line: String,
    pub launch_history: Vec<String>,
    pub deep_selected: usize,
    pub queue_scope: QueueScope,
    pub search_query: String,
    pub error_title: String,
    pub error_lines: Vec<String>,
    pub artifact_title: String,
    pub artifact_lines: Vec<String>,
    /// Cached rmcp-mux supervisor snapshots (from
    /// `crate::mux::current_summaries`). Refreshed on every `App::refresh`
    /// so the Monitor tab can render MCP daemon health without doing IO
    /// inside the draw path.
    pub mux_summaries: Vec<crate::mux::MuxSummary>,
    pub mux_subscriber: Option<crate::mux::MuxSubscriber>,
    /// Cached polarize prism intents discovered under
    /// `$VIBECRAFTED_HOME/artifacts/**/polarize/<run_id>/prism.json`.
    /// Refreshed with the run board so draw code remains pure rendering.
    pub polarize_intents: Vec<PolarizeIntent>,
    /// Cached Mission Control view derived from
    /// `~/.vibecrafted/artifacts/**/*.meta.json` plus live control-plane
    /// runs. Built on every `refresh` so the dashboard tab can render
    /// without doing IO inside the draw path. The artifact root is
    /// resolved once via `mission_control::default_artifact_root()`.
    pub mission_control: MissionControlState,
    /// Selected panel index inside the Mission Control tab (0..7).
    pub mission_focus: usize,
    /// Resolved artifact root used by the mission control aggregator;
    /// kept on the App so tests can swap it explicitly.
    pub mission_artifact_root: PathBuf,
}

impl App {
    pub fn new(config: AppConfig) -> anyhow::Result<Self> {
        let state = ControlPlaneState::load(&config.state_root)
            .unwrap_or_else(|_| ControlPlaneState::empty(&config.state_root));
        let runs = render_runs(&state);
        let launch_runtime = config.launch_runtime;
        let mission_artifact_root = mission_control::default_artifact_root();
        let mut app = Self {
            config,
            state,
            runs,
            selected: 0,
            active_tab: AppTab::Monitor.index(),
            launch_kind: LaunchKind::Workflow,
            launch_agent: 0,
            launch_prompt: default_prompt(LaunchKind::Workflow),
            launch_runtime,
            dispatch_selected: DispatchFocus::Kind as usize,
            focus: LaunchFocus::Browse,
            status_line: String::new(),
            launch_history: Vec::new(),
            deep_selected: 0,
            queue_scope: QueueScope::Live,
            search_query: String::new(),
            error_title: String::new(),
            error_lines: Vec::new(),
            artifact_title: String::new(),
            artifact_lines: Vec::new(),
            mux_summaries: Vec::new(),
            mux_subscriber: None,
            polarize_intents: Vec::new(),
            mission_control: MissionControlState::default(),
            mission_focus: 0,
            mission_artifact_root,
        };
        apply_run_filters(&mut app.runs, app.queue_scope, &app.search_query);
        app.sync_selection();
        app.refresh_mux();
        app.refresh_polarize();
        app.refresh_mission_control();
        let path = rmcp_mux::ipc::server::socket_path();
        let summaries = std::sync::Arc::new(std::sync::RwLock::new(app.mux_summaries.clone()));
        app.mux_subscriber = Some(crate::mux::MuxSubscriber::start(path, summaries));
        Ok(app)
    }

    pub fn refresh(&mut self) {
        let state = ControlPlaneState::load(&self.config.state_root)
            .unwrap_or_else(|_| ControlPlaneState::empty(&self.config.state_root));
        self.state = state;
        let mut runs = render_runs(&self.state);
        apply_run_filters(&mut runs, self.queue_scope, &self.search_query);
        self.runs = runs;
        self.sync_selection();
        self.refresh_mux();
        self.refresh_polarize();
        self.refresh_mission_control();
    }

    /// Refresh the cached Mission Control view. Cheap on small artifact
    /// trees (a few directories of `*.meta.json`); bounded on huge
    /// trees by `mission_control::META_SCAN_CAP`. Called on every
    /// `refresh()` so the dashboard surfaces stay live without doing
    /// disk IO inside the draw path.
    pub fn refresh_mission_control(&mut self) {
        self.mission_control = MissionControlState::build_with_intents(
            &self.state,
            &self.mission_artifact_root,
            &self.polarize_intents,
        );
        if self.mission_focus >= mission_panel_count() {
            self.mission_focus = mission_panel_count().saturating_sub(1);
        }
    }

    pub fn move_mission_focus(&mut self, delta: isize) {
        let count = mission_panel_count() as isize;
        if count == 0 {
            self.mission_focus = 0;
            return;
        }
        let mut index = self.mission_focus as isize + delta;
        while index < 0 {
            index += count;
        }
        self.mission_focus = (index % count) as usize;
    }

    /// Refresh cached rmcp-mux status snapshots from the discovered
    /// status files. Cheap (a few small JSON reads) so it is safe to call
    /// on the same cadence as the run-state refresh.
    pub fn refresh_mux(&mut self) {
        self.mux_summaries = crate::mux::current_summaries();
    }

    pub fn refresh_polarize(&mut self) {
        self.polarize_intents = crate::polarize::current_intents(&self.config.launch_root);
    }

    pub fn handle_ipc_event(&mut self, _event: rmcp_mux::ipc::IpcEvent) {
        // The subscriber pushes events. We can either do a full IO refresh,
        // or apply the diff. The safest and most robust path is just calling refresh_mux().
        self.refresh_mux();
    }

    /// Lines for the Monitor tab "MCP daemons" panel. Returns an empty
    /// vec when no mux services are known (operator may simply not be
    /// running rmcp-mux), otherwise one summary line per service plus a
    /// header.
    pub fn mux_status_lines(&self) -> Vec<String> {
        if self.mux_summaries.is_empty() {
            return Vec::new();
        }
        let total = self.mux_summaries.len();
        let unhealthy = self
            .mux_summaries
            .iter()
            .filter(|summary| !summary.is_healthy())
            .count();
        let mut lines = Vec::with_capacity(total + 1);
        if unhealthy == 0 {
            lines.push(format!("MCP daemons ({total} healthy):"));
        } else {
            lines.push(format!("MCP daemons ({unhealthy}/{total} need attention):"));
        }
        for summary in &self.mux_summaries {
            let marker = if summary.is_healthy() {
                "  • "
            } else {
                "  ! "
            };
            lines.push(format!("{marker}{}", summary.summary_line()));
        }
        lines
    }

    pub fn polarize_status_lines(&self) -> Vec<String> {
        if self.polarize_intents.is_empty() {
            return Vec::new();
        }
        let doctrine = self
            .polarize_intents
            .iter()
            .filter(|intent| intent.band == PolarizeBand::Doctrine)
            .count();
        let mut lines = Vec::with_capacity(self.polarize_intents.len() + 1);
        if doctrine == 0 {
            lines.push(format!(
                "Polarize intents ({}):",
                self.polarize_intents.len()
            ));
        } else {
            lines.push(format!("Polarize intents ({} doctrine):", doctrine));
        }
        lines.extend(self.polarize_intents.iter().map(|intent| {
            format!(
                "  {} {}",
                polarize_marker(intent.band),
                intent.summary_line()
            )
        }));
        lines
    }

    pub fn toggle_filter(&mut self) {
        self.queue_scope = self.queue_scope.next();
        self.refresh();
        self.append_status(format!(
            "queue scope: {} ({} runs visible)",
            self.queue_scope.label(),
            self.runs.len()
        ));
    }

    pub fn set_search_query<S: Into<String>>(&mut self, query: S) {
        self.search_query = query.into();
        self.refresh();
    }

    pub fn clear_search(&mut self) {
        if !self.search_query.is_empty() {
            self.search_query.clear();
            self.refresh();
            self.append_status("search cleared");
        }
    }

    pub fn archive_selected_run(&mut self) -> anyhow::Result<()> {
        let Some(run_id) = self.selected_run().map(|run| run.snapshot.run_id.clone()) else {
            self.append_status("No run selected to archive.");
            return Ok(());
        };
        let archive_dir = self.config.state_root.join("runs/.archived");
        fs::create_dir_all(&archive_dir)?;
        let marker_path = archive_dir.join(format!("{}.json", safe_marker_name(&run_id)));
        let marker = serde_json::json!({
            "run_id": run_id,
            "archived_by": "vc-tui",
            "archived_at": chrono::Utc::now().to_rfc3339(),
        });
        fs::write(&marker_path, serde_json::to_vec_pretty(&marker)?)?;
        self.refresh();
        self.append_status(format!(
            "archived run from operator view: {}",
            marker
                .get("run_id")
                .and_then(|value| value.as_str())
                .unwrap_or("unknown")
        ));
        Ok(())
    }

    pub fn selected_run(&self) -> Option<&RenderedRun> {
        self.runs.get(self.selected)
    }

    pub fn active_tab(&self) -> AppTab {
        AppTab::from_index(self.active_tab)
    }

    pub fn next_tab(&mut self) {
        self.active_tab = (self.active_tab + 1) % AppTab::TITLES.len();
        self.focus = LaunchFocus::Browse;
    }

    pub fn previous_tab(&mut self) {
        self.active_tab = if self.active_tab == 0 {
            AppTab::TITLES.len() - 1
        } else {
            self.active_tab - 1
        };
        self.focus = LaunchFocus::Browse;
    }

    pub fn set_active_tab(&mut self, tab: AppTab) {
        self.active_tab = tab.index();
        self.focus = LaunchFocus::Browse;
    }

    pub fn set_launch_kind(&mut self, kind: LaunchKind) {
        self.launch_kind = kind;
        self.launch_prompt = default_prompt(kind);
        self.active_tab = AppTab::Dispatch.index();
        self.dispatch_selected = DispatchFocus::Kind as usize;
        self.focus = LaunchFocus::Browse;
    }

    pub fn cycle_agent(&mut self) {
        self.shift_agent(1);
    }

    pub fn cycle_runtime(&mut self) {
        self.shift_runtime(1);
    }

    pub fn selected_agent(&self) -> &'static str {
        agents()[self.launch_agent]
    }

    pub fn shift_agent(&mut self, delta: isize) {
        let len = agents().len() as isize;
        let mut index = self.launch_agent as isize + delta;
        while index < 0 {
            index += len;
        }
        self.launch_agent = (index % len) as usize;
    }

    pub fn shift_runtime(&mut self, delta: isize) {
        let runtimes = [
            LaunchRuntime::Headless,
            LaunchRuntime::Terminal,
            LaunchRuntime::Visible,
        ];
        let current = runtimes
            .iter()
            .position(|runtime| *runtime == self.launch_runtime)
            .unwrap_or(1) as isize;
        let len = runtimes.len() as isize;
        let mut index = current + delta;
        while index < 0 {
            index += len;
        }
        self.launch_runtime = runtimes[(index % len) as usize];
    }

    pub fn shift_launch_kind(&mut self, delta: isize) {
        let kinds = [
            LaunchKind::Workflow,
            LaunchKind::Research,
            LaunchKind::Review,
            LaunchKind::Marbles,
        ];
        let current = kinds
            .iter()
            .position(|kind| *kind == self.launch_kind)
            .unwrap_or(0) as isize;
        let len = kinds.len() as isize;
        let mut index = current + delta;
        while index < 0 {
            index += len;
        }
        self.launch_kind = kinds[(index % len) as usize];
        self.launch_prompt = default_prompt(self.launch_kind);
    }

    pub fn dispatch_focus(&self) -> DispatchFocus {
        DispatchFocus::from_index(self.dispatch_selected)
    }

    pub fn move_dispatch_selection(&mut self, delta: isize) {
        let len = DispatchFocus::COUNT as isize;
        let mut index = self.dispatch_selected as isize + delta;
        while index < 0 {
            index += len;
        }
        self.dispatch_selected = (index % len) as usize;
    }

    pub fn adjust_dispatch_selection(&mut self, delta: isize) {
        match self.dispatch_focus() {
            DispatchFocus::Kind => self.shift_launch_kind(delta),
            DispatchFocus::Agent => self.shift_agent(delta),
            DispatchFocus::Runtime => self.shift_runtime(delta),
            DispatchFocus::Prompt => {
                self.focus = LaunchFocus::EditPrompt;
            }
        }
    }

    pub fn launch_request(&self) -> LaunchRequest {
        LaunchRequest {
            kind: self.launch_kind,
            agent: self.selected_agent().to_string(),
            prompt: self.launch_prompt.clone(),
            runtime: self.launch_runtime,
            root: Some(self.config.launch_root.clone()),
            terminal_binary: Some(self.config.terminal_binary.clone()),
            env: self.launch_env(),
            count: Some(3),
            depth: Some(3),
            session_name: match self.launch_runtime {
                LaunchRuntime::Terminal | LaunchRuntime::Visible => {
                    Some(default_session_name(self.launch_kind))
                }
                LaunchRuntime::Headless => None,
            },
        }
    }

    pub fn launch_command(&self) -> LaunchCommand {
        build_launch_command(&self.config.command_deck, &self.launch_request())
    }

    pub fn append_status<S: Into<String>>(&mut self, status: S) {
        self.status_line = status.into();
    }

    pub fn show_error<S: Into<String>>(&mut self, title: S, lines: Vec<String>) {
        self.error_title = title.into();
        self.error_lines = if lines.is_empty() {
            vec!["No error detail was captured.".to_string()]
        } else {
            lines
        };
        self.status_line = self.error_title.clone();
        self.focus = LaunchFocus::Error;
    }

    pub fn error_lines(&self) -> Vec<String> {
        let mut lines = vec![self.error_title.clone(), String::new()];
        lines.extend(self.error_lines.clone());
        lines
    }

    pub fn finish_prompt_edit(&mut self) {
        self.focus = LaunchFocus::Browse;
        self.append_status(format!(
            "prompt updated: {} chars across {} line(s)",
            self.launch_prompt.chars().count(),
            self.launch_prompt.lines().count().max(1)
        ));
    }

    pub fn push_launch_history<S: Into<String>>(&mut self, entry: S) {
        self.launch_history.push(entry.into());
        if self.launch_history.len() > 6 {
            self.launch_history.drain(0..self.launch_history.len() - 6);
        }
    }

    pub fn move_selection(&mut self, delta: isize) {
        if self.runs.is_empty() {
            self.selected = 0;
            return;
        }
        let len = self.runs.len() as isize;
        let mut index = self.selected as isize + delta;
        if index < 0 {
            index = len - 1;
        }
        if index >= len {
            index = 0;
        }
        self.selected = index as usize;
    }

    pub fn sync_selection(&mut self) {
        if self.selected >= self.runs.len() && !self.runs.is_empty() {
            self.selected = self.runs.len() - 1;
        }
        let deep_len = self.deep_actions().len();
        if deep_len == 0 {
            self.deep_selected = 0;
        } else if self.deep_selected >= deep_len {
            self.deep_selected = deep_len - 1;
        }
    }

    pub fn status_summary(&self) -> String {
        if self.runs.is_empty() {
            return format!("no {} runs loaded", self.queue_scope.label());
        }
        let mut counts = BTreeMap::new();
        for run in &self.runs {
            *counts.entry(run.kind.label()).or_insert(0usize) += 1;
        }
        let mut parts = vec![format!("runs: {}", self.runs.len())];
        for label in [
            "active",
            "stalled",
            "failed",
            "paused",
            "recent",
            "completed",
            "unknown",
        ] {
            if let Some(count) = counts.get(label)
                && *count > 0
            {
                parts.push(format!("{label} {count}"));
            }
        }
        parts.join(" | ")
    }

    pub fn detail_lines(&self) -> Vec<String> {
        let Some(run) = self.selected_run() else {
            return vec![
                "No runs found in the control-plane state directory yet.".to_string(),
                String::new(),
                "Start here:".to_string(),
                "1 -> Workflow for the normal path".to_string(),
                "2 -> Research swarm if the surface is still unclear".to_string(),
                "3 -> Review if something already exists and needs truth".to_string(),
                "4 -> Marbles when the system works but still drifts".to_string(),
                String::new(),
                "Use a / v / e / Enter in the launch panel below.".to_string(),
                "Press ? for the in-app operator guide.".to_string(),
                String::new(),
                format!("State root: {}", path_display(&self.config.state_root)),
                format!("Launch root: {}", path_display(&self.config.launch_root)),
            ];
        };

        let snapshot = &run.snapshot;
        let mut lines = vec![
            format!("run_id: {}", snapshot.run_id),
            format!(
                "status: {} ({})",
                run.kind.label(),
                snapshot.display_state()
            ),
            format!("agent: {}", snapshot.agent.as_deref().unwrap_or("unknown")),
            format!("skill: {}", snapshot.skill.as_deref().unwrap_or("unknown")),
            format!("mode: {}", snapshot.mode.as_deref().unwrap_or("unknown")),
            format!("age: {}", run.age_label),
            format!(
                "operator_session: {}",
                snapshot.operator_session.as_deref().unwrap_or("none")
            ),
        ];
        if let Some(session_id) = snapshot.session_id.as_deref() {
            lines.push(format!("session_id: {session_id}"));
        }

        if let Some(root) = snapshot.root.as_deref() {
            lines.push(format!("root: {root}"));
        }
        if let Some(report) = snapshot.latest_report.as_deref() {
            lines.push(format!("latest_report: {report}"));
        }
        if let Some(transcript) = snapshot.latest_transcript.as_deref() {
            lines.push(format!("latest_transcript: {transcript}"));
        }
        if let Some(error) = snapshot.last_error.as_deref() {
            lines.push(format!("last_error: {error}"));
        }
        if let Some(session) = snapshot.operator_session.as_deref() {
            lines.push(String::new());
            lines.push(format!(
                "Attach hint: vibecrafted dashboard attach {session}"
            ));
            lines.push(format!("Zellij hint: zellij attach {session}"));
        }
        if let Some(agent) = snapshot.agent.as_deref() {
            lines.push(format!("Resume hint: vibecrafted resume {agent}"));
        }
        lines.push(String::new());
        lines.push(format!(
            "State root: {}",
            path_display(&self.config.state_root)
        ));
        lines
    }

    pub fn event_lines(&self) -> Vec<String> {
        let Some(run) = self.selected_run() else {
            return Vec::new();
        };
        if run.recent_events.is_empty() {
            return vec!["No recent events for this run.".to_string()];
        }
        run.recent_events
            .iter()
            .map(|event| {
                let message = event.message.as_deref().unwrap_or(event.kind.as_str());
                format!("{} {}", event.ts, message)
            })
            .collect()
    }

    pub fn prompt_lines(&self) -> Vec<String> {
        let command_preview = self.launch_command().command_line();
        let mut lines = vec![
            dispatch_line(
                self.dispatch_focus() == DispatchFocus::Kind,
                format!(
                    "mission: {}  {}",
                    self.launch_kind.human_title(),
                    self.launch_kind.human_description()
                ),
            ),
            dispatch_line(
                self.dispatch_focus() == DispatchFocus::Agent,
                format!("agent: {}", self.selected_agent()),
            ),
            dispatch_line(
                self.dispatch_focus() == DispatchFocus::Runtime,
                format!("runtime: {}", self.launch_runtime.label()),
            ),
            dispatch_line(
                self.dispatch_focus() == DispatchFocus::Prompt,
                format!("prompt: {}", one_line_prompt(&self.launch_prompt)),
            ),
            String::new(),
            "Arrows: ↑/↓ choose field  ←/→ change field  Enter launch".to_string(),
            "Shortcuts: 1-4 mission  a agent  v runtime  e edit prompt  / search".to_string(),
            String::new(),
            format!("root: {}", path_display(&self.config.launch_root)),
            format!("command: {}", command_preview),
        ];
        if let Some(last) = self.launch_history.last() {
            lines.push(String::new());
            lines.push(format!("last launch: {last}"));
        }
        lines
    }

    pub fn help_lines(&self) -> Vec<String> {
        vec![
            "Operator guide".to_string(),
            String::new(),
            "This console is the human front door into Vibecrafted control-plane state.".to_string(),
            "Browse runs on the left, inspect truth on the right, and launch new work below.".to_string(),
            String::new(),
            "Quick start".to_string(),
            "1 Workflow  -> normal path for most tasks".to_string(),
            "2 Research  -> send a research swarm first".to_string(),
            "3 Review    -> audit an existing surface".to_string(),
            "4 Marbles   -> convergence loop for fragile systems".to_string(),
            String::new(),
            "Tabs".to_string(),
            "Tab / Shift+Tab switch between Monitor, Dispatch, and Controls.".to_string(),
            "Monitor keeps the live board. Dispatch shapes the next run. Controls opens attach/report actions.".to_string(),
            String::new(),
            "Keys".to_string(),
            "↑/↓ or j/k  navigate inside the active tab".to_string(),
            "a           cycle launch agent".to_string(),
            "v           cycle runtime (terminal / visible / headless)".to_string(),
            "e           edit launch prompt".to_string(),
            "Ctrl+S/Esc  save prompt edits; Enter inserts a prompt newline".to_string(),
            "Enter       launch selected action".to_string(),
            "d           selected-run deep controls".to_string(),
            "y           copy resume/report/run identity to clipboard".to_string(),
            "f           cycle queue scope: live, history, all".to_string(),
            "/           search runs by id, agent, skill, status, path".to_string(),
            "x           archive selected run from the operator view".to_string(),
            "r           refresh control-plane state".to_string(),
            "?           close this guide".to_string(),
            "q / Esc     quit".to_string(),
            String::new(),
            "Operator rule".to_string(),
            "Use this to decide and launch. Let worker agents execute; do not overload the shell as your only dashboard.".to_string(),
        ]
    }

    pub fn active_run_count(&self) -> usize {
        self.runs
            .iter()
            .filter(|run| matches!(run.kind, RunKind::Active | RunKind::Stalled))
            .count()
    }

    pub fn tab_labels(&self) -> [String; 4] {
        let monitor = if self.search_query.is_empty() {
            format!("Monitor {} {}", self.queue_scope.label(), self.runs.len())
        } else {
            format!("Monitor {} {}?", self.queue_scope.label(), self.runs.len())
        };
        let dispatch = format!(
            "Dispatch {}/{}",
            self.launch_kind.label(),
            self.selected_agent()
        );
        let controls = format!("Controls {}", self.deep_actions().len());
        let mission = format!(
            "Mission {}|{}",
            self.mission_control.active_dispatches.len(),
            self.mission_control.action_queue.len()
        );
        [monitor, dispatch, controls, mission]
    }

    pub fn deep_actions(&self) -> Vec<DeepAction> {
        let mut actions = Vec::new();
        if let Some(run) = self.selected_run() {
            let snapshot = &run.snapshot;
            if let Some(session) = snapshot
                .operator_session
                .as_ref()
                .filter(|value| !value.is_empty())
            {
                actions.push(DeepAction::AttachSession(session.clone()));
            }
            if let (Some(agent), Some(session)) = (
                snapshot.agent.as_ref().filter(|value| !value.is_empty()),
                snapshot
                    .session_id
                    .as_ref()
                    .filter(|value| !value.is_empty()),
            ) {
                actions.push(DeepAction::ResumeSession {
                    agent: agent.clone(),
                    session: session.clone(),
                });
            }
            if let Some(report) = snapshot
                .latest_report
                .as_ref()
                .filter(|value| !value.is_empty())
            {
                actions.push(DeepAction::OpenReport(PathBuf::from(report)));
            }
            if let Some(transcript) = snapshot
                .latest_transcript
                .as_ref()
                .filter(|value| !value.is_empty())
            {
                actions.push(DeepAction::OpenTranscript(PathBuf::from(transcript)));
            }
            if let Some(root) = snapshot.root.as_ref().filter(|value| !value.is_empty()) {
                actions.push(DeepAction::OpenRoot(PathBuf::from(root)));
            }
        }
        // MCP daemon health-check actions are always available (one per
        // known rmcp-mux service), independent of whether a run is
        // selected. Operators commonly need to check the supervisor when
        // *no* run is healthy, so gating these on selection would defeat
        // the surface.
        for summary in &self.mux_summaries {
            actions.push(DeepAction::MuxHealth {
                service: summary.display_name.clone(),
            });
        }
        for intent in &self.polarize_intents {
            actions.push(DeepAction::PolarizeIntent {
                band: intent.band,
                score: intent.score,
                run_id: intent.run_id.clone(),
                prism_path: intent.prism_path.clone(),
            });
        }
        for entry in skills_catalog::CATALOG {
            let agent = resolve_skill_agent(entry.default_agent, self.selected_agent());
            let payload = match entry.accepts {
                SkillPayloadKind::None => SkillPayload::None,
                SkillPayloadKind::Optional | SkillPayloadKind::PromptOrFile => {
                    if self.launch_prompt.trim().is_empty() {
                        SkillPayload::None
                    } else {
                        SkillPayload::Prompt(self.launch_prompt.clone())
                    }
                }
            };
            actions.push(DeepAction::SkillLaunch {
                skill: entry.slug.to_string(),
                agent,
                payload,
            });
        }
        actions
    }

    pub fn selected_deep_action(&self) -> Option<DeepAction> {
        self.deep_actions().get(self.deep_selected).cloned()
    }

    pub fn deep_action_index_for_primary_mission_queue_item(&self) -> Option<usize> {
        let item = self.mission_control.action_queue.first()?;
        self.deep_actions()
            .iter()
            .position(|action| mission_queue_item_matches_deep_action(item, action))
    }

    pub fn preselect_controls_from_mission_queue(&mut self) -> bool {
        if let Some(index) = self.deep_action_index_for_primary_mission_queue_item() {
            self.deep_selected = index;
            return true;
        }
        false
    }

    pub fn move_deep_selection(&mut self, delta: isize) {
        let len = self.deep_actions().len();
        if len == 0 {
            self.deep_selected = 0;
            return;
        }
        let len = len as isize;
        let mut index = self.deep_selected as isize + delta;
        if index < 0 {
            index = len - 1;
        }
        if index >= len {
            index = 0;
        }
        self.deep_selected = index as usize;
    }

    pub fn deep_control_lines(&self) -> Vec<String> {
        let actions = self.deep_actions();
        if actions.is_empty() {
            return vec![
                "Deep controls".to_string(),
                "No attach/resume/report actions are available for the selected run.".to_string(),
                "Pick another run or launch a fresh one below.".to_string(),
            ];
        }
        let mut lines = vec![
            "Deep controls".to_string(),
            "Enter runs the selected action. Esc returns to browse.".to_string(),
            String::new(),
        ];
        lines.extend(actions.iter().enumerate().map(|(idx, action)| {
            let prefix = if self.active_tab() == AppTab::Controls && idx == self.deep_selected {
                "▶"
            } else {
                " "
            };
            format!("{prefix} {}", action.label())
        }));
        lines
    }

    pub fn prompt_edit_lines(&self) -> Vec<String> {
        let mut lines = vec![
            "Prompt editor".to_string(),
            format!(
                "{} chars across {} line(s)",
                self.launch_prompt.chars().count(),
                self.launch_prompt.lines().count().max(1)
            ),
            String::new(),
        ];
        if self.launch_prompt.is_empty() {
            lines.push("Type the worker prompt here.".to_string());
        } else {
            lines.extend(self.launch_prompt.lines().map(ToOwned::to_owned));
        }
        lines.push(String::new());
        lines.push("Enter inserts newline. Ctrl+S or Esc saves.".to_string());
        lines
    }

    pub fn open_artifact(&mut self, action: &DeepAction) -> anyhow::Result<()> {
        let (title, path) = match action {
            DeepAction::OpenReport(path) => ("Report", path),
            DeepAction::OpenTranscript(path) => ("Transcript", path),
            DeepAction::OpenRoot(path) => ("Run root", path),
            _ => return Ok(()),
        };
        self.artifact_title = format!("{title}: {}", path_display(path));
        let run_root = self
            .selected_run()
            .and_then(|run| run.snapshot.root.as_deref());
        self.artifact_lines = artifact_lines(path, run_root)?;
        self.focus = LaunchFocus::Artifact;
        self.append_status(format!("opened {} in operator viewer", path_display(path)));
        Ok(())
    }

    pub fn open_polarize_intent(&mut self, action: &DeepAction) -> anyhow::Result<()> {
        let DeepAction::PolarizeIntent {
            band,
            score,
            run_id,
            prism_path,
        } = action
        else {
            return Ok(());
        };
        self.artifact_title = format!(
            "Polarize prism: {} score {} run {}",
            band.label(),
            score,
            run_id
        );
        self.artifact_lines = crate::polarize::prism_preview_lines(prism_path)?;
        self.focus = LaunchFocus::Artifact;
        self.append_status(format!(
            "opened polarize prism {}",
            path_display(prism_path)
        ));
        Ok(())
    }

    pub fn artifact_lines(&self) -> Vec<String> {
        let mut lines = vec![self.artifact_title.clone(), String::new()];
        lines.extend(self.artifact_lines.clone());
        lines
    }

    pub fn clipboard_payload(&self) -> Option<String> {
        let run = self.selected_run()?;
        let snapshot = &run.snapshot;
        if let (Some(agent), Some(session)) =
            (snapshot.agent.as_deref(), snapshot.session_id.as_deref())
        {
            return Some(format!("vibecrafted resume {agent} --session {session}"));
        }
        if let Some(report) = snapshot.latest_report.as_deref() {
            return Some(report.to_string());
        }
        Some(snapshot.run_id.clone())
    }

    pub fn copy_selected_run_to_clipboard(&mut self) -> anyhow::Result<()> {
        let Some(payload) = self.clipboard_payload() else {
            self.append_status("No selected run to copy.");
            return Ok(());
        };
        let mut clipboard = arboard::Clipboard::new()?;
        clipboard.set_text(payload.clone())?;
        self.append_status(format!("copied to clipboard: {payload}"));
        Ok(())
    }

    pub(crate) fn launch_env(&self) -> BTreeMap<String, OsString> {
        let mut env = BTreeMap::new();
        env.insert(
            "VIBECRAFTED_ROOT".to_string(),
            self.config.launch_root.as_os_str().to_os_string(),
        );
        env.insert(
            "VIBECRAFT_OPERATOR_STATE_ROOT".to_string(),
            self.config.state_root.as_os_str().to_os_string(),
        );
        if let Some(config_dir) =
            std::env::var_os("ZELLIJ_CONFIG_DIR").filter(|value| !value.is_empty())
        {
            env.insert("ZELLIJ_CONFIG_DIR".to_string(), config_dir);
        }
        env
    }
}

fn dispatch_line(selected: bool, content: String) -> String {
    if selected {
        format!("▶ {content}")
    } else {
        format!("  {content}")
    }
}

/// Number of Mission Control panels rendered in the dashboard. Kept as a
/// const so navigation wrap-around stays in sync with PLAN_23 §4 (seven
/// panels: active dispatches, wave atlas, per-agent stats, per-skill
/// stats, fleet health, failure board, operator action queue).
pub const MISSION_PANEL_COUNT: usize = 7;

pub fn mission_panel_count() -> usize {
    MISSION_PANEL_COUNT
}

fn mission_queue_item_matches_deep_action(item: &ActionQueueItem, action: &DeepAction) -> bool {
    match (item.kind, action) {
        (ActionQueueKind::StalledRun, DeepAction::OpenRoot(path)) => {
            path_match(item.source_path.as_deref(), path)
        }
        (ActionQueueKind::Failure | ActionQueueKind::ReportReady, DeepAction::OpenReport(path)) => {
            path_match(item.source_path.as_deref(), path)
                || run_id_match(action_queue_run_id(item), path)
        }
        (
            ActionQueueKind::Polarize,
            DeepAction::PolarizeIntent {
                run_id, prism_path, ..
            },
        ) => {
            path_match(item.source_path.as_deref(), prism_path)
                || action_queue_run_id(item) == Some(run_id.as_str())
        }
        _ => false,
    }
}

fn action_queue_run_id(item: &ActionQueueItem) -> Option<&str> {
    let mut parts = item.summary.split_whitespace();
    parts.next()?;
    parts.next().map(|segment| segment.trim())
}

fn run_id_match(run_id: Option<&str>, path: &Path) -> bool {
    run_id
        .map(|value| path.to_string_lossy().contains(value))
        .unwrap_or(false)
}

fn path_match(expected: Option<&Path>, actual: &Path) -> bool {
    expected == Some(actual)
}

pub fn default_prompt(kind: LaunchKind) -> String {
    match kind {
        LaunchKind::Workflow => "Plan and implement the task I am looking at now.".to_string(),
        LaunchKind::Research => {
            "Research the task I am looking at now and report the ground truth.".to_string()
        }
        LaunchKind::Review => {
            "Review the selected surface and call out concrete risks.".to_string()
        }
        LaunchKind::Marbles => {
            "Run a convergence loop on the selected surface until the lies are exposed.".to_string()
        }
    }
}

pub fn agents() -> [&'static str; 3] {
    ["claude", "codex", "gemini"]
}

fn is_live_run(kind: RunKind) -> bool {
    matches!(kind, RunKind::Active | RunKind::Stalled | RunKind::Paused)
}

fn apply_run_filters(runs: &mut Vec<RenderedRun>, queue_scope: QueueScope, search_query: &str) {
    match queue_scope {
        QueueScope::Live => runs.retain(|run| is_live_run(run.kind)),
        QueueScope::History => runs.retain(|run| !is_live_run(run.kind)),
        QueueScope::All => {}
    }
    let query = search_query.trim().to_ascii_lowercase();
    if !query.is_empty() {
        runs.retain(|run| run_matches_query(run, &query));
    }
}

fn run_matches_query(run: &RenderedRun, query: &str) -> bool {
    let snapshot = &run.snapshot;
    [
        Some(snapshot.run_id.as_str()),
        snapshot.session_id.as_deref(),
        snapshot.agent.as_deref(),
        snapshot.skill.as_deref(),
        snapshot.mode.as_deref(),
        snapshot.state.as_deref(),
        snapshot.status.as_deref(),
        snapshot.root.as_deref(),
        snapshot.latest_report.as_deref(),
        snapshot.latest_transcript.as_deref(),
    ]
    .into_iter()
    .flatten()
    .any(|value| value.to_ascii_lowercase().contains(query))
}

fn one_line_prompt(prompt: &str) -> String {
    let collapsed = prompt.split_whitespace().collect::<Vec<_>>().join(" ");
    if collapsed.chars().count() > 96 {
        let mut short = collapsed.chars().take(93).collect::<String>();
        short.push_str("...");
        short
    } else {
        collapsed
    }
}

fn safe_marker_name(run_id: &str) -> String {
    run_id
        .chars()
        .map(|ch| {
            if ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_' | '.') {
                ch
            } else {
                '_'
            }
        })
        .collect()
}

fn polarize_marker(band: PolarizeBand) -> &'static str {
    match band {
        PolarizeBand::Abort => "!",
        PolarizeBand::Memo => "-",
        PolarizeBand::Pass => ">",
        PolarizeBand::Doctrine => "*",
    }
}

fn resolve_skill_agent(default_agent: SkillAgent, selected_agent: &str) -> SkillAgent {
    match default_agent {
        SkillAgent::Any => SkillAgent::from_cli_token(selected_agent),
        concrete => concrete,
    }
}

fn artifact_lines(path: &Path, run_root: Option<&str>) -> anyhow::Result<Vec<String>> {
    let path = safe_artifact_path(path, run_root)?;
    if path.is_dir() {
        let mut rows = Vec::new();
        // `safe_artifact_path` canonicalizes this path and constrains it to the selected run root.
        let entries = fs::read_dir(&path)?; // nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path
        for entry in entries {
            let entry = entry?;
            let file_type = entry.file_type()?;
            let suffix = if file_type.is_dir() { "/" } else { "" };
            rows.push(format!("{}{}", entry.file_name().to_string_lossy(), suffix));
        }
        rows.sort();
        if rows.is_empty() {
            rows.push("(empty directory)".to_string());
        }
        return Ok(rows);
    }
    // `safe_artifact_path` canonicalizes this path and constrains it to the selected run root.
    let text = fs::read_to_string(&path)?; // nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path
    let mut lines = text
        .lines()
        .take(400)
        .map(ToOwned::to_owned)
        .collect::<Vec<_>>();
    if text.lines().count() > 400 {
        lines.push("[truncated after 400 lines]".to_string());
    }
    Ok(lines)
}

fn default_session_name(kind: LaunchKind) -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let suffix = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| (d.as_millis() % 100_000) as u32)
        .unwrap_or(0);
    format!("vc-op-{}-{:05}", kind.label(), suffix)
}

fn safe_artifact_path(path: &Path, run_root: Option<&str>) -> anyhow::Result<PathBuf> {
    let meta = fs::symlink_metadata(path)?;
    if meta.file_type().is_symlink() {
        anyhow::bail!(
            "refusing to open symlinked artifact: {}",
            path_display(path)
        );
    }
    let canonical = fs::canonicalize(path)?;
    let Some(run_root) = run_root.filter(|root| !root.trim().is_empty()) else {
        anyhow::bail!("selected run has no root; refusing artifact path");
    };
    let root = fs::canonicalize(run_root)?;
    if !canonical.starts_with(&root) {
        anyhow::bail!(
            "refusing artifact outside selected run root: {}",
            path_display(&canonical)
        );
    }
    Ok(canonical)
}