leviath-cli 0.3.9

Command-line interface for Leviath agent framework
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
//! New-run screen: the agent catalog, the `@` file completion, and the async
//! spawn lane.
//!
//! `lev dash` could only ever watch runs somebody else had started. This is the
//! other half: pick an agent, write the task, press Enter. The screen is modal
//! like the MCP one, and for the same reason - it owns the keys while open, so
//! typing a task can never also mean "kill the selected run".
//!
//! Resolving a blueprint reads and parses files, and the spawn is a socket
//! round trip, so both go to [`spawn_background_loop`] and come back as toasts.
//! `send_spawn` is deliberately not reused: it prints its report on stdout,
//! which would land in the middle of the alternate screen.

use std::collections::HashMap;
use std::path::Path;

use leviath_runtime::control_socket::{ControlClient, ControlResponse};
use tokio::sync::mpsc;

use super::state::Dashboard;
use super::types::{
    ConfirmAction, NewRunAgent, NewRunContext, NewRunPane, SpawnCommand, SpawnOutcome, ToastLevel,
};
use crate::commands::list::{ListFilter, build_list_report};
use crate::config::Config;
use crate::daemon::client::{LaunchRequest, never_interactive, resolve_spawn_args};
use crate::tui::widgets::confirm::Confirm;

/// How many ticks the dashboard waits for a just-started run to appear before
/// giving up on opening its page. At the 250ms tick the loop runs on, this is
/// about fifteen seconds.
pub(super) const OPEN_RUN_TICKS: u32 = 60;
use ratatui::text::Line;

/// How many workdir files the `@` completion will offer.
///
/// A repository can hold hundreds of thousands of files; walking all of them
/// would stall the screen open, and the menu only ever shows the first handful
/// of matches anyway. Typing more of the path is what narrows it, not a longer
/// list.
const FILE_CANDIDATE_CAP: usize = 2_000;

/// How many completions are shown at once.
const FILE_REF_VISIBLE: usize = 8;

impl Dashboard {
    /// Open the new-run screen: rebuild the agent catalog, walk the workdir for
    /// `@` candidates, and start from an empty task.
    pub(super) fn open_new_run_screen(&mut self) {
        self.new_run_screen = true;
        self.new_run_focus = NewRunPane::Agents;
        self.new_run_filter.clear();
        self.new_run_selected = 0;
        self.new_run_task = ratatui_textarea::TextArea::default();
        // Unattended is off every time the screen opens. It is a consequential
        // setting, and one that survived out of sight is one somebody can
        // leave on and forget.
        self.new_run_yolo = false;
        self.close_file_ref();
        self.refresh_new_run_agents();
        self.new_run_files = collect_workdir_files(&self.new_run_ctx.workdir, FILE_CANDIDATE_CAP);
    }

    /// Close the screen, discarding whatever was typed.
    pub(super) fn close_new_run_screen(&mut self) {
        self.new_run_screen = false;
        self.close_file_ref();
    }

    /// Rebuild the agent list from the same catalog `lev list` reports, so the
    /// picker offers exactly the agents `lev run` can resolve - plus the
    /// bundled blueprints, which say so when they are not installed yet.
    pub(super) fn refresh_new_run_agents(&mut self) {
        let ctx = &self.new_run_ctx;
        let config = Config::load_from_path_public(&ctx.config_path).unwrap_or_default();
        let report = build_list_report(&ctx.agents_dir, &ctx.workdir, &config, ListFilter::All);
        let mut agents: Vec<NewRunAgent> = report
            .agents
            .iter()
            .map(|a| NewRunAgent {
                name: a.info.name.clone(),
                source: a.source.to_string(),
                description: a.info.description.clone(),
                path: a.path.clone(),
            })
            .collect();
        // A bundled blueprint already installed under the same name is the same
        // agent twice; the installed copy is the one that resolves, so it wins.
        for entry in &report.bundled {
            if !agents.iter().any(|a| a.name == entry.name) {
                agents.push(NewRunAgent {
                    name: entry.name.clone(),
                    source: "bundled".to_string(),
                    description: format!("v{} - install with `lev setup`", entry.version),
                    path: entry.name.clone(),
                });
            }
        }
        agents.sort_by(|a, b| a.name.cmp(&b.name));
        self.new_run_agents = agents;
        self.clamp_new_run_selection();
    }

    /// Indices into `new_run_agents` that match the filter, in display order.
    pub(super) fn filtered_new_run_agents(&self) -> Vec<usize> {
        let query = self.new_run_filter.to_lowercase();
        self.new_run_agents
            .iter()
            .enumerate()
            .filter(|(_, a)| {
                query.is_empty()
                    || a.name.to_lowercase().contains(&query)
                    || a.description.to_lowercase().contains(&query)
            })
            .map(|(i, _)| i)
            .collect()
    }

    /// The agent the picker is pointing at, if the filtered list is not empty.
    pub(super) fn new_run_selected_agent(&self) -> Option<&NewRunAgent> {
        let visible = self.filtered_new_run_agents();
        visible
            .get(self.new_run_selected)
            .and_then(|i| self.new_run_agents.get(*i))
    }

    /// Keep the selection inside the filtered list after a filter edit.
    fn clamp_new_run_selection(&mut self) {
        let len = self.filtered_new_run_agents().len();
        if self.new_run_selected >= len {
            self.new_run_selected = len.saturating_sub(1);
        }
    }

    /// Dispatch the run to the background lane and close the screen, so the new
    /// row shows up in the list the user is returned to.
    pub(super) fn submit_new_run(&mut self) {
        let Some(agent) = self.new_run_selected_agent().cloned() else {
            self.toast("Pick an agent blueprint first", ToastLevel::Error);
            return;
        };
        let task = self.new_run_task.lines().join("\n").trim().to_string();
        // An agent driven entirely by `--<region>` flags takes no task, and
        // there is no way to give it one here; that is a `lev run` command line,
        // and the daemon says so if this screen is pointed at such an agent.
        if task.is_empty() {
            self.toast("Write a task first", ToastLevel::Error);
            return;
        }
        let _ = self.spawn_cmd_tx.send(SpawnCommand {
            agent_path: agent.path.clone(),
            task,
            workdir: self.new_run_ctx.workdir.display().to_string(),
            yolo: self.new_run_yolo,
        });
        let how = match self.new_run_yolo {
            true => " unattended",
            false => "",
        };
        self.toast(format!("Starting '{}'{how}", agent.name), ToastLevel::Info);
        self.add_log(format!("run requested: {}", agent.name));
        self.close_new_run_screen();
    }

