Fix native Windows Codex cursor flicker (#4907)
This commit is contained in:
@@ -316,7 +316,7 @@ describe('pane terminal output scheduler', () => {
|
||||
vi.runOnlyPendingTimers()
|
||||
|
||||
expect(terminal.write).toHaveBeenCalledWith(
|
||||
'\x1b[?2026h\x1b[?25l\x1b[10;8H\x1b[?25h\x1b[?2026ltyped',
|
||||
'\x1b[?2026h\x1b[?25l\x1b[10;8H\x1b[?2026ltyped',
|
||||
expect.any(Function)
|
||||
)
|
||||
})
|
||||
@@ -346,7 +346,53 @@ describe('pane terminal output scheduler', () => {
|
||||
vi.runOnlyPendingTimers()
|
||||
|
||||
expect(terminal.write).toHaveBeenCalledWith(
|
||||
'\x1b[?2026h\x1b[?25l\x1b[13;14Hr\x1b[?25h\x1b[?2026l',
|
||||
'\x1b[?2026h\x1b[?25l\x1b[13;14Hr\x1b[?2026l',
|
||||
expect.any(Function)
|
||||
)
|
||||
})
|
||||
|
||||
it('strips synchronized cursor shows that end before the real cursor restore', async () => {
|
||||
vi.useFakeTimers()
|
||||
const { writeTerminalOutput } = await loadScheduler()
|
||||
const terminal = createTerminal()
|
||||
|
||||
writeTerminalOutput(terminal, '\x1b[?2026h\x1b[?25l\x1b[26;59H\x1b[?25h\x1b[?2026l', {
|
||||
foreground: true,
|
||||
latencySensitive: true,
|
||||
stripTransientCursorShows: true,
|
||||
coalesceForeground: true
|
||||
})
|
||||
|
||||
vi.advanceTimersByTime(16)
|
||||
vi.runOnlyPendingTimers()
|
||||
|
||||
expect(terminal.write).toHaveBeenCalledWith(
|
||||
'\x1b[?2026h\x1b[?25l\x1b[26;59H\x1b[?2026l',
|
||||
expect.any(Function)
|
||||
)
|
||||
})
|
||||
|
||||
it('drains synchronized endings with final cursor placement before the fallback', async () => {
|
||||
vi.useFakeTimers()
|
||||
const { writeTerminalOutput } = await loadScheduler()
|
||||
const terminal = createTerminal()
|
||||
|
||||
writeTerminalOutput(
|
||||
terminal,
|
||||
'\x1b[?2026h\x1b[?25l\x1b[13;14Hr\x1b[5 q\x1b[?25h\x1b[19;3H\x1b[?2026l',
|
||||
{
|
||||
foreground: true,
|
||||
latencySensitive: true,
|
||||
stripTransientCursorShows: true,
|
||||
coalesceForeground: true
|
||||
}
|
||||
)
|
||||
|
||||
vi.advanceTimersByTime(0)
|
||||
|
||||
expect(terminal.write).toHaveBeenCalledTimes(1)
|
||||
expect(terminal.write).toHaveBeenCalledWith(
|
||||
'\x1b[?2026h\x1b[?25l\x1b[13;14Hr\x1b[5 q\x1b[19;3H\x1b[?25h\x1b[?2026l',
|
||||
expect.any(Function)
|
||||
)
|
||||
})
|
||||
@@ -391,8 +437,8 @@ describe('pane terminal output scheduler', () => {
|
||||
|
||||
expect(terminal.write).toHaveBeenCalledTimes(2)
|
||||
expect(terminal.write.mock.calls.map(([data]) => data)).toEqual([
|
||||
'\x1b[?2026h\x1b[0 q\x1b[?25l\x1b[19;3Hx\x1b[?25h\x1b[?2026l',
|
||||
'\x1b[?2026h\x1b[0 q\x1b[?25l\x1b[19;4Hx\x1b[?25h\x1b[?2026l'
|
||||
'\x1b[?2026h\x1b[0 q\x1b[?25l\x1b[19;3Hx\x1b[?2026l',
|
||||
'\x1b[?2026h\x1b[0 q\x1b[?25l\x1b[19;4Hx\x1b[?2026l'
|
||||
])
|
||||
})
|
||||
|
||||
|
||||
@@ -306,6 +306,29 @@ function isEntryDrainable(entry: QueueEntry): boolean {
|
||||
return !entry.foregroundHold && !entry.foregroundCoalesce
|
||||
}
|
||||
|
||||
function findCursorPositionSequenceEnd(
|
||||
data: string,
|
||||
fromIndex: number,
|
||||
toIndex = data.length
|
||||
): number {
|
||||
let offset = data.indexOf('\x1b[', fromIndex)
|
||||
while (offset !== -1 && offset < toIndex) {
|
||||
let index = offset + 2
|
||||
while (index < toIndex) {
|
||||
const char = data[index]
|
||||
if (char === 'G' || char === 'H' || char === 'f') {
|
||||
return index + 1
|
||||
}
|
||||
if ((char < '0' || char > '9') && char !== ';') {
|
||||
break
|
||||
}
|
||||
index += 1
|
||||
}
|
||||
offset = data.indexOf('\x1b[', offset + 2)
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
function removeTransientCursorShowSequences(data: string): string {
|
||||
let result = ''
|
||||
let offset = 0
|
||||
@@ -315,8 +338,35 @@ function removeTransientCursorShowSequences(data: string): string {
|
||||
CURSOR_HIDE_SEQUENCE,
|
||||
showIndex + CURSOR_SHOW_SEQUENCE.length
|
||||
)
|
||||
const nextPositionEnd = findCursorPositionSequenceEnd(
|
||||
data,
|
||||
showIndex + CURSOR_SHOW_SEQUENCE.length,
|
||||
nextHideIndex === -1 ? data.length : nextHideIndex
|
||||
)
|
||||
if (nextHideIndex === -1) {
|
||||
break
|
||||
if (nextPositionEnd === -1) {
|
||||
const synchronizedEndIndex = data.indexOf(
|
||||
SYNCHRONIZED_OUTPUT_END_SEQUENCE,
|
||||
showIndex + CURSOR_SHOW_SEQUENCE.length
|
||||
)
|
||||
if (synchronizedEndIndex === -1) {
|
||||
break
|
||||
}
|
||||
// Why: a synchronized frame can end while parked on footer/status
|
||||
// text. Do not expose that transient cell as the visible cursor.
|
||||
result += data.slice(offset, showIndex)
|
||||
offset = showIndex + CURSOR_SHOW_SEQUENCE.length
|
||||
showIndex = data.indexOf(CURSOR_SHOW_SEQUENCE, offset)
|
||||
continue
|
||||
}
|
||||
// Why: Codex can show the cursor before its final synchronized-frame
|
||||
// placement. Place first so xterm cannot rasterize the stale cell.
|
||||
result += data.slice(offset, showIndex)
|
||||
result += data.slice(showIndex + CURSOR_SHOW_SEQUENCE.length, nextPositionEnd)
|
||||
result += CURSOR_SHOW_SEQUENCE
|
||||
offset = nextPositionEnd
|
||||
showIndex = data.indexOf(CURSOR_SHOW_SEQUENCE, offset)
|
||||
continue
|
||||
}
|
||||
result += data.slice(offset, showIndex)
|
||||
offset = showIndex + CURSOR_SHOW_SEQUENCE.length
|
||||
@@ -360,6 +410,28 @@ function containsDrainableCursorRestore(data: string): boolean {
|
||||
)
|
||||
}
|
||||
|
||||
function containsFinalCursorPlacementBeforeSynchronizedEnd(data: string): boolean {
|
||||
const synchronizedEndIndex = data.lastIndexOf(SYNCHRONIZED_OUTPUT_END_SEQUENCE)
|
||||
if (synchronizedEndIndex === -1) {
|
||||
return false
|
||||
}
|
||||
const lastShowIndex = data.lastIndexOf(CURSOR_SHOW_SEQUENCE, synchronizedEndIndex)
|
||||
if (lastShowIndex === -1) {
|
||||
return false
|
||||
}
|
||||
const lastHideIndex = data.lastIndexOf(CURSOR_HIDE_SEQUENCE, synchronizedEndIndex)
|
||||
if (lastHideIndex > lastShowIndex) {
|
||||
return false
|
||||
}
|
||||
return (
|
||||
findCursorPositionSequenceEnd(
|
||||
data,
|
||||
lastShowIndex + CURSOR_SHOW_SEQUENCE.length,
|
||||
synchronizedEndIndex
|
||||
) !== -1
|
||||
)
|
||||
}
|
||||
|
||||
function previewQueuedData(entry: QueueEntry, limit: number): string {
|
||||
let data = ''
|
||||
for (let index = entry.chunkIndex; index < entry.chunks.length; index += 1) {
|
||||
@@ -383,7 +455,11 @@ function coalescedQueuedDataNeedsCursorRestore(entry: QueueEntry): boolean {
|
||||
0,
|
||||
synchronizedEndIndex + SYNCHRONIZED_OUTPUT_END_SEQUENCE.length
|
||||
)
|
||||
return containsCursorRestore(synchronizedFrame) && !containsDrainableCursorRestore(data)
|
||||
return (
|
||||
containsCursorRestore(synchronizedFrame) &&
|
||||
!containsFinalCursorPlacementBeforeSynchronizedEnd(synchronizedFrame) &&
|
||||
!containsDrainableCursorRestore(data)
|
||||
)
|
||||
}
|
||||
|
||||
function takeQueuedChunk(entry: QueueEntry, limit: number): QueuedWrite | null {
|
||||
|
||||
@@ -83,6 +83,7 @@ type QueuedMessageFrame = ScreenSnapshot & {
|
||||
elapsedMs: number
|
||||
rasterCursorCells: RasterCursorCell[]
|
||||
rasterWorkingCursorCells: RasterCursorCell[]
|
||||
rasterNonInputCursorCells: RasterCursorCell[]
|
||||
}
|
||||
|
||||
type RasterFrameInput = {
|
||||
@@ -102,7 +103,9 @@ const CURSOR_THEME = {
|
||||
cursor: '#23ff45',
|
||||
cursorAccent: '#001000',
|
||||
foreground: '#f4f4f5',
|
||||
background: '#050505'
|
||||
background: '#050505',
|
||||
green: '#44aa66',
|
||||
brightGreen: '#58c979'
|
||||
}
|
||||
|
||||
const CODEX_TUI_READY_RE = /Ask Codex|OpenAI/i
|
||||
@@ -110,7 +113,7 @@ const CODEX_TRUST_PROMPT_RE = /Do you trust the contents of this directory\?/i
|
||||
const CODEX_UPDATE_PROMPT_RE = /Update available|Skip until next version/i
|
||||
const CODEX_WORKING_STATUS_RE =
|
||||
/W[\s\S]{0,20}o[\s\S]{0,20}r[\s\S]{0,20}k[\s\S]{0,20}i[\s\S]{0,20}n[\s\S]{0,20}g[\s\S]{0,40}\(/i
|
||||
const QUEUED_CURSOR_SAMPLE_INTERVAL_MS = 20
|
||||
const QUEUED_CURSOR_SAMPLE_INTERVAL_MS = 5
|
||||
const QUEUED_CURSOR_CAPTURE_MS = 6_000
|
||||
const ARTIFACT_DIR = path.join(process.cwd(), '.tmp', 'cursor-jitter-repro')
|
||||
const CONPTY_DA1_RESPONSE = '\x1b[?61;4c'
|
||||
@@ -145,6 +148,26 @@ function isQueuedInputLine(text: string): boolean {
|
||||
)
|
||||
}
|
||||
|
||||
function isInputCursorRow(snapshot: ScreenSnapshot, row: number): boolean {
|
||||
const text = snapshot.lines.find((line) => line.row === row)?.text ?? ''
|
||||
const trimmed = text.trimStart()
|
||||
return isQueuedInputLine(text) || trimmed.startsWith('›')
|
||||
}
|
||||
|
||||
function isPromptCursorFrame(frame: ScreenSnapshot): boolean {
|
||||
if (!frame.marker || frame.coreCursorHidden !== false) {
|
||||
return false
|
||||
}
|
||||
return isInputCursorRow(frame, frame.marker.cellY)
|
||||
}
|
||||
|
||||
function isUnexpectedVisibleCursorFrame(frame: ScreenSnapshot): boolean {
|
||||
if (!frame.marker || frame.coreCursorHidden !== false) {
|
||||
return false
|
||||
}
|
||||
return !isInputCursorRow(frame, frame.marker.cellY)
|
||||
}
|
||||
|
||||
function quotePowerShellSingleQuoted(value: string): string {
|
||||
return `'${value.replaceAll("'", "''")}'`
|
||||
}
|
||||
@@ -590,6 +613,7 @@ async function captureQueuedMessageFrames(
|
||||
const rasterInputs: RasterFrameInput[] = []
|
||||
const screenSamples: QueuedMessageFrame[] = []
|
||||
const startedAt = Date.now()
|
||||
let suspiciousScreenshotCount = 0
|
||||
|
||||
const cdp = await page.context().newCDPSession(page)
|
||||
cdp.on('Page.screencastFrame', (params) => {
|
||||
@@ -611,7 +635,8 @@ async function captureQueuedMessageFrames(
|
||||
at: Date.now(),
|
||||
elapsedMs: Date.now() - startedAt,
|
||||
rasterCursorCells: [],
|
||||
rasterWorkingCursorCells: []
|
||||
rasterWorkingCursorCells: [],
|
||||
rasterNonInputCursorCells: []
|
||||
})
|
||||
await page.waitForTimeout(QUEUED_CURSOR_SAMPLE_INTERVAL_MS)
|
||||
}
|
||||
@@ -620,30 +645,37 @@ async function captureQueuedMessageFrames(
|
||||
await cdp.detach().catch(() => {})
|
||||
}
|
||||
|
||||
let suspiciousScreenshotCount = 0
|
||||
const fallbackSample = screenSamples[0] ?? {
|
||||
...(await readScreenSnapshot(page, label, tabId, ptyId)),
|
||||
index: 0,
|
||||
at: Date.now(),
|
||||
elapsedMs: Date.now() - startedAt,
|
||||
rasterCursorCells: [],
|
||||
rasterWorkingCursorCells: []
|
||||
rasterWorkingCursorCells: [],
|
||||
rasterNonInputCursorCells: []
|
||||
}
|
||||
const frames: QueuedMessageFrame[] = []
|
||||
for (const [index, input] of rasterInputs.entries()) {
|
||||
const nearestSample = screenSamples.reduce(
|
||||
(nearest, sample) =>
|
||||
Math.abs(sample.elapsedMs - input.elapsedMs) < Math.abs(nearest.elapsedMs - input.elapsedMs)
|
||||
? sample
|
||||
const sample = screenSamples.reduce(
|
||||
(nearest, candidate) =>
|
||||
Math.abs(candidate.elapsedMs - input.elapsedMs) <
|
||||
Math.abs(nearest.elapsedMs - input.elapsedMs)
|
||||
? candidate
|
||||
: nearest,
|
||||
fallbackSample
|
||||
)
|
||||
const rasterCursorCells = analyzeRasterCursorCells(input.buffer, target, viewport)
|
||||
const rasterWorkingCursorCells = rasterCursorCells.filter((cell) =>
|
||||
workingRows(nearestSample).has(cell.cellY)
|
||||
workingRows(sample).has(cell.cellY)
|
||||
)
|
||||
if (rasterWorkingCursorCells.length > 0 && suspiciousScreenshotCount < 8) {
|
||||
const filename = `queued-message-raster-working-cursor-${index}.png`
|
||||
const rasterNonInputCursorCells = rasterCursorCells.filter(
|
||||
(cell) => !isInputCursorRow(sample, cell.cellY)
|
||||
)
|
||||
if (
|
||||
(rasterWorkingCursorCells.length > 0 || rasterNonInputCursorCells.length > 0) &&
|
||||
suspiciousScreenshotCount < 8
|
||||
) {
|
||||
const filename = `queued-message-raster-suspicious-cursor-${index}.png`
|
||||
await testInfo.attach(filename, {
|
||||
body: input.buffer,
|
||||
contentType: 'image/png'
|
||||
@@ -652,12 +684,13 @@ async function captureQueuedMessageFrames(
|
||||
suspiciousScreenshotCount += 1
|
||||
}
|
||||
frames.push({
|
||||
...nearestSample,
|
||||
...sample,
|
||||
index,
|
||||
at: input.at,
|
||||
elapsedMs: input.elapsedMs,
|
||||
rasterCursorCells,
|
||||
rasterWorkingCursorCells
|
||||
rasterWorkingCursorCells,
|
||||
rasterNonInputCursorCells
|
||||
})
|
||||
}
|
||||
return frames
|
||||
@@ -700,6 +733,13 @@ test.describe('Codex terminal cursor jitter repro', () => {
|
||||
)
|
||||
.toBe(true)
|
||||
await applyCursorProbeTheme(orcaPage, tabId)
|
||||
const workingOnlyFrames = await captureQueuedMessageFrames(
|
||||
orcaPage,
|
||||
`${shellCase.label}-no-input`,
|
||||
tabId,
|
||||
ptyId,
|
||||
testInfo
|
||||
)
|
||||
await orcaPage.keyboard.insertText('s')
|
||||
await expect
|
||||
.poll(
|
||||
@@ -721,7 +761,12 @@ test.describe('Codex terminal cursor jitter repro', () => {
|
||||
|
||||
const snapshot = await readScreenSnapshot(orcaPage, shellCase.label, tabId, ptyId)
|
||||
const rawChunks = await readPtyOutputDiagnostics(orcaPage)
|
||||
const visibleWorkingOnlyCursorFrames = workingOnlyFrames.filter(isPromptCursorFrame)
|
||||
const unexpectedWorkingOnlyCursorFrames = workingOnlyFrames.filter(
|
||||
isUnexpectedVisibleCursorFrame
|
||||
)
|
||||
const visibleWorkingCursorFrames = frames.filter(isVisibleWorkingCursorFrame)
|
||||
const unexpectedQueuedCursorFrames = frames.filter(isUnexpectedVisibleCursorFrame)
|
||||
const rasterWorkingCursorFrames = frames.filter(
|
||||
(frame) => frame.rasterWorkingCursorCells.length > 0
|
||||
)
|
||||
@@ -729,6 +774,10 @@ test.describe('Codex terminal cursor jitter repro', () => {
|
||||
body: JSON.stringify(frames, null, 2),
|
||||
contentType: 'application/json'
|
||||
})
|
||||
await testInfo.attach('working-no-input-cursor-frames.json', {
|
||||
body: JSON.stringify(workingOnlyFrames, null, 2),
|
||||
contentType: 'application/json'
|
||||
})
|
||||
await testInfo.attach('queued-message-screen.json', {
|
||||
body: JSON.stringify(snapshot, null, 2),
|
||||
contentType: 'application/json'
|
||||
@@ -745,6 +794,10 @@ test.describe('Codex terminal cursor jitter repro', () => {
|
||||
path.join(ARTIFACT_DIR, 'queued-message-cursor-frames.json'),
|
||||
`${JSON.stringify(frames, null, 2)}\n`
|
||||
)
|
||||
writeFileSync(
|
||||
path.join(ARTIFACT_DIR, 'working-no-input-cursor-frames.json'),
|
||||
`${JSON.stringify(workingOnlyFrames, null, 2)}\n`
|
||||
)
|
||||
writeFileSync(
|
||||
path.join(ARTIFACT_DIR, 'queued-message-pty-chunks.txt'),
|
||||
`${formatPtyChunks(rawChunks)}\n`
|
||||
@@ -757,6 +810,14 @@ test.describe('Codex terminal cursor jitter repro', () => {
|
||||
queuedInputFrames.length,
|
||||
'queued input should be captured before checking cursor placement'
|
||||
).toBeGreaterThan(0)
|
||||
expect(
|
||||
visibleWorkingOnlyCursorFrames,
|
||||
'Codex should expose its prompt cursor during the no-input capture so the repro can detect misplaced cursor restores'
|
||||
).not.toEqual([])
|
||||
expect(
|
||||
unexpectedWorkingOnlyCursorFrames,
|
||||
`visible cursor should only appear on Codex prompt rows while Working: ${JSON.stringify(unexpectedWorkingOnlyCursorFrames)}`
|
||||
).toEqual([])
|
||||
expect(
|
||||
visibleWorkingCursorFrames,
|
||||
`cursor should not be visibly parked on Working row: ${JSON.stringify(visibleWorkingCursorFrames)}`
|
||||
@@ -765,5 +826,9 @@ test.describe('Codex terminal cursor jitter repro', () => {
|
||||
rasterWorkingCursorFrames,
|
||||
`rasterized terminal should not paint cursor-colored pixels on Working row: ${JSON.stringify(rasterWorkingCursorFrames)}`
|
||||
).toEqual([])
|
||||
expect(
|
||||
unexpectedQueuedCursorFrames,
|
||||
`visible cursor should only appear on Codex prompt rows after queuing input: ${JSON.stringify(unexpectedQueuedCursorFrames)}`
|
||||
).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user