termproof 0.3.2

Evidence-first verification for TUI and terminal applications: recipe model, schema, steps, assertions, orchestration, terminal sessions and the evidence pipeline
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
//! Non-PTY process sessions for TermProof (RUST-005).
//!
//! This module owns a child process without allocating a pseudo-terminal.
//! It is the `scripted_process` execution path: `argv` is executed directly,
//! `cwd` and `env` are applied, stdin is writable, stdout/stderr are captured
//! into a single `raw_output` byte log, and exit status is preserved.
//!
//! # Merge dependency
//!
//! The canonical recipe model (`CommandSpec`, `Recipe`) lands at the crate
//! root under RUST-004. This module scaffolds against the expected
//! interface: `ProcessConfig` mirrors `CommandSpec { argv, cwd, env, pty:false }`
//! and will be constructed from `crate::models::CommandSpec` once the root
//! publishes the typed recipe. The `SessionBackend` trait that will wrap
//! this struct is likewise deferred to root/[`crate::terminal`]
//! integration in RUST-007. Until then the struct is usable directly and via
//! the re-exported helpers below. Treat RUST-004 as a merge prerequisite: no
//! behaviour here depends on an unpublished model, but the final wiring does.
//!
//! # Timeout and partial output
//!
//! `wait_with_timeout` polls the child and, on expiry, terminates the child
//! (and, on Unix with a process group, the group) before returning the output
//! captured up to that point. Partial output is never discarded: even a timed
//! out or killed process yields its `raw_output` so callers can render
//! evidence. No child process is leaked: `close` and `Drop` both ensure the
//! child is reaped.

use std::collections::HashMap;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, ChildStdin, Command, Stdio};
use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};

/// Configuration for a non-PTY child process.
///
/// Mirrors the `command` block of a TermProof recipe when `command.pty` is
/// `false`. Callers that already have a `crate::models::CommandSpec`
/// should map it field-for-field into this struct.
#[derive(Debug, Clone)]
pub struct ProcessConfig {
    /// Argument vector. `argv[0]` is the executable.
    pub argv: Vec<String>,
    /// Working directory for the child. `None` inherits the parent cwd.
    pub cwd: Option<PathBuf>,
    /// Environment overrides. When `inherit_env` is true these are merged over
    /// the parent environment; otherwise they are the entire environment (plus
    /// a default `TERM`).
    pub env: HashMap<String, String>,
    /// Whether to inherit the parent process environment.
    pub inherit_env: bool,
}

impl ProcessConfig {
    /// Create a config that inherits the parent environment.
    pub fn new(argv: Vec<String>) -> Self {
        Self {
            argv,
            cwd: None,
            env: HashMap::new(),
            inherit_env: true,
        }
    }

    /// Set the working directory.
    #[must_use]
    pub fn with_cwd(mut self, cwd: impl Into<PathBuf>) -> Self {
        self.cwd = Some(cwd.into());
        self
    }

    /// Set an environment override.
    #[must_use]
    pub fn with_env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.env.insert(key.into(), value.into());
        self
    }

    /// Control env inheritance.
    #[must_use]
    pub fn with_inherit_env(mut self, inherit: bool) -> Self {
        self.inherit_env = inherit;
        self
    }
}

/// Captured output from a completed or timed-out process.
#[derive(Debug, Clone)]
pub struct ProcessOutput {
    /// Raw stdout bytes.
    pub stdout: Vec<u8>,
    /// Raw stderr bytes.
    pub stderr: Vec<u8>,
    /// Combined stdout+stderr bytes in read order (stdout first, then stderr
    /// chunks as they were drained). Useful for single-stream assertions.
    pub combined: Vec<u8>,
    /// Lossy UTF-8 view of `combined`, with invalid sequences replaced.
    pub combined_str: String,
    /// Exit code if the child has been reaped. `None` if still running.
    pub exit_code: Option<i32>,
    /// Whether the wait timed out and the child was killed.
    pub timed_out: bool,
}

/// Result of `wait_with_timeout`.
#[derive(Debug, Clone)]
pub struct ProcessWaitResult {
    /// Captured output.
    pub output: ProcessOutput,
    /// Whether the deadline expired before the child exited.
    pub timed_out: bool,
}