    /// Drain finished spawns into toasts, mirroring [`Self::drain_mcp_outcomes`].
    pub(super) fn drain_spawn_outcomes(&mut self) {
        while let Ok(outcome) = self.spawn_outcome_rx.try_recv() {
            let level = match outcome.ok {
                true => ToastLevel::Info,
                false => ToastLevel::Error,
            };
            self.add_log(outcome.message.clone());
            self.toast(outcome.message, level);
            // Starting a run is a request to watch it, so the dashboard goes
            // to its page. Not here though: the daemon has only just been told,
            // and the run reaches the list on a later sync.
            if let Some(run_id) = outcome.run_id {
                self.pending_open_run = Some((run_id, OPEN_RUN_TICKS));
            }
        }
    }

    /// Open the page of a run started from this screen, once it exists.
    ///
    /// Bounded rather than open-ended: a run that never appears is a run the
    /// daemon dropped, and a dashboard that jumps to a page minutes later,
    /// after the user has moved on, is worse than one that quietly gives up.
    /// The toast already said the run started.
    pub(super) fn open_pending_run(&mut self) {
        let Some((run_id, ticks)) = self.pending_open_run.take() else {
            return;
        };
        // Only from the list. Somewhere else means the user went there
        // themselves, and yanking them out of it is not what they asked for.
        if self.detail_view || self.mcp_screen || self.new_run_screen {
            return;
        }
        match self
            .display_indices
            .iter()
            .position(|i| self.agents.get(*i).is_some_and(|agent| agent.id == run_id))
        {
            Some(row) => {
                self.selected = row;
                self.table_state.select(Some(row));
                self.open_detail_view();
            }
            None => {
                if let Some(left) = ticks.checked_sub(1) {
                    self.pending_open_run = Some((run_id, left));
                }
            }
        }
    }

    // ── `@` file references ──────────────────────────────────────────────────

    /// The workdir paths matching what has been typed after the `@`, capped to
    /// what the popup shows.
    pub(super) fn file_ref_matches(&self) -> Vec<&str> {
        if !self.new_run_file_ref {
            return Vec::new();
        }
        let query = self.new_run_file_query.to_lowercase();
        self.new_run_files
            .iter()
            .filter(|p| p.to_lowercase().contains(&query))
            .take(FILE_REF_VISIBLE)
            .map(String::as_str)
            .collect()
    }

    /// Replace the typed query with the highlighted path and close the popup.
    /// With nothing matching there is nothing to insert, so the text stands as
    /// typed.
    fn accept_file_ref(&mut self) {
        let chosen = self
            .file_ref_matches()
            .get(self.new_run_file_selected)
            .map(|p| p.to_string());
        if let Some(path) = chosen {
            for _ in 0..self.new_run_file_query.chars().count() {
                self.new_run_task.delete_char();
            }
            self.new_run_task.insert_str(&path);
        }
        self.close_file_ref();
    }

    /// Dismiss the completion, leaving the task text exactly as typed.
    fn close_file_ref(&mut self) {
        self.new_run_file_ref = false;
        self.new_run_file_query.clear();
        self.new_run_file_selected = 0;
    }

    /// Ctrl-Y: turn unattended runs on or off for this screen.
    ///
    /// A chord rather than a letter because both panes here consume printable
    /// characters - one filters the agent list, the other is the task itself -
    /// so there is no letter left that could mean anything but text.
    ///
    /// Turning it *on* asks first, once per sitting. Turning it off never asks:
    /// nothing needs confirming about deciding to be asked more.
    pub(super) fn toggle_new_run_yolo(&mut self) {
        if self.new_run_yolo {
            self.new_run_yolo = false;
            self.toast("Unattended off: you will be asked", ToastLevel::Info);
            return;
        }
        if self.yolo_warning_silenced {
            self.new_run_yolo = true;
            self.toast("Unattended on", ToastLevel::Warning);
            return;
        }
        self.pending_confirm = Some((ConfirmAction::EnableYolo, yolo_warning()));
    }

    /// Apply the answer to that warning.
    pub(super) fn accept_yolo_warning(&mut self, silence: bool) {
        self.new_run_yolo = true;
        self.yolo_warning_silenced = silence;
        self.toast("Unattended on", ToastLevel::Warning);
    }

    // ── Keys ─────────────────────────────────────────────────────────────────

    /// Keys while the new-run screen is open. The `@` popup takes them first -
    /// it is a menu over the text being typed, not a mode beside it - then the
    /// focused pane.
    pub(super) fn handle_new_run_key(&mut self, key: crossterm::event::KeyEvent) {
        if self.new_run_file_ref {
            self.handle_file_ref_key(key);
            return;
        }
        // Ahead of both panes: these belong to the screen, not to whichever
        // half of it currently has the cursor. F1 rather than `?`, which is a
        // question mark in both a filter box and a task.
        if key.code == crossterm::event::KeyCode::F(1) {
            self.show_help = true;
            return;
        }
        if key
            .modifiers
            .contains(crossterm::event::KeyModifiers::CONTROL)
            && key.code == crossterm::event::KeyCode::Char('y')
        {
            self.toggle_new_run_yolo();
            return;
        }
        match self.new_run_focus {
            NewRunPane::Agents => self.handle_new_run_agents_key(key.code),
            NewRunPane::Task => self.handle_new_run_task_key(key),
        }
    }

