dpc-tau-cli-term 0.2.1

A minimal Unix-first coding agent.
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
//! Bounded subprocess execution for CLI prompt/completion commands.
//!
//! This module centralizes stdout limits, elapsed timeouts, inherited-pipe
//! handling, process-group cleanup, and foreground-terminal ownership for
//! interactive prompt/completion commands.

use std::sync::mpsc as path_std_sync_mpsc;
use std::{
    fs as path_std_fs, os as path_std_os, process as path_std_process, sync as path_std_sync,
    time as path_std_time,
};

use nix::sys::signal as path_nix_sys_signal;
use nix::{errno as path_nix_errno, sys as path_nix_sys, unistd as path_nix_unistd};

#[cfg(test)]
mod tests;

use std::io::{self, Read, Write as _};
#[cfg(unix)]
use std::os::fd::AsFd as _;
#[cfg(unix)]
use std::os::unix::process::CommandExt as _;
#[cfg(test)]
use std::sync::Mutex;
#[cfg(test)]
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicUsize, Ordering};

const POST_EXIT_PIPE_CLOSE_TIMEOUT: std::time::Duration = path_std_time::Duration::from_secs(1);
#[cfg(test)]
static FAIL_NEXT_FOREGROUND_CLAIM: AtomicBool = AtomicBool::new(false);
#[cfg(test)]
thread_local! {
    /// Per-test-thread restoration failure injection.
    static FAIL_FOREGROUND_RESTORE_LOCAL: std::cell::Cell<bool> =
        const { std::cell::Cell::new(false) };
}
#[cfg(test)]
pub(super) static FAIL_FOREGROUND_RESTORE: ForegroundRestoreFailureInjection =
    ForegroundRestoreFailureInjection;
#[cfg(test)]
static FOREGROUND_RESTORE_ATTEMPTS: AtomicUsize = AtomicUsize::new(0);
#[cfg(test)]
static LAST_FAILED_FOREGROUND_CHILD_ID: AtomicU32 = AtomicU32::new(0);
#[cfg(test)]
pub(super) static FOREGROUND_CLAIM_TEST_LOCK: Mutex<()> = Mutex::new(());

/// Test-only foreground-restoration failure switch isolated per test thread.
#[cfg(test)]
pub(super) struct ForegroundRestoreFailureInjection;

#[cfg(test)]
impl ForegroundRestoreFailureInjection {
    /// Sets restoration failure injection for the current test thread.
    pub(super) fn store(&self, enabled: bool, _ordering: Ordering) {
        FAIL_FOREGROUND_RESTORE_LOCAL.set(enabled);
    }

    /// Reads restoration failure injection for the current test thread.
    fn load(&self, _ordering: Ordering) -> bool {
        FAIL_FOREGROUND_RESTORE_LOCAL.get()
    }
}

/// How much subprocess ownership the bounded runner should take on failures.
///
/// Use [`ProcessOwnership::ProcessGroup`] for bounded non-interactive
/// completion/git helpers: a separate process group lets Tau kill descendants
/// that inherit stdout without changing foreground terminal ownership. Use
/// [`ProcessOwnership::ForegroundProcessGroup`] for terminal-releasing prompt
/// shell actions, where interactive descendants also need temporary foreground
/// control of the terminal.
#[derive(Clone, Copy)]
pub(crate) enum ProcessOwnership {
    /// Put the child in a new process group and terminate that group on errors,
    /// without changing foreground terminal ownership.
    ProcessGroup,
    /// Put the child in a new process group, hand it foreground terminal
    /// ownership, and terminate that group on errors.
    ForegroundProcessGroup,
}

/// Captured output and exit status from a bounded subprocess.
#[derive(Debug)]
pub(crate) struct BoundedCommandOutput {
    /// Direct child exit status.
    pub(crate) status: std::process::ExitStatus,
    /// Captured stdout, guaranteed to be at most the configured byte limit.
    pub(crate) stdout: Vec<u8>,
}

/// Exit status from a bounded subprocess whose stdio is owned by the caller.
#[derive(Debug)]
pub(crate) struct BoundedCommandStatus {
    /// Direct child exit status.
    pub(crate) status: std::process::ExitStatus,
}

/// Failure from a bounded subprocess, including terminal-ownership failures.
#[derive(Debug)]
pub(crate) enum BoundedCommandError {
    /// The command failed while Tau's foreground ownership was confirmed.
    Command(String),
    /// Tau could not confirm foreground ownership after settling the child.
    ForegroundOwnershipUnconfirmed {
        /// Child outcome or command error observed before restoration.
        primary: String,
        /// Error returned by the checked foreground-restoration attempt.
        restoration: ForegroundRestorationError,
    },
}

impl BoundedCommandError {
    /// Returns whether terminal input and redraw must remain paused.
    pub(crate) fn is_foreground_ownership_unconfirmed(&self) -> bool {
        matches!(self, Self::ForegroundOwnershipUnconfirmed { .. })
    }

