mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
5591912f0b
Fixes multiple scrollback and terminal resize issues: #5538, #5576, #8352, #12223, #16165, and #15380. ## Why Codex writes finalized transcript output into terminal scrollback after wrapping it for the current viewport width. A later terminal resize could leave that scrollback shaped for the old width, so wider windows kept narrow output and narrower windows could show stale wrapping artifacts until enough new output replaced the visible area. This is also the foundation PR for responsive markdown tables. Table rendering needs finalized transcript content to be width-sensitive after insertion, not only while content is first streaming. Markdown table rendering itself stays in #18576. ## Stack - PR1: resize backlog reflow and interrupt cleanup - #18576: markdown table support ## What Changed - Rebuild source-backed transcript history when the terminal width changes. `terminal_resize_reflow` is introduced through the experimental feature system, but is enabled by default for this rollout so we can validate behavior across real terminals. - Preserve assistant and plan stream source so finalized streaming output can participate in resize reflow after consolidation. - Debounce resize work, but force a final source-backed reflow when a resize happened during active or unconsolidated streaming output. - Clear stale pending history lines on resize so old-width wrapped output is not emitted just before rebuilt scrollback. - Bound replay work with `[tui.terminal_resize_reflow].max_rows`: omitted uses terminal-specific defaults, `0` keeps all rendered rows, and a positive value sets an explicit cap. The cap applies both while initially replaying a resumed transcript into scrollback and when rebuilding scrollback after terminal resize. - Consolidate interrupted assistant streams before cleanup, then clear pending stream output and active-tail state consistently. - Move resize reflow and thread event buffering helpers out of `app.rs` into dedicated TUI modules. - Add focused coverage for resize reflow, feature-gated behavior, streaming source preservation, interrupted output cleanup, unicode-neutral text, terminal-specific row caps, and composer/layout stability. ## Runtime Bounds Resize reflow keeps only the most recent rendered rows when a row cap is active. The default is `auto`, which maps to the detected terminal's default scrollback size where Codex can identify it: VS Code `1000`, Windows Terminal `9001`, WezTerm `3500`, and Alacritty `10000`. Terminals without a dedicated mapping use the conservative fallback of `1000` rows. Users can override this with `[tui.terminal_resize_reflow] max_rows = N`, or set `max_rows = 0` to disable row limiting. ## Validation - `just fmt` - `git diff --check` - `cargo test --manifest-path codex-rs/Cargo.toml -p codex-tui reflow` - `cargo test --manifest-path codex-rs/Cargo.toml -p codex-tui transcript_reflow` - `just fix -p codex-tui` - PR CI in progress on the squashed branch
124 lines
4.3 KiB
Rust
124 lines
4.3 KiB
Rust
//! Streaming primitives used by the TUI transcript pipeline.
|
|
//!
|
|
//! `StreamState` owns newline-gated markdown collection and a FIFO queue of committed render lines.
|
|
//! Higher-level modules build on top of this state:
|
|
//! - `controller` adapts queued lines into `HistoryCell` emission rules for message and plan streams.
|
|
//! - `chunking` computes adaptive drain plans from queue pressure.
|
|
//! - `commit_tick` binds policy decisions to concrete controller drains.
|
|
//!
|
|
//! The key invariant is queue ordering. All drains pop from the front, and enqueue records an
|
|
//! arrival timestamp so policy code can reason about oldest queued age without peeking into text.
|
|
|
|
use std::collections::VecDeque;
|
|
use std::path::Path;
|
|
use std::time::Duration;
|
|
use std::time::Instant;
|
|
|
|
use ratatui::text::Line;
|
|
|
|
use crate::markdown_stream::MarkdownStreamCollector;
|
|
pub(crate) mod chunking;
|
|
pub(crate) mod commit_tick;
|
|
pub(crate) mod controller;
|
|
|
|
struct QueuedLine {
|
|
line: Line<'static>,
|
|
enqueued_at: Instant,
|
|
}
|
|
|
|
/// Holds in-flight markdown stream state and queued committed lines.
|
|
pub(crate) struct StreamState {
|
|
pub(crate) collector: MarkdownStreamCollector,
|
|
queued_lines: VecDeque<QueuedLine>,
|
|
pub(crate) has_seen_delta: bool,
|
|
}
|
|
|
|
impl StreamState {
|
|
/// Create stream state whose markdown collector renders local file links relative to `cwd`.
|
|
///
|
|
/// Controllers are expected to pass the session cwd here once and keep it stable for the
|
|
/// lifetime of the active stream.
|
|
pub(crate) fn new(width: Option<usize>, cwd: &Path) -> Self {
|
|
Self {
|
|
collector: MarkdownStreamCollector::new(width, cwd),
|
|
queued_lines: VecDeque::new(),
|
|
has_seen_delta: false,
|
|
}
|
|
}
|
|
/// Resets collector and queue state for the next stream lifecycle.
|
|
pub(crate) fn clear(&mut self) {
|
|
self.collector.clear();
|
|
self.queued_lines.clear();
|
|
self.has_seen_delta = false;
|
|
}
|
|
/// Drains one queued line from the front of the queue.
|
|
pub(crate) fn step(&mut self) -> Vec<Line<'static>> {
|
|
self.queued_lines
|
|
.pop_front()
|
|
.map(|queued| queued.line)
|
|
.into_iter()
|
|
.collect()
|
|
}
|
|
/// Drains up to `max_lines` queued lines from the front of the queue.
|
|
///
|
|
/// Callers that pass very large values still get bounded behavior because this method clamps to
|
|
/// the currently available queue length.
|
|
pub(crate) fn drain_n(&mut self, max_lines: usize) -> Vec<Line<'static>> {
|
|
let end = max_lines.min(self.queued_lines.len());
|
|
self.queued_lines
|
|
.drain(..end)
|
|
.map(|queued| queued.line)
|
|
.collect()
|
|
}
|
|
/// Clears queued lines while keeping collector/turn lifecycle state intact.
|
|
pub(crate) fn clear_queue(&mut self) {
|
|
self.queued_lines.clear();
|
|
}
|
|
/// Returns whether no lines are queued for commit.
|
|
pub(crate) fn is_idle(&self) -> bool {
|
|
self.queued_lines.is_empty()
|
|
}
|
|
/// Returns the current queue depth.
|
|
pub(crate) fn queued_len(&self) -> usize {
|
|
self.queued_lines.len()
|
|
}
|
|
/// Returns the age of the oldest queued line.
|
|
pub(crate) fn oldest_queued_age(&self, now: Instant) -> Option<Duration> {
|
|
self.queued_lines
|
|
.front()
|
|
.map(|queued| now.saturating_duration_since(queued.enqueued_at))
|
|
}
|
|
/// Appends committed lines to the queue with a shared enqueue timestamp.
|
|
pub(crate) fn enqueue(&mut self, lines: Vec<Line<'static>>) {
|
|
let now = Instant::now();
|
|
self.queued_lines
|
|
.extend(lines.into_iter().map(|line| QueuedLine {
|
|
line,
|
|
enqueued_at: now,
|
|
}));
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use pretty_assertions::assert_eq;
|
|
use std::path::PathBuf;
|
|
|
|
fn test_cwd() -> PathBuf {
|
|
// These tests only need a stable absolute cwd; using temp_dir() avoids baking Unix- or
|
|
// Windows-specific root semantics into the fixtures.
|
|
std::env::temp_dir()
|
|
}
|
|
|
|
#[test]
|
|
fn drain_n_clamps_to_available_lines() {
|
|
let mut state = StreamState::new(/*width*/ None, &test_cwd());
|
|
state.enqueue(vec![Line::from("one")]);
|
|
|
|
let drained = state.drain_n(/*max_lines*/ 8);
|
|
assert_eq!(drained, vec![Line::from("one")]);
|
|
assert!(state.is_idle());
|
|
}
|
|
}
|