dirge-agent 0.11.1

Minimalistic coding agent written in Rust, optimized for memory footprint and performance
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
//! PTY relay: isolate a subprocess (SSH) on a pseudo-terminal pair so it
//! never touches `/dev/tty` directly. A relay loop copies bytes between
//! the PTY primary and the real terminal until the child exits.
//!
//! This replaces the previous approach of giving SSH direct `/dev/tty`
//! access, which caused terminal corruption because crossterm's raw-mode
//! event reader, SSH's cooked-mode I/O, and the TUI's alt-screen state
//! machine all fought over the same file descriptors.
//!
//! ```text
//! SSH ──→ PTY secondary (cooked, line discipline)
//!//!        PTY primary
//!//!     relay thread ──→ /dev/tty (exclusive owner during SSH session)
//! ```

use std::io::{self, Read, Write};
use std::os::unix::io::{AsRawFd, FromRawFd, OwnedFd};

/// Holds a PTY pair with a spawned child whose stdin/stdout/stderr are
/// attached to the PTY secondary.
pub(crate) struct PtyRelay {
    primary: std::fs::File,
    /// Held alive so the PTY secondary stays open; its raw fd is
    /// stored in `secondary_raw_fd` for the deferred ECHO-disable
    /// after SSH completes its PTY allocation.
    _secondary: std::fs::File,
    secondary_raw_fd: libc::c_int,
    child: std::process::Child,
    counters: RelayCounters,
}

// ── relay byte accounting (timing-diagnostics feature) ───────────

#[cfg(feature = "timing-diagnostics")]
mod counters {
    /// Byte-accounting counters for the relay loop. Asserted on drop
    /// to catch lost bytes in either direction.
    pub(crate) struct RelayCounters {
        pub tty_bytes_read: u64,
        pub pty_bytes_written: u64,
        pub pty_bytes_read: u64,
        pub tty_bytes_written: u64,
        pub wouldblock_count: u64,
        pub poll_count: u64,
        /// Bytes injected via write_to_primary (drain-and-reinject).
        /// These came from tty but bypassed the relay read path.
        pub drain_injected: u64,
    }

    impl RelayCounters {
        pub fn new() -> Self {
            Self {
                tty_bytes_read: 0,
                pty_bytes_written: 0,
                pty_bytes_read: 0,
                tty_bytes_written: 0,
                wouldblock_count: 0,
                poll_count: 0,
                drain_injected: 0,
            }
        }
        pub fn poll(&mut self) {
            self.poll_count += 1;
        }
        pub fn tty_read(&mut self, n: usize) {
            self.tty_bytes_read += n as u64;
        }
        pub fn pty_read(&mut self, n: usize) {
            self.pty_bytes_read += n as u64;
        }
        pub fn tty_write(&mut self, n: usize) {
            self.tty_bytes_written += n as u64;
        }
        pub fn pty_write(&mut self, n: usize) {
            self.pty_bytes_written += n as u64;
        }
        pub fn wouldblock(&mut self) {
            self.wouldblock_count += 1;
        }
        pub fn drain_injected(&mut self, n: usize) {
            self.drain_injected += n as u64;
        }
    }

    impl Drop for RelayCounters {
        fn drop(&mut self) {
            let tty_loss = self.tty_bytes_read.saturating_sub(self.pty_bytes_written);
            let pty_loss = self.pty_bytes_read.saturating_sub(self.tty_bytes_written);
            eprintln!(
                "[timing-diagnostics] relay exit: \
                 tty_read={} pty_written={} tty_loss={} \
                 pty_read={} tty_written={} pty_loss={} \
                 wouldblock={} poll={}",
                self.tty_bytes_read,
                self.pty_bytes_written,
                tty_loss,
                self.pty_bytes_read,
                self.tty_bytes_written,
                pty_loss,
                self.wouldblock_count,
                self.poll_count,
            );
            // Assert no loss beyond retry-buffer pending bytes.
            // tty_loss > 0 means we read keystrokes from tty that never
            // reached the PTY (either in flight or lost).> 0 is expected
            // if retry buffers have pending bytes at exit; stress tests
            // check exact equality after waiting for buffers to drain.
            assert!(
                self.tty_bytes_read >= self.pty_bytes_written,
                "RELAY LOSS tty→PTY: read {} bytes from tty, wrote {} to PTY (loss {})",
                self.tty_bytes_read,
                self.pty_bytes_written,
                tty_loss,
            );
            assert!(
                self.pty_bytes_read >= self.tty_bytes_written,
                "RELAY LOSS PTY→tty: read {} bytes from PTY, wrote {} to tty (loss {})",
                self.pty_bytes_read,
                self.tty_bytes_written,
                pty_loss,
            );
        }
    }
}