    /// Returns the bounded restoration diagnostic for an ownership fail-stop.
    pub(crate) fn foreground_restoration_diagnostic(
        &self,
    ) -> Option<ForegroundRestorationDiagnostic> {
        match self {
            Self::ForegroundOwnershipUnconfirmed { restoration, .. } => {
                Some(restoration.diagnostic())
            }
            Self::Command(_) => None,
        }
    }
}

impl std::fmt::Display for BoundedCommandError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Command(error) => formatter.write_str(error),
            Self::ForegroundOwnershipUnconfirmed {
                primary,
                restoration,
            } => write!(
                formatter,
                "terminal foreground ownership remains unconfirmed after {primary}: {restoration}"
            ),
        }
    }
}

impl std::error::Error for BoundedCommandError {}

impl From<String> for BoundedCommandError {
    fn from(error: String) -> Self {
        Self::Command(error)
    }
}

/// Bounded private diagnostic for one terminal foreground-restoration failure.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ForegroundRestorationDiagnostic {
    /// Fixed failure class suitable for private operational logging.
    class: &'static str,
    /// Platform errno from the failed syscall, when one caused the failure.
    errno: Option<i32>,
}

impl ForegroundRestorationDiagnostic {
    /// Builds the fixed diagnostic for an unconfirmed `tcsetpgrp` failure.
    #[must_use]
    pub fn tcsetpgrp_unconfirmed(errno: i32) -> Self {
        Self {
            class: "tcsetpgrp-unconfirmed",
            errno: Some(errno),
        }
    }

    /// Returns the fixed restoration failure class.
    #[must_use]
    pub fn class(self) -> &'static str {
        self.class
    }

    /// Returns the platform errno when the failure originated from a syscall.
    #[must_use]
    pub fn errno(self) -> Option<i32> {
        self.errno
    }
}

/// Checked terminal foreground-restoration failure.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct ForegroundRestorationError {
    /// Fixed private diagnostic class.
    class: &'static str,
    /// Platform error from the failed ownership syscall, when present.
    errno: Option<path_nix_errno::Errno>,
}

impl ForegroundRestorationError {
    /// Builds the bounded failure returned when `tcsetpgrp` remains
    /// unconfirmed.
    pub(crate) fn tcsetpgrp_unconfirmed(errno: path_nix_errno::Errno) -> Self {
        Self {
            class: "tcsetpgrp-unconfirmed",
            errno: Some(errno),
        }
    }

    /// Builds the failure returned when Tau was not initially in the
    /// foreground.
    fn initial_foreground_mismatch() -> Self {
        Self {
            class: "initial-foreground-mismatch",
            errno: None,
        }
    }

    /// Builds the failure returned when initial foreground ownership cannot be
    /// read.
    fn initial_foreground_unconfirmed(errno: path_nix_errno::Errno) -> Self {
        Self {
            class: "initial-foreground-unconfirmed",
            errno: Some(errno),
        }
    }

    /// Builds the failure returned when a failed handoff leaves ownership
    /// unknown.
    fn foreground_handoff_unconfirmed(errno: path_nix_errno::Errno) -> Self {
        Self {
            class: "foreground-handoff-unconfirmed",
            errno: Some(errno),
        }
    }

    /// Projects the bounded fields retained for private UI logging.
    fn diagnostic(self) -> ForegroundRestorationDiagnostic {
        ForegroundRestorationDiagnostic {
            class: self.class,
            errno: self.errno.map(|errno| errno as i32),
        }
    }
}

impl std::fmt::Display for ForegroundRestorationError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str(self.class)?;
        if let Some(errno) = self.errno {
            write!(formatter, ": {errno}")?;
        }
        Ok(())
    }
}

/// Runs a child process while bounding captured stdout and elapsed time.
///
/// Callers must configure stderr before calling. This helper always captures
/// stdout and sets stdin to a pipe when `stdin_input` is present; otherwise
/// callers should configure stdin explicitly (usually `Stdio::null()`). Stdout
/// is drained on a background thread before any optional stdin write is
/// attempted, so a child that writes before reading cannot deadlock the prompt.
/// If stdout crosses `stdout_limit`, if stdin writing fails for anything other
/// than `BrokenPipe`, if the direct child exceeds `timeout`, or if inherited
/// pipes do not close shortly after the direct child exits, the child or owned
/// process group is killed/reaped when possible and an error is returned.
pub(crate) fn run_with_bounded_stdout(
    command: &mut std::process::Command,
    stdin_input: Option<&[u8]>,
    stdout_limit: usize,
    timeout: std::time::Duration,
    ownership: ProcessOwnership,
) -> Result<BoundedCommandOutput, BoundedCommandError> {
    run_with_bounded_stdout_after_spawn(
        command,
        stdin_input,
        stdout_limit,
        timeout,
        ownership,
        || Ok(()),
    )
}

