mxc-sdk 1.2.0

Importable library for starting MXC sandboxes in-process, with streaming stdio.
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Streaming (handle-based) API tests: live stdio, kill, and wait.
//! Seatbelt-specific cases run only on macOS.
//!
//! These drive the real consumer path by constructing a [`ContainerRequest`]
//! and passing it to `mxc_sdk::v1::spawn`.

#![cfg(target_os = "macos")]

mod inherited_stdio_contract;
mod unix_pty_contract;

use mxc_sdk::v1::{
    spawn, spawn_with_pty, ContainerRequest, FilesystemPolicy, SpawnWithPtyOptions, WaitResult,
};

#[test]
fn seatbelt_inherited_stdio_uses_caller_stdio() {
    inherited_stdio_contract::assert_uses_caller_stdio(
        "seatbelt_inherited_stdio_uses_caller_stdio",
        |command| seatbelt_request(&command, 30_000),
        |marker, exit_code| {
            format!(
                "IFS= read -r line; printf '{marker}_OUT:%s\\n' \"$line\"; printf '{marker}_ERR\\n' >&2; exit {exit_code}"
            )
        },
    );
}

/// A Seatbelt streaming request (`/tmp` read-write) with the given command and
/// timeout (ms; `0` == run until exit, required for interactive/long cases).
#[cfg(target_os = "macos")]
fn seatbelt_request(command: &str, timeout_ms: u32) -> ContainerRequest {
    ContainerRequest {
        filesystem: Some(FilesystemPolicy {
            readwrite_paths: vec!["/tmp".to_string()],
            readonly_paths: vec![],
            denied_paths: vec![],
            clear_policy_on_exit: None,
        }),
        timeout_ms: (timeout_ms != 0).then_some(timeout_ms),
        ..ContainerRequest::new(command)
    }
}

#[cfg(target_os = "macos")]
#[test]
fn streaming_double_take_returns_none() {
    let mut proc = spawn(seatbelt_request("cat", 0), Default::default()).expect("spawn");

    assert!(
        proc.take_stdin().is_some(),
        "first take_stdin yields the pipe"
    );
    assert!(proc.take_stdin().is_none(), "second take_stdin yields None");
    assert!(
        proc.take_stdout().is_some(),
        "first take_stdout yields the pipe"
    );
    assert!(
        proc.take_stdout().is_none(),
        "second take_stdout yields None"
    );
    assert!(
        proc.take_stderr().is_some(),
        "first take_stderr yields the pipe"
    );
    assert!(
        proc.take_stderr().is_none(),
        "second take_stderr yields None"
    );

    proc.kill().expect("kill");
    let _ = proc.wait();
}

#[test]
fn seatbelt_pty_supports_io_resize_and_merged_output() {
    unix_pty_contract::assert_round_trip(seatbelt_request(
        unix_pty_contract::ROUND_TRIP_COMMAND,
        30_000,
    ));
}

#[test]
fn seatbelt_pty_enforces_script_timeout() {
    unix_pty_contract::assert_timeout(
        seatbelt_request(unix_pty_contract::TIMEOUT_COMMAND, 1_000),
        std::time::Duration::from_secs(15),
    );
}

#[test]
fn seatbelt_pty_preserves_explicit_timeout_kill() {
    unix_pty_contract::assert_explicit_timeout_kill(seatbelt_request(
        unix_pty_contract::TIMEOUT_COMMAND,
        30_000,
    ));
}

#[test]
fn seatbelt_pty_transfers_native_stdio() {
    unix_pty_contract::assert_native_stdio(seatbelt_request(
        unix_pty_contract::NATIVE_STDIO_COMMAND,
        30_000,
    ));
}

#[cfg(target_os = "macos")]
#[test]
fn streaming_try_wait_reports_exit_after_completion() {
    let mut proc = spawn(seatbelt_request("true", 0), Default::default()).expect("spawn");

    // Poll try_wait until the quick command exits; it must then report Some.
    let mut code = None;
    for _ in 0..100 {
        if let Some(c) = proc.try_wait().expect("try_wait") {
            code = Some(c);
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(50));
    }
    let code = code.expect("process should exit and try_wait report it");
    assert_eq!(code, 0, "quick command should exit 0");
}

#[test]
fn streaming_kill_after_reap_is_a_noop() {
    // Regression: once the child has exited and been reaped (here via `wait`),
    // `kill()` must not signal its pid/pgid again — a recycled pid could belong
    // to an unrelated process (group). The post-reap `kill()` is a clean no-op.
    let mut proc = spawn(seatbelt_request("true", 0), Default::default()).expect("spawn");
    assert_eq!(
        proc.wait().expect("wait"),
        WaitResult::Exited(0),
        "quick command should exit 0"
    );
    proc.kill().expect("kill after reap is a no-op Ok");
    proc.kill().expect("repeat kill after reap stays Ok");
}

#[test]
fn streaming_kill_after_try_wait_reap_is_a_noop() {
    // The exact race the review flagged: `try_wait()` reaps the exited child, so
    // a later `kill()` must not signal the now-recycled pid/pgid. Poll try_wait
    // to completion, then `kill()` must stay a clean no-op.
    let mut proc = spawn(seatbelt_request("true", 0), Default::default()).expect("spawn");
    let mut reaped = false;
    for _ in 0..100 {
        if proc.try_wait().expect("try_wait").is_some() {
            reaped = true;
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(50));
    }
    assert!(
        reaped,
        "quick command should exit and try_wait should reap it"
    );
    proc.kill().expect("kill after try_wait reap is a no-op Ok");
    proc.kill().expect("repeat kill stays Ok");
}

