onerig-core 0.2.4

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
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
//! The subprocess driver. It runs one described call to its end and knows
//! nothing about harnesses or runs: it starts the child in its own process
//! group, streams its output, and kills the whole group when a limit is hit,
//! the stop flag is set, or the child is done.

use std::ffi::OsStr;
use std::io::{BufRead, BufReader, Read, Write};
use std::os::unix::process::CommandExt;
use std::path::Path;
use std::process::{Child, Command, ExitStatus, Stdio};
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender};
use std::thread;
use std::time::{Duration, Instant};

use crate::adapter::{Output, Spawn};
use crate::halt::Halt;

/// How long a child gets to leave after SIGTERM, and how long its pipes may
/// stay open after it has exited.
const GRACE: Duration = Duration::from_secs(2);
/// How often limits and the stop flag are checked.
const TICK: Duration = Duration::from_millis(20);
/// How long output is still collected after the process group is killed.
const DRAIN: Duration = Duration::from_millis(500);
const STDERR_TAIL: usize = 16 * 1024;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Limits {
    /// Longest time without a new stdout line.
    pub idle: Duration,
    /// Longest time the job may run.
    pub total: Duration,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Ended {
    Exited(Output),
    IdleTimeout(Output),
    JobTimeout(Output),
    /// The stop flag was set.
    Stopped(Output),
    SpawnFailed(String),
}

pub fn run(
    spawn: &Spawn,
    cwd: &Path,
    limits: Limits,
    stop: &Halt,
    on_spawn: &mut dyn FnMut(i32),
    on_line: &mut dyn FnMut(&str),
) -> Ended {
    if let Err(message) = write_files(spawn, cwd) {
        return Ended::SpawnFailed(message);
    }
    let mut job = match Job::start(spawn, cwd) {
        Ok(job) => job,
        Err(message) => return Ended::SpawnFailed(message),
    };
    on_spawn(job.pgid);
    let reason = job.wait_for_end(limits, stop, on_line);
    finish(job, reason, on_line)
}

fn finish(mut job: Job, reason: Reason, on_line: &mut dyn FnMut(&str)) -> Ended {
    if reason == Reason::Exited {
        signal_group(job.pgid, libc::SIGKILL);
    } else {
        job.terminate(on_line);
    }
    job.drain(on_line);
    let output = job.output();
    match reason {
        Reason::Exited => Ended::Exited(output),
        Reason::Idle => Ended::IdleTimeout(output),
        Reason::Total => Ended::JobTimeout(output),
        Reason::Stopped => Ended::Stopped(output),
    }
}

/// Relative paths are taken from `cwd`.
fn write_files(spawn: &Spawn, cwd: &Path) -> Result<(), String> {
    for (path, text) in &spawn.files {
        let full = cwd.join(path);
        let write = || -> std::io::Result<()> {
            if let Some(parent) = full.parent() {
                std::fs::create_dir_all(parent)?;
            }
            std::fs::write(&full, text)
        };
        write().map_err(|e| format!("cannot write {}: {e}", path.display()))?;
    }
    Ok(())
}

/// An exact variable name, or a prefix followed by `*`.
fn is_removed(name: &OsStr, patterns: &[String]) -> bool {
    let Some(name) = name.to_str() else {
        return false;
    };
    patterns
        .iter()
        .any(|pattern| match pattern.strip_suffix('*') {
            Some(prefix) => name.starts_with(prefix),
            None => name == pattern,
        })
}

fn signal_group(pgid: i32, signal: i32) {
    // SAFETY: killpg only sends a signal; a group that is gone gives ESRCH,
    // which needs no handling.
    unsafe {
        libc::killpg(pgid, signal);
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Reason {
    Exited,
    Idle,
    Total,
    Stopped,
}

enum Piece {
    Line(String),
    Stderr(Vec<u8>),
    Closed,
}

struct Job {
    child: Child,
    pgid: i32,
    pieces: Receiver<Piece>,
    open_pipes: u8,
    stdout: Vec<String>,
    stderr: Vec<u8>,
    status: Option<ExitStatus>,
    started: Instant,
    last_line: Instant,
    exited_at: Option<Instant>,
}

impl Job {
    fn start(spawn: &Spawn, cwd: &Path) -> Result<Job, String> {
        let mut command = Command::new(&spawn.program);
        command
            .args(&spawn.args)
            .current_dir(cwd)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .process_group(0);
        for (name, _) in std::env::vars_os() {
            if is_removed(&name, &spawn.env_remove) {
                command.env_remove(&name);
            }
        }
        command.envs(spawn.env_set.iter().map(|(k, v)| (k, v)));
        let mut child = command
            .spawn()
            .map_err(|e| format!("cannot start {}: {e}", spawn.program.display()))?;
        feed_stdin(&mut child, spawn.stdin.clone());
        let pieces = read_pipes(&mut child);
        let now = Instant::now();
        Ok(Job {
            pgid: child.id() as i32,
            child,
            pieces,
            open_pipes: 2,
            stdout: Vec::new(),
            stderr: Vec::new(),
            status: None,
            started: now,
            last_line: now,
            exited_at: None,
        })
    }

    fn wait_for_end(
        &mut self,
        limits: Limits,
        stop: &Halt,
        on_line: &mut dyn FnMut(&str),
    ) -> Reason {
        loop {
            self.step(on_line);
            if let Some(reason) = self.reason(limits, stop) {
                return reason;
            }
        }
    }

    fn step(&mut self, on_line: &mut dyn FnMut(&str)) {
        match self.pieces.recv_timeout(TICK) {
            Ok(piece) => self.take(piece, on_line),
            Err(RecvTimeoutError::Disconnected) => thread::sleep(TICK),
            Err(RecvTimeoutError::Timeout) => {}
        }
        self.poll_exit();
    }

    fn take(&mut self, piece: Piece, on_line: &mut dyn FnMut(&str)) {
        match piece {
            Piece::Line(line) => {
                on_line(&line);
                self.stdout.push(line);
                self.last_line = Instant::now();
            }
            Piece::Stderr(bytes) => {
                self.stderr.extend(bytes);
                let excess = self.stderr.len().saturating_sub(STDERR_TAIL);
                self.stderr.drain(..excess);
            }
            Piece::Closed => self.open_pipes = self.open_pipes.saturating_sub(1),
        }
    }

    fn poll_exit(&mut self) {
        if self.status.is_none()
            && let Ok(Some(status)) = self.child.try_wait()
        {
            self.status = Some(status);
            self.exited_at = Some(Instant::now());
        }
    }

    fn reason(&self, limits: Limits, stop: &Halt) -> Option<Reason> {
        let done = |at: Instant| self.open_pipes == 0 || at.elapsed() >= GRACE;
        if self.exited_at.is_some_and(done) {
            Some(Reason::Exited)
        } else if stop.is_set() {
            Some(Reason::Stopped)
        } else if self.last_line.elapsed() >= limits.idle {
            Some(Reason::Idle)
        } else if self.started.elapsed() >= limits.total {
            Some(Reason::Total)
        } else {
            None
        }
    }

    /// SIGTERM to the group, SIGKILL after the grace period; the group is
    /// killed in any case so no descendant outlives the job.
    fn terminate(&mut self, on_line: &mut dyn FnMut(&str)) {
        signal_group(self.pgid, libc::SIGTERM);
        let deadline = Instant::now() + GRACE;
        while self.status.is_none() && Instant::now() < deadline {
            self.step(on_line);
        }
        signal_group(self.pgid, libc::SIGKILL);
        if self.status.is_none() {
            self.status = self.child.wait().ok();
        }
    }

    fn drain(&mut self, on_line: &mut dyn FnMut(&str)) {
        let deadline = Instant::now() + DRAIN;
        while self.open_pipes > 0 {
            let left = deadline.saturating_duration_since(Instant::now());
            match self.pieces.recv_timeout(left) {
                Ok(piece) => self.take(piece, on_line),
                Err(_) => break,
            }
        }
    }

    fn output(self) -> Output {
        Output {
            exit_code: self.status.and_then(|status| status.code()),
            stdout: self.stdout,
            stderr_tail: String::from_utf8_lossy(&self.stderr).into_owned(),
        }
    }
}

/// Writes stdin from its own thread, so a child that never reads it cannot
/// block the job, and closes it.
fn feed_stdin(child: &mut Child, text: Option<String>) {
    let Some(mut stdin) = child.stdin.take() else {
        return;
    };
    if let Some(text) = text {
        // A child that exits without reading makes this fail with a broken
        // pipe, which is not an error of the job.
        thread::spawn(move || stdin.write_all(text.as_bytes()));
    }
}

fn read_pipes(child: &mut Child) -> Receiver<Piece> {
    let (sender, receiver) = mpsc::channel();
    if let Some(stdout) = child.stdout.take() {
        let sender = sender.clone();
        thread::spawn(move || read_lines(stdout, &sender));
    }
    if let Some(stderr) = child.stderr.take() {
        thread::spawn(move || read_chunks(stderr, &sender));
    }
    receiver
}

fn read_lines(pipe: impl Read, sender: &Sender<Piece>) {
    let mut reader = BufReader::new(pipe);
    let mut buffer = Vec::new();
    while matches!(reader.read_until(b'\n', &mut buffer), Ok(n) if n > 0) {
        let line = buffer.strip_suffix(b"\n").unwrap_or(&buffer);
        let line = line.strip_suffix(b"\r").unwrap_or(line);
        let _ = sender.send(Piece::Line(String::from_utf8_lossy(line).into_owned()));
        buffer.clear();
    }
    let _ = sender.send(Piece::Closed);
}

fn read_chunks(mut pipe: impl Read, sender: &Sender<Piece>) {
    let mut chunk = [0u8; 4096];
    while let Ok(n @ 1..) = pipe.read(&mut chunk) {
        let _ = sender.send(Piece::Stderr(chunk[..n].to_vec()));
    }
    let _ = sender.send(Piece::Closed);
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::process::{Command, Stdio};
    use std::thread;
    use std::time::Instant;

    /// Limits no passing test comes near; a regression fails instead of hanging.
    const ROOMY: Limits = Limits {
        idle: Duration::from_secs(10),
        total: Duration::from_secs(10),
    };

    struct Seen {
        ended: Ended,
        pgids: Vec<i32>,
        lines: Vec<String>,
        took: Duration,
    }

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

    fn drive(spawn: &Spawn, cwd: &Path, limits: Limits, stop: &Halt) -> Seen {
        let (mut pgids, mut lines) = (Vec::new(), Vec::new());
        let started = Instant::now();
        let ended = run(
            spawn,
            cwd,
            limits,
            stop,
            &mut |pgid| pgids.push(pgid),
            &mut |line| lines.push(line.to_owned()),
        );
        Seen {
            ended,
            pgids,
            lines,
            took: started.elapsed(),
        }
    }

    fn drive_here(spawn: &Spawn, limits: Limits) -> Seen {
        drive(spawn, Path::new("."), limits, &Halt::new())
    }

    fn exited(ended: Ended) -> Output {
        match ended {
            Ended::Exited(output) => output,
            other => panic!("expected an exit, got {other:?}"),
        }
    }

    /// Whether a process disappears within a second.
    fn is_gone(pid: &str) -> bool {
        (0..100).any(|_| {
            thread::sleep(Duration::from_millis(10));
            !Command::new("/bin/kill")
                .args(["-0", pid])
                .stderr(Stdio::null())
                .status()
                .unwrap()
                .success()
        })
    }

    #[test]
    fn a_child_that_exits_reports_its_lines_its_exit_code_and_its_stderr() {
        let spawn = sh("echo one; echo two; echo oops >&2; exit 3");

        let seen = drive_here(&spawn, ROOMY);

        assert_eq!(
            seen.ended,
            Ended::Exited(Output {
                exit_code: Some(3),
                stdout: vec!["one".into(), "two".into()],
                stderr_tail: "oops\n".into(),
            })
        );
        assert!(seen.took < DRAIN, "took {:?}", seen.took);
    }

    #[test]
    fn empty_lines_are_kept() {
        let spawn = sh("printf 'a\\n\\nb\\n'");

        let seen = drive_here(&spawn, ROOMY);

        assert_eq!(seen.lines, ["a", "", "b"]);
    }

    #[test]
    fn a_variable_name_that_is_not_utf8_is_never_removed() {
        use std::os::unix::ffi::OsStrExt;

        let removed = is_removed(OsStr::from_bytes(b"\xffNAME"), &["*".to_owned()]);

        assert!(!removed);
    }

    #[test]
    fn every_stdout_line_reaches_on_line_without_its_newline() {
        let spawn = sh("echo first; printf 'no newline at the end'");

        let seen = drive_here(&spawn, ROOMY);

        assert_eq!(seen.lines, ["first", "no newline at the end"]);
    }

    #[test]
    fn stdin_is_delivered_to_the_child_and_closed() {
        let spawn = Spawn {
            stdin: Some("alpha\nbeta\n".into()),
            ..sh("cat; echo end")
        };

        let seen = drive_here(&spawn, ROOMY);

        assert_eq!(exited(seen.ended).stdout, ["alpha", "beta", "end"]);
    }

    #[test]
    fn without_stdin_the_child_reads_end_of_file() {
        let spawn = sh("cat; echo end");

        let seen = drive_here(&spawn, ROOMY);

        assert_eq!(exited(seen.ended).stdout, ["end"]);
    }

    #[test]
    fn stdin_the_child_never_reads_does_not_block_the_job() {
        let spawn = Spawn {
            stdin: Some("x".repeat(1 << 20)),
            ..sh("exit 0")
        };

        let seen = drive_here(&spawn, ROOMY);

        assert_eq!(exited(seen.ended).exit_code, Some(0));
    }

    #[test]
    fn env_remove_drops_a_prefix_from_the_inherited_environment_and_env_set_is_applied_last() {
        assert!(
            std::env::var_os("CARGO_PKG_NAME").is_some(),
            "this test needs the variables cargo sets for a test run"
        );
        let spawn = Spawn {
            env_remove: vec!["CARGO_PKG_*".into()],
            env_set: vec![("CARGO_PKG_VERSION".into(), "set".into())],
            ..sh(
                r#"echo "${CARGO_PKG_NAME-removed} ${CARGO_PKG_VERSION-removed} ${CARGO_MANIFEST_DIR:+kept}""#,
            )
        };

        let seen = drive_here(&spawn, ROOMY);

        assert_eq!(seen.lines, ["removed set kept"]);
    }

    #[test]
    fn env_remove_matches_an_exact_name_or_the_prefix_before_a_star() {
        let patterns = ["CLAUDE_*".to_owned(), "ANTHROPIC_API_KEY".to_owned()];

        let removed = |name: &str| is_removed(OsStr::new(name), &patterns);

        assert!(removed("CLAUDE_CODE_ENTRYPOINT"));
        assert!(removed("ANTHROPIC_API_KEY"));
        assert!(!removed("ANTHROPIC_API_KEY_FILE"));
        assert!(!removed("MY_CLAUDE_HOME"));
        assert!(!removed("PATH"));
    }

    #[test]
    fn files_are_written_with_their_parent_directories_before_the_child_starts() {
        let dir = tempfile::tempdir().unwrap();
        let spawn = Spawn {
            files: vec![
                (dir.path().join("a/b/absolute.txt"), "absolute".into()),
                ("c/relative.txt".into(), "relative".into()),
            ],
            ..sh("cat a/b/absolute.txt; echo; cat c/relative.txt")
        };

        let seen = drive(&spawn, dir.path(), ROOMY, &Halt::new());

        assert_eq!(seen.lines, ["absolute", "relative"]);
    }

    #[test]
    fn a_file_that_cannot_be_written_fails_the_spawn_before_anything_starts() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("blocker"), "").unwrap();
        let spawn = Spawn {
            files: vec![("blocker/prompt.txt".into(), "x".into())],
            ..sh("exit 0")
        };

        let seen = drive(&spawn, dir.path(), ROOMY, &Halt::new());

        assert!(
            matches!(&seen.ended, Ended::SpawnFailed(message) if message.contains("blocker/prompt.txt")),
            "{:?}",
            seen.ended
        );
        assert!(seen.pgids.is_empty());
    }

    #[test]
    fn a_missing_program_is_a_spawn_failure_that_names_the_program() {
        let spawn = Spawn {
            program: "/nonexistent/onerig-harness".into(),
            ..Spawn::default()
        };

        let seen = drive_here(&spawn, ROOMY);

        assert!(
            matches!(&seen.ended, Ended::SpawnFailed(message) if message.contains("/nonexistent/onerig-harness")),
            "{:?}",
            seen.ended
        );
        assert!(seen.pgids.is_empty());
    }

    #[test]
    fn on_spawn_receives_the_process_group_the_child_leads() {
        let spawn = sh("ps -o pgid= -p $$");

        let seen = drive_here(&spawn, ROOMY);

        assert_eq!(seen.pgids.len(), 1);
        assert!(seen.pgids[0] > 0);
        assert_eq!(seen.lines[0].trim(), seen.pgids[0].to_string());
    }

    #[test]
    fn a_child_with_no_new_stdout_line_ends_as_an_idle_timeout() {
        let limits = Limits {
            idle: Duration::from_millis(300),
            ..ROOMY
        };
        let spawn = sh("echo one; while :; do echo noise >&2; sleep 0.05; done");

        let seen = drive_here(&spawn, limits);

        let Ended::IdleTimeout(output) = seen.ended else {
            panic!("expected an idle timeout, got {:?}", seen.ended);
        };
        assert_eq!(output.stdout, ["one"]);
        assert_eq!(output.exit_code, None);
        assert!(output.stderr_tail.contains("noise"));
        assert!(
            seen.took < GRACE,
            "SIGTERM alone should end it: {:?}",
            seen.took
        );
    }

    #[test]
    fn a_child_that_keeps_talking_past_the_total_limit_ends_as_a_job_timeout() {
        let limits = Limits {
            idle: Duration::from_millis(500),
            total: Duration::from_secs(1),
        };
        let spawn = sh("while :; do echo tick; sleep 0.05; done");

        let seen = drive_here(&spawn, limits);

        let Ended::JobTimeout(output) = seen.ended else {
            panic!("expected a job timeout, got {:?}", seen.ended);
        };
        assert!(output.stdout.len() > 1);
        assert!(output.stdout.iter().all(|line| line == "tick"));
        assert!(seen.took >= limits.total);
    }

    #[test]
    fn a_child_that_ignores_sigterm_is_killed_with_its_descendants_after_the_grace_period() {
        let limits = Limits {
            idle: Duration::from_millis(300),
            ..ROOMY
        };
        let spawn = sh("trap '' TERM; sleep 30 & echo $!; wait");

        let seen = drive_here(&spawn, limits);

        let Ended::IdleTimeout(output) = seen.ended else {
            panic!("expected an idle timeout, got {:?}", seen.ended);
        };
        assert_eq!(output.exit_code, None);
        assert!(seen.took >= GRACE, "took {:?}", seen.took);
        assert!(seen.took < 2 * GRACE, "took {:?}", seen.took);
        assert!(is_gone(&output.stdout[0]));
    }

    #[test]
    fn a_descendant_that_keeps_stdout_open_does_not_hang_the_job_and_is_killed() {
        let spawn = sh("sleep 30 & echo $!");

        let seen = drive_here(&spawn, ROOMY);

        let output = exited(seen.ended);
        assert_eq!(output.exit_code, Some(0));
        assert!(seen.took >= GRACE, "took {:?}", seen.took);
        assert!(seen.took < 2 * GRACE, "took {:?}", seen.took);
        assert!(is_gone(&output.stdout[0]));
    }

    #[test]
    fn the_stop_flag_set_from_another_thread_ends_the_job_as_stopped() {
        let stop = Halt::new();
        let spawn = sh("echo started; sleep 30");

        let seen = thread::scope(|scope| {
            scope.spawn(|| {
                thread::sleep(Duration::from_millis(300));
                stop.stop();
            });
            drive(&spawn, Path::new("."), ROOMY, &stop)
        });

        assert!(
            matches!(&seen.ended, Ended::Stopped(output) if output.stdout == ["started"]),
            "{:?}",
            seen.ended
        );
        assert!(seen.took < GRACE, "took {:?}", seen.took);
    }

    #[test]
    fn output_that_is_not_utf8_is_decoded_lossily() {
        let spawn = sh(r"printf '\377 out\n'; printf '\377 err' >&2");

        let seen = drive_here(&spawn, ROOMY);

        let output = exited(seen.ended);
        assert_eq!(output.stdout, ["\u{fffd} out"]);
        assert_eq!(output.stderr_tail, "\u{fffd} err");
    }

    #[test]
    fn only_the_last_16_kib_of_stderr_are_kept() {
        let spawn = sh("yes 0123456789 | head -n 3000 >&2; echo last >&2");

        let seen = drive_here(&spawn, ROOMY);

        let tail = exited(seen.ended).stderr_tail;
        assert_eq!(tail.len(), 16 * 1024);
        assert!(tail.ends_with("0123456789\nlast\n"));
    }

    #[test]
    fn a_child_killed_by_a_signal_has_no_exit_code() {
        let spawn = sh("kill -KILL $$");

        let seen = drive_here(&spawn, ROOMY);

        assert_eq!(exited(seen.ended).exit_code, None);
    }

    #[test]
    fn a_child_that_closes_its_pipes_and_runs_on_still_reports_its_exit_code() {
        let spawn = sh("exec >&- 2>&-; sleep 0.4; exit 7");

        let seen = drive_here(&spawn, ROOMY);

        assert_eq!(exited(seen.ended).exit_code, Some(7));
    }
}