Keep terminals responsive under agent output load (#7139)
Co-authored-by: Neil <neil@stably.ai>
This commit is contained in:
@@ -898,10 +898,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 () => {
|
||||
|
||||
@@ -68,10 +68,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
|
||||
const MAX_BACKGROUND_QUEUE_CHARS = 2 * 1024 * 1024
|
||||
@@ -728,10 +729,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
|
||||
@@ -760,6 +769,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) {
|
||||
|
||||
Reference in New Issue
Block a user