overbrainer 0.8.1

Distill a large LLM into a smaller open-weights model: generate questions, collect answers, fine-tune, visualize.
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
//! Auto mode: `A`, or `auto` in the `r` menu, asks once, then runs every
//! stage, each once the one before ended without failed items, then starts a
//! training run as `t` does and follows it. A failure stops the chain at its
//! link; running auto again resumes it, as the stages skip what is done.

use crossterm::event::KeyCode;

use super::app::{Action, App, Confirm, Effect, Overlay, Severity, View};
use super::pipeline::command_name;
use super::start::{self, AutoPlan, Catalog};
use super::tasks::{Task, TaskId, TrainJob};
use crate::cli::data::Command;
use crate::config::CONFIG_FILE;

/// The stages auto mode runs, in order, before training.
pub(super) const STAGES: [Command; 4] = [
    Command::Subtopics,
    Command::Questions,
    Command::Answers,
    Command::Split,
];

/// The name of the chain's last link.
const TRAIN: &str = "train";

/// What `c` in the Pipeline view says once the chain trains.
const TRAINING_CANCEL: &str = "auto mode is training: c in the Training view cancels the run";

/// Where a link of the chain is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Mark {
    /// Not reached.
    Pending,
    /// Running.
    Running,
    /// Ended well.
    Done,
    /// Failed or cancelled: the chain stopped there.
    Stopped,
}

/// How the chain is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ChainState {
    /// A link runs.
    Running,
    /// Every link ended well.
    Done,
    /// A link failed, or was cancelled: the chain stopped there.
    Stopped,
}

/// Auto mode running, or how it last ended.
#[derive(Debug, Clone, PartialEq)]
pub(super) struct Chain {
    /// What runs after split.
    pub(super) plan: AutoPlan,
    /// The link running, or where the chain stopped: a position in
    /// [`STAGES`], then training.
    pub(super) at: usize,
    /// How it is.
    pub(super) state: ChainState,
    /// The training task, once started.
    pub(super) train_task: Option<TaskId>,
}

impl Chain {
    fn new(plan: AutoPlan) -> Self {
        Self {
            plan,
            at: 0,
            state: ChainState::Running,
            train_task: None,
        }
    }

    /// Whether a link still runs.
    pub(super) fn running(&self) -> bool {
        self.state == ChainState::Running
    }

    /// Whether it runs a stage, not training.
    fn in_stages(&self) -> bool {
        self.running() && self.at < STAGES.len()
    }

    /// The links, each with where it is: the stages, then `train` when
    /// training is configured.
    pub(super) fn links(&self) -> Vec<(&'static str, Mark)> {
        let mut names: Vec<&'static str> = STAGES.iter().map(|c| command_name(*c)).collect();
        if self.plan.run.is_some() {
            names.push(TRAIN);
        }
        names
            .into_iter()
            .enumerate()
            .map(|(at, name)| {
                let mark = match (self.state, at.cmp(&self.at)) {
                    (ChainState::Done, _) | (_, std::cmp::Ordering::Less) => Mark::Done,
                    (_, std::cmp::Ordering::Greater) => Mark::Pending,
                    (ChainState::Running, _) => Mark::Running,
                    (ChainState::Stopped, _) => Mark::Stopped,
                };
                (name, mark)
            })
            .collect()
    }
}

/// Auto mode's state in the app.
#[derive(Debug, Clone, Default, PartialEq)]
pub(super) struct Auto {
    /// The task preparing its confirmation, if any.
    pub(super) prepare: Option<TaskId>,
    /// The chain running, or the last one.
    pub(super) chain: Option<Chain>,
}

impl Auto {
    /// Whether a chain runs.
    pub(super) fn running(&self) -> bool {
        self.chain.as_ref().is_some_and(Chain::running)
    }

    /// The chain's training task, while it runs.
    pub(super) fn train_task(&self) -> Option<TaskId> {
        self.chain
            .as_ref()
            .filter(|chain| chain.running())
            .and_then(|chain| chain.train_task)
    }
}