/// Runs a bounded command after allowing a test fixture to observe its spawn.
pub(crate) fn run_with_bounded_stdout_after_spawn(
    command: &mut std::process::Command,
    stdin_input: Option<&[u8]>,
    stdout_limit: usize,
    timeout: std::time::Duration,
    ownership: ProcessOwnership,
    after_spawn: impl FnOnce() -> Result<(), String>,
) -> Result<BoundedCommandOutput, BoundedCommandError> {
    configure_process_ownership(command, ownership)?;
    if stdin_input.is_some() {
        command.stdin(path_std_process::Stdio::piped());
    }
    let mut child = command
        .spawn()
        .map_err(|e| BoundedCommandError::Command(format!("could not spawn command: {e}")))?;
    let mut process_group = match claim_process_group_handle(ownership, child.id()) {
        Ok(process_group) => process_group,
        Err(error) => {
            #[cfg(test)]
            LAST_FAILED_FOREGROUND_CHILD_ID.store(child.id(), Ordering::SeqCst);
            let child_pgid = match ownership {
                ProcessOwnership::ProcessGroup | ProcessOwnership::ForegroundProcessGroup => {
                    process_group_id(child.id())
                }
            };
            terminate_child(&mut child, child_pgid);
            return Err(error);
        }
    };
    if let Err(error) = after_spawn() {
        terminate_child(&mut child, process_group.child_pgid());
        return settle_after_child(&mut process_group, Err(error), |_| {
            unreachable!("after-spawn failure has no successful child output")
        });
    }

    match BoundedStdoutRun::start(child, process_group, stdin_input, stdout_limit) {
        Ok(run) => run.finish(timeout),
        Err(BoundedStdoutStartFailure {
            mut process_group,
            primary,
        }) => settle_after_child(&mut process_group, Err(primary), |_| {
            unreachable!("captured setup failure has no successful child output")
        }),
    }
}

/// State for one bounded stdout child run.
///
/// The direct-child waiter, stdout reader, and optional stdin writer each send
/// completion events into one channel. [`Self::finish`] blocks on that channel
/// with the command deadline as its `recv_timeout` limit, so child monitoring
/// does not need a poll/sleep loop. After the direct child exits, stdout and
/// stdin still receive short bounded post-exit grace waits; those waits detect
/// descendants that inherited prompt-owned pipes without letting Tau wait
/// indefinitely. Cleanup and post-exit waits may observe unrelated pending
/// events, so event handling must remain valid in every phase of the run.
struct BoundedStdoutRun {
    /// Owned process-group/foreground-terminal handle for the child.
    process_group: ProcessGroupHandle,
    /// Child/writer/reader event channel.
    event_rx: path_std_sync::mpsc::Receiver<BoundedStdoutEvent>,
    /// Completed stdout read observed before the direct child exited.
    stdout_result: Option<io::Result<LimitedRead>>,
    /// Whether the optional stdin writer has completed.
    stdin_done: bool,
    /// Completed direct-child wait result.
    child_result: Option<io::Result<std::process::ExitStatus>>,
    /// Maximum number of stdout bytes allowed before terminating the child.
    stdout_limit: usize,
}

/// Captured-command setup failure retaining foreground cleanup authority.
struct BoundedStdoutStartFailure {
    /// Process-group handle that must perform checked foreground restoration.
    process_group: ProcessGroupHandle,
    /// Primary setup failure observed after foreground transfer.
    primary: String,
}

impl BoundedStdoutRun {
    /// Starts background pipe handling for a spawned child.
    fn start(
        mut child: std::process::Child,
        process_group: ProcessGroupHandle,
        stdin_input: Option<&[u8]>,
        stdout_limit: usize,
    ) -> Result<Self, BoundedStdoutStartFailure> {
        let Some(stdout) = child.stdout.take() else {
            terminate_child(&mut child, process_group.child_pgid());
            return Err(BoundedStdoutStartFailure {
                process_group,
                primary: "command stdout was not captured".to_owned(),
            });
        };
        let (event_tx, event_rx) = path_std_sync::mpsc::channel();
        spawn_stdout_reader(stdout, stdout_limit, event_tx.clone());
        let stdin_done = match spawn_stdin_writer(&mut child, stdin_input, event_tx.clone()) {
            StdinWriterState::Done => true,
            StdinWriterState::Running => false,
        };
        spawn_bounded_stdout_child_waiter(child, event_tx);
        Ok(Self {
            process_group,
            event_rx,
            stdout_result: None,
            stdin_done,
            child_result: None,
            stdout_limit,
        })
    }

    /// Waits until the child exits, a pipe error occurs, or the timeout
    /// expires.
    fn finish(
        mut self,
        timeout: std::time::Duration,
    ) -> Result<BoundedCommandOutput, BoundedCommandError> {
        let result = self.wait_for_completion(timeout);
        settle_after_child(&mut self.process_group, result, |output| {
            format!("command exited with {}", output.status)
        })
    }