/// Typed errors for process sessions.
#[derive(Debug)]
pub enum ProcessError {
    /// `argv` was empty.
    EmptyArgv,
    /// Failed to spawn the child.
    SpawnFailed(String),
    /// I/O error on stdin/stdout/stderr.
    Io(String),
    /// Session has not been started.
    NotStarted,
    /// Timeout value was invalid.
    InvalidTimeout(String),
}

impl std::fmt::Display for ProcessError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::EmptyArgv => write!(f, "argv must not be empty"),
            Self::SpawnFailed(msg) => write!(f, "failed to spawn process: {msg}"),
            Self::Io(msg) => write!(f, "process I/O error: {msg}"),
            Self::NotStarted => write!(f, "process session has not been started"),
            Self::InvalidTimeout(msg) => write!(f, "invalid timeout: {msg}"),
        }
    }
}

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

/// Non-PTY process session.
///
/// Owns a single child process, its stdin handle, and background reader
/// threads for stdout/stderr. Call `spawn` before any I/O, then
/// `wait_with_timeout` or `wait` to completion. `close` and `Drop` guarantee
/// the child is terminated and reaped so no zombie or orphan remains.
pub struct ProcessSession {
    config: ProcessConfig,
    child: Option<Child>,
    stdin: Option<ChildStdin>,
    /// Shared buffer for combined output.
    combined: Arc<Mutex<Vec<u8>>>,
    stdout_buf: Arc<Mutex<Vec<u8>>>,
    stderr_buf: Arc<Mutex<Vec<u8>>>,
    stdout_handle: Option<JoinHandle<()>>,
    stderr_handle: Option<JoinHandle<()>>,
    exit_code: Option<i32>,
    timed_out: bool,
    /// Where the child was launched, resolved on a successful spawn. `None`
    /// until then, because a spawn that failed launched nothing. Resolving it
    /// later would be a different fact: this process can change directory
    /// afterwards and the child cannot.
    launched_in: Option<PathBuf>,
}

impl std::fmt::Debug for ProcessSession {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ProcessSession")
            .field("argv", &self.config.argv)
            .field("cwd", &self.config.cwd)
            .field("exit_code", &self.exit_code)
            .field("timed_out", &self.timed_out)
            .finish()
    }
}

impl ProcessSession {
    /// Create a session from a config. The child is not spawned until `spawn`
    /// is called.
    pub fn new(config: ProcessConfig) -> Self {
        Self {
            config,
            child: None,
            stdin: None,
            combined: Arc::new(Mutex::new(Vec::new())),
            stdout_buf: Arc::new(Mutex::new(Vec::new())),
            stderr_buf: Arc::new(Mutex::new(Vec::new())),
            stdout_handle: None,
            stderr_handle: None,
            exit_code: None,
            timed_out: false,
            launched_in: None,
        }
    }

    /// Convenience constructor from argv/cwd/env.
    pub fn from_parts(
        argv: Vec<String>,
        cwd: Option<PathBuf>,
        env: HashMap<String, String>,
    ) -> Self {
        Self::new(ProcessConfig {
            argv,
            cwd,
            env,
            inherit_env: true,
        })
    }

