From 22813f17bc55327a59fcdf1c4a396d39b5e23619 Mon Sep 17 00:00:00 2001 From: Bill Sommers Date: Tue, 10 Nov 2026 06:56:54 -0500 Subject: [PATCH] perf(vt): stateful single-pass control-sequence framer in front of the engine The side scanner was stateless per PTY read: only OSC 52 survived a split, each ESC handler searched to the end of the chunk (quadratic for many unterminated OSCs), and the OSC 52 carry was re-scanned from scratch on every read (quadratic for a long unterminated sequence). src/vt/framer.rs is a byte state machine that follows the engine's own boundaries (vte 0.13 table: ESC restarts, CAN/SUB cancel, C0 executes inside ESC/CSI, no 8-bit C1 introducers). It withholds ESC/CSI/OSC until complete, keeps DCS/APC/SOS/PM away from the engine, and reports completed sequences to the handlers, which now receive exact bodies. Handlers that read engine state run after the engine has consumed their sequence. The carry is bounded and an over-long sequence is dropped whole. Also fixed, each with a regression test in tests/vt_split_invariance.rs: split CSI/OSC/DCS/APC lost; split Sixel/Kitty payload printed on the grid; title ending at a later BEL; CSI 6n reporting the end-of-read cursor; split ESC # n lost; CSI ?38h swallowing earlier sequences; split OSC 1337;Canvas. Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 4 + docs/DEVELOPER_AND_AGENT_GUIDE.md | 3 +- src/pane/mod.rs | 8 +- src/vt/framer.rs | 455 ++++++++++++++++++++++++++++++ src/vt/graphics.rs | 30 -- src/vt/mod.rs | 109 ++++--- src/vt/scan.rs | 158 ++++++++--- src/vt/scan_osc_dcs.rs | 243 ++++++---------- src/vt/tektronix.rs | 18 ++ tests/vt_split_invariance.rs | 356 +++++++++++++++++++++++ 10 files changed, 1103 insertions(+), 281 deletions(-) create mode 100644 src/vt/framer.rs create mode 100644 tests/vt_split_invariance.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 95efbd86..1540e129 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Fixed +- **Control sequences split across PTY reads**: the side scanner that handles what the VT engine does not (OSC 0/7/8/9/52/133/777/1337, DECSET flags, Kitty keyboard, DECRQSS/XTGETTCAP, Sixel, Kitty graphics, DEC line size, Tektronix entry) is now a stateful framer (`src/vt/framer.rs`). A sequence cut by a read boundary is handled exactly as if it arrived whole; previously only OSC 52 survived a split, and a split Sixel or Kitty image printed its payload on the grid. +- **Scanner cost**: each output byte is consumed once. A long unterminated OSC 52 fed in small reads, and many unterminated OSCs in one read, were quadratic. +- **Sequence boundaries**: a title or notification now ends at its own terminator instead of at a later `BEL`; `CSI 6n` and image placements use the cursor position at the sequence, not at the end of the read; `CSI ?38h` no longer swallows earlier sequences that shared its read. +- **Bounded carry**: an unterminated `CSI` (256 bytes), `OSC` (1 MiB + 64), `APC` (Kitty payload limit + 4 KiB) or `DCS` (4 MiB) is dropped whole at its limit and the rest is skipped without being stored. - **Remote tmux link (`:ssh-tmux`) rendering**: the remote tmux client is now as large as the linked pane instead of the whole Bilterm window (full-screen programs and wrapped lines drew in the wrong place); a hydrate restores alternate screen, cursor position and visibility, application cursor keys and mouse modes (re-attaching while `vim` ran left the cursor in the wrong place and stale rows on the primary screen); a UTF-8 character split across two `%output` lines is no longer turned into replacement characters; `%extended-output` is understood. - **Remote tmux link flooding**: Bilterm asks tmux for `pause-after` flow control and re-captures a paused pane, so a flooding remote pane no longer starves input (Ctrl-C typed in Bilterm now arrives). - **Remote tmux link input**: a paste larger than 4096 bytes was silently truncated; pastes are now delivered complete and in order. diff --git a/docs/DEVELOPER_AND_AGENT_GUIDE.md b/docs/DEVELOPER_AND_AGENT_GUIDE.md index e1a5e265..32cfa837 100644 --- a/docs/DEVELOPER_AND_AGENT_GUIDE.md +++ b/docs/DEVELOPER_AND_AGENT_GUIDE.md @@ -97,7 +97,8 @@ src/ ├── vt/ │ ├── mod.rs # VtParser state machine, CommandBlock tracking, scrollback & size management │ ├── backend.rs # TerminalBackend port trait, MockTerminalBackend, and conformance suite (ARCH-R03) -│ ├── scan.rs # Single-pass ESC (\x1b) stream pre-scanner (DECSET traps, CSI DECRPM/DA1/DSR) +│ ├── framer.rs # Stateful control-sequence framer in front of the engine: split-safe, linear, bounded carry +│ ├── scan.rs # Framed-chunk dispatch to the engine + CSI handlers (DECSET traps, DECRPM/DA1/DSR, SGR) │ ├── scan_osc_dcs.rs # OSC 7/8/9/52/133/777/1337, DCS XTGETTCAP/Sixel & APC Kitty scanners │ ├── graphics.rs # Kitty Graphics (APC G) & DEC Sixel (DCS q) decoders and placements │ ├── tektronix.rs # Tektronix 4010/4014 10-bit/12-bit vector storage-tube state machine & phosphor decay diff --git a/src/pane/mod.rs b/src/pane/mod.rs index f87be00e..f6d2556e 100644 --- a/src/pane/mod.rs +++ b/src/pane/mod.rs @@ -822,8 +822,8 @@ impl CompositePane { self.mirror_output_tap(bytes); self.vt.process(bytes); self.drain_osc52_decoded(); - self.canvas_scene - .scan_osc_canvas_sequences(self.vt.tektronix_mut(), bytes); + self.vt + .drain_canvas_osc(|tek, seq| self.canvas_scene.scan_osc_canvas_sequences(tek, seq)); if let Some(wt) = self.vt.window_title() { self.title = format!("{wt} [#{}]", self.id); } @@ -877,8 +877,8 @@ impl CompositePane { self.mirror_output_tap(&coalesced); self.vt.process(&coalesced); self.drain_osc52_decoded(); - self.canvas_scene - .scan_osc_canvas_sequences(self.vt.tektronix_mut(), &coalesced); + self.vt + .drain_canvas_osc(|tek, seq| self.canvas_scene.scan_osc_canvas_sequences(tek, seq)); if let Some(wt) = self.vt.window_title() { self.title = format!("{wt} [#{}]", self.id); } diff --git a/src/vt/framer.rs b/src/vt/framer.rs new file mode 100644 index 00000000..23d6463d --- /dev/null +++ b/src/vt/framer.rs @@ -0,0 +1,455 @@ +//! Stateful, single-pass control-sequence framer (`ARCH-R04`). +//! +//! [`SeqFramer`] sits in front of both the VT engine and the side handlers in `scan.rs` / +//! `scan_osc_dcs.rs`. It is a byte-level state machine that carries its state across +//! [`SeqFramer::feed`] calls, so what it produces is a function of the byte stream alone, never of +//! how the stream was chunked by PTY reads: +//! +//! * Ground bytes go straight to the engine buffer. +//! * `ESC` / `CSI` / `OSC` sequences are withheld until complete and then released whole, so the +//! per-chunk rewriting stages behind the framer never see half a sequence. +//! * `DCS` / `APC` / `SOS` / `PM` strings never reach the engine (it would print their payload). +//! * Completed sequences the side handlers care about are reported as [`FramedSeq`] events. +//! +//! Sequence boundaries follow the engine's own parser (`vte` 0.13 state table): `ESC` restarts a +//! sequence from any state, `CAN` / `SUB` cancel it, C0 controls inside `ESC` / `CSI` are executed +//! at once (they are handed to the engine ahead of the withheld sequence, which is equivalent), +//! bytes `>= 0x7f` inside `ESC` / `CSI` are ignored, and 8-bit C1 bytes are *not* introducers +//! (the stream is UTF-8, where `0x80..=0x9f` are continuation bytes). Strings end at `BEL`, `ST` +//! (`ESC \`), `CAN`, `SUB` or a bare `ESC`; only `BEL` and `ST` complete them, anything else +//! cancels without an event. +//! +//! Linear time: every input byte is examined once (`steps` counts them), copied at most once into +//! the carry and once into the output, and a completed body is handed to exactly one handler. +//! Nothing is ever re-scanned on the next `feed`. The carry is bounded: a sequence longer than its +//! limit is dropped whole (no event, nothing forwarded) and the rest of it is skipped unstored. + +use super::codec::MAX_OSC52_PAYLOAD_BYTES; +use super::graphics::MAX_KITTY_PAYLOAD_B64_BYTES; +use super::scan::split_incomplete_utf8_tail; + +/// Longest `CSI` held (bytes after `ESC [`); same bound as the count clamp and the Tektronix buffer. +pub(super) const MAX_CSI_BODY_BYTES: usize = 256; +/// Longest `OSC` held (introducer included): the largest forwardable `OSC 52` write plus its header. +pub(super) const MAX_OSC_BODY_BYTES: usize = MAX_OSC52_PAYLOAD_BYTES + 64; +/// Longest `APC` body held: the largest accepted Kitty payload plus its control keys. +pub(super) const MAX_APC_BODY_BYTES: usize = MAX_KITTY_PAYLOAD_B64_BYTES + 4096; +/// Longest `DCS` body held. Sixel has no payload clamp of its own (the decoder clamps the image +/// to `MAX_GRAPHIC_DIM_PX` instead), so this is the bound on a Sixel stream. +pub(super) const MAX_DCS_BODY_BYTES: usize = 4 * 1024 * 1024; +/// Most intermediates kept for a plain `ESC` sequence (the engine honours two). +const MAX_ESC_BODY_BYTES: usize = 8; +/// Largest buffer capacity kept between feeds. +pub(super) const RETAINED_BUFFER_BYTES: usize = 64 * 1024; +/// CSI final bytes the side handlers act on; other finals are forwarded without an event. +const CSI_EVENT_FINALS: &[u8] = b"hlnucqpmJ"; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum SeqKind { + /// `ESC # 3..6` (DEC line size) or `ESC c` (RIS); body is the bytes after `ESC`. + Esc, + /// Body is the parameter, intermediate and final bytes after `ESC [`. + Csi, + Osc, + Dcs, + Apc, + /// `CSI ? 38 h`: the stream switches to the Tektronix machine. Not forwarded to the engine. + TekEnter, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum StringTerminator { + Bel, + St, +} + +impl StringTerminator { + pub(super) fn as_str(self) -> &'static str { + match self { + Self::Bel => "\x07", + Self::St => "\x1b\\", + } + } +} + +/// One completed control sequence. +#[derive(Clone, Copy, Debug)] +pub(super) struct FramedSeq { + pub(super) kind: SeqKind, + /// How a string sequence ended (`Bel` for non-string kinds). + pub(super) terminator: StringTerminator, + /// Length of [`FrameOutput::engine`] once this sequence (and everything before it) is in it. + pub(super) engine_end: usize, + in_engine: bool, + start: usize, + end: usize, +} + +/// Result of one [`SeqFramer::feed`]; reused across calls to keep its allocations. +#[derive(Debug, Default)] +pub(super) struct FrameOutput { + /// Bytes for the VT engine, in stream order. + pub(super) engine: Vec, + /// Completed sequences, in stream order. + pub(super) seqs: Vec, + strings: Vec, +} + +impl FrameOutput { + pub(super) fn body(&self, seq: &FramedSeq) -> &[u8] { + let buf = if seq.in_engine { &self.engine } else { &self.strings }; + &buf[seq.start..seq.end] + } + + fn clear(&mut self) { + for buf in [&mut self.engine, &mut self.strings] { + if buf.capacity() > RETAINED_BUFFER_BYTES { + *buf = Vec::new(); + } else { + buf.clear(); + } + } + self.seqs.clear(); + } +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +enum State { + #[default] + Ground, + Escape, + EscapeIntermediate, + Csi, + /// Over-long `CSI`: skipped up to its final byte. + CsiDiscard, + Osc, + OscEsc, + /// `DCS` (`true`) or `APC` (`false`) body being collected. + Str(bool), + StrEsc(bool), + /// `SOS` / `PM`, or an over-long `OSC` / `DCS` / `APC`: skipped up to its terminator. + StrDiscard, +} + +/// `CSI` grammar position, mirroring the engine's entry / param / intermediate / ignore states. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +enum CsiPhase { + #[default] + Entry, + Param, + Intermediate, + Ignore, +} + +#[derive(Debug, Default)] +pub(super) struct SeqFramer { + state: State, + /// Withheld `ESC` / `CSI` / `OSC` bytes (from the `ESC`), or the `DCS` / `APC` body. + held: Vec, + csi_phase: CsiPhase, + /// Incomplete trailing UTF-8 char of the previous feed's ground text. + utf8_tail: Vec, + steps: u64, + dropped: u64, +} + +fn is_string_end(b: u8) -> bool { + matches!(b, 0x07 | 0x18 | 0x1a | 0x1b) +} + +impl SeqFramer { + /// Bytes currently withheld (bounded by the limits above). + pub(super) fn carry_len(&self) -> usize { + self.held.len() + self.utf8_tail.len() + } + + /// Input bytes consumed so far; each byte is counted exactly once. + pub(super) fn steps(&self) -> u64 { + self.steps + } + + /// Sequences dropped for exceeding their length limit. + pub(super) fn dropped(&self) -> u64 { + self.dropped + } + + /// Consumes `input` into `out` (cleared first) and returns how many bytes were consumed: + /// all of them, unless a [`SeqKind::TekEnter`] stopped the feed early. + pub(super) fn feed(&mut self, input: &[u8], out: &mut FrameOutput) -> usize { + out.clear(); + out.engine.append(&mut self.utf8_tail); + let mut i = 0; + while i < input.len() { + let rest = &input[i..]; + let run = match self.state { + State::Ground => { + let run = rest.iter().position(|&b| b == 0x1b).unwrap_or(rest.len()); + out.engine.extend_from_slice(&rest[..run]); + run + } + State::Osc | State::Str(_) | State::StrDiscard => { + let run = rest.iter().position(|&b| is_string_end(b)).unwrap_or(rest.len()); + self.collect_string(&rest[..run]); + run + } + _ => 0, + }; + i += run; + self.steps += run as u64; + let Some(&b) = input.get(i) else { break }; + i += 1; + self.steps += 1; + if self.step(b, out) { + return i; + } + } + if self.state == State::Ground { + let (complete, tail) = split_incomplete_utf8_tail(&out.engine); + if !tail.is_empty() { + let keep = complete.len(); + self.utf8_tail.extend_from_slice(tail); + out.engine.truncate(keep); + } + } + input.len() + } + + fn collect_string(&mut self, run: &[u8]) { + let limit = match self.state { + State::Osc => MAX_OSC_BODY_BYTES, + State::Str(true) => MAX_DCS_BODY_BYTES, + State::Str(false) => MAX_APC_BODY_BYTES, + _ => return, + }; + if self.held.len() + run.len() > limit { + self.drop_overlong(State::StrDiscard); + } else { + self.held.extend_from_slice(run); + } + } + + fn drop_overlong(&mut self, discard: State) { + self.held = Vec::new(); + self.dropped += 1; + self.state = discard; + } + + /// Gives back the allocation of a large released sequence (an image, a clipboard write). + fn shrink_held(&mut self) { + if self.held.capacity() > RETAINED_BUFFER_BYTES { + self.held = Vec::new(); + } + } + + fn begin_escape(&mut self) { + self.held.clear(); + self.held.push(0x1b); + self.state = State::Escape; + } + + /// `CAN` / `SUB`: the engine drops the sequence and executes the control byte. + fn cancel(&mut self, b: u8, out: &mut FrameOutput) { + self.held.clear(); + out.engine.push(b); + self.state = State::Ground; + } + + /// Advances by one byte; `true` when the feed must stop (Tektronix capture begins). + fn step(&mut self, b: u8, out: &mut FrameOutput) -> bool { + match self.state { + State::Ground => self.begin_escape(), + State::Escape | State::EscapeIntermediate | State::Csi | State::CsiDiscard => { + return self.step_escape(b, out); + } + State::Osc => match b { + 0x07 => self.finish_osc(StringTerminator::Bel, out), + 0x1b => self.state = State::OscEsc, + _ => self.cancel(b, out), + }, + State::OscEsc | State::StrEsc(_) => { + if b == b'\\' { + match self.state { + State::OscEsc => self.finish_osc(StringTerminator::St, out), + State::StrEsc(dcs) => self.finish_string(dcs, StringTerminator::St, out), + _ => {} + } + } else { + // A bare ESC cancels the string and begins a new sequence with `b`. + self.begin_escape(); + return self.step_escape(b, out); + } + } + State::Str(dcs) => match b { + 0x07 => self.finish_string(dcs, StringTerminator::Bel, out), + 0x1b => self.state = State::StrEsc(dcs), + _ => self.cancel(b, out), + }, + State::StrDiscard => match b { + 0x07 => self.state = State::Ground, + 0x1b => self.begin_escape(), + _ => self.cancel(b, out), + }, + } + false + } + + fn step_escape(&mut self, b: u8, out: &mut FrameOutput) -> bool { + match b { + 0x1b => self.begin_escape(), + 0x18 | 0x1a => self.cancel(b, out), + // Executed by the engine immediately; the pending sequence is unaffected. + 0x00..=0x1f => out.engine.push(b), + // Ignored by the engine inside escape sequences (DEL and non-ASCII). + 0x7f..=0xff => {} + _ => return self.step_escape_printable(b, out), + } + false + } + + fn step_escape_printable(&mut self, b: u8, out: &mut FrameOutput) -> bool { + match self.state { + State::Escape => match b { + 0x20..=0x2f => { + self.held.push(b); + self.state = State::EscapeIntermediate; + } + b'[' => { + self.held.push(b); + self.csi_phase = CsiPhase::Entry; + self.state = State::Csi; + } + b']' => { + self.held.push(b); + self.state = State::Osc; + } + b'P' | b'_' => { + self.held.clear(); + self.state = State::Str(b == b'P'); + } + b'X' | b'^' => { + self.held.clear(); + self.state = State::StrDiscard; + } + _ => self.finish_esc(b, out), + }, + State::EscapeIntermediate => match b { + 0x20..=0x2f => { + if self.held.len() <= MAX_ESC_BODY_BYTES { + self.held.push(b); + } + } + _ => self.finish_esc(b, out), + }, + State::Csi => match b { + 0x20..=0x3f => { + self.csi_phase = match (self.csi_phase, b) { + (CsiPhase::Ignore, _) => CsiPhase::Ignore, + (_, 0x20..=0x2f) => CsiPhase::Intermediate, + (CsiPhase::Entry, _) | (CsiPhase::Param, 0x30..=0x3b) => CsiPhase::Param, + _ => CsiPhase::Ignore, + }; + if self.held.len() >= MAX_CSI_BODY_BYTES + 2 { + self.drop_overlong(State::CsiDiscard); + } else { + self.held.push(b); + } + } + _ => return self.finish_csi(b, out), + }, + State::CsiDiscard => { + if (0x40..=0x7e).contains(&b) { + self.state = State::Ground; + } + } + _ => {} + } + false + } + + /// Releases the withheld sequence to the engine; returns where it starts in the buffer. + fn release(&mut self, out: &mut FrameOutput) -> usize { + let start = out.engine.len(); + out.engine.append(&mut self.held); + self.shrink_held(); + self.state = State::Ground; + start + } + + fn finish_esc(&mut self, b: u8, out: &mut FrameOutput) { + self.held.push(b); + let event = matches!(self.held[1..], [b'#', b'3'..=b'6'] | [b'c']); + let start = self.release(out); + if event { + let end = out.engine.len(); + out.seqs.push(FramedSeq { + kind: SeqKind::Esc, + terminator: StringTerminator::Bel, + engine_end: end, + in_engine: true, + start: start + 1, + end, + }); + } + } + + fn finish_csi(&mut self, b: u8, out: &mut FrameOutput) -> bool { + self.held.push(b); + let well_formed = self.csi_phase != CsiPhase::Ignore; + if well_formed && self.held[2..] == *b"?38h" { + self.held.clear(); + self.state = State::Ground; + let end = out.engine.len(); + out.seqs.push(FramedSeq { + kind: SeqKind::TekEnter, + terminator: StringTerminator::Bel, + engine_end: end, + in_engine: true, + start: end, + end, + }); + return true; + } + let start = self.release(out); + if well_formed && CSI_EVENT_FINALS.contains(&b) { + let end = out.engine.len(); + out.seqs.push(FramedSeq { + kind: SeqKind::Csi, + terminator: StringTerminator::Bel, + engine_end: end, + in_engine: true, + start: start + 2, + end, + }); + } + false + } + + fn finish_osc(&mut self, terminator: StringTerminator, out: &mut FrameOutput) { + let start = self.release(out); + let end = out.engine.len(); + out.engine.extend_from_slice(terminator.as_str().as_bytes()); + out.seqs.push(FramedSeq { + kind: SeqKind::Osc, + terminator, + engine_end: out.engine.len(), + in_engine: true, + start: start + 2, + end, + }); + } + + fn finish_string(&mut self, dcs: bool, terminator: StringTerminator, out: &mut FrameOutput) { + let start = out.strings.len(); + out.strings.append(&mut self.held); + self.shrink_held(); + self.state = State::Ground; + out.seqs.push(FramedSeq { + kind: if dcs { SeqKind::Dcs } else { SeqKind::Apc }, + terminator, + engine_end: out.engine.len(), + in_engine: false, + start, + end: out.strings.len(), + }); + } +} diff --git a/src/vt/graphics.rs b/src/vt/graphics.rs index f395aaf9..cdd7babd 100644 --- a/src/vt/graphics.rs +++ b/src/vt/graphics.rs @@ -43,36 +43,6 @@ impl VtParser { } } -pub(super) fn strip_apc_and_dcs_envelopes(input: &str) -> String { - let mut out = String::with_capacity(input.len()); - let mut rest = input; - while !rest.is_empty() { - let next_apc = rest.find("\x1b_"); - let next_dcs = rest.find("\x1bP"); - let next_env = match (next_apc, next_dcs) { - (Some(a), Some(d)) => Some(a.min(d)), - (Some(a), None) => Some(a), - (None, Some(d)) => Some(d), - (None, None) => None, - }; - if let Some(pos) = next_env { - out.push_str(&rest[..pos]); - let after_start = &rest[pos + 2..]; - if let Some(end_st) = after_start.find("\x1b\\") { - rest = &after_start[end_st + 2..]; - } else if let Some(end_bel) = after_start.find('\x07') { - rest = &after_start[end_bel + 1..]; - } else { - break; - } - } else { - out.push_str(rest); - break; - } - } - out -} - /// Expands a decoded Kitty payload into `w * h` RGBA pixels; `None` (nothing allocated) when /// either dimension exceeds [`MAX_GRAPHIC_DIM_PX`]. pub(super) fn expand_kitty_payload_to_rgba( diff --git a/src/vt/mod.rs b/src/vt/mod.rs index 4e47099e..0dabae9a 100644 --- a/src/vt/mod.rs +++ b/src/vt/mod.rs @@ -1,6 +1,7 @@ pub mod backend; pub mod codec; pub mod detect; +mod framer; pub mod ghostty_backend; pub mod graphics; pub mod recording; @@ -23,8 +24,7 @@ pub use tektronix::{ use std::path::{Path, PathBuf}; use std::time::Instant; -use crate::vt::codec::{is_binary_noise_chunk, sanitize_binary_noise_chunk}; -use crate::vt::scan::{normalize_extended_sgr_for_vt100, split_incomplete_utf8_tail}; +use crate::vt::scan::split_incomplete_utf8_tail; pub use crate::vt::scrollback::PaneScrollMode; pub use crate::vt::snapshot::{LineSizeAttr, scan_dec_line_size_escapes}; use crate::vt::snapshot::{ @@ -138,8 +138,12 @@ pub struct VtParser { line_size_attrs: std::collections::BTreeMap, tektronix: TektronixState, csi_count_clamp: codec::CsiCountClamp, + /// Incomplete trailing UTF-8 char of the recorded stream (recording only). utf8_carry: Vec, - osc52_carry: Vec, + framer: framer::SeqFramer, + framed: framer::FrameOutput, + /// Complete `OSC 1337;Canvas=` sequences awaiting [`VtParser::drain_canvas_osc`]. + pending_canvas_osc: Vec, primary_stream_log: Vec, /// `false` once `primary_stream_log` has been truncated or invalidated by a rebuild, after /// which column changes reflow via [`VtParser::rebuild`] instead of replaying the log. @@ -216,7 +220,9 @@ impl VtParser { tektronix: TektronixState::new(), csi_count_clamp: codec::CsiCountClamp::default(), utf8_carry: Vec::new(), - osc52_carry: Vec::new(), + framer: framer::SeqFramer::default(), + framed: framer::FrameOutput::default(), + pending_canvas_osc: Vec::new(), primary_stream_log: Vec::new(), primary_log_complete: true, } @@ -225,6 +231,9 @@ impl VtParser { /// Processes a chunk of raw PTY output bytes, updating the VT100 grid, `DECSET 47/1047/1049`, /// `DECSET 2004`, `OSC 52`, and `OSC 133` semantic prompt markers. /// + /// Bytes pass through the stateful [`framer::SeqFramer`], so a control sequence split across + /// two calls is handled exactly as if it had arrived whole (see `framer.rs`). + /// /// When a raw binary noise stream (such as `tail -f /dev/random` or `cat /dev/urandom`) is /// detected while not in alternate screen mode, stray `0x1b` (`ESC`) and raw C0/C1 control /// bytes are sanitized into visible glyphs so random bytes cannot falsely trigger `DECSET` @@ -232,66 +241,34 @@ impl VtParser { pub fn process(&mut self, raw_bytes: &[u8]) { self.generation = self.generation.wrapping_add(1); self.total_bytes_processed = self.total_bytes_processed.saturating_add(raw_bytes.len()); - let combined: std::borrow::Cow<'_, [u8]> = if self.utf8_carry.is_empty() { - std::borrow::Cow::Borrowed(raw_bytes) - } else { - let mut v = std::mem::take(&mut self.utf8_carry); - v.extend_from_slice(raw_bytes); - std::borrow::Cow::Owned(v) - }; - let (bytes, tail) = split_incomplete_utf8_tail(&combined); - if !tail.is_empty() { - self.utf8_carry.extend_from_slice(tail); - } - if bytes.is_empty() { - return; - } if let Some(t0) = self.recording_started_at { - let elapsed = t0.elapsed().as_secs_f64(); - self.recording_events - .push((elapsed, String::from_utf8_lossy(bytes).into_owned())); - } - if !self.is_alternate_screen() && is_binary_noise_chunk(bytes) { - let sanitized = sanitize_binary_noise_chunk(bytes); - let newline_count = sanitized.iter().filter(|&&b| b == b'\n').count(); - self.total_lines_scrolled = self.total_lines_scrolled.saturating_add(newline_count); - self.append_primary_stream_log(&sanitized); - self.csi_count_clamp.reset(); - self.parser.process(&sanitized); - return; - } - let was_alt = self.is_alternate_screen(); - let (start_row, _) = self.parser.screen().cursor_position(); - let vt_bytes = self.tektronix.process_and_filter_vt100(bytes); - let newline_count = vt_bytes.iter().filter(|&&b| b == b'\n').count(); - self.total_lines_scrolled = self.total_lines_scrolled.saturating_add(newline_count); - if !vt_bytes.is_empty() { - let normalized = normalize_extended_sgr_for_vt100(&vt_bytes); - // vt100-ctt does not clamp ICH / IL / SD counts; bound them to the grid first. - let (grid_rows, grid_cols) = self.parser.screen().size(); - let normalized = self.csi_count_clamp.filter(&normalized, grid_rows.max(grid_cols)); - self.parser.process(&normalized); - scan_dec_line_size_escapes( - &vt_bytes, - start_row, - self.rows, - &mut self.line_size_attrs, - ); - if !was_alt && !self.is_alternate_screen() { - self.append_primary_stream_log(&normalized); + let mut combined = std::mem::take(&mut self.utf8_carry); + combined.extend_from_slice(raw_bytes); + let (bytes, tail) = split_incomplete_utf8_tail(&combined); + self.utf8_carry.extend_from_slice(tail); + if !bytes.is_empty() { + let elapsed = t0.elapsed().as_secs_f64(); + self.recording_events + .push((elapsed, String::from_utf8_lossy(bytes).into_owned())); } } - let scan_bytes: std::borrow::Cow<'_, [u8]> = if self.osc52_carry.is_empty() { - std::borrow::Cow::Borrowed(bytes) - } else { - let mut combined = std::mem::take(&mut self.osc52_carry); - combined.extend_from_slice(bytes); - std::borrow::Cow::Owned(combined) - }; - self.scan_control_sequences(&scan_bytes); + let mut framed = std::mem::take(&mut self.framed); + let mut rest = raw_bytes; + while !rest.is_empty() { + // In-band Tektronix capture (`CSI ?38h`) has its own grammar; it bypasses the framer. + let used = if self.tektronix.stream_capture { + self.tektronix.consume_captured(rest) + } else { + let used = self.framer.feed(rest, &mut framed); + self.process_framed(&framed); + used + }; + rest = &rest[used..]; + } + self.framed = framed; } - fn append_primary_stream_log(&mut self, bytes: &[u8]) { + pub(super) fn append_primary_stream_log(&mut self, bytes: &[u8]) { const MAX_PRIMARY_LOG: usize = 65_536; self.primary_stream_log.extend_from_slice(bytes); if self.primary_stream_log.len() > MAX_PRIMARY_LOG { @@ -721,7 +698,19 @@ impl VtParser { pub fn is_report_color_scheme_2031(&self) -> bool { self.report_color_scheme_2031 } pub fn is_in_band_size_2048(&self) -> bool { self.in_band_size_2048 } pub fn is_backarrow_key_67(&self) -> bool { self.backarrow_key_67 } - pub fn osc52_carry_len(&self) -> usize { self.osc52_carry.len() } + /// Bytes of an incomplete control sequence currently withheld by the scanner (bounded). + pub fn osc52_carry_len(&self) -> usize { self.framer.carry_len() } + /// Input bytes the control-sequence scanner has consumed; each byte counts exactly once. + pub fn scanner_steps(&self) -> u64 { self.framer.steps() } + /// Control sequences dropped for exceeding the scanner's length limits. + pub fn scanner_dropped_sequences(&self) -> u64 { self.framer.dropped() } + /// Hands each complete, queued `OSC 1337;Canvas=` sequence to `apply` and clears the queue. + pub fn drain_canvas_osc(&mut self, mut apply: impl FnMut(&mut TektronixState, &[u8])) { + let queued = std::mem::take(&mut self.pending_canvas_osc); + if !queued.is_empty() { + apply(&mut self.tektronix, &queued); + } + } pub fn kitty_keyboard_flags(&self) -> u8 { let stack = if self.is_alternate_screen() { diff --git a/src/vt/scan.rs b/src/vt/scan.rs index 22665570..b599782b 100644 --- a/src/vt/scan.rs +++ b/src/vt/scan.rs @@ -1,9 +1,31 @@ -//! Single-pass VT control sequence scanner and CSI / extended SGR handlers (`ARCH-R04`, `P2.2`). +//! Framed-chunk dispatch to the VT engine and the CSI / extended SGR side handlers (`ARCH-R04`, `P2.2`). -use super::graphics::strip_apc_and_dcs_envelopes; -use super::snapshot::{RgbTriplet, UnderlineKind}; +use super::codec::{is_binary_noise_chunk, sanitize_binary_noise_chunk}; +use super::framer::{FrameOutput, SeqKind, StringTerminator}; +use super::snapshot::{LineSizeAttr, RgbTriplet, UnderlineKind}; use super::VtParser; +const CANVAS_OSC_PREFIX: &[u8] = b"1337;Canvas="; +/// Most bytes of `OSC 1337;Canvas=` sequences queued for the pane between drains. +const MAX_PENDING_CANVAS_OSC_BYTES: usize = 1024 * 1024; + +/// `true` for sequences whose handler reads engine state, so the engine must be caught up first: +/// cursor / mode reports (`CSI .. n`, `CSI .. $p`), alternate-screen switches (they select the +/// Kitty keyboard stack), graphics placements (`DCS`, `APC`) and `ESC` line-size / reset. +fn needs_engine_sync(kind: SeqKind, body: &[u8]) -> bool { + match kind { + SeqKind::Dcs | SeqKind::Apc | SeqKind::Esc => true, + SeqKind::Csi => match body.split_last() { + Some((b'n' | b'p', _)) => true, + Some((b'h' | b'l', params)) => params + .strip_prefix(b"?") + .is_some_and(|p| p.split(|&b| b == b';').any(|m| matches!(m, b"47" | b"1047" | b"1049"))), + _ => false, + }, + SeqKind::Osc | SeqKind::TekEnter => false, + } +} + impl VtParser { pub(super) fn record_escape_sequence(&mut self, seq: impl Into) { if self.recent_escape_sequences.len() >= 16 { @@ -12,31 +34,100 @@ impl VtParser { self.recent_escape_sequences.push(seq.into()); } - /// Scans raw bytes in a single `ESC` (`\x1b`) pass for `CSI` (`\x1b[`), `OSC` (`\x1b]`), - /// `DCS` (`\x1bP`), and `APC` (`\x1b_`) control sequences (`P2.2`). - pub(super) fn scan_control_sequences(&mut self, bytes: &[u8]) { - if !bytes.contains(&0x1b) { + /// Runs one framed chunk: engine bytes go to the VT engine, completed sequences to their + /// side handler. A handler that reads engine state (cursor, alternate screen, modes) runs + /// only after the engine has consumed everything up to and including its sequence, so the + /// result does not depend on what else shared the chunk. + pub(super) fn process_framed(&mut self, framed: &FrameOutput) { + let engine = framed.engine.as_slice(); + if !self.is_alternate_screen() && is_binary_noise_chunk(engine) { + // Random bytes must not trigger handlers; show them as visible glyphs instead. + let sanitized = sanitize_binary_noise_chunk(engine); + let newline_count = sanitized.iter().filter(|&&b| b == b'\n').count(); + self.total_lines_scrolled = self.total_lines_scrolled.saturating_add(newline_count); + self.append_primary_stream_log(&sanitized); + self.csi_count_clamp.reset(); + self.parser.process(&sanitized); return; } - let text = String::from_utf8_lossy(bytes); - for (idx, _) in text.match_indices('\x1b') { - let after_esc = &text[idx + 1..]; - let Some(&kind) = after_esc.as_bytes().first() else { - self.osc52_carry.clear(); - self.osc52_carry.push(0x1b); - break; - }; - // `kind` may be the lead byte of a multi-byte char (`ESC é`): not a control introducer. - let Some(payload_rest) = after_esc.get(1..) else { - continue; - }; - match kind { - b'[' => self.scan_csi_sequence(payload_rest), - b']' => self.scan_osc_sequence(payload_rest), - b'P' => self.scan_dcs_sequence(payload_rest), - b'_' => self.scan_apc_sequence(payload_rest), - _ => {} + let mut fed = 0; + for seq in &framed.seqs { + let body = framed.body(seq); + if needs_engine_sync(seq.kind, body) { + self.feed_engine(&engine[fed..seq.engine_end]); + fed = seq.engine_end; + } + self.dispatch_sequence(seq.kind, seq.terminator, body); + } + self.feed_engine(&engine[fed..]); + } + + fn feed_engine(&mut self, bytes: &[u8]) { + if bytes.is_empty() { + return; + } + let was_alt = self.is_alternate_screen(); + let newline_count = bytes.iter().filter(|&&b| b == b'\n').count(); + self.total_lines_scrolled = self.total_lines_scrolled.saturating_add(newline_count); + let normalized = normalize_extended_sgr_for_vt100(bytes); + // vt100-ctt does not clamp ICH / IL / SD counts; bound them to the grid first. + let (grid_rows, grid_cols) = self.parser.screen().size(); + let normalized = self.csi_count_clamp.filter(&normalized, grid_rows.max(grid_cols)); + self.parser.process(&normalized); + if !was_alt && !self.is_alternate_screen() { + self.append_primary_stream_log(&normalized); + } + } + + fn dispatch_sequence(&mut self, kind: SeqKind, terminator: StringTerminator, body: &[u8]) { + match kind { + SeqKind::Csi => { + // Always ASCII: the framer drops non-ASCII bytes inside a CSI, as the engine does. + if let Ok(csi) = std::str::from_utf8(body) { + self.scan_csi_sequence(csi); + } + } + SeqKind::Osc => { + if body.starts_with(CANVAS_OSC_PREFIX) { + self.queue_canvas_osc(body); + } else { + self.scan_osc_sequence(&String::from_utf8_lossy(body), terminator); + } + } + SeqKind::Dcs => self.scan_dcs_sequence(&String::from_utf8_lossy(body)), + SeqKind::Apc => self.scan_apc_sequence(&String::from_utf8_lossy(body)), + SeqKind::Esc => self.apply_esc_sequence(body), + SeqKind::TekEnter => self.tektronix.enter_stream_capture(), + } + } + + /// `ESC # 3..6` (DEC line size, applied to the cursor row) and `ESC c` (RIS). + fn apply_esc_sequence(&mut self, body: &[u8]) { + let (row, _) = self.parser.screen().cursor_position(); + match body { + b"#3" => { + self.line_size_attrs.insert(row, LineSizeAttr::DoubleHeightTop); + } + b"#4" => { + self.line_size_attrs.insert(row, LineSizeAttr::DoubleHeightBottom); + } + b"#5" => { + self.line_size_attrs.remove(&row); } + b"#6" => { + self.line_size_attrs.insert(row, LineSizeAttr::DoubleWidth); + } + b"c" => self.line_size_attrs.clear(), + _ => {} + } + } + + /// Queues a complete `OSC 1337;Canvas=` sequence for the pane; dropped once the queue is full. + fn queue_canvas_osc(&mut self, body: &[u8]) { + if self.pending_canvas_osc.len() + body.len() + 3 <= MAX_PENDING_CANVAS_OSC_BYTES { + self.pending_canvas_osc.extend_from_slice(b"\x1b]"); + self.pending_canvas_osc.extend_from_slice(body); + self.pending_canvas_osc.push(0x07); } } @@ -159,8 +250,12 @@ impl VtParser { } } - /// Dispatches a single `CSI` (`\x1b[...`) sequence starting at `csi_rest` (`&text[idx + 2..]`). + /// Dispatches one complete `CSI`; `csi_rest` is its parameter, intermediate and final bytes. fn scan_csi_sequence(&mut self, csi_rest: &str) { + if matches!(csi_rest, "2J" | "3J") { + self.line_size_attrs.clear(); + return; + } if let Some(rest) = csi_rest.strip_prefix('?') { let param_len = rest .chars() @@ -437,19 +532,14 @@ pub(super) fn split_incomplete_utf8_tail(bytes: &[u8]) -> (&[u8], &[u8]) { } /// Rewrites extended SGR sub-parameters (`4:1..4:5` -> `4`, `4:0` -> `24`, and strips `58;2;r;g;b` / `58:2:...`) -/// and strips APC (`\x1b_...`) / DCS (`\x1bP...`) envelopes so `vt100_ctt::Parser` applies standard attributes -/// without dumping raw Kitty/Sixel graphics payloads onto the character grid. +/// so `vt100_ctt::Parser` applies standard attributes. Input is whole sequences (the framer withholds +/// partial ones and removes APC / DCS envelopes before this stage). pub(super) fn normalize_extended_sgr_for_vt100(bytes: &[u8]) -> Vec { let raw_cow = std::str::from_utf8(bytes) .map(std::borrow::Cow::Borrowed) .unwrap_or_else(|_| String::from_utf8_lossy(bytes)); let raw_text = raw_cow.as_ref(); - let stripped_cow = if raw_text.contains("\x1b_") || raw_text.contains("\x1bP") { - std::borrow::Cow::Owned(strip_apc_and_dcs_envelopes(raw_text)) - } else { - std::borrow::Cow::Borrowed(raw_text) - }; - let text = stripped_cow.as_ref(); + let text = raw_text; if !text.contains("\x1b[") || (!text.contains("4:") && !text.contains("58") && !text.contains("38:") && !text.contains("48:")) { diff --git a/src/vt/scan_osc_dcs.rs b/src/vt/scan_osc_dcs.rs index cdd13c91..1ea85933 100644 --- a/src/vt/scan_osc_dcs.rs +++ b/src/vt/scan_osc_dcs.rs @@ -1,4 +1,4 @@ -//! Single-pass OSC, DCS, and APC control sequence handlers (`ARCH-R04`, `P2.2`). +//! OSC, DCS, and APC handlers; each receives one complete sequence body from the framer (`ARCH-R04`, `P2.2`). use std::path::PathBuf; use std::time::Instant; @@ -8,6 +8,7 @@ use super::codec::{ truncate_on_char_boundary, }; use super::detect::Osc8Hyperlink; +use super::framer::StringTerminator; use super::graphics::{ GraphicProtocol, MAX_GRAPHIC_PLACEMENTS, MAX_KITTY_PAYLOAD_B64_BYTES, TerminalGraphicPlacement, decode_sixel_placement, expand_kitty_payload_to_rgba, @@ -28,16 +29,14 @@ const MAX_NOTIFICATION_TITLE_BYTES: usize = 256; const MAX_NOTIFICATION_BODY_BYTES: usize = 1024; impl VtParser { - /// Dispatches a single `OSC` (`\x1b]...`) sequence starting at `osc_rest` (`&text[idx + 2..]`). - pub(super) fn scan_osc_sequence(&mut self, osc_rest: &str) { + /// Dispatches one complete `OSC`; `osc_rest` is its body (between `ESC ]` and the terminator). + pub(super) fn scan_osc_sequence(&mut self, osc_rest: &str, terminator: StringTerminator) { if let Some(rest) = osc_rest .strip_prefix("0;") .or_else(|| osc_rest.strip_prefix("2;")) { - if let Some(end) = rest.find('\x07').or_else(|| rest.find("\x1b\\")) { - let title = truncate_on_char_boundary(&rest[..end], MAX_WINDOW_TITLE_BYTES); - self.window_title = Some(title.to_string()); - } + let title = truncate_on_char_boundary(rest, MAX_WINDOW_TITLE_BYTES); + self.window_title = Some(title.to_string()); return; } @@ -47,7 +46,7 @@ impl VtParser { ("12;?", 12u8, self.theme_cursor_rgb), ] { if let Some(rest) = osc_rest.strip_prefix(prefix) { - if rest.starts_with('\x07') || rest.starts_with("\x1b\\") { + if rest.is_empty() { let reply = format_osc_color_reply(code, None, rgb); self.pending_query_responses.push(reply.into_bytes()); } @@ -55,168 +54,116 @@ impl VtParser { } } - if let Some(rest) = osc_rest.strip_prefix("4;") { - if let Some(end) = rest.find('\x07').or_else(|| rest.find("\x1b\\")) { - let body = &rest[..end]; - if let Some((idx_str, spec)) = body.split_once(';') - && spec.trim() == "?" - && let Ok(palette_idx) = idx_str.trim().parse::() - { - let rgb = ansi_256_index_to_rgb(palette_idx); - let reply = format_osc_color_reply(4, Some(palette_idx), rgb); - self.pending_query_responses.push(reply.into_bytes()); - } + if let Some(body) = osc_rest.strip_prefix("4;") { + if let Some((idx_str, spec)) = body.split_once(';') + && spec.trim() == "?" + && let Ok(palette_idx) = idx_str.trim().parse::() + { + let rgb = ansi_256_index_to_rgb(palette_idx); + let reply = format_osc_color_reply(4, Some(palette_idx), rgb); + self.pending_query_responses.push(reply.into_bytes()); } return; } if let Some(rest) = osc_rest.strip_prefix("7;") { - if let Some(end) = rest.find('\x07').or_else(|| rest.find("\x1b\\")) { - let raw_uri = rest[..end].trim(); - let path_part = if let Some(after_scheme) = raw_uri.strip_prefix("file://") { - if let Some(slash_idx) = after_scheme.find('/') { - &after_scheme[slash_idx..] - } else { - after_scheme - } + let raw_uri = rest.trim(); + let path_part = if let Some(after_scheme) = raw_uri.strip_prefix("file://") { + if let Some(slash_idx) = after_scheme.find('/') { + &after_scheme[slash_idx..] } else { - raw_uri - }; - if !path_part.is_empty() { - self.reported_cwd = Some(PathBuf::from(path_part)); + after_scheme } + } else { + raw_uri + }; + if !path_part.is_empty() { + self.reported_cwd = Some(PathBuf::from(path_part)); } return; } - if let Some(rest) = osc_rest.strip_prefix("8;") { - if let Some(end) = rest.find('\x07').or_else(|| rest.find("\x1b\\")) { - let payload = &rest[..end]; - if let Some((params, uri)) = payload.split_once(';') - && !uri.is_empty() - && uri.len() <= MAX_OSC8_URI_BYTES - { - let id = params - .split(':') - .find_map(|kv| kv.strip_prefix("id=")) - .map(|id| truncate_on_char_boundary(id, MAX_OSC8_ID_BYTES).to_string()); - push_bounded( - &mut self.osc8_links, - Osc8Hyperlink { - id, - uri: uri.to_string(), - }, - MAX_OSC8_LINKS, - ); - self.record_escape_sequence(format!("OSC 8 {uri}")); - } + if let Some(payload) = osc_rest.strip_prefix("8;") { + if let Some((params, uri)) = payload.split_once(';') + && !uri.is_empty() + && uri.len() <= MAX_OSC8_URI_BYTES + { + let id = params + .split(':') + .find_map(|kv| kv.strip_prefix("id=")) + .map(|id| truncate_on_char_boundary(id, MAX_OSC8_ID_BYTES).to_string()); + push_bounded( + &mut self.osc8_links, + Osc8Hyperlink { + id, + uri: uri.to_string(), + }, + MAX_OSC8_LINKS, + ); + self.record_escape_sequence(format!("OSC 8 {uri}")); } return; } if let Some(rest) = osc_rest.strip_prefix("9;4;") { - if let Some(end) = rest.find('\x07').or_else(|| rest.find("\x1b\\")) { - let payload = rest[..end].trim(); - let mut parts = payload.split(';'); - let state = parts - .next() - .and_then(|s| s.trim().parse::().ok()) - .unwrap_or(0); - let pct = parts - .next() - .and_then(|s| s.trim().parse::().ok()) - .unwrap_or(0) - .min(100); - self.osc9_progress = match state { - 0 => None, - 1 => Some(Osc9Progress::Normal(pct)), - 2 => Some(Osc9Progress::Error(pct)), - 3 => Some(Osc9Progress::Indeterminate), - 4 => Some(Osc9Progress::Paused(pct)), - _ => self.osc9_progress, - }; - self.record_escape_sequence(format!("OSC 9;4;{payload}")); - } + let payload = rest.trim(); + let mut parts = payload.split(';'); + let state = parts + .next() + .and_then(|s| s.trim().parse::().ok()) + .unwrap_or(0); + let pct = parts + .next() + .and_then(|s| s.trim().parse::().ok()) + .unwrap_or(0) + .min(100); + self.osc9_progress = match state { + 0 => None, + 1 => Some(Osc9Progress::Normal(pct)), + 2 => Some(Osc9Progress::Error(pct)), + 3 => Some(Osc9Progress::Indeterminate), + 4 => Some(Osc9Progress::Paused(pct)), + _ => self.osc9_progress, + }; + self.record_escape_sequence(format!("OSC 9;4;{payload}")); return; } if let Some(rest) = osc_rest.strip_prefix("9;") { - if let Some(end) = rest.find('\x07').or_else(|| rest.find("\x1b\\")) { - let msg = truncate_on_char_boundary(rest[..end].trim(), MAX_NOTIFICATION_BODY_BYTES); - if !msg.is_empty() { - self.push_notification(TerminalNotification { - title: "OSC 9".to_string(), - body: msg.to_string(), - }); - self.unread_bell = true; - self.record_escape_sequence(format!("OSC 9 Notify: {msg}")); - } - } - return; - } - - if let Some(rest) = osc_rest.strip_prefix("777;notify;") { - if let Some(end) = rest.find('\x07').or_else(|| rest.find("\x1b\\")) { - let payload = &rest[..end]; - let (title, body) = payload.split_once(';').unwrap_or(("NOTIFY", payload)); - let title = truncate_on_char_boundary(title.trim(), MAX_NOTIFICATION_TITLE_BYTES); - let body = truncate_on_char_boundary(body.trim(), MAX_NOTIFICATION_BODY_BYTES); + let msg = truncate_on_char_boundary(rest.trim(), MAX_NOTIFICATION_BODY_BYTES); + if !msg.is_empty() { self.push_notification(TerminalNotification { - title: title.trim().to_string(), - body: body.trim().to_string(), + title: "OSC 9".to_string(), + body: msg.to_string(), }); self.unread_bell = true; - self.record_escape_sequence(format!("OSC 777 Notify: {title}")); + self.record_escape_sequence(format!("OSC 9 Notify: {msg}")); } return; } - if "52;".starts_with(osc_rest) && osc_rest.len() < 3 { - self.osc52_carry.clear(); - self.osc52_carry.extend_from_slice(b"\x1b]"); - self.osc52_carry.extend_from_slice(osc_rest.as_bytes()); + if let Some(payload) = osc_rest.strip_prefix("777;notify;") { + let (title, body) = payload.split_once(';').unwrap_or(("NOTIFY", payload)); + let title = truncate_on_char_boundary(title.trim(), MAX_NOTIFICATION_TITLE_BYTES); + let body = truncate_on_char_boundary(body.trim(), MAX_NOTIFICATION_BODY_BYTES); + self.push_notification(TerminalNotification { + title: title.trim().to_string(), + body: body.trim().to_string(), + }); + self.unread_bell = true; + self.record_escape_sequence(format!("OSC 777 Notify: {title}")); return; } if osc_rest.starts_with("52;") { - // Terminate at the first BEL or ESC of *this* OSC; an ESC that does not begin ST - // aborts the sequence so no foreign escapes can ride along to the host terminal. - if let Some(end) = osc_rest.find(['\x07', '\x1b']) { - let rest_from_end = &osc_rest[end..]; - if rest_from_end == "\x1b" { - self.osc52_carry.clear(); - self.osc52_carry.extend_from_slice(b"\x1b]"); - self.osc52_carry.extend_from_slice(osc_rest.as_bytes()); - return; - } - let terminator = match rest_from_end { - t if t.starts_with('\x07') => "\x07", - t if t.starts_with("\x1b\\") => "\x1b\\", - _ => { - self.osc52_carry.clear(); - return; - } - }; - let body = &osc_rest[..end]; - if is_forwardable_osc52_write(body) { - self.pending_osc52.push(format!("\x1b]{body}{terminator}")); - if let Some(decoded) = decode_osc52_payload(body) { - self.pending_osc52_decoded.push(decoded); - } - } - self.osc52_carry.clear(); - } else { - const MAX_CARRY: usize = super::codec::MAX_OSC52_PAYLOAD_BYTES + 64; - if osc_rest.len() <= MAX_CARRY { - if osc_rest.contains('\x1b') { - self.osc52_carry.clear(); - } else { - self.osc52_carry.clear(); - self.osc52_carry.extend_from_slice(b"\x1b]"); - self.osc52_carry.extend_from_slice(osc_rest.as_bytes()); - } - } else { - self.osc52_carry.clear(); + // The framer completes an OSC only at BEL or ST: an ESC that does not begin ST cancels + // it, so no foreign escape can ride along to the host terminal. The forwarded string + // is rebuilt from the validated body, never copied from the stream. + if is_forwardable_osc52_write(osc_rest) { + self.pending_osc52 + .push(format!("\x1b]{osc_rest}{}", terminator.as_str())); + if let Some(decoded) = decode_osc52_payload(osc_rest) { + self.pending_osc52_decoded.push(decoded); } } return; @@ -265,12 +212,8 @@ impl VtParser { } } - /// Dispatches a single `DCS` (`\x1bP...`) sequence starting at `dcs_rest` (`&text[idx + 2..]`). - pub(super) fn scan_dcs_sequence(&mut self, dcs_rest: &str) { - let Some(end) = dcs_rest.find("\x1b\\").or_else(|| dcs_rest.find('\x07')) else { - return; - }; - let dcs_body = &dcs_rest[..end]; + /// Dispatches one complete `DCS`; `dcs_body` is its body (between `ESC P` and the terminator). + pub(super) fn scan_dcs_sequence(&mut self, dcs_body: &str) { if let Some(query_body) = dcs_body.strip_prefix("+q") { for hex_cap in query_body.split(';') { @@ -328,15 +271,11 @@ impl VtParser { } } - /// Dispatches a single `APC` (`\x1b_...`) sequence starting at `apc_rest` (`&text[idx + 2..]`). - pub(super) fn scan_apc_sequence(&mut self, apc_rest: &str) { - let Some(rest) = apc_rest.strip_prefix('G') else { - return; - }; - let Some(end) = rest.find("\x1b\\").or_else(|| rest.find('\x07')) else { + /// Dispatches one complete `APC`; `apc_body` is its body (between `ESC _` and the terminator). + pub(super) fn scan_apc_sequence(&mut self, apc_body: &str) { + let Some(body) = apc_body.strip_prefix('G') else { return; }; - let body = &rest[..end]; let (ctrl_str, b64_payload) = body.split_once(';').unwrap_or((body, "")); let mut action = 'T'; let mut image_id: Option = None; diff --git a/src/vt/tektronix.rs b/src/vt/tektronix.rs index 5248ec79..726a060e 100644 --- a/src/vt/tektronix.rs +++ b/src/vt/tektronix.rs @@ -270,6 +270,24 @@ impl TektronixState { vt_out } + /// Feeds `bytes` to the state machine while in-band stream capture is active and returns how + /// many it consumed; capture ends at `CSI ?38l` or `ESC ETX`, leaving the rest for the VT path. + pub(super) fn consume_captured(&mut self, bytes: &[u8]) -> usize { + let mut used = 0; + while used < bytes.len() && self.stream_capture { + self.process_byte(bytes[used]); + used += 1; + } + used + } + + /// Enters in-band stream capture (`CSI ?38h`). + pub(super) fn enter_stream_capture(&mut self) { + self.active = true; + self.stream_capture = true; + self.mode = TekMode::Alpha; + } + fn process_byte(&mut self, b: u8) { if self.csi_pending { if self.csi_buf.len() < MAX_TEK_CSI_BUF { diff --git a/tests/vt_split_invariance.rs b/tests/vt_split_invariance.rs new file mode 100644 index 00000000..087ac9d2 --- /dev/null +++ b/tests/vt_split_invariance.rs @@ -0,0 +1,356 @@ +//! The control-sequence scanner is stateful: however PTY output is chunked, the terminal ends up +//! in the same state and emits the same events as when the bytes arrive in one piece. +//! +//! Covers split invariance over a corpus of real and malformed sequences (deterministic seed), +//! one regression test per bug the stateless scanner had, and linear-time evidence counted in +//! scanner steps (no wall-clock assertions). + +use bilterm::vt::snapshot::UnderlineKind; +use bilterm::vt::{LineSizeAttr, VtParser}; +use bilterm::wm::WindowManager; + +const ROWS: u16 = 6; +const COLS: u16 = 24; + +fn vt() -> VtParser { + VtParser::new(ROWS, COLS, 50) +} + +/// Everything observable about a parser except wall-clock values and the per-call generation. +fn fingerprint(vt: &mut VtParser) -> String { + let mut snap = vt.snapshot(); + snap.generation = 0; + let history: Vec> = + (0..vt.total_available_rows()).map(|r| vt.abs_row_text(r)).collect(); + let modes = ( + vt.is_alternate_screen(), + vt.is_bracketed_paste(), + vt.is_application_cursor(), + vt.mouse_report_level(), + vt.is_mouse_sgr_or_tracking(), + vt.is_focus_reporting(), + vt.is_synchronized_output(), + vt.is_mouse_alternate_scroll(), + vt.is_grapheme_cluster_2027(), + vt.is_report_color_scheme_2031(), + vt.is_in_band_size_2048(), + vt.is_backarrow_key_67(), + ); + let style = ( + vt.kitty_keyboard_flags(), + vt.active_underline_kind(), + vt.active_underline_color(), + vt.is_overline(), + ); + let events = ( + vt.window_title().map(str::to_owned), + vt.reported_cwd().map(std::path::Path::to_path_buf), + vt.osc133_phase(), + vt.last_exit_code(), + vt.osc9_progress(), + vt.notifications().to_vec(), + vt.notifications_received(), + vt.unread_bell(), + vt.osc8_links().to_vec(), + vt.graphic_placements().to_vec(), + vt.recent_escape_sequences().to_vec(), + ); + let outbound = ( + vt.take_query_responses(), + vt.take_outbound_osc52(), + vt.take_osc52_decoded(), + vt.tektronix().stream_capture, + vt.total_bytes_processed(), + vt.scanner_dropped_sequences(), + ); + format!("{snap:?}\n{history:?}\n{modes:?}\n{style:?}\n{events:?}\n{outbound:?}") +} + +fn whole(input: &[u8]) -> String { + let mut vt = vt(); + vt.process(input); + fingerprint(&mut vt) +} + +fn chunked(input: &[u8], sizes: impl Iterator) -> String { + let mut vt = vt(); + let mut rest = input; + for size in sizes { + if rest.is_empty() { + break; + } + let (head, tail) = rest.split_at(size.clamp(1, rest.len())); + vt.process(head); + rest = tail; + } + vt.process(rest); + fingerprint(&mut vt) +} + +struct XorShift(u64); + +impl XorShift { + fn next(&mut self) -> u64 { + self.0 ^= self.0 << 13; + self.0 ^= self.0 >> 7; + self.0 ^= self.0 << 17; + self.0 + } + fn below(&mut self, n: usize) -> usize { + (self.next() % n as u64) as usize + } +} + +/// Well-formed sequences as real programs emit them, plus malformed ones. All valid UTF-8. +fn corpus() -> Vec> { + let items: &[&[u8]] = &[ + b"plain text\r\n", + "h\u{e9}llo \u{4e16}\u{754c} \u{276f} \u{1f389} caf\u{e9}\r\n".as_bytes(), + "c1 as text \u{9b}31m \u{9d}0;x\u{9c} \u{90}q\r\n".as_bytes(), + b"\x1b[31mred\x1b[0m \x1b[1;4mbold-ul\x1b[m\r\n", + b"\x1b[4:3mcurly\x1b[58:2::255:0:0mcol\x1b[4:0m\x1b[59m ", + b"\x1b[38:2::10:20:30mfg\x1b[48;2;1;2;3mbg\x1b[38;5;42m256\x1b[0m\x1b[53mover\x1b[55m", + b"\x1b[2;3Hmoved\x1b[K\x1b[1;1H\x1b[2Kline\x1b[3B\x1b[5C*", + b"\x1b[?1049h\x1b[Halt screen\x1b[>7u\x1b[?u\x1b[c\x1b[>q\x1b[?996n", + b"\x1b[?2004$p\x1b[?25$p\x1b[?1049$p\x1b[4$p\x1b[?9999$p", + b"\x1b[>5u\x1b[=3;2u\x1b[?u\x1b[>1u\x1b[<2u\x1b[?u", + "\x1b]0;t\u{ef}tle \u{4e16}\x07after-title ".as_bytes(), + b"\x1b]2;st title\x1b\\ following text\x07", + b"\x1b]4;1;?\x07\x1b]10;?\x1b\\\x1b]11;?\x07\x1b]12;?\x07", + b"\x1b]7;file://host/home/u/proj\x07\x1b]8;id=a;https://example.com/x\x1b\\link\x1b]8;;\x1b\\", + b"\x1b]9;4;1;50\x07\x1b]9;build done\x07\x1b]777;notify;Title;Body text\x1b\\", + b"\x1b]52;c;aGVsbG8gd29ybGQ=\x07\x1b]52;p;Zm9v\x1b\\\x1b]52;c;?\x07", + b"\x1b]133;A\x07$ \x1b]133;B\x07ls\r\n\x1b]133;C\x07out\r\n\x1b]133;D;3\x07", + b"\x1b]1337;Canvas=line 1 1 30 15\x07\x1b]1337;SetMark\x07", + b"\x1bP+q544e;436f;7a7a\x1b\\\x1bP$qm\x1b\\\x1bP$qr\x1b\\\x1bP$q q\x1b\\\x1bP$qzz\x1b\\", + b"ab\x1bP0;1;q\"1;1;4;6#1;2;100;0;0#1~~~~-~~~~\x1b\\cd", + b"x\x1b_Ga=T,f=24,s=2,v=2,i=7;AAAAAAAAAAAAAAAA\x1b\\y\x1b_Ga=q,i=9;AAAA\x1b\\z", + b"\x1b_Ga=d,i=7\x1b\\\x1b^privacy message\x1b\\\x1bXsos\x1b\\ok", + b"\x1b#3TOP\r\n\x1b#4BOTTOM\r\n\x1b#6WIDE\r\n\x1b#5\x1b7\x1b8\x1bM\x1b(B", + b"\x1bPtmux;\x1b\x1b]52;c;dG11eA==\x07\x1b\\after", + // Malformed / hostile shapes. + b"lone \x1b", + b"\x1b[31\x18cancelled \x1b[4\x1asubbed \x1b[1\x1b[32mrestart", + b"\x1b[3\n1mC0 inside CSI\x1b\r[0m", + b"\x1b]0;aborted\x1b[7mby csi\x1b[0m \x1b]0;can\x18celled \x1b]52;c;QQ==\x1b[2J\x07", + b"\x1bP$qm\x07bel-dcs \x1bPq#1~~\x1bXnested\x1b\\ \x1b_Gbroken\x18x", + "\x1b\u{e9}[1mESC then non-ascii\x1b[\u{4e16}0m".as_bytes(), + b"\x1b[?1;?2h\x1b[1 2m\x1b[ q\x1b[?2004\x1b\\h", + b"\x1b]\x07\x1b[\x1b]\x1b\\\x1bP\x1b\\\x1b_\x07\x1b[m", + ]; + let mut out: Vec> = items.iter().map(|i| i.to_vec()).collect(); + // An over-long CSI (dropped) and a long, valid SGR. + out.push([b"\x1b[".as_slice(), "1;".repeat(200).as_bytes(), b"mlong"].concat()); + out.push([b"\x1b[".as_slice(), "38;5;1;".repeat(20).as_bytes(), b"mok"].concat()); + out +} + +#[test] +fn every_corpus_item_is_split_invariant_at_every_boundary() { + for (n, item) in corpus().iter().enumerate() { + let expected = whole(item); + assert_eq!(chunked(item, std::iter::repeat(1)), expected, "item {n}: 1 byte at a time"); + for cut in 1..item.len() { + assert_eq!(chunked(item, std::iter::once(cut)), expected, "item {n}: split at {cut}"); + } + } +} + +#[test] +fn generated_streams_are_split_invariant_under_random_chunking() { + let corpus = corpus(); + let mut rng = XorShift(0x9e37_79b9_7f4a_7c15); + for stream_no in 0..40 { + let mut stream = Vec::new(); + for _ in 0..30 { + stream.extend_from_slice(&corpus[rng.below(corpus.len())]); + if rng.below(3) == 0 { + stream.extend_from_slice(format!("t{}\u{276f}\r\n", rng.below(1000)).as_bytes()); + } + } + let expected = whole(&stream); + assert_eq!(chunked(&stream, std::iter::repeat(1)), expected, "stream {stream_no}: bytes"); + for max in [2usize, 7, 64, 1024] { + let mut sizes = XorShift(rng.next() | 1); + let got = chunked(&stream, std::iter::repeat_with(|| 1 + sizes.below(max))); + assert_eq!(got, expected, "stream {stream_no}: random chunks up to {max}"); + } + } +} + +/// Raw 8-bit C1 bytes and other invalid UTF-8: never introducers, never a panic, same result. +/// (Fewer than two binary C0 bytes each, so the per-chunk binary-noise heuristic stays out.) +#[test] +fn invalid_utf8_and_raw_c1_bytes_are_split_invariant() { + let items: &[&[u8]] = &[ + b"a\x9b31mb\x9d0;title\x9c\x90q\x9cz", + b"\xe2\x1b[1mbold\xe2\x9d\x1b[0m\xf0\x9f\x1b]0;t\xe2\x07x\xff", + b"\x1b]0;caf\xc3\x07 \x1b]9;\xe2\x9d\xaf\xe2\x1b\\ \x1bP$q\xffm\x1b\\", + b"\x1b[\xff3\x801m\xc3 red \x1b\xc3\xa9[0m", + ]; + for (n, item) in items.iter().enumerate() { + let expected = whole(item); + assert_eq!(chunked(item, std::iter::repeat(1)), expected, "item {n}: 1 byte at a time"); + for cut in 1..item.len() { + assert_eq!(chunked(item, std::iter::once(cut)), expected, "item {n}: split at {cut}"); + } + } + let mut vt = vt(); + vt.process(b"a\x9b31mb"); + assert_eq!(vt.snapshot().row_text(0).trim_end().chars().last(), Some('b')); + assert!(vt.snapshot().row_text(0).contains("31m"), "0x9b must not act as CSI"); +} + +// ── Regression tests: each failed with the stateless per-chunk scanner ──────────────────────── + +fn feed(parts: &[&[u8]]) -> VtParser { + let mut vt = vt(); + for part in parts { + vt.process(part); + } + vt +} + +#[test] +fn split_csi_mode_changes_are_not_lost() { + let vt = feed(&[b"\x1b[?10", b"04h\x1b[>", b"5u"]); + assert!(vt.is_focus_reporting(), "CSI ?1004h split across reads"); + assert_eq!(vt.kitty_keyboard_flags(), 5, "CSI >5u split across reads"); +} + +#[test] +fn split_osc_title_cwd_and_exit_code_are_not_lost() { + let vt = feed(&[b"\x1b]0;hel", b"lo\x07\x1b]7;file://h/tm", b"p\x1b", b"\\\x1b]133;D;", b"7\x07"]); + assert_eq!(vt.window_title(), Some("hello")); + assert_eq!(vt.reported_cwd(), Some(std::path::Path::new("/tmp"))); + assert_eq!(vt.last_exit_code(), Some(7)); +} + +#[test] +fn osc_ends_at_its_own_terminator_not_a_later_bel() { + let vt = feed(&[b"\x1b]0;one\x1b\\ text \x1b]9;note\x07"]); + assert_eq!(vt.window_title(), Some("one")); + assert_eq!(vt.notifications().len(), 1); + assert_eq!(vt.notifications()[0].body, "note"); +} + +#[test] +fn split_extended_sgr_is_tracked() { + let vt = feed(&[b"\x1b[4:", b"3mcurly"]); + assert_eq!(vt.active_underline_kind(), UnderlineKind::Curly); +} + +#[test] +fn split_graphics_payloads_place_an_image_and_print_nothing() { + let vt = feed(&[b"\x1bP0;1;q\"1;1;4;6#1;2;100;0;0#1~~", b"~~-~~~~\x1b", b"\\"]); + assert_eq!(vt.graphic_placements().len(), 1, "sixel split across reads"); + assert_eq!(vt.snapshot().contents().trim(), "", "sixel payload must not reach the grid"); + + let mut vt = feed(&[b"\x1b_Ga=T,f=24,s=2,v=2,i=7;AAAAAAAA", b"AAAAAAAA\x1b\\"]); + assert_eq!(vt.graphic_placements().len(), 1, "kitty image split across reads"); + assert_eq!(vt.snapshot().contents().trim(), "", "kitty payload must not reach the grid"); + assert_eq!(vt.take_query_responses(), vec![b"\x1b_Gi=7;OK\x1b\\".to_vec()]); +} + +#[test] +fn cursor_report_uses_the_position_at_the_query() { + let mut vt = feed(&[b"\x1b[6nabc"]); + assert_eq!(vt.take_query_responses(), vec![b"\x1b[1;1R".to_vec()]); +} + +#[test] +fn split_dec_line_size_escape_is_applied_to_the_cursor_row() { + let vt = feed(&[b"one\r\n\x1b#", b"3TOP"]); + assert_eq!(vt.line_size_attr(1), LineSizeAttr::DoubleHeightTop); + assert_eq!(vt.line_size_attr(0), LineSizeAttr::default()); +} + +#[test] +fn tektronix_entry_neither_eats_earlier_sequences_nor_needs_one_read() { + let vt = feed(&[b"\x1b[2Jx\x1b[?38h"]); + assert!(vt.tektronix().stream_capture); + assert_eq!(vt.snapshot().contents().trim(), "x", "CSI 2J must not print its final byte"); + + let vt = feed(&[b"\x1b[?3", b"8h\x1dAB", b"\x1b[?38", b"lback"]); + assert!(!vt.tektronix().stream_capture, "capture ended by a split CSI ?38l"); + assert_eq!(vt.snapshot().contents().trim(), "back"); +} + +#[test] +fn split_canvas_osc_reaches_the_pane() { + let mut wm = WindowManager::new_headless(24, 80); + wm.active_pane_mut().process_pty_output(b"\x1b]1337;Canvas=line 1 1"); + assert_eq!(wm.active_pane().canvas_scene().primitives.len(), 0); + wm.active_pane_mut().process_pty_output(b" 30 15\x07"); + assert_eq!(wm.active_pane().canvas_scene().primitives.len(), 1); +} + +#[test] +fn osc52_is_forwarded_once_for_every_split_and_aborted_by_a_bare_esc() { + let seq = b"\x1b]52;c;aGVsbG8=\x1b\\"; + for cut in 1..seq.len() { + let mut vt = feed(&[&seq[..cut], &seq[cut..]]); + assert_eq!(vt.take_outbound_osc52(), vec!["\x1b]52;c;aGVsbG8=\x1b\\".to_string()], "cut {cut}"); + assert_eq!(vt.take_osc52_decoded(), vec!["hello".to_string()]); + } + let mut vt = feed(&[b"\x1b]52;c;QQ==", b"\x1b", b"[2J\x07"]); + assert!(vt.take_outbound_osc52().is_empty(), "ESC that is not ST aborts the OSC"); +} + +// ── Bounded carry and linear time ──────────────────────────────────────────────────────────── + +#[test] +fn unterminated_sequences_have_a_bounded_carry_and_are_dropped_whole() { + const OSC_LIMIT: usize = 1024 * 1024 + 64; + let mut vt = vt(); + vt.process(b"\x1b]0;"); + let block = [b'A'; 4096]; + let mut peak = 0; + for _ in 0..600 { + vt.process(&block); + peak = peak.max(vt.osc52_carry_len()); + } + assert!(peak <= OSC_LIMIT, "carry peaked at {peak}"); + assert_eq!(vt.osc52_carry_len(), 0, "over-long OSC is dropped, not kept"); + assert_eq!(vt.scanner_dropped_sequences(), 1); + vt.process(b"tail\x07shown"); + assert_eq!(vt.window_title(), None, "no title from an over-long OSC"); + assert_eq!(vt.snapshot().contents().trim(), "shown"); + + // DCS / APC: same rule with the graphics limit; nothing is printed. + let mut vt = self::vt(); + vt.process(b"\x1bPq"); + for _ in 0..1100 { + vt.process(&block); + } + assert_eq!(vt.osc52_carry_len(), 0); + vt.process(b"\x1b\\after"); + assert_eq!(vt.graphic_placements().len(), 0); + assert_eq!(vt.snapshot().contents().trim(), "after"); +} + +/// The stateless scanner re-read the whole carried OSC 52 on every chunk (quadratic). The framer +/// consumes each byte exactly once, whatever the chunking. +#[test] +fn scanner_steps_equal_input_bytes_for_long_unterminated_sequences() { + let mut data = b"\x1b]52;c;".to_vec(); + data.resize(512 * 1024, b'A'); + for chunk in [1usize, 64, 4096, data.len()] { + let mut vt = vt(); + for part in data.chunks(chunk) { + vt.process(part); + } + assert_eq!(vt.scanner_steps(), data.len() as u64, "chunk size {chunk}"); + } + + // Many unterminated introducers in one read: each one used to scan to the end of the chunk. + let data = b"\x1b]0;xxxxxxxxxxxx".repeat(16_000); + let mut vt = vt(); + vt.process(&data); + assert_eq!(vt.scanner_steps(), data.len() as u64); + assert_eq!(vt.window_title(), None); +}