    fn wait_for_completion(
        &mut self,
        timeout: std::time::Duration,
    ) -> Result<BoundedCommandOutput, String> {
        let deadline = path_std_time::Instant::now() + timeout;
        loop {
            let Some(remaining) = deadline.checked_duration_since(path_std_time::Instant::now())
            else {
                self.terminate();
                self.wait_for_stdin_writer();
                return Err(format!("command exceeded {}s timeout", timeout.as_secs()));
            };

            match self.event_rx.recv_timeout(remaining) {
                Ok(event) => self.handle_event(event)?,
                Err(path_std_sync_mpsc::RecvTimeoutError::Timeout) => {
                    self.terminate();
                    self.wait_for_stdin_writer();
                    return Err(format!("command exceeded {}s timeout", timeout.as_secs()));
                }
                Err(path_std_sync_mpsc::RecvTimeoutError::Disconnected) => {
                    self.terminate();
                    self.wait_for_stdin_writer();
                    return Err("command monitor stopped unexpectedly".to_owned());
                }
            }

            if let Some(output) = self.take_child_output()? {
                return Ok(output);
            }
        }
    }

    /// Incorporates a completed child, stdin, or stdout event.
    fn handle_event(&mut self, event: BoundedStdoutEvent) -> Result<(), String> {
        match event {
            BoundedStdoutEvent::Stdout(Ok(stdout)) if stdout.overflowed => {
                self.terminate();
                self.wait_for_stdin_writer();
                Err(format!(
                    "command stdout exceeded {} bytes",
                    self.stdout_limit
                ))
            }
            BoundedStdoutEvent::Stdout(Err(error)) => {
                self.terminate();
                self.wait_for_stdin_writer();
                Err(format!("could not read command stdout: {error}"))
            }
            BoundedStdoutEvent::Stdout(result) => {
                self.stdout_result = Some(result);
                Ok(())
            }
            BoundedStdoutEvent::Stdin(Ok(())) => {
                self.stdin_done = true;
                Ok(())
            }
            BoundedStdoutEvent::Stdin(Err(error)) => {
                self.terminate();
                Err(format!("could not write to command stdin: {error}"))
            }
            BoundedStdoutEvent::Child(result) => {
                self.child_result = Some(result);
                Ok(())
            }
        }
    }

    /// Checks direct-child status and collects final pipe results after exit.
    fn take_child_output(&mut self) -> Result<Option<BoundedCommandOutput>, String> {
        let Some(result) = self.child_result.take() else {
            return Ok(None);
        };
        let status = match result {
            Ok(status) => status,
            Err(error) => {
                self.terminate_process_group_if_owned();
                return Err(format!("could not wait for command: {error}"));
            }
        };
        let stdout = match self.receive_stdout_after_child_exit() {
            Ok(stdout) => stdout,
            Err(error) => {
                self.terminate_process_group_if_owned();
                return Err(error);
            }
        };
        if let Err(error) = self.wait_for_stdin_after_child_exit() {
            self.terminate_process_group_if_owned();
            return Err(error);
        }
        Ok(Some(BoundedCommandOutput {
            status,
            stdout: stdout.bytes,
        }))
    }

    /// Waits briefly for stdout EOF after the direct child has exited.
    fn receive_stdout_after_child_exit(&mut self) -> Result<LimitedRead, String> {
        while self.stdout_result.is_none() {
            let event = self
                .event_rx
                .recv_timeout(POST_EXIT_PIPE_CLOSE_TIMEOUT)
                .map_err(|_| "command stdout pipe did not close after child exit".to_owned())?;
            self.handle_event(event)?;
        }
        let stdout_result = self
            .stdout_result
            .take()
            .expect("stdout result must be available after post-exit wait");
        receive_stdout_result(stdout_result, self.stdout_limit)
    }

    /// Waits briefly for the stdin writer after the direct child has exited.
    fn wait_for_stdin_after_child_exit(&mut self) -> Result<(), String> {
        while !self.stdin_done {
            let event = self
                .event_rx
                .recv_timeout(POST_EXIT_PIPE_CLOSE_TIMEOUT)
                .map_err(|_| "command stdin pipe did not close after child exit".to_owned())?;
            self.handle_event(event)?;
        }
        Ok(())
    }

    /// Terminates the direct child or its process group and reaps it.
    fn terminate(&mut self) {
        terminate_process_group_if_owned(self.process_group.child_pgid());
        let _ = self.wait_for_child_after_terminate();
    }

    /// Terminates the owned process group without waiting for the
    /// already-exited child.
    fn terminate_process_group_if_owned(&self) {
        terminate_process_group_if_owned(self.process_group.child_pgid());
    }

    /// Waits briefly for an in-flight stdin writer after child termination.
    fn wait_for_stdin_writer(&mut self) {
        let _ = self.wait_for_stdin_after_child_exit();
    }

