overbrainer 0.9.0

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
//! The look at `overbrainer.toml` and `.env` every few seconds: when their
//! stamp changed, they are read again off the UI thread, and a valid
//! configuration is applied as a save from the Project view applies it. A
//! stage or a run already started keeps the settings it read.

use std::time::{Duration, SystemTime};

use super::app::{App, Effect, Severity};
use super::project_edit::first_of;
use super::tasks::{Checked, Reread, Task, TaskId};
use crate::config::{CONFIG_FILE, ConfigError, DotenvKeys, ReloadError, Stamp};

/// Time between two looks at the files.
pub(super) const CHECK_EVERY: Duration = Duration::from_secs(2);

/// What the look at the configuration files knows.
#[derive(Debug)]
pub(super) struct ConfigWatch {
    /// The keys `.env` set at start.
    dotenv: DotenvKeys,
    /// The stamp of the files as last read, valid or not.
    stamp: Stamp,
    /// When the last look started.
    checked: Option<SystemTime>,
    /// The look running, if any: the next one waits for its end.
    check: Option<TaskId>,
}

impl ConfigWatch {
    /// Watches files read at `stamp`; `dotenv` are the keys `.env` set at
    /// start.
    pub(super) fn new(dotenv: DotenvKeys, stamp: Stamp) -> Self {
        Self {
            dotenv,
            stamp,
            checked: None,
            check: None,
        }
    }

    /// `overbrainer.toml` was just read at `stamp` (a save, the editor, a
    /// reload): the look running, which may have read it before, is ignored.
    /// Only its half of the stamp is taken: a `.env` changed meanwhile is read
    /// by the next look.
    pub(super) fn seen(&mut self, stamp: Stamp) {
        self.stamp = self.stamp.with_config(stamp);
        self.check = None;
    }
}

impl App {
    /// Looks at the files when it is time: not while a look runs, a save
    /// runs, the editor has `overbrainer.toml`, or the TUI is leaving.
    pub(super) fn check_config(&mut self) -> Vec<Effect> {
        let now = self.now;
        let busy =
            self.leaving.is_some() || self.project_view.save.is_some() || self.project_view.editing;
        let due = self.watch.as_ref().is_some_and(|watch| {
            watch.check.is_none()
                && watch.checked.is_none_or(
                    |at| !matches!(now.duration_since(at), Ok(since) if since < CHECK_EVERY),
                )
        });
        if busy || !due {
            return Vec::new();
        }
        let id = self.task_id();
        let Some(watch) = self.watch.as_mut() else {
            return Vec::new();
        };
        watch.checked = Some(now);
        watch.check = Some(id);
        let task = Task::CheckConfig {
            seen: watch.stamp,
            dotenv: watch.dotenv.clone(),
        };
        vec![Effect::Spawn(id, task)]
    }

    /// Look `id` ended with `checked`: a new valid configuration is applied, an
    /// invalid one is said and the previous one kept. Ignored when it is not
    /// the look running, or found nothing new.
    pub(super) fn config_checked(&mut self, id: TaskId, checked: Checked) -> Vec<Effect> {
        let Some(watch) = self.watch.as_mut().filter(|watch| watch.check == Some(id)) else {
            return Vec::new();
        };
        watch.check = None;
        if checked.stamp == watch.stamp {
            return Vec::new();
        }
        watch.stamp = checked.stamp;
        match checked.read {
            None => Vec::new(),
            Some(Ok(reread)) => self.reloaded(reread),
            Some(Err(error)) => {
                self.reload_refused(&error);
                Vec::new()
            },
        }
    }

    /// Whether `id` is the look running, which failed: the next tick looks
    /// again.
    pub(super) fn check_failed(&mut self, id: TaskId) -> bool {
        match self.watch.as_mut() {
            Some(watch) if watch.check == Some(id) => {
                watch.check = None;
                true
            },
            _ => false,
        }
    }