/// The confirmation text of `plan`: the stages, then the run's, as `t`
/// shows it, with the GPU types of the catalog once looked up.
pub(super) fn text(plan: &AutoPlan, gpus: Option<&Catalog>) -> Vec<String> {
    let mut text = vec![
        "stages      subtopics, questions, answers, split: each starts once the one before \
         ended without failed items; what is done is skipped"
            .to_string(),
    ];
    match &plan.run {
        None => text.push(format!(
            "training    none: no [training] in {CONFIG_FILE}, so auto mode stops after split"
        )),
        Some((run, _)) => {
            text.push("training    then a run starts, as t starts it:".to_string());
            text.extend(start::text_after_split(run, gpus));
        },
    }
    text
}

/// The dialog asking to run `plan`.
pub(super) fn dialog(plan: Box<AutoPlan>, gpus: Option<&Catalog>) -> Confirm {
    Confirm {
        title: " Run auto mode? ".to_string(),
        text: text(&plan, gpus),
        yes: "run",
        no: "cancel",
        action: Action::Auto(plan),
    }
}

impl App {
    /// `A`, or `auto` in the `r` menu: prepares the confirmation of auto
    /// mode, in a task; refused while a chain, a stage, an edit or a start
    /// runs, or while the TUI quits.
    pub(super) fn ask_auto(&mut self) -> Vec<Effect> {
        if self.auto.running() {
            self.say(Severity::Info, "auto mode is already running");
            return Vec::new();
        }
        if self.refuse_new("one task at a time", "stage") {
            return Vec::new();
        }
        if self.auto.prepare.is_some() {
            self.say(Severity::Info, "already preparing auto mode");
            return Vec::new();
        }
        let id = self.task_id();
        self.auto.prepare = Some(id);
        vec![Effect::Spawn(id, Task::PrepareAuto)]
    }

    /// The confirmation of auto mode is ready: it opens, and the GPU catalog
    /// of a Runpod run is read meanwhile. Dropped once the TUI quits, and
    /// while something else is open (the status line then says to press `A`
    /// again).
    pub(super) fn auto_prepared(&mut self, plan: Result<AutoPlan, String>) -> Vec<Effect> {
        self.auto.prepare = None;
        if self.leaving.is_some() {
            return Vec::new();
        }
        let plan = match plan {
            Ok(plan) => plan,
            Err(error) => {
                self.say(Severity::Warn, format!("refused: {error}"));
                return Vec::new();
            },
        };
        if self.overlay.is_some() || self.dataset.input.is_some() {
            self.say(
                Severity::Info,
                "auto mode is prepared: press A again to run it",
            );
            return Vec::new();
        }
        let gpu_count = plan
            .run
            .as_ref()
            .and_then(|(run, _)| run.runpod.as_ref())
            .map(|runpod| runpod.spec.gpu_count);
        self.overlay = Some(Overlay::Confirm(dialog(Box::new(plan), None)));
        self.start_catalog = None;
        self.start_gpus = None;
        let Some(gpu_count) = gpu_count else {
            return Vec::new();
        };
        let id = self.task_id();
        self.start_catalog = Some(id);
        vec![Effect::Spawn(id, Task::StartCatalog(gpu_count))]
    }

    /// `y` on the confirmation: the chain starts with its first stage.
    pub(super) fn confirm_auto(&mut self, plan: AutoPlan) -> Vec<Effect> {
        let effects = self.run_pipeline(STAGES[0]);
        if effects.is_empty() {
            return effects;
        }
        self.auto.chain = Some(Chain::new(plan));
        effects
    }