    /// Waits briefly for the child waiter after sending termination.
    fn wait_for_child_after_terminate(&mut self) -> Result<(), String> {
        while self.child_result.is_none() {
            let event = self
                .event_rx
                .recv_timeout(POST_EXIT_PIPE_CLOSE_TIMEOUT)
                .map_err(|_| "command child did not exit after termination".to_owned())?;
            self.handle_event(event)?;
        }
        Ok(())
    }
}

/// Events emitted by bounded stdout helper threads.
enum BoundedStdoutEvent {
    /// Direct child completed.
    Child(io::Result<std::process::ExitStatus>),
    /// Captured stdout reader completed.
    Stdout(io::Result<LimitedRead>),
    /// Optional stdin writer completed.
    Stdin(io::Result<()>),
}

/// State of the optional stdin writer after setup.
enum StdinWriterState {
    /// No stdin writer is active.
    Done,
    /// A stdin writer thread was spawned and will report completion.
    Running,
}

fn spawn_stdout_reader(
    stdout: impl Read + Send + 'static,
    stdout_limit: usize,
    event_tx: path_std_sync::mpsc::Sender<BoundedStdoutEvent>,
) {
    std::thread::spawn(move || {
        let _ = event_tx.send(BoundedStdoutEvent::Stdout(read_to_limit(
            stdout,
            stdout_limit,
        )));
    });
}

fn spawn_stdin_writer(
    child: &mut std::process::Child,
    input: Option<&[u8]>,
    event_tx: path_std_sync::mpsc::Sender<BoundedStdoutEvent>,
) -> StdinWriterState {
    let Some(input) = input else {
        return StdinWriterState::Done;
    };
    let Some(mut stdin) = child.stdin.take() else {
        return StdinWriterState::Done;
    };
    let input = input.to_vec();
    std::thread::spawn(move || {
        let result = match stdin.write_all(&input) {
            Ok(()) => Ok(()),
            Err(error) if error.kind() == io::ErrorKind::BrokenPipe => Ok(()),
            Err(error) => Err(error),
        };
        let _ = event_tx.send(BoundedStdoutEvent::Stdin(result));
    });
    StdinWriterState::Running
}

fn spawn_bounded_stdout_child_waiter(
    mut child: std::process::Child,
    event_tx: path_std_sync::mpsc::Sender<BoundedStdoutEvent>,
) {
    std::thread::spawn(move || {
        let _ = event_tx.send(BoundedStdoutEvent::Child(child.wait()));
    });
}

fn spawn_child_waiter(
    mut child: std::process::Child,
) -> path_std_sync::mpsc::Receiver<io::Result<std::process::ExitStatus>> {
    let (tx, rx) = path_std_sync::mpsc::channel();
    std::thread::spawn(move || {
        let _ = tx.send(child.wait());
    });
    rx
}

/// Bounded stdout read result from the reader thread.
struct LimitedRead {
    /// Captured bytes up to the configured limit.
    bytes: Vec<u8>,
    /// Whether the reader observed more bytes than the configured limit.
    overflowed: bool,
}

/// Runs a child process while bounding elapsed time without capturing output.
///
/// Callers must configure stdin/stdout/stderr before calling. This is intended
/// for terminal-releasing prompt edit actions where the child needs the real
/// terminal file descriptors rather than Tau-owned pipes. Process-group and
/// foreground ownership behavior matches [`run_with_bounded_stdout`].
pub(crate) fn run_with_inherited_stdio(
    command: &mut std::process::Command,
    timeout: std::time::Duration,
    ownership: ProcessOwnership,
) -> Result<BoundedCommandStatus, BoundedCommandError> {
    run_with_inherited_stdio_after_spawn(command, timeout, ownership, || Ok(()))
}

