Skip to main content

winx_code_agent/tools/
bash_command.rs

1//! Implementation of the `BashCommand` tool with WCGW parity.
2//!
3//! This module provides the implementation for the `BashCommand` tool, which is used
4//! to execute shell commands, check command status, and interact with the shell.
5//! Matches the behavior of wcgw Python implementation 1:1.
6
7use anyhow::Context as AnyhowContext;
8use rand::RngExt;
9use regex::Regex;
10use std::collections::HashMap;
11use std::fmt::Write as FmtWrite;
12use std::path::{Path, PathBuf};
13use std::sync::{Arc, Mutex as StdMutex};
14use std::time::{Duration, Instant};
15use tokio::sync::Mutex;
16use tokio::time::sleep;
17use tracing::{debug, error, info, warn};
18
19use crate::errors::{Result, WinxError};
20use crate::state::bash_state::BashState;
21use crate::state::live_terminal::ScreenUpdate;
22use crate::state::pty::PtyShell;
23use crate::state::terminal::{render_terminal_output, strip_ansi_codes};
24use crate::types::{normalize_thread_id, BashCommand, BashCommandAction, SpecialKey};
25
26type SharedPtyShell = Arc<Mutex<Option<PtyShell>>>;
27
28// ==================== WCGW-Style Constants ====================
29
30/// Default timeout for command execution (seconds) - matches WCGW Python Config.timeout
31const DEFAULT_TIMEOUT: f64 = 5.0;
32
33/// Extended timeout while output is still being produced - matches WCGW Python `Config.timeout_while_output`
34const TIMEOUT_WHILE_OUTPUT: f64 = 20.0;
35
36/// Number of iterations to wait without new output before giving up - matches WCGW Python `Config.output_wait_patience`
37const OUTPUT_WAIT_PATIENCE: i32 = 3;
38
39/// Polling slice for adaptive output reads. We read in chunks this long and
40/// return as soon as the prompt returns, instead of sleeping the full budget.
41const POLL_SLICE_SECS: f64 = 0.5;
42
43/// Chunk size for sending commands (characters) - matches WCGW Python (64 chars)
44const COMMAND_CHUNK_SIZE: usize = 64;
45
46/// Chunk size for sending text input (characters) - matches WCGW Python (128 chars)
47const TEXT_CHUNK_SIZE: usize = 128;
48
49/// Cheap byte-level safety net. We never even consider token counting if the
50/// raw payload is smaller than this — tokenizing is fast but not free, and
51/// the vast majority of responses are tiny status updates.
52const MAX_OUTPUT_LENGTH: usize = 100_000;
53
54/// Token budget reserved for a single PTY response when token-aware truncation
55/// kicks in. Picked to leave plenty of room for the surrounding context — most
56/// frontier models have 128k+ windows, so 25k for one shell payload is generous
57/// without monopolizing the conversation.
58const MAX_OUTPUT_TOKENS: usize = 25_000;
59
60/// Delay between typing text and the submitting Enter. Ink-based TUIs (Claude
61/// Code) collapse a burst of bytes into a single render tick and drop a CR that
62/// arrives glued to the text — the input box updates but never submits. Sending
63/// the Enter as a separate write after this short pause lets the TUI commit the
64/// typed text first, so `submit: true` reliably submits. ~40ms is imperceptible
65/// yet enough for the event loop to flush; harmless for plain shells.
66const SUBMIT_NUDGE_DELAY: Duration = Duration::from_millis(40);
67
68/// `screen` diff mode emits a per-line delta only when at most this many lines
69/// changed; beyond that the full frame is cheaper to read than a giant diff.
70const SCREEN_DIFF_THRESHOLD: usize = 10;
71
72/// Tail of `text` at most `max_len` bytes long, snapped up to a char boundary so
73/// we never slice through a multibyte UTF-8 sequence (which would panic).
74fn char_safe_tail(text: &str, max_len: usize) -> &str {
75    if text.len() <= max_len {
76        return text;
77    }
78    let mut start = text.len() - max_len;
79    while start < text.len() && !text.is_char_boundary(start) {
80        start += 1;
81    }
82    &text[start..]
83}
84
85/// Truncate `text` so its Claude token count stays under `max_tokens`.
86///
87/// We tokenize the tail of the string only when the raw byte length already
88/// exceeds the byte cap; otherwise we trust the byte budget and return as-is.
89/// When the tail still overshoots, we keep the last `max_tokens - reserve`
90/// tokens and prepend a "(...truncated)" marker — exactly what wcgw does in
91/// `_incremental_text`.
92fn truncate_to_token_budget(text: &str, max_tokens: usize) -> std::borrow::Cow<'_, str> {
93    if text.len() <= MAX_OUTPUT_LENGTH {
94        return std::borrow::Cow::Borrowed(text);
95    }
96
97    let Some(tokens) = crate::utils::encoder::encode_ids(text) else {
98        // Fallback to the byte-based truncation we used before the tokenizer.
99        return std::borrow::Cow::Owned(format!(
100            "(...truncated)\n{}",
101            char_safe_tail(text, MAX_OUTPUT_LENGTH)
102        ));
103    };
104
105    if tokens.len() <= max_tokens {
106        return std::borrow::Cow::Borrowed(text);
107    }
108
109    // Reserve one token slot for the marker overhead.
110    let keep = max_tokens.saturating_sub(1);
111    let tail = &tokens[tokens.len() - keep..];
112    let decoded = crate::utils::encoder::decode_ids(tail).unwrap_or_else(|| {
113        // Tokenizer present but decode failed: fall back to a byte tail.
114        char_safe_tail(text, MAX_OUTPUT_LENGTH).to_string()
115    });
116    std::borrow::Cow::Owned(format!("(...truncated)\n{decoded}"))
117}
118
119/// Message when a command is already running - matches WCGW Python `WAITING_INPUT_MESSAGE`
120const WAITING_INPUT_MESSAGE: &str = "A command is already running. NOTE: You can't run multiple shell commands in main shell, likely a previous program hasn't exited.
1211. Get its output using status check.
1222. Use `send_ascii` or `send_specials` to give inputs to the running program OR
1233. kill the previous program by sending ctrl+c first using `send_ascii` or `send_specials`
1244. Interrupt and run the process in background
125";
126
127// ==================== Background Shell Manager ====================
128
129/// Snapshot of a background shell that has exited but whose final output has not
130/// yet been consumed by the caller. We keep it around so the next call (typically
131/// a `status_check`) can return the trailing output before the entry is gone.
132#[derive(Debug, Clone)]
133pub struct ExitedShellInfo {
134    pub last_command: String,
135    pub final_output: String,
136    pub exited_at: Instant,
137}
138
139/// Manages background shell sessions - matches WCGW Python's `background_shells` dict
140#[derive(Debug, Default)]
141pub struct BackgroundShellManager {
142    shells: HashMap<String, SharedPtyShell>,
143    /// Recently exited shells that still owe their final output to the caller.
144    /// Entries are consumed the first time the caller queries the id, then dropped.
145    tombstones: HashMap<String, ExitedShellInfo>,
146}
147
148impl BackgroundShellManager {
149    /// Tombstones older than this are garbage-collected on the next prune pass.
150    const TOMBSTONE_TTL: Duration = Duration::from_secs(300);
151
152    /// Create a new background shell manager
153    pub fn new() -> Self {
154        Self { shells: HashMap::new(), tombstones: HashMap::new() }
155    }
156
157    /// Start a new background shell and return its command ID
158    pub fn start_new_shell(&mut self, working_dir: &Path, restricted_mode: bool) -> Result<String> {
159        let cid = format!("{:010x}", rand::rng().random::<u32>());
160
161        let shell = PtyShell::new(working_dir, restricted_mode).map_err(|e| {
162            WinxError::CommandExecutionError(format!("Failed to start background shell: {e}"))
163        })?;
164
165        self.shells.insert(cid.clone(), Arc::new(Mutex::new(Some(shell))));
166
167        info!("Started background shell with id: {}", cid);
168        Ok(cid)
169    }
170
171    /// Get a background shell by its command ID
172    pub fn get_shell(&self, bg_command_id: &str) -> Option<SharedPtyShell> {
173        self.shells.get(bg_command_id).cloned()
174    }
175
176    /// Remove and cleanup a background shell
177    pub fn remove_shell(&mut self, bg_command_id: &str) -> bool {
178        if let Some(shell_arc) = self.shells.remove(bg_command_id) {
179            if let Ok(mut guard) = shell_arc.try_lock() {
180                *guard = None;
181            }
182            info!("Removed background shell: {}", bg_command_id);
183            true
184        } else {
185            false
186        }
187    }
188
189    fn prune_finished_shells(&mut self) {
190        // GC old tombstones first.
191        let now = Instant::now();
192        self.tombstones.retain(|_, info| now.duration_since(info.exited_at) < Self::TOMBSTONE_TTL);
193
194        let mut finished: Vec<(String, Option<ExitedShellInfo>)> = Vec::new();
195
196        for (id, shell_arc) in &self.shells {
197            let Ok(mut guard) = shell_arc.try_lock() else {
198                continue;
199            };
200
201            let Some(shell) = guard.as_mut() else {
202                finished.push((id.clone(), None));
203                continue;
204            };
205
206            if !shell.is_alive() {
207                let tombstone = ExitedShellInfo {
208                    last_command: shell.last_command.clone(),
209                    final_output: std::mem::take(&mut shell.output_buffer),
210                    exited_at: now,
211                };
212                finished.push((id.clone(), Some(tombstone)));
213                continue;
214            }
215
216            // Never prune shells that haven't received a command yet.
217            // The global BG_SHELL_MANAGER is shared across parallel tests; a freshly
218            // spawned shell would otherwise be evicted between start_new_shell and
219            // the first send_command, leading to "Failed to get background shell".
220            if shell.last_command.is_empty() {
221                continue;
222            }
223
224            if shell.command_running {
225                // Non-blocking: draining here must NOT hold the global manager
226                // lock across a 100ms blocking read (that serializes every other
227                // background-shell op behind a single slow PTY).
228                shell.poll_output_nonblocking();
229            }
230
231            if !shell.command_running {
232                let tombstone = ExitedShellInfo {
233                    last_command: shell.last_command.clone(),
234                    final_output: std::mem::take(&mut shell.output_buffer),
235                    exited_at: now,
236                };
237                finished.push((id.clone(), Some(tombstone)));
238            }
239        }
240
241        for (id, tombstone) in finished {
242            self.remove_shell(&id);
243            if let Some(info) = tombstone {
244                self.tombstones.insert(id, info);
245            }
246        }
247    }
248
249    /// Look up the tombstone for a recently-exited shell, if any.
250    ///
251    /// The entry stays in the map until the TTL expires (see
252    /// `prune_finished_shells`), so repeated `status_check` calls on the same
253    /// `bg_command_id` keep returning the cached final output instead of
254    /// flipping to "shell not found" after the first read.
255    pub fn peek_tombstone(&self, bg_command_id: &str) -> Option<ExitedShellInfo> {
256        self.tombstones.get(bg_command_id).cloned()
257    }
258
259    /// Get info about all running background shells - matches WCGW Python `get_bg_running_commandsinfo`
260    pub fn get_running_info(&mut self) -> String {
261        self.prune_finished_shells();
262
263        if self.shells.is_empty() {
264            return "No command running in background.\n".to_string();
265        }
266
267        let mut running = Vec::new();
268        for (id, shell_arc) in &self.shells {
269            if let Ok(guard) = shell_arc.try_lock() {
270                if let Some(bash) = guard.as_ref() {
271                    if bash.command_running {
272                        running
273                            .push(format!("Command: {}, bg_command_id: {}", bash.last_command, id));
274                    }
275                }
276            } else {
277                running.push(format!("Command: <busy>, bg_command_id: {id}"));
278            }
279        }
280
281        if running.is_empty() {
282            "No command running in background.\n".to_string()
283        } else {
284            format!("Following background commands are attached:\n{}\n", running.join("\n"))
285        }
286    }
287}
288
289// Global background shell manager (thread-safe) - matches WCGW Python's BashState.background_shells
290lazy_static::lazy_static! {
291    static ref BG_SHELL_MANAGER: StdMutex<BackgroundShellManager> = StdMutex::new(BackgroundShellManager::new());
292}
293
294/// Lock the global background-shell manager, recovering from poisoning.
295///
296/// A panic while holding this lock (e.g. in the rendering path during a prune)
297/// must NOT permanently brick all background-shell functionality for the rest of
298/// the server's lifetime. The manager's data stays consistent across a panic, so
299/// recovering the inner guard is safe.
300fn lock_bg_manager() -> std::sync::MutexGuard<'static, BackgroundShellManager> {
301    BG_SHELL_MANAGER.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
302}
303
304// ==================== WCGW-Style Helper Functions ====================
305
306/// Get WCGW-style status string - matches WCGW Python's `get_status()`
307fn get_status(
308    bash_state: &BashState,
309    is_bg: bool,
310    bg_id: Option<&str>,
311    is_running: bool,
312    running_for: Option<&str>,
313) -> String {
314    let mut status = "\n\n---\n\n".to_string();
315
316    if is_bg {
317        if let Some(id) = bg_id {
318            let _ = writeln!(status, "bg_command_id = {id}");
319        }
320    }
321
322    if is_running {
323        status.push_str("status = still running\n");
324        if let Some(duration) = running_for {
325            let _ = writeln!(status, "running for = {duration}");
326        }
327    } else {
328        status.push_str("status = process exited\n");
329    }
330
331    let _ = writeln!(status, "cwd = {}", bash_state.cwd.display());
332
333    if !is_bg {
334        // Add background shell info for main shell - matches WCGW Python
335        {
336            let mut manager = lock_bg_manager();
337            status.push_str("This is the main shell. ");
338            status.push_str(&manager.get_running_info());
339        }
340    }
341
342    status.trim_end().to_string()
343}
344
345/// Process output with WCGW-style incremental text handling - matches WCGW Python _`incremental_text`
346fn wcgw_incremental_text(text: &str, last_pending_output: &str) -> String {
347    let truncated = truncate_to_token_budget(text, MAX_OUTPUT_TOKENS);
348    let text = truncated.as_ref();
349
350    if last_pending_output.is_empty() {
351        let rendered = render_terminal_output(text);
352        return rstrip_lines(&rendered).trim_start().to_string();
353    }
354
355    let last_rendered = render_terminal_output(last_pending_output);
356    if last_rendered.is_empty() {
357        return rstrip_lines(&render_terminal_output(text));
358    }
359
360    // Get text after last pending output. Snap the offset down to a char
361    // boundary: `last_pending_output.len()` is a byte count and may land inside
362    // a multibyte code point of `text`, which would panic on the slice.
363    let text_after_last = if text.len() > last_pending_output.len() {
364        let cut = crate::utils::floor_char_boundary(text, last_pending_output.len());
365        &text[cut..]
366    } else {
367        text
368    };
369
370    let combined = format!("{}\n{}", last_rendered.join("\n"), text_after_last);
371    let new_rendered = render_terminal_output(&combined);
372
373    // Get incremental part - matches WCGW Python get_incremental_output
374    let incremental = get_incremental_output(&last_rendered, &new_rendered);
375    rstrip_lines(&incremental)
376}
377
378fn extract_prompt_cwd(output: &str) -> Option<PathBuf> {
379    static PROMPT_RE: std::sync::OnceLock<Option<Regex>> = std::sync::OnceLock::new();
380    let prompt_regex =
381        PROMPT_RE.get_or_init(|| Regex::new(r"◉ (?P<cwd>[^\r\n]*?)──➤").ok()).as_ref()?;
382    let stripped = strip_ansi_codes(output);
383
384    prompt_regex
385        .captures_iter(&stripped)
386        .filter_map(|captures| captures.name("cwd").map(|cwd| cwd.as_str().trim()))
387        .filter(|cwd| !cwd.is_empty())
388        .last()
389        .map(PathBuf::from)
390}
391
392/// Right-strip each line and join - matches WCGW Python rstrip
393fn rstrip_lines(lines: &[String]) -> String {
394    lines.iter().map(|line| line.trim_end()).collect::<Vec<_>>().join("\n")
395}
396
397/// Get incremental output between old and new - matches WCGW Python `get_incremental_output`
398fn get_incremental_output(old_output: &[String], new_output: &[String]) -> Vec<String> {
399    if old_output.is_empty() {
400        return new_output.to_vec();
401    }
402
403    let nold = old_output.len();
404    let nnew = new_output.len();
405
406    // Find where old output ends in new output
407    for i in (0..nnew).rev() {
408        if new_output[i] != old_output[nold - 1] {
409            continue;
410        }
411
412        let mut matched = true;
413        for j in (0..i).rev() {
414            let old_idx = (nold as i64 - 1 + j as i64 - i as i64) as isize;
415            if old_idx < 0 {
416                break;
417            }
418            if new_output[j] != old_output[old_idx as usize] {
419                matched = false;
420                break;
421            }
422        }
423
424        if matched {
425            return new_output[i + 1..].to_vec();
426        }
427    }
428
429    new_output.to_vec()
430}
431
432fn send_utf8_in_byte_chunks(shell: &mut PtyShell, text: &str, chunk_size: usize) -> Result<()> {
433    let mut start = 0;
434
435    while start < text.len() {
436        let mut end = (start + chunk_size).min(text.len());
437        while !text.is_char_boundary(end) {
438            end -= 1;
439        }
440        if end == start {
441            end = text[start..].char_indices().nth(1).map_or(text.len(), |(idx, _)| start + idx);
442        }
443
444        shell.send_text(&text[start..end]).map_err(|e| {
445            WinxError::CommandExecutionError(format!("Failed to write PTY input: {e}"))
446        })?;
447        start = end;
448    }
449
450    Ok(())
451}
452
453/// Send the submitting Enter as a separate, slightly-delayed write so an
454/// Ink-based TUI doesn't swallow a CR glued to the preceding input. Briefly
455/// drops the shell lock during the pause so the reader thread keeps draining.
456/// See [`SUBMIT_NUDGE_DELAY`].
457async fn submit_enter(shell_arc: &SharedPtyShell) -> Result<()> {
458    tokio::time::sleep(SUBMIT_NUDGE_DELAY).await;
459    let mut guard = shell_arc.lock().await;
460    let bash = guard.as_mut().ok_or(WinxError::BashStateNotInitialized)?;
461    bash.send_special_key("Enter")
462        .map_err(|e| WinxError::CommandExecutionError(format!("Failed to submit: {e}")))?;
463    Ok(())
464}
465
466// ==================== Main Tool Handler ====================
467
468/// Handles the `BashCommand` tool call with WCGW parity
469///
470/// This function processes the `BashCommand` tool call following WCGW Python's
471/// `execute_bash()` function behavior exactly.
472#[tracing::instrument(level = "info", skip(bash_state_arc, bash_command))]
473pub async fn handle_tool_call(
474    bash_state_arc: &Arc<Mutex<Option<BashState>>>,
475    bash_command: BashCommand,
476) -> Result<String> {
477    info!("BashCommand tool called with: {:?}", bash_command);
478
479    let thread_id = normalize_thread_id(&bash_command.thread_id);
480
481    // Check if thread_id is empty
482    if thread_id.is_empty() {
483        error!("Empty thread_id provided in BashCommand");
484        return Err(WinxError::ThreadIdMismatch(
485            "Error: No saved bash state found for thread ID \"\". Please initialize first with this ID.".to_string()
486        ));
487    }
488
489    // Extract bash_state data
490    let mut bash_state: BashState;
491    {
492        let bash_state_guard = bash_state_arc.lock().await;
493
494        let Some(state) = &*bash_state_guard else {
495            error!("BashState not initialized");
496            return Err(WinxError::BashStateNotInitialized);
497        };
498
499        bash_state = state.clone();
500    }
501
502    // Verify thread ID matches - matches WCGW Python thread_id check
503    if thread_id != bash_state.current_thread_id {
504        // Try to load state from thread_id - matches WCGW Python load_state_from_thread_id
505        if !bash_state.load_state_from_disk(&thread_id).unwrap_or(false) {
506            return Err(WinxError::ThreadIdMismatch(format!(
507                "Error: No saved bash state found for thread_id `{thread_id}`. Please initialize first with this ID."
508            )));
509        }
510    }
511
512    // Calculate effective timeout - matches WCGW Python
513    // SECURITY: Ensure timeout is not negative to prevent unexpected behavior
514    let timeout_s = bash_command
515        .wait_for_seconds
516        .map_or(DEFAULT_TIMEOUT, |t| f64::from(t).max(0.0))
517        .min(TIMEOUT_WHILE_OUTPUT);
518
519    // Execute the action based on type - matches WCGW Python's _execute_bash()
520    let result = execute_bash_action(&mut bash_state, &bash_command.action_json, timeout_s).await;
521
522    {
523        let mut bash_state_guard = bash_state_arc.lock().await;
524        if let Some(state) = bash_state_guard.as_mut() {
525            state.cwd.clone_from(&bash_state.cwd);
526        }
527    }
528
529    // Remove echo if it's a command - matches WCGW Python
530    match result {
531        Ok(mut output) => {
532            if let BashCommandAction::Command { ref command, .. } = bash_command.action_json {
533                let cmd_trimmed = command.trim();
534                if output.starts_with(cmd_trimmed) {
535                    output = output[cmd_trimmed.len()..].to_string();
536                }
537            }
538            Ok(output)
539        }
540        Err(e) => Err(e),
541    }
542}
543
544/// Execute a bash action - matches WCGW Python's _`execute_bash()` function
545#[allow(clippy::too_many_lines)]
546async fn execute_bash_action(
547    bash_state: &mut BashState,
548    action: &BashCommandAction,
549    timeout_s: f64,
550) -> Result<String> {
551    let mut is_bg = false;
552    let mut bg_id: Option<String> = None;
553
554    // Handle bg_command_id routing - matches WCGW Python
555    let bg_shell: Option<SharedPtyShell> = match action {
556        BashCommandAction::Command { .. } => None, // Commands don't use bg_command_id for routing
557        BashCommandAction::StatusCheck { bg_command_id, .. }
558        | BashCommandAction::SendText { bg_command_id, .. }
559        | BashCommandAction::SendSpecials { bg_command_id, .. }
560        | BashCommandAction::SendAscii { bg_command_id, .. }
561        | BashCommandAction::Screen { bg_command_id, .. }
562        | BashCommandAction::WaitForTurn { bg_command_id, .. } => {
563            if let Some(id) = bg_command_id {
564                let mut manager = BG_SHELL_MANAGER.lock().map_err(|e| {
565                    WinxError::BashStateLockError(format!("Failed to lock bg manager: {e}"))
566                })?;
567                manager.prune_finished_shells();
568
569                if let Some(shell) = manager.get_shell(id) {
570                    is_bg = true;
571                    bg_id = Some(id.clone());
572                    Some(shell)
573                } else if let Some(tombstone) = manager.peek_tombstone(id) {
574                    // Shell already exited. For a status check we can hand back the
575                    // final cached output exactly once. For anything else (send_text,
576                    // send_specials, send_ascii) tell the caller the shell is gone
577                    // and include the captured output so they can recover state.
578                    drop(manager);
579                    return finalize_tombstone(&bash_state.cwd, id, tombstone, action);
580                } else {
581                    // Error message matches WCGW Python
582                    let error = format!(
583                        "No shell found running with command id {}.\n{}",
584                        id,
585                        manager.get_running_info()
586                    );
587                    return Err(WinxError::CommandExecutionError(error));
588                }
589            } else {
590                None
591            }
592        }
593    };
594
595    // Process based on action type - matches WCGW Python _execute_bash dispatch
596    match action {
597        BashCommandAction::Command { command, is_background, allow_multi } => {
598            execute_command(bash_state, command, *is_background, *allow_multi, timeout_s).await
599        }
600        BashCommandAction::StatusCheck { scrollback_lines, verbose, .. } => {
601            execute_status_check(
602                bash_state,
603                bg_shell,
604                is_bg,
605                bg_id.as_deref(),
606                timeout_s,
607                *scrollback_lines,
608                *verbose,
609            )
610            .await
611        }
612        BashCommandAction::SendText { send_text, submit, .. } => {
613            execute_send_text(
614                bash_state,
615                send_text,
616                *submit,
617                bg_shell,
618                is_bg,
619                bg_id.as_deref(),
620                timeout_s,
621            )
622            .await
623        }
624        BashCommandAction::SendSpecials { send_specials, submit, .. } => {
625            execute_send_specials(
626                bash_state,
627                send_specials,
628                *submit,
629                bg_shell,
630                is_bg,
631                bg_id.as_deref(),
632                timeout_s,
633            )
634            .await
635        }
636        BashCommandAction::SendAscii { send_ascii, submit, .. } => {
637            execute_send_ascii(
638                bash_state,
639                send_ascii,
640                *submit,
641                bg_shell,
642                is_bg,
643                bg_id.as_deref(),
644                timeout_s,
645            )
646            .await
647        }
648        BashCommandAction::Screen { lines, diff, .. } => {
649            execute_screen(bash_state, bg_shell, is_bg, bg_id.as_deref(), *lines, *diff).await
650        }
651        BashCommandAction::WaitForTurn { recognizer, quiet_ms, timeout_seconds, lines, .. } => {
652            execute_wait_for_turn(
653                bash_state,
654                bg_shell,
655                is_bg,
656                bg_id.as_deref(),
657                recognizer.as_deref(),
658                *quiet_ms,
659                *timeout_seconds,
660                *lines,
661            )
662            .await
663        }
664    }
665}
666
667/// Strip a trailing `| tail ...` from a command (wcgw parity, `strip_tail_pipe`).
668///
669/// LLMs habitually pipe output through `tail`, but we already truncate output
670/// server-side — stripping the pipe avoids hiding the earlier output the model
671/// usually wants. Only a `tail` at the very end of the command is removed.
672///
673/// This matches wcgw by default. Set `WINX_KEEP_TAIL_PIPE=1` to preserve the
674/// pipe instead (winx's original behavior), e.g. when you deliberately want only
675/// the tail of a huge log rather than the server-side truncation.
676fn strip_tail_pipe(command: &str) -> String {
677    strip_tail_pipe_impl(command, keep_tail_pipe())
678}
679
680/// Pure core of [`strip_tail_pipe`], split out so both modes are unit-testable
681/// without touching process-wide env vars (tests run concurrently).
682fn strip_tail_pipe_impl(command: &str, keep: bool) -> String {
683    static RE: std::sync::OnceLock<Option<regex::Regex>> = std::sync::OnceLock::new();
684    if keep {
685        return command.to_string();
686    }
687    let re = RE.get_or_init(|| regex::Regex::new(r"\|\s*tail(?:\s+(?:-n\s*|-)?(\d+))?\s*$").ok());
688    match re.as_ref().and_then(|re| re.find(command)) {
689        Some(matched) => command[..matched.start()].trim_end().to_string(),
690        None => command.to_string(),
691    }
692}
693
694/// Whether the user opted out of `| tail` stripping via `WINX_KEEP_TAIL_PIPE`.
695fn keep_tail_pipe() -> bool {
696    std::env::var("WINX_KEEP_TAIL_PIPE").is_ok_and(|value| {
697        let value = value.trim();
698        !value.is_empty() && value != "0" && !value.eq_ignore_ascii_case("false")
699    })
700}
701
702/// Execute a command - matches WCGW Python's Command handling in _`execute_bash`
703async fn execute_command(
704    bash_state: &mut BashState,
705    command: &str,
706    is_background: bool,
707    allow_multi: bool,
708    timeout_s: f64,
709) -> Result<String> {
710    // wcgw strips a trailing `| tail` before anything else (model_validator).
711    let stripped_command = strip_tail_pipe(command);
712    let command = stripped_command.as_str();
713    debug!("Processing Command action: {command:?} (allow_multi={allow_multi})");
714
715    // Check mode permissions - matches WCGW Python bash_command_mode check
716    if !bash_state.is_command_allowed(command) {
717        error!("Command '{}' not allowed in current mode", command);
718        return Err(WinxError::CommandNotAllowed(
719            "Error: BashCommand not allowed in current mode".to_string(),
720        ));
721    }
722
723    // Single-statement guard (wcgw parity). Callers can opt out via
724    // `allow_multi: true` when they knowingly want to chain commands
725    // without wrapping in `bash -lc '...'`.
726    let command = command.trim();
727    if !allow_multi {
728        crate::utils::bash_parser::assert_single_statement(command)?;
729    }
730
731    // If background execution requested, start new shell - matches WCGW Python is_background handling
732    if is_background {
733        return execute_in_background(bash_state, command, timeout_s).await;
734    }
735
736    // Check if a command is already running - matches WCGW Python state check
737    {
738        let bash_guard = bash_state.pty_shell.lock().await;
739
740        if let Some(ref bash) = *bash_guard {
741            if bash.command_running {
742                return Err(WinxError::CommandExecutionError(WAITING_INPUT_MESSAGE.to_string()));
743            }
744        }
745    }
746
747    // Initialize bash if needed
748    if bash_state.pty_shell.lock().await.is_none() {
749        bash_state
750            .init_pty_shell()
751            .await
752            .map_err(|e| WinxError::CommandExecutionError(format!("Failed to init bash: {e}")))?;
753    }
754
755    // Clear prompt before sending - matches WCGW Python clear_to_run.
756    // Drain any leftover output and, if the shell still looks busy, send
757    // Ctrl-C so the new command lands on a fresh prompt instead of being
758    // appended to whatever was hanging on stdin.
759    {
760        let needs_reset = {
761            let mut bash_guard = bash_state.pty_shell.lock().await;
762            match bash_guard.as_mut() {
763                Some(bash) => match bash.clear_to_run(DEFAULT_TIMEOUT as f32) {
764                    Ok(true) => false,
765                    Ok(false) => {
766                        warn!("clear_to_run: shell still busy after Ctrl-C, resetting it");
767                        true
768                    }
769                    Err(e) => {
770                        warn!("clear_to_run failed ({e}), resetting shell");
771                        true
772                    }
773                },
774                None => false,
775            }
776        };
777        // wcgw parity: a shell that won't return to a prompt even after Ctrl-C is
778        // recreated, so the new command lands on a fresh prompt instead of being
779        // appended to a hung shell. init_pty_shell rebuilds at the same cwd/mode.
780        if needs_reset {
781            if let Err(e) = bash_state.init_pty_shell().await {
782                warn!("Failed to reset shell after clear_to_run: {e}");
783            }
784        }
785    }
786
787    // Send command in chunks of 64 characters - matches WCGW Python exactly
788    {
789        let mut bash_guard = bash_state.pty_shell.lock().await;
790
791        let bash = bash_guard.as_mut().ok_or(WinxError::BashStateNotInitialized)?;
792
793        bash.output_buffer.clear();
794        bash.output_truncated = false;
795        // Send in chunks - matches WCGW Python: for i in range(0, len(command), 64)
796        send_utf8_in_byte_chunks(bash, command, COMMAND_CHUNK_SIZE)?;
797
798        // Send linesep to execute - matches WCGW Python bash_state.send(bash_state.linesep, ...)
799        bash.send_special_key("Enter").map_err(|e| {
800            WinxError::CommandExecutionError(format!("Failed to send newline: {e}"))
801        })?;
802
803        bash.last_command = command.to_string();
804        bash.command_running = true;
805    }
806
807    // Wait for output with WCGW-style patience handling
808    let shell_arc = bash_state.pty_shell.clone();
809    wait_for_output(bash_state, &shell_arc, timeout_s, false, None, false).await
810}
811
812/// Wait for command output with WCGW-style patience handling - matches WCGW Python expect/wait logic.
813///
814/// `shell_arc` selects which shell to read from (main shell or a bg shell handle).
815async fn wait_for_output(
816    bash_state: &mut BashState,
817    shell_arc: &SharedPtyShell,
818    timeout_s: f64,
819    is_bg: bool,
820    bg_id: Option<&str>,
821    is_status_check: bool,
822) -> Result<String> {
823    let start = Instant::now();
824    let wait = timeout_s.min(TIMEOUT_WHILE_OUTPUT);
825    let mut last_pending_output = String::new();
826    let mut complete = false;
827
828    // Adaptive polling instead of a blind sleep. wcgw sleeps the full `wait`
829    // budget before reading even once, so a `pwd` that finishes in 10ms still
830    // costs ~5s. Instead we read in short slices and return the moment the
831    // prompt comes back (`read_output` already exits early on prompt + drain),
832    // dropping fast-command latency from seconds to ~100ms. Long-running
833    // commands still consume the whole budget, since we loop until `complete`
834    // or `wait` elapses — identical upper-bound behavior, far snappier floor.
835    let mut output = String::new();
836    loop {
837        let elapsed = start.elapsed().as_secs_f64();
838        if elapsed >= wait {
839            break;
840        }
841        let slice = (wait - elapsed).clamp(0.1, POLL_SLICE_SECS);
842        let (out, done) = {
843            let mut bash_guard = shell_arc.lock().await;
844            match bash_guard.as_mut() {
845                Some(bash) => bash.read_output(slice as f32).map_err(|e| {
846                    WinxError::CommandExecutionError(format!("Failed to read output: {e}"))
847                })?,
848                None => (String::new(), true),
849            }
850        };
851        output = out;
852        complete = done;
853        if complete {
854            break;
855        }
856    }
857
858    // If not complete and this is a status check, use WCGW-style patience waiting.
859    //
860    // Treat `timeout_s` (== caller's `wait_for_seconds`, capped at
861    // `TIMEOUT_WHILE_OUTPUT`) as the hard upper bound on the TOTAL wall-clock
862    // spent inside this call. wcgw computes `remaining = TIMEOUT_WHILE_OUTPUT
863    // - wait`, which makes a 2-second `wait_for_seconds` block for almost 20s
864    // on a TUI that keeps emitting spinner frames. We diverge from wcgw here
865    // because driving agents expect their wait budget to be respected.
866    if !complete && is_status_check {
867        let budget_secs = timeout_s.min(TIMEOUT_WHILE_OUTPUT);
868        let iter_wait_secs = 0.5_f64;
869        let mut patience = OUTPUT_WAIT_PATIENCE;
870
871        let incremental = wcgw_incremental_text(&output, &last_pending_output);
872        if incremental.is_empty() {
873            patience -= 1;
874        }
875
876        let mut last_incremental = incremental;
877
878        while start.elapsed().as_secs_f64() < budget_secs && patience > 0 {
879            let remaining = (budget_secs - start.elapsed().as_secs_f64()).max(0.0);
880            if remaining < 0.1 {
881                break;
882            }
883            sleep(Duration::from_secs_f64(iter_wait_secs.min(remaining))).await;
884
885            let (new_output, done) = {
886                let mut bash_guard = shell_arc.lock().await;
887
888                if let Some(bash) = bash_guard.as_mut() {
889                    bash.read_output(0.5).map_err(|e| {
890                        WinxError::CommandExecutionError(format!("Failed to read output: {e}"))
891                    })?
892                } else {
893                    (String::new(), true)
894                }
895            };
896
897            if done {
898                complete = true;
899                output = new_output;
900                break;
901            }
902
903            // Check if output changed - matches WCGW Python patience logic
904            let new_incremental = wcgw_incremental_text(&new_output, &last_pending_output);
905            if new_incremental == last_incremental {
906                patience -= 1;
907            } else {
908                patience = OUTPUT_WAIT_PATIENCE; // Reset patience on new output
909            }
910            last_incremental = new_incremental;
911
912            output = new_output;
913        }
914
915        if !complete {
916            // Update pending output - matches WCGW Python bash_state.set_pending(text)
917            last_pending_output = output.clone();
918        }
919    }
920
921    if complete {
922        if let Some(cwd) = extract_prompt_cwd(&output) {
923            bash_state.cwd = cwd;
924        }
925    }
926
927    // Process output through terminal emulation - matches WCGW Python _incremental_text
928    let rendered = wcgw_incremental_text(&output, &last_pending_output);
929
930    // Conscious compression: collapse mechanical repetition (identical line runs,
931    // blank-line blocks) before truncating, to save tokens without dropping any
932    // unique context. Falls back to the raw text when nothing is safe to collapse.
933    let rendered = {
934        let before_lines = rendered.lines().count();
935        match crate::utils::output_compress::compress_output(&rendered) {
936            Some(compressed) => {
937                debug!(
938                    from_lines = before_lines,
939                    to_lines = compressed.lines().count(),
940                    "winx collapsed mechanical repetition in shell output"
941                );
942                compressed
943            }
944            None => rendered,
945        }
946    };
947
948    // Truncate if needed - matches WCGW Python token truncation
949    let rendered = truncate_to_token_budget(&rendered, MAX_OUTPUT_TOKENS).into_owned();
950
951    // Calculate running duration for status
952    let running_for =
953        if complete { None } else { Some(format!("{} seconds", start.elapsed().as_secs())) };
954
955    // Add status - matches WCGW Python get_status
956    let status = get_status(bash_state, is_bg, bg_id, !complete, running_for.as_deref());
957    Ok(format!("{rendered}{status}"))
958}
959
960/// Render the final cached output of an exited background shell.
961///
962/// `status_check` is allowed to "consume" the tombstone and return the trailing
963/// output exactly once. Send-style actions (`send_text`, `send_specials`,
964/// `send_ascii`) cannot interact with a dead shell, so we return an explicit
965/// error that still includes the captured output so the agent can recover state.
966fn finalize_tombstone(
967    cwd: &Path,
968    id: &str,
969    tombstone: ExitedShellInfo,
970    action: &BashCommandAction,
971) -> Result<String> {
972    let ExitedShellInfo { last_command, final_output, .. } = tombstone;
973    match action {
974        // A dead shell's turn is over: Screen/WaitForTurn hand back the final
975        // captured output as the snapshot, exactly like a status check.
976        BashCommandAction::StatusCheck { .. }
977        | BashCommandAction::Screen { .. }
978        | BashCommandAction::WaitForTurn { .. } => {
979            let rendered = wcgw_incremental_text(&final_output, "");
980            let rendered = truncate_to_token_budget(&rendered, MAX_OUTPUT_TOKENS).into_owned();
981            // Build a compact status block matching `get_status` for a finished bg shell.
982            let mut status = "\n\n---\n\n".to_string();
983            let _ = writeln!(status, "bg_command_id = {id}");
984            status.push_str("status = process exited\n");
985            let _ = writeln!(status, "cwd = {}", cwd.display());
986            Ok(format!("{rendered}{}", status.trim_end()))
987        }
988        BashCommandAction::SendText { .. }
989        | BashCommandAction::SendSpecials { .. }
990        | BashCommandAction::SendAscii { .. } => Err(WinxError::CommandExecutionError(format!(
991            "Background shell {id} already exited (last command: {last_command}).\nFinal captured output:\n{final_output}"
992        ))),
993        BashCommandAction::Command { .. } => {
994            // We only enter `finalize_tombstone` from the bg routing path, which
995            // never matches Command. Treat this as a programmer error.
996            unreachable!("finalize_tombstone called for non-bg action")
997        }
998    }
999}
1000
1001/// Execute a status check - matches WCGW Python's `StatusCheck` handling.
1002///
1003/// New behavior (v0.2.308):
1004/// - Deduplicates against the last response by fingerprint; when nothing
1005///   changed and `verbose=false`, returns a compact "no new output" payload
1006///   instead of resending the same screen.
1007/// - Optional `scrollback_lines` pulls bounded history from the `PtyShell`
1008///   ringbuffer so agents can reorient after a long pause.
1009async fn execute_status_check(
1010    bash_state: &mut BashState,
1011    bg_shell: Option<SharedPtyShell>,
1012    is_bg: bool,
1013    bg_id: Option<&str>,
1014    timeout_s: f64,
1015    scrollback_lines: Option<usize>,
1016    verbose: bool,
1017) -> Result<String> {
1018    debug!("Processing StatusCheck action (verbose={verbose}, scrollback={scrollback_lines:?})");
1019
1020    // Pick the shell we're going to inspect: bg shell when bg_command_id was provided,
1021    // otherwise fall back to the main interactive shell.
1022    let shell_arc = bg_shell.unwrap_or_else(|| bash_state.pty_shell.clone());
1023
1024    // Check if there's a running command - matches WCGW Python state check
1025    let is_running = {
1026        let guard = shell_arc.lock().await;
1027        if let Some(ref bash) = *guard {
1028            bash.command_running
1029        } else {
1030            false
1031        }
1032    };
1033
1034    // If no command running and not background, return error - matches WCGW Python
1035    if !is_running && !is_bg {
1036        let mut manager = lock_bg_manager();
1037        let error = format!(
1038            "No command is currently running, so there's nothing to check. The previous \
1039             command already finished and its output was returned when it completed. Start a \
1040             new command, or pass a bg_command_id if you launched one in the background.\n{}",
1041            manager.get_running_info()
1042        );
1043        return Err(WinxError::CommandExecutionError(error));
1044    }
1045
1046    // Read output with patience handling - this IS a status check
1047    let response = wait_for_output(bash_state, &shell_arc, timeout_s, is_bg, bg_id, true).await?;
1048
1049    // Inter-call dedup: hash only the response *body* (the chunk before the
1050    // `\n\n---\n` status footer). The footer contains a live "running for"
1051    // counter that would otherwise defeat the comparison.
1052    let body = response.split("\n\n---\n").next().unwrap_or(&response);
1053    if !verbose && scrollback_lines.is_none() {
1054        let mut guard = shell_arc.lock().await;
1055        if let Some(bash) = guard.as_mut() {
1056            let fingerprint = PtyShell::fingerprint(body);
1057            if Some(fingerprint) == bash.last_returned_hash {
1058                let status = get_status(bash_state, is_bg, bg_id, is_running, None);
1059                return Ok(format!("no new output since last check{status}"));
1060            }
1061            bash.last_returned_hash = Some(fingerprint);
1062        }
1063    } else if !verbose {
1064        // Still record the hash so subsequent non-scrollback calls can dedup.
1065        let mut guard = shell_arc.lock().await;
1066        if let Some(bash) = guard.as_mut() {
1067            bash.last_returned_hash = Some(PtyShell::fingerprint(body));
1068        }
1069    }
1070
1071    // Optional scrollback prefix — only ever pulled when the caller asks for it.
1072    if let Some(lines) = scrollback_lines {
1073        if lines > 0 {
1074            let scrollback = {
1075                let guard = shell_arc.lock().await;
1076                guard.as_ref().map(|s| s.collect_scrollback(lines)).unwrap_or_default()
1077            };
1078            if !scrollback.is_empty() {
1079                let count = scrollback.lines().count();
1080                return Ok(format!(
1081                    "--- scrollback ({count} lines) ---\n{scrollback}\n--- latest ---\n{response}"
1082                ));
1083            }
1084        }
1085    }
1086
1087    Ok(response)
1088}
1089
1090/// Execute `Screen` — a stable, point-in-time snapshot of a shell's live
1091/// terminal screen (consolidated grid, ANSI stripped). No waiting, no dedup;
1092/// the foundation for reading an interactive TUI's current frame.
1093async fn execute_screen(
1094    bash_state: &BashState,
1095    bg_shell: Option<SharedPtyShell>,
1096    is_bg: bool,
1097    bg_id: Option<&str>,
1098    lines: Option<usize>,
1099    diff: bool,
1100) -> Result<String> {
1101    let shell_arc = bg_shell.unwrap_or_else(|| bash_state.pty_shell.clone());
1102    let max_lines = lines.unwrap_or(0);
1103
1104    if diff {
1105        let (update, is_running, in_alt, cursor) = {
1106            let guard = shell_arc.lock().await;
1107            match guard.as_ref() {
1108                Some(bash) => (
1109                    bash.live_snapshot_diff(max_lines, SCREEN_DIFF_THRESHOLD),
1110                    bash.command_running,
1111                    bash.live_in_alt_screen(),
1112                    bash.live_cursor_position(),
1113                ),
1114                None => (ScreenUpdate::Full(Vec::new()), false, false, (0, 0)),
1115            }
1116        };
1117        let (crow, ccol) = cursor;
1118        let alt = if in_alt { " [alt-screen]" } else { "" };
1119        let status = get_status(bash_state, is_bg, bg_id, is_running, None);
1120        let body = match update {
1121            ScreenUpdate::Unchanged => "(no change since last screen)".to_string(),
1122            ScreenUpdate::Diff(changed) => {
1123                let mut out = String::from("(changed lines only)\n");
1124                for (row, content) in changed {
1125                    let _ = writeln!(out, "{row:>4}: {content}");
1126                }
1127                out
1128            }
1129            ScreenUpdate::Full(snap) => {
1130                let joined = snap.join("\n");
1131                if joined.trim().is_empty() {
1132                    "(screen is empty)".to_string()
1133                } else {
1134                    truncate_to_token_budget(&joined, MAX_OUTPUT_TOKENS).into_owned()
1135                }
1136            }
1137        };
1138        return Ok(format!(
1139            "--- live screen{alt} [cursor row={crow} col={ccol}] (diff) ---\n{body}{status}"
1140        ));
1141    }
1142
1143    let (snapshot, is_running, in_alt, cursor) = {
1144        let guard = shell_arc.lock().await;
1145        match guard.as_ref() {
1146            Some(bash) => (
1147                bash.live_snapshot(max_lines),
1148                bash.command_running,
1149                bash.live_in_alt_screen(),
1150                bash.live_cursor_position(),
1151            ),
1152            None => (Vec::new(), false, false, (0, 0)),
1153        }
1154    };
1155
1156    let joined = snapshot.join("\n");
1157    let body = if joined.trim().is_empty() {
1158        "(screen is empty)".to_string()
1159    } else {
1160        truncate_to_token_budget(&joined, MAX_OUTPUT_TOKENS).into_owned()
1161    };
1162    let alt = if in_alt { " [alt-screen]" } else { "" };
1163    let (crow, ccol) = cursor;
1164    let status = get_status(bash_state, is_bg, bg_id, is_running, None);
1165    Ok(format!("--- live screen{alt} [cursor row={crow} col={ccol}] ---\n{body}{status}"))
1166}
1167
1168/// Execute `WaitForTurn` — block until an interactive TUI finishes its turn.
1169///
1170/// Polls the live screen, combining a per-app recognizer (claude/codex/auto)
1171/// with a generic quiescence window: the turn is "ready" when the recognizer
1172/// reports awaiting-input/approval (after a short settle) or, for an unknown
1173/// TUI, when the screen simply stops changing for `quiet`. Returns the stable
1174/// snapshot plus the detected state.
1175#[allow(clippy::too_many_arguments)]
1176async fn execute_wait_for_turn(
1177    bash_state: &BashState,
1178    bg_shell: Option<SharedPtyShell>,
1179    is_bg: bool,
1180    bg_id: Option<&str>,
1181    recognizer_hint: Option<&str>,
1182    quiet_ms: Option<u64>,
1183    timeout_seconds: Option<f32>,
1184    lines: Option<usize>,
1185) -> Result<String> {
1186    use crate::state::turn::{recognizer_for, TurnState};
1187
1188    let shell_arc = bg_shell.unwrap_or_else(|| bash_state.pty_shell.clone());
1189    let recognizer = recognizer_for(recognizer_hint.unwrap_or("auto"));
1190    let quiet = Duration::from_millis(quiet_ms.unwrap_or(600).clamp(50, 10_000));
1191    let settle = quiet.min(Duration::from_millis(300));
1192    let hard_cap =
1193        Duration::from_secs_f64(f64::from(timeout_seconds.unwrap_or(30.0)).clamp(0.5, 600.0));
1194    let max_lines = lines.unwrap_or(0);
1195    let poll = Duration::from_millis(120);
1196    // Grace period so a freshly-prompted app has time to react before we'd
1197    // otherwise mistake the *pre-input* idle screen for a finished turn.
1198    let warmup = Duration::from_millis(2500);
1199
1200    let start = Instant::now();
1201    let mut last_hash: Option<u64> = None;
1202    let mut initial_hash: Option<u64> = None;
1203    let mut stable_since = Instant::now();
1204    let mut seen_busy = false;
1205
1206    loop {
1207        let (snapshot, in_alt, alive, running) = {
1208            let mut guard = shell_arc.lock().await;
1209            match guard.as_mut() {
1210                Some(bash) => (
1211                    bash.live_snapshot(max_lines),
1212                    bash.live_in_alt_screen(),
1213                    bash.is_alive(),
1214                    bash.command_running,
1215                ),
1216                None => (Vec::new(), false, false, false),
1217            }
1218        };
1219
1220        let hash = PtyShell::fingerprint(&snapshot.join("\n"));
1221        if initial_hash.is_none() {
1222            initial_hash = Some(hash);
1223        }
1224        if Some(hash) != last_hash {
1225            last_hash = Some(hash);
1226            stable_since = Instant::now();
1227        }
1228
1229        let state = recognizer.detect(&snapshot);
1230        if state == TurnState::Busy {
1231            seen_busy = true;
1232        }
1233        let stable_for = stable_since.elapsed();
1234        // Don't call it "done" until something actually happened since we began
1235        // waiting: the app went Busy, the screen changed from the first frame we
1236        // saw, or the warmup elapsed (instant reply / nothing to do).
1237        let activity = seen_busy || Some(hash) != initial_hash || start.elapsed() >= warmup;
1238        let ready = match state {
1239            TurnState::Busy => false,
1240            // A short settle is enough once we saw it go Busy; otherwise wait the
1241            // full quiet window so a slow first token isn't read as "done".
1242            TurnState::AwaitingInput | TurnState::AwaitingApproval => {
1243                activity && stable_for >= if seen_busy { settle } else { quiet }
1244            }
1245            TurnState::Unknown => activity && stable_for >= quiet,
1246        };
1247
1248        let timed_out = start.elapsed() >= hard_cap;
1249        if ready || !alive || timed_out {
1250            let reason = if !alive {
1251                "exited"
1252            } else if ready {
1253                "ready"
1254            } else {
1255                "timeout"
1256            };
1257            let joined = snapshot.join("\n");
1258            let body = if joined.trim().is_empty() {
1259                "(screen is empty)".to_string()
1260            } else {
1261                truncate_to_token_budget(&joined, MAX_OUTPUT_TOKENS).into_owned()
1262            };
1263            let alt = if in_alt { " [alt-screen]" } else { "" };
1264            let header = format!(
1265                "--- turn: {} ({}, recognizer={}, waited {:.1}s){} ---",
1266                state.as_str(),
1267                reason,
1268                recognizer.name(),
1269                start.elapsed().as_secs_f64(),
1270                alt
1271            );
1272            let status = get_status(bash_state, is_bg, bg_id, running, None);
1273            return Ok(format!("{header}\n{body}{status}"));
1274        }
1275
1276        tokio::time::sleep(poll).await;
1277    }
1278}
1279
1280/// Execute `send_text` - matches WCGW Python's `SendText` handling
1281async fn execute_send_text(
1282    bash_state: &mut BashState,
1283    text: &str,
1284    submit: bool,
1285    bg_shell: Option<SharedPtyShell>,
1286    is_bg: bool,
1287    bg_id: Option<&str>,
1288    timeout_s: f64,
1289) -> Result<String> {
1290    debug!("Processing SendText action: {text:?} (submit={submit})");
1291
1292    // Validate - matches WCGW Python
1293    if text.is_empty() {
1294        return Err(WinxError::CommandExecutionError(
1295            "Failure: send_text cannot be empty".to_string(),
1296        ));
1297    }
1298
1299    // Get the target shell
1300    let shell_arc = bg_shell.unwrap_or_else(|| bash_state.pty_shell.clone());
1301
1302    // Send text in chunks of 128 characters - matches WCGW Python exactly
1303    {
1304        let mut guard = shell_arc.lock().await;
1305
1306        let bash = guard.as_mut().ok_or(WinxError::BashStateNotInitialized)?;
1307
1308        // Send in chunks - matches WCGW Python: for i in range(0, len(command_data.send_text), 128)
1309        send_utf8_in_byte_chunks(bash, text, TEXT_CHUNK_SIZE)?;
1310    }
1311
1312    // Only submit when explicitly asked. The Enter goes as a separate, delayed
1313    // write so Ink TUIs (Claude Code) don't swallow a CR glued to the text — a
1314    // bare CR there is otherwise treated as a soft newline and never submits.
1315    if submit {
1316        submit_enter(&shell_arc).await?;
1317    }
1318
1319    // Wait for output
1320    wait_for_output(bash_state, &shell_arc, timeout_s, is_bg, bg_id, false).await
1321}
1322
1323/// Execute `send_specials` - matches WCGW Python's `SendSpecials` handling exactly
1324async fn execute_send_specials(
1325    bash_state: &mut BashState,
1326    keys: &[SpecialKey],
1327    submit: bool,
1328    bg_shell: Option<SharedPtyShell>,
1329    is_bg: bool,
1330    bg_id: Option<&str>,
1331    timeout_s: f64,
1332) -> Result<String> {
1333    debug!("Processing SendSpecials action: {keys:?} (submit={submit})");
1334
1335    // Validate - matches WCGW Python
1336    if keys.is_empty() {
1337        return Err(WinxError::CommandExecutionError(
1338            "Failure: send_specials cannot be empty".to_string(),
1339        ));
1340    }
1341
1342    let shell_arc = bg_shell.unwrap_or_else(|| bash_state.pty_shell.clone());
1343    let mut is_interrupt = false;
1344
1345    {
1346        let mut guard = shell_arc.lock().await;
1347
1348        let bash = guard.as_mut().ok_or(WinxError::BashStateNotInitialized)?;
1349
1350        // Send each special key - matches WCGW Python exactly
1351        for key in keys {
1352            match key {
1353                SpecialKey::KeyUp => {
1354                    // matches WCGW Python: bash_state.send("\033[A", ...)
1355                    bash.send_special_key("KeyUp").map_err(|e| {
1356                        WinxError::CommandExecutionError(format!("Failed to send KeyUp: {e}"))
1357                    })?;
1358                }
1359                SpecialKey::KeyDown => {
1360                    // matches WCGW Python: bash_state.send("\033[B", ...)
1361                    bash.send_special_key("KeyDown").map_err(|e| {
1362                        WinxError::CommandExecutionError(format!("Failed to send KeyDown: {e}"))
1363                    })?;
1364                }
1365                SpecialKey::KeyLeft => {
1366                    // matches WCGW Python: bash_state.send("\033[D", ...)
1367                    bash.send_special_key("KeyLeft").map_err(|e| {
1368                        WinxError::CommandExecutionError(format!("Failed to send KeyLeft: {e}"))
1369                    })?;
1370                }
1371                SpecialKey::KeyRight => {
1372                    // matches WCGW Python: bash_state.send("\033[C", ...)
1373                    bash.send_special_key("KeyRight").map_err(|e| {
1374                        WinxError::CommandExecutionError(format!("Failed to send KeyRight: {e}"))
1375                    })?;
1376                }
1377                SpecialKey::Enter => {
1378                    // matches WCGW Python: bash_state.send("\x0d", ...) - carriage return
1379                    bash.send_special_key("Enter").map_err(|e| {
1380                        WinxError::CommandExecutionError(format!("Failed to send Enter: {e}"))
1381                    })?;
1382                }
1383                SpecialKey::CtrlC => {
1384                    // matches WCGW Python: bash_state.sendintr()
1385                    bash.send_interrupt().map_err(|e| {
1386                        WinxError::CommandExecutionError(format!("Failed to send interrupt: {e}"))
1387                    })?;
1388                    is_interrupt = true;
1389                }
1390                SpecialKey::CtrlD => {
1391                    // matches WCGW Python: bash_state.sendintr() - same as Ctrl+C in WCGW
1392                    bash.send_eof().map_err(|e| {
1393                        WinxError::CommandExecutionError(format!("Failed to send Ctrl+D: {e}"))
1394                    })?;
1395                    is_interrupt = true;
1396                }
1397                SpecialKey::CtrlZ => {
1398                    // Ctrl+Z = SIGTSTP (suspend) - ASCII 0x1a
1399                    bash.send_suspend().map_err(|e| {
1400                        WinxError::CommandExecutionError(format!("Failed to send Ctrl+Z: {e}"))
1401                    })?;
1402                }
1403            }
1404        }
1405    }
1406
1407    // Submit as a separate, slightly-delayed write (see `submit_enter`) so an
1408    // Ink TUI doesn't drop the CR.
1409    if submit {
1410        submit_enter(&shell_arc).await?;
1411    }
1412
1413    // NOTE: wcgw treats a bare Enter as a status check and applies its
1414    // patience loop. We deliberately diverge: for a driving agent (e.g.,
1415    // pushing Enter to submit text in a TUI) the patience loop swallows the
1416    // immediate response. Callers that want patience semantics should use the
1417    // explicit `status_check` action instead.
1418
1419    // Wait for output
1420    let mut output =
1421        wait_for_output(bash_state, &shell_arc, timeout_s, is_bg, bg_id, false).await?;
1422
1423    // Add interrupt failure message if still running - matches WCGW Python exactly
1424    if is_interrupt && output.contains("status = still running") {
1425        output.push_str("\n---\n----\nFailure interrupting.\nYou may want to try Ctrl-c again or program specific exit interactive commands.\n");
1426    }
1427
1428    Ok(output)
1429}
1430
1431/// Execute `send_ascii` - matches WCGW Python's `SendAscii` handling
1432async fn execute_send_ascii(
1433    bash_state: &mut BashState,
1434    ascii_codes: &[u8],
1435    submit: bool,
1436    bg_shell: Option<SharedPtyShell>,
1437    is_bg: bool,
1438    bg_id: Option<&str>,
1439    timeout_s: f64,
1440) -> Result<String> {
1441    debug!("Processing SendAscii action: {ascii_codes:?} (submit={submit})");
1442
1443    // Validate - matches WCGW Python
1444    if ascii_codes.is_empty() {
1445        return Err(WinxError::CommandExecutionError(
1446            "Failure: send_ascii cannot be empty".to_string(),
1447        ));
1448    }
1449
1450    let shell_arc = bg_shell.unwrap_or_else(|| bash_state.pty_shell.clone());
1451    let mut is_interrupt = false;
1452
1453    {
1454        let mut guard = shell_arc.lock().await;
1455
1456        let bash = guard.as_mut().ok_or(WinxError::BashStateNotInitialized)?;
1457
1458        // Send each ASCII code - matches WCGW Python
1459        for &code in ascii_codes {
1460            // matches WCGW Python: bash_state.send(chr(ascii_char), ...)
1461            bash.send_bytes(&[code]).map_err(|e| {
1462                WinxError::CommandExecutionError(format!("Failed to write ASCII code: {e}"))
1463            })?;
1464
1465            // Check for interrupt - matches WCGW Python: if ascii_char == 3: is_interrupt = True
1466            if code == 3 {
1467                is_interrupt = true;
1468            }
1469        }
1470    }
1471
1472    // Submit as a separate, slightly-delayed write (see `submit_enter`) so an
1473    // Ink TUI doesn't drop the CR.
1474    if submit {
1475        submit_enter(&shell_arc).await?;
1476    }
1477
1478    // Same divergence from wcgw as in `execute_send_specials`: send_ascii [10]
1479    // or [13] is treated as a direct write, not a status check. Callers that
1480    // need patience-aware reads should use `status_check`.
1481
1482    // Wait for output
1483    let mut output =
1484        wait_for_output(bash_state, &shell_arc, timeout_s, is_bg, bg_id, false).await?;
1485
1486    // Add interrupt failure message if still running - matches WCGW Python
1487    if is_interrupt && output.contains("status = still running") {
1488        output.push_str("\n---\n----\nFailure interrupting.\nYou may want to try Ctrl-c again or program specific exit interactive commands.\n");
1489    }
1490
1491    Ok(output)
1492}
1493
1494/// Execute command in background - matches WCGW Python's `is_background` handling
1495async fn execute_in_background(
1496    bash_state: &mut BashState,
1497    command: &str,
1498    timeout_s: f64,
1499) -> Result<String> {
1500    debug!("Executing command in background: {}", command);
1501
1502    // Start a new background shell - matches WCGW Python bash_state.start_new_bg_shell
1503    let restricted_mode =
1504        matches!(bash_state.bash_command_mode.bash_mode, crate::types::BashMode::RestrictedMode);
1505
1506    let bg_id = {
1507        let mut manager = lock_bg_manager();
1508        manager.start_new_shell(&bash_state.cwd, restricted_mode)?
1509    };
1510
1511    // Get the shell
1512    let shell_arc = {
1513        let manager = lock_bg_manager();
1514        manager.get_shell(&bg_id).ok_or_else(|| {
1515            WinxError::CommandExecutionError("Failed to get background shell".to_string())
1516        })?
1517    };
1518
1519    // Send command via the same PTY path used by foreground execute_command.
1520    {
1521        let mut guard = shell_arc.lock().await;
1522        let bash = guard.as_mut().ok_or(WinxError::BashStateNotInitialized)?;
1523        bash.send_command(command).map_err(|e| {
1524            WinxError::CommandExecutionError(format!("Failed to send bg command: {e}"))
1525        })?;
1526    }
1527    debug!("bg[{}]: send_command returned, replying with bg_command_id", bg_id);
1528
1529    let _ = timeout_s;
1530    let _ = shell_arc;
1531    Ok(get_status(bash_state, true, Some(&bg_id), true, None))
1532}
1533
1534#[cfg(test)]
1535mod tests {
1536    use super::strip_tail_pipe_impl;
1537
1538    #[test]
1539    fn strips_trailing_tail_by_default() {
1540        assert_eq!(strip_tail_pipe_impl("seq 1 5 | tail -2", false), "seq 1 5");
1541        assert_eq!(strip_tail_pipe_impl("cat log | tail -n 20", false), "cat log");
1542        assert_eq!(strip_tail_pipe_impl("cat log | tail", false), "cat log");
1543        assert_eq!(strip_tail_pipe_impl("ls -la|tail -5", false), "ls -la");
1544    }
1545
1546    #[test]
1547    fn keeps_command_without_trailing_tail() {
1548        // tail not at the end, or piped further, must be left alone.
1549        assert_eq!(strip_tail_pipe_impl("tail -f log | grep err", false), "tail -f log | grep err");
1550        assert_eq!(strip_tail_pipe_impl("echo hi", false), "echo hi");
1551        assert_eq!(
1552            strip_tail_pipe_impl("cat a | tail -5 | wc -l", false),
1553            "cat a | tail -5 | wc -l"
1554        );
1555    }
1556
1557    #[test]
1558    fn keep_mode_preserves_tail_pipe() {
1559        // WINX_KEEP_TAIL_PIPE behavior: command passes through untouched.
1560        assert_eq!(strip_tail_pipe_impl("seq 1 5 | tail -2", true), "seq 1 5 | tail -2");
1561        assert_eq!(strip_tail_pipe_impl("cat log | tail -n 20", true), "cat log | tail -n 20");
1562    }
1563}