//! Screen-state view of an agent's PTY output. //! //! Raw PTY bytes are fed through a terminal emulator so callers can ask what //! is actually on screen (is the input box ready? what text is in it?) instead //! of guessing from output timing. use crate::output_buffer::OutputBuffer; /// What the agent's input box currently holds. #[derive(Debug, Clone, PartialEq, Eq)] pub struct InputBox { /// Text typed into the box; wrapped rows are joined with `\n`. pub text: String, } impl InputBox { pub fn is_empty(&self) -> bool { self.text.trim().is_empty() } } /// Drops all whitespace so text that was wrapped or re-flowed by the terminal /// compares equal to the original. pub fn squash(s: &str) -> String { s.chars().filter(|c| !c.is_whitespace()).collect() } fn is_prompt_marker(c: char) -> bool { c == '>' || c == '❯' } fn is_border(row: &str) -> bool { let t = row.trim_start(); t.starts_with('╭') || t.starts_with('╰') || t.starts_with("──") || t.starts_with('─') } fn is_top_border(row: &str) -> bool { let t = row.trim_start(); t.starts_with('╭') || t.starts_with('─') } /// Strips the box side border and padding from a row. fn strip_sides(row: &str) -> &str { let t = row.trim(); let t = t.strip_prefix('│').unwrap_or(t); let t = t.strip_suffix('│').unwrap_or(t); t.trim() } /// Finds the input box in a list of screen rows: the last prompt row that is /// preceded by a top border and followed (after any wrapped rows) by a bottom /// border. Echoed turns in the transcript also start with `>`, but are never /// enclosed by borders. pub fn find_input_box(rows: &[String]) -> Option { for start in (0..rows.len()).rev() { let first = strip_sides(&rows[start]); let mut chars = first.chars(); let Some(marker) = chars.next() else { continue }; if !is_prompt_marker(marker) || start == 0 || !is_top_border(&rows[start - 1]) { continue; } let mut lines = vec![chars.as_str().trim().to_string()]; let mut end = start + 1; while end < rows.len() && !is_border(&rows[end]) { lines.push(strip_sides(&rows[end]).to_string()); end += 1; } if end >= rows.len() { continue; // no bottom border: mid-redraw } while lines.last().is_some_and(|l| l.is_empty()) { lines.pop(); } return Some(InputBox { text: lines.join("\n"), }); } None } /// Incrementally feeds an [`OutputBuffer`] into a terminal emulator. pub struct ScreenTracker { parser: vt100::Parser, offset: usize, rows: u16, cols: u16, } impl ScreenTracker { /// Replays everything currently in `buffer` onto a fresh `rows`×`cols` /// screen. pub fn new(buffer: &OutputBuffer, rows: u16, cols: u16) -> Self { let mut t = Self { parser: vt100::Parser::new(rows, cols, 0), offset: 0, rows, cols, }; t.sync(buffer); t } /// Feeds any output written since the last sync. pub fn sync(&mut self, buffer: &OutputBuffer) { let (mut bytes, mut next, gap) = buffer.read_from(self.offset); if self.offset == 0 || gap { (bytes, next) = buffer.attach_snapshot(); } self.parser.process(&bytes); self.offset = next; } pub fn rows(&self) -> Vec { self.parser.screen().rows(0, self.cols).collect() } /// Screen rows with dim cells blanked. Claude Code draws a dim /// placeholder hint (`Try "..."`) in its empty input box, and that must /// not read as typed text. fn rows_without_dim(&self) -> Vec { let screen = self.parser.screen(); (0..self.rows) .map(|r| { let mut row = String::new(); for c in 0..self.cols { match screen.cell(r, c) { Some(cell) if cell.is_wide_continuation() => {} Some(cell) if cell.has_contents() && !cell.dim() => { row.push_str(cell.contents()) } _ => row.push(' '), } } row }) .collect() } pub fn input_box(&self) -> Option { find_input_box(&self.rows_without_dim()) } /// The input box is drawn and empty, so keystrokes will land in it. pub fn is_ready(&self) -> bool { self.input_box().is_some_and(|b| b.is_empty()) } /// Whether `text` appears on screen outside the input box (an echoed turn). pub fn echoes(&self, text: &str) -> bool { let rows = self.rows(); let boxed = self.box_row_range(&rows); let outside: String = rows .iter() .enumerate() .filter(|(i, _)| !boxed.contains(i)) .map(|(_, r)| squash(&strip_echo_marker(r))) .collect(); outside.contains(&squash(text)) } /// True when `text` is too large to expect it fully visible on screen. pub fn exceeds_screen(&self, text: &str) -> bool { squash(text).chars().count() > (self.rows as usize * self.cols as usize) / 2 } fn box_row_range(&self, rows: &[String]) -> std::ops::Range { let Some(bottom) = (0..rows.len()).rev().find(|&i| { let t = rows[i].trim_start(); t.starts_with('╰') || t.starts_with('─') }) else { return 0..0; }; let top = (0..bottom) .rev() .find(|&i| is_top_border(&rows[i])) .unwrap_or(bottom); top..bottom + 1 } } fn strip_echo_marker(row: &str) -> String { let t = row.trim_start(); match t.chars().next() { Some(c) if is_prompt_marker(c) => t[c.len_utf8()..].to_string(), _ => row.to_string(), } } /// Conservative phase classification; an absent box never establishes readiness. pub fn input_phase(screen: &vt100::Screen) -> &'static str { let (height, width) = screen.size(); let rows: Vec = (0..height) .map(|r| { (0..width) .map(|c| match screen.cell(r, c) { Some(cell) if cell.has_contents() && !cell.dim() => cell.contents().to_string(), _ => " ".to_string(), }) .collect() }) .collect(); let text = rows.join("\n").to_lowercase(); if text.contains("esc to cancel") || text.contains("do you want to proceed") || text.contains("yes, allow") || text.contains("trust this folder") { "dialog" } else if let Some(b) = find_input_box(&rows) { if b.is_empty() { "input_ready" } else { "has_draft" } } else { "busy" } } #[cfg(test)] mod tests { use super::*; fn rows(lines: &[&str]) -> Vec { lines.iter().map(|s| s.to_string()).collect() } #[test] fn given_screen_state_should_distinguish_ready_draft_dialog_and_unknown() { let buffer = OutputBuffer::new(); assert_eq!(buffer.input_phase(), "busy"); buffer.write("────────\r\n❯ \x1b[2mTry this\x1b[22m\r\n────────".as_bytes()); assert_eq!(buffer.input_phase(), "input_ready"); buffer.write("\x1b[2;3H\x1b[Kdraft".as_bytes()); assert_eq!(buffer.input_phase(), "has_draft"); buffer.write(b"\x1b[5;1HDo you want to proceed? Esc to cancel"); assert_eq!(buffer.input_phase(), "dialog"); } #[test] fn given_empty_box_should_report_empty_input() { let r = rows(&["╭────╮", "│ > │", "╰────╯"]); let b = find_input_box(&r).unwrap(); assert!(b.is_empty()); } #[test] fn given_typed_text_should_return_it() { let r = rows(&["╭──────╮", "│ > hi │", "╰──────╯"]); assert_eq!(find_input_box(&r).unwrap().text, "hi"); } #[test] fn given_wrapped_text_should_join_rows() { let r = rows(&["╭──────╮", "│ > ab │", "│ cd │", "╰──────╯"]); assert_eq!(squash(&find_input_box(&r).unwrap().text), "abcd"); } #[test] fn given_echoed_turn_above_box_should_read_only_the_box() { let r = rows(&["> old turn", "", "╭──────╮", "│ > x │", "╰──────╯"]); assert_eq!(find_input_box(&r).unwrap().text, "x"); } #[test] fn given_rule_style_input_should_be_found() { let r = rows(&["────────", "❯ hello", "────────"]); assert_eq!(find_input_box(&r).unwrap().text, "hello"); } #[test] fn given_no_box_should_return_none() { assert!(find_input_box(&rows(&["loading hooks..."])).is_none()); } #[test] fn given_mid_redraw_without_bottom_border_should_return_none() { assert!(find_input_box(&rows(&["╭────╮", "│ > a│"])).is_none()); } #[test] fn given_pty_output_should_track_ready_and_echo() { let buf = OutputBuffer::new(); buf.write(b"loading...\r\n"); let mut t = ScreenTracker::new(&buf, 24, 80); assert!(!t.is_ready()); buf.write( "\x1b[2J\x1b[H> full text here\r\n╭────────╮\r\n│ > │\r\n╰────────╯\r\n" .as_bytes(), ); t.sync(&buf); assert!(t.is_ready()); assert!(t.echoes("full text here")); assert!(!t.echoes("something else")); } #[test] fn given_dim_placeholder_in_empty_box_should_be_ready() { // Claude Code's empty box, as captured from a live agent. let buf = OutputBuffer::new(); buf.write( "\x1b[38;2;136;136;136m────────────────────\r\n\x1b[39m❯\u{a0}\x1b[2mTry \"fix typecheck errors\"\r\n\x1b[22m\x1b[38;2;136;136;136m────────────────────\x1b[39m\r\n" .as_bytes(), ); let t = ScreenTracker::new(&buf, 24, 80); assert!(t.input_box().is_some_and(|b| b.is_empty())); assert!(t.is_ready()); } #[test] fn given_typed_text_after_placeholder_should_read_typed_text() { let buf = OutputBuffer::new(); buf.write( "────────\r\n❯\u{a0}\x1b[2mTry \"x\"\x1b[22m\r\n────────\r\n\x1b[2;3H\x1b[Khello\r\n" .as_bytes(), ); let t = ScreenTracker::new(&buf, 24, 80); assert_eq!(t.input_box().unwrap().text, "hello"); } // --- ConPTY captures from WIN-1.1 (`spikes/conpty`) ------------------ // // `results/.raw.txt` is the escaped byte stream a real ConPTY sent // (120x30), and `fixtures/.txt` is the screen the spike parsed from // it. Replaying the bytes must give that screen, and the heuristics read // the parsed screen (with its attributes: Claude's placeholder is dim) // into the phase the spike recorded (REQ-WINAPP-018). macro_rules! conpty_capture { ($name:literal) => { ( $name, include_str!(concat!( env!("CARGO_MANIFEST_DIR"), "/../../spikes/conpty/results/", $name, ".raw.txt" )), include_str!(concat!( env!("CARGO_MANIFEST_DIR"), "/../../spikes/conpty/fixtures/", $name, ".txt" )), ) }; } /// Inverse of the spike's `escape_raw`: `\e \r \n \a \xHH` are escapes, /// a real newline follows each `\n`, and any other backslash is literal. fn unescape_capture(raw: &str) -> Vec { let raw = raw.replace("\\n\n", "\\n"); let mut out = Vec::new(); let mut chars = raw.chars().peekable(); while let Some(c) = chars.next() { let escaped = match (c, chars.peek()) { ('\\', Some('e')) => Some(0x1b), ('\\', Some('r')) => Some(b'\r'), ('\\', Some('n')) => Some(b'\n'), ('\\', Some('a')) => Some(0x07), _ => None, }; if let Some(byte) = escaped { chars.next(); out.push(byte); continue; } if c == '\\' && chars.peek() == Some(&'x') { let hex: String = chars.clone().skip(1).take(2).collect(); if let Ok(byte) = u8::from_str_radix(&hex, 16) && hex.len() == 2 { chars.nth(2); out.push(byte); continue; } } let mut utf8 = [0; 4]; out.extend_from_slice(c.encode_utf8(&mut utf8).as_bytes()); } out } /// The spike's `screen_text`: rows trimmed, trailing blank rows dropped. fn fixture_text(screen: &vt100::Screen) -> String { let (_, cols) = screen.size(); let rows: Vec = screen .rows(0, cols) .map(|r| r.trim_end().to_string()) .collect(); format!("{}\n", rows.join("\n").trim_end_matches('\n')) } #[test] fn given_conpty_captures_should_replay_to_their_fixtures_and_recorded_phases() { for ((name, raw, fixture), phase) in [ (conpty_capture!("pwsh"), "busy"), (conpty_capture!("cmd"), "busy"), (conpty_capture!("cmd-conpty"), "busy"), (conpty_capture!("cmd-passthrough"), "busy"), (conpty_capture!("claude"), "input_ready"), (conpty_capture!("claude-conpty"), "input_ready"), (conpty_capture!("claude-passthrough"), "input_ready"), (conpty_capture!("claude-untrusted-dir"), "dialog"), // Redacting the user name changed its width, so the relative // cursor moves around it land elsewhere: the phase still holds. (conpty_capture!("codex"), "dialog"), (conpty_capture!("codex-trusted-override"), "busy"), (conpty_capture!("codex-conpty"), "busy"), (conpty_capture!("codex-passthrough"), "busy"), ] { let mut parser = vt100::Parser::new(30, 120, 0); parser.process(&unescape_capture(raw)); if name != "codex" { assert_eq!(fixture_text(parser.screen()), fixture, "{name}: screen"); } assert_eq!(input_phase(parser.screen()), phase, "{name}: phase"); // The same bytes through the engine's own buffer and tracker. let buffer = OutputBuffer::new(); buffer.write(&unescape_capture(raw)); let tracker = ScreenTracker::new(&buffer, 30, 120); assert_eq!(tracker.is_ready(), phase == "input_ready", "{name}: ready"); } } /// Plain-text fixtures with no attributes to lose: typed text in the /// Claude box is a draft, and dialogs and bare prompts are never ready. #[test] fn given_conpty_screen_fixtures_should_classify_like_the_spike() { for (name, fixture, phase) in [ ( "claude-typed", include_str!(concat!( env!("CARGO_MANIFEST_DIR"), "/../../spikes/conpty/fixtures/claude-typed.txt" )), "has_draft", ), ( "claude-untrusted-dir", include_str!(concat!( env!("CARGO_MANIFEST_DIR"), "/../../spikes/conpty/fixtures/claude-untrusted-dir.txt" )), "dialog", ), ( "pwsh-resize-after-70x30", include_str!(concat!( env!("CARGO_MANIFEST_DIR"), "/../../spikes/conpty/fixtures/pwsh-resize-after-70x30.txt" )), "busy", ), ] { let mut parser = vt100::Parser::new(30, 120, 0); parser.process(fixture.replace('\n', "\r\n").as_bytes()); assert_eq!(input_phase(parser.screen()), phase, "{name}"); } let typed = include_str!(concat!( env!("CARGO_MANIFEST_DIR"), "/../../spikes/conpty/fixtures/claude-typed.txt" )); let rows: Vec = typed.lines().map(str::to_owned).collect(); assert!(!find_input_box(&rows).unwrap().is_empty()); } }