    /// Applies the configuration read again, as a save does; `u` then has
    /// nothing to undo.
    fn reloaded(&mut self, reread: Reread) -> Vec<Effect> {
        let Reread { config, env } = reread;
        self.env = env;
        let used = self.adopt(config);
        self.project_view.undo = None;
        self.say(Severity::Info, "✓ config reloaded");
        let mut effects = vec![used];
        effects.extend(self.reload());
        effects
    }

    /// Says why the files read again cannot be used; the Project view marks
    /// the fields the problems name.
    fn reload_refused(&mut self, error: &ReloadError) {
        let said = match error {
            ReloadError::Config(ConfigError::Invalid(problems)) => {
                self.note_errors(problems);
                format!("✗ {CONFIG_FILE}: {}", first_of(problems))
            },
            // A syntax error already names the file.
            ReloadError::Config(ConfigError::Parse(problem)) if problem.contains(CONFIG_FILE) => {
                format!("✗ {problem}")
            },
            ReloadError::Config(ConfigError::Parse(problem)) => {
                format!("✗ {CONFIG_FILE}: {problem}")
            },
            ReloadError::Config(error @ ConfigError::Read { .. }) => format!("✗ {error}"),
            ReloadError::Dotenv(error) => format!("✗ {error}"),
        };
        self.say(Severity::Error, said);
    }
}

#[cfg(test)]
mod tests {
    use std::fs;
    use std::os::unix::process::ExitStatusExt as _;
    use std::path::Path;

    use crossterm::event::KeyCode;

    use super::*;
    use crate::config::{DOTENV_FILE, EnvSource, Source, stamp};
    use crate::tui::app::View;
    use crate::tui::project::{ProjectConfig, Undo};
    use crate::tui::project_edit::save_config;
    use crate::tui::snapshots::{
        NOW, PROJECT_CONFIG, SECRET, at, draw, key, project_app, project_env, text,
    };
    use crate::tui::tasks::{Done, Tasks, check_config};

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

    /// The Project view on [`PROJECT_CONFIG`], written in a project directory
    /// whose files are watched.
    fn watched() -> Result<(tempfile::TempDir, App), Box<dyn std::error::Error>> {
        let dir = tempfile::tempdir()?;
        fs::write(dir.path().join(CONFIG_FILE), PROJECT_CONFIG)?;
        let mut app = project_app()?;
        app.project.dir = dir.path().to_path_buf();
        app.env = project_env();
        app.now = at(NOW);
        app.watch = Some(ConfigWatch::new(DotenvKeys::default(), stamp(dir.path())));
        Ok((dir, app))
    }

    /// The look the tick at `seconds` starts: its ID and the stamp it knows.
    fn tick(app: &mut App, seconds: u64) -> Result<(TaskId, Stamp), String> {
        let effects = app.on_tick(at(seconds));
        match effects.as_slice() {
            [Effect::Spawn(id, Task::CheckConfig { seen, .. })] => Ok((*id, *seen)),
            _ => Err(format!("no look: {effects:?}")),
        }
    }

    /// What a look finds in `dir` once `text` replaced `overbrainer.toml`,
    /// read with the environment of the fixtures.
    fn rewritten(dir: &Path, text: &str) -> Result<Checked, Box<dyn std::error::Error>> {
        rewritten_with(dir, text, &project_env())
    }

    /// [`rewritten`], read with `env`.
    fn rewritten_with(
        dir: &Path,
        text: &str,
        env: &EnvSource,
    ) -> Result<Checked, Box<dyn std::error::Error>> {
        fs::write(dir.join(CONFIG_FILE), text)?;
        let now = stamp(dir);
        let read = ProjectConfig::new(text, env)
            .map(|mut config| {
                config.stamp = Some(now);
                Reread {
                    config,
                    env: env.clone(),
                }
            })
            .map_err(ReloadError::from);
        Ok(Checked {
            stamp: now,
            read: Some(read),
        })
    }

