inferlab-runtime 0.8.0

Framework-neutral runtime lifecycle mechanics for the InferLab product.
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
mod cleanup;
mod launch;
mod observation;
mod readiness;

use crate::operation_bound::{OperationBound, OperationTimingEvidence};
use crate::plan::{CommandPlan, LaunchFilePlan, LaunchPlan, ProcessEndpointPlan, ReadinessPlan};
use crate::process_group::{SignalEvidence, process_start_time};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::time::Duration;

#[cfg(test)]
use crate::operation_bound::OperationTerminalCause;
#[cfg(test)]
use crate::plan::TargetRegistryExpectedTarget;
#[cfg(test)]
use crate::shell::shell_quote_path;
use cleanup::removal_summary;
#[cfg(test)]
use cleanup::terminate_local;
#[cfg(test)]
use launch::{
    command_output_with_input, materialize_local_launch_files, remote_launch_file_script,
    spawn_local,
};
#[cfg(test)]
use observation::{run_status_command, verified_local_status};
#[cfg(test)]
use readiness::{
    HttpTargetRegistryProbe, READINESS_ATTEMPT_CAP, match_target_registry, probe_http,
    probe_http_json, wait_http_target_registry_ready,
};
#[cfg(test)]
use sha2::{Digest, Sha256};
#[cfg(test)]
use std::collections::BTreeMap;
#[cfg(test)]
use std::fs;
#[cfg(test)]
use std::io::{Read, Write};
#[cfg(test)]
use std::os::unix::process::CommandExt;
#[cfg(test)]
use std::process::{Command, Output, Stdio};
#[cfg(test)]
use std::thread;
#[cfg(test)]
use std::time::Instant;

pub const REMOTE_LOG_SYNC_DEADLINE: Duration = Duration::from_secs(30);

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HostProcessHandle {
    pub leader_pid: u32,
    pub process_group: u32,
    pub leader_start_time_ticks: u64,
    /// The container this process launched, when the command is a
    /// containerized substitution: the daemon-owned cleanup handle the
    /// process-group kill cannot reach ([[RFC-0003:C-RUNTIME-WORKFLOWS]]).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub container: Option<String>,
}

impl HostProcessHandle {
    fn new(leader_pid: u32, container: Option<String>) -> Result<Self, String> {
        if leader_pid == 0 {
            return Err("host process-group handle requires a non-zero leader pid".to_owned());
        }
        let leader_start_time_ticks = process_start_time(leader_pid)
            .map_err(|error| error.to_string())?
            .ok_or_else(|| {
                format!("host process {leader_pid} exited before its identity could be recorded")
            })?;
        Ok(Self {
            leader_pid,
            process_group: leader_pid,
            leader_start_time_ticks,
            container,
        })
    }