/// Runs an inherited-stdio command after exposing deterministic child
/// readiness.
fn run_with_inherited_stdio_after_spawn(
    command: &mut std::process::Command,
    timeout: std::time::Duration,
    ownership: ProcessOwnership,
    after_spawn: impl FnOnce() -> Result<(), String>,
) -> Result<BoundedCommandStatus, BoundedCommandError> {
    configure_process_ownership(command, ownership)?;
    let mut child = command
        .spawn()
        .map_err(|e| BoundedCommandError::Command(format!("could not spawn command: {e}")))?;
    let mut process_group = match claim_process_group_handle(ownership, child.id()) {
        Ok(process_group) => process_group,
        Err(error) => {
            #[cfg(test)]
            LAST_FAILED_FOREGROUND_CHILD_ID.store(child.id(), Ordering::SeqCst);
            let child_pgid = match ownership {
                ProcessOwnership::ProcessGroup | ProcessOwnership::ForegroundProcessGroup => {
                    process_group_id(child.id())
                }
            };
            terminate_child(&mut child, child_pgid);
            return Err(error);
        }
    };
    if let Err(error) = after_spawn() {
        terminate_child(&mut child, process_group.child_pgid());
        return settle_after_child(&mut process_group, Err(error), |_| {
            unreachable!("after-spawn failure has no successful child status")
        });
    }

    let child_rx = spawn_child_waiter(child);
    let result = match child_rx.recv_timeout(timeout) {
        Ok(Ok(status)) => Ok(BoundedCommandStatus { status }),
        Ok(Err(error)) => {
            terminate_process_group_if_owned(process_group.child_pgid());
            Err(format!("could not wait for command: {error}"))
        }
        Err(path_std_sync_mpsc::RecvTimeoutError::Timeout) => {
            terminate_process_group_if_owned(process_group.child_pgid());
            let _ = child_rx.recv_timeout(POST_EXIT_PIPE_CLOSE_TIMEOUT);
            Err(format!("command exceeded {}s timeout", timeout.as_secs()))
        }
        Err(path_std_sync_mpsc::RecvTimeoutError::Disconnected) => {
            terminate_process_group_if_owned(process_group.child_pgid());
            Err("command monitor stopped unexpectedly".to_owned())
        }
    };
    settle_after_child(&mut process_group, result, |output| {
        format!("command exited with {}", output.status)
    })
}

/// Restores foreground ownership after a child has been fully settled.
fn settle_after_child<T>(
    process_group: &mut ProcessGroupHandle,
    primary: Result<T, String>,
    describe_success: impl FnOnce(&T) -> String,
) -> Result<T, BoundedCommandError> {
    settle_after_child_with_restore(primary, describe_success, || {
        process_group.restore_foreground()
    })
}

/// Combines the primary child result with one checked restoration attempt.
fn settle_after_child_with_restore<T>(
    primary: Result<T, String>,
    describe_success: impl FnOnce(&T) -> String,
    restore_foreground: impl FnOnce() -> Result<(), ForegroundRestorationError>,
) -> Result<T, BoundedCommandError> {
    let primary_description = match &primary {
        Ok(value) => describe_success(value),
        Err(error) => format!("command failure ({error})"),
    };
    match restore_foreground() {
        Ok(()) => primary.map_err(BoundedCommandError::Command),
        Err(restoration) => Err(BoundedCommandError::ForegroundOwnershipUnconfirmed {
            primary: primary_description,
            restoration,
        }),
    }
}

fn read_to_limit(mut reader: impl Read, limit: usize) -> io::Result<LimitedRead> {
    let mut bytes = Vec::new();
    let mut buf = [0; 8192];
    loop {
        let n = reader.read(&mut buf)?;
        if n == 0 {
            break;
        }
        let remaining = limit.saturating_sub(bytes.len());
        if remaining < n {
            bytes.extend_from_slice(&buf[..remaining]);
            return Ok(LimitedRead {
                bytes,
                overflowed: true,
            });
        }
        bytes.extend_from_slice(&buf[..n]);
    }
    Ok(LimitedRead {
        bytes,
        overflowed: false,
    })
}

fn receive_stdout_result(
    stdout_result: io::Result<LimitedRead>,
    stdout_limit: usize,
) -> Result<LimitedRead, String> {
    let stdout = stdout_result.map_err(|e| format!("could not read command stdout: {e}"))?;
    if stdout.overflowed {
        return Err(format!("command stdout exceeded {} bytes", stdout_limit));
    }
    Ok(stdout)
}

/// Process group id assigned to a spawned child.
#[derive(Clone, Copy)]
struct ChildProcessGroupId(i32);

impl ChildProcessGroupId {
    /// Builds the child process group id from the direct child pid because
    /// owned subprocesses are spawned with `process_group(0)`.
    fn from_child_id(child_id: u32) -> Self {
        Self(child_id as i32)
    }

    #[cfg(unix)]
    fn as_nix_pid(self) -> nix::unistd::Pid {
        path_nix_unistd::Pid::from_raw(self.0)
    }
}

fn terminate_child(child: &mut std::process::Child, child_pgid: Option<ChildProcessGroupId>) {
    if let Some(child_pgid) = child_pgid {
        terminate_process_group(child_pgid);
    } else {
        let _ = child.kill();
    }
    let _ = child.wait();
}

fn terminate_process_group_if_owned(child_pgid: Option<ChildProcessGroupId>) {
    if let Some(child_pgid) = child_pgid {
        terminate_process_group(child_pgid);
    }
}

fn terminate_process_group(child_pgid: ChildProcessGroupId) {
    #[cfg(unix)]
    {
        let pgid = child_pgid.as_nix_pid();
        let _ = path_nix_sys::signal::killpg(pgid, path_nix_sys_signal::Signal::SIGTERM);
        std::thread::sleep(path_std_time::Duration::from_millis(100));
        let _ = path_nix_sys::signal::killpg(pgid, path_nix_sys_signal::Signal::SIGKILL);
    }
}