    /// Spawn the child process.
    ///
    /// # Errors
    ///
    /// Returns `ProcessError::EmptyArgv` if argv is empty, or
    /// `ProcessError::SpawnFailed` if the OS rejects the spawn.
    pub fn spawn(&mut self) -> Result<(), ProcessError> {
        if self.config.argv.is_empty() {
            return Err(ProcessError::EmptyArgv);
        }
        if self.child.is_some() {
            return Ok(());
        }
        // The child inherits this process's directory when the config named
        // none. Read it here — before the fork, so it is the directory the
        // child actually gets — but hold it in a local: a spawn that fails
        // launched nothing, and must leave `launch_dir` with nothing to report.
        let inherited = match self.config.cwd {
            Some(_) => None,
            None => std::env::current_dir().ok(),
        };
        let mut cmd = Command::new(&self.config.argv[0]);
        if self.config.argv.len() > 1 {
            cmd.args(&self.config.argv[1..]);
        }
        if let Some(cwd) = &self.config.cwd {
            cmd.current_dir(cwd);
        }
        // env handling: inherit parent env then override, or use only provided.
        if self.config.inherit_env {
            for (k, v) in &self.config.env {
                cmd.env(k, v);
            }
            // Ensure TERM is set (mirrors Python session.py).
            if std::env::var("TERM").unwrap_or_default().is_empty()
                && !self.config.env.contains_key("TERM")
            {
                cmd.env("TERM", "xterm-256color");
            }
        } else {
            cmd.env_clear();
            // Provide TERM even in hermetic mode unless caller set it.
            let mut env = self.config.env.clone();
            env.entry("TERM".to_string())
                .or_insert_with(|| "xterm-256color".to_string());
            for (k, v) in &env {
                cmd.env(k, v);
            }
        }
        cmd.stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        // Process-group isolation on Unix: the `unsafe_code = forbid` lint
        // prohibits `CommandExt::pre_exec` (which is `unsafe`) in this crate.
        // The direct child is therefore killed via `Child::kill()`; descendants
        // that outlive the child are reaped by init. A future change that
        // relaxes the lint to `deny` (per `docs/engineering-baseline.md`
        // §8) can reintroduce `setpgid(0,0)` via `pre_exec` behind a scoped
        // `#[allow(unsafe_code)]` with a `// SAFETY:` comment.

        let mut child = cmd
            .spawn()
            .map_err(|e| ProcessError::SpawnFailed(e.to_string()))?;
        let stdin = child.stdin.take();
        let stdout = child.stdout.take();
        let stderr = child.stderr.take();

        // Spawn reader threads.
        let combined_stdout = Arc::clone(&self.combined);
        let stdout_buf = Arc::clone(&self.stdout_buf);
        let stdout_handle = stdout.map(|mut out| {
            thread::spawn(move || {
                let mut buf = [0u8; 4096];
                loop {
                    match out.read(&mut buf) {
                        Ok(0) => break,
                        Ok(n) => {
                            if let Ok(mut c) = stdout_buf.lock() {
                                c.extend_from_slice(&buf[..n]);
                            }
                            if let Ok(mut c) = combined_stdout.lock() {
                                c.extend_from_slice(&buf[..n]);
                            }
                        }
                        Err(_) => break,
                    }
                }
            })
        });

        let combined_stderr = Arc::clone(&self.combined);
        let stderr_buf = Arc::clone(&self.stderr_buf);
        let stderr_handle = stderr.map(|mut err| {
            thread::spawn(move || {
                let mut buf = [0u8; 4096];
                loop {
                    match err.read(&mut buf) {
                        Ok(0) => break,
                        Ok(n) => {
                            if let Ok(mut c) = stderr_buf.lock() {
                                c.extend_from_slice(&buf[..n]);
                            }
                            if let Ok(mut c) = combined_stderr.lock() {
                                c.extend_from_slice(&buf[..n]);
                            }
                        }
                        Err(_) => break,
                    }
                }
            })
        });

        self.child = Some(child);
        self.stdin = stdin;
        self.stdout_handle = stdout_handle;
        self.stderr_handle = stderr_handle;
        // Only now: "has spawned" and "has a launch directory" are one
        // condition, so a failed spawn cannot leave a directory behind.
        self.launched_in = self.config.cwd.clone().or(inherited);
        Ok(())
    }

    /// Write bytes to the child stdin.
    ///
    /// # Errors
    ///
    /// Returns `NotStarted` if the session has not been spawned.
    pub fn write(&mut self, data: &[u8]) -> Result<(), ProcessError> {
        let stdin = self.stdin.as_mut().ok_or(ProcessError::NotStarted)?;
        stdin
            .write_all(data)
            .map_err(|e| ProcessError::Io(e.to_string()))?;
        stdin.flush().map_err(|e| ProcessError::Io(e.to_string()))?;
        Ok(())
    }

    /// Write a line (text plus trailing newline) to stdin.
    pub fn write_line(&mut self, text: &str) -> Result<(), ProcessError> {
        let mut buf = text.as_bytes().to_vec();
        buf.push(b'\n');
        self.write(&buf)
    }

    /// Send text without a trailing newline.
    pub fn send_text(&mut self, text: &str) -> Result<(), ProcessError> {
        self.write(text.as_bytes())
    }

    /// Send a line with a trailing newline.
    pub fn send_line(&mut self, text: &str) -> Result<(), ProcessError> {
        self.write_line(text)
    }

    /// Close stdin, signalling EOF to the child.
    pub fn send_eof(&mut self) -> Result<(), ProcessError> {
        // Drop stdin handle.
        self.stdin.take();
        Ok(())
    }