#[test]
fn streaming_double_kill_before_wait_completes_promptly() {
    // Calling `kill()` twice before `wait()` must be stable (both Ok), and
    // `wait()` must then complete promptly rather than hang.
    let mut proc = spawn(seatbelt_request("sleep 30", 0), Default::default()).expect("spawn");
    proc.kill().expect("first kill");
    proc.kill().expect("second kill stays Ok");
    let start = std::time::Instant::now();
    let _ = proc.wait();
    assert!(
        start.elapsed() < std::time::Duration::from_secs(5),
        "wait() after a double kill should complete promptly, took {:?}",
        start.elapsed()
    );
}

#[cfg(target_os = "macos")]
#[test]
fn streaming_stdout_closer_unblocks_parked_read_without_killing() {
    use std::io::Read;

    // `sleep` produces no output yet holds its stdout pipe write-end open, so a
    // read parks indefinitely (mirroring a backgrounded descendant that keeps a
    // pipe open past the foreground command's exit). The stdout closer must EOF
    // that read promptly *without* terminating the still-running child — a plain
    // `kill()` would defeat the point.
    let mut proc = spawn(seatbelt_request("sleep 30", 0), Default::default()).expect("spawn");

    let mut stdout = proc.take_stdout().expect("stdout available");
    // The closer is valid even though stdout has already been taken.
    let closer = proc.stdout_closer().expect("stdout closer available");
    assert!(
        proc.stderr_closer().is_some(),
        "stderr closer should also be available in pipes mode"
    );

    // Park a blocking read on a worker thread; with the writer held open it
    // cannot return on its own.
    let reader = std::thread::spawn(move || {
        let mut buf = [0u8; 64];
        let start = std::time::Instant::now();
        let n = stdout.read(&mut buf).expect("read returns");
        (n, start.elapsed())
    });

    // Let the read park, confirm the child is still running, then close.
    std::thread::sleep(std::time::Duration::from_millis(200));
    assert!(
        proc.try_wait().expect("try_wait").is_none(),
        "child should still be running while the read is parked"
    );
    closer.close();

    let (n, elapsed) = reader.join().expect("reader thread");
    assert_eq!(n, 0, "closed stream reports EOF");
    assert!(
        elapsed < std::time::Duration::from_secs(10),
        "read should return promptly after close (elapsed: {elapsed:?})"
    );

    // The closer must not have terminated the child.
    assert!(
        proc.try_wait().expect("try_wait").is_none(),
        "stdout_closer must not terminate the child"
    );

    // A second close is a harmless no-op.
    closer.close();

    proc.kill().expect("kill");
    let _ = proc.wait();
}

#[cfg(target_os = "macos")]
#[test]
fn streaming_wait_discards_untaken_streams() {
    let mut proc = spawn(seatbelt_request("echo streamed-out", 0), Default::default())
        .expect("spawn should succeed");
    // Take nothing -> wait() drains and discards the output, returning only
    // the exit code.
    assert_eq!(
        proc.wait().expect("wait should succeed"),
        WaitResult::Exited(0)
    );
}