    fn status(app: &App) -> &str {
        app.status
            .as_ref()
            .map_or("", |status| status.text.as_str())
    }

    #[test]
    fn a_changed_file_is_read_once_and_applied() -> TestResult {
        let (dir, mut app) = watched()?;
        let (id, _) = tick(&mut app, NOW)?;
        assert_eq!(app.on_tick(at(NOW + 1)), [], "one look at a time");
        let renamed = PROJECT_CONFIG.replace("rust_expert", "rust_pro");
        let effects = app.on_done(
            id,
            Ok(Done::ConfigChecked(Box::new(rewritten(
                dir.path(),
                &renamed,
            )?))),
        );
        assert!(
            matches!(
                effects.as_slice(),
                [Effect::UseConfig(Source { text: Some(text), env }), Effect::Spawn(_, Task::Load)]
                    if *text == renamed && *env == project_env()
            ),
            "{effects:?}"
        );
        assert_eq!(status(&app), "✓ config reloaded");
        assert_eq!(app.project.name, "rust_pro");
        assert_eq!(
            app.config.as_ref().map(|config| config.text.as_str()),
            Some(renamed.as_str())
        );
        // The next look knows the new stamp: nothing to read again.
        let (_, seen) = tick(&mut app, NOW + 3)?;
        assert!(
            check_config(dir.path(), seen, &DotenvKeys::default())
                .read
                .is_none()
        );
        Ok(())
    }

    #[test]
    fn an_invalid_file_keeps_the_settings_and_marks_the_field() -> TestResult {
        let (dir, mut app) = watched()?;
        let (id, _) = tick(&mut app, NOW)?;
        let invalid = PROJECT_CONFIG.replace("subtopics = 2", "subtopics = 0");
        app.on_done(
            id,
            Ok(Done::ConfigChecked(Box::new(rewritten(
                dir.path(),
                &invalid,
            )?))),
        );
        assert!(
            status(&app).starts_with("✗ overbrainer.toml: "),
            "{}",
            status(&app)
        );
        assert_eq!(
            app.config.as_ref().map(|config| config.text.as_str()),
            Some(PROJECT_CONFIG)
        );
        assert_eq!(app.project.name, "rust_expert");
        assert!(
            app.project_view
                .errors
                .keys()
                .any(|key| key.starts_with("topics.ownership")),
            "{:?}",
            app.project_view.errors
        );
        let screen = text(&draw(&mut app, 120, 40)?).join("\n");
        assert!(screen.contains("✗ overbrainer.toml"), "{screen}");
        assert!(!screen.contains(SECRET));
        // The same invalid file is not said again.
        let (id, _) = tick(&mut app, NOW + 2)?;
        let again = Checked {
            stamp: stamp(dir.path()),
            read: None,
        };
        assert_eq!(
            app.on_done(id, Ok(Done::ConfigChecked(Box::new(again)))),
            []
        );
        Ok(())
    }

    #[tokio::test]
    async fn after_a_refused_reload_the_next_task_runs_on_the_kept_configuration() -> TestResult {
        let (dir, mut app) = watched()?;
        let mut tasks = Tasks::new(dir.path(), tokio::sync::mpsc::unbounded_channel().0);
        let mut apply = |effects: Vec<Effect>| {
            for effect in effects {
                if let Effect::UseConfig(source) = effect {
                    tasks.use_config(source);
                }
            }
        };
        apply(app.start());
        let (id, _) = tick(&mut app, NOW)?;
        let invalid = PROJECT_CONFIG.replace("subtopics = 2", "subtopics = 0");
        let checked = rewritten(dir.path(), &invalid)?;
        apply(app.on_done(id, Ok(Done::ConfigChecked(Box::new(checked)))));
        assert!(status(&app).starts_with('✗'), "{}", status(&app));
        tasks.spawn(TaskId(500), Task::Prepare);
        let next = tokio::time::timeout(std::time::Duration::from_secs(30), tasks.next()).await?;
        let Some((TaskId(500), Ok(Done::Prepared(Ok(plan))))) = next else {
            return Err(format!("not run on the kept configuration: {next:?}").into());
        };
        assert_eq!(plan.target, "gpu_cloud");
        Ok(())
    }

