fix(terminal): read lines that end in CR CR LF instead of returning them empty (#38838)

* fix(terminal): end tail lines at CR CR LF instead of reading them as empty

Raw-mode programs end lines with CR CR LF. The output normalizer folded only
the CR directly before LF, so the leftover CR made each completed line read
as the empty text after it, and terminal.read returned blank lines. A run of
CRs lands on the same column as one CR, so treat the whole run before LF as
part of the line ending.

Fixes #38667

* chore: shorten the CR run comment

Refs #38667

* fix(terminal): defer CR until kept output decides it

A CR followed only by discarded escapes and then LF (text CR SGR CR LF)
still read as an empty line, and a restore cut landing just before CR CR LF
seeded a blank first row. The normalizer now holds each CR until the next
kept output: before LF it ends the line, and runs or escape-split CRs act as
one. The restore anchor skips a whole CR-run line ending.

Refs #38667

* test: size the restore-cut case from the exported seed limit

Refs #38667
This commit is contained in:
Neil
2026-11-06 06:43:14 -08:00
committed by GitHub
parent c5383afb6c
commit 6e7f39ba55
3 changed files with 83 additions and 36 deletions
@@ -1,6 +1,10 @@
import { describe, expect, it } from 'vitest'
import { normalizeTerminalChunk } from '../terminal-ansi-normalization'
import { appendNormalizedToTailBuffer } from './terminal-tail-buffer'
import {
buildRestoredTerminalTailSeed,
MAX_RESTORE_TAIL_SEED_CHARS
} from './terminal-tail-restore-seed'
import { MAX_TAIL_PARTIAL_CHARS } from './terminal-tail-limits'
import {
@@ -47,6 +51,35 @@ describe('terminal tail CRLF chunk boundaries', () => {
}
)
it('ends lines at CR CR LF, the sequence raw-mode programs emit', async () => {
const ptyId = 'pty-cr-cr-lf'
const { runtime, leaf } = runtimeWithLeaf(ptyId)
try {
runtime.onPtyData(ptyId, 'first\r\r\nsecond\r\x1b[0m\r\nthird\r', 1)
runtime.onPtyData(ptyId, '\r', 2)
runtime.onPtyData(ptyId, '\n', 3)
runtime.onPtyData(ptyId, 'fourth\r', 4)
runtime.onPtyData(ptyId, '\x1b[0m', 5)
runtime.onPtyData(ptyId, '\r\n', 6)
const lines = ['first', 'second', 'third', 'fourth']
expect(readPtyTail(runtime, ptyId)).toEqual(lines)
expect(leaf.tailTranscriptBuffer).toEqual(lines)
expect(leaf.tailPendingAnsi).toBe('')
} finally {
await runtime.onPtyExit(ptyId, 0)
}
})
it('restores without a blank first row when the cut lands before CR CR LF', () => {
// The kept suffix starts exactly at CR CR LF.
const lineCount = Math.floor((MAX_RESTORE_TAIL_SEED_CHARS - 3) / 5)
const filler = 'x'.repeat(MAX_RESTORE_TAIL_SEED_CHARS - 3 - lineCount * 5)
const suffix = `\r\r\n${filler}${'line\n'.repeat(lineCount)}`
expect(suffix).toHaveLength(MAX_RESTORE_TAIL_SEED_CHARS)
const seed = buildRestoredTerminalTailSeed(`earlier output${suffix}`)
expect(seed?.linesTotal).toBe(lineCount)
})
it('does not erase the final partial line on process exit', async () => {
const ptyId = 'pty-crlf-exit'
const { runtime, leaf } = runtimeWithLeaf(ptyId)
@@ -4,7 +4,7 @@ import { appendCompletedTerminalTranscript, buildPreview } from './terminal-tail
import type { RetainedTailRedrawCursor } from './terminal-tail-redraw-buffer'
// Multi-MB restore payloads retain only a tail; cap spawn-path parsing to the relevant suffix.
const MAX_RESTORE_TAIL_SEED_CHARS = 256 * 1024
export const MAX_RESTORE_TAIL_SEED_CHARS = 256 * 1024
export type RestoredTerminalTailSeed = {
lines: string[]
@@ -62,12 +62,10 @@ export function buildRestoredTerminalTailSeed(text: string): RestoredTerminalTai
let sliced = false
if (bounded.length > MAX_RESTORE_TAIL_SEED_CHARS) {
bounded = bounded.slice(-MAX_RESTORE_TAIL_SEED_CHARS)
// A sliced suffix may start mid-escape; restart after CR/LF and consume CRLF together to avoid a phantom blank row.
const anchor = bounded.search(/[\r\n]/)
if (anchor !== -1) {
bounded = bounded.slice(
bounded[anchor] === '\r' && bounded[anchor + 1] === '\n' ? anchor + 2 : anchor + 1
)
// A sliced suffix may start mid-escape; restart after a whole line ending (CRs then LF) to avoid a phantom blank row.
const anchor = /\r+\n?|\n/.exec(bounded)
if (anchor) {
bounded = bounded.slice(anchor.index + anchor[0].length)
}
sliced = true
}
@@ -102,7 +102,7 @@ export function normalizeTerminalChunk(
return { text: chunk, pendingAnsi: '' }
}
const combined = `${pendingAnsi}${chunk}`
const parts: string[] = []
const out = new NormalizedTerminalText(combined)
let textStart = 0
// Copy spans and skip parameter parsing for controls the preview discards.
for (let index = 0; index < combined.length; index += 1) {
@@ -111,18 +111,15 @@ export function normalizeTerminalChunk(
continue
}
if (code === 0x1b) {
appendTerminalNormalizedSpan(parts, combined, textStart, index)
out.appendSpan(textStart, index)
if (index + 1 >= combined.length) {
return { text: parts.join(''), pendingAnsi: combined.slice(index) }
return out.finish(combined.slice(index))
}
const introducer = classifyTerminalEscapeIntroducer(combined.charCodeAt(index + 1))
const endIndex = findTerminalEscapeSequenceEnd(combined, index, introducer)
if (endIndex === -1) {
return {
text: parts.join(''),
// Own the tail so it stops pinning the consumed chunk it was sliced from.
pendingAnsi: ownRetainedString(trimPendingAnsiControl(combined.slice(index)))
}
// Own the tail so it stops pinning the consumed chunk it was sliced from.
return out.finish(ownRetainedString(trimPendingAnsiControl(combined.slice(index))))
}
if (
introducer === 'csi' &&
@@ -131,44 +128,63 @@ export function normalizeTerminalChunk(
const parsed = parseTerminalCsiAtEnd(combined, index, endIndex)
if (isTerminalPreviewLineControl(parsed)) {
// Preserve Codex ANSI-only redraws so the tail overwrites the previous frame.
parts.push(combined.slice(index, endIndex + 1))
out.appendSpan(index, endIndex + 1)
}
}
index = endIndex
textStart = index + 1
continue
}
if (code === 0x0d && index + 1 === combined.length) {
// A final CR may be half of CRLF; keep the painted row until the next byte decides.
appendTerminalNormalizedSpan(parts, combined, textStart, index)
return { text: parts.join(''), pendingAnsi: '\r' }
}
if (code === 0x0d && combined.charCodeAt(index + 1) === 0x0a) {
// CRLF keeps only its LF, which starts the next span.
appendTerminalNormalizedSpan(parts, combined, textStart, index)
if (code === 0x0d) {
out.appendSpan(textStart, index)
out.deferCarriageReturn()
textStart = index + 1
continue
}
if (code === 0x08 || code === 0x09 || code === 0x0a || code === 0x0d) {
if (code === 0x08 || code === 0x09 || code === 0x0a) {
continue
}
if (!isTerminalPreviewPrintableCodeUnit(code)) {
appendTerminalNormalizedSpan(parts, combined, textStart, index)
out.appendSpan(textStart, index)
textStart = index + 1
}
}
appendTerminalNormalizedSpan(parts, combined, textStart, combined.length)
return { text: parts.join(''), pendingAnsi: '' }
out.appendSpan(textStart, combined.length)
return out.finish('')
}
function appendTerminalNormalizedSpan(
parts: string[],
value: string,
start: number,
end: number
): void {
if (end > start) {
parts.push(value.slice(start, end))
/**
* Holds a CR until the next kept output decides it: before LF it is part of the line ending,
* and runs of CRs, or CRs split by discarded escapes, act as one. Raw-mode programs end lines
* with CR CR LF, and an early CR must not erase the row it ends.
*/
class NormalizedTerminalText {
private readonly parts: string[] = []
private carriageReturnPending = false
constructor(private readonly value: string) {}
appendSpan(start: number, end: number): void {
if (end <= start) {
return
}
if (this.carriageReturnPending && this.value.charCodeAt(start) !== 0x0a) {
this.parts.push('\r')
}
this.carriageReturnPending = false
this.parts.push(this.value.slice(start, end))
}
deferCarriageReturn(): void {
this.carriageReturnPending = true
}
/** An undecided CR stays pending: it may be half of a CRLF split across chunks. */
finish(pendingTail: string): { text: string; pendingAnsi: string } {
return {
text: this.parts.join(''),
pendingAnsi: this.carriageReturnPending ? `\r${pendingTail}` : pendingTail
}
}
}