#[cfg(not(feature = "timing-diagnostics"))]
mod counters {
    /// Zero-cost stub: all methods compile to nothing.
    pub(crate) struct RelayCounters;
    impl RelayCounters {
        #[inline(always)]
        pub fn new() -> Self {
            Self
        }
        #[inline(always)]
        pub fn poll(&mut self) {}
        #[inline(always)]
        pub fn tty_read(&mut self, _n: usize) {}
        #[inline(always)]
        pub fn pty_read(&mut self, _n: usize) {}
        #[inline(always)]
        pub fn tty_write(&mut self, _n: usize) {}
        #[inline(always)]
        pub fn pty_write(&mut self, _n: usize) {}
        #[inline(always)]
        pub fn wouldblock(&mut self) {}
        #[inline(always)]
        pub fn drain_injected(&mut self, _n: usize) {}
    }
}

use counters::RelayCounters;

impl PtyRelay {
    /// Create a PTY pair, spawn `cmd` with the PTY secondary as
    /// stdin/stdout/stderr, and return a `PtyRelay` ready to relay I/O.
    pub(crate) fn spawn(cmd: &mut std::process::Command) -> io::Result<Self> {
        // ── open PTY ────────────────────────────────────────────────
        let primary_fd = unsafe { libc::posix_openpt(libc::O_RDWR | libc::O_NOCTTY) };
        if primary_fd < 0 {
            return Err(io::Error::last_os_error());
        }
        if unsafe { libc::grantpt(primary_fd) } < 0 || unsafe { libc::unlockpt(primary_fd) } < 0 {
            let e = io::Error::last_os_error();
            unsafe { libc::close(primary_fd) };
            return Err(e);
        }
        let secondary_name = unsafe { libc::ptsname(primary_fd) };
        if secondary_name.is_null() {
            let e = io::Error::last_os_error();
            unsafe { libc::close(primary_fd) };
            return Err(e);
        }
        let secondary_fd = unsafe { libc::open(secondary_name, libc::O_RDWR | libc::O_NOCTTY) };
        if secondary_fd < 0 {
            let e = io::Error::last_os_error();
            unsafe { libc::close(primary_fd) };
            return Err(e);
        }

        let primary = unsafe { std::fs::File::from_raw_fd(primary_fd) };
        let secondary = unsafe { std::fs::File::from_raw_fd(secondary_fd) };

        let secondary_raw_fd = secondary.as_raw_fd();

        // Set raw mode on the PTY secondary so the line discipline
        // doesn't echo or buffer — the relay owns all I/O processing.
        make_raw(secondary_raw_fd)?;

        // Temporarily re-enable ECHO so the SSH client reads ECHO=1
        // during its PTY allocation request (RFC 4254 §8). ECHO is
        // disabled again in the relay loop after the first data transfer
        // (guaranteeing SSH has completed its PTY allocation).
        {
            let mut termios: libc::termios = unsafe { std::mem::zeroed() };
            if unsafe { libc::tcgetattr(secondary_raw_fd, &mut termios) } == 0 {
                termios.c_lflag |= libc::ECHO;
                unsafe { libc::tcsetattr(secondary_raw_fd, libc::TCSANOW, &termios) };
            }
        }

        // ── spawn child on PTY secondary ─────────────────────────────
        let secondary_dup_in = secondary.try_clone()?;
        let secondary_dup_out = secondary.try_clone()?;
        let secondary_dup_err = secondary.try_clone()?;
        // SAFETY: we own these fds; the child inherits them.
        unsafe {
            cmd.stdin(stdio_from_fd(secondary_dup_in));
            cmd.stdout(stdio_from_fd(secondary_dup_out));
            cmd.stderr(stdio_from_fd(secondary_dup_err));
        }

        let child = cmd.spawn()?;

        Ok(PtyRelay {
            primary,
            _secondary: secondary,
            secondary_raw_fd,
            child,
            counters: RelayCounters::new(),
        })
    }

    /// Disable ECHO on the guest PTY secondary now, rather than waiting
    /// for the first data transfer. Call this when the command is known
    /// to not need the ECHO=1 handshake (e.g. `cat -u` in tests).
    #[cfg(all(test, feature = "sandbox-microvm"))]
    pub(crate) fn disable_guest_echo(&self) {
        disable_echo(self.secondary_raw_fd);
    }