    #[test]
    fn a_broken_env_file_is_said_by_its_line_only() -> TestResult {
        let (dir, mut app) = watched()?;
        let (id, seen) = tick(&mut app, NOW)?;
        fs::write(
            dir.path().join(DOTENV_FILE),
            format!("OVERBRAINER_PIPELINE__CONCURRENCY=8\nBROKEN {SECRET}\n"),
        )?;
        let checked = check_config(dir.path(), seen, &DotenvKeys::default());
        app.on_done(id, Ok(Done::ConfigChecked(Box::new(checked))));
        assert_eq!(status(&app), "✗ cannot parse .env (syntax error at line 2)");
        let screen = text(&draw(&mut app, 120, 40)?).join("\n");
        assert!(!screen.contains(SECRET));
        assert_eq!(app.env, project_env(), "the previous environment is kept");
        Ok(())
    }

    #[test]
    fn a_running_stage_is_left_alone_and_the_next_tasks_get_the_new_env() -> TestResult {
        let (dir, mut app) = watched()?;
        app.pipeline_task = Some(TaskId(90));
        let (id, _) = tick(&mut app, NOW)?;
        let changed = PROJECT_CONFIG.replace("concurrency = 16", "concurrency = 12");
        let EnvSource::Vars(mut vars) = project_env() else {
            return Err("the fixtures' env is a list".into());
        };
        vars.push(("OVERBRAINER_PIPELINE__SEED".into(), "99".into()));
        let env = EnvSource::Vars(vars);
        let effects = app.on_done(
            id,
            Ok(Done::ConfigChecked(Box::new(rewritten_with(
                dir.path(),
                &changed,
                &env,
            )?))),
        );
        assert!(
            !effects
                .iter()
                .any(|effect| matches!(effect, Effect::Cancel(_) | Effect::Abandon(_)))
        );
        let used = Source {
            text: Some(changed),
            env: env.clone(),
        };
        assert!(effects.contains(&Effect::UseConfig(used)), "{effects:?}");
        assert_eq!(app.env, env);
        assert_eq!(app.pipeline_task, Some(TaskId(90)));
        Ok(())
    }

    #[test]
    fn a_reload_leaves_nothing_to_undo() -> TestResult {
        let (dir, mut app) = watched()?;
        app.project_view.undo = Some(Undo {
            before: PROJECT_CONFIG.replace("rust_expert", "rust_old"),
            after: PROJECT_CONFIG.to_string(),
        });
        let (id, _) = tick(&mut app, NOW)?;
        let renamed = PROJECT_CONFIG.replace("rust_expert", "rust_pro");
        app.on_done(
            id,
            Ok(Done::ConfigChecked(Box::new(rewritten(
                dir.path(),
                &renamed,
            )?))),
        );
        assert_eq!(status(&app), "✓ config reloaded");
        assert_eq!(app.project_view.undo, None);
        app.view = View::Project;
        assert_eq!(app.on_input(&key(KeyCode::Char('u'))), []);
        assert_eq!(status(&app), "nothing to undo");
        assert_eq!(fs::read_to_string(dir.path().join(CONFIG_FILE))?, renamed);
        Ok(())
    }

