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): queue the pressure lever's releases through the drain

enes354ec38fc43e
diff --git a/src/renderer/terminal/webgl-budget.test.ts b/src/renderer/terminal/webgl-budget.test.ts
index 87e91f722..1c0cbafed 100644
--- a/src/renderer/terminal/webgl-budget.test.ts
+++ b/src/renderer/terminal/webgl-budget.test.ts
@@ -4,6 +4,7 @@ import {
__resetWebglBudgetForTests,
getWebglBudget,
loseWebglContexts,
+ releaseAllHiddenGrants,
setWebglBudget,
setWebglEnabled,
setWebglGesture,
@@ -455,6 +456,85 @@ describe('webgl-budget coordinator', () => {
expect(clients.every((c) => !c.rec.held)).toBe(true)
})
})
+
+ describe('releaseAllHiddenGrants (memory-pressure lever)', () => {
+ it('gives back a hidden holder without waiting out the release delay', () => {
+ const a = fakeClient('a')
+ grant(a)
+ a.handle.setVisible(false)
+ // Well inside the warm window — the lever is what ends it, not the timer.
+ expect(a.rec.held).toBe(true)
+ releaseAllHiddenGrants()
+ vi.advanceTimersByTime(WEBGL_DRAIN_MS * 3)
+ expect(a.rec.held).toBe(false)
+ expect(a.rec.releases).toBe(1)
+ })
+
+ it('never touches a VISIBLE holder', () => {
+ const visible = fakeClient('visible')
+ const hidden = fakeClient('hidden')
+ grant(visible)
+ grant(hidden)
+ hidden.handle.setVisible(false)
+ releaseAllHiddenGrants()
+ vi.advanceTimersByTime(WEBGL_DRAIN_MS * 5)
+ expect(hidden.rec.held).toBe(false)
+ expect(visible.rec.held).toBe(true)
+ expect(visible.rec.releases).toBe(0)
+ })
+
+ it('is idempotent — a second call releases nothing extra', () => {
+ const a = fakeClient('a')
+ grant(a)
+ a.handle.setVisible(false)
+ releaseAllHiddenGrants()
+ releaseAllHiddenGrants()
+ vi.advanceTimersByTime(WEBGL_DRAIN_MS * 5)
+ expect(a.rec.releases).toBe(1)
+ releaseAllHiddenGrants() // nothing granted left to give back
+ vi.advanceTimersByTime(WEBGL_DRAIN_MS * 5)
+ expect(a.rec.releases).toBe(1)
+ })
+
+ it('queues the releases through the drain instead of swapping in one frame', () => {
+ const clients = ['a', 'b', 'c', 'd', 'e'].map((id) => fakeClient(id))
+ clients.forEach(grant)
+ clients.forEach((c) => c.handle.setVisible(false))
+ releaseAllHiddenGrants()
+ // A synchronous reclaim loop would have released all five here — that is the swap storm.
+ expect(clients.filter((c) => c.rec.held).length).toBe(clients.length - WEBGL_SWAPS_PER_DRAIN)
+ vi.advanceTimersByTime(WEBGL_DRAIN_MS * 5)
+ expect(clients.every((c) => !c.rec.held)).toBe(true)
+ })
+
+ it('releases NOTHING mid-gesture, then trickles once the canvas is at rest', () => {
+ const clients = ['a', 'b', 'c', 'd'].map((id) => fakeClient(id))
+ clients.forEach(grant)
+ setWebglGesture(true)
+ clients.forEach((c) => c.handle.setVisible(false))
+ releaseAllHiddenGrants()
+ vi.advanceTimersByTime(WEBGL_DRAIN_MS * 10)
+ // The load-bearing rule: no renderer swap while the user pans/zooms, pressure or not.
+ expect(clients.every((c) => c.rec.held)).toBe(true)
+ setWebglGesture(false)
+ expect(clients.filter((c) => c.rec.held).length).toBe(clients.length - WEBGL_SWAPS_PER_DRAIN)
+ vi.advanceTimersByTime(WEBGL_DRAIN_MS * 5)
+ expect(clients.every((c) => !c.rec.held)).toBe(true)
+ })
+
+ it('forgives a queued release for a client that became visible again', () => {
+ const a = fakeClient('a')
+ grant(a)
+ a.handle.setVisible(false)
+ setWebglGesture(true)
+ releaseAllHiddenGrants()
+ a.handle.setVisible(true) // pans back before the drain ran
+ setWebglGesture(false)
+ vi.advanceTimersByTime(WEBGL_DRAIN_MS * 5)
+ expect(a.rec.held).toBe(true)
+ expect(a.rec.releases).toBe(0)
+ })
+ })
})
describe('loseWebglContexts', () => {
diff --git a/src/renderer/terminal/webgl-budget.ts b/src/renderer/terminal/webgl-budget.ts
index b1d7c9d7e..c93b8cacb 100644
--- a/src/renderer/terminal/webgl-budget.ts
+++ b/src/renderer/terminal/webgl-budget.ts
@@ -345,17 +345,30 @@ function reclaim(c: Client): void {
}
/**
- * Memory-pressure lever: reclaim EVERY hidden holder's context now, bypassing the release delay.
- * Visible holders are untouched — the same invariant `lruHiddenHolder` applies, and for the same
- * reason: taking a context off a terminal the user is looking at trades memory for a visible
- * downgrade. Idempotent (a client that holds nothing is skipped), and each reclaimed client
- * re-grants through the normal budget-gated path on its next visibility transition.
+ * Memory-pressure lever: give back EVERY hidden holder's context, skipping the release delay's
+ * warm-for-a-pan-back window. Visible holders are untouched — the same invariant `lruHiddenHolder`
+ * applies, and for the same reason: taking a context off a terminal the user is looking at trades
+ * memory for a visible downgrade.
+ *
+ * The releases are QUEUED (`owed` + `drain`), not executed here — pressure wants the memory back
+ * SOON, not this frame. Releasing ~24 contexts in one synchronous loop is a swap storm: exactly
+ * what the zoom threshold's one-frame mass release turned out to be, and mid-gesture it lands in
+ * the GPU-pressure window where a swap dies midway and strands a terminal black. The drain path
+ * is what keeps "soon" honest: it no-ops while a gesture runs, re-checks `!c.visible` before each
+ * release (a client that came back keeps its warm context), and trickles at
+ * `WEBGL_SWAPS_PER_DRAIN` per `WEBGL_DRAIN_MS`.
+ *
+ * Idempotent: a client already queued with `releaseOwed` is simply re-added to a Set — the flag is
+ * never cleared here, so a second call cannot cancel a pending release.
*/
export function releaseAllHiddenGrants(): void {
for (const c of clients.values()) {
if (!c.granted || c.visible) continue
- reclaim(c)
+ cancelRelease(c) // the delayed release is superseded by the queued one
+ c.releaseOwed = true
+ owed.add(c)
}
+ drain()
}
/** The least-recently-visible HIDDEN holder, or null if every holder is currently visible. */