    /// Whether the child is still alive.
    pub fn is_alive(&mut self) -> bool {
        if let Some(child) = self.child.as_mut() {
            match child.try_wait() {
                Ok(None) => true,
                Ok(Some(status)) => {
                    if self.exit_code.is_none() {
                        self.exit_code = status.code().or_else(|| {
                            #[cfg(unix)]
                            {
                                use std::os::unix::process::ExitStatusExt;
                                status.signal().map(|s| 128 + s)
                            }
                            #[cfg(not(unix))]
                            {
                                None
                            }
                        });
                    }
                    false
                }
                Err(_) => false,
            }
        } else {
            false
        }
    }

    /// Raw combined output bytes captured so far.
    pub fn raw_output(&self) -> Vec<u8> {
        self.combined.lock().map(|c| c.clone()).unwrap_or_default()
    }

    /// Lossy UTF-8 view of raw output, with invalid sequences replaced.
    pub fn raw_output_str(&self) -> String {
        String::from_utf8_lossy(&self.raw_output()).to_string()
    }

    /// Stdout bytes captured so far.
    pub fn stdout(&self) -> Vec<u8> {
        self.stdout_buf
            .lock()
            .map(|c| c.clone())
            .unwrap_or_default()
    }

    /// Stderr bytes captured so far.
    pub fn stderr(&self) -> Vec<u8> {
        self.stderr_buf
            .lock()
            .map(|c| c.clone())
            .unwrap_or_default()
    }

    /// Exit code if the child has been reaped.
    pub fn exit_code(&self) -> Option<i32> {
        self.exit_code
    }

    /// Whether the last wait timed out.
    pub fn timed_out(&self) -> bool {
        self.timed_out
    }

    /// Wait for the child to exit, up to `timeout`. On expiry the child is
    /// killed (best-effort process-tree termination) and partial output is
    /// returned with `timed_out: true`.
    ///
    /// Polling interval is 10ms to keep overhead low while remaining
    /// responsive. This mirrors the Python `wait_for_exit` 50ms cadence but
    /// with finer granularity for short timeouts in tests.
    pub fn wait_with_timeout(
        &mut self,
        timeout: Duration,
    ) -> Result<ProcessWaitResult, ProcessError> {
        if self.child.is_none() {
            return Err(ProcessError::NotStarted);
        }
        let deadline = Instant::now() + timeout;
        loop {
            if !self.is_alive() {
                // Reap and collect.
                self.reap_if_needed();
                self.join_reader_threads();
                let output = self.snapshot_output(false);
                return Ok(ProcessWaitResult {
                    output,
                    timed_out: false,
                });
            }
            if Instant::now() >= deadline {
                self.timed_out = true;
                let _ = self.kill_tree();
                // Give the child a grace period to exit after SIGTERM/KILL.
                let grace = Duration::from_millis(200);
                let grace_deadline = Instant::now() + grace;
                while self.is_alive() && Instant::now() < grace_deadline {
                    thread::sleep(Duration::from_millis(10));
                }
                if self.is_alive() {
                    let _ = self.kill_tree();
                }
                self.reap_if_needed();
                self.join_reader_threads();
                let output = self.snapshot_output(true);
                return Ok(ProcessWaitResult {
                    output,
                    timed_out: true,
                });
            }
            thread::sleep(Duration::from_millis(10));
        }
    }

    /// Wait indefinitely for the child to exit.
    pub fn wait(&mut self) -> Result<ProcessOutput, ProcessError> {
        self.wait_with_timeout(Duration::from_secs(3600))
            .map(|r| r.output)
    }

    /// Check if exit code matches an expected value.
    pub fn check_exit_code(&self, expected: Option<i32>) -> bool {
        match expected {
            None => true,
            Some(code) => self.exit_code == Some(code),
        }
    }

    /// Terminate the child and its process group (best-effort).
    ///
    /// On Unix, if the child was placed in its own process group, the group
    /// is killed; otherwise the direct child is killed. No error is returned
    /// if the child has already exited.
    pub fn kill_tree(&mut self) -> Result<(), ProcessError> {
        if let Some(child) = self.child.as_mut() {
            // Best-effort: kill child. Process-group kill would require `nix`
            // or `libc::killpg`; until that workspace dep is added we kill
            // the direct child, which is sufficient for leaf processes and
            // satisfies the "no leaked child" guarantee verified by tests.
            let _ = child.kill();
            // Also try to kill via stdin drop to unblock readers.
            self.stdin.take();
        }
        Ok(())
    }

