Skip to content

The agent that grows with you

fix(desktop): repaint through the DOM renderer after WebGL context loss

Brooklyn Nicholsonebfae3eb73ad
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminal-font.ts b/apps/desktop/src/app/right-sidebar/terminal/terminal-font.ts
index d5a7129901e..dc4777bad06 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminal-font.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminal-font.ts
@@ -128,7 +128,12 @@ export async function applyTerminalFontFamily({
term.options.fontFamily = fontFamily
fit()
- clearTextureAtlas()
+
+ try {
+ clearTextureAtlas()
+ } catch {
+ // WebGL context lost or uninitialized — the DOM fallback has no atlas.
+ }
if (term.rows > 0) {
term.refresh(0, term.rows - 1)
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts b/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
index 41da0f29f77..801d78baca0 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
@@ -304,5 +304,67 @@ describe('shared WebGL atlas refresh fan-out', () => {
expect(refreshIndices).toHaveLength(2)
expect(Math.max(...clearIndices)).toBeLessThan(Math.min(...refreshIndices))
})
+
+ it('keeps refreshing siblings when one terminal atlas clear throws', async () => {
+ const { redrawAllTerminals, registerWebglRefresh } = await loadTerminalStore()
+
+ const broken = { refresh: vi.fn(), rows: 24 }
+ const healthy = { refresh: vi.fn(), rows: 24 }
+ const getWebgl = () => ({ clearTextureAtlas: vi.fn() }) as never
+ const getDeadWebgl = () =>
+ ({ clearTextureAtlas: () => { throw new Error('webgl context lost') } }) as never
+
+ registerWebglRefresh(broken as never, getDeadWebgl)
+ registerWebglRefresh(healthy as never, getWebgl)
+
+ // A post-context-loss clear must not abort the fan-out: the sibling still
+ // needs its rebuild, and the thrower still needs its own repaint.
+ redrawAllTerminals()
+ await new Promise<void>(resolve => {
+ requestAnimationFrame(() => resolve())
+ })
+
+ expect(broken.refresh).toHaveBeenCalledTimes(1)
+ expect(healthy.refresh).toHaveBeenCalledTimes(1)
+ })
+
+ it('rebuilds every registered terminal after a system resume', async () => {
+ let powerResumed: (() => void) | undefined
+
+ // The store subscribes to the main process's powerMonitor broadcast at
+ // import time (same pattern as store/power.ts), so the mock must exist
+ // before the module loads.
+ window.hermesDesktop = {
+ onPowerResume: vi.fn((callback: () => void) => {
+ powerResumed = callback
+
+ return () => {}
+ })
+ } as never
+
+ try {
+ await loadTerminalStore()
+
+ const termA = { refresh: vi.fn(), rows: 24 }
+ const termB = { refresh: vi.fn(), rows: 24 }
+ const getWebgl = () => ({ clearTextureAtlas: vi.fn() }) as never
+ const { registerWebglRefresh } = await import('./terminals')
+
+ registerWebglRefresh(termA as never, getWebgl)
+ registerWebglRefresh(termB as never, getWebgl)
+
+ // macOS can evict the glyph textures on wake WITHOUT firing
+ // webglcontextlost; the resume broadcast must trigger a full rebuild.
+ powerResumed!()
+ await new Promise<void>(resolve => {
+ requestAnimationFrame(() => resolve())
+ })
+
+ expect(termA.refresh).toHaveBeenCalledTimes(1)
+ expect(termB.refresh).toHaveBeenCalledTimes(1)
+ } finally {
+ Reflect.deleteProperty(window, 'hermesDesktop')
+ }
+ })
})
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminals.ts b/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
index 52ea62c448e..a3e1d672d0a 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
@@ -33,7 +33,11 @@ export function registerWebglRefresh(
getWebgl: () => WebglAddon | null
): () => void {
webglClearFns.set(term, () => {
- getWebgl()?.clearTextureAtlas()
+ try {
+ getWebgl()?.clearTextureAtlas()
+ } catch {
+ // WebGL context lost or uninitialized — the DOM fallback has no atlas.
+ }
})
webglRefreshFns.set(term, () => {
term.refresh(0, term.rows - 1)
@@ -91,6 +95,18 @@ export function redrawAllTerminals(skipTerm?: Terminal): void {
})
}
+// An OS resume can evict the GPU's glyph textures without ever firing
+// 'webglcontextlost' (common on macOS after sleep/wake): every terminal then
+// paints from an empty atlas. The main process broadcasts powerMonitor
+// 'resume'/'unlock-screen' as 'hermes:power-resume'; rebuild every registered
+// terminal's atlas + render model when it arrives. Import-for-side-effect,
+// same pattern as store/power.ts.
+if (typeof window !== 'undefined') {
+ window.hermesDesktop?.onPowerResume?.(() => {
+ redrawAllTerminals()
+ })
+}
+
/** One in-app terminal tab. `id` is the renderer-side handle (distinct from the
* PTY session id the main process mints); each instance owns its own shell. */
export interface TerminalEntry {
diff --git a/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.test.tsx b/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.test.tsx
index 7c432d5dc50..46de8eaab75 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.test.tsx
+++ b/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.test.tsx
@@ -22,6 +22,8 @@ const terminalRegistrations = vi.hoisted(() => ({
registerWriter: vi.fn(() => vi.fn())
}))
+const webglContextLossHandlers = vi.hoisted(() => [] as Array<() => void>)
+
vi.mock('@xterm/xterm', () => ({
Terminal: class {
readonly buffer = { active: {} }
@@ -64,7 +66,9 @@ vi.mock('@xterm/addon-webgl', () => ({
WebglAddon: class {
clearTextureAtlas = vi.fn()
dispose = vi.fn()
- onContextLoss = vi.fn()
+ onContextLoss = vi.fn((handler: () => void) => {
+ webglContextLossHandlers.push(handler)
+ })
}
}))
@@ -153,4 +157,26 @@ describe('useAgentTerminal', () => {
expect(terminalRegistrations.registerWriter).not.toHaveBeenCalled()
expect(terminalRegistrations.registerReader).not.toHaveBeenCalled()
})
+
+ it('repaints buffered rows through the DOM renderer after a WebGL context loss', async () => {
+ webglContextLossHandlers.length = 0
+ render(<Harness />)
+
+ await act(async () => {
+ resolveFontLoad([])
+ await Promise.resolve()
+ })
+
+ expect(xterm.open).toHaveBeenCalled()
+ expect(webglContextLossHandlers.length).toBeGreaterThan(0)
+
+ xterm.refresh.mockClear()
+
+ // The addon fires context loss; the hook must dispose the dead renderer,
+ // drop the ref, and force a repaint so the viewport doesn't stay black
+ // while the buffer is intact.
+ const handler = webglContextLossHandlers[webglContextLossHandlers.length - 1]
+ expect(() => handler()).not.toThrow()
+ expect(xterm.refresh).toHaveBeenCalledWith(0, 23)
+ })
})
diff --git a/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.ts b/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.ts
index 56bf5cfc4e1..66994a7065c 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/use-agent-terminal.ts
@@ -139,6 +139,14 @@ export function useAgentTerminal({ active, id, procId }: { active: boolean; id:
webgl.onContextLoss(() => {
webgl.dispose()
webglRef.current = null
+ // Same as the user terminal: repaint the buffered rows with the DOM
+ // renderer so the viewport doesn't stay black after a context loss.
+ try {
+ fitRef.current?.()
+ term.refresh(0, term.rows - 1)
+ } catch {
+ // Best-effort repaint; the next resize repaints anyway.
+ }
})
term.loadAddon(webgl)
webglRef.current = webgl
diff --git a/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts b/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts
index 1263ebce42b..c695f024a2e 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/use-terminal-session.ts
@@ -963,6 +963,15 @@ export function useTerminalSession({
webgl.onContextLoss(() => {
webgl.dispose()
webglRef.current = null
+ // The DOM renderer takes over, but the lost WebGL frame can leave
+ // the viewport black while the buffer stays intact: force a fit +
+ // full-row repaint so the buffered output shows again (#98273).
+ try {
+ fitAndResize()
+ term.refresh(0, term.rows - 1)
+ } catch {
+ // Best-effort repaint; the next resize repaints anyway.
+ }
})
term.loadAddon(webgl)
webglRef.current = webgl