    /// A pipeline task ended, its outcome in the Pipeline view: the chain's
    /// next link starts when its stage ended well; a failure, or the TUI
    /// quitting, stops it. The caller has reloaded the data.
    pub(super) fn auto_stage_ended(&mut self) -> Vec<Effect> {
        let Some(chain) = self.auto.chain.as_ref().filter(|chain| chain.in_stages()) else {
            return Vec::new();
        };
        let (at, run) = (chain.at, chain.plan.run.clone());
        let stage = command_name(STAGES[at]);
        let error = match &self.pipeline.outcome {
            Some(Ok(())) if self.leaving.is_none() => None,
            Some(Err(error)) => Some(error.clone()),
            _ => Some("the TUI is quitting".to_string()),
        };
        if let Some(error) = error {
            self.chain_ended(ChainState::Stopped);
            if self.leaving.is_none() {
                self.say(
                    Severity::Error,
                    format!("auto stopped at {stage}: {error}; A resumes it"),
                );
            }
            return Vec::new();
        }
        if let Some(chain) = self.auto.chain.as_mut() {
            chain.at = at + 1;
        }
        let effects = match (STAGES.get(at + 1), run) {
            (Some(next), _) => self.run_pipeline(*next),
            (None, None) => {
                self.chain_ended(ChainState::Done);
                self.say(
                    Severity::Info,
                    format!("✓ auto done after split: no [training] in {CONFIG_FILE}"),
                );
                return Vec::new();
            },
            (None, Some((run, _))) => self.start_run(&run),
        };
        let task = effects.iter().find_map(|effect| match effect {
            Effect::Spawn(id, Task::Train(TrainJob::Start { .. }) | Task::Pipeline(_)) => Some(*id),
            _ => None,
        });
        let Some(task) = task else {
            // Refused: the status line already says why.
            self.chain_ended(ChainState::Stopped);
            return effects;
        };
        if at + 1 < STAGES.len() {
            return effects;
        }
        if let Some(chain) = self.auto.chain.as_mut() {
            chain.train_task = Some(task);
        }
        let mut effects = effects;
        effects.extend(self.show(View::Training));
        effects
    }

    /// The chain ends in `state`.
    fn chain_ended(&mut self, state: ChainState) {
        if let Some(chain) = self.auto.chain.as_mut() {
            chain.state = state;
        }
    }

    /// Training task `id` ended on run `run`, failing with `error`, or
    /// stopped with a snapshot at step `stopped_at`: the chain ends there when
    /// it is its run, saying where the model is, or how to resume it.
    pub(super) fn auto_trained(
        &mut self,
        id: TaskId,
        run: &str,
        error: Option<&str>,
        stopped_at: Option<u64>,
    ) {
        if self.auto.train_task() != Some(id) {
            return;
        }
        let Some(chain) = self.auto.chain.as_mut() else {
            return;
        };
        let leaving = self.leaving.is_some();
        let said = match (error, &chain.plan.run) {
            // Stopped with a snapshot: the model is not finished.
            (None, Some(_)) if !leaving && stopped_at.is_some() => {
                chain.state = ChainState::Stopped;
                (
                    Severity::Info,
                    format!(
                        "auto stopped at {TRAIN}: snapshot at step {}; T resumes it",
                        stopped_at.unwrap_or_default()
                    ),
                )
            },
            (None, Some((_, outputs))) if !leaving => {
                chain.state = ChainState::Done;
                let paths: Vec<String> = outputs
                    .paths(run)
                    .into_iter()
                    .enumerate()
                    .map(|(at, (what, path))| {
                        if at == 0 {
                            path
                        } else {
                            format!("{what} {path}")
                        }
                    })
                    .collect();
                (Severity::Info, format!("✓ auto done: {}", paths.join(", ")))
            },
            (Some(error), _) => {
                chain.state = ChainState::Stopped;
                (Severity::Warn, format!("auto stopped at {TRAIN}: {error}"))
            },
            _ => {
                chain.state = ChainState::Stopped;
                return;
            },
        };
        if !leaving {
            self.say(said.0, said.1);
        }
    }

    /// A key in the Pipeline view: `c` asks to cancel the chain while it runs
    /// a stage.
    pub(super) fn on_pipeline_key(&mut self, code: KeyCode) {
        if code != KeyCode::Char('c') {
            return;
        }
        let Some(chain) = self.auto.chain.as_ref().filter(|chain| chain.running()) else {
            return;
        };
        if !chain.in_stages() {
            self.say(Severity::Info, TRAINING_CANCEL);
            return;
        }
        let stage = command_name(STAGES[chain.at]);
        self.overlay = Some(Overlay::Confirm(Confirm {
            title: " Cancel auto mode? ".to_string(),
            text: vec![format!(
                "{stage} stops now (what it finished is kept) and the rest of the chain does \
                 not run. A runs it again from where it stopped."
            )],
            yes: "cancel it",
            no: "keep going",
            action: Action::CancelAuto,
        }));
    }