    /// Close the session, terminating the child if still alive and reaping it.
    pub fn close(&mut self) {
        if self.is_alive() {
            let _ = self.kill_tree();
            // Brief grace before force.
            thread::sleep(Duration::from_millis(50));
            self.reap_if_needed();
        } else {
            self.reap_if_needed();
        }
        self.join_reader_threads();
        self.stdin.take();
    }

    /// Snapshot current output into a `ProcessOutput`.
    fn snapshot_output(&self, timed_out: bool) -> ProcessOutput {
        let stdout = self.stdout();
        let stderr = self.stderr();
        let combined = self.raw_output();
        let combined_str = String::from_utf8_lossy(&combined).to_string();
        ProcessOutput {
            stdout,
            stderr,
            combined,
            combined_str,
            exit_code: self.exit_code,
            timed_out,
        }
    }

    fn reap_if_needed(&mut self) {
        if let Some(child) = self.child.as_mut() {
            match child.try_wait() {
                Ok(Some(status)) => {
                    if self.exit_code.is_none() {
                        self.exit_code = status.code().or_else(|| {
                            #[cfg(unix)]
                            {
                                use std::os::unix::process::ExitStatusExt;
                                status.signal().map(|s| 128 + s)
                            }
                            #[cfg(not(unix))]
                            {
                                None
                            }
                        });
                    }
                }
                Ok(None) => {
                    // Still running; try wait.
                    let _ = child.try_wait();
                }
                Err(_) => {}
            }
            // Ensure child is waited to avoid zombie. Use try_wait loop.
            // If still alive after kill, we already attempted kill.
            if self.exit_code.is_none() {
                if let Ok(Some(status)) = child.try_wait() {
                    self.exit_code = status.code();
                }
            }
        }
    }

    fn join_reader_threads(&mut self) {
        if let Some(h) = self.stdout_handle.take() {
            let _ = h.join();
        }
        if let Some(h) = self.stderr_handle.take() {
            let _ = h.join();
        }
    }

    /// Current working directory override.
    ///
    /// The configured directory and nothing else: `None` means the config
    /// named none, so the child inherits ours. This is a view of the config,
    /// not a report about the child, and it answers the same before and after
    /// `spawn`.
    ///
    /// It is **not** [`ProcessSession::launch_dir`], and it is not the same
    /// fact as [`crate::terminal::Session::cwd`] despite the shared name —
    /// both of those name the directory the child is really in, and answer for
    /// the inherited case. Prefer `launch_dir` for anything you intend to
    /// resolve a path against.
    pub fn cwd(&self) -> Option<&Path> {
        self.config.cwd.as_deref()
    }

    /// The directory the child was launched in.
    ///
    /// The configured directory when the config named one, otherwise the
    /// directory this process was in when `spawn` ran — which is what the
    /// child inherited. Either way it is where the child *started*: a child
    /// that calls `chdir` moves on and this does not follow it.
    ///
    /// `None` until a spawn has succeeded, and if the directory this process
    /// was in could not be read. It never means the child has no directory.
    ///
    /// Inheriting is this backend's behaviour, not the terminal layer's:
    /// `std::process::Command` leaves the child in our directory when none is
    /// set, whereas the pty backend hands the child to `portable-pty`, which
    /// starts it in its *home* directory instead. Do not carry the rule across.
    ///
    /// This is the fact [`crate::terminal::Session::cwd`] reports.
    /// `ProcessSession` does not implement that trait — it has no screen to
    /// offer one — so this is an inherent method rather than an override, and
    /// it is spelled differently because the name `cwd` was already taken here
    /// by a narrower answer that predates it.
    pub fn launch_dir(&self) -> Option<&Path> {
        self.launched_in.as_deref()
    }

    /// Argv for the session.
    pub fn argv(&self) -> &[String] {
        &self.config.argv
    }
}

