xterm.js icon indicating copy to clipboard operation
xterm.js copied to clipboard

SU (`CSI Ps S`) in a scroll region anchored at the top row discards the scrolled-out rows instead of adding them to scrollback

Open SSFSKIM opened this issue 2 months ago • 2 comments

Version: @xterm/xterm 6.0.0 (also @xterm/headless 6.0.0). The same code is in 6.1.0-beta.304 (src/common/InputHandler.ts, scrollUp).

Isolated reproduction

In a 40-row terminal:

seq 1 60; printf '\e[1;39r\e[10S\e[r'

The sequence sets the scroll region to rows 1 to 39, scrolls it up 10 rows, then resets the region.

Expected (tmux 3.7b, iTerm2, kitty):

  • The 10 rows that leave the top of the region go into scrollback, so all of 1 to 60 can still be scrolled to.
  • Row 40, outside the region, is unchanged.
  • The region's bottom 10 rows are blank, with the current background.

Observed (xterm.js 6.0.0):

  • The visible screen is the same as tmux's.
  • The 10 rows are gone: they are not in scrollback. After the sequence, the buffer holds 50 of the 60 numbers.

Control: the same bytes in tmux 3.7b, in a 189×40 pane with history-limit 10000, keep all 60 rows (tmux capture-pane -p -S -).

Cause

InputHandler.scrollUp (6.0.0, src/common/InputHandler.ts lines 1429 to 1438) runs this for every step:

this._activeBuffer.lines.splice(this._activeBuffer.ybase + this._activeBuffer.scrollTop, 1);
this._activeBuffer.lines.splice(this._activeBuffer.ybase + this._activeBuffer.scrollBottom, 0, this._activeBuffer.getBlankLine(this._eraseAttrData()));

This deletes the top row of the region whatever the region's top is. The line-feed path (BufferService.scroll) already treats scrollTop === 0 specially: it inserts after the region's bottom and advances ybase, so the row enters scrollback. SU with scrollTop === 0 on the normal buffer could take the same path, Ps times (clamped to the region's height).

Why it matters

Codex CLI's inline TUI (0.155.1, also reported in openai/codex #27644 against 0.139.0) pushes finished transcript lines and the shell rows above it out of its viewport with exactly CSI 1;B r CSI k S CSI r. Codex expects those rows to land in scrollback and never repaints them, so in xterm.js hosts (the VS Code terminal, Electron embeds) they are lost:

  • Starting codex and quitting at once, in a 189×40 terminal after seq 1 60, loses 17 of the 60 rows.
  • Longer conversations get permanent gaps in the transcript.

Workaround used downstream

An embedder registers terminal.parser.registerCsiHandler({ final: 'S' }, …). On the normal buffer with the scroll region's top at row 1, it scrolls through the core's line-feed scroll with SU's erase attributes and returns true; otherwise it returns false. This needs private members (_core._bufferService.scroll, _core._inputHandler._eraseAttrData), which is why a fix in xterm.js is preferable.

SSFSKIM avatar Sep 29 '26 04:09 SSFSKIM

This looks like the same bug as #6010, and #6011 has a fix open for it. I ran the steps above with @xterm/headless: 6.0.0 and 6.1.0-beta.303 both keep 50 of the 60 lines. With the change from #6011 applied on top of current master, all 60 are kept.

leroyomey avatar Oct 08 '26 22:10 leroyomey

Duplicate of #6010

SU (CSI Ps S) with the scroll region's top at row 1 deletes the scrolled-out rows instead of moving them into scrollback. #6011 has a fix open, and it was confirmed above that it keeps all 60 rows with the repro from this issue. Carrying over to #6010: the one-line repro seq 1 60; printf '\e[1;39r\e[10S\e[r' in a 40-row terminal, the tmux 3.7b / iTerm2 / kitty behaviour as the expected result, and the Codex CLI inline TUI as a real-world trigger.

xtermjs-bot avatar Oct 29 '26 13:10 xtermjs-bot