Skip to main content

winx_code_agent/state/
pty.rs

1//! Real PTY implementation using portable-pty
2//!
3//! This module provides a true pseudo-terminal interface for interactive
4//! shell sessions, enabling proper handling of:
5//! - ANSI escape sequences and colors
6//! - Interactive programs (sudo, vim, less, etc.)
7//! - Terminal resize events
8//! - Job control signals (Ctrl+C, Ctrl+Z, etc.)
9
10use crate::utils::executable::{shell_kind_of, ShellKind};
11use anyhow::{anyhow, Context, Result};
12use portable_pty::{native_pty_system, Child, CommandBuilder, MasterPty, PtySize};
13use std::collections::hash_map::DefaultHasher;
14use std::hash::{Hash, Hasher};
15use std::io::{Read, Write};
16use std::path::{Path, PathBuf};
17use std::process::Command;
18use std::sync::atomic::{AtomicBool, AtomicU64, Ordering as AtomicOrdering};
19use std::sync::mpsc::{self, TryRecvError};
20use std::sync::{Arc, Mutex as StdMutex};
21use std::thread;
22use std::time::{Duration, Instant};
23use tokio::sync::Mutex;
24use tracing::{debug, info, warn};
25
26use crate::state::line_ring::LineRing;
27use crate::state::live_terminal::{LiveTerminal, ScreenUpdate};
28
29/// Default terminal dimensions (columns x rows)
30pub const DEFAULT_COLS: u16 = 200;
31pub const DEFAULT_ROWS: u16 = 50;
32
33/// Maximum output buffer size to prevent memory issues
34const MAX_OUTPUT_SIZE: usize = 1_000_000;
35
36/// Cap on bytes streamed to one command's output-offload scratch file, so a
37/// command emitting gigabytes cannot fill the disk. The head beyond this is
38/// dropped (the agent still gets the first 50 MB plus the live tail).
39const SCRATCH_MAX_BYTES: u64 = 50 * 1024 * 1024;
40
41/// Sleep injected between PTY reads once the current command's output is pure
42/// discard (scratch budget exhausted — only the live tail is still retained).
43/// With 4 KiB reads this caps consumption at roughly 2 MB/s, so a runaway
44/// producer (`head -c 5T /dev/zero | tr '\0' x` was the production incident)
45/// back-pressures against the kernel PTY buffer instead of pinning a core in
46/// the terminal parsers at full stream speed. A real TUI redraw burst
47/// (~40 KiB/frame) still renders at ~50 fps under this cap, so interactive
48/// piloting is unaffected.
49const DISCARD_READ_THROTTLE: Duration = Duration::from_millis(2);
50
51/// How many fully-formed lines to keep in the per-shell ringbuffer. Callers can
52/// ask for at most this many lines of historical context via
53/// `StatusCheck.scrollback_lines`.
54pub const RING_BUFFER_LINES: usize = 2_000;
55
56/// Cap on the unterminated tail held in `line_ring_partial`. Bounds memory when
57/// a program emits megabytes without ever printing a newline (a `\r`-redrawn
58/// progress bar, a binary blob). Generous enough to hold any real terminal line.
59const MAX_PARTIAL_LINE_BYTES: usize = 64 * 1024;
60
61/// WCGW-style prompt pattern for command completion detection
62const WCGW_PROMPT_PATTERN: &str = "◉";
63const WCGW_PROMPT_END: &str = "──➤";
64/// Upper bound on waiting for a freshly spawned shell's first prompt. Linux bash
65/// answers in milliseconds; Git for Windows bash under `ConPTY` can take seconds
66/// on a cold start.
67const PROMPT_BOOTSTRAP_TIMEOUT: Duration = Duration::from_secs(5);
68
69fn attachable_command(restricted_mode: bool) -> (CommandBuilder, Option<String>, ShellKind) {
70    let requested = crate::config::env_text("WINX_ATTACH_TERMINAL")
71        .or_else(|| crate::config::env_text("WINX_USE_SCREEN"))
72        .unwrap_or_default();
73    if requested == "tmux"
74        || requested == "screen"
75        || crate::config::parse_bool(&requested) == Some(true)
76    {
77        let session = format!("winx-{}-{}", std::process::id(), timestamp_millis());
78        if requested == "tmux" && command_available("tmux") {
79            let mut cmd = CommandBuilder::new("tmux");
80            cmd.args(["new-session", "-A", "-s", &session, "bash"]);
81            if restricted_mode {
82                cmd.arg("-r");
83            }
84            return (cmd, Some(format!("tmux attach -t {session}")), ShellKind::Bash);
85        }
86        if command_available("screen") {
87            // Parity with wcgw: ensure a sane ~/.screenrc and reap sessions whose
88            // creating winx process has died before spawning a fresh one.
89            ensure_screenrc();
90            cleanup_orphaned_screens();
91            let mut cmd = CommandBuilder::new("screen");
92            cmd.args(["-q", "-S", &session, "bash"]);
93            if restricted_mode {
94                cmd.arg("-r");
95            }
96            return (cmd, Some(format!("screen -x {session}")), ShellKind::Bash);
97        }
98    }
99
100    let shell = preferred_shell(restricted_mode);
101    // `WINX_SHELL=/bin/zsh` must get the zsh bootstrap too, not only `zsh`.
102    let kind = shell_kind_of(&shell);
103    let mut cmd = CommandBuilder::new(&shell);
104    match kind {
105        // zsh's restricted mode isn't the `-r` flag, so restricted always uses bash -r.
106        ShellKind::Bash if restricted_mode => {
107            cmd.arg("-r");
108        }
109        // The banner would otherwise land in the first command's output; the
110        // profile still runs so the user's PATH and aliases apply.
111        ShellKind::PowerShell => {
112            cmd.arg("-NoLogo");
113        }
114        ShellKind::Bash | ShellKind::Zsh => {}
115    }
116    // Operator-supplied extra shell arguments (`WINX_SHELL_ARGS="-i --login"`):
117    // whitespace-separated, applied to the direct shell path only.
118    if let Some(extra) = crate::config::env_text("WINX_SHELL_ARGS") {
119        for arg in extra.split_whitespace() {
120            cmd.arg(arg);
121        }
122    }
123    (cmd, None, kind)
124}
125
126/// The shell family a new PTY would get right now, from the same selection
127/// the spawner uses. Command policy (single-statement checks, static write
128/// analysis) consults it before a shell exists.
129pub fn configured_shell_kind(restricted_mode: bool) -> ShellKind {
130    shell_kind_of(&preferred_shell(restricted_mode))
131}
132
133/// Shell to spawn directly. Defaults to bash; honors `WINX_SHELL=zsh` when zsh is
134/// on PATH and we're not in restricted mode (zsh's restricted mode differs from
135/// `bash -r`, so restricted falls back to bash). On Windows the Git for Windows
136/// `bash.exe` is resolved to an absolute path so the `ConPTY` child never lands
137/// on the WSL launcher in `System32`.
138/// On Windows the shell must be an absolute Git for Windows `bash.exe`; the
139/// `System32` WSL launcher is never used. If no usable bash is found, fail with
140/// an actionable message instead of an opaque spawn error later.
141#[cfg(windows)]
142fn ensure_windows_shell_available(restricted_mode: bool) -> Result<()> {
143    // An absolute `WINX_SHELL` (honored only outside restricted mode) wins.
144    if !restricted_mode {
145        if let Ok(requested) = std::env::var("WINX_SHELL") {
146            let path = Path::new(&requested);
147            if path.is_absolute() && path.is_file() {
148                return Ok(());
149            }
150        }
151    }
152    if crate::utils::executable::bash_executable().is_some() {
153        return Ok(());
154    }
155    if crate::utils::executable::powershell_executable().is_some() {
156        if restricted_mode {
157            return Err(anyhow!(
158                "restricted mode needs bash -r; install Git for Windows or set WINX_SHELL to an \
159                 absolute bash.exe path (PowerShell has no equivalent restricted mode)"
160            ));
161        }
162        return Ok(());
163    }
164    Err(anyhow!(
165        "no usable shell found: install Git for Windows (the System32 WSL launcher is not \
166         used) or PowerShell 7, or set WINX_SHELL to an absolute bash.exe or pwsh.exe path"
167    ))
168}
169
170fn preferred_shell(restricted_mode: bool) -> String {
171    if !restricted_mode {
172        if let Ok(requested) = std::env::var("WINX_SHELL") {
173            if requested == "zsh" && command_available("zsh") {
174                return "zsh".to_string();
175            }
176            // `pwsh`/`powershell` select PowerShell wherever it is installed;
177            // restricted mode never does, because `bash -r` has no equivalent.
178            if matches!(requested.as_str(), "pwsh" | "powershell") {
179                if let Some(found) = crate::utils::executable::powershell_executable() {
180                    return found.to_string_lossy().into_owned();
181                }
182            }
183            // An absolute path selects that exact shell binary (a specific
184            // bash.exe on Windows, a custom build elsewhere).
185            if Path::new(&requested).is_absolute() && Path::new(&requested).is_file() {
186                return requested;
187            }
188        }
189    }
190    if cfg!(windows) {
191        if let Some(bash) = crate::utils::executable::bash_executable() {
192            return bash.to_string_lossy().into_owned();
193        }
194        // No Git for Windows: PowerShell keeps the native runtime usable
195        // without WSL, outside restricted mode.
196        if !restricted_mode {
197            if let Some(found) = crate::utils::executable::powershell_executable() {
198                return found.to_string_lossy().into_owned();
199            }
200        }
201    }
202    "bash".to_string()
203}
204
205/// One-line PowerShell bootstrap with the same completion contract as the
206/// bash prompt: `◉ <cwd>──➤<nonce>:<code> ` after every command.
207///
208/// `PSReadLine` is unloaded because it redraws the line with cursor movement
209/// that breaks marker detection; `PlainText` rendering keeps ANSI out of
210/// command output on PowerShell 7.2+. The exit code mirrors a POSIX shell:
211/// a native process code when one was set, `1` for a failed cmdlet
212/// (`$?` false), else `0`; `$LASTEXITCODE` is cleared so a later cmdlet
213/// does not inherit a stale native failure. Windows PowerShell 5.1 keeps
214/// `$?` true after a failing native command, which is why the code is read
215/// before `$?` is consulted.
216fn powershell_bootstrap(nonce: &str) -> String {
217    format!(
218        "Remove-Module PSReadLine -ErrorAction SilentlyContinue; \
219         if (Get-Variable PSStyle -ErrorAction SilentlyContinue) {{ $PSStyle.OutputRendering = 'PlainText' }}; \
220         $env:PAGER = 'cat'; $env:GIT_PAGER = 'cat'; $global:LASTEXITCODE = 0; \
221         function global:prompt {{ $ok = $?; $ec = $global:LASTEXITCODE; \
222         $code = if ($ec -is [int] -and $ec -ne 0) {{ $ec }} elseif ($ok) {{ 0 }} else {{ 1 }}; \
223         $global:LASTEXITCODE = 0; \
224         \"\u{25c9} $($ExecutionContext.SessionState.Path.CurrentLocation.ProviderPath)\u{2500}\u{2500}\u{27a4}{nonce}:$code \" }}"
225    )
226}
227
228fn command_available(command: &str) -> bool {
229    crate::utils::executable::is_available(command)
230}
231
232/// Create `~/.screenrc` with a large scrollback if the user has none, matching
233/// wcgw's `check_if_screen_command_available`. Never overwrites an existing file.
234fn ensure_screenrc() {
235    let Some(home) = home::home_dir() else {
236        return;
237    };
238    let screenrc = home.join(".screenrc");
239    if screenrc.exists() {
240        return;
241    }
242    let _ = std::fs::write(
243        &screenrc,
244        "defscrollback 10000\ntermcapinfo xterm* ti@:te@\nstartup_message off\n",
245    );
246}
247
248/// Reap detached `winx-*` screen sessions whose creating process is gone.
249///
250/// The session name embeds the creator PID (`winx-<pid>-<ts>`), so an orphan is
251/// simply a session whose `<pid>` no longer exists — the wcgw equivalent of
252/// detecting `parent_pid == 1`. Best-effort: any failure is silently ignored.
253fn cleanup_orphaned_screens() {
254    let Ok(output) = Command::new("screen").arg("-ls").output() else {
255        return;
256    };
257    // `screen -ls` exits non-zero when sessions exist, so we parse stdout regardless.
258    let listing = String::from_utf8_lossy(&output.stdout);
259    for line in listing.lines() {
260        let Some(session) = line.split_whitespace().next() else {
261            continue;
262        };
263        // session token looks like "<screen_pid>.winx-<creator_pid>-<ts>"
264        let Some((_, name)) = session.split_once('.') else {
265            continue;
266        };
267        if let Some(creator_pid) = winx_creator_pid(name) {
268            if !process_exists(creator_pid) {
269                let _ = Command::new("screen").args(["-S", session, "-X", "quit"]).output();
270            }
271        }
272    }
273}
274
275/// Extract the creator PID from a `winx-<pid>-<ts>` screen session name.
276fn winx_creator_pid(name: &str) -> Option<u32> {
277    name.strip_prefix("winx-")?.split('-').next()?.parse::<u32>().ok()
278}
279
280/// Whether a process with `pid` is currently alive. `kill(pid, 0)` is portable
281/// across supported Unix platforms and sends no signal.
282#[cfg(unix)]
283fn process_exists(pid: u32) -> bool {
284    crate::os::unix::process_exists(pid)
285}
286
287#[cfg(not(unix))]
288fn process_exists(pid: u32) -> bool {
289    crate::os::process_exists(pid)
290}
291
292/// Git for Windows bash reports its cwd in MSYS form (`/c/Users/x`, or
293/// `/cygdrive/c/...` under Cygwin). On Windows, convert it back to the native
294/// `C:\Users\x` spelling so relative file targets and `cd` tracking resolve
295/// against a directory that exists for the rest of Winx.
296pub(crate) fn native_cwd_from_prompt(cwd: &str) -> PathBuf {
297    if cfg!(windows) {
298        if let Some(native) = msys_to_native(cwd) {
299            return native;
300        }
301    }
302    PathBuf::from(cwd)
303}
304
305fn msys_to_native(cwd: &str) -> Option<PathBuf> {
306    let rest = cwd.strip_prefix("/cygdrive").unwrap_or(cwd);
307    let mut parts = rest.strip_prefix('/')?.splitn(2, '/');
308    let drive = parts.next()?;
309    if drive.len() != 1 || !drive.chars().all(|character| character.is_ascii_alphabetic()) {
310        return None;
311    }
312    let tail = parts.next().unwrap_or("");
313    let mut native = format!("{}:\\", drive.to_ascii_uppercase());
314    native.push_str(&tail.replace('/', "\\"));
315    Some(PathBuf::from(native))
316}
317
318fn timestamp_millis() -> u128 {
319    std::time::SystemTime::now()
320        .duration_since(std::time::UNIX_EPOCH)
321        .map_or(0, |duration| duration.as_millis())
322}
323
324/// Prompt text can be echoed only after submission, with whitespace reflowed.
325/// A receipt containing recognizable activity text cannot establish independent work.
326fn prompt_text_mimics_activity(
327    recognizer: &dyn crate::state::turn::TurnRecognizer,
328    text: &str,
329) -> bool {
330    let lines: Vec<String> = text.lines().map(str::to_string).collect();
331    let folded = [text.split_whitespace().collect::<Vec<_>>().join(" ")];
332    let mimics_activity = |screen: &[String]| {
333        matches!(
334            recognizer.detect_input(&crate::state::turn::TurnInput::screen_only(screen)),
335            crate::state::turn::TurnState::Busy | crate::state::turn::TurnState::AwaitingApproval
336        )
337    };
338    mimics_activity(&lines) || mimics_activity(&folded)
339}
340
341#[cfg(test)]
342mod workflow_activity_tests;
343
344/// Only the current prompt owns a live reader gate. Earlier receipts retain their flag.
345struct PromptActivityGate {
346    recognizer: Box<dyn crate::state::turn::TurnRecognizer>,
347    baseline_seq: u64,
348    observed: Arc<AtomicBool>,
349    submitted: bool,
350    ambiguous_echo: bool,
351    blocked_state: Option<crate::state::turn::TurnState>,
352}
353
354impl PromptActivityGate {
355    fn observe(&mut self, live: &LiveTerminal, seq: u64) {
356        if !self.submitted
357            || self.ambiguous_echo
358            || seq <= self.baseline_seq
359            || self.observed.load(AtomicOrdering::Acquire)
360        {
361            return;
362        }
363        let screen = live.snapshot(0);
364        let input = crate::state::turn::TurnInput {
365            screen: &screen,
366            osc_title: live.osc_title(),
367            osc_progress: live.osc_progress(),
368        };
369        let state = self.recognizer.detect_input(&input);
370        if self.blocked_state == Some(state) {
371            return;
372        }
373        self.blocked_state = None;
374        if matches!(
375            state,
376            crate::state::turn::TurnState::Busy | crate::state::turn::TurnState::AwaitingApproval
377        ) {
378            self.observed.store(true, AtomicOrdering::Release);
379        }
380    }
381}
382
383#[cfg(test)]
384mod workflow_reader_tests;
385
386/// Background thread that pumps the PTY master into the output channel and the
387/// live emulator. Owns the blocking `read` loop so the async side never blocks.
388fn spawn_pty_reader(
389    mut reader: Box<dyn Read + Send>,
390    output_tx: mpsc::SyncSender<String>,
391    live: Arc<StdMutex<LiveTerminal>>,
392    discard_throttle: Arc<AtomicBool>,
393    activity_seq: Arc<AtomicU64>,
394    queued_activity_seq: Arc<AtomicU64>,
395    prompt_activity: Arc<StdMutex<Option<PromptActivityGate>>>,
396) {
397    thread::spawn(move || {
398        let mut buf = [0u8; 4096];
399        // Bytes of an incomplete trailing UTF-8 char held back for the next
400        // read. Without this, a multibyte glyph split across a 4096-byte read
401        // boundary would decode to two U+FFFDs — corrupting the output buffer
402        // and the prompt/CWD detection that runs over it.
403        let mut carry: Vec<u8> = Vec::new();
404        loop {
405            match reader.read(&mut buf) {
406                Ok(0) => {
407                    debug!("PTY reader reached EOF");
408                    // EOF - PTY closed. Flush any held bytes lossily.
409                    if !carry.is_empty() {
410                        // EOF can publish replacement bytes after the valid prefix
411                        // from the same read; give this final delivery a fresh sequence.
412                        let seq =
413                            activity_seq.fetch_add(1, AtomicOrdering::Release).saturating_add(1);
414                        if output_tx.send(String::from_utf8_lossy(&carry).into_owned()).is_ok() {
415                            queued_activity_seq.store(seq, AtomicOrdering::Release);
416                        }
417                    }
418                    break;
419                }
420                Ok(n) => {
421                    debug!(bytes = n, "PTY reader chunk");
422                    // Tap the raw bytes into the live emulator first (brief
423                    // lock; feed is O(chunk len)). Feeding bytes — not the
424                    // lossy String — keeps the persistent VTE parser exact
425                    // across chunk boundaries. Recover from a poisoned lock
426                    // instead of dropping the feed silently.
427                    let seq = {
428                        let mut emu =
429                            live.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
430                        emu.feed(&buf[..n]);
431                        let seq =
432                            activity_seq.fetch_add(1, AtomicOrdering::Release).saturating_add(1);
433                        if let Some(gate) = prompt_activity
434                            .lock()
435                            .unwrap_or_else(std::sync::PoisonError::into_inner)
436                            .as_mut()
437                        {
438                            gate.observe(&emu, seq);
439                        }
440                        seq
441                    };
442                    // Decode the longest valid prefix; carry an incomplete
443                    // trailing char into the next read.
444                    carry.extend_from_slice(&buf[..n]);
445                    let (chunk, rest) = decode_keep_incomplete(&carry);
446                    carry = rest;
447                    if !chunk.is_empty() {
448                        if output_tx.send(chunk).is_err() {
449                            // Receiver dropped, exit thread.
450                            break;
451                        }
452                        // Publish only after enqueueing, outside the live/gate locks.
453                        queued_activity_seq.store(seq, AtomicOrdering::Release);
454                    }
455                    // Once the command's output is pure discard, stop
456                    // consuming at stream speed: the pause fills the kernel
457                    // PTY buffer and blocks the producer instead of burning
458                    // a core parsing bytes that are thrown away.
459                    if discard_throttle.load(AtomicOrdering::Relaxed) {
460                        thread::sleep(DISCARD_READ_THROTTLE);
461                    }
462                }
463                Err(e) => {
464                    debug!("PTY reader thread error: {}", e);
465                    break;
466                }
467            }
468        }
469        debug!("PTY reader thread exiting");
470    });
471}
472
473/// Real PTY-based interactive shell
474///
475/// Uses portable-pty for true pseudo-terminal functionality,
476/// enabling proper handling of interactive programs like sudo, vim, etc.
477pub struct PtyShell {
478    /// The PTY master handle for resize operations
479    master: Box<dyn MasterPty + Send>,
480    /// Child process running the shell
481    child: Box<dyn Child + Send + Sync>,
482    /// Writer for PTY input (taken from master)
483    writer: Box<dyn Write + Send>,
484    /// Channel receiver for output from reader thread
485    output_rx: mpsc::Receiver<String>,
486    /// Current terminal size
487    size: PtySize,
488    /// Last command executed
489    pub last_command: String,
490    /// Accumulated output buffer
491    pub output_buffer: String,
492    /// Whether a command is currently running
493    pub command_running: bool,
494    /// Last cwd observed in this shell's nonce-bearing prompt.
495    current_cwd: PathBuf,
496    /// Monotonic start time of the current or most recently completed command.
497    /// Status polling uses this instead of restarting the clock per tool call.
498    command_started_at: Option<Instant>,
499    /// Monotonic command generation used by daemon journals to distinguish two
500    /// consecutive commands whose terminal bytes happen to be identical.
501    command_generation: u64,
502    /// Random identity of this PTY instance. Unlike the numeric generation it
503    /// changes when a shell is reset and generations start again at zero.
504    incarnation: u64,
505    activity_seq: Arc<AtomicU64>,
506    /// Highest reader sequence whose decoded bytes have reached the output channel.
507    queued_activity_seq: Arc<AtomicU64>,
508    pub(crate) prompt_receipts:
509        std::collections::HashMap<String, crate::runtime::workflow::PromptRecord>,
510    prompt_activity: Arc<StdMutex<Option<PromptActivityGate>>>,
511    /// Exit code of the last completed foreground command, parsed from the prompt
512    /// marker (`──➤<nonce>:<code>`). `None` until a command finishes, and reset
513    /// to `None` while one is running.
514    pub last_exit_code: Option<i32>,
515    /// Maximum output size before truncation
516    max_output_size: usize,
517    /// Flag for output truncation
518    pub output_truncated: bool,
519    /// Bounded scrollback ring of raw PTY lines (newest at the back) plus the
520    /// in-flight partial line, with drop accounting so a truncated scrollback is
521    /// reported to the caller instead of silently shrinking. See `LineRing`.
522    ring: LineRing,
523    /// Hash of the last rendered output we shipped to the caller. Used by the
524    /// delta path in `status_check` to elide repeats when the screen is idle.
525    pub last_returned_hash: Option<u64>,
526    /// Cumulative PTY snapshot already delivered to the caller. Incremental
527    /// status polling diffs against this value, then advances it after rendering.
528    delivered_output: String,
529    /// True from a foreground command's start until a response has reported
530    /// its exit. The prompt can be drained by another reader (the daemon
531    /// journal) between two status polls; the next poll then finds the shell
532    /// idle and must still deliver the final output and exit code instead of
533    /// rejecting the call as if nothing had run.
534    pending_completion_report: bool,
535    /// Optional command a human can run to attach to the same terminal session.
536    pub attach_hint: Option<String>,
537    /// The exact suffix that ends this shell's prompt: `──➤<nonce>`. The nonce is
538    /// a per-shell random value embedded in `PROMPT_COMMAND`, so command output
539    /// can't impersonate the prompt (printing `◉ x──➤` no longer ends a command
540    /// early). Anchoring completion on this instead of the bare glyphs removes
541    /// the false-positive that truncated output when a program echoed them.
542    prompt_end_marker: String,
543    /// Live terminal emulator (fixed-viewport, `vt100`-backed) fed continuously
544    /// by the reader thread. Unlike the scrollback ring (raw lines), it keeps a
545    /// consolidated screen grid — cursor moves, redraws, alternate-screen
546    /// applied — so a TUI yields a stable, non-stacked snapshot. The fixed
547    /// viewport is what lets inline-rendering TUIs (the `agy`/Antigravity CLI,
548    /// which redraws with cursor-up + erase-line in the main screen) consolidate
549    /// instead of ghosting. See `live_snapshot`.
550    live: Arc<StdMutex<LiveTerminal>>,
551    /// Workspace root under which output-offload scratch files are written.
552    /// `None` until a caller sets it (the workspace is only known after
553    /// Initialize, and `execute_command` sets it per command). See
554    /// `crate::utils::scratch_file`.
555    scratch_workspace_root: Option<PathBuf>,
556    /// Scratch file holding the CURRENT command's dropped output head, created
557    /// lazily on the first truncation and reset per command.
558    scratch_path: Option<PathBuf>,
559    /// Bytes already streamed to `scratch_path`, used to enforce `SCRATCH_MAX_BYTES`.
560    scratch_bytes: u64,
561    /// Shared with the reader thread: when set, the current command's output
562    /// is pure discard (scratch budget exhausted), so the reader inserts
563    /// [`DISCARD_READ_THROTTLE`] between reads and lets the kernel PTY buffer
564    /// back-pressure the producer. Cleared on every new command.
565    discard_throttle: Arc<AtomicBool>,
566    /// Windows job object holding the shell and everything it spawns, so a
567    /// dropped shell takes its background children with it (the Unix path
568    /// signals the process group instead).
569    #[cfg(windows)]
570    job: Option<crate::os::windows::ProcessTreeJob>,
571}
572
573impl std::fmt::Debug for PtyShell {
574    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
575        f.debug_struct("PtyShell")
576            .field("size", &format!("{}x{}", self.size.cols, self.size.rows))
577            .field("last_command", &self.last_command)
578            .field("command_running", &self.command_running)
579            .field("current_cwd", &self.current_cwd)
580            .field("output_truncated", &self.output_truncated)
581            .field("output_buffer_len", &self.output_buffer.len())
582            .field("attach_hint", &self.attach_hint)
583            .finish_non_exhaustive()
584    }
585}
586
587/// Resolve the process group to signal for a PTY child, but ONLY when it is
588/// safe: the pid must be a real child (`> 1`) that leads its own group
589/// (`getpgid(pid) == pid`). portable-pty `setsid`s the shell, so a live PTY
590/// child is always its own group leader; if that ever stopped holding,
591/// `kill(-pgid, ...)` would hit some unrelated group, so we refuse instead of
592/// guessing.
593///
594/// The `pid > 1` check is load-bearing: `kill(-1, ...)` means "every process I
595/// may signal" and `kill(0, ...)` means "my own group" - either would be
596/// catastrophic from a teardown path.
597#[cfg(unix)]
598fn killable_group(pid: u32) -> Option<i32> {
599    crate::os::unix::owned_process_group(pid)
600}
601
602/// Send `signal` to the whole process group `pgid` (group-kill via negative
603/// pid). `pgid` must be one vetted by [`killable_group`].
604#[cfg(unix)]
605fn signal_group(pgid: i32, signal: i32) {
606    let _ = crate::os::unix::signal_group(pgid, signal);
607}
608
609impl Drop for PtyShell {
610    /// Kill and reap the shell child **and its whole process group** so neither
611    /// the shell nor anything it spawned leaks.
612    ///
613    /// `std::process::Child::drop` neither kills nor waits, and even
614    /// `Child::kill` signals only the shell itself: a background job it started
615    /// (`npm run dev &`, a `nohup`'d server) would be reparented to init and live
616    /// on. Because portable-pty runs the shell as a session/group leader, we can
617    /// signal the whole group with a negative pid - SIGTERM first so well-behaved
618    /// children can clean up, then `Child::kill` (SIGKILL on the leader, which
619    /// closes the PTY slave so the reader thread's `read()` returns EOF and the
620    /// thread exits), then SIGKILL the group to sweep anything still alive, then
621    /// reap. The pgid is captured while the leader is alive and reused: it stays
622    /// valid until we `wait()`, even after the leader dies. All best-effort.
623    fn drop(&mut self) {
624        // Only group-kill the direct bash/zsh path. Under screen/tmux (`attach_hint`
625        // is `Some`) the `Child` is a multiplexer *client*; the real shell runs in
626        // a detached daemon in another process group, so a group-kill here would
627        // miss the shell entirely and could disturb unrelated sessions of the same
628        // multiplexer. Let the multiplexer own its lifecycle in that case.
629        #[cfg(unix)]
630        let pgid = if self.attach_hint.is_some() {
631            None
632        } else {
633            self.child.process_id().and_then(killable_group)
634        };
635
636        #[cfg(unix)]
637        if let Some(pgid) = pgid {
638            signal_group(pgid, libc::SIGTERM);
639        }
640
641        // Windows: the job object takes the whole tree down at once.
642        #[cfg(windows)]
643        if let Some(job) = self.job.as_ref() {
644            job.terminate();
645        }
646
647        // SIGKILL the leader and close the PTY slave (unblocks the reader thread).
648        let _ = self.child.kill();
649
650        #[cfg(unix)]
651        if let Some(pgid) = pgid {
652            signal_group(pgid, libc::SIGKILL);
653        }
654
655        let _ = self.child.wait();
656    }
657}
658
659impl PtyShell {
660    /// Create a new PTY shell session
661    ///
662    /// # Arguments
663    /// * `initial_dir` - Starting directory for the shell
664    /// * `restricted_mode` - Whether to use bash restricted mode (-r)
665    ///
666    /// # Returns
667    /// A new `PtyShell` instance with an active bash session
668    pub fn new(initial_dir: &Path, restricted_mode: bool) -> Result<Self> {
669        Self::new_with_agent_paths(initial_dir, restricted_mode, None, None)
670    }
671
672    /// Create a PTY with Winx's stable workspace and managed helper paths in
673    /// the child environment. Direct callers keep using [`Self::new`].
674    pub(crate) fn new_with_agent_paths(
675        initial_dir: &Path,
676        restricted_mode: bool,
677        workspace_root: Option<&Path>,
678        temporary_artifact_dir: Option<&Path>,
679    ) -> Result<Self> {
680        info!(
681            "Creating new PTY shell (restricted: {}) in {}",
682            restricted_mode,
683            initial_dir.display()
684        );
685
686        // On Windows, fail fast with an actionable message if no Git for Windows
687        // bash is available, rather than letting `CommandBuilder::new("bash")`
688        // fail later with an opaque OS error.
689        #[cfg(windows)]
690        ensure_windows_shell_available(restricted_mode)?;
691
692        // Configure terminal size
693        let size =
694            PtySize { rows: DEFAULT_ROWS, cols: DEFAULT_COLS, pixel_width: 0, pixel_height: 0 };
695
696        // Open the PTY pair (master + slave)
697        let pair = native_pty_system().openpty(size).context("Failed to open PTY pair")?;
698
699        // Build the command
700        let (mut cmd, attach_hint, shell_kind) = attachable_command(restricted_mode);
701        if shell_kind.is_powershell() && restricted_mode {
702            return Err(anyhow!(
703                "restricted mode needs bash -r; PowerShell has no equivalent restricted mode"
704            ));
705        }
706
707        // Set up environment for proper terminal behavior
708        cmd.env("TERM", "xterm-256color");
709        cmd.env("COLORTERM", "truecolor");
710        cmd.env("PAGER", "cat");
711        cmd.env("GIT_PAGER", "cat");
712        // Agent commands (and the prompt bootstrap line) must not end up in the
713        // user's ~/.bash_history: bash skips saving history when HISTFILE is
714        // empty. Set before exec so it also holds under `bash -r`, which forbids
715        // changing HISTFILE afterwards.
716        cmd.env("HISTFILE", "");
717        cmd.env("COLUMNS", DEFAULT_COLS.to_string());
718        cmd.env("ROWS", DEFAULT_ROWS.to_string());
719        if let Some(workspace_root) = workspace_root {
720            cmd.env("WINX_WORKSPACE_ROOT", workspace_root);
721        }
722        if let Some(temporary_artifact_dir) = temporary_artifact_dir {
723            cmd.env("WINX_TEMP_DIR", temporary_artifact_dir);
724        }
725        // Per-shell random nonce embedded in the prompt so command output can't
726        // forge the completion marker. 64 bits of entropy in hex.
727        let nonce = format!("{:016x}", rand::random::<u64>());
728        // WCGW-style prompt for command completion detection
729        // Note: removed \r\e[2K which was erasing the prompt before it could be detected
730        // `__winx_ec=$?` captures the user command's exit status as the very
731        // first thing PROMPT_COMMAND does, then we print it after the marker.
732        cmd.env(
733            "PROMPT_COMMAND",
734            format!(r#"__winx_ec=$?; printf "◉ %s──➤{nonce}:%s " "$PWD" "$__winx_ec""#),
735        );
736        cmd.cwd(initial_dir);
737
738        // Spawn bash in the PTY slave
739        let child = pair.slave.spawn_command(cmd).context("Failed to spawn bash in PTY")?;
740        #[cfg(windows)]
741        let job = match crate::os::windows::ProcessTreeJob::new()
742            .and_then(|job| child.process_id().map_or(Ok(()), |pid| job.assign(pid)).map(|()| job))
743        {
744            Ok(job) => Some(job),
745            Err(error) => {
746                warn!(%error, "PTY child is not in a job object; background children may outlive the shell");
747                None
748            }
749        };
750
751        // Get reader and writer from master
752        let reader = pair.master.try_clone_reader().context("Failed to clone PTY reader")?;
753        let writer = pair.master.take_writer().context("Failed to take PTY writer")?;
754
755        // Bounded channel: if the consumer stalls, the reader thread blocks on
756        // send (natural backpressure via the PTY OS buffer) instead of growing
757        // memory without bound on a runaway producer like `yes`.
758        let (output_tx, output_rx) = mpsc::sync_channel::<String>(1024);
759
760        // Live terminal emulator, shared with the reader thread so the screen
761        // grid stays current without any consumer needing to poll.
762        let live = Arc::new(StdMutex::new(LiveTerminal::new(DEFAULT_ROWS, DEFAULT_COLS)));
763        let discard_throttle = Arc::new(AtomicBool::new(false));
764        let prompt_activity = Arc::new(StdMutex::new(None));
765
766        let activity_seq = Arc::new(AtomicU64::new(0));
767        let queued_activity_seq = Arc::new(AtomicU64::new(0));
768        spawn_pty_reader(
769            reader,
770            output_tx,
771            Arc::clone(&live),
772            Arc::clone(&discard_throttle),
773            activity_seq.clone(),
774            queued_activity_seq.clone(),
775            prompt_activity.clone(),
776        );
777
778        // Create the shell instance
779        let mut shell = Self {
780            master: pair.master,
781            child,
782            writer,
783            output_rx,
784            size,
785            last_command: String::new(),
786            output_buffer: String::new(),
787            command_running: false,
788            current_cwd: initial_dir.to_path_buf(),
789            command_started_at: None,
790            command_generation: 0,
791            incarnation: rand::random::<u64>().max(1),
792            activity_seq,
793            queued_activity_seq,
794            prompt_receipts: std::collections::HashMap::new(),
795            prompt_activity,
796            last_exit_code: None,
797            max_output_size: MAX_OUTPUT_SIZE,
798            output_truncated: false,
799            ring: LineRing::new(RING_BUFFER_LINES, MAX_PARTIAL_LINE_BYTES),
800            last_returned_hash: None,
801            delivered_output: String::new(),
802            pending_completion_report: false,
803            attach_hint,
804            prompt_end_marker: format!("{WCGW_PROMPT_END}{nonce}"),
805            live,
806            scratch_workspace_root: None,
807            scratch_path: None,
808            scratch_bytes: 0,
809            discard_throttle,
810            #[cfg(windows)]
811            job,
812        };
813
814        // Initialize the shell with WCGW-style prompt
815        shell.initialize_prompt(shell_kind, &nonce)?;
816
817        debug!("PTY shell created successfully");
818        Ok(shell)
819    }
820
821    /// Initialize the shell prompt for WCGW compatibility
822    fn initialize_prompt(&mut self, shell_kind: ShellKind, nonce: &str) -> Result<()> {
823        // Set up the dynamic prompt - matches WCGW Python PROMPT_STATEMENT.
824        // zsh ignores PROMPT_COMMAND, so it gets a precmd hook with a blanked
825        // default prompt instead.
826        // Blank the shell's own PS1/PROMPT so the line ends exactly at our `──➤`
827        // marker. Otherwise a user's ~/.bashrc/~/.zshrc PS1 (e.g. `[user@host]$`)
828        // is appended after the marker, and prompt detection — which anchors on a
829        // trailing `──➤` — only fires when the chunk happens to fragment right
830        // before the PS1, making command-completion detection flaky.
831        let prompt_statement = match shell_kind {
832            // Clear precmd_functions too: frameworks (oh-my-zsh/p10k) register the
833            // prompt via that array, so redefining `precmd` alone leaves their
834            // prompt in place and our `──➤` marker never ends the line.
835            ShellKind::Zsh => format!(
836                r#"export GIT_PAGER=cat PAGER=cat; precmd_functions=(); preexec_functions=(); PROMPT=''; RPROMPT=''; precmd() {{ local __winx_ec=$?; printf "◉ %s──➤{nonce}:%s " "$PWD" "$__winx_ec" }}"#
837            ),
838            ShellKind::Bash => format!(
839                r#"export GIT_PAGER=cat PAGER=cat PROMPT_COMMAND='__winx_ec=$?; printf "◉ %s──➤{nonce}:%s " "$PWD" "$__winx_ec"'; PS1=''"#
840            ),
841            ShellKind::PowerShell => powershell_bootstrap(nonce),
842        };
843
844        if shell_kind.is_powershell() {
845            // PSReadLine initializes asynchronously after the first prompt
846            // (it queries the cursor position first). Input written before
847            // that lands in its line editor with the Enter keys dropped, so
848            // the bootstrap and the first command fuse into one unsubmitted
849            // line. Wait for the default `PS ...>` prompt before typing.
850            self.wait_for_initial_powershell_prompt();
851        }
852        self.write_command(&prompt_statement)?;
853
854        // Wait for the first marker-bearing prompt instead of a fixed pause:
855        // everything before it (the shell's startup banner, the echoed
856        // bootstrap line) must never leak into the first command's output, and
857        // Git for Windows bash reaches its first prompt far later than Linux
858        // bash. Once the marker is seen, a short drain swallows the trailing
859        // bytes of that same prompt.
860        let deadline = Instant::now() + PROMPT_BOOTSTRAP_TIMEOUT;
861        let mut bootstrap = String::new();
862        let mut settled = false;
863        while Instant::now() < deadline {
864            match self.output_rx.recv_timeout(Duration::from_millis(50)) {
865                Ok(chunk) => {
866                    self.answer_terminal_queries(&chunk);
867                    bootstrap.push_str(&chunk);
868                    if Self::check_prompt_complete(&bootstrap, &self.prompt_end_marker) {
869                        settled = true;
870                        break;
871                    }
872                }
873                Err(mpsc::RecvTimeoutError::Timeout) => {}
874                Err(mpsc::RecvTimeoutError::Disconnected) => break,
875            }
876        }
877        if settled {
878            std::thread::sleep(Duration::from_millis(30));
879        } else {
880            warn!(
881                timeout_ms = PROMPT_BOOTSTRAP_TIMEOUT.as_millis(),
882                "PTY shell did not print its first prompt in time; continuing anyway"
883            );
884        }
885        bootstrap.push_str(&self.drain_output());
886        debug!(
887            bytes = bootstrap.len(),
888            settled,
889            child_pid = ?self.child.process_id(),
890            "PTY prompt bootstrap drained"
891        );
892
893        Ok(())
894    }
895
896    /// Block until PowerShell has drawn its initial prompt (or the bootstrap
897    /// timeout elapses), answering cursor-position queries on the way.
898    fn wait_for_initial_powershell_prompt(&mut self) {
899        let deadline = Instant::now() + PROMPT_BOOTSTRAP_TIMEOUT;
900        let mut seen = String::new();
901        while Instant::now() < deadline {
902            match self.output_rx.recv_timeout(Duration::from_millis(50)) {
903                Ok(chunk) => {
904                    self.answer_terminal_queries(&chunk);
905                    seen.push_str(&chunk);
906                    let clean = crate::state::terminal::strip_ansi_codes(&seen);
907                    if clean.contains("> ") || clean.contains(">\u{a0}") {
908                        // Give PSReadLine a moment to finish arming its input loop.
909                        std::thread::sleep(Duration::from_millis(100));
910                        return;
911                    }
912                }
913                Err(mpsc::RecvTimeoutError::Timeout) => {}
914                Err(mpsc::RecvTimeoutError::Disconnected) => return,
915            }
916        }
917        warn!(
918            timeout_ms = PROMPT_BOOTSTRAP_TIMEOUT.as_millis(),
919            "PowerShell did not draw its initial prompt in time; sending the bootstrap anyway"
920        );
921    }
922
923    /// Answer terminal queries the console host blocks on.
924    ///
925    /// Windows `ConPTY` is created with `INHERIT_CURSOR` (portable-pty's default)
926    /// and then asks the "terminal" for the cursor position with `ESC[6n`
927    /// before rendering a single byte of the child's output. Winx is that
928    /// terminal, so it must reply with a cursor position report or every shell
929    /// stays silent forever. The reply is harmless on Unix, where no PTY layer
930    /// issues the query.
931    fn answer_terminal_queries(&mut self, chunk: &str) {
932        const CURSOR_POSITION_REQUEST: &str = "\x1b[6n";
933        if !chunk.contains(CURSOR_POSITION_REQUEST) {
934            return;
935        }
936        debug!("PTY answered a cursor position request");
937        if let Err(error) = self.writer.write_all(b"\x1b[1;1R").and_then(|()| self.writer.flush()) {
938            debug!("PTY cursor position report failed: {error}");
939        }
940    }
941
942    /// Write a command to the PTY, submitting it with a carriage return.
943    fn write_command(&mut self, command: &str) -> Result<()> {
944        // Submit with `\r`, matching the foreground path (`send_special_key("Enter")`
945        // also sends `\r`) and a real terminal's Enter key. The two paths used to
946        // disagree — background sent `\n` here, foreground sent `\r` — which is a
947        // real difference for readline-based TUIs. bash's canonical line discipline
948        // maps CR to NL (ICRNL), so plain commands behave identically.
949        let cmd_with_newline = format!("{command}\r");
950        self.writer.write_all(cmd_with_newline.as_bytes()).context("Failed to write to PTY")?;
951        self.writer.flush().context("Failed to flush PTY")?;
952        Ok(())
953    }
954
955    /// Drain any pending output from the PTY channel
956    fn drain_output(&mut self) -> String {
957        let mut output = String::new();
958        let deadline = Instant::now() + Duration::from_millis(200);
959
960        // Drain all available output from the channel
961        while Instant::now() < deadline {
962            match self.output_rx.try_recv() {
963                Ok(chunk) => {
964                    self.answer_terminal_queries(&chunk);
965                    output.push_str(&chunk);
966
967                    // Prevent runaway reads
968                    if output.len() > self.max_output_size {
969                        self.output_truncated = true;
970                        break;
971                    }
972                }
973                Err(TryRecvError::Empty) => {
974                    // No more data, wait briefly for more
975                    thread::sleep(Duration::from_millis(10));
976                }
977                Err(TryRecvError::Disconnected) => {
978                    // Reader thread died
979                    break;
980                }
981            }
982        }
983
984        output
985    }
986
987    /// Send a command to the shell and start reading output
988    pub fn send_command(&mut self, command: &str) -> Result<()> {
989        debug!(bytes = command.len(), "PTY sending command");
990
991        // Clear previous state
992        self.output_buffer.clear();
993        self.output_truncated = false;
994        self.reset_scratch();
995        self.last_command = command.to_string();
996        self.command_running = true;
997        self.mark_command_started();
998        self.last_exit_code = None;
999        // A new command means the next status_check should return whatever
1000        // shows up — drop the dedup hash so we don't elide the first response.
1001        self.last_returned_hash = None;
1002        self.delivered_output.clear();
1003        // Drop retained OSC title/progress so the previous foreground
1004        // program's last title can't masquerade as this command's turn state.
1005        self.live.lock().unwrap_or_else(std::sync::PoisonError::into_inner).clear_osc();
1006
1007        // Write the command
1008        self.write_command(command)?;
1009
1010        Ok(())
1011    }
1012
1013    /// Start the monotonic runtime clock for a newly submitted command.
1014    pub fn mark_command_started(&mut self) {
1015        self.prompt_receipts.clear();
1016        *self.prompt_activity.lock().unwrap_or_else(std::sync::PoisonError::into_inner) = None;
1017        self.command_generation = self.command_generation.saturating_add(1);
1018        self.command_started_at = Some(Instant::now());
1019        self.pending_completion_report = true;
1020    }
1021
1022    /// Whether the most recent foreground command has finished without any
1023    /// response having reported its exit yet.
1024    pub fn completion_report_pending(&self) -> bool {
1025        !self.command_running && self.pending_completion_report
1026    }
1027
1028    /// Record that a response reported the current command as finished.
1029    pub fn mark_completion_reported(&mut self) {
1030        self.pending_completion_report = false;
1031    }
1032
1033    pub fn command_generation(&self) -> u64 {
1034        self.command_generation
1035    }
1036
1037    /// Monotonic count of actual PTY reads, including redraws with identical text.
1038    pub fn activity_seq(&self) -> u64 {
1039        self.activity_seq.load(AtomicOrdering::Acquire)
1040    }
1041
1042    /// Drain an observation without advancing legacy output delivery cursors.
1043    /// Sample the enqueue watermark first: later queued bytes must remain visible
1044    /// as a newer sequence even if the reader advances during this capture.
1045    pub(crate) fn poll_workflow_output(&mut self) -> u64 {
1046        let activity_seq = self.queued_activity_seq.load(AtomicOrdering::Acquire);
1047        self.poll_output_nonblocking();
1048        activity_seq
1049    }
1050
1051    /// Arm strict activity beside the reader using the same full-screen snapshot
1052    /// that validates readiness. The reader uses the same live -> gate lock order.
1053    pub(crate) fn arm_prompt_activity(
1054        &mut self,
1055        hint: &str,
1056        text: &str,
1057    ) -> crate::errors::Result<(u64, Arc<AtomicBool>)> {
1058        let recognizer = crate::state::turn::recognizer_for(hint);
1059        let ambiguous_echo = prompt_text_mimics_activity(recognizer.as_ref(), text);
1060        let live = self.live.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
1061        let screen = live.snapshot(0);
1062        let input = crate::state::turn::TurnInput {
1063            screen: &screen,
1064            osc_title: live.osc_title(),
1065            osc_progress: live.osc_progress(),
1066        };
1067        if recognizer.detect_input(&input) != crate::state::turn::TurnState::AwaitingInput {
1068            return Err(crate::errors::WinxError::InvalidInput(
1069                "native prompt requires a running command at a recognized input-ready prompt"
1070                    .to_string(),
1071            ));
1072        }
1073        let baseline_seq = self.activity_seq();
1074        let observed = Arc::new(AtomicBool::new(false));
1075        *self.prompt_activity.lock().unwrap_or_else(std::sync::PoisonError::into_inner) =
1076            Some(PromptActivityGate {
1077                recognizer,
1078                baseline_seq,
1079                observed: observed.clone(),
1080                submitted: false,
1081                ambiguous_echo,
1082                blocked_state: None,
1083            });
1084        Ok((baseline_seq, observed))
1085    }
1086
1087    /// Publish successful Enter delivery before the reader can classify its output.
1088    /// Typed echo present before Enter cannot itself satisfy strict activity.
1089    pub(crate) fn submit_prompt_enter(&mut self) -> Result<()> {
1090        let live = self.live.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
1091        let mut gate =
1092            self.prompt_activity.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
1093        let gate = gate.as_mut().ok_or_else(|| anyhow!("prompt activity gate is unavailable"))?;
1094        let screen = live.snapshot(0);
1095        let state = gate.recognizer.detect_input(&crate::state::turn::TurnInput {
1096            screen: &screen,
1097            osc_title: live.osc_title(),
1098            osc_progress: live.osc_progress(),
1099        });
1100        gate.blocked_state = matches!(
1101            state,
1102            crate::state::turn::TurnState::Busy | crate::state::turn::TurnState::AwaitingApproval
1103        )
1104        .then_some(state);
1105        self.writer.write_all(b"\r")?;
1106        self.writer.flush()?;
1107        gate.submitted = true;
1108        Ok(())
1109    }
1110
1111    pub(crate) fn retained_workflow(
1112        &self,
1113        activity_seq: u64,
1114    ) -> crate::runtime::workflow::RetainedWorkflow {
1115        crate::runtime::workflow::RetainedWorkflow {
1116            generation: self.command_generation(),
1117            activity_seq,
1118            screen: self.live_snapshot(0),
1119            osc_title: self.live_osc_title(),
1120            osc_progress: self.live_osc_progress(),
1121            receipts: self.prompt_receipts.clone(),
1122        }
1123    }
1124
1125    pub fn incarnation(&self) -> u64 {
1126        self.incarnation
1127    }
1128
1129    /// Elapsed runtime since the current command was submitted.
1130    pub fn command_elapsed(&self) -> Option<Duration> {
1131        self.command_started_at.map(|started_at| started_at.elapsed())
1132    }
1133
1134    /// Current working directory last reported by this shell's prompt.
1135    pub fn current_cwd(&self) -> &Path {
1136        &self.current_cwd
1137    }
1138
1139    /// Snapshot used as the left-hand side of incremental output rendering.
1140    pub fn delivered_output(&self) -> String {
1141        self.delivered_output.clone()
1142    }
1143
1144    /// Mark the cumulative output snapshot as delivered after rendering it.
1145    pub fn mark_output_delivered(&mut self, output: &str) {
1146        self.delivered_output.clear();
1147        self.delivered_output.push_str(output);
1148    }
1149
1150    /// Return up to `lines` recent lines from the scrollback ring, oldest
1151    /// first. When older lines were evicted or a long unterminated line was
1152    /// clipped, a `[winx: …]` notice is prepended so the caller knows the
1153    /// scrollback is incomplete rather than silently receiving less than asked.
1154    pub fn collect_scrollback(&self, lines: usize) -> String {
1155        // The ring holds raw PTY lines. Replay them through the terminal
1156        // emulator so cursor movements (readline echo, in-place redraws) are
1157        // *applied* — not merely stripped — yielding what the screen actually
1158        // showed. A plain strip can't undo a `\x1b[D`, so it would leave
1159        // `>>> p>>> pr>>> pri...` echo noise behind; the emulator collapses it.
1160        let rendered =
1161            crate::state::terminal::render_terminal_output(&self.ring.raw(lines)).join("\n");
1162        match self.ring.scrollback_notice(lines) {
1163            Some(notice) => format!("{notice}\n{rendered}"),
1164            None => rendered,
1165        }
1166    }
1167
1168    /// Hash arbitrary rendered output into a u64 dedup key.
1169    pub fn fingerprint(text: &str) -> u64 {
1170        let mut hasher = DefaultHasher::new();
1171        text.hash(&mut hasher);
1172        hasher.finish()
1173    }
1174
1175    /// Read output from the PTY with timeout
1176    ///
1177    /// Returns (output, `is_complete`) tuple where `is_complete` indicates
1178    /// whether the command has finished (prompt detected)
1179    pub fn read_output(&mut self, timeout_secs: f32) -> Result<(String, bool)> {
1180        let timeout = Duration::from_secs_f32(timeout_secs.clamp(0.1, 60.0));
1181        let start = Instant::now();
1182        let mut complete = false;
1183        let mut no_data_count = 0;
1184        let mut prompt_detected_at: Option<Instant> = None;
1185
1186        while start.elapsed() < timeout {
1187            match self.output_rx.try_recv() {
1188                Ok(chunk) => {
1189                    self.answer_terminal_queries(&chunk);
1190                    self.output_buffer.push_str(&chunk);
1191                    self.ring.push_chunk(&chunk);
1192                    no_data_count = 0;
1193
1194                    // Check for WCGW prompt indicating command completion
1195                    if prompt_detected_at.is_none()
1196                        && (Self::check_prompt_complete(&chunk, &self.prompt_end_marker)
1197                            || Self::check_prompt_complete(
1198                                &self.output_buffer,
1199                                &self.prompt_end_marker,
1200                            ))
1201                    {
1202                        prompt_detected_at = Some(Instant::now());
1203                        debug!("Prompt detected, draining remaining output...");
1204                    }
1205
1206                    // Truncate if too large (offloads the dropped head to scratch).
1207                    self.truncate_output_buffer_with_offload();
1208                }
1209                Err(TryRecvError::Empty) => {
1210                    // No data available, wait briefly
1211                    thread::sleep(Duration::from_millis(10));
1212                    no_data_count += 1;
1213
1214                    // If prompt was detected, check if we've drained long enough
1215                    if let Some(detected_time) = prompt_detected_at {
1216                        // Wait 100ms after prompt detection to capture any trailing output
1217                        if detected_time.elapsed() > Duration::from_millis(100) {
1218                            complete = true;
1219                            debug!("Command completed - prompt detected and drained");
1220                            break;
1221                        }
1222                    } else if no_data_count > 10
1223                        && Self::check_prompt_complete(&self.output_buffer, &self.prompt_end_marker)
1224                    {
1225                        // Prompt detected during empty reads
1226                        prompt_detected_at = Some(Instant::now());
1227                        debug!("Prompt detected after wait, draining...");
1228                    }
1229                }
1230                Err(TryRecvError::Disconnected) => {
1231                    // Reader thread died - PTY closed
1232                    warn!("PTY reader disconnected");
1233                    complete = true;
1234                    break;
1235                }
1236            }
1237        }
1238
1239        if complete || prompt_detected_at.is_some() {
1240            self.command_running = false;
1241            self.last_exit_code =
1242                Self::parse_prompt_exit_code(&self.output_buffer, &self.prompt_end_marker);
1243            self.update_current_cwd_from_prompt();
1244            complete = true;
1245        }
1246
1247        Ok((self.output_buffer.clone(), complete))
1248    }
1249
1250    /// Non-blocking drain of whatever the reader thread has already queued.
1251    ///
1252    /// Unlike [`read_output`](Self::read_output), this never sleeps: it consumes
1253    /// every chunk currently available, updates prompt/completion state, and
1254    /// returns immediately. `prune_finished_shells` uses it so the global
1255    /// background-shell lock is never held across a blocking 100ms `read_output`.
1256    /// Returns `true` if the command has completed (prompt seen or PTY closed).
1257    pub fn poll_output_nonblocking(&mut self) -> bool {
1258        let mut prompt_seen = false;
1259        loop {
1260            match self.output_rx.try_recv() {
1261                Ok(chunk) => {
1262                    self.answer_terminal_queries(&chunk);
1263                    let chunk_has_prompt =
1264                        Self::check_prompt_complete(&chunk, &self.prompt_end_marker);
1265                    self.output_buffer.push_str(&chunk);
1266                    self.ring.push_chunk(&chunk);
1267                    if chunk_has_prompt
1268                        || Self::check_prompt_complete(&self.output_buffer, &self.prompt_end_marker)
1269                    {
1270                        prompt_seen = true;
1271                    }
1272                    self.truncate_output_buffer_with_offload();
1273                }
1274                Err(TryRecvError::Empty) => break,
1275                Err(TryRecvError::Disconnected) => {
1276                    self.command_running = false;
1277                    return true;
1278                }
1279            }
1280        }
1281        if prompt_seen {
1282            self.command_running = false;
1283            self.last_exit_code =
1284                Self::parse_prompt_exit_code(&self.output_buffer, &self.prompt_end_marker);
1285            self.update_current_cwd_from_prompt();
1286        }
1287        // Completion is state, not an edge. Another consumer (notably the
1288        // background reaper) may have drained the prompt first; a later poll
1289        // must still report the already-idle shell as complete even when this
1290        // particular drain saw no new prompt bytes.
1291        !self.command_running
1292    }
1293
1294    fn update_current_cwd_from_prompt(&mut self) {
1295        let Some(last) =
1296            self.output_buffer.lines().rev().find(|line| line.contains(&self.prompt_end_marker))
1297        else {
1298            return;
1299        };
1300        let clean = crate::state::terminal::strip_ansi_codes(last);
1301        let Some((before_marker, _)) = clean.rsplit_once(&self.prompt_end_marker) else {
1302            return;
1303        };
1304        let Some((_, cwd)) = before_marker.rsplit_once("◉ ") else {
1305            return;
1306        };
1307        let cwd = cwd.trim();
1308        if !cwd.is_empty() {
1309            self.current_cwd = native_cwd_from_prompt(cwd);
1310        }
1311    }
1312
1313    /// Snapshot of the accumulated output buffer — the same text `read_output`
1314    /// returns. Lets callers poll non-blocking (`poll_output_nonblocking`) and
1315    /// grab the buffer once at the end instead of cloning it on every slice.
1316    pub fn output_snapshot(&self) -> String {
1317        self.output_buffer.clone()
1318    }
1319
1320    /// Check if the output ends with this shell's prompt.
1321    ///
1322    /// `prompt_end` is the per-shell `──➤<nonce>` suffix. Anchoring on the LAST
1323    /// non-empty line (not a global `contains`) plus the random nonce means
1324    /// command output that happens to print the prompt glyphs mid-stream — or
1325    /// even a forged `◉ x──➤ ` at end of line — can't be mistaken for the prompt
1326    /// and truncate output or end the command early.
1327    fn check_prompt_complete(text: &str, prompt_end: &str) -> bool {
1328        Self::parse_prompt_exit_code(text, prompt_end).is_some()
1329    }
1330
1331    /// If the last non-empty line is this shell's completion prompt
1332    /// (`◉ <pwd>──➤<nonce>:<code>`), return the exit code that follows the
1333    /// marker. Returns `None` when the prompt isn't present — which is exactly
1334    /// "the command is still running". The per-shell nonce plus the structured
1335    /// `:<digits>` suffix keep command output from forging completion.
1336    fn parse_prompt_exit_code(text: &str, prompt_end: &str) -> Option<i32> {
1337        let last = text.lines().rev().find(|line| !line.trim().is_empty())?;
1338        if !last.contains(WCGW_PROMPT_PATTERN) {
1339            return None;
1340        }
1341        // Everything after the last "──➤<nonce>" must be ":<digits>" followed by
1342        // the prompt's own terminator — this confirms the prompt ends the line
1343        // AND yields the code. The raw tail keeps its escape sequences: a
1344        // trailing erase/cursor sequence (":0 \x1b[K", or ConPTY's ":0\x1b[C")
1345        // counts as the terminator, so it is stripped only for parsing.
1346        let raw_tail = last.rsplit_once(prompt_end)?.1;
1347        let clean_tail = crate::state::terminal::strip_ansi_codes(raw_tail);
1348        let code_text = clean_tail.strip_prefix(':')?;
1349        let digits = code_text
1350            .char_indices()
1351            .take_while(|(index, c)| c.is_ascii_digit() || (*index == 0 && *c == '-'))
1352            .count();
1353        let (code, rest) = code_text.split_at(digits);
1354        if !rest.chars().all(char::is_whitespace) {
1355            return None;
1356        }
1357        // A read boundary can fall between the digits of "127": without the
1358        // terminator the record is still in flight, and accepting "1" now would
1359        // report the wrong exit code before the rest arrives.
1360        let terminated = !rest.is_empty() || raw_tail.len() > clean_tail.len();
1361        if !terminated {
1362            return None;
1363        }
1364        code.parse::<i32>().ok()
1365    }
1366
1367    /// Point output-offload scratch files at `root` (the workspace). Idempotent;
1368    /// callers set it per command so it always tracks the current workspace.
1369    pub fn set_scratch_root(&mut self, root: &Path) {
1370        self.scratch_workspace_root = Some(root.to_path_buf());
1371    }
1372
1373    /// The scratch file holding the current command's dropped output head, if any
1374    /// was offloaded this command.
1375    pub fn scratch_path(&self) -> Option<&Path> {
1376        self.scratch_path.as_deref()
1377    }
1378
1379    /// Forget the current command's scratch file so the next command starts a
1380    /// fresh one. Does not delete the file (pruned later by age).
1381    pub fn reset_scratch(&mut self) {
1382        self.scratch_path = None;
1383        self.scratch_bytes = 0;
1384        self.discard_throttle.store(false, AtomicOrdering::Relaxed);
1385    }
1386
1387    /// Append a just-dropped output `head` to the per-command scratch file so the
1388    /// agent can recover it via `ReadFiles`. Best-effort: needs a known workspace
1389    /// root, stops at `SCRATCH_MAX_BYTES`, and swallows IO errors.
1390    fn offload_dropped_head(&mut self, head: &str) {
1391        let Some(root) = self.scratch_workspace_root.clone() else {
1392            return;
1393        };
1394        // Clamp to the remaining byte budget so the file honors SCRATCH_MAX_BYTES
1395        // exactly (a single head can be up to keep_size, ~500 KB). Snap to a char
1396        // boundary so we never slice mid-UTF-8.
1397        let remaining = SCRATCH_MAX_BYTES.saturating_sub(self.scratch_bytes);
1398        if remaining == 0 {
1399            // From here on the head is discarded outright — reading faster
1400            // only burns CPU. Engage reader back-pressure until the next
1401            // command resets the scratch state.
1402            self.discard_throttle.store(true, AtomicOrdering::Relaxed);
1403            return;
1404        }
1405        let head = match usize::try_from(remaining) {
1406            Ok(rem) if rem < head.len() => &head[..crate::utils::floor_char_boundary(head, rem)],
1407            _ => head,
1408        };
1409        if self.scratch_path.is_none() {
1410            self.scratch_path = crate::utils::scratch_file::new_scratch_path(&root);
1411        }
1412        let Some(path) = self.scratch_path.clone() else {
1413            return;
1414        };
1415        match crate::utils::scratch_file::append_scratch(&path, head.as_bytes()) {
1416            Ok(0) if self.scratch_bytes == 0 => self.scratch_path = None,
1417            Ok(written) => {
1418                self.scratch_bytes = self.scratch_bytes.saturating_add(written as u64);
1419                if self.scratch_bytes >= SCRATCH_MAX_BYTES {
1420                    self.discard_throttle.store(true, AtomicOrdering::Relaxed);
1421                }
1422            }
1423            Err(e) => debug!("scratch: append to {} failed: {e}", path.display()),
1424        }
1425    }
1426
1427    /// Once `output_buffer` exceeds the cap, offload the head about to be dropped
1428    /// to the scratch file, then keep only the tail with a truncation marker.
1429    /// Shared by the blocking and non-blocking drain paths so the offload and the
1430    /// char-boundary handling live in one place.
1431    fn truncate_output_buffer_with_offload(&mut self) {
1432        if self.output_buffer.len() <= self.max_output_size {
1433            return;
1434        }
1435        self.output_truncated = true;
1436        let keep_size = self.max_output_size / 2;
1437        // Snap to a char boundary: a raw byte offset can land mid-UTF-8
1438        // (CJK/emoji/box-drawing/the prompt glyphs) and slicing there would panic.
1439        let cut = crate::utils::floor_char_boundary(
1440            &self.output_buffer,
1441            self.output_buffer.len() - keep_size,
1442        );
1443        let head = self.output_buffer[..cut].to_owned();
1444        self.offload_dropped_head(&head);
1445        // Replace the dropped head in place instead of `format!`-ing a brand-new
1446        // String: `replace_range` shifts the kept tail down once and reuses the
1447        // buffer's allocation. `cut` is already a char boundary (above), and 0 is
1448        // always one, so the range is valid.
1449        self.output_buffer.replace_range(..cut, "\n(...output truncated...)\n");
1450    }
1451
1452    /// Send Ctrl+C (interrupt) to the PTY
1453    pub fn send_interrupt(&mut self) -> Result<()> {
1454        debug!("PTY sending Ctrl+C");
1455        self.writer
1456            .write_all(&[0x03]) // ASCII ETX (Ctrl+C)
1457            .context("Failed to send Ctrl+C")?;
1458        self.writer.flush()?;
1459        Ok(())
1460    }
1461
1462    /// Send Ctrl+D (EOF) to the PTY
1463    pub fn send_eof(&mut self) -> Result<()> {
1464        debug!("PTY sending Ctrl+D");
1465        self.writer
1466            .write_all(&[0x04]) // ASCII EOT (Ctrl+D)
1467            .context("Failed to send Ctrl+D")?;
1468        self.writer.flush()?;
1469        Ok(())
1470    }
1471
1472    /// Send Ctrl+Z (suspend) to the PTY
1473    pub fn send_suspend(&mut self) -> Result<()> {
1474        debug!("PTY sending Ctrl+Z");
1475        self.writer
1476            .write_all(&[0x1A]) // ASCII SUB (Ctrl+Z)
1477            .context("Failed to send Ctrl+Z")?;
1478        self.writer.flush()?;
1479        Ok(())
1480    }
1481
1482    /// Send text directly to the PTY (for interactive input)
1483    pub fn send_text(&mut self, text: &str) -> Result<()> {
1484        debug!(bytes = text.len(), "PTY sending text");
1485        self.send_bytes(text.as_bytes()).context("Failed to send text")?;
1486        Ok(())
1487    }
1488
1489    /// Send raw bytes directly to the PTY.
1490    pub fn send_bytes(&mut self, bytes: &[u8]) -> Result<()> {
1491        self.writer.write_all(bytes).context("Failed to send bytes")?;
1492        self.writer.flush()?;
1493        Ok(())
1494    }
1495
1496    /// Send a special key sequence
1497    pub fn send_special_key(&mut self, key: &str) -> Result<()> {
1498        let bytes: &[u8] = match key {
1499            "Enter" => b"\r",
1500            "Tab" => b"\t",
1501            "Backspace" => b"\x7F",
1502            "Escape" => b"\x1B",
1503            "Up" | "KeyUp" => b"\x1B[A",
1504            "Down" | "KeyDown" => b"\x1B[B",
1505            "Right" | "KeyRight" => b"\x1B[C",
1506            "Left" | "KeyLeft" => b"\x1B[D",
1507            "Home" => b"\x1B[H",
1508            "End" => b"\x1B[F",
1509            "PageUp" => b"\x1B[5~",
1510            "PageDown" => b"\x1B[6~",
1511            "Delete" => b"\x1B[3~",
1512            "Insert" => b"\x1B[2~",
1513            "CtrlC" | "Ctrl-C" => b"\x03",
1514            "CtrlD" | "Ctrl-D" => b"\x04",
1515            "CtrlZ" | "Ctrl-Z" => b"\x1A",
1516            "CtrlL" | "Ctrl-L" => b"\x0C",
1517            _ => return Err(anyhow!("Unknown special key: {key}")),
1518        };
1519
1520        debug!("PTY sending special key: {} ({:?})", key, bytes);
1521        self.send_bytes(bytes)?;
1522        Ok(())
1523    }
1524
1525    /// Snapshot the live terminal screen — a stable, consolidated view of what a
1526    /// human would currently see, with cursor moves/redraws applied and ANSI
1527    /// stripped. `max_lines` of 0 returns the full screen buffer. This is the
1528    /// foundation for piloting interactive TUIs (the `claude` CLI, vim, ...).
1529    pub fn live_snapshot(&self, max_lines: usize) -> Vec<String> {
1530        // Recover from a poisoned lock instead of returning a blank screen
1531        // forever — a single panic while holding `live` would otherwise wedge
1532        // every future snapshot (and `wait_for_turn`/`screen` with it).
1533        self.live.lock().unwrap_or_else(std::sync::PoisonError::into_inner).snapshot(max_lines)
1534    }
1535
1536    /// Whether the live terminal is currently on the alternate screen buffer
1537    /// (a full-screen app like vim/htop/less is running).
1538    pub fn live_in_alt_screen(&self) -> bool {
1539        self.live.lock().unwrap_or_else(std::sync::PoisonError::into_inner).in_alt_screen()
1540    }
1541
1542    /// The latest OSC 0/2 window title emitted by the foreground program (`""`
1543    /// when none) — an out-of-band turn signal for the manifest recognizers
1544    /// (claude/codex render a spinner glyph in the title while generating).
1545    pub fn live_osc_title(&self) -> String {
1546        self.live.lock().unwrap_or_else(std::sync::PoisonError::into_inner).osc_title().to_string()
1547    }
1548
1549    /// The latest OSC 9 payload after `9;` (`""` when none) — ConEmu-style
1550    /// progress reads `4;<state>;<percent>`.
1551    pub fn live_osc_progress(&self) -> String {
1552        self.live
1553            .lock()
1554            .unwrap_or_else(std::sync::PoisonError::into_inner)
1555            .osc_progress()
1556            .to_string()
1557    }
1558
1559    /// Cursor position `(row, col)` on the live screen (0-based), so a piloting
1560    /// agent knows where focus is in a menu/form. `(0, 0)` if the lock is poisoned.
1561    pub fn live_cursor_position(&self) -> (u16, u16) {
1562        self.live.lock().unwrap_or_else(std::sync::PoisonError::into_inner).cursor_position()
1563    }
1564
1565    /// Diff the live screen against what the client last saw, returning only the
1566    /// changed lines when the change is small (huge token savings over many
1567    /// polls). Updates the baseline as a side effect. See [`ScreenUpdate`].
1568    pub fn live_snapshot_diff(&self, max_lines: usize, threshold: usize) -> ScreenUpdate {
1569        self.live
1570            .lock()
1571            .unwrap_or_else(std::sync::PoisonError::into_inner)
1572            .snapshot_diff(max_lines, threshold)
1573    }
1574
1575    /// Resize the terminal
1576    pub fn resize(&mut self, cols: u16, rows: u16) -> Result<()> {
1577        debug!("PTY resizing to {}x{}", cols, rows);
1578
1579        let new_size = PtySize { rows, cols, pixel_width: 0, pixel_height: 0 };
1580
1581        self.master.resize(new_size).context("Failed to resize PTY")?;
1582
1583        // Keep the live emulator's viewport in lockstep with the PTY size.
1584        self.live.lock().unwrap_or_else(std::sync::PoisonError::into_inner).resize(rows, cols);
1585
1586        self.size = new_size;
1587        Ok(())
1588    }
1589
1590    /// Get current terminal size
1591    pub fn get_size(&self) -> (u16, u16) {
1592        (self.size.cols, self.size.rows)
1593    }
1594
1595    /// OS process id of the shell that owns this PTY.
1596    pub fn process_id(&self) -> Option<u32> {
1597        self.child.process_id()
1598    }
1599
1600    /// Check if the shell is still alive
1601    pub fn is_alive(&mut self) -> bool {
1602        self.child.try_wait().is_ok_and(|status| status.is_none())
1603    }
1604}
1605
1606/// Thread-safe wrapper for `PtyShell`
1607pub type SharedPtyShell = Arc<Mutex<Option<PtyShell>>>;
1608
1609/// Create a new shared PTY shell
1610pub fn create_shared_pty(initial_dir: &Path, restricted_mode: bool) -> Result<SharedPtyShell> {
1611    let shell = PtyShell::new(initial_dir, restricted_mode)?;
1612    Ok(Arc::new(Mutex::new(Some(shell))))
1613}
1614
1615/// Split a byte slice into the longest valid-UTF-8 prefix (decoded) and the
1616/// trailing bytes of an incomplete final char (to carry into the next read).
1617/// Genuinely invalid sequences become a single U+FFFD and are consumed, so the
1618/// carry only ever holds a recoverable incomplete tail and never grows unbounded.
1619///
1620/// Iterative on purpose: a recursive version blows the reader thread's stack on
1621/// a burst of invalid bytes (e.g. `cat`-ing a binary), which aborts under
1622/// `panic = "abort"`. This stays O(1) in stack depth regardless of input.
1623fn decode_keep_incomplete(bytes: &[u8]) -> (String, Vec<u8>) {
1624    let mut decoded = String::new();
1625    let mut rest = bytes;
1626    loop {
1627        match std::str::from_utf8(rest) {
1628            Ok(text) => {
1629                decoded.push_str(text);
1630                return (decoded, Vec::new());
1631            }
1632            Err(error) => {
1633                let valid = error.valid_up_to();
1634                // `rest[..valid]` is valid UTF-8 by construction (no allocation
1635                // for the borrowed case).
1636                decoded.push_str(&String::from_utf8_lossy(&rest[..valid]));
1637                match error.error_len() {
1638                    // Incomplete final char: keep the tail for the next read.
1639                    None => return (decoded, rest[valid..].to_vec()),
1640                    // Genuinely invalid run: emit one replacement and skip past it.
1641                    Some(bad) => {
1642                        decoded.push('\u{FFFD}');
1643                        rest = &rest[valid + bad..];
1644                    }
1645                }
1646            }
1647        }
1648    }
1649}
1650
1651#[cfg(test)]
1652mod tests {
1653    use super::*;
1654    #[cfg(unix)] // only the shell-spawning tests need a workspace
1655    use tempfile::TempDir;
1656
1657    #[test]
1658    fn decode_passes_clean_utf8_through() {
1659        let (text, carry) = decode_keep_incomplete("hello ✓ café".as_bytes());
1660        assert_eq!(text, "hello ✓ café");
1661        assert!(carry.is_empty(), "expected empty carry");
1662    }
1663
1664    #[test]
1665    fn decode_carries_incomplete_trailing_char_across_reads() {
1666        // "é" is 0xC3 0xA9; a read boundary splits it. The first half must NOT
1667        // turn into U+FFFD — it's held and completed on the next read.
1668        let (text1, carry1) = decode_keep_incomplete(&[b'a', 0xC3]);
1669        assert_eq!(text1, "a");
1670        assert_eq!(carry1, vec![0xC3]);
1671
1672        let mut next = carry1;
1673        next.push(0xA9); // continuation byte arrives in the next read
1674        let (text2, carry2) = decode_keep_incomplete(&next);
1675        assert_eq!(text2, "é");
1676        assert!(carry2.is_empty(), "expected empty carry2");
1677    }
1678
1679    #[test]
1680    fn decode_replaces_genuinely_invalid_bytes() {
1681        // 0xFF is never valid UTF-8 and is not an incomplete prefix.
1682        let (text, carry) = decode_keep_incomplete(&[b'x', 0xFF, b'y']);
1683        assert_eq!(text, "x\u{FFFD}y");
1684        assert!(carry.is_empty(), "expected empty carry");
1685    }
1686
1687    #[test]
1688    fn decode_handles_large_invalid_burst_without_overflow() {
1689        // Regression: the first (recursive) version blew the stack — one frame
1690        // per invalid byte — and aborted under panic="abort" on a binary dump.
1691        // The iterative version must handle far more than the 4KB reader buffer.
1692        let big = vec![0xFFu8; 200_000];
1693        let (text, carry) = decode_keep_incomplete(&big);
1694        assert!(carry.is_empty(), "expected empty carry");
1695        assert_eq!(text.chars().filter(|&c| c == '\u{FFFD}').count(), 200_000);
1696    }
1697
1698    #[test]
1699    fn screen_session_name_parser_is_strict() {
1700        assert_eq!(super::winx_creator_pid("winx-123-456"), Some(123));
1701        assert_eq!(super::winx_creator_pid("winx-nope-456"), None);
1702        assert_eq!(super::winx_creator_pid("other-123-456"), None);
1703    }
1704
1705    #[cfg(unix)]
1706    #[test]
1707    fn process_probe_recognizes_current_process_and_rejects_reserved_pids() {
1708        assert!(super::process_exists(std::process::id()));
1709        assert!(!super::process_exists(0));
1710        assert!(!super::process_exists(1));
1711    }
1712
1713    #[test]
1714    fn msys_paths_convert_to_native_drives() {
1715        use super::msys_to_native;
1716        assert_eq!(msys_to_native("/c/Users/x/ws"), Some(PathBuf::from("C:\\Users\\x\\ws")));
1717        assert_eq!(msys_to_native("/cygdrive/d/work"), Some(PathBuf::from("D:\\work")));
1718        assert_eq!(msys_to_native("/c"), Some(PathBuf::from("C:\\")));
1719        assert_eq!(msys_to_native("/home/x"), None);
1720        assert_eq!(msys_to_native("relative"), None);
1721    }
1722
1723    #[test]
1724    fn prompt_detection_is_suffix_anchored() {
1725        // Per-shell nonce + exit code after the arrow: `──➤<nonce>:<code>`.
1726        let end = "──➤deadbeefcafe0001";
1727        // real prompt on the last line -> complete
1728        assert!(PtyShell::check_prompt_complete("out\nmore\n◉ /home/x──➤deadbeefcafe0001:0 ", end));
1729        // prompt with trailing ANSI erase -> still complete
1730        assert!(PtyShell::check_prompt_complete("◉ /home/x──➤deadbeefcafe0001:0 \u{1b}[K", end));
1731        // the bug: ◉ and ──➤ appear MID-output, last line is normal -> NOT complete
1732        assert!(!PtyShell::check_prompt_complete(
1733            "menu: ◉ start ──➤deadbeefcafe0001 stop\nstill running",
1734            end
1735        ));
1736        // command echoed after the prompt (not the waiting prompt) -> not complete
1737        assert!(!PtyShell::check_prompt_complete("◉ /home/x──➤deadbeefcafe0001:0 ls -la", end));
1738        // no prompt at all
1739        assert!(!PtyShell::check_prompt_complete("just some output\n", end));
1740        // the nonce's payoff: output that forges the bare glyphs but NOT the
1741        // nonce (or omits the exit code) must not be mistaken for the prompt.
1742        assert!(!PtyShell::check_prompt_complete("◉ /fake──➤ ", end));
1743        assert!(!PtyShell::check_prompt_complete("◉ /fake──➤wrongnonce:0 ", end));
1744        assert!(!PtyShell::check_prompt_complete("◉ /home/x──➤deadbeefcafe0001 ", end));
1745    }
1746
1747    #[test]
1748    fn powershell_bootstrap_defines_a_marker_prompt_without_psreadline() {
1749        let bootstrap = super::powershell_bootstrap("cafe1234");
1750        assert!(bootstrap.contains("Remove-Module PSReadLine"), "{bootstrap}");
1751        assert!(bootstrap.contains("function global:prompt"), "{bootstrap}");
1752        assert!(bootstrap.contains("──➤cafe1234:$code "), "{bootstrap}");
1753        assert!(bootstrap.contains("$global:LASTEXITCODE = 0"), "{bootstrap}");
1754        assert!(!bootstrap.contains('\n'), "bootstrap must be a single input line");
1755    }
1756
1757    #[test]
1758    fn parses_exit_code_from_prompt_marker() {
1759        let end = "──➤cafe1234";
1760        assert_eq!(PtyShell::parse_prompt_exit_code("ok\n◉ /x──➤cafe1234:0 ", end), Some(0));
1761        assert_eq!(PtyShell::parse_prompt_exit_code("oops\n◉ /x──➤cafe1234:1 ", end), Some(1));
1762        assert_eq!(PtyShell::parse_prompt_exit_code("◉ /x──➤cafe1234:42 ", end), Some(42));
1763        // A record split by a read boundary inside the digits is still in flight.
1764        assert_eq!(PtyShell::parse_prompt_exit_code("◉ /x──➤cafe1234:1", end), None);
1765        assert_eq!(PtyShell::parse_prompt_exit_code("◉ /x──➤cafe1234:127", end), None);
1766        assert_eq!(PtyShell::parse_prompt_exit_code("◉ /x──➤cafe1234:127 ", end), Some(127));
1767        // ConPTY may replace the trailing space with a cursor move.
1768        assert_eq!(PtyShell::parse_prompt_exit_code("◉ /x──➤cafe1234:127\u{1b}[C", end), Some(127));
1769        assert_eq!(PtyShell::parse_prompt_exit_code("◉ /x──➤cafe1234:0\u{1b}[K", end), Some(0));
1770        assert_eq!(PtyShell::parse_prompt_exit_code("◉ /x──➤cafe1234:-1 ", end), Some(-1));
1771        assert_eq!(PtyShell::parse_prompt_exit_code("◉ /x──➤cafe1234:12a ", end), None);
1772        // still running (no completion prompt yet) -> None
1773        assert_eq!(PtyShell::parse_prompt_exit_code("building...", end), None);
1774        // forged with the wrong nonce -> None
1775        assert_eq!(PtyShell::parse_prompt_exit_code("◉ /x──➤wrong:0 ", end), None);
1776    }
1777
1778    use proptest::prelude::*;
1779
1780    proptest! {
1781        // Exit-code parsing runs on untrusted program output — arbitrary text
1782        // (and a forgeable marker) must never panic, only ever return None or a
1783        // parsed i32.
1784        #[test]
1785        fn parse_prompt_exit_code_never_panics(
1786            text in prop::collection::vec(any::<char>(), 0..256),
1787            nonce in "[0-9a-f]{0,16}",
1788        ) {
1789            let s: String = text.into_iter().collect();
1790            let end = format!("{WCGW_PROMPT_END}{nonce}");
1791            // Must not panic; any Option<i32> is acceptable.
1792            let _ = PtyShell::parse_prompt_exit_code(&s, &end);
1793        }
1794
1795        // A well-formed marker round-trips the embedded code for any i32.
1796        #[test]
1797        fn parse_prompt_exit_code_round_trips(code in any::<i32>(), nonce in "[0-9a-f]{1,16}") {
1798            let end = format!("{WCGW_PROMPT_END}{nonce}");
1799            let line = format!("output line\n{WCGW_PROMPT_PATTERN} /home/x{end}:{code} ");
1800            prop_assert_eq!(PtyShell::parse_prompt_exit_code(&line, &end), Some(code));
1801        }
1802    }
1803
1804    #[cfg(unix)] // spawns a real shell
1805    #[test]
1806    fn test_pty_shell_creation() -> Result<()> {
1807        let temp_dir = TempDir::new()?;
1808        let result = PtyShell::new(temp_dir.path(), false);
1809        assert!(result.is_ok(), "Failed to create PTY shell: {:?}", result.err());
1810        Ok(())
1811    }
1812
1813    #[cfg(unix)] // spawns a real shell
1814    #[test]
1815    fn discard_throttle_engages_at_scratch_cap_and_resets_per_command() -> Result<()> {
1816        let temp_dir = TempDir::new()?;
1817        let mut shell = PtyShell::new(temp_dir.path(), false)?;
1818        assert!(!shell.discard_throttle.load(AtomicOrdering::Relaxed));
1819
1820        // Saturated scratch budget: the next dropped head is pure discard and
1821        // must engage reader back-pressure.
1822        shell.scratch_workspace_root = Some(temp_dir.path().to_path_buf());
1823        shell.scratch_bytes = SCRATCH_MAX_BYTES;
1824        shell.offload_dropped_head("dropped beyond the budget");
1825        assert!(shell.discard_throttle.load(AtomicOrdering::Relaxed));
1826
1827        // A new command resets scratch state and releases the throttle.
1828        shell.send_command("echo done")?;
1829        assert!(!shell.discard_throttle.load(AtomicOrdering::Relaxed));
1830        Ok(())
1831    }
1832
1833    #[cfg(unix)] // spawns a real shell
1834    #[test]
1835    fn discard_throttle_engages_when_a_write_reaches_the_cap() -> Result<()> {
1836        let temp_dir = TempDir::new()?;
1837        let mut shell = PtyShell::new(temp_dir.path(), false)?;
1838        shell.scratch_workspace_root = Some(temp_dir.path().to_path_buf());
1839        // One byte of budget left: this write saturates the cap exactly.
1840        shell.scratch_bytes = SCRATCH_MAX_BYTES - 1;
1841        shell.offload_dropped_head("xx");
1842        assert!(shell.discard_throttle.load(AtomicOrdering::Relaxed));
1843        Ok(())
1844    }
1845
1846    #[cfg(unix)] // spawns a real shell
1847    #[test]
1848    fn test_pty_shell_echo() -> Result<()> {
1849        let temp_dir = TempDir::new()?;
1850        let mut shell = PtyShell::new(temp_dir.path(), false)?;
1851
1852        shell.send_command("echo 'hello pty'")?;
1853        let (output, complete) = shell.read_output(2.0)?;
1854
1855        assert!(output.contains("hello pty"), "Output should contain 'hello pty': {output}");
1856        // The bootstrap (prompt export line, shell banner) must have been
1857        // consumed before the first command, and the first command must
1858        // already report its exit code.
1859        assert!(complete, "first command must complete: {output}");
1860        assert!(!output.contains("PROMPT_COMMAND"), "bootstrap leaked into output: {output}");
1861        assert_eq!(shell.last_exit_code, Some(0), "{output}");
1862        Ok(())
1863    }
1864
1865    #[cfg(unix)] // spawns a real shell
1866    #[test]
1867    fn agent_paths_are_exported_to_the_pty() -> Result<()> {
1868        let workspace = TempDir::new()?;
1869        let temporary_artifact_dir = workspace.path().join(".winx/tmp/session-test");
1870        let mut shell = PtyShell::new_with_agent_paths(
1871            workspace.path(),
1872            false,
1873            Some(workspace.path()),
1874            Some(&temporary_artifact_dir),
1875        )?;
1876
1877        shell.send_command("printf '%s\\n%s\\n' \"$WINX_WORKSPACE_ROOT\" \"$WINX_TEMP_DIR\"")?;
1878        let (output, _complete) = shell.read_output(2.0)?;
1879
1880        assert!(output.contains(&workspace.path().to_string_lossy().to_string()), "{output}");
1881        assert!(output.contains(&temporary_artifact_dir.to_string_lossy().to_string()), "{output}");
1882        Ok(())
1883    }
1884
1885    #[cfg(unix)] // spawns a real shell
1886    #[test]
1887    fn completed_nonblocking_poll_is_level_triggered() -> Result<()> {
1888        let temp_dir = TempDir::new()?;
1889        let mut shell = PtyShell::new(temp_dir.path(), false)?;
1890
1891        shell.send_command("true")?;
1892        let deadline = Instant::now() + Duration::from_secs(2);
1893        while Instant::now() < deadline && !shell.poll_output_nonblocking() {
1894            std::thread::sleep(Duration::from_millis(10));
1895        }
1896
1897        assert!(!shell.command_running, "shell did not complete before the deadline");
1898        assert!(
1899            shell.poll_output_nonblocking(),
1900            "a later consumer must still observe the completed state"
1901        );
1902        Ok(())
1903    }
1904
1905    #[cfg(unix)] // spawns a real shell
1906    #[test]
1907    fn test_pty_shell_pwd() -> Result<()> {
1908        let temp_dir = TempDir::new()?;
1909        let mut shell = PtyShell::new(temp_dir.path(), false)?;
1910
1911        // Simply verify shell responds to pwd command
1912        // Use single quotes like echo test for consistency
1913        shell.send_command("pwd && echo 'pwd_done'")?;
1914        let (output, _complete) = shell.read_output(2.0)?;
1915
1916        // Verify the echo marker appears (proves command executed)
1917        assert!(output.contains("pwd_done"), "Output should contain 'pwd_done': {output}");
1918        Ok(())
1919    }
1920
1921    #[cfg(unix)] // spawns a real shell
1922    #[test]
1923    fn test_pty_resize() -> Result<()> {
1924        let temp_dir = TempDir::new()?;
1925        let mut shell = PtyShell::new(temp_dir.path(), false)?;
1926
1927        let result = shell.resize(120, 40);
1928        assert!(result.is_ok());
1929
1930        let (cols, rows) = shell.get_size();
1931        assert_eq!(cols, 120);
1932        assert_eq!(rows, 40);
1933        Ok(())
1934    }
1935
1936    /// `kill(pid, 0)` probes existence without sending a signal: 0 = the pid
1937    /// exists, -1 (ESRCH) = it's gone. A freshly-killed orphan lingers briefly
1938    /// as a zombie under init until reaped, so callers poll.
1939    #[cfg(unix)]
1940    fn pid_alive(pid: i32) -> bool {
1941        u32::try_from(pid).is_ok_and(crate::os::unix::process_exists)
1942    }
1943
1944    #[cfg(unix)]
1945    #[test]
1946    fn dropping_shell_kills_orphaned_background_child() -> Result<()> {
1947        // A background job the shell starts must not outlive the shell. Spawn a
1948        // long `sleep` in the background, record its pid, drop the shell, and
1949        // confirm the sleep is gone - proving the group-kill reached past the
1950        // shell to a child `Child::kill` alone would have orphaned.
1951        let dir = TempDir::new()?;
1952        let pidfile = dir.path().join("child.pid");
1953        let mut shell = PtyShell::new(dir.path(), false)?;
1954
1955        shell
1956            .send_command(&format!("sleep 300 & echo $! > {} ; echo spawned", pidfile.display()))?;
1957
1958        // The PTY echoes the input line, so `out` containing "spawned" only proves
1959        // the line was sent - not that bash ran it. Under CI load bash can lag, so
1960        // poll the pidfile (it only appears once `echo $! > pidfile` executes) while
1961        // draining the PTY, instead of trusting the echo and racing the redirect.
1962        let mut child_pid = None;
1963        for _ in 0..150 {
1964            let _ = shell.read_output(0.1);
1965            if let Some(pid) =
1966                std::fs::read_to_string(&pidfile).ok().and_then(|c| c.trim().parse::<i32>().ok())
1967            {
1968                child_pid = Some(pid);
1969                break;
1970            }
1971            thread::sleep(Duration::from_millis(20));
1972        }
1973        let child_pid =
1974            child_pid.ok_or_else(|| anyhow!("background child pid never materialized"))?;
1975        assert!(pid_alive(child_pid), "background child should be running before drop");
1976
1977        drop(shell);
1978
1979        // The group-kill plus init reaping the orphan are async w.r.t. us; poll.
1980        let mut gone = false;
1981        for _ in 0..150 {
1982            if !pid_alive(child_pid) {
1983                gone = true;
1984                break;
1985            }
1986            thread::sleep(Duration::from_millis(20));
1987        }
1988        assert!(gone, "background child {child_pid} survived shell teardown (orphan leak)");
1989        Ok(())
1990    }
1991}