mirror of
https://github.com/openai/codex.git
synced 2026-05-18 02:02:30 +00:00
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
184 lines
6.1 KiB
Rust
184 lines
6.1 KiB
Rust
//! Terminal-specific row caps for resize reflow.
|
|
//!
|
|
//! The auto cap mirrors documented scrollback defaults for terminals we can identify. Console Host
|
|
//! does not expose its configured screen buffer through terminal metadata, so it usually lands in
|
|
//! the fallback bucket.
|
|
//!
|
|
//! These caps are deliberately conservative: Codex is rebuilding normal terminal scrollback, not an
|
|
//! internal virtual transcript. Replaying more rows than the terminal retains wastes work and can
|
|
//! make interactive resize feel worse without giving the user more usable history.
|
|
|
|
use codex_config::types::DEFAULT_TERMINAL_RESIZE_REFLOW_FALLBACK_MAX_ROWS;
|
|
use codex_terminal_detection::TerminalInfo;
|
|
use codex_terminal_detection::TerminalName;
|
|
use codex_terminal_detection::terminal_info;
|
|
|
|
use crate::legacy_core::config::TerminalResizeReflowConfig;
|
|
use crate::legacy_core::config::TerminalResizeReflowMaxRows;
|
|
|
|
const VSCODE_RESIZE_REFLOW_MAX_ROWS: usize = 1_000;
|
|
const WINDOWS_TERMINAL_RESIZE_REFLOW_MAX_ROWS: usize = 9_001;
|
|
const WEZTERM_RESIZE_REFLOW_MAX_ROWS: usize = 3_500;
|
|
const ALACRITTY_RESIZE_REFLOW_MAX_ROWS: usize = 10_000;
|
|
|
|
/// Resolve the configured row cap for resize and initial replay.
|
|
///
|
|
/// `Auto` uses terminal detection plus the VS Code environment probe because VS Code can run shells
|
|
/// whose terminal-name metadata points at the host shell rather than VS Code itself. Returning
|
|
/// `None` means the user explicitly disabled row limiting with `max_rows = 0`.
|
|
pub(crate) fn resize_reflow_max_rows(config: TerminalResizeReflowConfig) -> Option<usize> {
|
|
resize_reflow_max_rows_for(
|
|
config,
|
|
&terminal_info(),
|
|
crate::tui::running_in_vscode_terminal(),
|
|
)
|
|
}
|
|
|
|
fn resize_reflow_max_rows_for(
|
|
config: TerminalResizeReflowConfig,
|
|
terminal: &TerminalInfo,
|
|
running_in_vscode_terminal: bool,
|
|
) -> Option<usize> {
|
|
match config.max_rows {
|
|
TerminalResizeReflowMaxRows::Auto => Some(auto_resize_reflow_max_rows(
|
|
terminal.name,
|
|
running_in_vscode_terminal,
|
|
)),
|
|
TerminalResizeReflowMaxRows::Disabled => None,
|
|
TerminalResizeReflowMaxRows::Limit(max_rows) => Some(max_rows),
|
|
}
|
|
}
|
|
|
|
fn auto_resize_reflow_max_rows(
|
|
terminal_name: TerminalName,
|
|
running_in_vscode_terminal: bool,
|
|
) -> usize {
|
|
if running_in_vscode_terminal {
|
|
return VSCODE_RESIZE_REFLOW_MAX_ROWS;
|
|
}
|
|
|
|
match terminal_name {
|
|
TerminalName::VsCode => VSCODE_RESIZE_REFLOW_MAX_ROWS,
|
|
TerminalName::WindowsTerminal => WINDOWS_TERMINAL_RESIZE_REFLOW_MAX_ROWS,
|
|
TerminalName::WezTerm => WEZTERM_RESIZE_REFLOW_MAX_ROWS,
|
|
TerminalName::Alacritty => ALACRITTY_RESIZE_REFLOW_MAX_ROWS,
|
|
TerminalName::AppleTerminal
|
|
| TerminalName::Ghostty
|
|
| TerminalName::Iterm2
|
|
| TerminalName::WarpTerminal
|
|
| TerminalName::Kitty
|
|
| TerminalName::Konsole
|
|
| TerminalName::GnomeTerminal
|
|
| TerminalName::Vte
|
|
| TerminalName::Dumb
|
|
| TerminalName::Unknown => DEFAULT_TERMINAL_RESIZE_REFLOW_FALLBACK_MAX_ROWS,
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use codex_terminal_detection::Multiplexer;
|
|
|
|
fn test_terminal(name: TerminalName) -> TerminalInfo {
|
|
TerminalInfo {
|
|
name,
|
|
term_program: None,
|
|
version: None,
|
|
term: None,
|
|
multiplexer: None,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn auto_resize_reflow_max_rows_uses_terminal_defaults() {
|
|
let cases = [
|
|
(TerminalName::VsCode, VSCODE_RESIZE_REFLOW_MAX_ROWS),
|
|
(
|
|
TerminalName::WindowsTerminal,
|
|
WINDOWS_TERMINAL_RESIZE_REFLOW_MAX_ROWS,
|
|
),
|
|
(TerminalName::WezTerm, WEZTERM_RESIZE_REFLOW_MAX_ROWS),
|
|
(TerminalName::Alacritty, ALACRITTY_RESIZE_REFLOW_MAX_ROWS),
|
|
(
|
|
TerminalName::Ghostty,
|
|
DEFAULT_TERMINAL_RESIZE_REFLOW_FALLBACK_MAX_ROWS,
|
|
),
|
|
(
|
|
TerminalName::Unknown,
|
|
DEFAULT_TERMINAL_RESIZE_REFLOW_FALLBACK_MAX_ROWS,
|
|
),
|
|
];
|
|
|
|
for (terminal_name, expected_max_rows) in cases {
|
|
assert_eq!(
|
|
auto_resize_reflow_max_rows(
|
|
terminal_name,
|
|
/*running_in_vscode_terminal*/ false
|
|
),
|
|
expected_max_rows
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn auto_resize_reflow_max_rows_prefers_vscode_probe() {
|
|
assert_eq!(
|
|
auto_resize_reflow_max_rows(
|
|
TerminalName::WindowsTerminal,
|
|
/*running_in_vscode_terminal*/ true
|
|
),
|
|
VSCODE_RESIZE_REFLOW_MAX_ROWS
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn configured_resize_reflow_max_rows_overrides_auto_detection() {
|
|
let terminal = test_terminal(TerminalName::VsCode);
|
|
let config = TerminalResizeReflowConfig {
|
|
max_rows: TerminalResizeReflowMaxRows::Limit(42),
|
|
};
|
|
|
|
assert_eq!(
|
|
resize_reflow_max_rows_for(
|
|
config, &terminal, /*running_in_vscode_terminal*/ false
|
|
),
|
|
Some(42)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn disabled_resize_reflow_max_rows_keeps_all_rows() {
|
|
let terminal = test_terminal(TerminalName::VsCode);
|
|
let config = TerminalResizeReflowConfig {
|
|
max_rows: TerminalResizeReflowMaxRows::Disabled,
|
|
};
|
|
|
|
assert_eq!(
|
|
resize_reflow_max_rows_for(
|
|
config, &terminal, /*running_in_vscode_terminal*/ false
|
|
),
|
|
None
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn unknown_terminal_uses_fallback_even_under_multiplexer() {
|
|
let terminal = TerminalInfo {
|
|
name: TerminalName::Unknown,
|
|
term_program: None,
|
|
version: None,
|
|
term: Some("xterm-256color".to_string()),
|
|
multiplexer: Some(Multiplexer::Tmux { version: None }),
|
|
};
|
|
let config = TerminalResizeReflowConfig::default();
|
|
|
|
assert_eq!(
|
|
resize_reflow_max_rows_for(
|
|
config, &terminal, /*running_in_vscode_terminal*/ false
|
|
),
|
|
Some(DEFAULT_TERMINAL_RESIZE_REFLOW_FALLBACK_MAX_ROWS)
|
|
);
|
|
}
|
|
}
|