    /// Agent picker keys. Every printable character filters, so there is no
    /// separate search mode to enter - and no letter is free to mean something
    /// else here.
    fn handle_new_run_agents_key(&mut self, key_code: crossterm::event::KeyCode) {
        use crossterm::event::KeyCode;
        match key_code {
            // Esc clears the filter first, then closes - the same two-step the
            // main list's filter uses.
            KeyCode::Esc => match self.new_run_filter.is_empty() {
                true => self.close_new_run_screen(),
                false => {
                    self.new_run_filter.clear();
                    self.new_run_selected = 0;
                }
            },
            KeyCode::Tab | KeyCode::BackTab | KeyCode::Enter => {
                self.new_run_focus = NewRunPane::Task;
            }
            KeyCode::Up => {
                self.new_run_selected = self.new_run_selected.saturating_sub(1);
            }
            KeyCode::Down => {
                if self.new_run_selected + 1 < self.filtered_new_run_agents().len() {
                    self.new_run_selected += 1;
                }
            }
            KeyCode::Backspace => {
                self.new_run_filter.pop();
                self.new_run_selected = 0;
            }
            KeyCode::Char(c) => {
                self.new_run_filter.push(c);
                self.new_run_selected = 0;
            }
            _ => {}
        }
    }

    /// Task editor keys. Enter starts the run and Alt+Enter breaks the line,
    /// matching the response pane so the two editors do not disagree.
    fn handle_new_run_task_key(&mut self, key: crossterm::event::KeyEvent) {
        use crossterm::event::KeyCode;
        match key.code {
            KeyCode::Esc => self.new_run_focus = NewRunPane::Agents,
            KeyCode::Tab | KeyCode::BackTab => self.new_run_focus = NewRunPane::Agents,
            KeyCode::Enter if key.modifiers.is_empty() => self.submit_new_run(),
            KeyCode::Char('@') => {
                self.new_run_task.insert_char('@');
                self.new_run_file_ref = true;
            }
            _ => {
                self.new_run_task.input(ratatui_textarea::Input::from(key));
            }
        }
    }

    /// Keys while an `@` completion is open.
    fn handle_file_ref_key(&mut self, key: crossterm::event::KeyEvent) {
        use crossterm::event::KeyCode;
        let matches = self.file_ref_matches().len();
        match key.code {
            KeyCode::Esc => self.close_file_ref(),
            KeyCode::Enter | KeyCode::Tab => self.accept_file_ref(),
            KeyCode::Up => {
                self.new_run_file_selected = self.new_run_file_selected.saturating_sub(1);
            }
            KeyCode::Down => {
                if self.new_run_file_selected + 1 < matches {
                    self.new_run_file_selected += 1;
                }
            }
            KeyCode::Backspace => {
                self.new_run_task.delete_char();
                // Backspacing over the `@` itself ends the reference; there is
                // nothing left to complete.
                match self.new_run_file_query.pop() {
                    // The match set changed, so the highlight goes back to the top.
                    Some(_) => self.new_run_file_selected = 0,
                    None => self.close_file_ref(),
                }
            }
            KeyCode::Char(c) => {
                self.new_run_task.insert_char(c);
                self.new_run_file_query.push(c);
                self.new_run_file_selected = 0;
            }
            _ => {}
        }
    }
}

/// The warning shown before the first unattended run of a sitting.
///
/// It says what `--yolo` does *and* what it does not: a run that still stops
/// for a person reads as a hang to somebody who was told it would not stop, and
/// that misunderstanding is worse than the setting itself.
fn yolo_warning() -> Confirm {
    Confirm::new(
        "Run unattended?",
        vec![
            Line::from(
                "Agent runs started from this screen will approve their own tool calls: \
                 editing files, running shell commands, fetching URLs, whatever \
                 the agent's permissions allow, without stopping to ask you.",
            ),
            Line::from(""),
            Line::from(
                "It does not skip checkpoints a blueprint asks a person for. \
                 Those still stop, and one nobody answers ends the run when the \
                 interaction timeout expires.",
            ),
            Line::from(""),
            Line::from("Ctrl-Y turns it off again."),
        ],
        "Run unattended",
        "Keep asking me",
    )
    .danger()
    .with_remember("Don't ask again while this dashboard is open")
}

/// Workdir-relative paths of the files under `root`, sorted, at most `cap`.
///
/// There is no `.gitignore` reader in the dependency tree and adding one to
/// populate a completion menu is not worth it, so the walk skips what an
/// ignore file would almost always cover anyway: dot-entries (`.git`,
/// `.venv`, editor state) and `target`. An unreadable directory is skipped
/// rather than reported - this list is a convenience, and half of it beats an
/// error message.
fn collect_workdir_files(root: &Path, cap: usize) -> Vec<String> {
    let mut out = Vec::new();
    let mut stack = vec![(root.to_path_buf(), String::new())];
    while let Some((dir, prefix)) = stack.pop() {
        let Ok(entries) = std::fs::read_dir(&dir) else {
            continue;
        };
        for entry in entries.flatten() {
            if out.len() >= cap {
                out.sort();
                return out;
            }
            let name = entry.file_name().to_string_lossy().to_string();
            if name.starts_with('.') || name == "target" {
                continue;
            }
            let relative = match prefix.is_empty() {
                true => name,
                false => format!("{prefix}/{name}"),
            };
            match entry.path().is_dir() {
                true => stack.push((entry.path(), relative)),
                false => out.push(relative),
            }
        }
    }
    out.sort();
    out
}

/// Resolve and spawn each requested run off the UI loop, reporting every
/// result back so a refused spawn is surfaced rather than swallowed.
pub(super) async fn spawn_background_loop(
    control: ControlClient,
    mut cmd_rx: mpsc::UnboundedReceiver<SpawnCommand>,
    outcome_tx: mpsc::UnboundedSender<SpawnOutcome>,
) {
    while let Some(cmd) = cmd_rx.recv().await {
        if outcome_tx.send(run_spawn(&control, cmd).await).is_err() {
            return; // dashboard dropped the receiver
        }
    }
}