    fn validate(&self) -> Result<(), String> {
        validate_process_identity(
            self.leader_pid,
            self.process_group,
            self.leader_start_time_ticks,
            "host",
        )
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct SshProcessHandle {
    pub target: String,
    pub leader_pid: u32,
    pub process_group: u32,
    pub leader_start_time_ticks: u64,
    pub stdout: PathBuf,
    pub stderr: PathBuf,
    /// The container this process launched, when the command is a
    /// containerized substitution: the daemon-owned cleanup handle the
    /// process-group kill cannot reach ([[RFC-0003:C-RUNTIME-WORKFLOWS]]).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub container: Option<String>,
}

impl SshProcessHandle {
    fn validate(&self) -> Result<(), String> {
        if self.target.is_empty() {
            return Err("SSH process handle requires a target".to_owned());
        }
        validate_process_identity(
            self.leader_pid,
            self.process_group,
            self.leader_start_time_ticks,
            "SSH",
        )
    }
}

fn validate_process_identity(
    leader_pid: u32,
    process_group: u32,
    leader_start_time_ticks: u64,
    kind: &str,
) -> Result<(), String> {
    if leader_pid == 0 || process_group == 0 || leader_start_time_ticks == 0 {
        return Err(format!("{kind} process-group handle requires non-zero ids"));
    }
    if leader_pid != process_group {
        return Err(format!(
            "{kind} process-group handle requires leader_pid to equal process_group"
        ));
    }
    Ok(())
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
pub enum ProcessHandle {
    Local(HostProcessHandle),
    Ssh(SshProcessHandle),
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum CleanupTrigger {
    StartupRollback,
    Stop,
    Recovery,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct CleanupEvidence {
    pub trigger: CleanupTrigger,
    pub elapsed_ms: u64,
    pub status_deadline_ms: u64,
    pub term_grace_ms: u64,
    pub kill_grace_ms: u64,
    pub reap_grace_ms: Option<u64>,
    pub remote_deadline_ms: Option<u64>,
    pub verified: bool,
    pub already_exited: bool,
    pub forced: bool,
    pub signals: Vec<SignalEvidence>,
    pub error: Option<String>,
    /// Confirmed removal of the process's container on its launch machine
    /// ([[RFC-0003:C-RUNTIME-WORKFLOWS]]); present when the cleanup path
    /// attempted to remove a known container — from a running server's
    /// handle, or from a launch failure whose command already named one
    /// before any handle existed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub container_removal: Option<ContainerRemovalEvidence>,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ContainerRemovalEvidence {
    pub container: String,
    pub elapsed_ms: u64,
    pub operation_elapsed_ms: u64,
    pub deadline_ms: u64,
    pub client_cleanup: Option<crate::container::CommandCleanupEvidence>,
    pub confirmed: bool,
    pub already_absent: bool,
    pub error: Option<String>,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ProcessStatus {
    pub queried: bool,
    pub alive: bool,
    pub error: Option<String>,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct TargetRegistryMatchEvidence {
    pub url: String,
    pub role: String,
    pub healthy: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bootstrap_port: Option<u16>,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum ReadinessEvidence {
    Http {
        url: String,
        attempts: u32,
        ready_unix_ms: u64,
        timing: OperationTimingEvidence,
        diagnostic_attempts: Vec<ReadinessAttemptEvidence>,
    },
    HttpTargetRegistry {
        readiness_url: String,
        registry_url: String,
        attempts: u32,
        ready_unix_ms: u64,
        matched_targets: Vec<TargetRegistryMatchEvidence>,
        timing: OperationTimingEvidence,
        diagnostic_attempts: Vec<ReadinessAttemptEvidence>,
    },
    ProcessAlive {
        ready_unix_ms: u64,
        timing: OperationTimingEvidence,
    },
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ReadinessAttemptEvidence {
    pub operation: String,
    pub effective_bound_ms: u64,
    pub succeeded: bool,
    pub error: Option<String>,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ReadinessFailureKind {
    Exited,
    Interrupted,
    Timeout,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ReadinessFailure {
    pub kind: ReadinessFailureKind,
    pub message: String,
    pub timing: Option<OperationTimingEvidence>,
    pub diagnostic_attempts: Vec<ReadinessAttemptEvidence>,
}

pub struct ProcessSpec<'a> {
    pub launch: &'a LaunchPlan,
    pub command: &'a CommandPlan,
    pub launch_files: &'a [LaunchFilePlan],
    pub cache_root: &'a Path,
    pub stdout: &'a Path,
    pub stderr: &'a Path,
    pub remote_dir: &'a Path,
    /// The resolver-assigned container name when the command is a
    /// containerized substitution.
    pub container: Option<&'a str>,
}

#[derive(Debug, thiserror::Error)]
pub enum ServerLaunchError {
    #[error("{message}")]
    Preparation { message: String },
    #[error("failed to {operation} {path}: {source}")]
    FileIo {
        operation: &'static str,
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error("failed to launch {program:?}: {source}")]
    Process {
        program: String,
        #[source]
        source: std::io::Error,
    },
    #[error(transparent)]
    Ssh(#[from] crate::ssh::SshError),
    #[error("failed to send launch file input over SSH to {target:?}: {source}")]
    SshInput {
        target: String,
        #[source]
        source: std::io::Error,
    },
    #[error("{operation} exited with {status}: {diagnostics}")]
    Exit {
        operation: String,
        status: std::process::ExitStatus,
        diagnostics: String,
    },
    #[error("SSH launch on {target:?} returned non-UTF-8 identity: {source}")]
    NonUtf8Identity {
        target: String,
        #[source]
        source: std::string::FromUtf8Error,
    },
    #[error("SSH launch on {target:?} returned no process id")]
    MissingProcessId { target: String },
    #[error("SSH launch on {target:?} returned no process start time")]
    MissingStartTime { target: String },
    #[error("SSH launch on {target:?} returned invalid process id {value:?}: {source}")]
    InvalidProcessId {
        target: String,
        value: String,
        #[source]
        source: std::num::ParseIntError,
    },
    #[error("SSH launch on {target:?} returned invalid process start time {value:?}: {source}")]
    InvalidStartTime {
        target: String,
        value: String,
        #[source]
        source: std::num::ParseIntError,
    },
    #[error("SSH launch on {target:?} returned an invalid process identity: {details}")]
    InvalidIdentity { target: String, details: String },
    #[error("existing launch file target {path} is not a regular file", path = path.display())]
    NotRegularFile { path: PathBuf },
    #[error(
        "existing launch file {path} does not match declared digest {expected}; found {actual}",
        path = path.display()
    )]
    FileDigestMismatch {
        path: PathBuf,
        expected: String,
        actual: String,
    },
}

#[derive(Debug)]
pub struct LaunchFailure {
    pub error: ServerLaunchError,
    pub ownership_unknown: bool,
    /// The structured outcome of removing the container this launch may
    /// have created, when the failure attempted one; the record's cleanup
    /// evidence carries the actual container and reason rather than a
    /// generic note ([[RFC-0003:C-RUNTIME-WORKFLOWS]]).
    pub container_removal: Option<Box<ContainerRemovalEvidence>>,
    pub cleanup: Option<Box<CleanupEvidence>>,
    cleanup_note: Option<String>,
}

impl LaunchFailure {
    pub fn before_launch(message: String) -> Self {
        Self {
            error: ServerLaunchError::Preparation { message },
            ownership_unknown: false,
            container_removal: None,
            cleanup: None,
            cleanup_note: None,
        }
    }

    fn from_error(error: ServerLaunchError) -> Self {
        Self {
            error,
            ownership_unknown: false,
            container_removal: None,
            cleanup: None,
            cleanup_note: None,
        }
    }

    pub fn message(&self) -> String {
        let mut message = self.error.to_string();
        if let Some(note) = &self.cleanup_note {
            message = format!("{message}; {note}");
        } else if let Some(cleanup) = &self.cleanup
            && let Some(error) = &cleanup.error
        {
            message = format!("{message}; local launch cleanup was not verified: {error}");
        }
        if let Some(removal) = &self.container_removal {
            message = format!("{message}; {}", removal_summary(removal));
        }
        message
    }

    pub fn unresolved_ownership(message: String) -> Self {
        Self {
            error: ServerLaunchError::Preparation { message },
            ownership_unknown: true,
            container_removal: None,
            cleanup: None,
            cleanup_note: None,
        }
    }
}

pub trait ProcessLauncher {
    fn spawn(&self, spec: ProcessSpec<'_>) -> Result<ProcessHandle, LaunchFailure>;
}

pub trait ProcessObserver {
    fn status(&self, handle: &ProcessHandle) -> ProcessStatus;
    fn status_with_bound(&self, handle: &ProcessHandle, bound: &OperationBound) -> ProcessStatus;
    fn sync_logs(
        &self,
        handle: &ProcessHandle,
        stdout: &Path,
        stderr: &Path,
        cleanup: bool,
    ) -> Result<(), LogSyncError>;
}

pub trait ReadinessObserver {
    fn wait_ready(
        &self,
        handle: &ProcessHandle,
        endpoint: &ProcessEndpointPlan,
        readiness: &ReadinessPlan,
        on_probe_failure: &mut dyn FnMut(&str),
    ) -> Result<ReadinessEvidence, ReadinessFailure>;
}

pub trait ProcessCleanup {
    fn terminate(
        &self,
        handle: &ProcessHandle,
        trigger: CleanupTrigger,
        on_container_removal: &mut dyn FnMut(&str),
    ) -> CleanupEvidence;
}

pub trait ServerRuntime:
    ProcessLauncher + ProcessObserver + ReadinessObserver + ProcessCleanup
{
}

impl<T> ServerRuntime for T where
    T: ProcessLauncher + ProcessObserver + ReadinessObserver + ProcessCleanup
{
}

#[derive(Debug, thiserror::Error)]
pub enum ProcessCommandError {
    #[error("{operation} deadline expired")]
    Deadline { operation: String },
    #[error("{operation} was interrupted")]
    Interrupted { operation: String },
    #[error("failed to launch {operation}: {source}")]
    Launch {
        operation: String,
        #[source]
        source: std::io::Error,
    },
    #[error("failed to launch {operation}: {source}")]
    Ssh {
        operation: String,
        #[source]
        source: crate::ssh::SshError,
    },
    #[error("{operation} failed: {source}")]
    Io {
        operation: String,
        #[source]
        source: std::io::Error,
    },
    #[error("{operation} wait failed: {source}; child cleanup: {cleanup}")]
    WaitCleanup {
        operation: String,
        #[source]
        source: std::io::Error,
        cleanup: String,
    },
    #[error("{operation} exited with {status}: {stderr}")]
    Exit {
        operation: String,
        status: std::process::ExitStatus,
        stderr: String,
    },
}

#[derive(Debug, thiserror::Error)]
pub enum LogSyncError {
    #[error("failed to read remote log {path}: {source}")]
    ReadRemote {
        path: PathBuf,
        #[source]
        source: ProcessCommandError,
    },
    #[error("failed to read remote log {path}: command exited with {status}: {stderr}")]
    RemoteExit {
        path: PathBuf,
        status: std::process::ExitStatus,
        stderr: String,
    },
    #[error("failed to write local log {path}: {source}")]
    WriteLocal {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
}

#[derive(Clone, Copy, Debug, Default)]
pub struct SystemProcessRuntime;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::plan::LaunchFilePlan;
    use std::io::{BufRead, BufReader};
    use std::net::TcpListener;
    use std::os::unix::fs::{MetadataExt, PermissionsExt};

    #[test]
    fn expired_readiness_owner_prevents_a_fresh_network_attempt() {
        let bound = OperationBound::finite(Duration::ZERO);
        let error = probe_http("127.0.0.1", 9, "/ready", &bound)
            .err()
            .map(|error| error.to_string())
            .unwrap_or_default();

        assert_eq!(error, "readiness operation deadline expired");
    }

    #[test]
    fn readiness_attempt_deadline_bounds_a_trickled_status_line() -> Result<(), String> {
        let listener = TcpListener::bind(("127.0.0.1", 0)).map_err(|error| error.to_string())?;
        let port = listener
            .local_addr()
            .map_err(|error| error.to_string())?
            .port();
        let server = thread::spawn(move || -> Result<(), String> {
            let (mut stream, _) = listener.accept().map_err(|error| error.to_string())?;
            let mut request = [0_u8; 1024];
            let _ = stream.read(&mut request);
            let response = format!("HTTP/1.1 200 {}\r\n", " ".repeat(96));
            for byte in response.bytes() {
                if stream.write_all(&[byte]).is_err() {
                    break;
                }
                thread::sleep(Duration::from_millis(20));
            }
            Ok(())
        });

        let started = Instant::now();
        let error = probe_http("127.0.0.1", port, "/ready", &OperationBound::unbounded())
            .err()
            .map(|error| error.to_string())
            .unwrap_or_default();
        let elapsed = started.elapsed();
        server
            .join()
            .map_err(|_| "trickle fixture panicked".to_owned())??;

        assert!(error.contains("deadline expired"), "{error}");
        assert!(
            elapsed < Duration::from_secs(1),
            "a 250ms attempt lasted {elapsed:?}"
        );
        Ok(())
    }

    #[test]
    fn registry_attempt_deadline_bounds_a_trickled_body() -> Result<(), String> {
        let listener = TcpListener::bind(("127.0.0.1", 0)).map_err(|error| error.to_string())?;
        let port = listener
            .local_addr()
            .map_err(|error| error.to_string())?
            .port();
        let server = thread::spawn(move || -> Result<(), String> {
            let (mut stream, _) = listener.accept().map_err(|error| error.to_string())?;
            let mut request = [0_u8; 1024];
            let _ = stream.read(&mut request);
            stream
                .write_all(b"HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n")
                .map_err(|error| error.to_string())?;
            let body = format!("{}{{}}", " ".repeat(96));
            for byte in body.bytes() {
                if stream.write_all(&[byte]).is_err() {
                    break;
                }
                thread::sleep(Duration::from_millis(20));
            }
            Ok(())
        });

        let started = Instant::now();
        let error = probe_http_json(
            "127.0.0.1",
            port,
            "/workers",
            "target registry",
            &OperationBound::unbounded(),
        )
        .err()
        .map(|error| error.to_string())
        .unwrap_or_default();
        let elapsed = started.elapsed();
        server
            .join()
            .map_err(|_| "registry trickle fixture panicked".to_owned())??;

        assert!(error.contains("deadline expired"), "{error}");
        assert!(
            elapsed < Duration::from_secs(1),
            "a 250ms registry attempt lasted {elapsed:?}"
        );
        Ok(())
    }

    #[test]
    fn expired_readiness_owner_rejects_a_registry_match() {
        let expected = vec![TargetRegistryExpectedTarget {
            url: "http://decode:30001".to_owned(),
            role: "decode".to_owned(),
            bootstrap_port: None,
        }];
        let response = serde_json::json!({
            "workers": [{
                "url": "http://decode:30001",
                "worker_type": "decode",
                "is_healthy": true
            }]
        });

        let error = match_target_registry(
            &response,
            &target_registry_probe(&expected),
            &OperationBound::finite(Duration::ZERO),
        )
        .err()
        .map(|error| error.to_string())
        .unwrap_or_default();

        assert_eq!(error, "readiness operation deadline expired");
    }

    #[test]
    fn process_status_command_cannot_outlive_the_readiness_owner() {
        let started = Instant::now();
        let error = run_status_command(
            &["sh", "-c", "sleep 5"],
            &OperationBound::finite(Duration::from_millis(50)),
        )
        .err()
        .map(|error| error.to_string())
        .unwrap_or_default();

        assert_eq!(error, "process status attempt deadline expired");
        assert!(
            started.elapsed() < Duration::from_secs(1),
            "bounded process status did not stop promptly"
        );
    }

    #[test]
    fn unbounded_process_status_command_does_not_acquire_a_timeout() -> Result<(), String> {
        let output = run_status_command(
            &["sh", "-c", "sleep 0.1; printf alive"],
            &OperationBound::unbounded(),
        )
        .map_err(|error| error.to_string())?;

        assert!(output.status.success());
        assert_eq!(output.stdout, b"alive");
        Ok(())
    }

    fn launch_file(root: &Path, text: &str, name: &str) -> LaunchFilePlan {
        let sha256 = format!("{:x}", Sha256::digest(text.as_bytes()));
        let relative_path = format!("launch-files/{sha256}/{name}");
        LaunchFilePlan {
            resolved_path: root.join(&relative_path),
            relative_path,
            text: text.to_owned(),
            sha256,
        }
    }

    fn run_script_with_input(script: &str, input: &[u8]) -> Result<Output, String> {
        let mut command = Command::new("bash");
        command.args(["-c", script]);
        command_output_with_input(command, input).map_err(|error| error.to_string())
    }

    fn target_registry_endpoint(
        registry_body: String,
    ) -> Result<(ProcessEndpointPlan, thread::JoinHandle<Result<(), String>>), String> {
        let listener = TcpListener::bind(("127.0.0.1", 0)).map_err(|error| error.to_string())?;
        let port = listener
            .local_addr()
            .map_err(|error| error.to_string())?
            .port();
        let server = thread::spawn(move || {
            for _ in 0..2 {
                let (mut stream, _) = listener.accept().map_err(|error| error.to_string())?;
                let mut request_line = String::new();
                let mut reader =
                    BufReader::new(stream.try_clone().map_err(|error| error.to_string())?);
                reader
                    .read_line(&mut request_line)
                    .map_err(|error| error.to_string())?;
                loop {
                    let mut header = String::new();
                    reader
                        .read_line(&mut header)
                        .map_err(|error| error.to_string())?;
                    if header == "\r\n" || header.is_empty() {
                        break;
                    }
                }
                let body = if request_line.starts_with("GET /workers ") {
                    registry_body.as_bytes()
                } else {
                    b""
                };
                let mut response = format!(
                    "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                    body.len()
                )
                .into_bytes();
                response.extend_from_slice(body);
                stream
                    .write_all(&response)
                    .map_err(|error| error.to_string())?;
            }
            Ok(())
        });
        Ok((
            ProcessEndpointPlan {
                host: "127.0.0.1".to_owned(),
                port,
            },
            server,
        ))
    }

    fn target_registry_probe<'a>(
        expected_targets: &'a [TargetRegistryExpectedTarget],
    ) -> HttpTargetRegistryProbe<'a> {
        HttpTargetRegistryProbe {
            readiness_path: "/readiness",
            registry_path: "/workers",
            targets_field: "workers",
            target_url_field: "url",
            target_role_field: "worker_type",
            target_healthy_field: "is_healthy",
            target_bootstrap_port_field: "bootstrap_port",
            expected_targets,
        }
    }

    fn alive_status() -> ProcessStatus {
        ProcessStatus {
            queried: true,
            alive: true,
            error: None,
        }
    }

    #[test]
    fn local_launch_file_publication_reuses_the_immutable_target() -> Result<(), String> {
        let root = tempfile::tempdir().map_err(|error| error.to_string())?;
        let launch_file = launch_file(
            root.path(),
            "worker: \u{2603}\nmode: context\n",
            "worker.yaml",
        );

        materialize_local_launch_files(std::slice::from_ref(&launch_file))
            .map_err(|error| error.to_string())?;
        let first_metadata =
            fs::metadata(&launch_file.resolved_path).map_err(|error| error.to_string())?;
        materialize_local_launch_files(std::slice::from_ref(&launch_file))
            .map_err(|error| error.to_string())?;
        let second_metadata =
            fs::metadata(&launch_file.resolved_path).map_err(|error| error.to_string())?;

        assert_eq!(
            fs::read_to_string(&launch_file.resolved_path).map_err(|error| error.to_string())?,
            launch_file.text
        );
        assert_eq!(first_metadata.ino(), second_metadata.ino());
        assert_eq!(second_metadata.permissions().mode() & 0o222, 0);
        Ok(())
    }

    #[test]
    fn local_launch_file_mismatch_fails_before_spawn_without_replacing_it() -> Result<(), String> {
        let root = tempfile::tempdir().map_err(|error| error.to_string())?;
        let cache = root.path().join("cache");
        let launch_file = launch_file(&cache, "expected\n", "worker.yaml");
        let parent = launch_file
            .resolved_path
            .parent()
            .ok_or_else(|| "launch file has no parent".to_owned())?;
        fs::create_dir_all(parent).map_err(|error| error.to_string())?;
        fs::write(&launch_file.resolved_path, "stale\n").map_err(|error| error.to_string())?;
        let marker = root.path().join("spawned");
        let command = CommandPlan {
            argv: vec![
                "sh".to_owned(),
                "-c".to_owned(),
                format!("printf launched > {}", shell_quote_path(&marker)),
            ],
            env: BTreeMap::new(),
            explicit_env: Vec::new(),
            pass_env: Vec::new(),
            cwd: root.path().to_path_buf(),
        };
        let launch_files = vec![launch_file.clone()];

        let result = spawn_local(ProcessSpec {
            launch: &LaunchPlan::Local,
            command: &command,
            launch_files: &launch_files,
            cache_root: &cache,
            stdout: &root.path().join("stdout.log"),
            stderr: &root.path().join("stderr.log"),
            remote_dir: &root.path().join("remote"),
            container: None,
        });

        let failure = match result {
            Err(failure) => failure,
            Ok(handle) => {
                let _ = terminate_local(&handle, CleanupTrigger::StartupRollback);
                return Err("mismatched launch file unexpectedly spawned a process".to_owned());
            }
        };
        assert!(!failure.ownership_unknown, "{failure:?}");
        assert!(failure.message().contains("does not match"), "{failure:?}");
        assert!(!marker.exists());
        assert_eq!(
            fs::read_to_string(&launch_file.resolved_path).map_err(|error| error.to_string())?,
            "stale\n"
        );
        Ok(())
    }

    #[test]
    fn remote_launch_file_script_publishes_stdin_without_replacing_targets() -> Result<(), String> {
        let root = tempfile::tempdir().map_err(|error| error.to_string())?;
        let published = launch_file(
            root.path(),
            "worker: \u{96ea}\nmode: context\n",
            "worker.yaml",
        );
        let script = remote_launch_file_script(&published).map_err(|error| error.to_string())?;

        let first = run_script_with_input(&script, published.text.as_bytes())?;
        assert!(
            first.status.success(),
            "{}",
            String::from_utf8_lossy(&first.stderr)
        );
        let first_metadata =
            fs::metadata(&published.resolved_path).map_err(|error| error.to_string())?;
        let first_inode = first_metadata.ino();
        assert_eq!(first_metadata.permissions().mode() & 0o222, 0);
        let reused = run_script_with_input(&script, published.text.as_bytes())?;
        assert!(
            reused.status.success(),
            "{}",
            String::from_utf8_lossy(&reused.stderr)
        );
        assert_eq!(
            fs::read_to_string(&published.resolved_path).map_err(|error| error.to_string())?,
            published.text
        );
        assert_eq!(
            fs::metadata(&published.resolved_path)
                .map_err(|error| error.to_string())?
                .ino(),
            first_inode
        );

        let corrupt = launch_file(root.path(), "expected\n", "corrupt.yaml");
        let corrupt_parent = corrupt
            .resolved_path
            .parent()
            .ok_or_else(|| "launch file has no parent".to_owned())?;
        fs::create_dir_all(corrupt_parent).map_err(|error| error.to_string())?;
        fs::write(&corrupt.resolved_path, "stale\n").map_err(|error| error.to_string())?;
        let rejected = run_script_with_input(
            &remote_launch_file_script(&corrupt).map_err(|error| error.to_string())?,
            corrupt.text.as_bytes(),
        )?;
        assert!(!rejected.status.success());
        assert_eq!(
            fs::read_to_string(&corrupt.resolved_path).map_err(|error| error.to_string())?,
            "stale\n"
        );
        Ok(())
    }

    #[test]
    fn target_registry_readiness_records_all_expected_targets() -> Result<(), String> {
        let (endpoint, server) = target_registry_endpoint(
            serde_json::json!({
                "workers": [
                    {
                        "url": "http://prefill:30000",
                        "worker_type": "prefill",
                        "is_healthy": true,
                        "bootstrap_port": 8998
                    },
                    {
                        "url": "http://decode:30001",
                        "worker_type": "decode",
                        "is_healthy": true
                    }
                ]
            })
            .to_string(),
        )?;
        let expected = vec![
            TargetRegistryExpectedTarget {
                url: "http://prefill:30000".to_owned(),
                role: "prefill".to_owned(),
                bootstrap_port: Some(8998),
            },
            TargetRegistryExpectedTarget {
                url: "http://decode:30001".to_owned(),
                role: "decode".to_owned(),
                bootstrap_port: None,
            },
        ];

        let evidence = wait_http_target_registry_ready(
            |_| alive_status(),
            &endpoint,
            target_registry_probe(&expected),
            None,
            &mut |_| {},
        )
        .map_err(|failure| failure.message)?;
        server
            .join()
            .map_err(|_| "target registry fixture panicked".to_owned())??;

        let record_value = serde_json::to_value(&evidence).map_err(|error| error.to_string())?;
        assert_eq!(record_value["kind"], "http_target_registry");
        assert_eq!(
            record_value["matched_targets"].as_array().map(Vec::len),
            Some(2)
        );
        let ReadinessEvidence::HttpTargetRegistry {
            readiness_url,
            registry_url,
            attempts,
            matched_targets,
            timing,
            diagnostic_attempts,
            ready_unix_ms: _,
        } = evidence
        else {
            return Err("target registry readiness returned the wrong evidence kind".to_owned());
        };
        assert_eq!(
            readiness_url,
            format!("http://127.0.0.1:{}/readiness", endpoint.port)
        );
        assert_eq!(
            registry_url,
            format!("http://127.0.0.1:{}/workers", endpoint.port)
        );
        assert_eq!(attempts, 1);
        assert_eq!(
            timing.budget,
            crate::operation_bound::OperationBudgetEvidence::Unbounded
        );
        assert_eq!(
            timing.terminal_cause,
            crate::operation_bound::OperationTerminalCause::Succeeded
        );
        assert_eq!(diagnostic_attempts.len(), 2);
        assert!(diagnostic_attempts.iter().all(|attempt| {
            attempt.succeeded
                && (1..=crate::operation_bound::duration_millis(READINESS_ATTEMPT_CAP))
                    .contains(&attempt.effective_bound_ms)
        }));
        assert_eq!(
            matched_targets,
            vec![
                TargetRegistryMatchEvidence {
                    url: "http://prefill:30000".to_owned(),
                    role: "prefill".to_owned(),
                    healthy: true,
                    bootstrap_port: Some(8998),
                },
                TargetRegistryMatchEvidence {
                    url: "http://decode:30001".to_owned(),
                    role: "decode".to_owned(),
                    healthy: true,
                    bootstrap_port: None,
                },
            ]
        );
        Ok(())
    }

    #[test]
    fn target_registry_readiness_rejects_partial_registration() -> Result<(), String> {
        let (endpoint, server) = target_registry_endpoint(
            serde_json::json!({
                "workers": [{
                    "url": "http://prefill:30000",
                    "worker_type": "prefill",
                    "is_healthy": true,
                    "bootstrap_port": 8998
                }]
            })
            .to_string(),
        )?;
        let expected = vec![
            TargetRegistryExpectedTarget {
                url: "http://prefill:30000".to_owned(),
                role: "prefill".to_owned(),
                bootstrap_port: Some(8998),
            },
            TargetRegistryExpectedTarget {
                url: "http://decode:30001".to_owned(),
                role: "decode".to_owned(),
                bootstrap_port: None,
            },
        ];

        let mut probe_failures = Vec::new();
        let failure = match wait_http_target_registry_ready(
            |_| alive_status(),
            &endpoint,
            target_registry_probe(&expected),
            Some(1),
            &mut |failure| probe_failures.push(failure.to_owned()),
        ) {
            Err(failure) => failure,
            Ok(evidence) => {
                return Err(format!(
                    "partial target registration unexpectedly became ready: {evidence:?}"
                ));
            }
        };
        server
            .join()
            .map_err(|_| "target registry fixture panicked".to_owned())??;

        assert_eq!(failure.kind, ReadinessFailureKind::Timeout);
        let timing = failure
            .timing
            .as_ref()
            .ok_or_else(|| "readiness timeout has no timing evidence".to_owned())?;
        assert_eq!(
            timing.budget,
            crate::operation_bound::OperationBudgetEvidence::Finite {
                configured_ms: 1_000,
            }
        );
        assert_eq!(timing.terminal_cause, OperationTerminalCause::TimedOut);
        assert!(probe_failures.iter().any(|failure| {
            failure.contains("target registry has no \"decode\" target at \"http://decode:30001\"")
        }));
        Ok(())
    }

    #[test]
    fn termination_waits_for_the_group_after_the_launcher_exits() -> Result<(), String> {
        let mut child = Command::new("sh")
            .args([
                "-c",
                "trap 'exit 0' TERM; sh -c 'trap \"\" TERM; exec sleep 30' & wait",
            ])
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .process_group(0)
            .spawn()
            .map_err(|error| error.to_string())?;
        let handle = HostProcessHandle::new(child.id(), None)?;
        thread::sleep(Duration::from_millis(100));
        let reaper = thread::spawn(move || child.wait());

        let cleanup = terminate_local(&handle, CleanupTrigger::Stop);
        if !cleanup.verified {
            let _ = Command::new("kill")
                .args(["-KILL", "--", &format!("-{}", handle.process_group)])
                .status();
        }
        let _ = reaper.join();

        assert!(cleanup.verified, "{cleanup:?}");
        assert!(cleanup.forced);
        assert!(cleanup.elapsed_ms >= cleanup.term_grace_ms);
        assert_eq!(cleanup.status_deadline_ms, 2_000);
        assert_eq!(cleanup.term_grace_ms, 2_000);
        assert_eq!(cleanup.kill_grace_ms, 10_000);
        Ok(())
    }

    #[test]
    fn rejects_a_reused_process_identity() -> Result<(), Box<dyn std::error::Error>> {
        let pid = std::process::id();
        let actual = process_start_time(pid)
            .map_err(std::io::Error::other)?
            .ok_or_else(|| std::io::Error::other("test process has no /proc identity"))?;
        let recorded = if actual == u64::MAX {
            actual - 1
        } else {
            actual + 1
        };
        let status = verified_local_status(&HostProcessHandle {
            leader_pid: pid,
            process_group: pid,
            leader_start_time_ticks: recorded,
            container: None,
        });

        assert!(status.queried);
        assert!(!status.alive);
        assert!(
            status
                .error
                .is_some_and(|error| error.contains("pid was reused"))
        );
        Ok(())
    }
}