    #[test]
    fn a_save_takes_the_new_stamp_and_is_not_read_again() -> TestResult {
        let (dir, mut app) = watched()?;
        let (running, _) = tick(&mut app, NOW)?;
        let saved = save_config(
            dir.path(),
            &PROJECT_CONFIG.replace("rust_expert", "rust_pro"),
            PROJECT_CONFIG,
            &project_env(),
        )
        .map_err(|refusal| format!("{refusal:?}"))?;
        let id = app.task_id();
        app.project_view.save = Some(id);
        app.on_done(id, Ok(Done::ConfigSaved(Ok(Box::new(saved)))));
        assert_eq!(status(&app), "✓ saved overbrainer.toml");
        // The look started before the save is ignored.
        let stale = Checked {
            stamp: stamp(dir.path()),
            read: None,
        };
        assert_eq!(
            app.on_done(running, Ok(Done::ConfigChecked(Box::new(stale)))),
            []
        );
        let (_, seen) = tick(&mut app, NOW + 2)?;
        assert!(
            check_config(dir.path(), seen, &DotenvKeys::default())
                .read
                .is_none()
        );
        assert_eq!(status(&app), "✓ saved overbrainer.toml");
        Ok(())
    }

    #[test]
    fn an_env_file_changed_during_a_save_is_still_read() -> TestResult {
        let (dir, mut app) = watched()?;
        fs::write(
            dir.path().join(DOTENV_FILE),
            "OVERBRAINER_PIPELINE__SEED=7\n",
        )?;
        let saved = save_config(
            dir.path(),
            &PROJECT_CONFIG.replace("rust_expert", "rust_pro"),
            PROJECT_CONFIG,
            &project_env(),
        )
        .map_err(|refusal| format!("{refusal:?}"))?;
        let id = app.task_id();
        app.project_view.save = Some(id);
        app.on_done(id, Ok(Done::ConfigSaved(Ok(Box::new(saved)))));
        let (_, seen) = tick(&mut app, NOW)?;
        let checked = check_config(dir.path(), seen, &DotenvKeys::default());
        assert!(
            matches!(&checked.read, Some(Ok(reread)) if reread.config.settings.pipeline.seed == 7),
            "{:?}",
            checked.read.as_ref().map(Result::is_ok)
        );
        Ok(())
    }

    #[test]
    fn an_invalid_file_left_by_the_editor_is_said_once() -> TestResult {
        let (dir, mut app) = watched()?;
        app.project_view.editing = true;
        fs::write(
            dir.path().join(CONFIG_FILE),
            PROJECT_CONFIG.replace("subtopics = 2", "subtopics = 0"),
        )?;
        let exited = std::process::ExitStatus::from_raw(0);
        assert_eq!(app.on_editor_exit(Ok(exited)), []);
        assert!(status(&app).contains("subtopics"), "{}", status(&app));
        assert!(
            app.project_view
                .errors
                .keys()
                .any(|key| key.starts_with("topics.ownership")),
            "{:?}",
            app.project_view.errors
        );
        let (_, seen) = tick(&mut app, NOW)?;
        assert!(
            check_config(dir.path(), seen, &DotenvKeys::default())
                .read
                .is_none(),
            "not read again"
        );
        Ok(())
    }

    #[test]
    fn a_toml_syntax_error_is_said_without_the_file_name_twice() -> TestResult {
        let (dir, mut app) = watched()?;
        let (id, _) = tick(&mut app, NOW)?;
        let checked = rewritten(dir.path(), "[project\nname = 1\n")?;
        app.on_done(id, Ok(Done::ConfigChecked(Box::new(checked))));
        assert_eq!(
            status(&app).matches(CONFIG_FILE).count(),
            1,
            "{}",
            status(&app)
        );
        assert!(status(&app).starts_with("✗ TOML syntax error in overbrainer.toml"));
        Ok(())
    }

    #[test]
    fn no_look_while_a_save_runs_or_the_editor_has_the_file() -> TestResult {
        let (_dir, mut app) = watched()?;
        app.project_view.save = Some(TaskId(50));
        assert_eq!(app.on_tick(at(NOW)), []);
        app.project_view.save = None;
        app.project_view.editing = true;
        assert_eq!(app.on_tick(at(NOW + 5)), []);
        app.project_view.editing = false;
        tick(&mut app, NOW + 6)?;
        Ok(())
    }
}