    /// `y` on the cancel dialog: the chain stops, and its stage with it.
    pub(super) fn cancel_auto(&mut self) -> Vec<Effect> {
        let Some(chain) = self.auto.chain.as_mut().filter(|chain| chain.in_stages()) else {
            // The chain reached training while the dialog was open.
            if self.auto.running() {
                self.say(Severity::Info, TRAINING_CANCEL);
            }
            return Vec::new();
        };
        chain.state = ChainState::Stopped;
        let stage = command_name(STAGES[chain.at]);
        self.say(
            Severity::Info,
            format!("auto cancelled: {stage} stops; A resumes it"),
        );
        self.pipeline_task
            .map(|id| vec![Effect::Cancel(id)])
            .unwrap_or_default()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::train::Outputs;
    use crate::tui::snapshots::{app, key, runpod_plan};
    use crate::tui::tasks::Done;

    type TestResult = Result<(), Box<dyn std::error::Error>>;

    fn press(app: &mut App, codes: &[KeyCode]) -> Vec<Effect> {
        codes
            .iter()
            .flat_map(|code| app.on_input(&key(*code)))
            .collect()
    }

    /// The tasks `effects` spawn, but the reads of the data, the runs and the
    /// compares.
    fn spawned(effects: &[Effect]) -> Vec<(TaskId, Task)> {
        effects
            .iter()
            .filter_map(|effect| match effect {
                Effect::Spawn(id, task)
                    if !matches!(task, Task::Load | Task::Runs | Task::Compares) =>
                {
                    Some((*id, task.clone()))
                },
                _ => None,
            })
            .collect()
    }

    /// A plan with a local run, or none.
    fn plan(training: bool) -> AutoPlan {
        let mut run = runpod_plan();
        run.runpod = None;
        run.target = "local".into();
        run.kind = "local, docker".into();
        AutoPlan {
            run: training.then(|| {
                (
                    Box::new(run),
                    Outputs {
                        adapter: crate::config::Adapter::Qlora,
                        merge: true,
                    },
                )
            }),
        }
    }

    /// An app whose `A` was answered with `plan` and `y`: the first stage is
    /// spawned. Returns it.
    fn confirmed(app: &mut App, plan: AutoPlan) -> Result<TaskId, String> {
        app.view = View::Pipeline;
        let effects = press(app, &[KeyCode::Char('A')]);
        let [(prepare, Task::PrepareAuto)] = spawned(&effects)[..] else {
            return Err(format!("{effects:?}"));
        };
        app.on_done(prepare, Ok(Done::PreparedAuto(Ok(plan))));
        let effects = press(app, &[KeyCode::Char('y')]);
        match spawned(&effects)[..] {
            [(id, Task::Pipeline(Command::Subtopics))] => Ok(id),
            _ => Err(format!("{effects:?}")),
        }
    }

    /// Ends pipeline task `id` with `outcome`; returns the task spawned next.
    fn end(app: &mut App, id: TaskId, outcome: Result<(), String>) -> Vec<(TaskId, Task)> {
        spawned(&app.on_done(id, Ok(Done::Pipeline(outcome))))
    }

    fn status(app: &App) -> String {
        app.status
            .as_ref()
            .map(|s| s.text.clone())
            .unwrap_or_default()
    }

    #[test]
    fn the_confirmation_lists_the_stages_then_the_run() {
        let with = text(&plan(true), None);
        assert!(with[0].starts_with("stages      subtopics, questions, answers, split"));
        assert_eq!(with[1], "training    then a run starts, as t starts it:");
        assert!(
            with.iter()
                .any(|line| line.starts_with("target      local"))
        );
        assert!(with.iter().any(|line| line.contains("rebuilt by split")));
        let without = text(&plan(false), None);
        assert_eq!(
            without[1],
            "training    none: no [training] in overbrainer.toml, so auto mode stops after split"
        );
    }

    #[test]
    fn the_chain_runs_each_stage_after_the_last_then_trains() -> TestResult {
        let mut app = app();
        let mut id = confirmed(&mut app, plan(true))?;
        for next in [Command::Questions, Command::Answers, Command::Split] {
            let spawned = end(&mut app, id, Ok(()));
            let [(next_id, Task::Pipeline(command))] = spawned[..] else {
                return Err(format!("{spawned:?}").into());
            };
            assert_eq!(command, next);
            id = next_id;
        }
        let spawned = end(&mut app, id, Ok(()));
        let [(train, Task::Train(TrainJob::Start { .. }))] = spawned[..] else {
            return Err(format!("{spawned:?}").into());
        };
        assert_eq!(
            app.view,
            View::Training,
            "training start shows the Training view"
        );
        app.on_message(crate::tui::tasks::Msg::Report(
            train,
            crate::cli::front::Report::RunCreated("r7".into()),
        ));
        app.on_done(train, Ok(Done::Trained(Ok(()))));
        assert_eq!(
            status(&app),
            "✓ auto done: runs/r7/output, merged model runs/r7/output/merged"
        );
        assert!(!app.auto.running());
        Ok(())
    }

    #[test]
    fn a_run_stopped_with_a_snapshot_stops_the_chain_and_says_how_to_resume() -> TestResult {
        let mut app = app();
        let mut id = confirmed(&mut app, plan(true))?;
        for _ in 0..3 {
            let spawned = end(&mut app, id, Ok(()));
            let [(next, Task::Pipeline(_))] = spawned[..] else {
                return Err(format!("{spawned:?}").into());
            };
            id = next;
        }
        let spawned = end(&mut app, id, Ok(()));
        let [(train, Task::Train(TrainJob::Start { .. }))] = spawned[..] else {
            return Err(format!("{spawned:?}").into());
        };
        app.on_message(crate::tui::tasks::Msg::Report(
            train,
            crate::cli::front::Report::RunCreated("r7".into()),
        ));
        app.on_message(crate::tui::tasks::Msg::Report(
            train,
            crate::cli::front::Report::RunStopped(1240),
        ));
        app.on_done(train, Ok(Done::Trained(Ok(()))));
        assert_eq!(
            status(&app),
            "auto stopped at train: snapshot at step 1240; T resumes it"
        );
        assert!(!app.auto.running());
        let chain = app.auto.chain.as_ref().ok_or("no chain")?;
        assert_eq!(chain.state, ChainState::Stopped);
        Ok(())
    }

    #[test]
    fn a_stage_that_failed_stops_the_chain_with_its_error() -> TestResult {
        let mut app = app();
        let id = confirmed(&mut app, plan(true))?;
        let spawned = end(&mut app, id, Err("2 items failed".into()));
        assert!(spawned.is_empty(), "{spawned:?}");
        assert_eq!(
            status(&app),
            "auto stopped at subtopics: 2 items failed; A resumes it"
        );
        let chain = app.auto.chain.as_ref().ok_or("no chain")?;
        assert_eq!(chain.links()[0], ("subtopics", Mark::Stopped));
        assert_eq!(chain.links()[1], ("questions", Mark::Pending));
        Ok(())
    }

    #[test]
    fn without_training_the_chain_stops_after_split_and_says_so() -> TestResult {
        let mut app = app();
        let mut id = confirmed(&mut app, plan(false))?;
        for _ in 0..3 {
            let spawned = end(&mut app, id, Ok(()));
            let [(next, Task::Pipeline(_))] = spawned[..] else {
                return Err(format!("{spawned:?}").into());
            };
            id = next;
        }
        assert_eq!(
            end(&mut app, id, Ok(())),
            [] as [(crate::tui::tasks::TaskId, crate::tui::tasks::Task); 0]
        );
        assert_eq!(
            status(&app),
            "✓ auto done after split: no [training] in overbrainer.toml"
        );
        let chain = app.auto.chain.as_ref().ok_or("no chain")?;
        assert_eq!(chain.links().len(), 4);
        assert!(chain.links().iter().all(|(_, mark)| *mark == Mark::Done));
        Ok(())
    }

    #[test]
    fn c_cancels_the_running_stage_and_the_rest_after_asking() -> TestResult {
        let mut app = app();
        let id = confirmed(&mut app, plan(true))?;
        assert_eq!(
            press(&mut app, &[KeyCode::Char('c'), KeyCode::Char('n')]),
            []
        );
        assert!(app.auto.running(), "n keeps it going");
        let effects = press(&mut app, &[KeyCode::Char('c'), KeyCode::Char('y')]);
        assert_eq!(effects, [Effect::Cancel(id)]);
        let spawned = end(&mut app, id, Err("interrupted".into()));
        assert!(spawned.is_empty(), "{spawned:?}");
        assert!(!app.auto.running());
        Ok(())
    }

    #[test]
    fn a_cancel_confirmed_once_training_started_says_where_to_cancel() -> TestResult {
        let mut app = app();
        confirmed(&mut app, plan(true))?;
        press(&mut app, &[KeyCode::Char('c')]);
        let chain = app.auto.chain.as_mut().ok_or("no chain")?;
        chain.at = STAGES.len();
        chain.train_task = Some(TaskId(99));
        assert_eq!(press(&mut app, &[KeyCode::Char('y')]), []);
        assert_eq!(status(&app), TRAINING_CANCEL);
        assert!(app.auto.running());
        Ok(())
    }

    #[test]
    fn a_is_refused_while_a_stage_runs_and_its_failure_is_said() -> TestResult {
        let mut app = app();
        confirmed(&mut app, plan(true))?;
        assert_eq!(press(&mut app, &[KeyCode::Char('A')]), []);
        assert_eq!(status(&app), "auto mode is already running");

        let mut app = crate::tui::snapshots::app();
        app.view = View::Project;
        let effects = press(&mut app, &[KeyCode::Char('A')]);
        let [(prepare, Task::PrepareAuto)] = spawned(&effects)[..] else {
            return Err(format!("{effects:?}").into());
        };
        app.on_done(
            prepare,
            Ok(Done::PreparedAuto(Err("invalid configuration".into()))),
        );
        assert_eq!(status(&app), "refused: invalid configuration");
        assert!(app.overlay.is_none());
        Ok(())
    }

    #[test]
    fn the_pipeline_view_draws_the_chain() -> TestResult {
        let mut app = app();
        app.view = View::Pipeline;
        // The fixture runs answers, after subtopics and questions.
        crate::tui::snapshots::pipeline_running(&mut app);
        app.auto.chain = Some(Chain {
            at: 2,
            ..Chain::new(plan(true))
        });
        crate::tui::snapshots::snapshot("pipeline_auto_running", &mut app)?;
        let task = app.pipeline_task.ok_or("no stage runs")?;
        end(&mut app, task, Err("1 item failed".into()));
        assert_eq!(
            status(&app),
            "auto stopped at answers: 1 item failed; A resumes it"
        );
        crate::tui::snapshots::snapshot("pipeline_auto_stopped", &mut app)?;
        Ok(())
    }

    #[test]
    fn the_confirmation_is_drawn_with_the_run_and_its_cost() -> TestResult {
        let mut app = app();
        app.view = View::Pipeline;
        let effects = press(&mut app, &[KeyCode::Char('A')]);
        let [(prepare, Task::PrepareAuto)] = spawned(&effects)[..] else {
            return Err(format!("{effects:?}").into());
        };
        let plan = AutoPlan {
            run: Some((
                Box::new(runpod_plan()),
                Outputs {
                    adapter: crate::config::Adapter::Lora,
                    merge: false,
                },
            )),
        };
        let effects = app.on_done(prepare, Ok(Done::PreparedAuto(Ok(plan))));
        let [(catalog, Task::StartCatalog(1))] = spawned(&effects)[..] else {
            return Err(format!("{effects:?}").into());
        };
        app.on_done(
            catalog,
            Ok(Done::StartCatalog(crate::tui::snapshots::looked_up(Ok(
                crate::tui::snapshots::gpu_types()?,
            )))),
        );
        crate::tui::snapshots::snapshot("auto_dialog", &mut app)?;
        Ok(())
    }

    #[test]
    fn opening_in_auto_mode_prepares_its_confirmation() {
        let mut app = app();
        app.opening = crate::tui::app::Opening::Auto;
        let effects = app.start();
        assert!(
            spawned(&effects)
                .iter()
                .any(|(_, task)| *task == Task::PrepareAuto)
        );
        let mut app = crate::tui::snapshots::app();
        assert!(
            spawned(&app.start()).is_empty(),
            "the Project view: nothing more"
        );
    }
}