onerig-core 0.2.2

Core library of onerig: run state machine, lane policy and harness adapters.
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
//! The real `Runner`: one adapter per lane, run through the process driver.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::time::Duration;

use crate::adapter::{Adapter, Output, Spawn};
use crate::halt::Halt;
use crate::process::{self, Ended, Limits};
use crate::runner::{
    FailureKind, HarnessEvent, Job, Outcome, Probe, ProbeError, QuotaReport, Runner,
};

/// Probes and quota calls must answer within this time.
const PROBE_LIMIT: Duration = Duration::from_secs(30);

pub struct HarnessRunner {
    adapters: BTreeMap<String, Box<dyn Adapter>>,
    limits: Limits,
    stop: Halt,
    /// Working directory of probe and quota calls: the main checkout.
    probe_cwd: PathBuf,
}

impl HarnessRunner {
    pub fn new(
        adapters: BTreeMap<String, Box<dyn Adapter>>,
        limits: Limits,
        stop: Halt,
        probe_cwd: PathBuf,
    ) -> HarnessRunner {
        HarnessRunner {
            adapters,
            limits,
            stop,
            probe_cwd,
        }
    }

    /// Runs a probe or quota call; `Err` when it gives no output to judge.
    fn quick(&self, spawn: &Spawn) -> Result<Output, String> {
        let limits = Limits {
            idle: PROBE_LIMIT,
            total: PROBE_LIMIT,
        };
        match process::run(
            spawn,
            &self.probe_cwd,
            limits,
            &self.stop,
            &mut |_| {},
            &mut |_| {},
        ) {
            Ended::Exited(output) => Ok(output),
            Ended::SpawnFailed(message) => Err(message),
            Ended::IdleTimeout(_) | Ended::JobTimeout(_) => Err(format!(
                "`{}` did not answer within {} s",
                spawn.program.display(),
                PROBE_LIMIT.as_secs()
            )),
            Ended::Stopped(_) => Err("stopped".to_string()),
        }
    }
}

impl Runner for HarnessRunner {
    fn preflight(&mut self, lane: &str) -> Result<Probe, ProbeError> {
        let adapter = self
            .adapters
            .get(lane)
            .ok_or_else(|| ProbeError::Missing(format!("lane {lane} has no adapter")))?;
        let outputs = adapter
            .probe_spawns()
            .iter()
            .map(|spawn| self.quick(spawn))
            .collect::<Result<Vec<_>, _>>()
            .map_err(ProbeError::Missing)?;
        adapter.probe_result(&outputs)
    }

    fn quota(&mut self, lane: &str) -> Option<QuotaReport> {
        let adapter = self.adapters.get(lane)?;
        let output = self.quick(&adapter.quota_spawn()?).ok()?;
        adapter.quota_result(&output)
    }

    fn run(
        &mut self,
        lane: &str,
        job: &Job,
        on_spawn: &mut dyn FnMut(i32),
        on_event: &mut dyn FnMut(HarnessEvent),
    ) -> Outcome {
        let Some(adapter) = self.adapters.get(lane) else {
            return Outcome::failed(FailureKind::Unknown, format!("lane {lane} has no adapter"));
        };
        let spawn = adapter.spawn(job);
        let ended = process::run(
            &spawn,
            &job.cwd,
            self.limits,
            &self.stop,
            on_spawn,
            &mut |line| {
                adapter.parse(line).into_iter().for_each(&mut *on_event);
            },
        );
        let (outcome, output) = judge(adapter.as_ref(), job, ended, self.limits);
        if let (Outcome::Failed { .. }, Some(output)) = (&outcome, &output) {
            // The raw output only helps a person diagnose the failure; not
            // being able to keep it changes nothing about the run.
            let _ = write_raw_log(&job.raw_log, output);
        }
        outcome
    }
}

/// The outcome of an ended job, with its output when there is one.
fn judge(
    adapter: &dyn Adapter,
    job: &Job,
    ended: Ended,
    limits: Limits,
) -> (Outcome, Option<Output>) {
    match ended {
        Ended::Exited(output) => (adapter.outcome(job, &output), Some(output)),
        Ended::IdleTimeout(output) => (
            Outcome::failed(
                FailureKind::Timeout,
                format!("no output for {}", minutes(limits.idle)),
            ),
            Some(output),
        ),
        Ended::JobTimeout(output) => (
            Outcome::failed(
                FailureKind::Timeout,
                format!("still running after {}", minutes(limits.total)),
            ),
            Some(output),
        ),
        Ended::Stopped(output) => (
            Outcome::failed(FailureKind::Unknown, "stopped".to_string()),
            Some(output),
        ),
        Ended::SpawnFailed(message) => (Outcome::failed(FailureKind::Unknown, message), None),
    }
}