    /// Write bytes to the PTY primary. Used to inject keystrokes
    /// drained from stdin before the relay was started, so keys
    /// typed during the TUI suspend window aren't lost.
    pub(crate) fn write_to_primary(&mut self, data: &[u8]) -> io::Result<()> {
        use std::io::Write;
        self.primary.write_all(data)?;
        self.counters.drain_injected(data.len());
        Ok(())
    }

    /// Return the child process id. Used in tests to externally
    /// kill the child to make the relay exit.
    #[cfg(all(test, feature = "sandbox-microvm"))]
    pub(crate) fn child_pid(&self) -> u32 {
        self.child.id()
    }

    /// Block until the child exits, relaying I/O between PTY primary and
    /// `/dev/tty`. Returns the child's exit status.
    ///
    /// Uses `poll(2)` with retry buffers. When a non-blocking `write`
    /// returns `WouldBlock` (other side isn't draining fast enough),
    /// unsent bytes are queued and retried on the next poll iteration.
    pub(crate) fn relay(self) -> io::Result<std::process::ExitStatus> {
        // ── relay priority: below input reader (-20), above KVM (19) ──
        unsafe {
            libc::setpriority(libc::PRIO_PROCESS, 0, -19);
        }
        let tty = std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .open("/dev/tty")?;
        self.relay_to_fd(tty)
    }

    /// Same as [`relay`] but takes an explicit tty file descriptor instead
    /// of opening `/dev/tty`. Used by stress tests that inject keystrokes
    /// through a PTY pair instead of a real terminal.
    pub(crate) fn relay_to_fd(
        mut self,
        mut tty: std::fs::File,
    ) -> io::Result<std::process::ExitStatus> {
        #[cfg(feature = "timing-diagnostics")]
        let t_relay_enter = std::time::Instant::now();

        set_pty_winsize(&self.primary)?;

        let counters = &mut self.counters;

        let pty_fd = self.primary.as_raw_fd();
        let tty_fd = tty.as_raw_fd();

        // Both fds must be non-blocking so writes don't stall the
        // relay loop when the other side isn't draining fast enough.
        // The retry-buffer pattern handles WouldBlock on both reads
        // and writes for both fds.
        set_nonblocking(pty_fd)?;
        set_nonblocking(tty_fd)?;

        // Retry buffers: bytes queued when write hits WouldBlock.
        let mut pty_write_buf: Vec<u8> = Vec::with_capacity(4096);
        let mut tty_write_buf: Vec<u8> = Vec::with_capacity(4096);

        let mut fds = [
            libc::pollfd {
                fd: pty_fd,
                events: 0,
                revents: 0,
            },
            libc::pollfd {
                fd: tty_fd,
                events: 0,
                revents: 0,
            },
        ];

        let mut read_buf = [0u8; 4096];

        #[cfg(feature = "timing-diagnostics")]
        let mut first_poll = true;

        // Disable ECHO on the secondary after the first data flows
        // through the relay — guarantees SSH has completed its PTY
        // allocation (RFC 4254 §8) and the remote shell will echo
        // keystrokes. Before this point ECHO=1 so SSH reads it during
        // its startup; after, ECHO=0 prevents local double-echo.
        let mut echo_disabled = false;

        let mut exit_status: Option<std::process::ExitStatus> = None;

        loop {
            match self.child.try_wait() {
                Ok(Some(status)) => {
                    exit_status = Some(status);
                    break;
                }
                Ok(None) => {}
                Err(e) => return Err(e),
            }

            #[cfg(feature = "timing-diagnostics")]
            if first_poll {
                first_poll = false;
                eprintln!(
                    "[timing-diag] relay_first_poll: {:?} after relay_enter",
                    t_relay_enter.elapsed()
                );
            }

            fds[0].events = libc::POLLIN;
            fds[1].events = libc::POLLIN;
            if !pty_write_buf.is_empty() {
                fds[0].events |= libc::POLLOUT;
            }
            if !tty_write_buf.is_empty() {
                fds[1].events |= libc::POLLOUT;
            }
            fds[0].revents = 0;
            fds[1].revents = 0;

            let ret = unsafe { libc::poll(fds.as_mut_ptr(), 2, 20) };
            counters.poll();
            if ret < 0 {
                let e = io::Error::last_os_error();
                if e.kind() == io::ErrorKind::Interrupted {
                    continue;
                }
                break;
            }

            // ── flush buffered writes first ───────────────────────
            if fds[0].revents & libc::POLLOUT != 0 && !pty_write_buf.is_empty() {
                match self.primary.write(&pty_write_buf) {
                    Ok(n) if n > 0 => {
                        counters.pty_write(n);
                        pty_write_buf.drain(..n);
                    }
                    Ok(_) => {}
                    Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
                        counters.wouldblock();
                    }
                    Err(_) => break,
                }
            }
            if fds[1].revents & libc::POLLOUT != 0 && !tty_write_buf.is_empty() {
                match tty.write(&tty_write_buf) {
                    Ok(n) if n > 0 => {
                        counters.tty_write(n);
                        tty_write_buf.drain(..n);
                    }
                    Ok(_) => {}
                    Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
                        counters.wouldblock();
                    }
                    Err(_) => break,
                }
            }

