Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(webgl): zoom-gated context suspension + lower macOS budget

enes912fe09d1ab1
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 888a55693..ca52174b2 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -30,6 +30,7 @@ import {
import { solveFitPadding } from './fit-view'
import { SshReconnector } from '../lib/sshReconnect'
import { terminalKey } from '../terminal/terminal-config'
+import { setWebglZoom } from '../terminal/webgl-budget'
import { StickyNode } from '../nodes/StickyNode'
import { GroupNode, setWorktreeActionHandler } from '../nodes/GroupNode'
import { LazyEditorNode, LazyDiffNode } from '../nodes/lazyMonacoNodes'
@@ -1534,6 +1535,9 @@ export function Canvas() {
setViewport(project.viewport)
setZoomPct(Math.round(project.viewport.zoom * 100))
setGroupLabelBoost(project.viewport.zoom)
+ // A project can load already zoomed way out (saved viewport) — seed the WebGL zoom gate
+ // before the mount-time IntersectionObserver reports make every node request a context.
+ setWebglZoom(project.viewport.zoom)
}
// Let load-induced changes settle before we start tracking edits as dirty.
const t = setTimeout(() => {
@@ -4958,6 +4962,9 @@ export function Canvas() {
zoomRafRef.current = null
setZoomPct(Math.round(viewportRef.current.zoom * 100))
setGroupLabelBoost(viewportRef.current.zoom)
+ // Feed the WebGL budget's zoom gate (suspend GPU rendering when zoomed way out).
+ // Idempotent + hysteresis inside; per-frame call cost is a float compare.
+ setWebglZoom(viewportRef.current.zoom)
})
}
},
diff --git a/src/renderer/main.tsx b/src/renderer/main.tsx
index b6f7ecdb4..381cc6722 100644
--- a/src/renderer/main.tsx
+++ b/src/renderer/main.tsx
@@ -15,12 +15,17 @@ async function bootstrap(): Promise<void> {
} else {
// Electron desktop: main raised Chromium's WebGL context cap (--max-active-webgl-contexts),
// so the terminal GPU-renderer budget can rise to match. A browser tab (Server Edition)
- // cannot raise its cap and stays on the default budget. See src/shared/webgl.ts.
- const [{ setWebglBudget }, { WEBGL_BUDGET_DESKTOP }] = await Promise.all([
- import('./terminal/webgl-budget'),
- import('../shared/webgl')
- ])
- setWebglBudget(WEBGL_BUDGET_DESKTOP)
+ // cannot raise its cap and stays on the default budget. On MAC desktop the budget goes the
+ // other way — DOWN: the macOS compositor mishandles many live WebGL canvases (black
+ // terminals after a zoom-out grant burst, whole-window flicker) with no JS-visible error,
+ // so the binding limit there is the compositor, not Chromium's cap. See src/shared/webgl.ts.
+ const [{ setWebglBudget }, { WEBGL_BUDGET_DESKTOP, WEBGL_BUDGET_DESKTOP_MAC }, { isMacPlatform }] =
+ await Promise.all([
+ import('./terminal/webgl-budget'),
+ import('../shared/webgl'),
+ import('../shared/platform-utils')
+ ])
+ setWebglBudget(isMacPlatform() ? WEBGL_BUDGET_DESKTOP_MAC : WEBGL_BUDGET_DESKTOP)
}
await import('./boot')
}
diff --git a/src/renderer/terminal/webgl-budget.test.ts b/src/renderer/terminal/webgl-budget.test.ts
index 8a0d74738..52d6b6e18 100644
--- a/src/renderer/terminal/webgl-budget.test.ts
+++ b/src/renderer/terminal/webgl-budget.test.ts
@@ -6,7 +6,10 @@ import {
loseWebglContexts,
setWebglBudget,
setWebglEnabled,
+ setWebglZoom,
WEBGL_ACQUIRE_DEBOUNCE_MS,
+ WEBGL_RESUME_AT_ZOOM,
+ WEBGL_SUSPEND_BELOW_ZOOM,
WEBGL_BUDGET,
WEBGL_LOSS_STREAK_MAX,
WEBGL_REACQUIRE_AFTER_LOSS_MS,
@@ -310,6 +313,72 @@ describe('webgl-budget coordinator', () => {
__resetWebglBudgetForTests()
expect(getWebglBudget()).toBe(WEBGL_BUDGET)
})
+
+ describe('zoom gate', () => {
+ it('crossing below the suspend threshold reclaims every held context immediately', () => {
+ const a = fakeClient('a')
+ const b = fakeClient('b')
+ grant(a)
+ grant(b)
+ expect(a.rec.held).toBe(true)
+ expect(b.rec.held).toBe(true)
+ setWebglZoom(WEBGL_SUSPEND_BELOW_ZOOM - 0.01)
+ expect(a.rec.held).toBe(false)
+ expect(b.rec.held).toBe(false)
+ })
+
+ it('blocks grants while suspended, re-grants visible clients on resume', () => {
+ setWebglZoom(WEBGL_SUSPEND_BELOW_ZOOM - 0.01)
+ const a = fakeClient('a')
+ grant(a)
+ expect(a.rec.held).toBe(false)
+ // Below the resume threshold nothing changes (hysteresis).
+ setWebglZoom(WEBGL_RESUME_AT_ZOOM - 0.01)
+ vi.advanceTimersByTime(WEBGL_ACQUIRE_DEBOUNCE_MS * 2)
+ expect(a.rec.held).toBe(false)
+ setWebglZoom(WEBGL_RESUME_AT_ZOOM)
+ expect(a.rec.held).toBe(true)
+ })
+
+ it('hysteresis: hovering between the two thresholds never churns contexts', () => {
+ const a = fakeClient('a')
+ grant(a)
+ const before = a.rec.releases
+ // Not suspended: dipping to just above the SUSPEND threshold does nothing…
+ setWebglZoom(WEBGL_SUSPEND_BELOW_ZOOM + 0.01)
+ setWebglZoom(WEBGL_RESUME_AT_ZOOM - 0.01)
+ setWebglZoom(WEBGL_SUSPEND_BELOW_ZOOM + 0.01)
+ expect(a.rec.releases).toBe(before)
+ expect(a.rec.held).toBe(true)
+ // …and once suspended, wobbling below the RESUME threshold does not re-grant.
+ setWebglZoom(WEBGL_SUSPEND_BELOW_ZOOM - 0.01)
+ expect(a.rec.held).toBe(false)
+ const acquires = a.rec.acquires
+ setWebglZoom(WEBGL_RESUME_AT_ZOOM - 0.01)
+ setWebglZoom(WEBGL_SUSPEND_BELOW_ZOOM - 0.01)
+ vi.advanceTimersByTime(WEBGL_ACQUIRE_DEBOUNCE_MS * 2)
+ expect(a.rec.acquires).toBe(acquires)
+ })
+
+ it('resume respects the master switch: disabled stays DOM-only', () => {
+ const a = fakeClient('a')
+ grant(a)
+ setWebglEnabled(false)
+ expect(a.rec.held).toBe(false)
+ setWebglZoom(WEBGL_SUSPEND_BELOW_ZOOM - 0.01)
+ setWebglZoom(WEBGL_RESUME_AT_ZOOM)
+ vi.advanceTimersByTime(WEBGL_ACQUIRE_DEBOUNCE_MS * 2)
+ expect(a.rec.held).toBe(false)
+ })
+
+ it('ignores non-finite zoom values', () => {
+ const a = fakeClient('a')
+ grant(a)
+ setWebglZoom(NaN)
+ setWebglZoom(Infinity)
+ expect(a.rec.held).toBe(true)
+ })
+ })
})
describe('loseWebglContexts', () => {
diff --git a/src/renderer/terminal/webgl-budget.ts b/src/renderer/terminal/webgl-budget.ts
index 98cf9dd37..9cea5e14c 100644
--- a/src/renderer/terminal/webgl-budget.ts
+++ b/src/renderer/terminal/webgl-budget.ts
@@ -98,6 +98,44 @@ export function getWebglBudget(): number {
return budget
}
+/**
+ * Below this canvas zoom the coordinator holds NO contexts at all. At such scale terminal text
+ * is unreadable anyway, and a zoom-out is exactly the moment a BURST of newly-visible clients
+ * would be granted fresh contexts — which is the observed macOS-compositor failure (terminals
+ * compositing black with zero JS-visible errors; see WEBGL_BUDGET_DESKTOP_MAC). The DOM renderer
+ * carries the thumbnails; zooming back in re-grants within the budget as usual.
+ */
+export const WEBGL_SUSPEND_BELOW_ZOOM = 0.4
+/** Resume threshold, ABOVE the suspend threshold on purpose (hysteresis): a pinch hovering at
+ * one boundary must not thrash dispose/create cycles — context churn is itself the hazard. */
+export const WEBGL_RESUME_AT_ZOOM = 0.45
+
+/** True while the canvas is zoomed below the suspend threshold — grants are blocked. */
+let zoomSuspended = false
+
+/**
+ * Report the canvas zoom (React Flow viewport scale). Cheap and idempotent — call it from the
+ * zoom/pan handler; state only changes when a hysteresis boundary is crossed. Crossing DOWN
+ * reclaims every held context immediately (each client falls back to the DOM renderer, which
+ * repaints via its release heal); crossing UP re-attempts a budget-gated grant for every
+ * visible client, exactly like the master toggle's ON path.
+ */
+export function setWebglZoom(zoom: number): void {
+ if (!Number.isFinite(zoom)) return
+ const next = zoomSuspended ? zoom < WEBGL_RESUME_AT_ZOOM : zoom < WEBGL_SUSPEND_BELOW_ZOOM
+ if (next === zoomSuspended) return
+ zoomSuspended = next
+ if (zoomSuspended) {
+ for (const c of clients.values()) {
+ cancelAcquire(c)
+ reclaim(c)
+ }
+ return
+ }
+ if (!enabled) return
+ for (const c of clients.values()) if (c.visible) tryGrant(c)
+}
+
/**
* Explicitly lose the WebGL context of every canvas under `root` (a terminal's element), via
* `WEBGL_lose_context`. Chromium counts a context against its per-page cap until it is GC'd OR
@@ -254,6 +292,8 @@ function tryGrant(c: Client): void {
if (!clients.has(c.id) || !c.visible || c.granted) return
// Master switch off → never grant; every terminal stays on the DOM renderer.
if (!enabled) return
+ // Zoomed below the suspend threshold → never grant (see setWebglZoom).
+ if (zoomSuspended) return
if (grantCount() < budget) {
doGrant(c)
return
@@ -393,4 +433,5 @@ export function __resetWebglBudgetForTests(): void {
visibilityClock = 0
budget = WEBGL_BUDGET
enabled = true
+ zoomSuspended = false
}
diff --git a/src/shared/webgl.ts b/src/shared/webgl.ts
index bd6fef142..bff004b1f 100644
--- a/src/shared/webgl.ts
+++ b/src/shared/webgl.ts
@@ -20,3 +20,16 @@ export const WEBGL_CONTEXT_CAP_DESKTOP = 32
/** Renderer budget on desktop — comfortably under `WEBGL_CONTEXT_CAP_DESKTOP`, same margin
* philosophy as the default 12-under-16. */
export const WEBGL_BUDGET_DESKTOP = 24
+
+/**
+ * Renderer budget on MAC desktop, deliberately much lower. Two field reports on macOS point at
+ * the OS compositor mishandling many simultaneous WebGL canvases: whole-window flicker (the
+ * reason the GPU-rendering master toggle exists), and terminals compositing BLACK after a
+ * zoom-out grants a burst of contexts — with zero JS-visible errors in either case (no context
+ * loss event, so nothing our repaint heals can reach: `term.refresh` re-draws, the compositor
+ * still doesn't present it). Staying under the browser CAP is not enough there — the pressure
+ * the macOS compositor tolerates is lower than what Chromium allows. ~10 keeps GPU rendering
+ * for the terminals the user is actually looking at while staying inside what macOS
+ * compositing handles reliably.
+ */
+export const WEBGL_BUDGET_DESKTOP_MAC = 10