#[cfg(target_os = "macos")]
#[test]
fn streaming_wait_with_output_captures_both_streams() {
    // wait_with_output drains stdout and stderr concurrently, so a child that
    // writes to both is captured without the take-both deadlock foot-gun.
    let proc = spawn(
        seatbelt_request("echo to-out; echo to-err 1>&2", 0),
        Default::default(),
    )
    .expect("spawn should succeed");
    let output = proc
        .wait_with_output()
        .expect("wait_with_output should succeed");
    assert_eq!(output.outcome, WaitResult::Exited(0));
    assert!(
        String::from_utf8_lossy(&output.stdout).contains("to-out"),
        "stdout: {:?}",
        String::from_utf8_lossy(&output.stdout)
    );
    assert!(
        String::from_utf8_lossy(&output.stderr).contains("to-err"),
        "stderr: {:?}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[cfg(target_os = "macos")]
#[test]
fn streaming_bidirectional_stdio() {
    use std::io::{Read, Write};

    // `cat` echoes stdin to stdout until EOF, then exits.
    let mut proc = spawn(seatbelt_request("cat", 0), Default::default()).expect("spawn");

    let mut stdin = proc.take_stdin().expect("stdin available");
    let mut stdout = proc.take_stdout().expect("stdout available");

    stdin.write_all(b"ping-pong\n").expect("write stdin");
    drop(stdin); // close -> cat sees EOF and exits

    let mut out = String::new();
    stdout.read_to_string(&mut out).expect("read stdout");
    assert!(out.contains("ping-pong"), "got: {:?}", out);

    assert_eq!(proc.wait().expect("wait"), WaitResult::Exited(0));
}

#[cfg(target_os = "macos")]
#[test]
fn streaming_kill_terminates_process() {
    let mut proc = spawn(seatbelt_request("sleep 30", 0), Default::default()).expect("spawn");

    // Still running shortly after spawn.
    assert!(proc.try_wait().expect("try_wait").is_none());

    proc.kill().expect("kill should succeed");

    // After kill, the process must be reapable and not report success.
    assert_ne!(
        proc.wait().expect("wait after kill"),
        WaitResult::Exited(0),
        "killed process should not exit 0"
    );
}

#[cfg(target_os = "macos")]
#[test]
fn streaming_kill_terminates_forked_descendant_quickly() {
    // Regression for the early-kill race: when the shell *forks* the inner
    // command (`echo` then `sleep`), an early `kill()` could SIGTERM the shell
    // (which dies) before the just-forked `sleep` joined the group — leaving
    // `sleep` alive and the follow-up `wait()` blocking for its full runtime.
    // The whole tree must die promptly regardless.
    let mut proc =
        spawn(seatbelt_request("echo hi; sleep 30", 0), Default::default()).expect("spawn");

    proc.kill().expect("kill should succeed");

    let start = std::time::Instant::now();
    let _ = proc.wait();
    assert!(
        start.elapsed() < std::time::Duration::from_secs(10),
        "wait() must return promptly after kill(), not wait out the child's \
         30s runtime (elapsed: {:?})",
        start.elapsed()
    );
}

#[cfg(target_os = "macos")]
fn pid_alive(pid: u32) -> bool {
    // Signal 0 probes existence without delivering a signal — no PID-reuse
    // race from spawning `ps`, and no false "dead" if the probe itself fails.
    let rc = unsafe { libc::kill(pid as libc::pid_t, 0) };
    if rc == 0 {
        return true;
    }
    // ESRCH => no such process (dead). Any other errno (e.g. EPERM: the pid
    // exists but we may not signal it) means it is still alive.
    std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH)
}

#[cfg(target_os = "macos")]
fn assert_pty_job_control_tree_is_killed(timeout_ms: u32, explicit_kill: bool) {
    use std::io::{BufRead, BufReader};

    let terminal = spawn_with_pty(
        seatbelt_request(
            "exec /bin/bash -c 'set -m; trap \"\" HUP; sleep 300 & echo CHILD=$!; wait'",
            timeout_ms,
        ),
        SpawnWithPtyOptions::default(),
    )
    .expect("spawn PTY job-control shell");
    let reader = terminal.try_clone_reader().expect("PTY reader");
    let closer = terminal.stdout_closer().expect("PTY reader closer");
    let (sender, receiver) = std::sync::mpsc::sync_channel(1);
    let reader_thread = std::thread::spawn(move || {
        let mut reader = BufReader::new(reader);
        let mut line = String::new();
        let mut readiness_sent = false;
        loop {
            line.clear();
            match reader.read_line(&mut line) {
                Ok(0) => {
                    if !readiness_sent {
                        let _ = sender.send(Err("PTY output ended before CHILD=<pid>".to_string()));
                    }
                    return;
                }
                Ok(_) => {
                    if !readiness_sent {
                        if let Some(value) = line.trim().strip_prefix("CHILD=") {
                            let _ = sender.send(
                                value
                                    .parse::<u32>()
                                    .map_err(|error| format!("invalid descendant pid: {error}")),
                            );
                            readiness_sent = true;
                        }
                    }
                }
                Err(error) => {
                    if !readiness_sent {
                        let _ = sender.send(Err(format!("failed to read descendant pid: {error}")));
                    }
                    return;
                }
            }
        }
    });
    let descendant = match receiver.recv_timeout(std::time::Duration::from_secs(10)) {
        Ok(Ok(descendant)) => descendant,
        Ok(Err(error)) => {
            closer.close();
            let _ = terminal.kill();
            reader_thread.join().expect("PTY reader thread");
            panic!("{error}");
        }
        Err(error) => {
            closer.close();
            let _ = terminal.kill();
            reader_thread.join().expect("PTY reader thread");
            panic!("timed out waiting for PTY job-control readiness: {error}");
        }
    };
    // SAFETY: `getpgid` only queries the process identified by `descendant`.
    let descendant_group = unsafe { libc::getpgid(descendant as libc::pid_t) };
    assert!(descendant_group > 0, "descendant process group");
    assert_ne!(
        descendant_group as u32,
        terminal.id(),
        "job control must place the descendant outside the shell's process group"
    );
    let terminal = std::sync::Arc::new(terminal);
    let leader = terminal.id();
    let waiter = {
        let terminal = std::sync::Arc::clone(&terminal);
        std::thread::spawn(move || {
            if explicit_kill {
                terminal.kill().expect("kill PTY session");
            }
            terminal.wait()
        })
    };
    let (sender, receiver) = std::sync::mpsc::sync_channel(1);
    std::thread::spawn(move || {
        let _ = sender.send(waiter.join());
    });
    let outcome = match receiver.recv_timeout(std::time::Duration::from_secs(20)) {
        Ok(Ok(Ok(outcome))) => outcome,
        Ok(Ok(Err(error))) => {
            closer.close();
            reader_thread.join().expect("PTY reader thread");
            panic!("PTY termination failed: {error}");
        }
        Ok(Err(_)) => {
            closer.close();
            let _ = terminal.kill();
            reader_thread.join().expect("PTY reader thread");
            panic!("PTY termination thread panicked");
        }
        Err(error) => {
            closer.close();
            // SAFETY: these are the two process IDs created and observed by this test.
            unsafe {
                libc::kill(descendant as libc::pid_t, libc::SIGKILL);
                libc::kill(leader as libc::pid_t, libc::SIGKILL);
            }
            reader_thread.join().expect("PTY reader thread");
            panic!("PTY termination did not complete within 20 seconds: {error}");
        }
    };
    closer.close();
    reader_thread.join().expect("PTY reader thread");
    if explicit_kill {
        assert_ne!(outcome, WaitResult::Exited(0));
    } else {
        assert_eq!(outcome, WaitResult::TimedOut);
    }

    for _ in 0..60 {
        if !pid_alive(leader) && !pid_alive(descendant) {
            return;
        }
        std::thread::sleep(std::time::Duration::from_millis(50));
    }
    panic!(
        "PTY termination left processes alive: leader={leader} alive={}, descendant={descendant} \
         alive={}",
        pid_alive(leader),
        pid_alive(descendant)
    );
}

#[cfg(target_os = "macos")]
#[test]
fn seatbelt_pty_kill_terminates_job_control_groups() {
    assert_pty_job_control_tree_is_killed(0, true);
}

#[cfg(target_os = "macos")]
#[test]
fn seatbelt_pty_timeout_terminates_job_control_groups() {
    assert_pty_job_control_tree_is_killed(1_000, false);
}

#[cfg(target_os = "macos")]
#[test]
fn seatbelt_pty_kill_sweeps_session_after_leader_exit() {
    use std::io::{BufRead, BufReader};

    let terminal = spawn_with_pty(
        seatbelt_request(
            "exec /bin/bash -c 'set -m; trap \"\" HUP; sleep 300 & echo CHILD=$!; exit 0'",
            0,
        ),
        SpawnWithPtyOptions::default(),
    )
    .expect("spawn PTY job-control shell");
    let mut reader = BufReader::new(terminal.try_clone_reader().expect("PTY reader"));
    let mut line = String::new();
    let descendant = loop {
        line.clear();
        reader.read_line(&mut line).expect("read descendant pid");
        if let Some(value) = line.trim().strip_prefix("CHILD=") {
            break value.parse::<u32>().expect("descendant pid");
        }
    };
    for _ in 0..100 {
        if terminal.try_wait().expect("poll leader").is_some() {
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(10));
    }
    assert!(
        terminal.try_wait().expect("confirm leader exit").is_some(),
        "PTY leader did not exit"
    );

    terminal.kill().expect("sweep surviving PTY session");
    for _ in 0..60 {
        if !pid_alive(descendant) {
            return;
        }
        std::thread::sleep(std::time::Duration::from_millis(50));
    }
    panic!("job-control descendant {descendant} survived leader exit and kill");
}

#[cfg(target_os = "macos")]
#[test]
fn streaming_kill_terminates_process_tree() {
    use std::io::{BufRead, BufReader};

    // The sandboxed shell backgrounds a `sleep` (a descendant), prints its
    // pid, then blocks. `kill()` must take the whole process group down,
    // including that descendant.
    let mut proc = spawn(
        seatbelt_request("sleep 300 & echo CHILD=$!; sleep 300", 0),
        Default::default(),
    )
    .expect("spawn");

    assert!(proc.id() > 0, "id() should expose the child pid");

    let stdout = proc.take_stdout().expect("stdout");
    let mut reader = BufReader::new(stdout);
    let mut line = String::new();
    reader.read_line(&mut line).expect("read descendant pid");
    let descendant: u32 = line
        .trim()
        .strip_prefix("CHILD=")
        .expect("CHILD= prefix")
        .parse()
        .expect("descendant pid");

    assert!(
        pid_alive(descendant),
        "descendant {descendant} should be running before kill"
    );

    proc.kill().expect("kill");
    let _ = proc.wait();

    let mut gone = false;
    for _ in 0..60 {
        if !pid_alive(descendant) {
            gone = true;
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(50));
    }
    assert!(
        gone,
        "descendant {descendant} should be killed with the process tree"
    );
}

#[cfg(target_os = "macos")]
#[test]
fn streaming_timeout_kills_process_tree() {
    use std::io::{BufRead, BufReader};

    // 1s timeout; the shell backgrounds a long sleep (descendant), prints its
    // pid, then blocks past the timeout. wait()'s timeout branch must group-
    // kill, taking the descendant down too.
    let mut proc = spawn(
        seatbelt_request("sleep 300 & echo CHILD=$!; sleep 300", 1000),
        Default::default(),
    )
    .expect("spawn");

    let stdout = proc.take_stdout().expect("stdout");
    let mut reader = BufReader::new(stdout);
    let mut line = String::new();
    reader.read_line(&mut line).expect("read descendant pid");
    let descendant: u32 = line
        .trim()
        .strip_prefix("CHILD=")
        .expect("CHILD= prefix")
        .parse()
        .expect("descendant pid");

    assert_eq!(
        proc.wait().expect("wait yields an outcome"),
        WaitResult::TimedOut,
        "timed-out process should report a timeout"
    );

    let mut gone = false;
    for _ in 0..60 {
        if !pid_alive(descendant) {
            gone = true;
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(50));
    }
    assert!(gone, "descendant {descendant} should be killed on timeout");
}

#[cfg(target_os = "macos")]
#[test]
fn streaming_wait_returns_when_descendant_holds_not_taken_stream_open() {
    // The foreground command exits immediately, but a backgrounded `sleep`
    // inherits and holds stdout's write end open. We take NOTHING, so `wait()`
    // drains stdout/stderr itself; with no timeout (wait-forever) it must still
    // return promptly once the foreground child exits — the held-open descendant
    // pipe must not wedge the discard drain (cr-002 regression).
    let mut proc = spawn(seatbelt_request("sleep 30 & exit 0", 0), Default::default())
        .expect("spawn should succeed");

    let start = std::time::Instant::now();
    assert_eq!(
        proc.wait().expect("wait should return"),
        WaitResult::Exited(0),
        "foreground command exits 0"
    );
    assert!(
        start.elapsed() < std::time::Duration::from_secs(10),
        "wait() must return promptly, not block on the descendant's 30s pipe hold \
         (elapsed: {:?})",
        start.elapsed()
    );
}

#[cfg(target_os = "macos")]
#[test]
fn streaming_honors_sub_500ms_timeout() {
    // A sub-500ms timeout used to be rejected outright; it must now be accepted
    // and enforced (cr-011), and fire with low latency (cr-016). `sleep 30`
    // exceeds it, so wait() reports a timeout promptly.
    let mut proc = spawn(seatbelt_request("sleep 30", 200), Default::default()).expect("spawn");
    let start = std::time::Instant::now();
    assert_eq!(
        proc.wait().expect("wait yields an outcome"),
        WaitResult::TimedOut,
        "sub-500ms timeout should fire"
    );
    assert!(
        start.elapsed() < std::time::Duration::from_secs(5),
        "timeout should fire near 200ms, not wait out the 30s sleep (elapsed: {:?})",
        start.elapsed()
    );
}

#[test]
fn seatbelt_capture_failure_after_timeout_is_a_warning() {
    // The workload removes the report directory, so the report write fails
    // after the timeout. The timeout stays the result; the failure is a warning.
    let dir = tempfile::tempdir_in("/private/tmp").expect("tempdir");
    let out = dir.path().join("out");
    std::fs::create_dir(&out).expect("report directory");
    let request = serde_json::json!({
        "version": "1.1.0-alpha",
        "containment": "seatbelt",
        "process": {
            "commandLine": format!("rmdir {} && exec sleep 30", out.display()),
            "timeout": 1000
        },
        "filesystem": { "readwritePaths": [dir.path()] },
        "seatbelt": { "captureDenials": { "outputPath": out.join("denials.json") } }
    });
    let mut proc =
        mxc_sdk::v1::dev::spawn_json(&request.to_string(), Default::default()).expect("spawn");
    assert_eq!(proc.wait().expect("wait"), WaitResult::TimedOut);
    let warnings = proc.warnings();
    assert!(
        warnings.iter().any(|warning| {
            warning.contains("captureDenials failed after the run timed out")
                && warning.contains("failed to create temporary actionable output file")
        }),
        "{warnings:?}"
    );
}

#[cfg(target_os = "macos")]
#[test]
fn seatbelt_capture_failure_after_exit_reports_the_kept_trace() {
    use std::os::unix::fs::PermissionsExt;
    // The workload makes the report directory read-only, so the report write
    // fails after a normal exit while the open trace keeps its file.
    let dir = tempfile::tempdir_in("/private/tmp").expect("tempdir");
    let out = dir.path().join("out");
    std::fs::create_dir(&out).expect("report directory");
    let request = serde_json::json!({
        "version": "1.1.0-alpha",
        "containment": "seatbelt",
        "process": { "commandLine": format!("chmod a-w {}", out.display()) },
        "filesystem": { "readwritePaths": [dir.path()] },
        "seatbelt": {
            "captureDenials": { "outputPath": out.join("denials.json"), "retainEtl": true }
        }
    });
    let mut proc =
        mxc_sdk::v1::dev::spawn_json(&request.to_string(), Default::default()).expect("spawn");
    let error = proc.wait().expect_err("the report write fails").to_string();
    let metadata = proc.output_metadata();
    let warnings = proc.warnings();
    std::fs::set_permissions(&out, std::fs::Permissions::from_mode(0o755)).expect("restore");

    assert!(
        warnings.iter().any(
            |warning| warning.starts_with("Seatbelt captureDenials failed: ")
                && warning.contains(&error)
        ),
        "{warnings:?}"
    );

    let failure = metadata
        .as_ref()
        .and_then(|metadata| metadata.capture_denials_error.as_ref())
        .expect("captureDenialsError");
    let trace = std::path::Path::new(&failure.etl_path);
    assert!(error.contains(&failure.etl_path), "{error}");
    assert_eq!(trace.parent(), Some(out.as_path()));
    assert!(
        failure.etl_path.ends_with(".seatbelt.log"),
        "{}",
        failure.etl_path
    );
    assert!(
        failure.message.contains("captureDenials"),
        "{}",
        failure.message
    );
    assert!(trace.exists());
}

/// A capture request whose workload reads an ungranted file, and that file.
#[cfg(target_os = "macos")]
fn capture_denials_request(
    dir: &std::path::Path,
    command: &str,
    retain_trace: bool,
) -> (String, std::path::PathBuf) {
    let secret = dir.join("secret.txt");
    std::fs::write(&secret, "secret").expect("secret");
    std::fs::create_dir(dir.join("out")).expect("report directory");
    let request = serde_json::json!({
        "version": "1.1.0-alpha",
        "containment": "seatbelt",
        "process": { "commandLine": command.replace("{SECRET}", &secret.display().to_string()) },
        "seatbelt": {
            "captureDenials": {
                "outputPath": dir.join("out").join("denials.json"),
                "retainEtl": retain_trace
            }
        }
    });
    (request.to_string(), secret)
}

#[cfg(target_os = "macos")]
fn assert_report_lists(
    metadata: Option<mxc_sdk::v1::ExecutionMetadata>,
    exit_code: i32,
    secret: &std::path::Path,
) {
    let capture = metadata
        .and_then(|metadata| metadata.capture_denials)
        .expect("captureDenials metadata");
    assert_eq!(capture.exit_code, exit_code);
    let report: serde_json::Value =
        serde_json::from_slice(&std::fs::read(&capture.output_path).expect("report"))
            .expect("report JSON");
    let secret = secret.display().to_string();
    assert!(
        report["denials"]
            .as_array()
            .expect("denials")
            .iter()
            .any(|denial| denial["resource"] == secret.as_str()),
        "{report}"
    );
}

#[cfg(target_os = "macos")]
#[test]
fn seatbelt_capture_denials_reports_a_piped_run() {
    let dir = tempfile::tempdir_in("/private/tmp").expect("tempdir");
    let (request, secret) =
        capture_denials_request(dir.path(), "cat {SECRET} 2>/dev/null; exit 3", false);
    let mut proc = mxc_sdk::v1::dev::spawn_json(&request, Default::default()).expect("spawn");
    assert_eq!(proc.wait().expect("wait"), WaitResult::Exited(3));
    assert_report_lists(proc.output_metadata().cloned(), 3, &secret);
}

#[cfg(target_os = "macos")]
#[test]
fn seatbelt_capture_denials_reports_a_pty_run() {
    let dir = tempfile::tempdir_in("/private/tmp").expect("tempdir");
    let (request, secret) = capture_denials_request(
        dir.path(),
        "test -t 1 || exit 12; cat {SECRET}; exit 4",
        false,
    );
    let terminal =
        mxc_sdk::v1::dev::spawn_with_pty_json(&request, Default::default()).expect("spawn PTY");
    assert_eq!(terminal.wait().expect("wait"), WaitResult::Exited(4));
    assert_report_lists(terminal.output_metadata(), 4, &secret);
}

#[cfg(target_os = "macos")]
#[test]
fn seatbelt_capture_denials_drop_without_wait_leaves_no_files() {
    let dir = tempfile::tempdir_in("/private/tmp").expect("tempdir");
    let (request, _) = capture_denials_request(dir.path(), "cat {SECRET}; exec sleep 30", true);
    let out = dir.path().join("out");
    let proc = mxc_sdk::v1::dev::spawn_json(&request, Default::default()).expect("spawn");
    let files = || std::fs::read_dir(&out).expect("out").count();
    assert_eq!(
        files(),
        1,
        "the retained trace exists while the run is live"
    );
    drop(proc);
    assert_eq!(files(), 0);
}

// Process lifetime: nothing the workload starts may outlive the run, including
// a process that leaves MXC's session (`detached: true` calls `setsid()`).

/// The `node` on `PATH` and the directory the sandbox must be able to read for
/// it to start. Homebrew builds link Homebrew dylibs, so that whole root is
/// granted for them.
#[cfg(target_os = "macos")]
fn node_install() -> (std::path::PathBuf, std::path::PathBuf) {
    let path = std::env::var_os("PATH").unwrap_or_default();
    let node = std::env::split_paths(&path)
        .map(|dir| dir.join("node"))
        .find(|candidate| candidate.is_file())
        .expect("node must be on PATH for the Seatbelt process-lifetime tests");
    let resolved = std::fs::canonicalize(&node).expect("resolve node");
    let root = if resolved.starts_with("/opt/homebrew") {
        std::path::PathBuf::from("/opt/homebrew")
    } else if resolved.starts_with("/usr/local/Cellar") {
        std::path::PathBuf::from("/usr/local")
    } else {
        resolved
            .ancestors()
            .nth(2)
            .expect("node install prefix")
            .to_path_buf()
    };
    (resolved, root)
}

/// A per-test directory under `/tmp`, which the sandbox can write.
#[cfg(target_os = "macos")]
struct Scratch(std::path::PathBuf);

#[cfg(target_os = "macos")]
impl Scratch {
    fn new(tag: &str) -> Self {
        let nanos = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("clock")
            .as_nanos();
        let dir = std::path::PathBuf::from(format!(
            "/private/tmp/mxc-lifetime-{tag}-{}-{nanos}",
            std::process::id()
        ));
        std::fs::create_dir_all(&dir).expect("create scratch dir");
        Self(dir)
    }

    fn path(&self, name: &str) -> String {
        self.0.join(name).display().to_string()
    }
}

#[cfg(target_os = "macos")]
impl Drop for Scratch {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

/// SIGKILLs a leaked descendant when its test fails, so a failing run does not
/// leave it on the host. Not done on success: the pid may have been reused.
#[cfg(target_os = "macos")]
struct KillIfFailing(u32);

#[cfg(target_os = "macos")]
impl Drop for KillIfFailing {
    fn drop(&mut self) {
        if std::thread::panicking() && pid_alive(self.0) {
            // SAFETY: this pid was just observed alive as this test's leaked descendant.
            unsafe { libc::kill(self.0 as libc::pid_t, libc::SIGKILL) };
        }
    }
}

#[cfg(target_os = "macos")]
fn lifetime_request(
    command: String,
    timeout_ms: Option<u32>,
    node_root: &std::path::Path,
) -> ContainerRequest {
    ContainerRequest {
        filesystem: Some(FilesystemPolicy {
            readwrite_paths: vec!["/private/tmp".to_string()],
            readonly_paths: vec![node_root.display().to_string()],
            denied_paths: vec![],
            clear_policy_on_exit: None,
        }),
        timeout_ms,
        ..ContainerRequest::new(command)
    }
}

/// A shell command that uses Node to start `sleep 300` with
/// `spawn(..., { detached: true, stdio: 'ignore' })`, prints `DETACHED=<pid>`,
/// waits until that process has started, then runs `tail`.
#[cfg(target_os = "macos")]
fn detached_descendant_command(scratch: &Scratch, tail: &str) -> (String, std::path::PathBuf) {
    let (node, root) = node_install();
    let script = scratch.path("detach.js");
    std::fs::write(
        &script,
        r#"const { spawn } = require('child_process');
const child = spawn('/bin/sh', ['-c', 'echo started > "$0"; exec /bin/sleep 300', process.argv[2]], {
  detached: true,
  stdio: 'ignore',
});
child.unref();
console.log(`DETACHED=${child.pid}`);
"#,
    )
    .expect("write detach.js");
    let marker = scratch.path("started");
    let command = format!(
        "'{node}' '{script}' '{marker}' || exit 96; i=0; \
         while [ ! -s '{marker}' ] && [ $i -lt 200 ]; do sleep 0.05; i=$((i+1)); done; \
         [ -s '{marker}' ] || {{ echo DETACHED_NOT_STARTED; exit 97; }}; {tail}",
        node = node.display()
    );
    (command, root)
}

#[cfg(target_os = "macos")]
fn detached_pid(output: &str) -> u32 {
    output
        .lines()
        .find_map(|line| line.trim().strip_prefix("DETACHED="))
        .unwrap_or_else(|| panic!("no DETACHED=<pid> in output: {output}"))
        .parse()
        .expect("detached pid")
}

/// Reads stdout until `READY`, returning the detached descendant's pid.
#[cfg(target_os = "macos")]
fn read_detached_pid_until_ready(proc: &mut mxc_sdk::v1::MxcProcess) -> u32 {
    use std::io::{BufRead, BufReader};

    let mut reader = BufReader::new(proc.take_stdout().expect("stdout"));
    let mut output = String::new();
    loop {
        let mut line = String::new();
        let read = reader.read_line(&mut line).expect("read stdout");
        assert!(read != 0, "stdout ended before READY: {output}");
        output.push_str(&line);
        if line.trim() == "READY" {
            return detached_pid(&output);
        }
    }
}

/// Waits for the run and returns its outcome, stdout, and stderr. Unlike
/// `run()`, it keeps the handle, so the caller can check what `wait()` alone
/// cleaned up before dropping the handle cleans up again.
#[cfg(target_os = "macos")]
fn wait_keeping_handle(proc: &mut mxc_sdk::v1::MxcProcess) -> (WaitResult, String, String) {
    use std::io::Read;

    fn capture(stream: Option<Box<dyn Read + Send>>) -> std::thread::JoinHandle<String> {
        std::thread::spawn(move || {
            let mut text = String::new();
            if let Some(mut stream) = stream {
                let _ = stream.read_to_string(&mut text);
            }
            text
        })
    }

    let stdout = capture(proc.take_stdout());
    let stderr = capture(proc.take_stderr());
    let outcome = proc.wait().expect("wait");
    (
        outcome,
        stdout.join().expect("stdout reader"),
        stderr.join().expect("stderr reader"),
    )
}

#[cfg(target_os = "macos")]
fn assert_descendant_gone(pid: u32, when: &str) {
    for _ in 0..100 {
        if !pid_alive(pid) {
            return;
        }
        std::thread::sleep(std::time::Duration::from_millis(50));
    }
    panic!("detached descendant {pid} is still running {when}");
}

#[cfg(target_os = "macos")]
#[test]
fn wait_kills_detached_descendant_after_normal_exit() {
    let scratch = Scratch::new("normal");
    let (command, node_root) = detached_descendant_command(&scratch, "exit 0");
    let mut proc = spawn(
        lifetime_request(command, None, &node_root),
        Default::default(),
    )
    .expect("spawn");

    let (outcome, stdout, stderr) = wait_keeping_handle(&mut proc);
    assert_eq!(
        outcome,
        WaitResult::Exited(0),
        "stdout: {stdout}\nstderr: {stderr}"
    );
    let descendant = detached_pid(&stdout);
    let _guard = KillIfFailing(descendant);

    assert_descendant_gone(descendant, "after wait() returned");
    drop(proc);
}

#[cfg(target_os = "macos")]
#[test]
fn dropping_handle_kills_detached_descendant() {
    let scratch = Scratch::new("drop");
    let (command, node_root) = detached_descendant_command(&scratch, "echo READY; sleep 300");
    let mut proc = spawn(
        lifetime_request(command, None, &node_root),
        Default::default(),
    )
    .expect("spawn");
    let descendant = read_detached_pid_until_ready(&mut proc);
    let _guard = KillIfFailing(descendant);
    assert!(pid_alive(descendant), "descendant should run before drop");

    drop(proc);

    assert_descendant_gone(descendant, "after the handle was dropped");
}

#[cfg(target_os = "macos")]
#[test]
fn streaming_kill_terminates_detached_descendant() {
    let scratch = Scratch::new("kill");
    let (command, node_root) = detached_descendant_command(&scratch, "echo READY; sleep 300");
    let mut proc = spawn(
        lifetime_request(command, None, &node_root),
        Default::default(),
    )
    .expect("spawn");
    let descendant = read_detached_pid_until_ready(&mut proc);
    let _guard = KillIfFailing(descendant);
    assert!(pid_alive(descendant), "descendant should run before kill");

    proc.kill().expect("kill");

    assert_descendant_gone(descendant, "after kill()");
    let _ = proc.wait();
}

#[cfg(target_os = "macos")]
#[test]
fn streaming_timeout_kills_detached_descendant() {
    let scratch = Scratch::new("timeout");
    let (command, node_root) = detached_descendant_command(&scratch, "echo READY; sleep 300");
    let mut proc = spawn(
        lifetime_request(command, Some(5_000), &node_root),
        Default::default(),
    )
    .expect("spawn");
    let descendant = read_detached_pid_until_ready(&mut proc);
    let _guard = KillIfFailing(descendant);
    assert!(
        pid_alive(descendant),
        "descendant should run before the timeout"
    );

    assert_eq!(proc.wait().expect("wait"), WaitResult::TimedOut);

    assert_descendant_gone(descendant, "after the timeout");
}

/// Ending one run must not touch the processes of another run, including a
/// run whose profile allows or denies Mach names by their last character.
#[cfg(target_os = "macos")]
#[test]
fn streaming_kill_leaves_concurrent_run_running() {
    use mxc_sdk::v1::configs::SeatbeltConfig;
    use mxc_sdk::v1::Containment;
    use std::io::{BufRead, BufReader};

    const SPLIT_PROFILE: &str =
        "(version 1)(allow default)(deny mach-lookup (global-name-regex #\"[89a-f]$\"))";

    fn start(profile_override: Option<&str>) -> (mxc_sdk::v1::MxcProcess, u32, KillIfFailing) {
        let mut request = seatbelt_request("sleep 300 & echo CHILD=$!; sleep 300", 0);
        if let Some(profile) = profile_override {
            let mut config = SeatbeltConfig::default();
            config.profile_override = Some(profile.to_string());
            request.containment = Containment::Seatbelt(config);
        }
        let mut proc = spawn(request, Default::default()).expect("spawn");
        let mut line = String::new();
        BufReader::new(proc.take_stdout().expect("stdout"))
            .read_line(&mut line)
            .expect("read child pid");
        let child = line
            .trim()
            .strip_prefix("CHILD=")
            .expect("CHILD= prefix")
            .parse()
            .expect("child pid");
        (proc, child, KillIfFailing(child))
    }

    fn end(mut proc: mxc_sdk::v1::MxcProcess, child: u32, survivor: u32) {
        proc.kill().expect("kill");
        assert_descendant_gone(child, "after its run was killed");
        let _ = proc.wait();
        assert!(
            pid_alive(survivor),
            "killing one run also killed {survivor} from a concurrent run"
        );
    }

    let (first, first_child, _first_guard) = start(None);
    let (split, split_child, _split_guard) = start(Some(SPLIT_PROFILE));
    end(first, first_child, split_child);

    let (last, last_child, _last_guard) = start(None);
    end(split, split_child, last_child);

    let mut last = last;
    last.kill().expect("kill last run");
    assert_descendant_gone(last_child, "after its run was killed");
    let _ = last.wait();
}

/// Owns a hopping chain's scratch directory. On drop it creates the stop file,
/// waits for the chain to stop writing, and only then removes the directory;
/// a chain that keeps running keeps its directory and stop file.
#[cfg(target_os = "macos")]
struct ChainScratch(Option<Scratch>);

#[cfg(target_os = "macos")]
impl ChainScratch {
    fn path(&self, name: &str) -> String {
        self.0.as_ref().expect("scratch").path(name)
    }
}

#[cfg(target_os = "macos")]
impl Drop for ChainScratch {
    fn drop(&mut self) {
        let _ = std::fs::write(self.path("stop"), b"");
        let log = self.path("chain.log");
        let mut last = chain_length(&log);
        for _ in 0..50 {
            std::thread::sleep(std::time::Duration::from_millis(100));
            let now = chain_length(&log);
            if now == last {
                return;
            }
            last = now;
        }
        let scratch = self.0.take().expect("scratch");
        eprintln!("process chain under {} did not stop", scratch.0.display());
        std::mem::forget(scratch);
    }
}

#[cfg(target_os = "macos")]
fn chain_length(log: &str) -> usize {
    std::fs::read_to_string(log)
        .map(|text| text.lines().count())
        .unwrap_or(0)
}

/// Each process in a chain forks the next and exits at once, so a scan that
/// lists processes and then inspects them can see a process exit before it is
/// inspected while its replacement, started after the listing, is missed by
/// that scan and by the next. Several chains hop in parallel. None may outlive
/// the run.
#[cfg(target_os = "macos")]
#[test]
fn wait_kills_descendant_chain_that_outpaces_process_scans() {
    let scratch = ChainScratch(Some(Scratch::new("chain")));
    let log = scratch.path("chain.log");
    let command = format!(
        r#"LOG='{log}' STOP='{stop}' G=0
hop() {{ [ -e "$STOP" ] && exit 0; [ "$G" -ge 100000 ] && exit 0; G=$((G+1)); echo x >> "$LOG"; ( hop ) </dev/null >/dev/null 2>&1 & exit 0; }}
: > "$LOG"
for chain in 1 2 3 4 5 6 7 8; do ( hop ) </dev/null >/dev/null 2>&1 & done
i=0; while [ $(($(wc -l < "$LOG"))) -lt 200 ] && [ $i -lt 200 ]; do sleep 0.05; i=$((i+1)); done
[ $(($(wc -l < "$LOG"))) -ge 200 ] || {{ echo CHAIN_NOT_HOPPING; exit 97; }}"#,
        stop = scratch.path("stop"),
    );
    let request = ContainerRequest {
        filesystem: Some(FilesystemPolicy {
            readwrite_paths: vec!["/private/tmp".to_string()],
            readonly_paths: vec![],
            denied_paths: vec![],
            clear_policy_on_exit: None,
        }),
        ..ContainerRequest::new(command)
    };

    let mut proc = spawn(request, Default::default()).expect("spawn");

    let (outcome, stdout, stderr) = wait_keeping_handle(&mut proc);
    assert_eq!(
        outcome,
        WaitResult::Exited(0),
        "stdout: {stdout}\nstderr: {stderr}"
    );

    std::thread::sleep(std::time::Duration::from_millis(250));
    let before = chain_length(&log);
    std::thread::sleep(std::time::Duration::from_secs(1));
    let after = chain_length(&log);
    assert_eq!(
        before, after,
        "the process chain kept running after wait() returned ({before} -> {after} hops)"
    );
    drop(proc);
}