fn minutes(duration: Duration) -> String {
    let seconds = duration.as_secs();
    if seconds >= 60 && seconds.is_multiple_of(60) {
        format!("{} min", seconds / 60)
    } else {
        format!("{seconds} s")
    }
}

fn write_raw_log(path: &Path, output: &Output) -> std::io::Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let exit = output.exit_code.map_or_else(
        || "none (killed by a signal)".to_string(),
        |code| code.to_string(),
    );
    let text = format!(
        "exit code: {exit}\n--- stdout ---\n{}\n--- stderr (tail) ---\n{}",
        output.stdout.join("\n"),
        output.stderr_tail
    );
    std::fs::write(path, text)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runner::Role;
    use std::sync::Mutex;

    /// Runs `sh -c <script>`; each stdout line becomes a text event, and the
    /// outcome is Done for exit 0.
    struct Shell {
        script: &'static str,
        probes: Vec<&'static str>,
        quota: Option<&'static str>,
    }

    fn sh(script: &str) -> Spawn {
        Spawn {
            program: "/bin/sh".into(),
            args: vec!["-c".into(), script.into()],
            ..Spawn::default()
        }
    }

    impl Adapter for Shell {
        fn probe_spawns(&self) -> Vec<Spawn> {
            self.probes.iter().map(|script| sh(script)).collect()
        }

        fn probe_result(&self, outputs: &[Output]) -> Result<Probe, ProbeError> {
            Ok(Probe {
                version: outputs
                    .iter()
                    .flat_map(|o| o.stdout.clone())
                    .collect::<Vec<_>>()
                    .join("+"),
            })
        }

        fn quota_spawn(&self) -> Option<Spawn> {
            self.quota.map(sh)
        }

        fn quota_result(&self, output: &Output) -> Option<QuotaReport> {
            Some(QuotaReport {
                remaining_fraction: output.stdout.first()?.parse().ok()?,
                reset_at: None,
            })
        }

        fn spawn(&self, _job: &Job) -> Spawn {
            sh(self.script)
        }

        fn parse(&self, line: &str) -> Vec<HarnessEvent> {
            vec![HarnessEvent::Text {
                text: line.to_string(),
            }]
        }

        fn outcome(&self, _job: &Job, output: &Output) -> Outcome {
            match output.exit_code {
                Some(0) => Outcome::Done {
                    text: output.stdout.join("\n"),
                    structured: None,
                },
                _ => Outcome::failed(FailureKind::Unknown, "exit".to_string()),
            }
        }
    }

    fn runner(adapter: Shell, limits: Limits) -> HarnessRunner {
        let adapters: BTreeMap<String, Box<dyn Adapter>> =
            BTreeMap::from([("lane".to_string(), Box::new(adapter) as Box<dyn Adapter>)]);
        HarnessRunner::new(adapters, limits, Halt::new(), std::env::temp_dir())
    }

    fn shell(script: &'static str) -> Shell {
        Shell {
            script,
            probes: vec![],
            quota: None,
        }
    }

    const ROOMY: Limits = Limits {
        idle: Duration::from_secs(10),
        total: Duration::from_secs(10),
    };

    fn job(dir: &Path) -> Job {
        Job {
            role: Role::Worker,
            prompt: String::new(),
            cwd: dir.to_path_buf(),
            run_dir: dir.to_path_buf(),
            raw_log: dir.join("raw-1.log"),
            schema: None,
        }
    }

    #[test]
    fn a_job_streams_its_events_reports_its_group_and_ends_with_the_adapters_outcome() {
        let dir = tempfile::tempdir().unwrap();
        let events = Mutex::new(Vec::new());
        let mut groups = Vec::new();

        let outcome = runner(shell("echo one; echo two"), ROOMY).run(
            "lane",
            &job(dir.path()),
            &mut |pgid| groups.push(pgid),
            &mut |event| events.lock().unwrap().push(event),
        );

        assert_eq!(
            outcome,
            Outcome::Done {
                text: "one\ntwo".into(),
                structured: None
            }
        );
        assert_eq!(events.lock().unwrap().len(), 2);
        assert_eq!(groups.len(), 1);
        assert!(!dir.path().join("raw-1.log").exists());
    }

    #[test]
    fn a_failed_job_keeps_its_raw_output() {
        let dir = tempfile::tempdir().unwrap();

        let outcome = runner(shell("echo out; echo err >&2; exit 4"), ROOMY).run(
            "lane",
            &job(dir.path()),
            &mut |_| {},
            &mut |_| {},
        );

        assert!(matches!(
            outcome,
            Outcome::Failed {
                kind: FailureKind::Unknown,
                ..
            }
        ));
        let raw = std::fs::read_to_string(dir.path().join("raw-1.log")).unwrap();
        assert_eq!(
            raw,
            "exit code: 4\n--- stdout ---\nout\n--- stderr (tail) ---\nerr\n"
        );
    }

    #[test]
    fn a_silent_job_times_out_without_asking_the_adapter() {
        let dir = tempfile::tempdir().unwrap();
        let limits = Limits {
            idle: Duration::from_millis(200),
            total: Duration::from_secs(10),
        };

        let outcome = runner(shell("sleep 5"), limits).run(
            "lane",
            &job(dir.path()),
            &mut |_| {},
            &mut |_| {},
        );

        assert_eq!(
            outcome,
            Outcome::failed(FailureKind::Timeout, "no output for 0 s".to_string())
        );
        assert!(dir.path().join("raw-1.log").exists());
    }

    #[test]
    fn a_lane_without_an_adapter_fails_its_probe_and_its_jobs() {
        let dir = tempfile::tempdir().unwrap();
        let mut runner = runner(shell("true"), ROOMY);

        let probe = runner.preflight("other");
        let outcome = runner.run("other", &job(dir.path()), &mut |_| {}, &mut |_| {});

        assert!(matches!(probe, Err(ProbeError::Missing(_))));
        assert!(matches!(
            outcome,
            Outcome::Failed {
                kind: FailureKind::Unknown,
                ..
            }
        ));
        assert_eq!(runner.quota("other"), None);
    }

    #[test]
    fn probes_pass_every_output_to_the_adapter() {
        let adapter = Shell {
            probes: vec!["echo 1.2", "echo ok"],
            ..shell("true")
        };

        let probe = runner(adapter, ROOMY).preflight("lane");

        assert_eq!(
            probe,
            Ok(Probe {
                version: "1.2+ok".into()
            })
        );
    }

    #[test]
    fn a_probe_program_that_cannot_start_means_the_harness_is_missing() {
        let adapter = Shell {
            probes: vec![],
            ..shell("true")
        };
        let mut runner = runner(adapter, ROOMY);
        runner
            .adapters
            .insert("missing".into(), Box::new(Missing) as Box<dyn Adapter>);

        assert!(
            matches!(runner.preflight("missing"), Err(ProbeError::Missing(m)) if m.contains("onerig-no-such-harness"))
        );
    }

    #[test]
    fn quota_comes_from_the_adapters_quota_call() {
        let adapter = Shell {
            quota: Some("echo 0.25"),
            ..shell("true")
        };
        let mut runner = runner(adapter, ROOMY);

        assert_eq!(
            runner.quota("lane"),
            Some(QuotaReport {
                remaining_fraction: 0.25,
                reset_at: None
            })
        );
    }

    #[test]
    fn durations_read_in_minutes_when_they_are_whole_minutes() {
        assert_eq!(minutes(Duration::from_secs(300)), "5 min");
        assert_eq!(minutes(Duration::from_secs(90)), "90 s");
    }

    struct Missing;

    impl Adapter for Missing {
        fn probe_spawns(&self) -> Vec<Spawn> {
            vec![Spawn {
                program: "/nonexistent/onerig-no-such-harness".into(),
                ..Spawn::default()
            }]
        }
        fn probe_result(&self, _: &[Output]) -> Result<Probe, ProbeError> {
            unreachable!("a spawn failure never reaches the adapter")
        }
        fn quota_spawn(&self) -> Option<Spawn> {
            None
        }
        fn quota_result(&self, _: &Output) -> Option<QuotaReport> {
            None
        }
        fn spawn(&self, _: &Job) -> Spawn {
            Spawn::default()
        }
        fn parse(&self, _: &str) -> Vec<HarnessEvent> {
            vec![]
        }
        fn outcome(&self, _: &Job, _: &Output) -> Outcome {
            Outcome::failed(FailureKind::Unknown, String::new())
        }
    }

    fn can_move_to_a_thread<T: Send>() {}

    #[test]
    fn runners_can_move_to_a_worker_thread() {
        can_move_to_a_thread::<HarnessRunner>();
        can_move_to_a_thread::<crate::fake::FakeRunner>();
    }
}