Merge branch 'pr-7139-head' into Jinwoo-H/test-7150-plus-7139

This commit is contained in:
Jinwoo-H
2026-07-02 18:29:18 -04:00
2 changed files with 75 additions and 5 deletions
@@ -907,10 +907,66 @@ describe('pane terminal output scheduler', () => {
expect(terminal.write).not.toHaveBeenCalled()
vi.advanceTimersByTime(0)
expect(terminal.write).toHaveBeenCalledTimes(16)
expect(terminal.write).toHaveBeenCalledTimes(2)
vi.advanceTimersByTime(1)
expect(terminal.write).toHaveBeenCalledTimes(32)
vi.advanceTimersByTime(4)
expect(terminal.write).toHaveBeenCalledTimes(4)
})
it('yields high-priority backlog drains when writes spend the frame budget', async () => {
vi.useFakeTimers()
const { writeTerminalOutput } = await loadScheduler()
const terminal = createTerminal()
const chunk = 'x'.repeat(16 * 1024)
let now = 0
const nowSpy = vi.spyOn(performance, 'now').mockImplementation(() => now)
terminal.write.mockImplementation((_data: string, callback?: () => void) => {
now += 9
callback?.()
})
try {
for (let i = 0; i < 64; i++) {
writeTerminalOutput(terminal, chunk, { foreground: false })
}
vi.advanceTimersByTime(0)
expect(terminal.write).toHaveBeenCalledTimes(1)
vi.advanceTimersByTime(4)
expect(terminal.write).toHaveBeenCalledTimes(2)
} finally {
nowSpy.mockRestore()
}
})
it('uses Date.now for drain budgeting when performance is unavailable', async () => {
vi.useFakeTimers()
vi.stubGlobal('performance', undefined)
const { writeTerminalOutput } = await loadScheduler()
const terminal = createTerminal()
const chunk = 'x'.repeat(16 * 1024)
let now = 0
const nowSpy = vi.spyOn(Date, 'now').mockImplementation(() => now)
terminal.write.mockImplementation((_data: string, callback?: () => void) => {
now += 9
callback?.()
})
try {
for (let i = 0; i < 64; i++) {
writeTerminalOutput(terminal, chunk, { foreground: false })
}
vi.advanceTimersByTime(0)
expect(terminal.write).toHaveBeenCalledTimes(1)
expect(nowSpy).toHaveBeenCalled()
vi.advanceTimersByTime(4)
expect(terminal.write).toHaveBeenCalledTimes(2)
} finally {
nowSpy.mockRestore()
}
})
it('caps hidden backlog memory and writes a warning instead of retaining all output', async () => {
@@ -74,10 +74,11 @@ type QueueEntry = {
const BACKGROUND_FLUSH_DELAY_MS = 50
const BACKGROUND_DRAIN_INTERVAL_MS = 16
const HIGH_PRIORITY_DRAIN_INTERVAL_MS = 1
const HIGH_PRIORITY_DRAIN_INTERVAL_MS = 4
const BACKGROUND_CHUNK_CHARS = 16 * 1024
const MAX_WRITES_PER_DRAIN = 2
const HIGH_PRIORITY_MAX_WRITES_PER_DRAIN = 16
const HIGH_PRIORITY_MAX_WRITES_PER_DRAIN = 2
const DRAIN_TIME_BUDGET_MS = 8
const LARGE_BACKLOG_CHARS = 512 * 1024
const SYNC_FOREGROUND_FLUSH_CHARS = 256 * 1024
// Why mutable: the cap scales with the user's scrollback setting (see
@@ -775,10 +776,18 @@ function writeQueuedChunk(entry: QueueEntry): 'foreground' | 'background' | null
return queuedWrite.foreground ? 'foreground' : 'background'
}
function getDrainNow(): number {
if (typeof performance !== 'undefined') {
return performance.now()
}
return Date.now()
}
function drainQueuedOutput(): void {
drainTimer = null
drainTimerDelayMs = null
let writes = 0
const startedAt = getDrainNow()
const maxWrites = hasHighPriorityBacklog()
? HIGH_PRIORITY_MAX_WRITES_PER_DRAIN
: MAX_WRITES_PER_DRAIN
@@ -807,6 +816,11 @@ function drainQueuedOutput(): void {
clearForegroundCoalesce(entry)
clearForegroundHoldSafety(entry)
}
// Why: xterm parsing and DOM work share the renderer thread with input.
// Keep backlog draining cooperative so WSL/agent output cannot pin the UI.
if (writes > 0 && getDrainNow() - startedAt >= DRAIN_TIME_BUDGET_MS) {
break
}
}
if (debugEnabled && writes > 0) {