fn configure_process_ownership(
    command: &mut std::process::Command,
    ownership: ProcessOwnership,
) -> Result<(), String> {
    match ownership {
        ProcessOwnership::ProcessGroup | ProcessOwnership::ForegroundProcessGroup => {
            configure_process_group(command)
        }
    }
}

#[cfg(unix)]
fn configure_process_group(command: &mut std::process::Command) -> Result<(), String> {
    command.process_group(0);
    Ok(())
}

#[cfg(not(unix))]
fn configure_process_group(_command: &mut std::process::Command) -> Result<(), String> {
    Ok(())
}

fn process_group_id(child_id: u32) -> Option<ChildProcessGroupId> {
    #[cfg(unix)]
    {
        Some(ChildProcessGroupId::from_child_id(child_id))
    }
    #[cfg(not(unix))]
    {
        let _ = child_id;
        None
    }
}

/// Foreground terminal/process-group state for an owned prompt action.
struct ProcessGroupHandle {
    /// Process group id assigned to the direct child and its descendants.
    child_pgid: Option<ChildProcessGroupId>,
    /// Tau's foreground process group, restored before the prompt resumes.
    #[cfg(unix)]
    parent_pgid: Option<nix::unistd::Pid>,
}

impl ProcessGroupHandle {
    fn new(ownership: ProcessOwnership, child_id: u32) -> Result<Self, BoundedCommandError> {
        match ownership {
            ProcessOwnership::ProcessGroup => Ok(Self {
                child_pgid: process_group_id(child_id),
                #[cfg(unix)]
                parent_pgid: None,
            }),
            ProcessOwnership::ForegroundProcessGroup => Self::claim_foreground(child_id),
        }
    }

    fn child_pgid(&self) -> Option<ChildProcessGroupId> {
        self.child_pgid
    }

    fn restore_foreground(&mut self) -> Result<(), ForegroundRestorationError> {
        #[cfg(unix)]
        if let Some(parent_pgid) = self.parent_pgid {
            #[cfg(test)]
            if FAIL_FOREGROUND_RESTORE.load(Ordering::SeqCst) {
                FOREGROUND_RESTORE_ATTEMPTS.fetch_add(1, Ordering::SeqCst);
                return Err(ForegroundRestorationError::tcsetpgrp_unconfirmed(
                    path_nix_errno::Errno::EIO,
                ));
            }
            restore_foreground_process_group(parent_pgid)?;
            self.parent_pgid = None;
        }
        Ok(())
    }

    #[cfg(unix)]
    fn claim_foreground(child_id: u32) -> Result<Self, BoundedCommandError> {
        let tau_pgid = nix::unistd::getpgrp();
        let child_pgid = ChildProcessGroupId::from_child_id(child_id);
        let claimed_foreground = claim_foreground_process_group(tau_pgid, child_pgid.as_nix_pid())?;
        if claimed_foreground {
            let _ = path_nix_sys::signal::killpg(
                child_pgid.as_nix_pid(),
                path_nix_sys_signal::Signal::SIGCONT,
            );
        }
        #[cfg(test)]
        let claimed_foreground =
            claimed_foreground || FAIL_FOREGROUND_RESTORE.load(Ordering::SeqCst);
        Ok(Self {
            child_pgid: Some(child_pgid),
            parent_pgid: claimed_foreground.then_some(tau_pgid),
        })
    }

    #[cfg(not(unix))]
    fn claim_foreground(_child_id: u32) -> Result<Self, BoundedCommandError> {
        Err(BoundedCommandError::Command(
            "foreground process-group prompt actions are unsupported on this platform".to_owned(),
        ))
    }
}

#[cfg(unix)]
impl Drop for ProcessGroupHandle {
    fn drop(&mut self) {
        let _ = self.restore_foreground();
    }
}

#[cfg(unix)]
fn claim_foreground_process_group(
    tau_pgid: nix::unistd::Pid,
    child_pgid: nix::unistd::Pid,
) -> Result<bool, BoundedCommandError> {
    match path_std_fs::OpenOptions::new()
        .read(true)
        .write(true)
        .open("/dev/tty")
    {
        Ok(tty) => claim_foreground_process_group_with(
            tau_pgid,
            || nix::unistd::tcgetpgrp(tty.as_fd()),
            || tcsetpgrp_blocking_sigtou(tty.as_fd(), child_pgid),
        ),
        Err(_) => claim_foreground_process_group_with(
            tau_pgid,
            || nix::unistd::tcgetpgrp(std::io::stdin().as_fd()),
            || tcsetpgrp_blocking_sigtou(std::io::stdin().as_fd(), child_pgid),
        ),
    }
}