            // ── PTY → tty (guest output → user screen) ────────────
            if fds[0].revents & libc::POLLIN != 0 {
                match self.primary.read(&mut read_buf) {
                    Ok(0) => return self.child.wait(),
                    Ok(n) => {
                        counters.pty_read(n);
                        if !echo_disabled {
                            echo_disabled = true;
                            disable_echo(self.secondary_raw_fd);
                        }
                        if !tty_write_buf.is_empty() {
                            match tty.write(&tty_write_buf) {
                                Ok(w) if w > 0 => {
                                    counters.tty_write(w);
                                    tty_write_buf.drain(..w);
                                }
                                Ok(_) => {}
                                Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
                                    counters.wouldblock();
                                }
                                Err(_) => break,
                            }
                        }
                        match tty.write(&read_buf[..n]) {
                            Ok(w) if w < n => {
                                counters.tty_write(w);
                                tty_write_buf.extend_from_slice(&read_buf[w..n]);
                            }
                            Ok(w) => {
                                counters.tty_write(w);
                            }
                            Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
                                counters.wouldblock();
                                tty_write_buf.extend_from_slice(&read_buf[..n]);
                            }
                            Err(_) => break,
                        }
                    }
                    Err(e) if e.kind() == io::ErrorKind::WouldBlock => {}
                    Err(_) => break,
                }
            }
            if fds[0].revents & (libc::POLLHUP | libc::POLLERR) != 0 {
                return self.child.wait();
            }

            // ── tty → PTY (user keystrokes → guest) ───────────────
            if fds[1].revents & libc::POLLIN != 0 {
                match tty.read(&mut read_buf) {
                    Ok(0) => break,
                    Ok(n) => {
                        counters.tty_read(n);
                        if !pty_write_buf.is_empty() {
                            match self.primary.write(&pty_write_buf) {
                                Ok(w) if w > 0 => {
                                    counters.pty_write(w);
                                    pty_write_buf.drain(..w);
                                }
                                Ok(_) => {}
                                Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
                                    counters.wouldblock();
                                }
                                Err(_) => break,
                            }
                        }
                        match self.primary.write(&read_buf[..n]) {
                            Ok(w) if w < n => {
                                counters.pty_write(w);
                                pty_write_buf.extend_from_slice(&read_buf[w..n]);
                            }
                            Ok(w) => {
                                counters.pty_write(w);
                            }
                            Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
                                counters.wouldblock();
                                pty_write_buf.extend_from_slice(&read_buf[..n]);
                            }
                            Err(_) => break,
                        }
                    }
                    Err(e) if e.kind() == io::ErrorKind::WouldBlock => {}
                    Err(_) => break,
                }
            }
            if fds[1].revents & (libc::POLLHUP | libc::POLLERR) != 0 {
                // Tty is gone — kill the child so we don't block in wait().
                let _ = self.child.kill();
                break;
            }
        }

        // ── final drain: flush write buffers + capture last PTY output ──
        if let Some(status) = exit_status {
            // Flush both write buffers. The child has exited so no new
            // data arrives on the tty side; we just need to push out
            // any buffered bytes. Cap retries so a blocked tty doesn't
            // keep the relay alive indefinitely.
            const MAX_FLUSH_ITER: usize = 50;
            let mut flush_iter = 0;
            while (!pty_write_buf.is_empty() || !tty_write_buf.is_empty())
                && flush_iter < MAX_FLUSH_ITER
            {
                flush_iter += 1;
                fds[0].events = 0;
                fds[1].events = 0;
                if !pty_write_buf.is_empty() {
                    fds[0].events |= libc::POLLOUT;
                }
                if !tty_write_buf.is_empty() {
                    fds[1].events |= libc::POLLOUT;
                }
                fds[0].revents = 0;
                fds[1].revents = 0;
                let ret = unsafe { libc::poll(fds.as_mut_ptr(), 2, 20) };
                if ret < 0 {
                    break;
                }
                if fds[0].revents & libc::POLLOUT != 0 && !pty_write_buf.is_empty() {
                    match self.primary.write(&pty_write_buf) {
                        Ok(n) if n > 0 => {
                            pty_write_buf.drain(..n);
                        }
                        Err(e) if e.kind() == io::ErrorKind::WouldBlock => {}
                        _ => break,
                    }
                }
                if fds[1].revents & libc::POLLOUT != 0 && !tty_write_buf.is_empty() {
                    match tty.write(&tty_write_buf) {
                        Ok(n) if n > 0 => {
                            tty_write_buf.drain(..n);
                        }
                        Err(e) if e.kind() == io::ErrorKind::WouldBlock => {}
                        _ => break,
                    }
                }
            }

            // Drain any remaining PTY output the child produced between
            // the last poll iteration and its exit.
            loop {
                fds[0].events = libc::POLLIN;
                fds[0].revents = 0;
                let ret = unsafe { libc::poll(fds.as_mut_ptr(), 1, 50) };
                if ret <= 0 {
                    break;
                }
                if fds[0].revents & libc::POLLIN != 0 {
                    match self.primary.read(&mut read_buf) {
                        Ok(0) | Err(_) => break,
                        Ok(n) => {
                            // Best-effort write to tty; ignore errors.
                            let _ = tty.write_all(&read_buf[..n]);
                        }
                    }
                } else {
                    break;
                }
            }

            return Ok(status);
        }

        self.child.wait()
    }
}

