Skip to content

The agent that grows with you

fix(desktop): two-phase atlas fan-out — clear all before refreshing any

Soheil Fakour1dc3edf62758
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 6dae1351536..41da0f29f77 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
@@ -269,4 +269,40 @@ describe('shared WebGL atlas refresh fan-out', () => {
expect(caller.refresh).not.toHaveBeenCalled()
expect(sibling.refresh).toHaveBeenCalledTimes(1)
})
+
+ it('clears all atlases before refreshing any terminal (two-phase ordering)', async () => {
+ const { redrawAllTerminals, registerWebglRefresh } = await loadTerminalStore()
+
+ // Track operation order across terminals sharing one atlas.
+ const ops: string[] = []
+ const sharedAtlas = { clearTextureAtlas: () => ops.push('clear') }
+ const getWebgl = () => sharedAtlas as never
+
+ const termA = {
+ refresh: () => ops.push('refresh-A'),
+ rows: 24
+ }
+ const termB = {
+ refresh: () => ops.push('refresh-B'),
+ rows: 24
+ }
+
+ registerWebglRefresh(termA as never, getWebgl)
+ registerWebglRefresh(termB as never, getWebgl)
+
+ redrawAllTerminals()
+ await new Promise<void>(resolve => {
+ requestAnimationFrame(() => resolve())
+ })
+
+ // Both clears must precede both refreshes. If a clear ran after a refresh,
+ // that terminal's rebuilt model would reference freed atlas rows.
+ const clearIndices = ops.map((op, i) => op === 'clear' ? i : -1).filter(i => i >= 0)
+ const refreshIndices = ops.map((op, i) => op.startsWith('refresh-') ? i : -1).filter(i => i >= 0)
+
+ expect(clearIndices).toHaveLength(2)
+ expect(refreshIndices).toHaveLength(2)
+ expect(Math.max(...clearIndices)).toBeLessThan(Math.min(...refreshIndices))
+ })
})
+
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminals.ts b/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
index 3fbde4ddcaa..52ea62c448e 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
@@ -19,6 +19,11 @@ import { seedAgentTerminalCommand } from './agent-terminal-stream'
// we stay pinned on 0.19.0, so every terminal that mutates the shared atlas
// must refresh its siblings itself. Registration is the inverse: each xterm
// registers its own redraw+atlas-rebuild and unregisters on dispose.
+// Two-phase fan-out: all clears FIRST, then all refreshes. A sequential
+// clear+refresh per terminal would let a later clear wipe the atlas that an
+// earlier terminal just rebuilt, leaving it with dangling glyph references.
+// Splitting the phases guarantees every terminal rebuilds against a clean atlas.
+const webglClearFns = new Map<Terminal, () => void>()
const webglRefreshFns = new Map<Terminal, () => void>()
/** Register a terminal's atlas-rebuild+redraw so sibling refreshes reach it.
@@ -27,12 +32,15 @@ export function registerWebglRefresh(
term: Terminal,
getWebgl: () => WebglAddon | null
): () => void {
- webglRefreshFns.set(term, () => {
+ webglClearFns.set(term, () => {
getWebgl()?.clearTextureAtlas()
+ })
+ webglRefreshFns.set(term, () => {
term.refresh(0, term.rows - 1)
})
return () => {
+ webglClearFns.delete(term)
webglRefreshFns.delete(term)
}
}
@@ -46,6 +54,10 @@ let refreshFrame = 0
* per-terminal effects, and fanning out on each would clear+rebuild the atlas
* N times over.
*
+ * Two-phase execution: all atlas clears run first, then all terminal refreshes.
+ * This prevents a later clear from invalidating an earlier terminal's rebuilt
+ * glyph model (GottZ triage, PR #76463).
+ *
* Skip the caller only when it already clears+redraws itself inline (font
* change); theme/activation callers must NOT skip — they need their own atlas
* rebuilt too (the theme colors changed / the frame was stale while hidden).
@@ -59,6 +71,16 @@ export function redrawAllTerminals(skipTerm?: Terminal): void {
refreshFrame = requestAnimationFrame(() => {
refreshFrame = 0
+ // Phase 1: clear all atlases first.
+ for (const [term, clear] of webglClearFns) {
+ if (skipTerm && term === skipTerm) {
+ continue
+ }
+
+ clear()
+ }
+
+ // Phase 2: rebuild render models against the now-clean atlas.
for (const [term, refresh] of webglRefreshFns) {
if (skipTerm && term === skipTerm) {
continue