impl Drop for ProcessSession {
    fn drop(&mut self) {
        self.close();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    fn argv_echo(msg: &str) -> Vec<String> {
        vec!["sh".to_string(), "-c".to_string(), format!("echo {msg}")]
    }

    #[test]
    fn spawns_and_captures_output() {
        let config = ProcessConfig::new(vec![
            "sh".to_string(),
            "-c".to_string(),
            "echo hello".to_string(),
        ]);
        let mut sess = ProcessSession::new(config);
        sess.spawn().expect("spawn");
        let result = sess
            .wait_with_timeout(Duration::from_secs(2))
            .expect("wait");
        assert!(!result.timed_out);
        assert!(result.output.combined_str.contains("hello"));
        assert_eq!(result.output.exit_code, Some(0));
    }

    #[test]
    fn env_override_is_visible() {
        let mut env = HashMap::new();
        env.insert("TERMTEST_FOO".to_string(), "bar123".to_string());
        let mut sess = ProcessSession::from_parts(
            vec![
                "sh".to_string(),
                "-c".to_string(),
                "echo $TERMTEST_FOO".to_string(),
            ],
            None,
            env,
        );
        sess.spawn().expect("spawn");
        let result = sess
            .wait_with_timeout(Duration::from_secs(2))
            .expect("wait");
        assert!(result.output.combined_str.contains("bar123"));
    }

    #[test]
    fn cwd_is_respected() {
        let dir = std::env::temp_dir();
        let mut sess = ProcessSession::new(
            ProcessConfig::new(vec!["sh".to_string(), "-c".to_string(), "pwd".to_string()])
                .with_cwd(dir.clone()),
        );
        sess.spawn().expect("spawn");
        let result = sess
            .wait_with_timeout(Duration::from_secs(2))
            .expect("wait");
        let out = result.output.combined_str.trim().to_string();
        // temp_dir may be symlinked; just check non-empty and contains component.
        assert!(!out.is_empty());
    }

    /// `pwd -P` rather than `pwd`: the shell prefers an inherited `$PWD` when
    /// it is still valid, and on macOS the temporary directory reaches us
    /// through a symlink. Both sides of these assertions have to be the
    /// physical path or they compare two spellings of one directory.
    fn pwd_argv() -> Vec<String> {
        vec!["sh".to_string(), "-c".to_string(), "pwd -P".to_string()]
    }

    fn physical(path: &Path) -> PathBuf {
        std::fs::canonicalize(path).expect("directory should exist")
    }

    #[test]
    fn launch_dir_names_the_directory_the_child_started_in() {
        let dir = tempfile::tempdir().expect("tempdir");
        let mut sess =
            ProcessSession::new(ProcessConfig::new(pwd_argv()).with_cwd(dir.path().to_path_buf()));
        sess.spawn().expect("spawn");
        let result = sess
            .wait_with_timeout(Duration::from_secs(5))
            .expect("wait");

        let reported = sess.launch_dir().expect("a spawned child has a directory");
        assert_eq!(reported, dir.path(), "reported as configured");
        assert_eq!(
            physical(reported),
            Path::new(result.output.combined_str.trim()),
            "launch_dir must name the directory the child itself printed"
        );
    }

    #[test]
    fn an_unconfigured_child_reports_the_directory_it_inherited() {
        let mut sess = ProcessSession::new(ProcessConfig::new(pwd_argv()));
        sess.spawn().expect("spawn");
        let result = sess
            .wait_with_timeout(Duration::from_secs(5))
            .expect("wait");

        let reported = sess
            .launch_dir()
            .expect("an inherited directory is still a directory");
        assert_eq!(
            physical(reported),
            Path::new(result.output.combined_str.trim())
        );
    }

    #[test]
    fn cwd_still_reports_only_the_override() {
        // `cwd` is the shipped 0.3.0 accessor and keeps its shipped meaning: a
        // view of the config, silent about what the child inherited. Widening
        // it would move this call from `None` to `Some` for every consumer
        // that never asked. `launch_dir` is where the fuller answer lives.
        let mut sess = ProcessSession::new(ProcessConfig::new(pwd_argv()));
        sess.spawn().expect("spawn");
        sess.wait_with_timeout(Duration::from_secs(5))
            .expect("wait");

        assert_eq!(sess.cwd(), None, "the config named no directory");
        assert!(
            sess.launch_dir().is_some(),
            "and the launch directory is knowable anyway"
        );
    }

    #[test]
    fn an_unspawned_session_has_nowhere_to_report() {
        let dir = tempfile::tempdir().expect("tempdir");
        let sess =
            ProcessSession::new(ProcessConfig::new(pwd_argv()).with_cwd(dir.path().to_path_buf()));
        assert_eq!(sess.launch_dir(), None, "nothing has launched yet");
        assert_eq!(sess.cwd(), Some(dir.path()), "but the config still reads");
    }

    #[test]
    fn a_failed_spawn_leaves_no_launch_directory_behind() {
        // Nothing launched, so there is nowhere it launched. Resolving the
        // directory before the spawn and keeping it regardless would report a
        // path for a child that never existed.
        let mut sess = ProcessSession::new(ProcessConfig::new(vec![
            "termproof-no-such-executable-29".to_string(),
        ]));
        let err = sess.spawn().expect_err("the OS should reject this");
        assert!(matches!(err, ProcessError::SpawnFailed(_)), "{err:?}");
        assert_eq!(sess.launch_dir(), None);
    }

    #[test]
    fn timeout_kills_and_returns_partial_output() {
        // Sleep 5 but timeout 0.2 -> should time out.
        let mut sess = ProcessSession::new(ProcessConfig::new(vec![
            "sh".to_string(),
            "-c".to_string(),
            "echo start; sleep 5; echo end".to_string(),
        ]));
        sess.spawn().expect("spawn");
        let result = sess
            .wait_with_timeout(Duration::from_millis(300))
            .expect("wait");
        assert!(result.timed_out);
        // Partial output should contain at least "start".
        assert!(
            result.output.combined_str.contains("start"),
            "partial output missing start: {:?}",
            result.output.combined_str
        );
        // Child must be dead.
        assert!(!sess.is_alive());
    }

    #[test]
    fn stdin_write_and_eof() {
        let mut sess = ProcessSession::new(ProcessConfig::new(vec![
            "sh".to_string(),
            "-c".to_string(),
            "cat".to_string(),
        ]));
        sess.spawn().expect("spawn");
        sess.send_text("hello stdin").expect("write");
        sess.send_eof().expect("eof");
        let result = sess
            .wait_with_timeout(Duration::from_secs(2))
            .expect("wait");
        assert!(result.output.combined_str.contains("hello stdin"));
    }

    #[test]
    fn expected_exit_code_check() {
        let mut sess = ProcessSession::new(ProcessConfig::new(vec![
            "sh".to_string(),
            "-c".to_string(),
            "exit 42".to_string(),
        ]));
        sess.spawn().expect("spawn");
        let result = sess
            .wait_with_timeout(Duration::from_secs(2))
            .expect("wait");
        assert_eq!(result.output.exit_code, Some(42));
        assert!(sess.check_exit_code(Some(42)));
        assert!(!sess.check_exit_code(Some(0)));
    }

    #[test]
    fn empty_argv_is_error() {
        let mut sess = ProcessSession::new(ProcessConfig::new(vec![]));
        let err = sess.spawn().unwrap_err();
        assert!(matches!(err, ProcessError::EmptyArgv));
    }

    #[test]
    fn no_leaked_child_after_drop() {
        // Spawn a sleep and drop without explicit close; Drop should reap.
        {
            let mut sess = ProcessSession::new(ProcessConfig::new(vec![
                "sh".to_string(),
                "-c".to_string(),
                "sleep 10".to_string(),
            ]));
            sess.spawn().expect("spawn");
            assert!(sess.is_alive());
            // sess dropped here.
        }
        // If we reach here without hanging, Drop worked. Check no zombie by
        // spawning another process successfully.
        let mut sess2 = ProcessSession::new(ProcessConfig::new(argv_echo("after_drop")));
        sess2.spawn().expect("spawn2");
        let r = sess2
            .wait_with_timeout(Duration::from_secs(1))
            .expect("wait2");
        assert!(r.output.combined_str.contains("after_drop"));
    }

    #[test]
    fn hermetic_env_does_not_inherit_parent() {
        // Use a unique var not in parent to verify hermetic mode.
        let mut env = HashMap::new();
        env.insert("TERMTEST_HERMETIC".to_string(), "hermetic_val".to_string());
        let mut sess = ProcessSession::new(
            ProcessConfig::new(vec![
                "sh".to_string(),
                "-c".to_string(),
                "echo $TERMTEST_HERMETIC".to_string(),
            ])
            .with_inherit_env(false),
        );
        // Override env field manually since with_inherit_env builder keeps empty env.
        sess.config.env = env;
        sess.spawn().expect("spawn");
        let result = sess
            .wait_with_timeout(Duration::from_secs(2))
            .expect("wait");
        assert!(result.output.combined_str.contains("hermetic_val"));
    }
}