/// Set the PTY window size to match the real terminal, so SSH -t and
/// full-screen programs (vim, less, htop) see correct dimensions.
fn set_pty_winsize(primary: &std::fs::File) -> io::Result<()> {
    let mut ws: libc::winsize = unsafe { std::mem::zeroed() };
    // Try to get the real terminal size from /dev/tty.
    if let Ok(tty) = std::fs::OpenOptions::new().read(true).open("/dev/tty") {
        if unsafe { libc::ioctl(tty.as_raw_fd(), libc::TIOCGWINSZ, &mut ws) } < 0 {
            // Fall through with defaults.
            ws.ws_row = 24;
            ws.ws_col = 80;
        }
    } else {
        ws.ws_row = 24;
        ws.ws_col = 80;
    }
    if unsafe { libc::ioctl(primary.as_raw_fd(), libc::TIOCSWINSZ, &ws) } < 0 {
        // Non-fatal: SSH uses a default size.
    }
    Ok(())
}

/// Set O_NONBLOCK on a file descriptor.
fn set_nonblocking(fd: libc::c_int) -> io::Result<()> {
    let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) };
    if flags < 0 {
        return Err(io::Error::last_os_error());
    }
    if unsafe { libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK) } < 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(())
}

/// Set raw mode (cfmakeraw) on a file descriptor. Disables echo,
/// canonical processing, and signal chars so the PTY line discipline
/// passes bytes through without local processing.
pub(crate) fn make_raw(fd: libc::c_int) -> io::Result<()> {
    let mut termios: libc::termios = unsafe { std::mem::zeroed() };
    if unsafe { libc::tcgetattr(fd, &mut termios) } < 0 {
        return Err(io::Error::last_os_error());
    }
    unsafe { libc::cfmakeraw(&mut termios) };
    // Re-enable ONLCR so \n is translated to \r\n on output.
    // cfmakeraw clears OPOST; ONLCR requires OPOST to be set.
    termios.c_oflag |= libc::OPOST | libc::ONLCR;
    if unsafe { libc::tcsetattr(fd, libc::TCSANOW, &termios) } < 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(())
}

/// Disable ECHO on an fd. Best-effort — failures are silent because
/// ECHO is cosmetic (double-echo at worst, never data loss).
fn disable_echo(fd: libc::c_int) {
    let mut termios: libc::termios = unsafe { std::mem::zeroed() };
    if unsafe { libc::tcgetattr(fd, &mut termios) } == 0 {
        termios.c_lflag &= !libc::ECHO;
        unsafe { libc::tcsetattr(fd, libc::TCSANOW, &termios) };
    }
}

/// Convert an owned `File` into a `Stdio` for `Command` plumbing.
/// SAFETY: the caller must ensure the fd is valid and not used elsewhere.
unsafe fn stdio_from_fd(file: std::fs::File) -> std::process::Stdio {
    let fd = file.as_raw_fd();
    // Leak the OwnedFd we'd get from into_raw_fd, transferring ownership
    // to the Stdio (which will close it when the child drops).
    let owned: OwnedFd = unsafe { OwnedFd::from_raw_fd(fd) };
    // Prevent the File's Drop from closing the fd.
    std::mem::forget(file);
    owned.into()
}