/// One spawn: resolve the blueprint locally, then hand it to the daemon.
async fn run_spawn(control: &ControlClient, cmd: SpawnCommand) -> SpawnOutcome {
    let args = match resolve_spawn_args(LaunchRequest {
        path: &cmd.agent_path,
        // Always `Some`, never `None`: `None` opens `$EDITOR`, which would
        // start a second full-screen program inside this one.
        task: Some(&cmd.task),
        stdin_is_terminal: &never_interactive,
        model: None,
        workdir: &cmd.workdir,
        yolo: cmd.yolo,
        allow: Vec::new(),
        max_depth: None,
        // Region seeds are a `lev run` command line; this screen writes a task.
        regions: HashMap::new(),
        no_seed_commands: false,
        output_request: None,
    }) {
        Ok(args) => args,
        Err(e) => {
            return SpawnOutcome {
                message: format!("Could not start '{}': {e}", cmd.agent_path),
                ok: false,
                run_id: None,
            };
        }
    };
    match control.spawn(args).await {
        Ok(ControlResponse::Spawned { run_id }) => SpawnOutcome {
            message: format!("Started {run_id}"),
            ok: true,
            run_id: Some(run_id),
        },
        Ok(ControlResponse::Error { message }) => SpawnOutcome {
            message: format!("The daemon refused the run: {message}"),
            ok: false,
            run_id: None,
        },
        Ok(other) => SpawnOutcome {
            message: format!("Unexpected daemon response to spawn: {other:?}"),
            ok: false,
            run_id: None,
        },
        Err(e) => SpawnOutcome {
            message: format!("Could not reach the daemon: {e}"),
            ok: false,
            run_id: None,
        },
    }
}

/// The production new-run context: the real agents directory, config, and
/// working directory `lev dash` was started in.
pub(super) fn production_new_run_context() -> NewRunContext {
    NewRunContext {
        agents_dir: leviath_core::paths::agents_dir().unwrap_or_default(),
        config_path: Config::config_path(),
        workdir: crate::commands::resolve_cwd().unwrap_or_default(),
    }
}

#[cfg(test)]
impl Dashboard {
    /// The retained spawn-command receiver, for asserting dispatches.
    pub(super) fn spawn_cmd_rx_for_test(&mut self) -> &mut mpsc::UnboundedReceiver<SpawnCommand> {
        &mut self
            .spawn_bg_ends
            .as_mut()
            .expect("background ends retained in tests")
            .0
    }

    /// Inject an outcome as if the background loop had produced it.
    pub(super) fn inject_spawn_outcome_for_test(&self, outcome: SpawnOutcome) {
        self.spawn_bg_ends
            .as_ref()
            .expect("background ends retained in tests")
            .1
            .send(outcome)
            .expect("outcome receiver is alive");
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::commands::dashboard::test_support::make_test_dashboard;
    use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

    /// A manifest the real parser accepts, for the agent picker's catalog.
    fn write_agent(dir: &Path, name: &str, description: &str) {
        std::fs::create_dir_all(dir).unwrap();
        std::fs::write(
            dir.join("agent.leviath"),
            format!(
                "[agent]\nname = \"{name}\"\nversion = \"0.1.0\"\n\
                 description = \"{description}\"\n\n\
                 [stages.main]\nmode = \"autonomous\"\n\n\
                 [stages.main.model]\n\
                 provider = \"anthropic\"\nmodel = \"claude-sonnet-5\"\n\n\
                 [context.regions]\n\
                 task = {{ kind = \"pinned\", max_tokens = 1000, seed = \"task\" }}\n\
                 conversation = {{ kind = \"sliding_window\", max_items = 20, \
                 max_tokens = 10000 }}\n"
            ),
        )
        .unwrap();
    }

    /// A dashboard whose new-run screen reads from `dir` only.
    fn dash_at(dir: &Path) -> Dashboard {
        let mut dash = make_test_dashboard();
        dash.new_run_ctx = NewRunContext {
            agents_dir: dir.join("agents"),
            config_path: dir.join("config.toml"),
            workdir: dir.join("work"),
        };
        std::fs::create_dir_all(dir.join("work")).unwrap();
        dash
    }

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    // ─── catalog ──────────────────────────────────────────────────────────

    #[test]
    fn opening_the_screen_loads_agents_and_files() {
        let dir = tempfile::tempdir().unwrap();
        write_agent(&dir.path().join("agents/writer"), "writer", "writes things");
        let mut dash = dash_at(dir.path());
        std::fs::write(dir.path().join("work/notes.md"), b"x").unwrap();

        dash.open_new_run_screen();
        assert!(dash.new_run_screen);
        assert_eq!(dash.new_run_focus, NewRunPane::Agents);
        let names: Vec<&str> = dash
            .new_run_agents
            .iter()
            .map(|a| a.name.as_str())
            .collect();
        assert!(
            names.contains(&"writer"),
            "installed agent listed: {names:?}"
        );
        assert_eq!(dash.new_run_files, vec!["notes.md".to_string()]);
    }

    #[test]
    fn the_catalog_includes_bundled_blueprints_once() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_at(dir.path());
        dash.refresh_new_run_agents();
        let bundled: Vec<&NewRunAgent> = dash
            .new_run_agents
            .iter()
            .filter(|a| a.source == "bundled")
            .collect();
        assert!(!bundled.is_empty(), "the binary ships blueprints");

        // Installing one of them under the same name replaces the bundled row
        // rather than adding a second one.
        let name = bundled[0].name.clone();
        write_agent(&dir.path().join("agents").join(&name), &name, "installed");
        dash.refresh_new_run_agents();
        let same: Vec<&NewRunAgent> = dash
            .new_run_agents
            .iter()
            .filter(|a| a.name == name)
            .collect();
        assert_eq!(same.len(), 1, "one row per name: {same:?}");
        assert_eq!(same[0].source, "installed");
    }

    #[test]
    fn a_local_agent_in_the_workdir_is_offered() {
        let dir = tempfile::tempdir().unwrap();
        write_agent(&dir.path().join("work"), "here", "the cwd agent");
        let mut dash = dash_at(dir.path());
        dash.refresh_new_run_agents();
        let local = dash
            .new_run_agents
            .iter()
            .find(|a| a.name == "here")
            .expect("the cwd agent is listed");
        assert_eq!(local.source, "local");
    }

    #[test]
    fn filtering_narrows_the_list_and_clamps_the_selection() {
        let dir = tempfile::tempdir().unwrap();
        write_agent(&dir.path().join("agents/alpha"), "alpha", "first");
        write_agent(&dir.path().join("agents/beta"), "beta", "second");
        let mut dash = dash_at(dir.path());
        dash.refresh_new_run_agents();

        let all = dash.filtered_new_run_agents().len();
        dash.new_run_selected = all - 1;
        dash.new_run_filter = "alpha".to_string();
        dash.clamp_new_run_selection();
        assert_eq!(dash.filtered_new_run_agents().len(), 1);
        assert_eq!(dash.new_run_selected, 0);
        assert_eq!(dash.new_run_selected_agent().unwrap().name, "alpha");

        // The description matches too, not just the name.
        dash.new_run_filter = "second".to_string();
        assert_eq!(dash.new_run_selected_agent().unwrap().name, "beta");
    }