#[cfg(unix)]
fn claim_foreground_process_group_with(
    tau_pgid: nix::unistd::Pid,
    mut get_foreground: impl FnMut() -> nix::Result<nix::unistd::Pid>,
    mut set_child_foreground: impl FnMut() -> nix::Result<()>,
) -> Result<bool, BoundedCommandError> {
    loop {
        match get_foreground() {
            Ok(foreground) if foreground == tau_pgid => break,
            Ok(_) => {
                return Err(BoundedCommandError::ForegroundOwnershipUnconfirmed {
                    primary: "prompt action foreground handoff".to_owned(),
                    restoration: ForegroundRestorationError::initial_foreground_mismatch(),
                });
            }
            Err(path_nix_errno::Errno::EINTR) => {}
            Err(path_nix_errno::Errno::ENOTTY) => return Ok(false),
            Err(error) => {
                return Err(BoundedCommandError::ForegroundOwnershipUnconfirmed {
                    primary: "prompt action foreground handoff".to_owned(),
                    restoration: ForegroundRestorationError::initial_foreground_unconfirmed(error),
                });
            }
        }
    }

    let set_error = loop {
        match set_child_foreground() {
            Ok(()) => return Ok(true),
            Err(path_nix_errno::Errno::EINTR) => {}
            Err(error) => break error,
        }
    };
    let tau_still_foreground = loop {
        match get_foreground() {
            Ok(foreground) => break foreground == tau_pgid,
            Err(path_nix_errno::Errno::EINTR) => {}
            Err(_) => break false,
        }
    };
    if tau_still_foreground {
        Err(BoundedCommandError::Command(format!(
            "could not hand terminal to prompt action: {set_error}"
        )))
    } else {
        Err(BoundedCommandError::ForegroundOwnershipUnconfirmed {
            primary: "prompt action foreground handoff".to_owned(),
            restoration: ForegroundRestorationError::foreground_handoff_unconfirmed(set_error),
        })
    }
}

#[cfg(unix)]
fn restore_foreground_process_group(
    pgid: nix::unistd::Pid,
) -> Result<(), ForegroundRestorationError> {
    match path_std_fs::OpenOptions::new()
        .read(true)
        .write(true)
        .open("/dev/tty")
    {
        Ok(tty) => restore_foreground_process_group_with(
            pgid,
            || tcsetpgrp_blocking_sigtou(tty.as_fd(), pgid),
            || nix::unistd::tcgetpgrp(tty.as_fd()),
        ),
        Err(_) => restore_foreground_process_group_with(
            pgid,
            || tcsetpgrp_blocking_sigtou(std::io::stdin().as_fd(), pgid),
            || nix::unistd::tcgetpgrp(std::io::stdin().as_fd()),
        ),
    }
}

#[cfg(unix)]
fn restore_foreground_process_group_with(
    pgid: nix::unistd::Pid,
    mut set_foreground: impl FnMut() -> nix::Result<()>,
    mut get_foreground: impl FnMut() -> nix::Result<nix::unistd::Pid>,
) -> Result<(), ForegroundRestorationError> {
    let set_error = loop {
        match set_foreground() {
            Ok(()) => return Ok(()),
            Err(path_nix_errno::Errno::EINTR) => {}
            Err(error) => break error,
        }
    };
    let foreground = loop {
        match get_foreground() {
            Ok(foreground) => break Some(foreground),
            Err(path_nix_errno::Errno::EINTR) => {}
            Err(_) => break None,
        }
    };
    if foreground == Some(pgid) {
        Ok(())
    } else {
        Err(ForegroundRestorationError::tcsetpgrp_unconfirmed(set_error))
    }
}

#[cfg(unix)]
fn tcsetpgrp_blocking_sigtou(
    fd: path_std_os::fd::BorrowedFd<'_>,
    pgid: nix::unistd::Pid,
) -> nix::Result<()> {
    let mut block = path_nix_sys_signal::SigSet::empty();
    block.add(path_nix_sys_signal::Signal::SIGTTOU);
    let mut previous = path_nix_sys_signal::SigSet::empty();
    path_nix_sys::signal::pthread_sigmask(
        path_nix_sys_signal::SigmaskHow::SIG_BLOCK,
        Some(&block),
        Some(&mut previous),
    )?;
    let result = nix::unistd::tcsetpgrp(fd, pgid);
    let restore = path_nix_sys::signal::pthread_sigmask(
        path_nix_sys_signal::SigmaskHow::SIG_SETMASK,
        Some(&previous),
        None,
    );
    result.and(restore)
}

fn claim_process_group_handle(
    ownership: ProcessOwnership,
    child_id: u32,
) -> Result<ProcessGroupHandle, BoundedCommandError> {
    #[cfg(test)]
    if matches!(ownership, ProcessOwnership::ForegroundProcessGroup)
        && FAIL_NEXT_FOREGROUND_CLAIM.swap(false, Ordering::SeqCst)
    {
        return Err(BoundedCommandError::Command(
            "could not hand terminal to prompt action: injected failure".to_owned(),
        ));
    }
    ProcessGroupHandle::new(ownership, child_id)
}