    #[test]
    fn a_filter_matching_nothing_selects_nothing() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_at(dir.path());
        dash.refresh_new_run_agents();
        dash.new_run_filter = "no-such-agent-anywhere".to_string();
        dash.clamp_new_run_selection();
        assert!(dash.filtered_new_run_agents().is_empty());
        assert!(dash.new_run_selected_agent().is_none());
    }

    #[test]
    fn an_unreadable_config_still_lists_the_bundled_catalog() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_at(dir.path());
        // A directory where the config file should be: load fails, defaults win.
        std::fs::create_dir(dir.path().join("config.toml")).unwrap();
        dash.refresh_new_run_agents();
        assert!(dash.new_run_agents.iter().any(|a| a.source == "bundled"));
    }

    #[test]
    fn a_configured_agent_path_is_scanned() {
        let dir = tempfile::tempdir().unwrap();
        write_agent(&dir.path().join("extra/scout"), "scout", "from the config");
        let mut dash = dash_at(dir.path());
        let mut config = Config::default();
        config.agent_paths.push(dir.path().join("extra"));
        config
            .save_to_path_public(&dash.new_run_ctx.config_path)
            .unwrap();
        dash.refresh_new_run_agents();
        let scout = dash
            .new_run_agents
            .iter()
            .find(|a| a.name == "scout")
            .expect("the configured agent is listed");
        assert_eq!(scout.source, "configured");
    }

    // ─── file walking ─────────────────────────────────────────────────────

    #[test]
    fn the_walk_recurses_and_skips_dots_and_target() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        std::fs::create_dir_all(root.join("src/deep")).unwrap();
        std::fs::create_dir_all(root.join(".git")).unwrap();
        std::fs::create_dir_all(root.join("target")).unwrap();
        std::fs::write(root.join("README.md"), b"x").unwrap();
        std::fs::write(root.join("src/main.rs"), b"x").unwrap();
        std::fs::write(root.join("src/deep/mod.rs"), b"x").unwrap();
        std::fs::write(root.join(".env"), b"x").unwrap();
        std::fs::write(root.join(".git/HEAD"), b"x").unwrap();
        std::fs::write(root.join("target/binary"), b"x").unwrap();

        let files = collect_workdir_files(root, 100);
        assert_eq!(
            files,
            vec![
                "README.md".to_string(),
                "src/deep/mod.rs".to_string(),
                "src/main.rs".to_string(),
            ]
        );
    }

    #[test]
    fn the_walk_stops_at_the_cap() {
        let dir = tempfile::tempdir().unwrap();
        for i in 0..10 {
            std::fs::write(dir.path().join(format!("f{i}")), b"x").unwrap();
        }
        assert_eq!(collect_workdir_files(dir.path(), 4).len(), 4);
    }

    #[test]
    fn an_unreadable_root_yields_nothing() {
        let dir = tempfile::tempdir().unwrap();
        assert!(collect_workdir_files(&dir.path().join("nope"), 10).is_empty());
    }

    // ─── keys: agent picker ───────────────────────────────────────────────

    #[test]
    fn typing_filters_and_backspace_undoes_it() {
        let dir = tempfile::tempdir().unwrap();
        write_agent(&dir.path().join("agents/alpha"), "alpha", "first");
        let mut dash = dash_at(dir.path());
        dash.open_new_run_screen();

        dash.handle_new_run_key(key(KeyCode::Char('a')));
        dash.handle_new_run_key(key(KeyCode::Char('l')));
        assert_eq!(dash.new_run_filter, "al");
        dash.handle_new_run_key(key(KeyCode::Backspace));
        assert_eq!(dash.new_run_filter, "a");
    }

    #[test]
    fn escape_clears_the_filter_then_closes() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_at(dir.path());
        dash.open_new_run_screen();
        dash.new_run_filter = "x".to_string();

        dash.handle_new_run_key(key(KeyCode::Esc));
        assert!(dash.new_run_filter.is_empty());
        assert!(dash.new_run_screen, "the first Esc only clears the filter");

        dash.handle_new_run_key(key(KeyCode::Esc));
        assert!(!dash.new_run_screen);
    }

    #[test]
    fn arrows_move_within_the_filtered_list_only() {
        let dir = tempfile::tempdir().unwrap();
        write_agent(&dir.path().join("agents/alpha"), "alpha", "first");
        write_agent(&dir.path().join("agents/beta"), "beta", "second");
        let mut dash = dash_at(dir.path());
        dash.open_new_run_screen();
        let count = dash.filtered_new_run_agents().len();

        dash.handle_new_run_key(key(KeyCode::Down));
        assert_eq!(dash.new_run_selected, 1);
        dash.handle_new_run_key(key(KeyCode::Up));
        assert_eq!(dash.new_run_selected, 0);
        dash.handle_new_run_key(key(KeyCode::Up));
        assert_eq!(dash.new_run_selected, 0, "up at the top stays put");

        for _ in 0..count + 3 {
            dash.handle_new_run_key(key(KeyCode::Down));
        }
        assert_eq!(dash.new_run_selected, count - 1, "down stops at the end");
    }

    #[test]
    fn tab_enter_and_backtab_all_reach_the_task_editor() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_at(dir.path());
        for code in [KeyCode::Tab, KeyCode::Enter, KeyCode::BackTab] {
            dash.open_new_run_screen();
            dash.handle_new_run_key(key(code));
            assert_eq!(dash.new_run_focus, NewRunPane::Task, "{code:?}");
        }
    }

    #[test]
    fn an_unbound_picker_key_does_nothing() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_at(dir.path());
        dash.open_new_run_screen();
        dash.handle_new_run_key(key(KeyCode::F(5)));
        assert!(dash.new_run_screen);
        assert!(dash.new_run_filter.is_empty());
    }

    // ─── keys: task editor ────────────────────────────────────────────────

    #[test]
    fn the_task_editor_takes_text_and_alt_enter_breaks_the_line() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_at(dir.path());
        dash.open_new_run_screen();
        dash.new_run_focus = NewRunPane::Task;

        dash.handle_new_run_key(key(KeyCode::Char('h')));
        dash.handle_new_run_key(key(KeyCode::Char('i')));
        dash.handle_new_run_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::ALT));
        dash.handle_new_run_key(key(KeyCode::Char('!')));
        assert_eq!(
            dash.new_run_task.lines(),
            vec!["hi".to_string(), "!".to_string()]
        );
    }

    #[test]
    fn escape_and_tab_hand_focus_back_to_the_picker() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_at(dir.path());
        for code in [KeyCode::Esc, KeyCode::Tab, KeyCode::BackTab] {
            dash.open_new_run_screen();
            dash.new_run_focus = NewRunPane::Task;
            dash.handle_new_run_key(key(code));
            assert_eq!(dash.new_run_focus, NewRunPane::Agents, "{code:?}");
        }
    }

    // ─── keys: `@` completion ─────────────────────────────────────────────

    /// A dashboard sitting in the task editor with three workdir files.
    fn dash_ready_to_type(dir: &Path) -> Dashboard {
        std::fs::create_dir_all(dir.join("work/src")).unwrap();
        std::fs::write(dir.join("work/README.md"), b"x").unwrap();
        std::fs::write(dir.join("work/src/main.rs"), b"x").unwrap();
        std::fs::write(dir.join("work/src/lib.rs"), b"x").unwrap();
        let mut dash = dash_at(dir);
        dash.open_new_run_screen();
        dash.new_run_focus = NewRunPane::Task;
        dash
    }

    #[test]
    fn at_opens_the_popup_and_typing_filters_it() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_ready_to_type(dir.path());

        dash.handle_new_run_key(key(KeyCode::Char('@')));
        assert!(dash.new_run_file_ref);
        assert_eq!(dash.file_ref_matches().len(), 3, "everything, unfiltered");

        dash.handle_new_run_key(key(KeyCode::Char('l')));
        dash.handle_new_run_key(key(KeyCode::Char('i')));
        assert_eq!(dash.new_run_file_query, "li");
        assert_eq!(dash.file_ref_matches(), vec!["src/lib.rs"]);
        // The typed characters are in the task text too, so an unaccepted
        // reference still reads as what the user wrote.
        assert_eq!(dash.new_run_task.lines(), vec!["@li".to_string()]);
    }

    #[test]
    fn accepting_a_completion_replaces_the_query_with_the_path() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_ready_to_type(dir.path());
        dash.handle_new_run_key(key(KeyCode::Char('r')));
        dash.handle_new_run_key(key(KeyCode::Char('e')));
        dash.handle_new_run_key(key(KeyCode::Char('a')));
        dash.handle_new_run_key(key(KeyCode::Char('d')));
        dash.handle_new_run_key(key(KeyCode::Char(' ')));
        dash.handle_new_run_key(key(KeyCode::Char('@')));
        dash.handle_new_run_key(key(KeyCode::Char('m')));
        dash.handle_new_run_key(key(KeyCode::Char('a')));
        dash.handle_new_run_key(key(KeyCode::Enter));

        assert!(!dash.new_run_file_ref, "the popup closes");
        assert_eq!(dash.new_run_task.lines(), vec!["read @src/main.rs"]);
    }

    #[test]
    fn tab_accepts_the_highlighted_completion() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_ready_to_type(dir.path());
        dash.handle_new_run_key(key(KeyCode::Char('@')));
        dash.handle_new_run_key(key(KeyCode::Char('s')));
        dash.handle_new_run_key(key(KeyCode::Down));
        assert_eq!(dash.new_run_file_selected, 1);
        dash.handle_new_run_key(key(KeyCode::Tab));
        assert_eq!(dash.new_run_task.lines(), vec!["@src/main.rs"]);
    }

    #[test]
    fn the_popup_selection_stops_at_both_ends() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_ready_to_type(dir.path());
        dash.handle_new_run_key(key(KeyCode::Char('@')));

        dash.handle_new_run_key(key(KeyCode::Up));
        assert_eq!(dash.new_run_file_selected, 0);
        for _ in 0..10 {
            dash.handle_new_run_key(key(KeyCode::Down));
        }
        assert_eq!(dash.new_run_file_selected, 2);
    }

    #[test]
    fn accepting_with_nothing_matching_leaves_the_text_alone() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_ready_to_type(dir.path());
        dash.handle_new_run_key(key(KeyCode::Char('@')));
        dash.handle_new_run_key(key(KeyCode::Char('z')));
        assert!(dash.file_ref_matches().is_empty());
        dash.handle_new_run_key(key(KeyCode::Enter));
        assert!(!dash.new_run_file_ref);
        assert_eq!(dash.new_run_task.lines(), vec!["@z"]);
    }

    #[test]
    fn escape_closes_the_popup_without_leaving_the_editor() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_ready_to_type(dir.path());
        dash.handle_new_run_key(key(KeyCode::Char('@')));
        dash.handle_new_run_key(key(KeyCode::Esc));
        assert!(!dash.new_run_file_ref);
        assert_eq!(dash.new_run_focus, NewRunPane::Task);
        assert!(dash.new_run_screen);
    }

    #[test]
    fn backspacing_over_the_at_ends_the_reference() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_ready_to_type(dir.path());
        dash.handle_new_run_key(key(KeyCode::Char('@')));
        dash.handle_new_run_key(key(KeyCode::Char('s')));
        dash.handle_new_run_key(key(KeyCode::Down));

        dash.handle_new_run_key(key(KeyCode::Backspace));
        assert_eq!(dash.new_run_file_query, "");
        assert_eq!(
            dash.new_run_file_selected, 0,
            "the match set changed, so the highlight resets"
        );

        dash.handle_new_run_key(key(KeyCode::Backspace));
        assert!(!dash.new_run_file_ref);
        assert_eq!(dash.new_run_task.lines(), vec![""]);
    }

    #[test]
    fn an_unbound_popup_key_does_nothing() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_ready_to_type(dir.path());
        dash.handle_new_run_key(key(KeyCode::Char('@')));
        dash.handle_new_run_key(key(KeyCode::F(5)));
        assert!(dash.new_run_file_ref);
    }

    #[test]
    fn matches_are_empty_with_no_popup_open() {
        let dash = make_test_dashboard();
        assert!(dash.file_ref_matches().is_empty());
    }

    // ─── submitting ───────────────────────────────────────────────────────

    #[test]
    fn submitting_dispatches_the_run_and_closes_the_screen() {
        let dir = tempfile::tempdir().unwrap();
        write_agent(&dir.path().join("agents/alpha"), "alpha", "first");
        let mut dash = dash_at(dir.path());
        dash.open_new_run_screen();
        dash.new_run_filter = "alpha".to_string();
        dash.new_run_focus = NewRunPane::Task;
        dash.new_run_task.insert_str("  ship it  ");

        dash.handle_new_run_key(key(KeyCode::Enter));

        assert!(!dash.new_run_screen, "the screen closes on dispatch");
        let cmd = dash
            .spawn_cmd_rx_for_test()
            .try_recv()
            .expect("a spawn was dispatched");
        assert_eq!(cmd.task, "ship it", "the task is trimmed");
        assert!(cmd.agent_path.ends_with("alpha"), "got: {}", cmd.agent_path);
        assert_eq!(cmd.workdir, dir.path().join("work").display().to_string());
    }

    #[test]
    fn submitting_without_a_task_is_refused() {
        let dir = tempfile::tempdir().unwrap();
        write_agent(&dir.path().join("agents/alpha"), "alpha", "first");
        let mut dash = dash_at(dir.path());
        dash.open_new_run_screen();
        dash.new_run_focus = NewRunPane::Task;
        dash.new_run_task.insert_str("   ");

        dash.handle_new_run_key(key(KeyCode::Enter));
        assert!(dash.new_run_screen, "the screen stays open to fix it");
        assert!(dash.spawn_cmd_rx_for_test().try_recv().is_err());
        let toasts = dash.toast_messages_for_test();
        assert!(toasts.iter().any(|m| m.contains("task")), "got: {toasts:?}");
    }

    #[test]
    fn submitting_without_an_agent_is_refused() {
        let dir = tempfile::tempdir().unwrap();
        let mut dash = dash_at(dir.path());
        dash.open_new_run_screen();
        dash.new_run_filter = "no-such-agent-anywhere".to_string();
        dash.new_run_task.insert_str("do a thing");

        dash.submit_new_run();
        assert!(dash.spawn_cmd_rx_for_test().try_recv().is_err());
        let toasts = dash.toast_messages_for_test();
        assert!(
            toasts.iter().any(|m| m.contains("agent")),
            "got: {toasts:?}"
        );
    }

    #[test]
    fn spawn_outcomes_drain_into_toasts() {
        let mut dash = make_test_dashboard();
        dash.inject_spawn_outcome_for_test(SpawnOutcome {
            message: "Started run-1".to_string(),
            ok: true,
            run_id: Some("run-1".to_string()),
        });
        dash.inject_spawn_outcome_for_test(SpawnOutcome {
            message: "boom".to_string(),
            ok: false,
            run_id: None,
        });
        dash.drain_spawn_outcomes();
        assert_eq!(
            dash.toast_messages_for_test(),
            vec!["Started run-1".to_string(), "boom".to_string()]
        );
    }

    #[test]
    fn draining_with_nothing_pending_is_a_noop() {
        let mut dash = make_test_dashboard();
        dash.drain_spawn_outcomes();
        assert!(dash.toast_messages_for_test().is_empty());
    }

    // ─── the spawn lane ───────────────────────────────────────────────────

    /// A daemon that replies `reply` (verbatim, newline added) to one request.
    /// `None` closes the connection without replying.
    fn replying_daemon(
        dir: &Path,
        reply: Option<&'static str>,
    ) -> (ControlClient, tokio::task::JoinHandle<()>) {
        use leviath_runtime::control_socket::{bind_control_listener, control_id};
        use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
        let id = control_id(dir);
        let mut listener = bind_control_listener(&id).unwrap();
        let handle = tokio::spawn(async move {
            let stream = listener
                .accept()
                .await
                .expect("accept succeeds")
                .expect("our own connection is admitted");
            let (read_half, mut write_half) = tokio::io::split(stream);
            let mut lines = BufReader::new(read_half).lines();
            let _ = lines.next_line().await;
            if let Some(reply) = reply {
                let _ = write_half.write_all(format!("{reply}\n").as_bytes()).await;
            }
        });
        (ControlClient::new(id), handle)
    }

    /// Drive one spawn of a real manifest through the loop against a daemon
    /// giving `reply`, and return what the dashboard would toast.
    async fn spawn_outcome(reply: Option<&'static str>) -> SpawnOutcome {
        let dir = tempfile::tempdir().unwrap();
        write_agent(&dir.path().join("alpha"), "alpha", "first");
        let (control, server) = replying_daemon(dir.path(), reply);
        let (cmd_tx, cmd_rx) = mpsc::unbounded_channel();
        let (out_tx, mut out_rx) = mpsc::unbounded_channel();
        tokio::spawn(spawn_background_loop(control, cmd_rx, out_tx));
        cmd_tx
            .send(SpawnCommand {
                agent_path: dir.path().join("alpha").display().to_string(),
                task: "ship it".to_string(),
                workdir: dir.path().display().to_string(),
                yolo: false,
            })
            .unwrap();
        let outcome = tokio::time::timeout(std::time::Duration::from_secs(5), out_rx.recv())
            .await
            .expect("an outcome was reported")
            .expect("the loop is alive");
        let _ = server.await;
        outcome
    }

    #[tokio::test]
    async fn every_daemon_answer_is_reported() {
        let started = spawn_outcome(Some(r#"{"result":"spawned","run_id":"alpha-1"}"#)).await;
        assert!(started.ok);
        assert!(started.message.contains("alpha-1"), "{}", started.message);

        let refused =
            spawn_outcome(Some(r#"{"result":"error","message":"no such blueprint"}"#)).await;
        assert!(!refused.ok);
        assert!(
            refused.message.contains("no such blueprint"),
            "{}",
            refused.message
        );

        let odd = spawn_outcome(Some(r#"{"result":"ok","ok":true}"#)).await;
        assert!(!odd.ok);
        assert!(odd.message.contains("Unexpected"), "{}", odd.message);

        // Connection closed with no reply: a transport error, surfaced as one.
        let broken = spawn_outcome(None).await;
        assert!(!broken.ok);
        assert!(
            broken.message.contains("Could not reach the daemon"),
            "{}",
            broken.message
        );
    }

    #[tokio::test]
    async fn an_unresolvable_agent_never_reaches_the_daemon() {
        let dir = tempfile::tempdir().unwrap();
        let control = ControlClient::new(leviath_runtime::control_socket::control_id(dir.path()));
        let outcome = run_spawn(
            &control,
            SpawnCommand {
                agent_path: dir.path().join("nope").display().to_string(),
                task: "ship it".to_string(),
                workdir: dir.path().display().to_string(),
                yolo: false,
            },
        )
        .await;
        assert!(!outcome.ok);
        assert!(
            outcome.message.contains("Could not start"),
            "{}",
            outcome.message
        );
    }

    #[tokio::test]
    async fn the_loop_stops_when_the_dashboard_drops_its_receiver() {
        let dir = tempfile::tempdir().unwrap();
        let control = ControlClient::new(leviath_runtime::control_socket::control_id(dir.path()));
        let (cmd_tx, cmd_rx) = mpsc::unbounded_channel();
        let (out_tx, out_rx) = mpsc::unbounded_channel();
        drop(out_rx);
        let handle = tokio::spawn(spawn_background_loop(control, cmd_rx, out_tx));
        cmd_tx
            .send(SpawnCommand {
                agent_path: dir.path().join("nope").display().to_string(),
                task: "t".to_string(),
                workdir: dir.path().display().to_string(),
                yolo: false,
            })
            .unwrap();
        tokio::time::timeout(std::time::Duration::from_secs(5), handle)
            .await
            .expect("the loop returns once nothing is listening")
            .unwrap();
    }

    #[test]
    fn the_production_context_points_at_the_real_paths() {
        let ctx = production_new_run_context();
        assert!(ctx.config_path.ends_with("config.toml"));
        assert!(ctx.agents_dir.ends_with("agents"));
    }

    // ─── unattended runs ─────────────────────────────────────────────────────

    fn ctrl(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::CONTROL)
    }

    /// The first Ctrl-Y of a sitting warns rather than arming anything, and the
    /// warning has to be accepted before the setting changes.
    #[test]
    fn the_first_unattended_toggle_asks_before_it_arms() {
        let mut dash = make_test_dashboard();
        dash.new_run_screen = true;
        assert!(!dash.new_run_yolo, "off until somebody says otherwise");

        dash.handle_key(ctrl(KeyCode::Char('y')));
        assert!(dash.pending_confirm.is_some(), "it asks first");
        assert!(!dash.new_run_yolo, "and arms nothing until answered");

        // Declining leaves it off.
        dash.handle_key(key(KeyCode::Esc));
        assert!(dash.pending_confirm.is_none());
        assert!(!dash.new_run_yolo);

        // Accepting turns it on.
        dash.handle_key(ctrl(KeyCode::Char('y')));
        dash.handle_key(key(KeyCode::Char('y')));
        assert!(dash.new_run_yolo);

        // Turning it back off never asks: nothing needs confirming about
        // choosing to be asked more.
        dash.handle_key(ctrl(KeyCode::Char('y')));
        assert!(dash.pending_confirm.is_none());
        assert!(!dash.new_run_yolo);
    }

    /// The box silences the warning for the rest of the sitting, and only for the
    /// warning: the setting itself still has to be turned on each time.
    #[test]
    fn dont_ask_again_lasts_the_session_but_does_not_arm_anything() {
        let mut dash = make_test_dashboard();
        dash.new_run_screen = true;

        dash.handle_key(ctrl(KeyCode::Char('y')));
        // Space ticks the box, then Enter on the focused button would decline, so
        // answer explicitly.
        dash.handle_key(key(KeyCode::Char(' ')));
        dash.handle_key(key(KeyCode::Char('y')));
        assert!(dash.new_run_yolo);
        assert!(dash.yolo_warning_silenced);

        // Off, then on again: no second warning.
        dash.handle_key(ctrl(KeyCode::Char('y')));
        dash.handle_key(ctrl(KeyCode::Char('y')));
        assert!(dash.pending_confirm.is_none(), "it was told not to ask");
        assert!(dash.new_run_yolo);

        // Re-opening the screen still resets the setting, silence or not: this is
        // the half that stops somebody leaving it armed and forgetting.
        dash.open_new_run_screen();
        assert!(!dash.new_run_yolo);
        assert!(dash.yolo_warning_silenced, "but the silence holds");
    }

    /// A fresh dashboard has forgotten the silence entirely, which is what makes
    /// it a session and not a preference.
    #[test]
    fn a_new_dashboard_asks_again() {
        let mut dash = make_test_dashboard();
        dash.new_run_screen = true;
        dash.handle_key(ctrl(KeyCode::Char('y')));
        dash.handle_key(key(KeyCode::Char(' ')));
        dash.handle_key(key(KeyCode::Char('y')));
        assert!(dash.yolo_warning_silenced);

        let fresh = make_test_dashboard();
        assert!(
            !fresh.yolo_warning_silenced,
            "closing the dashboard is what expires it"
        );
        assert!(!fresh.new_run_yolo);
    }

    /// The setting reaches the spawn rather than only the screen.
    #[test]
    fn the_setting_travels_with_the_run() {
        let dir = tempfile::tempdir().unwrap();
        write_agent(&dir.path().join("agents/alpha"), "alpha", "first");
        let mut dash = dash_at(dir.path());
        dash.open_new_run_screen();
        dash.new_run_focus = NewRunPane::Task;
        dash.new_run_task.insert_str("do the thing");
        dash.new_run_yolo = true;

        dash.submit_new_run();

        let cmd = dash
            .spawn_cmd_rx_for_test()
            .try_recv()
            .expect("a run was sent");
        assert!(cmd.yolo, "the run carries what the screen was set to");
        assert_eq!(cmd.task, "do the thing");
    }
}